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