]>
diplodocus.org Git - nmh/blob - sbr/fmt_scan.c
1 /* fmt_scan.c -- format string interpretation
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.
7 * This is the engine that processes the format instructions created by
8 * fmt_compile (found in fmt_compile.c).
12 #include <h/addrsbr.h>
13 #include <h/fmt_scan.h>
15 #include <h/fmt_compile.h>
19 #ifdef HAVE_SYS_TIME_H
20 # include <sys/time.h>
23 #ifdef MULTIBYTE_SUPPORT
28 struct mailname fmt_mnull
= { NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, 0, 0, 0, 0,
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);
40 * test if string "sub" appears anywhere in
41 * string "str" (case insensitive).
45 match (char *str
, char *sub
)
51 c1
= tolower((unsigned char)c1
);
52 while ((c2
= *str
++) && c1
!= tolower((unsigned char)c2
))
56 s1
= sub
+ 1; s2
= str
;
57 while ((c1
= *s1
++) &&
58 tolower((unsigned char)c1
) == tolower((unsigned char)(c2
= *s2
++)))
67 * copy a number to the destination subject to a maximum width
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
;
77 charstring_push_back (rev
, i
% 10 + '0');
79 } while (--wid
> 0 && 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. */
88 charstring_push_back (rev
, '-');
91 while (wid
-- > 0 && fill
!= 0) {
92 charstring_push_back (rev
, fill
);
94 if (num
< 0 && fill
== '0') {
95 charstring_push_back (rev
, '-');
99 /* Output the string in reverse. */
100 size_t b
= charstring_bytes (rev
);
101 const char *cp
= b
? &charstring_buffer (rev
)[b
] : NULL
;
104 charstring_push_back (dest
, *--cp
);
108 charstring_free (rev
);
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
118 cptrimmed(charstring_t dest
, char *str
, int wid
, char fill
, size_t max
) {
119 int remaining
; /* remaining output width available */
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 */
129 char *sp
; /* current position in source string */
134 if ((remaining
= wid
) < 0) {
135 remaining
= -remaining
;
138 if (remaining
> (int) max
) { remaining
= max
; }
140 trimmed
= rjust
? charstring_create(remaining
) : dest
;
143 #ifdef MULTIBYTE_SUPPORT
144 if (mbtowc(NULL
, NULL
, 0)) {} /* reset shift state */
147 while (*sp
&& remaining
> 0 && end
> 0) {
148 #ifdef MULTIBYTE_SUPPORT
149 char_len
= mbtowc(&wide_char
, sp
, end
);
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
159 char_len
= mbtowc(&wide_char
, altstr
, 1);
166 w
= wcwidth(wide_char
);
168 /* If w > remaining, w must be positive. */
175 if (iswcntrl(wide_char
) || iswspace(wide_char
)) {
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
)) {
187 charstring_push_back (trimmed
, ' ');
196 #ifdef MULTIBYTE_SUPPORT
197 if (w
>= 0 && remaining
>= w
) {
198 charstring_push_back_chars (trimmed
, altstr
? altstr
: sp
,
205 charstring_push_back (trimmed
, *sp
++);
211 while (remaining
-- > 0) {
212 charstring_push_back(dest
, fill
);
216 charstring_append(dest
, trimmed
);
217 charstring_free(trimmed
);
221 #ifdef MULTIBYTE_SUPPORT
223 cpstripped (charstring_t dest
, size_t max
, char *str
)
226 static char *oddchar
;
227 static size_t oddlen
;
228 static char *spacechar
;
229 static size_t spacelen
;
242 two
= MB_CUR_MAX
* 2; /* Varies at run-time. */
244 oddchar
= mh_xmalloc(two
);
245 oddlen
= wcstombs(oddchar
, L
"?", two
);
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);
255 return; /* It's unclear why no padding in this case. */
256 end
= str
+ strlen(str
);
258 if (mbtowc(NULL
, NULL
, 0))
259 {} /* Reset shift state. */
261 squash
= true; /* Trim `space' or `cntrl' from the start. */
264 return; /* It's unclear why no padding in this case. */
266 srclen
= mbtowc(&rune
, str
, end
- str
);
268 /* Invalid rune, or not enough bytes to finish it. */
272 str
++; /* Skip one byte. */
278 if (iswspace(rune
) || iswcntrl(rune
)) {
280 continue; /* Amidst a run of these. */
297 if ((size_t)w
> max
) {
298 /* No room for rune; pad. */
300 charstring_push_back_chars(dest
, spacechar
, spacelen
, 1);
304 charstring_push_back_chars(dest
, src
, srclen
, w
);
310 #ifndef MULTIBYTE_SUPPORT
312 cpstripped (charstring_t dest
, size_t max
, char *str
)
320 squash
= true; /* Strip leading cases. */
322 c
= (unsigned char)*str
++;
326 if (isspace(c
) || iscntrl(c
)) {
334 charstring_push_back(dest
, (char)c
);
339 static char *lmonth
[] = { "January", "February","March", "April",
340 "May", "June", "July", "August",
341 "September","October", "November","December" };
344 get_x400_friendly (char *mbox
, char *buffer
, int buffer_len
)
346 char given
[BUFSIZ
], surname
[BUFSIZ
];
355 if (get_x400_comp (mbox
, "/PN=", buffer
, buffer_len
)) {
356 for (mbox
= buffer
; (mbox
= strchr(mbox
, '.')); )
362 if (!get_x400_comp (mbox
, "/S=", surname
, sizeof(surname
)))
365 if (get_x400_comp (mbox
, "/G=", given
, sizeof(given
)))
366 snprintf (buffer
, buffer_len
, "%s %s", given
, surname
);
368 snprintf (buffer
, buffer_len
, "%s", surname
);
374 get_x400_comp (char *mbox
, char *key
, char *buffer
, int buffer_len
)
379 if ((idx
= stringdex (key
, mbox
)) < 0
380 || !(cp
= strchr(mbox
+= idx
+ strlen (key
), '/')))
383 snprintf (buffer
, buffer_len
, "%*.*s", (int)(cp
- mbox
), (int)(cp
- mbox
), mbox
);
388 fmt_scan (struct format
*format
, charstring_t scanlp
, int width
, int *dat
,
389 struct fmt_callbacks
*callbacks
)
393 char buffer
[NMH_BUFSIZ
], buffer2
[NMH_BUFSIZ
];
404 * max is the same as width, but unsigned so comparisons
405 * with charstring_chars() won't raise compile warnings.
408 savestr
= str
= NULL
;
411 for (fmt
= format
; fmt
->f_type
!= FT_DONE
; fmt
++)
412 switch (fmt
->f_type
) {
415 fmt
->f_comp
->c_flags
&= ~CF_PARSED
;
420 case FT_LS_DECODECOMP
:
422 * Trim these components of any newlines.
424 * But don't trim the "body" and "text" components.
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
;
442 for ( ; charstring_chars (scanlp
) < max
; ) {
443 switch (fmt
->f_type
) {
446 cpstripped (scanlp
, max
- charstring_chars (scanlp
),
447 fmt
->f_comp
->c_text
);
450 cptrimmed (scanlp
, fmt
->f_comp
->c_text
, fmt
->f_width
,
451 fmt
->f_fill
, max
- charstring_chars (scanlp
));
456 while ((c
= *sp
++) && charstring_chars (scanlp
) < max
) {
457 charstring_push_back (scanlp
, c
);
466 rjust
++; /* XXX should do something with this */
468 while ((c
= *sp
++) && --i
>= 0 && charstring_chars (scanlp
) < max
) {
469 charstring_push_back (scanlp
, c
);
471 while (--i
>= 0 && charstring_chars (scanlp
) < max
) {
472 charstring_push_back (scanlp
, fmt
->f_fill
);
477 cpstripped (scanlp
, max
- charstring_chars (scanlp
), str
);
480 cptrimmed (scanlp
, str
, fmt
->f_width
, fmt
->f_fill
,
481 max
- charstring_chars (scanlp
));
486 while ((c
= *sp
++) && charstring_chars (scanlp
) < max
) {
487 charstring_push_back (scanlp
, c
);
492 if (str
) charstring_push_back_chars (scanlp
, str
, strlen (str
), 0);
495 adios (NULL
, "internal error (FT_STRFW)");
501 for (wid
= num
<= 0; num
; ++wid
, num
/= 10) {}
502 cpnumber (scanlp
, value
, wid
, ' ',
503 max
- charstring_chars (scanlp
));
510 unsigned int whole
, tenths
;
511 unsigned int scale
= 0;
512 unsigned int val
= (unsigned int)value
;
513 char *kibisuff
= NULL
;
515 switch (fmt
->f_type
) {
516 case FT_LS_KILO
: scale
= 1000; kibisuff
= ""; break;
517 case FT_LS_KIBI
: scale
= 1024; kibisuff
= "i"; break;
521 snprintf(buffer
, sizeof(buffer
), "%u", val
);
523 /* To prevent divide by 0, found by clang static
525 if (scale
== 0) { scale
= 1; }
527 /* find correct scale for size (Kilo/Mega/Giga/Tera) */
528 for (unitcp
= "KMGT"; val
> (scale
* scale
); val
/= scale
) {
534 strcpy(buffer
, "huge");
536 /* val is scale times too big. we want tenths */
544 tenths
= val
- (whole
* 10);
547 snprintf(buffer
, sizeof(buffer
), "%u.%u%c%s",
548 whole
, tenths
, *unitcp
, kibisuff
);
550 snprintf(buffer
, sizeof(buffer
), "%u%c%s",
551 whole
, *unitcp
, kibisuff
);
559 cpnumber (scanlp
, value
, fmt
->f_width
, fmt
->f_fill
,
560 max
- charstring_chars (scanlp
));
564 charstring_push_back (scanlp
, fmt
->f_char
);
568 if (callbacks
&& callbacks
->trace_func
)
569 callbacks
->trace_func(callbacks
->trace_context
, fmt
, value
,
570 str
, charstring_buffer (scanlp
));
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
));
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
));
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
));
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
));
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
));
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
));
635 value
= match (str
, fmt
->f_text
);
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
));
651 value
= uprf (str
, fmt
->f_text
);
655 value
= (str
!= NULL
&& *str
!= 0);
659 value
= (str
== NULL
|| *str
== 0);
663 value
= (fmt
->f_value
== value
);
667 value
= (fmt
->f_value
!= value
);
671 value
= (fmt
->f_value
> value
);
675 if (callbacks
&& callbacks
->trace_func
)
676 callbacks
->trace_func(callbacks
->trace_context
, fmt
, value
,
677 str
, charstring_buffer (scanlp
));
685 str
= fmt
->f_comp
->c_text
;
691 if (!(str
= getenv (fmt
->f_text
)))
695 if (!(str
= context_find (fmt
->f_text
)))
699 case FT_LS_DECODECOMP
:
700 if (decode_rfc2047(fmt
->f_comp
->c_text
, buffer2
, sizeof(buffer2
)))
703 str
= fmt
->f_comp
->c_text
;
707 if (str
&& decode_rfc2047(str
, buffer2
, sizeof(buffer2
)))
716 strncpy(buffer
, str
, sizeof(buffer
));
717 buffer
[sizeof(buffer
)-1] = '\0';
719 while (isspace((unsigned char) *str
))
722 if ((i
= fmt
->f_width
) < 0) {
727 if (!rjust
&& i
> 0 && (int) strlen(str
) > i
)
730 xp
+= strlen(str
) - 1;
731 while (xp
> str
&& isspace((unsigned char) *xp
))
733 if (rjust
&& i
> 0 && (int) strlen(str
) > i
)
734 str
+= strlen(str
) - i
;
739 value
= (fmt
->f_comp
->c_flags
& CF_TRUE
) != 0;
742 value
= (comp
= fmt
->f_comp
)->c_text
? atoi(comp
->c_text
) : 0;
745 value
= fmt
->f_value
;
748 value
= dat
[fmt
->f_value
];
756 case FT_LV_CHAR_LEFT
:
757 value
= max
- charstring_bytes (scanlp
);
760 value
+= fmt
->f_value
;
763 value
= fmt
->f_value
- value
;
765 case FT_LV_MULTIPLY_L
:
766 value
*= fmt
->f_value
;
770 value
/= fmt
->f_value
;
776 value
%= fmt
->f_value
;
785 value
= fmt
->f_comp
->c_tws
->tw_sec
;
788 value
= fmt
->f_comp
->c_tws
->tw_min
;
791 value
= fmt
->f_comp
->c_tws
->tw_hour
;
794 value
= fmt
->f_comp
->c_tws
->tw_mday
;
797 value
= fmt
->f_comp
->c_tws
->tw_mon
+ 1;
800 str
= tw_moty
[fmt
->f_comp
->c_tws
->tw_mon
];
803 str
= lmonth
[fmt
->f_comp
->c_tws
->tw_mon
];
806 str
= dtwszone (fmt
->f_comp
->c_tws
);
809 value
= fmt
->f_comp
->c_tws
->tw_year
;
812 if (!(((tws
= fmt
->f_comp
->c_tws
)->tw_flags
) & (TW_SEXP
|TW_SIMP
)))
814 value
= tws
->tw_wday
;
817 if (!(((tws
= fmt
->f_comp
->c_tws
)->tw_flags
) & (TW_SEXP
|TW_SIMP
)))
819 str
= tw_dotw
[tws
->tw_wday
];
822 if (!(((tws
= fmt
->f_comp
->c_tws
)->tw_flags
) & (TW_SEXP
|TW_SIMP
)))
824 str
= tw_ldotw
[tws
->tw_wday
];
827 value
= fmt
->f_comp
->c_tws
->tw_yday
;
830 value
= fmt
->f_comp
->c_tws
->tw_zone
;
833 if ((value
= fmt
->f_comp
->c_tws
->tw_clock
) == 0)
834 value
= dmktime(fmt
->f_comp
->c_tws
);
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
;
842 if (!(((tws
= fmt
->f_comp
->c_tws
)->tw_flags
) & (TW_SEXP
|TW_SIMP
)))
844 switch (fmt
->f_comp
->c_tws
->tw_flags
& TW_SDAY
) {
854 if (fmt
->f_comp
->c_tws
->tw_flags
& TW_SZEXP
)
860 value
= fmt
->f_comp
->c_tws
->tw_flags
& TW_DST
? 1 : 0;
863 str
= dasctime (fmt
->f_comp
->c_tws
, TW_ZONE
);
866 str
= dasctime (fmt
->f_comp
->c_tws
, TW_NULL
);
870 str
= fmt
->f_comp
->c_mn
->m_pers
;
873 str
= fmt
->f_comp
->c_mn
->m_mbox
;
876 str
= fmt
->f_comp
->c_mn
->m_host
;
879 str
= fmt
->f_comp
->c_mn
->m_path
;
882 str
= fmt
->f_comp
->c_mn
->m_gname
;
885 str
= fmt
->f_comp
->c_mn
->m_note
;
888 str
= adrformat( fmt
->f_comp
->c_mn
);
891 value
= fmt
->f_comp
->c_mn
->m_type
;
894 value
= fmt
->f_comp
->c_mn
->m_ingrp
;
897 value
= fmt
->f_comp
->c_mn
->m_nohost
;
901 if ((mn
= fmt
->f_comp
->c_mn
) == &fmt_mnull
) {
902 str
= fmt
->f_comp
->c_text
;
905 if (fmt
->f_type
== FT_LS_ADDR
)
907 if ((str
= mn
->m_pers
) == NULL
) {
908 if ((str
= mn
->m_note
)) {
910 strncpy (buffer
, str
, sizeof(buffer
));
911 buffer
[sizeof(buffer
)-1] = '\0';
915 sp
= str
+ strlen(str
) - 1;
924 } else if (!(str
= get_x400_friendly (mn
->m_mbox
,
925 buffer
, sizeof(buffer
)))) {
927 switch (mn
->m_type
) {
932 snprintf (buffer
, sizeof(buffer
), "%s!%s",
933 mn
->m_host
, mn
->m_mbox
);
938 snprintf (buffer
, sizeof(buffer
), "%s@%s",
939 mn
->m_mbox
, mn
->m_host
);
951 /* UNQUOTEs RFC-2822 quoted-string and quoted-pair */
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
);
965 if ((t
= comp
->c_tws
->tw_clock
) == 0)
966 t
= dmktime(comp
->c_tws
);
967 tws
= dlocaltime(&t
);
973 if ((t
= comp
->c_tws
->tw_clock
) == 0)
974 t
= dmktime(comp
->c_tws
);
981 if (comp
->c_flags
& CF_PARSED
)
983 if ((sp
= comp
->c_text
) && (tws
= dparsetime(sp
))) {
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
;
990 comp
->c_flags
|= CF_PARSED
;
994 /* hook for custom address list formatting (see replsbr.c) */
995 if (callbacks
&& callbacks
->formataddr
)
996 str
= callbacks
->formataddr (savestr
, str
);
998 str
= formataddr (savestr
, str
);
1002 /* The same as formataddr, but doesn't do duplicate suppression */
1003 if (callbacks
&& callbacks
->concataddr
)
1004 str
= callbacks
->concataddr (savestr
, str
);
1006 str
= concataddr (savestr
, str
);
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
1018 int indent
, wid
, len
;
1022 len
= str
? strlen (str
) : 0;
1024 indent
= strlen (sp
);
1027 adios(NULL
, "putaddr -- num register (%d) must be greater "
1028 "than label width (%d)", value
, indent
);
1030 while ((c
= *sp
++) && charstring_chars (scanlp
) < max
) {
1031 charstring_push_back (scanlp
, c
);
1034 /* try to break at a comma; failing that, break at a
1037 lastb
= 0; sp
= lp
+ wid
;
1038 while (sp
> lp
&& (c
= (unsigned char) *--sp
) != ',') {
1039 if (! lastb
&& isspace(c
))
1043 if (! (sp
= lastb
)) {
1045 while (*sp
&& *sp
!= ',' &&
1046 !isspace((unsigned char) *sp
))
1053 while (lp
<= sp
&& charstring_chars (scanlp
) < max
) {
1054 charstring_push_back (scanlp
, *lp
++);
1056 while (isspace((unsigned char) *lp
))
1059 if (charstring_chars (scanlp
) < max
) {
1060 charstring_push_back (scanlp
, '\n');
1063 charstring_chars (scanlp
) < max
&& i
> 0;
1065 charstring_push_back (scanlp
, ' ');
1068 cpstripped (scanlp
, max
- charstring_chars (scanlp
), lp
);
1074 if (comp
->c_flags
& CF_PARSED
)
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))) {
1083 comp
->c_flags
|= CF_PARSED
;
1085 while (getname("")) /* XXX */
1087 comp
->c_mn
= &fmt_mnull
;
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.
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))) {
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
1113 comp
->c_flags
&= ~CF_TRUE
;
1115 while ((sp
= getname(sp
)))
1116 if ((comp
->c_flags
& CF_TRUE
) == 0 &&
1117 (mn
= getm (sp
, NULL
, 0, NULL
, 0)))
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
1125 free (comp
->c_text
);
1126 comp
->c_text
= str
= strdup (mn
->m_text
);
1129 comp
->c_flags
|= CF_PARSED
;
1131 while (getname("")) /* XXX */
1133 if (comp
->c_text
== 0)
1134 comp
->c_flags
|= CF_TRUE
;
1136 comp
->c_flags
&= ~CF_TRUE
;
1138 comp
->c_mn
= &fmt_mnull
;
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
;
1149 * Call our tracing callback function, if one was supplied
1152 if (callbacks
&& callbacks
->trace_func
)
1153 callbacks
->trace_func(callbacks
->trace_context
, fmt
, value
,
1154 str
, charstring_buffer (scanlp
));
1158 /* Emit any trailing sequences of zero display length. */
1159 while (fmt
->f_type
!= FT_DONE
) {
1160 if (fmt
->f_type
== FT_LS_LIT
) {
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). */
1171 for (sp
= str
; *sp
; ++sp
) {
1172 charstring_push_back (scanlp
, *sp
);
1175 if (callbacks
&& callbacks
->trace_func
)
1176 callbacks
->trace_func(callbacks
->trace_context
, fmt
, value
,
1177 str
, charstring_buffer (scanlp
));
1183 if (charstring_bytes (scanlp
) > 0) {
1185 * Append a newline if the last character wasn't.
1187 #ifdef MULTIBYTE_SUPPORT
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
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 */
1198 if (last_char_len
> 1 ||
1199 charstring_buffer (scanlp
)[charstring_bytes (scanlp
) - 1] != '\n') {
1200 charstring_push_back (scanlp
, '\n');