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)
86 #define D_PA (1 << 10)
108 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;
155 fprintf (stderr, "%s: bad command line parameter.\n", Progname);
161 if (optind != argc) {
162 if ((f = fopen (argv[optind], "r")) == NULL) {
163 fprintf (stderr, "%s: Can't open %s (%s).\n",
164 Progname, argv[optind], strerror (errno));
171 switch (OutputFormat) {
179 fprintf (stderr, "%s: No output format specified.\n", Progname);
190 static void add_var (const char *name)
192 outbuf = realloc (outbuf, (++outcount) * sizeof (var_t));
193 outbuf[outcount - 1].seen = 0;
194 outbuf[outcount - 1].name = strdup (name);
195 outbuf[outcount - 1].descr = NULL;
198 static int add_s (const char *s)
200 size_t lnew = STRLEN (s), lold = STRLEN (outbuf[outcount - 1].descr);
204 if (!outbuf[outcount - 1].seen) {
206 outbuf[outcount - 1].seen = 1;
210 outbuf[outcount - 1].descr = strdup (s);
212 outbuf[outcount - 1].descr =
213 realloc (outbuf[outcount - 1].descr, lold + lnew + 1);
214 memcpy (&(outbuf[outcount - 1].descr[lold - 1]) + 1, s, lnew);
216 outbuf[outcount - 1].descr[lold + lnew] = '\0';
220 static int add_c (int c)
222 char buf[2] = "\0\0";
225 return (add_s (buf));
228 static void makedoc (FILE * in, FILE * out)
230 char buffer[BUFFSIZE];
231 char token[BUFFSIZE];
235 int docstat = D_INIT;
237 while ((fgets (buffer, sizeof (buffer), in))) {
239 if ((p = strchr (buffer, '\n')) == NULL) {
240 fprintf (stderr, "%s: Line %d too long. Ask a wizard to enlarge\n"
241 "%s: my buffer size.\n", Progname, line, Progname);
247 if (!(p = get_token (token, sizeof (token), buffer)))
251 fprintf (stderr, "%s: line %d. first token: \"%s\".\n",
252 Progname, line, token);
255 if (!strcmp (token, "/*++*/"))
257 else if (!strcmp (token, "/*--*/")) {
258 docstat = flush_doc (docstat);
261 else if (active && (!strcmp (token, "/**") || !strcmp (token, "**")))
262 docstat = handle_docline (p, docstat);
263 else if (active && !strcmp (token, "{")) {
264 docstat = flush_doc (docstat);
270 qsort (outbuf, outcount, sizeof (var_t), &var_cmp);
271 for (line = 0; line < outcount; line++) {
272 if (outbuf[line].descr) {
273 fprintf (out, "%s\n", outbuf[line].descr);
274 free (outbuf[line].descr);
276 free (outbuf[line].name);
281 /* skip whitespace */
283 static char *skip_ws (char *s)
285 while (*s && isspace ((unsigned char) *s))
291 /* isolate a token */
293 static char single_char_tokens[] = "[]{},;|";
295 static char *get_token (char *d, size_t l, char *s)
302 fprintf (stderr, "%s: get_token called for `%s'.\n", Progname, s);
307 fprintf (stderr, "%s: argumet after skip_ws(): `%s'.\n", Progname, s);
311 fprintf (stderr, "%s: no more tokens on this line.\n", Progname);
315 if (strchr (single_char_tokens, *s)) {
317 fprintf (stderr, "%s: found single character token `%c'.\n",
327 fprintf (stderr, "%s: found quote character.\n", Progname);
334 for (t = s; *t && --l > 0; t++) {
335 if (*t == '\\' && !t[1])
338 if (is_quoted && *t == '\\') {
339 switch ((*d = *++t)) {
358 if (is_quoted && *t == '"') {
362 else if (!is_quoted && strchr (single_char_tokens, *t))
364 else if (!is_quoted && isspace ((unsigned char) *t))
373 fprintf (stderr, "%s: Got %stoken: `%s'.\n",
374 Progname, is_quoted ? "quoted " : "", dd);
375 fprintf (stderr, "%s: Remainder: `%s'.\n", Progname, t);
383 ** Configuration line parser
385 ** The following code parses a line from init.h which declares
386 ** a configuration variable.
390 /* note: the following enum must be in the same order as the
391 * following string definitions!
414 "DT_NONE", "-none-"}, {
415 "DT_BOOL", "boolean"}, {
416 "DT_NUM", "number"}, {
417 "DT_STR", "string"}, {
418 "DT_PATH", "path"}, {
419 "DT_QUAD", "quadoption"}, {
420 "DT_SORT", "sort order"}, {
421 "DT_RX", "regular expression"}, {
422 "DT_MAGIC", "folder magic"}, {
424 "DT_ADDR", "e-mail address"}, {
425 "DT_SYS", "system property"}, {
430 static int buff2type (const char *s)
434 for (type = DT_NONE; types[type].machine; type++)
435 if (!strcmp (types[type].machine, s))
441 static const char *type2human (int type)
443 return types[type].human;
445 static void handle_confline (char *s)
447 char varname[BUFFSIZE];
453 /* xxx - put this into an actual state machine? */
456 if (!(s = get_token (varname, sizeof (varname), s)))
460 if (!(s = get_token (buff, sizeof (buff), s)))
464 if (!(s = get_token (buff, sizeof (buff), s)))
467 type = buff2type (buff);
469 /* possibly a "|" or comma */
470 if (!(s = get_token (buff, sizeof (buff), s)))
473 if (!strcmp (buff, "|")) {
475 fprintf (stderr, "%s: Expecting <subtype> <comma>.\n", Progname);
476 /* ignore subtype and comma */
477 if (!(s = get_token (buff, sizeof (buff), s)))
479 if (!(s = get_token (buff, sizeof (buff), s)))
486 if (!(s = get_token (buff, sizeof (buff), s)))
488 if (!strcmp (buff, ","))
492 /* option name or UL &address */
493 if (!(s = get_token (buff, sizeof (buff), s)))
495 if (!strcmp (buff, "UL"))
496 if (!(s = get_token (buff, sizeof (buff), s)))
500 if (!(s = get_token (buff, sizeof (buff), s)))
504 fprintf (stderr, "%s: Expecting default value.\n", Progname);
506 /* <default value> or UL <default value> */
507 if (!(s = get_token (buff, sizeof (buff), s)))
509 if (!strcmp (buff, "UL")) {
511 fprintf (stderr, "%s: Skipping UL.\n", Progname);
512 if (!(s = get_token (buff, sizeof (buff), s)))
516 memset (val, 0, sizeof (val));
519 if (!strcmp (buff, "}"))
522 strncpy (val + STRLEN (val), buff, sizeof (val) - STRLEN (val));
524 while ((s = get_token (buff, sizeof (buff), s)));
527 print_confline (varname, type, val);
530 static void char_to_escape (char *dest, unsigned int c)
534 strcpy (dest, "\\r");
535 break; /* __STRCPY_CHECKED__ */
537 strcpy (dest, "\\n");
538 break; /* __STRCPY_CHECKED__ */
540 strcpy (dest, "\\t");
541 break; /* __STRCPY_CHECKED__ */
543 strcpy (dest, "\\f");
544 break; /* __STRCPY_CHECKED__ */
546 sprintf (dest, "\\%03o", c);
550 static void conf_char_to_escape (unsigned int c)
554 char_to_escape (buff, c);
558 static void conf_print_strval (const char *v)
561 if (*v < ' ' || *v & 0x80) {
562 conf_char_to_escape ((unsigned int) *v);
566 if (*v == '"' || *v == '\\')
572 static void man_print_strval (const char *v)
575 if (*v < ' ' || *v & 0x80) {
577 conf_char_to_escape ((unsigned int) *v);
590 static void sgml_print_strval (const char *v)
595 if (*v < ' ' || *v & 0x80) {
596 char_to_escape (buff, (unsigned int) *v);
600 sgml_fputc ((unsigned int) *v);
604 static int sgml_fputc (int c)
608 return add_s ("<");
610 return add_s (">");
612 return add_s ("$");
614 return add_s ("_");
616 return add_s ("%");
618 return add_s ("&");
620 return add_s ("\");
622 return add_s (""");
624 return add_s ("[");
626 return add_s ("]");
628 return add_s ("˜");
634 static int sgml_fputs (const char *s)
637 if (sgml_fputc ((unsigned int) *s) == EOF)
643 /* reduce CDATA to ID */
644 static int sgml_id_fputs (const char *s) {
652 if (sgml_fputc ((unsigned int) id) == EOF)
658 static void print_confline (const char *varname, int type, const char *val)
663 switch (OutputFormat) {
664 /* configuration file */
667 if (type == DT_SYS) {
674 if (type == DT_STR || type == DT_RX || type == DT_ADDR
675 || type == DT_PATH) {
679 conf_print_strval (val);
682 else if (type != DT_SYN) {
689 add_s ("\n#\n# Name: ");
691 add_s ("\n# Type: ");
692 add_s (type2human (type));
693 if (type == DT_STR || type == DT_RX || type == DT_ADDR
694 || type == DT_PATH) {
695 add_s ("\n# Default: \"");
696 conf_print_strval (val);
700 add_s ("\n# Default: ");
715 add_s (type2human (type));
717 if (type == DT_STR || type == DT_RX || type == DT_ADDR
718 || type == DT_PATH) {
719 add_s ("Default: \\(lq");
720 man_print_strval (val);
724 add_s (type == DT_SYS ? "Value: " : "Default: ");
733 /* SGML based manual */
736 add_s ("\n<sect1 id=\"");
737 sgml_id_fputs (varname);
738 add_s ("\">\n<title>");
739 sgml_fputs (varname);
740 add_s ("</title>\n<para>Type: <literal>");
741 add_s (type2human (type));
742 add_s ("</literal></para>\n\n");
744 if (type == DT_STR || type == DT_RX || type == DT_ADDR
745 || type == DT_PATH) {
746 add_s ("<para>\nDefault: <literal>"");
747 sgml_print_strval (val);
748 add_s (""</literal>");
752 add_s (type == DT_SYS ? "Value: " : "Default: ");
755 add_s ("</literal>");
767 ** Documentation line parser
769 ** The following code parses specially formatted documentation
770 ** comments in init.h.
772 ** The format is very remotely inspired by nroff. Most important, it's
773 ** easy to parse and convert, and it was easy to generate from the SGML
774 ** source of mutt's original manual.
776 ** - \fI switches to italics
777 ** - \fB switches to boldface
778 ** - \fT switches to typewriter for SGML
779 ** - \fP switches to normal display
780 ** - .dl on a line starts a definition list (name taken taken from HTML).
781 ** - .dt starts a term in a definition list.
782 ** - .dd starts a definition in a definition list.
783 ** - .de on a line finishes a definition list.
784 ** - .ts on a line starts a "tscreen" environment (name taken from SGML).
785 ** - .te on a line finishes this environment.
786 ** - .pp on a line starts a paragraph.
787 ** - \$word will be converted to a reference to word, where appropriate.
788 ** Note that \$$word is possible as well.
789 ** - '. ' in the beginning of a line expands to two space characters.
790 ** This is used to protect indentations in tables.
793 /* close eventually-open environments. */
795 static int fd_recurse = 0;
797 static int flush_doc (int docstat)
799 if (docstat & D_INIT)
803 fprintf (stderr, "%s: Internal error, recursion in flush_doc()!\n",
808 if (docstat & (D_PA))
809 docstat = print_it (SP_END_PAR, NULL, docstat);
811 if (docstat & (D_TAB))
812 docstat = print_it (SP_END_TAB, NULL, docstat);
814 if (docstat & (D_DL))
815 docstat = print_it (SP_END_DL, NULL, docstat);
817 if (docstat & (D_EM | D_BF | D_TT))
818 docstat = print_it (SP_END_FT, NULL, docstat);
820 docstat = print_it (SP_END_SECT, NULL, docstat);
822 docstat = print_it (SP_NEWLINE, NULL, 0);
828 /* print something. */
830 static int print_it (int special, char *str, int docstat)
832 int onl = docstat & (D_NL | D_NP);
834 docstat &= ~(D_NL | D_NP | D_INIT);
836 switch (OutputFormat) {
837 /* configuration file */
841 static int Continuation = 0;
844 docstat &= ~(D_EM | D_BF | D_TT);
922 if (docstat & D_DT) {
925 for (i = STRLEN (str); i < 8; i++)
943 docstat &= ~(D_EM | D_BF | D_TT);
950 docstat &= ~(D_EM | D_TT);
957 docstat &= ~(D_BF | D_TT);
963 docstat &= ~(D_BF | D_EM);
992 add_s ("\n.IP\n.DS\n.sp\n.ft CR\n.nf\n");
993 docstat |= D_TAB | D_NL;
998 add_s ("\n.fi\n.ec\n.ft P\n.sp\n");
1028 for (; *str; str++) {
1031 else if (*str == '\\')
1033 else if (!strncmp (str, "``", 2)) {
1037 else if (!strncmp (str, "''", 2)) {
1051 /* SGML based manual */
1058 add_s ("</emphasis>");
1060 add_s ("</emphasis>");
1062 add_s ("</literal>");
1063 docstat &= ~(D_EM | D_BF | D_TT);
1068 add_s ("<emphasis role=\"bold\">");
1070 docstat &= ~(D_EM | D_TT);
1075 add_s ("<emphasis>");
1077 docstat &= ~(D_BF | D_TT);
1082 add_s ("<literal>");
1084 docstat &= ~(D_EM | D_BF);
1107 add_s ("</para>\n");
1116 add_s ("\n<screen>\n");
1117 docstat |= D_TAB | D_NL;
1122 add_s ("\n</screen>");
1129 add_s ("\n<variablelist>\n");
1135 add_s ("<varlistentry><term>");
1140 add_s ("</term>\n<listitem><para>\n");
1146 add_s ("</para></listitem></varlistentry></variablelist>\n");
1147 docstat &= ~(D_DL|D_DD);
1152 add_s ("</para>\n");
1158 add_s ("</para></listitem></varlistentry>\n");
1164 add_s ("</sect1>\n");
1169 if (docstat & D_TAB)
1178 /* make gcc happy (unreached) */
1186 void print_ref (int output_dollar, const char *ref)
1188 switch (OutputFormat) {
1197 add_s ("<link linkend=\"");
1198 sgml_id_fputs (ref);
1211 static int commit_buff (char *buff, char **d, int docstat)
1215 docstat = print_it (SP_STR, buff, docstat);
1222 static int handle_docline (char *l, int docstat)
1224 char buff[BUFFSIZE];
1230 fprintf (stderr, "%s: handle_docline `%s'\n", Progname, l);
1232 if (!strncmp (l, ".pp", 3))
1233 return print_it (SP_NEWPAR, NULL, docstat);
1234 else if (!strncmp (l, ".ts", 3))
1235 return print_it (SP_START_TAB, NULL, docstat);
1236 else if (!strncmp (l, ".te", 3))
1237 return print_it (SP_END_TAB, NULL, docstat);
1238 else if (!strncmp (l, ".dl", 3))
1239 return print_it (SP_START_DL, NULL, docstat);
1240 else if (!strncmp (l, ".de", 3))
1241 return print_it (SP_END_DL, NULL, docstat);
1242 else if (!strncmp (l, ". ", 2))
1245 for (s = l, d = buff; *s; s++) {
1246 if (!strncmp (s, "\\(as", 4)) {
1250 else if (!strncmp (s, "\\(rs", 4)) {
1254 else if (!strncmp (s, "\\fI", 3)) {
1255 docstat = commit_buff (buff, &d, docstat);
1256 docstat = print_it (SP_START_EM, NULL, docstat);
1259 else if (!strncmp (s, "\\fB", 3)) {
1260 docstat = commit_buff (buff, &d, docstat);
1261 docstat = print_it (SP_START_BF, NULL, docstat);
1264 else if (!strncmp (s, "\\fT", 3)) {
1265 docstat = commit_buff (buff, &d, docstat);
1266 docstat = print_it (SP_START_TT, NULL, docstat);
1269 else if (!strncmp (s, "\\fP", 3)) {
1270 docstat = commit_buff (buff, &d, docstat);
1271 docstat = print_it (SP_END_FT, NULL, docstat);
1274 else if (!strncmp (s, ".dt", 3)) {
1275 if (docstat & D_DD) {
1276 docstat = commit_buff (buff, &d, docstat);
1277 docstat = print_it (SP_END_DD, NULL, docstat);
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);