OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [uclinux/] [userland/] [termcap/] [tparam.c] - Blame information for rev 1770

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.