1 /* Merge parameters into a termcap entry string.
2 Copyright (C) 1985, 87, 93, 95, 2000 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)
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., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA. */
19 /* Emacs config.h may rename various library functions such as malloc. */
25 #include "lisp.h" /* for xmalloc */
36 /* Do this after the include, in case string.h prototypes bcopy. */
37 #if (defined(HAVE_STRING_H) || defined(STDC_HEADERS)) && !defined(bcopy)
38 #define bcopy(s, d, n) memcpy ((d), (s), (n))
41 #endif /* not emacs */
44 #define NULL (char *) 0
51 write (2, "virtual memory exhausted\n", 25);
59 register char *tem
= malloc (size
);
71 register char *tem
= realloc (ptr
, size
);
77 #endif /* not emacs */
79 /* Assuming STRING is the value of a termcap string entry
80 containing `%' constructs to expand parameters,
81 merge in parameter values and store result in block OUTSTRING points to.
82 LEN is the length of OUTSTRING. If more space is needed,
83 a block is allocated with `malloc'.
85 The value returned is the address of the resulting string.
86 This may be OUTSTRING or may be the address of a block got with `malloc'.
87 In the latter case, the caller must free the block.
89 The fourth and following args to tparam serve as the parameter values. */
91 static char *tparam1 ();
95 tparam (string
, outstring
, len
, arg0
, arg1
, arg2
, arg3
)
99 int arg0
, arg1
, arg2
, arg3
;
107 return tparam1 (string
, outstring
, len
, NULL
, NULL
, arg
);
110 /* These are already defined in the System framework in Mac OS X and
111 cause prebinding to fail. */
116 static char tgoto_buf
[50];
119 tgoto (cm
, hpos
, vpos
)
128 return tparam1 (cm
, tgoto_buf
, 50, UP
, BC
, args
);
133 tparam1 (string
, outstring
, len
, up
, left
, argp
)
141 register char *p
= string
;
142 register char *op
= outstring
;
147 int *old_argp
= argp
; /* can move */
148 int *fixed_argp
= argp
; /* never moves */
149 int explicit_param_p
= 0; /* set by %p */
153 outend
= outstring
+ len
;
157 /* If the buffer might be too short, make it bigger. */
158 if (op
+ 5 >= outend
)
161 int offset
= op
- outstring
;
166 new = (char *) xmalloc (outlen
);
167 bcopy (outstring
, new, offset
);
172 new = (char *) xrealloc (outstring
, outlen
);
176 outend
= new + outlen
;
185 if (explicit_param_p
)
186 explicit_param_p
= 0;
191 case 'd': /* %d means output in decimal. */
196 case '3': /* %3 means output in decimal, 3 digits. */
199 *op
++ = tem
/ 1000 + '0';
202 *op
++ = tem
/ 100 + '0';
203 case '2': /* %2 means output in decimal, 2 digits. */
206 *op
++ = tem
/ 10 + '0';
208 *op
++ = tem
% 10 + '0';
211 case 'p': /* %pN means use param N for next subst. */
212 tem
= fixed_argp
[(*p
++) - '1'];
213 explicit_param_p
= 1;
216 /* For c-100: print quotient of value by 96, if nonzero,
223 case '+': /* %+x means add character code of char x. */
225 case '.': /* %. means output as character. */
228 /* If want to forbid output of 0 and \n and \t,
229 and this is one of them, increment it. */
230 while (tem
== 0 || tem
== '\n' || tem
== '\t')
233 if (argp
== old_argp
)
234 doup
++, outend
-= strlen (up
);
236 doleft
++, outend
-= strlen (left
);
239 *op
++ = tem
? tem
: 0200;
240 case 'f': /* %f means discard next arg. */
244 case 'b': /* %b means back up one arg (and re-use it). */
248 case 'r': /* %r means interchange following two args. */
254 case '>': /* %>xy means if arg is > char code of x, */
255 if (argp
[0] > *p
++) /* then add char code of y to the arg, */
256 argp
[0] += *p
; /* and in any case don't output. */
257 p
++; /* Leave the arg to be output later. */
260 case 'a': /* %a means arithmetic. */
261 /* Next character says what operation.
262 Add or subtract either a constant or some other arg. */
263 /* First following character is + to add or - to subtract
265 /* Next following char is 'p' and an arg spec
266 (0100 plus position of that arg relative to this one)
267 or 'c' and a constant stored in a character. */
270 tem
= argp
[tem
- 0100];
273 else if (p
[0] == '+')
275 else if (p
[0] == '*')
277 else if (p
[0] == '/')
285 case 'i': /* %i means add one to arg, */
286 argp
[0] ++; /* and leave it to be output later. */
287 argp
[1] ++; /* Increment the following arg, too! */
290 case '%': /* %% means output %; no arg. */
293 case 'n': /* %n means xor each of next two args with 140. */
298 case 'm': /* %m means xor each of next two args with 177. */
303 case 'B': /* %B means express arg as BCD char code. */
304 argp
[0] += 6 * (tem
/ 10);
307 case 'D': /* %D means weird Delta Data transformation. */
308 argp
[0] -= 2 * (tem
% 16);
316 /* Ordinary character in the argument string. */
336 args
[0] = atoi (argv
[2]);
337 args
[1] = atoi (argv
[3]);
338 args
[2] = atoi (argv
[4]);
339 tparam1 (argv
[1], buf
, "LEFT", "UP", args
);
340 printf ("%s\n", buf
);
346 /* arch-tag: 83f7b5ac-a808-4f75-b87a-123de009b402
347 (do not change this comment) */