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