]> diplodocus.org Git - nmh/blob - docs/historical/SRI-NOSC/scansub.c
Remove the #ifdef UK for the default scan format
[nmh] / docs / historical / SRI-NOSC / scansub.c
1 #include "mh.h"
2 #include "iobuf.h"
3
4 #define FROM 13 /* Start of From field */
5 #define SFROM 16 /* Length of " " */
6 #define DATE 5 /* Start of Date field */
7 #define SDATE 7 /* Length */
8 #define SUBJ 31 /* Start of Subject field */
9 #define SSUBJ (79-SUBJ) /* Size of Subject field */
10 #define BSUBJ 20 /* Room needed in Sub field to */
11 /* add stuff from the body */
12 #define MSGN 0 /* Start of msg name field */
13 #define SMSGN 3 /* Length */
14 #define FLGS 3 /* Start of flags field */
15 #define SFLGS 2 /* Width of flag field */
16
17 struct iobuf scnout, fout;
18 int errno;
19 char scanl[82];
20
21 scan(inb, innum, outnum, curflg)
22 struct iobuf *inb;
23 int outnum;
24 {
25
26 char buf[512], name[NAMESZ], tobuf[32], frombuf[32];
27 register char *cp;
28 int state, subsz, first, compnum;
29 static char *myname;
30
31 if(!myname)
32 myname = getlogn(getruid());
33 tobuf[0] = frombuf[0] = 0;
34 errno = first = 0;
35 state = FLD;
36 compnum = 1;
37
38 for(;;) {
39
40 state = m_getfld(state, name, buf, sizeof buf, inb);
41 if(!first++ && state >= FLD && state != FILEEOF) {
42 if(outnum) {
43 if(fcreat((cp = m_name(outnum)),&scnout)<0) {
44 printf("Error creating msg "); flush();
45 perror(cp); exit(-1);
46 }
47 chmod(cp, m_gmprot());
48 }
49 sfill(scanl, sizeof scanl);
50 scanl[sizeof scanl - 1] = 0;
51 subsz = 0;
52 tobuf[0] = 0;
53 }
54
55 switch(state) {
56
57 case FLD:
58 case FLDEOF:
59 case FLDPLUS:
60 compnum++;
61 if(uleq(name, "from"))
62 frombuf[cpyfrm(buf, frombuf, sizeof frombuf)]=0;
63 else if(uleq(name, "date"))
64 cpydat(buf, scanl+DATE, SDATE);
65 else if(uleq(name, "subject") && scanl[SUBJ] == ' ')
66 subsz = cpy(buf, scanl+SUBJ, SSUBJ);
67 else if(uleq(name, "to") && !tobuf[0])
68 tobuf[cpyfrm(buf, tobuf, sizeof tobuf-1)] = 0;
69 else if(uleq(name, "replied"))
70 cpy("-", scanl+FLGS+1, 1);
71 put(name, buf, &scnout);
72 while(state == FLDPLUS) {
73 state=m_getfld(state,name,buf,sizeof buf,inb);
74 puts(buf, &scnout);
75 }
76 if(state == FLDEOF)
77 goto putscan;
78 continue;
79
80 case BODY:
81 case BODYEOF:
82 compnum = -1;
83 if(buf[0] && subsz < SSUBJ - BSUBJ) {
84 scanl[SUBJ+subsz+1] = '<';
85 scanl[SUBJ+subsz+2] = '<';
86 cpy(buf, scanl+SUBJ+subsz+3, SSUBJ-subsz-3);
87 subsz = SSUBJ;
88 }
89 if(buf[0] && scnout.b_fildes) {
90 putc('\n', &scnout);
91 if(errno) {
92 perror("Write error:");exit(-1);
93 }
94 }
95 puts(buf, &scnout);
96 while(state == BODY) {
97 state=m_getfld(state,name,buf,sizeof buf,inb);
98 puts(buf, &scnout);
99 }
100 if(state == BODYEOF) {
101 putscan: cpymsgn(m_name(innum), scanl+MSGN, SMSGN);
102 if(!frombuf[0] || uleq(frombuf, myname)) {
103 cpy("To:", scanl+FROM, 3);
104 cpy(tobuf, scanl+FROM+3, SFROM-3);
105 } else
106 cpy(frombuf, scanl+FROM, SFROM);
107 if(curflg)
108 cpy("+", scanl+FLGS, SFLGS);
109 trim(scanl);
110 puts(scanl, &fout);
111
112 if(scnout.b_fildes) {
113 fflush(&scnout);
114 if(errno) {
115 perror("Write error");
116 exit(-1);
117 }
118 close(scnout.b_fildes);
119 scnout.b_fildes = 0;
120 }
121 return(1);
122 }
123 break;
124
125 case LENERR:
126 case FMTERR:
127 printf("??Message Format Error ");
128 if(outnum) printf("(Message %d) ", outnum);
129 if(compnum < 0) printf("in the Body.\n");
130 else printf("in Component #%d.\n", compnum);
131 goto badret;
132 default:
133 printf("Getfld returned %d\n", state);
134 badret: if(scnout.b_fildes)
135 fflush(&scnout);
136 return(-1);
137 case FILEEOF:
138 return(0);
139
140 }
141
142 }
143 }
144
145
146 trim(str)
147 {
148 register char *cp;
149
150 cp = str;
151 while(*cp) cp++;
152 while(*--cp == ' ') ;
153 cp++;
154 *cp++ = '\n';
155 *cp++ = 0;
156 }
157
158 sfill(str, cnt)
159 {
160 register char *cp;
161 register int i;
162
163 cp = str; i = cnt;
164 do
165 *cp++ = ' ';
166 while(--i);
167 }
168
169
170 put(name, buf, iob)
171 {
172 register struct iobuf *ip;
173
174 ip = iob;
175 if(ip->b_fildes) {
176 puts(name, iob);
177 putc(':', iob);
178 if(errno) { perror("Write error");exit(-1);}
179 puts(buf, iob);
180 }
181 }
182
183
184 cpy(sfrom, sto, cnt)
185 {
186 register char *from, *to;
187 register int c;
188 int i;
189
190 from = sfrom; to = sto;
191 i = cnt;
192 while(*from == ' ' || *from == '\t' || *from == '\n')
193 from++;
194 while(i--)
195 if(c = *from) {
196 if(c == '\t' || c == ' ' || c == '\n') {
197 *to++ = ' ';
198 do
199 from++;
200 while((c= *from)==' '||c=='\t'||c=='\n');
201 continue;
202 } else
203 *to++ = c;
204 from++;
205 } else
206 break;
207 return(from - sfrom - 1);
208 }
209
210
211 cpydat(sfrom, sto, cnt)
212 {
213 register char *from, *cp;
214 register int c;
215 static int *locvec;
216 long now;
217 char *to;
218
219 if(!locvec) {
220 time(&now);
221 locvec = localtime(&now);
222 }
223 to = sto;
224 for(from = sfrom; (c = *from) < '0' || c > '9'; from++)
225 if(!c)
226 return;
227 c = cnt;
228 for(cp = from; (*cp >= '0' && *cp <= '9') || *cp == ' '; cp++);
229 if(cp = findmonth(cp)) {
230 if(!cp[1]) {
231 *to++ = ' ';
232 c--;
233 }
234 while(*cp && c--)
235 *to++ = *cp++;
236 c--; *to++ = '/';
237 if(from[1] == ' ') {
238 *to++ = ' ';
239 c--;
240 }
241 while(*from >= '0' && *from <= '9' && c--)
242 *to++ = *from++;
243 if(c >= 2) {
244 while(*from < '0' || *from > '9') from++;
245 if((c = atoi(from)) > 1970 && c-1900 < locvec[5]) {
246 *to++ = '/';
247 *to++ = c - 1970 + '0';
248 }
249 }
250 return;
251 }
252 if(from[1] == ' ') {
253 *to++ = ' ';
254 c--;
255 }
256 while(*from && c--)
257 *to++ = *from++;
258 }
259
260
261 char *fromp, fromdlm, pfromdlm;
262
263 cpyfrm(sfrom, sto, cnt)
264 {
265 register char *to, *cp;
266 register int c;
267
268 fromdlm = ' ';
269 fromp = sfrom; to = sto;
270 cp = frmtok();
271 do
272 if(c = *cp++)
273 *to++ = c;
274 else
275 break;
276 while(--cnt);
277 for(;;) {
278 if(cnt < 3) break;
279 if(*(cp = frmtok()) == 0) break;
280 if(*cp == '@' || uleq(cp, "at")) {
281 cp = frmtok();
282 if(!uleq(cp, "rand-unix")) {
283 *to++ = '@';
284 cnt--;
285 do
286 if(c = *cp++)
287 *to++ = c;
288 else
289 break;
290 while(--cnt);
291 }
292 } else if(cnt > 4) {
293 cnt--; *to++ = pfromdlm;
294 do
295 if(c = *cp++)
296 *to++ = c;
297 else
298 break;
299 while(--cnt);
300 }
301 }
302 return(to - sto);
303 }
304
305
306 frmtok()
307 {
308 static tokbuf[64];
309 register char *cp;
310 register int c;
311
312 pfromdlm = fromdlm;
313 cp = tokbuf; *cp = 0;
314 while(c = *fromp++) {
315 if(c == '\t')
316 c = ' ';
317 if(c == ' ' && cp == tokbuf)
318 continue;
319 if(c == ' ' || c == '\n' || c == ',')
320 break;
321 *cp++ = c;
322 *cp = 0;
323 if(c == '@' || *fromp == '@' || cp == &tokbuf[63])
324 break;
325 }
326 fromdlm = c;
327 return(tokbuf);
328 }
329
330
331 /* num specific! */
332
333 cpymsgn(msgnam, addr, len)
334 char *msgnam, *addr;
335 {
336 register char *cp, *sp;
337
338 sp = msgnam;
339 cp = addr + (len - length(sp));
340 while(*sp)
341 *cp++ = *sp++;
342 }
343
344 char *monthtab[] {
345 "jan", "feb", "mar", "apr", "may", "jun",
346 "jul", "aug", "sep", "oct", "nov", "dec",
347 };
348
349 findmonth(str)
350 {
351 register char *cp, *sp;
352 register int i;
353 char buf[4];
354
355 for(cp=str, sp=buf; (*sp++ = *cp++) && sp < &buf[3] && *cp != ' '; );
356 *sp = 0;
357 for(i = 0; i < 12; i++)
358 if(uleq(buf, monthtab[i]))
359 return(locv(0, i+1));
360 return(0);
361 }