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