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