1 /* tparam.c - merge parameters into a termcap entry string. */
3 /* Copyright (C) 1985, 1986, 1993,1994, 1995, 1998, 2001,2003,2005,2006,2008,2009 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
7 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
21 /* Emacs config.h may rename various library functions such as malloc. */
28 extern char *getenv ();
29 extern char *malloc ();
30 extern char *realloc ();
33 #if defined (HAVE_STRING_H)
37 #if !defined (HAVE_BCOPY) && (defined (HAVE_STRING_H) || defined (STDC_HEADERS))
38 # define bcopy(s, d, n) memcpy ((d), (s), (n))
41 #else /* not HAVE_CONFIG_H */
43 #if defined(HAVE_STRING_H) || defined(STDC_HEADERS)
44 #define bcopy(s, d, n) memcpy ((d), (s), (n))
55 #endif /* not HAVE_CONFIG_H */
60 #define NULL (char *) 0
67 write (2, "virtual memory exhausted\n", 25);
75 register char *tem = malloc (size);
87 register char *tem = realloc (ptr, size);
93 #endif /* not emacs */
95 /* Assuming STRING is the value of a termcap string entry
96 containing `%' constructs to expand parameters,
97 merge in parameter values and store result in block OUTSTRING points to.
98 LEN is the length of OUTSTRING. If more space is needed,
99 a block is allocated with `malloc'.
101 The value returned is the address of the resulting string.
102 This may be OUTSTRING or may be the address of a block got with `malloc'.
103 In the latter case, the caller must free the block.
105 The fourth and following args to tparam serve as the parameter values. */
107 static char *tparam1 ();
111 tparam (string, outstring, len, arg0, arg1, arg2, arg3)
115 int arg0, arg1, arg2, arg3;
123 return tparam1 (string, outstring, len, NULL, NULL, arg);
126 __private_extern__ char *BC;
127 __private_extern__ char *UP;
129 static char tgoto_buf[50];
133 tgoto (cm, hpos, vpos)
142 return tparam1 (cm, tgoto_buf, 50, UP, BC, args);
146 tparam1 (string, outstring, len, up, left, argp)
154 register char *p = string;
155 register char *op = outstring;
160 int *old_argp = argp;
164 outend = outstring + len;
168 /* If the buffer might be too short, make it bigger. */
169 if (op + 5 >= outend)
175 new = (char *) xmalloc (outlen);
177 bcopy (outstring, new, op - outstring);
183 new = (char *) xrealloc (outstring, outlen);
185 op += new - outstring;
186 outend += new - outstring;
198 case 'd': /* %d means output in decimal. */
203 case '3': /* %3 means output in decimal, 3 digits. */
206 *op++ = tem / 1000 + '0';
209 *op++ = tem / 100 + '0';
210 case '2': /* %2 means output in decimal, 2 digits. */
213 *op++ = tem / 10 + '0';
215 *op++ = tem % 10 + '0';
220 /* For c-100: print quotient of value by 96, if nonzero,
227 case '+': /* %+x means add character code of char x. */
229 case '.': /* %. means output as character. */
232 /* If want to forbid output of 0 and \n and \t,
233 and this is one of them, increment it. */
234 while (tem == 0 || tem == '\n' || tem == '\t')
237 if (argp == old_argp)
238 doup++, outend -= strlen (up);
240 doleft++, outend -= strlen (left);
243 *op++ = tem ? tem : 0200;
244 case 'f': /* %f means discard next arg. */
248 case 'b': /* %b means back up one arg (and re-use it). */
252 case 'r': /* %r means interchange following two args. */
258 case '>': /* %>xy means if arg is > char code of x, */
259 if (argp[0] > *p++) /* then add char code of y to the arg, */
260 argp[0] += *p; /* and in any case don't output. */
261 p++; /* Leave the arg to be output later. */
264 case 'a': /* %a means arithmetic. */
265 /* Next character says what operation.
266 Add or subtract either a constant or some other arg. */
267 /* First following character is + to add or - to subtract
269 /* Next following char is 'p' and an arg spec
270 (0100 plus position of that arg relative to this one)
271 or 'c' and a constant stored in a character. */
274 tem = argp[tem - 0100];
277 else if (p[0] == '+')
279 else if (p[0] == '*')
281 else if (p[0] == '/')
289 case 'i': /* %i means add one to arg, */
290 argp[0] ++; /* and leave it to be output later. */
291 argp[1] ++; /* Increment the following arg, too! */
294 case '%': /* %% means output %; no arg. */
297 case 'n': /* %n means xor each of next two args with 140. */
302 case 'm': /* %m means xor each of next two args with 177. */
307 case 'B': /* %B means express arg as BCD char code. */
308 argp[0] += 6 * (tem / 10);
311 case 'D': /* %D means weird Delta Data transformation. */
312 argp[0] -= 2 * (tem % 16);
317 /* Ordinary character in the argument string. */
337 args[0] = atoi (argv[2]);
338 args[1] = atoi (argv[3]);
339 args[2] = atoi (argv[4]);
340 tparam1 (argv[1], buf, "LEFT", "UP", args);
341 printf ("%s\n", buf);