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