1 /****************************************************************************
2 * Copyright (c) 1998-2001,2002 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
31 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
32 * and: Thomas E. Dickey, 1996 on *
33 ****************************************************************************/
40 #include <curses.priv.h>
46 MODULE_ID("$Id: lib_tparm.c,v 1.62 2002/10/05 19:33:24 Frank.Henigman Exp $")
52 * Substitute the given parameters into the given string by the following
53 * rules (taken from terminfo(5)):
55 * Cursor addressing and other strings requiring parame-
56 * ters in the terminal are described by a parameterized string
57 * capability, with like escapes %x in it. For example, to
58 * address the cursor, the cup capability is given, using two
59 * parameters: the row and column to address to. (Rows and
60 * columns are numbered from zero and refer to the physical
61 * screen visible to the user, not to any unseen memory.) If
62 * the terminal has memory relative cursor addressing, that can
65 * The parameter mechanism uses a stack and special %
66 * codes to manipulate it. Typically a sequence will push one
67 * of the parameters onto the stack and then print it in some
68 * format. Often more complex operations are necessary.
70 * The % encodings have the following meanings:
73 * %c print pop() like %c in printf()
74 * %s print pop() like %s in printf()
75 * %[[:]flags][width[.precision]][doxXs]
76 * as in printf, flags are [-+#] and space
77 * The ':' is used to avoid making %+ or %-
78 * patterns (see below).
80 * %p[1-9] push ith parm
81 * %P[a-z] set dynamic variable [a-z] to pop()
82 * %g[a-z] get dynamic variable [a-z] and push it
83 * %P[A-Z] set static variable [A-Z] to pop()
84 * %g[A-Z] get static variable [A-Z] and push it
86 * %'c' push char constant c
87 * %{nn} push integer constant nn
90 * arithmetic (%m is mod): push(pop() op pop())
91 * %& %| %^ bit operations: push(pop() op pop())
92 * %= %> %< logical operations: push(pop() op pop())
93 * %A %O logical and & or operations for conditionals
94 * %! %~ unary operations push(op pop())
95 * %i add 1 to first two parms (for ANSI terminals)
97 * %? expr %t thenpart %e elsepart %;
98 * if-then-else, %e elsepart is optional.
99 * else-if's are possible ala Algol 68:
100 * %? c1 %t b1 %e c2 %t b2 %e c3 %t b3 %e c4 %t b4 %e b5 %;
102 * For those of the above operators which are binary and not commutative,
103 * the stack works in the usual way, with
105 * resulting in x mod y, not the reverse.
118 NCURSES_EXPORT_VAR(int) _nc_tparm_err = 0;
120 static stack_frame stack[STACKSIZE];
121 static int stack_ptr;
122 static const char *tparam_base = "";
125 static const char *tname;
128 static char *out_buff;
129 static size_t out_size;
130 static size_t out_used;
137 FreeAndNull(out_buff);
145 get_space(size_t need)
148 if (need > out_size) {
150 out_buff = typeRealloc(char, out_size, out_buff);
152 _nc_err_abort(MSG_NO_MEMORY);
157 save_text(const char *fmt, const char *s, int len)
159 size_t s_len = strlen(s);
160 if (len > (int) s_len)
163 get_space(s_len + 1);
165 (void) sprintf(out_buff + out_used, fmt, s);
166 out_used += strlen(out_buff + out_used);
170 save_number(const char *fmt, int number, int len)
173 len = 30; /* actually log10(MAX_INT)+1 */
175 get_space((unsigned) len + 1);
177 (void) sprintf(out_buff + out_used, fmt, number);
178 out_used += strlen(out_buff + out_used);
187 out_buff[out_used++] = c;
193 if (stack_ptr < STACKSIZE) {
194 stack[stack_ptr].num_type = TRUE;
195 stack[stack_ptr].data.num = x;
198 DEBUG(2, ("npush: stack overflow: %s", _nc_visbuf(tparam_base)));
209 if (stack[stack_ptr].num_type)
210 result = stack[stack_ptr].data.num;
212 DEBUG(2, ("npop: stack underflow: %s", _nc_visbuf(tparam_base)));
221 if (stack_ptr < STACKSIZE) {
222 stack[stack_ptr].num_type = FALSE;
223 stack[stack_ptr].data.str = x;
226 DEBUG(2, ("spush: stack overflow: %s", _nc_visbuf(tparam_base)));
234 static char dummy[] = ""; /* avoid const-cast */
235 char *result = dummy;
238 if (!stack[stack_ptr].num_type && stack[stack_ptr].data.str != 0)
239 result = stack[stack_ptr].data.str;
241 DEBUG(2, ("spop: stack underflow: %s", _nc_visbuf(tparam_base)));
247 static inline const char *
248 parse_format(const char *s, char *format, int *len)
251 bool allowminus = FALSE;
261 while (*s != '\0' && !done) {
263 case 'c': /* FALLTHRU */
264 case 'd': /* FALLTHRU */
265 case 'o': /* FALLTHRU */
266 case 'x': /* FALLTHRU */
267 case 'X': /* FALLTHRU */
276 } else { /* value before '.' is the width */
300 if (isdigit(UChar(*s))) {
301 value = (value * 10) + (*s - '0');
312 * If we found an error, ignore (and remove) the flags.
315 my_width = my_prec = value = 0;
322 * Any value after '.' is the precision. If we did not see '.', then
323 * the value is the width.
331 /* return maximum string length in print */
332 *len = (my_width > my_prec) ? my_width : my_prec;
336 #define isUPPER(c) ((c) >= 'A' && (c) <= 'Z')
337 #define isLOWER(c) ((c) >= 'a' && (c) <= 'z')
340 tparam_internal(const char *string, va_list ap)
353 register const char *cp;
354 static size_t len_fmt;
355 static char dummy[] = "";
357 static int dynamic_var[NUM_VARS];
358 static int static_vars[NUM_VARS];
364 if ((len2 = strlen(string)) > len_fmt) {
365 len_fmt = len2 + len_fmt + 2;
366 if ((format = typeRealloc(char, len_fmt, format)) == 0)
371 * Find the highest parameter-number referred to in the format string.
372 * Use this value to limit the number of arguments copied from the
373 * variable-length argument list.
379 memset(p_is_s, 0, sizeof(p_is_s));
382 * Analyze the string to see how many parameters we need from the varargs
383 * list, and what their types are. We will only accept string parameters
384 * if they appear as a %l or %s format following an explicit parameter
385 * reference (e.g., %p2%s). All other parameters are numbers.
387 * 'number' counts coarsely the number of pop's we see in the string, and
388 * 'popcount' shows the highest parameter number in the string. We would
389 * like to simply use the latter count, but if we are reading termcap
390 * strings, there may be cases that we cannot see the explicit parameter
393 for (cp = string; (cp - string) < (int) len2;) {
396 cp = parse_format(cp, format, &len);
401 case 'd': /* FALLTHRU */
402 case 'o': /* FALLTHRU */
403 case 'x': /* FALLTHRU */
404 case 'X': /* FALLTHRU */
405 case 'c': /* FALLTHRU */
413 p_is_s[lastpop - 1] = dummy;
420 if (i >= 0 && i <= 9) {
422 if (lastpop > popcount)
443 while (*cp >= '0' && *cp <= '9') {
484 for (i = 0; i < max(popcount, number); i++) {
486 * A few caps (such as plab_norm) have string-valued parms.
487 * We'll have to assume that the caller knows the difference, since
488 * a char* and an int may not be the same size on the stack. The
489 * normal prototype for this uses 9 long's, which is consistent with
490 * our va_arg() usage.
492 if (p_is_s[i] != 0) {
493 p_is_s[i] = va_arg(ap, char *);
495 param[i] = va_arg(ap, long int);
500 * This is a termcap compatibility hack. If there are no explicit pop
501 * operations in the string, load the stack in such a way that
502 * successive pops will grab successive parameters. That will make
503 * the expansion of (for example) \E[%d;%dH work correctly in termcap
504 * style, which means tparam() will expand termcap strings OK.
509 for (i = number - 1; i >= 0; i--)
513 if (_nc_tracing & TRACE_CALLS) {
514 for (i = 0; i < popcount; i++) {
516 save_text(", %s", _nc_visbuf(p_is_s[i]), 0);
518 save_number(", %d", param[i], 0);
520 _tracef(T_CALLED("%s(%s%s)"), tname, _nc_visbuf(string), out_buff);
526 if (*string != '%') {
529 tparam_base = string++;
530 string = parse_format(string, format, &len);
538 case 'd': /* FALLTHRU */
539 case 'o': /* FALLTHRU */
540 case 'x': /* FALLTHRU */
541 case 'X': /* FALLTHRU */
542 save_number(format, npop(), len);
545 case 'c': /* FALLTHRU */
550 save_number("%d", (int) strlen(spop()), 0);
554 save_text(format, spop(), len);
560 if (i >= 0 && i < 9) {
570 if (isUPPER(*string)) {
572 static_vars[i] = npop();
573 } else if (isLOWER(*string)) {
575 dynamic_var[i] = npop();
581 if (isUPPER(*string)) {
583 npush(static_vars[i]);
584 } else if (isLOWER(*string)) {
586 npush(dynamic_var[i]);
599 while (*string >= '0' && *string <= '9') {
600 number = number * 10 + *string - '0';
607 npush(npop() + npop());
617 npush(npop() * npop());
623 npush(y ? (x / y) : 0);
629 npush(y ? (x % y) : 0);
633 npush(npop() && npop());
637 npush(npop() || npop());
641 npush(npop() & npop());
645 npush(npop() | npop());
649 npush(npop() ^ npop());
691 /* scan forward for %e or %; at level zero */
695 if (*string == '%') {
699 else if (*string == ';') {
704 } else if (*string == 'e' && level == 0)
715 /* scan forward for a %; at level zero */
719 if (*string == '%') {
723 else if (*string == ';') {
739 } /* endswitch (*string) */
740 } /* endelse (*string == '%') */
746 } /* endwhile (*string) */
749 out_buff[out_used] = '\0';
751 T((T_RETURN("%s"), _nc_visbuf(out_buff)));
755 NCURSES_EXPORT(char *)
756 tparm(NCURSES_CONST char *string,...)
762 va_start(ap, string);
766 result = tparam_internal(string, ap);