(ex-write): uses better defaults.
[emacs.git] / tparam.c
bloba5ee362eaa4f5fcdefde568a7bb05474ceb0d04b
1 /* Merge parameters into a termcap entry string.
2 Copyright (C) 1985, 87, 93, 95 Free Software Foundation, Inc.
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.
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.
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. */
18 /* Emacs config.h may rename various library functions such as malloc. */
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
23 #ifndef emacs
24 #if defined(HAVE_STRING_H) || defined(STDC_HEADERS)
25 #define bcopy(s, d, n) memcpy ((d), (s), (n))
26 #endif
28 #ifdef STDC_HEADERS
29 #include <stdlib.h>
30 #include <string.h>
31 #else
32 char *malloc ();
33 char *realloc ();
34 #endif
36 #endif /* not emacs */
38 #ifndef NULL
39 #define NULL (char *) 0
40 #endif
42 #ifndef emacs
43 static void
44 memory_out ()
46 write (2, "virtual memory exhausted\n", 25);
47 exit (1);
50 static char *
51 xmalloc (size)
52 unsigned size;
54 register char *tem = malloc (size);
56 if (!tem)
57 memory_out ();
58 return tem;
61 static char *
62 xrealloc (ptr, size)
63 char *ptr;
64 unsigned size;
66 register char *tem = realloc (ptr, size);
68 if (!tem)
69 memory_out ();
70 return tem;
72 #endif /* not emacs */
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'.
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.
84 The fourth and following args to tparam serve as the parameter values. */
86 static char *tparam1 ();
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;
96 int arg[4];
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);
105 char *BC;
106 char *UP;
108 static char tgoto_buf[50];
110 char *
111 tgoto (cm, hpos, vpos)
112 char *cm;
113 int hpos, vpos;
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);
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;
131 register int c;
132 register char *p = string;
133 register char *op = outstring;
134 char *outend;
135 int outlen = 0;
137 register int tem;
138 int *old_argp = argp;
139 int doleft = 0;
140 int doup = 0;
142 outend = outstring + len;
144 while (1)
146 /* If the buffer might be too short, make it bigger. */
147 if (op + 5 >= outend)
149 register char *new;
150 if (outlen == 0)
152 outlen = len + 40;
153 new = (char *) xmalloc (outlen);
154 outend += 40;
155 bcopy (outstring, new, op - outstring);
157 else
159 outend += outlen;
160 outlen *= 2;
161 new = (char *) xrealloc (outstring, outlen);
163 op += new - outstring;
164 outend += new - outstring;
165 outstring = new;
167 c = *p++;
168 if (!c)
169 break;
170 if (c == '%')
172 c = *p++;
173 tem = *argp;
174 switch (c)
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)
184 *op++ = tem / 1000 + '0';
185 tem %= 1000;
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;
197 case 'C':
198 /* For c-100: print quotient of value by 96, if nonzero,
199 then do like %+. */
200 if (tem >= 96)
202 *op++ = tem / 96;
203 tem %= 96;
205 case '+': /* %+x means add character code of char x. */
206 tem += *p++;
207 case '.': /* %. means output as character. */
208 if (left)
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')
214 tem++;
215 if (argp == old_argp)
216 doup++, outend -= strlen (up);
217 else
218 doleft++, outend -= strlen (left);
221 *op++ = tem ? tem : 0200;
222 case 'f': /* %f means discard next arg. */
223 argp++;
224 break;
226 case 'b': /* %b means back up one arg (and re-use it). */
227 argp--;
228 break;
230 case 'r': /* %r means interchange following two args. */
231 argp[0] = argp[1];
232 argp[1] = tem;
233 old_argp++;
234 break;
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;
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;
264 p += 3;
265 break;
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;
272 case '%': /* %% means output %; no arg. */
273 goto ordinary;
275 case 'n': /* %n means xor each of next two args with 140. */
276 argp[0] ^= 0140;
277 argp[1] ^= 0140;
278 break;
280 case 'm': /* %m means xor each of next two args with 177. */
281 argp[0] ^= 0177;
282 argp[1] ^= 0177;
283 break;
285 case 'B': /* %B means express arg as BCD char code. */
286 argp[0] += 6 * (tem / 10);
287 break;
289 case 'D': /* %D means weird Delta Data transformation. */
290 argp[0] -= 2 * (tem % 16);
291 break;
294 else
295 /* Ordinary character in the argument string. */
296 ordinary:
297 *op++ = c;
299 *op = 0;
300 while (doup-- > 0)
301 strcat (op, up);
302 while (doleft-- > 0)
303 strcat (op, left);
304 return outstring;
307 #ifdef DEBUG
309 main (argc, argv)
310 int argc;
311 char **argv;
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;
323 #endif /* DEBUG */