0
|
1 /* Merge parameters into a termcap entry string.
|
|
2 Copyright (C) 1985, 1987, 1992, 1993, 1994 Free Software Foundation, Inc.
|
|
3
|
|
4 This file is part of XEmacs.
|
|
5
|
|
6 XEmacs is free software; you can redistribute it and/or modify it
|
|
7 under the terms of the GNU General Public License as published by the
|
|
8 Free Software Foundation; either version 2, or (at your option) any
|
|
9 later version.
|
|
10
|
|
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 for more details.
|
|
15
|
|
16 You should have received a copy of the GNU General Public License
|
|
17 along with XEmacs; see the file COPYING. If not, write to
|
|
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
19 Boston, MA 02111-1307, USA. */
|
|
20
|
|
21 /* Synched up with: Not synched with FSF. */
|
|
22
|
|
23 /* config.h may rename various library functions such as malloc. */
|
|
24 #ifdef emacs
|
|
25
|
|
26 #include <config.h>
|
|
27
|
|
28 #include <string.h>
|
|
29
|
551
|
30 #undef realloc
|
|
31 #undef malloc
|
|
32 #undef free
|
0
|
33 #define realloc xrealloc
|
|
34 #define malloc xmalloc
|
|
35 #define free xfree
|
458
|
36 extern void *xmalloc (size_t size);
|
|
37 extern void *xrealloc (void *, size_t size);
|
0
|
38
|
551
|
39 #else /* !emacs */
|
0
|
40
|
|
41 #include <stdlib.h>
|
|
42 #include <string.h>
|
|
43
|
|
44 #endif /* !emacs */
|
|
45
|
|
46 /* Assuming STRING is the value of a termcap string entry
|
|
47 containing `%' constructs to expand parameters,
|
|
48 merge in parameter values and store result in block OUTSTRING points to.
|
|
49 LEN is the length of OUTSTRING. If more space is needed,
|
|
50 a block is allocated with `malloc'.
|
|
51
|
|
52 The value returned is the address of the resulting string.
|
|
53 This may be OUTSTRING or may be the address of a block got with `malloc'.
|
|
54 In the latter case, the caller must free the block.
|
|
55
|
|
56 The fourth and following args to tparam serve as the parameter values. */
|
|
57
|
398
|
58 static char *tparam1 (const char *string, char *outstring, int len,
|
|
59 const char *up, const char *left,
|
0
|
60 int *argp);
|
|
61
|
|
62 /* XEmacs: renamed this function because just tparam() conflicts with
|
|
63 ncurses */
|
398
|
64 char *emacs_tparam (const char *string, char *outstring, int len, int arg0,
|
0
|
65 int arg1, int arg2, int arg3);
|
|
66 char *
|
398
|
67 emacs_tparam (const char *string, char *outstring, int len, int arg0,
|
0
|
68 int arg1, int arg2, int arg3)
|
|
69 {
|
|
70 int arg[4];
|
|
71 arg[0] = arg0;
|
|
72 arg[1] = arg1;
|
|
73 arg[2] = arg2;
|
|
74 arg[3] = arg3;
|
|
75 return tparam1 (string, outstring, len, 0, 0, arg);
|
|
76 }
|
|
77
|
398
|
78 const char *BC;
|
|
79 const char *UP;
|
0
|
80
|
|
81 static char tgoto_buf[50];
|
|
82
|
398
|
83 char *tgoto (const char *cm, int hpos, int vpos);
|
0
|
84 char *
|
398
|
85 tgoto (const char *cm, int hpos, int vpos)
|
0
|
86 {
|
|
87 int args[2];
|
|
88 if (!cm)
|
|
89 return 0;
|
|
90 args[0] = vpos;
|
|
91 args[1] = hpos;
|
|
92 return tparam1 (cm, tgoto_buf, 50, UP, BC, args);
|
|
93 }
|
|
94
|
|
95 static char *
|
398
|
96 tparam1 (const char *string, char *outstring, int len, const char *up,
|
|
97 const char *left, int *argp)
|
0
|
98 {
|
|
99 int c;
|
398
|
100 const char *p = string;
|
0
|
101 char *op = outstring;
|
|
102 char *outend;
|
|
103 int outlen = 0;
|
|
104
|
|
105 int tem;
|
|
106 int *old_argp = argp;
|
|
107 int doleft = 0;
|
|
108 int doup = 0;
|
|
109
|
|
110 outend = outstring + len;
|
|
111
|
|
112 while (1)
|
|
113 {
|
|
114 /* If the buffer might be too short, make it bigger. */
|
|
115 if (op + 5 >= outend)
|
|
116 {
|
|
117 char *new;
|
|
118 if (outlen == 0)
|
|
119 {
|
|
120 outlen = len + 40;
|
|
121 new = (char *) malloc (outlen);
|
|
122 outend += 40;
|
|
123 memcpy (new, outstring, op - outstring);
|
|
124 }
|
|
125 else
|
|
126 {
|
|
127 outend += outlen;
|
|
128 outlen *= 2;
|
|
129 new = (char *) realloc (outstring, outlen);
|
|
130 }
|
|
131 op += new - outstring;
|
|
132 outend += new - outstring;
|
|
133 outstring = new;
|
|
134 }
|
|
135 c = *p++;
|
|
136 if (!c)
|
|
137 break;
|
|
138 if (c == '%')
|
|
139 {
|
|
140 c = *p++;
|
|
141 tem = *argp;
|
|
142 switch (c)
|
|
143 {
|
|
144 case 'd': /* %d means output in decimal. */
|
|
145 if (tem < 10)
|
|
146 goto onedigit;
|
|
147 if (tem < 100)
|
|
148 goto twodigit;
|
|
149 case '3': /* %3 means output in decimal, 3 digits. */
|
|
150 if (tem > 999)
|
|
151 {
|
|
152 *op++ = tem / 1000 + '0';
|
|
153 tem %= 1000;
|
|
154 }
|
|
155 *op++ = tem / 100 + '0';
|
|
156 case '2': /* %2 means output in decimal, 2 digits. */
|
|
157 twodigit:
|
|
158 tem %= 100;
|
|
159 *op++ = tem / 10 + '0';
|
|
160 onedigit:
|
|
161 *op++ = tem % 10 + '0';
|
|
162 argp++;
|
|
163 break;
|
|
164
|
|
165 case 'C':
|
|
166 /* For c-100: print quotient of value by 96, if nonzero,
|
|
167 then do like %+. */
|
|
168 if (tem >= 96)
|
|
169 {
|
|
170 *op++ = tem / 96;
|
|
171 tem %= 96;
|
|
172 }
|
|
173 case '+': /* %+x means add character code of char x. */
|
|
174 tem += *p++;
|
|
175 case '.': /* %. means output as character. */
|
|
176 if (left)
|
|
177 {
|
|
178 /* If want to forbid output of 0 and \n and \t,
|
|
179 and this is one of them, increment it. */
|
|
180 while (tem == 0 || tem == '\n' || tem == '\t')
|
|
181 {
|
|
182 tem++;
|
|
183 if (argp == old_argp)
|
|
184 doup++, outend -= strlen (up);
|
|
185 else
|
|
186 doleft++, outend -= strlen (left);
|
|
187 }
|
|
188 }
|
|
189 *op++ = tem | 0200;
|
|
190 case 'f': /* %f means discard next arg. */
|
|
191 argp++;
|
|
192 break;
|
|
193
|
|
194 case 'b': /* %b means back up one arg (and re-use it). */
|
|
195 argp--;
|
|
196 break;
|
|
197
|
|
198 case 'r': /* %r means interchange following two args. */
|
|
199 argp[0] = argp[1];
|
|
200 argp[1] = tem;
|
|
201 old_argp++;
|
|
202 break;
|
|
203
|
|
204 case '>': /* %>xy means if arg is > char code of x, */
|
|
205 if (argp[0] > *p++) /* then add char code of y to the arg, */
|
|
206 argp[0] += *p; /* and in any case don't output. */
|
|
207 p++; /* Leave the arg to be output later. */
|
|
208 break;
|
|
209
|
|
210 case 'a': /* %a means arithmetic. */
|
|
211 /* Next character says what operation.
|
|
212 Add or subtract either a constant or some other arg. */
|
|
213 /* First following character is + to add or - to subtract
|
|
214 or = to assign. */
|
|
215 /* Next following char is 'p' and an arg spec
|
|
216 (0100 plus position of that arg relative to this one)
|
|
217 or 'c' and a constant stored in a character. */
|
|
218 tem = p[2] & 0177;
|
|
219 if (p[1] == 'p')
|
|
220 tem = argp[tem - 0100];
|
|
221 if (p[0] == '-')
|
|
222 argp[0] -= tem;
|
|
223 else if (p[0] == '+')
|
|
224 argp[0] += tem;
|
|
225 else if (p[0] == '*')
|
|
226 argp[0] *= tem;
|
|
227 else if (p[0] == '/')
|
|
228 argp[0] /= tem;
|
|
229 else
|
|
230 argp[0] = tem;
|
|
231
|
|
232 p += 3;
|
|
233 break;
|
|
234
|
|
235 case 'i': /* %i means add one to arg, */
|
|
236 argp[0] ++; /* and leave it to be output later. */
|
|
237 argp[1] ++; /* Increment the following arg, too! */
|
|
238 break;
|
|
239
|
|
240 case '%': /* %% means output %; no arg. */
|
|
241 goto ordinary;
|
|
242
|
|
243 case 'n': /* %n means xor each of next two args with 140. */
|
|
244 argp[0] ^= 0140;
|
|
245 argp[1] ^= 0140;
|
|
246 break;
|
|
247
|
|
248 case 'm': /* %m means xor each of next two args with 177. */
|
|
249 argp[0] ^= 0177;
|
|
250 argp[1] ^= 0177;
|
|
251 break;
|
|
252
|
|
253 case 'B': /* %B means express arg as BCD char code. */
|
|
254 argp[0] += 6 * (tem / 10);
|
|
255 break;
|
|
256
|
|
257 case 'D': /* %D means weird Delta Data transformation. */
|
|
258 argp[0] -= 2 * (tem % 16);
|
|
259 break;
|
|
260 }
|
|
261 }
|
|
262 else
|
|
263 /* Ordinary character in the argument string. */
|
|
264 ordinary:
|
|
265 *op++ = c;
|
|
266 }
|
|
267 *op = 0;
|
|
268 while (doup-- > 0)
|
|
269 strcat (op, up);
|
|
270 while (doleft-- > 0)
|
|
271 strcat (op, left);
|
|
272 return outstring;
|
|
273 }
|
|
274
|
|
275 #ifdef DEBUG
|
|
276
|
|
277 main (argc, argv)
|
|
278 int argc;
|
|
279 char **argv;
|
|
280 {
|
|
281 char buf[50];
|
|
282 int args[3];
|
|
283 args[0] = atoi (argv[2]);
|
|
284 args[1] = atoi (argv[3]);
|
|
285 args[2] = atoi (argv[4]);
|
|
286 tparam1 (argv[1], buf, "LEFT", "UP", args);
|
|
287 printf ("%s\n", buf);
|
|
288 return 0;
|
|
289 }
|
|
290
|
|
291 #endif /* DEBUG */
|