Mercurial > emacs
annotate src/tparam.c @ 46937:e406ed027b71
Escape an `@'.
author | Miles Bader <miles@gnu.org> |
---|---|
date | Mon, 19 Aug 2002 01:05:17 +0000 |
parents | 01b93e5e53a7 |
children | 23a1cea22d13 |
rev | line source |
---|---|
4687 | 1 /* Merge parameters into a termcap entry string. |
34360
c6d74397e65f
(tparam1): Change the way buffers are reallocated to be
Gerd Moellmann <gerd@gnu.org>
parents:
29805
diff
changeset
|
2 Copyright (C) 1985, 87, 93, 95, 2000 Free Software Foundation, Inc. |
4687 | 3 |
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) | |
7 any later version. | |
8 | |
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. | |
13 | |
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 | |
14414 | 16 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
17 Boston, MA 02111-1307, USA. */ | |
4687 | 18 |
19 /* Emacs config.h may rename various library functions such as malloc. */ | |
20 #ifdef HAVE_CONFIG_H | |
21 #include <config.h> | |
12994
bd38619285f7
Don't assume that HAVE_CONFIG_H implies emacs.
David J. MacKenzie <djm@gnu.org>
parents:
12678
diff
changeset
|
22 #endif |
4687 | 23 |
29805 | 24 #ifdef emacs |
25 #include "lisp.h" /* for xmalloc */ | |
26 #else | |
4687 | 27 |
28 #ifdef STDC_HEADERS | |
29 #include <stdlib.h> | |
30 #include <string.h> | |
31 #else | |
32 char *malloc (); | |
33 char *realloc (); | |
34 #endif | |
35 | |
43675
b9ee14ccc0c8
[!emacs]: Move #define of bcopy to after string.h.
Richard M. Stallman <rms@gnu.org>
parents:
34360
diff
changeset
|
36 /* Do this after the include, in case string.h prototypes bcopy. */ |
b9ee14ccc0c8
[!emacs]: Move #define of bcopy to after string.h.
Richard M. Stallman <rms@gnu.org>
parents:
34360
diff
changeset
|
37 #if (defined(HAVE_STRING_H) || defined(STDC_HEADERS)) && !defined(bcopy) |
b9ee14ccc0c8
[!emacs]: Move #define of bcopy to after string.h.
Richard M. Stallman <rms@gnu.org>
parents:
34360
diff
changeset
|
38 #define bcopy(s, d, n) memcpy ((d), (s), (n)) |
b9ee14ccc0c8
[!emacs]: Move #define of bcopy to after string.h.
Richard M. Stallman <rms@gnu.org>
parents:
34360
diff
changeset
|
39 #endif |
b9ee14ccc0c8
[!emacs]: Move #define of bcopy to after string.h.
Richard M. Stallman <rms@gnu.org>
parents:
34360
diff
changeset
|
40 |
12994
bd38619285f7
Don't assume that HAVE_CONFIG_H implies emacs.
David J. MacKenzie <djm@gnu.org>
parents:
12678
diff
changeset
|
41 #endif /* not emacs */ |
4687 | 42 |
43 #ifndef NULL | |
44 #define NULL (char *) 0 | |
45 #endif | |
46 | |
47 #ifndef emacs | |
48 static void | |
49 memory_out () | |
50 { | |
51 write (2, "virtual memory exhausted\n", 25); | |
52 exit (1); | |
53 } | |
54 | |
55 static char * | |
56 xmalloc (size) | |
57 unsigned size; | |
58 { | |
59 register char *tem = malloc (size); | |
60 | |
61 if (!tem) | |
62 memory_out (); | |
63 return tem; | |
64 } | |
65 | |
66 static char * | |
67 xrealloc (ptr, size) | |
68 char *ptr; | |
69 unsigned size; | |
70 { | |
71 register char *tem = realloc (ptr, size); | |
72 | |
73 if (!tem) | |
74 memory_out (); | |
75 return tem; | |
76 } | |
77 #endif /* not emacs */ | |
78 | |
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'. | |
84 | |
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. | |
88 | |
89 The fourth and following args to tparam serve as the parameter values. */ | |
90 | |
91 static char *tparam1 (); | |
92 | |
93 /* VARARGS 2 */ | |
94 char * | |
95 tparam (string, outstring, len, arg0, arg1, arg2, arg3) | |
96 char *string; | |
97 char *outstring; | |
98 int len; | |
99 int arg0, arg1, arg2, arg3; | |
100 { | |
101 int arg[4]; | |
12678
8fc56d171ada
(tparam): Remove arg array and the #ifdef.
David J. MacKenzie <djm@gnu.org>
parents:
4687
diff
changeset
|
102 |
4687 | 103 arg[0] = arg0; |
104 arg[1] = arg1; | |
105 arg[2] = arg2; | |
106 arg[3] = arg3; | |
107 return tparam1 (string, outstring, len, NULL, NULL, arg); | |
108 } | |
109 | |
44890
01b93e5e53a7
Patch for building Emacs on Mac OS X. April 26, 2002. See ChangeLog,
Andrew Choi <akochoi@shaw.ca>
parents:
43675
diff
changeset
|
110 /* These are already defined in the System framework in Mac OS X and |
01b93e5e53a7
Patch for building Emacs on Mac OS X. April 26, 2002. See ChangeLog,
Andrew Choi <akochoi@shaw.ca>
parents:
43675
diff
changeset
|
111 cause prebinding to fail. */ |
01b93e5e53a7
Patch for building Emacs on Mac OS X. April 26, 2002. See ChangeLog,
Andrew Choi <akochoi@shaw.ca>
parents:
43675
diff
changeset
|
112 #ifndef MAC_OSX |
4687 | 113 char *BC; |
114 char *UP; | |
115 | |
116 static char tgoto_buf[50]; | |
117 | |
118 char * | |
119 tgoto (cm, hpos, vpos) | |
120 char *cm; | |
121 int hpos, vpos; | |
122 { | |
123 int args[2]; | |
124 if (!cm) | |
125 return NULL; | |
126 args[0] = vpos; | |
127 args[1] = hpos; | |
128 return tparam1 (cm, tgoto_buf, 50, UP, BC, args); | |
129 } | |
44890
01b93e5e53a7
Patch for building Emacs on Mac OS X. April 26, 2002. See ChangeLog,
Andrew Choi <akochoi@shaw.ca>
parents:
43675
diff
changeset
|
130 #endif |
4687 | 131 |
132 static char * | |
133 tparam1 (string, outstring, len, up, left, argp) | |
134 char *string; | |
135 char *outstring; | |
136 int len; | |
137 char *up, *left; | |
138 register int *argp; | |
139 { | |
140 register int c; | |
141 register char *p = string; | |
142 register char *op = outstring; | |
143 char *outend; | |
144 int outlen = 0; | |
145 | |
146 register int tem; | |
147 int *old_argp = argp; | |
148 int doleft = 0; | |
149 int doup = 0; | |
150 | |
151 outend = outstring + len; | |
152 | |
153 while (1) | |
154 { | |
155 /* If the buffer might be too short, make it bigger. */ | |
156 if (op + 5 >= outend) | |
157 { | |
158 register char *new; | |
34360
c6d74397e65f
(tparam1): Change the way buffers are reallocated to be
Gerd Moellmann <gerd@gnu.org>
parents:
29805
diff
changeset
|
159 int offset = op - outstring; |
c6d74397e65f
(tparam1): Change the way buffers are reallocated to be
Gerd Moellmann <gerd@gnu.org>
parents:
29805
diff
changeset
|
160 |
4687 | 161 if (outlen == 0) |
162 { | |
163 outlen = len + 40; | |
164 new = (char *) xmalloc (outlen); | |
34360
c6d74397e65f
(tparam1): Change the way buffers are reallocated to be
Gerd Moellmann <gerd@gnu.org>
parents:
29805
diff
changeset
|
165 bcopy (outstring, new, offset); |
4687 | 166 } |
167 else | |
168 { | |
169 outlen *= 2; | |
170 new = (char *) xrealloc (outstring, outlen); | |
171 } | |
34360
c6d74397e65f
(tparam1): Change the way buffers are reallocated to be
Gerd Moellmann <gerd@gnu.org>
parents:
29805
diff
changeset
|
172 |
c6d74397e65f
(tparam1): Change the way buffers are reallocated to be
Gerd Moellmann <gerd@gnu.org>
parents:
29805
diff
changeset
|
173 op = new + offset; |
c6d74397e65f
(tparam1): Change the way buffers are reallocated to be
Gerd Moellmann <gerd@gnu.org>
parents:
29805
diff
changeset
|
174 outend = new + outlen; |
4687 | 175 outstring = new; |
176 } | |
177 c = *p++; | |
178 if (!c) | |
179 break; | |
180 if (c == '%') | |
181 { | |
182 c = *p++; | |
183 tem = *argp; | |
184 switch (c) | |
185 { | |
186 case 'd': /* %d means output in decimal. */ | |
187 if (tem < 10) | |
188 goto onedigit; | |
189 if (tem < 100) | |
190 goto twodigit; | |
191 case '3': /* %3 means output in decimal, 3 digits. */ | |
192 if (tem > 999) | |
193 { | |
194 *op++ = tem / 1000 + '0'; | |
195 tem %= 1000; | |
196 } | |
197 *op++ = tem / 100 + '0'; | |
198 case '2': /* %2 means output in decimal, 2 digits. */ | |
199 twodigit: | |
200 tem %= 100; | |
201 *op++ = tem / 10 + '0'; | |
202 onedigit: | |
203 *op++ = tem % 10 + '0'; | |
204 argp++; | |
205 break; | |
206 | |
207 case 'C': | |
208 /* For c-100: print quotient of value by 96, if nonzero, | |
209 then do like %+. */ | |
210 if (tem >= 96) | |
211 { | |
212 *op++ = tem / 96; | |
213 tem %= 96; | |
214 } | |
215 case '+': /* %+x means add character code of char x. */ | |
216 tem += *p++; | |
217 case '.': /* %. means output as character. */ | |
218 if (left) | |
219 { | |
220 /* If want to forbid output of 0 and \n and \t, | |
221 and this is one of them, increment it. */ | |
222 while (tem == 0 || tem == '\n' || tem == '\t') | |
223 { | |
224 tem++; | |
225 if (argp == old_argp) | |
226 doup++, outend -= strlen (up); | |
227 else | |
228 doleft++, outend -= strlen (left); | |
229 } | |
230 } | |
231 *op++ = tem ? tem : 0200; | |
232 case 'f': /* %f means discard next arg. */ | |
233 argp++; | |
234 break; | |
235 | |
236 case 'b': /* %b means back up one arg (and re-use it). */ | |
237 argp--; | |
238 break; | |
239 | |
240 case 'r': /* %r means interchange following two args. */ | |
241 argp[0] = argp[1]; | |
242 argp[1] = tem; | |
243 old_argp++; | |
244 break; | |
245 | |
246 case '>': /* %>xy means if arg is > char code of x, */ | |
247 if (argp[0] > *p++) /* then add char code of y to the arg, */ | |
248 argp[0] += *p; /* and in any case don't output. */ | |
249 p++; /* Leave the arg to be output later. */ | |
250 break; | |
251 | |
252 case 'a': /* %a means arithmetic. */ | |
253 /* Next character says what operation. | |
254 Add or subtract either a constant or some other arg. */ | |
255 /* First following character is + to add or - to subtract | |
256 or = to assign. */ | |
257 /* Next following char is 'p' and an arg spec | |
258 (0100 plus position of that arg relative to this one) | |
259 or 'c' and a constant stored in a character. */ | |
260 tem = p[2] & 0177; | |
261 if (p[1] == 'p') | |
262 tem = argp[tem - 0100]; | |
263 if (p[0] == '-') | |
264 argp[0] -= tem; | |
265 else if (p[0] == '+') | |
266 argp[0] += tem; | |
267 else if (p[0] == '*') | |
268 argp[0] *= tem; | |
269 else if (p[0] == '/') | |
270 argp[0] /= tem; | |
271 else | |
272 argp[0] = tem; | |
273 | |
274 p += 3; | |
275 break; | |
276 | |
277 case 'i': /* %i means add one to arg, */ | |
278 argp[0] ++; /* and leave it to be output later. */ | |
279 argp[1] ++; /* Increment the following arg, too! */ | |
280 break; | |
281 | |
282 case '%': /* %% means output %; no arg. */ | |
283 goto ordinary; | |
284 | |
285 case 'n': /* %n means xor each of next two args with 140. */ | |
286 argp[0] ^= 0140; | |
287 argp[1] ^= 0140; | |
288 break; | |
289 | |
290 case 'm': /* %m means xor each of next two args with 177. */ | |
291 argp[0] ^= 0177; | |
292 argp[1] ^= 0177; | |
293 break; | |
294 | |
295 case 'B': /* %B means express arg as BCD char code. */ | |
296 argp[0] += 6 * (tem / 10); | |
297 break; | |
298 | |
299 case 'D': /* %D means weird Delta Data transformation. */ | |
300 argp[0] -= 2 * (tem % 16); | |
301 break; | |
28573
cdc89dbad540
(tparam1): Abort when encountering an unknown
Gerd Moellmann <gerd@gnu.org>
parents:
14414
diff
changeset
|
302 |
cdc89dbad540
(tparam1): Abort when encountering an unknown
Gerd Moellmann <gerd@gnu.org>
parents:
14414
diff
changeset
|
303 default: |
cdc89dbad540
(tparam1): Abort when encountering an unknown
Gerd Moellmann <gerd@gnu.org>
parents:
14414
diff
changeset
|
304 abort (); |
4687 | 305 } |
306 } | |
307 else | |
308 /* Ordinary character in the argument string. */ | |
309 ordinary: | |
310 *op++ = c; | |
311 } | |
312 *op = 0; | |
313 while (doup-- > 0) | |
314 strcat (op, up); | |
315 while (doleft-- > 0) | |
316 strcat (op, left); | |
317 return outstring; | |
318 } | |
319 | |
320 #ifdef DEBUG | |
321 | |
322 main (argc, argv) | |
323 int argc; | |
324 char **argv; | |
325 { | |
326 char buf[50]; | |
327 int args[3]; | |
328 args[0] = atoi (argv[2]); | |
329 args[1] = atoi (argv[3]); | |
330 args[2] = atoi (argv[4]); | |
331 tparam1 (argv[1], buf, "LEFT", "UP", args); | |
332 printf ("%s\n", buf); | |
333 return 0; | |
334 } | |
335 | |
336 #endif /* DEBUG */ |