1 6635 ab196087 /* 2 6635 ab196087 * CDDL HEADER START 3 6635 ab196087 * 4 6635 ab196087 * The contents of this file are subject to the terms of the 5 6635 ab196087 * Common Development and Distribution License (the "License"). 6 6635 ab196087 * You may not use this file except in compliance with the License. 7 6635 ab196087 * 8 6635 ab196087 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 6635 ab196087 * or http://www.opensolaris.org/os/licensing. 10 6635 ab196087 * See the License for the specific language governing permissions 11 6635 ab196087 * and limitations under the License. 12 6635 ab196087 * 13 6635 ab196087 * When distributing Covered Code, include this CDDL HEADER in each 14 6635 ab196087 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 6635 ab196087 * If applicable, add the following below this CDDL HEADER, with the 16 6635 ab196087 * fields enclosed by brackets "[]" replaced with your own identifying 17 6635 ab196087 * information: Portions Copyright [yyyy] [name of copyright owner] 18 6635 ab196087 * 19 6635 ab196087 * CDDL HEADER END 20 6635 ab196087 */ 21 6635 ab196087 22 6635 ab196087 /* 23 8747 Ali * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 24 6635 ab196087 * Use is subject to license terms. 25 6635 ab196087 */ 26 6635 ab196087 27 6635 ab196087 28 6635 ab196087 /* 29 6635 ab196087 * Translate a string into C literal string constant notation. 30 6635 ab196087 */ 31 6635 ab196087 32 6635 ab196087 #include <stdio.h> 33 6635 ab196087 #include <ctype.h> 34 6635 ab196087 #include <_conv.h> 35 6635 ab196087 #include <c_literal_msg.h> 36 6635 ab196087 37 6635 ab196087 38 6635 ab196087 /* 39 6635 ab196087 * Convert characters to the form used by the C language to represent 40 6635 ab196087 * literal strings: 41 6635 ab196087 * - Printable characters are shown as themselves 42 6635 ab196087 * - Convert special characters to their 2-character escaped forms: 43 6635 ab196087 * alert (bell) \a 44 6635 ab196087 * backspace \b 45 6635 ab196087 * formfeed \f 46 6635 ab196087 * newline \n 47 6635 ab196087 * return \r 48 6635 ab196087 * horizontal tab \t 49 6635 ab196087 * vertical tab \v 50 6635 ab196087 * backspace \\ 51 6635 ab196087 * single quote \' 52 6635 ab196087 * double quote \" 53 6635 ab196087 * - Display other non-printable characters as 4-character escaped 54 6635 ab196087 * octal constants. 55 6635 ab196087 * 56 6635 ab196087 * entry: 57 6635 ab196087 * buf - Buffer of characters to be processed 58 6635 ab196087 * n # of characters in buf to be processed 59 6635 ab196087 * outfunc - Function to be called to move output characters. 60 6635 ab196087 * uvalue - User value. This argument is passed to outfunc without 61 6635 ab196087 * examination. The caller can use it to pass additional 62 6635 ab196087 * information required by the callback. 63 6635 ab196087 * 64 6635 ab196087 * exit: 65 6635 ab196087 * The string has been processed, with the resulting data passed 66 6635 ab196087 * to outfunc for processing. 67 6635 ab196087 */ 68 6635 ab196087 void 69 6635 ab196087 conv_str_to_c_literal(const char *buf, size_t n, 70 6635 ab196087 Conv_str_to_c_literal_func_t *outfunc, void *uvalue) 71 6635 ab196087 { 72 6635 ab196087 char bs_buf[2]; /* For two-character backslash codes */ 73 6635 ab196087 char octal_buf[10]; /* For \000 style octal constants */ 74 6635 ab196087 75 6635 ab196087 bs_buf[0] = '\\'; 76 6635 ab196087 while (n > 0) { 77 6635 ab196087 switch (*buf) { 78 6635 ab196087 case '\0': 79 6635 ab196087 bs_buf[1] = '0'; 80 6635 ab196087 break; 81 6635 ab196087 case '\a': 82 6635 ab196087 bs_buf[1] = 'a'; 83 6635 ab196087 break; 84 6635 ab196087 case '\b': 85 6635 ab196087 bs_buf[1] = 'b'; 86 6635 ab196087 break; 87 6635 ab196087 case '\f': 88 6635 ab196087 bs_buf[1] = 'f'; 89 6635 ab196087 break; 90 6635 ab196087 case '\n': 91 6635 ab196087 bs_buf[1] = 'n'; 92 6635 ab196087 break; 93 6635 ab196087 case '\r': 94 6635 ab196087 bs_buf[1] = 'r'; 95 6635 ab196087 break; 96 6635 ab196087 case '\t': 97 6635 ab196087 bs_buf[1] = 't'; 98 6635 ab196087 break; 99 6635 ab196087 case '\v': 100 6635 ab196087 bs_buf[1] = 'v'; 101 6635 ab196087 break; 102 6635 ab196087 case '\\': 103 6635 ab196087 bs_buf[1] = '\\'; 104 6635 ab196087 break; 105 6635 ab196087 case '\'': 106 6635 ab196087 bs_buf[1] = '\''; 107 6635 ab196087 break; 108 6635 ab196087 case '"': 109 6635 ab196087 bs_buf[1] = '"'; 110 6635 ab196087 break; 111 6635 ab196087 default: 112 6635 ab196087 bs_buf[1] = '\0'; 113 6635 ab196087 } 114 6635 ab196087 115 6635 ab196087 if (bs_buf[1] != '\0') { 116 6635 ab196087 (*outfunc)(bs_buf, 2, uvalue); 117 6635 ab196087 buf++; 118 6635 ab196087 n--; 119 6635 ab196087 } else if (isprint(*buf)) { 120 6635 ab196087 /* 121 6635 ab196087 * Output the entire sequence of printable 122 6635 ab196087 * characters in a single shot. 123 6635 ab196087 */ 124 6635 ab196087 const char *start = buf; 125 6635 ab196087 size_t outlen = 0; 126 6635 ab196087 127 6635 ab196087 for (start = buf; (n > 0) && isprint(*buf); buf++, n--) 128 6635 ab196087 outlen++; 129 6635 ab196087 (*outfunc)(start, outlen, uvalue); 130 6635 ab196087 } else { 131 6635 ab196087 /* Generic unprintable character: Use octal notation */ 132 6635 ab196087 (void) snprintf(octal_buf, sizeof (octal_buf), 133 8747 Ali MSG_ORIG(MSG_FMT_OCTCONST), (uchar_t)*buf); 134 6635 ab196087 (*outfunc)(octal_buf, strlen(octal_buf), uvalue); 135 6635 ab196087 buf++; 136 6635 ab196087 n--; 137 6635 ab196087 } 138 6635 ab196087 } 139 6635 ab196087 } 140