=================================================================== RCS file: /cvs/texi2mdoc/main.c,v retrieving revision 1.57 retrieving revision 1.61 diff -u -p -r1.57 -r1.61 --- texi2mdoc/main.c 2015/03/01 16:57:39 1.57 +++ texi2mdoc/main.c 2015/03/05 08:35:35 1.61 @@ -1,4 +1,4 @@ -/* $Id: main.c,v 1.57 2015/03/01 16:57:39 kristaps Exp $ */ +/* $Id: main.c,v 1.61 2015/03/05 08:35:35 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 @@ -725,13 +736,30 @@ 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_SC == cmd) + p->uppercase++; parsebracket(p, pos, 0); + if (TEXICMD_SC == cmd) + p->uppercase--; texipunctuate(p, pos); teximacroclose(p); } @@ -775,6 +803,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 +860,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]); @@ -1195,6 +1270,9 @@ dosymbol(struct texi *p, enum texicmd cmd, size_t *pos break; case (TEXICMD_AT): texiputchar(p, '@'); + break; + case (TEXICMD_BACKSLASH): + texiputchar(p, '\\'); break; case (TEXICMD_BANG): texiputchar(p, '!');