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