2 * Copyright (C) 1999-2000 Thomas Roessler <roessler@does-not-exist.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
20 ** This program parses mutt's init.h and generates documentation in
21 ** three different formats:
23 ** -> a commented muttrc configuration file
24 ** -> nroff, suitable for inclusion in a manual page
25 ** -> linuxdoc-sgml, suitable for inclusion in the
52 extern char *sys_errlist[];
55 #define strerror(x) ((x) > 0 && (x) < sys_nerr) ? sys_errlist[(x)] : 0
56 #endif /* !HAVE_STRERROR */
64 F_CONF, F_MAN, F_SGML, F_NONE
70 #define D_TAB (1 << 3)
72 #define D_INIT (1 << 5)
93 enum output_formats_t OutputFormat = F_NONE;
97 static char *get_token (char *, size_t, char *);
98 static char *skip_ws (char *);
99 static const char *type2human (int);
100 static int buff2type (const char *);
101 static int flush_doc (int, FILE *);
102 static int handle_docline (char *, FILE *, int);
103 static int print_it (int, char *, FILE *, int);
104 static void print_confline (const char *, int, const char *, FILE *);
105 static void handle_confline (char *, FILE *);
106 static void makedoc (FILE *, FILE *);
107 static void pretty_default (char *, size_t, const char *, int);
108 static int sgml_fputc (int, FILE *);
109 static int sgml_fputs (const char *, FILE *);
111 int main (int argc, char *argv[])
116 if ((Progname = strrchr (argv[0], '/')))
121 while ((c = getopt (argc, argv, "cmsd")) != EOF)
125 case 'c': OutputFormat = F_CONF; break;
126 case 'm': OutputFormat = F_MAN; break;
127 case 's': OutputFormat = F_SGML; break;
128 case 'd': Debug++; break;
131 fprintf (stderr, "%s: bad command line parameter.\n", Progname);
139 if ((f = fopen (argv[optind], "r")) == NULL)
141 fprintf (stderr, "%s: Can't open %s (%s).\n",
142 Progname, argv[optind], strerror (errno));
149 switch (OutputFormat)
153 case F_SGML: makedoc (f, stdout); break;
156 fprintf (stderr, "%s: No output format specified.\n",
169 static void makedoc (FILE *in, FILE *out)
171 char buffer[BUFFSIZE];
172 char token[BUFFSIZE];
176 int docstat = D_INIT;
178 while ((fgets (buffer, sizeof (buffer), in)))
181 if ((p = strchr (buffer, '\n')) == NULL)
183 fprintf (stderr, "%s: Line %d too long. Ask a wizard to enlarge\n"
184 "%s: my buffer size.\n", Progname, line, Progname);
190 if (!(p = get_token (token, sizeof (token), buffer)))
195 fprintf (stderr, "%s: line %d. first token: \"%s\".\n",
196 Progname, line, token);
199 if (!strcmp (token, "/*++*/"))
201 else if (!strcmp (token, "/*--*/"))
203 docstat = flush_doc (docstat, out);
206 else if (active && (!strcmp (token, "/**") || !strcmp (token, "**")))
207 docstat = handle_docline (p, out, docstat);
208 else if (active && !strcmp (token, "{"))
210 docstat = flush_doc (docstat, out);
211 handle_confline (p, out);
214 flush_doc (docstat, out);
218 /* skip whitespace */
220 static char *skip_ws (char *s)
222 while (*s && isspace ((unsigned char) *s))
228 /* isolate a token */
230 static char single_char_tokens[] = "[]{},;|";
232 static char *get_token (char *d, size_t l, char *s)
239 fprintf (stderr, "%s: get_token called for `%s'.\n",
245 fprintf (stderr, "%s: argumet after skip_ws(): `%s'.\n",
251 fprintf (stderr, "%s: no more tokens on this line.\n", Progname);
255 if (strchr (single_char_tokens, *s))
259 fprintf (stderr, "%s: found single character token `%c'.\n",
271 fprintf (stderr, "%s: found quote character.\n", Progname);
278 for (t = s; *t && --l > 0; t++)
280 if (*t == '\\' && !t[1])
283 if (is_quoted && *t == '\\')
287 case 'n': *d = '\n'; break;
288 case 't': *d = '\t'; break;
289 case 'r': *d = '\r'; break;
290 case 'a': *d = '\a'; break;
297 if (is_quoted && *t == '"')
302 else if (!is_quoted && strchr (single_char_tokens, *t))
304 else if (!is_quoted && isspace ((unsigned char) *t))
314 fprintf (stderr, "%s: Got %stoken: `%s'.\n",
315 Progname, is_quoted ? "quoted " : "", dd);
316 fprintf (stderr, "%s: Remainder: `%s'.\n",
325 ** Configuration line parser
327 ** The following code parses a line from init.h which declares
328 ** a configuration variable.
332 /* note: the following enum must be in the same order as the
333 * following string definitions!
358 { "DT_NONE", "-none-" },
359 { "DT_BOOL", "boolean" },
360 { "DT_NUM", "number" },
361 { "DT_STR", "string" },
362 { "DT_PATH", "path" },
363 { "DT_QUAD", "quadoption" },
364 { "DT_SORT", "sort order" },
365 { "DT_RX", "regular expression" },
366 { "DT_MAGIC", "folder magic" },
368 { "DT_ADDR", "e-mail address" },
373 static int buff2type (const char *s)
377 for (type = DT_NONE; types[type].machine; type++)
378 if (!strcmp (types[type].machine, s))
384 static const char *type2human (int type)
386 return types[type].human;
388 static void handle_confline (char *s, FILE *out)
390 char varname[BUFFSIZE];
397 /* xxx - put this into an actual state machine? */
400 if (!(s = get_token (varname, sizeof (varname), s))) return;
403 if (!(s = get_token (buff, sizeof (buff), s))) return;
406 if (!(s = get_token (buff, sizeof (buff), s))) return;
408 type = buff2type (buff);
410 /* possibly a "|" or comma */
411 if (!(s = get_token (buff, sizeof (buff), s))) return;
413 if (!strcmp (buff, "|"))
415 if (Debug) fprintf (stderr, "%s: Expecting <subtype> <comma>.\n", Progname);
416 /* ignore subtype and comma */
417 if (!(s = get_token (buff, sizeof (buff), s))) return;
418 if (!(s = get_token (buff, sizeof (buff), s))) return;
425 if (!(s = get_token (buff, sizeof (buff), s))) return;
426 if (!strcmp (buff, ","))
430 /* option name or UL &address */
431 if (!(s = get_token (buff, sizeof (buff), s))) return;
432 if (!strcmp (buff, "UL"))
433 if (!(s = get_token (buff, sizeof (buff), s))) return;
436 if (!(s = get_token (buff, sizeof (buff), s))) return;
438 if (Debug) fprintf (stderr, "%s: Expecting default value.\n", Progname);
440 /* <default value> or UL <default value> */
441 if (!(s = get_token (buff, sizeof (buff), s))) return;
442 if (!strcmp (buff, "UL"))
444 if (Debug) fprintf (stderr, "%s: Skipping UL.\n", Progname);
445 if (!(s = get_token (buff, sizeof (buff), s))) return;
448 memset (tmp, 0, sizeof (tmp));
452 if (!strcmp (buff, "}"))
455 strncpy (tmp + strlen (tmp), buff, sizeof (tmp) - strlen (tmp));
457 while ((s = get_token (buff, sizeof (buff), s)));
459 pretty_default (val, sizeof (val), tmp, type);
460 print_confline (varname, type, val, out);
463 static void pretty_default (char *t, size_t l, const char *s, int type)
472 if (!strcasecmp (s, "M_YES")) strncpy (t, "yes", l);
473 else if (!strcasecmp (s, "M_NO")) strncpy (t, "no", l);
474 else if (!strcasecmp (s, "M_ASKYES")) strncpy (t, "ask-yes", l);
475 else if (!strcasecmp (s, "M_ASKNO")) strncpy (t, "ask-no", l);
481 strncpy (t, "yes", l);
483 strncpy (t, "no", l);
489 strncpy (t, s + 5, l);
490 for (; *t; t++) *t = tolower ((unsigned char) *t);
496 strncpy (t, s + 2, l);
497 for (; *t; t++) *t = tolower ((unsigned char) *t);
505 if (!strcmp (s, "0"))
517 static void char_to_escape (char *dest, unsigned int c)
521 case '\r': strcpy (dest, "\\r"); break; /* __STRCPY_CHECKED__ */
522 case '\n': strcpy (dest, "\\n"); break; /* __STRCPY_CHECKED__ */
523 case '\t': strcpy (dest, "\\t"); break; /* __STRCPY_CHECKED__ */
524 case '\f': strcpy (dest, "\\f"); break; /* __STRCPY_CHECKED__ */
525 default: sprintf (dest, "\\%03o", c); break;
528 static void conf_char_to_escape (unsigned int c , FILE *out)
531 char_to_escape (buff, c);
535 static void conf_print_strval (const char *v, FILE *out)
539 if (*v < ' ' || *v & 0x80)
541 conf_char_to_escape ((unsigned int) *v, out);
545 if (*v == '"' || *v == '\\')
551 static void man_print_strval (const char *v, FILE *out)
555 if (*v < ' ' || *v & 0x80)
558 conf_char_to_escape ((unsigned int) *v, out);
563 fputs ("\\(rq", out);
571 static void sgml_print_strval (const char *v, FILE *out)
576 if (*v < ' ' || *v & 0x80)
578 char_to_escape (buff, (unsigned int) *v);
579 sgml_fputs (buff, out);
582 sgml_fputc ((unsigned int) *v, out);
586 static int sgml_fputc (int c, FILE *out)
590 case '<': return fputs ("<", out);
591 case '>': return fputs (">", out);
592 case '$': return fputs ("$", out);
593 case '_': return fputs ("_", out);
594 case '%': return fputs ("%", out);
595 case '&': return fputs ("&", out);
596 case '\\': return fputs ("\", out);
597 case '"': return fputs ("&dquot;", out);
598 case '[': return fputs ("[", out);
599 case ']': return fputs ("]", out);
600 case '~': return fputs ("˜", out);
601 default: return fputc (c, out);
605 static int sgml_fputs (const char *s, FILE *out)
608 if (sgml_fputc ((unsigned int) *s, out) == EOF)
614 static void print_confline (const char *varname, int type, const char *val, FILE *out)
616 if (type == DT_SYN) return;
618 switch (OutputFormat)
620 /* configuration file */
623 if (type == DT_STR || type == DT_RX || type == DT_ADDR || type == DT_PATH)
625 fprintf (out, "\n# set %s=\"", varname);
626 conf_print_strval (val, out);
629 else if (type != DT_SYN)
630 fprintf (out, "\n# set %s=%s", varname, val);
632 fprintf (out, "\n#\n# Name: %s", varname);
633 fprintf (out, "\n# Type: %s", type2human (type));
634 if (type == DT_STR || type == DT_RX || type == DT_ADDR || type == DT_PATH)
636 fputs ("\n# Default: \"", out);
637 conf_print_strval (val, out);
641 fprintf (out, "\n# Default: %s", val);
650 fprintf (out, "\n.TP\n.B %s\n", varname);
651 fputs (".nf\n", out);
652 fprintf (out, "Type: %s\n", type2human (type));
653 if (type == DT_STR || type == DT_RX || type == DT_ADDR || type == DT_PATH)
655 fputs ("Default: \\(lq", out);
656 man_print_strval (val, out);
657 fputs ("\\(rq\n", out);
660 fprintf (out, "Default: %s\n", val);
667 /* SGML based manual */
670 fputs ("\n<sect2>", out); sgml_fputs (varname, out);
671 fprintf (out, "<label id=\"%s\">", varname);
672 fprintf (out, "\n<p>\nType: %s<newline>", type2human (type));
674 if (type == DT_STR || type == DT_RX || type == DT_ADDR || type == DT_PATH)
676 fputs ("\nDefault: &dquot;", out);
677 sgml_print_strval (val, out);
678 fputs ("&dquot;\n", out);
681 fprintf (out, "\nDefault: %s\n", val);
691 ** Documentation line parser
693 ** The following code parses specially formatted documentation
694 ** comments in init.h.
696 ** The format is very remotely inspired by nroff. Most important, it's
697 ** easy to parse and convert, and it was easy to generate from the SGML
698 ** source of mutt's original manual.
700 ** - \fI switches to italics
701 ** - \fB switches to boldface
702 ** - \fP switches to normal display
703 ** - .dl on a line starts a definition list (name taken taken from HTML).
704 ** - .dt starts a term in a definition list.
705 ** - .dd starts a definition in a definition list.
706 ** - .de on a line finishes a definition list.
707 ** - .ts on a line starts a "tscreen" environment (name taken from SGML).
708 ** - .te on a line finishes this environment.
709 ** - .pp on a line starts a paragraph.
710 ** - \$word will be converted to a reference to word, where appropriate.
711 ** Note that \$$word is possible as well.
712 ** - '. ' in the beginning of a line expands to two space characters.
713 ** This is used to protect indentations in tables.
716 /* close eventually-open environments. */
718 static int fd_recurse = 0;
720 static int flush_doc (int docstat, FILE *out)
722 if (docstat & D_INIT)
727 fprintf (stderr, "%s: Internal error, recursion in flush_doc()!\n", Progname);
731 if (docstat & (D_TAB))
732 docstat = print_it (SP_END_TAB, NULL, out, docstat);
734 if (docstat & (D_DL))
735 docstat = print_it (SP_END_DL, NULL, out, docstat);
737 if (docstat & (D_EM | D_BF))
738 docstat = print_it (SP_END_FT, NULL, out, docstat);
740 docstat = print_it (SP_NEWLINE, NULL, out, 0);
746 /* print something. */
748 static int print_it (int special, char *str, FILE *out, int docstat)
750 int onl = docstat & (D_NL|D_NP);
752 docstat &= ~(D_NL|D_NP|D_INIT);
754 switch (OutputFormat)
756 /* configuration file */
761 static int Continuation = 0;
763 case SP_END_FT: docstat &= ~(D_EM|D_BF); break;
764 case SP_START_BF: docstat |= D_BF; break;
765 case SP_START_EM: docstat |= D_EM; break;
840 for (i = strlen (str) ; i < 8 ; i++)
859 docstat &= ~(D_EM|D_BF);
897 fputs (".IP\n", out);
904 fputs ("\n.IP\n.DS\n.sp\n.ft CR\n.nf\n", out);
905 docstat |= D_TAB | D_NL;
910 fputs ("\n.fi\n.ec\n.ft P\n.sp\n", out);
917 fputs ("\n.RS", out);
923 fputs ("\n.IP ", out);
933 fputs ("\n.RE", out);
944 fputs ("\\(rq", out);
945 else if (*str == '\\')
947 else if (!strncmp (str, "``", 2))
949 fputs ("\\(lq", out);
952 else if (!strncmp (str, "''", 2))
954 fputs ("\\(rq", out);
967 /* SGML based manual */
974 if (docstat & D_EM) fputs ("</em>", out);
975 if (docstat & D_BF) fputs ("</bf>", out);
976 docstat &= ~(D_EM|D_BF);
1014 fputs ("<p>\n", out);
1021 fputs ("\n<tscreen><verb>\n", out);
1022 docstat |= D_TAB | D_NL;
1027 fputs ("\n</verb></tscreen>", out);
1034 fputs ("\n<descrip>\n", out);
1040 fputs ("<tag>", out);
1045 fputs ("</tag>", out);
1050 fputs ("</descrip>\n", out);
1056 if (docstat & D_TAB)
1059 sgml_fputs (str, out);
1065 /* make gcc happy (unreached) */
1073 void print_ref (FILE *out, int output_dollar, const char *ref)
1075 switch (OutputFormat)
1085 fprintf (out, "<ref id=\"%s\" name=\"", ref);
1087 fputs ("$", out);
1088 sgml_fputs (ref, out);
1097 static int commit_buff (char *buff, char **d, FILE *out, int docstat)
1102 docstat = print_it (SP_STR, buff, out, docstat);
1109 static int handle_docline (char *l, FILE *out, int docstat)
1111 char buff[BUFFSIZE];
1116 fprintf (stderr, "%s: handle_docline `%s'\n", Progname, l);
1118 if (!strncmp (l, ".pp", 3))
1119 return print_it (SP_NEWPAR, NULL, out, docstat);
1120 else if (!strncmp (l, ".ts", 3))
1121 return print_it (SP_START_TAB, NULL, out, docstat);
1122 else if (!strncmp (l, ".te", 3))
1123 return print_it (SP_END_TAB, NULL, out, docstat);
1124 else if (!strncmp (l, ".dl", 3))
1125 return print_it (SP_START_DL, NULL, out, docstat);
1126 else if (!strncmp (l, ".de", 3))
1127 return print_it (SP_END_DL, NULL, out, docstat);
1128 else if (!strncmp (l, ". ", 2))
1131 for (s = l, d = buff; *s; s++)
1133 if (!strncmp (s, "\\(as", 4))
1138 else if (!strncmp (s, "\\(rs", 4))
1143 else if (!strncmp (s, "\\fI", 3))
1145 docstat = commit_buff (buff, &d, out, docstat);
1146 docstat = print_it (SP_START_EM, NULL, out, docstat);
1149 else if (!strncmp (s, "\\fB", 3))
1151 docstat = commit_buff (buff, &d, out, docstat);
1152 docstat = print_it (SP_START_BF, NULL, out, docstat);
1155 else if (!strncmp (s, "\\fP", 3))
1157 docstat = commit_buff (buff, &d, out, docstat);
1158 docstat = print_it (SP_END_FT, NULL, out, docstat);
1161 else if (!strncmp (s, ".dt", 3))
1163 docstat = commit_buff (buff, &d, out, docstat);
1164 docstat = print_it (SP_DT, NULL, out, docstat);
1167 else if (!strncmp (s, ".dd", 3))
1169 docstat = commit_buff (buff, &d, out, docstat);
1170 docstat = print_it (SP_DD, NULL, out, docstat);
1175 int output_dollar = 0;
1192 while (isalnum ((unsigned char) *s) || *s == '-' || *s == '_')
1195 docstat = commit_buff (buff, &d, out, docstat);
1198 print_ref (out, output_dollar, ref);
1207 docstat = commit_buff (buff, &d, out, docstat);
1208 return print_it (SP_NEWLINE, NULL, out, docstat);