1 /****************************************************************************
2 * Copyright (c) 1998-2008,2010 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.21 2010/01/16 17:11:23 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 NCURSES_EXPORT(char *)
53 _nc_tic_expand(const char *srcp
, bool tic_format
, int numbers
)
59 const char *str
= VALID_STRING(srcp
) ? srcp
: "\0\0";
60 bool islong
= (strlen(str
) > 3);
61 size_t need
= (2 + strlen(str
)) * 4;
73 if (buffer
== 0 || need
> length
) {
74 if ((buffer
= typeRealloc(char, length
= need
, buffer
)) == 0)
79 while ((ch
= UChar(*str
)) != 0) {
80 if (ch
== '%' && REALPRINT(str
+ 1)) {
81 buffer
[bufp
++] = *str
++;
83 * Though the character literals are more compact, most
84 * terminal descriptions use numbers and are not easy
85 * to read in character-literal form.
92 && str
[2] == S_QUOTE
) {
93 sprintf(buffer
+ bufp
, "{%d}", str
[1]);
94 bufp
+= (int) strlen(buffer
+ bufp
);
97 buffer
[bufp
++] = *str
;
101 * If we have a "%{number}", try to translate it into
102 * a "%'char'" form, since that will run a little faster
103 * when we're interpreting it. Also, having one form
104 * for the constant makes it simpler to compare terminal
108 if (str
[0] == L_BRACE
109 && isdigit(UChar(str
[1]))) {
111 long value
= strtol(str
+ 1, &dst
, 0);
115 && value
!= '\\' /* FIXME */
116 && isprint((int) value
)) {
118 buffer
[bufp
++] = S_QUOTE
;
121 buffer
[bufp
++] = '\\';
122 buffer
[bufp
++] = (char) ch
;
123 buffer
[bufp
++] = S_QUOTE
;
126 buffer
[bufp
++] = *str
;
129 buffer
[bufp
++] = *str
;
133 buffer
[bufp
++] = *str
;
136 } else if (ch
== 128) {
137 buffer
[bufp
++] = '\\';
138 buffer
[bufp
++] = '0';
139 } else if (ch
== '\033') {
140 buffer
[bufp
++] = '\\';
141 buffer
[bufp
++] = 'E';
142 } else if (ch
== '\\' && tic_format
&& (str
== srcp
|| str
[-1] != '^')) {
143 buffer
[bufp
++] = '\\';
144 buffer
[bufp
++] = '\\';
145 } else if (ch
== ' ' && tic_format
&& (str
== srcp
||
146 trailing_spaces(str
))) {
147 buffer
[bufp
++] = '\\';
148 buffer
[bufp
++] = 's';
149 } else if ((ch
== ',' || ch
== ':' || ch
== '^') && tic_format
) {
150 buffer
[bufp
++] = '\\';
151 buffer
[bufp
++] = (char) ch
;
152 } else if (REALPRINT(str
)
155 && !(ch
== '!' && !tic_format
)
157 buffer
[bufp
++] = (char) ch
;
158 #if 0 /* FIXME: this would be more readable (in fact the whole 'islong' logic should be removed) */
159 else if (ch
== '\b') {
160 buffer
[bufp
++] = '\\';
161 buffer
[bufp
++] = 'b';
162 } else if (ch
== '\f') {
163 buffer
[bufp
++] = '\\';
164 buffer
[bufp
++] = 'f';
165 } else if (ch
== '\t' && islong
) {
166 buffer
[bufp
++] = '\\';
167 buffer
[bufp
++] = 't';
170 else if (ch
== '\r' && (islong
|| (strlen(srcp
) > 2 && str
[1] == '\0'))) {
171 buffer
[bufp
++] = '\\';
172 buffer
[bufp
++] = 'r';
173 } else if (ch
== '\n' && islong
) {
174 buffer
[bufp
++] = '\\';
175 buffer
[bufp
++] = 'n';
177 #define UnCtl(c) ((c) + '@')
178 else if (REALCTL(str
) && ch
!= '\\'
179 && (!islong
|| isdigit(UChar(str
[1])))) {
180 (void) sprintf(&buffer
[bufp
], "^%c", UnCtl(ch
));
183 (void) sprintf(&buffer
[bufp
], "\\%03o", ch
);