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
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2010 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Translate a string into C literal string constant notation.
*/
#include <stdio.h>
#include <ctype.h>
#include <_conv.h>
#include <c_literal_msg.h>
/*
* Convert characters to the form used by the C language to represent
* literal strings:
* - Printable characters are shown as themselves
* - Convert special characters to their 2-character escaped forms:
* alert (bell) \a
* backspace \b
* formfeed \f
* newline \n
* return \r
* horizontal tab \t
* vertical tab \v
* backspace \\
* single quote \'
* double quote \"
* - Display other non-printable characters as 4-character escaped
* octal constants.
*
* entry:
* buf - Buffer of characters to be processed
* n # of characters in buf to be processed
* outfunc - Function to be called to move output characters.
* uvalue - User value. This argument is passed to outfunc without
* examination. The caller can use it to pass additional
* information required by the callback.
*
* exit:
* The string has been processed, with the resulting data passed
* to outfunc for processing.
*/
void
conv_str_to_c_literal(const char *buf, size_t n,
Conv_str_to_c_literal_func_t *outfunc, void *uvalue)
{
char bs_buf[2]; /* For two-character backslash codes */
char octal_buf[10]; /* For \000 style octal constants */
bs_buf[0] = '\\';
while (n > 0) {
switch (*buf) {
case '\0':
bs_buf[1] = '0';
break;
case '\a':
bs_buf[1] = 'a';
break;
case '\b':
bs_buf[1] = 'b';
break;
case '\f':
bs_buf[1] = 'f';
break;
case '\n':
bs_buf[1] = 'n';
break;
case '\r':
bs_buf[1] = 'r';
break;
case '\t':
bs_buf[1] = 't';
break;
case '\v':
bs_buf[1] = 'v';
break;
case '\\':
bs_buf[1] = '\\';
break;
case '\'':
bs_buf[1] = '\'';
break;
case '"':
bs_buf[1] = '"';
break;
default:
bs_buf[1] = '\0';
}
if (bs_buf[1] != '\0') {
(*outfunc)(bs_buf, 2, uvalue);
buf++;
n--;
} else if (isprint(*buf)) {
/*
* Output the entire sequence of printable
* characters in a single shot.
*/
const char *start = buf;
size_t outlen = 0;
for (start = buf; (n > 0) && isprint(*buf); buf++, n--)
outlen++;
(*outfunc)(start, outlen, uvalue);
} else {
/* Generic unprintable character: Use octal notation */
(void) snprintf(octal_buf, sizeof (octal_buf),
MSG_ORIG(MSG_FMT_OCTCONST), (uchar_t)*buf);
(*outfunc)(octal_buf, strlen(octal_buf), uvalue);
buf++;
n--;
}
}
}
/*
* Given the pointer to the character following a '\' character in
* a C style literal, return the ASCII character code it represents,
* and advance the string pointer to the character following the last
* character in the escape sequence.
*
* entry:
* str - Address of string pointer to first character following
* the backslash.
*
* exit:
* If the character is not valid, -1 is returned. Otherwise
* it returns the ASCII code for the translated character, and
* *str has been advanced.
*/
int
conv_translate_c_esc(char **str)
{
char *s = *str;
int ch, i;
ch = *s++;
switch (ch) {
case 'a':
ch = '\a';
break;
case 'b':
ch = '\b';
break;
case 'f':
ch = '\f';
break;
case 'n':
ch = '\n';
break;
case 'r':
ch = '\r';
break;
case 't':
ch = '\t';
break;
case 'v':
ch = '\v';
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
/* Octal constant: There can be up to 3 digits */
ch -= '0';
for (i = 0; i < 2; i++) {
if ((*s < '0') || (*s > '7'))
break;
ch = (ch << 3) + (*s++ - '0');
}
break;
/*
* There are some cases where ch already has the desired value.
* These cases exist simply to remove the special meaning that
* character would otherwise have. We need to match them to
* prevent them from falling into the default error case.
*/
case '\\':
case '\'':
case '"':
break;
default:
ch = -1;
break;
}
*str = s;
return (ch);
}
|