=================================================================== RCS file: /cvs/texi2mdoc/main.c,v retrieving revision 1.16 retrieving revision 1.20 diff -u -p -r1.16 -r1.20 --- texi2mdoc/main.c 2015/02/19 10:20:31 1.16 +++ texi2mdoc/main.c 2015/02/19 16:15:17 1.20 @@ -1,4 +1,4 @@ -/* $Id: main.c,v 1.16 2015/02/19 10:20:31 kristaps Exp $ */ +/* $Id: main.c,v 1.20 2015/02/19 16:15:17 kristaps Exp $ */ /* * Copyright (c) 2015 Kristaps Dzonsons * @@ -42,6 +42,7 @@ enum texicmd { TEXICMD_ANCHOR, TEXICMD_APPENDIX, TEXICMD_APPENDIXSEC, + TEXICMD_APPENDIXSUBSEC, TEXICMD_ASTERISK, TEXICMD_AT, TEXICMD_AUTHOR, @@ -56,6 +57,7 @@ enum texicmd { TEXICMD_CITE, TEXICMD_CODE, TEXICMD_COLON, + TEXICMD_COLUMNFRACTIONS, TEXICMD_COMMAND, TEXICMD_COMMENT, TEXICMD_COMMENT_LONG, @@ -95,11 +97,17 @@ enum texicmd { TEXICMD_ENV, TEXICMD_ERROR, TEXICMD_EXAMPLE, + TEXICMD_EXPANSION, TEXICMD_FILE, + TEXICMD_FINALOUT, + TEXICMD_FINDEX, + TEXICMD_FTABLE, + TEXICMD_FORMAT, TEXICMD_GRAVE, TEXICMD_GROUP, TEXICMD_HEADING, TEXICMD_HEADINGS, + TEXICMD_HEADITEM, TEXICMD_HYPHEN, TEXICMD_I, TEXICMD_IFCLEAR, @@ -116,16 +124,21 @@ enum texicmd { TEXICMD_IFTEX, TEXICMD_IFSET, TEXICMD_IFXML, + TEXICMD_IGNORE, TEXICMD_IMAGE, TEXICMD_INCLUDE, TEXICMD_INDENTBLOCK, TEXICMD_INSERTCOPYING, TEXICMD_ITEM, TEXICMD_ITEMIZE, + TEXICMD_ITEMX, TEXICMD_KBD, + TEXICMD_KEY, + TEXICMD_KINDEX, TEXICMD_LATEX, TEXICMD_MATH, TEXICMD_MENU, + TEXICMD_MULTITABLE, TEXICMD_NEED, TEXICMD_NEWLINE, TEXICMD_NODE, @@ -151,16 +164,21 @@ enum texicmd { TEXICMD_SLANTED, TEXICMD_SP, TEXICMD_SPACE, + TEXICMD_SMALLBOOK, TEXICMD_SMALLDISPLAY, TEXICMD_SMALLEXAMPLE, + TEXICMD_SMALLFORMAT, TEXICMD_SMALLINDENTBLOCK, TEXICMD_SQUIGGLE_LEFT, TEXICMD_SQUIGGLE_RIGHT, TEXICMD_STRONG, + TEXICMD_SUBHEADING, TEXICMD_SUBSECTION, TEXICMD_SUBTITLE, + TEXICMD_SYNCODEINDEX, TEXICMD_T, TEXICMD_TAB, + TEXICMD_TABSYM, TEXICMD_TABLE, TEXICMD_TEX, TEXICMD_TEXSYM, @@ -172,11 +190,14 @@ enum texicmd { TEXICMD_UMLAUT, TEXICMD_UNNUMBERED, TEXICMD_UNNUMBEREDSEC, + TEXICMD_UNNUMBEREDSUBSEC, TEXICMD_UREF, TEXICMD_URL, TEXICMD_VAR, TEXICMD_VERBATIMINCLUDE, + TEXICMD_VINDEX, TEXICMD_VSKIP, + TEXICMD_VTABLE, TEXICMD_W, TEXICMD_XREF, TEXICMD__MAX @@ -215,6 +236,7 @@ enum texilist { TEXILIST_NONE = 0, TEXILIST_ITEM, TEXILIST_NOITEM, + TEXILIST_TABLE }; /* @@ -266,6 +288,7 @@ static void doitem(struct texi *, enum texicmd, const static void doitemize(struct texi *, enum texicmd, const char *, size_t, size_t *); static void dolink(struct texi *, enum texicmd, const char *, size_t, size_t *); static void domath(struct texi *, enum texicmd, const char *, size_t, size_t *); +static void domultitable(struct texi *, enum texicmd, const char *, size_t, size_t *); static void doquotation(struct texi *, enum texicmd, const char *, size_t, size_t *); static void dotable(struct texi *, enum texicmd, const char *, size_t, size_t *); static void dotop(struct texi *, enum texicmd, const char *, size_t, size_t *); @@ -273,16 +296,19 @@ static void dosection(struct texi *, enum texicmd, con static void dosp(struct texi *, enum texicmd, const char *, size_t, size_t *); static void dosubsection(struct texi *, enum texicmd, const char *, size_t, size_t *); static void dosymbol(struct texi *, enum texicmd, const char *, size_t, size_t *); +static void dotab(struct texi *, enum texicmd, const char *, size_t, size_t *); static void dotitle(struct texi *, enum texicmd, const char *, size_t, size_t *); static void doverbinclude(struct texi *, enum texicmd, const char *, size_t, size_t *); static const struct texitok texitoks[TEXICMD__MAX] = { + /* TEXICMD__BEGIN */ { doignargn, "acronym", 7 }, /* TEXICMD_ACRONYM */ { doaccent, "'", 1 }, /* TEXICMD_ACUTE */ { doignline, "afourpaper", 10 }, /* TEXICMD_A4PAPER */ { doignbracket, "anchor", 6 }, /* TEXICMD_ANCHOR */ { dosection, "appendix", 8 }, /* TEXICMD_APPENDIX */ { dosection, "appendixsec", 11 }, /* TEXICMD_APPENDIXSEC */ + { dosubsection, "appendixsubsec", 14 }, /* TEXICMD_APPENDIXSUBSEC */ { dosymbol, "*", 1 }, /* TEXICMD_ASTERISK */ { dosymbol, "@", 1 }, /* TEXICMD_AT */ { doignline, "author", 6 }, /* TEXICMD_AUTHOR */ @@ -297,6 +323,7 @@ static const struct texitok texitoks[TEXICMD__MAX] = { { dofont, "code", 4 }, /* TEXICMD_CODE */ { dofont, "cite", 4 }, /* TEXICMD_CITE */ { dosymbol, ":", 1 }, /* TEXICMD_COLON */ + { NULL, "columnfractions", 15 }, /* TEXICMD_COLUMNFRACTIONS */ { doinline, "command", 7 }, /* TEXICMD_COMMAND */ { doignline, "c", 1 }, /* TEXICMD_COMMENT */ { doignline, "comment", 7 }, /* TEXICMD_COMMENT_LONG */ @@ -336,11 +363,17 @@ static const struct texitok texitoks[TEXICMD__MAX] = { { doinline, "env", 3 }, /* TEXICMD_ENV */ { dosymbol, "error", 5 }, /* TEXICMD_ERROR */ { doexample, "example", 7 }, /* TEXICMD_EXAMPLE */ + { dosymbol, "expansion", 9 }, /* TEXICMD_EXPANSION */ { doinline, "file", 4 }, /* TEXICMD_FILE */ + { doignline, "finalout", 8 }, /* TEXICMD_FINALOUT */ + { doignline, "findex", 6 }, /* TEXICMD_FINDEX */ + { dotable, "ftable", 6 }, /* TEXICMD_FTABLE */ + { dodisplay, "format", 6 }, /* TEXICMD_FORMAT */ { doaccent, "`", 1 }, /* TEXICMD_GRAVE */ { doblock, "group", 5 }, /* TEXICMD_GROUP */ { dosection, "heading", 7 }, /* TEXICMD_HEADING */ { doignline, "headings", 8 }, /* TEXICMD_HEADINGS */ + { doitem, "headitem", 8 }, /* TEXICMD_HEADITEM */ { dosymbol, "-", 1 }, /* TEXICMD_HYPHEN */ { dofont, "i", 1 }, /* TEXICMD_I */ { doignblock, "ifclear", 7 }, /* TEXICMD_IFCLEAR */ @@ -357,16 +390,21 @@ static const struct texitok texitoks[TEXICMD__MAX] = { { doignblock, "iftex", 5 }, /* TEXICMD_IFTEX */ { doignblock, "ifset", 5 }, /* TEXICMD_IFSET */ { doignblock, "ifxml", 5 }, /* TEXICMD_IFXML */ + { doignblock, "ignore", 6 }, /* TEXICMD_IGNORE */ { doignbracket, "image", 5 }, /* TEXICMD_IMAGE */ { doinclude, "include", 7 }, /* TEXICMD_INCLUDE */ { dodisplay, "indentblock", 11 }, /* TEXICMD_INDENTBLOCK */ { doignline, "insertcopying", 13 }, /* TEXICMD_INSERTCOPYING */ { doitem, "item", 4 }, /* TEXICMD_ITEM */ { doitemize, "itemize", 7 }, /* TEXICMD_ITEMIZE */ + { doitem, "itemx", 5 }, /* TEXICMD_ITEMX */ { dofont, "kbd", 3 }, /* TEXICMD_KBD */ + { dobracket, "key", 3 }, /* TEXICMD_KEY */ + { doignline, "kindex", 6 }, /* TEXICMD_KINDEX */ { dosymbol, "LaTeX", 5 }, /* TEXICMD_LATEX */ { domath, "math", 4 }, /* TEXICMD_MATH */ { doignblock, "menu", 4 }, /* TEXICMD_MENU */ + { domultitable, "multitable", 10 }, /* TEXICMD_MULTITABLE */ { doignline, "need", 4 }, /* TEXICMD_NEED */ { dosymbol, "\n", 1 }, /* TEXICMD_NEWLINE */ { doignline, "node", 4 }, /* TEXICMD_NODE */ @@ -392,16 +430,21 @@ static const struct texitok texitoks[TEXICMD__MAX] = { { dofont, "slanted", 7 }, /* TEXICMD_SLANTED */ { dosp, "sp", 2 }, /* TEXICMD_SP */ { dosymbol, " ", 1 }, /* TEXICMD_SPACE */ + { doignline, "smallbook", 9 }, /* TEXICMD_SMALLBOOK */ { dodisplay, "smalldisplay", 12 }, /* TEXICMD_SMALLDISPLAY */ { doexample, "smallexample", 12 }, /* TEXICMD_SMALLEXAMPLE */ + { dodisplay, "smallformat", 11 }, /* TEXICMD_SMALLFORMAT */ { dodisplay, "smallindentblock", 16 }, /* TEXICMD_SMALLINDENTBLOCK */ { dosymbol, "{", 1 }, /* TEXICMD_SQUIGGLE_LEFT */ { dosymbol, "}", 1 }, /* TEXICMD_SQUIGGLE_RIGHT */ { dofont, "strong", 6 }, /* TEXICMD_STRONG */ + { dosubsection, "subheading", 10 }, /* TEXICMD_SUBHEADING */ { dosubsection, "subsection", 10 }, /* TEXICMD_SUBSECTION */ { doignline, "subtitle", 8 }, /* TEXICMD_SUBTITLE */ + { doignline, "syncodeindex", 12 }, /* TEXICMD_SYNCODEINDEX */ { dofont, "t", 1 }, /* TEXICMD_T */ - { dosymbol, "\t", 1 }, /* TEXICMD_TAB */ + { dotab, "tab", 3 }, /* TEXICMD_TAB */ + { dosymbol, "\t", 1 }, /* TEXICMD_TABSYM */ { dotable, "table", 5 }, /* TEXICMD_TABLE */ { doignblock, "tex", 3 }, /* TEXICMD_TEX */ { dosymbol, "TeX", 3 }, /* TEXICMD_TEXSYM */ @@ -413,13 +456,17 @@ static const struct texitok texitoks[TEXICMD__MAX] = { { doaccent, "\"", 1 }, /* TEXICMD_UMLAUT */ { dosection, "unnumbered", 10 }, /* TEXICMD_UNNUMBERED */ { dosection, "unnumberedsec", 13 }, /* TEXICMD_UNNUMBEREDSEC */ + { dosubsection, "unnumberedsubsec", 16 }, /* TEXICMD_UNNUMBEREDSUBSEC */ { dolink, "uref", 4 }, /* TEXICMD_UREF */ { dolink, "url", 3 }, /* TEXICMD_URL */ { doinline, "var", 3 }, /* TEXICMD_VAR */ { doverbinclude, "verbatiminclude", 15 }, /* TEXICMD_VERBATIMINCLUDE */ + { doignline, "vindex", 6 }, /* TEXICMD_VINDEX */ { dosp, "vskip", 5 }, /* TEXICMD_VSKIP */ + { dotable, "vtable", 6 }, /* TEXICMD_VTABLE */ { dobracket, "w", 1 }, /* TEXICMD_W */ { dolink, "xref", 4 }, /* TEXICMD_XREF */ + /* TEXICMD__END */ }; /* @@ -515,6 +562,7 @@ texierr(struct texi *p, const char *fmt, ...) /* * Put a single data character to the output if we're not ignoring. * Adjusts our output status. + * This shouldn't be called for macros: just for ordinary text. */ static void texiputchar(struct texi *p, char c) @@ -522,6 +570,10 @@ texiputchar(struct texi *p, char c) if (p->ign) return; + + if ('.' == c && 0 == p->outcol) + fputs("\\&", stdout); + putchar(c); p->seenvs = 0; if ('\n' == c) { @@ -533,6 +585,7 @@ texiputchar(struct texi *p, char c) /* * Put multiple characters (see texiputchar()). + * This shouldn't be called for macros: just for ordinary text. */ static void texiputchars(struct texi *p, const char *s) @@ -550,9 +603,13 @@ static void teximacroclose(struct texi *p) { - /* FIXME: punctuation. */ - if (0 == --p->outmacro) - texiputchar(p, '\n'); + if (p->ign) + return; + + if (0 == --p->outmacro) { + putchar('\n'); + p->outcol = p->seenws = 0; + } } /* @@ -563,15 +620,26 @@ teximacroclose(struct texi *p) static void teximacroopen(struct texi *p, const char *s) { + int rc; - if (p->outcol && 0 == p->outmacro) - texiputchar(p, '\n'); + if (p->ign) + return; + + if (p->outcol && 0 == p->outmacro) { + putchar('\n'); + p->outcol = 0; + } + if (0 == p->outmacro) - texiputchar(p, '.'); + putchar('.'); else - texiputchar(p, ' '); - texiputchars(p, s); - texiputchar(p, ' '); + putchar(' '); + + if (EOF != (rc = fputs(s, stdout))) + p->outcol += rc; + + putchar(' '); + p->outcol++; p->outmacro++; p->seenws = 0; } @@ -583,17 +651,20 @@ static void teximacro(struct texi *p, const char *s) { + if (p->ign) + return; + if (p->outmacro) texierr(p, "\"%s\" in open line scope!?", s); - else if (p->literal) + if (p->literal) texierr(p, "\"%s\" in a literal scope!?", s); if (p->outcol) - texiputchar(p, '\n'); + putchar('\n'); - texiputchar(p, '.'); - texiputchars(p, s); - texiputchar(p, '\n'); + putchar('.'); + puts(s); + p->outcol = p->seenws = 0; } static void @@ -729,7 +800,7 @@ texiword(struct texi *p, const char *buf, size_t sz, size_t *pos, char extra) { - if (0 == p->outmacro && p->outcol > 72 && 0 == p->literal) + if (p->seenws && 0 == p->outmacro && p->outcol > 72 && 0 == p->literal) texiputchar(p, '\n'); /* FIXME: abstract this: we use it elsewhere. */ if (p->seenws && p->outcol && 0 == p->literal) @@ -917,11 +988,11 @@ parseeoln(struct texi *p, const char *buf, size_t sz, size_t end; enum texicmd cmd; - assert(0 == p->literal); - while (*pos < sz && '\n' != buf[*pos]) { while (*pos < sz && isws(buf[*pos])) { p->seenws = 1; + if (p->literal) + texiputchar(p, buf[*pos]); advance(p, buf, pos); } switch (buf[*pos]) { @@ -1429,6 +1500,10 @@ dofont(struct texi *p, enum texicmd cmd, abort(); } + if (p->seenws) { + texiputchar(p, ' '); + p->seenws = 0; + } texiputchars(p, font); parsebracket(p, buf, sz, pos); texiputchars(p, "\\fP"); @@ -1447,7 +1522,16 @@ dodisplay(struct texi *p, enum texicmd cmd, const char *buf, size_t sz, size_t *pos) { - teximacro(p, "Bd -filled -offset indent"); + switch (cmd) { + case (TEXICMD_FORMAT): + case (TEXICMD_SMALLFORMAT): + teximacro(p, "Bd -filled"); + break; + default: + teximacro(p, "Bd -filled -offset indent"); + break; + } + p->seenvs = 1; /* FIXME: ignore and parseeoln. */ advanceeoln(p, buf, sz, pos, 1); @@ -1592,7 +1676,7 @@ dosymbol(struct texi *p, enum texicmd cmd, case (TEXICMD_ASTERISK): case (TEXICMD_NEWLINE): case (TEXICMD_SPACE): - case (TEXICMD_TAB): + case (TEXICMD_TABSYM): texiputchar(p, ' '); break; case (TEXICMD_AT): @@ -1613,6 +1697,9 @@ dosymbol(struct texi *p, enum texicmd cmd, case (TEXICMD_ERROR): texiputchars(p, "error\\(->"); break; + case (TEXICMD_EXPANSION): + texiputchars(p, "\\(->"); + break; case (TEXICMD_LATEX): texiputchars(p, "LaTeX"); break; @@ -1806,6 +1893,13 @@ dotop(struct texi *p, enum texicmd cmd, time_t t; char date[32]; + /* + * Here we print our standard mdoc(7) prologue. + * We use the title set with @settitle for the `Nd' description + * and the source document filename (the first one as invoked on + * the command line) for the title. + * The date is set to the current date. + */ t = time(NULL); strftime(date, sizeof(date), "%F", localtime(&t)); @@ -1836,6 +1930,12 @@ doitem(struct texi *p, enum texicmd cmd, const char *buf, size_t sz, size_t *pos) { + /* Multitable is using raw tbl(7). */ + if (TEXILIST_TABLE == p->list) { + texiputchar(p, '\n'); + return; + } + if (p->outmacro) texierr(p, "item in open line scope!?"); else if (p->literal) @@ -1852,8 +1952,9 @@ doitem(struct texi *p, enum texicmd cmd, texivspace(p); break; } - p->seenvs = 1; + /* Trick so we don't start with Pp. */ + p->seenvs = 1; parseeoln(p, buf, sz, pos); if (TEXILIST_ITEM == p->list) @@ -1863,6 +1964,80 @@ doitem(struct texi *p, enum texicmd cmd, } static void +dotab(struct texi *p, enum texicmd cmd, + const char *buf, size_t sz, size_t *pos) +{ + + /* This command is only useful in @multitable. */ + if (TEXILIST_TABLE == p->list) + texiputchar(p, '\t'); +} + +static void +domultitable(struct texi *p, enum texicmd cmd, + const char *buf, size_t sz, size_t *pos) +{ + enum texilist sv = p->list; + enum texicmd type; + size_t i, end, columns; + + p->list = TEXILIST_TABLE; + teximacro(p, "TS"); + columns = 0; + + /* Advance to the first argument... */ + while (*pos < sz && isws(buf[*pos])) + advance(p, buf, pos); + + /* Make sure we don't print anything when scanning. */ + p->ign++; + if ('@' == buf[*pos]) { + /* + * Look for @columnfractions. + * We ignore these, but we do use the number of + * arguments to set the number of columns that we'll + * have. + */ + type = texicmd(p, buf, *pos, sz, &end); + advanceto(p, buf, pos, end); + if (TEXICMD_COLUMNFRACTIONS != type) + texierr(p, "unknown multitable type"); + while (*pos < sz && '\n' != buf[*pos]) { + while (*pos < sz && isws(buf[*pos])) + advance(p, buf, pos); + while (*pos < sz && ! isws(buf[*pos])) { + if ('\n' == buf[*pos]) + break; + advance(p, buf, pos); + } + columns++; + } + } else + /* + * We have arguments. + * We could parse these, but it's easier to just let + * tbl(7) figure it out. + * So use this only to count arguments. + */ + while (parselinearg(p, buf, sz, pos) > 0) + columns++; + p->ign--; + + /* Left-justify each table entry. */ + for (i = 0; i < columns; i++) { + if (i > 0) + texiputchar(p, ' '); + texiputchar(p, 'l'); + } + texiputchars(p, ".\n"); + p->outmacro++; + parseto(p, buf, sz, pos, texitoks[cmd].tok); + p->outmacro--; + teximacro(p, "TE"); + p->list = sv; +} + +static void dotable(struct texi *p, enum texicmd cmd, const char *buf, size_t sz, size_t *pos) { @@ -1873,7 +2048,7 @@ dotable(struct texi *p, enum texicmd cmd, /* FIXME: ignore and parseeoln. */ advanceeoln(p, buf, sz, pos, 1); p->seenvs = 1; - parseto(p, buf, sz, pos, "table"); + parseto(p, buf, sz, pos, texitoks[cmd].tok); teximacro(p, "El"); p->list = sv; }