(parse-time-string): Simplify.
[emacs.git] / src / tparam.c
blob8f7e79c32f79e340a3352552fa9ca3bbc04082aa
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.
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.
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.
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. */
20 /* Emacs config.h may rename various library functions such as malloc. */
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
25 #ifdef emacs
26 #include "lisp.h" /* for xmalloc */
27 #else
29 #ifdef STDC_HEADERS
30 #include <stdlib.h>
31 #include <string.h>
32 #else
33 char *malloc ();
34 char *realloc ();
35 #endif
37 /* Do this after the include, in case string.h prototypes bcopy. */
38 #if (defined(HAVE_STRING_H) || defined(STDC_HEADERS)) && !defined(bcopy)
39 #define bcopy(s, d, n) memcpy ((d), (s), (n))
40 #endif
42 #endif /* not emacs */
44 #ifndef NULL
45 #define NULL (char *) 0
46 #endif
48 #ifndef emacs
49 static void
50 memory_out ()
52 write (2, "virtual memory exhausted\n", 25);
53 exit (1);
56 static char *
57 xmalloc (size)
58 unsigned size;
60 register char *tem = malloc (size);
62 if (!tem)
63 memory_out ();
64 return tem;
67 static char *
68 xrealloc (ptr, size)
69 char *ptr;
70 unsigned size;
72 register char *tem = realloc (ptr, size);
74 if (!tem)
75 memory_out ();
76 return tem;
78 #endif /* not emacs */
80 /* Assuming STRING is the value of a termcap string entry
81 containing `%' constructs to expand parameters,
82 merge in parameter values and store result in block OUTSTRING points to.
83 LEN is the length of OUTSTRING. If more space is needed,
84 a block is allocated with `malloc'.
86 The value returned is the address of the resulting string.
87 This may be OUTSTRING or may be the address of a block got with `malloc'.
88 In the latter case, the caller must free the block.
90 The fourth and following args to tparam serve as the parameter values. */
92 static char *tparam1 ();
94 /* VARARGS 2 */
95 char *
96 tparam (string, outstring, len, arg0, arg1, arg2, arg3)
97 char *string;
98 char *outstring;
99 int len;
100 int arg0, arg1, arg2, arg3;
102 int arg[4];
104 arg[0] = arg0;
105 arg[1] = arg1;
106 arg[2] = arg2;
107 arg[3] = arg3;
108 return tparam1 (string, outstring, len, NULL, NULL, arg);
111 /* These are already defined in the System framework in Mac OS X and
112 cause prebinding to fail. */
113 #ifndef MAC_OSX
114 char *BC;
115 char *UP;
117 static char tgoto_buf[50];
119 char *
120 tgoto (cm, hpos, vpos)
121 char *cm;
122 int hpos, vpos;
124 int args[2];
125 if (!cm)
126 return NULL;
127 args[0] = vpos;
128 args[1] = hpos;
129 return tparam1 (cm, tgoto_buf, 50, UP, BC, args);
131 #endif
133 static char *
134 tparam1 (string, outstring, len, up, left, argp)
135 char *string;
136 char *outstring;
137 int len;
138 char *up, *left;
139 register int *argp;
141 register int c;
142 register char *p = string;
143 register char *op = outstring;
144 char *outend;
145 int outlen = 0;
147 register int tem;
148 int *old_argp = argp; /* can move */
149 int *fixed_argp = argp; /* never moves */
150 int explicit_param_p = 0; /* set by %p */
151 int doleft = 0;
152 int doup = 0;
154 outend = outstring + len;
156 while (1)
158 /* If the buffer might be too short, make it bigger. */
159 if (op + 5 >= outend)
161 register char *new;
162 int offset = op - outstring;
164 if (outlen == 0)
166 outlen = len + 40;
167 new = (char *) xmalloc (outlen);
168 bcopy (outstring, new, offset);
170 else
172 outlen *= 2;
173 new = (char *) xrealloc (outstring, outlen);
176 op = new + offset;
177 outend = new + outlen;
178 outstring = new;
180 c = *p++;
181 if (!c)
182 break;
183 if (c == '%')
185 c = *p++;
186 if (explicit_param_p)
187 explicit_param_p = 0;
188 else
189 tem = *argp;
190 switch (c)
192 case 'd': /* %d means output in decimal. */
193 if (tem < 10)
194 goto onedigit;
195 if (tem < 100)
196 goto twodigit;
197 case '3': /* %3 means output in decimal, 3 digits. */
198 if (tem > 999)
200 *op++ = tem / 1000 + '0';
201 tem %= 1000;
203 *op++ = tem / 100 + '0';
204 case '2': /* %2 means output in decimal, 2 digits. */
205 twodigit:
206 tem %= 100;
207 *op++ = tem / 10 + '0';
208 onedigit:
209 *op++ = tem % 10 + '0';
210 argp++;
211 break;
212 case 'p': /* %pN means use param N for next subst. */
213 tem = fixed_argp[(*p++) - '1'];
214 explicit_param_p = 1;
215 break;
216 case 'C':
217 /* For c-100: print quotient of value by 96, if nonzero,
218 then do like %+. */
219 if (tem >= 96)
221 *op++ = tem / 96;
222 tem %= 96;
224 case '+': /* %+x means add character code of char x. */
225 tem += *p++;
226 case '.': /* %. means output as character. */
227 if (left)
229 /* If want to forbid output of 0 and \n and \t,
230 and this is one of them, increment it. */
231 while (tem == 0 || tem == '\n' || tem == '\t')
233 tem++;
234 if (argp == old_argp)
235 doup++, outend -= strlen (up);
236 else
237 doleft++, outend -= strlen (left);
240 *op++ = tem ? tem : 0200;
241 case 'f': /* %f means discard next arg. */
242 argp++;
243 break;
245 case 'b': /* %b means back up one arg (and re-use it). */
246 argp--;
247 break;
249 case 'r': /* %r means interchange following two args. */
250 argp[0] = argp[1];
251 argp[1] = tem;
252 old_argp++;
253 break;
255 case '>': /* %>xy means if arg is > char code of x, */
256 if (argp[0] > *p++) /* then add char code of y to the arg, */
257 argp[0] += *p; /* and in any case don't output. */
258 p++; /* Leave the arg to be output later. */
259 break;
261 case 'a': /* %a means arithmetic. */
262 /* Next character says what operation.
263 Add or subtract either a constant or some other arg. */
264 /* First following character is + to add or - to subtract
265 or = to assign. */
266 /* Next following char is 'p' and an arg spec
267 (0100 plus position of that arg relative to this one)
268 or 'c' and a constant stored in a character. */
269 tem = p[2] & 0177;
270 if (p[1] == 'p')
271 tem = argp[tem - 0100];
272 if (p[0] == '-')
273 argp[0] -= tem;
274 else if (p[0] == '+')
275 argp[0] += tem;
276 else if (p[0] == '*')
277 argp[0] *= tem;
278 else if (p[0] == '/')
279 argp[0] /= tem;
280 else
281 argp[0] = tem;
283 p += 3;
284 break;
286 case 'i': /* %i means add one to arg, */
287 argp[0] ++; /* and leave it to be output later. */
288 argp[1] ++; /* Increment the following arg, too! */
289 break;
291 case '%': /* %% means output %; no arg. */
292 goto ordinary;
294 case 'n': /* %n means xor each of next two args with 140. */
295 argp[0] ^= 0140;
296 argp[1] ^= 0140;
297 break;
299 case 'm': /* %m means xor each of next two args with 177. */
300 argp[0] ^= 0177;
301 argp[1] ^= 0177;
302 break;
304 case 'B': /* %B means express arg as BCD char code. */
305 argp[0] += 6 * (tem / 10);
306 break;
308 case 'D': /* %D means weird Delta Data transformation. */
309 argp[0] -= 2 * (tem % 16);
310 break;
312 default:
313 abort ();
316 else
317 /* Ordinary character in the argument string. */
318 ordinary:
319 *op++ = c;
321 *op = 0;
322 while (doup-- > 0)
323 strcat (op, up);
324 while (doleft-- > 0)
325 strcat (op, left);
326 return outstring;
329 #ifdef DEBUG
331 main (argc, argv)
332 int argc;
333 char **argv;
335 char buf[50];
336 int args[3];
337 args[0] = atoi (argv[2]);
338 args[1] = atoi (argv[3]);
339 args[2] = atoi (argv[4]);
340 tparam1 (argv[1], buf, "LEFT", "UP", args);
341 printf ("%s\n", buf);
342 return 0;
345 #endif /* DEBUG */
347 /* arch-tag: 83f7b5ac-a808-4f75-b87a-123de009b402
348 (do not change this comment) */