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