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 SAME 0
197c478bd9Sstevel@tonic-gate #define NFLAB 3000 /* number of bytes to record all labels */
207c478bd9Sstevel@tonic-gate #define NLABC 1000 /* max number of labels */
217c478bd9Sstevel@tonic-gate
227c478bd9Sstevel@tonic-gate static char sig[MXSIG];
237c478bd9Sstevel@tonic-gate static char bflab[NFLAB];
247c478bd9Sstevel@tonic-gate static char *labtab[NLABC];
257c478bd9Sstevel@tonic-gate static char *lbp = bflab;
267c478bd9Sstevel@tonic-gate static char labc[NLABC];
277c478bd9Sstevel@tonic-gate static char stbuff[50];
287c478bd9Sstevel@tonic-gate static int prevsig;
297c478bd9Sstevel@tonic-gate
30*11a8fa6cSceastha extern void addch();
31*11a8fa6cSceastha extern void append();
32*11a8fa6cSceastha extern void err();
33*11a8fa6cSceastha extern void flout();
34*11a8fa6cSceastha extern int prefix();
35*11a8fa6cSceastha
36*11a8fa6cSceastha char keylet(char *, int);
37*11a8fa6cSceastha static void initadd(char *, char *, char *);
38*11a8fa6cSceastha static void mycpy(char *, char *);
39*11a8fa6cSceastha static void mycpy2(char *, char *, int);
40*11a8fa6cSceastha void tokeytab(char *, int);
41*11a8fa6cSceastha
42*11a8fa6cSceastha /* choose signal style */
43*11a8fa6cSceastha void
putsig(int nf,char * flds[],int nref,char * nstline,char * endline,int toindex)44*11a8fa6cSceastha putsig(int nf, char *flds[], int nref, char *nstline,
45*11a8fa6cSceastha char *endline, int toindex)
467c478bd9Sstevel@tonic-gate {
477c478bd9Sstevel@tonic-gate char t[100], t1[MXSIG], t2[100], format[10], *sd, *stline;
487c478bd9Sstevel@tonic-gate int addon, another = 0;
497c478bd9Sstevel@tonic-gate static FILE *fhide = 0;
507c478bd9Sstevel@tonic-gate int i;
517c478bd9Sstevel@tonic-gate char tag;
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate if (labels) {
547c478bd9Sstevel@tonic-gate if (nf == 0) /* old */
557c478bd9Sstevel@tonic-gate sprintf(t, "%s%c", labtab[nref], labc[nref]);
567c478bd9Sstevel@tonic-gate else {
577c478bd9Sstevel@tonic-gate *t = 0;
587c478bd9Sstevel@tonic-gate if (keywant)
597c478bd9Sstevel@tonic-gate fpar(nf, flds, t, keywant, 1, 0);
607c478bd9Sstevel@tonic-gate if (science && t[0] == 0) {
617c478bd9Sstevel@tonic-gate if (fpar(nf, flds, t, 'A', 1, 0) != 0) {
62*11a8fa6cSceastha if (fpar(nf, flds, t2, 'D',
63*11a8fa6cSceastha 1, 0) != 0) {
647c478bd9Sstevel@tonic-gate strcat(t, ", ");
657c478bd9Sstevel@tonic-gate strcat(t, t2);
667c478bd9Sstevel@tonic-gate }
677c478bd9Sstevel@tonic-gate }
687c478bd9Sstevel@tonic-gate } else if (t[0] == 0) {
697c478bd9Sstevel@tonic-gate sprintf(format,
707c478bd9Sstevel@tonic-gate nmlen > 0 ? "%%.%ds%%s" : "%%s%%s",
717c478bd9Sstevel@tonic-gate nmlen);
727c478bd9Sstevel@tonic-gate /* format is %s%s for default labels */
737c478bd9Sstevel@tonic-gate /* or %.3s%s eg if wanted */
747c478bd9Sstevel@tonic-gate if (fpar(nf, flds, t2, 'D', 1, 0)) {
757c478bd9Sstevel@tonic-gate sd = t2;
767c478bd9Sstevel@tonic-gate if (dtlen > 0) {
777c478bd9Sstevel@tonic-gate int n = strlen(sd) - dtlen;
787c478bd9Sstevel@tonic-gate if (n > 0)
797c478bd9Sstevel@tonic-gate sd += n;
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate } else {
827c478bd9Sstevel@tonic-gate sd = "";
837c478bd9Sstevel@tonic-gate }
847c478bd9Sstevel@tonic-gate t1[0] = 0;
857c478bd9Sstevel@tonic-gate fpar(nf, flds, t1, 'A', 1, 0);
867c478bd9Sstevel@tonic-gate sprintf(t, format, t1, sd);
877c478bd9Sstevel@tonic-gate }
887c478bd9Sstevel@tonic-gate if (keywant) {
897c478bd9Sstevel@tonic-gate addon = 0;
907c478bd9Sstevel@tonic-gate for (sd = t; *sd; sd++)
917c478bd9Sstevel@tonic-gate ;
927c478bd9Sstevel@tonic-gate if (*--sd == '-') {
937c478bd9Sstevel@tonic-gate addon = 1;
947c478bd9Sstevel@tonic-gate *sd = 0;
957c478bd9Sstevel@tonic-gate }
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate if ((!keywant || addon) && !science) {
987c478bd9Sstevel@tonic-gate addch(t, keylet(t, nref));
99*11a8fa6cSceastha } else {
1007c478bd9Sstevel@tonic-gate tokeytab(t, nref);
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate }
103*11a8fa6cSceastha } else {
1047c478bd9Sstevel@tonic-gate if (sort)
1057c478bd9Sstevel@tonic-gate sprintf(t, "%c%d%c", FLAG, nref, FLAG);
1067c478bd9Sstevel@tonic-gate else
1077c478bd9Sstevel@tonic-gate sprintf(t, "%d", nref);
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate another = (sd = lookat()) ? prefix(".[", sd) : 0;
1107c478bd9Sstevel@tonic-gate if (another && (strcmp(".[\n", sd) != SAME))
111*11a8fa6cSceastha fprintf(stderr, (char *)gettext(
112*11a8fa6cSceastha "File %s line %d: punctuation ignored from: %s"),
1137c478bd9Sstevel@tonic-gate Ifile, Iline, sd);
1147c478bd9Sstevel@tonic-gate if ((strlen(sig) + strlen(t)) > MXSIG)
1157c478bd9Sstevel@tonic-gate err(gettext("sig overflow (%d)"), MXSIG);
1167c478bd9Sstevel@tonic-gate strcat(sig, t);
1177c478bd9Sstevel@tonic-gate #if EBUG
1187c478bd9Sstevel@tonic-gate fprintf(stderr, "sig is now %s leng %d\n", sig, strlen(sig));
1197c478bd9Sstevel@tonic-gate #endif
1207c478bd9Sstevel@tonic-gate trimnl(nstline);
1217c478bd9Sstevel@tonic-gate trimnl(endline);
1227c478bd9Sstevel@tonic-gate stline = stbuff;
1237c478bd9Sstevel@tonic-gate if (prevsig == 0) {
1247c478bd9Sstevel@tonic-gate strcpy(stline, nstline);
1257c478bd9Sstevel@tonic-gate prevsig = 1;
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate if (stline[2] || endline[2]) {
1287c478bd9Sstevel@tonic-gate stline += 2;
1297c478bd9Sstevel@tonic-gate endline += 2;
130*11a8fa6cSceastha } else {
1317c478bd9Sstevel@tonic-gate stline = "\\*([.";
1327c478bd9Sstevel@tonic-gate endline = "\\*(.]";
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate if (science) {
1357c478bd9Sstevel@tonic-gate stline = " (";
1367c478bd9Sstevel@tonic-gate endline = ")";
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate if (bare == 0) {
1397c478bd9Sstevel@tonic-gate if (!another) {
1407c478bd9Sstevel@tonic-gate sprintf(t1, "%s%s%s\n", stline, sig, endline);
1417c478bd9Sstevel@tonic-gate if (strlen(t1) > MXSIG)
1427c478bd9Sstevel@tonic-gate err(gettext("t1 overflow (%d)"), MXSIG);
1437c478bd9Sstevel@tonic-gate append(t1);
1447c478bd9Sstevel@tonic-gate flout();
1457c478bd9Sstevel@tonic-gate sig[0] = 0;
1467c478bd9Sstevel@tonic-gate prevsig = 0;
1477c478bd9Sstevel@tonic-gate if (fo == fhide) {
1487c478bd9Sstevel@tonic-gate int ch;
1497c478bd9Sstevel@tonic-gate fclose(fhide);
1507c478bd9Sstevel@tonic-gate fhide = fopen(hidenam, "r");
1517c478bd9Sstevel@tonic-gate fo = ftemp;
1527c478bd9Sstevel@tonic-gate while ((ch = getc(fhide)) != EOF)
1537c478bd9Sstevel@tonic-gate putc(ch, fo);
1547c478bd9Sstevel@tonic-gate fclose(fhide);
1557c478bd9Sstevel@tonic-gate unlink(hidenam);
1567c478bd9Sstevel@tonic-gate }
157*11a8fa6cSceastha } else {
1587c478bd9Sstevel@tonic-gate if (labels) {
1597c478bd9Sstevel@tonic-gate strcat(sig, ",\\|");
1607c478bd9Sstevel@tonic-gate } else {
1617c478bd9Sstevel@tonic-gate /*
1627c478bd9Sstevel@tonic-gate * Seperate reference numbers with AFLAG
1637c478bd9Sstevel@tonic-gate * for later sorting and condensing.
1647c478bd9Sstevel@tonic-gate */
1657c478bd9Sstevel@tonic-gate t1[0] = AFLAG;
1667c478bd9Sstevel@tonic-gate t1[1] = '\0';
1677c478bd9Sstevel@tonic-gate strcat(sig, t1);
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate if (fo == ftemp) { /* hide if need be */
1707c478bd9Sstevel@tonic-gate sprintf(hidenam, "/tmp/rj%dc", getpid());
1717c478bd9Sstevel@tonic-gate #if EBUG
1727c478bd9Sstevel@tonic-gate fprintf(stderr, "hiding in %s\n", hidenam);
1737c478bd9Sstevel@tonic-gate #endif
1747c478bd9Sstevel@tonic-gate fhide = fopen(hidenam, "w");
1757c478bd9Sstevel@tonic-gate if (fhide == NULL)
176*11a8fa6cSceastha err(gettext(
177*11a8fa6cSceastha "Can't get scratch file %s"),
1787c478bd9Sstevel@tonic-gate hidenam);
1797c478bd9Sstevel@tonic-gate fo = fhide;
1807c478bd9Sstevel@tonic-gate }
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate if (bare < 2)
1847c478bd9Sstevel@tonic-gate if (nf > 0 && toindex)
1857c478bd9Sstevel@tonic-gate fprintf(fo, ".ds [F %s%c", t, sep);
1867c478bd9Sstevel@tonic-gate if (bare > 0)
1877c478bd9Sstevel@tonic-gate flout();
1887c478bd9Sstevel@tonic-gate #if EBUG
1897c478bd9Sstevel@tonic-gate fprintf(stderr, "sig is now %s\n", sig);
1907c478bd9Sstevel@tonic-gate #endif
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate
1937c478bd9Sstevel@tonic-gate char *
fpar(int nf,char * flds[],char * out,int c,int seq,int prepend)194*11a8fa6cSceastha fpar(int nf, char *flds[], char *out, int c, int seq, int prepend)
1957c478bd9Sstevel@tonic-gate {
1967c478bd9Sstevel@tonic-gate char *p, *s;
1977c478bd9Sstevel@tonic-gate int i, fnd = 0;
1987c478bd9Sstevel@tonic-gate
1997c478bd9Sstevel@tonic-gate for (i = 0; i < nf; i++)
2007c478bd9Sstevel@tonic-gate if (flds[i][1] == c && ++fnd >= seq) {
2017c478bd9Sstevel@tonic-gate /* for titles use first word otherwise last */
2027c478bd9Sstevel@tonic-gate if (c == 'T' || c == 'J') {
2037c478bd9Sstevel@tonic-gate p = flds[i]+3;
2047c478bd9Sstevel@tonic-gate if (prefix("A ", p))
2057c478bd9Sstevel@tonic-gate p += 2;
2067c478bd9Sstevel@tonic-gate if (prefix("An ", p))
2077c478bd9Sstevel@tonic-gate p += 3;
2087c478bd9Sstevel@tonic-gate if (prefix("The ", p))
2097c478bd9Sstevel@tonic-gate p += 4;
2107c478bd9Sstevel@tonic-gate mycpy2(out, p, 20);
2117c478bd9Sstevel@tonic-gate return (out);
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate /* if its not 'L' then use just the last word */
2147c478bd9Sstevel@tonic-gate s = p = flds[i]+2;
2157c478bd9Sstevel@tonic-gate if (c != 'L') {
216*11a8fa6cSceastha for (; *p; p++)
217*11a8fa6cSceastha ;
2187c478bd9Sstevel@tonic-gate while (p > s && *p != ' ')
2197c478bd9Sstevel@tonic-gate p--;
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate /* special wart for authors */
2227c478bd9Sstevel@tonic-gate if (c == 'A' && (p[-1] == ',' || p[1] == '(')) {
2237c478bd9Sstevel@tonic-gate p--;
2247c478bd9Sstevel@tonic-gate while (p > s && *p != ' ')
2257c478bd9Sstevel@tonic-gate p--;
2267c478bd9Sstevel@tonic-gate mycpy(out, p+1);
227*11a8fa6cSceastha } else
2287c478bd9Sstevel@tonic-gate strcpy(out, p+1);
2297c478bd9Sstevel@tonic-gate if (c == 'A' && prepend)
2307c478bd9Sstevel@tonic-gate initadd(out, flds[i]+2, p);
2317c478bd9Sstevel@tonic-gate return (out);
2327c478bd9Sstevel@tonic-gate }
2337c478bd9Sstevel@tonic-gate return (0);
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate
236*11a8fa6cSceastha void
putkey(int nf,char * flds[],int nref,char * keystr)237*11a8fa6cSceastha putkey(int nf, char *flds[], int nref, char *keystr)
2387c478bd9Sstevel@tonic-gate {
2397c478bd9Sstevel@tonic-gate char t1[50], *sf;
2407c478bd9Sstevel@tonic-gate int ctype, i, count;
2417c478bd9Sstevel@tonic-gate
2427c478bd9Sstevel@tonic-gate fprintf(fo, ".\\\"");
2437c478bd9Sstevel@tonic-gate if (nf <= 0)
2447c478bd9Sstevel@tonic-gate fprintf(fo, "%s%c%c", labtab[nref], labc[nref], sep);
2457c478bd9Sstevel@tonic-gate else {
2467c478bd9Sstevel@tonic-gate while (ctype = *keystr++) {
2477c478bd9Sstevel@tonic-gate count = atoi(keystr);
2487c478bd9Sstevel@tonic-gate if (*keystr == '+')
2497c478bd9Sstevel@tonic-gate count = 999;
2507c478bd9Sstevel@tonic-gate if (count <= 0)
2517c478bd9Sstevel@tonic-gate count = 1;
2527c478bd9Sstevel@tonic-gate for (i = 1; i <= count; i++) {
2537c478bd9Sstevel@tonic-gate sf = fpar(nf, flds, t1, ctype, i, 1);
2547c478bd9Sstevel@tonic-gate if (sf == 0)
2557c478bd9Sstevel@tonic-gate break;
2567c478bd9Sstevel@tonic-gate sf = artskp(sf);
2577c478bd9Sstevel@tonic-gate fprintf(fo, "%s%c", sf, '-');
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate }
2607c478bd9Sstevel@tonic-gate fprintf(fo, "%c%d%c%c", FLAG, nref, FLAG, sep);
2617c478bd9Sstevel@tonic-gate }
2627c478bd9Sstevel@tonic-gate }
2637c478bd9Sstevel@tonic-gate
2647c478bd9Sstevel@tonic-gate
265*11a8fa6cSceastha void
tokeytab(char * t,int nref)266*11a8fa6cSceastha tokeytab(char *t, int nref)
2677c478bd9Sstevel@tonic-gate {
2687c478bd9Sstevel@tonic-gate strcpy(labtab[nref] = lbp, t);
2697c478bd9Sstevel@tonic-gate while (*lbp++)
2707c478bd9Sstevel@tonic-gate ;
2717c478bd9Sstevel@tonic-gate }
2727c478bd9Sstevel@tonic-gate
273*11a8fa6cSceastha char
keylet(char * t,int nref)274*11a8fa6cSceastha keylet(char *t, int nref)
2757c478bd9Sstevel@tonic-gate {
2767c478bd9Sstevel@tonic-gate int i;
2777c478bd9Sstevel@tonic-gate int x = 'a' - 1;
2787c478bd9Sstevel@tonic-gate
2797c478bd9Sstevel@tonic-gate for (i = 1; i < nref; i++) {
2807c478bd9Sstevel@tonic-gate if (strcmp(labtab[i], t) == 0)
2817c478bd9Sstevel@tonic-gate x = labc[i];
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate tokeytab(t, nref);
2847c478bd9Sstevel@tonic-gate if (lbp-bflab > NFLAB)
2857c478bd9Sstevel@tonic-gate err(gettext("bflab overflow (%d)"), NFLAB);
2867c478bd9Sstevel@tonic-gate if (nref > NLABC)
2877c478bd9Sstevel@tonic-gate err(gettext("nref in labc overflow (%d)"), NLABC);
2887c478bd9Sstevel@tonic-gate #if EBUG
2897c478bd9Sstevel@tonic-gate fprintf(stderr, "lbp up to %d of %d\n", lbp-bflab, NFLAB);
2907c478bd9Sstevel@tonic-gate #endif
2917c478bd9Sstevel@tonic-gate return (labc[nref] = x+1);
2927c478bd9Sstevel@tonic-gate }
2937c478bd9Sstevel@tonic-gate
294*11a8fa6cSceastha static void
mycpy(char * s,char * t)295*11a8fa6cSceastha mycpy(char *s, char *t)
2967c478bd9Sstevel@tonic-gate {
2977c478bd9Sstevel@tonic-gate while (*t && *t != ',' && *t != ' ')
2987c478bd9Sstevel@tonic-gate *s++ = *t++;
2997c478bd9Sstevel@tonic-gate *s = 0;
3007c478bd9Sstevel@tonic-gate }
3017c478bd9Sstevel@tonic-gate
302*11a8fa6cSceastha static void
mycpy2(char * s,char * t,int n)303*11a8fa6cSceastha mycpy2(char *s, char *t, int n)
3047c478bd9Sstevel@tonic-gate {
3057c478bd9Sstevel@tonic-gate int c;
3067c478bd9Sstevel@tonic-gate
3077c478bd9Sstevel@tonic-gate while (n-- && (c = *t++) > 0) {
3087c478bd9Sstevel@tonic-gate if (c == ' ')
3097c478bd9Sstevel@tonic-gate c = '-';
3107c478bd9Sstevel@tonic-gate *s++ = c;
3117c478bd9Sstevel@tonic-gate }
3127c478bd9Sstevel@tonic-gate *s = 0;
3137c478bd9Sstevel@tonic-gate }
3147c478bd9Sstevel@tonic-gate
315*11a8fa6cSceastha static void
initadd(char * to,char * from,char * stop)316*11a8fa6cSceastha initadd(char *to, char *from, char *stop)
3177c478bd9Sstevel@tonic-gate {
3187c478bd9Sstevel@tonic-gate int c, nalph = 1;
3197c478bd9Sstevel@tonic-gate
3207c478bd9Sstevel@tonic-gate while (*to)
3217c478bd9Sstevel@tonic-gate to++;
3227c478bd9Sstevel@tonic-gate while (from < stop) {
3237c478bd9Sstevel@tonic-gate c = *from++;
3247c478bd9Sstevel@tonic-gate if (!isalpha(c)) {
3257c478bd9Sstevel@tonic-gate if (nalph)
3267c478bd9Sstevel@tonic-gate *to++ = '.';
3277c478bd9Sstevel@tonic-gate nalph = 0;
3287c478bd9Sstevel@tonic-gate continue;
3297c478bd9Sstevel@tonic-gate }
3307c478bd9Sstevel@tonic-gate if (nalph++ == 0)
3317c478bd9Sstevel@tonic-gate *to++ = c;
3327c478bd9Sstevel@tonic-gate }
3337c478bd9Sstevel@tonic-gate *to = 0;
3347c478bd9Sstevel@tonic-gate }
3357c478bd9Sstevel@tonic-gate
3367c478bd9Sstevel@tonic-gate static char *articles[] = {
3377c478bd9Sstevel@tonic-gate "the ", "an ", "a ", 0
3387c478bd9Sstevel@tonic-gate };
3397c478bd9Sstevel@tonic-gate
3407c478bd9Sstevel@tonic-gate char *
artskp(s)3417c478bd9Sstevel@tonic-gate artskp(s) /* skips over initial "a ", "an ", "the " in s */
3427c478bd9Sstevel@tonic-gate char *s;
3437c478bd9Sstevel@tonic-gate {
3447c478bd9Sstevel@tonic-gate
3457c478bd9Sstevel@tonic-gate char **p, *r1, *r2;
3467c478bd9Sstevel@tonic-gate
3477c478bd9Sstevel@tonic-gate for (p = articles; *p; p++) {
3487c478bd9Sstevel@tonic-gate r2 = s;
3497c478bd9Sstevel@tonic-gate for (r1 = *p; ((*r1 ^ *r2) & ~040) == 0; r1++)
3507c478bd9Sstevel@tonic-gate r2++;
3517c478bd9Sstevel@tonic-gate if (*r1 == 0 && *r2 != 0)
3527c478bd9Sstevel@tonic-gate return (r2);
3537c478bd9Sstevel@tonic-gate }
3547c478bd9Sstevel@tonic-gate return (s);
3557c478bd9Sstevel@tonic-gate }
356