1 /**************************************************************************** 2 * Copyright (c) 1998-2011,2012 Free Software Foundation, Inc. * 3 * * 4 * Permission is hereby granted, free of charge, to any person obtaining a * 5 * copy of this software and associated documentation files (the * 6 * "Software"), to deal in the Software without restriction, including * 7 * without limitation the rights to use, copy, modify, merge, publish, * 8 * distribute, distribute with modifications, sublicense, and/or sell * 9 * copies of the Software, and to permit persons to whom the Software is * 10 * furnished to do so, subject to the following conditions: * 11 * * 12 * The above copyright notice and this permission notice shall be included * 13 * in all copies or substantial portions of the Software. * 14 * * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * 16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * 17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * 18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * 21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * 22 * * 23 * Except as contained in this notice, the name(s) of the above copyright * 24 * holders shall not be used in advertising or otherwise to promote the * 25 * sale, use or other dealings in this Software without prior written * 26 * authorization. * 27 ****************************************************************************/ 28 29 /**************************************************************************** 30 * Author: Thomas E. Dickey <dickey@clark.net> 1998 * 31 ****************************************************************************/ 32 33 #include <curses.priv.h> 34 35 #include <ctype.h> 36 #include <tic.h> 37 38 MODULE_ID("$Id: comp_expand.c,v 1.25 2012/03/24 18:37:17 tom Exp $") 39 40 static int 41 trailing_spaces(const char *src) 42 { 43 while (*src == ' ') 44 src++; 45 return *src == 0; 46 } 47 48 /* this deals with differences over whether 0x7f and 0x80..0x9f are controls */ 49 #define REALCTL(s) (UChar(*(s)) < 127 && iscntrl(UChar(*(s)))) 50 #define REALPRINT(s) (UChar(*(s)) < 127 && isprint(UChar(*(s)))) 51 52 #define P_LIMIT(p) (length - (size_t)(p)) 53 54 NCURSES_EXPORT(char *) 55 _nc_tic_expand(const char *srcp, bool tic_format, int numbers) 56 { 57 static char *buffer; 58 static size_t length; 59 60 int bufp; 61 const char *str = VALID_STRING(srcp) ? srcp : "\0\0"; 62 bool islong = (strlen(str) > 3); 63 size_t need = (2 + strlen(str)) * 4; 64 int ch; 65 66 if (srcp == 0) { 67 #if NO_LEAKS 68 if (buffer != 0) { 69 FreeAndNull(buffer); 70 length = 0; 71 } 72 #endif 73 return 0; 74 } 75 if (buffer == 0 || need > length) { 76 if ((buffer = typeRealloc(char, length = need, buffer)) == 0) 77 return 0; 78 } 79 80 bufp = 0; 81 while ((ch = UChar(*str)) != 0) { 82 if (ch == '%' && REALPRINT(str + 1)) { 83 buffer[bufp++] = *str++; 84 /* 85 * Though the character literals are more compact, most 86 * terminal descriptions use numbers and are not easy 87 * to read in character-literal form. 88 */ 89 switch (numbers) { 90 case -1: 91 if (str[0] == S_QUOTE 92 && str[1] != '\\' 93 && REALPRINT(str + 1) 94 && str[2] == S_QUOTE) { 95 _nc_SPRINTF(buffer + bufp, _nc_SLIMIT(P_LIMIT(bufp)) 96 "{%d}", str[1]); 97 bufp += (int) strlen(buffer + bufp); 98 str += 2; 99 } else { 100 buffer[bufp++] = *str; 101 } 102 break; 103 /* 104 * If we have a "%{number}", try to translate it into 105 * a "%'char'" form, since that will run a little faster 106 * when we're interpreting it. Also, having one form 107 * for the constant makes it simpler to compare terminal 108 * descriptions. 109 */ 110 case 1: 111 if (str[0] == L_BRACE 112 && isdigit(UChar(str[1]))) { 113 char *dst = 0; 114 long value = strtol(str + 1, &dst, 0); 115 if (dst != 0 116 && *dst == R_BRACE 117 && value < 127 118 && value != '\\' /* FIXME */ 119 && isprint((int) value)) { 120 ch = (int) value; 121 buffer[bufp++] = S_QUOTE; 122 if (ch == '\\' 123 || ch == S_QUOTE) 124 buffer[bufp++] = '\\'; 125 buffer[bufp++] = (char) ch; 126 buffer[bufp++] = S_QUOTE; 127 str = dst; 128 } else { 129 buffer[bufp++] = *str; 130 } 131 } else { 132 buffer[bufp++] = *str; 133 } 134 break; 135 default: 136 buffer[bufp++] = *str; 137 break; 138 } 139 } else if (ch == 128) { 140 buffer[bufp++] = '\\'; 141 buffer[bufp++] = '0'; 142 } else if (ch == '\033') { 143 buffer[bufp++] = '\\'; 144 buffer[bufp++] = 'E'; 145 } else if (ch == '\\' && tic_format && (str == srcp || str[-1] != '^')) { 146 buffer[bufp++] = '\\'; 147 buffer[bufp++] = '\\'; 148 } else if (ch == ' ' && tic_format && (str == srcp || 149 trailing_spaces(str))) { 150 buffer[bufp++] = '\\'; 151 buffer[bufp++] = 's'; 152 } else if ((ch == ',' || ch == ':' || ch == '^') && tic_format) { 153 buffer[bufp++] = '\\'; 154 buffer[bufp++] = (char) ch; 155 } else if (REALPRINT(str) 156 && (ch != ',' 157 && ch != ':' 158 && !(ch == '!' && !tic_format) 159 && ch != '^')) 160 buffer[bufp++] = (char) ch; 161 #if 0 /* FIXME: this would be more readable (in fact the whole 'islong' logic should be removed) */ 162 else if (ch == '\b') { 163 buffer[bufp++] = '\\'; 164 buffer[bufp++] = 'b'; 165 } else if (ch == '\f') { 166 buffer[bufp++] = '\\'; 167 buffer[bufp++] = 'f'; 168 } else if (ch == '\t' && islong) { 169 buffer[bufp++] = '\\'; 170 buffer[bufp++] = 't'; 171 } 172 #endif 173 else if (ch == '\r' && (islong || (strlen(srcp) > 2 && str[1] == '\0'))) { 174 buffer[bufp++] = '\\'; 175 buffer[bufp++] = 'r'; 176 } else if (ch == '\n' && islong) { 177 buffer[bufp++] = '\\'; 178 buffer[bufp++] = 'n'; 179 } 180 #define UnCtl(c) ((c) + '@') 181 else if (REALCTL(str) && ch != '\\' 182 && (!islong || isdigit(UChar(str[1])))) { 183 _nc_SPRINTF(&buffer[bufp], _nc_SLIMIT(P_LIMIT(bufp)) 184 "^%c", UnCtl(ch)); 185 bufp += 2; 186 } else { 187 _nc_SPRINTF(&buffer[bufp], _nc_SLIMIT(P_LIMIT(bufp)) 188 "\\%03o", ch); 189 bufp += 4; 190 } 191 192 str++; 193 } 194 195 buffer[bufp] = '\0'; 196 return (buffer); 197 } 198