]>
diplodocus.org Git - nmh/blob - sbr/fmt_scan.c
83e48b8b4306f5beff856db266dcfcb5d80593f0
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
)
225 int prevCtrl
= 1; /* This is 1 so we strip out leading spaces */
237 if (mbtowc(NULL
, NULL
, 0)) {} /* Reset shift state */
240 * Process each character at a time; if we have multibyte support
241 * then deal with that here.
244 while (*str
!= '\0' && len
> 0 && max
> 0) {
245 char_len
= mbtowc(&wide_char
, str
, len
);
248 * If mbrtowc() failed, then we have a character that isn't valid
249 * in the current encoding, or len wasn't enough for the whole
250 * multi-byte rune to be read. Replace it with a '?'. We do that by
251 * setting the alstr variable to the value of the replacement string;
252 * altstr is used below when the bytes are copied into the output
257 char_len
= mbtowc(&wide_char
, altstr
, 1);
266 if (iswcntrl(wide_char
) || iswspace(wide_char
)) {
269 charstring_push_back (dest
, ' ');
279 w
= wcwidth(wide_char
);
281 if (max
>= (size_t) w
) {
282 charstring_push_back_chars (dest
, altstr
? altstr
: str
, char_len
, w
);
287 /* Not enough width available for the last character. Output
290 charstring_push_back (dest
, ' ');
298 #ifndef MULTIBYTE_SUPPORT
300 cpstripped (charstring_t dest
, size_t max
, char *str
)
302 int prevCtrl
= 1; /* This is 1 so we strip out leading spaces */
312 * Process each character at a time; if we have multibyte support
313 * then deal with that here.
316 while (*str
!= '\0' && len
> 0 && max
> 0) {
317 int c
= (unsigned char) *str
;
319 if (iscntrl(c
) || isspace(c
)) {
322 charstring_push_back (dest
, ' ');
332 charstring_push_back (dest
, *str
++);
338 static char *lmonth
[] = { "January", "February","March", "April",
339 "May", "June", "July", "August",
340 "September","October", "November","December" };
343 get_x400_friendly (char *mbox
, char *buffer
, int buffer_len
)
345 char given
[BUFSIZ
], surname
[BUFSIZ
];
354 if (get_x400_comp (mbox
, "/PN=", buffer
, buffer_len
)) {
355 for (mbox
= buffer
; (mbox
= strchr(mbox
, '.')); )
361 if (!get_x400_comp (mbox
, "/S=", surname
, sizeof(surname
)))
364 if (get_x400_comp (mbox
, "/G=", given
, sizeof(given
)))
365 snprintf (buffer
, buffer_len
, "%s %s", given
, surname
);
367 snprintf (buffer
, buffer_len
, "%s", surname
);
373 get_x400_comp (char *mbox
, char *key
, char *buffer
, int buffer_len
)
378 if ((idx
= stringdex (key
, mbox
)) < 0
379 || !(cp
= strchr(mbox
+= idx
+ strlen (key
), '/')))
382 snprintf (buffer
, buffer_len
, "%*.*s", (int)(cp
- mbox
), (int)(cp
- mbox
), mbox
);
387 fmt_scan (struct format
*format
, charstring_t scanlp
, int width
, int *dat
,
388 struct fmt_callbacks
*callbacks
)
392 char buffer
[NMH_BUFSIZ
], buffer2
[NMH_BUFSIZ
];
403 * max is the same as width, but unsigned so comparisons
404 * with charstring_chars() won't raise compile warnings.
407 savestr
= str
= NULL
;
410 for (fmt
= format
; fmt
->f_type
!= FT_DONE
; fmt
++)
411 switch (fmt
->f_type
) {
414 fmt
->f_comp
->c_flags
&= ~CF_PARSED
;
419 case FT_LS_DECODECOMP
:
421 * Trim these components of any newlines.
423 * But don't trim the "body" and "text" components.
428 if (! (comp
->c_flags
& CF_TRIMMED
) && comp
->c_text
&&
429 (i
= strlen(comp
->c_text
)) > 0) {
430 if (comp
->c_text
[i
- 1] == '\n' &&
431 strcmp(comp
->c_name
, "body") != 0 &&
432 strcmp(comp
->c_name
, "text") != 0)
433 comp
->c_text
[i
- 1] = '\0';
434 comp
->c_flags
|= CF_TRIMMED
;
441 for ( ; charstring_chars (scanlp
) < max
; ) {
442 switch (fmt
->f_type
) {
445 cpstripped (scanlp
, max
- charstring_chars (scanlp
),
446 fmt
->f_comp
->c_text
);
449 cptrimmed (scanlp
, fmt
->f_comp
->c_text
, fmt
->f_width
,
450 fmt
->f_fill
, max
- charstring_chars (scanlp
));
455 while ((c
= *sp
++) && charstring_chars (scanlp
) < max
) {
456 charstring_push_back (scanlp
, c
);
465 rjust
++; /* XXX should do something with this */
467 while ((c
= *sp
++) && --i
>= 0 && charstring_chars (scanlp
) < max
) {
468 charstring_push_back (scanlp
, c
);
470 while (--i
>= 0 && charstring_chars (scanlp
) < max
) {
471 charstring_push_back (scanlp
, fmt
->f_fill
);
476 cpstripped (scanlp
, max
- charstring_chars (scanlp
), str
);
479 cptrimmed (scanlp
, str
, fmt
->f_width
, fmt
->f_fill
,
480 max
- charstring_chars (scanlp
));
485 while ((c
= *sp
++) && charstring_chars (scanlp
) < max
) {
486 charstring_push_back (scanlp
, c
);
491 if (str
) charstring_push_back_chars (scanlp
, str
, strlen (str
), 0);
494 adios (NULL
, "internal error (FT_STRFW)");
500 for (wid
= num
<= 0; num
; ++wid
, num
/= 10) {}
501 cpnumber (scanlp
, value
, wid
, ' ',
502 max
- charstring_chars (scanlp
));
509 unsigned int whole
, tenths
;
510 unsigned int scale
= 0;
511 unsigned int val
= (unsigned int)value
;
512 char *kibisuff
= NULL
;
514 switch (fmt
->f_type
) {
515 case FT_LS_KILO
: scale
= 1000; kibisuff
= ""; break;
516 case FT_LS_KIBI
: scale
= 1024; kibisuff
= "i"; break;
520 snprintf(buffer
, sizeof(buffer
), "%u", val
);
522 /* To prevent divide by 0, found by clang static
524 if (scale
== 0) { scale
= 1; }
526 /* find correct scale for size (Kilo/Mega/Giga/Tera) */
527 for (unitcp
= "KMGT"; val
> (scale
* scale
); val
/= scale
) {
533 strcpy(buffer
, "huge");
535 /* val is scale times too big. we want tenths */
543 tenths
= val
- (whole
* 10);
546 snprintf(buffer
, sizeof(buffer
), "%u.%u%c%s",
547 whole
, tenths
, *unitcp
, kibisuff
);
549 snprintf(buffer
, sizeof(buffer
), "%u%c%s",
550 whole
, *unitcp
, kibisuff
);
558 cpnumber (scanlp
, value
, fmt
->f_width
, fmt
->f_fill
,
559 max
- charstring_chars (scanlp
));
563 charstring_push_back (scanlp
, fmt
->f_char
);
567 if (callbacks
&& callbacks
->trace_func
)
568 callbacks
->trace_func(callbacks
->trace_context
, fmt
, value
,
569 str
, charstring_buffer (scanlp
));
573 if (!(value
= (str
&& *str
))) {
574 if (callbacks
&& callbacks
->trace_func
)
575 callbacks
->trace_func(callbacks
->trace_context
, fmt
, value
,
576 str
, charstring_buffer (scanlp
));
583 if (!(value
= (str
== NULL
|| *str
== 0))) {
584 if (callbacks
&& callbacks
->trace_func
)
585 callbacks
->trace_func(callbacks
->trace_context
, fmt
, value
,
586 str
, charstring_buffer (scanlp
));
593 if (value
!= fmt
->f_value
) {
594 if (callbacks
&& callbacks
->trace_func
)
595 callbacks
->trace_func(callbacks
->trace_context
, fmt
, value
,
596 str
, charstring_buffer (scanlp
));
603 if (value
== fmt
->f_value
) {
604 if (callbacks
&& callbacks
->trace_func
)
605 callbacks
->trace_func(callbacks
->trace_context
, fmt
, value
,
606 str
, charstring_buffer (scanlp
));
613 if (value
<= fmt
->f_value
) {
614 if (callbacks
&& callbacks
->trace_func
)
615 callbacks
->trace_func(callbacks
->trace_context
, fmt
, value
,
616 str
, charstring_buffer (scanlp
));
623 if (!(value
= (str
&& match (str
, fmt
->f_text
)))) {
624 if (callbacks
&& callbacks
->trace_func
)
625 callbacks
->trace_func(callbacks
->trace_context
, fmt
, value
,
626 str
, charstring_buffer (scanlp
));
634 value
= match (str
, fmt
->f_text
);
640 if (!(value
= (str
&& uprf (str
, fmt
->f_text
)))) {
641 if (callbacks
&& callbacks
->trace_func
)
642 callbacks
->trace_func(callbacks
->trace_context
, fmt
, value
,
643 str
, charstring_buffer (scanlp
));
650 value
= uprf (str
, fmt
->f_text
);
654 value
= (str
!= NULL
&& *str
!= 0);
658 value
= (str
== NULL
|| *str
== 0);
662 value
= (fmt
->f_value
== value
);
666 value
= (fmt
->f_value
!= value
);
670 value
= (fmt
->f_value
> value
);
674 if (callbacks
&& callbacks
->trace_func
)
675 callbacks
->trace_func(callbacks
->trace_context
, fmt
, value
,
676 str
, charstring_buffer (scanlp
));
684 str
= fmt
->f_comp
->c_text
;
690 if (!(str
= getenv (fmt
->f_text
)))
694 if (!(str
= context_find (fmt
->f_text
)))
698 case FT_LS_DECODECOMP
:
699 if (decode_rfc2047(fmt
->f_comp
->c_text
, buffer2
, sizeof(buffer2
)))
702 str
= fmt
->f_comp
->c_text
;
706 if (str
&& decode_rfc2047(str
, buffer2
, sizeof(buffer2
)))
715 strncpy(buffer
, str
, sizeof(buffer
));
716 buffer
[sizeof(buffer
)-1] = '\0';
718 while (isspace((unsigned char) *str
))
721 if ((i
= fmt
->f_width
) < 0) {
726 if (!rjust
&& i
> 0 && (int) strlen(str
) > i
)
729 xp
+= strlen(str
) - 1;
730 while (xp
> str
&& isspace((unsigned char) *xp
))
732 if (rjust
&& i
> 0 && (int) strlen(str
) > i
)
733 str
+= strlen(str
) - i
;
738 value
= (fmt
->f_comp
->c_flags
& CF_TRUE
) != 0;
741 value
= (comp
= fmt
->f_comp
)->c_text
? atoi(comp
->c_text
) : 0;
744 value
= fmt
->f_value
;
747 value
= dat
[fmt
->f_value
];
755 case FT_LV_CHAR_LEFT
:
756 value
= max
- charstring_bytes (scanlp
);
759 value
+= fmt
->f_value
;
762 value
= fmt
->f_value
- value
;
764 case FT_LV_MULTIPLY_L
:
765 value
*= fmt
->f_value
;
769 value
/= fmt
->f_value
;
775 value
%= fmt
->f_value
;
784 value
= fmt
->f_comp
->c_tws
->tw_sec
;
787 value
= fmt
->f_comp
->c_tws
->tw_min
;
790 value
= fmt
->f_comp
->c_tws
->tw_hour
;
793 value
= fmt
->f_comp
->c_tws
->tw_mday
;
796 value
= fmt
->f_comp
->c_tws
->tw_mon
+ 1;
799 str
= tw_moty
[fmt
->f_comp
->c_tws
->tw_mon
];
802 str
= lmonth
[fmt
->f_comp
->c_tws
->tw_mon
];
805 str
= dtwszone (fmt
->f_comp
->c_tws
);
808 value
= fmt
->f_comp
->c_tws
->tw_year
;
811 if (!(((tws
= fmt
->f_comp
->c_tws
)->tw_flags
) & (TW_SEXP
|TW_SIMP
)))
813 value
= tws
->tw_wday
;
816 if (!(((tws
= fmt
->f_comp
->c_tws
)->tw_flags
) & (TW_SEXP
|TW_SIMP
)))
818 str
= tw_dotw
[tws
->tw_wday
];
821 if (!(((tws
= fmt
->f_comp
->c_tws
)->tw_flags
) & (TW_SEXP
|TW_SIMP
)))
823 str
= tw_ldotw
[tws
->tw_wday
];
826 value
= fmt
->f_comp
->c_tws
->tw_yday
;
829 value
= fmt
->f_comp
->c_tws
->tw_zone
;
832 if ((value
= fmt
->f_comp
->c_tws
->tw_clock
) == 0)
833 value
= dmktime(fmt
->f_comp
->c_tws
);
836 if ((value
= fmt
->f_comp
->c_tws
->tw_clock
) == 0)
837 value
= dmktime(fmt
->f_comp
->c_tws
);
838 value
= time((time_t *) 0) - value
;
841 if (!(((tws
= fmt
->f_comp
->c_tws
)->tw_flags
) & (TW_SEXP
|TW_SIMP
)))
843 switch (fmt
->f_comp
->c_tws
->tw_flags
& TW_SDAY
) {
853 if (fmt
->f_comp
->c_tws
->tw_flags
& TW_SZEXP
)
859 value
= fmt
->f_comp
->c_tws
->tw_flags
& TW_DST
? 1 : 0;
862 str
= dasctime (fmt
->f_comp
->c_tws
, TW_ZONE
);
865 str
= dasctime (fmt
->f_comp
->c_tws
, TW_NULL
);
869 str
= fmt
->f_comp
->c_mn
->m_pers
;
872 str
= fmt
->f_comp
->c_mn
->m_mbox
;
875 str
= fmt
->f_comp
->c_mn
->m_host
;
878 str
= fmt
->f_comp
->c_mn
->m_path
;
881 str
= fmt
->f_comp
->c_mn
->m_gname
;
884 str
= fmt
->f_comp
->c_mn
->m_note
;
887 str
= adrformat( fmt
->f_comp
->c_mn
);
890 value
= fmt
->f_comp
->c_mn
->m_type
;
893 value
= fmt
->f_comp
->c_mn
->m_ingrp
;
896 value
= fmt
->f_comp
->c_mn
->m_nohost
;
900 if ((mn
= fmt
->f_comp
->c_mn
) == &fmt_mnull
) {
901 str
= fmt
->f_comp
->c_text
;
904 if (fmt
->f_type
== FT_LS_ADDR
)
906 if ((str
= mn
->m_pers
) == NULL
) {
907 if ((str
= mn
->m_note
)) {
909 strncpy (buffer
, str
, sizeof(buffer
));
910 buffer
[sizeof(buffer
)-1] = '\0';
914 sp
= str
+ strlen(str
) - 1;
923 } else if (!(str
= get_x400_friendly (mn
->m_mbox
,
924 buffer
, sizeof(buffer
)))) {
926 switch (mn
->m_type
) {
931 snprintf (buffer
, sizeof(buffer
), "%s!%s",
932 mn
->m_host
, mn
->m_mbox
);
937 snprintf (buffer
, sizeof(buffer
), "%s@%s",
938 mn
->m_mbox
, mn
->m_host
);
950 /* UNQUOTEs RFC-2822 quoted-string and quoted-pair */
954 strncpy(buffer
, str
, sizeof(buffer
));
955 /* strncpy doesn't NUL-terminate if it fills the buffer */
956 buffer
[sizeof(buffer
)-1] = '\0';
957 unquote_string(buffer
, buffer2
);
964 if ((t
= comp
->c_tws
->tw_clock
) == 0)
965 t
= dmktime(comp
->c_tws
);
966 tws
= dlocaltime(&t
);
972 if ((t
= comp
->c_tws
->tw_clock
) == 0)
973 t
= dmktime(comp
->c_tws
);
980 if (comp
->c_flags
& CF_PARSED
)
982 if ((sp
= comp
->c_text
) && (tws
= dparsetime(sp
))) {
984 comp
->c_flags
&= ~CF_TRUE
;
985 } else if ((comp
->c_flags
& CF_DATEFAB
) == 0) {
986 memset (comp
->c_tws
, 0, sizeof *comp
->c_tws
);
987 comp
->c_flags
= CF_TRUE
;
989 comp
->c_flags
|= CF_PARSED
;
993 /* hook for custom address list formatting (see replsbr.c) */
994 if (callbacks
&& callbacks
->formataddr
)
995 str
= callbacks
->formataddr (savestr
, str
);
997 str
= formataddr (savestr
, str
);
1001 /* The same as formataddr, but doesn't do duplicate suppression */
1002 if (callbacks
&& callbacks
->concataddr
)
1003 str
= callbacks
->concataddr (savestr
, str
);
1005 str
= concataddr (savestr
, str
);
1009 /* output the str register as an address component,
1010 * splitting it into multiple lines if necessary. The
1011 * value reg. contains the max line length. The lit.
1012 * field may contain a string to prepend to the result
1017 int indent
, wid
, len
;
1021 len
= str
? strlen (str
) : 0;
1023 indent
= strlen (sp
);
1026 adios(NULL
, "putaddr -- num register (%d) must be greater "
1027 "than label width (%d)", value
, indent
);
1029 while ((c
= *sp
++) && charstring_chars (scanlp
) < max
) {
1030 charstring_push_back (scanlp
, c
);
1033 /* try to break at a comma; failing that, break at a
1036 lastb
= 0; sp
= lp
+ wid
;
1037 while (sp
> lp
&& (c
= (unsigned char) *--sp
) != ',') {
1038 if (! lastb
&& isspace(c
))
1042 if (! (sp
= lastb
)) {
1044 while (*sp
&& *sp
!= ',' &&
1045 !isspace((unsigned char) *sp
))
1052 while (lp
<= sp
&& charstring_chars (scanlp
) < max
) {
1053 charstring_push_back (scanlp
, *lp
++);
1055 while (isspace((unsigned char) *lp
))
1058 if (charstring_chars (scanlp
) < max
) {
1059 charstring_push_back (scanlp
, '\n');
1062 charstring_chars (scanlp
) < max
&& i
> 0;
1064 charstring_push_back (scanlp
, ' ');
1067 cpstripped (scanlp
, max
- charstring_chars (scanlp
), lp
);
1073 if (comp
->c_flags
& CF_PARSED
)
1075 if (comp
->c_mn
!= &fmt_mnull
)
1076 mnfree (comp
->c_mn
);
1077 if ((sp
= comp
->c_text
) && (sp
= getname(sp
)) &&
1078 (mn
= getm (sp
, NULL
, 0, NULL
, 0))) {
1082 comp
->c_flags
|= CF_PARSED
;
1084 while (getname("")) /* XXX */
1086 comp
->c_mn
= &fmt_mnull
;
1094 * if there's no component, we say true. Otherwise we
1095 * say "true" only if we can parse the address and it
1096 * matches one of our addresses.
1099 if (comp
->c_mn
!= &fmt_mnull
)
1100 mnfree (comp
->c_mn
);
1101 if ((sp
= comp
->c_text
) && (sp
= getname(sp
)) &&
1102 (mn
= getm (sp
, NULL
, 0, NULL
, 0))) {
1105 comp
->c_flags
|= CF_TRUE
;
1106 /* Set str for use with FT_GETMYMBOX. With
1107 FT_GETMYADDR, comp->c_mn will be run through
1108 FT_LS_ADDR, which will strip off any pers
1112 comp
->c_flags
&= ~CF_TRUE
;
1114 while ((sp
= getname(sp
)))
1115 if ((comp
->c_flags
& CF_TRUE
) == 0 &&
1116 (mn
= getm (sp
, NULL
, 0, NULL
, 0)))
1118 comp
->c_flags
|= CF_TRUE
;
1119 /* Set str and comp->c_text for use with
1120 FT_GETMYMBOX. With FT_GETMYADDR,
1121 comp->c_mn will be run through
1122 FT_LS_ADDR, which will strip off any
1124 free (comp
->c_text
);
1125 comp
->c_text
= str
= strdup (mn
->m_text
);
1128 comp
->c_flags
|= CF_PARSED
;
1130 while (getname("")) /* XXX */
1132 if (comp
->c_text
== 0)
1133 comp
->c_flags
|= CF_TRUE
;
1135 comp
->c_flags
&= ~CF_TRUE
;
1137 comp
->c_mn
= &fmt_mnull
;
1139 if ((comp
->c_flags
& CF_TRUE
) == 0 &&
1140 (fmt
->f_type
== FT_GETMYMBOX
|| fmt
->f_type
== FT_GETMYADDR
)) {
1141 /* Fool FT_LS_ADDR into not producing an address. */
1142 comp
->c_mn
= &fmt_mnull
; comp
->c_text
= NULL
;
1148 * Call our tracing callback function, if one was supplied
1151 if (callbacks
&& callbacks
->trace_func
)
1152 callbacks
->trace_func(callbacks
->trace_context
, fmt
, value
,
1153 str
, charstring_buffer (scanlp
));
1157 /* Emit any trailing sequences of zero display length. */
1158 while (fmt
->f_type
!= FT_DONE
) {
1159 if (fmt
->f_type
== FT_LS_LIT
) {
1161 if (callbacks
&& callbacks
->trace_func
)
1162 callbacks
->trace_func(callbacks
->trace_context
, fmt
, value
,
1163 str
, charstring_buffer (scanlp
));
1164 } else if (fmt
->f_type
== FT_STRLITZ
) {
1165 /* Don't want to emit part of an escape sequence. So if
1166 there isn't enough room in the buffer for the entire
1167 string, skip it completely. Need room for null
1168 terminator, and maybe trailing newline (added below). */
1170 for (sp
= str
; *sp
; ++sp
) {
1171 charstring_push_back (scanlp
, *sp
);
1174 if (callbacks
&& callbacks
->trace_func
)
1175 callbacks
->trace_func(callbacks
->trace_context
, fmt
, value
,
1176 str
, charstring_buffer (scanlp
));
1182 if (charstring_bytes (scanlp
) > 0) {
1184 * Append a newline if the last character wasn't.
1186 #ifdef MULTIBYTE_SUPPORT
1188 * It's a little tricky because the last byte might be part of
1189 * a multibyte character, in which case we assume that wasn't
1192 size_t last_char_len
= charstring_last_char_len (scanlp
);
1193 #else /* ! MULTIBYTE_SUPPORT */
1194 size_t last_char_len
= 1;
1195 #endif /* ! MULTIBYTE_SUPPORT */
1197 if (last_char_len
> 1 ||
1198 charstring_buffer (scanlp
)[charstring_bytes (scanlp
) - 1] != '\n') {
1199 charstring_push_back (scanlp
, '\n');