Commit | Line | Data |
---|---|---|
e0dd62f6 | 1 | /* Merge parameters into a termcap entry string. |
429ab54e | 2 | Copyright (C) 1985, 1987, 1993, 1995, 2000, 2001, 2002, 2003, 2004, |
0a64641e | 3 | 2005, 2006, 2007, 2008 Free Software Foundation, Inc. |
e0dd62f6 RM |
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 | |
4fc5845f LK |
17 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
18 | Boston, MA 02110-1301, USA. */ | |
e0dd62f6 RM |
19 | |
20 | /* Emacs config.h may rename various library functions such as malloc. */ | |
e0dd62f6 | 21 | #include <config.h> |
d7306fe6 | 22 | #include <setjmp.h> |
83c2ece5 | 23 | #include "lisp.h" /* for xmalloc */ |
e0dd62f6 RM |
24 | |
25 | #ifndef NULL | |
26 | #define NULL (char *) 0 | |
27 | #endif | |
28 | \f | |
e0dd62f6 RM |
29 | /* Assuming STRING is the value of a termcap string entry |
30 | containing `%' constructs to expand parameters, | |
31 | merge in parameter values and store result in block OUTSTRING points to. | |
32 | LEN is the length of OUTSTRING. If more space is needed, | |
33 | a block is allocated with `malloc'. | |
34 | ||
35 | The value returned is the address of the resulting string. | |
36 | This may be OUTSTRING or may be the address of a block got with `malloc'. | |
37 | In the latter case, the caller must free the block. | |
38 | ||
39 | The fourth and following args to tparam serve as the parameter values. */ | |
40 | ||
971de7fb | 41 | static char *tparam1 (char *string, char *outstring, int len, char *up, char *left, register int *argp); |
e0dd62f6 RM |
42 | |
43 | /* VARARGS 2 */ | |
44 | char * | |
971de7fb | 45 | tparam (char *string, char *outstring, int len, int arg0, int arg1, int arg2, int arg3) |
e0dd62f6 | 46 | { |
e0dd62f6 | 47 | int arg[4]; |
9e3295f9 | 48 | |
e0dd62f6 RM |
49 | arg[0] = arg0; |
50 | arg[1] = arg1; | |
51 | arg[2] = arg2; | |
52 | arg[3] = arg3; | |
53 | return tparam1 (string, outstring, len, NULL, NULL, arg); | |
e0dd62f6 RM |
54 | } |
55 | ||
56 | char *BC; | |
57 | char *UP; | |
58 | ||
59 | static char tgoto_buf[50]; | |
60 | ||
61 | char * | |
971de7fb | 62 | tgoto (char *cm, int hpos, int vpos) |
e0dd62f6 RM |
63 | { |
64 | int args[2]; | |
65 | if (!cm) | |
66 | return NULL; | |
67 | args[0] = vpos; | |
68 | args[1] = hpos; | |
69 | return tparam1 (cm, tgoto_buf, 50, UP, BC, args); | |
70 | } | |
71 | ||
72 | static char * | |
971de7fb | 73 | tparam1 (char *string, char *outstring, int len, char *up, char *left, register int *argp) |
e0dd62f6 RM |
74 | { |
75 | register int c; | |
76 | register char *p = string; | |
77 | register char *op = outstring; | |
78 | char *outend; | |
79 | int outlen = 0; | |
80 | ||
81 | register int tem; | |
9ad4bf7a TTN |
82 | int *old_argp = argp; /* can move */ |
83 | int *fixed_argp = argp; /* never moves */ | |
84 | int explicit_param_p = 0; /* set by %p */ | |
e0dd62f6 RM |
85 | int doleft = 0; |
86 | int doup = 0; | |
87 | ||
88 | outend = outstring + len; | |
89 | ||
90 | while (1) | |
91 | { | |
92 | /* If the buffer might be too short, make it bigger. */ | |
93 | if (op + 5 >= outend) | |
94 | { | |
95 | register char *new; | |
b5e55477 GM |
96 | int offset = op - outstring; |
97 | ||
e0dd62f6 RM |
98 | if (outlen == 0) |
99 | { | |
100 | outlen = len + 40; | |
101 | new = (char *) xmalloc (outlen); | |
72af86bd | 102 | memcpy (new, outstring, offset); |
e0dd62f6 RM |
103 | } |
104 | else | |
105 | { | |
e0dd62f6 RM |
106 | outlen *= 2; |
107 | new = (char *) xrealloc (outstring, outlen); | |
108 | } | |
177c0ea7 | 109 | |
b5e55477 GM |
110 | op = new + offset; |
111 | outend = new + outlen; | |
e0dd62f6 RM |
112 | outstring = new; |
113 | } | |
114 | c = *p++; | |
115 | if (!c) | |
116 | break; | |
117 | if (c == '%') | |
118 | { | |
119 | c = *p++; | |
9ad4bf7a TTN |
120 | if (explicit_param_p) |
121 | explicit_param_p = 0; | |
122 | else | |
123 | tem = *argp; | |
e0dd62f6 RM |
124 | switch (c) |
125 | { | |
126 | case 'd': /* %d means output in decimal. */ | |
127 | if (tem < 10) | |
128 | goto onedigit; | |
129 | if (tem < 100) | |
130 | goto twodigit; | |
131 | case '3': /* %3 means output in decimal, 3 digits. */ | |
132 | if (tem > 999) | |
133 | { | |
134 | *op++ = tem / 1000 + '0'; | |
135 | tem %= 1000; | |
136 | } | |
137 | *op++ = tem / 100 + '0'; | |
138 | case '2': /* %2 means output in decimal, 2 digits. */ | |
139 | twodigit: | |
140 | tem %= 100; | |
141 | *op++ = tem / 10 + '0'; | |
142 | onedigit: | |
143 | *op++ = tem % 10 + '0'; | |
144 | argp++; | |
145 | break; | |
9ad4bf7a TTN |
146 | case 'p': /* %pN means use param N for next subst. */ |
147 | tem = fixed_argp[(*p++) - '1']; | |
148 | explicit_param_p = 1; | |
149 | break; | |
e0dd62f6 RM |
150 | case 'C': |
151 | /* For c-100: print quotient of value by 96, if nonzero, | |
152 | then do like %+. */ | |
153 | if (tem >= 96) | |
154 | { | |
155 | *op++ = tem / 96; | |
156 | tem %= 96; | |
157 | } | |
158 | case '+': /* %+x means add character code of char x. */ | |
159 | tem += *p++; | |
160 | case '.': /* %. means output as character. */ | |
161 | if (left) | |
162 | { | |
163 | /* If want to forbid output of 0 and \n and \t, | |
164 | and this is one of them, increment it. */ | |
165 | while (tem == 0 || tem == '\n' || tem == '\t') | |
166 | { | |
167 | tem++; | |
168 | if (argp == old_argp) | |
169 | doup++, outend -= strlen (up); | |
170 | else | |
171 | doleft++, outend -= strlen (left); | |
172 | } | |
173 | } | |
174 | *op++ = tem ? tem : 0200; | |
175 | case 'f': /* %f means discard next arg. */ | |
176 | argp++; | |
177 | break; | |
178 | ||
179 | case 'b': /* %b means back up one arg (and re-use it). */ | |
180 | argp--; | |
181 | break; | |
182 | ||
183 | case 'r': /* %r means interchange following two args. */ | |
184 | argp[0] = argp[1]; | |
185 | argp[1] = tem; | |
186 | old_argp++; | |
187 | break; | |
188 | ||
189 | case '>': /* %>xy means if arg is > char code of x, */ | |
190 | if (argp[0] > *p++) /* then add char code of y to the arg, */ | |
191 | argp[0] += *p; /* and in any case don't output. */ | |
192 | p++; /* Leave the arg to be output later. */ | |
193 | break; | |
194 | ||
195 | case 'a': /* %a means arithmetic. */ | |
196 | /* Next character says what operation. | |
197 | Add or subtract either a constant or some other arg. */ | |
198 | /* First following character is + to add or - to subtract | |
199 | or = to assign. */ | |
200 | /* Next following char is 'p' and an arg spec | |
201 | (0100 plus position of that arg relative to this one) | |
202 | or 'c' and a constant stored in a character. */ | |
203 | tem = p[2] & 0177; | |
204 | if (p[1] == 'p') | |
205 | tem = argp[tem - 0100]; | |
206 | if (p[0] == '-') | |
207 | argp[0] -= tem; | |
208 | else if (p[0] == '+') | |
209 | argp[0] += tem; | |
210 | else if (p[0] == '*') | |
211 | argp[0] *= tem; | |
212 | else if (p[0] == '/') | |
213 | argp[0] /= tem; | |
214 | else | |
215 | argp[0] = tem; | |
216 | ||
217 | p += 3; | |
218 | break; | |
219 | ||
220 | case 'i': /* %i means add one to arg, */ | |
221 | argp[0] ++; /* and leave it to be output later. */ | |
222 | argp[1] ++; /* Increment the following arg, too! */ | |
223 | break; | |
224 | ||
225 | case '%': /* %% means output %; no arg. */ | |
226 | goto ordinary; | |
227 | ||
228 | case 'n': /* %n means xor each of next two args with 140. */ | |
229 | argp[0] ^= 0140; | |
230 | argp[1] ^= 0140; | |
231 | break; | |
232 | ||
233 | case 'm': /* %m means xor each of next two args with 177. */ | |
234 | argp[0] ^= 0177; | |
235 | argp[1] ^= 0177; | |
236 | break; | |
237 | ||
238 | case 'B': /* %B means express arg as BCD char code. */ | |
239 | argp[0] += 6 * (tem / 10); | |
240 | break; | |
241 | ||
242 | case 'D': /* %D means weird Delta Data transformation. */ | |
243 | argp[0] -= 2 * (tem % 16); | |
244 | break; | |
45e0dd95 GM |
245 | |
246 | default: | |
247 | abort (); | |
e0dd62f6 RM |
248 | } |
249 | } | |
250 | else | |
251 | /* Ordinary character in the argument string. */ | |
252 | ordinary: | |
253 | *op++ = c; | |
254 | } | |
255 | *op = 0; | |
256 | while (doup-- > 0) | |
257 | strcat (op, up); | |
258 | while (doleft-- > 0) | |
259 | strcat (op, left); | |
260 | return outstring; | |
261 | } | |
262 | \f | |
263 | #ifdef DEBUG | |
264 | ||
265 | main (argc, argv) | |
266 | int argc; | |
267 | char **argv; | |
268 | { | |
269 | char buf[50]; | |
270 | int args[3]; | |
271 | args[0] = atoi (argv[2]); | |
272 | args[1] = atoi (argv[3]); | |
273 | args[2] = atoi (argv[4]); | |
274 | tparam1 (argv[1], buf, "LEFT", "UP", args); | |
275 | printf ("%s\n", buf); | |
276 | return 0; | |
277 | } | |
278 | ||
279 | #endif /* DEBUG */ | |
ab5796a9 | 280 |