=================================================================== RCS file: /cvs/texi2mdoc/util.c,v retrieving revision 1.30 retrieving revision 1.32 diff -u -p -r1.30 -r1.32 --- texi2mdoc/util.c 2015/03/11 12:51:41 1.30 +++ texi2mdoc/util.c 2015/03/12 10:44:34 1.32 @@ -1,4 +1,4 @@ -/* $Id: util.c,v 1.30 2015/03/11 12:51:41 kristaps Exp $ */ +/* $Id: util.c,v 1.32 2015/03/12 10:44:34 kristaps Exp $ */ /* * Copyright (c) 2015 Kristaps Dzonsons * @@ -109,7 +109,7 @@ texidex_free(struct texidex *p) size_t i; for (i = 0; i < p->indexsz; i++) - free(p->index[i]); + free(p->index[i].term); free(p->index); free(p->name); @@ -126,7 +126,7 @@ void texindex(struct texi *p, const char *tok, size_t toksz, const char *index, size_t sz) { - size_t i; + size_t i, isz; #ifdef HAVE_INDEX char *cp; #endif @@ -146,22 +146,21 @@ texindex(struct texi *p, const char *tok, } assert(i < p->indexsz); + isz = p->indexs[i].indexsz; /* Reallocate index's terms. */ p->indexs[i].index = realloc (p->indexs[i].index, - (p->indexs[i].indexsz + 1) * - sizeof(char *)); + (isz + 1) * sizeof(struct texiterm)); if (NULL == p->indexs[i].index) texiabort(p, NULL); /* Add term to term array. */ - p->indexs[i].index[p->indexs[i].indexsz] = - malloc(sz + 1); - if (NULL == p->indexs[i].index[p->indexs[i].indexsz]) + 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); - memcpy(p->indexs[i].index[p->indexs[i].indexsz], - index, sz); - p->indexs[i].index[p->indexs[i].indexsz][sz] = '\0'; + memcpy(p->indexs[i].index[isz].term, index, sz); + p->indexs[i].index[isz].term[sz] = '\0'; /* Output mdoc(7) for index. */ #ifdef HAVE_INDEX @@ -169,11 +168,9 @@ texindex(struct texi *p, const char *tok, teximacroopen(p, "Ix"); texiputchars(p, "idx"); texiputchars(p, p->indexs[i].name); - cp = p->indexs[i].index[p->indexs[i].indexsz]; - while ('\n' != *cp) { - assert('\0' != *cp); + cp = p->indexs[i].index[isz].term; + while ('\n' != *cp) texiputchar(p, *cp++); - } teximacroclose(p); #endif p->indexs[i].indexsz++; @@ -204,7 +201,8 @@ texindex_add(struct texi *p, const char *tok, size_t s /* Reallocate indices. */ p->indexs = realloc(p->indexs, - sizeof(struct texidex) * (p->indexsz + 1)); + sizeof(struct texidex) * + (p->indexsz + 1)); if (NULL == p->indexs) texiabort(p, NULL); if (NULL == (cp = malloc(sz + 1))) @@ -246,6 +244,7 @@ texiexit(struct texi *p) for (i = 0; i < p->valsz; i++) texivaluefree(&p->vals[i]); + free(p->nodecache); free(p->macros); free(p->vals); free(p->indexs); @@ -1129,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; @@ -1646,26 +1645,28 @@ 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), "node1 7"); + snprintf(buf, sizeof(buf), "%s-%zd 7", + p->chapters, p->nodecache[p->nodecur].up); + p->seenvs = 0; teximacroopen(p, "Xr "); texiputchars(p, buf); texiputchars(p, " ;"); 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), - "node%zu 7", 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); if ( ! last) @@ -1673,26 +1674,59 @@ 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), - "node%zu 7", 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); teximacroclose(p); } 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) @@ -1700,23 +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) { - snprintf(fname, sizeof(fname), "%s/node%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));