Mention PCL-CVS.
[emacs.git] / tparam.c
blobae12e72ac7bda6ed249cd3a598a4c78ad275254c
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, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 /* Emacs config.h may rename various library functions such as malloc. */
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
24 #ifndef emacs
25 #if defined(HAVE_STRING_H) || defined(STDC_HEADERS)
26 #define bcopy(s, d, n) memcpy ((d), (s), (n))
27 #endif
29 #ifdef STDC_HEADERS
30 #include <stdlib.h>
31 #include <string.h>
32 #else
33 char *malloc ();
34 char *realloc ();
35 #endif
37 #endif /* not emacs */
39 #ifndef NULL
40 #define NULL (char *) 0
41 #endif
43 #ifndef emacs
44 static void
45 memory_out ()
47 write (2, "virtual memory exhausted\n", 25);
48 exit (1);
51 static char *
52 xmalloc (size)
53 unsigned size;
55 register char *tem = malloc (size);
57 if (!tem)
58 memory_out ();
59 return tem;
62 static char *
63 xrealloc (ptr, size)
64 char *ptr;
65 unsigned size;
67 register char *tem = realloc (ptr, size);
69 if (!tem)
70 memory_out ();
71 return tem;
73 #endif /* not emacs */
75 /* Assuming STRING is the value of a termcap string entry
76 containing `%' constructs to expand parameters,
77 merge in parameter values and store result in block OUTSTRING points to.
78 LEN is the length of OUTSTRING. If more space is needed,
79 a block is allocated with `malloc'.
81 The value returned is the address of the resulting string.
82 This may be OUTSTRING or may be the address of a block got with `malloc'.
83 In the latter case, the caller must free the block.
85 The fourth and following args to tparam serve as the parameter values. */
87 static char *tparam1 ();
89 /* VARARGS 2 */
90 char *
91 tparam (string, outstring, len, arg0, arg1, arg2, arg3)
92 char *string;
93 char *outstring;
94 int len;
95 int arg0, arg1, arg2, arg3;
97 int arg[4];
99 arg[0] = arg0;
100 arg[1] = arg1;
101 arg[2] = arg2;
102 arg[3] = arg3;
103 return tparam1 (string, outstring, len, NULL, NULL, arg);
106 char *BC;
107 char *UP;
109 static char tgoto_buf[50];
111 char *
112 tgoto (cm, hpos, vpos)
113 char *cm;
114 int hpos, vpos;
116 int args[2];
117 if (!cm)
118 return NULL;
119 args[0] = vpos;
120 args[1] = hpos;
121 return tparam1 (cm, tgoto_buf, 50, UP, BC, args);
124 static char *
125 tparam1 (string, outstring, len, up, left, argp)
126 char *string;
127 char *outstring;
128 int len;
129 char *up, *left;
130 register int *argp;
132 register int c;
133 register char *p = string;
134 register char *op = outstring;
135 char *outend;
136 int outlen = 0;
138 register int tem;
139 int *old_argp = argp;
140 int doleft = 0;
141 int doup = 0;
143 outend = outstring + len;
145 while (1)
147 /* If the buffer might be too short, make it bigger. */
148 if (op + 5 >= outend)
150 register char *new;
151 if (outlen == 0)
153 outlen = len + 40;
154 new = (char *) xmalloc (outlen);
155 outend += 40;
156 bcopy (outstring, new, op - outstring);
158 else
160 outend += outlen;
161 outlen *= 2;
162 new = (char *) xrealloc (outstring, outlen);
164 op += new - outstring;
165 outend += new - outstring;
166 outstring = new;
168 c = *p++;
169 if (!c)
170 break;
171 if (c == '%')
173 c = *p++;
174 tem = *argp;
175 switch (c)
177 case 'd': /* %d means output in decimal. */
178 if (tem < 10)
179 goto onedigit;
180 if (tem < 100)
181 goto twodigit;
182 case '3': /* %3 means output in decimal, 3 digits. */
183 if (tem > 999)
185 *op++ = tem / 1000 + '0';
186 tem %= 1000;
188 *op++ = tem / 100 + '0';
189 case '2': /* %2 means output in decimal, 2 digits. */
190 twodigit:
191 tem %= 100;
192 *op++ = tem / 10 + '0';
193 onedigit:
194 *op++ = tem % 10 + '0';
195 argp++;
196 break;
198 case 'C':
199 /* For c-100: print quotient of value by 96, if nonzero,
200 then do like %+. */
201 if (tem >= 96)
203 *op++ = tem / 96;
204 tem %= 96;
206 case '+': /* %+x means add character code of char x. */
207 tem += *p++;
208 case '.': /* %. means output as character. */
209 if (left)
211 /* If want to forbid output of 0 and \n and \t,
212 and this is one of them, increment it. */
213 while (tem == 0 || tem == '\n' || tem == '\t')
215 tem++;
216 if (argp == old_argp)
217 doup++, outend -= strlen (up);
218 else
219 doleft++, outend -= strlen (left);
222 *op++ = tem ? tem : 0200;
223 case 'f': /* %f means discard next arg. */
224 argp++;
225 break;
227 case 'b': /* %b means back up one arg (and re-use it). */
228 argp--;
229 break;
231 case 'r': /* %r means interchange following two args. */
232 argp[0] = argp[1];
233 argp[1] = tem;
234 old_argp++;
235 break;
237 case '>': /* %>xy means if arg is > char code of x, */
238 if (argp[0] > *p++) /* then add char code of y to the arg, */
239 argp[0] += *p; /* and in any case don't output. */
240 p++; /* Leave the arg to be output later. */
241 break;
243 case 'a': /* %a means arithmetic. */
244 /* Next character says what operation.
245 Add or subtract either a constant or some other arg. */
246 /* First following character is + to add or - to subtract
247 or = to assign. */
248 /* Next following char is 'p' and an arg spec
249 (0100 plus position of that arg relative to this one)
250 or 'c' and a constant stored in a character. */
251 tem = p[2] & 0177;
252 if (p[1] == 'p')
253 tem = argp[tem - 0100];
254 if (p[0] == '-')
255 argp[0] -= tem;
256 else if (p[0] == '+')
257 argp[0] += tem;
258 else if (p[0] == '*')
259 argp[0] *= tem;
260 else if (p[0] == '/')
261 argp[0] /= tem;
262 else
263 argp[0] = tem;
265 p += 3;
266 break;
268 case 'i': /* %i means add one to arg, */
269 argp[0] ++; /* and leave it to be output later. */
270 argp[1] ++; /* Increment the following arg, too! */
271 break;
273 case '%': /* %% means output %; no arg. */
274 goto ordinary;
276 case 'n': /* %n means xor each of next two args with 140. */
277 argp[0] ^= 0140;
278 argp[1] ^= 0140;
279 break;
281 case 'm': /* %m means xor each of next two args with 177. */
282 argp[0] ^= 0177;
283 argp[1] ^= 0177;
284 break;
286 case 'B': /* %B means express arg as BCD char code. */
287 argp[0] += 6 * (tem / 10);
288 break;
290 case 'D': /* %D means weird Delta Data transformation. */
291 argp[0] -= 2 * (tem % 16);
292 break;
295 else
296 /* Ordinary character in the argument string. */
297 ordinary:
298 *op++ = c;
300 *op = 0;
301 while (doup-- > 0)
302 strcat (op, up);
303 while (doleft-- > 0)
304 strcat (op, left);
305 return outstring;
308 #ifdef DEBUG
310 main (argc, argv)
311 int argc;
312 char **argv;
314 char buf[50];
315 int args[3];
316 args[0] = atoi (argv[2]);
317 args[1] = atoi (argv[3]);
318 args[2] = atoi (argv[4]);
319 tparam1 (argv[1], buf, "LEFT", "UP", args);
320 printf ("%s\n", buf);
321 return 0;
324 #endif /* DEBUG */