Mercurial > emacs
annotate src/tparam.c @ 109153:978941230a7c
* fringe.c (draw_fringe_bitmap_1): Use lookup_named_face to get
fringe face id, so face-remapping-alist works (Bug#6091).
author | Chong Yidong <cyd@stupidchicken.com> |
---|---|
date | Tue, 06 Jul 2010 16:24:57 -0400 |
parents | aec1143e8d85 |
children | 750db9f3e6d8 |
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 | |
109126
aec1143e8d85
Convert (most) functions in src to standard C.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
105669
diff
changeset
|
93 static char *tparam1 (char *string, char *outstring, int len, char *up, char *left, register int *argp); |
4687 | 94 |
95 /* VARARGS 2 */ | |
96 char * | |
109126
aec1143e8d85
Convert (most) functions in src to standard C.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
105669
diff
changeset
|
97 tparam (char *string, char *outstring, int len, int arg0, int arg1, int arg2, int arg3) |
4687 | 98 { |
99 int arg[4]; | |
12678
8fc56d171ada
(tparam): Remove arg array and the #ifdef.
David J. MacKenzie <djm@gnu.org>
parents:
4687
diff
changeset
|
100 |
4687 | 101 arg[0] = arg0; |
102 arg[1] = arg1; | |
103 arg[2] = arg2; | |
104 arg[3] = arg3; | |
105 return tparam1 (string, outstring, len, NULL, NULL, arg); | |
106 } | |
107 | |
108 char *BC; | |
109 char *UP; | |
110 | |
111 static char tgoto_buf[50]; | |
112 | |
113 char * | |
109126
aec1143e8d85
Convert (most) functions in src to standard C.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
105669
diff
changeset
|
114 tgoto (char *cm, int hpos, int vpos) |
4687 | 115 { |
116 int args[2]; | |
117 if (!cm) | |
118 return NULL; | |
119 args[0] = vpos; | |
120 args[1] = hpos; | |
121 return tparam1 (cm, tgoto_buf, 50, UP, BC, args); | |
122 } | |
123 | |
124 static char * | |
109126
aec1143e8d85
Convert (most) functions in src to standard C.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
105669
diff
changeset
|
125 tparam1 (char *string, char *outstring, int len, char *up, char *left, register int *argp) |
4687 | 126 { |
127 register int c; | |
128 register char *p = string; | |
129 register char *op = outstring; | |
130 char *outend; | |
131 int outlen = 0; | |
132 | |
133 register int tem; | |
53270
971b19b300e6
(tparam1): Add handling for `%pN', which
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
134 int *old_argp = argp; /* can move */ |
971b19b300e6
(tparam1): Add handling for `%pN', which
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
135 int *fixed_argp = argp; /* never moves */ |
971b19b300e6
(tparam1): Add handling for `%pN', which
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
136 int explicit_param_p = 0; /* set by %p */ |
4687 | 137 int doleft = 0; |
138 int doup = 0; | |
139 | |
140 outend = outstring + len; | |
141 | |
142 while (1) | |
143 { | |
144 /* If the buffer might be too short, make it bigger. */ | |
145 if (op + 5 >= outend) | |
146 { | |
147 register char *new; | |
34360
c6d74397e65f
(tparam1): Change the way buffers are reallocated to be
Gerd Moellmann <gerd@gnu.org>
parents:
29805
diff
changeset
|
148 int offset = op - outstring; |
c6d74397e65f
(tparam1): Change the way buffers are reallocated to be
Gerd Moellmann <gerd@gnu.org>
parents:
29805
diff
changeset
|
149 |
4687 | 150 if (outlen == 0) |
151 { | |
152 outlen = len + 40; | |
153 new = (char *) xmalloc (outlen); | |
34360
c6d74397e65f
(tparam1): Change the way buffers are reallocated to be
Gerd Moellmann <gerd@gnu.org>
parents:
29805
diff
changeset
|
154 bcopy (outstring, new, offset); |
4687 | 155 } |
156 else | |
157 { | |
158 outlen *= 2; | |
159 new = (char *) xrealloc (outstring, outlen); | |
160 } | |
49600
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
44890
diff
changeset
|
161 |
34360
c6d74397e65f
(tparam1): Change the way buffers are reallocated to be
Gerd Moellmann <gerd@gnu.org>
parents:
29805
diff
changeset
|
162 op = new + offset; |
c6d74397e65f
(tparam1): Change the way buffers are reallocated to be
Gerd Moellmann <gerd@gnu.org>
parents:
29805
diff
changeset
|
163 outend = new + outlen; |
4687 | 164 outstring = new; |
165 } | |
166 c = *p++; | |
167 if (!c) | |
168 break; | |
169 if (c == '%') | |
170 { | |
171 c = *p++; | |
53270
971b19b300e6
(tparam1): Add handling for `%pN', which
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
172 if (explicit_param_p) |
971b19b300e6
(tparam1): Add handling for `%pN', which
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
173 explicit_param_p = 0; |
971b19b300e6
(tparam1): Add handling for `%pN', which
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
174 else |
971b19b300e6
(tparam1): Add handling for `%pN', which
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
175 tem = *argp; |
4687 | 176 switch (c) |
177 { | |
178 case 'd': /* %d means output in decimal. */ | |
179 if (tem < 10) | |
180 goto onedigit; | |
181 if (tem < 100) | |
182 goto twodigit; | |
183 case '3': /* %3 means output in decimal, 3 digits. */ | |
184 if (tem > 999) | |
185 { | |
186 *op++ = tem / 1000 + '0'; | |
187 tem %= 1000; | |
188 } | |
189 *op++ = tem / 100 + '0'; | |
190 case '2': /* %2 means output in decimal, 2 digits. */ | |
191 twodigit: | |
192 tem %= 100; | |
193 *op++ = tem / 10 + '0'; | |
194 onedigit: | |
195 *op++ = tem % 10 + '0'; | |
196 argp++; | |
197 break; | |
53270
971b19b300e6
(tparam1): Add handling for `%pN', which
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
198 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
|
199 tem = fixed_argp[(*p++) - '1']; |
971b19b300e6
(tparam1): Add handling for `%pN', which
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
200 explicit_param_p = 1; |
971b19b300e6
(tparam1): Add handling for `%pN', which
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
201 break; |
4687 | 202 case 'C': |
203 /* For c-100: print quotient of value by 96, if nonzero, | |
204 then do like %+. */ | |
205 if (tem >= 96) | |
206 { | |
207 *op++ = tem / 96; | |
208 tem %= 96; | |
209 } | |
210 case '+': /* %+x means add character code of char x. */ | |
211 tem += *p++; | |
212 case '.': /* %. means output as character. */ | |
213 if (left) | |
214 { | |
215 /* If want to forbid output of 0 and \n and \t, | |
216 and this is one of them, increment it. */ | |
217 while (tem == 0 || tem == '\n' || tem == '\t') | |
218 { | |
219 tem++; | |
220 if (argp == old_argp) | |
221 doup++, outend -= strlen (up); | |
222 else | |
223 doleft++, outend -= strlen (left); | |
224 } | |
225 } | |
226 *op++ = tem ? tem : 0200; | |
227 case 'f': /* %f means discard next arg. */ | |
228 argp++; | |
229 break; | |
230 | |
231 case 'b': /* %b means back up one arg (and re-use it). */ | |
232 argp--; | |
233 break; | |
234 | |
235 case 'r': /* %r means interchange following two args. */ | |
236 argp[0] = argp[1]; | |
237 argp[1] = tem; | |
238 old_argp++; | |
239 break; | |
240 | |
241 case '>': /* %>xy means if arg is > char code of x, */ | |
242 if (argp[0] > *p++) /* then add char code of y to the arg, */ | |
243 argp[0] += *p; /* and in any case don't output. */ | |
244 p++; /* Leave the arg to be output later. */ | |
245 break; | |
246 | |
247 case 'a': /* %a means arithmetic. */ | |
248 /* Next character says what operation. | |
249 Add or subtract either a constant or some other arg. */ | |
250 /* First following character is + to add or - to subtract | |
251 or = to assign. */ | |
252 /* Next following char is 'p' and an arg spec | |
253 (0100 plus position of that arg relative to this one) | |
254 or 'c' and a constant stored in a character. */ | |
255 tem = p[2] & 0177; | |
256 if (p[1] == 'p') | |
257 tem = argp[tem - 0100]; | |
258 if (p[0] == '-') | |
259 argp[0] -= tem; | |
260 else if (p[0] == '+') | |
261 argp[0] += tem; | |
262 else if (p[0] == '*') | |
263 argp[0] *= tem; | |
264 else if (p[0] == '/') | |
265 argp[0] /= tem; | |
266 else | |
267 argp[0] = tem; | |
268 | |
269 p += 3; | |
270 break; | |
271 | |
272 case 'i': /* %i means add one to arg, */ | |
273 argp[0] ++; /* and leave it to be output later. */ | |
274 argp[1] ++; /* Increment the following arg, too! */ | |
275 break; | |
276 | |
277 case '%': /* %% means output %; no arg. */ | |
278 goto ordinary; | |
279 | |
280 case 'n': /* %n means xor each of next two args with 140. */ | |
281 argp[0] ^= 0140; | |
282 argp[1] ^= 0140; | |
283 break; | |
284 | |
285 case 'm': /* %m means xor each of next two args with 177. */ | |
286 argp[0] ^= 0177; | |
287 argp[1] ^= 0177; | |
288 break; | |
289 | |
290 case 'B': /* %B means express arg as BCD char code. */ | |
291 argp[0] += 6 * (tem / 10); | |
292 break; | |
293 | |
294 case 'D': /* %D means weird Delta Data transformation. */ | |
295 argp[0] -= 2 * (tem % 16); | |
296 break; | |
28573
cdc89dbad540
(tparam1): Abort when encountering an unknown
Gerd Moellmann <gerd@gnu.org>
parents:
14414
diff
changeset
|
297 |
cdc89dbad540
(tparam1): Abort when encountering an unknown
Gerd Moellmann <gerd@gnu.org>
parents:
14414
diff
changeset
|
298 default: |
cdc89dbad540
(tparam1): Abort when encountering an unknown
Gerd Moellmann <gerd@gnu.org>
parents:
14414
diff
changeset
|
299 abort (); |
4687 | 300 } |
301 } | |
302 else | |
303 /* Ordinary character in the argument string. */ | |
304 ordinary: | |
305 *op++ = c; | |
306 } | |
307 *op = 0; | |
308 while (doup-- > 0) | |
309 strcat (op, up); | |
310 while (doleft-- > 0) | |
311 strcat (op, left); | |
312 return outstring; | |
313 } | |
314 | |
315 #ifdef DEBUG | |
316 | |
317 main (argc, argv) | |
318 int argc; | |
319 char **argv; | |
320 { | |
321 char buf[50]; | |
322 int args[3]; | |
323 args[0] = atoi (argv[2]); | |
324 args[1] = atoi (argv[3]); | |
325 args[2] = atoi (argv[4]); | |
326 tparam1 (argv[1], buf, "LEFT", "UP", args); | |
327 printf ("%s\n", buf); | |
328 return 0; | |
329 } | |
330 | |
331 #endif /* DEBUG */ | |
52401 | 332 |
333 /* arch-tag: 83f7b5ac-a808-4f75-b87a-123de009b402 | |
334 (do not change this comment) */ |