]> ocean-lang.org Git - ocean/commitdiff
mdcode: allow a specific section to be extracted.
authorNeilBrown <neil@brown.name>
Sun, 5 May 2019 01:38:22 +0000 (11:38 +1000)
committerNeilBrown <neil@brown.name>
Sun, 5 May 2019 01:38:22 +0000 (11:38 +1000)
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 <neil@brown.name>
csrc/mdcode.mdc

index e3a258efa7b9b4687bc0d287fd9d028667fc287e..8b9a8dcc8a02f1ae45fe9d9fb104e7d0140bff2b 100644 (file)
@@ -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);
        }
-