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