1 /* Merge parameters into a termcap entry string.
2 Copyright (C) 1985, 1987, 1993, 1995, 2000, 2002, 2003, 2004,
3 2005, 2006 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)
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. */
26 #include "lisp.h" /* for xmalloc */
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))
42 #endif /* not emacs */
45 #define NULL (char *) 0
52 write (2, "virtual memory exhausted\n", 25);
60 register char *tem
= malloc (size
);
72 register char *tem
= realloc (ptr
, size
);
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 ();
96 tparam (string
, outstring
, len
, arg0
, arg1
, arg2
, arg3
)
100 int arg0
, arg1
, arg2
, 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. */
117 static char tgoto_buf
[50];
120 tgoto (cm
, hpos
, vpos
)
129 return tparam1 (cm
, tgoto_buf
, 50, UP
, BC
, args
);
134 tparam1 (string
, outstring
, len
, up
, left
, argp
)
142 register char *p
= string
;
143 register char *op
= outstring
;
148 int *old_argp
= argp
; /* can move */
149 int *fixed_argp
= argp
; /* never moves */
150 int explicit_param_p
= 0; /* set by %p */
154 outend
= outstring
+ len
;
158 /* If the buffer might be too short, make it bigger. */
159 if (op
+ 5 >= outend
)
162 int offset
= op
- outstring
;
167 new = (char *) xmalloc (outlen
);
168 bcopy (outstring
, new, offset
);
173 new = (char *) xrealloc (outstring
, outlen
);
177 outend
= new + outlen
;
186 if (explicit_param_p
)
187 explicit_param_p
= 0;
192 case 'd': /* %d means output in decimal. */
197 case '3': /* %3 means output in decimal, 3 digits. */
200 *op
++ = tem
/ 1000 + '0';
203 *op
++ = tem
/ 100 + '0';
204 case '2': /* %2 means output in decimal, 2 digits. */
207 *op
++ = tem
/ 10 + '0';
209 *op
++ = tem
% 10 + '0';
212 case 'p': /* %pN means use param N for next subst. */
213 tem
= fixed_argp
[(*p
++) - '1'];
214 explicit_param_p
= 1;
217 /* For c-100: print quotient of value by 96, if nonzero,
224 case '+': /* %+x means add character code of char x. */
226 case '.': /* %. means output as character. */
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')
234 if (argp
== old_argp
)
235 doup
++, outend
-= strlen (up
);
237 doleft
++, outend
-= strlen (left
);
240 *op
++ = tem
? tem
: 0200;
241 case 'f': /* %f means discard next arg. */
245 case 'b': /* %b means back up one arg (and re-use it). */
249 case 'r': /* %r means interchange following two args. */
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. */
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
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. */
271 tem
= argp
[tem
- 0100];
274 else if (p
[0] == '+')
276 else if (p
[0] == '*')
278 else if (p
[0] == '/')
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! */
291 case '%': /* %% means output %; no arg. */
294 case 'n': /* %n means xor each of next two args with 140. */
299 case 'm': /* %m means xor each of next two args with 177. */
304 case 'B': /* %B means express arg as BCD char code. */
305 argp
[0] += 6 * (tem
/ 10);
308 case 'D': /* %D means weird Delta Data transformation. */
309 argp
[0] -= 2 * (tem
% 16);
317 /* Ordinary character in the argument string. */
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
);
347 /* arch-tag: 83f7b5ac-a808-4f75-b87a-123de009b402
348 (do not change this comment) */