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