=================================================================== RCS file: /cvs/texi2mdoc/main.c,v retrieving revision 1.54 retrieving revision 1.60 diff -u -p -r1.54 -r1.60 --- texi2mdoc/main.c 2015/02/28 13:16:44 1.54 +++ texi2mdoc/main.c 2015/03/05 08:18:56 1.60 @@ -1,4 +1,4 @@ -/* $Id: main.c,v 1.54 2015/02/28 13:16:44 kristaps Exp $ */ +/* $Id: main.c,v 1.60 2015/03/05 08:18:56 kristaps Exp $ */ /* * Copyright (c) 2015 Kristaps Dzonsons * @@ -26,7 +26,6 @@ #include #include #include -#include #include #include "extern.h" @@ -95,7 +94,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 */ @@ -269,7 +269,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 */ @@ -342,6 +342,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) { @@ -350,7 +356,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++; @@ -358,18 +363,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 @@ -726,13 +734,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); } @@ -943,6 +968,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): @@ -954,8 +981,6 @@ dodisplay(struct texi *p, enum texicmd cmd, size_t *po } p->seenvs = 1; - /* FIXME: ignore and parseeoln. */ - advanceeoln(p, pos, 1); parseto(p, pos, texitoks[cmd].tok); teximacro(p, "Ed"); } @@ -964,9 +989,9 @@ 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--; @@ -984,20 +1009,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++; - 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 @@ -1190,6 +1222,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; @@ -1411,7 +1446,7 @@ dovalue(struct texi *p, enum texicmd cmd, size_t *pos) texiputchar(p, ' '); p->seenws = 0; if (NULL != (cp = valueblookup(p, pos))) - texisplice(p, cp, strlen(cp), pos); + texisplice(p, cp, strlen(cp), *pos); else texiputchars(p, "{No value}"); } else if (TEXICMD_IFCLEAR == cmd) { @@ -1553,9 +1588,11 @@ dosection(struct texi *p, enum texicmd cmd, size_t *po int sec; switch (cmd) { + case (TEXICMD_TOP): + sec = 0; + break; case (TEXICMD_APPENDIX): case (TEXICMD_CHAPTER): - case (TEXICMD_TOP): case (TEXICMD_UNNUMBERED): sec = sectioner(p, 0); break; @@ -1574,6 +1611,11 @@ dosection(struct texi *p, enum texicmd cmd, size_t *po else if (p->literal) texierr(p, "\"%s\" in a literal scope!?", sects[sec]); + if (0 == sec && NULL != p->chapters) { + teximdocclose(p, 0); + teximdocopen(p, pos); + } + teximacroopen(p, sects[sec]); parseeoln(p, pos); teximacroclose(p); @@ -1581,60 +1623,26 @@ dosection(struct texi *p, enum texicmd cmd, size_t *po } static void -dosp(struct texi *p, enum texicmd cmd, size_t *pos) -{ - - if (p->literal) - texiputchar(p, '\n'); - else - texivspace(p); - /* FIXME: ignore and parseeoln. */ - advanceeoln(p, pos, 1); -} - -static void dotop(struct texi *p, enum texicmd cmd, size_t *pos) { - const char *cp; - time_t t; - char date[32]; if (--p->ign) texierr(p, "@top command while ignoring"); - /* - * Here we print our standard mdoc(7) prologue. - * We use the title set with @settitle for the `Nd' description - * and the source document filename (the first one as invoked on - * the command line) for the title. - * The date is set to the current date. - */ - t = time(NULL); - strftime(date, sizeof(date), "%F", localtime(&t)); + if (NULL == p->chapters) + teximdocopen(p, pos); + dosection(p, cmd, pos); +} - teximacroopen(p, "Dd"); - texiputchars(p, date); - teximacroclose(p); - teximacroopen(p, "Dt"); - for (cp = p->title; '\0' != *cp; cp++) - texiputchar(p, toupper((unsigned int)*cp)); - texiputchars(p, " 7"); - teximacroclose(p); - teximacro(p, "Os"); - teximacro(p, "Sh NAME"); - teximacroopen(p, "Nm"); - for (cp = p->title; '\0' != *cp; cp++) - texiputchar(p, *cp); - teximacroclose(p); - teximacroopen(p, "Nd"); - if (NULL != p->subtitle) - for (cp = p->subtitle; '\0' != *cp; cp++) - texiputchar(p, *cp); +static void +dosp(struct texi *p, enum texicmd cmd, size_t *pos) +{ + + advanceeoln(p, pos, 1); + if (p->literal) + texiputchar(p, '\n'); else - texiputchars(p, "Unknown description"); - teximacroclose(p); - p->seenvs = 1; - dosection(p, cmd, pos); + texivspace(p); } static void @@ -1706,7 +1714,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 @@ -1758,10 +1766,10 @@ 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"); @@ -1779,7 +1787,8 @@ doend(struct texi *p, enum texicmd cmd, size_t *pos) while (*pos < BUFSZ(p) && '\n' != BUF(p)[*pos]) advance(p, pos); - texiwarn(p, "unexpected \"end\": %.*s", (int)(*pos - start), &BUF(p)[start]); + texiwarn(p, "unexpected \"end\": %.*s", + (int)(*pos - start), &BUF(p)[start]); advanceeoln(p, pos, 1); } @@ -1788,12 +1797,12 @@ 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->list = sv; } @@ -1803,12 +1812,12 @@ 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->list = sv; } @@ -1826,8 +1835,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); } /* @@ -1885,10 +1893,14 @@ main(int argc, char *argv[]) memset(&texi, 0, sizeof(struct texi)); texi.ign = 1; + texi.outfile = stdout; Idir = NULL; - while (-1 != (c = getopt(argc, argv, "I:"))) + while (-1 != (c = getopt(argc, argv, "C:I:"))) switch (c) { + case ('C'): + texi.chapters = optarg; + break; case ('I'): Idir = optarg; break; @@ -1922,8 +1934,8 @@ main(int argc, char *argv[]) } texiexit(&texi); - return(EXIT_FAILURE); + exit(EXIT_SUCCESS); usage: - fprintf(stderr, "usage: %s [-Idirs] [file]\n", progname); + fprintf(stderr, "usage: %s [-Cdir] [-Idirs] [file]\n", progname); return(EXIT_FAILURE); }