ncurses 5.2
[ncurses.git] / ncurses / tinfo / comp_expand.c
1 /****************************************************************************
2  * Copyright (c) 1998 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.11 1999/03/07 00:51:07 tom Exp $")
39
40 static int trailing_spaces(const char *src)
41 {
42         while (*src == ' ')
43                 src++;
44         return *src == 0;
45 }
46
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)))
51
52 char *_nc_tic_expand(const char *srcp, bool tic_format, int numbers)
53 {
54 static char *   buffer;
55 static size_t   length;
56
57 int             bufp;
58 const char      *ptr, *str = VALID_STRING(srcp) ? srcp : "";
59 bool            islong = (strlen(str) > 3);
60 size_t          need = (2 + strlen(str)) * 4;
61 int             ch;
62
63         if (buffer == 0 || need > length) {
64                 if ((buffer = typeRealloc(char, length = need, buffer)) == 0)
65                         return 0;
66         }
67
68         bufp = 0;
69         ptr = str;
70         while ((ch = (*str & 0xff)) != 0) {
71                 if (ch == '%' && REALPRINT(str+1)) {
72                         buffer[bufp++] = *str++;
73                         /*
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.
77                          */
78                         switch (numbers) {
79                         case -1:
80                             if (str[0] == S_QUOTE
81                              && str[1] != '\\'
82                              && REALPRINT(str+1)
83                              && str[2] == S_QUOTE) {
84                                 sprintf(buffer+bufp, "{%d}", str[1]);
85                                 bufp += strlen(buffer+bufp);
86                                 str += 2;
87                             } else {
88                                 buffer[bufp++] = *str;
89                             }
90                             break;
91                         /*
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
96                          * descriptions.
97                          */
98                         case 1:
99                             if (str[0] == L_BRACE
100                              && isdigit(str[1])) {
101                                 char *dst = 0;
102                                 long value = strtol(str+1, &dst, 0);
103                                 if (dst != 0
104                                  && *dst == R_BRACE
105                                  && value < 127
106                                  && value != '\\'       /* FIXME */
107                                  && isprint((int)value)) {
108                                         ch = (int)value;
109                                         buffer[bufp++] = S_QUOTE;
110                                         if (ch == '\\'
111                                          || ch == S_QUOTE)
112                                                 buffer[bufp++] = '\\';
113                                         buffer[bufp++] = ch;
114                                         buffer[bufp++] = S_QUOTE;
115                                         str = dst;
116                                 } else {
117                                         buffer[bufp++] = *str;
118                                 }
119                             } else {
120                                     buffer[bufp++] = *str;
121                             }
122                             break;
123                         default:
124                             buffer[bufp++] = *str;
125                             break;
126                         }
127                 }
128                 else if (ch == 128) {
129                         buffer[bufp++] = '\\';
130                         buffer[bufp++] = '0';
131                 }
132                 else if (ch == '\033') {
133                         buffer[bufp++] = '\\';
134                         buffer[bufp++] = 'E';
135                 }
136                 else if (ch == '\\' && tic_format && (str == srcp || str[-1] != '^')) {
137                         buffer[bufp++] = '\\';
138                         buffer[bufp++] = '\\';
139                 }
140                 else if (ch == ' ' && tic_format && (str == srcp || trailing_spaces(str))) {
141                         buffer[bufp++] = '\\';
142                         buffer[bufp++] = 's';
143                 }
144                 else if ((ch == ',' || ch == ':' || ch == '^') && tic_format) {
145                         buffer[bufp++] = '\\';
146                         buffer[bufp++] = ch;
147                 }
148                 else if (REALPRINT(str) && (ch != ',' && ch != ':' && !(ch == '!' && !tic_format) && ch != '^'))
149                         buffer[bufp++] = 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';
154                 }
155                 else if (ch == '\f') {
156                         buffer[bufp++] = '\\';
157                         buffer[bufp++] = 'f';
158                 }
159                 else if (ch == '\t' && islong) {
160                         buffer[bufp++] = '\\';
161                         buffer[bufp++] = 't';
162                 }
163 #endif
164                 else if (ch == '\r' && (islong || (strlen(srcp) > 2 && str[1] == '\0'))) {
165                         buffer[bufp++] = '\\';
166                         buffer[bufp++] = 'r';
167                 }
168                 else if (ch == '\n' && islong) {
169                         buffer[bufp++] = '\\';
170                         buffer[bufp++] = 'n';
171                 }
172 #define UnCtl(c) ((c) + '@')
173                 else if (REALCTL(str) && ch != '\\' && (!islong || isdigit(str[1])))
174                 {
175                         (void) sprintf(&buffer[bufp], "^%c", UnCtl(ch));
176                         bufp += 2;
177                 }
178                 else
179                 {
180                         (void) sprintf(&buffer[bufp], "\\%03o", ch);
181                         bufp += 4;
182                 }
183
184                 str++;
185         }
186
187         buffer[bufp] = '\0';
188         return(buffer);
189 }