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