Mercurial > emacs
annotate src/tparam.c @ 112093:f8b430cb91a7
Mail-utils fix for bug#7746. (tiny change)
* lisp/mail/mail-utils.el (mail-strip-quoted-names): Avoid clobbering
an existing temp buffer.
author | Glenn Morris <rgm@gnu.org> |
---|---|
date | Sat, 01 Jan 2011 18:43:26 -0800 |
parents | 68dd71358159 |
children | aec1143e8d85 |
rev | line source |
---|---|
4687 | 1 /* Merge parameters into a termcap entry string. |
75227
e90d04cd455a
Update copyright for years from Emacs 21 to present (mainly adding
Glenn Morris <rgm@gnu.org>
parents:
68651
diff
changeset
|
2 Copyright (C) 1985, 1987, 1993, 1995, 2000, 2001, 2002, 2003, 2004, |
100960
69177b934405
Revert copyright year changes for files that are not part of Emacs.
Glenn Morris <rgm@gnu.org>
parents:
100951
diff
changeset
|
3 2005, 2006, 2007, 2008 Free Software Foundation, Inc. |
4687 | 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 | |
64084 | 17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
18 Boston, MA 02110-1301, USA. */ | |
4687 | 19 |
20 /* Emacs config.h may rename various library functions such as malloc. */ | |
21 #ifdef HAVE_CONFIG_H | |
22 #include <config.h> | |
12994
bd38619285f7
Don't assume that HAVE_CONFIG_H implies emacs.
David J. MacKenzie <djm@gnu.org>
parents:
12678
diff
changeset
|
23 #endif |
4687 | 24 |
29805 | 25 #ifdef emacs |
105669
68dd71358159
* alloc.c: Do not define struct catchtag.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
100960
diff
changeset
|
26 #include <setjmp.h> |
29805 | 27 #include "lisp.h" /* for xmalloc */ |
28 #else | |
4687 | 29 |
30 #ifdef STDC_HEADERS | |
31 #include <stdlib.h> | |
32 #include <string.h> | |
33 #else | |
34 char *malloc (); | |
35 char *realloc (); | |
36 #endif | |
37 | |
43675
b9ee14ccc0c8
[!emacs]: Move #define of bcopy to after string.h.
Richard M. Stallman <rms@gnu.org>
parents:
34360
diff
changeset
|
38 /* 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
|
39 #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
|
40 #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
|
41 #endif |
b9ee14ccc0c8
[!emacs]: Move #define of bcopy to after string.h.
Richard M. Stallman <rms@gnu.org>
parents:
34360
diff
changeset
|
42 |
12994
bd38619285f7
Don't assume that HAVE_CONFIG_H implies emacs.
David J. MacKenzie <djm@gnu.org>
parents:
12678
diff
changeset
|
43 #endif /* not emacs */ |
4687 | 44 |
45 #ifndef NULL | |
46 #define NULL (char *) 0 | |
47 #endif | |
48 | |
49 #ifndef emacs | |
50 static void | |
51 memory_out () | |
52 { | |
53 write (2, "virtual memory exhausted\n", 25); | |
54 exit (1); | |
55 } | |
56 | |
57 static char * | |
58 xmalloc (size) | |
59 unsigned size; | |
60 { | |
61 register char *tem = malloc (size); | |
62 | |
63 if (!tem) | |
64 memory_out (); | |
65 return tem; | |
66 } | |
67 | |
68 static char * | |
69 xrealloc (ptr, size) | |
70 char *ptr; | |
71 unsigned size; | |
72 { | |
73 register char *tem = realloc (ptr, size); | |
74 | |
75 if (!tem) | |
76 memory_out (); | |
77 return tem; | |
78 } | |
79 #endif /* not emacs */ | |
80 | |
81 /* Assuming STRING is the value of a termcap string entry | |
82 containing `%' constructs to expand parameters, | |
83 merge in parameter values and store result in block OUTSTRING points to. | |
84 LEN is the length of OUTSTRING. If more space is needed, | |
85 a block is allocated with `malloc'. | |
86 | |
87 The value returned is the address of the resulting string. | |
88 This may be OUTSTRING or may be the address of a block got with `malloc'. | |
89 In the latter case, the caller must free the block. | |
90 | |
91 The fourth and following args to tparam serve as the parameter values. */ | |
92 | |
93 static char *tparam1 (); | |
94 | |
95 /* VARARGS 2 */ | |
96 char * | |
97 tparam (string, outstring, len, arg0, arg1, arg2, arg3) | |
98 char *string; | |
99 char *outstring; | |
100 int len; | |
101 int arg0, arg1, arg2, arg3; | |
102 { | |
103 int arg[4]; | |
12678
8fc56d171ada
(tparam): Remove arg array and the #ifdef.
David J. MacKenzie <djm@gnu.org>
parents:
4687
diff
changeset
|
104 |
4687 | 105 arg[0] = arg0; |
106 arg[1] = arg1; | |
107 arg[2] = arg2; | |
108 arg[3] = arg3; | |
109 return tparam1 (string, outstring, len, NULL, NULL, arg); | |
110 } | |
111 | |
112 char *BC; | |
113 char *UP; | |
114 | |
115 static char tgoto_buf[50]; | |
116 | |
117 char * | |
118 tgoto (cm, hpos, vpos) | |
119 char *cm; | |
120 int hpos, vpos; | |
121 { | |
122 int args[2]; | |
123 if (!cm) | |
124 return NULL; | |
125 args[0] = vpos; | |
126 args[1] = hpos; | |
127 return tparam1 (cm, tgoto_buf, 50, UP, BC, args); | |
128 } | |
129 | |
130 static char * | |
131 tparam1 (string, outstring, len, up, left, argp) | |
132 char *string; | |
133 char *outstring; | |
134 int len; | |
135 char *up, *left; | |
136 register int *argp; | |
137 { | |
138 register int c; | |
139 register char *p = string; | |
140 register char *op = outstring; | |
141 char *outend; | |
142 int outlen = 0; | |
143 | |
144 register int tem; | |
53270
971b19b300e6
(tparam1): Add handling for `%pN', which
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
145 int *old_argp = argp; /* can move */ |
971b19b300e6
(tparam1): Add handling for `%pN', which
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
146 int *fixed_argp = argp; /* never moves */ |
971b19b300e6
(tparam1): Add handling for `%pN', which
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
147 int explicit_param_p = 0; /* set by %p */ |
4687 | 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 } | |
49600
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
44890
diff
changeset
|
172 |
34360
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++; | |
53270
971b19b300e6
(tparam1): Add handling for `%pN', which
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
183 if (explicit_param_p) |
971b19b300e6
(tparam1): Add handling for `%pN', which
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
184 explicit_param_p = 0; |
971b19b300e6
(tparam1): Add handling for `%pN', which
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
185 else |
971b19b300e6
(tparam1): Add handling for `%pN', which
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
186 tem = *argp; |
4687 | 187 switch (c) |
188 { | |
189 case 'd': /* %d means output in decimal. */ | |
190 if (tem < 10) | |
191 goto onedigit; | |
192 if (tem < 100) | |
193 goto twodigit; | |
194 case '3': /* %3 means output in decimal, 3 digits. */ | |
195 if (tem > 999) | |
196 { | |
197 *op++ = tem / 1000 + '0'; | |
198 tem %= 1000; | |
199 } | |
200 *op++ = tem / 100 + '0'; | |
201 case '2': /* %2 means output in decimal, 2 digits. */ | |
202 twodigit: | |
203 tem %= 100; | |
204 *op++ = tem / 10 + '0'; | |
205 onedigit: | |
206 *op++ = tem % 10 + '0'; | |
207 argp++; | |
208 break; | |
53270
971b19b300e6
(tparam1): Add handling for `%pN', which
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
209 case 'p': /* %pN means use param N for next subst. */ |
971b19b300e6
(tparam1): Add handling for `%pN', which
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
210 tem = fixed_argp[(*p++) - '1']; |
971b19b300e6
(tparam1): Add handling for `%pN', which
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
211 explicit_param_p = 1; |
971b19b300e6
(tparam1): Add handling for `%pN', which
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
212 break; |
4687 | 213 case 'C': |
214 /* For c-100: print quotient of value by 96, if nonzero, | |
215 then do like %+. */ | |
216 if (tem >= 96) | |
217 { | |
218 *op++ = tem / 96; | |
219 tem %= 96; | |
220 } | |
221 case '+': /* %+x means add character code of char x. */ | |
222 tem += *p++; | |
223 case '.': /* %. means output as character. */ | |
224 if (left) | |
225 { | |
226 /* If want to forbid output of 0 and \n and \t, | |
227 and this is one of them, increment it. */ | |
228 while (tem == 0 || tem == '\n' || tem == '\t') | |
229 { | |
230 tem++; | |
231 if (argp == old_argp) | |
232 doup++, outend -= strlen (up); | |
233 else | |
234 doleft++, outend -= strlen (left); | |
235 } | |
236 } | |
237 *op++ = tem ? tem : 0200; | |
238 case 'f': /* %f means discard next arg. */ | |
239 argp++; | |
240 break; | |
241 | |
242 case 'b': /* %b means back up one arg (and re-use it). */ | |
243 argp--; | |
244 break; | |
245 | |
246 case 'r': /* %r means interchange following two args. */ | |
247 argp[0] = argp[1]; | |
248 argp[1] = tem; | |
249 old_argp++; | |
250 break; | |
251 | |
252 case '>': /* %>xy means if arg is > char code of x, */ | |
253 if (argp[0] > *p++) /* then add char code of y to the arg, */ | |
254 argp[0] += *p; /* and in any case don't output. */ | |
255 p++; /* Leave the arg to be output later. */ | |
256 break; | |
257 | |
258 case 'a': /* %a means arithmetic. */ | |
259 /* Next character says what operation. | |
260 Add or subtract either a constant or some other arg. */ | |
261 /* First following character is + to add or - to subtract | |
262 or = to assign. */ | |
263 /* Next following char is 'p' and an arg spec | |
264 (0100 plus position of that arg relative to this one) | |
265 or 'c' and a constant stored in a character. */ | |
266 tem = p[2] & 0177; | |
267 if (p[1] == 'p') | |
268 tem = argp[tem - 0100]; | |
269 if (p[0] == '-') | |
270 argp[0] -= tem; | |
271 else if (p[0] == '+') | |
272 argp[0] += tem; | |
273 else if (p[0] == '*') | |
274 argp[0] *= tem; | |
275 else if (p[0] == '/') | |
276 argp[0] /= tem; | |
277 else | |
278 argp[0] = tem; | |
279 | |
280 p += 3; | |
281 break; | |
282 | |
283 case 'i': /* %i means add one to arg, */ | |
284 argp[0] ++; /* and leave it to be output later. */ | |
285 argp[1] ++; /* Increment the following arg, too! */ | |
286 break; | |
287 | |
288 case '%': /* %% means output %; no arg. */ | |
289 goto ordinary; | |
290 | |
291 case 'n': /* %n means xor each of next two args with 140. */ | |
292 argp[0] ^= 0140; | |
293 argp[1] ^= 0140; | |
294 break; | |
295 | |
296 case 'm': /* %m means xor each of next two args with 177. */ | |
297 argp[0] ^= 0177; | |
298 argp[1] ^= 0177; | |
299 break; | |
300 | |
301 case 'B': /* %B means express arg as BCD char code. */ | |
302 argp[0] += 6 * (tem / 10); | |
303 break; | |
304 | |
305 case 'D': /* %D means weird Delta Data transformation. */ | |
306 argp[0] -= 2 * (tem % 16); | |
307 break; | |
28573
cdc89dbad540
(tparam1): Abort when encountering an unknown
Gerd Moellmann <gerd@gnu.org>
parents:
14414
diff
changeset
|
308 |
cdc89dbad540
(tparam1): Abort when encountering an unknown
Gerd Moellmann <gerd@gnu.org>
parents:
14414
diff
changeset
|
309 default: |
cdc89dbad540
(tparam1): Abort when encountering an unknown
Gerd Moellmann <gerd@gnu.org>
parents:
14414
diff
changeset
|
310 abort (); |
4687 | 311 } |
312 } | |
313 else | |
314 /* Ordinary character in the argument string. */ | |
315 ordinary: | |
316 *op++ = c; | |
317 } | |
318 *op = 0; | |
319 while (doup-- > 0) | |
320 strcat (op, up); | |
321 while (doleft-- > 0) | |
322 strcat (op, left); | |
323 return outstring; | |
324 } | |
325 | |
326 #ifdef DEBUG | |
327 | |
328 main (argc, argv) | |
329 int argc; | |
330 char **argv; | |
331 { | |
332 char buf[50]; | |
333 int args[3]; | |
334 args[0] = atoi (argv[2]); | |
335 args[1] = atoi (argv[3]); | |
336 args[2] = atoi (argv[4]); | |
337 tparam1 (argv[1], buf, "LEFT", "UP", args); | |
338 printf ("%s\n", buf); | |
339 return 0; | |
340 } | |
341 | |
342 #endif /* DEBUG */ | |
52401 | 343 |
344 /* arch-tag: 83f7b5ac-a808-4f75-b87a-123de009b402 | |
345 (do not change this comment) */ |