1: # include "refer.h" 2: # define SAME 0 3: 4: #define NLABC (NREF+1) /* start from 1 */ 5: 6: static char bflab[NFLAB]; 7: static char *labtab[NLABC]; 8: static char *lbp bflab; 9: char labc[NLABC]; 10: int prevsig; 11: static char punct; 12: char lastpunct(); 13: 14: putsig (nf, flds, nref, nstline, endline) 15: char *flds[], *nstline, *endline; 16: { 17: /* choose signal style */ 18: char t[100], t1[100], t2[100], format[10], *sd; 19: int another 0; 20: int addon; 21: char *stline; 22: static FILE *fhide 0; 23: 24: trimnl(nstline); 25: trimnl(endline); 26: if (nonblank(nstline+2) || nonblank(endline+2)) 27: { 28: stline = nstline+2; 29: endline += 2; 30: } 31: else 32: { 33: #ifdef MFLAG 34: stline = labels ? "\\*((." : "\\*([."; 35: endline = labels ? "\\*(.)" : "\\*(.]"; 36: #else 37: stline = "\\*([."; 38: endline = "\\*(.]"; 39: #endif 40: } 41: if (nref==0) /* non-existent paper, just flush */ 42: goto flush; 43: if (prevsig==0) 44: { 45: /* save period or comma from last line */ 46: punct=lastpunct(); 47: output(stline); 48: prevsig=1; 49: } 50: if (labels) 51: { 52: if (nf==0) { /* old */ 53: if (labc[nref] == 1) 54: sprintf(t,"%s%c%d%c",labtab[nref],KEYLET, 55: nref,KEYLET); 56: else 57: sprintf(t, "%s%c", labtab[nref], labc[nref]); 58: } 59: else 60: { 61: *t=0; 62: if (keywant) 63: fpar(nf, flds, t, keywant, 1, 0); 64: if (t[0]==0) 65: { 66: #ifdef MFLAG 67: if (mflag) { 68: fpar(nf, flds, t, 'A', 1, 0); 69: if (fpar(nf, flds, t2, 'A', 2, 0)) { 70: if (fpar(nf, flds, t1, 'A', 3, 0)) 71: /* Author1 et al, date */ 72: sprintf(t1, "%s \\*([e",t); 73: else 74: /* Author1 and Author2, date */ 75: sprintf(t1,"%s \\*(&1 %s",t,t2); 76: } 77: else 78: /* Author, date */ 79: strcpy(t1,t); 80: } 81: else 82: #endif 83: sprintf(format, nmlen>0 ? "%%.%ds%%s" : "%%s%%s", nmlen); 84: /* format is %s%s for default labels 85: or %.3s%s eg if wanted */ 86: sd = fpar(nf, flds, t2, 'D', 1, 0); 87: if (dtlen>0) 88: { 89: char *sdb; 90: for(sdb=sd; *sd; sd++); 91: sd = sd-dtlen; 92: if (sd<sdb) sd=sdb; 93: } 94: #ifdef MFLAG 95: if (mflag) 96: sprintf(t,"%s, %s",t1,sd); 97: else 98: #endif 99: sprintf(t,format,fpar(nf,flds,t1,'A',1,0), sd); 100: } 101: if (keywant) 102: { 103: addon=0; 104: for(sd=t; *sd; sd++); 105: if (*--sd == '-') 106: { 107: addon=1; 108: *sd=0; 109: } 110: } 111: if (!keywant || addon) { 112: addon = keylet(t, nref); 113: if (addon == 1) { 114: sprintf(t1,"%c%d%c",KEYLET,nref,KEYLET); 115: strcat(t,t1); 116: } else 117: addch(t, addon); 118: } 119: } 120: } 121: else 122: { 123: if (sort) 124: sprintf(t, "%c%d%c", FLAG, nref, FLAG); 125: else 126: sprintf(t, "%d", nref); 127: } 128: flush: 129: another = prefix (".[", sd=lookat()); 130: if (another && nonblank(sd+2)) 131: fprintf(stderr, "File %s, line %d: punctuation ignored: %s", 132: Ifile, Iline, sd); 133: if (nref) 134: output(t); 135: if (bare==0) 136: { 137: if (another==0) 138: { 139: output(endline); 140: if (punct) { 141: #ifdef MFLAG 142: sprintf(t1,labels? "%c\n" : "\\*(>%c\n", punct); 143: #else 144: sprintf(t1, "\\*(>%c\n", punct); 145: #endif 146: output(t1); 147: punct = 0; 148: } 149: else 150: output("\n"); 151: flout(); 152: prevsig=0; 153: if (fo == fhide && fhide != (FILE *)NULL) 154: { 155: int ch; 156: fclose(fhide); 157: fhide= fopen(hidenam, "r"); 158: fo= ftemp; 159: while ((ch = getc(fhide)) != EOF) 160: putc(ch, fo); 161: fclose(fhide); 162: unlink(hidenam); 163: } 164: } 165: else 166: { 167: #ifdef MFLAG 168: output(mflag ? "; " : (labels ? ", " : ",\\|")); 169: #else 170: output(labels ? ", " : ",\\|"); 171: #endif 172: /* hide if need be */ 173: if (fo == ftemp) 174: { 175: sprintf(hidenam, "/tmp/rj%dc", getpid()); 176: fhide= fopen(hidenam, "w"); 177: if (fhide==NULL) err("Can't get scratch file %s", hidenam); 178: fo = fhide; 179: } 180: } 181: } 182: if (nf>0) { 183: fprintf(fo,".ds [F %s%c",t,sep); 184: if (sort) 185: fprintf(fo,".nr [F %c%d%c%c",FLAG,nref,FLAG,sep); 186: else 187: fprintf(fo,".nr [F %d%c",nref,sep); 188: } 189: if (bare>0) 190: flout(); 191: } 192: 193: nonblank(s) 194: char *s; 195: { 196: for (; *s; s++) 197: if (*s != ' ' && *s != '\n') 198: return(1); 199: return(0); 200: } 201: 202: fpar (nf, flds, out, c, seq, prepend) 203: char *flds[], *out; 204: { 205: char *p, *s; 206: int i, fnd 0; 207: for(i=0; i<nf; i++) 208: if (flds[i][1]==c && ++fnd >= seq) 209: { 210: /* for titles use first word otherwise last */ 211: if (c=='T' || c == 'J') 212: { 213: p=flds[i]+3; 214: if (prefix("A ", p)) p +=2; 215: if (prefix("An ", p)) p +=3; 216: if (prefix("The ", p)) p+= 4; 217: mycpy2(out, p, 20); 218: return(out); 219: } 220: for(s=p= flds[i]+2; *p; p++); 221: while (p>s && *p != ' ') p--; 222: /* special wart for authors */ 223: if (c=='A' && (p[-1] == ',' || p[1] =='(')) 224: { 225: p--; 226: while (p>s && *p != ' ') p--; 227: mycpy (out, p+1); 228: } 229: else 230: strcpy (out, p+1); 231: if (c=='A' && prepend) 232: initadd(out, flds[i]+2, p); 233: return(out); 234: } 235: return(0); 236: } 237: putkey(nf, flds, nref, keystr) 238: char *flds[], *keystr; 239: { 240: char t1[50], *sf; 241: int ctype, i, count; 242: fprintf(fo, ".\\\""); 243: while (ctype= *keystr++) 244: { 245: count = atoi(keystr); 246: if (*keystr=='+') count=999; 247: if (count<=0) count=1; 248: for(i=1; i<=count; i++) 249: { 250: sf= fpar(nf, flds, t1, ctype, i, 1); 251: if (sf==0) 252: break; 253: sf = artskp(sf); 254: fprintf(fo, "%s%c", sf, '-'); 255: } 256: } 257: fprintf(fo, "%c%d%c%c", FLAG, nref, FLAG, sep); 258: } 259: keylet(t, nref) 260: char *t; 261: { 262: register i; 263: register x 'a'-1; 264: register first = 0; 265: 266: if (nref>NREF) 267: err("Too many references for labels",0); 268: for(i=1; i<nref;i++) 269: { 270: if (strcmp(labtab[i], t) == 0) { 271: x = labc[i]; 272: #ifdef MFLAG 273: if (x==1) 274: x = 'a'; 275: #endif 276: if (first == 0) 277: first = i; 278: } 279: } 280: if (first == 0) { 281: if (lbp+strlen(t) >= bflab+NFLAB) 282: err("Label buffer overflow",0); 283: strcpy(labtab[nref]=lbp, t); 284: while (*lbp++) ; 285: #ifdef MFLAG 286: return(labc[nref] = mflag ? 1 : 'a'); 287: #else 288: return(labc[nref] = 'a'); 289: #endif 290: } else { 291: labtab[nref] = labtab[first]; 292: #ifdef MFLAG 293: labc[first] = 'a'; 294: #endif 295: } 296: return(labc[nref]=x+1); 297: } 298: mycpy(s,t) 299: char *s, *t; 300: { 301: while (*t && *t != ',' && *t != ' ') 302: *s++ = *t++; 303: *s=0; 304: } 305: mycpy2 (s, t, n) 306: char *s, *t; 307: { 308: int c; 309: while (n-- && (c= *t++)>0) 310: { 311: if (c==' ')c= '-'; 312: *s++ = c; 313: } 314: *s=0; 315: } 316: initadd(to, from, stop) 317: char *to, *from, *stop; 318: { 319: int c, nalph 1; 320: while (*to) to++; 321: while (from<stop) 322: { 323: c = *from++; 324: if (!isalpha(c)) 325: { 326: if (nalph) 327: *to++ = '.'; 328: nalph=0; 329: continue; 330: } 331: if (nalph++ ==0) 332: *to++ = c; 333: } 334: *to=0; 335: } 336: 337: static char *articles[] { 338: "the ", "an ", "a ", 0}; 339: artskp(s) 340: char *s; 341: { 342: /* skips over initial "a ", "an ", or "the " in s */ 343: char **p, *r1, *r2; 344: for(p=articles; *p; p++) 345: { 346: r2 = s; 347: for (r1= *p; ((*r1 ^ *r2) & ~040 ) == 0; r1++) 348: r2++; 349: if (*r1==0 && *r2 != 0) 350: return(r2); 351: } 352: return(s); 353: }