* frames.texi (Cursor Parameters): Fix typo. (Bug#5760)
[emacs.git] / src / tparam.c
blobdcf79a3b61714f2d7c173bf8851500998c437af2
1 /* Merge parameters into a termcap entry string.
2 Copyright (C) 1985, 1987, 1993, 1995, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
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.
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.
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
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA. */
20 /* Emacs config.h may rename various library functions such as malloc. */
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
25 #ifdef emacs
26 #include <setjmp.h>
27 #include "lisp.h" /* for xmalloc */
28 #else
30 #ifdef STDC_HEADERS
31 #include <stdlib.h>
32 #include <string.h>
33 #else
34 char *malloc ();
35 char *realloc ();
36 #endif
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
43 #endif /* not emacs */
45 #ifndef NULL
46 #define NULL (char *) 0
47 #endif
49 #ifndef emacs
50 static void
51 memory_out ()
53 write (2, "virtual memory exhausted\n", 25);
54 exit (1);
57 static char *
58 xmalloc (size)
59 unsigned size;
61 register char *tem = malloc (size);
63 if (!tem)
64 memory_out ();
65 return tem;
68 static char *
69 xrealloc (ptr, size)
70 char *ptr;
71 unsigned size;
73 register char *tem = realloc (ptr, size);
75 if (!tem)
76 memory_out ();
77 return tem;
79 #endif /* not emacs */
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'.
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.
91 The fourth and following args to tparam serve as the parameter values. */
93 static char *tparam1 ();
95 /* VARARGS 2 */
96 char *
97 tparam (string, outstring, len, arg0, arg1, arg2, arg3)
98 char *string;
99 char *outstring;
100 int len;
101 int arg0, arg1, arg2, arg3;
103 int arg[4];
105 arg[0] = arg0;
106 arg[1] = arg1;
107 arg[2] = arg2;
108 arg[3] = arg3;
109 return tparam1 (string, outstring, len, NULL, NULL, arg);
112 char *BC;
113 char *UP;
115 static char tgoto_buf[50];
117 char *
118 tgoto (cm, hpos, vpos)
119 char *cm;
120 int hpos, vpos;
122 int args[2];
123 if (!cm)
124 return NULL;
125 args[0] = vpos;
126 args[1] = hpos;
127 return tparam1 (cm, tgoto_buf, 50, UP, BC, args);
130 static char *
131 tparam1 (string, outstring, len, up, left, argp)
132 char *string;
133 char *outstring;
134 int len;
135 char *up, *left;
136 register int *argp;
138 register int c;
139 register char *p = string;
140 register char *op = outstring;
141 char *outend;
142 int outlen = 0;
144 register int tem;
145 int *old_argp = argp; /* can move */
146 int *fixed_argp = argp; /* never moves */
147 int explicit_param_p = 0; /* set by %p */
148 int doleft = 0;
149 int doup = 0;
151 outend = outstring + len;
153 while (1)
155 /* If the buffer might be too short, make it bigger. */
156 if (op + 5 >= outend)
158 register char *new;
159 int offset = op - outstring;
161 if (outlen == 0)
163 outlen = len + 40;
164 new = (char *) xmalloc (outlen);
165 bcopy (outstring, new, offset);
167 else
169 outlen *= 2;
170 new = (char *) xrealloc (outstring, outlen);
173 op = new + offset;
174 outend = new + outlen;
175 outstring = new;
177 c = *p++;
178 if (!c)
179 break;
180 if (c == '%')
182 c = *p++;
183 if (explicit_param_p)
184 explicit_param_p = 0;
185 else
186 tem = *argp;
187 switch (c)
189 case 'd': /* %d means output in decimal. */
190 if (tem < 10)
191 goto onedigit;
192 if (tem < 100)
193 goto twodigit;
194 case '3': /* %3 means output in decimal, 3 digits. */
195 if (tem > 999)
197 *op++ = tem / 1000 + '0';
198 tem %= 1000;
200 *op++ = tem / 100 + '0';
201 case '2': /* %2 means output in decimal, 2 digits. */
202 twodigit:
203 tem %= 100;
204 *op++ = tem / 10 + '0';
205 onedigit:
206 *op++ = tem % 10 + '0';
207 argp++;
208 break;
209 case 'p': /* %pN means use param N for next subst. */
210 tem = fixed_argp[(*p++) - '1'];
211 explicit_param_p = 1;
212 break;
213 case 'C':
214 /* For c-100: print quotient of value by 96, if nonzero,
215 then do like %+. */
216 if (tem >= 96)
218 *op++ = tem / 96;
219 tem %= 96;
221 case '+': /* %+x means add character code of char x. */
222 tem += *p++;
223 case '.': /* %. means output as character. */
224 if (left)
226 /* If want to forbid output of 0 and \n and \t,
227 and this is one of them, increment it. */
228 while (tem == 0 || tem == '\n' || tem == '\t')
230 tem++;
231 if (argp == old_argp)
232 doup++, outend -= strlen (up);
233 else
234 doleft++, outend -= strlen (left);
237 *op++ = tem ? tem : 0200;
238 case 'f': /* %f means discard next arg. */
239 argp++;
240 break;
242 case 'b': /* %b means back up one arg (and re-use it). */
243 argp--;
244 break;
246 case 'r': /* %r means interchange following two args. */
247 argp[0] = argp[1];
248 argp[1] = tem;
249 old_argp++;
250 break;
252 case '>': /* %>xy means if arg is > char code of x, */
253 if (argp[0] > *p++) /* then add char code of y to the arg, */
254 argp[0] += *p; /* and in any case don't output. */
255 p++; /* Leave the arg to be output later. */
256 break;
258 case 'a': /* %a means arithmetic. */
259 /* Next character says what operation.
260 Add or subtract either a constant or some other arg. */
261 /* First following character is + to add or - to subtract
262 or = to assign. */
263 /* Next following char is 'p' and an arg spec
264 (0100 plus position of that arg relative to this one)
265 or 'c' and a constant stored in a character. */
266 tem = p[2] & 0177;
267 if (p[1] == 'p')
268 tem = argp[tem - 0100];
269 if (p[0] == '-')
270 argp[0] -= tem;
271 else 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
278 argp[0] = tem;
280 p += 3;
281 break;
283 case 'i': /* %i means add one to arg, */
284 argp[0] ++; /* and leave it to be output later. */
285 argp[1] ++; /* Increment the following arg, too! */
286 break;
288 case '%': /* %% means output %; no arg. */
289 goto ordinary;
291 case 'n': /* %n means xor each of next two args with 140. */
292 argp[0] ^= 0140;
293 argp[1] ^= 0140;
294 break;
296 case 'm': /* %m means xor each of next two args with 177. */
297 argp[0] ^= 0177;
298 argp[1] ^= 0177;
299 break;
301 case 'B': /* %B means express arg as BCD char code. */
302 argp[0] += 6 * (tem / 10);
303 break;
305 case 'D': /* %D means weird Delta Data transformation. */
306 argp[0] -= 2 * (tem % 16);
307 break;
309 default:
310 abort ();
313 else
314 /* Ordinary character in the argument string. */
315 ordinary:
316 *op++ = c;
318 *op = 0;
319 while (doup-- > 0)
320 strcat (op, up);
321 while (doleft-- > 0)
322 strcat (op, left);
323 return outstring;
326 #ifdef DEBUG
328 main (argc, argv)
329 int argc;
330 char **argv;
332 char buf[50];
333 int args[3];
334 args[0] = atoi (argv[2]);
335 args[1] = atoi (argv[3]);
336 args[2] = atoi (argv[4]);
337 tparam1 (argv[1], buf, "LEFT", "UP", args);
338 printf ("%s\n", buf);
339 return 0;
342 #endif /* DEBUG */
344 /* arch-tag: 83f7b5ac-a808-4f75-b87a-123de009b402
345 (do not change this comment) */