1 /****************************************************************************
2 * Copyright (c) 1998-2011,2012 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Thomas E. Dickey <dickey@clark.net> 1998 *
31 ****************************************************************************/
33 #include <curses.priv.h>
38 MODULE_ID("$Id: comp_expand.c,v 1.25 2012/03/24 18:37:17 tom Exp $")
41 trailing_spaces(const char *src)
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))))
52 #define P_LIMIT(p) (length - (size_t)(p))
54 NCURSES_EXPORT(char *)
55 _nc_tic_expand(const char *srcp, bool tic_format, int numbers)
61 const char *str = VALID_STRING(srcp) ? srcp : "\0\0";
62 bool islong = (strlen(str) > 3);
63 size_t need = (2 + strlen(str)) * 4;
75 if (buffer == 0 || need > length) {
76 if ((buffer = typeRealloc(char, length = need, buffer)) == 0)
81 while ((ch = UChar(*str)) != 0) {
82 if (ch == '%' && REALPRINT(str + 1)) {
83 buffer[bufp++] = *str++;
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.
94 && str[2] == S_QUOTE) {
95 _nc_SPRINTF(buffer + bufp, _nc_SLIMIT(P_LIMIT(bufp))
97 bufp += (int) strlen(buffer + bufp);
100 buffer[bufp++] = *str;
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
111 if (str[0] == L_BRACE
112 && isdigit(UChar(str[1]))) {
114 long value = strtol(str + 1, &dst, 0);
118 && value != '\\' /* FIXME */
119 && isprint((int) value)) {
121 buffer[bufp++] = S_QUOTE;
124 buffer[bufp++] = '\\';
125 buffer[bufp++] = (char) ch;
126 buffer[bufp++] = S_QUOTE;
129 buffer[bufp++] = *str;
132 buffer[bufp++] = *str;
136 buffer[bufp++] = *str;
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)
158 && !(ch == '!' && !tic_format)
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';
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';
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))
187 _nc_SPRINTF(&buffer[bufp], _nc_SLIMIT(P_LIMIT(bufp))