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