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