Commit | Line | Data |
---|---|---|
e0dd62f6 | 1 | /* Merge parameters into a termcap entry string. |
b5e55477 | 2 | Copyright (C) 1985, 87, 93, 95, 2000 Free Software Foundation, Inc. |
e0dd62f6 RM |
3 | |
4 | This program is free software; you can redistribute it and/or modify | |
5 | it under the terms of the GNU General Public License as published by | |
6 | the Free Software Foundation; either version 2, or (at your option) | |
7 | any later version. | |
8 | ||
9 | This program is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | GNU General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU General Public License | |
15 | along with this program; see the file COPYING. If not, write to | |
ba4a8e51 KH |
16 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
17 | Boston, MA 02111-1307, USA. */ | |
e0dd62f6 RM |
18 | |
19 | /* Emacs config.h may rename various library functions such as malloc. */ | |
20 | #ifdef HAVE_CONFIG_H | |
21 | #include <config.h> | |
c70c2705 | 22 | #endif |
e0dd62f6 | 23 | |
83c2ece5 DL |
24 | #ifdef emacs |
25 | #include "lisp.h" /* for xmalloc */ | |
26 | #else | |
e0dd62f6 RM |
27 | |
28 | #ifdef STDC_HEADERS | |
29 | #include <stdlib.h> | |
30 | #include <string.h> | |
31 | #else | |
32 | char *malloc (); | |
33 | char *realloc (); | |
34 | #endif | |
35 | ||
71091322 RS |
36 | /* Do this after the include, in case string.h prototypes bcopy. */ |
37 | #if (defined(HAVE_STRING_H) || defined(STDC_HEADERS)) && !defined(bcopy) | |
38 | #define bcopy(s, d, n) memcpy ((d), (s), (n)) | |
39 | #endif | |
40 | ||
c70c2705 | 41 | #endif /* not emacs */ |
e0dd62f6 RM |
42 | |
43 | #ifndef NULL | |
44 | #define NULL (char *) 0 | |
45 | #endif | |
46 | \f | |
47 | #ifndef emacs | |
48 | static void | |
49 | memory_out () | |
50 | { | |
51 | write (2, "virtual memory exhausted\n", 25); | |
52 | exit (1); | |
53 | } | |
54 | ||
55 | static char * | |
56 | xmalloc (size) | |
57 | unsigned size; | |
58 | { | |
59 | register char *tem = malloc (size); | |
60 | ||
61 | if (!tem) | |
62 | memory_out (); | |
63 | return tem; | |
64 | } | |
65 | ||
66 | static char * | |
67 | xrealloc (ptr, size) | |
68 | char *ptr; | |
69 | unsigned size; | |
70 | { | |
71 | register char *tem = realloc (ptr, size); | |
72 | ||
73 | if (!tem) | |
74 | memory_out (); | |
75 | return tem; | |
76 | } | |
77 | #endif /* not emacs */ | |
78 | \f | |
79 | /* Assuming STRING is the value of a termcap string entry | |
80 | containing `%' constructs to expand parameters, | |
81 | merge in parameter values and store result in block OUTSTRING points to. | |
82 | LEN is the length of OUTSTRING. If more space is needed, | |
83 | a block is allocated with `malloc'. | |
84 | ||
85 | The value returned is the address of the resulting string. | |
86 | This may be OUTSTRING or may be the address of a block got with `malloc'. | |
87 | In the latter case, the caller must free the block. | |
88 | ||
89 | The fourth and following args to tparam serve as the parameter values. */ | |
90 | ||
91 | static char *tparam1 (); | |
92 | ||
93 | /* VARARGS 2 */ | |
94 | char * | |
95 | tparam (string, outstring, len, arg0, arg1, arg2, arg3) | |
96 | char *string; | |
97 | char *outstring; | |
98 | int len; | |
99 | int arg0, arg1, arg2, arg3; | |
100 | { | |
e0dd62f6 | 101 | int arg[4]; |
9e3295f9 | 102 | |
e0dd62f6 RM |
103 | arg[0] = arg0; |
104 | arg[1] = arg1; | |
105 | arg[2] = arg2; | |
106 | arg[3] = arg3; | |
107 | return tparam1 (string, outstring, len, NULL, NULL, arg); | |
e0dd62f6 RM |
108 | } |
109 | ||
e0f712ba AC |
110 | /* These are already defined in the System framework in Mac OS X and |
111 | cause prebinding to fail. */ | |
112 | #ifndef MAC_OSX | |
e0dd62f6 RM |
113 | char *BC; |
114 | char *UP; | |
115 | ||
116 | static char tgoto_buf[50]; | |
117 | ||
118 | char * | |
119 | tgoto (cm, hpos, vpos) | |
120 | char *cm; | |
121 | int hpos, vpos; | |
122 | { | |
123 | int args[2]; | |
124 | if (!cm) | |
125 | return NULL; | |
126 | args[0] = vpos; | |
127 | args[1] = hpos; | |
128 | return tparam1 (cm, tgoto_buf, 50, UP, BC, args); | |
129 | } | |
e0f712ba | 130 | #endif |
e0dd62f6 RM |
131 | |
132 | static char * | |
133 | tparam1 (string, outstring, len, up, left, argp) | |
134 | char *string; | |
135 | char *outstring; | |
136 | int len; | |
137 | char *up, *left; | |
138 | register int *argp; | |
139 | { | |
140 | register int c; | |
141 | register char *p = string; | |
142 | register char *op = outstring; | |
143 | char *outend; | |
144 | int outlen = 0; | |
145 | ||
146 | register int tem; | |
9ad4bf7a TTN |
147 | int *old_argp = argp; /* can move */ |
148 | int *fixed_argp = argp; /* never moves */ | |
149 | int explicit_param_p = 0; /* set by %p */ | |
e0dd62f6 RM |
150 | int doleft = 0; |
151 | int doup = 0; | |
152 | ||
153 | outend = outstring + len; | |
154 | ||
155 | while (1) | |
156 | { | |
157 | /* If the buffer might be too short, make it bigger. */ | |
158 | if (op + 5 >= outend) | |
159 | { | |
160 | register char *new; | |
b5e55477 GM |
161 | int offset = op - outstring; |
162 | ||
e0dd62f6 RM |
163 | if (outlen == 0) |
164 | { | |
165 | outlen = len + 40; | |
166 | new = (char *) xmalloc (outlen); | |
b5e55477 | 167 | bcopy (outstring, new, offset); |
e0dd62f6 RM |
168 | } |
169 | else | |
170 | { | |
e0dd62f6 RM |
171 | outlen *= 2; |
172 | new = (char *) xrealloc (outstring, outlen); | |
173 | } | |
177c0ea7 | 174 | |
b5e55477 GM |
175 | op = new + offset; |
176 | outend = new + outlen; | |
e0dd62f6 RM |
177 | outstring = new; |
178 | } | |
179 | c = *p++; | |
180 | if (!c) | |
181 | break; | |
182 | if (c == '%') | |
183 | { | |
184 | c = *p++; | |
9ad4bf7a TTN |
185 | if (explicit_param_p) |
186 | explicit_param_p = 0; | |
187 | else | |
188 | tem = *argp; | |
e0dd62f6 RM |
189 | switch (c) |
190 | { | |
191 | case 'd': /* %d means output in decimal. */ | |
192 | if (tem < 10) | |
193 | goto onedigit; | |
194 | if (tem < 100) | |
195 | goto twodigit; | |
196 | case '3': /* %3 means output in decimal, 3 digits. */ | |
197 | if (tem > 999) | |
198 | { | |
199 | *op++ = tem / 1000 + '0'; | |
200 | tem %= 1000; | |
201 | } | |
202 | *op++ = tem / 100 + '0'; | |
203 | case '2': /* %2 means output in decimal, 2 digits. */ | |
204 | twodigit: | |
205 | tem %= 100; | |
206 | *op++ = tem / 10 + '0'; | |
207 | onedigit: | |
208 | *op++ = tem % 10 + '0'; | |
209 | argp++; | |
210 | break; | |
9ad4bf7a TTN |
211 | case 'p': /* %pN means use param N for next subst. */ |
212 | tem = fixed_argp[(*p++) - '1']; | |
213 | explicit_param_p = 1; | |
214 | break; | |
e0dd62f6 RM |
215 | case 'C': |
216 | /* For c-100: print quotient of value by 96, if nonzero, | |
217 | then do like %+. */ | |
218 | if (tem >= 96) | |
219 | { | |
220 | *op++ = tem / 96; | |
221 | tem %= 96; | |
222 | } | |
223 | case '+': /* %+x means add character code of char x. */ | |
224 | tem += *p++; | |
225 | case '.': /* %. means output as character. */ | |
226 | if (left) | |
227 | { | |
228 | /* If want to forbid output of 0 and \n and \t, | |
229 | and this is one of them, increment it. */ | |
230 | while (tem == 0 || tem == '\n' || tem == '\t') | |
231 | { | |
232 | tem++; | |
233 | if (argp == old_argp) | |
234 | doup++, outend -= strlen (up); | |
235 | else | |
236 | doleft++, outend -= strlen (left); | |
237 | } | |
238 | } | |
239 | *op++ = tem ? tem : 0200; | |
240 | case 'f': /* %f means discard next arg. */ | |
241 | argp++; | |
242 | break; | |
243 | ||
244 | case 'b': /* %b means back up one arg (and re-use it). */ | |
245 | argp--; | |
246 | break; | |
247 | ||
248 | case 'r': /* %r means interchange following two args. */ | |
249 | argp[0] = argp[1]; | |
250 | argp[1] = tem; | |
251 | old_argp++; | |
252 | break; | |
253 | ||
254 | case '>': /* %>xy means if arg is > char code of x, */ | |
255 | if (argp[0] > *p++) /* then add char code of y to the arg, */ | |
256 | argp[0] += *p; /* and in any case don't output. */ | |
257 | p++; /* Leave the arg to be output later. */ | |
258 | break; | |
259 | ||
260 | case 'a': /* %a means arithmetic. */ | |
261 | /* Next character says what operation. | |
262 | Add or subtract either a constant or some other arg. */ | |
263 | /* First following character is + to add or - to subtract | |
264 | or = to assign. */ | |
265 | /* Next following char is 'p' and an arg spec | |
266 | (0100 plus position of that arg relative to this one) | |
267 | or 'c' and a constant stored in a character. */ | |
268 | tem = p[2] & 0177; | |
269 | if (p[1] == 'p') | |
270 | tem = argp[tem - 0100]; | |
271 | if (p[0] == '-') | |
272 | argp[0] -= tem; | |
273 | else if (p[0] == '+') | |
274 | argp[0] += tem; | |
275 | else if (p[0] == '*') | |
276 | argp[0] *= tem; | |
277 | else if (p[0] == '/') | |
278 | argp[0] /= tem; | |
279 | else | |
280 | argp[0] = tem; | |
281 | ||
282 | p += 3; | |
283 | break; | |
284 | ||
285 | case 'i': /* %i means add one to arg, */ | |
286 | argp[0] ++; /* and leave it to be output later. */ | |
287 | argp[1] ++; /* Increment the following arg, too! */ | |
288 | break; | |
289 | ||
290 | case '%': /* %% means output %; no arg. */ | |
291 | goto ordinary; | |
292 | ||
293 | case 'n': /* %n means xor each of next two args with 140. */ | |
294 | argp[0] ^= 0140; | |
295 | argp[1] ^= 0140; | |
296 | break; | |
297 | ||
298 | case 'm': /* %m means xor each of next two args with 177. */ | |
299 | argp[0] ^= 0177; | |
300 | argp[1] ^= 0177; | |
301 | break; | |
302 | ||
303 | case 'B': /* %B means express arg as BCD char code. */ | |
304 | argp[0] += 6 * (tem / 10); | |
305 | break; | |
306 | ||
307 | case 'D': /* %D means weird Delta Data transformation. */ | |
308 | argp[0] -= 2 * (tem % 16); | |
309 | break; | |
45e0dd95 GM |
310 | |
311 | default: | |
312 | abort (); | |
e0dd62f6 RM |
313 | } |
314 | } | |
315 | else | |
316 | /* Ordinary character in the argument string. */ | |
317 | ordinary: | |
318 | *op++ = c; | |
319 | } | |
320 | *op = 0; | |
321 | while (doup-- > 0) | |
322 | strcat (op, up); | |
323 | while (doleft-- > 0) | |
324 | strcat (op, left); | |
325 | return outstring; | |
326 | } | |
327 | \f | |
328 | #ifdef DEBUG | |
329 | ||
330 | main (argc, argv) | |
331 | int argc; | |
332 | char **argv; | |
333 | { | |
334 | char buf[50]; | |
335 | int args[3]; | |
336 | args[0] = atoi (argv[2]); | |
337 | args[1] = atoi (argv[3]); | |
338 | args[2] = atoi (argv[4]); | |
339 | tparam1 (argv[1], buf, "LEFT", "UP", args); | |
340 | printf ("%s\n", buf); | |
341 | return 0; | |
342 | } | |
343 | ||
344 | #endif /* DEBUG */ | |
ab5796a9 MB |
345 | |
346 | /* arch-tag: 83f7b5ac-a808-4f75-b87a-123de009b402 | |
347 | (do not change this comment) */ |