1 /****************************************************************************
2 * Copyright 2020,2021 Thomas E. Dickey *
3 * Copyright 1998-2016,2017 Free Software Foundation, Inc. *
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: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
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. *
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 *
28 ****************************************************************************/
30 /****************************************************************************
31 * Author: Thomas E. Dickey 1998 *
32 ****************************************************************************/
34 #include <curses.priv.h>
39 MODULE_ID("$Id: comp_expand.c,v 1.34 2021/09/04 10:29:15 tom Exp $")
42 #define DEBUG_THIS(p) DEBUG(9, p)
44 #define DEBUG_THIS(p) /* nothing */
48 trailing_spaces(const char *src)
55 /* this deals with differences over whether 0x7f and 0x80..0x9f are controls */
56 #define REALPRINT(s) (UChar(*(s)) < 127 && isprint(UChar(*(s))))
58 #define P_LIMIT(p) (length - (size_t)(p))
60 NCURSES_EXPORT(char *)
61 _nc_tic_expand(const char *srcp, bool tic_format, int numbers)
67 const char *str = VALID_STRING(srcp) ? srcp : "\0\0";
68 size_t need = (2 + strlen(str)) * 4;
74 } fixups[MAX_TC_FIXUPS];
85 if (buffer == 0 || need > length) {
86 if ((buffer = typeRealloc(char, length = need, buffer)) == 0)
90 DEBUG_THIS(("_nc_tic_expand %s:%s:%s",
91 tic_format ? "ti" : "tc",
95 while ((ch = UChar(*str)) != 0) {
96 if (ch == '%' && REALPRINT(str + 1)) {
97 buffer[bufp++] = *str++;
99 * Though the character literals are more compact, most
100 * terminal descriptions use numbers and are not easy
101 * to read in character-literal form.
105 if (str[0] == S_QUOTE
107 && REALPRINT(str + 1)
108 && str[2] == S_QUOTE) {
109 _nc_SPRINTF(buffer + bufp, _nc_SLIMIT(P_LIMIT(bufp))
111 bufp += (int) strlen(buffer + bufp);
114 buffer[bufp++] = *str;
118 * If we have a "%{number}", try to translate it into
119 * a "%'char'" form, since that will run a little faster
120 * when we're interpreting it. Also, having one form
121 * for the constant makes it simpler to compare terminal
125 if (str[0] == L_BRACE
126 && isdigit(UChar(str[1]))) {
128 long value = strtol(str + 1, &dst, 0);
132 && value != '\\' /* FIXME */
133 && isprint((int) value)) {
135 buffer[bufp++] = S_QUOTE;
138 buffer[bufp++] = '\\';
139 buffer[bufp++] = (char) ch;
140 buffer[bufp++] = S_QUOTE;
143 buffer[bufp++] = *str;
146 buffer[bufp++] = *str;
150 if (*str == ',') /* minitel1 uses this */
151 buffer[bufp++] = '\\';
152 buffer[bufp++] = *str;
155 } else if (ch == 128) {
156 buffer[bufp++] = '\\';
157 buffer[bufp++] = '0';
158 } else if (ch == '\033') {
159 buffer[bufp++] = '\\';
160 buffer[bufp++] = 'E';
161 } else if (ch == '\\' && tic_format && (str == srcp || str[-1] != '^')) {
162 buffer[bufp++] = '\\';
163 buffer[bufp++] = '\\';
164 } else if (ch == ' ' && tic_format && (str == srcp ||
165 trailing_spaces(str))) {
166 buffer[bufp++] = '\\';
167 buffer[bufp++] = 's';
168 } else if ((ch == ',' || ch == '^') && tic_format) {
169 buffer[bufp++] = '\\';
170 buffer[bufp++] = (char) ch;
171 } else if (REALPRINT(str)
173 && !(ch == ':' && !tic_format)
174 && !(ch == '!' && !tic_format)
176 buffer[bufp++] = (char) ch;
177 else if (ch == '\r') {
178 buffer[bufp++] = '\\';
179 buffer[bufp++] = 'r';
180 } else if (ch == '\n') {
181 buffer[bufp++] = '\\';
182 buffer[bufp++] = 'n';
184 #define UnCtl(c) ((c) + '@')
185 else if (UChar(ch) < 32
186 && isdigit(UChar(str[1]))) {
187 _nc_SPRINTF(&buffer[bufp], _nc_SLIMIT(P_LIMIT(bufp))
191 _nc_SPRINTF(&buffer[bufp], _nc_SLIMIT(P_LIMIT(bufp))
193 if ((octals < MAX_TC_FIXUPS) &&
194 ((tic_format && (ch == 127)) || ch < 32)) {
195 fixups[octals].ch = UChar(ch);
196 fixups[octals].offset = bufp;
208 * If most of a short string is ASCII control characters, reformat the
209 * string to show those in up-arrow format. For longer strings, it is
210 * more likely that the characters are just binary coding.
212 * If we're formatting termcap, just use the shorter format (up-arrows).
214 if (octals != 0 && (!tic_format || (bufp - (4 * octals)) < MIN_TC_FIXUPS)) {
215 while (--octals >= 0) {
216 char *p = buffer + fixups[octals].offset;
218 *p++ = (char) ((fixups[octals].ch == 127)
220 : (fixups[octals].ch + (int) '@'));
221 while ((p[0] = p[2]) != 0) {
226 DEBUG_THIS(("... %s", _nc_visbuf(buffer)));