4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
29 * Translate a string into C literal string constant notation.
35 #include <c_literal_msg.h>
39 * Convert characters to the form used by the C language to represent
41 * - Printable characters are shown as themselves
42 * - Convert special characters to their 2-character escaped forms:
53 * - Display other non-printable characters as 4-character escaped
57 * buf - Buffer of characters to be processed
58 * n # of characters in buf to be processed
59 * outfunc - Function to be called to move output characters.
60 * uvalue - User value. This argument is passed to outfunc without
61 * examination. The caller can use it to pass additional
62 * information required by the callback.
65 * The string has been processed, with the resulting data passed
66 * to outfunc for processing.
69 conv_str_to_c_literal(const char *buf
, size_t n
,
70 Conv_str_to_c_literal_func_t
*outfunc
, void *uvalue
)
72 char bs_buf
[2]; /* For two-character backslash codes */
73 char octal_buf
[10]; /* For \000 style octal constants */
115 if (bs_buf
[1] != '\0') {
116 (*outfunc
)(bs_buf
, 2, uvalue
);
119 } else if (isprint(*buf
)) {
121 * Output the entire sequence of printable
122 * characters in a single shot.
124 const char *start
= buf
;
127 for (start
= buf
; (n
> 0) && isprint(*buf
); buf
++, n
--)
129 (*outfunc
)(start
, outlen
, uvalue
);
131 /* Generic unprintable character: Use octal notation */
132 (void) snprintf(octal_buf
, sizeof (octal_buf
),
133 MSG_ORIG(MSG_FMT_OCTCONST
), (uchar_t
)*buf
);
134 (*outfunc
)(octal_buf
, strlen(octal_buf
), uvalue
);
142 * Given the pointer to the character following a '\' character in
143 * a C style literal, return the ASCII character code it represents,
144 * and advance the string pointer to the character following the last
145 * character in the escape sequence.
148 * str - Address of string pointer to first character following
152 * If the character is not valid, -1 is returned. Otherwise
153 * it returns the ASCII code for the translated character, and
154 * *str has been advanced.
157 conv_translate_c_esc(char **str
)
194 /* Octal constant: There can be up to 3 digits */
196 for (i
= 0; i
< 2; i
++) {
197 if ((*s
< '0') || (*s
> '7'))
199 ch
= (ch
<< 3) + (*s
++ - '0');
204 * There are some cases where ch already has the desired value.
205 * These cases exist simply to remove the special meaning that
206 * character would otherwise have. We need to match them to
207 * prevent them from falling into the default error case.