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