doctags.c (2062B)
1 /* vim:set ts=4 sw=4: 2 * 3 * This program makes a tags file for help text. 4 * 5 * Usage: doctags *.txt ... >tags 6 * 7 * A tag in this context is an identifier between stars, e.g. *c_files* 8 */ 9 10 #include <stdio.h> 11 #include <string.h> 12 #include <stdlib.h> 13 14 #define LINELEN 200 15 16 int 17 main(int argc, char **argv) 18 { 19 char line[LINELEN]; 20 char *p1, *p2; 21 char *p; 22 FILE *fd; 23 int len; 24 int in_example; 25 26 if (argc <= 1) 27 { 28 fprintf(stderr, "Usage: doctags docfile ... >tags\n"); 29 exit(1); 30 } 31 printf("help-tags\ttags\t1\n"); 32 while (--argc > 0) 33 { 34 ++argv; 35 fd = fopen(argv[0], "r"); 36 if (fd == NULL) 37 { 38 fprintf(stderr, "Unable to open %s for reading\n", argv[0]); 39 continue; 40 } 41 in_example = 0; 42 while (fgets(line, LINELEN, fd) != NULL) 43 { 44 if (in_example) 45 { 46 // skip over example; non-blank in first column ends example 47 if (strchr(" \t\n\r", line[0]) != NULL) 48 continue; 49 in_example = 0; 50 } 51 p1 = strchr(line, '*'); // find first '*' 52 while (p1 != NULL) 53 { 54 p2 = strchr(p1 + 1, '*'); // find second '*' 55 if (p2 != NULL && p2 > p1 + 1) // skip "*" and "**" 56 { 57 for (p = p1 + 1; p < p2; ++p) 58 if (*p == ' ' || *p == '\t' || *p == '|') 59 break; 60 // Only accept a *tag* when it consists of valid 61 // characters, there is white space before it and is 62 // followed by a white character or end-of-line. 63 if (p == p2 64 && (p1 == line || p1[-1] == ' ' || p1[-1] == '\t') 65 && (strchr(" \t\n\r", p[1]) != NULL 66 || p[1] == '\0')) 67 { 68 *p2 = '\0'; 69 ++p1; 70 printf("%s\t%s\t/*", p1, argv[0]); 71 while (*p1) 72 { 73 // insert backslash before '\\' and '/' 74 if (*p1 == '\\' || *p1 == '/') 75 putchar('\\'); 76 putchar(*p1); 77 ++p1; 78 } 79 printf("*\n"); 80 p2 = strchr(p2 + 1, '*'); // find next '*' 81 } 82 } 83 p1 = p2; 84 } 85 len = strlen(line); 86 if ((len == 2 && strcmp(&line[len - 2], ">\n") == 0) 87 || (len >= 3 && strcmp(&line[len - 3], " >\n") == 0)) 88 in_example = 1; 89 } 90 fclose(fd); 91 } 92 return 0; 93 }