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)
103 enum output_formats_t OutputFormat = F_NONE;
107 static char *get_token (char *, size_t, char *);
108 static char *skip_ws (char *);
109 static const char *type2human (int);
110 static int buff2type (const char *);
111 static int flush_doc (int);
112 static int handle_docline (char *, int);
113 static int print_it (int, char *, int);
114 static void print_confline (const char *, int, const char *);
115 static void handle_confline (char *);
116 static void makedoc (FILE *, FILE *);
117 static int sgml_fputc (int);
118 static int sgml_fputs (const char *);
119 static void add_var (const char *);
120 static int add_s (const char *);
121 static int add_c (int);
123 int main (int argc, char *argv[])
128 if ((Progname = strrchr (argv[0], '/')))
133 while ((c = getopt (argc, argv, "cmsd")) != EOF) {
136 OutputFormat = F_CONF;
139 OutputFormat = F_MAN;
142 OutputFormat = F_SGML;
149 fprintf (stderr, "%s: bad command line parameter.\n", Progname);
155 if (optind != argc) {
156 if ((f = fopen (argv[optind], "r")) == NULL) {
157 fprintf (stderr, "%s: Can't open %s (%s).\n",
158 Progname, argv[optind], strerror (errno));
165 switch (OutputFormat) {
173 fprintf (stderr, "%s: No output format specified.\n", Progname);
184 static void add_var (const char *name)
186 outbuf = realloc (outbuf, (++outcount) * sizeof (var_t));
187 outbuf[outcount - 1].seen = 0;
188 outbuf[outcount - 1].name = strdup (name);
189 outbuf[outcount - 1].descr = NULL;
192 static int add_s (const char *s)
194 size_t lnew = STRLEN (s), lold = STRLEN (outbuf[outcount - 1].descr);
198 if (!outbuf[outcount - 1].seen) {
200 outbuf[outcount - 1].seen = 1;
204 outbuf[outcount - 1].descr = strdup (s);
206 outbuf[outcount - 1].descr =
207 realloc (outbuf[outcount - 1].descr, lold + lnew + 1);
208 memcpy (&(outbuf[outcount - 1].descr[lold - 1]) + 1, s, lnew);
210 outbuf[outcount - 1].descr[lold + lnew] = '\0';
214 static int add_c (int c)
216 char buf[2] = "\0\0";
219 return (add_s (buf));
222 static void makedoc (FILE * in, FILE * out)
224 char buffer[BUFFSIZE];
225 char token[BUFFSIZE];
229 int docstat = D_INIT;
231 while ((fgets (buffer, sizeof (buffer), in))) {
233 if ((p = strchr (buffer, '\n')) == NULL) {
234 fprintf (stderr, "%s: Line %d too long. Ask a wizard to enlarge\n"
235 "%s: my buffer size.\n", Progname, line, Progname);
241 if (!(p = get_token (token, sizeof (token), buffer)))
245 fprintf (stderr, "%s: line %d. first token: \"%s\".\n",
246 Progname, line, token);
249 if (!strcmp (token, "/*++*/"))
251 else if (!strcmp (token, "/*--*/")) {
252 docstat = flush_doc (docstat);
255 else if (active && (!strcmp (token, "/**") || !strcmp (token, "**")))
256 docstat = handle_docline (p, docstat);
257 else if (active && !strcmp (token, "{")) {
258 docstat = flush_doc (docstat);
264 qsort (outbuf, outcount, sizeof (var_t), &var_cmp);
265 for (line = 0; line < outcount; line++) {
266 if (outbuf[line].descr) {
267 fprintf (out, "%s\n", outbuf[line].descr);
268 free (outbuf[line].descr);
270 free (outbuf[line].name);
275 /* skip whitespace */
277 static char *skip_ws (char *s)
279 while (*s && isspace ((unsigned char) *s))
285 /* isolate a token */
287 static char single_char_tokens[] = "[]{},;|";
289 static char *get_token (char *d, size_t l, char *s)
296 fprintf (stderr, "%s: get_token called for `%s'.\n", Progname, s);
301 fprintf (stderr, "%s: argumet after skip_ws(): `%s'.\n", Progname, s);
305 fprintf (stderr, "%s: no more tokens on this line.\n", Progname);
309 if (strchr (single_char_tokens, *s)) {
311 fprintf (stderr, "%s: found single character token `%c'.\n",
321 fprintf (stderr, "%s: found quote character.\n", Progname);
328 for (t = s; *t && --l > 0; t++) {
329 if (*t == '\\' && !t[1])
332 if (is_quoted && *t == '\\') {
333 switch ((*d = *++t)) {
352 if (is_quoted && *t == '"') {
356 else if (!is_quoted && strchr (single_char_tokens, *t))
358 else if (!is_quoted && isspace ((unsigned char) *t))
367 fprintf (stderr, "%s: Got %stoken: `%s'.\n",
368 Progname, is_quoted ? "quoted " : "", dd);
369 fprintf (stderr, "%s: Remainder: `%s'.\n", Progname, t);
377 ** Configuration line parser
379 ** The following code parses a line from init.h which declares
380 ** a configuration variable.
384 /* note: the following enum must be in the same order as the
385 * following string definitions!
407 "DT_NONE", "-none-"}, {
408 "DT_BOOL", "boolean"}, {
409 "DT_NUM", "number"}, {
410 "DT_STR", "string"}, {
411 "DT_PATH", "path"}, {
412 "DT_QUAD", "quadoption"}, {
413 "DT_SORT", "sort order"}, {
414 "DT_RX", "regular expression"}, {
415 "DT_MAGIC", "folder magic"}, {
417 "DT_ADDR", "e-mail address"}, {
422 static int buff2type (const char *s)
426 for (type = DT_NONE; types[type].machine; type++)
427 if (!strcmp (types[type].machine, s))
433 static const char *type2human (int type)
435 return types[type].human;
437 static void handle_confline (char *s)
439 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 (val, 0, sizeof (val));
511 if (!strcmp (buff, "}"))
514 strncpy (val + STRLEN (val), buff, sizeof (val) - STRLEN (val));
516 while ((s = get_token (buff, sizeof (buff), s)));
519 print_confline (varname, type, val);
522 static void char_to_escape (char *dest, unsigned int c)
526 strcpy (dest, "\\r");
527 break; /* __STRCPY_CHECKED__ */
529 strcpy (dest, "\\n");
530 break; /* __STRCPY_CHECKED__ */
532 strcpy (dest, "\\t");
533 break; /* __STRCPY_CHECKED__ */
535 strcpy (dest, "\\f");
536 break; /* __STRCPY_CHECKED__ */
538 sprintf (dest, "\\%03o", c);
542 static void conf_char_to_escape (unsigned int c)
546 char_to_escape (buff, c);
550 static void conf_print_strval (const char *v)
553 if (*v < ' ' || *v & 0x80) {
554 conf_char_to_escape ((unsigned int) *v);
558 if (*v == '"' || *v == '\\')
564 static void man_print_strval (const char *v)
567 if (*v < ' ' || *v & 0x80) {
569 conf_char_to_escape ((unsigned int) *v);
582 static void sgml_print_strval (const char *v)
587 if (*v < ' ' || *v & 0x80) {
588 char_to_escape (buff, (unsigned int) *v);
592 sgml_fputc ((unsigned int) *v);
596 static int sgml_fputc (int c)
600 return add_s ("<");
602 return add_s (">");
604 return add_s ("$");
606 return add_s ("_");
608 return add_s ("%");
610 return add_s ("&");
612 return add_s ("\");
614 return add_s ("&dquot;");
616 return add_s ("[");
618 return add_s ("]");
620 return add_s ("˜");
626 static int sgml_fputs (const char *s)
629 if (sgml_fputc ((unsigned int) *s) == EOF)
635 static void print_confline (const char *varname, int type, const char *val)
640 switch (OutputFormat) {
641 /* configuration file */
644 if (type == DT_STR || type == DT_RX || type == DT_ADDR
645 || type == DT_PATH) {
649 conf_print_strval (val);
652 else if (type != DT_SYN) {
659 add_s ("\n#\n# Name: ");
661 add_s ("\n# Type: ");
662 add_s (type2human (type));
663 if (type == DT_STR || type == DT_RX || type == DT_ADDR
664 || type == DT_PATH) {
665 add_s ("\n# Default: \"");
666 conf_print_strval (val);
670 add_s ("\n# Default: ");
685 add_s (type2human (type));
687 if (type == DT_STR || type == DT_RX || type == DT_ADDR
688 || type == DT_PATH) {
689 add_s ("Default: \\(lq");
690 man_print_strval (val);
703 /* SGML based manual */
707 sgml_fputs (varname);
708 add_s ("<label id=\"");
711 add_s ("\n<p>\nType: <tt>");
712 add_s (type2human (type));
715 if (type == DT_STR || type == DT_RX || type == DT_ADDR
716 || type == DT_PATH) {
717 add_s ("<p>\nDefault: <tt>&dquot;");
718 sgml_print_strval (val);
719 add_s ("&dquot;</tt>");
722 add_s ("<p>\nDefault: <tt>");
735 ** Documentation line parser
737 ** The following code parses specially formatted documentation
738 ** comments in init.h.
740 ** The format is very remotely inspired by nroff. Most important, it's
741 ** easy to parse and convert, and it was easy to generate from the SGML
742 ** source of mutt's original manual.
744 ** - \fI switches to italics
745 ** - \fB switches to boldface
746 ** - \fT switches to typewriter for SGML
747 ** - \fP switches to normal display
748 ** - .dl on a line starts a definition list (name taken taken from HTML).
749 ** - .dt starts a term in a definition list.
750 ** - .dd starts a definition in a definition list.
751 ** - .de on a line finishes a definition list.
752 ** - .ts on a line starts a "tscreen" environment (name taken from SGML).
753 ** - .te on a line finishes this environment.
754 ** - .pp on a line starts a paragraph.
755 ** - \$word will be converted to a reference to word, where appropriate.
756 ** Note that \$$word is possible as well.
757 ** - '. ' in the beginning of a line expands to two space characters.
758 ** This is used to protect indentations in tables.
761 /* close eventually-open environments. */
763 static int fd_recurse = 0;
765 static int flush_doc (int docstat)
767 if (docstat & D_INIT)
771 fprintf (stderr, "%s: Internal error, recursion in flush_doc()!\n",
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_NEWLINE, NULL, 0);
791 /* print something. */
793 static int print_it (int special, char *str, int docstat)
795 int onl = docstat & (D_NL | D_NP);
797 docstat &= ~(D_NL | D_NP | D_INIT);
799 switch (OutputFormat) {
800 /* configuration file */
804 static int Continuation = 0;
807 docstat &= ~(D_EM | D_BF | D_TT);
885 if (docstat & D_DT) {
888 for (i = STRLEN (str); i < 8; i++)
906 docstat &= ~(D_EM | D_BF | D_TT);
913 docstat &= ~(D_EM | D_TT);
920 docstat &= ~(D_BF | D_TT);
926 docstat &= ~(D_BF | D_EM);
955 add_s ("\n.IP\n.DS\n.sp\n.ft CR\n.nf\n");
956 docstat |= D_TAB | D_NL;
961 add_s ("\n.fi\n.ec\n.ft P\n.sp\n");
991 for (; *str; str++) {
994 else if (*str == '\\')
996 else if (!strncmp (str, "``", 2)) {
1000 else if (!strncmp (str, "''", 2)) {
1014 /* SGML based manual */
1026 docstat &= ~(D_EM | D_BF | D_TT);
1033 docstat &= ~(D_EM | D_TT);
1040 docstat &= ~(D_BF | D_TT);
1047 docstat &= ~(D_EM | D_BF);
1076 add_s ("\n<tscreen><verb>\n");
1077 docstat |= D_TAB | D_NL;
1082 add_s ("\n</verb></tscreen>");
1089 add_s ("\n<descrip>\n");
1105 add_s ("</descrip>\n");
1111 if (docstat & D_TAB)
1120 /* make gcc happy (unreached) */
1128 void print_ref (int output_dollar, const char *ref)
1130 switch (OutputFormat) {
1139 add_s ("<ref id=\"");
1141 add_s ("\" name=\"");
1153 static int commit_buff (char *buff, char **d, int docstat)
1157 docstat = print_it (SP_STR, buff, docstat);
1164 static int handle_docline (char *l, int docstat)
1166 char buff[BUFFSIZE];
1172 fprintf (stderr, "%s: handle_docline `%s'\n", Progname, l);
1174 if (!strncmp (l, ".pp", 3))
1175 return print_it (SP_NEWPAR, NULL, docstat);
1176 else if (!strncmp (l, ".ts", 3))
1177 return print_it (SP_START_TAB, NULL, docstat);
1178 else if (!strncmp (l, ".te", 3))
1179 return print_it (SP_END_TAB, NULL, docstat);
1180 else if (!strncmp (l, ".dl", 3))
1181 return print_it (SP_START_DL, NULL, docstat);
1182 else if (!strncmp (l, ".de", 3))
1183 return print_it (SP_END_DL, NULL, docstat);
1184 else if (!strncmp (l, ". ", 2))
1187 for (s = l, d = buff; *s; s++) {
1188 if (!strncmp (s, "\\(as", 4)) {
1192 else if (!strncmp (s, "\\(rs", 4)) {
1196 else if (!strncmp (s, "\\fI", 3)) {
1197 docstat = commit_buff (buff, &d, docstat);
1198 docstat = print_it (SP_START_EM, NULL, docstat);
1201 else if (!strncmp (s, "\\fB", 3)) {
1202 docstat = commit_buff (buff, &d, docstat);
1203 docstat = print_it (SP_START_BF, NULL, docstat);
1206 else if (!strncmp (s, "\\fT", 3)) {
1207 docstat = commit_buff (buff, &d, docstat);
1208 docstat = print_it (SP_START_TT, NULL, docstat);
1211 else if (!strncmp (s, "\\fP", 3)) {
1212 docstat = commit_buff (buff, &d, docstat);
1213 docstat = print_it (SP_END_FT, NULL, docstat);
1216 else if (!strncmp (s, ".dt", 3)) {
1217 docstat = commit_buff (buff, &d, docstat);
1218 docstat = print_it (SP_DT, NULL, docstat);
1221 else if (!strncmp (s, ".dd", 3)) {
1222 docstat = commit_buff (buff, &d, docstat);
1223 docstat = print_it (SP_DD, NULL, docstat);
1226 else if (*s == '$') {
1227 int output_dollar = 0;
1241 while (isalnum ((unsigned char) *s) || *s == '-' || *s == '_')
1244 docstat = commit_buff (buff, &d, docstat);
1247 print_ref (output_dollar, ref);
1256 docstat = commit_buff (buff, &d, docstat);
1257 return print_it (SP_NEWLINE, NULL, docstat);