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