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