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