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

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


# include "refer.h"
static	char	seenstr[60];

static int etal(char *);
static int seen(int);

void putref(int n, char *tvec[])
{
	char	*s, *tx, *sq;
	char	buf1[LLINE], buf2[50];
	int	nauth = 0, ned = 0, edrev = authrev, i, lastype = 0, cch, macro = 0, la, le;
	int	lauth = 0, led = 0, ltitle = 0, lother = 0;

# if D1
	fprintf(stderr, "in putref, fo is %o\n", fo);
#endif
	if (!preserve)
		fprintf(fo, ".]-%c", sep);
	else
		fprintf(fo, ".[%c", sep);
# define dsde (macro? "de" : "ds")
# define ifnl (macro? sep : ' ')
	seenstr[0] = 0;
	for (i = 0; i < n; i++) {
		s = tvec[i];
		if (*s == 0) 
			continue;
		if (control(s[0]) ) {
			if (lastype && macro)
				fprintf(fo, "..%c", sep);
			if (control(s[1])) {
				cch = s[2];
				tx = s + 3;
				macro = 1;
			} else {
				cch = s[1];
				tx = s + 2;
				macro = 0;
			}
		} else {
			cch = lastype;
			tx = s;
		}
# if D1
		fprintf(stderr, "smallcaps %s cch %c\n", smallcaps, cch);
#endif
		if (preserve) {
			fprintf(fo, "%s%c", s, sep);
			continue;
		}
		if (mindex (smallcaps, cch))
			tx = caps(tx, buf1);
# if D1
		fprintf(stderr, " s %o tx %o %s\n", s, tx, tx);
#endif
		if (!control(s[0]) || (s[1] != 'A' && s[1] != 'E' && mindex(appfld, s[1]) && seen(s[1]))) /* append to previous item */ {
			if (control(s[0]))
				lastype = s[1];
			if (lastype != 0) {
				if (macro)
					fprintf(fo, "%s%c", tx, sep);
				else
					fprintf(fo, ".as [%c \" %s%c", lastype, tx, sep);
				if (lastype == 'T')
					ltitle = (mindex(".;,?", last(tx)) != 0);
				if (lastype == 'O')
					lother = (mindex(".;,?", last(tx)) != 0);
			}
			continue;
		}
		if (mindex(ignfld, cch)) /* skip XYZ[] */ {
			lastype = 0;
			continue;
		} else if (cch == 'A') {
			if (nauth < authrev)
				tx = revname(tx, buf2);
			if (nauth++ == 0)
				if (macro)
					fprintf(fo, ".de [%c%c%s%c", cch, sep, tx, sep);
				else
					fprintf(fo, ".ds [%c%s%c", cch, tx, sep);
			else {
				la = (tvec[i+1][1] != cch);
				fprintf(fo, ".as [%c \"", cch);
				if (!la)
					fprintf(fo, ",");
				if (la && !etal(tx)) {
					if (strncmp(tx, " others", 7) != 0)
						fprintf(fo, "%s", 
						    mindex(smallcaps, cch) ? " \\s-2\\*([z\\s+2" : "\\*([a");
					else
						fprintf(fo, "%s", 
						    mindex(smallcaps, cch) ? " \\s-2AND\\s+2" : " and");
				}
				fprintf(fo, "%s%c", tx, sep);
			}
			lauth = last(tx) == '.';
		} else if (cch == 'E') {
			if (ned < edrev)
				tx = revname(tx, buf2);
			if (ned++ == 0)
				if (macro)
					fprintf(fo, ".de [%c%c%s%c", cch, sep, tx, sep);
				else
					fprintf(fo, ".ds [%c%s%c", cch, tx, sep);
			else {
				le = (tvec[i+1][1] != cch);
				fprintf(fo, ".as [%c \"", cch);
				if (!le)
					fprintf(fo, ",");
				if (le && !etal(tx)) {
					if (strncmp(tx, " others", 7) != 0)
						fprintf(fo, "%s", 
						    mindex(smallcaps, cch) ? " \\s-2\\*([z\\s+2" : " \\*([a");
					else
						fprintf(fo, "%s", 
						    mindex(smallcaps, cch) ? " \\s-2AND\\s+2" : " and");
				}
				fprintf(fo, "%s%c", tx, sep);
			}
			led = last(tx) == '.';
		} else {
			if (cch == 'D' && yearonly) {
				sq = tx + strlen(tx);
				while (*--sq != ' ' && sq > tx)
					;
				if (sq > tx) 
					tx = sq;
			}
			if (macro)
				fprintf(fo, ".de [%c%c%s%c", cch, sep, tx, sep);
			else
				fprintf(fo, ".ds [%c%s%c", cch, tx, sep);
		}
		if (cch == 'P')
			fprintf(fo, ".nr [P %d%c", mindex(s, '-') != 0, sep);
		lastype = cch;
		if (cch == 'T')
			ltitle = (mindex(".;,?", last(tx)) != 0);
		if (cch == 'O')
			lother = (mindex(".;,?", last(tx)) != 0);
	}
	if (lastype && macro)
		fprintf(fo, "..%c", sep);
	if (!preserve) {
		fprintf(fo, ".nr [T %d%c", ltitle, sep);
		fprintf(fo, ".nr [A %d%c", lauth, sep);
		fprintf(fo, ".nr [E %d%c", led, sep);
		fprintf(fo, ".nr [O %d%c", lother, sep);
		fprintf (fo, ".][ %s%c", class(n, tvec), '\n');
	} else
		fprintf(fo, ".]\n");
}

