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