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

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


# include "refer.h"
# include "error.h"
# define NFLAB 3000
# define NLABC 1000
#if SMALL
# define SIGLEN 400
#else
# define SIGLEN 2000
#endif
static char	sig[SIGLEN];

static char	bflab[NFLAB];
static char	*labtab[NLABC];
char	*lookat(), *artskp();
static char	*lbp = bflab;
static char	labc[NLABC];
static char	stbuff[50];
static int	prevsig;

void putsig(int nf, char *flds[], int nref, char *nstline, char *endline)
{
	/* choose signal style */
	char	t[200], t1[200], t2[200], format[10], *sd;
	int	another;
	int	addon = 0, addlet;
	char	*stline, *pr;
	static FILE *fhide = 0;

# if D1
	fprintf(stderr, "in putsig, bare %d labels %o nf %d nref %d\n", bare, labels, nf, nref);
#endif
	if (labels) {
		if (nf == 0) /* old */
			sprintf(t, "%s%c", labtab[nref], labc[nref]);
		else
		 {
			*t = 0;
			if (keywant) {
				pr = fpar(nf, flds, t1, keywant, 1, 0);
				if (pr) 
					strcpy(t, pr);
			}
			if (t[0] == 0) {
				if (labblkflg) {
					if (nmlen != INF_LENGTH)
						sprintf(format, "%%.%ds%s%%s", nmlen, labblkflg);
					else
						sprintf(format, "%%s%s%%s", labblkflg);
				} else

					sprintf(format, (nmlen == INF_LENGTH) ? "%%s%%s" : "%%.%ds%%s", nmlen);
# if D1
				fprintf(stderr, "format is /%s/\n", format);
#endif
				/* format is %s%s for default labels or %.3s%s eg if wanted */
				sd = fpar(nf, flds, t2, 'D', 1, 0);
				if (sd == NULL) 
					sd = "";
				if (dtlen != INF_LENGTH) {
					char	*sdb;
					for (sdb = sd; *sd; sd++)
						;
					sd = sd - dtlen;
					if (sd < sdb) 
						sd = sdb;
				}
				sprintf(t, format, fpar(nf, flds, t1, 'A', 1, 0), sd);
# if D1
				fprintf(stderr, "tag is /%s/\n", t);
#endif
			}
			if (keywant) {
				addon = 0;
				for (sd = t; *sd; sd++)
					;
				if (*--sd == '-') {
					addon = 1;
					*sd = 0;
				}
			}
			addlet = keylet(t, nref);
			if (!keywant || addon)
				addch( t, addlet);
		}
		if (sort)
			sprintf(t, "%c%d%c", FLAG, nref, FLAG);
	} else	 {
		if (sort)
			sprintf(t, "%c%d%c", FLAG, nref, FLAG);
		else
			sprintf(t, "%d", nref);
	}
	another = (sd = lookat()) != NULL && prefix (".[", sd);
# if D1
	fprintf(stderr, "t leng %d another %d\n", strlen(t), another);
#endif
	if (another && (strcmp(".[\n", sd) != SAME))
		fprintf(stderr, "File %s, line %d- punctuation ignored from: %s", Ifile, Iline, sd);
	if (bare == 0)
		strcat (sig, t);
# if D1
	fprintf(stderr, "past strcat\n");
#endif
	if (strlen(sig) >= SIGLEN)
		err("signal length exceeds %d chars", SIGLEN);
# if D1
	fprintf(stderr, "sig is now %s leng %d\n", sig, strlen(sig));
#endif
	trimnl(nstline);
	trimnl(endline);
	stline = stbuff;
	if (prevsig == 0) {
		strcpy (stline, nstline);
		prevsig = 1;
	}
	if (stline[2] || endline[2]) {
		stline += 2;
		endline += 2;
	} else {
		stline  = "\\*([.";
		endline = "\\*(.]";
	}
# if D1
	fprintf(stderr, "bare %d fhide %o fo %o another %d\n", bare, fhide, fo, another);
#endif
	if (bare == 0) {
		if (another == 0) {
			sprintf(t1, "%s%s%s\n", stline, sig, endline);
			append(t1);
			flout();
			sig[0] = 0;
			prevsig = 0;
			if (fo != NULL && fo == fhide) {
				int	ch;
# if D1
				fprintf(stderr, "more hiding\n");
#endif
				fclose(fhide); 
				fhide = efopen(hidenam, "r");
				fo = ftemp;
				while ((ch = getc(fhide)) != EOF)
					putc(ch, fo);
				fclose(fhide);
				remove(hidenam);
# if D1
				fprintf(stderr, "past this stuff\n");
#endif
			}
		} else {
			if (labsepstr)
				strcat(sig, labsepstr);
			else
				strcat (sig, (labels ? ", " : ",\\|"));
			/* hide if need be */
			if (fo == ftemp) {
				sprintf(hidenam, "/tmp/rj%dc", getpid());
# if D1
				fprintf(stderr, "hiding in %s\n", hidenam);
#endif
				fhide = efopen(hidenam, "w");
				fo = fhide;
			}
		}
	}
	if (bare < 2)
		if (nf > 0) {
			fprintf(fo, ".ds [F %s%c", t, sep);
			biglab(t);
		}
	if (bare > 0)
		flout();
# if D1
	fprintf(stderr, "sig is now %s\n", sig);
#endif
}

