xref: /illumos-gate/usr/src/cmd/refer/refer6.c (revision 2a8bcb4e)
1 /*
2  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
7 /*	  All Rights Reserved  	*/
8 
9 /*
10  * Copyright (c) 1980 Regents of the University of California.
11  * All rights reserved. The Berkeley software License Agreement
12  * specifies the terms and conditions for redistribution.
13  */
14 
15 #include "refer..c"
16 #define	dsde (macro? "de" : "ds")
17 #define	ifnl (macro? sep : ' ')
18 
19 extern int control();
20 extern char *mindex();
21 
22 int hastype(int, char *[], char);
23 static char last(char *);
24 char *class(int, char *[]);
25 char *caps(char *, char *);
26 char *revauth(char *, char *);
27 
28 void
putref(int n,char * tvec[])29 putref(int n, char *tvec[])
30 {
31 	char *s, *tx;
32 	char buf1[BUFSIZ], buf2[50];
33 	int nauth = 0, i, lastype = 0, cch, macro = 0, la;
34 	int lauth = 0, ltitle = 0, lother = 0;
35 
36 	fprintf(fo, ".]-%c", sep);
37 	for (i = 0; i < n; i++) {
38 		s = tvec[i];
39 		if (*s == 0)
40 			continue;
41 		if (control(s[0])) {
42 			if (lastype && macro)
43 				fprintf(fo, "..%c", sep);
44 			if (control(s[1])) {
45 				cch = s[2];
46 				tx = s+3;
47 				macro = 1;
48 			} else {
49 				cch = s[1];
50 				tx = s+2;
51 				macro = 0;
52 			}
53 		} else {
54 			cch = lastype;
55 			tx = s;
56 		}
57 #if EBUG
58 		fprintf(stderr, "smallcaps %s cch %c\n", smallcaps, cch);
59 #endif
60 		if (mindex(smallcaps, cch))
61 			tx = caps(tx, buf1);
62 #if EBUG
63 		fprintf(stderr, " s %o tx %o %s\n", s, tx, tx);
64 #endif
65 		if (!control(s[0])) {	/* append to previous item */
66 			if (lastype != 0) {
67 				if (macro)
68 					fprintf(fo, "%s%c", tx, sep);
69 				else
70 					fprintf(fo, ".as [%c \" %s%c",
71 					    lastype, tx, sep);
72 				if (lastype == 'T')
73 					ltitle = (mindex(".;,?",
74 					    last(tx)) != 0);
75 				if (lastype == 'A')
76 					lauth = last(tx) == '.';
77 			}
78 			continue;
79 		}
80 		if (mindex("XYZ[]", cch)) {	/* skip these */
81 			lastype = 0;
82 			continue;
83 		} else {
84 			if (cch == 'A') {
85 				if (nauth < authrev)
86 					tx = revauth(tx, buf2);
87 				if (nauth++ == 0)
88 					if (macro)
89 						fprintf(fo, ".de [%c%c%s%c",
90 						    cch, sep, tx, sep);
91 					else
92 						fprintf(fo, ".ds [%c%s%c",
93 						    cch, tx, sep);
94 				else {
95 					la = (tvec[i+1][1] != 'A');
96 					fprintf(fo, ".as [A \"");
97 					if (la == 0 || nauth != 2)
98 						fprintf(fo, ",");
99 					if (la)
100 						fprintf(fo, "%s",
101 						    mindex(smallcaps, 'A') ?
102 						    " \\s-2AND\\s+2" : " and");
103 					fprintf(fo, "%s%c", tx, sep);
104 				}
105 				lauth = last(tx) == '.';
106 			} else {
107 				if (macro)
108 					fprintf(fo, ".de [%c%c%s%c",
109 					    cch, sep, tx, sep);
110 				else
111 					fprintf(fo, ".ds [%c%s%c",
112 					    cch, tx, sep);
113 			}
114 		}
115 		if (cch == 'P')
116 			fprintf(fo, ".nr [P %d%c", mindex(s, '-') != 0, sep);
117 		lastype = cch;
118 		if (cch == 'T')
119 			ltitle = (mindex(".;,?", last(tx)) != 0);
120 		if (cch == 'O')
121 			lother = (mindex(".;,?", last(tx)) != 0);
122 	}
123 	if (lastype && macro)
124 		fprintf(fo, "..%c", sep);
125 	fprintf(fo, ".nr [T %d%c", ltitle, sep);
126 	fprintf(fo, ".nr [A %d%c", lauth, sep);
127 	fprintf(fo, ".nr [O %d%c", lother, sep);
128 	fprintf(fo, ".][ %s%c", class(n, tvec), '\n');
129 }
130 
131 int
tabs(char * sv[],char * line)132 tabs(char *sv[], char *line)
133 {
134 	char *p;
135 	int n = 0;
136 
137 	sv[n++] = line;
138 	for (p = line; *p; p++) {
139 		if (*p == '\n') {
140 			*p = 0;
141 			sv[n++] = p+1;
142 		}
143 	}
144 	return (n-1);
145 }
146 
147 char *
class(int nt,char * tv[])148 class(int nt, char *tv[])
149 {
150 	if (hastype(nt, tv, 'J'))
151 		return ("1 journal-article");
152 	if (hastype(nt, tv, 'B'))
153 		return ("3 article-in-book");
154 	if (hastype(nt, tv, 'R'))
155 		return ("4 tech-report");
156 	if (hastype(nt, tv, 'G'))
157 		return ("4 tech-report");
158 	if (hastype(nt, tv, 'I'))
159 		return ("2 book");
160 	if (hastype(nt, tv, 'M'))
161 		return ("5 bell-tm");
162 	return ("0 other");
163 }
164 
165 int
hastype(int nt,char * tv[],char c)166 hastype(int nt, char *tv[], char c)
167 {
168 	int i;
169 	for (i = 0; i < nt; i++)
170 		if (control(tv[i][0]) && tv[i][1] == c)
171 			return (1);
172 	return (0);
173 }
174 
175 char *
caps(char * a,char * b)176 caps(char *a, char *b)
177 {
178 	char *p;
179 	int c, alph, this;
180 
181 	p = b;
182 	alph = 0;
183 	while (c = *a++) {
184 		this = isalpha(c);
185 		if (this && alph == 1) {
186 			*b++ = '\\';
187 			*b++ = 's';
188 			*b++ = '-';
189 			*b++ = '2';
190 		}
191 		if (!this && alph > 1) {
192 			*b++ = '\\';
193 			*b++ = 's';
194 			*b++ = '+';
195 			*b++ = '2';
196 		}
197 		if (this)
198 			c &= (~040);
199 		*b++ = c;
200 		alph = this ? alph+1 : 0;
201 	}
202 	if (alph > 1) {
203 		*b++ = '\\';
204 		*b++ = 's';
205 		*b++ = '+';
206 		*b++ = '2';
207 	}
208 	*b = 0;
209 	return (p);
210 }
211 
212 char *
revauth(char * s,char * b)213 revauth(char *s, char *b)
214 {
215 	char *init, *name, *jr, *p, *bcop;
216 
217 	bcop = b;
218 	init = name = s;
219 	while (*name)
220 		name++;
221 	jr = name;
222 	while (name > init && *name != ' ')
223 		name--;
224 	if (name[-1] == ',' || name[-1] == '(') {
225 		jr = --name;
226 		while (name > init && *name != ' ')
227 			name--;
228 	}
229 	p = name;
230 	while (p < jr)
231 		*b++ = *p++;
232 	*b++ = ',';
233 	while (init < name)
234 		*b++ = *init++;
235 	if (*jr)
236 		jr++;
237 	while (*jr)
238 		*b++ = *jr++;
239 	*b++ = 0;
240 	return (bcop);
241 }
242 
243 static char
last(char * s)244 last(char *s)
245 {
246 	while (*s)
247 		s++;
248 	return (*--s);
249 }
250