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