=================================================================== RCS file: /cvs/texi2mdoc/main.c,v retrieving revision 1.27 retrieving revision 1.32 diff -u -p -r1.27 -r1.32 --- texi2mdoc/main.c 2015/02/21 17:00:33 1.27 +++ texi2mdoc/main.c 2015/02/23 12:28:20 1.32 @@ -1,4 +1,4 @@ -/* $Id: main.c,v 1.27 2015/02/21 17:00:33 kristaps Exp $ */ +/* $Id: main.c,v 1.32 2015/02/23 12:28:20 kristaps Exp $ */ /* * Copyright (c) 2015 Kristaps Dzonsons * @@ -58,6 +58,7 @@ static void doinclude(struct texi *, enum texicmd, con static void doitem(struct texi *, enum texicmd, const char *, size_t, size_t *); 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 domacro(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 *); @@ -135,12 +136,14 @@ static const struct texitok __texitoks[TEXICMD__MAX] = { doignline, "dircategory", 11 }, /* TEXICMD_DIRCATEGORY */ { doignblock, "direntry", 8 }, /* TEXICMD_DIRENTRY */ { dodisplay, "display", 7 }, /* TEXICMD_DISPLAY */ + { doignbracket, "dmn", 3 }, /* TEXICMD_DMN */ { dosymbol, "dots", 4 }, /* TEXICMD_DOTS */ { dolink, "email", 5 }, /* TEXICMD_EMAIL */ { doinline, "emph", 4 }, /* TEXICMD_EMPH */ { NULL, "end", 3 }, /* TEXICMD_END */ { doenumerate, "enumerate", 9 }, /* TEXICMD_ENUMERATE */ { doinline, "env", 3 }, /* TEXICMD_ENV */ + { dosymbol, "equiv", 5 }, /* TEXICMD_EQUIV */ { dosymbol, "error", 5 }, /* TEXICMD_ERROR */ { doexample, "example", 7 }, /* TEXICMD_EXAMPLE */ { doignline, "exdent", 6 }, /* TEXICMD_EXDENT */ @@ -148,6 +151,7 @@ static const struct texitok __texitoks[TEXICMD__MAX] = { doinline, "file", 4 }, /* TEXICMD_FILE */ { doignline, "finalout", 8 }, /* TEXICMD_FINALOUT */ { doignline, "findex", 6 }, /* TEXICMD_FINDEX */ + { doignbracket, "footnote", 8 }, /* TEXICMD_FOOTNOTE */ { dotable, "ftable", 6 }, /* TEXICMD_FTABLE */ { dodisplay, "format", 6 }, /* TEXICMD_FORMAT */ { doaccent, "`", 1 }, /* TEXICMD_GRAVE */ @@ -170,13 +174,13 @@ static const struct texitok __texitoks[TEXICMD__MAX] = { doblock, "ifnotxml", 8 }, /* TEXICMD_IFNOTXML */ { doblock, "ifplaintext", 11 }, /* TEXICMD_IFPLAINTEXT */ { doignblock, "iftex", 5 }, /* TEXICMD_IFTEX */ - { doignblock, "ifset", 5 }, /* TEXICMD_IFSET */ + { dovalue, "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, "", 0 }, /* TEXICMD_INDEX */ + { doignline, "", 0 }, /* TEXICMD_USER_INDEX */ { doignline, "insertcopying", 13 }, /* TEXICMD_INSERTCOPYING */ { doitem, "item", 4 }, /* TEXICMD_ITEM */ { doitemize, "itemize", 7 }, /* TEXICMD_ITEMIZE */ @@ -186,6 +190,7 @@ static const struct texitok __texitoks[TEXICMD__MAX] = { doignline, "kindex", 6 }, /* TEXICMD_KINDEX */ { dosymbol, "LaTeX", 5 }, /* TEXICMD_LATEX */ { dosecoffs, "lowersections", 13 }, /* TEXICMD_LOWERSECTIONS */ + { domacro, "macro", 5 }, /* TEXICMD_MACRO */ { domath, "math", 4 }, /* TEXICMD_MATH */ { doignblock, "menu", 4 }, /* TEXICMD_MENU */ { dosymbol, "minus", 5 }, /* TEXICMD_MINUS */ @@ -201,10 +206,12 @@ static const struct texitok __texitoks[TEXICMD__MAX] = { doignline, "page", 4 }, /* TEXICMD_PAGE */ { doignline, "paragraphindent", 15 }, /* TEXICMD_PARINDENT */ { dosymbol, ".", 1 }, /* TEXICMD_PERIOD */ + { doignline, "pindex", 6 }, /* TEXICMD_PINDEX */ { doignline, "printindex", 10 }, /* TEXICMD_PRINTINDEX */ { doinline, "r", 1 }, /* TEXICMD_R */ { dosecoffs, "raisesections", 13 }, /* TEXICMD_RAISESECTIONS */ { dobracket, "ref", 3 }, /* TEXICMD_REF */ + { doignline, "refill", 6 }, /* TEXICMD_REFILL */ { dosymbol, "result", 6 }, /* TEXICMD_RESULT */ { doinline, "samp", 4 }, /* TEXICMD_SAMP */ { doinline, "sansserif", 9 }, /* TEXICMD_SANSSERIF */ @@ -228,6 +235,7 @@ static const struct texitok __texitoks[TEXICMD__MAX] = { doinline, "strong", 6 }, /* TEXICMD_STRONG */ { dosubsection, "subheading", 10 }, /* TEXICMD_SUBHEADING */ { dosubsection, "subsection", 10 }, /* TEXICMD_SUBSECTION */ + { dosubsubsection, "subsubheading", 13 }, /* TEXICMD_SUBSUBHEADING */ { dosubsubsection, "subsubsection", 13 }, /* TEXICMD_SUBSUBSECTION */ { doignline, "subtitle", 8 }, /* TEXICMD_SUBTITLE */ { doignline, "summarycontents", 15 }, /* TEXICMD_SUMMARYCONTENTS */ @@ -421,6 +429,65 @@ dodefn(struct texi *p, enum texicmd cmd, } static void +domacro(struct texi *p, enum texicmd cmd, + const char *buf, size_t sz, size_t *pos) +{ + size_t start, end, endtoksz, len; + struct teximacro m; + const char *endtok, *blk; + + memset(&m, 0, sizeof(struct teximacro)); + + while (*pos < sz && isws(buf[*pos])) + advance(p, buf, pos); + + for (start = end = *pos; end < sz; end++) + if (ismspace(buf[end]) || '{' == buf[end]) + break; + + if (start == end) + texierr(p, "zero-length macro name"); + + advanceto(p, buf, pos, end); + + m.key = malloc(end - start + 1); + if (NULL == m.key) + texiabort(p, NULL); + memcpy(m.key, &buf[start], end - start); + m.key[end - start] = '\0'; + + m.args = argparse(p, buf, sz, pos, &m.argsz, 0); + advanceeoln(p, buf, sz, pos, 0); + + start = *pos; + endtok = "\n@end macro\n"; + endtoksz = strlen(endtok); + blk = memmem(&buf[start], sz, endtok, endtoksz); + if (NULL == blk) + texierr(p, "unterminated macro body"); + while (&buf[*pos] != blk) + advance(p, buf, pos); + assert('\n' == buf[*pos]); + advance(p, buf, pos); + len = blk - &buf[start]; + m.value = malloc(len + 1); + if (NULL == m.value) + texiabort(p, NULL); + memcpy(m.value, &buf[start], len); + m.value[len] = '\0'; + + p->macros = realloc + (p->macros, + (p->macrosz + 1) * + sizeof(struct teximacro)); + if (NULL == p->macros) + texiabort(p, NULL); + + p->macros[p->macrosz++] = m; + advanceeoln(p, buf, sz, pos, 1); +} + +static void doignblock(struct texi *p, enum texicmd cmd, const char *buf, size_t sz, size_t *pos) { @@ -579,7 +646,7 @@ doverbinclude(struct texi *p, enum texicmd cmd, advance(p, buf, pos); continue; } - type = texicmd(p, buf, *pos, sz, &end); + type = texicmd(p, buf, *pos, sz, &end, NULL); advanceto(p, buf, pos, end); if (TEXICMD_VALUE != type) texierr(p, "unknown verbatiminclude command"); @@ -638,7 +705,7 @@ doinclude(struct texi *p, enum texicmd cmd, advance(p, buf, pos); continue; } - type = texicmd(p, buf, *pos, sz, &end); + type = texicmd(p, buf, *pos, sz, &end, NULL); advanceto(p, buf, pos, end); if (TEXICMD_VALUE != type) texierr(p, "unknown include command"); @@ -870,6 +937,9 @@ dosymbol(struct texi *p, enum texicmd cmd, case (TEXICMD_DOTS): texiputchars(p, "..."); break; + case (TEXICMD_EQUIV): + texiputchars(p, "\\(=="); + break; case (TEXICMD_ERROR): texiputchars(p, "error\\(->"); break; @@ -904,7 +974,6 @@ dosymbol(struct texi *p, enum texicmd cmd, case (TEXICMD_HYPHEN): break; default: - texiwarn(p, "sym: %d", cmd); abort(); } @@ -961,7 +1030,7 @@ static void dovalue(struct texi *p, enum texicmd cmd, const char *buf, size_t sz, size_t *pos) { - size_t start, end; + size_t start, end, i; char *key, *val; const char *cp; @@ -1000,15 +1069,29 @@ dovalue(struct texi *p, enum texicmd cmd, if (p->seenws) texiputchar(p, ' '); p->seenws = 0; - if (NULL == (cp = valueblookup(p, buf, sz, pos))) + if (NULL != (cp = valueblookup(p, buf, sz, pos))) { + for (i = 0; i < p->valstackpos; i++) + if (cp == p->valstack[i]) + break; + if (i < p->valstackpos) + texierr(p, "recursive value"); + if (64 == p->valstackpos) + texierr(p, "too many nested values"); + p->valstack[p->valstackpos++] = cp; + parsemembuf(p, cp, strlen(cp)); + p->valstackpos--; + } else texiputchars(p, "{No value}"); - else - texiputchars(p, cp); } else if (TEXICMD_IFCLEAR == cmd) { if (NULL != valuellookup(p, buf, sz, pos)) doignblock(p, cmd, buf, sz, pos); else parseto(p, buf, sz, pos, texitoks[cmd].tok); + } else if (TEXICMD_IFSET == cmd) { + if (NULL == valuellookup(p, buf, sz, pos)) + doignblock(p, cmd, buf, sz, pos); + else + parseto(p, buf, sz, pos, texitoks[cmd].tok); } else if (TEXICMD_CLEAR == cmd) valuelclear(p, buf, sz, pos); } @@ -1269,10 +1352,16 @@ domultitable(struct texi *p, enum texicmd cmd, const char *buf, size_t sz, size_t *pos) { enum texilist sv = p->list; + int svliteral = p->literal; enum texicmd type; size_t i, end, columns; p->list = TEXILIST_TABLE; + /* + * TS/TE blocks aren't "in mdoc(7)", so we can disregard the + * fact that we're in literal mode right now. + */ + p->literal = 0; teximacro(p, "TS"); columns = 0; @@ -1289,7 +1378,7 @@ domultitable(struct texi *p, enum texicmd cmd, * arguments to set the number of columns that we'll * have. */ - type = texicmd(p, buf, *pos, sz, &end); + type = texicmd(p, buf, *pos, sz, &end, NULL); advanceto(p, buf, pos, end); if (TEXICMD_COLUMNFRACTIONS != type) texierr(p, "unknown multitable command"); @@ -1325,6 +1414,7 @@ domultitable(struct texi *p, enum texicmd cmd, parseto(p, buf, sz, pos, texitoks[cmd].tok); p->outmacro--; teximacro(p, "TE"); + p->literal = svliteral; p->list = sv; }