1 /****************************************************************************
2 * Copyright (c) 1998-2016,2017 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 1998 *
31 ****************************************************************************/
33 #include <curses.priv.h>
38 MODULE_ID("$Id: comp_expand.c,v 1.31 2017/04/20 08:55:08 tom Exp $")
41 #define DEBUG_THIS(p) DEBUG(9, p)
43 #define DEBUG_THIS(p) /* nothing */
47 trailing_spaces(const char *src)
54 /* this deals with differences over whether 0x7f and 0x80..0x9f are controls */
55 #define REALPRINT(s) (UChar(*(s)) < 127 && isprint(UChar(*(s))))
57 #define P_LIMIT(p) (length - (size_t)(p))
59 NCURSES_EXPORT(char *)
60 _nc_tic_expand(const char *srcp, bool tic_format, int numbers)
66 const char *str = VALID_STRING(srcp) ? srcp : "\0\0";
67 size_t need = (2 + strlen(str)) * 4;
73 } fixups[MAX_TC_FIXUPS];
84 if (buffer == 0 || need > length) {
85 if ((buffer = typeRealloc(char, length = need, buffer)) == 0)
89 DEBUG_THIS(("_nc_tic_expand %s", _nc_visbuf(srcp)));
91 while ((ch = UChar(*str)) != 0) {
92 if (ch == '%' && REALPRINT(str + 1)) {
93 buffer[bufp++] = *str++;
95 * Though the character literals are more compact, most
96 * terminal descriptions use numbers and are not easy
97 * to read in character-literal form.
101 if (str[0] == S_QUOTE
103 && REALPRINT(str + 1)
104 && str[2] == S_QUOTE) {
105 _nc_SPRINTF(buffer + bufp, _nc_SLIMIT(P_LIMIT(bufp))
107 bufp += (int) strlen(buffer + bufp);
110 buffer[bufp++] = *str;
114 * If we have a "%{number}", try to translate it into
115 * a "%'char'" form, since that will run a little faster
116 * when we're interpreting it. Also, having one form
117 * for the constant makes it simpler to compare terminal
121 if (str[0] == L_BRACE
122 && isdigit(UChar(str[1]))) {
124 long value = strtol(str + 1, &dst, 0);
128 && value != '\\' /* FIXME */
129 && isprint((int) value)) {
131 buffer[bufp++] = S_QUOTE;
134 buffer[bufp++] = '\\';
135 buffer[bufp++] = (char) ch;
136 buffer[bufp++] = S_QUOTE;
139 buffer[bufp++] = *str;
142 buffer[bufp++] = *str;
146 if (*str == ',') /* minitel1 uses this */
147 buffer[bufp++] = '\\';
148 buffer[bufp++] = *str;
151 } else if (ch == 128) {
152 buffer[bufp++] = '\\';
153 buffer[bufp++] = '0';
154 } else if (ch == '\033') {
155 buffer[bufp++] = '\\';
156 buffer[bufp++] = 'E';
157 } else if (ch == '\\' && tic_format && (str == srcp || str[-1] != '^')) {
158 buffer[bufp++] = '\\';
159 buffer[bufp++] = '\\';
160 } else if (ch == ' ' && tic_format && (str == srcp ||
161 trailing_spaces(str))) {
162 buffer[bufp++] = '\\';
163 buffer[bufp++] = 's';
164 } else if ((ch == ',' || ch == ':' || ch == '^') && tic_format) {
165 buffer[bufp++] = '\\';
166 buffer[bufp++] = (char) ch;
167 } else if (REALPRINT(str)
170 && !(ch == '!' && !tic_format)
172 buffer[bufp++] = (char) ch;
173 else if (ch == '\r') {
174 buffer[bufp++] = '\\';
175 buffer[bufp++] = 'r';
176 } else if (ch == '\n') {
177 buffer[bufp++] = '\\';
178 buffer[bufp++] = 'n';
180 #define UnCtl(c) ((c) + '@')
181 else if (UChar(ch) < 32
182 && isdigit(UChar(str[1]))) {
183 _nc_SPRINTF(&buffer[bufp], _nc_SLIMIT(P_LIMIT(bufp))
187 _nc_SPRINTF(&buffer[bufp], _nc_SLIMIT(P_LIMIT(bufp))
189 if ((octals < MAX_TC_FIXUPS) &&
190 ((tic_format && (ch == 127)) || ch < 32)) {
191 fixups[octals].ch = UChar(ch);
192 fixups[octals].offset = bufp;
204 * If most of a short string is ASCII control characters, reformat the
205 * string to show those in up-arrow format. For longer strings, it's
206 * more likely that the characters are just binary coding.
208 * If we're formatting termcap, just use the shorter format (up-arrows).
210 if (octals != 0 && (!tic_format || (bufp - (4 * octals)) < MIN_TC_FIXUPS)) {
211 while (--octals >= 0) {
212 char *p = buffer + fixups[octals].offset;
214 *p++ = (char) ((fixups[octals].ch == 127)
216 : (fixups[octals].ch + (int) '@'));
217 while ((p[0] = p[2]) != 0) {
222 DEBUG_THIS(("... %s", _nc_visbuf(buffer)));