]> diplodocus.org Git - nmh/blob - sbr/m_getfld.c
e80da57be5a69bd490ac91faf87ec1dd67af8f62
[nmh] / sbr / m_getfld.c
1
2 /*
3 * m_getfld.c -- read/parse a message
4 *
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 */
9
10 #include <h/mh.h>
11 #include <h/mts.h>
12 #include <h/utils.h>
13
14 /* This module has a long and checkered history. First, it didn't burst
15 maildrops correctly because it considered two CTRL-A:s in a row to be
16 an inter-message delimiter. It really is four CTRL-A:s followed by a
17 newline. Unfortunately, MMDF will convert this delimiter *inside* a
18 message to a CTRL-B followed by three CTRL-A:s and a newline. This
19 caused the old version of m_getfld() to declare eom prematurely. The
20 fix was a lot slower than
21
22 c == '\001' && peekc (iob) == '\001'
23
24 but it worked, and to increase generality, MBOX style maildrops could
25 be parsed as well. Unfortunately the speed issue finally caught up with
26 us since this routine is at the very heart of MH.
27
28 To speed things up considerably, the routine Eom() was made an auxilary
29 function called by the macro eom(). Unless we are bursting a maildrop,
30 the eom() macro returns FALSE saying we aren't at the end of the
31 message.
32
33 The next thing to do is to read the mts.conf file and initialize
34 delimiter[] and delimlen accordingly...
35
36 After mhl was made a built-in in msh, m_getfld() worked just fine
37 (using m_unknown() at startup). Until one day: a message which was
38 the result of a bursting was shown. Then, since the burst boundaries
39 aren't CTRL-A:s, m_getfld() would blinding plunge on past the boundary.
40 Very sad. The solution: introduce m_eomsbr(). This hook gets called
41 after the end of each line (since testing for eom involves an fseek()).
42 This worked fine, until one day: a message with no body portion arrived.
43 Then the
44
45 while (eom (c = Getc (iob), iob))
46 continue;
47
48 loop caused m_getfld() to return FMTERR. So, that logic was changed to
49 check for (*eom_action) and act accordingly.
50
51 This worked fine, until one day: someone didn't use four CTRL:A's as
52 their delimiters. So, the bullet got bit and we read mts.h and
53 continue to struggle on. It's not that bad though, since the only time
54 the code gets executed is when inc (or msh) calls it, and both of these
55 have already called mts_init().
56
57 ------------------------
58 (Written by Van Jacobson for the mh6 m_getfld, January, 1986):
59
60 This routine was accounting for 60% of the cpu time used by most mh
61 programs. I spent a bit of time tuning and it now accounts for <10%
62 of the time used. Like any heavily tuned routine, it's a bit
63 complex and you want to be sure you understand everything that it's
64 doing before you start hacking on it. Let me try to emphasize
65 that: every line in this atrocity depends on every other line,
66 sometimes in subtle ways. You should understand it all, in detail,
67 before trying to change any part. If you do change it, test the
68 result thoroughly (I use a hand-constructed test file that exercises
69 all the ways a header name, header body, header continuation,
70 header-body separator, body line and body eom can align themselves
71 with respect to a buffer boundary). "Minor" bugs in this routine
72 result in garbaged or lost mail.
73
74 If you hack on this and slow it down, I, my children and my
75 children's children will curse you.
76
77 This routine gets used on three different types of files: normal,
78 single msg files, "packed" unix or mmdf mailboxs (when used by inc)
79 and packed, directoried bulletin board files (when used by msh).
80 The biggest impact of different file types is in "eom" testing. The
81 code has been carefully organized to test for eom at appropriate
82 times and at no other times (since the check is quite expensive).
83 I have tried to arrange things so that the eom check need only be
84 done on entry to this routine. Since an eom can only occur after a
85 newline, this is easy to manage for header fields. For the msg
86 body, we try to efficiently search the input buffer to see if
87 contains the eom delimiter. If it does, we take up to the
88 delimiter, otherwise we take everything in the buffer. (The change
89 to the body eom/copy processing produced the most noticeable
90 performance difference, particularly for "inc" and "show".)
91
92 There are three qualitatively different things this routine busts
93 out of a message: field names, field text and msg bodies. Field
94 names are typically short (~8 char) and the loop that extracts them
95 might terminate on a colon, newline or max width. I considered
96 using a Vax "scanc" to locate the end of the field followed by a
97 "bcopy" but the routine call overhead on a Vax is too large for this
98 to work on short names. If Berkeley ever makes "inline" part of the
99 C optimiser (so things like "scanc" turn into inline instructions) a
100 change here would be worthwhile.
101
102 Field text is typically 60 - 100 characters so there's (barely)
103 a win in doing a routine call to something that does a "locc"
104 followed by a "bmove". About 30% of the fields have continuations
105 (usually the 822 "received:" lines) and each continuation generates
106 another routine call. "Inline" would be a big win here, as well.
107
108 Messages, as of this writing, seem to come in two flavors: small
109 (~1K) and long (>2K). Most messages have 400 - 600 bytes of headers
110 so message bodies average at least a few hundred characters.
111 Assuming your system uses reasonably sized stdio buffers (1K or
112 more), this routine should be able to remove the body in large
113 (>500 byte) chunks. The makes the cost of a call to "bcopy"
114 small but there is a premium on checking for the eom in packed
115 maildrops. The eom pattern is always a simple string so we can
116 construct an efficient pattern matcher for it (e.g., a Vax "matchc"
117 instruction). Some thought went into recognizing the start of
118 an eom that has been split across two buffers.
119
120 This routine wants to deal with large chunks of data so, rather
121 than "getc" into a local buffer, it uses stdio's buffer. If
122 you try to use it on a non-buffered file, you'll get what you
123 deserve. This routine "knows" that struct FILEs have a _ptr
124 and a _cnt to describe the current state of the buffer and
125 it knows that _filbuf ignores the _ptr & _cnt and simply fills
126 the buffer. If stdio on your system doesn't work this way, you
127 may have to make small changes in this routine.
128
129 This routine also "knows" that an EOF indication on a stream is
130 "sticky" (i.e., you will keep getting EOF until you reposition the
131 stream). If your system doesn't work this way it is broken and you
132 should complain to the vendor. As a consequence of the sticky
133 EOF, this routine will never return any kind of EOF status when
134 there is data in "name" or "buf").
135 */
136
137 /*
138 Purpose
139 =======
140 Reads an Internet message (RFC 5322), or one or more messages stored in a
141 maildrop in mbox (RFC 4155) or MMDF format, from a file stream. Each call
142 to m_getfld() reads one header field, or a portion of the body, in sequence.
143
144 Inputs
145 ======
146 state: message parse state
147 bufsz: maximum number of characters to load into buf
148 iob: input file stream
149
150 Outputs
151 =======
152 name: header field name (array of size NAMESZ=999)
153 buf: either a header field body or message body
154 bufsz: number of characters loaded into buf
155 (return value): message parse state on return from function
156
157 Functions (part of Inputs, really)
158 =========
159 void m_unknown(FILE *iob): Determines the message delimiter string for the
160 maildrop. Called by inc, scan, and msh when reading from a maildrop file.
161
162 void m_eomsbr (int (*action)(int)): Sets the hook to check for end of
163 message in a maildrop. Called only by msh.
164
165 Those functions save state in the State variables listed below.
166
167 Definitions
168 ===========
169 state is one of:
170 FLD // Field returned
171 FLDPLUS // Field returned with more to come
172 FLDEOF // Field returned ending at eom
173 BODY // Body returned with more to come
174 BODYEOF // Body returned ending at eom
175 FILEEOF // Reached end of input file
176 FMTERR // Message Format error
177 LENERR // Name too long error from getfld
178
179 msg_style is maildrop style, one of:
180 MS_UNKNOWN // type not known yet
181 MS_DEFAULT // default (one msg per file)
182 MS_MBOX // Unix-style "from" lines
183 MS_MMDF // string mmdlm2
184 MS_MSH // whacko msh
185
186 State variables (part of Outputs)
187 ===============
188 m_getfld() retains state internally between calls in some state variables.
189
190 These two variables are global, but only used internally by m_getfld.c:
191 int msg_style
192 char *msg_delim
193
194 These are used for the end-of-message matcher when reading maildrops:
195 static unsigned char **pat_map
196 static unsigned char *fdelim
197 static unsigned char *delimend
198 static int fdelimlen
199 static unsigned char *edelim
200 static int edelimlen
201
202 Restriction
203 ===========
204 m_getfld() is restricted to operate on one file stream at a time because of
205 the retained state (see "State variables" above).
206
207 Current usage
208 =============
209 The first call to m_getfld() on a file stream is with a state of FLD.
210 Subsequent calls provide the state returned by the previous call.
211
212 Along the way, I thought of these possible interface changes that we
213 might want to consider before rototilling the internals:
214
215 1) To remove the state argument from the signature:
216 Given the Current usage and Restriction above, the state variable could
217 be removed from the signature and just retained internally.
218
219 2) To remove the Restriction above:
220 One approach would be for m_getfld() to retain multiple copies of that
221 state, one per iob that it sees. Another approach would be for the
222 caller to store it in an opaque struct, the address of which is passed
223 through the interface.
224 */
225
226 /*
227 * static prototypes
228 */
229 static int m_Eom (int, FILE *);
230 static unsigned char *matchc(int, char *, int, char *);
231 static unsigned char *locc(int, unsigned char *, unsigned char);
232
233 #define Getc(iob) getc(iob)
234 #define eom(c,iob) (msg_style != MS_DEFAULT && \
235 (((c) == *msg_delim && m_Eom(c,iob)) ||\
236 (eom_action && (*eom_action)(c))))
237
238 static unsigned char **pat_map;
239
240 static int msg_style = MS_DEFAULT;
241
242 /*
243 * The "full" delimiter string for a packed maildrop consists
244 * of a newline followed by the actual delimiter. E.g., the
245 * full string for a Unix maildrop would be: "\n\nFrom ".
246 * "Fdelim" points to the start of the full string and is used
247 * in the BODY case of the main routine to search the buffer for
248 * a possible eom. Msg_delim points to the first character of
249 * the actual delim. string (i.e., fdelim+1). Edelim
250 * points to the 2nd character of actual delimiter string. It
251 * is used in m_Eom because the first character of the string
252 * has been read and matched before m_Eom is called.
253 */
254 static char *msg_delim = "";
255 static unsigned char *fdelim;
256 static unsigned char *delimend;
257 static int fdelimlen;
258 static unsigned char *edelim;
259 static int edelimlen;
260
261 static int (*eom_action)(int) = NULL;
262
263 #ifdef _FSTDIO
264 # define _ptr _p /* Gag */
265 # define _cnt _r /* Retch */
266 # define _filbuf __srget /* Puke */
267 # define DEFINED__FILBUF_TO_SOMETHING_SPECIFIC
268
269 # if defined __CYGWIN__
270 /* Cygwin's stdio.h does not declare __srget(). */
271 int __srget(FILE *);
272 # endif /* __CYGWIN__ */
273 #endif
274
275 #ifndef DEFINED__FILBUF_TO_SOMETHING_SPECIFIC
276 extern int _filbuf(FILE*);
277 #endif
278
279
280 int
281 m_getfld (int state, unsigned char name[NAMESZ], unsigned char *buf,
282 int *bufsz, FILE *iob)
283 {
284 register unsigned char *bp, *cp, *ep, *sp;
285 register int cnt, c, i, j;
286
287 if ((c = Getc(iob)) < 0) {
288 *bufsz = 0;
289 *buf = 0;
290 return FILEEOF;
291 }
292 if (eom (c, iob)) {
293 if (! eom_action) {
294 /* flush null messages */
295 while ((c = Getc(iob)) >= 0 && eom (c, iob))
296 ;
297 if (c >= 0)
298 ungetc(c, iob);
299 }
300 *bufsz = 0;
301 *buf = 0;
302 return FILEEOF;
303 }
304
305 switch (state) {
306 case FLDEOF:
307 case BODYEOF:
308 case FLD:
309 if (c == '\n' || c == '-') {
310 /* we hit the header/body separator */
311 while (c != '\n' && (c = Getc(iob)) >= 0)
312 ;
313
314 if (c < 0 || (c = Getc(iob)) < 0 || eom (c, iob)) {
315 if (! eom_action) {
316 /* flush null messages */
317 while ((c = Getc(iob)) >= 0 && eom (c, iob))
318 ;
319 if (c >= 0)
320 ungetc(c, iob);
321 }
322 *bufsz = 0;
323 *buf = 0;
324 return FILEEOF;
325 }
326 state = BODY;
327 goto body;
328 }
329 /*
330 * get the name of this component. take characters up
331 * to a ':', a newline or NAMESZ-1 characters, whichever
332 * comes first.
333 */
334 cp = name;
335 i = NAMESZ - 1;
336 for (;;) {
337 #ifdef LINUX_STDIO
338 bp = sp = (unsigned char *) iob->_IO_read_ptr - 1;
339 j = (cnt = ((long) iob->_IO_read_end -
340 (long) iob->_IO_read_ptr) + 1) < i ? cnt : i;
341 #elif defined(__DragonFly__)
342 bp = sp = (unsigned char *) ((struct __FILE_public *)iob)->_p - 1;
343 j = (cnt = ((struct __FILE_public *)iob)->_r+1) < i ? cnt : i;
344 #else
345 bp = sp = (unsigned char *) iob->_ptr - 1;
346 j = (cnt = iob->_cnt+1) < i ? cnt : i;
347 #endif
348 while (--j >= 0 && (c = *bp++) != ':' && c != '\n')
349 *cp++ = c;
350
351 j = bp - sp;
352 if ((cnt -= j) <= 0) {
353 #ifdef LINUX_STDIO
354 iob->_IO_read_ptr = iob->_IO_read_end;
355 if (__underflow(iob) == EOF) {
356 #elif defined(__DragonFly__)
357 if (__srget(iob) == EOF) {
358 #else
359 if (_filbuf(iob) == EOF) {
360 #endif
361 *cp = *buf = 0;
362 advise (NULL, "eof encountered in field \"%s\"", name);
363 return FMTERR;
364 }
365 #ifdef LINUX_STDIO
366 iob->_IO_read_ptr++; /* NOT automatic in __underflow()! */
367 #endif
368 } else {
369 #ifdef LINUX_STDIO
370 iob->_IO_read_ptr = bp + 1;
371 #elif defined(__DragonFly__)
372 ((struct __FILE_public *)iob)->_p = bp + 1;
373 ((struct __FILE_public *)iob)->_r = cnt - 1;
374 #else
375 iob->_ptr = bp + 1;
376 iob->_cnt = cnt - 1;
377 #endif
378 }
379 if (c == ':')
380 break;
381
382 /*
383 * something went wrong. possibilities are:
384 * . hit a newline (error)
385 * . got more than namesz chars. (error)
386 * . hit the end of the buffer. (loop)
387 */
388 if (c == '\n') {
389 /* We hit the end of the line without seeing ':' to
390 * terminate the field name. This is usually (always?)
391 * spam. But, blowing up is lame, especially when
392 * scan(1)ing a folder with such messages. Pretend such
393 * lines are the first of the body (at least mutt also
394 * handles it this way). */
395
396 /* See if buf can hold this line, since we were assuming
397 * we had a buffer of NAMESZ, not bufsz. */
398 /* + 1 for the newline */
399 if (*bufsz < j + 1) {
400 /* No, it can't. Oh well, guess we'll blow up. */
401 *cp = *buf = 0;
402 advise (NULL, "eol encountered in field \"%s\"", name);
403 state = FMTERR;
404 goto finish;
405 }
406 memcpy (buf, name, j - 1);
407 buf[j - 1] = '\n';
408 buf[j] = '\0';
409 /* mhparse.c:get_content wants to find the position of the
410 * body start, but it thinks there's a blank line between
411 * the header and the body (naturally!), so seek back so
412 * that things line up even though we don't have that
413 * blank line in this case. Simpler parsers (e.g. mhl)
414 * get extra newlines, but that should be harmless enough,
415 * right? This is a corrupt message anyway. */
416 fseek (iob, ftell (iob) - 2, SEEK_SET);
417 return BODY;
418 }
419 if ((i -= j) <= 0) {
420 *cp = *buf = 0;
421 advise (NULL, "field name \"%s\" exceeds %d bytes", name, NAMESZ - 2);
422 state = LENERR;
423 goto finish;
424 }
425 }
426
427 while (isspace (*--cp) && cp >= name)
428 ;
429 *++cp = 0;
430 /* fall through */
431
432 case FLDPLUS:
433 /*
434 * get (more of) the text of a field. take
435 * characters up to the end of this field (newline
436 * followed by non-blank) or bufsz-1 characters.
437 */
438 cp = buf; i = *bufsz-1;
439 for (;;) {
440 #ifdef LINUX_STDIO
441 cnt = (long) iob->_IO_read_end - (long) iob->_IO_read_ptr;
442 bp = (unsigned char *) --iob->_IO_read_ptr;
443 #elif defined(__DragonFly__)
444 cnt = ((struct __FILE_public *)iob)->_r++;
445 bp = (unsigned char *) --((struct __FILE_public *)iob)->_p;
446 #else
447 cnt = iob->_cnt++;
448 bp = (unsigned char *) --iob->_ptr;
449 #endif
450 c = cnt < i ? cnt : i;
451 while ((ep = locc( c, bp, '\n' ))) {
452 /*
453 * if we hit the end of this field, return.
454 */
455 if ((j = *++ep) != ' ' && j != '\t') {
456 #ifdef LINUX_STDIO
457 j = ep - (unsigned char *) iob->_IO_read_ptr;
458 memcpy (cp, iob->_IO_read_ptr, j);
459 iob->_IO_read_ptr = ep;
460 #elif defined(__DragonFly__)
461 j = ep - (unsigned char *) ((struct __FILE_public *)iob)->_p;
462 memcpy (cp, ((struct __FILE_public *)iob)->_p, j);
463 ((struct __FILE_public *)iob)->_p = ep;
464 ((struct __FILE_public *)iob)->_r -= j;
465 #else
466 j = ep - (unsigned char *) iob->_ptr;
467 memcpy (cp, iob->_ptr, j);
468 iob->_ptr = ep;
469 iob->_cnt -= j;
470 #endif
471 cp += j;
472 state = FLD;
473 goto finish;
474 }
475 c -= ep - bp;
476 bp = ep;
477 }
478 /*
479 * end of input or dest buffer - copy what we've found.
480 */
481 #ifdef LINUX_STDIO
482 c += bp - (unsigned char *) iob->_IO_read_ptr;
483 memcpy( cp, iob->_IO_read_ptr, c);
484 #elif defined(__DragonFly__)
485 c += bp - (unsigned char *) ((struct __FILE_public *)iob)->_p;
486 memcpy( cp, ((struct __FILE_public *)iob)->_p, c);
487 #else
488 c += bp - (unsigned char *) iob->_ptr;
489 memcpy( cp, iob->_ptr, c);
490 #endif
491 i -= c;
492 cp += c;
493 if (i <= 0) {
494 /* the dest buffer is full */
495 #ifdef LINUX_STDIO
496 iob->_IO_read_ptr += c;
497 #elif defined(__DragonFly__)
498 ((struct __FILE_public *)iob)->_r -= c;
499 ((struct __FILE_public *)iob)->_p += c;
500 #else
501 iob->_cnt -= c;
502 iob->_ptr += c;
503 #endif
504 state = FLDPLUS;
505 break;
506 }
507 /*
508 * There's one character left in the input buffer.
509 * Copy it & fill the buffer. If the last char
510 * was a newline and the next char is not whitespace,
511 * this is the end of the field. Otherwise loop.
512 */
513 --i;
514 #ifdef LINUX_STDIO
515 *cp++ = j = *(iob->_IO_read_ptr + c);
516 iob->_IO_read_ptr = iob->_IO_read_end;
517 c = __underflow(iob);
518 iob->_IO_read_ptr++; /* NOT automatic! */
519 #elif defined(__DragonFly__)
520 *cp++ =j = *(((struct __FILE_public *)iob)->_p + c);
521 c = __srget(iob);
522 #else
523 *cp++ = j = *(iob->_ptr + c);
524 c = _filbuf(iob);
525 #endif
526 if (c == EOF ||
527 ((j == '\0' || j == '\n') && c != ' ' && c != '\t')) {
528 if (c != EOF) {
529 #ifdef LINUX_STDIO
530 --iob->_IO_read_ptr;
531 #elif defined(__DragonFly__)
532 --((struct __FILE_public *)iob)->_p;
533 ++((struct __FILE_public *)iob)->_r;
534 #else
535 --iob->_ptr;
536 ++iob->_cnt;
537 #endif
538 }
539 state = FLD;
540 break;
541 }
542 }
543 break;
544
545 case BODY:
546 body:
547 /*
548 * get the message body up to bufsz characters or the
549 * end of the message. Sleazy hack: if bufsz is negative
550 * we assume that we were called to copy directly into
551 * the output buffer and we don't add an eos.
552 */
553 i = (*bufsz < 0) ? -*bufsz : *bufsz-1;
554 #ifdef LINUX_STDIO
555 bp = (unsigned char *) --iob->_IO_read_ptr;
556 cnt = (long) iob->_IO_read_end - (long) iob->_IO_read_ptr;
557 #elif defined(__DragonFly__)
558 bp = (unsigned char *) --((struct __FILE_public *)iob)->_p;
559 cnt = ++((struct __FILE_public *)iob)->_r;
560 #else
561 bp = (unsigned char *) --iob->_ptr;
562 cnt = ++iob->_cnt;
563 #endif
564 c = (cnt < i ? cnt : i);
565 if (msg_style != MS_DEFAULT && c > 1) {
566 /*
567 * packed maildrop - only take up to the (possible)
568 * start of the next message. This "matchc" should
569 * probably be a Boyer-Moore matcher for non-vaxen,
570 * particularly since we have the alignment table
571 * all built for the end-of-buffer test (next).
572 * But our vax timings indicate that the "matchc"
573 * instruction is 50% faster than a carefully coded
574 * B.M. matcher for most strings. (So much for elegant
575 * algorithms vs. brute force.) Since I (currently)
576 * run MH on a vax, we use the matchc instruction. --vj
577 */
578 if ((ep = matchc( fdelimlen, fdelim, c, bp )))
579 c = ep - bp + 1;
580 else {
581 /*
582 * There's no delim in the buffer but there may be
583 * a partial one at the end. If so, we want to leave
584 * it so the "eom" check on the next call picks it up.
585 * Use a modified Boyer-Moore matcher to make this
586 * check relatively cheap. The first "if" figures
587 * out what position in the pattern matches the last
588 * character in the buffer. The inner "while" matches
589 * the pattern against the buffer, backwards starting
590 * at that position. Note that unless the buffer
591 * ends with one of the characters in the pattern
592 * (excluding the first and last), we do only one test.
593 */
594 ep = bp + c - 1;
595 if ((sp = pat_map[*ep])) {
596 do {
597 /* This if() is true unless (a) the buffer is too
598 * small to contain this delimiter prefix, or
599 * (b) it contains exactly enough chars for the
600 * delimiter prefix.
601 * For case (a) obviously we aren't going to match.
602 * For case (b), if the buffer really contained exactly
603 * a delim prefix, then the m_eom call at entry
604 * should have found it. Thus it's not a delim
605 * and we know we won't get a match.
606 */
607 if (((sp - fdelim) + 2) <= c) {
608 cp = sp;
609 /* Unfortunately although fdelim has a preceding NUL
610 * we can't use this as a sentinel in case the buffer
611 * contains a NUL in exactly the wrong place (this
612 * would cause us to run off the front of fdelim).
613 */
614 while (*--ep == *--cp)
615 if (cp < fdelim)
616 break;
617 if (cp < fdelim) {
618 /* we matched the entire delim prefix,
619 * so only take the buffer up to there.
620 * we know ep >= bp -- check above prevents underrun
621 */
622 c = (ep - bp) + 2;
623 break;
624 }
625 }
626 /* try matching one less char of delim string */
627 ep = bp + c - 1;
628 } while (--sp > fdelim);
629 }
630 }
631 }
632 memcpy( buf, bp, c );
633 #ifdef LINUX_STDIO
634 iob->_IO_read_ptr += c;
635 #elif defined(__DragonFly__)
636 ((struct __FILE_public *)iob)->_r -= c;
637 ((struct __FILE_public *)iob)->_p += c;
638 #else
639 iob->_cnt -= c;
640 iob->_ptr += c;
641 #endif
642 if (*bufsz < 0) {
643 *bufsz = c;
644 return (state);
645 }
646 cp = buf + c;
647 break;
648
649 default:
650 adios (NULL, "m_getfld() called with bogus state of %d", state);
651 }
652 finish:
653 *cp = 0;
654 *bufsz = cp - buf;
655 return (state);
656 }
657
658
659 void
660 m_unknown(FILE *iob)
661 {
662 register int c;
663 register long pos;
664 char text[10];
665 register char *cp;
666 register char *delimstr;
667
668 /*
669 * Figure out what the message delimitter string is for this
670 * maildrop. (This used to be part of m_Eom but I didn't like
671 * the idea of an "if" statement that could only succeed on the
672 * first call to m_Eom getting executed on each call, i.e., at
673 * every newline in the message).
674 *
675 * If the first line of the maildrop is a Unix "From " line, we
676 * say the style is MBOX and eat the rest of the line. Otherwise
677 * we say the style is MMDF and look for the delimiter string
678 * specified when nmh was built (or from the mts.conf file).
679 */
680
681 msg_style = MS_UNKNOWN;
682
683 pos = ftell (iob);
684 if (fread (text, sizeof(*text), 5, iob) == 5
685 && strncmp (text, "From ", 5) == 0) {
686 msg_style = MS_MBOX;
687 delimstr = "\nFrom ";
688 while ((c = getc (iob)) != '\n' && c >= 0)
689 ;
690 } else {
691 /* not a Unix style maildrop */
692 fseek (iob, pos, SEEK_SET);
693 if (mmdlm2 == NULL || *mmdlm2 == 0)
694 mmdlm2 = "\001\001\001\001\n";
695 delimstr = mmdlm2;
696 msg_style = MS_MMDF;
697 }
698 c = strlen (delimstr);
699 fdelim = (unsigned char *) mh_xmalloc((size_t) (c + 3));
700 *fdelim++ = '\0';
701 *fdelim = '\n';
702 msg_delim = (char *)fdelim+1;
703 edelim = (unsigned char *)msg_delim+1;
704 fdelimlen = c + 1;
705 edelimlen = c - 1;
706 strcpy (msg_delim, delimstr);
707 delimend = (unsigned char *)msg_delim + edelimlen;
708 if (edelimlen <= 1)
709 adios (NULL, "maildrop delimiter must be at least 2 bytes");
710 /*
711 * build a Boyer-Moore end-position map for the matcher in m_getfld.
712 * N.B. - we don't match just the first char (since it's the newline
713 * separator) or the last char (since the matchc would have found it
714 * if it was a real delim).
715 */
716 pat_map = (unsigned char **) calloc (256, sizeof(unsigned char *));
717
718 for (cp = (char *) fdelim + 1; cp < (char *) delimend; cp++ )
719 pat_map[(unsigned char)*cp] = (unsigned char *) cp;
720
721 if (msg_style == MS_MMDF) {
722 /* flush extra msg hdrs */
723 while ((c = Getc(iob)) >= 0 && eom (c, iob))
724 ;
725 if (c >= 0)
726 ungetc(c, iob);
727 }
728 }
729
730
731 void
732 m_eomsbr (int (*action)(int))
733 {
734 if ((eom_action = action)) {
735 msg_style = MS_MSH;
736 *msg_delim = 0;
737 fdelimlen = 1;
738 delimend = fdelim;
739 } else {
740 msg_style = MS_MMDF;
741 msg_delim = (char *)fdelim + 1;
742 fdelimlen = strlen((char *)fdelim);
743 delimend = (unsigned char *)(msg_delim + edelimlen);
744 }
745 }
746
747
748 /*
749 * test for msg delimiter string
750 */
751
752 static int
753 m_Eom (int c, FILE *iob)
754 {
755 register long pos = 0L;
756 register int i;
757 char text[10];
758
759 pos = ftell (iob);
760 if ((i = fread (text, sizeof *text, edelimlen, iob)) != edelimlen
761 || strncmp (text, (char *)edelim, edelimlen)) {
762 if (i == 0 && msg_style == MS_MBOX)
763 /* the final newline in the (brain damaged) unix-format
764 * maildrop is part of the delimitter - delete it.
765 */
766 return 1;
767
768 #if 0
769 fseek (iob, pos, SEEK_SET);
770 #endif
771
772 fseek (iob, (long)(pos-1), SEEK_SET);
773 getc (iob); /* should be OK */
774 return 0;
775 }
776
777 if (msg_style == MS_MBOX) {
778 while ((c = getc (iob)) != '\n')
779 if (c < 0)
780 break;
781 }
782
783 return 1;
784 }
785
786
787 static unsigned char *
788 matchc(int patln, char *pat, int strln, char *str)
789 {
790 register char *es = str + strln - patln;
791 register char *sp;
792 register char *pp;
793 register char *ep = pat + patln;
794 register char pc = *pat++;
795
796 for(;;) {
797 while (pc != *str++)
798 if (str > es)
799 return 0;
800 if (str > es+1)
801 return 0;
802 sp = str; pp = pat;
803 while (pp < ep && *sp++ == *pp)
804 pp++;
805 if (pp >= ep)
806 return ((unsigned char *)--str);
807 }
808 }
809
810
811 /*
812 * Locate character "term" in the next "cnt" characters of "src".
813 * If found, return its address, otherwise return 0.
814 */
815
816 static unsigned char *
817 locc(int cnt, unsigned char *src, unsigned char term)
818 {
819 while (*src++ != term && --cnt > 0);
820
821 return (cnt > 0 ? --src : (unsigned char *)0);
822 }
823