comp_expand.c revision 76726
1/**************************************************************************** 2 * Copyright (c) 1998,2000 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 38MODULE_ID("$Id: comp_expand.c,v 1.15 2000/12/10 01:30:10 tom Exp $") 39 40static int 41trailing_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 CHAR_OF(s) (*(unsigned const char *)(s)) 50#define REALCTL(s) (CHAR_OF(s) < 127 && iscntrl(CHAR_OF(s))) 51#define REALPRINT(s) (CHAR_OF(s) < 127 && isprint(CHAR_OF(s))) 52 53NCURSES_EXPORT(char *) 54_nc_tic_expand 55(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 : ""; 62 bool islong = (strlen(str) > 3); 63 size_t need = (2 + strlen(str)) * 4; 64 int ch; 65 66 if (buffer == 0 || need > length) { 67 if ((buffer = typeRealloc(char, length = need, buffer)) == 0) 68 return 0; 69 } 70 71 bufp = 0; 72 while ((ch = CharOf(*str)) != 0) { 73 if (ch == '%' && REALPRINT(str + 1)) { 74 buffer[bufp++] = *str++; 75 /* 76 * Though the character literals are more compact, most 77 * terminal descriptions use numbers and are not easy 78 * to read in character-literal form. 79 */ 80 switch (numbers) { 81 case -1: 82 if (str[0] == S_QUOTE 83 && str[1] != '\\' 84 && REALPRINT(str + 1) 85 && str[2] == S_QUOTE) { 86 sprintf(buffer + bufp, "{%d}", str[1]); 87 bufp += strlen(buffer + bufp); 88 str += 2; 89 } else { 90 buffer[bufp++] = *str; 91 } 92 break; 93 /* 94 * If we have a "%{number}", try to translate it into 95 * a "%'char'" form, since that will run a little faster 96 * when we're interpreting it. Also, having one form 97 * for the constant makes it simpler to compare terminal 98 * descriptions. 99 */ 100 case 1: 101 if (str[0] == L_BRACE 102 && isdigit(CharOf(str[1]))) { 103 char *dst = 0; 104 long value = strtol(str + 1, &dst, 0); 105 if (dst != 0 106 && *dst == R_BRACE 107 && value < 127 108 && value != '\\' /* FIXME */ 109 && isprint((int) value)) { 110 ch = (int) value; 111 buffer[bufp++] = S_QUOTE; 112 if (ch == '\\' 113 || ch == S_QUOTE) 114 buffer[bufp++] = '\\'; 115 buffer[bufp++] = ch; 116 buffer[bufp++] = S_QUOTE; 117 str = dst; 118 } else { 119 buffer[bufp++] = *str; 120 } 121 } else { 122 buffer[bufp++] = *str; 123 } 124 break; 125 default: 126 buffer[bufp++] = *str; 127 break; 128 } 129 } else if (ch == 128) { 130 buffer[bufp++] = '\\'; 131 buffer[bufp++] = '0'; 132 } else if (ch == '\033') { 133 buffer[bufp++] = '\\'; 134 buffer[bufp++] = 'E'; 135 } else if (ch == '\\' && tic_format && (str == srcp || str[-1] != '^')) { 136 buffer[bufp++] = '\\'; 137 buffer[bufp++] = '\\'; 138 } else if (ch == ' ' && tic_format && (str == srcp || 139 trailing_spaces(str))) { 140 buffer[bufp++] = '\\'; 141 buffer[bufp++] = 's'; 142 } else if ((ch == ',' || ch == ':' || ch == '^') && tic_format) { 143 buffer[bufp++] = '\\'; 144 buffer[bufp++] = ch; 145 } else if (REALPRINT(str) 146 && (ch != ',' 147 && ch != ':' 148 && !(ch == '!' && !tic_format) 149 && ch != '^')) 150 buffer[bufp++] = ch; 151#if 0 /* FIXME: this would be more readable (in fact the whole 'islong' logic should be removed) */ 152 else if (ch == '\b') { 153 buffer[bufp++] = '\\'; 154 buffer[bufp++] = 'b'; 155 } else if (ch == '\f') { 156 buffer[bufp++] = '\\'; 157 buffer[bufp++] = 'f'; 158 } else if (ch == '\t' && islong) { 159 buffer[bufp++] = '\\'; 160 buffer[bufp++] = 't'; 161 } 162#endif 163 else if (ch == '\r' && (islong || (strlen(srcp) > 2 && str[1] == '\0'))) { 164 buffer[bufp++] = '\\'; 165 buffer[bufp++] = 'r'; 166 } else if (ch == '\n' && islong) { 167 buffer[bufp++] = '\\'; 168 buffer[bufp++] = 'n'; 169 } 170#define UnCtl(c) ((c) + '@') 171 else if (REALCTL(str) && ch != '\\' 172 && (!islong || isdigit(CharOf(str[1])))) { 173 (void) sprintf(&buffer[bufp], "^%c", UnCtl(ch)); 174 bufp += 2; 175 } else { 176 (void) sprintf(&buffer[bufp], "\\%03o", ch); 177 bufp += 4; 178 } 179 180 str++; 181 } 182 183 buffer[bufp] = '\0'; 184 return (buffer); 185} 186