xref: /titanic_53/usr/src/cmd/refer/refer7.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
2*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
3*7c478bd9Sstevel@tonic-gate 
4*7c478bd9Sstevel@tonic-gate 
5*7c478bd9Sstevel@tonic-gate /*
6*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
7*7c478bd9Sstevel@tonic-gate  * All rights reserved. The Berkeley software License Agreement
8*7c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
9*7c478bd9Sstevel@tonic-gate  */
10*7c478bd9Sstevel@tonic-gate 
11*7c478bd9Sstevel@tonic-gate /*
12*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1983, 1984 1985, 1986, 1987, 1988, Sun Microsystems, Inc.
13*7c478bd9Sstevel@tonic-gate  * All Rights Reserved.
14*7c478bd9Sstevel@tonic-gate  */
15*7c478bd9Sstevel@tonic-gate 
16*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
17*7c478bd9Sstevel@tonic-gate 
18*7c478bd9Sstevel@tonic-gate #include "refer..c"
19*7c478bd9Sstevel@tonic-gate #include <locale.h>
20*7c478bd9Sstevel@tonic-gate 
21*7c478bd9Sstevel@tonic-gate int newr[250];
22*7c478bd9Sstevel@tonic-gate 
23*7c478bd9Sstevel@tonic-gate chkdup(tag)
24*7c478bd9Sstevel@tonic-gate char *tag;
25*7c478bd9Sstevel@tonic-gate {
26*7c478bd9Sstevel@tonic-gate 	int i;
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate 	for(i = 1; i <= refnum; i++) {
29*7c478bd9Sstevel@tonic-gate 		if (reftable[i] && strcmp(reftable[i], tag)==0)
30*7c478bd9Sstevel@tonic-gate 			return(i);
31*7c478bd9Sstevel@tonic-gate 	}
32*7c478bd9Sstevel@tonic-gate 	reftable[refnum+1] = rtp;
33*7c478bd9Sstevel@tonic-gate 	if (refnum >= NRFTBL)
34*7c478bd9Sstevel@tonic-gate 		err(gettext("too many references (%d) for table"), refnum);
35*7c478bd9Sstevel@tonic-gate 	strcpy(rtp, tag);
36*7c478bd9Sstevel@tonic-gate 	while (*rtp++);
37*7c478bd9Sstevel@tonic-gate 	if (rtp > reftext + NRFTXT)
38*7c478bd9Sstevel@tonic-gate 		err(gettext("reference pointers too long (%d)"), rtp-reftext);
39*7c478bd9Sstevel@tonic-gate 	return(0);
40*7c478bd9Sstevel@tonic-gate }
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate dumpold()
43*7c478bd9Sstevel@tonic-gate {
44*7c478bd9Sstevel@tonic-gate 	FILE *fi;
45*7c478bd9Sstevel@tonic-gate 	int c, g1 = 0, nr = 1;
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate 	if (!endpush)
48*7c478bd9Sstevel@tonic-gate 		return;
49*7c478bd9Sstevel@tonic-gate 	fclose(fo);
50*7c478bd9Sstevel@tonic-gate 	fo = NULL;
51*7c478bd9Sstevel@tonic-gate 	if (sort) {
52*7c478bd9Sstevel@tonic-gate 		char comm[100];
53*7c478bd9Sstevel@tonic-gate 		sprintf(comm, "sort -f %s -o %s", tfile, tfile);
54*7c478bd9Sstevel@tonic-gate 		system(comm);
55*7c478bd9Sstevel@tonic-gate 	}
56*7c478bd9Sstevel@tonic-gate 	fi = fopen(tfile, "r");
57*7c478bd9Sstevel@tonic-gate 	if (fi == NULL)
58*7c478bd9Sstevel@tonic-gate 		return;
59*7c478bd9Sstevel@tonic-gate 	flout();
60*7c478bd9Sstevel@tonic-gate 	fprintf(ftemp, ".]<\n");
61*7c478bd9Sstevel@tonic-gate 	while ((c = getc(fi)) > 0) {
62*7c478bd9Sstevel@tonic-gate 		if (c == '\n') {
63*7c478bd9Sstevel@tonic-gate 			nr++;
64*7c478bd9Sstevel@tonic-gate 			g1 = 0;
65*7c478bd9Sstevel@tonic-gate 		}
66*7c478bd9Sstevel@tonic-gate 		if (c == sep)
67*7c478bd9Sstevel@tonic-gate 			c = '\n';
68*7c478bd9Sstevel@tonic-gate 		if (c == FLAG) {
69*7c478bd9Sstevel@tonic-gate 			/* make old-new ref number table */
70*7c478bd9Sstevel@tonic-gate 			char tb[20];
71*7c478bd9Sstevel@tonic-gate 			char *s = tb;
72*7c478bd9Sstevel@tonic-gate 			while ((c = getc(fi)) != FLAG)
73*7c478bd9Sstevel@tonic-gate 				*s++ = c;
74*7c478bd9Sstevel@tonic-gate 			*s = 0;
75*7c478bd9Sstevel@tonic-gate 			if (g1++ == 0)
76*7c478bd9Sstevel@tonic-gate 				newr[atoi(tb)] = nr;
77*7c478bd9Sstevel@tonic-gate #if EBUG
78*7c478bd9Sstevel@tonic-gate 			fprintf(stderr,
79*7c478bd9Sstevel@tonic-gate 				"nr %d assigned to atoi(tb) %d\n",nr,atoi(tb));
80*7c478bd9Sstevel@tonic-gate # endif
81*7c478bd9Sstevel@tonic-gate 			fprintf(ftemp,"%d", nr);
82*7c478bd9Sstevel@tonic-gate 			continue;
83*7c478bd9Sstevel@tonic-gate 		}
84*7c478bd9Sstevel@tonic-gate 		putc(c, ftemp);
85*7c478bd9Sstevel@tonic-gate 	}
86*7c478bd9Sstevel@tonic-gate 	fclose(fi);
87*7c478bd9Sstevel@tonic-gate #ifndef TF
88*7c478bd9Sstevel@tonic-gate 	unlink(tfile);
89*7c478bd9Sstevel@tonic-gate #endif
90*7c478bd9Sstevel@tonic-gate 	fprintf(ftemp, ".]>\n");
91*7c478bd9Sstevel@tonic-gate }
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate recopy (fnam)
94*7c478bd9Sstevel@tonic-gate char *fnam;
95*7c478bd9Sstevel@tonic-gate {
96*7c478bd9Sstevel@tonic-gate 	int c;
97*7c478bd9Sstevel@tonic-gate 	int *wref = NULL;
98*7c478bd9Sstevel@tonic-gate 	int wcnt = 0;
99*7c478bd9Sstevel@tonic-gate 	int wsize = 50;
100*7c478bd9Sstevel@tonic-gate 	int finalrn;
101*7c478bd9Sstevel@tonic-gate 	char sig[MXSIG];
102*7c478bd9Sstevel@tonic-gate 	extern int *realloc();
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate 	wref = (int *)calloc((unsigned)wsize, (unsigned)sizeof(int));
105*7c478bd9Sstevel@tonic-gate 	fclose(ftemp);
106*7c478bd9Sstevel@tonic-gate 	ftemp = fopen(fnam, "r");
107*7c478bd9Sstevel@tonic-gate 	if (ftemp == NULL) {
108*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("Can't reopen %s\n"), fnam);
109*7c478bd9Sstevel@tonic-gate 		exit(1);
110*7c478bd9Sstevel@tonic-gate 	}
111*7c478bd9Sstevel@tonic-gate 	while ((c = getc(ftemp)) != EOF) {
112*7c478bd9Sstevel@tonic-gate 		if (c == FLAG) {
113*7c478bd9Sstevel@tonic-gate 			char tb[10];
114*7c478bd9Sstevel@tonic-gate 			char *s = tb;
115*7c478bd9Sstevel@tonic-gate 			while ((c = getc(ftemp)) != FLAG)
116*7c478bd9Sstevel@tonic-gate 				*s++ = c;
117*7c478bd9Sstevel@tonic-gate 			*s = 0;
118*7c478bd9Sstevel@tonic-gate 			/*
119*7c478bd9Sstevel@tonic-gate 			 * If sort was done, permute the reference number
120*7c478bd9Sstevel@tonic-gate 			 * to obtain the final reference number, finalrn.
121*7c478bd9Sstevel@tonic-gate 			 */
122*7c478bd9Sstevel@tonic-gate 			if (sort)
123*7c478bd9Sstevel@tonic-gate 				finalrn = newr[atoi(tb)];
124*7c478bd9Sstevel@tonic-gate 			else
125*7c478bd9Sstevel@tonic-gate 				finalrn = atoi(tb);
126*7c478bd9Sstevel@tonic-gate 			if ((++wcnt > wsize) &&
127*7c478bd9Sstevel@tonic-gate 			 ((wref=realloc(wref,(wsize+=50)*sizeof(int)))==NULL)){
128*7c478bd9Sstevel@tonic-gate 				fprintf(stderr, gettext("Ref condense out of memory."));
129*7c478bd9Sstevel@tonic-gate 				exit(1);
130*7c478bd9Sstevel@tonic-gate 			}
131*7c478bd9Sstevel@tonic-gate 			wref[wcnt-1] = finalrn;
132*7c478bd9Sstevel@tonic-gate 			if ((c = getc(ftemp)) == AFLAG)
133*7c478bd9Sstevel@tonic-gate 				continue;
134*7c478bd9Sstevel@tonic-gate 			wref[wcnt] = 0;
135*7c478bd9Sstevel@tonic-gate 			condense(wref,wcnt,sig);
136*7c478bd9Sstevel@tonic-gate 			wcnt = 0;
137*7c478bd9Sstevel@tonic-gate 			printf("%s", sig);
138*7c478bd9Sstevel@tonic-gate 		}
139*7c478bd9Sstevel@tonic-gate 		putchar(c);
140*7c478bd9Sstevel@tonic-gate 	}
141*7c478bd9Sstevel@tonic-gate 	fclose(ftemp);
142*7c478bd9Sstevel@tonic-gate 	unlink(fnam);
143*7c478bd9Sstevel@tonic-gate }
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate /*
146*7c478bd9Sstevel@tonic-gate  * sort and condense reference signals when they are placed in
147*7c478bd9Sstevel@tonic-gate  * the text. Viz, the signal 1,2,3,4 is condensed to 1-4 and signals
148*7c478bd9Sstevel@tonic-gate  * of the form 5,2,9 are converted to 2,5,9
149*7c478bd9Sstevel@tonic-gate  */
150*7c478bd9Sstevel@tonic-gate condense(wref, wcnt, sig)
151*7c478bd9Sstevel@tonic-gate int	*wref;
152*7c478bd9Sstevel@tonic-gate int	wcnt;
153*7c478bd9Sstevel@tonic-gate char	*sig;
154*7c478bd9Sstevel@tonic-gate {
155*7c478bd9Sstevel@tonic-gate 	register int i = 0;
156*7c478bd9Sstevel@tonic-gate 	char wt[4];
157*7c478bd9Sstevel@tonic-gate 	extern int wswap();
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate 	qsort(wref, wcnt, sizeof(int), wswap);
160*7c478bd9Sstevel@tonic-gate 	sig[0] = 0;
161*7c478bd9Sstevel@tonic-gate 	while (i < wcnt) {
162*7c478bd9Sstevel@tonic-gate 		sprintf(wt,"%d",wref[i]);
163*7c478bd9Sstevel@tonic-gate 		strcat(sig,wt);
164*7c478bd9Sstevel@tonic-gate 		if ((i+2 < wcnt) && (wref[i] == (wref[i+2] - 2))) {
165*7c478bd9Sstevel@tonic-gate 			while (wref[i] == (wref[i+1] - 1))
166*7c478bd9Sstevel@tonic-gate 				i++;
167*7c478bd9Sstevel@tonic-gate 			strcat(sig, "-");
168*7c478bd9Sstevel@tonic-gate 		} else if (++i < wcnt)
169*7c478bd9Sstevel@tonic-gate 			strcat(sig,",\\|");
170*7c478bd9Sstevel@tonic-gate 	}
171*7c478bd9Sstevel@tonic-gate }
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate wswap(iw1, iw2)
174*7c478bd9Sstevel@tonic-gate register int *iw1,*iw2;
175*7c478bd9Sstevel@tonic-gate {
176*7c478bd9Sstevel@tonic-gate 	return(*iw1 - *iw2);
177*7c478bd9Sstevel@tonic-gate }
178