]> diplodocus.org Git - nmh/blob - docs/historical/mh-nov-1983/cmds/rescue.c
add documentation on friendly function
[nmh] / docs / historical / mh-nov-1983 / 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 int i;
77
78 if ((in = fopen(name, "r")) == NULL) {
79 perror(name);
80 errs++;
81 return;
82 }
83 from[0] = date[0] = 0;
84 for (; from[0] == 0 || date[0] == 0;) {
85 if (fgets(linebuf, BUFSIZ, in) == NULL)
86 goto noheader;
87 if (strlen(linebuf) == 1)
88 goto noheader;
89 if (linebuf[0] == '-')
90 goto noheader;
91 if (isfield(linebuf, "from")) {
92 cp = index(linebuf, ':');
93 if (cp == 0)
94 goto noheader;
95 cp++;
96 while (*cp && isspace(*cp))
97 cp++;
98 /*
99 ** the below was: strcpy(from, cp);
100 ** but this will not work, since
101 ** headers can be like this:
102 ** From: foo (foo bar)
103 ** which /usr/ucb/Mail gags on... -layer
104 */
105 i = 0;
106 while(*cp && !isspace(*cp))
107 from[i++] = *cp++;
108 from[i] = '\0';
109 continue;
110 }
111 if (isfield(linebuf, "date")) {
112 cp = index(linebuf, ':');
113 if (cp == 0)
114 goto noheader;
115 cp++;
116 while (*cp && isspace(*cp))
117 cp++;
118 strcpy(date, cp);
119 continue;
120 }
121 }
122 /*
123 * Ready to print the from line
124 */
125 zap(from);
126 zap(date);
127 if (!lastblank)
128 printf("\n");
129 printf("From %s %s\n", from, reform(date));
130 lastblank = 0;
131 rewind(in);
132 inhead = 1;
133 while (fgets(linebuf, BUFSIZ, in) != NULL) {
134 if (inhead &&
135 (isfield(linebuf, "from") || isfield(linebuf, "date")))
136 continue;
137 if (strlen(linebuf) == 1)
138 inhead = 0;
139 fputs(linebuf, stdout);
140 lastblank = 0;
141 if (linebuf[0] == '\n' && linebuf[1] == 0)
142 lastblank = 1;
143 }
144 fclose(in);
145 return;
146 noheader:
147 fprintf(stderr, "%s: Missing, bad, or incomplete header\n", name);
148 errs++;
149 fclose(in);
150 }
151
152 /*
153 * Determine if the passed line is a header of the given field name
154 */
155 isfield(buf, field)
156 char buf[];
157 char field[];
158 {
159 register char *cp, *cp2;
160
161 cp = buf;
162 cp2 = field;
163 while (lower(*cp++) == lower(*cp2++))
164 ;
165 if (*--cp == ':' && *--cp2 == 0)
166 return(1);
167 return(0);
168 }
169
170 /*
171 * Lower case the given character
172 */
173 lower(c)
174 register int c;
175 {
176
177 if (isupper(c))
178 return(tolower(c));
179 return(c);
180 }
181
182 /*
183 * Remove trailing newline from str, if present.
184 */
185 zap(str)
186 char str[];
187 {
188 register char *cp;
189
190 cp = index(str, '\n');
191 if (cp != 0)
192 *cp = 0;
193 }
194
195 /*
196 * Reformat the given Arpa net style date
197 * back into a unix ctime(3) date.
198 * Unfortunately, there appears to be NO standard arpa net
199 * date format.
200 */
201
202 char *month = "janfebmaraprmayjunjulaugsepoctnovdec";
203
204 char *
205 reform(date)
206 char date[];
207 {
208 static char retdate[35];
209 char dbuf[BUFSIZ];
210 register char *cp, *cp2, *mptr;
211 struct tm d;
212 long x, then;
213
214 cp = date;
215 cp2 = dbuf;
216 while (*cp) {
217 if (*cp == '(') {
218 while (*cp != ')' && *cp)
219 cp++;
220 if (*cp)
221 cp++;
222 goto more;
223 }
224 if (*cp == '\t') {
225 *cp2++ = ' ';
226 cp++;
227 continue;
228 }
229 if (*cp == '-') {
230 *cp2++ = ' ';
231 cp++;
232 continue;
233 }
234 if (isupper(*cp)) {
235 *cp2++ = tolower(*cp++);
236 continue;
237 }
238 *cp2++ = *cp++;
239 more: ;
240 }
241 *cp2 = 0;
242 /*
243 * Okie dokie. Now pick off the date part and store
244 * it away. Only possible formats here are:
245 * mm/dd/yy
246 * and
247 * dd monthname year
248 */
249 if (index(dbuf, '/')) {
250 d.tm_mon = atoi(dbuf) - 1;
251 cp = index(dbuf, '/') + 1;
252 d.tm_mday = atoi(cp);
253 if ((cp = index(cp, '/')) == 0)
254 goto baddate;
255 cp++;
256 d.tm_year = atoi(cp);
257 if (d.tm_year > 1900)
258 d.tm_year -= 1900;
259 cp = index(cp, ' ');
260 if (cp == 0)
261 goto baddate;
262 }
263 else {
264 d.tm_mday = atoi(dbuf);
265 cp = index(dbuf, ' ');
266 if (cp == 0)
267 goto baddate;
268 while (*cp && isspace(*cp))
269 cp++;
270 if (*cp == 0)
271 goto baddate;
272 for (mptr = month; *mptr; mptr += 3)
273 if (strcmpn(mptr, cp, 3) == 0)
274 break;
275 if (*mptr == 0)
276 goto baddate;
277 d.tm_mon = (mptr - month)/3;
278 cp = index(cp, ' ');
279 if (cp == 0)
280 goto baddate;
281 while (*cp && isspace(*cp))
282 cp++;
283 if (*cp == 0)
284 goto baddate;
285 d.tm_year = atoi(cp);
286 if (d.tm_year > 1900)
287 d.tm_year -= 1900;
288 cp = index(cp, ' ');
289 if (cp == 0)
290 goto baddate;
291 }
292 /*
293 * Got the month part, now fix up the time.
294 * Possibilities are:
295 * hh:mm
296 * hh:mm [am|pm]
297 * hhmm edt
298 * hh:mm:ss edt
299 * Basically, we lose by ignoring time zone.
300 */
301 for (;;) {
302 while (*cp && isspace(*cp))
303 cp++;
304 if (*cp == 0)
305 goto baddate;
306 if (strcmpn(cp, "at ", 3) != 0)
307 break;
308 cp += 3;
309 }
310 d.tm_sec = 0;
311 if (index(cp, ':')) {
312 d.tm_hour = atoi(cp);
313 cp = index(cp, ':') + 1;
314 d.tm_min = atoi(cp);
315 if (index(cp, ':')) {
316 cp = index(cp, ':') + 1;
317 d.tm_sec = atoi(cp);
318 }
319 while (*cp && !isspace(*cp))
320 cp++;
321 while (*cp && isspace(*cp))
322 cp++;
323 if (strcmpn(cp, "pm", 2) == 0 && d.tm_hour < 12)
324 d.tm_hour += 12;
325 }
326 else {
327 x = atoi(cp);
328 d.tm_hour = x / 100;
329 d.tm_min = x % 100;
330 }
331 then = emitl(&d);
332 strcpy(retdate, ctime(&then));
333 zap(retdate);
334 return(retdate);
335
336 baddate:
337 dateerrs++;
338 if (dflag)
339 strcpy(retdate, "************************");
340 else {
341 then = time(0);
342 strcpy(retdate, ctime(&then));
343 zap(retdate);
344 }
345 return(retdate);
346 }
347
348 /*
349 * Test the arpa net to UNIX date modifier.
350 * Reads lines from standard input, converting them
351 * to unix format, and displaying both on stdout.
352 */
353 dtest()
354 {
355 char buf[BUFSIZ];
356 register char *cp;
357
358 while (gets(buf)) {
359 cp = reform(buf);
360 printf("\"%s\" \"%s\"\n", buf, cp);
361 }
362 }
363
364 /*
365 * Routine to convert a localtime(3) format date back into
366 * a system format date.
367 *
368 * Hats off to Bob Kridle for the insight that the way to do
369 * this is by binary search of the system date space.
370 */
371
372 struct tm *localtime();
373
374 long
375 emitl(dp)
376 struct tm *dp;
377 {
378 long conv;
379 register int i, bit;
380 struct tm dcopy;
381
382 dcopy = *dp;
383 dp = &dcopy;
384 conv = 0;
385 for (i = 31; i >= 0; i--) {
386 bit = 1 << i;
387 conv |= bit;
388 if (dcmp(localtime(&conv), dp) > 0)
389 conv &= ~bit;
390 }
391 return(conv);
392 }
393
394 /*
395 * Compare two localtime dates, return result.
396 */
397
398 #define DECIDE(a) \
399 if (dp->a > dp2->a) \
400 return(1); \
401 if (dp->a < dp2->a) \
402 return(-1)
403
404 dcmp(dp, dp2)
405 register struct tm *dp, *dp2;
406 {
407
408 DECIDE(tm_year);
409 DECIDE(tm_mon);
410 DECIDE(tm_mday);
411 DECIDE(tm_hour);
412 DECIDE(tm_min);
413 DECIDE(tm_sec);
414 return(0);
415 }