From 27711a085eae65366c023323b15857131c6d8ad8 Mon Sep 17 00:00:00 2001 From: NeilBrown Date: Sat, 10 May 2014 20:58:02 +1000 Subject: [PATCH] mdcode.mdc: Allow more sections than just Example: to be ignored. "Example:" is no longer a special case. Any section name with starts "Word:" for some "Word", does not need to be included in other sections. "File:" is still a special case of that and will be stored in the named file. Signed-off-by: NeilBrown --- csrc/mdcode.mdc | 52 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 17 deletions(-) diff --git a/csrc/mdcode.mdc b/csrc/mdcode.mdc index 50250b8..ac34a54 100644 --- a/csrc/mdcode.mdc +++ b/csrc/mdcode.mdc @@ -55,20 +55,25 @@ times. Allowing this might make some sense, but it is probably a mistake, and prohibiting it make some of the code a bit cleaner. Equally, every section of code should be interpolated at least once - -with two exceptions. These exceptions are imposed by the tool, not -the library. A different client could impose different rules on the -names of top-level code sections. - -The first exception we have already seen. A section name starting -__Example:__ indicates code that is not to be included in the final product. - -The second exception is for the top level code sections which will be -written to files. Again these are identified by their section name. -This must start with __File:__ the following text (after optional -spaces) will be used as a file name. - -Any section containing code that does not start __Example:__ or -__File:__ must be included in some other section exactly once. +with one exception. This exception is imposed by the +tool, not the library. A different client could impose different +rules on the names of top-level code sections. + +One example of the exception we have already seen. A section name +starting __Example:__ indicates code that is not to be included in the +final product. Any leading word will do, providing there is a space, +and the first space is preceded by a colon, that section name will be +ignored. + +A special case of this exception exists for the leading word +__File__. These sections are the top level code sections and they +will be written to the named file. Thus a section named +__File: foo__ should not be referenced by another section, and its +contents after all references are expanded will be written to the file +__foo__. + +Any section containing code that does not start __Word:__ +must be included in some other section exactly once. ### Multiple files @@ -871,6 +876,15 @@ we are done. fprintf(stderr, "%s\n", msg); } + static char *strnchr(char *haystack, int len, char needle) + { + while (len > 0 && *haystack && *haystack != needle) { + haystack++; + len--; + } + return len > 0 && *haystack == needle ? haystack : NULL; + } + int main(int argc, char *argv[]) { int fd; @@ -897,10 +911,14 @@ we are done. (code_free(s->code), prev = s, s = s->next, free(prev))) { FILE *fl; char fname[1024]; - if (strncmp(s->section.txt, "Example:", 8) == 0) + 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:", 5) != 0) { - fprintf(stderr, "Code in unreferenced section that is not a file name: %.*s\n", + if (strncmp(s->section.txt, "File: ", 6) != 0) { + fprintf(stderr, "Code in unreferenced section that is not ignored or a file name: %.*s\n", s->section.len, s->section.txt); errs++; continue; -- 2.43.0