xref: /illumos-gate/usr/src/cmd/refer/refer7.c (revision 2a8bcb4e)
1*11a8fa6cSceastha /*
2*11a8fa6cSceastha  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3*11a8fa6cSceastha  * Use is subject to license terms.
4*11a8fa6cSceastha  */
5*11a8fa6cSceastha 
67c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
77c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
87c478bd9Sstevel@tonic-gate 
97c478bd9Sstevel@tonic-gate /*
107c478bd9Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
117c478bd9Sstevel@tonic-gate  * All rights reserved. The Berkeley software License Agreement
127c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
137c478bd9Sstevel@tonic-gate  */
147c478bd9Sstevel@tonic-gate 
157c478bd9Sstevel@tonic-gate #include "refer..c"
167c478bd9Sstevel@tonic-gate #include <locale.h>
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate int newr[250];
197c478bd9Sstevel@tonic-gate 
20*11a8fa6cSceastha extern void err();
21*11a8fa6cSceastha extern void flout();
22*11a8fa6cSceastha 
23*11a8fa6cSceastha static void condense(int *, int, char *);
24*11a8fa6cSceastha 
25*11a8fa6cSceastha int
chkdup(char * tag)26*11a8fa6cSceastha chkdup(char *tag)
277c478bd9Sstevel@tonic-gate {
287c478bd9Sstevel@tonic-gate 	int i;
297c478bd9Sstevel@tonic-gate 
30*11a8fa6cSceastha 	for (i = 1; i <= refnum; i++) {
31*11a8fa6cSceastha 		if (reftable[i] && strcmp(reftable[i], tag) == 0)
32*11a8fa6cSceastha 			return (i);
337c478bd9Sstevel@tonic-gate 	}
347c478bd9Sstevel@tonic-gate 	reftable[refnum+1] = rtp;
357c478bd9Sstevel@tonic-gate 	if (refnum >= NRFTBL)
367c478bd9Sstevel@tonic-gate 		err(gettext("too many references (%d) for table"), refnum);
377c478bd9Sstevel@tonic-gate 	strcpy(rtp, tag);
38*11a8fa6cSceastha 	while (*rtp++)
39*11a8fa6cSceastha 		;
407c478bd9Sstevel@tonic-gate 	if (rtp > reftext + NRFTXT)
417c478bd9Sstevel@tonic-gate 		err(gettext("reference pointers too long (%d)"), rtp-reftext);
42*11a8fa6cSceastha 	return (0);
437c478bd9Sstevel@tonic-gate }
447c478bd9Sstevel@tonic-gate 
45*11a8fa6cSceastha void
dumpold(void)46*11a8fa6cSceastha dumpold(void)
477c478bd9Sstevel@tonic-gate {
487c478bd9Sstevel@tonic-gate 	FILE *fi;
497c478bd9Sstevel@tonic-gate 	int c, g1 = 0, nr = 1;
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate 	if (!endpush)
527c478bd9Sstevel@tonic-gate 		return;
537c478bd9Sstevel@tonic-gate 	fclose(fo);
547c478bd9Sstevel@tonic-gate 	fo = NULL;
557c478bd9Sstevel@tonic-gate 	if (sort) {
567c478bd9Sstevel@tonic-gate 		char comm[100];
577c478bd9Sstevel@tonic-gate 		sprintf(comm, "sort -f %s -o %s", tfile, tfile);
587c478bd9Sstevel@tonic-gate 		system(comm);
597c478bd9Sstevel@tonic-gate 	}
607c478bd9Sstevel@tonic-gate 	fi = fopen(tfile, "r");
617c478bd9Sstevel@tonic-gate 	if (fi == NULL)
627c478bd9Sstevel@tonic-gate 		return;
637c478bd9Sstevel@tonic-gate 	flout();
647c478bd9Sstevel@tonic-gate 	fprintf(ftemp, ".]<\n");
657c478bd9Sstevel@tonic-gate 	while ((c = getc(fi)) > 0) {
667c478bd9Sstevel@tonic-gate 		if (c == '\n') {
677c478bd9Sstevel@tonic-gate 			nr++;
687c478bd9Sstevel@tonic-gate 			g1 = 0;
697c478bd9Sstevel@tonic-gate 		}
707c478bd9Sstevel@tonic-gate 		if (c == sep)
717c478bd9Sstevel@tonic-gate 			c = '\n';
727c478bd9Sstevel@tonic-gate 		if (c == FLAG) {
737c478bd9Sstevel@tonic-gate 			/* make old-new ref number table */
747c478bd9Sstevel@tonic-gate 			char tb[20];
757c478bd9Sstevel@tonic-gate 			char *s = tb;
767c478bd9Sstevel@tonic-gate 			while ((c = getc(fi)) != FLAG)
777c478bd9Sstevel@tonic-gate 				*s++ = c;
787c478bd9Sstevel@tonic-gate 			*s = 0;
797c478bd9Sstevel@tonic-gate 			if (g1++ == 0)
807c478bd9Sstevel@tonic-gate 				newr[atoi(tb)] = nr;
817c478bd9Sstevel@tonic-gate #if EBUG
82*11a8fa6cSceastha 			fprintf(stderr, "nr %d assigned to atoi(tb) %d\n",
83*11a8fa6cSceastha 			    nr, atoi(tb));
84*11a8fa6cSceastha #endif
85*11a8fa6cSceastha 			fprintf(ftemp, "%d", nr);
867c478bd9Sstevel@tonic-gate 			continue;
877c478bd9Sstevel@tonic-gate 		}
887c478bd9Sstevel@tonic-gate 		putc(c, ftemp);
897c478bd9Sstevel@tonic-gate 	}
907c478bd9Sstevel@tonic-gate 	fclose(fi);
917c478bd9Sstevel@tonic-gate #ifndef TF
927c478bd9Sstevel@tonic-gate 	unlink(tfile);
937c478bd9Sstevel@tonic-gate #endif
947c478bd9Sstevel@tonic-gate 	fprintf(ftemp, ".]>\n");
957c478bd9Sstevel@tonic-gate }
967c478bd9Sstevel@tonic-gate 
97*11a8fa6cSceastha void
recopy(char * fnam)98*11a8fa6cSceastha recopy(char *fnam)
997c478bd9Sstevel@tonic-gate {
1007c478bd9Sstevel@tonic-gate 	int c;
1017c478bd9Sstevel@tonic-gate 	int *wref = NULL;
1027c478bd9Sstevel@tonic-gate 	int wcnt = 0;
1037c478bd9Sstevel@tonic-gate 	int wsize = 50;
1047c478bd9Sstevel@tonic-gate 	int finalrn;
1057c478bd9Sstevel@tonic-gate 	char sig[MXSIG];
1067c478bd9Sstevel@tonic-gate 	extern int *realloc();
1077c478bd9Sstevel@tonic-gate 
108*11a8fa6cSceastha 	wref = (int *)calloc((unsigned)wsize, (unsigned)sizeof (int));
1097c478bd9Sstevel@tonic-gate 	fclose(ftemp);
1107c478bd9Sstevel@tonic-gate 	ftemp = fopen(fnam, "r");
1117c478bd9Sstevel@tonic-gate 	if (ftemp == NULL) {
1127c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("Can't reopen %s\n"), fnam);
1137c478bd9Sstevel@tonic-gate 		exit(1);
1147c478bd9Sstevel@tonic-gate 	}
1157c478bd9Sstevel@tonic-gate 	while ((c = getc(ftemp)) != EOF) {
1167c478bd9Sstevel@tonic-gate 		if (c == FLAG) {
1177c478bd9Sstevel@tonic-gate 			char tb[10];
1187c478bd9Sstevel@tonic-gate 			char *s = tb;
1197c478bd9Sstevel@tonic-gate 			while ((c = getc(ftemp)) != FLAG)
1207c478bd9Sstevel@tonic-gate 				*s++ = c;
1217c478bd9Sstevel@tonic-gate 			*s = 0;
1227c478bd9Sstevel@tonic-gate 			/*
1237c478bd9Sstevel@tonic-gate 			 * If sort was done, permute the reference number
1247c478bd9Sstevel@tonic-gate 			 * to obtain the final reference number, finalrn.
1257c478bd9Sstevel@tonic-gate 			 */
1267c478bd9Sstevel@tonic-gate 			if (sort)
1277c478bd9Sstevel@tonic-gate 				finalrn = newr[atoi(tb)];
1287c478bd9Sstevel@tonic-gate 			else
1297c478bd9Sstevel@tonic-gate 				finalrn = atoi(tb);
130*11a8fa6cSceastha 			if ((++wcnt > wsize) && ((wref = realloc(wref,
131*11a8fa6cSceastha 			    (wsize += 50) * sizeof (int))) == NULL)) {
132*11a8fa6cSceastha 				fprintf(stderr, gettext(
133*11a8fa6cSceastha 				    "Ref condense out of memory."));
1347c478bd9Sstevel@tonic-gate 				exit(1);
1357c478bd9Sstevel@tonic-gate 			}
1367c478bd9Sstevel@tonic-gate 			wref[wcnt-1] = finalrn;
137*11a8fa6cSceastha 			if ((c = getc(ftemp)) == AFLAG)
1387c478bd9Sstevel@tonic-gate 				continue;
1397c478bd9Sstevel@tonic-gate 			wref[wcnt] = 0;
140*11a8fa6cSceastha 			condense(wref, wcnt, sig);
1417c478bd9Sstevel@tonic-gate 			wcnt = 0;
1427c478bd9Sstevel@tonic-gate 			printf("%s", sig);
1437c478bd9Sstevel@tonic-gate 		}
1447c478bd9Sstevel@tonic-gate 		putchar(c);
1457c478bd9Sstevel@tonic-gate 	}
1467c478bd9Sstevel@tonic-gate 	fclose(ftemp);
1477c478bd9Sstevel@tonic-gate 	unlink(fnam);
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate /*
1517c478bd9Sstevel@tonic-gate  * sort and condense reference signals when they are placed in
1527c478bd9Sstevel@tonic-gate  * the text. Viz, the signal 1,2,3,4 is condensed to 1-4 and signals
1537c478bd9Sstevel@tonic-gate  * of the form 5,2,9 are converted to 2,5,9
1547c478bd9Sstevel@tonic-gate  */
155*11a8fa6cSceastha static void
condense(int * wref,int wcnt,char * sig)156*11a8fa6cSceastha condense(int *wref, int wcnt, char *sig)
1577c478bd9Sstevel@tonic-gate {
158*11a8fa6cSceastha 	int i = 0;
1597c478bd9Sstevel@tonic-gate 	char wt[4];
1607c478bd9Sstevel@tonic-gate 	extern int wswap();
1617c478bd9Sstevel@tonic-gate 
162*11a8fa6cSceastha 	qsort(wref, wcnt, sizeof (int), wswap);
1637c478bd9Sstevel@tonic-gate 	sig[0] = 0;
1647c478bd9Sstevel@tonic-gate 	while (i < wcnt) {
165*11a8fa6cSceastha 		sprintf(wt, "%d", wref[i]);
166*11a8fa6cSceastha 		strcat(sig, wt);
1677c478bd9Sstevel@tonic-gate 		if ((i+2 < wcnt) && (wref[i] == (wref[i+2] - 2))) {
1687c478bd9Sstevel@tonic-gate 			while (wref[i] == (wref[i+1] - 1))
1697c478bd9Sstevel@tonic-gate 				i++;
1707c478bd9Sstevel@tonic-gate 			strcat(sig, "-");
1717c478bd9Sstevel@tonic-gate 		} else if (++i < wcnt)
172*11a8fa6cSceastha 			strcat(sig, ",\\|");
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate 
176*11a8fa6cSceastha int
wswap(int * iw1,int * iw2)177*11a8fa6cSceastha wswap(int *iw1, int *iw2)
1787c478bd9Sstevel@tonic-gate {
179*11a8fa6cSceastha 	return (*iw1 - *iw2);
1807c478bd9Sstevel@tonic-gate }
181