0
+ − 1 /* Merge parameters into a termcap entry string.
+ − 2 Copyright (C) 1985, 1987, 1992, 1993, 1994 Free Software Foundation, Inc.
+ − 3
+ − 4 This file is part of XEmacs.
+ − 5
+ − 6 XEmacs is free software; you can redistribute it and/or modify it
+ − 7 under the terms of the GNU General Public License as published by the
+ − 8 Free Software Foundation; either version 2, or (at your option) any
+ − 9 later version.
+ − 10
+ − 11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
+ − 12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ − 13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ − 14 for more details.
+ − 15
+ − 16 You should have received a copy of the GNU General Public License
+ − 17 along with XEmacs; see the file COPYING. If not, write to
+ − 18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ − 19 Boston, MA 02111-1307, USA. */
+ − 20
+ − 21 /* Synched up with: Not synched with FSF. */
+ − 22
+ − 23 /* config.h may rename various library functions such as malloc. */
+ − 24 #include <config.h>
647
+ − 25 #include "lisp.h"
0
+ − 26
551
+ − 27 #undef realloc
+ − 28 #undef malloc
+ − 29 #undef free
0
+ − 30 #define realloc xrealloc
+ − 31 #define malloc xmalloc
+ − 32 #define free xfree
665
+ − 33 extern void *xmalloc (Bytecount size);
+ − 34 extern void *xrealloc (void *, Bytecount size);
0
+ − 35
+ − 36 /* Assuming STRING is the value of a termcap string entry
+ − 37 containing `%' constructs to expand parameters,
+ − 38 merge in parameter values and store result in block OUTSTRING points to.
+ − 39 LEN is the length of OUTSTRING. If more space is needed,
+ − 40 a block is allocated with `malloc'.
+ − 41
+ − 42 The value returned is the address of the resulting string.
+ − 43 This may be OUTSTRING or may be the address of a block got with `malloc'.
+ − 44 In the latter case, the caller must free the block.
+ − 45
+ − 46 The fourth and following args to tparam serve as the parameter values. */
+ − 47
398
+ − 48 static char *tparam1 (const char *string, char *outstring, int len,
+ − 49 const char *up, const char *left,
0
+ − 50 int *argp);
+ − 51
+ − 52 /* XEmacs: renamed this function because just tparam() conflicts with
+ − 53 ncurses */
398
+ − 54 char *emacs_tparam (const char *string, char *outstring, int len, int arg0,
0
+ − 55 int arg1, int arg2, int arg3);
+ − 56 char *
398
+ − 57 emacs_tparam (const char *string, char *outstring, int len, int arg0,
0
+ − 58 int arg1, int arg2, int arg3)
+ − 59 {
+ − 60 int arg[4];
+ − 61 arg[0] = arg0;
+ − 62 arg[1] = arg1;
+ − 63 arg[2] = arg2;
+ − 64 arg[3] = arg3;
+ − 65 return tparam1 (string, outstring, len, 0, 0, arg);
+ − 66 }
+ − 67
398
+ − 68 const char *BC;
+ − 69 const char *UP;
0
+ − 70
+ − 71 static char tgoto_buf[50];
+ − 72
398
+ − 73 char *tgoto (const char *cm, int hpos, int vpos);
0
+ − 74 char *
398
+ − 75 tgoto (const char *cm, int hpos, int vpos)
0
+ − 76 {
+ − 77 int args[2];
+ − 78 if (!cm)
+ − 79 return 0;
+ − 80 args[0] = vpos;
+ − 81 args[1] = hpos;
+ − 82 return tparam1 (cm, tgoto_buf, 50, UP, BC, args);
+ − 83 }
+ − 84
+ − 85 static char *
398
+ − 86 tparam1 (const char *string, char *outstring, int len, const char *up,
+ − 87 const char *left, int *argp)
0
+ − 88 {
+ − 89 int c;
398
+ − 90 const char *p = string;
0
+ − 91 char *op = outstring;
+ − 92 char *outend;
+ − 93 int outlen = 0;
+ − 94
+ − 95 int tem;
+ − 96 int *old_argp = argp;
+ − 97 int doleft = 0;
+ − 98 int doup = 0;
+ − 99
+ − 100 outend = outstring + len;
+ − 101
+ − 102 while (1)
+ − 103 {
+ − 104 /* If the buffer might be too short, make it bigger. */
+ − 105 if (op + 5 >= outend)
+ − 106 {
+ − 107 char *new;
+ − 108 if (outlen == 0)
+ − 109 {
+ − 110 outlen = len + 40;
+ − 111 new = (char *) malloc (outlen);
+ − 112 outend += 40;
+ − 113 memcpy (new, outstring, op - outstring);
+ − 114 }
+ − 115 else
+ − 116 {
+ − 117 outend += outlen;
+ − 118 outlen *= 2;
+ − 119 new = (char *) realloc (outstring, outlen);
+ − 120 }
+ − 121 op += new - outstring;
+ − 122 outend += new - outstring;
+ − 123 outstring = new;
+ − 124 }
+ − 125 c = *p++;
+ − 126 if (!c)
+ − 127 break;
+ − 128 if (c == '%')
+ − 129 {
+ − 130 c = *p++;
+ − 131 tem = *argp;
+ − 132 switch (c)
+ − 133 {
+ − 134 case 'd': /* %d means output in decimal. */
+ − 135 if (tem < 10)
+ − 136 goto onedigit;
+ − 137 if (tem < 100)
+ − 138 goto twodigit;
+ − 139 case '3': /* %3 means output in decimal, 3 digits. */
+ − 140 if (tem > 999)
+ − 141 {
+ − 142 *op++ = tem / 1000 + '0';
+ − 143 tem %= 1000;
+ − 144 }
+ − 145 *op++ = tem / 100 + '0';
+ − 146 case '2': /* %2 means output in decimal, 2 digits. */
+ − 147 twodigit:
+ − 148 tem %= 100;
+ − 149 *op++ = tem / 10 + '0';
+ − 150 onedigit:
+ − 151 *op++ = tem % 10 + '0';
+ − 152 argp++;
+ − 153 break;
+ − 154
+ − 155 case 'C':
+ − 156 /* For c-100: print quotient of value by 96, if nonzero,
+ − 157 then do like %+. */
+ − 158 if (tem >= 96)
+ − 159 {
+ − 160 *op++ = tem / 96;
+ − 161 tem %= 96;
+ − 162 }
+ − 163 case '+': /* %+x means add character code of char x. */
+ − 164 tem += *p++;
+ − 165 case '.': /* %. means output as character. */
+ − 166 if (left)
+ − 167 {
+ − 168 /* If want to forbid output of 0 and \n and \t,
+ − 169 and this is one of them, increment it. */
+ − 170 while (tem == 0 || tem == '\n' || tem == '\t')
+ − 171 {
+ − 172 tem++;
+ − 173 if (argp == old_argp)
+ − 174 doup++, outend -= strlen (up);
+ − 175 else
+ − 176 doleft++, outend -= strlen (left);
+ − 177 }
+ − 178 }
+ − 179 *op++ = tem | 0200;
+ − 180 case 'f': /* %f means discard next arg. */
+ − 181 argp++;
+ − 182 break;
+ − 183
+ − 184 case 'b': /* %b means back up one arg (and re-use it). */
+ − 185 argp--;
+ − 186 break;
+ − 187
+ − 188 case 'r': /* %r means interchange following two args. */
+ − 189 argp[0] = argp[1];
+ − 190 argp[1] = tem;
+ − 191 old_argp++;
+ − 192 break;
+ − 193
+ − 194 case '>': /* %>xy means if arg is > char code of x, */
+ − 195 if (argp[0] > *p++) /* then add char code of y to the arg, */
+ − 196 argp[0] += *p; /* and in any case don't output. */
+ − 197 p++; /* Leave the arg to be output later. */
+ − 198 break;
+ − 199
+ − 200 case 'a': /* %a means arithmetic. */
+ − 201 /* Next character says what operation.
+ − 202 Add or subtract either a constant or some other arg. */
+ − 203 /* First following character is + to add or - to subtract
+ − 204 or = to assign. */
+ − 205 /* Next following char is 'p' and an arg spec
+ − 206 (0100 plus position of that arg relative to this one)
+ − 207 or 'c' and a constant stored in a character. */
+ − 208 tem = p[2] & 0177;
+ − 209 if (p[1] == 'p')
+ − 210 tem = argp[tem - 0100];
+ − 211 if (p[0] == '-')
+ − 212 argp[0] -= tem;
+ − 213 else if (p[0] == '+')
+ − 214 argp[0] += tem;
+ − 215 else if (p[0] == '*')
+ − 216 argp[0] *= tem;
+ − 217 else if (p[0] == '/')
+ − 218 argp[0] /= tem;
+ − 219 else
+ − 220 argp[0] = tem;
+ − 221
+ − 222 p += 3;
+ − 223 break;
+ − 224
+ − 225 case 'i': /* %i means add one to arg, */
+ − 226 argp[0] ++; /* and leave it to be output later. */
+ − 227 argp[1] ++; /* Increment the following arg, too! */
+ − 228 break;
+ − 229
+ − 230 case '%': /* %% means output %; no arg. */
+ − 231 goto ordinary;
+ − 232
+ − 233 case 'n': /* %n means xor each of next two args with 140. */
+ − 234 argp[0] ^= 0140;
+ − 235 argp[1] ^= 0140;
+ − 236 break;
+ − 237
+ − 238 case 'm': /* %m means xor each of next two args with 177. */
+ − 239 argp[0] ^= 0177;
+ − 240 argp[1] ^= 0177;
+ − 241 break;
+ − 242
+ − 243 case 'B': /* %B means express arg as BCD char code. */
+ − 244 argp[0] += 6 * (tem / 10);
+ − 245 break;
+ − 246
+ − 247 case 'D': /* %D means weird Delta Data transformation. */
+ − 248 argp[0] -= 2 * (tem % 16);
+ − 249 break;
+ − 250 }
+ − 251 }
+ − 252 else
+ − 253 /* Ordinary character in the argument string. */
+ − 254 ordinary:
+ − 255 *op++ = c;
+ − 256 }
+ − 257 *op = 0;
+ − 258 while (doup-- > 0)
+ − 259 strcat (op, up);
+ − 260 while (doleft-- > 0)
+ − 261 strcat (op, left);
+ − 262 return outstring;
+ − 263 }