=================================================================== RCS file: /cvs/texi2mdoc/util.c,v retrieving revision 1.31 retrieving revision 1.32 diff -u -p -r1.31 -r1.32 --- texi2mdoc/util.c 2015/03/12 04:24:19 1.31 +++ texi2mdoc/util.c 2015/03/12 10:44:34 1.32 @@ -1,4 +1,4 @@ -/* $Id: util.c,v 1.31 2015/03/12 04:24:19 kristaps Exp $ */ +/* $Id: util.c,v 1.32 2015/03/12 10:44:34 kristaps Exp $ */ /* * Copyright (c) 2015 Kristaps Dzonsons * @@ -155,7 +155,7 @@ texindex(struct texi *p, const char *tok, texiabort(p, NULL); /* Add term to term array. */ - p->indexs[i].index[isz].chapter = p->nodesz - 1; + p->indexs[i].index[isz].chapter = p->nodecur; p->indexs[i].index[isz].term = malloc(sz + 1); if (NULL == p->indexs[i].index[isz].term) texiabort(p, NULL); @@ -169,10 +169,8 @@ texindex(struct texi *p, const char *tok, texiputchars(p, "idx"); texiputchars(p, p->indexs[i].name); cp = p->indexs[i].index[isz].term; - while ('\n' != *cp) { - assert('\0' != *cp); + while ('\n' != *cp) texiputchar(p, *cp++); - } teximacroclose(p); #endif p->indexs[i].indexsz++; @@ -246,7 +244,7 @@ texiexit(struct texi *p) for (i = 0; i < p->valsz; i++) texivaluefree(&p->vals[i]); - free(p->nodes); + free(p->nodecache); free(p->macros); free(p->vals); free(p->indexs); @@ -1130,7 +1128,7 @@ peekcmd(const struct texi *p, size_t pos) * Parse a single word or command. * This will return immediately at the EOF. */ -static void +void parsesingle(struct texi *p, size_t *pos) { size_t end, sv; @@ -1647,15 +1645,16 @@ teximdocclose(struct texi *p, int last) { char buf[PATH_MAX]; - if (NULL == p->chapters || 0 == p->chapnum) + if (NULL == p->chapters || 1 == p->nodesz) return; teximacro(p, "Sh INFO NAVIGATION"); /* Print a reference to the "top" node. */ - if (p->chapnum > 1) { + if (-1 != p->nodecache[p->nodecur].up) { texiputchars(p, "Top node,"); - snprintf(buf, sizeof(buf), "%s-1 7", p->chapters); + snprintf(buf, sizeof(buf), "%s-%zd 7", + p->chapters, p->nodecache[p->nodecur].up); p->seenvs = 0; teximacroopen(p, "Xr "); texiputchars(p, buf); @@ -1663,11 +1662,10 @@ teximdocclose(struct texi *p, int last) teximacroclose(p); } - /* Print a reference to the previous node. */ - if (p->chapnum > 2) { + if (-1 != p->nodecache[p->nodecur].prev) { texiputchars(p, "previous node,"); - snprintf(buf, sizeof(buf), - "%s-%zu 7", p->chapters, p->chapnum - 1); + snprintf(buf, sizeof(buf), "%s-%zd 7", + p->chapters, p->nodecache[p->nodecur].prev); p->seenvs = 0; teximacroopen(p, "Xr "); texiputchars(p, buf); @@ -1676,14 +1674,10 @@ teximdocclose(struct texi *p, int last) teximacroclose(p); } - /* Print a reference to the next node. */ - if ( ! last) { - if (1 == p->chapnum) - texiputchars(p, "Next node,"); - else - texiputchars(p, "next node,"); - snprintf(buf, sizeof(buf), - "%s-%zu 7", p->chapters, p->chapnum + 1); + if (-1 != p->nodecache[p->nodecur].next) { + texiputchars(p, "next node,"); + snprintf(buf, sizeof(buf), "%s-%zd 7", + p->chapters, p->nodecache[p->nodecur].next); p->seenvs = 0; teximacroopen(p, "Xr "); texiputchars(p, buf); @@ -1691,12 +1685,48 @@ teximdocclose(struct texi *p, int last) } fclose(p->outfile); + p->outfile = NULL; } +ssize_t +texicache(struct texi *p, const char *buf, size_t sz) +{ + size_t i; + + for (i = 0; i < p->nodecachesz; i++) { + if (sz != strlen(p->nodecache[i].name)) + continue; + if (strncmp(buf, p->nodecache[i].name, sz)) + continue; + break; + } + if (i < p->nodecachesz) + return(i); + if (NULL == buf) + return(-1); + p->nodecache = realloc + (p->nodecache, + (p->nodecachesz + 1) * sizeof(struct texinode)); + if (NULL == p->nodecache) + texiabort(p, NULL); + p->nodecache[p->nodecachesz].name = malloc(sz + 1); + if (NULL == p->nodecache[p->nodecachesz].name) + texiabort(p, NULL); + memcpy(p->nodecache[p->nodecachesz].name, buf, sz); + p->nodecache[p->nodecachesz].name[sz] = '\0'; + p->nodecache[p->nodecachesz].up = + p->nodecache[p->nodecachesz].next = + p->nodecache[p->nodecachesz].prev = -1; + p->nodecachesz++; + return(p->nodecachesz - 1); +} + /* - * Open a mdoc(7) context. - * If we're printing chapters, then open the outfile here, too. - * Otherwise just print the mdoc(7) prologue. + * 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. */ void teximdocopen(struct texi *p, size_t *pos) @@ -1704,34 +1734,7 @@ teximdocopen(struct texi *p, size_t *pos) const char *cp; time_t t; char date[32]; - char fname[PATH_MAX]; - if (NULL != p->chapters) { - p->chapnum++; - snprintf(fname, sizeof(fname), "%s-%zu", - p->chapters, p->chapnum); - p->nodes = realloc(p->nodes, - (p->nodesz + 1) * sizeof(char *)); - if (NULL == p->nodes) - texiabort(p, NULL); - p->nodes[p->nodesz] = strdup(fname); - if (NULL == p->nodes[p->nodesz]) - texiabort(p, NULL); - p->nodesz++; - snprintf(fname, sizeof(fname), "%s-%zu.7", - p->chapters, p->chapnum); - p->outfile = fopen(fname, "w"); - if (NULL == p->outfile) - texiabort(p, fname); - } - - /* - * 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));