From: NeilBrown Date: Sun, 5 May 2019 01:38:22 +0000 (+1000) Subject: mdcode: allow a specific section to be extracted. X-Git-Tag: JamisonCreek-3~27 X-Git-Url: https://ocean-lang.org/code/?p=ocean;a=commitdiff_plain;h=9d5ec7afa939dcea026eb7a888ab7a92f509fd8c mdcode: allow a specific section to be extracted. Previously, the md2c tool only extracted "File:" sections. Sometimes we need more control, so allow a second argument to identify a single section to be extracted. This will be written to stdout. Signed-off-by: NeilBrown --- diff --git a/csrc/mdcode.mdc b/csrc/mdcode.mdc index e3a258e..8b9a8dc 100644 --- a/csrc/mdcode.mdc +++ b/csrc/mdcode.mdc @@ -900,13 +900,19 @@ we are done. int fd; size_t len; char *file; + struct text section = {NULL, 0}; struct section *table, *s, *prev; errs = 0; - if (argc != 2) { - fprintf(stderr, "Usage: mdcode file.mdc\n"); + if (argc != 2 && argc != 3) { + fprintf(stderr, "Usage: mdcode file.mdc [section]\n"); exit(2); } + if (argc == 3) { + section.txt = argv[2]; + section.len = strlen(argv[2]); + } + fd = open(argv[1], O_RDONLY); if (fd < 0) { fprintf(stderr, "mdcode: cannot open %s: %s\n", @@ -923,16 +929,23 @@ we are done. char fname[1024]; char *spc = strnchr(s->section.txt, s->section.len, ' '); - if (spc > s->section.txt && spc[-1] == ':' && - strncmp(s->section.txt, "File: ", 6) != 0) - /* Ignore this section */ - continue; - if (strncmp(s->section.txt, "File: ", 6) != 0) { + if (spc > s->section.txt && spc[-1] == ':') { + if (strncmp(s->section.txt, "File: ", 6) != 0 && + (section.txt == NULL || + text_cmp(s->section, section) != 0)) + /* Ignore this section */ + continue; + } else { fprintf(stderr, "Code in unreferenced section that is not ignored or a file name: %.*s\n", s->section.len, s->section.txt); errs++; continue; } + if (section.txt) { + if (text_cmp(s->section, section) == 0) + code_node_print(stdout, s->code, argv[1]); + break; + } copy_fname(fname, sizeof(fname), s->section); if (fname[0] == 0) { fprintf(stderr, "Missing file name at:%.*s\n", @@ -952,4 +965,3 @@ we are done. } exit(!!errs); } -