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
34 #include <lib-lib/str.h>
47 extern char *sys_errlist[];
50 #define strerror(x) ((x) > 0 && (x) < sys_nerr) ? sys_errlist[(x)] : 0
51 #endif /* !HAVE_STRERROR */
57 #define STRLEN(s) (s ? strlen(s) : 0)
65 static int outcount = 0;
66 static var_t *outbuf = NULL;
68 static int var_cmp (const void *a, const void *b)
70 return (strcmp (((var_t *) a)->name, ((var_t *) b)->name));
73 enum output_formats_t {
74 F_CONF, F_MAN, F_SGML, F_NONE
81 #define D_TAB (1 << 4)
83 #define D_INIT (1 << 6)
87 #define D_PA (1 << 10)
109 enum output_formats_t OutputFormat = F_NONE;
112 static char *get_token (char *, size_t, char *);
113 static char *skip_ws (char *);
114 static const char *type2human (int);
115 static int buff2type (const char *);
116 static int flush_doc (int);
117 static int handle_docline (char *, int);
118 static int print_it (int, char *, int);
119 static void print_confline (const char *, int, const char *);
120 static void handle_confline (char *);
121 static void makedoc (FILE *, FILE *);
122 static int sgml_fputc (int);
123 static int sgml_fputs (const char *);
124 static int sgml_id_fputs (const char *);
125 static void add_var (const char *);
126 static int add_s (const char *);
127 static int add_c (int);
129 int main (int argc, char *argv[])
134 if ((Progname = strrchr (argv[0], '/')))
139 while ((c = getopt (argc, argv, "cmsd")) != EOF) {
142 OutputFormat = F_CONF;
145 OutputFormat = F_MAN;
148 OutputFormat = F_SGML;
152 fprintf (stderr, "%s: bad command line parameter.\n", Progname);
158 if (optind != argc) {
159 if ((f = fopen (argv[optind], "r")) == NULL) {
160 fprintf (stderr, "%s: Can't open %s (%s).\n",
161 Progname, argv[optind], strerror (errno));
168 switch (OutputFormat) {
176 fprintf (stderr, "%s: No output format specified.\n", Progname);
187 static void add_var (const char *name)
189 outbuf = realloc (outbuf, (++outcount) * sizeof (var_t));
190 outbuf[outcount - 1].seen = 0;
191 outbuf[outcount - 1].name = strdup(name);
192 outbuf[outcount - 1].descr = NULL;
195 static int add_s (const char *s)
197 size_t lnew = STRLEN (s), lold = STRLEN (outbuf[outcount - 1].descr);
201 if (!outbuf[outcount - 1].seen) {
203 outbuf[outcount - 1].seen = 1;
207 outbuf[outcount - 1].descr = strdup(s);
209 outbuf[outcount - 1].descr =
210 realloc (outbuf[outcount - 1].descr, lold + lnew + 1);
211 memcpy (&(outbuf[outcount - 1].descr[lold - 1]) + 1, s, lnew);
213 outbuf[outcount - 1].descr[lold + lnew] = '\0';
217 static int add_c (int c)
219 char buf[2] = "\0\0";
222 return (add_s (buf));
225 static void makedoc (FILE * in, FILE * out)
227 char buffer[BUFFSIZE];
228 char token[BUFFSIZE];
232 int docstat = D_INIT;
234 while ((fgets (buffer, sizeof (buffer), in))) {
236 if ((p = strchr (buffer, '\n')) == NULL) {
237 fprintf (stderr, "%s: Line %d too long. Ask a wizard to enlarge\n"
238 "%s: my buffer size.\n", Progname, line, Progname);
244 if (!(p = get_token (token, sizeof (token), buffer)))
247 if (!strcmp (token, "/*++*/"))
249 else if (!strcmp (token, "/*--*/")) {
250 docstat = flush_doc (docstat);
253 else if (active && (!strcmp (token, "/**") || !strcmp (token, "**")))
254 docstat = handle_docline (p, docstat);
255 else if (active && !strcmp (token, "{")) {
256 docstat = flush_doc (docstat);
262 qsort (outbuf, outcount, sizeof (var_t), &var_cmp);
263 for (line = 0; line < outcount; line++) {
264 if (outbuf[line].descr) {
265 fprintf (out, "%s\n", outbuf[line].descr);
266 free (outbuf[line].descr);
268 free (outbuf[line].name);
273 /* skip whitespace */
275 static char *skip_ws (char *s)
277 while (*s && isspace ((unsigned char) *s))
283 /* isolate a token */
285 static char single_char_tokens[] = "[]{},;|";
287 static char *get_token (char *d, size_t l, char *s)
299 if (strchr (single_char_tokens, *s)) {
310 for (t = s; *t && --l > 0; t++) {
311 if (*t == '\\' && !t[1])
314 if (is_quoted && *t == '\\') {
315 switch ((*d = *++t)) {
334 if (is_quoted && *t == '"') {
338 else if (!is_quoted && strchr (single_char_tokens, *t))
340 else if (!is_quoted && isspace ((unsigned char) *t))
353 ** Configuration line parser
355 ** The following code parses a line from init.h which declares
356 ** a configuration variable.
360 /* note: the following enum must be in the same order as the
361 * following string definitions!
384 "DT_NONE", "-none-"}, {
385 "DT_BOOL", "boolean"}, {
386 "DT_NUM", "number"}, {
387 "DT_STR", "string"}, {
388 "DT_PATH", "path"}, {
389 "DT_QUAD", "quadoption"}, {
390 "DT_SORT", "sort order"}, {
391 "DT_RX", "regular expression"}, {
392 "DT_MAGIC", "folder magic"}, {
394 "DT_ADDR", "e-mail address"}, {
395 "DT_SYS", "system property"}, {
400 static int buff2type (const char *s)
404 for (type = DT_NONE; types[type].machine; type++)
405 if (!strcmp (types[type].machine, s))
411 static const char *type2human (int type)
413 return types[type].human;
415 static void handle_confline (char *s)
417 char varname[BUFFSIZE];
423 /* xxx - put this into an actual state machine? */
426 if (!(s = get_token (varname, sizeof (varname), s)))
430 if (!(s = get_token (buff, sizeof (buff), s)))
434 if (!(s = get_token (buff, sizeof (buff), s)))
437 type = buff2type (buff);
439 /* possibly a "|" or comma */
440 if (!(s = get_token (buff, sizeof (buff), s)))
443 if (!strcmp (buff, "|")) {
444 /* ignore subtype and comma */
445 if (!(s = get_token (buff, sizeof (buff), s)))
447 if (!(s = get_token (buff, sizeof (buff), s)))
454 if (!(s = get_token (buff, sizeof (buff), s)))
456 if (!strcmp (buff, ","))
460 /* option name or UL &address */
461 if (!(s = get_token (buff, sizeof (buff), s)))
463 if (!strcmp (buff, "UL"))
464 if (!(s = get_token (buff, sizeof (buff), s)))
468 if (!(s = get_token (buff, sizeof (buff), s)))
471 /* <default value> or UL <default value> */
472 if (!(s = get_token (buff, sizeof (buff), s)))
474 if (!strcmp (buff, "UL")) {
475 if (!(s = get_token (buff, sizeof (buff), s)))
479 memset(val, 0, sizeof(val));
482 if (!strcmp (buff, "}"))
485 m_strcat(val, sizeof(val), buff);
487 while ((s = get_token (buff, sizeof (buff), s)));
490 print_confline (varname, type, val);
493 static void char_to_escape (char *dest, unsigned int c)
497 strcpy (dest, "\\r");
498 break; /* __STRCPY_CHECKED__ */
500 strcpy (dest, "\\n");
501 break; /* __STRCPY_CHECKED__ */
503 strcpy (dest, "\\t");
504 break; /* __STRCPY_CHECKED__ */
506 strcpy (dest, "\\f");
507 break; /* __STRCPY_CHECKED__ */
509 sprintf (dest, "\\%03o", c);
513 static void conf_char_to_escape (unsigned int c)
517 char_to_escape (buff, c);
521 static void conf_print_strval (const char *v)
524 if (*v < ' ' || *v & 0x80) {
525 conf_char_to_escape ((unsigned int) *v);
529 if (*v == '"' || *v == '\\')
535 static void man_print_strval (const char *v)
538 if (*v < ' ' || *v & 0x80) {
540 conf_char_to_escape ((unsigned int) *v);
553 static void sgml_print_strval (const char *v)
558 if (*v < ' ' || *v & 0x80) {
559 char_to_escape (buff, (unsigned int) *v);
563 sgml_fputc ((unsigned int) *v);
567 static int sgml_fputc (int c)
571 return add_s ("<");
573 return add_s (">");
576 return add_s ("$");
578 return add_s ("_");
580 return add_s ("%");
583 return add_s ("&");
586 return add_s ("\");
588 return add_s (""");
590 return add_s ("[");
592 return add_s ("]");
594 return add_s ("˜");
601 static int sgml_fputs (const char *s)
604 if (sgml_fputc ((unsigned int) *s) == EOF)
610 /* reduce CDATA to ID */
611 static int sgml_id_fputs (const char *s) {
619 if (sgml_fputc ((unsigned int) id) == EOF)
625 static void print_confline (const char *varname, int type, const char *val)
630 switch (OutputFormat) {
631 /* configuration file */
634 if (type == DT_SYS) {
641 if (type == DT_STR || type == DT_RX || type == DT_ADDR
642 || type == DT_PATH) {
646 conf_print_strval (val);
649 else if (type != DT_SYN) {
656 add_s ("\n#\n# Name: ");
658 add_s ("\n# Type: ");
659 add_s (type2human (type));
660 if (type == DT_STR || type == DT_RX || type == DT_ADDR
661 || type == DT_PATH) {
662 add_s ("\n# Default: \"");
663 conf_print_strval (val);
667 add_s ("\n# Default: ");
682 add_s (type2human (type));
684 if (type == DT_STR || type == DT_RX || type == DT_ADDR
685 || type == DT_PATH) {
686 add_s ("Default: \\(lq");
687 man_print_strval (val);
691 add_s (type == DT_SYS ? "Value: " : "Default: ");
700 /* SGML based manual */
703 add_s ("\n<madmutt-doc:vardef name=\"");
704 sgml_fputs (varname);
705 add_s ("\">\n<para>Type: <literal>");
706 add_s (type2human (type));
707 add_s ("</literal></para>\n");
709 if (type == DT_STR || type == DT_RX || type == DT_ADDR
710 || type == DT_PATH) {
711 add_s ("<para>\nDefault: <literal>"");
712 sgml_print_strval (val);
713 add_s (""</literal>");
717 add_s (type == DT_SYS ? "Value: " : "Default: ");
720 add_s ("</literal>");
732 ** Documentation line parser
734 ** The following code parses specially formatted documentation
735 ** comments in init.h.
737 ** The format is very remotely inspired by nroff. Most important, it's
738 ** easy to parse and convert, and it was easy to generate from the SGML
739 ** source of mutt's original manual.
741 ** - \fI switches to italics
742 ** - \fB switches to boldface
743 ** - \fT switches to typewriter for SGML
744 ** - \fP switches to normal display
745 ** - .dl on a line starts a definition list (name taken taken from HTML).
746 ** - .dt starts a term in a definition list.
747 ** - .dd starts a definition in a definition list.
748 ** - .de on a line finishes a definition list.
749 ** - .ts on a line starts a "tscreen" environment (name taken from SGML).
750 ** - .te on a line finishes this environment.
751 ** - .pp on a line starts a paragraph.
752 ** - \$word will be converted to a reference to word, where appropriate.
753 ** Note that \$$word is possible as well.
754 ** - '. ' in the beginning of a line expands to two space characters.
755 ** This is used to protect indentations in tables.
758 /* close eventually-open environments. */
760 static int fd_recurse = 0;
762 static int flush_doc (int docstat)
764 if (docstat & D_INIT)
768 fprintf (stderr, "%s: Internal error, recursion in flush_doc()!\n",
773 if (docstat & (D_PA))
774 docstat = print_it (SP_END_PAR, NULL, docstat);
776 if (docstat & (D_TAB))
777 docstat = print_it (SP_END_TAB, NULL, docstat);
779 if (docstat & (D_DL))
780 docstat = print_it (SP_END_DL, NULL, docstat);
782 if (docstat & (D_EM | D_BF | D_TT))
783 docstat = print_it (SP_END_FT, NULL, docstat);
785 docstat = print_it (SP_END_SECT, NULL, docstat);
787 docstat = print_it (SP_NEWLINE, NULL, 0);
793 /* print something. */
795 static int print_it (int special, char *str, int docstat)
797 int onl = docstat & (D_NL | D_NP);
799 docstat &= ~(D_NL | D_NP | D_INIT);
801 switch (OutputFormat) {
802 /* configuration file */
806 static int Continuation = 0;
809 docstat &= ~(D_EM | D_BF | D_TT);
887 if (docstat & D_DT) {
890 for (i = STRLEN (str); i < 8; i++)
908 docstat &= ~(D_EM | D_BF | D_TT);
915 docstat &= ~(D_EM | D_TT);
922 docstat &= ~(D_BF | D_TT);
928 docstat &= ~(D_BF | D_EM);
957 add_s ("\n.IP\n.DS\n.sp\n.ft CR\n.nf\n");
958 docstat |= D_TAB | D_NL;
963 add_s ("\n.fi\n.ec\n.ft P\n.sp\n");
993 for (; *str; str++) {
996 else if (*str == '\\')
998 else if (!strncmp (str, "``", 2)) {
1002 else if (!strncmp (str, "''", 2)) {
1016 /* SGML based manual */
1023 add_s ("</emphasis>");
1025 add_s ("</emphasis>");
1027 add_s ("</literal>");
1028 docstat &= ~(D_EM | D_BF | D_TT);
1033 add_s ("<emphasis role=\"bold\">");
1035 docstat &= ~(D_EM | D_TT);
1040 add_s ("<emphasis>");
1042 docstat &= ~(D_BF | D_TT);
1047 add_s ("<literal>");
1049 docstat &= ~(D_EM | D_BF);
1072 add_s ("</para>\n");
1081 add_s ("\n<screen>\n");
1082 docstat |= D_TAB | D_NL;
1087 add_s ("\n</screen>");
1094 add_s ("\n<variablelist>\n");
1100 add_s ("<varlistentry><term>");
1105 add_s ("</term>\n<listitem><para>\n");
1111 add_s ("</para></listitem></varlistentry></variablelist>\n");
1112 docstat &= ~(D_DL|D_DD);
1117 add_s ("</para>\n");
1123 add_s ("</para></listitem></varlistentry>\n");
1129 add_s ("</madmutt-doc:vardef>\n");
1134 if (docstat & D_TAB)
1143 /* make gcc happy (unreached) */
1151 void print_ref (int output_dollar, const char *ref)
1153 switch (OutputFormat) {
1162 add_s ("<link linkend=\"");
1163 sgml_id_fputs (ref);
1176 static int commit_buff (char *buff, char **d, int docstat)
1180 docstat = print_it (SP_STR, buff, docstat);
1187 static int handle_docline (char *l, int docstat)
1189 char buff[BUFFSIZE];
1194 if (!strncmp (l, ".pp", 3))
1195 return print_it (SP_NEWPAR, NULL, docstat);
1196 else if (!strncmp (l, ".ts", 3))
1197 return print_it (SP_START_TAB, NULL, docstat);
1198 else if (!strncmp (l, ".te", 3))
1199 return print_it (SP_END_TAB, NULL, docstat);
1200 else if (!strncmp (l, ".dl", 3))
1201 return print_it (SP_START_DL, NULL, docstat);
1202 else if (!strncmp (l, ".de", 3))
1203 return print_it (SP_END_DL, NULL, docstat);
1204 else if (!strncmp (l, ". ", 2))
1207 for (s = l, d = buff; *s; s++) {
1208 if (!strncmp (s, "\\(as", 4)) {
1212 else if (!strncmp (s, "\\(rs", 4)) {
1216 else if (!strncmp (s, "\\fI", 3)) {
1217 docstat = commit_buff (buff, &d, docstat);
1218 docstat = print_it (SP_START_EM, NULL, docstat);
1221 else if (!strncmp (s, "\\fB", 3)) {
1222 docstat = commit_buff (buff, &d, docstat);
1223 docstat = print_it (SP_START_BF, NULL, docstat);
1226 else if (!strncmp (s, "\\fT", 3)) {
1227 docstat = commit_buff (buff, &d, docstat);
1228 docstat = print_it (SP_START_TT, NULL, docstat);
1231 else if (!strncmp (s, "\\fP", 3)) {
1232 docstat = commit_buff (buff, &d, docstat);
1233 docstat = print_it (SP_END_FT, NULL, docstat);
1236 else if (!strncmp (s, ".dt", 3)) {
1237 if (docstat & D_DD) {
1238 docstat = commit_buff (buff, &d, docstat);
1239 docstat = print_it (SP_END_DD, NULL, docstat);
1241 docstat = commit_buff (buff, &d, docstat);
1242 docstat = print_it (SP_DT, NULL, docstat);
1245 else if (!strncmp (s, ".dd", 3)) {
1246 docstat = commit_buff (buff, &d, docstat);
1247 docstat = print_it (SP_DD, NULL, docstat);
1250 else if (*s == '$') {
1251 int output_dollar = 0;
1265 while (isalnum ((unsigned char) *s) || *s == '-' || *s == '_')
1268 docstat = commit_buff (buff, &d, docstat);
1271 print_ref (output_dollar, ref);
1280 docstat = commit_buff (buff, &d, docstat);
1281 return print_it (SP_NEWLINE, NULL, docstat);