xref: /openbsd/lib/libcurses/tinfo/comp_expand.c (revision 3cab2bb3)
1 /* $OpenBSD: comp_expand.c,v 1.7 2010/01/12 23:22:06 nicm Exp $ */
2 
3 /****************************************************************************
4  * Copyright (c) 1998-2007,2008 Free Software Foundation, Inc.              *
5  *                                                                          *
6  * Permission is hereby granted, free of charge, to any person obtaining a  *
7  * copy of this software and associated documentation files (the            *
8  * "Software"), to deal in the Software without restriction, including      *
9  * without limitation the rights to use, copy, modify, merge, publish,      *
10  * distribute, distribute with modifications, sublicense, and/or sell       *
11  * copies of the Software, and to permit persons to whom the Software is    *
12  * furnished to do so, subject to the following conditions:                 *
13  *                                                                          *
14  * The above copyright notice and this permission notice shall be included  *
15  * in all copies or substantial portions of the Software.                   *
16  *                                                                          *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
20  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
21  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
22  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
23  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
24  *                                                                          *
25  * Except as contained in this notice, the name(s) of the above copyright   *
26  * holders shall not be used in advertising or otherwise to promote the     *
27  * sale, use or other dealings in this Software without prior written       *
28  * authorization.                                                           *
29  ****************************************************************************/
30 
31 /****************************************************************************
32  *  Author: Thomas E. Dickey <dickey@clark.net> 1998                        *
33  ****************************************************************************/
34 
35 #include <curses.priv.h>
36 
37 #include <ctype.h>
38 #include <tic.h>
39 
40 MODULE_ID("$Id: comp_expand.c,v 1.7 2010/01/12 23:22:06 nicm Exp $")
41 
42 static int
43 trailing_spaces(const char *src)
44 {
45     while (*src == ' ')
46 	src++;
47     return *src == 0;
48 }
49 
50 /* this deals with differences over whether 0x7f and 0x80..0x9f are controls */
51 #define REALCTL(s) (UChar(*(s)) < 127 && iscntrl(UChar(*(s))))
52 #define REALPRINT(s) (UChar(*(s)) < 127 && isprint(UChar(*(s))))
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 NO_LEAKS
67     if (srcp == 0) {
68 	if (buffer != 0) {
69 	    FreeAndNull(buffer);
70 	    length = 0;
71 	}
72 	return 0;
73     }
74 #endif
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 		    snprintf(buffer + bufp, length - bufp, "{%d}", str[1]);
96 		    bufp += strlen(buffer + bufp);
97 		    str += 2;
98 		} else {
99 		    buffer[bufp++] = *str;
100 		}
101 		break;
102 		/*
103 		 * If we have a "%{number}", try to translate it into
104 		 * a "%'char'" form, since that will run a little faster
105 		 * when we're interpreting it.  Also, having one form
106 		 * for the constant makes it simpler to compare terminal
107 		 * descriptions.
108 		 */
109 	    case 1:
110 		if (str[0] == L_BRACE
111 		    && isdigit(UChar(str[1]))) {
112 		    char *dst = 0;
113 		    long value = strtol(str + 1, &dst, 0);
114 		    if (dst != 0
115 			&& *dst == R_BRACE
116 			&& value < 127
117 			&& value != '\\'	/* FIXME */
118 			&& isprint((int) value)) {
119 			ch = (int) value;
120 			buffer[bufp++] = S_QUOTE;
121 			if (ch == '\\'
122 			    || ch == S_QUOTE)
123 			    buffer[bufp++] = '\\';
124 			buffer[bufp++] = (char) ch;
125 			buffer[bufp++] = S_QUOTE;
126 			str = dst;
127 		    } else {
128 			buffer[bufp++] = *str;
129 		    }
130 		} else {
131 		    buffer[bufp++] = *str;
132 		}
133 		break;
134 	    default:
135 		buffer[bufp++] = *str;
136 		break;
137 	    }
138 	} else if (ch == 128) {
139 	    buffer[bufp++] = '\\';
140 	    buffer[bufp++] = '0';
141 	} else if (ch == '\033') {
142 	    buffer[bufp++] = '\\';
143 	    buffer[bufp++] = 'E';
144 	} else if (ch == '\\' && tic_format && (str == srcp || str[-1] != '^')) {
145 	    buffer[bufp++] = '\\';
146 	    buffer[bufp++] = '\\';
147 	} else if (ch == ' ' && tic_format && (str == srcp ||
148 					       trailing_spaces(str))) {
149 	    buffer[bufp++] = '\\';
150 	    buffer[bufp++] = 's';
151 	} else if ((ch == ',' || ch == ':' || ch == '^') && tic_format) {
152 	    buffer[bufp++] = '\\';
153 	    buffer[bufp++] = (char) ch;
154 	} else if (REALPRINT(str)
155 		   && (ch != ','
156 		       && ch != ':'
157 		       && !(ch == '!' && !tic_format)
158 		       && ch != '^'))
159 	    buffer[bufp++] = (char) ch;
160 #if 0				/* FIXME: this would be more readable (in fact the whole 'islong' logic should be removed) */
161 	else if (ch == '\b') {
162 	    buffer[bufp++] = '\\';
163 	    buffer[bufp++] = 'b';
164 	} else if (ch == '\f') {
165 	    buffer[bufp++] = '\\';
166 	    buffer[bufp++] = 'f';
167 	} else if (ch == '\t' && islong) {
168 	    buffer[bufp++] = '\\';
169 	    buffer[bufp++] = 't';
170 	}
171 #endif
172 	else if (ch == '\r' && (islong || (strlen(srcp) > 2 && str[1] == '\0'))) {
173 	    buffer[bufp++] = '\\';
174 	    buffer[bufp++] = 'r';
175 	} else if (ch == '\n' && islong) {
176 	    buffer[bufp++] = '\\';
177 	    buffer[bufp++] = 'n';
178 	}
179 #define UnCtl(c) ((c) + '@')
180 	else if (REALCTL(str) && ch != '\\'
181 		 && (!islong || isdigit(UChar(str[1])))) {
182 	    (void) snprintf(buffer + bufp, length - bufp, "^%c", UnCtl(ch));
183 	    bufp += strlen(buffer + bufp);
184 	} else {
185 	    (void) snprintf(buffer + bufp, length - bufp, "\\%03o", ch);
186 	    bufp += strlen(buffer + bufp);
187 	}
188 
189 	str++;
190     }
191 
192     buffer[bufp] = '\0';
193     return (buffer);
194 }
195