=================================================================== RCS file: /cvs/texi2mdoc/main.c,v retrieving revision 1.26 retrieving revision 1.28 diff -u -p -r1.26 -r1.28 --- texi2mdoc/main.c 2015/02/20 15:37:33 1.26 +++ texi2mdoc/main.c 2015/02/21 21:49:18 1.28 @@ -1,4 +1,4 @@ -/* $Id: main.c,v 1.26 2015/02/20 15:37:33 kristaps Exp $ */ +/* $Id: main.c,v 1.28 2015/02/21 21:49:18 kristaps Exp $ */ /* * Copyright (c) 2015 Kristaps Dzonsons * @@ -44,6 +44,7 @@ static void doaccent(struct texi *, enum texicmd, cons static void doblock(struct texi *, enum texicmd, const char *, size_t, size_t *); static void dobracket(struct texi *, enum texicmd, const char *, size_t, size_t *); static void dobye(struct texi *, enum texicmd, const char *, size_t, size_t *); +static void dodefindex(struct texi *, enum texicmd, const char *, size_t, size_t *); static void dodefn(struct texi *, enum texicmd, const char *, size_t, size_t *); static void dodisplay(struct texi *, enum texicmd, const char *, size_t, size_t *); static void doenumerate(struct texi *, enum texicmd, const char *, size_t, size_t *); @@ -107,8 +108,10 @@ static const struct texitok __texitoks[TEXICMD__MAX] = { doignline, "contents", 8 }, /* TEXICMD_CONTENTS */ { doignblock, "copying", 7 }, /* TEXICMD_COPYING */ { dosymbol, "copyright", 9 }, /* TEXICMD_COPYRIGHT */ + { dodefindex, "defcodeindex", 12 }, /* TEXICMD_DEFCODEINDEX */ { dodefn, "deffn", 5 }, /* TEXICMD_DEFFN */ { dodefn, "deffnx", 6 }, /* TEXICMD_DEFFNX */ + { dodefindex, "defindex", 8 }, /* TEXICMD_DEFINDEX */ { dodefn, "defmac", 6 }, /* TEXICMD_DEFMAC */ { dodefn, "defmacx", 7 }, /* TEXICMD_DEFMACX */ { dodefn, "deftp", 5 }, /* TEXICMD_DEFTP */ @@ -132,12 +135,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 */ @@ -173,6 +178,7 @@ static const struct texitok __texitoks[TEXICMD__MAX] = { doignbracket, "image", 5 }, /* TEXICMD_IMAGE */ { doinclude, "include", 7 }, /* TEXICMD_INCLUDE */ { dodisplay, "indentblock", 11 }, /* TEXICMD_INDENTBLOCK */ + { doignline, "", 0 }, /* TEXICMD_INDEX */ { doignline, "insertcopying", 13 }, /* TEXICMD_INSERTCOPYING */ { doitem, "item", 4 }, /* TEXICMD_ITEM */ { doitemize, "itemize", 7 }, /* TEXICMD_ITEMIZE */ @@ -197,10 +203,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 */ @@ -224,9 +232,11 @@ 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 */ + { doignline, "synindex", 8 }, /* TEXICMD_SYNINDEX */ { doignline, "syncodeindex", 12 }, /* TEXICMD_SYNCODEINDEX */ { doinline, "t", 1 }, /* TEXICMD_T */ { dotab, "tab", 3 }, /* TEXICMD_TAB */ @@ -262,6 +272,37 @@ static const struct texitok __texitoks[TEXICMD__MAX] = const struct texitok *const texitoks = __texitoks; static void +dodefindex(struct texi *p, enum texicmd cmd, + const char *buf, size_t sz, size_t *pos) +{ + size_t start, end; + char *cp; + + while (*pos < sz && isws(buf[*pos])) + advance(p, buf, pos); + + start = end = *pos; + while (end < sz && ! ismspace(buf[end])) + end++; + + if (start == end) { + advanceeoln(p, buf, sz, pos, 1); + return; + } else if (NULL == (cp = malloc(end - start + 1))) + texiabort(p, NULL); + + memcpy(cp, &buf[start], end - start); + cp[end - start] = '\0'; + + p->indexs = realloc(p->indexs, + sizeof(char *) * (p->indexsz + 1)); + + if (NULL == p->indexs) + texiabort(p, NULL); + p->indexs[p->indexsz++] = cp; +} + +static void dodefn(struct texi *p, enum texicmd cmd, const char *buf, size_t sz, size_t *pos) { @@ -713,6 +754,8 @@ dotitle(struct texi *p, enum texicmd cmd, advanceeoln(p, buf, sz, pos, 1); free(p->subtitle); p->subtitle = malloc(end - start + 1); + if (NULL == p->subtitle) + texiabort(p, NULL); memcpy(p->subtitle, &buf[start], end - start); p->subtitle[end - start] = '\0'; } @@ -832,6 +875,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; @@ -923,7 +969,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; @@ -939,10 +985,8 @@ dovalue(struct texi *p, enum texicmd cmd, advanceto(p, buf, pos, end); key = malloc(end - start + 1); - if (NULL == key) { - perror(NULL); - exit(EXIT_FAILURE); - } + if (NULL == key) + texiabort(p, NULL); memcpy(key, &buf[start], end - start); key[end - start] = '\0'; @@ -955,10 +999,8 @@ dovalue(struct texi *p, enum texicmd cmd, advanceeoln(p, buf, sz, pos, 1); val = malloc(end - start + 1); - if (NULL == val) { - perror(NULL); - exit(EXIT_FAILURE); - } + if (NULL == val) + texiabort(p, NULL); memcpy(val, &buf[start], end - start); val[end - start] = '\0'; valueadd(p, key, val); @@ -966,10 +1008,17 @@ 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"); + p->valstack[p->valstackpos++] = cp; + parseeof(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); @@ -1368,7 +1417,7 @@ doignline(struct texi *p, enum texicmd cmd, * This does NOT sanitise the directories! */ static char ** -parsedirs(const char *base, const char *cp, size_t *sz) +parsedirs(struct texi *p, const char *base, const char *cp, size_t *sz) { char *tok, *str, *tofree; const char *cpp; @@ -1380,28 +1429,20 @@ parsedirs(const char *base, const char *cp, size_t *sz for ( ; NULL != (cpp = strchr(cpp, ':')); (*sz)++) cpp++; - dirs = calloc(*sz, sizeof(char *)); - if (NULL == dirs) { - perror(NULL); - exit(EXIT_FAILURE); - } else if (NULL == (dirs[0] = strdup(base))) { - perror(NULL); - exit(EXIT_FAILURE); - } + if (NULL == (dirs = calloc(*sz, sizeof(char *)))) + texiabort(p, NULL); + else if (NULL == (dirs[0] = strdup(base))) + texiabort(p, NULL); if (NULL == cp) return(dirs); - if (NULL == (tofree = tok = str = strdup(cp))) { - perror(NULL); - exit(EXIT_FAILURE); - } + if (NULL == (tofree = tok = str = strdup(cp))) + texiabort(p, NULL); for (i = 1; NULL != (tok = strsep(&str, ":")); i++) - if (NULL == (dirs[i] = strdup(tok))) { - perror(NULL); - exit(EXIT_FAILURE); - } + if (NULL == (dirs[i] = strdup(tok))) + texiabort(p, NULL); free(tofree); return(dirs); @@ -1437,14 +1478,11 @@ main(int argc, char *argv[]) if (0 == (argc -= optind)) goto usage; - if (NULL == (path = strdup(argv[0]))) { - perror(NULL); - exit(EXIT_FAILURE); - } else if (NULL == (dir = dirname(path))) { - perror(argv[0]); - free(path); - exit(EXIT_FAILURE); - } + if (NULL == (path = strdup(argv[0]))) + texiabort(&texi, NULL); + else if (NULL == (dir = dirname(path))) + texiabort(&texi, NULL); + free(path); if (NULL != (cp = strrchr(argv[0], '/'))) @@ -1452,14 +1490,13 @@ main(int argc, char *argv[]) else texi.title = strdup(argv[0]); - if (NULL == texi.title) { - perror(NULL); - exit(EXIT_FAILURE); - } else if (NULL != (path = strchr(texi.title, '.'))) + if (NULL == texi.title) + texiabort(&texi, NULL); + else if (NULL != (path = strchr(texi.title, '.'))) *path = '\0'; texi.ign = 1; - texi.dirs = parsedirs(dir, Idir, &texi.dirsz); + texi.dirs = parsedirs(&texi, dir, Idir, &texi.dirsz); parsefile(&texi, argv[0], 1); /* We shouldn't get here. */ texiexit(&texi);