Sync docs of some X, W32, NS C functions.
[emacs.git] / src / tparam.c
blobd8a8f0260f03ac0d8c20a079988c7728ed79e1ee
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 */
25 #ifndef NULL
26 #define NULL (char *) 0
27 #endif
29 /* Assuming STRING is the value of a termcap string entry
30 containing `%' constructs to expand parameters,
31 merge in parameter values and store result in block OUTSTRING points to.
32 LEN is the length of OUTSTRING. If more space is needed,
33 a block is allocated with `malloc'.
35 The value returned is the address of the resulting string.
36 This may be OUTSTRING or may be the address of a block got with `malloc'.
37 In the latter case, the caller must free the block.
39 The fourth and following args to tparam serve as the parameter values. */
41 static char *tparam1 (char *string, char *outstring, int len, char *up, char *left, register int *argp);
43 /* VARARGS 2 */
44 char *
45 tparam (char *string, char *outstring, int len, int arg0, int arg1, int arg2, int arg3)
47 int arg[4];
49 arg[0] = arg0;
50 arg[1] = arg1;
51 arg[2] = arg2;
52 arg[3] = arg3;
53 return tparam1 (string, outstring, len, NULL, NULL, arg);
56 char *BC;
57 char *UP;
59 static char tgoto_buf[50];
61 char *
62 tgoto (char *cm, int hpos, int vpos)
64 int args[2];
65 if (!cm)
66 return NULL;
67 args[0] = vpos;
68 args[1] = hpos;
69 return tparam1 (cm, tgoto_buf, 50, UP, BC, args);
72 static char *
73 tparam1 (char *string, char *outstring, int len, char *up, char *left, register int *argp)
75 register int c;
76 register char *p = string;
77 register char *op = outstring;
78 char *outend;
79 int outlen = 0;
81 register int tem;
82 int *old_argp = argp; /* can move */
83 int *fixed_argp = argp; /* never moves */
84 int explicit_param_p = 0; /* set by %p */
85 int doleft = 0;
86 int doup = 0;
88 outend = outstring + len;
90 while (1)
92 /* If the buffer might be too short, make it bigger. */
93 if (op + 5 >= outend)
95 register char *new;
96 int offset = op - outstring;
98 if (outlen == 0)
100 outlen = len + 40;
101 new = (char *) xmalloc (outlen);
102 memcpy (new, outstring, offset);
104 else
106 outlen *= 2;
107 new = (char *) xrealloc (outstring, outlen);
110 op = new + offset;
111 outend = new + outlen;
112 outstring = new;
114 c = *p++;
115 if (!c)
116 break;
117 if (c == '%')
119 c = *p++;
120 if (explicit_param_p)
121 explicit_param_p = 0;
122 else
123 tem = *argp;
124 switch (c)
126 case 'd': /* %d means output in decimal. */
127 if (tem < 10)
128 goto onedigit;
129 if (tem < 100)
130 goto twodigit;
131 case '3': /* %3 means output in decimal, 3 digits. */
132 if (tem > 999)
134 *op++ = tem / 1000 + '0';
135 tem %= 1000;
137 *op++ = tem / 100 + '0';
138 case '2': /* %2 means output in decimal, 2 digits. */
139 twodigit:
140 tem %= 100;
141 *op++ = tem / 10 + '0';
142 onedigit:
143 *op++ = tem % 10 + '0';
144 argp++;
145 break;
146 case 'p': /* %pN means use param N for next subst. */
147 tem = fixed_argp[(*p++) - '1'];
148 explicit_param_p = 1;
149 break;
150 case 'C':
151 /* For c-100: print quotient of value by 96, if nonzero,
152 then do like %+. */
153 if (tem >= 96)
155 *op++ = tem / 96;
156 tem %= 96;
158 case '+': /* %+x means add character code of char x. */
159 tem += *p++;
160 case '.': /* %. means output as character. */
161 if (left)
163 /* If want to forbid output of 0 and \n and \t,
164 and this is one of them, increment it. */
165 while (tem == 0 || tem == '\n' || tem == '\t')
167 tem++;
168 if (argp == old_argp)
169 doup++, outend -= strlen (up);
170 else
171 doleft++, outend -= strlen (left);
174 *op++ = tem ? tem : 0200;
175 case 'f': /* %f means discard next arg. */
176 argp++;
177 break;
179 case 'b': /* %b means back up one arg (and re-use it). */
180 argp--;
181 break;
183 case 'r': /* %r means interchange following two args. */
184 argp[0] = argp[1];
185 argp[1] = tem;
186 old_argp++;
187 break;
189 case '>': /* %>xy means if arg is > char code of x, */
190 if (argp[0] > *p++) /* then add char code of y to the arg, */
191 argp[0] += *p; /* and in any case don't output. */
192 p++; /* Leave the arg to be output later. */
193 break;
195 case 'a': /* %a means arithmetic. */
196 /* Next character says what operation.
197 Add or subtract either a constant or some other arg. */
198 /* First following character is + to add or - to subtract
199 or = to assign. */
200 /* Next following char is 'p' and an arg spec
201 (0100 plus position of that arg relative to this one)
202 or 'c' and a constant stored in a character. */
203 tem = p[2] & 0177;
204 if (p[1] == 'p')
205 tem = argp[tem - 0100];
206 if (p[0] == '-')
207 argp[0] -= tem;
208 else if (p[0] == '+')
209 argp[0] += tem;
210 else if (p[0] == '*')
211 argp[0] *= tem;
212 else if (p[0] == '/')
213 argp[0] /= tem;
214 else
215 argp[0] = tem;
217 p += 3;
218 break;
220 case 'i': /* %i means add one to arg, */
221 argp[0] ++; /* and leave it to be output later. */
222 argp[1] ++; /* Increment the following arg, too! */
223 break;
225 case '%': /* %% means output %; no arg. */
226 goto ordinary;
228 case 'n': /* %n means xor each of next two args with 140. */
229 argp[0] ^= 0140;
230 argp[1] ^= 0140;
231 break;
233 case 'm': /* %m means xor each of next two args with 177. */
234 argp[0] ^= 0177;
235 argp[1] ^= 0177;
236 break;
238 case 'B': /* %B means express arg as BCD char code. */
239 argp[0] += 6 * (tem / 10);
240 break;
242 case 'D': /* %D means weird Delta Data transformation. */
243 argp[0] -= 2 * (tem % 16);
244 break;
246 default:
247 abort ();
250 else
251 /* Ordinary character in the argument string. */
252 ordinary:
253 *op++ = c;
255 *op = 0;
256 while (doup-- > 0)
257 strcat (op, up);
258 while (doleft-- > 0)
259 strcat (op, left);
260 return outstring;
263 #ifdef DEBUG
265 main (argc, argv)
266 int argc;
267 char **argv;
269 char buf[50];
270 int args[3];
271 args[0] = atoi (argv[2]);
272 args[1] = atoi (argv[3]);
273 args[2] = atoi (argv[4]);
274 tparam1 (argv[1], buf, "LEFT", "UP", args);
275 printf ("%s\n", buf);
276 return 0;
279 #endif /* DEBUG */
281 /* arch-tag: 83f7b5ac-a808-4f75-b87a-123de009b402
282 (do not change this comment) */