Merge from trunk.
[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. */
e0dd62f6 21#include <config.h>
d7306fe6 22#include <setjmp.h>
83c2ece5 23#include "lisp.h" /* for xmalloc */
50938595 24#include "tparam.h"
e0dd62f6
RM
25
26#ifndef NULL
27#define NULL (char *) 0
28#endif
29\f
e0dd62f6
RM
30/* Assuming STRING is the value of a termcap string entry
31 containing `%' constructs to expand parameters,
32 merge in parameter values and store result in block OUTSTRING points to.
33 LEN is the length of OUTSTRING. If more space is needed,
34 a block is allocated with `malloc'.
35
36 The value returned is the address of the resulting string.
37 This may be OUTSTRING or may be the address of a block got with `malloc'.
38 In the latter case, the caller must free the block.
39
40 The fourth and following args to tparam serve as the parameter values. */
41
50938595
PE
42static char *tparam1 (char const *string, char *outstring, int len,
43 char *up, char *left, int *argp);
e0dd62f6 44
e0dd62f6 45char *
50938595
PE
46tparam (const char *string, char *outstring, int len,
47 int arg0, int arg1, int arg2, int arg3)
e0dd62f6 48{
e0dd62f6 49 int arg[4];
9e3295f9 50
e0dd62f6
RM
51 arg[0] = arg0;
52 arg[1] = arg1;
53 arg[2] = arg2;
54 arg[3] = arg3;
55 return tparam1 (string, outstring, len, NULL, NULL, arg);
e0dd62f6
RM
56}
57
58char *BC;
59char *UP;
60
61static char tgoto_buf[50];
62
63char *
50938595 64tgoto (const char *cm, int hpos, int vpos)
e0dd62f6
RM
65{
66 int args[2];
67 if (!cm)
68 return NULL;
69 args[0] = vpos;
70 args[1] = hpos;
71 return tparam1 (cm, tgoto_buf, 50, UP, BC, args);
72}
73
74static char *
50938595
PE
75tparam1 (const char *string, char *outstring, int len,
76 char *up, char *left, register int *argp)
e0dd62f6
RM
77{
78 register int c;
50938595 79 register const char *p = string;
e0dd62f6
RM
80 register char *op = outstring;
81 char *outend;
8fbadbe3
PE
82 char *new = 0;
83 ptrdiff_t outlen = 0;
e0dd62f6
RM
84
85 register int tem;
9ad4bf7a
TTN
86 int *old_argp = argp; /* can move */
87 int *fixed_argp = argp; /* never moves */
88 int explicit_param_p = 0; /* set by %p */
8fbadbe3
PE
89 ptrdiff_t doleft = 0;
90 ptrdiff_t doup = 0;
91 ptrdiff_t append_len = 0;
e0dd62f6
RM
92
93 outend = outstring + len;
94
95 while (1)
96 {
97 /* If the buffer might be too short, make it bigger. */
8fbadbe3 98 while (outend - op - append_len <= 5)
e0dd62f6 99 {
8fbadbe3 100 ptrdiff_t offset = op - outstring;
b5e55477 101
e0dd62f6
RM
102 if (outlen == 0)
103 {
8fbadbe3
PE
104 if (min (PTRDIFF_MAX, SIZE_MAX) - 40 < len)
105 goto out_of_memory;
e0dd62f6
RM
106 outlen = len + 40;
107 new = (char *) xmalloc (outlen);
72af86bd 108 memcpy (new, outstring, offset);
e0dd62f6
RM
109 }
110 else
111 {
8fbadbe3
PE
112 if (min (PTRDIFF_MAX, SIZE_MAX) / 2 < outlen)
113 goto out_of_memory;
e0dd62f6
RM
114 outlen *= 2;
115 new = (char *) xrealloc (outstring, outlen);
116 }
177c0ea7 117
b5e55477
GM
118 op = new + offset;
119 outend = new + outlen;
e0dd62f6
RM
120 outstring = new;
121 }
122 c = *p++;
123 if (!c)
124 break;
125 if (c == '%')
126 {
127 c = *p++;
9ad4bf7a
TTN
128 if (explicit_param_p)
129 explicit_param_p = 0;
130 else
131 tem = *argp;
e0dd62f6
RM
132 switch (c)
133 {
134 case 'd': /* %d means output in decimal. */
135 if (tem < 10)
136 goto onedigit;
137 if (tem < 100)
138 goto twodigit;
139 case '3': /* %3 means output in decimal, 3 digits. */
140 if (tem > 999)
141 {
142 *op++ = tem / 1000 + '0';
143 tem %= 1000;
144 }
145 *op++ = tem / 100 + '0';
146 case '2': /* %2 means output in decimal, 2 digits. */
147 twodigit:
148 tem %= 100;
149 *op++ = tem / 10 + '0';
150 onedigit:
151 *op++ = tem % 10 + '0';
152 argp++;
153 break;
9ad4bf7a
TTN
154 case 'p': /* %pN means use param N for next subst. */
155 tem = fixed_argp[(*p++) - '1'];
156 explicit_param_p = 1;
157 break;
e0dd62f6
RM
158 case 'C':
159 /* For c-100: print quotient of value by 96, if nonzero,
160 then do like %+. */
161 if (tem >= 96)
162 {
163 *op++ = tem / 96;
164 tem %= 96;
165 }
166 case '+': /* %+x means add character code of char x. */
167 tem += *p++;
168 case '.': /* %. means output as character. */
169 if (left)
170 {
171 /* If want to forbid output of 0 and \n and \t,
172 and this is one of them, increment it. */
173 while (tem == 0 || tem == '\n' || tem == '\t')
174 {
8fbadbe3 175 ptrdiff_t append_len_incr;
e0dd62f6
RM
176 tem++;
177 if (argp == old_argp)
8fbadbe3 178 doup++, append_len_incr = strlen (up);
e0dd62f6 179 else
8fbadbe3
PE
180 doleft++, append_len_incr = strlen (left);
181 if (PTRDIFF_MAX - append_len < append_len_incr)
182 {
183 out_of_memory:
184 xfree (new);
185 memory_full (SIZE_MAX);
186 }
187 append_len += append_len_incr;
e0dd62f6
RM
188 }
189 }
190 *op++ = tem ? tem : 0200;
191 case 'f': /* %f means discard next arg. */
192 argp++;
193 break;
194
195 case 'b': /* %b means back up one arg (and re-use it). */
196 argp--;
197 break;
198
199 case 'r': /* %r means interchange following two args. */
200 argp[0] = argp[1];
201 argp[1] = tem;
202 old_argp++;
203 break;
204
205 case '>': /* %>xy means if arg is > char code of x, */
206 if (argp[0] > *p++) /* then add char code of y to the arg, */
207 argp[0] += *p; /* and in any case don't output. */
208 p++; /* Leave the arg to be output later. */
209 break;
210
211 case 'a': /* %a means arithmetic. */
212 /* Next character says what operation.
213 Add or subtract either a constant or some other arg. */
214 /* First following character is + to add or - to subtract
215 or = to assign. */
216 /* Next following char is 'p' and an arg spec
217 (0100 plus position of that arg relative to this one)
218 or 'c' and a constant stored in a character. */
219 tem = p[2] & 0177;
220 if (p[1] == 'p')
221 tem = argp[tem - 0100];
222 if (p[0] == '-')
223 argp[0] -= tem;
224 else if (p[0] == '+')
225 argp[0] += tem;
226 else if (p[0] == '*')
227 argp[0] *= tem;
228 else if (p[0] == '/')
229 argp[0] /= tem;
230 else
231 argp[0] = tem;
232
233 p += 3;
234 break;
235
236 case 'i': /* %i means add one to arg, */
237 argp[0] ++; /* and leave it to be output later. */
238 argp[1] ++; /* Increment the following arg, too! */
239 break;
240
241 case '%': /* %% means output %; no arg. */
242 goto ordinary;
243
244 case 'n': /* %n means xor each of next two args with 140. */
245 argp[0] ^= 0140;
246 argp[1] ^= 0140;
247 break;
248
249 case 'm': /* %m means xor each of next two args with 177. */
250 argp[0] ^= 0177;
251 argp[1] ^= 0177;
252 break;
253
254 case 'B': /* %B means express arg as BCD char code. */
255 argp[0] += 6 * (tem / 10);
256 break;
257
258 case 'D': /* %D means weird Delta Data transformation. */
259 argp[0] -= 2 * (tem % 16);
260 break;
45e0dd95
GM
261
262 default:
263 abort ();
e0dd62f6
RM
264 }
265 }
266 else
267 /* Ordinary character in the argument string. */
268 ordinary:
269 *op++ = c;
270 }
271 *op = 0;
272 while (doup-- > 0)
273 strcat (op, up);
274 while (doleft-- > 0)
275 strcat (op, left);
276 return outstring;
277}
278\f
279#ifdef DEBUG
280
1dae0f0a
AS
281int
282main (int argc, char **argv)
e0dd62f6
RM
283{
284 char buf[50];
285 int args[3];
286 args[0] = atoi (argv[2]);
287 args[1] = atoi (argv[3]);
288 args[2] = atoi (argv[4]);
289 tparam1 (argv[1], buf, "LEFT", "UP", args);
290 printf ("%s\n", buf);
291 return 0;
292}
293
294#endif /* DEBUG */