xref: /titanic_53/usr/src/cmd/refer/refer7.c (revision 11a8fa6cb17403e630122ac19b39a323c6e64142)
1*11a8fa6cSceastha /*
2*11a8fa6cSceastha  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3*11a8fa6cSceastha  * Use is subject to license terms.
4*11a8fa6cSceastha  */
5*11a8fa6cSceastha 
67c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
77c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
87c478bd9Sstevel@tonic-gate 
97c478bd9Sstevel@tonic-gate /*
107c478bd9Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
117c478bd9Sstevel@tonic-gate  * All rights reserved. The Berkeley software License Agreement
127c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
137c478bd9Sstevel@tonic-gate  */
147c478bd9Sstevel@tonic-gate 
157c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
167c478bd9Sstevel@tonic-gate 
177c478bd9Sstevel@tonic-gate #include "refer..c"
187c478bd9Sstevel@tonic-gate #include <locale.h>
197c478bd9Sstevel@tonic-gate 
207c478bd9Sstevel@tonic-gate int newr[250];
217c478bd9Sstevel@tonic-gate 
22*11a8fa6cSceastha extern void err();
23*11a8fa6cSceastha extern void flout();
24*11a8fa6cSceastha 
25*11a8fa6cSceastha static void condense(int *, int, char *);
26*11a8fa6cSceastha 
27*11a8fa6cSceastha int
chkdup(char * tag)28*11a8fa6cSceastha chkdup(char *tag)
297c478bd9Sstevel@tonic-gate {
307c478bd9Sstevel@tonic-gate 	int i;
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate 	for (i = 1; i <= refnum; i++) {
337c478bd9Sstevel@tonic-gate 		if (reftable[i] && strcmp(reftable[i], tag) == 0)
347c478bd9Sstevel@tonic-gate 			return (i);
357c478bd9Sstevel@tonic-gate 	}
367c478bd9Sstevel@tonic-gate 	reftable[refnum+1] = rtp;
377c478bd9Sstevel@tonic-gate 	if (refnum >= NRFTBL)
387c478bd9Sstevel@tonic-gate 		err(gettext("too many references (%d) for table"), refnum);
397c478bd9Sstevel@tonic-gate 	strcpy(rtp, tag);
40*11a8fa6cSceastha 	while (*rtp++)
41*11a8fa6cSceastha 		;
427c478bd9Sstevel@tonic-gate 	if (rtp > reftext + NRFTXT)
437c478bd9Sstevel@tonic-gate 		err(gettext("reference pointers too long (%d)"), rtp-reftext);
447c478bd9Sstevel@tonic-gate 	return (0);
457c478bd9Sstevel@tonic-gate }
467c478bd9Sstevel@tonic-gate 
47*11a8fa6cSceastha void
dumpold(void)48*11a8fa6cSceastha dumpold(void)
497c478bd9Sstevel@tonic-gate {
507c478bd9Sstevel@tonic-gate 	FILE *fi;
517c478bd9Sstevel@tonic-gate 	int c, g1 = 0, nr = 1;
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate 	if (!endpush)
547c478bd9Sstevel@tonic-gate 		return;
557c478bd9Sstevel@tonic-gate 	fclose(fo);
567c478bd9Sstevel@tonic-gate 	fo = NULL;
577c478bd9Sstevel@tonic-gate 	if (sort) {
587c478bd9Sstevel@tonic-gate 		char comm[100];
597c478bd9Sstevel@tonic-gate 		sprintf(comm, "sort -f %s -o %s", tfile, tfile);
607c478bd9Sstevel@tonic-gate 		system(comm);
617c478bd9Sstevel@tonic-gate 	}
627c478bd9Sstevel@tonic-gate 	fi = fopen(tfile, "r");
637c478bd9Sstevel@tonic-gate 	if (fi == NULL)
647c478bd9Sstevel@tonic-gate 		return;
657c478bd9Sstevel@tonic-gate 	flout();
667c478bd9Sstevel@tonic-gate 	fprintf(ftemp, ".]<\n");
677c478bd9Sstevel@tonic-gate 	while ((c = getc(fi)) > 0) {
687c478bd9Sstevel@tonic-gate 		if (c == '\n') {
697c478bd9Sstevel@tonic-gate 			nr++;
707c478bd9Sstevel@tonic-gate 			g1 = 0;
717c478bd9Sstevel@tonic-gate 		}
727c478bd9Sstevel@tonic-gate 		if (c == sep)
737c478bd9Sstevel@tonic-gate 			c = '\n';
747c478bd9Sstevel@tonic-gate 		if (c == FLAG) {
757c478bd9Sstevel@tonic-gate 			/* make old-new ref number table */
767c478bd9Sstevel@tonic-gate 			char tb[20];
777c478bd9Sstevel@tonic-gate 			char *s = tb;
787c478bd9Sstevel@tonic-gate 			while ((c = getc(fi)) != FLAG)
797c478bd9Sstevel@tonic-gate 				*s++ = c;
807c478bd9Sstevel@tonic-gate 			*s = 0;
817c478bd9Sstevel@tonic-gate 			if (g1++ == 0)
827c478bd9Sstevel@tonic-gate 				newr[atoi(tb)] = nr;
837c478bd9Sstevel@tonic-gate #if EBUG
84*11a8fa6cSceastha 			fprintf(stderr, "nr %d assigned to atoi(tb) %d\n",
85*11a8fa6cSceastha 			    nr, atoi(tb));
867c478bd9Sstevel@tonic-gate #endif
877c478bd9Sstevel@tonic-gate 			fprintf(ftemp, "%d", nr);
887c478bd9Sstevel@tonic-gate 			continue;
897c478bd9Sstevel@tonic-gate 		}
907c478bd9Sstevel@tonic-gate 		putc(c, ftemp);
917c478bd9Sstevel@tonic-gate 	}
927c478bd9Sstevel@tonic-gate 	fclose(fi);
937c478bd9Sstevel@tonic-gate #ifndef TF
947c478bd9Sstevel@tonic-gate 	unlink(tfile);
957c478bd9Sstevel@tonic-gate #endif
967c478bd9Sstevel@tonic-gate 	fprintf(ftemp, ".]>\n");
977c478bd9Sstevel@tonic-gate }
987c478bd9Sstevel@tonic-gate 
99*11a8fa6cSceastha void
recopy(char * fnam)100*11a8fa6cSceastha recopy(char *fnam)
1017c478bd9Sstevel@tonic-gate {
1027c478bd9Sstevel@tonic-gate 	int c;
1037c478bd9Sstevel@tonic-gate 	int *wref = NULL;
1047c478bd9Sstevel@tonic-gate 	int wcnt = 0;
1057c478bd9Sstevel@tonic-gate 	int wsize = 50;
1067c478bd9Sstevel@tonic-gate 	int finalrn;
1077c478bd9Sstevel@tonic-gate 	char sig[MXSIG];
1087c478bd9Sstevel@tonic-gate 	extern int *realloc();
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	wref = (int *)calloc((unsigned)wsize, (unsigned)sizeof (int));
1117c478bd9Sstevel@tonic-gate 	fclose(ftemp);
1127c478bd9Sstevel@tonic-gate 	ftemp = fopen(fnam, "r");
1137c478bd9Sstevel@tonic-gate 	if (ftemp == NULL) {
1147c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("Can't reopen %s\n"), fnam);
1157c478bd9Sstevel@tonic-gate 		exit(1);
1167c478bd9Sstevel@tonic-gate 	}
1177c478bd9Sstevel@tonic-gate 	while ((c = getc(ftemp)) != EOF) {
1187c478bd9Sstevel@tonic-gate 		if (c == FLAG) {
1197c478bd9Sstevel@tonic-gate 			char tb[10];
1207c478bd9Sstevel@tonic-gate 			char *s = tb;
1217c478bd9Sstevel@tonic-gate 			while ((c = getc(ftemp)) != FLAG)
1227c478bd9Sstevel@tonic-gate 				*s++ = c;
1237c478bd9Sstevel@tonic-gate 			*s = 0;
1247c478bd9Sstevel@tonic-gate 			/*
1257c478bd9Sstevel@tonic-gate 			 * If sort was done, permute the reference number
1267c478bd9Sstevel@tonic-gate 			 * to obtain the final reference number, finalrn.
1277c478bd9Sstevel@tonic-gate 			 */
1287c478bd9Sstevel@tonic-gate 			if (sort)
1297c478bd9Sstevel@tonic-gate 				finalrn = newr[atoi(tb)];
1307c478bd9Sstevel@tonic-gate 			else
1317c478bd9Sstevel@tonic-gate 				finalrn = atoi(tb);
132*11a8fa6cSceastha 			if ((++wcnt > wsize) && ((wref = realloc(wref,
133*11a8fa6cSceastha 			    (wsize += 50) * sizeof (int))) == NULL)) {
134*11a8fa6cSceastha 				fprintf(stderr, gettext(
135*11a8fa6cSceastha 				    "Ref condense out of memory."));
1367c478bd9Sstevel@tonic-gate 				exit(1);
1377c478bd9Sstevel@tonic-gate 			}
1387c478bd9Sstevel@tonic-gate 			wref[wcnt-1] = finalrn;
1397c478bd9Sstevel@tonic-gate 			if ((c = getc(ftemp)) == AFLAG)
1407c478bd9Sstevel@tonic-gate 				continue;
1417c478bd9Sstevel@tonic-gate 			wref[wcnt] = 0;
1427c478bd9Sstevel@tonic-gate 			condense(wref, wcnt, sig);
1437c478bd9Sstevel@tonic-gate 			wcnt = 0;
1447c478bd9Sstevel@tonic-gate 			printf("%s", sig);
1457c478bd9Sstevel@tonic-gate 		}
1467c478bd9Sstevel@tonic-gate 		putchar(c);
1477c478bd9Sstevel@tonic-gate 	}
1487c478bd9Sstevel@tonic-gate 	fclose(ftemp);
1497c478bd9Sstevel@tonic-gate 	unlink(fnam);
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate /*
1537c478bd9Sstevel@tonic-gate  * sort and condense reference signals when they are placed in
1547c478bd9Sstevel@tonic-gate  * the text. Viz, the signal 1,2,3,4 is condensed to 1-4 and signals
1557c478bd9Sstevel@tonic-gate  * of the form 5,2,9 are converted to 2,5,9
1567c478bd9Sstevel@tonic-gate  */
157*11a8fa6cSceastha static void
condense(int * wref,int wcnt,char * sig)158*11a8fa6cSceastha condense(int *wref, int wcnt, char *sig)
1597c478bd9Sstevel@tonic-gate {
160*11a8fa6cSceastha 	int i = 0;
1617c478bd9Sstevel@tonic-gate 	char wt[4];
1627c478bd9Sstevel@tonic-gate 	extern int wswap();
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	qsort(wref, wcnt, sizeof (int), wswap);
1657c478bd9Sstevel@tonic-gate 	sig[0] = 0;
1667c478bd9Sstevel@tonic-gate 	while (i < wcnt) {
1677c478bd9Sstevel@tonic-gate 		sprintf(wt, "%d", wref[i]);
1687c478bd9Sstevel@tonic-gate 		strcat(sig, wt);
1697c478bd9Sstevel@tonic-gate 		if ((i+2 < wcnt) && (wref[i] == (wref[i+2] - 2))) {
1707c478bd9Sstevel@tonic-gate 			while (wref[i] == (wref[i+1] - 1))
1717c478bd9Sstevel@tonic-gate 				i++;
1727c478bd9Sstevel@tonic-gate 			strcat(sig, "-");
1737c478bd9Sstevel@tonic-gate 		} else if (++i < wcnt)
1747c478bd9Sstevel@tonic-gate 			strcat(sig, ",\\|");
1757c478bd9Sstevel@tonic-gate 	}
1767c478bd9Sstevel@tonic-gate }
1777c478bd9Sstevel@tonic-gate 
178*11a8fa6cSceastha int
wswap(int * iw1,int * iw2)179*11a8fa6cSceastha wswap(int *iw1, int *iw2)
1807c478bd9Sstevel@tonic-gate {
1817c478bd9Sstevel@tonic-gate 	return (*iw1 - *iw2);
1827c478bd9Sstevel@tonic-gate }
183