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)
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. */
23 #include "lisp.h" /* for xmalloc */
26 /* Assuming STRING is the value of a termcap string entry
27 containing `%' constructs to expand parameters,
28 merge in parameter values and store result in block OUTSTRING points to.
29 LEN is the length of OUTSTRING. If more space is needed,
30 a block is allocated with `malloc'.
32 The value returned is the address of the resulting string.
33 This may be OUTSTRING or may be the address of a block got with `malloc'.
34 In the latter case, the caller must free the block.
36 The fourth and following args to tparam serve as the parameter values. */
38 static char *tparam1 (char const *string
, char *outstring
, int len
,
39 char *up
, char *left
, int *argp
);
42 tparam (const char *string
, char *outstring
, int len
,
43 int arg0
, int arg1
, int arg2
, int arg3
)
51 return tparam1 (string
, outstring
, len
, NULL
, NULL
, arg
);
57 static char tgoto_buf
[50];
60 tgoto (const char *cm
, int hpos
, int vpos
)
67 return tparam1 (cm
, tgoto_buf
, 50, UP
, BC
, args
);
71 tparam1 (const char *string
, char *outstring
, int len
,
72 char *up
, char *left
, register int *argp
)
75 register const char *p
= string
;
76 register char *op
= outstring
;
82 int *old_argp
= argp
; /* can move */
83 int *fixed_argp
= argp
; /* never moves */
84 int explicit_param_p
= 0; /* set by %p */
87 ptrdiff_t append_len
= 0;
89 outend
= outstring
+ len
;
93 /* If the buffer might be too short, make it bigger. */
94 while (outend
- op
- append_len
<= 5)
96 ptrdiff_t offset
= op
- outstring
;
101 new = xmalloc (outlen
);
102 memcpy (new, outstring
, offset
);
106 new = xpalloc (outstring
, &outlen
, 1, -1, 1);
110 outend
= new + outlen
;
119 if (explicit_param_p
)
120 explicit_param_p
= 0;
125 case 'd': /* %d means output in decimal. */
130 case '3': /* %3 means output in decimal, 3 digits. */
133 *op
++ = tem
/ 1000 + '0';
136 *op
++ = tem
/ 100 + '0';
137 case '2': /* %2 means output in decimal, 2 digits. */
140 *op
++ = tem
/ 10 + '0';
142 *op
++ = tem
% 10 + '0';
145 case 'p': /* %pN means use param N for next subst. */
146 tem
= fixed_argp
[(*p
++) - '1'];
147 explicit_param_p
= 1;
150 /* For c-100: print quotient of value by 96, if nonzero,
157 case '+': /* %+x means add character code of char x. */
159 case '.': /* %. means output as character. */
162 /* If want to forbid output of 0 and \n and \t,
163 and this is one of them, increment it. */
164 while (tem
== 0 || tem
== '\n' || tem
== '\t')
166 ptrdiff_t append_len_incr
;
168 if (argp
== old_argp
)
169 doup
++, append_len_incr
= strlen (up
);
171 doleft
++, append_len_incr
= strlen (left
);
172 if (INT_ADD_OVERFLOW (append_len
, append_len_incr
))
173 memory_full (SIZE_MAX
);
174 append_len
+= append_len_incr
;
177 *op
++ = tem
? tem
: 0200;
178 case 'f': /* %f means discard next arg. */
182 case 'b': /* %b means back up one arg (and re-use it). */
186 case 'r': /* %r means interchange following two args. */
192 case '>': /* %>xy means if arg is > char code of x, */
193 if (argp
[0] > *p
++) /* then add char code of y to the arg, */
194 argp
[0] += *p
; /* and in any case don't output. */
195 p
++; /* Leave the arg to be output later. */
198 case 'a': /* %a means arithmetic. */
199 /* Next character says what operation.
200 Add or subtract either a constant or some other arg. */
201 /* First following character is + to add or - to subtract
203 /* Next following char is 'p' and an arg spec
204 (0100 plus position of that arg relative to this one)
205 or 'c' and a constant stored in a character. */
208 tem
= argp
[tem
- 0100];
211 else if (p
[0] == '+')
213 else if (p
[0] == '*')
215 else if (p
[0] == '/')
223 case 'i': /* %i means add one to arg, */
224 argp
[0] ++; /* and leave it to be output later. */
225 argp
[1] ++; /* Increment the following arg, too! */
228 case '%': /* %% means output %; no arg. */
231 case 'n': /* %n means xor each of next two args with 140. */
236 case 'm': /* %m means xor each of next two args with 177. */
241 case 'B': /* %B means express arg as BCD char code. */
242 argp
[0] += 6 * (tem
/ 10);
245 case 'D': /* %D means weird Delta Data transformation. */
246 argp
[0] -= 2 * (tem
% 16);
254 /* Ordinary character in the argument string. */
269 main (int argc
, char **argv
)
273 args
[0] = atoi (argv
[2]);
274 args
[1] = atoi (argv
[3]);
275 args
[2] = atoi (argv
[4]);
276 tparam1 (argv
[1], buf
, 50, "LEFT", "UP", args
);
277 printf ("%s\n", buf
);