1// RUN: %clangxx -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s
2//
3// UNSUPPORTED: target={{.*(linux|solaris).*}}, darwin
4
5#include <ctype.h>
6#include <err.h>
7#include <inttypes.h>
8#include <stdint.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <vis.h>
12
13void test_vis() {
14 char visout[5];
15 int ch = toascii(0x1);
16 vis(visout, ch, VIS_SAFE | VIS_NOSLASH, 0);
17 printf(format: "vis: %s\n", visout);
18}
19
20void test_nvis() {
21 char visout[5];
22 int ch = toascii(0x2);
23 nvis(visout, sizeof visout, ch, VIS_SAFE | VIS_NOSLASH, 0);
24 printf(format: "nvis: %s\n", visout);
25}
26
27void test_strvis() {
28 char visout[5];
29 strvis(visout, "\3", VIS_SAFE | VIS_NOSLASH);
30 printf(format: "strvis: %s\n", visout);
31}
32
33void test_stravis() {
34 char *visout;
35 stravis(&visout, "\4", VIS_SAFE | VIS_NOSLASH);
36 printf(format: "stravis: %s\n", visout);
37 free(ptr: visout);
38}
39
40void test_strnvis() {
41 char visout[5];
42 strnvis(visout, sizeof visout, "\5", VIS_SAFE | VIS_NOSLASH);
43 printf(format: "strnvis: %s\n", visout);
44}
45
46void test_strvisx() {
47 char visout[5];
48 char src[] = "\6";
49 strvisx(visout, src, sizeof src - 1 /* skip final \0 */,
50 VIS_SAFE | VIS_NOSLASH);
51 printf(format: "strvisx: %s\n", visout);
52}
53
54void test_strnvisx() {
55 char visout[5];
56 char src[] = "\1";
57 strnvisx(visout, sizeof visout, src, sizeof src - 1 /* skip final \0 */,
58 VIS_SAFE | VIS_NOSLASH);
59 printf(format: "strnvisx: %s\n", visout);
60}
61
62void test_strenvisx() {
63 char visout[5];
64 char src[] = "\2";
65 strenvisx(visout, sizeof visout, src, sizeof src - 1 /* skip final \0 */,
66 VIS_SAFE | VIS_NOSLASH, NULL);
67 printf(format: "strenvisx: %s\n", visout);
68}
69
70void test_svis() {
71 char visout[5];
72 int ch = toascii(0x3);
73 svis(visout, ch, VIS_SAFE | VIS_NOSLASH, 0, "x");
74 printf(format: "svis: %s\n", visout);
75}
76
77void test_snvis() {
78 char visout[5];
79 int ch = toascii(0x2);
80 snvis(visout, sizeof visout, ch, VIS_SAFE | VIS_NOSLASH, 0, "x");
81 printf(format: "snvis: %s\n", visout);
82}
83
84void test_strsvis() {
85 char visout[5];
86 strsvis(visout, "\4", VIS_SAFE | VIS_NOSLASH, "x");
87 printf(format: "strsvis: %s\n", visout);
88}
89
90void test_strsnvis() {
91 char visout[5];
92 strsnvis(visout, sizeof visout, "\5", VIS_SAFE | VIS_NOSLASH, "x");
93 printf(format: "strsnvis: %s\n", visout);
94}
95
96void test_strsvisx() {
97 char visout[5];
98 char src[] = "\5";
99 strsvisx(visout, src, sizeof src - 1 /* skip final \0 */,
100 VIS_SAFE | VIS_NOSLASH, "x");
101 printf(format: "strsvisx: %s\n", visout);
102}
103
104void test_strsnvisx() {
105 char visout[5];
106 char src[] = "\6";
107 strsnvisx(visout, sizeof visout, src, sizeof src - 1 /* skip final \0 */,
108 VIS_SAFE | VIS_NOSLASH, "x");
109 printf(format: "strsnvisx: %s\n", visout);
110}
111
112void test_strsenvisx() {
113 char visout[5];
114 char src[] = "\1";
115 strsenvisx(visout, sizeof visout, src, sizeof src - 1 /* skip final \0 */,
116 VIS_SAFE | VIS_NOSLASH, "x", NULL);
117 printf(format: "strsenvisx: %s\n", visout);
118}
119
120void test_unvis() {
121 char visout[5];
122 int ch = toascii(0x1);
123 vis(visout, ch, VIS_SAFE, 0);
124
125 int state = 0;
126 char out;
127 char *p = visout;
128 while ((ch = *(p++)) != '\0') {
129 again:
130 switch (unvis(&out, ch, &state, 0)) {
131 case 0:
132 case UNVIS_NOCHAR:
133 break;
134 case UNVIS_VALID:
135 printf(format: "unvis: %" PRIx8 "\n", (unsigned char)out);
136 break;
137 case UNVIS_VALIDPUSH:
138 printf(format: "unvis: %" PRIx8 "\n", (unsigned char)out);
139 goto again;
140 case UNVIS_SYNBAD:
141 errx(status: 1, format: "Bad character sequence!");
142 }
143 }
144 if (unvis(&out, '\0', &state, UNVIS_END) == UNVIS_VALID)
145 printf(format: "unvis: %" PRIx8 "\n", (unsigned char)out);
146}
147
148void test_strunvis() {
149 char visout[5];
150 int ch = toascii(0x2);
151 vis(visout, ch, VIS_SAFE, 0);
152
153 char p[5];
154 strunvis(p, visout);
155
156 char *pp = p;
157 while ((ch = *(pp++)) != '\0')
158 printf(format: "strunvis: %" PRIx8 "\n", (unsigned char)ch);
159}
160
161void test_strnunvis() {
162 char visout[5];
163 int ch = toascii(0x3);
164 vis(visout, ch, VIS_SAFE, 0);
165
166 char p[5];
167 strnunvis(p, sizeof p, visout);
168
169 char *pp = p;
170 while ((ch = *(pp++)) != '\0')
171 printf(format: "strnunvis: %" PRIx8 "\n", (unsigned char)ch);
172}
173
174void test_strunvisx() {
175 char visout[5];
176 int ch = toascii(0x4);
177 vis(visout, ch, VIS_SAFE, 0);
178
179 char p[5];
180 strunvisx(p, visout, VIS_SAFE);
181
182 char *pp = p;
183 while ((ch = *(pp++)) != '\0')
184 printf(format: "strunvisx: %" PRIx8 "\n", (unsigned char)ch);
185}
186
187void test_strnunvisx() {
188 char visout[5];
189 int ch = toascii(0x5);
190 vis(visout, ch, VIS_SAFE, 0);
191
192 char p[5];
193 strnunvisx(p, sizeof p, visout, VIS_SAFE);
194
195 char *pp = p;
196 while ((ch = *(pp++)) != '\0')
197 printf(format: "strnunvisx: %" PRIx8 "\n", (unsigned char)ch);
198}
199
200int main(void) {
201 printf(format: "vis\n");
202
203 test_vis();
204 test_nvis();
205 test_strvis();
206 test_stravis();
207 test_strnvis();
208 test_strvisx();
209 test_strnvisx();
210 test_strenvisx();
211 test_svis();
212 test_snvis();
213 test_strsvis();
214 test_strsnvis();
215 test_strsvisx();
216 test_strsnvisx();
217 test_strsenvisx();
218 test_unvis();
219 test_strunvis();
220 test_strnunvis();
221 test_strunvisx();
222 test_strnunvisx();
223
224 // CHECK: vis
225 // CHECK: vis: ^A
226 // CHECK: nvis: ^B
227 // CHECK: strvis: ^C
228 // CHECK: stravis: ^D
229 // CHECK: strnvis: ^E
230 // CHECK: strvisx: ^F
231 // CHECK: strnvisx: ^A
232 // CHECK: strenvisx: ^B
233 // CHECK: svis: ^C
234 // CHECK: snvis: ^B
235 // CHECK: strsvis: ^D
236 // CHECK: strsnvis: ^E
237 // CHECK: strsvisx: ^E
238 // CHECK: strsnvisx: ^F
239 // CHECK: strsenvisx: ^A
240 // CHECK: unvis: 1
241 // CHECK: strunvis: 2
242 // CHECK: strnunvis: 3
243 // CHECK: strunvisx: 4
244 // CHECK: strnunvisx: 5
245
246 return 0;
247}
248

source code of compiler-rt/test/sanitizer_common/TestCases/Posix/vis.cpp