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
79 #define D_TAB (1 << 3)
81 #define D_INIT (1 << 5)
101 enum output_formats_t OutputFormat = F_NONE;
105 static char *get_token (char *, size_t, char *);
106 static char *skip_ws (char *);
107 static const char *type2human (int);
108 static int buff2type (const char *);
109 static int flush_doc (int);
110 static int handle_docline (char *, int);
111 static int print_it (int, char *, int);
112 static void print_confline (const char *, int, const char *);
113 static void handle_confline (char *);
114 static void makedoc (FILE *, FILE *);
115 static void pretty_default (char *, size_t, const char *, int);
116 static int sgml_fputc (int);
117 static int sgml_fputs (const char *);
118 static void add_var (const char *);
119 static int add_s (const char *);
120 static int add_c (int);
122 int main (int argc, char *argv[])
127 if ((Progname = strrchr (argv[0], '/')))
132 while ((c = getopt (argc, argv, "cmsd")) != EOF) {
135 OutputFormat = F_CONF;
138 OutputFormat = F_MAN;
141 OutputFormat = F_SGML;
148 fprintf (stderr, "%s: bad command line parameter.\n", Progname);
154 if (optind != argc) {
155 if ((f = fopen (argv[optind], "r")) == NULL) {
156 fprintf (stderr, "%s: Can't open %s (%s).\n",
157 Progname, argv[optind], strerror (errno));
164 switch (OutputFormat) {
172 fprintf (stderr, "%s: No output format specified.\n", Progname);
183 static void add_var (const char *name)
185 outbuf = realloc (outbuf, (++outcount) * sizeof (var_t));
186 outbuf[outcount - 1].seen = 0;
187 outbuf[outcount - 1].name = strdup (name);
188 outbuf[outcount - 1].descr = NULL;
191 static int add (const char *s)
193 size_t lnew = STRLEN (s), lold = STRLEN (outbuf[outcount - 1].descr);
197 if (!outbuf[outcount - 1].seen) {
199 outbuf[outcount - 1].seen = 1;
203 outbuf[outcount - 1].descr = strdup (s);
205 outbuf[outcount - 1].descr =
206 realloc (outbuf[outcount - 1].descr, lold + lnew + 1);
207 memcpy (&(outbuf[outcount - 1].descr[lold - 1]) + 1, s, lnew);
209 outbuf[outcount - 1].descr[lold + lnew] = '\0';
213 static int add_c (int c)
215 char buf[2] = "\0\0";
221 static void makedoc (FILE * in, FILE * out)
223 char buffer[BUFFSIZE];
224 char token[BUFFSIZE];
228 int docstat = D_INIT;
230 while ((fgets (buffer, sizeof (buffer), in))) {
232 if ((p = strchr (buffer, '\n')) == NULL) {
233 fprintf (stderr, "%s: Line %d too long. Ask a wizard to enlarge\n"
234 "%s: my buffer size.\n", Progname, line, Progname);
240 if (!(p = get_token (token, sizeof (token), buffer)))
244 fprintf (stderr, "%s: line %d. first token: \"%s\".\n",
245 Progname, line, token);
248 if (!strcmp (token, "/*++*/"))
250 else if (!strcmp (token, "/*--*/")) {
251 docstat = flush_doc (docstat);
254 else if (active && (!strcmp (token, "/**") || !strcmp (token, "**")))
255 docstat = handle_docline (p, docstat);
256 else if (active && !strcmp (token, "{")) {
257 docstat = flush_doc (docstat);
263 qsort (outbuf, outcount, sizeof (var_t), &var_cmp);
264 for (line = 0; line < outcount; line++) {
265 if (outbuf[line].descr) {
266 fprintf (out, "%s\n", outbuf[line].descr);
267 free (outbuf[line].descr);
269 free (outbuf[line].name);
274 /* skip whitespace */
276 static char *skip_ws (char *s)
278 while (*s && isspace ((unsigned char) *s))
284 /* isolate a token */
286 static char single_char_tokens[] = "[]{},;|";
288 static char *get_token (char *d, size_t l, char *s)
295 fprintf (stderr, "%s: get_token called for `%s'.\n", Progname, s);
300 fprintf (stderr, "%s: argumet after skip_ws(): `%s'.\n", Progname, s);
304 fprintf (stderr, "%s: no more tokens on this line.\n", Progname);
308 if (strchr (single_char_tokens, *s)) {
310 fprintf (stderr, "%s: found single character token `%c'.\n",
320 fprintf (stderr, "%s: found quote character.\n", Progname);
327 for (t = s; *t && --l > 0; t++) {
328 if (*t == '\\' && !t[1])
331 if (is_quoted && *t == '\\') {
332 switch ((*d = *++t)) {
351 if (is_quoted && *t == '"') {
355 else if (!is_quoted && strchr (single_char_tokens, *t))
357 else if (!is_quoted && isspace ((unsigned char) *t))
366 fprintf (stderr, "%s: Got %stoken: `%s'.\n",
367 Progname, is_quoted ? "quoted " : "", dd);
368 fprintf (stderr, "%s: Remainder: `%s'.\n", Progname, t);
376 ** Configuration line parser
378 ** The following code parses a line from init.h which declares
379 ** a configuration variable.
383 /* note: the following enum must be in the same order as the
384 * following string definitions!
406 "DT_NONE", "-none-"}, {
407 "DT_BOOL", "boolean"}, {
408 "DT_NUM", "number"}, {
409 "DT_STR", "string"}, {
410 "DT_PATH", "path"}, {
411 "DT_QUAD", "quadoption"}, {
412 "DT_SORT", "sort order"}, {
413 "DT_RX", "regular expression"}, {
414 "DT_MAGIC", "folder magic"}, {
416 "DT_ADDR", "e-mail address"}, {
421 static int buff2type (const char *s)
425 for (type = DT_NONE; types[type].machine; type++)
426 if (!strcmp (types[type].machine, s))
432 static const char *type2human (int type)
434 return types[type].human;
436 static void handle_confline (char *s)
438 char varname[BUFFSIZE];
445 /* xxx - put this into an actual state machine? */
448 if (!(s = get_token (varname, sizeof (varname), s)))
452 if (!(s = get_token (buff, sizeof (buff), s)))
456 if (!(s = get_token (buff, sizeof (buff), s)))
459 type = buff2type (buff);
461 /* possibly a "|" or comma */
462 if (!(s = get_token (buff, sizeof (buff), s)))
465 if (!strcmp (buff, "|")) {
467 fprintf (stderr, "%s: Expecting <subtype> <comma>.\n", Progname);
468 /* ignore subtype and comma */
469 if (!(s = get_token (buff, sizeof (buff), s)))
471 if (!(s = get_token (buff, sizeof (buff), s)))
478 if (!(s = get_token (buff, sizeof (buff), s)))
480 if (!strcmp (buff, ","))
484 /* option name or UL &address */
485 if (!(s = get_token (buff, sizeof (buff), s)))
487 if (!strcmp (buff, "UL"))
488 if (!(s = get_token (buff, sizeof (buff), s)))
492 if (!(s = get_token (buff, sizeof (buff), s)))
496 fprintf (stderr, "%s: Expecting default value.\n", Progname);
498 /* <default value> or UL <default value> */
499 if (!(s = get_token (buff, sizeof (buff), s)))
501 if (!strcmp (buff, "UL")) {
503 fprintf (stderr, "%s: Skipping UL.\n", Progname);
504 if (!(s = get_token (buff, sizeof (buff), s)))
508 memset (tmp, 0, sizeof (tmp));
511 if (!strcmp (buff, "}"))
514 strncpy (tmp + STRLEN (tmp), buff, sizeof (tmp) - STRLEN (tmp));
516 while ((s = get_token (buff, sizeof (buff), s)));
518 pretty_default (val, sizeof (val), tmp, type);
520 print_confline (varname, type, val);
523 static void pretty_default (char *t, size_t l, const char *s, int type)
531 if (!strcasecmp (s, "M_YES"))
532 strncpy (t, "yes", l);
533 else if (!strcasecmp (s, "M_NO"))
534 strncpy (t, "no", l);
535 else if (!strcasecmp (s, "M_ASKYES"))
536 strncpy (t, "ask-yes", l);
537 else if (!strcasecmp (s, "M_ASKNO"))
538 strncpy (t, "ask-no", l);
544 strncpy (t, "yes", l);
546 strncpy (t, "no", l);
552 strncpy (t, s + 5, l);
554 *t = tolower ((unsigned char) *t);
560 strncpy (t, s + 2, l);
562 *t = tolower ((unsigned char) *t);
570 if (!strcmp (s, "0"))
582 static void char_to_escape (char *dest, unsigned int c)
586 strcpy (dest, "\\r");
587 break; /* __STRCPY_CHECKED__ */
589 strcpy (dest, "\\n");
590 break; /* __STRCPY_CHECKED__ */
592 strcpy (dest, "\\t");
593 break; /* __STRCPY_CHECKED__ */
595 strcpy (dest, "\\f");
596 break; /* __STRCPY_CHECKED__ */
598 sprintf (dest, "\\%03o", c);
602 static void conf_char_to_escape (unsigned int c)
606 char_to_escape (buff, c);
610 static void conf_print_strval (const char *v)
613 if (*v < ' ' || *v & 0x80) {
614 conf_char_to_escape ((unsigned int) *v);
618 if (*v == '"' || *v == '\\')
624 static void man_print_strval (const char *v)
627 if (*v < ' ' || *v & 0x80) {
629 conf_char_to_escape ((unsigned int) *v);
642 static void sgml_print_strval (const char *v)
647 if (*v < ' ' || *v & 0x80) {
648 char_to_escape (buff, (unsigned int) *v);
652 sgml_fputc ((unsigned int) *v);
656 static int sgml_fputc (int c)
664 return add ("$");
666 return add ("_");
668 return add ("%");
670 return add ("&");
672 return add ("\");
674 return add ("&dquot;");
676 return add ("[");
678 return add ("]");
680 return add ("˜");
686 static int sgml_fputs (const char *s)
689 if (sgml_fputc ((unsigned int) *s) == EOF)
695 static void print_confline (const char *varname, int type, const char *val)
700 switch (OutputFormat) {
701 /* configuration file */
704 if (type == DT_STR || type == DT_RX || type == DT_ADDR
705 || type == DT_PATH) {
709 conf_print_strval (val);
712 else if (type != DT_SYN) {
719 add ("\n#\n# Name: ");
722 add (type2human (type));
723 if (type == DT_STR || type == DT_RX || type == DT_ADDR
724 || type == DT_PATH) {
725 add ("\n# Default: \"");
726 conf_print_strval (val);
730 add ("\n# Default: ");
745 add (type2human (type));
747 if (type == DT_STR || type == DT_RX || type == DT_ADDR
748 || type == DT_PATH) {
749 add ("Default: \\(lq");
750 man_print_strval (val);
763 /* SGML based manual */
767 sgml_fputs (varname);
768 add ("<label id=\"");
771 add ("\n<p>\nType: <tt>");
772 add (type2human (type));
775 if (type == DT_STR || type == DT_RX || type == DT_ADDR
776 || type == DT_PATH) {
777 add ("<p>\nDefault: <tt>&dquot;");
778 sgml_print_strval (val);
779 add ("&dquot;</tt>");
782 add ("<p>\nDefault: <tt>");
795 ** Documentation line parser
797 ** The following code parses specially formatted documentation
798 ** comments in init.h.
800 ** The format is very remotely inspired by nroff. Most important, it's
801 ** easy to parse and convert, and it was easy to generate from the SGML
802 ** source of mutt's original manual.
804 ** - \fI switches to italics
805 ** - \fB switches to boldface
806 ** - \fP switches to normal display
807 ** - .dl on a line starts a definition list (name taken taken from HTML).
808 ** - .dt starts a term in a definition list.
809 ** - .dd starts a definition in a definition list.
810 ** - .de on a line finishes a definition list.
811 ** - .ts on a line starts a "tscreen" environment (name taken from SGML).
812 ** - .te on a line finishes this environment.
813 ** - .pp on a line starts a paragraph.
814 ** - \$word will be converted to a reference to word, where appropriate.
815 ** Note that \$$word is possible as well.
816 ** - '. ' in the beginning of a line expands to two space characters.
817 ** This is used to protect indentations in tables.
820 /* close eventually-open environments. */
822 static int fd_recurse = 0;
824 static int flush_doc (int docstat)
826 if (docstat & D_INIT)
830 fprintf (stderr, "%s: Internal error, recursion in flush_doc()!\n",
835 if (docstat & (D_TAB))
836 docstat = print_it (SP_END_TAB, NULL, docstat);
838 if (docstat & (D_DL))
839 docstat = print_it (SP_END_DL, NULL, docstat);
841 if (docstat & (D_EM | D_BF))
842 docstat = print_it (SP_END_FT, NULL, docstat);
844 docstat = print_it (SP_NEWLINE, NULL, 0);
850 /* print something. */
852 static int print_it (int special, char *str, int docstat)
854 int onl = docstat & (D_NL | D_NP);
856 docstat &= ~(D_NL | D_NP | D_INIT);
858 switch (OutputFormat) {
859 /* configuration file */
863 static int Continuation = 0;
866 docstat &= ~(D_EM | D_BF);
941 if (docstat & D_DT) {
944 for (i = STRLEN (str); i < 8; i++)
962 docstat &= ~(D_EM | D_BF);
1005 add ("\n.IP\n.DS\n.sp\n.ft CR\n.nf\n");
1006 docstat |= D_TAB | D_NL;
1011 add ("\n.fi\n.ec\n.ft P\n.sp\n");
1041 for (; *str; str++) {
1044 else if (*str == '\\')
1046 else if (!strncmp (str, "``", 2)) {
1050 else if (!strncmp (str, "''", 2)) {
1064 /* SGML based manual */
1074 docstat &= ~(D_EM | D_BF);
1117 add ("\n<tscreen><verb>\n");
1118 docstat |= D_TAB | D_NL;
1123 add ("\n</verb></tscreen>");
1130 add ("\n<descrip>\n");
1146 add ("</descrip>\n");
1152 if (docstat & D_TAB)
1161 /* make gcc happy (unreached) */
1169 void print_ref (int output_dollar, const char *ref)
1171 switch (OutputFormat) {
1194 static int commit_buff (char *buff, char **d, int docstat)
1198 docstat = print_it (SP_STR, buff, docstat);
1205 static int handle_docline (char *l, int docstat)
1207 char buff[BUFFSIZE];
1213 fprintf (stderr, "%s: handle_docline `%s'\n", Progname, l);
1215 if (!strncmp (l, ".pp", 3))
1216 return print_it (SP_NEWPAR, NULL, docstat);
1217 else if (!strncmp (l, ".ts", 3))
1218 return print_it (SP_START_TAB, NULL, docstat);
1219 else if (!strncmp (l, ".te", 3))
1220 return print_it (SP_END_TAB, NULL, docstat);
1221 else if (!strncmp (l, ".dl", 3))
1222 return print_it (SP_START_DL, NULL, docstat);
1223 else if (!strncmp (l, ".de", 3))
1224 return print_it (SP_END_DL, NULL, docstat);
1225 else if (!strncmp (l, ". ", 2))
1228 for (s = l, d = buff; *s; s++) {
1229 if (!strncmp (s, "\\(as", 4)) {
1233 else if (!strncmp (s, "\\(rs", 4)) {
1237 else if (!strncmp (s, "\\fI", 3)) {
1238 docstat = commit_buff (buff, &d, docstat);
1239 docstat = print_it (SP_START_EM, NULL, docstat);
1242 else if (!strncmp (s, "\\fB", 3)) {
1243 docstat = commit_buff (buff, &d, docstat);
1244 docstat = print_it (SP_START_BF, NULL, docstat);
1247 else if (!strncmp (s, "\\fP", 3)) {
1248 docstat = commit_buff (buff, &d, docstat);
1249 docstat = print_it (SP_END_FT, NULL, docstat);
1252 else if (!strncmp (s, ".dt", 3)) {
1253 docstat = commit_buff (buff, &d, docstat);
1254 docstat = print_it (SP_DT, NULL, docstat);
1257 else if (!strncmp (s, ".dd", 3)) {
1258 docstat = commit_buff (buff, &d, docstat);
1259 docstat = print_it (SP_DD, NULL, docstat);
1262 else if (*s == '$') {
1263 int output_dollar = 0;
1277 while (isalnum ((unsigned char) *s) || *s == '-' || *s == '_')
1280 docstat = commit_buff (buff, &d, docstat);
1283 print_ref (output_dollar, ref);
1292 docstat = commit_buff (buff, &d, docstat);
1293 return print_it (SP_NEWLINE, NULL, docstat);