Added check for EPERM when checking errno after a failed link(2).
[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              * But don't trim the "body" and "text" components.
324              */
325
326             comp = fmt->f_comp;
327
328             if (! (comp->c_flags & CF_TRIMMED) && comp->c_text) {
329                 i = strlen(comp->c_text);
330                 if (comp->c_text[i - 1] == '\n' &&
331                         strcmp(comp->c_name, "body") != 0 &&
332                         strcmp(comp->c_name, "text") != 0)
333                     comp->c_text[i - 1] = '\0';
334                 comp->c_flags |= CF_TRIMMED;
335             }
336             break;
337         }
338
339     fmt = format;
340
341     while (cp < ep) {
342         switch (fmt->f_type) {
343
344         case FT_COMP:
345             cpstripped (&cp, ep, fmt->f_comp->c_text);
346             break;
347         case FT_COMPF:
348             cptrimmed (&cp, fmt->f_comp->c_text, fmt->f_width, fmt->f_fill, ep - cp);
349             break;
350
351         case FT_LIT:
352             sp = fmt->f_text;
353             while( (c = *sp++) && cp < ep)
354                 *cp++ = c;
355             break;
356         case FT_LITF:
357             sp = fmt->f_text;
358             ljust = 0;
359             i = fmt->f_width;
360             if (i < 0) {
361                 i = -i;
362                 ljust++;                /* XXX should do something with this */
363             }
364             while( (c = *sp++) && --i >= 0 && cp < ep)
365                 *cp++ = c;
366             while( --i >= 0 && cp < ep)
367                 *cp++ = fmt->f_fill;
368             break;
369
370         case FT_STR:
371             cpstripped (&cp, ep, str);
372             break;
373         case FT_STRF:
374             cptrimmed (&cp, str, fmt->f_width, fmt->f_fill, ep - cp);
375             break;
376         case FT_STRLIT:
377             sp = str;
378             while ((c = *sp++) && cp < ep)
379                 *cp++ = c;
380             break;
381         case FT_STRFW:
382             adios (NULL, "internal error (FT_STRFW)");
383
384         case FT_NUM:
385             n = snprintf(cp, ep - cp + 1, "%d", value);
386             if (n >= 0) {
387                 if (n >= ep - cp) {
388                     cp = ep;
389                 } else
390                     cp += n;
391             }
392             break;
393         case FT_NUMF:
394             cpnumber (&cp, value, fmt->f_width, fmt->f_fill, ep - cp);
395             break;
396
397         case FT_CHAR:
398             *cp++ = fmt->f_char;
399             break;
400
401         case FT_DONE:
402             goto finished;
403
404         case FT_IF_S:
405             if (!(value = (str && *str))) {
406                 fmt += fmt->f_skip;
407                 continue;
408             }
409             break;
410
411         case FT_IF_S_NULL:
412             if (!(value = (str == NULL || *str == 0))) {
413                 fmt += fmt->f_skip;
414                 continue;
415             }
416             break;
417
418         case FT_IF_V_EQ:
419             if (value != fmt->f_value) {
420                 fmt += fmt->f_skip;
421                 continue;
422             }
423             break;
424
425         case FT_IF_V_NE:
426             if (value == fmt->f_value) {
427                 fmt += fmt->f_skip;
428                 continue;
429             }
430             break;
431
432         case FT_IF_V_GT:
433             if (value <= fmt->f_value) {
434                 fmt += fmt->f_skip;
435                 continue;
436             }
437             break;
438
439         case FT_IF_MATCH:
440             if (!(value = (str && match (str, fmt->f_text)))) {
441                 fmt += fmt->f_skip;
442                 continue;
443             }
444             break;
445
446         case FT_V_MATCH:
447             if (str)
448                 value = match (str, fmt->f_text);
449             else
450                 value = 0;
451             break;
452
453         case FT_IF_AMATCH:
454             if (!(value = (str && uprf (str, fmt->f_text)))) {
455                 fmt += fmt->f_skip;
456                 continue;
457             }
458             break;
459
460         case FT_V_AMATCH:
461             value = uprf (str, fmt->f_text);
462             break;
463
464         case FT_S_NONNULL:
465             value = (str != NULL && *str != 0);
466             break;
467
468         case FT_S_NULL:
469             value = (str == NULL || *str == 0);
470             break;
471
472         case FT_V_EQ:
473             value = (fmt->f_value == value);
474             break;
475
476         case FT_V_NE:
477             value = (fmt->f_value != value);
478             break;
479
480         case FT_V_GT:
481             value = (fmt->f_value > value);
482             break;
483
484         case FT_GOTO:
485             fmt += fmt->f_skip;
486             continue;
487
488         case FT_NOP:
489             break;
490
491         case FT_LS_COMP:
492             str = fmt->f_comp->c_text;
493             break;
494         case FT_LS_LIT:
495             str = fmt->f_text;
496             break;
497         case FT_LS_GETENV:
498             if (!(str = getenv (fmt->f_text)))
499                 str = "";
500             break;
501         case FT_LS_CFIND:
502             if (!(str = context_find (fmt->f_text)))
503                 str = "";
504             break;
505
506         case FT_LS_DECODECOMP:
507             if (decode_rfc2047(fmt->f_comp->c_text, buffer2, sizeof(buffer2)))
508                 str = buffer2;
509             else
510                 str = fmt->f_comp->c_text;
511             break;
512
513         case FT_LS_DECODE:
514             if (str && decode_rfc2047(str, buffer2, sizeof(buffer2)))
515                 str = buffer2;
516             break;
517
518         case FT_LS_TRIM:
519             if (str) {
520                     unsigned char *xp;
521
522                     strncpy(buffer, str, sizeof(buffer));
523                     buffer[sizeof(buffer)-1] = '\0';
524                     str = buffer;
525                     while (isspace(*str))
526                             str++;
527                     ljust = 0;
528                     if ((i = fmt->f_width) < 0) {
529                             i = -i;
530                             ljust++;
531                     }
532
533                     if (!ljust && i > 0 && (int) strlen(str) > i)
534                             str[i] = '\0';
535                     xp = str;
536                     xp += strlen(str) - 1;
537                     while (xp > str && isspace(*xp))
538                             *xp-- = '\0';
539                     if (ljust && i > 0 && (int) strlen(str) > i)
540                         str += strlen(str) - i;
541             }
542             break;
543
544         case FT_LV_COMPFLAG:
545             value = (fmt->f_comp->c_flags & CF_TRUE) != 0;
546             break;
547         case FT_LV_COMP:
548             value = (comp = fmt->f_comp)->c_text ? atoi(comp->c_text) : 0;
549             break;
550         case FT_LV_LIT:
551             value = fmt->f_value;
552             break;
553         case FT_LV_DAT:
554             value = dat[fmt->f_value];
555             break;
556         case FT_LV_STRLEN:
557             if (str != NULL)
558                     value = strlen(str);
559             else
560                     value = 0;
561             break;
562         case FT_LV_CHAR_LEFT:
563             value = width - (cp - scanl);
564             break;
565         case FT_LV_PLUS_L:
566             value += fmt->f_value;
567             break;
568         case FT_LV_MINUS_L:
569             value = fmt->f_value - value;
570             break;
571         case FT_LV_DIVIDE_L:
572             if (fmt->f_value)
573                 value = value / fmt->f_value;
574             else
575                 value = 0;
576             break;
577         case FT_LV_MODULO_L:
578             if (fmt->f_value)
579                 value = value % fmt->f_value;
580             else
581                 value = 0;
582             break;
583         case FT_SAVESTR:
584             savestr = str;
585             break;
586
587         case FT_LV_SEC:
588             value = fmt->f_comp->c_tws->tw_sec;
589             break;
590         case FT_LV_MIN:
591             value = fmt->f_comp->c_tws->tw_min;
592             break;
593         case FT_LV_HOUR:
594             value = fmt->f_comp->c_tws->tw_hour;
595             break;
596         case FT_LV_MDAY:
597             value = fmt->f_comp->c_tws->tw_mday;
598             break;
599         case FT_LV_MON:
600             value = fmt->f_comp->c_tws->tw_mon + 1;
601             break;
602         case FT_LS_MONTH:
603             str = tw_moty[fmt->f_comp->c_tws->tw_mon];
604             break;
605         case FT_LS_LMONTH:
606             str = lmonth[fmt->f_comp->c_tws->tw_mon];
607             break;
608         case FT_LS_ZONE:
609             str = dtwszone (fmt->f_comp->c_tws);
610             break;
611         case FT_LV_YEAR:
612             value = fmt->f_comp->c_tws->tw_year;
613             break;
614         case FT_LV_WDAY:
615             if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
616                 set_dotw (tws);
617             value = tws->tw_wday;
618             break;
619         case FT_LS_DAY:
620             if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
621                 set_dotw (tws);
622             str = tw_dotw[tws->tw_wday];
623             break;
624         case FT_LS_WEEKDAY:
625             if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
626                 set_dotw (tws);
627             str = tw_ldotw[tws->tw_wday];
628             break;
629         case FT_LV_YDAY:
630             value = fmt->f_comp->c_tws->tw_yday;
631             break;
632         case FT_LV_ZONE:
633             value = fmt->f_comp->c_tws->tw_zone;
634             break;
635         case FT_LV_CLOCK:
636             if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
637                 value = dmktime(fmt->f_comp->c_tws);
638             break;
639         case FT_LV_RCLOCK:
640             if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
641                 value = dmktime(fmt->f_comp->c_tws);
642             value = time((time_t *) 0) - value;
643             break;
644         case FT_LV_DAYF:
645             if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
646                 set_dotw (tws);
647             switch (fmt->f_comp->c_tws->tw_flags & TW_SDAY) {
648                 case TW_SEXP:
649                     value = 1; break;
650                 case TW_SIMP:
651                     value = 0; break;
652                 default:
653                     value = -1; break;
654             }
655         case FT_LV_ZONEF:
656             if ((fmt->f_comp->c_tws->tw_flags & TW_SZONE) == TW_SZEXP)
657                     value = 1;
658             else
659                     value = -1;
660             break;
661         case FT_LV_DST:
662             value = fmt->f_comp->c_tws->tw_flags & TW_DST;
663             break;
664         case FT_LS_822DATE:
665             str = dasctime (fmt->f_comp->c_tws , TW_ZONE);
666             break;
667         case FT_LS_PRETTY:
668             str = dasctime (fmt->f_comp->c_tws, TW_NULL);
669             break;
670
671         case FT_LS_PERS:
672             str = fmt->f_comp->c_mn->m_pers;
673             break;
674         case FT_LS_MBOX:
675             str = fmt->f_comp->c_mn->m_mbox;
676             break;
677         case FT_LS_HOST:
678             str = fmt->f_comp->c_mn->m_host;
679             break;
680         case FT_LS_PATH:
681             str = fmt->f_comp->c_mn->m_path;
682             break;
683         case FT_LS_GNAME:
684             str = fmt->f_comp->c_mn->m_gname;
685             break;
686         case FT_LS_NOTE:
687             str = fmt->f_comp->c_mn->m_note;
688             break;
689         case FT_LS_822ADDR:
690             str = adrformat( fmt->f_comp->c_mn );
691             break;
692         case FT_LV_HOSTTYPE:
693             value = fmt->f_comp->c_mn->m_type;
694             break;
695         case FT_LV_INGRPF:
696             value = fmt->f_comp->c_mn->m_ingrp;
697             break;
698         case FT_LV_NOHOSTF:
699             value = fmt->f_comp->c_mn->m_nohost;
700             break;
701         case FT_LS_ADDR:
702         case FT_LS_FRIENDLY:
703             if ((mn = fmt->f_comp->c_mn) == &fmt_mnull) {
704                 str = fmt->f_comp->c_text;
705                 break;
706             }
707             if (fmt->f_type == FT_LS_ADDR)
708                 goto unfriendly;
709             if ((str = mn->m_pers) == NULL) {
710                 if ((str = mn->m_note)) {
711                     strncpy (buffer, str, sizeof(buffer));
712                     buffer[sizeof(buffer)-1] = '\0';
713                     str = buffer;
714                     if (*str == '(')
715                         str++;
716                     sp = str + strlen(str) - 1;
717                     if (*sp == ')') {
718                         *sp-- = '\0';
719                         while (sp >= str)
720                             if (*sp == ' ')
721                                 *sp-- = '\0';
722                             else
723                                 break;
724                     }
725                 } else if (!(str = get_x400_friendly (mn->m_mbox,
726                                 buffer, sizeof(buffer)))) {
727         unfriendly: ;
728                   switch (mn->m_type) {
729                     case LOCALHOST:
730                         str = mn->m_mbox;
731                         break;
732                     case UUCPHOST:
733                         snprintf (buffer, sizeof(buffer), "%s!%s",
734                                 mn->m_host, mn->m_mbox);
735                         str = buffer;
736                         break;
737                     default:
738                         if (mn->m_mbox) {
739                             snprintf (buffer, sizeof(buffer), "%s@%s",
740                                 mn->m_mbox, mn->m_host);
741                             str= buffer;
742                         }
743                         else
744                             str = mn->m_text;
745                         break;
746                   }
747                 }
748             }
749             break;
750
751
752                 /* UNQUOTEs RFC-2822 quoted-string and quoted-pair */
753         case FT_LS_UNQUOTE:
754             if (str) {          
755                 int m;
756                 strncpy(buffer, str, sizeof(buffer));
757                 /* strncpy doesn't NUL-terminate if it fills the buffer */
758                 buffer[sizeof(buffer)-1] = '\0';
759                 str = buffer;
760         
761                 /* we will parse from buffer to buffer2 */
762                 n = 0; /* n is the input position in str */
763                 m = 0; /* m is the ouput position in buffer2 */
764
765                 while ( str[n] != '\0') {
766                     switch ( str[n] ) {
767                         case '\\':
768                             n++;
769                             if ( str[n] != '\0')
770                                 buffer2[m++] = str[n++];
771                             break;
772                         case '"':
773                             n++;
774                             break;
775                         default:
776                             buffer2[m++] = str[n++];
777                             break;
778                         }
779                 }
780                 buffer2[m] = '\0';
781                 str = buffer2;
782             }
783             break;
784
785         case FT_LOCALDATE:
786             comp = fmt->f_comp;
787             if ((t = comp->c_tws->tw_clock) == 0)
788                 t = dmktime(comp->c_tws);
789             tws = dlocaltime(&t);
790             *comp->c_tws = *tws;
791             break;
792
793         case FT_GMTDATE:
794             comp = fmt->f_comp;
795             if ((t = comp->c_tws->tw_clock) == 0)
796                 t = dmktime(comp->c_tws);
797             tws = dgmtime(&t);
798             *comp->c_tws = *tws;
799             break;
800
801         case FT_PARSEDATE:
802             comp = fmt->f_comp;
803             if (comp->c_flags & CF_PARSED)
804                 break;
805             if ((sp = comp->c_text) && (tws = dparsetime(sp))) {
806                 *comp->c_tws = *tws;
807                 comp->c_flags &= ~CF_TRUE;
808             } else if ((comp->c_flags & CF_DATEFAB) == 0) {
809                 memset ((char *) comp->c_tws, 0, sizeof *comp->c_tws);
810                 comp->c_flags = CF_TRUE;
811             }
812             comp->c_flags |= CF_PARSED;
813             break;
814
815         case FT_FORMATADDR:
816             /* hook for custom address list formatting (see replsbr.c) */
817             str = formataddr (savestr, str);
818             break;
819
820         case FT_CONCATADDR:
821             /* The same as formataddr, but doesn't do duplicate suppression */
822             str = concataddr (savestr, str);
823             break;
824
825         case FT_PUTADDR:
826             /* output the str register as an address component,
827              * splitting it into multiple lines if necessary.  The
828              * value reg. contains the max line length.  The lit.
829              * field may contain a string to prepend to the result
830              * (e.g., "To: ")
831              */
832             {
833             unsigned char *lp;
834             char *lastb;
835             int indent, wid, len;
836
837             lp = str;
838             wid = value;
839             len = strlen (str);
840             sp = fmt->f_text;
841             indent = strlen (sp);
842             wid -= indent;
843             if (wid <= 0) {
844                 adios(NULL, "putaddr -- num register (%d) must be greater "
845                             "than label width (%d)", value, indent);
846             }
847             while( (c = *sp++) && cp < ep)
848                 *cp++ = c;
849             while (len > wid) {
850                 /* try to break at a comma; failing that, break at a
851                  * space.
852                  */
853                 lastb = 0; sp = lp + wid;
854                 while (sp > lp && (c = *--sp) != ',') {
855                     if (! lastb && isspace(c))
856                         lastb = sp - 1;
857                 }
858                 if (sp == lp) {
859                     if (! (sp = lastb)) {
860                         sp = lp + wid - 1;
861                         while (*sp && *sp != ',' && !isspace(*sp))
862                             sp++;
863                         if (*sp != ',')
864                             sp--;
865                     }
866                 }
867                 len -= sp - lp + 1;
868                 while (cp < ep && lp <= sp)
869                     *cp++ = *lp++;
870                 while (isspace(*lp))
871                     lp++, len--;
872                 if (*lp) {
873                     if (cp < ep)
874                         *cp++ = '\n';
875                     for (i=indent; cp < ep && i > 0; i--)
876                         *cp++ = ' ';
877                 }
878             }
879             cpstripped (&cp, ep, lp);
880             }
881             break;
882
883         case FT_PARSEADDR:
884             comp = fmt->f_comp;
885             if (comp->c_flags & CF_PARSED)
886                 break;
887             if (comp->c_mn != &fmt_mnull)
888                 mnfree (comp->c_mn);
889             if ((sp = comp->c_text) && (sp = getname(sp)) &&
890                 (mn = getm (sp, NULL, 0, fmt_norm, NULL))) {
891                 comp->c_mn = mn;
892                 while (getname(""))
893                     ;
894                 comp->c_flags |= CF_PARSED;
895             } else {
896                 while (getname(""))             /* XXX */
897                     ;
898                 comp->c_mn = &fmt_mnull;
899             }
900             break;
901
902         case FT_MYMBOX:
903             /*
904              * if there's no component, we say true.  Otherwise we
905              * say "true" only if we can parse the address and it
906              * matches one of our addresses.
907              */
908             comp = fmt->f_comp;
909             if (comp->c_mn != &fmt_mnull)
910                 mnfree (comp->c_mn);
911             if ((sp = comp->c_text) && (sp = getname(sp)) &&
912                 (mn = getm (sp, NULL, 0, AD_NAME, NULL))) {
913                 comp->c_mn = mn;
914                 if (ismymbox(mn))
915                     comp->c_flags |= CF_TRUE;
916                 else
917                     comp->c_flags &= ~CF_TRUE;
918                 while ((sp = getname(sp)))
919                     if ((comp->c_flags & CF_TRUE) == 0 &&
920                         (mn = getm (sp, NULL, 0, AD_NAME, NULL)))
921                         if (ismymbox(mn))
922                             comp->c_flags |= CF_TRUE;
923             } else {
924                 while (getname(""))             /* XXX */
925                     ;
926                 if (comp->c_text == 0)
927                     comp->c_flags |= CF_TRUE;
928                 else
929                     comp->c_flags &= ~CF_TRUE;
930                 comp->c_mn = &fmt_mnull;
931             }
932             break;
933
934         case FT_ADDTOSEQ:
935 #ifdef LBL
936             /* If we're working on a folder (as opposed to a file), add the
937              * current msg to sequence given in literal field.  Don't
938              * disturb string or value registers.
939              */
940             if (fmt_current_folder)
941                     seq_addmsg(fmt_current_folder, fmt->f_text, dat[0], -1);
942 #endif
943             break;
944         }
945         fmt++;
946     }
947 #ifndef JLR
948     finished:;
949     if (cp[-1] != '\n')
950         *cp++ = '\n';
951     *cp   = 0;
952     return ((struct format *)0);
953 #else /* JLR */
954     if (cp[-1] != '\n')
955         *cp++ = '\n';
956     while (fmt->f_type != FT_DONE)
957         fmt++;
958
959     finished:;
960     *cp = '\0';
961     return (fmt->f_value ? ++fmt : (struct format *) 0);
962
963 #endif /* JLR */
964 }