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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 ** -> docbook-xml, suitable for inclusion in the
49 #include "makedoc-defs.h"
54 extern char *sys_errlist[];
57 #define strerror(x) ((x) > 0 && (x) < sys_nerr) ? sys_errlist[(x)] : 0
58 #endif /* !HAVE_STRERROR */
66 F_CONF, F_MAN, F_SGML, F_NONE
72 #define D_TAB (1 << 3)
74 #define D_INIT (1 << 5)
100 enum output_formats_t OutputFormat = F_NONE;
104 static char *get_token (char *, size_t, char *);
105 static char *skip_ws (char *);
106 static const char *type2human (int);
107 static int buff2type (const char *);
108 static int flush_doc (int, FILE *);
109 static int handle_docline (char *, FILE *, int);
110 static int print_it (int, char *, FILE *, int);
111 static void print_confline (const char *, int, const char *, FILE *);
112 static void handle_confline (char *, FILE *);
113 static void makedoc (FILE *, FILE *);
114 static void pretty_default (char *, size_t, const char *, int);
115 static int sgml_fputc (int, FILE *);
116 static int sgml_fputs (const char *, FILE *);
117 static int sgml_id_fputs (const char *, FILE *);
119 int main (int argc, char *argv[])
124 if ((Progname = strrchr (argv[0], '/')))
129 while ((c = getopt (argc, argv, "cmsd")) != EOF)
133 case 'c': OutputFormat = F_CONF; break;
134 case 'm': OutputFormat = F_MAN; break;
135 case 's': OutputFormat = F_SGML; break;
136 case 'd': Debug++; break;
139 fprintf (stderr, "%s: bad command line parameter.\n", Progname);
147 if ((f = fopen (argv[optind], "r")) == NULL)
149 fprintf (stderr, "%s: Can't open %s (%s).\n",
150 Progname, argv[optind], strerror (errno));
157 switch (OutputFormat)
161 case F_SGML: makedoc (f, stdout); break;
164 fprintf (stderr, "%s: No output format specified.\n",
177 static void makedoc (FILE *in, FILE *out)
179 char buffer[BUFFSIZE];
180 char token[BUFFSIZE];
184 int docstat = D_INIT;
186 while ((fgets (buffer, sizeof (buffer), in)))
189 if ((p = strchr (buffer, '\n')) == NULL)
191 fprintf (stderr, "%s: Line %d too long. Ask a wizard to enlarge\n"
192 "%s: my buffer size.\n", Progname, line, Progname);
198 if (!(p = get_token (token, sizeof (token), buffer)))
203 fprintf (stderr, "%s: line %d. first token: \"%s\".\n",
204 Progname, line, token);
207 if (!strcmp (token, "/*++*/"))
209 else if (!strcmp (token, "/*--*/"))
211 docstat = flush_doc (docstat, out);
214 else if (active && (!strcmp (token, "/**") || !strcmp (token, "**")))
215 docstat = handle_docline (p, out, docstat);
216 else if (active && !strcmp (token, "{"))
218 docstat = flush_doc (docstat, out);
219 handle_confline (p, out);
222 flush_doc (docstat, out);
226 /* skip whitespace */
228 static char *skip_ws (char *s)
230 while (*s && isspace ((unsigned char) *s))
236 /* isolate a token */
238 static char single_char_tokens[] = "[]{},;|";
240 static char *get_token (char *d, size_t l, char *s)
247 fprintf (stderr, "%s: get_token called for `%s'.\n",
253 fprintf (stderr, "%s: argumet after skip_ws(): `%s'.\n",
259 fprintf (stderr, "%s: no more tokens on this line.\n", Progname);
263 if (strchr (single_char_tokens, *s))
267 fprintf (stderr, "%s: found single character token `%c'.\n",
279 fprintf (stderr, "%s: found quote character.\n", Progname);
286 for (t = s; *t && --l > 0; t++)
288 if (*t == '\\' && !t[1])
291 if (is_quoted && *t == '\\')
295 case 'n': *d = '\n'; break;
296 case 't': *d = '\t'; break;
297 case 'r': *d = '\r'; break;
298 case 'a': *d = '\a'; break;
305 if (is_quoted && *t == '"')
310 else if (!is_quoted && strchr (single_char_tokens, *t))
312 else if (!is_quoted && isspace ((unsigned char) *t))
322 fprintf (stderr, "%s: Got %stoken: `%s'.\n",
323 Progname, is_quoted ? "quoted " : "", dd);
324 fprintf (stderr, "%s: Remainder: `%s'.\n",
333 ** Configuration line parser
335 ** The following code parses a line from init.h which declares
336 ** a configuration variable.
340 /* note: the following enum must be in the same order as the
341 * following string definitions!
366 { "DT_NONE", "-none-" },
367 { "DT_BOOL", "boolean" },
368 { "DT_NUM", "number" },
369 { "DT_STR", "string" },
370 { "DT_PATH", "path" },
371 { "DT_QUAD", "quadoption" },
372 { "DT_SORT", "sort order" },
373 { "DT_RX", "regular expression" },
374 { "DT_MAGIC", "folder magic" },
376 { "DT_ADDR", "e-mail address" },
381 static int buff2type (const char *s)
385 for (type = DT_NONE; types[type].machine; type++)
386 if (!strcmp (types[type].machine, s))
392 static const char *type2human (int type)
394 return types[type].human;
396 static void handle_confline (char *s, FILE *out)
398 char varname[BUFFSIZE];
405 /* xxx - put this into an actual state machine? */
408 if (!(s = get_token (varname, sizeof (varname), s))) return;
411 if (!(s = get_token (buff, sizeof (buff), s))) return;
414 if (!(s = get_token (buff, sizeof (buff), s))) return;
416 type = buff2type (buff);
418 /* possibly a "|" or comma */
419 if (!(s = get_token (buff, sizeof (buff), s))) return;
421 if (!strcmp (buff, "|"))
423 if (Debug) fprintf (stderr, "%s: Expecting <subtype> <comma>.\n", Progname);
424 /* ignore subtype and comma */
425 if (!(s = get_token (buff, sizeof (buff), s))) return;
426 if (!(s = get_token (buff, sizeof (buff), s))) return;
433 if (!(s = get_token (buff, sizeof (buff), s))) return;
434 if (!strcmp (buff, ","))
438 /* option name or UL &address */
439 if (!(s = get_token (buff, sizeof (buff), s))) return;
440 if (!strcmp (buff, "UL"))
441 if (!(s = get_token (buff, sizeof (buff), s))) return;
444 if (!(s = get_token (buff, sizeof (buff), s))) return;
446 if (Debug) fprintf (stderr, "%s: Expecting default value.\n", Progname);
448 /* <default value> or UL <default value> */
449 if (!(s = get_token (buff, sizeof (buff), s))) return;
450 if (!strcmp (buff, "UL"))
452 if (Debug) fprintf (stderr, "%s: Skipping UL.\n", Progname);
453 if (!(s = get_token (buff, sizeof (buff), s))) return;
456 memset (tmp, 0, sizeof (tmp));
460 if (!strcmp (buff, "}"))
463 strncpy (tmp + strlen (tmp), buff, sizeof (tmp) - strlen (tmp));
465 while ((s = get_token (buff, sizeof (buff), s)));
467 pretty_default (val, sizeof (val), tmp, type);
468 print_confline (varname, type, val, out);
471 static void pretty_default (char *t, size_t l, const char *s, int type)
480 if (!strcasecmp (s, "M_YES")) strncpy (t, "yes", l);
481 else if (!strcasecmp (s, "M_NO")) strncpy (t, "no", l);
482 else if (!strcasecmp (s, "M_ASKYES")) strncpy (t, "ask-yes", l);
483 else if (!strcasecmp (s, "M_ASKNO")) strncpy (t, "ask-no", l);
489 strncpy (t, "yes", l);
491 strncpy (t, "no", l);
497 strncpy (t, s + 5, l);
498 for (; *t; t++) *t = tolower ((unsigned char) *t);
504 strncpy (t, s + 2, l);
505 for (; *t; t++) *t = tolower ((unsigned char) *t);
513 if (!strcmp (s, "0"))
525 static void char_to_escape (char *dest, unsigned int c)
529 case '\r': strcpy (dest, "\\r"); break; /* __STRCPY_CHECKED__ */
530 case '\n': strcpy (dest, "\\n"); break; /* __STRCPY_CHECKED__ */
531 case '\t': strcpy (dest, "\\t"); break; /* __STRCPY_CHECKED__ */
532 case '\f': strcpy (dest, "\\f"); break; /* __STRCPY_CHECKED__ */
533 default: sprintf (dest, "\\%03o", c); break;
536 static void conf_char_to_escape (unsigned int c , FILE *out)
539 char_to_escape (buff, c);
543 static void conf_print_strval (const char *v, FILE *out)
547 if (*v < ' ' || *v & 0x80)
549 conf_char_to_escape ((unsigned int) *v, out);
553 if (*v == '"' || *v == '\\')
559 static void man_print_strval (const char *v, FILE *out)
563 if (*v < ' ' || *v & 0x80)
566 conf_char_to_escape ((unsigned int) *v, out);
571 fputs ("\\(rq", out);
579 static void sgml_print_strval (const char *v, FILE *out)
584 if (*v < ' ' || *v & 0x80)
586 char_to_escape (buff, (unsigned int) *v);
587 sgml_fputs (buff, out);
590 sgml_fputc ((unsigned int) *v, out);
594 static int sgml_fputc (int c, FILE *out)
598 case '<': return fputs ("<", out);
599 case '>': return fputs (">", out);
600 case '$': return fputs ("$", out);
601 case '_': return fputs ("_", out);
602 case '%': return fputs ("%", out);
603 case '&': return fputs ("&", out);
604 case '\\': return fputs ("\", out);
605 case '"': return fputs (""", out);
606 case '[': return fputs ("[", out);
607 case ']': return fputs ("]", out);
608 case '~': return fputs ("˜", out);
609 default: return fputc (c, out);
613 static int sgml_fputs (const char *s, FILE *out)
616 if (sgml_fputc ((unsigned int) *s, out) == EOF)
622 /* reduce CDATA to ID */
623 static int sgml_id_fputs (const char *s, FILE* out)
634 if (fputc ((unsigned int) id, out) == EOF)
641 static void print_confline (const char *varname, int type, const char *val, FILE *out)
643 if (type == DT_SYN) return;
645 switch (OutputFormat)
647 /* configuration file */
650 if (type == DT_STR || type == DT_RX || type == DT_ADDR || type == DT_PATH)
652 fprintf (out, "\n# set %s=\"", varname);
653 conf_print_strval (val, out);
656 else if (type != DT_SYN)
657 fprintf (out, "\n# set %s=%s", varname, val);
659 fprintf (out, "\n#\n# Name: %s", varname);
660 fprintf (out, "\n# Type: %s", type2human (type));
661 if (type == DT_STR || type == DT_RX || type == DT_ADDR || type == DT_PATH)
663 fputs ("\n# Default: \"", out);
664 conf_print_strval (val, out);
668 fprintf (out, "\n# Default: %s", val);
677 fprintf (out, "\n.TP\n.B %s\n", varname);
678 fputs (".nf\n", out);
679 fprintf (out, "Type: %s\n", type2human (type));
680 if (type == DT_STR || type == DT_RX || type == DT_ADDR || type == DT_PATH)
682 fputs ("Default: \\(lq", out);
683 man_print_strval (val, out);
684 fputs ("\\(rq\n", out);
687 fprintf (out, "Default: %s\n", val);
694 /* SGML based manual */
697 fputs ("\n<sect2 id=\"", out);
698 sgml_id_fputs(varname, out);
699 fputs ("\">\n<title>", out);
700 sgml_fputs (varname, out);
701 fprintf (out, "</title>\n<literallayout>Type: %s", type2human (type));
703 if (type == DT_STR || type == DT_RX || type == DT_ADDR || type == DT_PATH)
705 fputs ("\nDefault: "", out);
706 sgml_print_strval (val, out);
707 fputs (""</literallayout>\n", out);
710 fprintf (out, "\nDefault: %s</literallayout>\n", val);
720 ** Documentation line parser
722 ** The following code parses specially formatted documentation
723 ** comments in init.h.
725 ** The format is very remotely inspired by nroff. Most important, it's
726 ** easy to parse and convert, and it was easy to generate from the SGML
727 ** source of mutt's original manual.
729 ** - \fI switches to italics
730 ** - \fB switches to boldface
731 ** - \fP switches to normal display
732 ** - .dl on a line starts a definition list (name taken taken from HTML).
733 ** - .dt starts a term in a definition list.
734 ** - .dd starts a definition in a definition list.
735 ** - .de on a line finishes a definition list.
736 ** - .ts on a line starts a "tscreen" environment (name taken from SGML).
737 ** - .te on a line finishes this environment.
738 ** - .pp on a line starts a paragraph.
739 ** - \$word will be converted to a reference to word, where appropriate.
740 ** Note that \$$word is possible as well.
741 ** - '. ' in the beginning of a line expands to two space characters.
742 ** This is used to protect indentations in tables.
745 /* close eventually-open environments. */
747 static int fd_recurse = 0;
749 static int flush_doc (int docstat, FILE *out)
751 if (docstat & D_INIT)
756 fprintf (stderr, "%s: Internal error, recursion in flush_doc()!\n", Progname);
760 if (docstat & (D_PA))
761 docstat = print_it (SP_END_PAR, NULL, out, docstat);
763 if (docstat & (D_TAB))
764 docstat = print_it (SP_END_TAB, NULL, out, docstat);
766 if (docstat & (D_DL))
767 docstat = print_it (SP_END_DL, NULL, out, docstat);
769 if (docstat & (D_EM | D_BF))
770 docstat = print_it (SP_END_FT, NULL, out, docstat);
772 docstat = print_it (SP_END_SECT, NULL, out, docstat);
774 docstat = print_it (SP_NEWLINE, NULL, out, 0);
780 /* print something. */
782 static int print_it (int special, char *str, FILE *out, int docstat)
784 int onl = docstat & (D_NL|D_NP);
786 docstat &= ~(D_NL|D_NP|D_INIT);
788 switch (OutputFormat)
790 /* configuration file */
795 static int Continuation = 0;
797 case SP_END_FT: docstat &= ~(D_EM|D_BF); break;
798 case SP_START_BF: docstat |= D_BF; break;
799 case SP_START_EM: docstat |= D_EM; break;
874 for (i = strlen (str) ; i < 8 ; i++)
893 docstat &= ~(D_EM|D_BF);
931 fputs (".IP\n", out);
938 fputs ("\n.IP\n.DS\n.sp\n.ft CR\n.nf\n", out);
939 docstat |= D_TAB | D_NL;
944 fputs ("\n.fi\n.ec\n.ft P\n.sp\n", out);
951 fputs (".RS\n.PD 0\n", out);
957 fputs (".TP\n", out);
967 fputs (".RE\n.PD 1", out);
978 fputs ("\\(rq", out);
979 else if (*str == '\\')
981 else if (!strncmp (str, "``", 2))
983 fputs ("\\(lq", out);
986 else if (!strncmp (str, "''", 2))
988 fputs ("\\(rq", out);
1001 /* SGML based manual */
1008 if (docstat & D_EM) fputs ("</emphasis>", out);
1009 if (docstat & D_BF) fputs ("</emphasis>", out);
1010 docstat &= ~(D_EM|D_BF);
1015 fputs ("<emphasis role=\"bold\">", out);
1022 fputs ("<emphasis>", out);
1049 fputs("</para>\n", out);
1050 fputs ("<para>\n", out);
1059 fputs ("</para>\n", out);
1065 fputs ("\n<screen>\n", out);
1066 docstat |= D_TAB | D_NL;
1071 fputs ("\n</screen>", out);
1078 fputs ("\n<variablelist>\n", out);
1084 fputs ("<varlistentry><term>", out);
1090 fputs ("</term>\n<listitem><para>", out);
1096 fputs ("</para></listitem></varlistentry>\n", out);
1101 fputs ("</para></listitem></varlistentry></variablelist>\n", out);
1102 docstat &= ~(D_DD|D_DL);
1107 fputs ("</sect2>", out);
1112 if (docstat & D_TAB)
1115 sgml_fputs (str, out);
1121 /* make gcc happy (unreached) */
1129 void print_ref (FILE *out, int output_dollar, const char *ref)
1131 switch (OutputFormat)
1141 fputs ("<link linkend=\"", out);
1142 sgml_id_fputs (ref, out);
1145 fputs ("$", out);
1146 sgml_fputs (ref, out);
1147 fputs ("</link>", out);
1155 static int commit_buff (char *buff, char **d, FILE *out, int docstat)
1160 docstat = print_it (SP_STR, buff, out, docstat);
1167 static int handle_docline (char *l, FILE *out, int docstat)
1169 char buff[BUFFSIZE];
1174 fprintf (stderr, "%s: handle_docline `%s'\n", Progname, l);
1176 if (!strncmp (l, ".pp", 3))
1177 return print_it (SP_NEWPAR, NULL, out, docstat);
1178 else if (!strncmp (l, ".ts", 3))
1179 return print_it (SP_START_TAB, NULL, out, docstat);
1180 else if (!strncmp (l, ".te", 3))
1181 return print_it (SP_END_TAB, NULL, out, docstat);
1182 else if (!strncmp (l, ".dl", 3))
1183 return print_it (SP_START_DL, NULL, out, docstat);
1184 else if (!strncmp (l, ".de", 3))
1185 return print_it (SP_END_DL, NULL, out, docstat);
1186 else if (!strncmp (l, ". ", 2))
1189 for (s = l, d = buff; *s; s++)
1191 if (!strncmp (s, "\\(as", 4))
1196 else if (!strncmp (s, "\\(rs", 4))
1201 else if (!strncmp (s, "\\fI", 3))
1203 docstat = commit_buff (buff, &d, out, docstat);
1204 docstat = print_it (SP_START_EM, NULL, out, docstat);
1207 else if (!strncmp (s, "\\fB", 3))
1209 docstat = commit_buff (buff, &d, out, docstat);
1210 docstat = print_it (SP_START_BF, NULL, out, docstat);
1213 else if (!strncmp (s, "\\fP", 3))
1215 docstat = commit_buff (buff, &d, out, docstat);
1216 docstat = print_it (SP_END_FT, NULL, out, docstat);
1219 else if (!strncmp (s, ".dt", 3))
1223 docstat = commit_buff (buff, &d, out, docstat);
1224 docstat = print_it (SP_END_DD, NULL, out, docstat);
1226 docstat = commit_buff (buff, &d, out, docstat);
1227 docstat = print_it (SP_DT, NULL, out, docstat);
1230 else if (!strncmp (s, ".dd", 3))
1232 docstat = commit_buff (buff, &d, out, docstat);
1233 docstat = print_it (SP_DD, NULL, out, docstat);
1238 int output_dollar = 0;
1255 while (isalnum ((unsigned char) *s) || *s == '-' || *s == '_')
1258 docstat = commit_buff (buff, &d, out, docstat);
1261 print_ref (out, output_dollar, ref);
1270 docstat = commit_buff (buff, &d, out, docstat);
1271 return print_it (SP_NEWLINE, NULL, out, docstat);