2 * Copyright notice from original mutt:
3 * Copyright (C) 1999-2000 Thomas Roessler <roessler@does-not-exist.org>
5 * Parts were written/modified by:
6 * Rocco Rutte <pdmef@cs.tu-berlin.de>
8 * This file is part of mutt-ng, see http://www.muttng.org/.
9 * It's licensed under the GNU General Public License,
10 * please see the file GPL in the top level source directory.
14 ** This program parses mutt's init.h and generates documentation in
15 ** three different formats:
17 ** -> a commented muttrc configuration file
18 ** -> nroff, suitable for inclusion in a manual page
19 ** -> linuxdoc-sgml, suitable for inclusion in the
46 extern char *sys_errlist[];
49 #define strerror(x) ((x) > 0 && (x) < sys_nerr) ? sys_errlist[(x)] : 0
50 #endif /* !HAVE_STRERROR */
56 #define STRLEN(s) (s ? strlen(s) : 0)
64 static int outcount = 0;
65 static var_t *outbuf = NULL;
67 static int var_cmp (const void *a, const void *b)
69 return (strcmp (((var_t *) a)->name, ((var_t *) b)->name));
72 enum output_formats_t {
73 F_CONF, F_MAN, F_SGML, F_NONE
80 #define D_TAB (1 << 4)
82 #define D_INIT (1 << 6)
103 enum output_formats_t OutputFormat = F_NONE;
107 static char *get_token (char *, size_t, char *);
108 static char *skip_ws (char *);
109 static const char *type2human (int);
110 static int buff2type (const char *);
111 static int flush_doc (int);
112 static int handle_docline (char *, int);
113 static int print_it (int, char *, int);
114 static void print_confline (const char *, int, const char *);
115 static void handle_confline (char *);
116 static void makedoc (FILE *, FILE *);
117 static void pretty_default (char *, size_t, const char *, int);
118 static int sgml_fputc (int);
119 static int sgml_fputs (const char *);
120 static void add_var (const char *);
121 static int add_s (const char *);
122 static int add_c (int);
124 int main (int argc, char *argv[])
129 if ((Progname = strrchr (argv[0], '/')))
134 while ((c = getopt (argc, argv, "cmsd")) != EOF) {
137 OutputFormat = F_CONF;
140 OutputFormat = F_MAN;
143 OutputFormat = F_SGML;
150 fprintf (stderr, "%s: bad command line parameter.\n", Progname);
156 if (optind != argc) {
157 if ((f = fopen (argv[optind], "r")) == NULL) {
158 fprintf (stderr, "%s: Can't open %s (%s).\n",
159 Progname, argv[optind], strerror (errno));
166 switch (OutputFormat) {
174 fprintf (stderr, "%s: No output format specified.\n", Progname);
185 static void add_var (const char *name)
187 outbuf = realloc (outbuf, (++outcount) * sizeof (var_t));
188 outbuf[outcount - 1].seen = 0;
189 outbuf[outcount - 1].name = strdup (name);
190 outbuf[outcount - 1].descr = NULL;
193 static int add_s (const char *s)
195 size_t lnew = STRLEN (s), lold = STRLEN (outbuf[outcount - 1].descr);
199 if (!outbuf[outcount - 1].seen) {
201 outbuf[outcount - 1].seen = 1;
205 outbuf[outcount - 1].descr = strdup (s);
207 outbuf[outcount - 1].descr =
208 realloc (outbuf[outcount - 1].descr, lold + lnew + 1);
209 memcpy (&(outbuf[outcount - 1].descr[lold - 1]) + 1, s, lnew);
211 outbuf[outcount - 1].descr[lold + lnew] = '\0';
215 static int add_c (int c)
217 char buf[2] = "\0\0";
220 return (add_s (buf));
223 static void makedoc (FILE * in, FILE * out)
225 char buffer[BUFFSIZE];
226 char token[BUFFSIZE];
230 int docstat = D_INIT;
232 while ((fgets (buffer, sizeof (buffer), in))) {
234 if ((p = strchr (buffer, '\n')) == NULL) {
235 fprintf (stderr, "%s: Line %d too long. Ask a wizard to enlarge\n"
236 "%s: my buffer size.\n", Progname, line, Progname);
242 if (!(p = get_token (token, sizeof (token), buffer)))
246 fprintf (stderr, "%s: line %d. first token: \"%s\".\n",
247 Progname, line, token);
250 if (!strcmp (token, "/*++*/"))
252 else if (!strcmp (token, "/*--*/")) {
253 docstat = flush_doc (docstat);
256 else if (active && (!strcmp (token, "/**") || !strcmp (token, "**")))
257 docstat = handle_docline (p, docstat);
258 else if (active && !strcmp (token, "{")) {
259 docstat = flush_doc (docstat);
265 qsort (outbuf, outcount, sizeof (var_t), &var_cmp);
266 for (line = 0; line < outcount; line++) {
267 if (outbuf[line].descr) {
268 fprintf (out, "%s\n", outbuf[line].descr);
269 free (outbuf[line].descr);
271 free (outbuf[line].name);
276 /* skip whitespace */
278 static char *skip_ws (char *s)
280 while (*s && isspace ((unsigned char) *s))
286 /* isolate a token */
288 static char single_char_tokens[] = "[]{},;|";
290 static char *get_token (char *d, size_t l, char *s)
297 fprintf (stderr, "%s: get_token called for `%s'.\n", Progname, s);
302 fprintf (stderr, "%s: argumet after skip_ws(): `%s'.\n", Progname, s);
306 fprintf (stderr, "%s: no more tokens on this line.\n", Progname);
310 if (strchr (single_char_tokens, *s)) {
312 fprintf (stderr, "%s: found single character token `%c'.\n",
322 fprintf (stderr, "%s: found quote character.\n", Progname);
329 for (t = s; *t && --l > 0; t++) {
330 if (*t == '\\' && !t[1])
333 if (is_quoted && *t == '\\') {
334 switch ((*d = *++t)) {
353 if (is_quoted && *t == '"') {
357 else if (!is_quoted && strchr (single_char_tokens, *t))
359 else if (!is_quoted && isspace ((unsigned char) *t))
368 fprintf (stderr, "%s: Got %stoken: `%s'.\n",
369 Progname, is_quoted ? "quoted " : "", dd);
370 fprintf (stderr, "%s: Remainder: `%s'.\n", Progname, t);
378 ** Configuration line parser
380 ** The following code parses a line from init.h which declares
381 ** a configuration variable.
385 /* note: the following enum must be in the same order as the
386 * following string definitions!
408 "DT_NONE", "-none-"}, {
409 "DT_BOOL", "boolean"}, {
410 "DT_NUM", "number"}, {
411 "DT_STR", "string"}, {
412 "DT_PATH", "path"}, {
413 "DT_QUAD", "quadoption"}, {
414 "DT_SORT", "sort order"}, {
415 "DT_RX", "regular expression"}, {
416 "DT_MAGIC", "folder magic"}, {
418 "DT_ADDR", "e-mail address"}, {
423 static int buff2type (const char *s)
427 for (type = DT_NONE; types[type].machine; type++)
428 if (!strcmp (types[type].machine, s))
434 static const char *type2human (int type)
436 return types[type].human;
438 static void handle_confline (char *s)
440 char varname[BUFFSIZE];
447 /* xxx - put this into an actual state machine? */
450 if (!(s = get_token (varname, sizeof (varname), s)))
454 if (!(s = get_token (buff, sizeof (buff), s)))
458 if (!(s = get_token (buff, sizeof (buff), s)))
461 type = buff2type (buff);
463 /* possibly a "|" or comma */
464 if (!(s = get_token (buff, sizeof (buff), s)))
467 if (!strcmp (buff, "|")) {
469 fprintf (stderr, "%s: Expecting <subtype> <comma>.\n", Progname);
470 /* ignore subtype and comma */
471 if (!(s = get_token (buff, sizeof (buff), s)))
473 if (!(s = get_token (buff, sizeof (buff), s)))
480 if (!(s = get_token (buff, sizeof (buff), s)))
482 if (!strcmp (buff, ","))
486 /* option name or UL &address */
487 if (!(s = get_token (buff, sizeof (buff), s)))
489 if (!strcmp (buff, "UL"))
490 if (!(s = get_token (buff, sizeof (buff), s)))
494 if (!(s = get_token (buff, sizeof (buff), s)))
498 fprintf (stderr, "%s: Expecting default value.\n", Progname);
500 /* <default value> or UL <default value> */
501 if (!(s = get_token (buff, sizeof (buff), s)))
503 if (!strcmp (buff, "UL")) {
505 fprintf (stderr, "%s: Skipping UL.\n", Progname);
506 if (!(s = get_token (buff, sizeof (buff), s)))
510 memset (tmp, 0, sizeof (tmp));
513 if (!strcmp (buff, "}"))
516 strncpy (tmp + STRLEN (tmp), buff, sizeof (tmp) - STRLEN (tmp));
518 while ((s = get_token (buff, sizeof (buff), s)));
520 pretty_default (val, sizeof (val), tmp, type);
522 print_confline (varname, type, val);
525 static void pretty_default (char *t, size_t l, const char *s, int type)
533 if (!strcasecmp (s, "M_YES"))
534 strncpy (t, "yes", l);
535 else if (!strcasecmp (s, "M_NO"))
536 strncpy (t, "no", l);
537 else if (!strcasecmp (s, "M_ASKYES"))
538 strncpy (t, "ask-yes", l);
539 else if (!strcasecmp (s, "M_ASKNO"))
540 strncpy (t, "ask-no", l);
546 strncpy (t, "yes", l);
548 strncpy (t, "no", l);
554 strncpy (t, s + 5, l);
556 *t = tolower ((unsigned char) *t);
562 strncpy (t, s + 2, l);
564 *t = tolower ((unsigned char) *t);
572 if (!strcmp (s, "0"))
584 static void char_to_escape (char *dest, unsigned int c)
588 strcpy (dest, "\\r");
589 break; /* __STRCPY_CHECKED__ */
591 strcpy (dest, "\\n");
592 break; /* __STRCPY_CHECKED__ */
594 strcpy (dest, "\\t");
595 break; /* __STRCPY_CHECKED__ */
597 strcpy (dest, "\\f");
598 break; /* __STRCPY_CHECKED__ */
600 sprintf (dest, "\\%03o", c);
604 static void conf_char_to_escape (unsigned int c)
608 char_to_escape (buff, c);
612 static void conf_print_strval (const char *v)
615 if (*v < ' ' || *v & 0x80) {
616 conf_char_to_escape ((unsigned int) *v);
620 if (*v == '"' || *v == '\\')
626 static void man_print_strval (const char *v)
629 if (*v < ' ' || *v & 0x80) {
631 conf_char_to_escape ((unsigned int) *v);
644 static void sgml_print_strval (const char *v)
649 if (*v < ' ' || *v & 0x80) {
650 char_to_escape (buff, (unsigned int) *v);
654 sgml_fputc ((unsigned int) *v);
658 static int sgml_fputc (int c)
662 return add_s ("<");
664 return add_s (">");
666 return add_s ("$");
668 return add_s ("_");
670 return add_s ("%");
672 return add_s ("&");
674 return add_s ("\");
676 return add_s ("&dquot;");
678 return add_s ("[");
680 return add_s ("]");
682 return add_s ("˜");
688 static int sgml_fputs (const char *s)
691 if (sgml_fputc ((unsigned int) *s) == EOF)
697 static void print_confline (const char *varname, int type, const char *val)
702 switch (OutputFormat) {
703 /* configuration file */
706 if (type == DT_STR || type == DT_RX || type == DT_ADDR
707 || type == DT_PATH) {
711 conf_print_strval (val);
714 else if (type != DT_SYN) {
721 add_s ("\n#\n# Name: ");
723 add_s ("\n# Type: ");
724 add_s (type2human (type));
725 if (type == DT_STR || type == DT_RX || type == DT_ADDR
726 || type == DT_PATH) {
727 add_s ("\n# Default: \"");
728 conf_print_strval (val);
732 add_s ("\n# Default: ");
747 add_s (type2human (type));
749 if (type == DT_STR || type == DT_RX || type == DT_ADDR
750 || type == DT_PATH) {
751 add_s ("Default: \\(lq");
752 man_print_strval (val);
765 /* SGML based manual */
769 sgml_fputs (varname);
770 add_s ("<label id=\"");
773 add_s ("\n<p>\nType: <tt>");
774 add_s (type2human (type));
777 if (type == DT_STR || type == DT_RX || type == DT_ADDR
778 || type == DT_PATH) {
779 add_s ("<p>\nDefault: <tt>&dquot;");
780 sgml_print_strval (val);
781 add_s ("&dquot;</tt>");
784 add_s ("<p>\nDefault: <tt>");
797 ** Documentation line parser
799 ** The following code parses specially formatted documentation
800 ** comments in init.h.
802 ** The format is very remotely inspired by nroff. Most important, it's
803 ** easy to parse and convert, and it was easy to generate from the SGML
804 ** source of mutt's original manual.
806 ** - \fI switches to italics
807 ** - \fB switches to boldface
808 ** - \fT switches to typewriter for SGML
809 ** - \fP switches to normal display
810 ** - .dl on a line starts a definition list (name taken taken from HTML).
811 ** - .dt starts a term in a definition list.
812 ** - .dd starts a definition in a definition list.
813 ** - .de on a line finishes a definition list.
814 ** - .ts on a line starts a "tscreen" environment (name taken from SGML).
815 ** - .te on a line finishes this environment.
816 ** - .pp on a line starts a paragraph.
817 ** - \$word will be converted to a reference to word, where appropriate.
818 ** Note that \$$word is possible as well.
819 ** - '. ' in the beginning of a line expands to two space characters.
820 ** This is used to protect indentations in tables.
823 /* close eventually-open environments. */
825 static int fd_recurse = 0;
827 static int flush_doc (int docstat)
829 if (docstat & D_INIT)
833 fprintf (stderr, "%s: Internal error, recursion in flush_doc()!\n",
838 if (docstat & (D_TAB))
839 docstat = print_it (SP_END_TAB, NULL, docstat);
841 if (docstat & (D_DL))
842 docstat = print_it (SP_END_DL, NULL, docstat);
844 if (docstat & (D_EM | D_BF | D_TT))
845 docstat = print_it (SP_END_FT, NULL, docstat);
847 docstat = print_it (SP_NEWLINE, NULL, 0);
853 /* print something. */
855 static int print_it (int special, char *str, int docstat)
857 int onl = docstat & (D_NL | D_NP);
859 docstat &= ~(D_NL | D_NP | D_INIT);
861 switch (OutputFormat) {
862 /* configuration file */
866 static int Continuation = 0;
869 docstat &= ~(D_EM | D_BF | D_TT);
947 if (docstat & D_DT) {
950 for (i = STRLEN (str); i < 8; i++)
968 docstat &= ~(D_EM | D_BF | D_TT);
975 docstat &= ~(D_EM | D_TT);
982 docstat &= ~(D_BF | D_TT);
988 docstat &= ~(D_BF | D_EM);
1017 add_s ("\n.IP\n.DS\n.sp\n.ft CR\n.nf\n");
1018 docstat |= D_TAB | D_NL;
1023 add_s ("\n.fi\n.ec\n.ft P\n.sp\n");
1053 for (; *str; str++) {
1056 else if (*str == '\\')
1058 else if (!strncmp (str, "``", 2)) {
1062 else if (!strncmp (str, "''", 2)) {
1076 /* SGML based manual */
1088 docstat &= ~(D_EM | D_BF | D_TT);
1095 docstat &= ~(D_EM | D_TT);
1102 docstat &= ~(D_BF | D_TT);
1109 docstat &= ~(D_EM | D_BF);
1138 add_s ("\n<tscreen><verb>\n");
1139 docstat |= D_TAB | D_NL;
1144 add_s ("\n</verb></tscreen>");
1151 add_s ("\n<descrip>\n");
1167 add_s ("</descrip>\n");
1173 if (docstat & D_TAB)
1182 /* make gcc happy (unreached) */
1190 void print_ref (int output_dollar, const char *ref)
1192 switch (OutputFormat) {
1201 add_s ("<ref id=\"");
1203 add_s ("\" name=\"");
1215 static int commit_buff (char *buff, char **d, int docstat)
1219 docstat = print_it (SP_STR, buff, docstat);
1226 static int handle_docline (char *l, int docstat)
1228 char buff[BUFFSIZE];
1234 fprintf (stderr, "%s: handle_docline `%s'\n", Progname, l);
1236 if (!strncmp (l, ".pp", 3))
1237 return print_it (SP_NEWPAR, NULL, docstat);
1238 else if (!strncmp (l, ".ts", 3))
1239 return print_it (SP_START_TAB, NULL, docstat);
1240 else if (!strncmp (l, ".te", 3))
1241 return print_it (SP_END_TAB, NULL, docstat);
1242 else if (!strncmp (l, ".dl", 3))
1243 return print_it (SP_START_DL, NULL, docstat);
1244 else if (!strncmp (l, ".de", 3))
1245 return print_it (SP_END_DL, NULL, docstat);
1246 else if (!strncmp (l, ". ", 2))
1249 for (s = l, d = buff; *s; s++) {
1250 if (!strncmp (s, "\\(as", 4)) {
1254 else if (!strncmp (s, "\\(rs", 4)) {
1258 else if (!strncmp (s, "\\fI", 3)) {
1259 docstat = commit_buff (buff, &d, docstat);
1260 docstat = print_it (SP_START_EM, NULL, docstat);
1263 else if (!strncmp (s, "\\fB", 3)) {
1264 docstat = commit_buff (buff, &d, docstat);
1265 docstat = print_it (SP_START_BF, NULL, docstat);
1268 else if (!strncmp (s, "\\fT", 3)) {
1269 docstat = commit_buff (buff, &d, docstat);
1270 docstat = print_it (SP_START_TT, NULL, docstat);
1273 else if (!strncmp (s, "\\fP", 3)) {
1274 docstat = commit_buff (buff, &d, docstat);
1275 docstat = print_it (SP_END_FT, NULL, docstat);
1278 else if (!strncmp (s, ".dt", 3)) {
1279 docstat = commit_buff (buff, &d, docstat);
1280 docstat = print_it (SP_DT, NULL, docstat);
1283 else if (!strncmp (s, ".dd", 3)) {
1284 docstat = commit_buff (buff, &d, docstat);
1285 docstat = print_it (SP_DD, NULL, docstat);
1288 else if (*s == '$') {
1289 int output_dollar = 0;
1303 while (isalnum ((unsigned char) *s) || *s == '-' || *s == '_')
1306 docstat = commit_buff (buff, &d, docstat);
1309 print_ref (output_dollar, ref);
1318 docstat = commit_buff (buff, &d, docstat);
1319 return print_it (SP_NEWLINE, NULL, docstat);