]> diplodocus.org Git - nmh/blob - docs/historical/2.9BSD/cmds/rescue.c
Added test of encoded Resent-To: and Resent-cc:.
[nmh] / docs / historical / 2.9BSD / cmds / rescue.c
1 /*
2 * Read up the messages in the named files and rehabilitate them into
3 * UNIX+ style messages on standard output.
4 *
5 * +Unix is a trademark of Bell Laboratories.
6 * The optional -d flag tests the arpa net to unix date format
7 * transformer by reading lines from standard input, modifying them
8 * to unix format, and outputing them on standard output.
9 */
10
11 #include <stdio.h>
12 #include <ctype.h>
13 #include <sys/time.h>
14
15 char *index(), *rindex(), *reform();
16 long emitl();
17 int errs;
18 int dateerrs;
19 int dflag;
20 int lastblank;
21
22 main(argc, argv)
23 char **argv;
24 {
25 register char *cp, *fname;
26
27 if (argc < 2) {
28 fprintf(stderr, "Usage: rescue name ...\n");
29 _exit(1);
30 }
31 lastblank = 1;
32 while (--argc) {
33 fname = *++argv;
34 if (strcmp(fname, "-d") == 0) {
35 dflag++;
36 dtest();
37 exit(0);
38 }
39 cp = rindex(fname, '/');
40 if (cp == 0)
41 cp = fname;
42 else
43 cp++;
44 if (strcmp(cp, "cur") == 0)
45 continue;
46 if (strcmp(cp, "select") == 0)
47 continue;
48 if (*cp == ',')
49 continue;
50 while (*cp)
51 if (!isdigit(*cp++)) {
52 fprintf(stderr, "%s: not mh message file\n",
53 fname);
54 goto blog;
55 }
56 fix(fname);
57 blog: ;
58 }
59 if (dateerrs)
60 fprintf(stderr, "%d bad date(s) replaced by current date\n",
61 dateerrs);
62 exit(errs);
63 }
64
65 /*
66 * Fix the named file by putting together a reasonable header
67 * and outputing the stuff onto standard output.
68 */
69 fix(name)
70 char name[];
71 {
72 register FILE *in;
73 char linebuf[BUFSIZ], from[BUFSIZ], date[BUFSIZ];
74 register int inhead;
75 register char *cp;
76
77 if ((in = fopen(name, "r")) == NULL) {
78 perror(name);
79 errs++;
80 return;
81 }
82 from[0] = date[0] = 0;
83 for (; from[0] == 0 || date[0] == 0;) {
84 if (fgets(linebuf, BUFSIZ, in) == NULL)
85 goto noheader;
86 if (strlen(linebuf) == 1)
87 goto noheader;
88 if (linebuf[0] == '-')
89 goto noheader;
90 if (isfield(linebuf, "from")) {
91 cp = index(linebuf, ':');
92 if (cp == 0)
93 goto noheader;
94 cp++;
95 while (*cp && isspace(*cp))
96 cp++;
97 strcpy(from, cp);
98 continue;
99 }
100 if (isfield(linebuf, "date")) {
101 cp = index(linebuf, ':');
102 if (cp == 0)
103 goto noheader;
104 cp++;
105 while (*cp && isspace(*cp))
106 cp++;
107 strcpy(date, cp);
108 continue;
109 }
110 }
111 /*
112 * Ready to print the from line
113 */
114 zap(from);
115 zap(date);
116 if (!lastblank)
117 printf("\n");
118 printf("From %s %s\n", from, reform(date));
119 lastblank = 0;
120 rewind(in);
121 inhead = 1;
122 while (fgets(linebuf, BUFSIZ, in) != NULL) {
123 if (inhead &&
124 (isfield(linebuf, "from") || isfield(linebuf, "date")))
125 continue;
126 if (strlen(linebuf) == 1)
127 inhead = 0;
128 fputs(linebuf, stdout);
129 lastblank = 0;
130 if (linebuf[0] == '\n' && linebuf[1] == 0)
131 lastblank = 1;
132 }
133 fclose(in);
134 return;
135 noheader:
136 fprintf(stderr, "%s: Missing, bad, or incomplete header\n", name);
137 errs++;
138 fclose(in);
139 }
140
141 /*
142 * Determine if the passed line is a header of the given field name
143 */
144 isfield(buf, field)
145 char buf[];
146 char field[];
147 {
148 register char *cp, *cp2;
149
150 cp = buf;
151 cp2 = field;
152 while (lower(*cp++) == lower(*cp2++))
153 ;
154 if (*--cp == ':' && *--cp2 == 0)
155 return(1);
156 return(0);
157 }
158
159 /*
160 * Lower case the given character
161 */
162 lower(c)
163 register int c;
164 {
165
166 if (isupper(c))
167 return(tolower(c));
168 return(c);
169 }
170
171 /*
172 * Remove trailing newline from str, if present.
173 */
174 zap(str)
175 char str[];
176 {
177 register char *cp;
178
179 cp = index(str, '\n');
180 if (cp != 0)
181 *cp = 0;
182 }
183
184 /*
185 * Reformat the given Arpa net style date
186 * back into a unix ctime(3) date.
187 * Unfortunately, there appears to be NO standard arpa net
188 * date format.
189 */
190
191 char *month = "janfebmaraprmayjunjulaugsepoctnovdec";
192
193 char *
194 reform(date)
195 char date[];
196 {
197 static char retdate[35];
198 char dbuf[BUFSIZ];
199 register char *cp, *cp2, *mptr;
200 struct tm d;
201 long x, then;
202
203 cp = date;
204 cp2 = dbuf;
205 while (*cp) {
206 if (*cp == '(') {
207 while (*cp != ')' && *cp)
208 cp++;
209 if (*cp)
210 cp++;
211 goto more;
212 }
213 if (*cp == '\t') {
214 *cp2++ = ' ';
215 cp++;
216 continue;
217 }
218 if (*cp == '-') {
219 *cp2++ = ' ';
220 cp++;
221 continue;
222 }
223 if (isupper(*cp)) {
224 *cp2++ = tolower(*cp++);
225 continue;
226 }
227 *cp2++ = *cp++;
228 more: ;
229 }
230 *cp2 = 0;
231 /*
232 * Okie dokie. Now pick off the date part and store
233 * it away. Only possible formats here are:
234 * mm/dd/yy
235 * and
236 * dd monthname year
237 */
238 if (index(dbuf, '/')) {
239 d.tm_mon = atoi(dbuf) - 1;
240 cp = index(dbuf, '/') + 1;
241 d.tm_mday = atoi(cp);
242 if ((cp = index(cp, '/')) == 0)
243 goto baddate;
244 cp++;
245 d.tm_year = atoi(cp);
246 if (d.tm_year > 1900)
247 d.tm_year -= 1900;
248 cp = index(cp, ' ');
249 if (cp == 0)
250 goto baddate;
251 }
252 else {
253 d.tm_mday = atoi(dbuf);
254 cp = index(dbuf, ' ');
255 if (cp == 0)
256 goto baddate;
257 while (*cp && isspace(*cp))
258 cp++;
259 if (*cp == 0)
260 goto baddate;
261 for (mptr = month; *mptr; mptr += 3)
262 if (strncmp(mptr, cp, 3) == 0)
263 break;
264 if (*mptr == 0)
265 goto baddate;
266 d.tm_mon = (mptr - month)/3;
267 cp = index(cp, ' ');
268 if (cp == 0)
269 goto baddate;
270 while (*cp && isspace(*cp))
271 cp++;
272 if (*cp == 0)
273 goto baddate;
274 d.tm_year = atoi(cp);
275 if (d.tm_year > 1900)
276 d.tm_year -= 1900;
277 cp = index(cp, ' ');
278 if (cp == 0)
279 goto baddate;
280 }
281 /*
282 * Got the month part, now fix up the time.
283 * Possibilities are:
284 * hh:mm
285 * hh:mm [am|pm]
286 * hhmm edt
287 * hh:mm:ss edt
288 * Basically, we lose by ignoring time zone.
289 */
290 for (;;) {
291 while (*cp && isspace(*cp))
292 cp++;
293 if (*cp == 0)
294 goto baddate;
295 if (strncmp(cp, "at ", 3) != 0)
296 break;
297 cp += 3;
298 }
299 d.tm_sec = 0;
300 if (index(cp, ':')) {
301 d.tm_hour = atoi(cp);
302 cp = index(cp, ':') + 1;
303 d.tm_min = atoi(cp);
304 if (index(cp, ':')) {
305 cp = index(cp, ':') + 1;
306 d.tm_sec = atoi(cp);
307 }
308 while (*cp && !isspace(*cp))
309 cp++;
310 while (*cp && isspace(*cp))
311 cp++;
312 if (strncmp(cp, "pm", 2) == 0 && d.tm_hour < 12)
313 d.tm_hour += 12;
314 }
315 else {
316 x = atoi(cp);
317 d.tm_hour = x / 100;
318 d.tm_min = x % 100;
319 }
320 then = emitl(&d);
321 strcpy(retdate, ctime(&then));
322 zap(retdate);
323 return(retdate);
324
325 baddate:
326 dateerrs++;
327 if (dflag)
328 strcpy(retdate, "************************");
329 else {
330 then = time(0);
331 strcpy(retdate, ctime(&then));
332 zap(retdate);
333 }
334 return(retdate);
335 }
336
337 /*
338 * Test the arpa net to UNIX date modifier.
339 * Reads lines from standard input, converting them
340 * to unix format, and displaying both on stdout.
341 */
342 dtest()
343 {
344 char buf[BUFSIZ];
345 register char *cp;
346
347 while (gets(buf)) {
348 cp = reform(buf);
349 printf("\"%s\" \"%s\"\n", buf, cp);
350 }
351 }
352
353 /*
354 * Routine to convert a localtime(3) format date back into
355 * a system format date.
356 *
357 * Hats off to Bob Kridle for the insight that the way to do
358 * this is by binary search of the system date space.
359 */
360
361 struct tm *localtime();
362
363 long
364 emitl(dp)
365 struct tm *dp;
366 {
367 long conv;
368 register int i, bit;
369 struct tm dcopy;
370
371 dcopy = *dp;
372 dp = &dcopy;
373 conv = 0;
374 for (i = 31; i >= 0; i--) {
375 bit = 1 << i;
376 conv |= bit;
377 if (dcmp(localtime(&conv), dp) > 0)
378 conv &= ~bit;
379 }
380 return(conv);
381 }
382
383 /*
384 * Compare two localtime dates, return result.
385 */
386
387 #define DECIDE(a) \
388 if (dp->a > dp2->a) \
389 return(1); \
390 if (dp->a < dp2->a) \
391 return(-1)
392
393 dcmp(dp, dp2)
394 register struct tm *dp, *dp2;
395 {
396
397 DECIDE(tm_year);
398 DECIDE(tm_mon);
399 DECIDE(tm_mday);
400 DECIDE(tm_hour);
401 DECIDE(tm_min);
402 DECIDE(tm_sec);
403 return(0);
404 }