=================================================================== RCS file: /cvs/texi2mdoc/main.c,v retrieving revision 1.56 retrieving revision 1.63 diff -u -p -r1.56 -r1.63 --- texi2mdoc/main.c 2015/03/01 13:39:51 1.56 +++ texi2mdoc/main.c 2015/03/05 15:18:13 1.63 @@ -1,4 +1,4 @@ -/* $Id: main.c,v 1.56 2015/03/01 13:39:51 kristaps Exp $ */ +/* $Id: main.c,v 1.63 2015/03/05 15:18:13 kristaps Exp $ */ /* * Copyright (c) 2015 Kristaps Dzonsons * @@ -42,6 +42,7 @@ static void doaccent(struct texi *, enum texicmd, size static void doblock(struct texi *, enum texicmd, size_t *); static void dobracket(struct texi *, enum texicmd, size_t *); static void dobye(struct texi *, enum texicmd, size_t *); +static void docopying(struct texi *, enum texicmd, size_t *); static void dodefindex(struct texi *, enum texicmd, size_t *); static void dodefn(struct texi *, enum texicmd, size_t *); static void dodisplay(struct texi *, enum texicmd, size_t *); @@ -54,6 +55,7 @@ static void doignbracket(struct texi *, enum texicmd, static void doignline(struct texi *, enum texicmd, size_t *); static void doinline(struct texi *, enum texicmd, size_t *); static void doinclude(struct texi *, enum texicmd, size_t *); +static void doinsertcopying(struct texi *, enum texicmd, size_t *); static void doitem(struct texi *, enum texicmd, size_t *); static void doitemize(struct texi *, enum texicmd, size_t *); static void dolink(struct texi *, enum texicmd, size_t *); @@ -94,7 +96,8 @@ static const struct texitok __texitoks[TEXICMD__MAX] = { dosymbol, "*", 1 }, /* TEXICMD_ASTERISK */ { dosymbol, "@", 1 }, /* TEXICMD_AT */ { doignline, "author", 6 }, /* TEXICMD_AUTHOR */ - { doinline, "b", 1 }, /* TEXICMD_BOLD */ + { doinline, "b", 1 }, /* TEXICMD_B */ + { dosymbol, "\\", 1 }, /* TEXICMD_BACKSLASH */ { dosymbol, "!", 1 }, /* TEXICMD_BANG */ { dosymbol, "bullet", 6 }, /* TEXICMD_BULLET */ { dobye, "bye", 3 }, /* TEXICMD_BYE */ @@ -114,7 +117,7 @@ static const struct texitok __texitoks[TEXICMD__MAX] = { doignline, "c", 1 }, /* TEXICMD_COMMENT */ { doignline, "comment", 7 }, /* TEXICMD_COMMENT_LONG */ { doignline, "contents", 8 }, /* TEXICMD_CONTENTS */ - { doignblock, "copying", 7 }, /* TEXICMD_COPYING */ + { docopying, "copying", 7 }, /* TEXICMD_COPYING */ { dosymbol, "copyright", 9 }, /* TEXICMD_COPYRIGHT */ { dodefindex, "defcodeindex", 12 }, /* TEXICMD_DEFCODEINDEX */ { dodefn, "deffn", 5 }, /* TEXICMD_DEFFN */ @@ -213,7 +216,7 @@ static const struct texitok __texitoks[TEXICMD__MAX] = { dodisplay, "indentblock", 11 }, /* TEXICMD_INDENTBLOCK */ { dolink, "indicateurl", 11 }, /* TEXICMD_INDICATEURL */ { dolink, "inforef", 7 }, /* TEXICMD_INFOREF */ - { doignline, "insertcopying", 13 }, /* TEXICMD_INSERTCOPYING */ + { doinsertcopying, "insertcopying", 13 }, /* TEXICMD_INSERTCOPYING */ { doitem, "item", 4 }, /* TEXICMD_ITEM */ { doitemize, "itemize", 7 }, /* TEXICMD_ITEMIZE */ { doitem, "itemx", 5 }, /* TEXICMD_ITEMX */ @@ -268,7 +271,7 @@ static const struct texitok __texitoks[TEXICMD__MAX] = { doaccent, "ringaccent", 10 }, /* TEXICMD_RINGACCENT */ { doinline, "samp", 4 }, /* TEXICMD_SAMP */ { doinline, "sansserif", 9 }, /* TEXICMD_SANSSERIF */ - { dobracket, "sc", 2 }, /* TEXICMD_SC */ + { doinline, "sc", 2 }, /* TEXICMD_SC */ { dosection, "section", 7 }, /* TEXICMD_SECTION */ { dovalue, "set", 3 }, /* TEXICMD_SET */ { doignline, "setchapternewpage", 17 }, /* TEXICMD_SETCHAPNEWPAGE */ @@ -341,6 +344,12 @@ static const struct texitok __texitoks[TEXICMD__MAX] = const struct texitok *const texitoks = __texitoks; +/* + * Texinfo has lots of indexes. + * You can add new ones in a variety of ways. + * We maintain an array of all of these index names (usually a few + * letters) and pass unknown commands through the array list. + */ static void dodefindex(struct texi *p, enum texicmd cmd, size_t *pos) { @@ -349,7 +358,6 @@ dodefindex(struct texi *p, enum texicmd cmd, size_t *p while (*pos < BUFSZ(p) && isws(BUF(p)[*pos])) advance(p, pos); - start = end = *pos; while (end < BUFSZ(p) && ! ismspace(BUF(p)[end])) end++; @@ -357,18 +365,21 @@ dodefindex(struct texi *p, enum texicmd cmd, size_t *p if (start == end) { advanceeoln(p, pos, 1); return; - } else if (NULL == (cp = malloc(end - start + 1))) + } + + if (NULL == (cp = malloc(end - start + 1))) texiabort(p, NULL); - memcpy(cp, &BUF(p)[start], end - start); cp[end - start] = '\0'; + /* FIXME: use reallocarray(). */ p->indexs = realloc(p->indexs, sizeof(char *) * (p->indexsz + 1)); - if (NULL == p->indexs) texiabort(p, NULL); p->indexs[p->indexsz++] = cp; + + advanceeoln(p, pos, 1); } static void @@ -533,9 +544,9 @@ dodefn(struct texi *p, enum texicmd cmd, size_t *pos) break; } teximacro(p, "Bd -filled -offset indent"); - p->seenvs = 1; parseto(p, pos, blk); teximacro(p, "Ed"); + p->seenvs = 1; } static void @@ -725,13 +736,34 @@ doinline(struct texi *p, enum texicmd cmd, size_t *pos } if (NULL == macro || p->literal || TEXILIST_TABLE == p->list) { + if (TEXICMD_SC == cmd) + p->uppercase++; parsebracket(p, pos, 0); + if (TEXICMD_SC == cmd) + p->uppercase--; return; } + /* + * If we haven't seen any whitespace, then we don't want the + * subsequent macro to insert any whitespace. + */ + if (p->outmacro && 0 == p->seenws) { + teximacroopen(p, "Ns"); + teximacroclose(p); + } + teximacroopen(p, macro); p->seenws = 0; + if (TEXICMD_CODE == cmd) + p->literal++; + if (TEXICMD_SC == cmd) + p->uppercase++; parsebracket(p, pos, 0); + if (TEXICMD_SC == cmd) + p->uppercase--; + if (TEXICMD_CODE == cmd) + p->literal--; texipunctuate(p, pos); teximacroclose(p); } @@ -775,6 +807,53 @@ doverb(struct texi *p, enum texicmd cmd, size_t *pos) } static void +doinsertcopying(struct texi *p, enum texicmd cmd, size_t *pos) +{ + + advanceeoln(p, pos, 0); + if (NULL == p->copying) + return; + texisplice(p, p->copying, p->copyingsz, *pos); +} + +static void +docopying(struct texi *p, enum texicmd cmd, size_t *pos) +{ + const char *end, *term; + size_t endsz, endpos; + + /* We retain our starting (but not ending) newlines. */ + end = "\n@end copying\n"; + endsz = strlen(end); + advanceeoln(p, pos, 0); + if (*pos == BUFSZ(p)) { + texiwarn(p, "unterminated \"%s\"", texitoks[cmd].tok); + return; + } + + term = memmem(&BUF(p)[*pos], BUFSZ(p) - *pos, end, endsz); + if (NULL == term) { + texiwarn(p, "unterminated \"%s\"", texitoks[cmd].tok); + endpos = BUFSZ(p); + } else + endpos = *pos + (size_t)(term - &BUF(p)[*pos]); + + assert(endpos <= BUFSZ(p)); + assert('\n' == BUF(p)[*pos]); + advance(p, pos); + + p->copying = malloc(endpos - *pos + 1); + p->copyingsz = endpos - *pos; + memcpy(p->copying, &BUF(p)[*pos], p->copyingsz); + p->copying[endpos - *pos] = '\0'; + + while (*pos < endpos) + advance(p, pos); + if (*pos < BUFSZ(p)) + advanceto(p, pos, endpos + endsz); +} + +static void doverbatim(struct texi *p, enum texicmd cmd, size_t *pos) { const char *end, *term; @@ -785,13 +864,13 @@ doverbatim(struct texi *p, enum texicmd cmd, size_t *p endsz = strlen(end); advanceeoln(p, pos, 0); if (*pos == BUFSZ(p)) { - texiwarn(p, "unexpected end of file"); + texiwarn(p, "unterminated \"%s\"", texitoks[cmd].tok); return; } term = memmem(&BUF(p)[*pos], BUFSZ(p) - *pos, end, endsz); if (NULL == term) { - texiwarn(p, "unterminated verbatim block"); + texiwarn(p, "unterminated \"%s\"", texitoks[cmd].tok); endpos = BUFSZ(p); } else endpos = *pos + (size_t)(term - &BUF(p)[*pos]); @@ -805,6 +884,7 @@ doverbatim(struct texi *p, enum texicmd cmd, size_t *p advance(p, pos); } teximacro(p, "Ed"); + p->seenvs = 1; if (*pos < BUFSZ(p)) advanceto(p, pos, endpos + endsz); } @@ -942,6 +1022,8 @@ static void dodisplay(struct texi *p, enum texicmd cmd, size_t *pos) { + advanceeoln(p, pos, 1); + switch (cmd) { case (TEXICMD_FORMAT): case (TEXICMD_SMALLFORMAT): @@ -952,24 +1034,23 @@ dodisplay(struct texi *p, enum texicmd cmd, size_t *po break; } - p->seenvs = 1; - /* FIXME: ignore and parseeoln. */ - advanceeoln(p, pos, 1); parseto(p, pos, texitoks[cmd].tok); teximacro(p, "Ed"); + p->seenvs = 1; } static void doexample(struct texi *p, enum texicmd cmd, size_t *pos) { - teximacro(p, "Bd -literal -offset indent"); - /* FIXME: ignore and parseeoln. */ advanceeoln(p, pos, 1); + + teximacro(p, "Bd -literal -offset indent"); p->literal++; parseto(p, pos, texitoks[cmd].tok); p->literal--; teximacro(p, "Ed"); + p->seenvs = 1; } static void @@ -983,22 +1064,27 @@ dobye(struct texi *p, enum texicmd cmd, size_t *pos) static void dotitle(struct texi *p, enum texicmd cmd, size_t *pos) { - size_t start, end; + size_t start; while (*pos < BUFSZ(p) && isws(BUF(p)[*pos])) advance(p, pos); - start = end = *pos; - while (end < BUFSZ(p) && '\n' != BUF(p)[end]) - end++; - if (end < BUFSZ(p)) - end++; - advanceeoln(p, pos, 1); + + /* We want to suck down the entire line, inclusive \n. */ + start = *pos; + while (*pos < BUFSZ(p) && '\n' != BUF(p)[*pos]) { + if ('@' == BUF(p)[*pos]) + advance(p, pos); + advance(p, pos); + } + if (*pos < BUFSZ(p)) + advance(p, pos); + + /* Copy this into a buffer. */ free(p->subtitle); - p->subtitle = malloc(end - start + 1); - if (NULL == p->subtitle) + if (NULL == (p->subtitle = malloc(*pos - start + 1))) texiabort(p, NULL); - memcpy(p->subtitle, &BUF(p)[start], end - start); - p->subtitle[end - start] = '\0'; + memcpy(p->subtitle, &BUF(p)[start], *pos - start); + p->subtitle[*pos - start] = '\0'; } static void @@ -1191,6 +1277,9 @@ dosymbol(struct texi *p, enum texicmd cmd, size_t *pos case (TEXICMD_AT): texiputchar(p, '@'); break; + case (TEXICMD_BACKSLASH): + texiputchar(p, '\\'); + break; case (TEXICMD_BANG): texiputchar(p, '!'); break; @@ -1585,7 +1674,6 @@ dosection(struct texi *p, enum texicmd cmd, size_t *po teximacroopen(p, sects[sec]); parseeoln(p, pos); teximacroclose(p); - p->seenvs = 1; } static void @@ -1604,12 +1692,11 @@ static void dosp(struct texi *p, enum texicmd cmd, size_t *pos) { + advanceeoln(p, pos, 1); if (p->literal) texiputchar(p, '\n'); else texivspace(p); - /* FIXME: ignore and parseeoln. */ - advanceeoln(p, pos, 1); } static void @@ -1640,7 +1727,6 @@ doitem(struct texi *p, enum texicmd cmd, size_t *pos) } /* Trick so we don't start with Pp. */ - p->seenvs = 1; parseeoln(p, pos); if (TEXILIST_ITEM == p->list) @@ -1681,7 +1767,7 @@ domultitable(struct texi *p, enum texicmd cmd, size_t /* Make sure we don't print anything when scanning. */ p->ign++; - if ('@' == BUF(p)[*pos]) { + if (*pos < BUFSZ(p) && '@' == BUF(p)[*pos]) { /* * Look for @columnfractions. * We ignore these, but we do use the number of @@ -1733,13 +1819,13 @@ dotable(struct texi *p, enum texicmd cmd, size_t *pos) { enum texilist sv = p->list; + advanceeoln(p, pos, 1); + p->list = TEXILIST_ITEM; teximacro(p, "Bl -tag -width Ds"); - /* FIXME: ignore and parseeoln. */ - advanceeoln(p, pos, 1); - p->seenvs = 1; parseto(p, pos, texitoks[cmd].tok); teximacro(p, "El"); + p->seenvs = 1; p->list = sv; } @@ -1764,13 +1850,13 @@ doenumerate(struct texi *p, enum texicmd cmd, size_t * { enum texilist sv = p->list; + advanceeoln(p, pos, 1); + p->list = TEXILIST_NOITEM; teximacro(p, "Bl -enum"); - p->seenvs = 1; - /* FIXME: ignore and parseeoln. */ - advanceeoln(p, pos, 1); - parseto(p, pos, "enumerate"); + parseto(p, pos, texitoks[cmd].tok); teximacro(p, "El"); + p->seenvs = 1; p->list = sv; } @@ -1779,13 +1865,13 @@ doitemize(struct texi *p, enum texicmd cmd, size_t *po { enum texilist sv = p->list; + advanceeoln(p, pos, 1); + p->list = TEXILIST_NOITEM; teximacro(p, "Bl -bullet"); - p->seenvs = 1; - /* FIXME: ignore and parseeoln. */ - advanceeoln(p, pos, 1); - parseto(p, pos, "itemize"); + parseto(p, pos, texitoks[cmd].tok); teximacro(p, "El"); + p->seenvs = 1; p->list = sv; } @@ -1802,8 +1888,7 @@ static void doignline(struct texi *p, enum texicmd cmd, size_t *pos) { - /* FIXME: ignore and parseeoln. */ - advanceeoln(p, pos, 1); + advanceeoln(p, pos, 1); } /*