=================================================================== RCS file: /cvs/texi2mdoc/main.c,v retrieving revision 1.29 retrieving revision 1.30 diff -u -p -r1.29 -r1.30 --- texi2mdoc/main.c 2015/02/21 22:01:32 1.29 +++ texi2mdoc/main.c 2015/02/23 11:44:30 1.30 @@ -1,4 +1,4 @@ -/* $Id: main.c,v 1.29 2015/02/21 22:01:32 kristaps Exp $ */ +/* $Id: main.c,v 1.30 2015/02/23 11:44:30 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 *); @@ -172,13 +173,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 */ @@ -188,6 +189,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 */ @@ -426,6 +428,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); + 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) { @@ -584,7 +645,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"); @@ -643,7 +704,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"); @@ -912,7 +973,6 @@ dosymbol(struct texi *p, enum texicmd cmd, case (TEXICMD_HYPHEN): break; default: - texiwarn(p, "sym: %d", cmd); abort(); } @@ -1026,6 +1086,11 @@ dovalue(struct texi *p, enum texicmd cmd, 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); } @@ -1306,7 +1371,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");