Revert commit 255d4c646c0d7aa6b049052fef47fa083b1b1506 and solve
[mmh] / sbr / fmt_scan.c
1
2 /*
3  * fmt_scan.c -- format string interpretation
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  * This is the engine that processes the format instructions created by
10  * fmt_compile (found in fmt_compile.c).
11  */
12
13 #include <h/mh.h>
14 #include <h/addrsbr.h>
15 #include <h/fmt_scan.h>
16 #include <h/tws.h>
17 #include <h/fmt_compile.h>
18
19 #ifdef HAVE_SYS_TIME_H
20 # include <sys/time.h>
21 #endif
22 #include <time.h>
23 #ifdef MULTIBYTE_SUPPORT
24 #  include <wctype.h>
25 #  include <wchar.h>
26 #endif
27
28 #ifdef LBL
29 struct msgs *fmt_current_folder; /* current folder (set by main program) */
30 #endif
31
32 extern int fmt_norm;            /* defined in sbr/fmt_def.c = AD_NAME */
33 struct mailname fmt_mnull = { NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0,
34                               NULL, NULL };
35
36 /*
37  * static prototypes
38  */
39 static int match (char *, char *);
40 static char *get_x400_friendly (char *, char *, int);
41 static int get_x400_comp (char *, char *, char *, int);
42
43
44 /*
45  * test if string "sub" appears anywhere in
46  * string "str" (case insensitive).
47  */
48
49 static int
50 match (char *str, char *sub)
51 {
52     int c1, c2;
53     char *s1, *s2;
54
55 #ifdef LOCALE
56     while ((c1 = *sub)) {
57         c1 = (isalpha(c1) && isupper(c1)) ? tolower(c1) : c1;
58         while ((c2 = *str++) && c1 != ((isalpha(c2) && isupper(c2)) ? tolower(c2) : c2))
59             ;
60         if (! c2)
61             return 0;
62         s1 = sub + 1; s2 = str;
63         while ((c1 = *s1++) && ((isalpha(c1) && isupper(c1)) ? tolower(c1) : c1) == ((isalpha(c2 =*s2++) && isupper(c2)) ? tolower(c2) : c2))
64             ;
65         if (! c1)
66             return 1;
67     }
68 #else
69     while ((c1 = *sub)) {
70         while ((c2 = *str++) && (c1 | 040) != (c2 | 040))
71             ;
72         if (! c2)
73             return 0;
74         s1 = sub + 1; s2 = str;
75         while ((c1 = *s1++) && (c1 | 040) == (*s2++ | 040))
76             ;
77         if (! c1)
78             return 1;
79     }
80 #endif
81     return 1;
82 }
83
84 /*
85  * copy a number to the destination subject to a maximum width
86  */
87 static void
88 cpnumber(char **dest, int num, unsigned int wid, char fill, size_t n) {
89     int i, c;
90     char *sp;
91     char *cp = *dest;
92     char *ep = cp + n;
93
94     if (cp + wid < ep) {
95         if ((i = (num)) < 0)
96             i = -(num);
97         if ((c = (wid)) < 0)
98             c = -c;
99         sp = cp + c;
100         do {
101             *--sp = (i % 10) + '0';
102             i /= 10;
103         } while (i > 0 && sp > cp);
104         if (i > 0)
105             *sp = '?';
106         else if ((num) < 0 && sp > cp)
107             *--sp = '-';
108         while (sp > cp)
109             *--sp = fill;
110         cp += c;
111     }
112     *dest = cp;
113 }
114
115 /*
116  * copy string from str to dest padding with the fill character to a size
117  * of wid characters. if wid is negative, the string is right aligned
118  * no more than n bytes are copied
119  */
120 static void
121 cptrimmed(char **dest, char *str, unsigned int wid, char fill, size_t n) {
122     int remaining;     /* remaining output width available */
123     int c, ljust;
124     int end;           /* number of input bytes remaining in str */
125 #ifdef MULTIBYTE_SUPPORT
126     int char_len;      /* bytes in current character */
127     int w;
128     wchar_t wide_char;
129 #endif
130     char *sp;          /* current position in source string */
131     char *cp = *dest;  /* current position in destination string */
132     char *ep = cp + n; /* end of destination buffer */
133     int prevCtrl = 1;
134
135     /* get alignment */
136     ljust = 0;
137     if ((remaining = (wid)) < 0) {
138         remaining = -remaining;
139         ljust++;
140     }
141     if ((sp = (str))) {
142         mbtowc(NULL, NULL, 0); /* reset shift state */
143         end = strlen(str);
144         while (*sp && remaining > 0 && end > 0) {
145 #ifdef MULTIBYTE_SUPPORT
146             char_len = mbtowc(&wide_char, sp, end);
147             if (char_len <= 0 || (cp + char_len > ep))
148                 break;
149
150             end -= char_len;
151
152             if (iswcntrl(wide_char) || iswspace(wide_char)) {
153                 sp += char_len;
154 #else
155             end--;
156             /* isnctrl(), etc., take an int argument.  Cygwin's ctype.h
157                intentionally warns if they are passed a char. */
158             int c = *sp;
159             if (iscntrl(c) || isspace(c)) {
160                 sp++;
161 #endif
162                 if (!prevCtrl) {
163                     *cp++ = ' ';
164                     remaining--;
165                 }
166
167                 prevCtrl = 1;
168                 continue;
169             }
170             prevCtrl = 0;
171
172 #ifdef MULTIBYTE_SUPPORT
173             w = wcwidth(wide_char);
174             if (w >= 0 && remaining >= w) {
175                 strncpy(cp, sp, char_len);
176                 cp += char_len;
177                 remaining -= w;
178             }
179             sp += char_len;
180 #else
181             *cp++ = *sp++;
182             remaining--;
183 #endif
184         }
185     }
186
187     if (ljust) {
188         if (cp + remaining > ep)
189             remaining = ep - cp;
190         ep = cp + remaining;
191         if (remaining > 0) {
192             /* copy string to the right */
193             while (--cp >= *dest)
194                 *(cp + remaining) = *cp;
195             /* add padding at the beginning */
196             cp += remaining;
197             for (c=remaining; c>0; c--)
198                 *cp-- = fill;
199         }
200         *dest = ep;
201     } else {
202         /* pad remaining space */
203         while (remaining-- > 0 && cp < ep)
204                 *cp++ = fill;
205         *dest = cp;
206     }
207 }
208
209 static void
210 cpstripped (char **start, char *end, char *str)
211 {
212     int c;
213     char *s = str;
214
215     if (!s)
216         return;
217
218     /* skip any initial control characters or spaces */
219     while ((c = (unsigned char) *s) &&
220 #ifdef LOCALE
221             (iscntrl(c) || isspace(c)))
222 #else
223             (c <= 32))
224 #endif
225         s++;
226
227     /* compact repeated control characters and spaces into a single space */
228     while((c = (unsigned char) *s++) && *start < end)
229         if (!iscntrl(c) && !isspace(c))
230             *(*start)++ = c;
231         else {
232             while ((c = (unsigned char) *s) &&
233 #ifdef LOCALE
234                     (iscntrl(c) || isspace(c)))
235 #else
236                     (c <= 32))
237 #endif
238                 s++;
239             *(*start)++ = ' ';
240         }
241 }
242
243 static char *lmonth[] = { "January",  "February","March",   "April",
244                           "May",      "June",    "July",    "August",
245                           "September","October", "November","December" };
246
247 static char *
248 get_x400_friendly (char *mbox, char *buffer, int buffer_len)
249 {
250     char given[BUFSIZ], surname[BUFSIZ];
251
252     if (mbox == NULL)
253         return NULL;
254     if (*mbox == '"')
255         mbox++;
256     if (*mbox != '/')
257         return NULL;
258
259     if (get_x400_comp (mbox, "/PN=", buffer, buffer_len)) {
260         for (mbox = buffer; (mbox = strchr(mbox, '.')); )
261             *mbox++ = ' ';
262
263         return buffer;
264     }
265
266     if (!get_x400_comp (mbox, "/S=", surname, sizeof(surname)))
267         return NULL;
268
269     if (get_x400_comp (mbox, "/G=", given, sizeof(given)))
270         snprintf (buffer, buffer_len, "%s %s", given, surname);
271     else
272         snprintf (buffer, buffer_len, "%s", surname);
273
274     return buffer;
275 }
276
277 static int
278 get_x400_comp (char *mbox, char *key, char *buffer, int buffer_len)
279 {
280     int idx;
281     char *cp;
282
283     if ((idx = stringdex (key, mbox)) < 0
284             || !(cp = strchr(mbox += idx + strlen (key), '/')))
285         return 0;
286
287     snprintf (buffer, buffer_len, "%*.*s", (int)(cp - mbox), (int)(cp - mbox), mbox);
288     return 1;
289 }
290
291 struct format *
292 fmt_scan (struct format *format, char *scanl, int width, int *dat)
293 {
294     char *cp, *ep;
295     unsigned char *sp;
296     char *savestr = NULL;
297     unsigned char *str = NULL;
298     char buffer[BUFSIZ], buffer2[BUFSIZ];
299     int i, c, ljust, n;
300     int value = 0;
301     time_t t;
302     struct format *fmt;
303     struct comp *comp;
304     struct tws *tws;
305     struct mailname *mn;
306
307     cp = scanl;
308     ep = scanl + width - 1;
309
310     for (fmt = format; fmt->f_type != FT_DONE; fmt++)
311         switch (fmt->f_type) {
312         case FT_PARSEADDR:
313         case FT_PARSEDATE:
314             fmt->f_comp->c_flags &= ~CF_PARSED;
315             break;
316         case FT_COMP:
317         case FT_COMPF:
318         case FT_LS_COMP:
319         case FT_LS_DECODECOMP:
320             /*
321              * Trim these components of any newlines
322              */
323             if (! (fmt->f_comp->c_flags & CF_TRIMMED) &&
324                                         fmt->f_comp->c_text) {
325                 int i = strlen(fmt->f_comp->c_text);
326                 if (fmt->f_comp->c_text[i - 1] == '\n')
327                     fmt->f_comp->c_text[i - 1] = '\0';
328                 fmt->f_comp->c_flags |= CF_TRIMMED;
329             }
330             break;
331         }
332
333     fmt = format;
334
335     while (cp < ep) {
336         switch (fmt->f_type) {
337
338         case FT_COMP:
339             cpstripped (&cp, ep, fmt->f_comp->c_text);
340             break;
341         case FT_COMPF:
342             cptrimmed (&cp, fmt->f_comp->c_text, fmt->f_width, fmt->f_fill, ep - cp);
343             break;
344
345         case FT_LIT:
346             sp = fmt->f_text;
347             while( (c = *sp++) && cp < ep)
348                 *cp++ = c;
349             break;
350         case FT_LITF:
351             sp = fmt->f_text;
352             ljust = 0;
353             i = fmt->f_width;
354             if (i < 0) {
355                 i = -i;
356                 ljust++;                /* XXX should do something with this */
357             }
358             while( (c = *sp++) && --i >= 0 && cp < ep)
359                 *cp++ = c;
360             while( --i >= 0 && cp < ep)
361                 *cp++ = fmt->f_fill;
362             break;
363
364         case FT_STR:
365             cpstripped (&cp, ep, str);
366             break;
367         case FT_STRF:
368             cptrimmed (&cp, str, fmt->f_width, fmt->f_fill, ep - cp);
369             break;
370         case FT_STRLIT:
371             sp = str;
372             while ((c = *sp++) && cp < ep)
373                 *cp++ = c;
374             break;
375         case FT_STRFW:
376             adios (NULL, "internal error (FT_STRFW)");
377
378         case FT_NUM:
379             n = snprintf(cp, ep - cp + 1, "%d", value);
380             if (n >= 0) {
381                 if (n >= ep - cp) {
382                     cp = ep;
383                 } else
384                     cp += n;
385             }
386             break;
387         case FT_NUMF:
388             cpnumber (&cp, value, fmt->f_width, fmt->f_fill, ep - cp);
389             break;
390
391         case FT_CHAR:
392             *cp++ = fmt->f_char;
393             break;
394
395         case FT_DONE:
396             goto finished;
397
398         case FT_IF_S:
399             if (!(value = (str && *str))) {
400                 fmt += fmt->f_skip;
401                 continue;
402             }
403             break;
404
405         case FT_IF_S_NULL:
406             if (!(value = (str == NULL || *str == 0))) {
407                 fmt += fmt->f_skip;
408                 continue;
409             }
410             break;
411
412         case FT_IF_V_EQ:
413             if (value != fmt->f_value) {
414                 fmt += fmt->f_skip;
415                 continue;
416             }
417             break;
418
419         case FT_IF_V_NE:
420             if (value == fmt->f_value) {
421                 fmt += fmt->f_skip;
422                 continue;
423             }
424             break;
425
426         case FT_IF_V_GT:
427             if (value <= fmt->f_value) {
428                 fmt += fmt->f_skip;
429                 continue;
430             }
431             break;
432
433         case FT_IF_MATCH:
434             if (!(value = (str && match (str, fmt->f_text)))) {
435                 fmt += fmt->f_skip;
436                 continue;
437             }
438             break;
439
440         case FT_V_MATCH:
441             if (str)
442                 value = match (str, fmt->f_text);
443             else
444                 value = 0;
445             break;
446
447         case FT_IF_AMATCH:
448             if (!(value = (str && uprf (str, fmt->f_text)))) {
449                 fmt += fmt->f_skip;
450                 continue;
451             }
452             break;
453
454         case FT_V_AMATCH:
455             value = uprf (str, fmt->f_text);
456             break;
457
458         case FT_S_NONNULL:
459             value = (str != NULL && *str != 0);
460             break;
461
462         case FT_S_NULL:
463             value = (str == NULL || *str == 0);
464             break;
465
466         case FT_V_EQ:
467             value = (fmt->f_value == value);
468             break;
469
470         case FT_V_NE:
471             value = (fmt->f_value != value);
472             break;
473
474         case FT_V_GT:
475             value = (fmt->f_value > value);
476             break;
477
478         case FT_GOTO:
479             fmt += fmt->f_skip;
480             continue;
481
482         case FT_NOP:
483             break;
484
485         case FT_LS_COMP:
486             str = fmt->f_comp->c_text;
487             break;
488         case FT_LS_LIT:
489             str = fmt->f_text;
490             break;
491         case FT_LS_GETENV:
492             if (!(str = getenv (fmt->f_text)))
493                 str = "";
494             break;
495         case FT_LS_CFIND:
496             if (!(str = context_find (fmt->f_text)))
497                 str = "";
498             break;
499
500         case FT_LS_DECODECOMP:
501             if (decode_rfc2047(fmt->f_comp->c_text, buffer2, sizeof(buffer2)))
502                 str = buffer2;
503             else
504                 str = fmt->f_comp->c_text;
505             break;
506
507         case FT_LS_DECODE:
508             if (str && decode_rfc2047(str, buffer2, sizeof(buffer2)))
509                 str = buffer2;
510             break;
511
512         case FT_LS_TRIM:
513             if (str) {
514                     unsigned char *xp;
515
516                     strncpy(buffer, str, sizeof(buffer));
517                     buffer[sizeof(buffer)-1] = '\0';
518                     str = buffer;
519                     while (isspace(*str))
520                             str++;
521                     ljust = 0;
522                     if ((i = fmt->f_width) < 0) {
523                             i = -i;
524                             ljust++;
525                     }
526
527                     if (!ljust && i > 0 && (int) strlen(str) > i)
528                             str[i] = '\0';
529                     xp = str;
530                     xp += strlen(str) - 1;
531                     while (xp > str && isspace(*xp))
532                             *xp-- = '\0';
533                     if (ljust && i > 0 && (int) strlen(str) > i)
534                         str += strlen(str) - i;
535             }
536             break;
537
538         case FT_LV_COMPFLAG:
539             value = (fmt->f_comp->c_flags & CF_TRUE) != 0;
540             break;
541         case FT_LV_COMP:
542             value = (comp = fmt->f_comp)->c_text ? atoi(comp->c_text) : 0;
543             break;
544         case FT_LV_LIT:
545             value = fmt->f_value;
546             break;
547         case FT_LV_DAT:
548             value = dat[fmt->f_value];
549             break;
550         case FT_LV_STRLEN:
551             if (str != NULL)
552                     value = strlen(str);
553             else
554                     value = 0;
555             break;
556         case FT_LV_CHAR_LEFT:
557             value = width - (cp - scanl);
558             break;
559         case FT_LV_PLUS_L:
560             value += fmt->f_value;
561             break;
562         case FT_LV_MINUS_L:
563             value = fmt->f_value - value;
564             break;
565         case FT_LV_DIVIDE_L:
566             if (fmt->f_value)
567                 value = value / fmt->f_value;
568             else
569                 value = 0;
570             break;
571         case FT_LV_MODULO_L:
572             if (fmt->f_value)
573                 value = value % fmt->f_value;
574             else
575                 value = 0;
576             break;
577         case FT_SAVESTR:
578             savestr = str;
579             break;
580
581         case FT_LV_SEC:
582             value = fmt->f_comp->c_tws->tw_sec;
583             break;
584         case FT_LV_MIN:
585             value = fmt->f_comp->c_tws->tw_min;
586             break;
587         case FT_LV_HOUR:
588             value = fmt->f_comp->c_tws->tw_hour;
589             break;
590         case FT_LV_MDAY:
591             value = fmt->f_comp->c_tws->tw_mday;
592             break;
593         case FT_LV_MON:
594             value = fmt->f_comp->c_tws->tw_mon + 1;
595             break;
596         case FT_LS_MONTH:
597             str = tw_moty[fmt->f_comp->c_tws->tw_mon];
598             break;
599         case FT_LS_LMONTH:
600             str = lmonth[fmt->f_comp->c_tws->tw_mon];
601             break;
602         case FT_LS_ZONE:
603             str = dtwszone (fmt->f_comp->c_tws);
604             break;
605         case FT_LV_YEAR:
606             value = fmt->f_comp->c_tws->tw_year;
607             break;
608         case FT_LV_WDAY:
609             if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
610                 set_dotw (tws);
611             value = tws->tw_wday;
612             break;
613         case FT_LS_DAY:
614             if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
615                 set_dotw (tws);
616             str = tw_dotw[tws->tw_wday];
617             break;
618         case FT_LS_WEEKDAY:
619             if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
620                 set_dotw (tws);
621             str = tw_ldotw[tws->tw_wday];
622             break;
623         case FT_LV_YDAY:
624             value = fmt->f_comp->c_tws->tw_yday;
625             break;
626         case FT_LV_ZONE:
627             value = fmt->f_comp->c_tws->tw_zone;
628             break;
629         case FT_LV_CLOCK:
630             if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
631                 value = dmktime(fmt->f_comp->c_tws);
632             break;
633         case FT_LV_RCLOCK:
634             if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
635                 value = dmktime(fmt->f_comp->c_tws);
636             value = time((time_t *) 0) - value;
637             break;
638         case FT_LV_DAYF:
639             if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
640                 set_dotw (tws);
641             switch (fmt->f_comp->c_tws->tw_flags & TW_SDAY) {
642                 case TW_SEXP:
643                     value = 1; break;
644                 case TW_SIMP:
645                     value = 0; break;
646                 default:
647                     value = -1; break;
648             }
649         case FT_LV_ZONEF:
650             if ((fmt->f_comp->c_tws->tw_flags & TW_SZONE) == TW_SZEXP)
651                     value = 1;
652             else
653                     value = -1;
654             break;
655         case FT_LV_DST:
656             value = fmt->f_comp->c_tws->tw_flags & TW_DST;
657             break;
658         case FT_LS_822DATE:
659             str = dasctime (fmt->f_comp->c_tws , TW_ZONE);
660             break;
661         case FT_LS_PRETTY:
662             str = dasctime (fmt->f_comp->c_tws, TW_NULL);
663             break;
664
665         case FT_LS_PERS:
666             str = fmt->f_comp->c_mn->m_pers;
667             break;
668         case FT_LS_MBOX:
669             str = fmt->f_comp->c_mn->m_mbox;
670             break;
671         case FT_LS_HOST:
672             str = fmt->f_comp->c_mn->m_host;
673             break;
674         case FT_LS_PATH:
675             str = fmt->f_comp->c_mn->m_path;
676             break;
677         case FT_LS_GNAME:
678             str = fmt->f_comp->c_mn->m_gname;
679             break;
680         case FT_LS_NOTE:
681             str = fmt->f_comp->c_mn->m_note;
682             break;
683         case FT_LS_822ADDR:
684             str = adrformat( fmt->f_comp->c_mn );
685             break;
686         case FT_LV_HOSTTYPE:
687             value = fmt->f_comp->c_mn->m_type;
688             break;
689         case FT_LV_INGRPF:
690             value = fmt->f_comp->c_mn->m_ingrp;
691             break;
692         case FT_LV_NOHOSTF:
693             value = fmt->f_comp->c_mn->m_nohost;
694             break;
695         case FT_LS_ADDR:
696         case FT_LS_FRIENDLY:
697             if ((mn = fmt->f_comp->c_mn) == &fmt_mnull) {
698                 str = fmt->f_comp->c_text;
699                 break;
700             }
701             if (fmt->f_type == FT_LS_ADDR)
702                 goto unfriendly;
703             if ((str = mn->m_pers) == NULL) {
704                 if ((str = mn->m_note)) {
705                     strncpy (buffer, str, sizeof(buffer));
706                     buffer[sizeof(buffer)-1] = '\0';
707                     str = buffer;
708                     if (*str == '(')
709                         str++;
710                     sp = str + strlen(str) - 1;
711                     if (*sp == ')') {
712                         *sp-- = '\0';
713                         while (sp >= str)
714                             if (*sp == ' ')
715                                 *sp-- = '\0';
716                             else
717                                 break;
718                     }
719                 } else if (!(str = get_x400_friendly (mn->m_mbox,
720                                 buffer, sizeof(buffer)))) {
721         unfriendly: ;
722                   switch (mn->m_type) {
723                     case LOCALHOST:
724                         str = mn->m_mbox;
725                         break;
726                     case UUCPHOST:
727                         snprintf (buffer, sizeof(buffer), "%s!%s",
728                                 mn->m_host, mn->m_mbox);
729                         str = buffer;
730                         break;
731                     default:
732                         if (mn->m_mbox) {
733                             snprintf (buffer, sizeof(buffer), "%s@%s",
734                                 mn->m_mbox, mn->m_host);
735                             str= buffer;
736                         }
737                         else
738                             str = mn->m_text;
739                         break;
740                   }
741                 }
742             }
743             break;
744
745
746                 /* UNQUOTEs RFC-2822 quoted-string and quoted-pair */
747         case FT_LS_UNQUOTE:
748             if (str) {          
749                 int m;
750                 strncpy(buffer, str, sizeof(buffer));
751                 /* strncpy doesn't NUL-terminate if it fills the buffer */
752                 buffer[sizeof(buffer)-1] = '\0';
753                 str = buffer;
754         
755                 /* we will parse from buffer to buffer2 */
756                 n = 0; /* n is the input position in str */
757                 m = 0; /* m is the ouput position in buffer2 */
758
759                 while ( str[n] != '\0') {
760                     switch ( str[n] ) {
761                         case '\\':
762                             n++;
763                             if ( str[n] != '\0')
764                                 buffer2[m++] = str[n++];
765                             break;
766                         case '"':
767                             n++;
768                             break;
769                         default:
770                             buffer2[m++] = str[n++];
771                             break;
772                         }
773                 }
774                 buffer2[m] = '\0';
775                 str = buffer2;
776             }
777             break;
778
779         case FT_LOCALDATE:
780             comp = fmt->f_comp;
781             if ((t = comp->c_tws->tw_clock) == 0)
782                 t = dmktime(comp->c_tws);
783             tws = dlocaltime(&t);
784             *comp->c_tws = *tws;
785             break;
786
787         case FT_GMTDATE:
788             comp = fmt->f_comp;
789             if ((t = comp->c_tws->tw_clock) == 0)
790                 t = dmktime(comp->c_tws);
791             tws = dgmtime(&t);
792             *comp->c_tws = *tws;
793             break;
794
795         case FT_PARSEDATE:
796             comp = fmt->f_comp;
797             if (comp->c_flags & CF_PARSED)
798                 break;
799             if ((sp = comp->c_text) && (tws = dparsetime(sp))) {
800                 *comp->c_tws = *tws;
801                 comp->c_flags &= ~CF_TRUE;
802             } else if ((comp->c_flags & CF_DATEFAB) == 0) {
803                 memset ((char *) comp->c_tws, 0, sizeof *comp->c_tws);
804                 comp->c_flags = CF_TRUE;
805             }
806             comp->c_flags |= CF_PARSED;
807             break;
808
809         case FT_FORMATADDR:
810             /* hook for custom address list formatting (see replsbr.c) */
811             str = formataddr (savestr, str);
812             break;
813
814         case FT_CONCATADDR:
815             /* The same as formataddr, but doesn't do duplicate suppression */
816             str = concataddr (savestr, str);
817             break;
818
819         case FT_PUTADDR:
820             /* output the str register as an address component,
821              * splitting it into multiple lines if necessary.  The
822              * value reg. contains the max line length.  The lit.
823              * field may contain a string to prepend to the result
824              * (e.g., "To: ")
825              */
826             {
827             unsigned char *lp;
828             char *lastb;
829             int indent, wid, len;
830
831             lp = str;
832             wid = value;
833             len = strlen (str);
834             sp = fmt->f_text;
835             indent = strlen (sp);
836             wid -= indent;
837             if (wid <= 0) {
838                 adios(NULL, "putaddr -- num register (%d) must be greater "
839                             "than label width (%d)", value, indent);
840             }
841             while( (c = *sp++) && cp < ep)
842                 *cp++ = c;
843             while (len > wid) {
844                 /* try to break at a comma; failing that, break at a
845                  * space.
846                  */
847                 lastb = 0; sp = lp + wid;
848                 while (sp > lp && (c = *--sp) != ',') {
849                     if (! lastb && isspace(c))
850                         lastb = sp - 1;
851                 }
852                 if (sp == lp) {
853                     if (! (sp = lastb)) {
854                         sp = lp + wid - 1;
855                         while (*sp && *sp != ',' && !isspace(*sp))
856                             sp++;
857                         if (*sp != ',')
858                             sp--;
859                     }
860                 }
861                 len -= sp - lp + 1;
862                 while (cp < ep && lp <= sp)
863                     *cp++ = *lp++;
864                 while (isspace(*lp))
865                     lp++, len--;
866                 if (*lp) {
867                     if (cp < ep)
868                         *cp++ = '\n';
869                     for (i=indent; cp < ep && i > 0; i--)
870                         *cp++ = ' ';
871                 }
872             }
873             cpstripped (&cp, ep, lp);
874             }
875             break;
876
877         case FT_PARSEADDR:
878             comp = fmt->f_comp;
879             if (comp->c_flags & CF_PARSED)
880                 break;
881             if (comp->c_mn != &fmt_mnull)
882                 mnfree (comp->c_mn);
883             if ((sp = comp->c_text) && (sp = getname(sp)) &&
884                 (mn = getm (sp, NULL, 0, fmt_norm, NULL))) {
885                 comp->c_mn = mn;
886                 while (getname(""))
887                     ;
888                 comp->c_flags |= CF_PARSED;
889             } else {
890                 while (getname(""))             /* XXX */
891                     ;
892                 comp->c_mn = &fmt_mnull;
893             }
894             break;
895
896         case FT_MYMBOX:
897             /*
898              * if there's no component, we say true.  Otherwise we
899              * say "true" only if we can parse the address and it
900              * matches one of our addresses.
901              */
902             comp = fmt->f_comp;
903             if (comp->c_mn != &fmt_mnull)
904                 mnfree (comp->c_mn);
905             if ((sp = comp->c_text) && (sp = getname(sp)) &&
906                 (mn = getm (sp, NULL, 0, AD_NAME, NULL))) {
907                 comp->c_mn = mn;
908                 if (ismymbox(mn))
909                     comp->c_flags |= CF_TRUE;
910                 else
911                     comp->c_flags &= ~CF_TRUE;
912                 while ((sp = getname(sp)))
913                     if ((comp->c_flags & CF_TRUE) == 0 &&
914                         (mn = getm (sp, NULL, 0, AD_NAME, NULL)))
915                         if (ismymbox(mn))
916                             comp->c_flags |= CF_TRUE;
917             } else {
918                 while (getname(""))             /* XXX */
919                     ;
920                 if (comp->c_text == 0)
921                     comp->c_flags |= CF_TRUE;
922                 else
923                     comp->c_flags &= ~CF_TRUE;
924                 comp->c_mn = &fmt_mnull;
925             }
926             break;
927
928         case FT_ADDTOSEQ:
929 #ifdef LBL
930             /* If we're working on a folder (as opposed to a file), add the
931              * current msg to sequence given in literal field.  Don't
932              * disturb string or value registers.
933              */
934             if (fmt_current_folder)
935                     seq_addmsg(fmt_current_folder, fmt->f_text, dat[0], -1);
936 #endif
937             break;
938         }
939         fmt++;
940     }
941 #ifndef JLR
942     finished:;
943     if (cp[-1] != '\n')
944         *cp++ = '\n';
945     *cp   = 0;
946     return ((struct format *)0);
947 #else /* JLR */
948     if (cp[-1] != '\n')
949         *cp++ = '\n';
950     while (fmt->f_type != FT_DONE)
951         fmt++;
952
953     finished:;
954     *cp = '\0';
955     return (fmt->f_value ? ++fmt : (struct format *) 0);
956
957 #endif /* JLR */
958 }