1 /* $MirOS: src/usr.bin/oldroff/refer/refer6.c,v 1.2 2008/11/08 23:04:47 tg Exp $ */
2 
3 /*-
4  * Copyright (c) 1979, 1980, 1981, 1986, 1988, 1990, 1991, 1992
5  *     The Regents of the University of California.
6  * Copyright (C) Caldera International Inc.  2001-2002.
7  * Copyright (c) 2003, 2004
8  *	Thorsten "mirabilos" Glaser <tg@mirbsd.org>
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms,
12  * with or without modification, are permitted provided
13  * that the following conditions are met:
14  *
15  * Redistributions of source code and documentation must retain
16  * the above copyright notice, this list of conditions and the
17  * following disclaimer.  Redistributions in binary form must
18  * reproduce the above copyright notice, this list of conditions
19  * and the following disclaimer in the documentation and/or other
20  * materials provided with the distribution.
21  *
22  * All advertising materials mentioning features or use of this
23  * software must display the following acknowledgement:
24  *   This product includes software developed or owned by
25  *   Caldera International, Inc.
26  *
27  * Neither the name of Caldera International, Inc. nor the names
28  * of other contributors may be used to endorse or promote products
29  * derived from this software without specific prior written permission.
30  *
31  * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
32  * INTERNATIONAL, INC. AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
33  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
34  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35  * ARE DISCLAIMED.  IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE
36  * LIABLE FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR
37  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
38  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
39  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
40  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
41  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
42  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43  */
44 
45 #ifndef lint
46 static char sccsid[] = "@(#)refer6.c	4.2 (Berkeley) 4/18/91";
47 #endif /* not lint */
48 
49 #include "refer..c"
50 #define dsde (macro? "de" : "ds")
51 #define ifnl (macro? sep : ' ')
52 
putref(n,tvec)53 putref(n, tvec)
54 char *tvec[];
55 {
56 	char *s, *tx;
57 	char buf1[BUFSIZ], buf2[50];
58 	int nauth = 0, i, lastype = 0, cch, macro = 0, la;
59 	int lauth = 0, ltitle = 0, lother = 0;
60 
61 	fprintf(fo, ".]-%c", sep);
62 	for (i = 0; i < n; i++) {
63 		s = tvec[i];
64 		if (*s == 0)
65 			continue;
66 		if (control(s[0])) {
67 			if (lastype && macro)
68 				fprintf(fo, "..%c", sep);
69 			if (control(s[1])) {
70 				cch = s[2];
71 				tx = s+3;
72 				macro = 1;
73 			}
74 			else {
75 				cch = s[1];
76 				tx = s+2;
77 				macro = 0;
78 			}
79 		}
80 		else {
81 			cch = lastype;
82 			tx = s;
83 		}
84 #if EBUG
85 		fprintf(stderr, "smallcaps %s cch %c\n",smallcaps, cch);
86 #endif
87 		if (mindex(smallcaps, cch))
88 			tx = caps(tx, buf1);
89 #if EBUG
90 		fprintf(stderr, " s %o tx %o %s\n",s,tx,tx);
91 #endif
92 		if (!control(s[0])) {	/* append to previous item */
93 			if (lastype != 0) {
94 				if (macro)
95 					fprintf(fo, "%s%c", tx, sep);
96 				else
97 					fprintf(fo, ".as [%c \" %s%c",lastype,tx,sep);
98 				if (lastype == 'T')
99 					ltitle = (mindex(".;,?", last(tx))!=0);
100 				if (lastype == 'A')
101 					lauth = last(tx) == '.';
102 			}
103 			continue;
104 		}
105 		if (mindex("XYZ[]", cch)) {	/* skip these */
106 			lastype = 0;
107 			continue;
108 		}
109 		else {
110 			if (cch == 'A') {
111 				if (nauth < authrev)
112 					tx = revauth(tx, buf2);
113 				if (nauth++ == 0)
114 					if (macro)
115 						fprintf(fo,
116 						".de [%c%c%s%c",cch,sep,tx,sep);
117 					else
118 						fprintf(fo,
119 						".ds [%c%s%c", cch,tx,sep);
120 				else {
121 					la = (tvec[i+1][1]!='A');
122 					fprintf(fo, ".as [A \"");
123 					if (la == 0 || nauth != 2)
124 						fprintf(fo, ",");
125 					if (la)
126 						fprintf(fo,"%s",
127 						mindex(smallcaps, 'A') ? " \\s-2AND\\s+2" : " and");
128 					fprintf(fo, "%s%c", tx, sep);
129 				}
130 				lauth = last(tx) == '.';
131 			}
132 			else {
133 				if (macro)
134 					fprintf(fo,
135 						".de [%c%c%s%c",cch,sep,tx,sep);
136 				else
137 					fprintf(fo, ".ds [%c%s%c",cch,tx, sep);
138 			}
139 		}
140 		if (cch == 'P')
141 			fprintf(fo, ".nr [P %d%c", mindex(s, '-')!=0, sep);
142 		lastype = cch;
143 		if (cch == 'T')
144 			ltitle = (mindex(".;,?", last(tx)) != 0);
145 		if (cch == 'O')
146 			lother = (mindex(".;,?", last(tx)) != 0);
147 	}
148 	if (lastype && macro)
149 		fprintf(fo, "..%c", sep);
150 	fprintf(fo, ".nr [T %d%c", ltitle, sep);
151 	fprintf(fo, ".nr [A %d%c", lauth, sep);
152 	fprintf(fo, ".nr [O %d%c", lother, sep);
153 	fprintf(fo, ".][ %s%c", class(n, tvec), '\n');
154 }
155 
tabs(sv,line)156 tabs (sv, line)
157 char *sv[], *line;
158 {
159 	char *p;
160 	int n = 0;
161 
162 	sv[n++] = line;
163 	for (p = line; *p; p++) {
164 		if (*p == '\n') {
165 			*p = 0;
166 			sv[n++] = p+1;
167 		}
168 	}
169 	return(n-1);
170 }
171 
172 char *
class(nt,tv)173 class (nt, tv)
174 char *tv[];
175 {
176 	if (hastype (nt, tv, 'J'))
177 		return("1 journal-article");
178 	if (hastype (nt, tv, 'B'))
179 		return("3 article-in-book");
180 	if (hastype (nt, tv, 'R'))
181 		return ("4 tech-report");
182 	if (hastype (nt, tv, 'G'))
183 		return ("4 tech-report");
184 	if (hastype (nt, tv, 'I'))
185 		return("2 book");
186 	if (hastype (nt, tv,'M'))
187 		return ("5 bell-tm");
188 	return("0 other");
189 }
190 
hastype(nt,tv,c)191 hastype (nt, tv, c)
192 char *tv[];
193 {
194 	int i;
195 	for (i = 0; i < nt; i++)
196 		if (control(tv[i][0]) && tv[i][1]==c )
197 			return(1);
198 	return(0);
199 }
200 
201 char *
caps(a,b)202 caps(a, b)
203 char *a, *b;
204 {
205 	char *p;
206 	int c, alph, this;
207 
208 	p = b;
209 	alph = 0;
210 	while (c = *a++) {
211 		this = isalpha(c);
212 		if (this && alph == 1) {
213 			*b++ = '\\';
214 			*b++ = 's';
215 			*b++ = '-';
216 			*b++ = '2';
217 		}
218 		if (!this && alph > 1) {
219 			*b++ = '\\';
220 			*b++ = 's';
221 			*b++ = '+';
222 			*b++ = '2';
223 		}
224 		if (this)
225 			c &= (~040);
226 		*b++ = c;
227 		alph = this ? alph+1 : 0;
228 	}
229 	if (alph > 1) {
230 		*b++ = '\\';
231 		*b++ = 's';
232 		*b++ = '+';
233 		*b++ = '2';
234 	}
235 	*b = 0;
236 	return(p);
237 }
238 
239 char *
revauth(s,b)240 revauth(s, b)
241 char *s, *b;
242 {
243 	char *init, *name, *jr, *p, *bcop;
244 
245 	bcop = b;
246 	init = name = s;
247 	while (*name)
248 		name++;
249 	jr = name;
250 	while (name > init && *name!= ' ')
251 		name--;
252 	if (name[-1] == ',' || name[-1]== '(' ) {
253 		jr = --name;
254 		while (name>init && *name != ' ')
255 			name--;
256 	}
257 	p = name;
258 	while (p < jr)
259 		*b++ = *p++;
260 	*b++ = ',';
261 	while (init < name)
262 		*b++ = *init++;
263 	if (*jr)
264 		jr++;
265 	while(*jr)
266 		*b++ = *jr++;
267 	*b++ = 0;
268 	return(bcop);
269 }
270 
last(s)271 last(s)
272 char *s;
273 {
274 	while (*s)
275 		s++;
276 	return(*--s);
277 }
278