char *fpar(int nf, char *flds[], char *out, int c, int seq, int prepend)
{
	char	*p, *s;
	int	i, fnd = 0;
	for (i = 0; i < nf; i++)
		if (control(flds[i][0]) && flds[i][1] == c && ++fnd >= seq) {
			if (c == 'L') {
				p = flds[i] + 3;
				strcpy(out, p);
				return out;
			}
			if (c != 'A' && c != 'E' && c != 'D') /* if not author, editor, date use first word */ {
				p = flds[i] + 3;
				p = artskp (p);
				mycpy2(out, p, 20);
				return out;
			}
			if ((c == 'A' || c == 'E') && lfirst(p = flds[i] + 3)) /* author/editor in style Jones, A. */ {
				for (s = out; *p != ','; p++)
					*s++ = *p;
				*s++ = 0;
				if (prepend) {
					while (isspace(*p))
						p++;
					*s++ = *p;
					*s = 0;
				}
				return out;
			}
			for (s = p = flds[i] + 2; *p; p++)
				;
			while (p > s && *p != ' ') 
				p--;
			/* special wart for authors/editors */
			if ((c == 'A' || c == 'E') && (p[-1] == ',' || p[1] == '(')) {
				p--;
				while (p > s && *p != ' ') 
					p--;
				mycpy (out, p + 1);
			} else
				strcpy (out, p + 1);
			if ((c == 'A' || c == 'E') && prepend)
				initadd(out, flds[i] + 2, p);
			return out;
		}
	return 0;
}

void putkey(int nf, char *flds[], int nref, char *keystr)
{
	char	t1[50], *sf;
	int	ctype, i, count, hit, ed = 0;
# if D1
	int	klim = 0;
	fprintf(stderr, "in putkey, nf %d nref %d keystr %.20s\n", nf, nref, keystr);
#endif
# if D5
	return;
#endif
	fprintf(fo, ".\\\"");
	if (nf <= 0)
		fprintf(fo, "%s%c%c", labtab[nref], labc[nref], sep);
	else
	 {
		while (ctype = *keystr++) {
# if D1
			if (++klim > 100) 
				err("keystring ridiculous", 0);
#endif
			count = atoi(keystr);
			if (*keystr == '+') 
				count = 999;
			if (count <= 0) 
				count = 1;
			for (; ; ) {
				hit = 0;
				for (i = 1; i <= count; i++) {
					sf = fpar(nf, flds, t1, ctype, i, 1);
					if (sf == 0)
						break;
					hit++;
					sf = artskp(sf);
					fprintf(fo, "%s%c", sf, '-');
				}
				if (ctype != 'A' || hit)
					break;
				ed++;
				ctype = 'E';
			}
		}
		fprintf(fo, "%s-%c%d%c", ed ? "ed." : "", hsep, nref, sep);
	}
# if D1
	fprintf(stderr, "returning from putkey\n");
#endif
}

