]> diplodocus.org Git - nmh/blob - sbr/fmt_scan.c
83e48b8b4306f5beff856db266dcfcb5d80593f0
[nmh] / sbr / fmt_scan.c
1 /* fmt_scan.c -- format string interpretation
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 *
7 * This is the engine that processes the format instructions created by
8 * fmt_compile (found in fmt_compile.c).
9 */
10
11 #include <h/mh.h>
12 #include <h/addrsbr.h>
13 #include <h/fmt_scan.h>
14 #include <h/tws.h>
15 #include <h/fmt_compile.h>
16 #include <h/utils.h>
17 #include "unquote.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 struct mailname fmt_mnull = { NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0,
29 NULL, NULL };
30
31 /*
32 * static prototypes
33 */
34 static int match (char *, char *);
35 static char *get_x400_friendly (char *, char *, int);
36 static int get_x400_comp (char *, char *, char *, int);
37
38
39 /*
40 * test if string "sub" appears anywhere in
41 * string "str" (case insensitive).
42 */
43
44 static int
45 match (char *str, char *sub)
46 {
47 int c1, c2;
48 char *s1, *s2;
49
50 while ((c1 = *sub)) {
51 c1 = tolower((unsigned char)c1);
52 while ((c2 = *str++) && c1 != tolower((unsigned char)c2))
53 ;
54 if (! c2)
55 return 0;
56 s1 = sub + 1; s2 = str;
57 while ((c1 = *s1++) &&
58 tolower((unsigned char)c1) == tolower((unsigned char)(c2 = *s2++)))
59 ;
60 if (! c1)
61 return 1;
62 }
63 return 1;
64 }
65
66 /*
67 * copy a number to the destination subject to a maximum width
68 */
69 void
70 cpnumber(charstring_t dest, int num, unsigned int wid, char fill, size_t max) {
71 if (wid < (num >= 0 ? max : max-1)) {
72 /* Build up the string representation of num in reverse. */
73 charstring_t rev = charstring_create (0);
74 int i = num >= 0 ? num : -num;
75
76 do {
77 charstring_push_back (rev, i % 10 + '0');
78 i /= 10;
79 } while (--wid > 0 && i > 0);
80 if (i > 0) {
81 /* Overflowed the field (wid). */
82 charstring_push_back (rev, '?');
83 } else if (num < 0 && wid > 0) {
84 /* Shouldn't need the wid > 0 check, that's why the condition
85 at the top checks wid < max-1 when num < 0. */
86 --wid;
87 if (fill == ' ') {
88 charstring_push_back (rev, '-');
89 }
90 }
91 while (wid-- > 0 && fill != 0) {
92 charstring_push_back (rev, fill);
93 }
94 if (num < 0 && fill == '0') {
95 charstring_push_back (rev, '-');
96 }
97
98 {
99 /* Output the string in reverse. */
100 size_t b = charstring_bytes (rev);
101 const char *cp = b ? &charstring_buffer (rev)[b] : NULL;
102
103 for (; b > 0; --b) {
104 charstring_push_back (dest, *--cp);
105 }
106 }
107
108 charstring_free (rev);
109 }
110 }
111
112 /*
113 * copy string from str to dest padding with the fill character to a
114 * size of wid characters. if wid is negative, the string is right
115 * aligned no more than max characters are copied
116 */
117 void
118 cptrimmed(charstring_t dest, char *str, int wid, char fill, size_t max) {
119 int remaining; /* remaining output width available */
120 int rjust;
121 struct charstring *trimmed;
122 size_t end; /* number of input bytes remaining in str */
123 #ifdef MULTIBYTE_SUPPORT
124 int char_len; /* bytes in current character */
125 int w;
126 wchar_t wide_char;
127 char *altstr = NULL;
128 #endif
129 char *sp; /* current position in source string */
130 int prevCtrl = 1;
131
132 /* get alignment */
133 rjust = 0;
134 if ((remaining = wid) < 0) {
135 remaining = -remaining;
136 rjust++;
137 }
138 if (remaining > (int) max) { remaining = max; }
139
140 trimmed = rjust ? charstring_create(remaining) : dest;
141
142 if ((sp = str)) {
143 #ifdef MULTIBYTE_SUPPORT
144 if (mbtowc(NULL, NULL, 0)) {} /* reset shift state */
145 #endif
146 end = strlen(str);
147 while (*sp && remaining > 0 && end > 0) {
148 #ifdef MULTIBYTE_SUPPORT
149 char_len = mbtowc(&wide_char, sp, end);
150
151 /*
152 * See the relevant comments in cpstripped() to explain what's
153 * going on here; we want to handle the case where we get
154 * characters that mbtowc() cannot handle
155 */
156
157 if (char_len < 0) {
158 altstr = "?";
159 char_len = mbtowc(&wide_char, altstr, 1);
160 }
161
162 if (char_len <= 0) {
163 break;
164 }
165
166 w = wcwidth(wide_char);
167
168 /* If w > remaining, w must be positive. */
169 if (w > remaining) {
170 break;
171 }
172
173 end -= char_len;
174
175 if (iswcntrl(wide_char) || iswspace(wide_char)) {
176 sp += char_len;
177 #else
178 int c;
179 end--;
180 /* isnctrl(), etc., take an int argument. Cygwin's ctype.h
181 intentionally warns if they are passed a char. */
182 c = (unsigned char) *sp;
183 if (iscntrl(c) || isspace(c)) {
184 sp++;
185 #endif
186 if (!prevCtrl) {
187 charstring_push_back (trimmed, ' ');
188 remaining--;
189 }
190
191 prevCtrl = 1;
192 continue;
193 }
194 prevCtrl = 0;
195
196 #ifdef MULTIBYTE_SUPPORT
197 if (w >= 0 && remaining >= w) {
198 charstring_push_back_chars (trimmed, altstr ? altstr : sp,
199 char_len, w);
200 remaining -= w;
201 altstr = NULL;
202 }
203 sp += char_len;
204 #else
205 charstring_push_back (trimmed, *sp++);
206 remaining--;
207 #endif
208 }
209 }
210
211 while (remaining-- > 0) {
212 charstring_push_back(dest, fill);
213 }
214
215 if (rjust) {
216 charstring_append(dest, trimmed);
217 charstring_free(trimmed);
218 }
219 }
220
221 #ifdef MULTIBYTE_SUPPORT
222 static void
223 cpstripped (charstring_t dest, size_t max, char *str)
224 {
225 int prevCtrl = 1; /* This is 1 so we strip out leading spaces */
226 int len;
227 int char_len, w;
228 wchar_t wide_char;
229 char *altstr = NULL;
230
231 if (!str) {
232 return;
233 }
234
235 len = strlen(str);
236
237 if (mbtowc(NULL, NULL, 0)) {} /* Reset shift state */
238
239 /*
240 * Process each character at a time; if we have multibyte support
241 * then deal with that here.
242 */
243
244 while (*str != '\0' && len > 0 && max > 0) {
245 char_len = mbtowc(&wide_char, str, len);
246
247 /*
248 * If mbrtowc() failed, then we have a character that isn't valid
249 * in the current encoding, or len wasn't enough for the whole
250 * multi-byte rune to be read. Replace it with a '?'. We do that by
251 * setting the alstr variable to the value of the replacement string;
252 * altstr is used below when the bytes are copied into the output
253 * buffer.
254 */
255 if (char_len < 0) {
256 altstr = "?";
257 char_len = mbtowc(&wide_char, altstr, 1);
258 }
259
260 if (char_len <= 0) {
261 break;
262 }
263
264 len -= char_len;
265
266 if (iswcntrl(wide_char) || iswspace(wide_char)) {
267 str += char_len;
268 if (! prevCtrl) {
269 charstring_push_back (dest, ' ');
270 --max;
271 }
272
273 prevCtrl = 1;
274 continue;
275 }
276
277 prevCtrl = 0;
278
279 w = wcwidth(wide_char);
280 assert(w >= 0);
281 if (max >= (size_t) w) {
282 charstring_push_back_chars (dest, altstr ? altstr : str, char_len, w);
283 max -= w;
284 str += char_len;
285 altstr = NULL;
286 } else {
287 /* Not enough width available for the last character. Output
288 space(s) to fill. */
289 while (max-- > 0) {
290 charstring_push_back (dest, ' ');
291 }
292 break;
293 }
294 }
295 }
296 #endif
297
298 #ifndef MULTIBYTE_SUPPORT
299 static void
300 cpstripped (charstring_t dest, size_t max, char *str)
301 {
302 int prevCtrl = 1; /* This is 1 so we strip out leading spaces */
303 int len;
304
305 if (!str) {
306 return;
307 }
308
309 len = strlen(str);
310
311 /*
312 * Process each character at a time; if we have multibyte support
313 * then deal with that here.
314 */
315
316 while (*str != '\0' && len > 0 && max > 0) {
317 int c = (unsigned char) *str;
318 len--;
319 if (iscntrl(c) || isspace(c)) {
320 str++;
321 if (! prevCtrl) {
322 charstring_push_back (dest, ' ');
323 --max;
324 }
325
326 prevCtrl = 1;
327 continue;
328 }
329
330 prevCtrl = 0;
331
332 charstring_push_back (dest, *str++);
333 --max;
334 }
335 }
336 #endif
337
338 static char *lmonth[] = { "January", "February","March", "April",
339 "May", "June", "July", "August",
340 "September","October", "November","December" };
341
342 static char *
343 get_x400_friendly (char *mbox, char *buffer, int buffer_len)
344 {
345 char given[BUFSIZ], surname[BUFSIZ];
346
347 if (mbox == NULL)
348 return NULL;
349 if (*mbox == '"')
350 mbox++;
351 if (*mbox != '/')
352 return NULL;
353
354 if (get_x400_comp (mbox, "/PN=", buffer, buffer_len)) {
355 for (mbox = buffer; (mbox = strchr(mbox, '.')); )
356 *mbox++ = ' ';
357
358 return buffer;
359 }
360
361 if (!get_x400_comp (mbox, "/S=", surname, sizeof(surname)))
362 return NULL;
363
364 if (get_x400_comp (mbox, "/G=", given, sizeof(given)))
365 snprintf (buffer, buffer_len, "%s %s", given, surname);
366 else
367 snprintf (buffer, buffer_len, "%s", surname);
368
369 return buffer;
370 }
371
372 static int
373 get_x400_comp (char *mbox, char *key, char *buffer, int buffer_len)
374 {
375 int idx;
376 char *cp;
377
378 if ((idx = stringdex (key, mbox)) < 0
379 || !(cp = strchr(mbox += idx + strlen (key), '/')))
380 return 0;
381
382 snprintf (buffer, buffer_len, "%*.*s", (int)(cp - mbox), (int)(cp - mbox), mbox);
383 return 1;
384 }
385
386 struct format *
387 fmt_scan (struct format *format, charstring_t scanlp, int width, int *dat,
388 struct fmt_callbacks *callbacks)
389 {
390 char *sp;
391 char *savestr, *str;
392 char buffer[NMH_BUFSIZ], buffer2[NMH_BUFSIZ];
393 int i, c, rjust;
394 int value;
395 time_t t;
396 size_t max;
397 struct format *fmt;
398 struct comp *comp;
399 struct tws *tws;
400 struct mailname *mn;
401
402 /*
403 * max is the same as width, but unsigned so comparisons
404 * with charstring_chars() won't raise compile warnings.
405 */
406 max = width;
407 savestr = str = NULL;
408 value = 0;
409
410 for (fmt = format; fmt->f_type != FT_DONE; fmt++)
411 switch (fmt->f_type) {
412 case FT_PARSEADDR:
413 case FT_PARSEDATE:
414 fmt->f_comp->c_flags &= ~CF_PARSED;
415 break;
416 case FT_COMP:
417 case FT_COMPF:
418 case FT_LS_COMP:
419 case FT_LS_DECODECOMP:
420 /*
421 * Trim these components of any newlines.
422 *
423 * But don't trim the "body" and "text" components.
424 */
425
426 comp = fmt->f_comp;
427
428 if (! (comp->c_flags & CF_TRIMMED) && comp->c_text &&
429 (i = strlen(comp->c_text)) > 0) {
430 if (comp->c_text[i - 1] == '\n' &&
431 strcmp(comp->c_name, "body") != 0 &&
432 strcmp(comp->c_name, "text") != 0)
433 comp->c_text[i - 1] = '\0';
434 comp->c_flags |= CF_TRIMMED;
435 }
436 break;
437 }
438
439 fmt = format;
440
441 for ( ; charstring_chars (scanlp) < max; ) {
442 switch (fmt->f_type) {
443
444 case FT_COMP:
445 cpstripped (scanlp, max - charstring_chars (scanlp),
446 fmt->f_comp->c_text);
447 break;
448 case FT_COMPF:
449 cptrimmed (scanlp, fmt->f_comp->c_text, fmt->f_width,
450 fmt->f_fill, max - charstring_chars (scanlp));
451 break;
452
453 case FT_LIT:
454 sp = fmt->f_text;
455 while ((c = *sp++) && charstring_chars (scanlp) < max) {
456 charstring_push_back (scanlp, c);
457 }
458 break;
459 case FT_LITF:
460 sp = fmt->f_text;
461 rjust = 0;
462 i = fmt->f_width;
463 if (i < 0) {
464 i = -i;
465 rjust++; /* XXX should do something with this */
466 }
467 while ((c = *sp++) && --i >= 0 && charstring_chars (scanlp) < max) {
468 charstring_push_back (scanlp, c);
469 }
470 while (--i >= 0 && charstring_chars (scanlp) < max) {
471 charstring_push_back (scanlp, fmt->f_fill);
472 }
473 break;
474
475 case FT_STR:
476 cpstripped (scanlp, max - charstring_chars (scanlp), str);
477 break;
478 case FT_STRF:
479 cptrimmed (scanlp, str, fmt->f_width, fmt->f_fill,
480 max - charstring_chars (scanlp));
481 break;
482 case FT_STRLIT:
483 if (str) {
484 sp = str;
485 while ((c = *sp++) && charstring_chars (scanlp) < max) {
486 charstring_push_back (scanlp, c);
487 }
488 }
489 break;
490 case FT_STRLITZ:
491 if (str) charstring_push_back_chars (scanlp, str, strlen (str), 0);
492 break;
493 case FT_STRFW:
494 adios (NULL, "internal error (FT_STRFW)");
495
496 case FT_NUM: {
497 int num = value;
498 unsigned int wid;
499
500 for (wid = num <= 0; num; ++wid, num /= 10) {}
501 cpnumber (scanlp, value, wid, ' ',
502 max - charstring_chars (scanlp));
503 break;
504 }
505 case FT_LS_KILO:
506 case FT_LS_KIBI:
507 {
508 char *unitcp;
509 unsigned int whole, tenths;
510 unsigned int scale = 0;
511 unsigned int val = (unsigned int)value;
512 char *kibisuff = NULL;
513
514 switch (fmt->f_type) {
515 case FT_LS_KILO: scale = 1000; kibisuff = ""; break;
516 case FT_LS_KIBI: scale = 1024; kibisuff = "i"; break;
517 }
518
519 if (val < scale) {
520 snprintf(buffer, sizeof(buffer), "%u", val);
521 } else {
522 /* To prevent divide by 0, found by clang static
523 analyzer. */
524 if (scale == 0) { scale = 1; }
525
526 /* find correct scale for size (Kilo/Mega/Giga/Tera) */
527 for (unitcp = "KMGT"; val > (scale * scale); val /= scale) {
528 if (!*++unitcp)
529 break;
530 }
531
532 if (!*unitcp) {
533 strcpy(buffer, "huge");
534 } else {
535 /* val is scale times too big. we want tenths */
536 val *= 10;
537
538 /* round up */
539 val += (scale - 1);
540 val /= scale;
541
542 whole = val / 10;
543 tenths = val - (whole * 10);
544
545 if (tenths) {
546 snprintf(buffer, sizeof(buffer), "%u.%u%c%s",
547 whole, tenths, *unitcp, kibisuff);
548 } else {
549 snprintf(buffer, sizeof(buffer), "%u%c%s",
550 whole, *unitcp, kibisuff);
551 }
552 }
553 }
554 str = buffer;
555 }
556 break;
557 case FT_NUMF:
558 cpnumber (scanlp, value, fmt->f_width, fmt->f_fill,
559 max - charstring_chars (scanlp));
560 break;
561
562 case FT_CHAR:
563 charstring_push_back (scanlp, fmt->f_char);
564 break;
565
566 case FT_DONE:
567 if (callbacks && callbacks->trace_func)
568 callbacks->trace_func(callbacks->trace_context, fmt, value,
569 str, charstring_buffer (scanlp));
570 goto finished;
571
572 case FT_IF_S:
573 if (!(value = (str && *str))) {
574 if (callbacks && callbacks->trace_func)
575 callbacks->trace_func(callbacks->trace_context, fmt, value,
576 str, charstring_buffer (scanlp));
577 fmt += fmt->f_skip;
578 continue;
579 }
580 break;
581
582 case FT_IF_S_NULL:
583 if (!(value = (str == NULL || *str == 0))) {
584 if (callbacks && callbacks->trace_func)
585 callbacks->trace_func(callbacks->trace_context, fmt, value,
586 str, charstring_buffer (scanlp));
587 fmt += fmt->f_skip;
588 continue;
589 }
590 break;
591
592 case FT_IF_V_EQ:
593 if (value != fmt->f_value) {
594 if (callbacks && callbacks->trace_func)
595 callbacks->trace_func(callbacks->trace_context, fmt, value,
596 str, charstring_buffer (scanlp));
597 fmt += fmt->f_skip;
598 continue;
599 }
600 break;
601
602 case FT_IF_V_NE:
603 if (value == fmt->f_value) {
604 if (callbacks && callbacks->trace_func)
605 callbacks->trace_func(callbacks->trace_context, fmt, value,
606 str, charstring_buffer (scanlp));
607 fmt += fmt->f_skip;
608 continue;
609 }
610 break;
611
612 case FT_IF_V_GT:
613 if (value <= fmt->f_value) {
614 if (callbacks && callbacks->trace_func)
615 callbacks->trace_func(callbacks->trace_context, fmt, value,
616 str, charstring_buffer (scanlp));
617 fmt += fmt->f_skip;
618 continue;
619 }
620 break;
621
622 case FT_IF_MATCH:
623 if (!(value = (str && match (str, fmt->f_text)))) {
624 if (callbacks && callbacks->trace_func)
625 callbacks->trace_func(callbacks->trace_context, fmt, value,
626 str, charstring_buffer (scanlp));
627 fmt += fmt->f_skip;
628 continue;
629 }
630 break;
631
632 case FT_V_MATCH:
633 if (str)
634 value = match (str, fmt->f_text);
635 else
636 value = 0;
637 break;
638
639 case FT_IF_AMATCH:
640 if (!(value = (str && uprf (str, fmt->f_text)))) {
641 if (callbacks && callbacks->trace_func)
642 callbacks->trace_func(callbacks->trace_context, fmt, value,
643 str, charstring_buffer (scanlp));
644 fmt += fmt->f_skip;
645 continue;
646 }
647 break;
648
649 case FT_V_AMATCH:
650 value = uprf (str, fmt->f_text);
651 break;
652
653 case FT_S_NONNULL:
654 value = (str != NULL && *str != 0);
655 break;
656
657 case FT_S_NULL:
658 value = (str == NULL || *str == 0);
659 break;
660
661 case FT_V_EQ:
662 value = (fmt->f_value == value);
663 break;
664
665 case FT_V_NE:
666 value = (fmt->f_value != value);
667 break;
668
669 case FT_V_GT:
670 value = (fmt->f_value > value);
671 break;
672
673 case FT_GOTO:
674 if (callbacks && callbacks->trace_func)
675 callbacks->trace_func(callbacks->trace_context, fmt, value,
676 str, charstring_buffer (scanlp));
677 fmt += fmt->f_skip;
678 continue;
679
680 case FT_NOP:
681 break;
682
683 case FT_LS_COMP:
684 str = fmt->f_comp->c_text;
685 break;
686 case FT_LS_LIT:
687 str = fmt->f_text;
688 break;
689 case FT_LS_GETENV:
690 if (!(str = getenv (fmt->f_text)))
691 str = "";
692 break;
693 case FT_LS_CFIND:
694 if (!(str = context_find (fmt->f_text)))
695 str = "";
696 break;
697
698 case FT_LS_DECODECOMP:
699 if (decode_rfc2047(fmt->f_comp->c_text, buffer2, sizeof(buffer2)))
700 str = buffer2;
701 else
702 str = fmt->f_comp->c_text;
703 break;
704
705 case FT_LS_DECODE:
706 if (str && decode_rfc2047(str, buffer2, sizeof(buffer2)))
707 str = buffer2;
708 break;
709
710 case FT_LS_TRIM:
711 if (str) {
712 char *xp;
713
714 if (str != buffer)
715 strncpy(buffer, str, sizeof(buffer));
716 buffer[sizeof(buffer)-1] = '\0';
717 str = buffer;
718 while (isspace((unsigned char) *str))
719 str++;
720 rjust = 0;
721 if ((i = fmt->f_width) < 0) {
722 i = -i;
723 rjust++;
724 }
725
726 if (!rjust && i > 0 && (int) strlen(str) > i)
727 str[i] = '\0';
728 xp = str;
729 xp += strlen(str) - 1;
730 while (xp > str && isspace((unsigned char) *xp))
731 *xp-- = '\0';
732 if (rjust && i > 0 && (int) strlen(str) > i)
733 str += strlen(str) - i;
734 }
735 break;
736
737 case FT_LV_COMPFLAG:
738 value = (fmt->f_comp->c_flags & CF_TRUE) != 0;
739 break;
740 case FT_LV_COMP:
741 value = (comp = fmt->f_comp)->c_text ? atoi(comp->c_text) : 0;
742 break;
743 case FT_LV_LIT:
744 value = fmt->f_value;
745 break;
746 case FT_LV_DAT:
747 value = dat[fmt->f_value];
748 break;
749 case FT_LV_STRLEN:
750 if (str != NULL)
751 value = strlen(str);
752 else
753 value = 0;
754 break;
755 case FT_LV_CHAR_LEFT:
756 value = max - charstring_bytes (scanlp);
757 break;
758 case FT_LV_PLUS_L:
759 value += fmt->f_value;
760 break;
761 case FT_LV_MINUS_L:
762 value = fmt->f_value - value;
763 break;
764 case FT_LV_MULTIPLY_L:
765 value *= fmt->f_value;
766 break;
767 case FT_LV_DIVIDE_L:
768 if (fmt->f_value)
769 value /= fmt->f_value;
770 else
771 value = 0;
772 break;
773 case FT_LV_MODULO_L:
774 if (fmt->f_value)
775 value %= fmt->f_value;
776 else
777 value = 0;
778 break;
779 case FT_SAVESTR:
780 savestr = str;
781 break;
782
783 case FT_LV_SEC:
784 value = fmt->f_comp->c_tws->tw_sec;
785 break;
786 case FT_LV_MIN:
787 value = fmt->f_comp->c_tws->tw_min;
788 break;
789 case FT_LV_HOUR:
790 value = fmt->f_comp->c_tws->tw_hour;
791 break;
792 case FT_LV_MDAY:
793 value = fmt->f_comp->c_tws->tw_mday;
794 break;
795 case FT_LV_MON:
796 value = fmt->f_comp->c_tws->tw_mon + 1;
797 break;
798 case FT_LS_MONTH:
799 str = tw_moty[fmt->f_comp->c_tws->tw_mon];
800 break;
801 case FT_LS_LMONTH:
802 str = lmonth[fmt->f_comp->c_tws->tw_mon];
803 break;
804 case FT_LS_ZONE:
805 str = dtwszone (fmt->f_comp->c_tws);
806 break;
807 case FT_LV_YEAR:
808 value = fmt->f_comp->c_tws->tw_year;
809 break;
810 case FT_LV_WDAY:
811 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
812 set_dotw (tws);
813 value = tws->tw_wday;
814 break;
815 case FT_LS_DAY:
816 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
817 set_dotw (tws);
818 str = tw_dotw[tws->tw_wday];
819 break;
820 case FT_LS_WEEKDAY:
821 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
822 set_dotw (tws);
823 str = tw_ldotw[tws->tw_wday];
824 break;
825 case FT_LV_YDAY:
826 value = fmt->f_comp->c_tws->tw_yday;
827 break;
828 case FT_LV_ZONE:
829 value = fmt->f_comp->c_tws->tw_zone;
830 break;
831 case FT_LV_CLOCK:
832 if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
833 value = dmktime(fmt->f_comp->c_tws);
834 break;
835 case FT_LV_RCLOCK:
836 if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
837 value = dmktime(fmt->f_comp->c_tws);
838 value = time((time_t *) 0) - value;
839 break;
840 case FT_LV_DAYF:
841 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
842 set_dotw (tws);
843 switch (fmt->f_comp->c_tws->tw_flags & TW_SDAY) {
844 case TW_SEXP:
845 value = 1; break;
846 case TW_SIMP:
847 value = 0; break;
848 default:
849 value = -1; break;
850 }
851 break;
852 case FT_LV_ZONEF:
853 if (fmt->f_comp->c_tws->tw_flags & TW_SZEXP)
854 value = 1;
855 else
856 value = -1;
857 break;
858 case FT_LV_DST:
859 value = fmt->f_comp->c_tws->tw_flags & TW_DST ? 1 : 0;
860 break;
861 case FT_LS_822DATE:
862 str = dasctime (fmt->f_comp->c_tws , TW_ZONE);
863 break;
864 case FT_LS_PRETTY:
865 str = dasctime (fmt->f_comp->c_tws, TW_NULL);
866 break;
867
868 case FT_LS_PERS:
869 str = fmt->f_comp->c_mn->m_pers;
870 break;
871 case FT_LS_MBOX:
872 str = fmt->f_comp->c_mn->m_mbox;
873 break;
874 case FT_LS_HOST:
875 str = fmt->f_comp->c_mn->m_host;
876 break;
877 case FT_LS_PATH:
878 str = fmt->f_comp->c_mn->m_path;
879 break;
880 case FT_LS_GNAME:
881 str = fmt->f_comp->c_mn->m_gname;
882 break;
883 case FT_LS_NOTE:
884 str = fmt->f_comp->c_mn->m_note;
885 break;
886 case FT_LS_822ADDR:
887 str = adrformat( fmt->f_comp->c_mn );
888 break;
889 case FT_LV_HOSTTYPE:
890 value = fmt->f_comp->c_mn->m_type;
891 break;
892 case FT_LV_INGRPF:
893 value = fmt->f_comp->c_mn->m_ingrp;
894 break;
895 case FT_LV_NOHOSTF:
896 value = fmt->f_comp->c_mn->m_nohost;
897 break;
898 case FT_LS_ADDR:
899 case FT_LS_FRIENDLY:
900 if ((mn = fmt->f_comp->c_mn) == &fmt_mnull) {
901 str = fmt->f_comp->c_text;
902 break;
903 }
904 if (fmt->f_type == FT_LS_ADDR)
905 goto unfriendly;
906 if ((str = mn->m_pers) == NULL) {
907 if ((str = mn->m_note)) {
908 if (str != buffer)
909 strncpy (buffer, str, sizeof(buffer));
910 buffer[sizeof(buffer)-1] = '\0';
911 str = buffer;
912 if (*str == '(')
913 str++;
914 sp = str + strlen(str) - 1;
915 if (*sp == ')') {
916 *sp-- = '\0';
917 while (sp >= str)
918 if (*sp == ' ')
919 *sp-- = '\0';
920 else
921 break;
922 }
923 } else if (!(str = get_x400_friendly (mn->m_mbox,
924 buffer, sizeof(buffer)))) {
925 unfriendly:
926 switch (mn->m_type) {
927 case LOCALHOST:
928 str = mn->m_mbox;
929 break;
930 case UUCPHOST:
931 snprintf (buffer, sizeof(buffer), "%s!%s",
932 mn->m_host, mn->m_mbox);
933 str = buffer;
934 break;
935 default:
936 if (mn->m_mbox) {
937 snprintf (buffer, sizeof(buffer), "%s@%s",
938 mn->m_mbox, mn->m_host);
939 str= buffer;
940 }
941 else
942 str = mn->m_text;
943 break;
944 }
945 }
946 }
947 break;
948
949
950 /* UNQUOTEs RFC-2822 quoted-string and quoted-pair */
951 case FT_LS_UNQUOTE:
952 if (str) {
953 if (str != buffer)
954 strncpy(buffer, str, sizeof(buffer));
955 /* strncpy doesn't NUL-terminate if it fills the buffer */
956 buffer[sizeof(buffer)-1] = '\0';
957 unquote_string(buffer, buffer2);
958 str = buffer2;
959 }
960 break;
961
962 case FT_LOCALDATE:
963 comp = fmt->f_comp;
964 if ((t = comp->c_tws->tw_clock) == 0)
965 t = dmktime(comp->c_tws);
966 tws = dlocaltime(&t);
967 *comp->c_tws = *tws;
968 break;
969
970 case FT_GMTDATE:
971 comp = fmt->f_comp;
972 if ((t = comp->c_tws->tw_clock) == 0)
973 t = dmktime(comp->c_tws);
974 tws = dgmtime(&t);
975 *comp->c_tws = *tws;
976 break;
977
978 case FT_PARSEDATE:
979 comp = fmt->f_comp;
980 if (comp->c_flags & CF_PARSED)
981 break;
982 if ((sp = comp->c_text) && (tws = dparsetime(sp))) {
983 *comp->c_tws = *tws;
984 comp->c_flags &= ~CF_TRUE;
985 } else if ((comp->c_flags & CF_DATEFAB) == 0) {
986 memset (comp->c_tws, 0, sizeof *comp->c_tws);
987 comp->c_flags = CF_TRUE;
988 }
989 comp->c_flags |= CF_PARSED;
990 break;
991
992 case FT_FORMATADDR:
993 /* hook for custom address list formatting (see replsbr.c) */
994 if (callbacks && callbacks->formataddr)
995 str = callbacks->formataddr (savestr, str);
996 else
997 str = formataddr (savestr, str);
998 break;
999
1000 case FT_CONCATADDR:
1001 /* The same as formataddr, but doesn't do duplicate suppression */
1002 if (callbacks && callbacks->concataddr)
1003 str = callbacks->concataddr (savestr, str);
1004 else
1005 str = concataddr (savestr, str);
1006 break;
1007
1008 case FT_PUTADDR:
1009 /* output the str register as an address component,
1010 * splitting it into multiple lines if necessary. The
1011 * value reg. contains the max line length. The lit.
1012 * field may contain a string to prepend to the result
1013 * (e.g., "To: ")
1014 */
1015 {
1016 char *lp, *lastb;
1017 int indent, wid, len;
1018
1019 lp = str;
1020 wid = value;
1021 len = str ? strlen (str) : 0;
1022 sp = fmt->f_text;
1023 indent = strlen (sp);
1024 wid -= indent;
1025 if (wid <= 0) {
1026 adios(NULL, "putaddr -- num register (%d) must be greater "
1027 "than label width (%d)", value, indent);
1028 }
1029 while ((c = *sp++) && charstring_chars (scanlp) < max) {
1030 charstring_push_back (scanlp, c);
1031 }
1032 while (len > wid) {
1033 /* try to break at a comma; failing that, break at a
1034 * space.
1035 */
1036 lastb = 0; sp = lp + wid;
1037 while (sp > lp && (c = (unsigned char) *--sp) != ',') {
1038 if (! lastb && isspace(c))
1039 lastb = sp - 1;
1040 }
1041 if (sp == lp) {
1042 if (! (sp = lastb)) {
1043 sp = lp + wid - 1;
1044 while (*sp && *sp != ',' &&
1045 !isspace((unsigned char) *sp))
1046 sp++;
1047 if (*sp != ',')
1048 sp--;
1049 }
1050 }
1051 len -= sp - lp + 1;
1052 while (lp <= sp && charstring_chars (scanlp) < max) {
1053 charstring_push_back (scanlp, *lp++);
1054 }
1055 while (isspace((unsigned char) *lp))
1056 lp++, len--;
1057 if (*lp) {
1058 if (charstring_chars (scanlp) < max) {
1059 charstring_push_back (scanlp, '\n');
1060 }
1061 for (i=indent;
1062 charstring_chars (scanlp) < max && i > 0;
1063 i--)
1064 charstring_push_back (scanlp, ' ');
1065 }
1066 }
1067 cpstripped (scanlp, max - charstring_chars (scanlp), lp);
1068 }
1069 break;
1070
1071 case FT_PARSEADDR:
1072 comp = fmt->f_comp;
1073 if (comp->c_flags & CF_PARSED)
1074 break;
1075 if (comp->c_mn != &fmt_mnull)
1076 mnfree (comp->c_mn);
1077 if ((sp = comp->c_text) && (sp = getname(sp)) &&
1078 (mn = getm (sp, NULL, 0, NULL, 0))) {
1079 comp->c_mn = mn;
1080 while (getname(""))
1081 ;
1082 comp->c_flags |= CF_PARSED;
1083 } else {
1084 while (getname("")) /* XXX */
1085 ;
1086 comp->c_mn = &fmt_mnull;
1087 }
1088 break;
1089
1090 case FT_MYMBOX:
1091 case FT_GETMYMBOX:
1092 case FT_GETMYADDR:
1093 /*
1094 * if there's no component, we say true. Otherwise we
1095 * say "true" only if we can parse the address and it
1096 * matches one of our addresses.
1097 */
1098 comp = fmt->f_comp;
1099 if (comp->c_mn != &fmt_mnull)
1100 mnfree (comp->c_mn);
1101 if ((sp = comp->c_text) && (sp = getname(sp)) &&
1102 (mn = getm (sp, NULL, 0, NULL, 0))) {
1103 comp->c_mn = mn;
1104 if (ismymbox(mn)) {
1105 comp->c_flags |= CF_TRUE;
1106 /* Set str for use with FT_GETMYMBOX. With
1107 FT_GETMYADDR, comp->c_mn will be run through
1108 FT_LS_ADDR, which will strip off any pers
1109 name. */
1110 str = mn->m_text;
1111 } else {
1112 comp->c_flags &= ~CF_TRUE;
1113 }
1114 while ((sp = getname(sp)))
1115 if ((comp->c_flags & CF_TRUE) == 0 &&
1116 (mn = getm (sp, NULL, 0, NULL, 0)))
1117 if (ismymbox(mn)) {
1118 comp->c_flags |= CF_TRUE;
1119 /* Set str and comp->c_text for use with
1120 FT_GETMYMBOX. With FT_GETMYADDR,
1121 comp->c_mn will be run through
1122 FT_LS_ADDR, which will strip off any
1123 pers name. */
1124 free (comp->c_text);
1125 comp->c_text = str = strdup (mn->m_text);
1126 comp->c_mn = mn;
1127 }
1128 comp->c_flags |= CF_PARSED;
1129 } else {
1130 while (getname("")) /* XXX */
1131 ;
1132 if (comp->c_text == 0)
1133 comp->c_flags |= CF_TRUE;
1134 else {
1135 comp->c_flags &= ~CF_TRUE;
1136 }
1137 comp->c_mn = &fmt_mnull;
1138 }
1139 if ((comp->c_flags & CF_TRUE) == 0 &&
1140 (fmt->f_type == FT_GETMYMBOX || fmt->f_type == FT_GETMYADDR)) {
1141 /* Fool FT_LS_ADDR into not producing an address. */
1142 comp->c_mn = &fmt_mnull; comp->c_text = NULL;
1143 }
1144 break;
1145 }
1146
1147 /*
1148 * Call our tracing callback function, if one was supplied
1149 */
1150
1151 if (callbacks && callbacks->trace_func)
1152 callbacks->trace_func(callbacks->trace_context, fmt, value,
1153 str, charstring_buffer (scanlp));
1154 fmt++;
1155 }
1156
1157 /* Emit any trailing sequences of zero display length. */
1158 while (fmt->f_type != FT_DONE) {
1159 if (fmt->f_type == FT_LS_LIT) {
1160 str = fmt->f_text;
1161 if (callbacks && callbacks->trace_func)
1162 callbacks->trace_func(callbacks->trace_context, fmt, value,
1163 str, charstring_buffer (scanlp));
1164 } else if (fmt->f_type == FT_STRLITZ) {
1165 /* Don't want to emit part of an escape sequence. So if
1166 there isn't enough room in the buffer for the entire
1167 string, skip it completely. Need room for null
1168 terminator, and maybe trailing newline (added below). */
1169 if (str) {
1170 for (sp = str; *sp; ++sp) {
1171 charstring_push_back (scanlp, *sp);
1172 }
1173 }
1174 if (callbacks && callbacks->trace_func)
1175 callbacks->trace_func(callbacks->trace_context, fmt, value,
1176 str, charstring_buffer (scanlp));
1177 }
1178 fmt++;
1179 }
1180
1181 finished:
1182 if (charstring_bytes (scanlp) > 0) {
1183 /*
1184 * Append a newline if the last character wasn't.
1185 */
1186 #ifdef MULTIBYTE_SUPPORT
1187 /*
1188 * It's a little tricky because the last byte might be part of
1189 * a multibyte character, in which case we assume that wasn't
1190 * a newline.
1191 */
1192 size_t last_char_len = charstring_last_char_len (scanlp);
1193 #else /* ! MULTIBYTE_SUPPORT */
1194 size_t last_char_len = 1;
1195 #endif /* ! MULTIBYTE_SUPPORT */
1196
1197 if (last_char_len > 1 ||
1198 charstring_buffer (scanlp)[charstring_bytes (scanlp) - 1] != '\n') {
1199 charstring_push_back (scanlp, '\n');
1200 }
1201 }
1202
1203 return (NULL);
1204 }