]>
diplodocus.org Git - nmh/blob - docs/historical/mh-nov-1983/cmds/rescue.c
2 * Read up the messages in the named files and rehabilitate them into
3 * UNIX+ style messages on standard output.
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.
15 char *index(), *rindex(), *reform();
25 register char *cp
, *fname
;
28 fprintf(stderr
, "Usage: rescue name ...\n");
34 if (strcmp(fname
, "-d") == 0) {
39 cp
= rindex(fname
, '/');
44 if (strcmp(cp
, "cur") == 0)
46 if (strcmp(cp
, "select") == 0)
51 if (!isdigit(*cp
++)) {
52 fprintf(stderr
, "%s: not mh message file\n",
60 fprintf(stderr
, "%d bad date(s) replaced by current date\n",
66 * Fix the named file by putting together a reasonable header
67 * and outputing the stuff onto standard output.
73 char linebuf
[BUFSIZ
], from
[BUFSIZ
], date
[BUFSIZ
];
78 if ((in
= fopen(name
, "r")) == NULL
) {
83 from
[0] = date
[0] = 0;
84 for (; from
[0] == 0 || date
[0] == 0;) {
85 if (fgets(linebuf
, BUFSIZ
, in
) == NULL
)
87 if (strlen(linebuf
) == 1)
89 if (linebuf
[0] == '-')
91 if (isfield(linebuf
, "from")) {
92 cp
= index(linebuf
, ':');
96 while (*cp
&& isspace(*cp
))
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
106 while(*cp
&& !isspace(*cp
))
111 if (isfield(linebuf
, "date")) {
112 cp
= index(linebuf
, ':');
116 while (*cp
&& isspace(*cp
))
123 * Ready to print the from line
129 printf("From %s %s\n", from
, reform(date
));
133 while (fgets(linebuf
, BUFSIZ
, in
) != NULL
) {
135 (isfield(linebuf
, "from") || isfield(linebuf
, "date")))
137 if (strlen(linebuf
) == 1)
139 fputs(linebuf
, stdout
);
141 if (linebuf
[0] == '\n' && linebuf
[1] == 0)
147 fprintf(stderr
, "%s: Missing, bad, or incomplete header\n", name
);
153 * Determine if the passed line is a header of the given field name
159 register char *cp
, *cp2
;
163 while (lower(*cp
++) == lower(*cp2
++))
165 if (*--cp
== ':' && *--cp2
== 0)
171 * Lower case the given character
183 * Remove trailing newline from str, if present.
190 cp
= index(str
, '\n');
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
202 char *month
= "janfebmaraprmayjunjulaugsepoctnovdec";
208 static char retdate
[35];
210 register char *cp
, *cp2
, *mptr
;
218 while (*cp
!= ')' && *cp
)
235 *cp2
++ = tolower(*cp
++);
243 * Okie dokie. Now pick off the date part and store
244 * it away. Only possible formats here are:
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)
256 d
.tm_year
= atoi(cp
);
257 if (d
.tm_year
> 1900)
264 d
.tm_mday
= atoi(dbuf
);
265 cp
= index(dbuf
, ' ');
268 while (*cp
&& isspace(*cp
))
272 for (mptr
= month
; *mptr
; mptr
+= 3)
273 if (strcmpn(mptr
, cp
, 3) == 0)
277 d
.tm_mon
= (mptr
- month
)/3;
281 while (*cp
&& isspace(*cp
))
285 d
.tm_year
= atoi(cp
);
286 if (d
.tm_year
> 1900)
293 * Got the month part, now fix up the time.
299 * Basically, we lose by ignoring time zone.
302 while (*cp
&& isspace(*cp
))
306 if (strcmpn(cp
, "at ", 3) != 0)
311 if (index(cp
, ':')) {
312 d
.tm_hour
= atoi(cp
);
313 cp
= index(cp
, ':') + 1;
315 if (index(cp
, ':')) {
316 cp
= index(cp
, ':') + 1;
319 while (*cp
&& !isspace(*cp
))
321 while (*cp
&& isspace(*cp
))
323 if (strcmpn(cp
, "pm", 2) == 0 && d
.tm_hour
< 12)
332 strcpy(retdate
, ctime(&then
));
339 strcpy(retdate
, "************************");
342 strcpy(retdate
, ctime(&then
));
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.
360 printf("\"%s\" \"%s\"\n", buf
, cp
);
365 * Routine to convert a localtime(3) format date back into
366 * a system format date.
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.
372 struct tm
*localtime();
385 for (i
= 31; i
>= 0; i
--) {
388 if (dcmp(localtime(&conv
), dp
) > 0)
395 * Compare two localtime dates, return result.
399 if (dp->a > dp2->a) \
401 if (dp->a < dp2->a) \
405 register struct tm
*dp
, *dp2
;