]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/comp_expand.c
ncurses 6.0 - patch 20160924
[ncurses.git] / ncurses / tinfo / comp_expand.c
1 /****************************************************************************
2  * Copyright (c) 1998-2012,2016 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
38 MODULE_ID("$Id: comp_expand.c,v 1.26 2016/09/24 21:15:51 tom Exp $")
39
40 static int
41 trailing_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
52 #define P_LIMIT(p)   (length - (size_t)(p))
53
54 NCURSES_EXPORT(char *)
55 _nc_tic_expand(const char *srcp, bool tic_format, int numbers)
56 {
57     static char *buffer;
58     static size_t length;
59
60     int bufp;
61     const char *str = VALID_STRING(srcp) ? srcp : "\0\0";
62     bool islong = (strlen(str) > 3);
63     size_t need = (2 + strlen(str)) * 4;
64     int ch;
65
66     if (srcp == 0) {
67 #if NO_LEAKS
68         if (buffer != 0) {
69             FreeAndNull(buffer);
70             length = 0;
71         }
72 #endif
73         return 0;
74     }
75     if (buffer == 0 || need > length) {
76         if ((buffer = typeRealloc(char, length = need, buffer)) == 0)
77               return 0;
78     }
79
80     bufp = 0;
81     while ((ch = UChar(*str)) != 0) {
82         if (ch == '%' && REALPRINT(str + 1)) {
83             buffer[bufp++] = *str++;
84             /*
85              * Though the character literals are more compact, most
86              * terminal descriptions use numbers and are not easy
87              * to read in character-literal form.
88              */
89             switch (numbers) {
90             case -1:
91                 if (str[0] == S_QUOTE
92                     && str[1] != '\\'
93                     && REALPRINT(str + 1)
94                     && str[2] == S_QUOTE) {
95                     _nc_SPRINTF(buffer + bufp, _nc_SLIMIT(P_LIMIT(bufp))
96                                 "{%d}", str[1]);
97                     bufp += (int) strlen(buffer + bufp);
98                     str += 2;
99                 } else {
100                     buffer[bufp++] = *str;
101                 }
102                 break;
103                 /*
104                  * If we have a "%{number}", try to translate it into
105                  * a "%'char'" form, since that will run a little faster
106                  * when we're interpreting it.  Also, having one form
107                  * for the constant makes it simpler to compare terminal
108                  * descriptions.
109                  */
110             case 1:
111                 if (str[0] == L_BRACE
112                     && isdigit(UChar(str[1]))) {
113                     char *dst = 0;
114                     long value = strtol(str + 1, &dst, 0);
115                     if (dst != 0
116                         && *dst == R_BRACE
117                         && value < 127
118                         && value != '\\'        /* FIXME */
119                         && isprint((int) value)) {
120                         ch = (int) value;
121                         buffer[bufp++] = S_QUOTE;
122                         if (ch == '\\'
123                             || ch == S_QUOTE)
124                             buffer[bufp++] = '\\';
125                         buffer[bufp++] = (char) ch;
126                         buffer[bufp++] = S_QUOTE;
127                         str = dst;
128                     } else {
129                         buffer[bufp++] = *str;
130                     }
131                 } else {
132                     buffer[bufp++] = *str;
133                 }
134                 break;
135             default:
136                 if (*str == ',')        /* minitel1 uses this */
137                     buffer[bufp++] = '\\';
138                 buffer[bufp++] = *str;
139                 break;
140             }
141         } else if (ch == 128) {
142             buffer[bufp++] = '\\';
143             buffer[bufp++] = '0';
144         } else if (ch == '\033') {
145             buffer[bufp++] = '\\';
146             buffer[bufp++] = 'E';
147         } else if (ch == '\\' && tic_format && (str == srcp || str[-1] != '^')) {
148             buffer[bufp++] = '\\';
149             buffer[bufp++] = '\\';
150         } else if (ch == ' ' && tic_format && (str == srcp ||
151                                                trailing_spaces(str))) {
152             buffer[bufp++] = '\\';
153             buffer[bufp++] = 's';
154         } else if ((ch == ',' || ch == ':' || ch == '^') && tic_format) {
155             buffer[bufp++] = '\\';
156             buffer[bufp++] = (char) ch;
157         } else if (REALPRINT(str)
158                    && (ch != ','
159                        && ch != ':'
160                        && !(ch == '!' && !tic_format)
161                        && ch != '^'))
162             buffer[bufp++] = (char) ch;
163 #if 0                           /* FIXME: this would be more readable (in fact the whole 'islong' logic should be removed) */
164         else if (ch == '\b') {
165             buffer[bufp++] = '\\';
166             buffer[bufp++] = 'b';
167         } else if (ch == '\f') {
168             buffer[bufp++] = '\\';
169             buffer[bufp++] = 'f';
170         } else if (ch == '\t' && islong) {
171             buffer[bufp++] = '\\';
172             buffer[bufp++] = 't';
173         }
174 #endif
175         else if (ch == '\r' && (islong || (strlen(srcp) > 2 && str[1] == '\0'))) {
176             buffer[bufp++] = '\\';
177             buffer[bufp++] = 'r';
178         } else if (ch == '\n' && islong) {
179             buffer[bufp++] = '\\';
180             buffer[bufp++] = 'n';
181         }
182 #define UnCtl(c) ((c) + '@')
183         else if (REALCTL(str) && ch != '\\'
184                  && (!islong || isdigit(UChar(str[1])))) {
185             _nc_SPRINTF(&buffer[bufp], _nc_SLIMIT(P_LIMIT(bufp))
186                         "^%c", UnCtl(ch));
187             bufp += 2;
188         } else {
189             _nc_SPRINTF(&buffer[bufp], _nc_SLIMIT(P_LIMIT(bufp))
190                         "\\%03o", ch);
191             bufp += 4;
192         }
193
194         str++;
195     }
196
197     buffer[bufp] = '\0';
198     return (buffer);
199 }