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