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