OSDN Git Service

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