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