* doc/lispref/minibuf.texi (Programmed Completion): Document metadata method.
[emacs.git] / src / tparam.c
blobac21667d65bf8e2288736562dd64f41176973fd9
1 /* Merge parameters into a termcap entry string.
2 Copyright (C) 1985, 1987, 1993, 1995, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
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.
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.
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
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA. */
20 /* Emacs config.h may rename various library functions such as malloc. */
21 #include <config.h>
22 #include <setjmp.h>
23 #include "lisp.h" /* for xmalloc */
24 #include "tparam.h"
26 #ifndef NULL
27 #define NULL (char *) 0
28 #endif
30 /* Assuming STRING is the value of a termcap string entry
31 containing `%' constructs to expand parameters,
32 merge in parameter values and store result in block OUTSTRING points to.
33 LEN is the length of OUTSTRING. If more space is needed,
34 a block is allocated with `malloc'.
36 The value returned is the address of the resulting string.
37 This may be OUTSTRING or may be the address of a block got with `malloc'.
38 In the latter case, the caller must free the block.
40 The fourth and following args to tparam serve as the parameter values. */
42 static char *tparam1 (char const *string, char *outstring, int len,
43 char *up, char *left, int *argp);
45 char *
46 tparam (const char *string, char *outstring, int len,
47 int arg0, int arg1, int arg2, int arg3)
49 int arg[4];
51 arg[0] = arg0;
52 arg[1] = arg1;
53 arg[2] = arg2;
54 arg[3] = arg3;
55 return tparam1 (string, outstring, len, NULL, NULL, arg);
58 char *BC;
59 char *UP;
61 static char tgoto_buf[50];
63 char *
64 tgoto (const char *cm, int hpos, int vpos)
66 int args[2];
67 if (!cm)
68 return NULL;
69 args[0] = vpos;
70 args[1] = hpos;
71 return tparam1 (cm, tgoto_buf, 50, UP, BC, args);
74 static char *
75 tparam1 (const char *string, char *outstring, int len,
76 char *up, char *left, register int *argp)
78 register int c;
79 register const char *p = string;
80 register char *op = outstring;
81 char *outend;
82 char *new = 0;
83 ptrdiff_t outlen = 0;
85 register int tem;
86 int *old_argp = argp; /* can move */
87 int *fixed_argp = argp; /* never moves */
88 int explicit_param_p = 0; /* set by %p */
89 ptrdiff_t doleft = 0;
90 ptrdiff_t doup = 0;
91 ptrdiff_t append_len = 0;
93 outend = outstring + len;
95 while (1)
97 /* If the buffer might be too short, make it bigger. */
98 while (outend - op - append_len <= 5)
100 ptrdiff_t offset = op - outstring;
102 if (outlen == 0)
104 outlen = len + 40;
105 new = (char *) xmalloc (outlen);
106 memcpy (new, outstring, offset);
108 else
110 new = xpalloc (outstring, &outlen, 1, -1, 1);
113 op = new + offset;
114 outend = new + outlen;
115 outstring = new;
117 c = *p++;
118 if (!c)
119 break;
120 if (c == '%')
122 c = *p++;
123 if (explicit_param_p)
124 explicit_param_p = 0;
125 else
126 tem = *argp;
127 switch (c)
129 case 'd': /* %d means output in decimal. */
130 if (tem < 10)
131 goto onedigit;
132 if (tem < 100)
133 goto twodigit;
134 case '3': /* %3 means output in decimal, 3 digits. */
135 if (tem > 999)
137 *op++ = tem / 1000 + '0';
138 tem %= 1000;
140 *op++ = tem / 100 + '0';
141 case '2': /* %2 means output in decimal, 2 digits. */
142 twodigit:
143 tem %= 100;
144 *op++ = tem / 10 + '0';
145 onedigit:
146 *op++ = tem % 10 + '0';
147 argp++;
148 break;
149 case 'p': /* %pN means use param N for next subst. */
150 tem = fixed_argp[(*p++) - '1'];
151 explicit_param_p = 1;
152 break;
153 case 'C':
154 /* For c-100: print quotient of value by 96, if nonzero,
155 then do like %+. */
156 if (tem >= 96)
158 *op++ = tem / 96;
159 tem %= 96;
161 case '+': /* %+x means add character code of char x. */
162 tem += *p++;
163 case '.': /* %. means output as character. */
164 if (left)
166 /* If want to forbid output of 0 and \n and \t,
167 and this is one of them, increment it. */
168 while (tem == 0 || tem == '\n' || tem == '\t')
170 ptrdiff_t append_len_incr;
171 tem++;
172 if (argp == old_argp)
173 doup++, append_len_incr = strlen (up);
174 else
175 doleft++, append_len_incr = strlen (left);
176 if (INT_ADD_OVERFLOW (append_len, append_len_incr))
177 memory_full (SIZE_MAX);
178 append_len += append_len_incr;
181 *op++ = tem ? tem : 0200;
182 case 'f': /* %f means discard next arg. */
183 argp++;
184 break;
186 case 'b': /* %b means back up one arg (and re-use it). */
187 argp--;
188 break;
190 case 'r': /* %r means interchange following two args. */
191 argp[0] = argp[1];
192 argp[1] = tem;
193 old_argp++;
194 break;
196 case '>': /* %>xy means if arg is > char code of x, */
197 if (argp[0] > *p++) /* then add char code of y to the arg, */
198 argp[0] += *p; /* and in any case don't output. */
199 p++; /* Leave the arg to be output later. */
200 break;
202 case 'a': /* %a means arithmetic. */
203 /* Next character says what operation.
204 Add or subtract either a constant or some other arg. */
205 /* First following character is + to add or - to subtract
206 or = to assign. */
207 /* Next following char is 'p' and an arg spec
208 (0100 plus position of that arg relative to this one)
209 or 'c' and a constant stored in a character. */
210 tem = p[2] & 0177;
211 if (p[1] == 'p')
212 tem = argp[tem - 0100];
213 if (p[0] == '-')
214 argp[0] -= tem;
215 else if (p[0] == '+')
216 argp[0] += tem;
217 else if (p[0] == '*')
218 argp[0] *= tem;
219 else if (p[0] == '/')
220 argp[0] /= tem;
221 else
222 argp[0] = tem;
224 p += 3;
225 break;
227 case 'i': /* %i means add one to arg, */
228 argp[0] ++; /* and leave it to be output later. */
229 argp[1] ++; /* Increment the following arg, too! */
230 break;
232 case '%': /* %% means output %; no arg. */
233 goto ordinary;
235 case 'n': /* %n means xor each of next two args with 140. */
236 argp[0] ^= 0140;
237 argp[1] ^= 0140;
238 break;
240 case 'm': /* %m means xor each of next two args with 177. */
241 argp[0] ^= 0177;
242 argp[1] ^= 0177;
243 break;
245 case 'B': /* %B means express arg as BCD char code. */
246 argp[0] += 6 * (tem / 10);
247 break;
249 case 'D': /* %D means weird Delta Data transformation. */
250 argp[0] -= 2 * (tem % 16);
251 break;
253 default:
254 abort ();
257 else
258 /* Ordinary character in the argument string. */
259 ordinary:
260 *op++ = c;
262 *op = 0;
263 while (doup-- > 0)
264 strcat (op, up);
265 while (doleft-- > 0)
266 strcat (op, left);
267 return outstring;
270 #ifdef DEBUG
273 main (int argc, char **argv)
275 char buf[50];
276 int args[3];
277 args[0] = atoi (argv[2]);
278 args[1] = atoi (argv[3]);
279 args[2] = atoi (argv[4]);
280 tparam1 (argv[1], buf, 50, "LEFT", "UP", args);
281 printf ("%s\n", buf);
282 return 0;
285 #endif /* DEBUG */