1 /****************************************************************************
2 * Copyright (c) 1998 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.11 1999/03/07 00:51:07 tom Exp $")
40 static int trailing_spaces(const char *src)
47 /* this deals with differences over whether 0x7f and 0x80..0x9f are controls */
48 #define CHAR_OF(s) (*(unsigned const char *)(s))
49 #define REALCTL(s) (CHAR_OF(s) < 127 && iscntrl(CHAR_OF(s)))
50 #define REALPRINT(s) (CHAR_OF(s) < 127 && isprint(CHAR_OF(s)))
52 char *_nc_tic_expand(const char *srcp, bool tic_format, int numbers)
58 const char *ptr, *str = VALID_STRING(srcp) ? srcp : "";
59 bool islong = (strlen(str) > 3);
60 size_t need = (2 + strlen(str)) * 4;
63 if (buffer == 0 || need > length) {
64 if ((buffer = typeRealloc(char, length = need, buffer)) == 0)
70 while ((ch = (*str & 0xff)) != 0) {
71 if (ch == '%' && REALPRINT(str+1)) {
72 buffer[bufp++] = *str++;
74 * Though the character literals are more compact, most
75 * terminal descriptions use numbers and are not easy
76 * to read in character-literal form.
83 && str[2] == S_QUOTE) {
84 sprintf(buffer+bufp, "{%d}", str[1]);
85 bufp += strlen(buffer+bufp);
88 buffer[bufp++] = *str;
92 * If we have a "%{number}", try to translate it into
93 * a "%'char'" form, since that will run a little faster
94 * when we're interpreting it. Also, having one form
95 * for the constant makes it simpler to compare terminal
100 && isdigit(str[1])) {
102 long value = strtol(str+1, &dst, 0);
106 && value != '\\' /* FIXME */
107 && isprint((int)value)) {
109 buffer[bufp++] = S_QUOTE;
112 buffer[bufp++] = '\\';
114 buffer[bufp++] = S_QUOTE;
117 buffer[bufp++] = *str;
120 buffer[bufp++] = *str;
124 buffer[bufp++] = *str;
128 else if (ch == 128) {
129 buffer[bufp++] = '\\';
130 buffer[bufp++] = '0';
132 else if (ch == '\033') {
133 buffer[bufp++] = '\\';
134 buffer[bufp++] = 'E';
136 else if (ch == '\\' && tic_format && (str == srcp || str[-1] != '^')) {
137 buffer[bufp++] = '\\';
138 buffer[bufp++] = '\\';
140 else if (ch == ' ' && tic_format && (str == srcp || trailing_spaces(str))) {
141 buffer[bufp++] = '\\';
142 buffer[bufp++] = 's';
144 else if ((ch == ',' || ch == ':' || ch == '^') && tic_format) {
145 buffer[bufp++] = '\\';
148 else if (REALPRINT(str) && (ch != ',' && ch != ':' && !(ch == '!' && !tic_format) && ch != '^'))
150 #if 0 /* FIXME: this would be more readable (in fact the whole 'islong' logic should be removed) */
151 else if (ch == '\b') {
152 buffer[bufp++] = '\\';
153 buffer[bufp++] = 'b';
155 else if (ch == '\f') {
156 buffer[bufp++] = '\\';
157 buffer[bufp++] = 'f';
159 else if (ch == '\t' && islong) {
160 buffer[bufp++] = '\\';
161 buffer[bufp++] = 't';
164 else if (ch == '\r' && (islong || (strlen(srcp) > 2 && str[1] == '\0'))) {
165 buffer[bufp++] = '\\';
166 buffer[bufp++] = 'r';
168 else if (ch == '\n' && islong) {
169 buffer[bufp++] = '\\';
170 buffer[bufp++] = 'n';
172 #define UnCtl(c) ((c) + '@')
173 else if (REALCTL(str) && ch != '\\' && (!islong || isdigit(str[1])))
175 (void) sprintf(&buffer[bufp], "^%c", UnCtl(ch));
180 (void) sprintf(&buffer[bufp], "\\%03o", ch);