9
9
/* VSTRING *unescape(result, input)
10
10
/* VSTRING *result;
11
11
/* const char *input;
13
/* VSTRING *escape(result, input, len)
13
18
/* unescape() translates C-like escape sequences in the null-terminated
14
19
/* string \fIinput\fR and places the result in \fIresult\fR. The result
15
20
/* is null-terminated, and is the function result value.
22
/* escape() does the reverse transformation.
17
24
/* Escape sequences and their translations:
131
/* escape - reverse transformation */
133
VSTRING *escape(VSTRING *result, const char *data, ssize_t len)
137
VSTRING_RESET(result);
143
VSTRING_ADDCH(result, ch);
144
VSTRING_ADDCH(result, ch);
146
} else if (ch == '\a') { /* \a -> audible bell */
147
vstring_strcat(result, "\\a");
149
} else if (ch == '\b') { /* \b -> backspace */
150
vstring_strcat(result, "\\b");
152
} else if (ch == '\f') { /* \f -> formfeed */
153
vstring_strcat(result, "\\f");
155
} else if (ch == '\n') { /* \n -> newline */
156
vstring_strcat(result, "\\n");
158
} else if (ch == '\r') { /* \r -> carriagereturn */
159
vstring_strcat(result, "\\r");
161
} else if (ch == '\t') { /* \t -> horizontal tab */
162
vstring_strcat(result, "\\t");
164
} else if (ch == '\v') { /* \v -> vertical tab */
165
vstring_strcat(result, "\\v");
169
if (ISDIGIT(*UCHAR(data)))
170
vstring_sprintf_append(result, "\\%03d", ch);
172
vstring_sprintf_append(result, "\\%d", ch);
174
VSTRING_TERMINATE(result);
126
183
#include <vstring_vstream.h>
128
int main(int unused_argc, char **unused_argv)
185
int main(int argc, char **argv)
130
187
VSTRING *in = vstring_alloc(10);
131
188
VSTRING *out = vstring_alloc(10);
133
while (vstring_fgets_nonl(in, VSTREAM_IN)) {
134
unescape(out, vstring_str(in));
135
vstream_fwrite(VSTREAM_OUT, vstring_str(out), VSTRING_LEN(out));
191
if (argc > 2 || (argc > 1 && (un_escape = strcmp(argv[1], "-e"))) != 0)
192
msg_fatal("usage: %s [-e (escape)]", argv[0]);
195
while (vstring_fgets_nonl(in, VSTREAM_IN)) {
196
unescape(out, vstring_str(in));
197
vstream_fwrite(VSTREAM_OUT, vstring_str(out), VSTRING_LEN(out));
200
while (vstring_fgets(in, VSTREAM_IN)) {
201
escape(out, vstring_str(in), VSTRING_LEN(in));
202
vstream_fwrite(VSTREAM_OUT, vstring_str(out), VSTRING_LEN(out));
137
205
vstream_fflush(VSTREAM_OUT);