Plan 9 from Bell Labs’s /usr/web/sources/contrib/steve/root/sys/src/cmd/refer/refer7.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


# include "refer.h"
# include "error.h"

# define NRS 500
int	newr[NRS];

extern char *maynard;
int chkdup(char *tag)
{
	int	i;
	for (i = 1; i <= refnum; i++) {
		if (reftable[i] != 0 && strcmp(reftable[i], tag) == 0) {
			if(0&&maynard) {
				fprintf(stderr, "warning: duplicate ref: %s, %s\n", tag, maynard);
				continue;
			}
			return i;
		}
	}
	reftable[refnum+1] = rtp;
	if (refnum >= NRFTBL)
		err("too many references (%d) for table", refnum);
	strcpy (rtp, tag);
	while (*rtp++)
		;
	if (rtp > reftext + NRFTXT)
		err("reftext too big (%d)", rtp - reftext);
	return 0;
}

#define HBUFSIZE 256

void dumpold(void)
{
	FILE * fi;
	int	c, oc = -1, g1 = 0, nr = 1, h = 0, inh, hstart, puth = 0;
	char	headbuff1[HBUFSIZE], headbuff2[HBUFSIZE];
	char	*thishbuff, *lasthbuff, *t;

	if (!endpush) 
		return;
	fclose(fo);
#if D1
	fprintf(stderr, "closing %o\n", fo);
#endif
	fo = NULL;
#if D1
	fprintf(stderr, "dumpold for %s ftemp is %o stdout %o\n", tfile, ftemp, stdout);
#endif
	if (sort) {
		char	comm[200];
		lim512(tfile);
		sprintf(comm, "PATH=/york/bin:/bin:/usr/bin sort -fd %s -o %s", tfile, tfile);
		system(comm);
	}
	fi = fopen(tfile, "r");
#if D1
	fprintf(stderr, "reopened %s as %o\n", tfile, fi);
#endif
	if (fi == NULL) 
		return;
	flout();
	fprintf(ftemp, ".]<\n");
	thishbuff = headbuff1;
	lasthbuff = headbuff2;
	hstart = 1;
	inh = 1;
	while ((c = getc(fi)) >= 0) {
		if (c == '\n') {
			inh = 1;
			h = 0;
			if (oc == sep) 
				continue;
			nr++;
			g1 = 0;
		}
		oc = c;
		if (c == hsep) {
			if (headers) {
				thishbuff[h] = '\0';
#if D1
				if (!hstart)
					fprintf(stderr, "comparing \"%s\" with \"%s\"\n", thishbuff, lasthbuff);
#endif D1
				puth = inh && (hstart || strcmp(thishbuff, lasthbuff));
				hstart = 0;
				t = thishbuff;
				thishbuff = lasthbuff;
				lasthbuff = t;
				inh = 0;
			}
			continue;
		}
		if (c == sep) {
			putc('\n', ftemp);
			if (puth)
				fputs(".nr h] 1\n", ftemp);
			puth = 0;
			continue;
		}
		if (c == FLAG) {
			/* make old-new ref number table */
			char	tb[20];
			int	irr = 0;
			char	*s = tb;

			while ( (c = getc(fi)) != FLAG)
				*s++ = c;
			*s = 0;
			if (g1++ == 0) {
				irr = atoi(tb);
				if (irr >= NRS)
					err("too many references to renumber", 0);
				newr[irr] = nr;
			}
#if D1
			fprintf(stderr, "nr %d assigned to atoi(tb) %d\n", nr, irr);
#endif
			/* order is -R flag and is for wierdo referencing */
			if (order) 
				expkey (irr, irr, ftemp);

			else if (!labels) 
				fprintf(ftemp, "%d", nr);

			else 
				expkey (irr, nr, ftemp);

			continue;
		}
		if (headers && inh)
			thishbuff[h++] = c;
		putc(c, ftemp);
	}
	fclose(fi);
#ifndef D1
	remove(tfile);
#endif
	widelab();
	fprintf(ftemp, ".]>\n");
}

void recopy(char *fnam)
{
	int	c;

	fclose(ftemp);
#if D1
	fprintf(stderr, "recopy, fnam %s\n", fnam);
#endif

	ftemp = efopen(fnam, "r");
	for (;;) {
		c = getc(ftemp);
#if D1
		fprintf(stderr, "read char %o /%c/\n", c, c);
#endif
		if (c == FLAG) {
			char	tb[20];
			char	*s = tb;
			int	tbx;
			while ( (c = getc(ftemp)) != FLAG && c != EOF)
				*s++ = c;
			*s = 0;
			tbx = atoi(tb);
#if D1
			fprintf(stderr, "prelim number is %d\n", tbx);
#endif
			/* order is -R flag and is for wierdo referencing */
			if (order)
				expkey (tbx, tbx, stdout);
			else if (!labels)
				printf("%d", newr[tbx]);
			else
				expkey (tbx, newr[tbx], stdout);

			continue;
		}
		if (c == EOF) 
			break;
		putchar(c);
	}
#if D1
	fprintf(stderr, "finished reading file \n");
#endif
	fclose(ftemp);
	remove(fnam);
#if D1
	fprintf(stderr, "leaving recopy\n");
#endif
}

void lim512(char *tfile)
{
	/* this stupid program merely breaks up very long citations into lines of 512 bytes */
	FILE * f1, *f2;
	char	*zline, *p, *q;
	extern char	one[];
	int	ln, no;
	zline = one;
	f1 = efopen(tfile, "r");
	f2 = efopen(tdfile, "w");
#if D1
	fprintf(stderr, "in lim512 %s is %o, %s is %o\n", tfile, f1, tdfile, f2);
#endif
	while (fgets(zline, ANSLEN, f1)) {
		if (strlen(zline) < 512) {
			fputs(zline, f2);
			continue;
		}
		no = 'a';
		for (p = zline; *p != sep; p++)
			;
		*p++ = 0;
		ln = 500 - strlen(zline);
		while (strlen(p) > ln) {
			for (q = p + ln; q > p && *q != sep; q--)
				;
			*q++ = 0;
			fprintf(f2, "%s%c%c%s%c\n", zline, no++, sep, p, sep);
			p = q;
		}
		fprintf(f2, "%s%c%c%s", zline, no, sep, p);
	}
	fclose(f1); 
	fclose(f2);
#if D1
	fprintf(stderr, "removing in lim512\n");
#endif
	rename(tdfile, tfile);
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.