2 * Automated Testing Framework (atf)
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/ioctl.h>
39 #include "atf-c/dynstr.h"
40 #include "atf-c/env.h"
41 #include "atf-c/error.h"
42 #include "atf-c/sanity.h"
43 #include "atf-c/text.h"
46 /* ---------------------------------------------------------------------
47 * Auxiliary functions.
48 * --------------------------------------------------------------------- */
54 static bool done
= false;
55 static size_t width
= 0;
58 if (atf_env_has("COLUMNS")) {
59 const char *cols
= atf_env_get("COLUMNS");
60 if (strlen(cols
) > 0) {
61 width
= atoi(cols
); /* XXX No error checking */
65 if (ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
) != -1)
80 format_paragraph(atf_dynstr_t
*dest
,
81 const char *tag
, bool repeat
, size_t col
, bool firstp
,
84 const size_t maxcol
= terminal_width();
87 atf_dynstr_t pad
, fullpad
;
90 err
= atf_dynstr_init_rep(&pad
, col
- strlen(tag
), ' ');
91 if (atf_is_error(err
))
94 err
= atf_dynstr_init_rep(&fullpad
, col
, ' ');
95 if (atf_is_error(err
))
99 err
= atf_dynstr_append_fmt(dest
, "%s%s", tag
,
100 atf_dynstr_cstring(&pad
));
102 err
= atf_dynstr_append_fmt(dest
, atf_dynstr_cstring(&fullpad
));
103 if (atf_is_error(err
))
106 last
= NULL
; /* Silence GCC warning. */
107 str2
= strtok_r(str
, " ", &last
);
110 const bool firstw
= (str
== str2
);
113 if (maxcol
> 0 && curcol
+ strlen(str2
) + 1 > maxcol
) {
115 err
= atf_dynstr_append_fmt
116 (dest
, "\n%s%s", tag
, atf_dynstr_cstring(&pad
));
118 err
= atf_dynstr_append_fmt
119 (dest
, "\n%s", atf_dynstr_cstring(&fullpad
));
122 err
= atf_dynstr_append_fmt(dest
, " ");
127 if (!atf_is_error(err
)) {
128 err
= atf_dynstr_append_fmt(dest
, str2
);
129 curcol
+= strlen(str2
);
131 str2
= strtok_r(NULL
, " ", &last
);
133 } while (!atf_is_error(err
) && str2
!= NULL
);
136 atf_dynstr_fini(&fullpad
);
138 atf_dynstr_fini(&pad
);
145 format_aux(atf_dynstr_t
*dest
,
146 const char *tag
, bool repeat
, size_t col
, char *str
)
151 PRE(col
== 0 || col
>= strlen(tag
));
155 str2
= str
+ strlen(str
);
156 while (str2
!= str
&& *--str2
== '\n')
159 last
= NULL
; /* Silence GCC warning. */
160 str2
= strtok_r(str
, "\n", &last
);
162 const bool first
= (str2
== str
);
163 err
= format_paragraph(dest
, tag
, repeat
, col
, first
, str2
);
164 str2
= strtok_r(NULL
, "\n", &last
);
165 if (!atf_is_error(err
) && str2
!= NULL
) {
167 err
= atf_dynstr_append_fmt(dest
, "\n%s\n", tag
);
169 err
= atf_dynstr_append_fmt(dest
, "\n\n");
171 } while (str2
!= NULL
&& !atf_is_error(err
));
176 /* ---------------------------------------------------------------------
178 * --------------------------------------------------------------------- */
181 atf_ui_format_ap(atf_dynstr_t
*dest
,
182 const char *tag
, bool repeat
, size_t col
,
183 const char *fmt
, va_list ap
)
190 err
= atf_text_format_ap(&src
, fmt
, ap2
);
192 if (!atf_is_error(err
)) {
193 err
= format_aux(dest
, tag
, repeat
, col
, src
);
201 atf_ui_format_fmt(atf_dynstr_t
*dest
,
202 const char *tag
, bool repeat
, size_t col
,
203 const char *fmt
, ...)
209 err
= atf_ui_format_ap(dest
, tag
, repeat
, col
, fmt
, ap
);