|
|
1.1 root 1: # include "stdio.h"
2: # include "ctype.h"
3: # include "assert.h"
4: # include "cbt.h"
5: # define SAME 0
6: bfile *townfile;
7: bfile *happened;
8: main(argc,argv)
9: char *argv[];
10: {
11: char x[100], y[500], z[100];
12: char city[100];
13: int dbgflg=0;
14: if (isatty(1)) dbgflg=1;
15: argc--; argv++;
16: townfile = bopen("/usr/spool/town/ustowns", 0);
17: happened = bopen("/usr/spool/town/happen", 2);
18: assert(townfile!=NULL);
19: while (argc--)
20: {
21: if (dbgflg) printf("story %s\n",*argv);
22: if (getstory(*argv++, x, y, z)==0)
23: continue;
24: if (dbgflg) printf("got story from %s, len %d start %.20s\n", x, strlen(y), y);
25: where (x, city);
26: if (nothing(y)) continue;
27: if (dbgflg) printf("approaching update city %s\n",city);
28: update (city, z, y);
29: if (dbgflg) printf("past update\n");
30: }
31: bclose(happened);
32: }
33: getstory(name, dateline, sentence, date)
34: char *name, *dateline, *sentence, *date;
35: {
36: char *s, line[100], *p, *t, *strchr(), *ctime();
37: int k;
38: long tn, time();
39: FILE *f;
40: f = fopen(name, "r");
41: if (f==NULL) return(0);
42: fgets(line, 100, f);
43: if (line[2]!='a')
44: {
45: fclose(f);
46: return(0);
47: }
48: while (fgets(line, 100, f))
49: if (line[0]=='\t') break;
50: for(p=line; isspace(*p); p++)
51: ;
52: sentence[0]=0;
53: s = strchr(p, '-');
54: if (s==NULL) return(0);
55: t = strchr(p, '(');
56: if (t==NULL || t>s) t=s;
57: *--t=0;
58: while (isspace(*--t))
59: *t=0;
60: strcpy(dateline, p);
61: while (isspace(*++s));
62: do {
63: trimnl(s);
64: k = sentend(s);
65: strcat(sentence, s);
66: if (k>0) break;
67: strcat(sentence, " ");
68: } while (fgets(s=line, 100, f));
69: tn = time(0);
70: p = ctime(&tn);
71: sprintf(date, "%.6s %.4s", p+4, p+20);
72: fclose(f);
73: return(1);
74: }
75: sentend(s)
76: char *s;
77: {
78: for( s+=2; *s; s++)
79: if (s[0]=='.')
80: {
81: char *p = s;
82: if (s[-2]=='.' || s[-2]==' ')
83: continue;
84: if (s[-3]==' ' &&strncase(s-2, "st", 2)==0)
85: continue;
86: if (strncase(s-2, "Mr", 2)==0)
87: continue;
88: if (strncase(s-3, "Mrs", 3)==0)
89: continue;
90: if (strncase(s-2, "Ms", 2)==0)
91: continue;
92: if (strncase(s-3, "Sen", 3)==0)
93: continue;
94: if (strncase(s-4, "Capt", 4)==0)
95: continue;
96: if (strncase(s-3, "Col", 3)==0)
97: continue;
98: if (strncase(s-3, "Gov", 3)==0)
99: continue;
100: if (strncase(s-3, "Rep", 3)==0)
101: continue;
102: if (strncase(s-2, "Co", 2)==0)
103: continue;
104: if (strncase(s-2, "Dr", 2)==0)
105: continue;
106: if (strncase (s-3, "Rev", 3)==0)
107: continue;
108: while (*++p ==' ' || *p == '\t');
109: if (p[0]=='\'' && p[1]=='\'')
110: {
111: p[2]=0;
112: return(1);
113: }
114: if (*p==0 || isupper(*p) )
115: {
116: s[1]=0;
117: return(1);
118: }
119: }
120: return(0);
121: }
122: strncase(s, t, n)
123: char *s, *t;
124: {
125: while (n--)
126: {
127: if ( (*s ^ *t) & 0337)
128: return(1);
129: if (*s==0) return(0);
130: s++; t++;
131: }
132: return(0);
133: }
134: trimnl(s)
135: char *s;
136: {
137: while (*s)s++;
138: if (*--s=='\n') *s=0;
139: }
140: struct {
141: char *old, *new;
142: } *st, stnames [] = {
143: {"Ala.", "al"},
144: {"Alaska", "ak"},
145: {"Ariz.", "az"},
146: {"Ark.", "ak"},
147: {"Calif.", "ca"},
148: {"Colo.", "co"},
149: {"Conn.", "ct"},
150: {"Del.", "de"},
151: {"Fla.", "fl"},
152: {"Ga.", "ga"},
153: {"Hawaii", "hi"},
154: {"Ida.", "id"},
155: {"Idaho", "id"},
156: {"Ill.", "il"},
157: {"Ind.", "in"},
158: {"Iowa", "ia"},
159: {"Kan.", "ks"},
160: {"Ky.", "ky"},
161: {"La.", "la"},
162: {"Mass.", "ma"},
163: {"Mass", "ma"},
164: {"Md.", "md"},
165: {"Me.", "me"},
166: {"Maine", "me"},
167: {"Mich.", "mi"},
168: {"Mich", "mi"},
169: {"Minn.", "mn"},
170: {"Miss.", "ms"},
171: {"Mo.", "mo"},
172: {"Mont.", "mt"},
173: {"N.C.", "nc"},
174: {"N.D.", "nd"},
175: {"N.H.", "nh"},
176: {"N.J.", "nj"},
177: {"N.M.", "nm"},
178: {"N.Y.", "ny"},
179: {"Neb.", "ne"},
180: {"Nev.", "nv"},
181: {"Ohio", "oh"},
182: {"OHIO", "oh"},
183: {"Okla.", "ok"},
184: {"Ore.", "or"},
185: {"Pa.", "pa"},
186: {"R.I.", "ri"},
187: {"S.C.", "sc"},
188: {"S.D.", "sd"},
189: {"Puerto Rico", "pr"},
190: {"Tenn.", "tn"},
191: {"Texas", "tx"},
192: {"Utah", "ut"},
193: {"Va.", "va"},
194: {"Vt.", "vt"},
195: {"W.Va.", "wv"},
196: {"Wash.", "wa"},
197: {"Wis.", "wi"},
198: {"Wyo.", "wy"},
199: {NULL, NULL},
200: };
201: where (dateline, city)
202: char *dateline, *city;
203: {
204: char *state, *p, *s;
205: state = strchr(dateline, ',');
206: if (state!=NULL)
207: {
208: *state++=0;
209: while (isspace(*state))state++;
210: for(st=stnames; st->old; st++)
211: if (strcmp(state, st->old)==0)
212: break;
213: if (st->old)
214: strcpy(state, st->new);
215: else
216: fprintf(stderr, "State? %s\n",state);
217: }
218: lcase(dateline);
219: find (dateline, state, city);
220: }
221: find (request, state, city)
222: char *request, *state, *city;
223: {
224: mbuf key, rkey, rrec;
225: int btype, bsize;
226: char rka[100], rra[100], best[100], *s;
227: best[0]=0;
228: btype='3'; bsize=' ';
229: key.mdata = request; key.mlen = strlen(request);
230: bseek(townfile, key);
231: rkey.mdata = rka; rrec.mdata = rra;
232: while (bread(townfile, &rkey, &rrec)==NULL)
233: {
234: rkey.mdata[rkey.mlen]=0;
235: rrec.mdata[rrec.mlen]=0;
236: if (strncmp(rkey.mdata, request, key.mlen)!=SAME)
237: break;
238: if (state)
239: {
240: s = strchr(rkey.mdata, ',');
241: if (strncmp(state, s+2, 2))
242: continue;
243: }
244: for(s=rrec.mdata; *s; s++)
245: ;
246: --s;
247: if (*s < btype || (*s == btype && s[-2] > bsize) )
248: {
249: if (state || (s[-2]>'7'))/* don't take small
250: american cities for Ottawa, Toronto...*/
251: {
252: btype = *s;
253: bsize = s[-2];
254: strcpy(best, rkey.mdata);
255: }
256: }
257: }
258: strcpy(city, best);
259: }
260: lcase(s)
261: char *s;
262: {
263: for (; *s; s++)
264: if (isupper(*s))
265: *s = tolower(*s);
266: }
267: update (city, date, text)
268: char *city, *date, *text;
269: {
270: mbuf key, val;
271: char buff[1000];
272: if (city[0]==0 || city[0]==' '|| city[0]=='\t') return;
273: sprintf(buff, "(%s) %s", date, text);
274: val.mdata = buff;
275: val.mlen = strlen(buff);
276: key.mdata = city;
277: key.mlen = strlen(city);
278: bwrite(happened, key, val);
279: printf("%s\n", city);
280: }
281: nothing(s)
282: char *s;
283: {
284: while (isspace(*s)) s++;
285: if (strlen(s)<20) return(1);
286: return(0);
287: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.