Mercurial > emacs
annotate src/tparam.c @ 40652:8dc305c093c1
Minor cleanup.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Fri, 02 Nov 2001 17:47:37 +0000 |
parents | c6d74397e65f |
children | b9ee14ccc0c8 |
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 #if defined(HAVE_STRING_H) || defined(STDC_HEADERS) |
28 #define bcopy(s, d, n) memcpy ((d), (s), (n)) | |
29 #endif | |
30 | |
31 #ifdef STDC_HEADERS | |
32 #include <stdlib.h> | |
33 #include <string.h> | |
34 #else | |
35 char *malloc (); | |
36 char *realloc (); | |
37 #endif | |
38 | |
12994
bd38619285f7
Don't assume that HAVE_CONFIG_H implies emacs.
David J. MacKenzie <djm@gnu.org>
parents:
12678
diff
changeset
|
39 #endif /* not emacs */ |
4687 | 40 |
41 #ifndef NULL | |
42 #define NULL (char *) 0 | |
43 #endif | |
44 | |
45 #ifndef emacs | |
46 static void | |
47 memory_out () | |
48 { | |
49 write (2, "virtual memory exhausted\n", 25); | |
50 exit (1); | |
51 } | |
52 | |
53 static char * | |
54 xmalloc (size) | |
55 unsigned size; | |
56 { | |
57 register char *tem = malloc (size); | |
58 | |
59 if (!tem) | |
60 memory_out (); | |
61 return tem; | |
62 } | |
63 | |
64 static char * | |
65 xrealloc (ptr, size) | |
66 char *ptr; | |
67 unsigned size; | |
68 { | |
69 register char *tem = realloc (ptr, size); | |
70 | |
71 if (!tem) | |
72 memory_out (); | |
73 return tem; | |
74 } | |
75 #endif /* not emacs */ | |
76 | |
77 /* Assuming STRING is the value of a termcap string entry | |
78 containing `%' constructs to expand parameters, | |
79 merge in parameter values and store result in block OUTSTRING points to. | |
80 LEN is the length of OUTSTRING. If more space is needed, | |
81 a block is allocated with `malloc'. | |
82 | |
83 The value returned is the address of the resulting string. | |
84 This may be OUTSTRING or may be the address of a block got with `malloc'. | |
85 In the latter case, the caller must free the block. | |
86 | |
87 The fourth and following args to tparam serve as the parameter values. */ | |
88 | |
89 static char *tparam1 (); | |
90 | |
91 /* VARARGS 2 */ | |
92 char * | |
93 tparam (string, outstring, len, arg0, arg1, arg2, arg3) | |
94 char *string; | |
95 char *outstring; | |
96 int len; | |
97 int arg0, arg1, arg2, arg3; | |
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 * | |
114 tgoto (cm, hpos, vpos) | |
115 char *cm; | |
116 int hpos, vpos; | |
117 { | |
118 int args[2]; | |
119 if (!cm) | |
120 return NULL; | |
121 args[0] = vpos; | |
122 args[1] = hpos; | |
123 return tparam1 (cm, tgoto_buf, 50, UP, BC, args); | |
124 } | |
125 | |
126 static char * | |
127 tparam1 (string, outstring, len, up, left, argp) | |
128 char *string; | |
129 char *outstring; | |
130 int len; | |
131 char *up, *left; | |
132 register int *argp; | |
133 { | |
134 register int c; | |
135 register char *p = string; | |
136 register char *op = outstring; | |
137 char *outend; | |
138 int outlen = 0; | |
139 | |
140 register int tem; | |
141 int *old_argp = argp; | |
142 int doleft = 0; | |
143 int doup = 0; | |
144 | |
145 outend = outstring + len; | |
146 | |
147 while (1) | |
148 { | |
149 /* If the buffer might be too short, make it bigger. */ | |
150 if (op + 5 >= outend) | |
151 { | |
152 register char *new; | |
34360
c6d74397e65f
(tparam1): Change the way buffers are reallocated to be
Gerd Moellmann <gerd@gnu.org>
parents:
29805
diff
changeset
|
153 int offset = op - outstring; |
c6d74397e65f
(tparam1): Change the way buffers are reallocated to be
Gerd Moellmann <gerd@gnu.org>
parents:
29805
diff
changeset
|
154 |
4687 | 155 if (outlen == 0) |
156 { | |
157 outlen = len + 40; | |
158 new = (char *) xmalloc (outlen); | |
34360
c6d74397e65f
(tparam1): Change the way buffers are reallocated to be
Gerd Moellmann <gerd@gnu.org>
parents:
29805
diff
changeset
|
159 bcopy (outstring, new, offset); |
4687 | 160 } |
161 else | |
162 { | |
163 outlen *= 2; | |
164 new = (char *) xrealloc (outstring, outlen); | |
165 } | |
34360
c6d74397e65f
(tparam1): Change the way buffers are reallocated to be
Gerd Moellmann <gerd@gnu.org>
parents:
29805
diff
changeset
|
166 |
c6d74397e65f
(tparam1): Change the way buffers are reallocated to be
Gerd Moellmann <gerd@gnu.org>
parents:
29805
diff
changeset
|
167 op = new + offset; |
c6d74397e65f
(tparam1): Change the way buffers are reallocated to be
Gerd Moellmann <gerd@gnu.org>
parents:
29805
diff
changeset
|
168 outend = new + outlen; |
4687 | 169 outstring = new; |
170 } | |
171 c = *p++; | |
172 if (!c) | |
173 break; | |
174 if (c == '%') | |
175 { | |
176 c = *p++; | |
177 tem = *argp; | |
178 switch (c) | |
179 { | |
180 case 'd': /* %d means output in decimal. */ | |
181 if (tem < 10) | |
182 goto onedigit; | |
183 if (tem < 100) | |
184 goto twodigit; | |
185 case '3': /* %3 means output in decimal, 3 digits. */ | |
186 if (tem > 999) | |
187 { | |
188 *op++ = tem / 1000 + '0'; | |
189 tem %= 1000; | |
190 } | |
191 *op++ = tem / 100 + '0'; | |
192 case '2': /* %2 means output in decimal, 2 digits. */ | |
193 twodigit: | |
194 tem %= 100; | |
195 *op++ = tem / 10 + '0'; | |
196 onedigit: | |
197 *op++ = tem % 10 + '0'; | |
198 argp++; | |
199 break; | |
200 | |
201 case 'C': | |
202 /* For c-100: print quotient of value by 96, if nonzero, | |
203 then do like %+. */ | |
204 if (tem >= 96) | |
205 { | |
206 *op++ = tem / 96; | |
207 tem %= 96; | |
208 } | |
209 case '+': /* %+x means add character code of char x. */ | |
210 tem += *p++; | |
211 case '.': /* %. means output as character. */ | |
212 if (left) | |
213 { | |
214 /* If want to forbid output of 0 and \n and \t, | |
215 and this is one of them, increment it. */ | |
216 while (tem == 0 || tem == '\n' || tem == '\t') | |
217 { | |
218 tem++; | |
219 if (argp == old_argp) | |
220 doup++, outend -= strlen (up); | |
221 else | |
222 doleft++, outend -= strlen (left); | |
223 } | |
224 } | |
225 *op++ = tem ? tem : 0200; | |
226 case 'f': /* %f means discard next arg. */ | |
227 argp++; | |
228 break; | |
229 | |
230 case 'b': /* %b means back up one arg (and re-use it). */ | |
231 argp--; | |
232 break; | |
233 | |
234 case 'r': /* %r means interchange following two args. */ | |
235 argp[0] = argp[1]; | |
236 argp[1] = tem; | |
237 old_argp++; | |
238 break; | |
239 | |
240 case '>': /* %>xy means if arg is > char code of x, */ | |
241 if (argp[0] > *p++) /* then add char code of y to the arg, */ | |
242 argp[0] += *p; /* and in any case don't output. */ | |
243 p++; /* Leave the arg to be output later. */ | |
244 break; | |
245 | |
246 case 'a': /* %a means arithmetic. */ | |
247 /* Next character says what operation. | |
248 Add or subtract either a constant or some other arg. */ | |
249 /* First following character is + to add or - to subtract | |
250 or = to assign. */ | |
251 /* Next following char is 'p' and an arg spec | |
252 (0100 plus position of that arg relative to this one) | |
253 or 'c' and a constant stored in a character. */ | |
254 tem = p[2] & 0177; | |
255 if (p[1] == 'p') | |
256 tem = argp[tem - 0100]; | |
257 if (p[0] == '-') | |
258 argp[0] -= tem; | |
259 else if (p[0] == '+') | |
260 argp[0] += tem; | |
261 else if (p[0] == '*') | |
262 argp[0] *= tem; | |
263 else if (p[0] == '/') | |
264 argp[0] /= tem; | |
265 else | |
266 argp[0] = tem; | |
267 | |
268 p += 3; | |
269 break; | |
270 | |
271 case 'i': /* %i means add one to arg, */ | |
272 argp[0] ++; /* and leave it to be output later. */ | |
273 argp[1] ++; /* Increment the following arg, too! */ | |
274 break; | |
275 | |
276 case '%': /* %% means output %; no arg. */ | |
277 goto ordinary; | |
278 | |
279 case 'n': /* %n means xor each of next two args with 140. */ | |
280 argp[0] ^= 0140; | |
281 argp[1] ^= 0140; | |
282 break; | |
283 | |
284 case 'm': /* %m means xor each of next two args with 177. */ | |
285 argp[0] ^= 0177; | |
286 argp[1] ^= 0177; | |
287 break; | |
288 | |
289 case 'B': /* %B means express arg as BCD char code. */ | |
290 argp[0] += 6 * (tem / 10); | |
291 break; | |
292 | |
293 case 'D': /* %D means weird Delta Data transformation. */ | |
294 argp[0] -= 2 * (tem % 16); | |
295 break; | |
28573
cdc89dbad540
(tparam1): Abort when encountering an unknown
Gerd Moellmann <gerd@gnu.org>
parents:
14414
diff
changeset
|
296 |
cdc89dbad540
(tparam1): Abort when encountering an unknown
Gerd Moellmann <gerd@gnu.org>
parents:
14414
diff
changeset
|
297 default: |
cdc89dbad540
(tparam1): Abort when encountering an unknown
Gerd Moellmann <gerd@gnu.org>
parents:
14414
diff
changeset
|
298 abort (); |
4687 | 299 } |
300 } | |
301 else | |
302 /* Ordinary character in the argument string. */ | |
303 ordinary: | |
304 *op++ = c; | |
305 } | |
306 *op = 0; | |
307 while (doup-- > 0) | |
308 strcat (op, up); | |
309 while (doleft-- > 0) | |
310 strcat (op, left); | |
311 return outstring; | |
312 } | |
313 | |
314 #ifdef DEBUG | |
315 | |
316 main (argc, argv) | |
317 int argc; | |
318 char **argv; | |
319 { | |
320 char buf[50]; | |
321 int args[3]; | |
322 args[0] = atoi (argv[2]); | |
323 args[1] = atoi (argv[3]); | |
324 args[2] = atoi (argv[4]); | |
325 tparam1 (argv[1], buf, "LEFT", "UP", args); | |
326 printf ("%s\n", buf); | |
327 return 0; | |
328 } | |
329 | |
330 #endif /* DEBUG */ |