Remove building with NOCRYPTO option
[minix.git] / external / bsd / bind / dist / unit / atf-src / atf-c++ / detail / ui.cpp
blob07bde4f87dba17b7b0d1c4ad7eb160e60f8d6098
1 //
2 // Automated Testing Framework (atf)
3 //
4 // Copyright (c) 2007 The NetBSD Foundation, Inc.
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
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 extern "C" {
31 #include <sys/ioctl.h>
33 #include <termios.h>
34 #include <unistd.h>
37 #include <sstream>
39 #include "env.hpp"
40 #include "text.hpp"
41 #include "sanity.hpp"
42 #include "text.hpp"
43 #include "ui.hpp"
45 namespace impl = atf::ui;
46 #define IMPL_NAME "atf::ui"
48 static
49 size_t
50 terminal_width(void)
52 static bool done = false;
53 static size_t width = 0;
55 if (!done) {
56 if (atf::env::has("COLUMNS")) {
57 const std::string cols = atf::env::get("COLUMNS");
58 if (cols.length() > 0) {
59 width = atf::text::to_type< size_t >(cols);
61 } else {
62 struct winsize ws;
63 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) != -1)
64 width = ws.ws_col;
67 if (width >= 80)
68 width -= 5;
70 done = true;
73 return width;
76 static
77 std::string
78 format_paragraph(const std::string& text,
79 const std::string& tag,
80 const bool first,
81 const bool repeat,
82 const size_t col)
84 PRE(text.find('\n') == std::string::npos);
86 const std::string pad(col - tag.length(), ' ');
87 const std::string fullpad(col, ' ');
89 std::string formatted;
90 if (first || repeat)
91 formatted = tag + pad;
92 else
93 formatted = fullpad;
94 INV(formatted.length() == col);
95 size_t curcol = col;
97 const size_t maxcol = terminal_width();
99 std::vector< std::string > words = atf::text::split(text, " ");
100 for (std::vector< std::string >::const_iterator iter = words.begin();
101 iter != words.end(); iter++) {
102 const std::string& word = *iter;
104 if (iter != words.begin() && maxcol > 0 &&
105 curcol + word.length() + 1 > maxcol) {
106 if (repeat)
107 formatted += '\n' + tag + pad;
108 else
109 formatted += '\n' + fullpad;
110 curcol = col;
111 } else if (iter != words.begin()) {
112 formatted += ' ';
113 curcol++;
116 formatted += word;
117 curcol += word.length();
120 return formatted;
123 std::string
124 impl::format_error(const std::string& prog_name, const std::string& error)
126 return format_text_with_tag("ERROR: " + error, prog_name + ": ", true);
129 std::string
130 impl::format_info(const std::string& prog_name, const std::string& msg)
132 return format_text_with_tag(msg, prog_name + ": ", true);
135 std::string
136 impl::format_text(const std::string& text)
138 return format_text_with_tag(text, "", false, 0);
141 std::string
142 impl::format_text_with_tag(const std::string& text, const std::string& tag,
143 bool repeat, size_t col)
145 PRE(col == 0 || col >= tag.length());
146 if (col == 0)
147 col = tag.length();
149 std::string formatted;
151 std::vector< std::string > lines = atf::text::split(text, "\n");
152 for (std::vector< std::string >::const_iterator iter = lines.begin();
153 iter != lines.end(); iter++) {
154 const std::string& line = *iter;
156 formatted += format_paragraph(line, tag, iter == lines.begin(),
157 repeat, col);
158 if (iter + 1 != lines.end()) {
159 if (repeat)
160 formatted += "\n" + tag + "\n";
161 else
162 formatted += "\n\n";
166 return formatted;
169 std::string
170 impl::format_warning(const std::string& prog_name, const std::string& error)
172 return format_text_with_tag("WARNING: " + error, prog_name + ": ", true);