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