Research V7 development
[unix-history] / usr / src / cmd / refer / refer5.c
CommitLineData
52960f2f
ML
1# include "refer..c"
2# define SAME 0
3# define NFLAB 2000
4# define NLABC 100
5static char sig[NLABC];
6
7static char bflab[NFLAB];
8static char *labtab[NLABC];
9static char *lbp bflab;
10static char labc[NLABC];
11static char stbuff[50];
12static int prevsig;
13
14putsig (nf, flds, nref, nstline, endline)
15 char *flds[], *nstline, *endline;
16{
17/* choose signal style */
18char t[100], t1[100], t2[100], format[10], *sd;
19int another 0;
20int addon;
21char *stline;
22static FILE *fhide 0;
23if (labels)
24 {
25 if (nf==0) /* old */
26 sprintf(t, "%s%c", labtab[nref], labc[nref]);
27 else
28 {
29 *t=0;
30 if (keywant)
31 sprintf(t, "%s", fpar(nf, flds, t1, keywant, 1, 0));
32 if (t[0]==0)
33 {
34 sprintf(format, nmlen>0 ? "%%.%ds%%s" : "%%s%%s", nmlen);
35 /* format is %s%s for default labels or %.3s%s eg if wanted */
36 sd = fpar(nf, flds, t2, 'D', 1, 0);
37 if (dtlen>0)
38 {
39 char *sdb;
40 for(sdb=sd; *sd; sd++);
41 sd = sd-dtlen;
42 if (sd<sdb) sd=sdb;
43 }
44 sprintf(t, format, fpar(nf, flds, t1, 'A', 1, 0), sd);
45 }
46 if (keywant)
47 {
48 addon=0;
49 for(sd=t; *sd; sd++);
50 if (*--sd == '-')
51 {
52 addon=1;
53 *sd=0;
54 }
55 }
56 if (!keywant || addon)
57 addch( t, keylet(t, nref));
58 }
59 }
60else
61 {
62 if (sort)
63 sprintf(t, "%c%d%c", FLAG, nref, FLAG);
64 else
65 sprintf(t, "%d", nref);
66 }
67another = prefix (".[", sd=lookat());
68if (another && (strcmp(".[\n", sd) != SAME))
69 fprintf(stderr, "File %s, line %d- punctuation ignored from: %s", Ifile, Iline, sd);
70strcat (sig, t);
71# if D1
72fprintf(stderr, "sig is now %s leng %d\n",sig,strlen(sig));
73# endif
74trimnl(nstline);
75trimnl(endline);
76stline=stbuff;
77if (prevsig==0)
78 {
79 strcpy (stline, nstline);
80 prevsig=1;
81 }
82if (stline[2] || endline[2])
83 {
84 stline += 2;
85 endline += 2;
86 }
87else
88 {
89 stline = "\\*([.";
90 endline = "\\*(.]";
91 }
92if (bare==0)
93 {
94 if (another==0)
95 {
96 sprintf(t1, "%s%s\%s\n", stline, sig, endline);
97 append(t1);
98 flout();
99 sig[0]=0;
100 prevsig=0;
101 if (fo == fhide)
102 {
103 int ch;
104 fclose(fhide); fhide= fopen(hidenam, "r");
105 fo= ftemp;
106 while ((ch = getc(fhide)) != EOF)
107 putc(ch, fo);
108 fclose(fhide);
109 unlink(hidenam);
110 }
111 }
112 else
113 {
114 strcat (sig, ",\\|");
115 /* hide if need be */
116 if (fo == ftemp)
117 {
118 sprintf(hidenam, "/tmp/rj%dc", getpid());
119# if D1
120fprintf(stderr, "hiding in %s\n", hidenam);
121# endif
122 fhide= fopen(hidenam, "w");
123 if (fhide==NULL) err("Can't get scratch file %s", hidenam);
124 fo = fhide;
125 }
126 }
127 }
128if (bare<2)
129 if (nf>0) fprintf(fo,".ds [F %s%c",t,sep);
130if (bare>0)
131 flout();
132# if D1
133fprintf(stderr, "sig is now %s\n",sig);
134# endif
135}
136fpar (nf, flds, out, c, seq, prepend)
137 char *flds[], *out;
138{
139char *p, *s;
140int i, fnd 0;
141for(i=0; i<nf; i++)
142 if (flds[i][1]==c && ++fnd >= seq)
143 {
144 if (c=='T' || c == 'J') /* for titles use first word otherwise last */
145 {
146 p=flds[i]+3;
147 if (prefix("A ", p)) p +=2;
148 if (prefix("An ", p)) p +=3;
149 if (prefix("The ", p)) p+= 4;
150 mycpy2(out, p, 20);
151 return(out);
152 }
153 for(s=p= flds[i]+2; *p; p++);
154 while (p>s && *p != ' ') p--;
155 /* special wart for authors */
156 if (c=='A' && (p[-1] == ',' || p[1] =='('))
157 {
158 p--;
159 while (p>s && *p != ' ') p--;
160 mycpy (out, p+1);
161 }
162 else
163 strcpy (out, p+1);
164 if (c=='A' && prepend)
165 initadd(out, flds[i]+2, p);
166 return(out);
167 }
168return(0);
169}
170putkey(nf, flds, nref, keystr)
171 char *flds[], *keystr;
172{
173char t1[50], *sf;
174int ctype, i, count;
175fprintf(fo, ".\\\"");
176if (nf <= 0)
177 fprintf(fo, "%s%c%c", labtab[nref], labc[nref], sep);
178else
179 {
180 while (ctype= *keystr++)
181 {
182 count = atoi(keystr);
183 if (*keystr=='+') count=999;
184 if (count<=0) count=1;
185 for(i=1; i<=count; i++)
186 {
187 sf= fpar(nf, flds, t1, ctype, i, 1);
188 if (sf==0)
189 break;
190 sf = artskp(sf);
191 fprintf(fo, "%s%c", sf, '-');
192 }
193 }
194 fprintf(fo, "%c%d%c%c", FLAG, nref, FLAG, sep);
195 }
196}
197keylet(t, nref)
198 char *t;
199{
200int i;
201int x 'a'-1;
202for(i=1; i<nref;i++)
203 {
204 if (strcmp(labtab[i], t) == 0)
205 x = labc[i];
206 }
207strcpy(labtab[nref]=lbp, t);
208while (*lbp++);
209if (lbp-bflab >NFLAB)
210 err("bflab overflow (%d)", NFLAB);
211if (nref >NLABC)
212 err ("nref in labc overflow (%d)", NLABC);
213# ifdef D1
214fprintf(stderr, "lbp up to %d of 2000\n", lbp-bflab);
215# endif
216return (labc[nref] = x+1);
217}
218mycpy(s,t)
219 char *s, *t;
220{
221while (*t && *t != ',' && *t != ' ')
222 *s++ = *t++;
223*s=0;
224}
225mycpy2 (s, t, n)
226 char *s, *t;
227{
228int c;
229while (n-- && (c= *t++)>0)
230 {
231 if (c==' ')c= '-';
232 *s++ = c;
233 }
234*s=0;
235}
236initadd(to, from, stop)
237 char *to, *from, *stop;
238{
239 int c, nalph 1;
240while (*to) to++;
241while (from<stop)
242 {
243 c = *from++;
244 if (!isalpha(c))
245 {
246 if (nalph)
247 *to++ = '.';
248 nalph=0;
249 continue;
250 }
251 if (nalph++ ==0)
252 *to++ = c;
253 }
254*to=0;
255}
256
257static char *articles[] {"the ", "an ", "a ", 0};
258artskp(s)
259 char *s;
260{
261/* skips over initial "a ", "an ", or "the " in s */
262 char **p, *r1, *r2;
263for(p=articles; *p; p++)
264 {
265 r2 = s;
266 for (r1= *p; ((*r1 ^ *r2) & ~040 ) == 0; r1++)
267 r2++;
268 if (*r1==0 && *r2 != 0)
269 return(r2);
270 }
271return(s);
272}