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