blob: 6e79a926b5ac59f33d738523676a3cf654c93449 [file] [log] [blame]
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301/****************************************************************************
2 * Copyright (c) 1998-2007,2008 Free Software Foundation, Inc. *
3 * *
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: *
11 * *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
14 * *
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. *
22 * *
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 *
26 * authorization. *
27 ****************************************************************************/
28
29/****************************************************************************
30 * Author: Thomas E. Dickey <dickey@clark.net> 1998 *
31 ****************************************************************************/
32
33#include <curses.priv.h>
34
35#include <ctype.h>
36#include <tic.h>
37
38MODULE_ID("$Id: comp_expand.c,v 1.20 2008/08/16 19:29:42 tom Exp $")
39
40static int
41trailing_spaces(const char *src)
42{
43 while (*src == ' ')
44 src++;
45 return *src == 0;
46}
47
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))))
51
52NCURSES_EXPORT(char *)
53_nc_tic_expand(const char *srcp, bool tic_format, int numbers)
54{
55 static char *buffer;
56 static size_t length;
57
58 int bufp;
59 const char *str = VALID_STRING(srcp) ? srcp : "\0\0";
60 bool islong = (strlen(str) > 3);
61 size_t need = (2 + strlen(str)) * 4;
62 int ch;
63
64#if NO_LEAKS
65 if (srcp == 0) {
66 if (buffer != 0) {
67 FreeAndNull(buffer);
68 length = 0;
69 }
70 return 0;
71 }
72#endif
73 if (buffer == 0 || need > length) {
74 if ((buffer = typeRealloc(char, length = need, buffer)) == 0)
75 return 0;
76 }
77
78 bufp = 0;
79 while ((ch = UChar(*str)) != 0) {
80 if (ch == '%' && REALPRINT(str + 1)) {
81 buffer[bufp++] = *str++;
82 /*
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.
86 */
87 switch (numbers) {
88 case -1:
89 if (str[0] == S_QUOTE
90 && str[1] != '\\'
91 && REALPRINT(str + 1)
92 && str[2] == S_QUOTE) {
93 sprintf(buffer + bufp, "{%d}", str[1]);
94 bufp += strlen(buffer + bufp);
95 str += 2;
96 } else {
97 buffer[bufp++] = *str;
98 }
99 break;
100 /*
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
105 * descriptions.
106 */
107 case 1:
108 if (str[0] == L_BRACE
109 && isdigit(UChar(str[1]))) {
110 char *dst = 0;
111 long value = strtol(str + 1, &dst, 0);
112 if (dst != 0
113 && *dst == R_BRACE
114 && value < 127
115 && value != '\\' /* FIXME */
116 && isprint((int) value)) {
117 ch = (int) value;
118 buffer[bufp++] = S_QUOTE;
119 if (ch == '\\'
120 || ch == S_QUOTE)
121 buffer[bufp++] = '\\';
122 buffer[bufp++] = (char) ch;
123 buffer[bufp++] = S_QUOTE;
124 str = dst;
125 } else {
126 buffer[bufp++] = *str;
127 }
128 } else {
129 buffer[bufp++] = *str;
130 }
131 break;
132 default:
133 buffer[bufp++] = *str;
134 break;
135 }
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)
153 && (ch != ','
154 && ch != ':'
155 && !(ch == '!' && !tic_format)
156 && ch != '^'))
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';
168 }
169#endif
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';
176 }
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));
181 bufp += 2;
182 } else {
183 (void) sprintf(&buffer[bufp], "\\%03o", ch);
184 bufp += 4;
185 }
186
187 str++;
188 }
189
190 buffer[bufp] = '\0';
191 return (buffer);
192}