-{ int citenums[MAXATONCE]; /* reference numbers */
- char *citeinfo[MAXATONCE]; /* reference information */
- char infoword[HUNTSIZE]; /* information line */
- int rtop, n, i, j; /* number of citations being dumped */
- char c, *p, *walloc();
-
-/* first gather contiguous references together, and order them if
- required */
-
- rtop = -1;
- do {
- n = 0;
- while (isdigit(getch(c, ifd)))
- n = 10 * n + (c - '0');
- if (c ^= CITEMARK)
- error("inconsistant citation found in pass two");
- if (n == 0) { /* reference not found */
- rtop++;
- j = rtop;
- citenums[j] = -1;
- citeinfo[j] = 0;
- }
- else {
- for (i = 0; i <= numrefs; i++)
- if (refspos[i] == n) { /* its the ith item in reference list */
- rtop++;
- j = rtop;
- if (ordcite)
- for ( ; j > 0 && citenums[j-1] > i; j--) {
- citenums[j] = citenums[j-1];
- citeinfo[j] = citeinfo[j-1];
- }
- citenums[j] = i;
- citeinfo[j] = 0;
- break;
- }
- if (i > numrefs)
- error("citation not found in pass two");
- }
- if (getch(c, ifd) != CITEEND) {
- for (p = infoword; c != CITEEND ; ) {
- *p++ = c;
- getch(c, ifd);
- }
- *p = 0;
- citeinfo[j] = walloc(infoword);
- }
- getch(c, ifd);
- } while (c == CITEMARK);
- ungetc(c, ifd);
-
- /* now dump out values */
- for (i = 0; i <= rtop; i++) {
- if (citenums[i] >= 0)
- fputs(citestr[citenums[i]], ofd);
- if (citeinfo[i]) {
- fputs(citeinfo[i], ofd);
- free(citeinfo[i]);
- }
- if (hyphen) {
- for (j = 1; j + i <= rtop && citenums[i+j] == citenums[i] + j; j++);
- if (j + i > rtop) j = rtop;
- else j = j + i - 1;
- }
- else
- j = i;
- if (j > i + 1) {
- fputs("\\*(]-", ofd);
- i = j - 1;
- }
- else if (i != rtop)
- fputs("\\*(],", ofd);
- if (foot) {
- fn++;
- footrefs[fn] = citenums[i];
- }
- }
- return(fn);
+{
+ struct cite cites[MAXATONCE];
+ char infoword[HUNTSIZE]; /* information line */
+ reg int i;
+ reg char *p;
+ reg int ncites, n, j; /* number of citations being dumped */
+ char c, *walloc();
+ int neg;
+ /*
+ * first gather contiguous references together,
+ * and order them if required
+ */
+
+ ncites = 0;
+ do {
+ neg = 1;
+ n = 0;
+ do{
+ getch(c, ifd);
+ if (isdigit(c))
+ n = 10 * n + (c - '0');
+ else if (c == '-')
+ neg *= -1;
+ else if (c == CITEMARK)
+ break;
+ else
+ error("bad cite char 0%03o in pass two",c);
+ } while(1);
+ if (neg < 0) { /* reference not found */
+ cites[ncites].num = -1;
+ cites[ncites].info = 0;
+ ncites++;
+ } else {
+ /*
+ * Find reference n in the references
+ */
+ int i;
+ for (i = 0; i < numrefs; i++){
+ if (refinfo[i].ri_n == n){
+ cites[ncites].num = i;
+ cites[ncites].info = 0;
+ ncites++;
+ break;
+ }
+ }
+ if (i == numrefs)
+ error("citation %d not found in pass 2", n);
+ }
+ if (getch(c, ifd) != CITEEND) {
+ for (p = infoword; c != CITEEND ; ) {
+ *p++ = c;
+ getch(c, ifd);
+ }
+ *p = 0;
+ cites[ncites-1].info = walloc(infoword);
+ }
+ getch(c, ifd);
+ } while (c == CITEMARK);
+ ungetc(c, ifd);
+ if (ordcite)
+ qsort(cites, ncites, sizeof(struct cite), citesort);
+
+ /* now dump out values */
+ for (i = 0; i < ncites; i++) {
+ if (cites[i].num >= 0) {
+ if (changecite){
+ char tempcite[128];
+ char ref[REFSIZE];
+ struct refinfo *p;
+ /*
+ * rebuild the citation string,
+ * using the current template in effect
+ */
+ p = &refinfo[cites[i].num];
+ rdref(p, ref);
+ bldcite(tempcite, cites[i].num, ref);
+ strcat(tempcite, p->ri_disambig);
+ if (doacite) fputs(tempcite, ofd);
+ } else {
+ if (doacite) fputs(refinfo[cites[i].num].ri_cite, ofd);
+ }
+ if (!doacite) fputs("\\&", ofd);
+ }
+ if (cites[i].info) {
+ if (doacite) fputs(cites[i].info, ofd);
+ if (!doacite) fputs("\\&", ofd);
+ free(cites[i].info);
+ }
+ if (hyphen) {
+ for (j = 1;
+ j + i <= ncites && cites[i+j].num == cites[i].num + j;
+ j++)/*VOID*/;
+ if (j + i > ncites)
+ j = ncites;
+ else
+ j = j + i - 1;
+ } else {
+ j = i;
+ }
+ if (j > i + 1) {
+ fputs("\\*(]-", ofd);
+ i = j - 1;
+ } else if (i != ncites - 1) {
+ fputs("\\*(],", ofd);
+ }
+ if (foot) {
+ fn++;
+ footrefs[fn] = cites[i].num;
+ }
+ }
+ return(fn);