Commit | Line | Data |
---|---|---|
e0dd62f6 | 1 | /* Merge parameters into a termcap entry string. |
ba318903 PE |
2 | Copyright (C) 1985, 1987, 1993, 1995, 2000-2008, 2013-2014 Free |
3 | Software Foundation, Inc. | |
e0dd62f6 RM |
4 | |
5 | This program is free software; you can redistribute it and/or modify | |
6 | it under the terms of the GNU General Public License as published by | |
7 | the Free Software Foundation; either version 2, or (at your option) | |
8 | any later version. | |
9 | ||
10 | This program is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU General Public License | |
fee0bd5f | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
e0dd62f6 RM |
17 | |
18 | /* Emacs config.h may rename various library functions such as malloc. */ | |
e0dd62f6 | 19 | #include <config.h> |
0328b6de | 20 | |
83c2ece5 | 21 | #include "lisp.h" /* for xmalloc */ |
50938595 | 22 | #include "tparam.h" |
e0dd62f6 | 23 | \f |
e0dd62f6 RM |
24 | /* Assuming STRING is the value of a termcap string entry |
25 | containing `%' constructs to expand parameters, | |
26 | merge in parameter values and store result in block OUTSTRING points to. | |
27 | LEN is the length of OUTSTRING. If more space is needed, | |
28 | a block is allocated with `malloc'. | |
29 | ||
30 | The value returned is the address of the resulting string. | |
31 | This may be OUTSTRING or may be the address of a block got with `malloc'. | |
32 | In the latter case, the caller must free the block. | |
33 | ||
34 | The fourth and following args to tparam serve as the parameter values. */ | |
35 | ||
50938595 PE |
36 | static char *tparam1 (char const *string, char *outstring, int len, |
37 | char *up, char *left, int *argp); | |
e0dd62f6 | 38 | |
e0dd62f6 | 39 | char * |
50938595 PE |
40 | tparam (const char *string, char *outstring, int len, |
41 | int arg0, int arg1, int arg2, int arg3) | |
e0dd62f6 | 42 | { |
e0dd62f6 | 43 | int arg[4]; |
9e3295f9 | 44 | |
e0dd62f6 RM |
45 | arg[0] = arg0; |
46 | arg[1] = arg1; | |
47 | arg[2] = arg2; | |
48 | arg[3] = arg3; | |
49 | return tparam1 (string, outstring, len, NULL, NULL, arg); | |
e0dd62f6 RM |
50 | } |
51 | ||
52 | char *BC; | |
53 | char *UP; | |
54 | ||
55 | static char tgoto_buf[50]; | |
56 | ||
57 | char * | |
50938595 | 58 | tgoto (const char *cm, int hpos, int vpos) |
e0dd62f6 RM |
59 | { |
60 | int args[2]; | |
61 | if (!cm) | |
62 | return NULL; | |
63 | args[0] = vpos; | |
64 | args[1] = hpos; | |
65 | return tparam1 (cm, tgoto_buf, 50, UP, BC, args); | |
66 | } | |
67 | ||
68 | static char * | |
50938595 PE |
69 | tparam1 (const char *string, char *outstring, int len, |
70 | char *up, char *left, register int *argp) | |
e0dd62f6 RM |
71 | { |
72 | register int c; | |
50938595 | 73 | register const char *p = string; |
e0dd62f6 RM |
74 | register char *op = outstring; |
75 | char *outend; | |
8fbadbe3 PE |
76 | char *new = 0; |
77 | ptrdiff_t outlen = 0; | |
e0dd62f6 RM |
78 | |
79 | register int tem; | |
9ad4bf7a TTN |
80 | int *old_argp = argp; /* can move */ |
81 | int *fixed_argp = argp; /* never moves */ | |
5df474aa | 82 | bool explicit_param_p = 0; /* set by %p */ |
8fbadbe3 PE |
83 | ptrdiff_t doleft = 0; |
84 | ptrdiff_t doup = 0; | |
85 | ptrdiff_t append_len = 0; | |
e0dd62f6 RM |
86 | |
87 | outend = outstring + len; | |
88 | ||
89 | while (1) | |
90 | { | |
91 | /* If the buffer might be too short, make it bigger. */ | |
8fbadbe3 | 92 | while (outend - op - append_len <= 5) |
e0dd62f6 | 93 | { |
8fbadbe3 | 94 | ptrdiff_t offset = op - outstring; |
b5e55477 | 95 | |
e0dd62f6 RM |
96 | if (outlen == 0) |
97 | { | |
98 | outlen = len + 40; | |
23f86fce | 99 | new = xmalloc (outlen); |
72af86bd | 100 | memcpy (new, outstring, offset); |
e0dd62f6 RM |
101 | } |
102 | else | |
103 | { | |
0065d054 | 104 | new = xpalloc (outstring, &outlen, 1, -1, 1); |
e0dd62f6 | 105 | } |
177c0ea7 | 106 | |
b5e55477 GM |
107 | op = new + offset; |
108 | outend = new + outlen; | |
e0dd62f6 RM |
109 | outstring = new; |
110 | } | |
111 | c = *p++; | |
112 | if (!c) | |
113 | break; | |
114 | if (c == '%') | |
115 | { | |
116 | c = *p++; | |
9ad4bf7a TTN |
117 | if (explicit_param_p) |
118 | explicit_param_p = 0; | |
119 | else | |
120 | tem = *argp; | |
e0dd62f6 RM |
121 | switch (c) |
122 | { | |
123 | case 'd': /* %d means output in decimal. */ | |
124 | if (tem < 10) | |
125 | goto onedigit; | |
126 | if (tem < 100) | |
127 | goto twodigit; | |
128 | case '3': /* %3 means output in decimal, 3 digits. */ | |
129 | if (tem > 999) | |
130 | { | |
131 | *op++ = tem / 1000 + '0'; | |
132 | tem %= 1000; | |
133 | } | |
134 | *op++ = tem / 100 + '0'; | |
135 | case '2': /* %2 means output in decimal, 2 digits. */ | |
136 | twodigit: | |
137 | tem %= 100; | |
138 | *op++ = tem / 10 + '0'; | |
139 | onedigit: | |
140 | *op++ = tem % 10 + '0'; | |
141 | argp++; | |
142 | break; | |
9ad4bf7a TTN |
143 | case 'p': /* %pN means use param N for next subst. */ |
144 | tem = fixed_argp[(*p++) - '1']; | |
145 | explicit_param_p = 1; | |
146 | break; | |
e0dd62f6 RM |
147 | case 'C': |
148 | /* For c-100: print quotient of value by 96, if nonzero, | |
149 | then do like %+. */ | |
150 | if (tem >= 96) | |
151 | { | |
152 | *op++ = tem / 96; | |
153 | tem %= 96; | |
154 | } | |
155 | case '+': /* %+x means add character code of char x. */ | |
156 | tem += *p++; | |
157 | case '.': /* %. means output as character. */ | |
158 | if (left) | |
159 | { | |
160 | /* If want to forbid output of 0 and \n and \t, | |
161 | and this is one of them, increment it. */ | |
162 | while (tem == 0 || tem == '\n' || tem == '\t') | |
163 | { | |
8fbadbe3 | 164 | ptrdiff_t append_len_incr; |
e0dd62f6 RM |
165 | tem++; |
166 | if (argp == old_argp) | |
8fbadbe3 | 167 | doup++, append_len_incr = strlen (up); |
e0dd62f6 | 168 | else |
8fbadbe3 | 169 | doleft++, append_len_incr = strlen (left); |
0065d054 PE |
170 | if (INT_ADD_OVERFLOW (append_len, append_len_incr)) |
171 | memory_full (SIZE_MAX); | |
8fbadbe3 | 172 | append_len += append_len_incr; |
e0dd62f6 RM |
173 | } |
174 | } | |
175 | *op++ = tem ? tem : 0200; | |
176 | case 'f': /* %f means discard next arg. */ | |
177 | argp++; | |
178 | break; | |
179 | ||
180 | case 'b': /* %b means back up one arg (and re-use it). */ | |
181 | argp--; | |
182 | break; | |
183 | ||
184 | case 'r': /* %r means interchange following two args. */ | |
185 | argp[0] = argp[1]; | |
186 | argp[1] = tem; | |
187 | old_argp++; | |
188 | break; | |
189 | ||
190 | case '>': /* %>xy means if arg is > char code of x, */ | |
191 | if (argp[0] > *p++) /* then add char code of y to the arg, */ | |
192 | argp[0] += *p; /* and in any case don't output. */ | |
193 | p++; /* Leave the arg to be output later. */ | |
194 | break; | |
195 | ||
196 | case 'a': /* %a means arithmetic. */ | |
197 | /* Next character says what operation. | |
198 | Add or subtract either a constant or some other arg. */ | |
199 | /* First following character is + to add or - to subtract | |
200 | or = to assign. */ | |
201 | /* Next following char is 'p' and an arg spec | |
202 | (0100 plus position of that arg relative to this one) | |
203 | or 'c' and a constant stored in a character. */ | |
204 | tem = p[2] & 0177; | |
205 | if (p[1] == 'p') | |
206 | tem = argp[tem - 0100]; | |
207 | if (p[0] == '-') | |
208 | argp[0] -= tem; | |
209 | else if (p[0] == '+') | |
210 | argp[0] += tem; | |
211 | else if (p[0] == '*') | |
212 | argp[0] *= tem; | |
213 | else if (p[0] == '/') | |
214 | argp[0] /= tem; | |
215 | else | |
216 | argp[0] = tem; | |
217 | ||
218 | p += 3; | |
219 | break; | |
220 | ||
221 | case 'i': /* %i means add one to arg, */ | |
222 | argp[0] ++; /* and leave it to be output later. */ | |
223 | argp[1] ++; /* Increment the following arg, too! */ | |
224 | break; | |
225 | ||
226 | case '%': /* %% means output %; no arg. */ | |
227 | goto ordinary; | |
228 | ||
229 | case 'n': /* %n means xor each of next two args with 140. */ | |
230 | argp[0] ^= 0140; | |
231 | argp[1] ^= 0140; | |
232 | break; | |
233 | ||
234 | case 'm': /* %m means xor each of next two args with 177. */ | |
235 | argp[0] ^= 0177; | |
236 | argp[1] ^= 0177; | |
237 | break; | |
238 | ||
239 | case 'B': /* %B means express arg as BCD char code. */ | |
240 | argp[0] += 6 * (tem / 10); | |
241 | break; | |
242 | ||
243 | case 'D': /* %D means weird Delta Data transformation. */ | |
244 | argp[0] -= 2 * (tem % 16); | |
245 | break; | |
45e0dd95 GM |
246 | |
247 | default: | |
1088b922 | 248 | emacs_abort (); |
e0dd62f6 RM |
249 | } |
250 | } | |
251 | else | |
252 | /* Ordinary character in the argument string. */ | |
253 | ordinary: | |
254 | *op++ = c; | |
255 | } | |
256 | *op = 0; | |
257 | while (doup-- > 0) | |
258 | strcat (op, up); | |
259 | while (doleft-- > 0) | |
260 | strcat (op, left); | |
261 | return outstring; | |
262 | } | |
263 | \f | |
264 | #ifdef DEBUG | |
265 | ||
1dae0f0a AS |
266 | int |
267 | main (int argc, char **argv) | |
e0dd62f6 RM |
268 | { |
269 | char buf[50]; | |
270 | int args[3]; | |
271 | args[0] = atoi (argv[2]); | |
272 | args[1] = atoi (argv[3]); | |
273 | args[2] = atoi (argv[4]); | |
0065d054 | 274 | tparam1 (argv[1], buf, 50, "LEFT", "UP", args); |
e0dd62f6 RM |
275 | printf ("%s\n", buf); |
276 | return 0; | |
277 | } | |
278 | ||
279 | #endif /* DEBUG */ |