int keylet(char *t, int nref)
{
	int	i;
	int	x = 'a' - 1;
	for (i = 1; i < nref; i++) {
		if (strcmp(labtab[i], t) == 0)
			x = labc[i];
	}
	if ((lbp - bflab)+strlen(t)+1 >= NFLAB)
		errline("bflab overflow (%d); %.20s...", Iline, NFLAB, t);
	if (nref >= NLABC)
		err ("nref in labc overflow (%d)", NLABC);
	strcpy(labtab[nref] = lbp, t);
	while (*lbp++)
		;
#if D1
	fprintf(stderr, "lbp up to %d of 2000\n", lbp - bflab);
#endif
	return labc[nref] = x + 1;
}

void mycpy(char *s, char *t)
{
	while (*t && *t != ',' && *t != ' ')
		*s++ = *t++;
	*s = 0;
}

void mycpy2(char *s, char *t, int n)
{
	int	c;
	while (n-- && (c = *t++) > 0) {
		if (c == ' ')
			c = '-';
		*s++ = c;
	}
	*s = 0;
}

void initadd(char *to, char *from, char *stop)
{
	int	c, nalph = 1;

	while (*to) 
		to++;
	while (from < stop) {
		c = *from++;
		if (!isalpha(c)) {
			if (nalph)
				*to++ = '.';
			nalph = 0;
			continue;
		}
		if (nalph++ == 0)
			*to++ = c;
	}
	*to = 0;
}

static char	*articles[] = {
	"the ", "an ", "a ", 0
};

char *artskp(char *s)
{
	/* skips over initial "a ", "an ", or "the " in s */
	char	**p, *r1, *r2;
	for (p = articles; *p; p++) {
		r2 = s;
		for (r1 = *p; ((*r1 ^ *r2) & ~040 ) == 0; r1++)
			r2++;
		if (*r1 == 0 && *r2 != 0)
			return r2;
	}
	return s;
}

void expkey(int or, int nr, FILE *fout)
{
	int	uniq, less, i;
	char	*s, temp[100];
# if D1
	fprintf(stderr, "old %d key %s: '%c' new %d\n", or, labtab[or], labc[or], nr);
#endif
	/* is this unique? how many are before it ? */
	uniq = 1; 
	less = 'a';
	s = labtab[or];
	for (i = 1; i <= refnum; i++) {
		if (i == or) 
			continue;
		if (strcmp(labtab[i], s) != SAME)
			continue;
		uniq = 0;
		if (newr[i] != 0 && newr[i] < nr)
			less++;
	}
	if (uniq)
		sprintf(temp, "%s", s);
	else
		sprintf(temp, "%s%c", s, less);
	biglab(temp);

	/* order = -R flag and is for wierdo reference lableling */
	if (order) 
		fprintf(fout, "%d", nr);
	else 
		fprintf(fout, "%s", temp);
}

int lfirst(char *s)
{
	/* decides if s is name of format Jones, A */
	char	*p;
	p = strchr(s, ',');
	if (p == NULL) 
		return 0;
	while (isspace(*++p)) 
		;
	if (strncmp(p, "Jr", 2) == 0)
		return 0;
	if (strncmp(p, "II", 2) == 0)
		return 0;
	if (isupper(*p))
		return 1;
	return 0;
}

static char	longlab[100] = "";
void biglab(char *s)
{
	if (strlen(s) > strlen(longlab))
		strcpy(longlab, s);
}

void widelab(void)
{
	/* order = -R flag and is for wierdo reference lableling */
	if (order)  
		printf(".nr [W \\w'1.'\n");
	else 
		printf(".nr [W \\w'%s'\n", longlab);
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].