int tabs(char *sv[], char *line, int nf)
{
	char	*p;
	int	n = 0;

	if (n >= nf)
		return -1;
	sv[n++] = line;
	for ( p = line; *p; p++) {
		if (*p == '\n') {
			*p = 0;
			if (n >= nf)
				return -1;
			sv[n++] = p + 1;
		}
	}
	return n - 1;
}

char*class(int nt, char *tv[])
{
	if (hastype (nt, tv, 'J'))
		return "1 journal-article";
	if (hastype (nt, tv, 'B'))
		return "3 article-in-book";
	if (hastype (nt, tv, 'R'))
		return "4 tech-report";
	if (hastype (nt, tv, 'G'))
		return "4 tech-report";
	if (hastype (nt, tv, 'I'))
		return "2 book";
	if (hastype (nt, tv, 'M'))
		return "5 bell-tm";
	return "0 other";
}

int hastype(int nt, char *tv[], int c)
{
	int	i;

	for (i = 0; i < nt; i++)
		if ( control(tv[i][0]) && tv[i][1] == c )
			return 1;
	return 0;
}

char *caps(char *a, char *b)
{
	char	*p;
	int	c, alph, this;

	p = b;
	alph = 0;
	while (c = *a++) {
		this = isalpha(c);
		if (this && alph == 1) {
			*b++ = '\\';
			*b++ = 's';
			*b++ = '-';
			*b++ = '2';
		}
		if (!this && alph > 1) {
			*b++ = '\\';
			*b++ = 's';
			*b++ = '+';
			*b++ = '2';
		}
		if (this)
			c &= (~040);
		*b++ = c;
		alph = this ? alph + 1 : 0;
	}
	if (alph > 1) {
		*b++ = '\\';
		*b++ = 's';
		*b++ = '+';
		*b++ = '2';
	}
	*b = 0;
	return p;
}

char *revname(char *s, char *b)
{
	char *init, *name, *jr, *p, *bcop;

	if (etal(s)) 
		return s;
	bcop = b;
	init = name = s;
	while (*name)
		name++;
	jr = name;
	while (name > init && *name != ' ')
		name--;
	if (name[-1] == ',' || name[-1] == '(' ) {
		jr = --name;
		while (name > init && *name != ' ')
			name--;
	}
	p = name;
	while (p < jr)
		*b++ = *p++;
	if (init < name && nocomma == 0)
		*b++ = ',';
	while (init < name)
		*b++ = *init++;
	if (*jr)
		jr++;
	while (*jr)
		*b++ = *jr++;
	*b = 0;
	return bcop;
}

int last(char *s)
{
	while (*s) 
		s++;
	return *--s;
}

static int seen(int c)
{
	char *s;

	s = mindex(seenstr, c);
	if (s != NULL) 
		return 1;
	for (s = seenstr; *s; s++)
		;
	*s++ = c;
	*s = 0;
	return 0;
}

static int etal(char *s)
{
	return strncmp(s, " et al", 6) == 0 || strncmp(s, " et. al.", 8) == 0;
}

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.