1 /* $MirOS: src/usr.bin/oldroff/refer/refer7.c,v 1.2 2008/11/08 23:04:47 tg Exp $ */
2 
3 /*-
4  * Copyright (c) 1979, 1980, 1981, 1986, 1988, 1990, 1991, 1992
5  *     The Regents of the University of California.
6  * Copyright (C) Caldera International Inc.  2001-2002.
7  * Copyright (c) 2003, 2004, 2005
8  *	Thorsten "mirabilos" Glaser <tg@mirbsd.org>
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms,
12  * with or without modification, are permitted provided
13  * that the following conditions are met:
14  *
15  * Redistributions of source code and documentation must retain
16  * the above copyright notice, this list of conditions and the
17  * following disclaimer.  Redistributions in binary form must
18  * reproduce the above copyright notice, this list of conditions
19  * and the following disclaimer in the documentation and/or other
20  * materials provided with the distribution.
21  *
22  * All advertising materials mentioning features or use of this
23  * software must display the following acknowledgement:
24  *   This product includes software developed or owned by
25  *   Caldera International, Inc.
26  *
27  * Neither the name of Caldera International, Inc. nor the names
28  * of other contributors may be used to endorse or promote products
29  * derived from this software without specific prior written permission.
30  *
31  * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
32  * INTERNATIONAL, INC. AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
33  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
34  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35  * ARE DISCLAIMED.  IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE
36  * LIABLE FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR
37  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
38  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
39  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
40  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
41  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
42  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43  */
44 
45 #ifndef lint
46 static char sccsid[] = "@(#)refer7.c	4.5 (Berkeley) 4/18/91";
47 #endif /* not lint */
48 
49 #include <stdlib.h>
50 
51 #include "refer..c"
52 
53 int newr[250];
54 
chkdup(tag)55 chkdup(tag)
56 char *tag;
57 {
58 	int i;
59 
60 	for(i = 1; i <= refnum; i++) {
61 		if (strcmp(reftable[i], tag)==0)
62 			return(i);
63 	}
64 	reftable[refnum+1] = rtp;
65 	if (refnum >= NRFTBL)
66 		err("too many references (%d) for table", refnum);
67 	strcpy(rtp, tag);
68 	while (*rtp++);
69 	if (rtp > reftext + NRFTXT)
70 		err("reference pointers too long (%d)", rtp-reftext);
71 	return(0);
72 }
73 
dumpold()74 dumpold()
75 {
76 	FILE *fi;
77 	int c, g1 = 0, nr = 1;
78 
79 	if (!endpush)
80 		return;
81 	if (fo != NULL) {
82 		fclose(fo);
83 		fo = NULL;
84 	}
85 	if (sort) {
86 		char comm[100];
87 		sprintf(comm, "sort -f %s -o %s", tfile, tfile);
88 		system(comm);
89 	}
90 	fi = fopen(tfile, "r");
91 	if (fi == NULL)
92 		return;
93 	flout();
94 	fprintf(ftemp, ".]<\n");
95 	while ((c = getc(fi)) > 0) {
96 		if (c == '\n') {
97 			nr++;
98 			g1 = 0;
99 		}
100 		if (c == sep)
101 			c = '\n';
102 		if (c == FLAG) {
103 			/* make old-new ref number table */
104 			char tb[20];
105 			char *s = tb;
106 			while ((c = getc(fi)) != FLAG)
107 				*s++ = c;
108 			*s = 0;
109 			if (g1++ == 0)
110 				newr[atoi(tb)] = nr;
111 #if EBUG
112 			fprintf(stderr,
113 				"nr %d assigned to atoi(tb) %d\n",nr,atoi(tb));
114 # endif
115 			fprintf(ftemp,"%d", nr);
116 			continue;
117 		}
118 		putc(c, ftemp);
119 	}
120 	fclose(fi);
121 #ifndef TF
122 	unlink(tfile);
123 #endif
124 	fprintf(ftemp, ".]>\n");
125 }
126 
recopy(fnam)127 recopy (fnam)
128 char *fnam;
129 {
130 	int c;
131 	int *wref = NULL;
132 	int wcnt = 0;
133 	int wsize = 50;
134 	int finalrn;
135 	char sig[MXSIG];
136 
137 	wref = (int *)calloc((unsigned)wsize, (unsigned)sizeof(int));
138 	fclose(ftemp);
139 	ftemp = fopen(fnam, "r");
140 	if (ftemp == NULL) {
141 		fprintf(stderr, "Can't reopen %s\n", fnam);
142 		exit(1);
143 	}
144 	while ((c = getc(ftemp)) != EOF) {
145 		if (c == FLAG) {
146 			char tb[10];
147 			char *s = tb;
148 			while ((c = getc(ftemp)) != FLAG)
149 				*s++ = c;
150 			*s = 0;
151 			/*
152 			 * If sort was done, permute the reference number
153 			 * to obtain the final reference number, finalrn.
154 			 */
155 			if (sort)
156 				finalrn = newr[atoi(tb)];
157 			else
158 				finalrn = atoi(tb);
159 			if ((++wcnt > wsize) &&
160 			 ((wref=realloc(wref,(wsize+=50)*sizeof(int)))==NULL)){
161 				fprintf(stderr, "Ref condense out of memory.");
162 				exit(1);
163 			}
164 			wref[wcnt-1] = finalrn;
165 			if ((c = getc(ftemp)) == AFLAG)
166 				continue;
167 			wref[wcnt] = 0;
168 			condense(wref,wcnt,sig);
169 			wcnt = 0;
170 			printf("%s", sig);
171 		}
172 		putchar(c);
173 	}
174 	fclose(ftemp);
175 	unlink(fnam);
176 }
177 
178 /*
179  * sort and condense referance signals when they are placed in
180  * the text. Viz, the signal 1,2,3,4 is condensed to 1-4 and signals
181  * of the form 5,2,9 are converted to 2,5,9
182  */
condense(wref,wcnt,sig)183 condense(wref, wcnt, sig)
184 int	*wref;
185 int	wcnt;
186 char	*sig;
187 {
188 	register int i = 0;
189 	char wt[4];
190 	extern int wswap();
191 
192 	qsort(wref, wcnt, sizeof(int), wswap);
193 	sig[0] = 0;
194 	while (i < wcnt) {
195 		sprintf(wt,"%d",wref[i]);
196 		strcat(sig,wt);
197 		if ((i+2 < wcnt) && (wref[i] == (wref[i+2] - 2))) {
198 			while (wref[i] == (wref[i+1] - 1))
199 				i++;
200 			strcat(sig, "-");
201 		} else if (++i < wcnt)
202 			strcat(sig,",\\|");
203 	}
204 }
205 
wswap(iw1,iw2)206 wswap(iw1, iw2)
207 register int *iw1,*iw2;
208 {
209 	return(*iw1 - *iw2);
210 }
211