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