]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/comp_expand.c
ncurses 4.2
[ncurses.git] / ncurses / 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.4 1998/02/11 12:14:00 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)
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) {
64                 buffer = malloc(length = need);
65         } else if (need > length) {
66                 buffer = realloc(buffer, length = need);
67         }
68
69         bufp = 0;
70         ptr = str;
71         while ((ch = (*str & 0xff)) != 0) {
72                 if (ch == '%' && REALPRINT(str+1)) {
73                         buffer[bufp++] = *str++;
74                         buffer[bufp++] = *str;
75                 }
76                 else if (ch == 128) {
77                         buffer[bufp++] = '\\';
78                         buffer[bufp++] = '0';
79                 }
80                 else if (ch == '\033') {
81                         buffer[bufp++] = '\\';
82                         buffer[bufp++] = 'E';
83                 }
84                 else if (ch == '\\' && tic_format && (str == srcp || str[-1] != '^')) {
85                         buffer[bufp++] = '\\';
86                         buffer[bufp++] = '\\';
87                 }
88                 else if (ch == ' ' && tic_format && (str == srcp || trailing_spaces(str))) {
89                         buffer[bufp++] = '\\';
90                         buffer[bufp++] = 's';
91                 }
92                 else if ((ch == ',' || ch == ':' || ch == '^') && tic_format) {
93                         buffer[bufp++] = '\\';
94                         buffer[bufp++] = ch;
95                 }
96                 else if (REALPRINT(str) && (ch != ',' && ch != ':' && !(ch == '!' && !tic_format) && ch != '^'))
97                         buffer[bufp++] = ch;
98 #if 0           /* FIXME: this would be more readable (in fact the whole 'islong' logic should be removed) */
99                 else if (ch == '\b') {
100                         buffer[bufp++] = '\\';
101                         buffer[bufp++] = 'b';
102                 }
103                 else if (ch == '\f') {
104                         buffer[bufp++] = '\\';
105                         buffer[bufp++] = 'f';
106                 }
107                 else if (ch == '\t' && islong) {
108                         buffer[bufp++] = '\\';
109                         buffer[bufp++] = 't';
110                 }
111 #endif
112                 else if (ch == '\r' && (islong || (strlen(srcp) > 2 && str[1] == '\0'))) {
113                         buffer[bufp++] = '\\';
114                         buffer[bufp++] = 'r';
115                 }
116                 else if (ch == '\n' && islong) {
117                         buffer[bufp++] = '\\';
118                         buffer[bufp++] = 'n';
119                 }
120 #define UnCtl(c) ((c) + '@')
121                 else if (REALCTL(str) && ch != '\\' && (!islong || isdigit(str[1])))
122                 {
123                         (void) sprintf(&buffer[bufp], "^%c", UnCtl(ch));
124                         bufp += 2;
125                 }
126                 else
127                 {
128                         (void) sprintf(&buffer[bufp], "\\%03o", ch);
129                         bufp += 4;
130                 }
131
132                 str++;
133         }
134
135         buffer[bufp] = '\0';
136         return(buffer);
137 }