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 #include "refer..c"
16*11a8fa6cSceastha #define dsde (macro? "de" : "ds")
17*11a8fa6cSceastha #define ifnl (macro? sep : ' ')
187c478bd9Sstevel@tonic-gate
19*11a8fa6cSceastha extern int control();
20*11a8fa6cSceastha extern char *mindex();
21*11a8fa6cSceastha
22*11a8fa6cSceastha int hastype(int, char *[], char);
23*11a8fa6cSceastha static char last(char *);
24*11a8fa6cSceastha char *class(int, char *[]);
25*11a8fa6cSceastha char *caps(char *, char *);
26*11a8fa6cSceastha char *revauth(char *, char *);
27*11a8fa6cSceastha
28*11a8fa6cSceastha void
putref(int n,char * tvec[])29*11a8fa6cSceastha putref(int n, char *tvec[])
307c478bd9Sstevel@tonic-gate {
317c478bd9Sstevel@tonic-gate char *s, *tx;
327c478bd9Sstevel@tonic-gate char buf1[BUFSIZ], buf2[50];
337c478bd9Sstevel@tonic-gate int nauth = 0, i, lastype = 0, cch, macro = 0, la;
347c478bd9Sstevel@tonic-gate int lauth = 0, ltitle = 0, lother = 0;
357c478bd9Sstevel@tonic-gate
367c478bd9Sstevel@tonic-gate fprintf(fo, ".]-%c", sep);
377c478bd9Sstevel@tonic-gate for (i = 0; i < n; i++) {
387c478bd9Sstevel@tonic-gate s = tvec[i];
397c478bd9Sstevel@tonic-gate if (*s == 0)
407c478bd9Sstevel@tonic-gate continue;
417c478bd9Sstevel@tonic-gate if (control(s[0])) {
427c478bd9Sstevel@tonic-gate if (lastype && macro)
437c478bd9Sstevel@tonic-gate fprintf(fo, "..%c", sep);
447c478bd9Sstevel@tonic-gate if (control(s[1])) {
457c478bd9Sstevel@tonic-gate cch = s[2];
467c478bd9Sstevel@tonic-gate tx = s+3;
477c478bd9Sstevel@tonic-gate macro = 1;
48*11a8fa6cSceastha } else {
497c478bd9Sstevel@tonic-gate cch = s[1];
507c478bd9Sstevel@tonic-gate tx = s+2;
517c478bd9Sstevel@tonic-gate macro = 0;
527c478bd9Sstevel@tonic-gate }
53*11a8fa6cSceastha } else {
547c478bd9Sstevel@tonic-gate cch = lastype;
557c478bd9Sstevel@tonic-gate tx = s;
567c478bd9Sstevel@tonic-gate }
577c478bd9Sstevel@tonic-gate #if EBUG
58*11a8fa6cSceastha fprintf(stderr, "smallcaps %s cch %c\n", smallcaps, cch);
597c478bd9Sstevel@tonic-gate #endif
607c478bd9Sstevel@tonic-gate if (mindex(smallcaps, cch))
617c478bd9Sstevel@tonic-gate tx = caps(tx, buf1);
627c478bd9Sstevel@tonic-gate #if EBUG
63*11a8fa6cSceastha fprintf(stderr, " s %o tx %o %s\n", s, tx, tx);
647c478bd9Sstevel@tonic-gate #endif
657c478bd9Sstevel@tonic-gate if (!control(s[0])) { /* append to previous item */
667c478bd9Sstevel@tonic-gate if (lastype != 0) {
677c478bd9Sstevel@tonic-gate if (macro)
687c478bd9Sstevel@tonic-gate fprintf(fo, "%s%c", tx, sep);
697c478bd9Sstevel@tonic-gate else
70*11a8fa6cSceastha fprintf(fo, ".as [%c \" %s%c",
71*11a8fa6cSceastha lastype, tx, sep);
727c478bd9Sstevel@tonic-gate if (lastype == 'T')
73*11a8fa6cSceastha ltitle = (mindex(".;,?",
74*11a8fa6cSceastha last(tx)) != 0);
757c478bd9Sstevel@tonic-gate if (lastype == 'A')
767c478bd9Sstevel@tonic-gate lauth = last(tx) == '.';
777c478bd9Sstevel@tonic-gate }
787c478bd9Sstevel@tonic-gate continue;
797c478bd9Sstevel@tonic-gate }
807c478bd9Sstevel@tonic-gate if (mindex("XYZ[]", cch)) { /* skip these */
817c478bd9Sstevel@tonic-gate lastype = 0;
827c478bd9Sstevel@tonic-gate continue;
83*11a8fa6cSceastha } else {
847c478bd9Sstevel@tonic-gate if (cch == 'A') {
857c478bd9Sstevel@tonic-gate if (nauth < authrev)
867c478bd9Sstevel@tonic-gate tx = revauth(tx, buf2);
877c478bd9Sstevel@tonic-gate if (nauth++ == 0)
887c478bd9Sstevel@tonic-gate if (macro)
89*11a8fa6cSceastha fprintf(fo, ".de [%c%c%s%c",
90*11a8fa6cSceastha cch, sep, tx, sep);
917c478bd9Sstevel@tonic-gate else
92*11a8fa6cSceastha fprintf(fo, ".ds [%c%s%c",
93*11a8fa6cSceastha cch, tx, sep);
947c478bd9Sstevel@tonic-gate else {
95*11a8fa6cSceastha la = (tvec[i+1][1] != 'A');
967c478bd9Sstevel@tonic-gate fprintf(fo, ".as [A \"");
977c478bd9Sstevel@tonic-gate if (la == 0 || nauth != 2)
987c478bd9Sstevel@tonic-gate fprintf(fo, ",");
997c478bd9Sstevel@tonic-gate if (la)
100*11a8fa6cSceastha fprintf(fo, "%s",
101*11a8fa6cSceastha mindex(smallcaps, 'A') ?
102*11a8fa6cSceastha " \\s-2AND\\s+2" : " and");
1037c478bd9Sstevel@tonic-gate fprintf(fo, "%s%c", tx, sep);
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate lauth = last(tx) == '.';
106*11a8fa6cSceastha } else {
1077c478bd9Sstevel@tonic-gate if (macro)
108*11a8fa6cSceastha fprintf(fo, ".de [%c%c%s%c",
109*11a8fa6cSceastha cch, sep, tx, sep);
1107c478bd9Sstevel@tonic-gate else
111*11a8fa6cSceastha fprintf(fo, ".ds [%c%s%c",
112*11a8fa6cSceastha cch, tx, sep);
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate if (cch == 'P')
116*11a8fa6cSceastha fprintf(fo, ".nr [P %d%c", mindex(s, '-') != 0, sep);
1177c478bd9Sstevel@tonic-gate lastype = cch;
1187c478bd9Sstevel@tonic-gate if (cch == 'T')
1197c478bd9Sstevel@tonic-gate ltitle = (mindex(".;,?", last(tx)) != 0);
1207c478bd9Sstevel@tonic-gate if (cch == 'O')
1217c478bd9Sstevel@tonic-gate lother = (mindex(".;,?", last(tx)) != 0);
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate if (lastype && macro)
1247c478bd9Sstevel@tonic-gate fprintf(fo, "..%c", sep);
1257c478bd9Sstevel@tonic-gate fprintf(fo, ".nr [T %d%c", ltitle, sep);
1267c478bd9Sstevel@tonic-gate fprintf(fo, ".nr [A %d%c", lauth, sep);
1277c478bd9Sstevel@tonic-gate fprintf(fo, ".nr [O %d%c", lother, sep);
1287c478bd9Sstevel@tonic-gate fprintf(fo, ".][ %s%c", class(n, tvec), '\n');
1297c478bd9Sstevel@tonic-gate }
1307c478bd9Sstevel@tonic-gate
131*11a8fa6cSceastha int
tabs(char * sv[],char * line)132*11a8fa6cSceastha tabs(char *sv[], char *line)
1337c478bd9Sstevel@tonic-gate {
1347c478bd9Sstevel@tonic-gate char *p;
1357c478bd9Sstevel@tonic-gate int n = 0;
1367c478bd9Sstevel@tonic-gate
1377c478bd9Sstevel@tonic-gate sv[n++] = line;
1387c478bd9Sstevel@tonic-gate for (p = line; *p; p++) {
1397c478bd9Sstevel@tonic-gate if (*p == '\n') {
1407c478bd9Sstevel@tonic-gate *p = 0;
1417c478bd9Sstevel@tonic-gate sv[n++] = p+1;
1427c478bd9Sstevel@tonic-gate }
1437c478bd9Sstevel@tonic-gate }
144*11a8fa6cSceastha return (n-1);
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate char *
class(int nt,char * tv[])148*11a8fa6cSceastha class(int nt, char *tv[])
1497c478bd9Sstevel@tonic-gate {
150*11a8fa6cSceastha if (hastype(nt, tv, 'J'))
151*11a8fa6cSceastha return ("1 journal-article");
152*11a8fa6cSceastha if (hastype(nt, tv, 'B'))
153*11a8fa6cSceastha return ("3 article-in-book");
154*11a8fa6cSceastha if (hastype(nt, tv, 'R'))
1557c478bd9Sstevel@tonic-gate return ("4 tech-report");
156*11a8fa6cSceastha if (hastype(nt, tv, 'G'))
1577c478bd9Sstevel@tonic-gate return ("4 tech-report");
158*11a8fa6cSceastha if (hastype(nt, tv, 'I'))
159*11a8fa6cSceastha return ("2 book");
160*11a8fa6cSceastha if (hastype(nt, tv, 'M'))
1617c478bd9Sstevel@tonic-gate return ("5 bell-tm");
162*11a8fa6cSceastha return ("0 other");
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate
165*11a8fa6cSceastha int
hastype(int nt,char * tv[],char c)166*11a8fa6cSceastha hastype(int nt, char *tv[], char c)
1677c478bd9Sstevel@tonic-gate {
1687c478bd9Sstevel@tonic-gate int i;
1697c478bd9Sstevel@tonic-gate for (i = 0; i < nt; i++)
170*11a8fa6cSceastha if (control(tv[i][0]) && tv[i][1] == c)
171*11a8fa6cSceastha return (1);
172*11a8fa6cSceastha return (0);
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate
1757c478bd9Sstevel@tonic-gate char *
caps(char * a,char * b)176*11a8fa6cSceastha caps(char *a, char *b)
1777c478bd9Sstevel@tonic-gate {
1787c478bd9Sstevel@tonic-gate char *p;
1797c478bd9Sstevel@tonic-gate int c, alph, this;
1807c478bd9Sstevel@tonic-gate
1817c478bd9Sstevel@tonic-gate p = b;
1827c478bd9Sstevel@tonic-gate alph = 0;
1837c478bd9Sstevel@tonic-gate while (c = *a++) {
1847c478bd9Sstevel@tonic-gate this = isalpha(c);
1857c478bd9Sstevel@tonic-gate if (this && alph == 1) {
1867c478bd9Sstevel@tonic-gate *b++ = '\\';
1877c478bd9Sstevel@tonic-gate *b++ = 's';
1887c478bd9Sstevel@tonic-gate *b++ = '-';
1897c478bd9Sstevel@tonic-gate *b++ = '2';
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate if (!this && alph > 1) {
1927c478bd9Sstevel@tonic-gate *b++ = '\\';
1937c478bd9Sstevel@tonic-gate *b++ = 's';
1947c478bd9Sstevel@tonic-gate *b++ = '+';
1957c478bd9Sstevel@tonic-gate *b++ = '2';
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate if (this)
1987c478bd9Sstevel@tonic-gate c &= (~040);
1997c478bd9Sstevel@tonic-gate *b++ = c;
2007c478bd9Sstevel@tonic-gate alph = this ? alph+1 : 0;
2017c478bd9Sstevel@tonic-gate }
2027c478bd9Sstevel@tonic-gate if (alph > 1) {
2037c478bd9Sstevel@tonic-gate *b++ = '\\';
2047c478bd9Sstevel@tonic-gate *b++ = 's';
2057c478bd9Sstevel@tonic-gate *b++ = '+';
2067c478bd9Sstevel@tonic-gate *b++ = '2';
2077c478bd9Sstevel@tonic-gate }
2087c478bd9Sstevel@tonic-gate *b = 0;
209*11a8fa6cSceastha return (p);
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate
2127c478bd9Sstevel@tonic-gate char *
revauth(char * s,char * b)213*11a8fa6cSceastha revauth(char *s, char *b)
2147c478bd9Sstevel@tonic-gate {
2157c478bd9Sstevel@tonic-gate char *init, *name, *jr, *p, *bcop;
2167c478bd9Sstevel@tonic-gate
2177c478bd9Sstevel@tonic-gate bcop = b;
2187c478bd9Sstevel@tonic-gate init = name = s;
2197c478bd9Sstevel@tonic-gate while (*name)
2207c478bd9Sstevel@tonic-gate name++;
2217c478bd9Sstevel@tonic-gate jr = name;
222*11a8fa6cSceastha while (name > init && *name != ' ')
2237c478bd9Sstevel@tonic-gate name--;
224*11a8fa6cSceastha if (name[-1] == ',' || name[-1] == '(') {
2257c478bd9Sstevel@tonic-gate jr = --name;
226*11a8fa6cSceastha while (name > init && *name != ' ')
2277c478bd9Sstevel@tonic-gate name--;
2287c478bd9Sstevel@tonic-gate }
2297c478bd9Sstevel@tonic-gate p = name;
2307c478bd9Sstevel@tonic-gate while (p < jr)
2317c478bd9Sstevel@tonic-gate *b++ = *p++;
2327c478bd9Sstevel@tonic-gate *b++ = ',';
2337c478bd9Sstevel@tonic-gate while (init < name)
2347c478bd9Sstevel@tonic-gate *b++ = *init++;
2357c478bd9Sstevel@tonic-gate if (*jr)
2367c478bd9Sstevel@tonic-gate jr++;
237*11a8fa6cSceastha while (*jr)
2387c478bd9Sstevel@tonic-gate *b++ = *jr++;
2397c478bd9Sstevel@tonic-gate *b++ = 0;
240*11a8fa6cSceastha return (bcop);
2417c478bd9Sstevel@tonic-gate }
2427c478bd9Sstevel@tonic-gate
243*11a8fa6cSceastha static char
last(char * s)244*11a8fa6cSceastha last(char *s)
2457c478bd9Sstevel@tonic-gate {
2467c478bd9Sstevel@tonic-gate while (*s)
2477c478bd9Sstevel@tonic-gate s++;
248*11a8fa6cSceastha return (*--s);
2497c478bd9Sstevel@tonic-gate }
250