xref: /titanic_52/usr/src/cmd/refer/refer6.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 #define	dsde (macro? "de" : "ds")
197c478bd9Sstevel@tonic-gate #define	ifnl (macro? sep : ' ')
207c478bd9Sstevel@tonic-gate 
21*11a8fa6cSceastha extern int control();
22*11a8fa6cSceastha extern char *mindex();
23*11a8fa6cSceastha 
24*11a8fa6cSceastha int hastype(int, char *[], char);
25*11a8fa6cSceastha static char last(char *);
26*11a8fa6cSceastha char *class(int, char *[]);
27*11a8fa6cSceastha char *caps(char *, char *);
28*11a8fa6cSceastha char *revauth(char *, char *);
29*11a8fa6cSceastha 
30*11a8fa6cSceastha void
31*11a8fa6cSceastha putref(int n, char *tvec[])
327c478bd9Sstevel@tonic-gate {
337c478bd9Sstevel@tonic-gate 	char *s, *tx;
347c478bd9Sstevel@tonic-gate 	char buf1[BUFSIZ], buf2[50];
357c478bd9Sstevel@tonic-gate 	int nauth = 0, i, lastype = 0, cch, macro = 0, la;
367c478bd9Sstevel@tonic-gate 	int lauth = 0, ltitle = 0, lother = 0;
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate 	fprintf(fo, ".]-%c", sep);
397c478bd9Sstevel@tonic-gate 	for (i = 0; i < n; i++) {
407c478bd9Sstevel@tonic-gate 		s = tvec[i];
417c478bd9Sstevel@tonic-gate 		if (*s == 0)
427c478bd9Sstevel@tonic-gate 			continue;
437c478bd9Sstevel@tonic-gate 		if (control(s[0])) {
447c478bd9Sstevel@tonic-gate 			if (lastype && macro)
457c478bd9Sstevel@tonic-gate 				fprintf(fo, "..%c", sep);
467c478bd9Sstevel@tonic-gate 			if (control(s[1])) {
477c478bd9Sstevel@tonic-gate 				cch = s[2];
487c478bd9Sstevel@tonic-gate 				tx = s+3;
497c478bd9Sstevel@tonic-gate 				macro = 1;
50*11a8fa6cSceastha 			} else {
517c478bd9Sstevel@tonic-gate 				cch = s[1];
527c478bd9Sstevel@tonic-gate 				tx = s+2;
537c478bd9Sstevel@tonic-gate 				macro = 0;
547c478bd9Sstevel@tonic-gate 			}
55*11a8fa6cSceastha 		} else {
567c478bd9Sstevel@tonic-gate 			cch = lastype;
577c478bd9Sstevel@tonic-gate 			tx = s;
587c478bd9Sstevel@tonic-gate 		}
597c478bd9Sstevel@tonic-gate #if EBUG
607c478bd9Sstevel@tonic-gate 		fprintf(stderr, "smallcaps %s cch %c\n", smallcaps, cch);
617c478bd9Sstevel@tonic-gate #endif
627c478bd9Sstevel@tonic-gate 		if (mindex(smallcaps, cch))
637c478bd9Sstevel@tonic-gate 			tx = caps(tx, buf1);
647c478bd9Sstevel@tonic-gate #if EBUG
657c478bd9Sstevel@tonic-gate 		fprintf(stderr, " s %o tx %o %s\n", s, tx, tx);
667c478bd9Sstevel@tonic-gate #endif
677c478bd9Sstevel@tonic-gate 		if (!control(s[0])) {	/* append to previous item */
687c478bd9Sstevel@tonic-gate 			if (lastype != 0) {
697c478bd9Sstevel@tonic-gate 				if (macro)
707c478bd9Sstevel@tonic-gate 					fprintf(fo, "%s%c", tx, sep);
717c478bd9Sstevel@tonic-gate 				else
72*11a8fa6cSceastha 					fprintf(fo, ".as [%c \" %s%c",
73*11a8fa6cSceastha 					    lastype, tx, sep);
747c478bd9Sstevel@tonic-gate 				if (lastype == 'T')
75*11a8fa6cSceastha 					ltitle = (mindex(".;,?",
76*11a8fa6cSceastha 					    last(tx)) != 0);
777c478bd9Sstevel@tonic-gate 				if (lastype == 'A')
787c478bd9Sstevel@tonic-gate 					lauth = last(tx) == '.';
797c478bd9Sstevel@tonic-gate 			}
807c478bd9Sstevel@tonic-gate 			continue;
817c478bd9Sstevel@tonic-gate 		}
827c478bd9Sstevel@tonic-gate 		if (mindex("XYZ[]", cch)) {	/* skip these */
837c478bd9Sstevel@tonic-gate 			lastype = 0;
847c478bd9Sstevel@tonic-gate 			continue;
85*11a8fa6cSceastha 		} else {
867c478bd9Sstevel@tonic-gate 			if (cch == 'A') {
877c478bd9Sstevel@tonic-gate 				if (nauth < authrev)
887c478bd9Sstevel@tonic-gate 					tx = revauth(tx, buf2);
897c478bd9Sstevel@tonic-gate 				if (nauth++ == 0)
907c478bd9Sstevel@tonic-gate 					if (macro)
91*11a8fa6cSceastha 						fprintf(fo, ".de [%c%c%s%c",
92*11a8fa6cSceastha 						    cch, sep, tx, sep);
937c478bd9Sstevel@tonic-gate 					else
94*11a8fa6cSceastha 						fprintf(fo, ".ds [%c%s%c",
95*11a8fa6cSceastha 						    cch, tx, sep);
967c478bd9Sstevel@tonic-gate 				else {
977c478bd9Sstevel@tonic-gate 					la = (tvec[i+1][1] != 'A');
987c478bd9Sstevel@tonic-gate 					fprintf(fo, ".as [A \"");
997c478bd9Sstevel@tonic-gate 					if (la == 0 || nauth != 2)
1007c478bd9Sstevel@tonic-gate 						fprintf(fo, ",");
1017c478bd9Sstevel@tonic-gate 					if (la)
1027c478bd9Sstevel@tonic-gate 						fprintf(fo, "%s",
103*11a8fa6cSceastha 						    mindex(smallcaps, 'A') ?
104*11a8fa6cSceastha 						    " \\s-2AND\\s+2" : " and");
1057c478bd9Sstevel@tonic-gate 					fprintf(fo, "%s%c", tx, sep);
1067c478bd9Sstevel@tonic-gate 				}
1077c478bd9Sstevel@tonic-gate 				lauth = last(tx) == '.';
108*11a8fa6cSceastha 			} else {
1097c478bd9Sstevel@tonic-gate 				if (macro)
110*11a8fa6cSceastha 					fprintf(fo, ".de [%c%c%s%c",
111*11a8fa6cSceastha 					    cch, sep, tx, sep);
1127c478bd9Sstevel@tonic-gate 				else
113*11a8fa6cSceastha 					fprintf(fo, ".ds [%c%s%c",
114*11a8fa6cSceastha 					    cch, tx, sep);
1157c478bd9Sstevel@tonic-gate 			}
1167c478bd9Sstevel@tonic-gate 		}
1177c478bd9Sstevel@tonic-gate 		if (cch == 'P')
1187c478bd9Sstevel@tonic-gate 			fprintf(fo, ".nr [P %d%c", mindex(s, '-') != 0, sep);
1197c478bd9Sstevel@tonic-gate 		lastype = cch;
1207c478bd9Sstevel@tonic-gate 		if (cch == 'T')
1217c478bd9Sstevel@tonic-gate 			ltitle = (mindex(".;,?", last(tx)) != 0);
1227c478bd9Sstevel@tonic-gate 		if (cch == 'O')
1237c478bd9Sstevel@tonic-gate 			lother = (mindex(".;,?", last(tx)) != 0);
1247c478bd9Sstevel@tonic-gate 	}
1257c478bd9Sstevel@tonic-gate 	if (lastype && macro)
1267c478bd9Sstevel@tonic-gate 		fprintf(fo, "..%c", sep);
1277c478bd9Sstevel@tonic-gate 	fprintf(fo, ".nr [T %d%c", ltitle, sep);
1287c478bd9Sstevel@tonic-gate 	fprintf(fo, ".nr [A %d%c", lauth, sep);
1297c478bd9Sstevel@tonic-gate 	fprintf(fo, ".nr [O %d%c", lother, sep);
1307c478bd9Sstevel@tonic-gate 	fprintf(fo, ".][ %s%c", class(n, tvec), '\n');
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate 
133*11a8fa6cSceastha int
134*11a8fa6cSceastha tabs(char *sv[], char *line)
1357c478bd9Sstevel@tonic-gate {
1367c478bd9Sstevel@tonic-gate 	char *p;
1377c478bd9Sstevel@tonic-gate 	int n = 0;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	sv[n++] = line;
1407c478bd9Sstevel@tonic-gate 	for (p = line; *p; p++) {
1417c478bd9Sstevel@tonic-gate 		if (*p == '\n') {
1427c478bd9Sstevel@tonic-gate 			*p = 0;
1437c478bd9Sstevel@tonic-gate 			sv[n++] = p+1;
1447c478bd9Sstevel@tonic-gate 		}
1457c478bd9Sstevel@tonic-gate 	}
1467c478bd9Sstevel@tonic-gate 	return (n-1);
1477c478bd9Sstevel@tonic-gate }
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate char *
150*11a8fa6cSceastha class(int nt, char *tv[])
1517c478bd9Sstevel@tonic-gate {
1527c478bd9Sstevel@tonic-gate 	if (hastype(nt, tv, 'J'))
1537c478bd9Sstevel@tonic-gate 		return ("1 journal-article");
1547c478bd9Sstevel@tonic-gate 	if (hastype(nt, tv, 'B'))
1557c478bd9Sstevel@tonic-gate 		return ("3 article-in-book");
1567c478bd9Sstevel@tonic-gate 	if (hastype(nt, tv, 'R'))
1577c478bd9Sstevel@tonic-gate 		return ("4 tech-report");
1587c478bd9Sstevel@tonic-gate 	if (hastype(nt, tv, 'G'))
1597c478bd9Sstevel@tonic-gate 		return ("4 tech-report");
1607c478bd9Sstevel@tonic-gate 	if (hastype(nt, tv, 'I'))
1617c478bd9Sstevel@tonic-gate 		return ("2 book");
1627c478bd9Sstevel@tonic-gate 	if (hastype(nt, tv, 'M'))
1637c478bd9Sstevel@tonic-gate 		return ("5 bell-tm");
1647c478bd9Sstevel@tonic-gate 	return ("0 other");
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate 
167*11a8fa6cSceastha int
168*11a8fa6cSceastha hastype(int nt, char *tv[], char c)
1697c478bd9Sstevel@tonic-gate {
1707c478bd9Sstevel@tonic-gate 	int i;
1717c478bd9Sstevel@tonic-gate 	for (i = 0; i < nt; i++)
1727c478bd9Sstevel@tonic-gate 		if (control(tv[i][0]) && tv[i][1] == c)
1737c478bd9Sstevel@tonic-gate 			return (1);
1747c478bd9Sstevel@tonic-gate 	return (0);
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate char *
178*11a8fa6cSceastha caps(char *a, char *b)
1797c478bd9Sstevel@tonic-gate {
1807c478bd9Sstevel@tonic-gate 	char *p;
1817c478bd9Sstevel@tonic-gate 	int c, alph, this;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	p = b;
1847c478bd9Sstevel@tonic-gate 	alph = 0;
1857c478bd9Sstevel@tonic-gate 	while (c = *a++) {
1867c478bd9Sstevel@tonic-gate 		this = isalpha(c);
1877c478bd9Sstevel@tonic-gate 		if (this && alph == 1) {
1887c478bd9Sstevel@tonic-gate 			*b++ = '\\';
1897c478bd9Sstevel@tonic-gate 			*b++ = 's';
1907c478bd9Sstevel@tonic-gate 			*b++ = '-';
1917c478bd9Sstevel@tonic-gate 			*b++ = '2';
1927c478bd9Sstevel@tonic-gate 		}
1937c478bd9Sstevel@tonic-gate 		if (!this && alph > 1) {
1947c478bd9Sstevel@tonic-gate 			*b++ = '\\';
1957c478bd9Sstevel@tonic-gate 			*b++ = 's';
1967c478bd9Sstevel@tonic-gate 			*b++ = '+';
1977c478bd9Sstevel@tonic-gate 			*b++ = '2';
1987c478bd9Sstevel@tonic-gate 		}
1997c478bd9Sstevel@tonic-gate 		if (this)
2007c478bd9Sstevel@tonic-gate 			c &= (~040);
2017c478bd9Sstevel@tonic-gate 		*b++ = c;
2027c478bd9Sstevel@tonic-gate 		alph = this ? alph+1 : 0;
2037c478bd9Sstevel@tonic-gate 	}
2047c478bd9Sstevel@tonic-gate 	if (alph > 1) {
2057c478bd9Sstevel@tonic-gate 		*b++ = '\\';
2067c478bd9Sstevel@tonic-gate 		*b++ = 's';
2077c478bd9Sstevel@tonic-gate 		*b++ = '+';
2087c478bd9Sstevel@tonic-gate 		*b++ = '2';
2097c478bd9Sstevel@tonic-gate 	}
2107c478bd9Sstevel@tonic-gate 	*b = 0;
2117c478bd9Sstevel@tonic-gate 	return (p);
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate char *
215*11a8fa6cSceastha revauth(char *s, char *b)
2167c478bd9Sstevel@tonic-gate {
2177c478bd9Sstevel@tonic-gate 	char *init, *name, *jr, *p, *bcop;
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	bcop = b;
2207c478bd9Sstevel@tonic-gate 	init = name = s;
2217c478bd9Sstevel@tonic-gate 	while (*name)
2227c478bd9Sstevel@tonic-gate 		name++;
2237c478bd9Sstevel@tonic-gate 	jr = name;
2247c478bd9Sstevel@tonic-gate 	while (name > init && *name != ' ')
2257c478bd9Sstevel@tonic-gate 		name--;
2267c478bd9Sstevel@tonic-gate 	if (name[-1] == ',' || name[-1] == '(') {
2277c478bd9Sstevel@tonic-gate 		jr = --name;
2287c478bd9Sstevel@tonic-gate 		while (name > init && *name != ' ')
2297c478bd9Sstevel@tonic-gate 			name--;
2307c478bd9Sstevel@tonic-gate 	}
2317c478bd9Sstevel@tonic-gate 	p = name;
2327c478bd9Sstevel@tonic-gate 	while (p < jr)
2337c478bd9Sstevel@tonic-gate 		*b++ = *p++;
2347c478bd9Sstevel@tonic-gate 	*b++ = ',';
2357c478bd9Sstevel@tonic-gate 	while (init < name)
2367c478bd9Sstevel@tonic-gate 		*b++ = *init++;
2377c478bd9Sstevel@tonic-gate 	if (*jr)
2387c478bd9Sstevel@tonic-gate 		jr++;
2397c478bd9Sstevel@tonic-gate 	while (*jr)
2407c478bd9Sstevel@tonic-gate 		*b++ = *jr++;
2417c478bd9Sstevel@tonic-gate 	*b++ = 0;
2427c478bd9Sstevel@tonic-gate 	return (bcop);
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate 
245*11a8fa6cSceastha static char
246*11a8fa6cSceastha last(char *s)
2477c478bd9Sstevel@tonic-gate {
2487c478bd9Sstevel@tonic-gate 	while (*s)
2497c478bd9Sstevel@tonic-gate 		s++;
2507c478bd9Sstevel@tonic-gate 	return (*--s);
2517c478bd9Sstevel@tonic-gate }
252