1 /****************************************************************************
2 * Copyright (c) 1998-2007,2008 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.76 2008/08/16 19:22:55 tom 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.
108 NCURSES_EXPORT_VAR(int) _nc_tparm_err = 0;
110 #define TPS(var) _nc_prescreen.tparm_state.var
116 if (TPS(out_buff) != 0) {
117 FreeAndNull(TPS(out_buff));
120 FreeAndNull(TPS(fmt_buff));
126 static NCURSES_INLINE void
127 get_space(size_t need)
129 need += TPS(out_used);
130 if (need > TPS(out_size)) {
131 TPS(out_size) = need * 2;
132 TPS(out_buff) = typeRealloc(char, TPS(out_size), TPS(out_buff));
133 if (TPS(out_buff) == 0)
134 _nc_err_abort(MSG_NO_MEMORY);
138 static NCURSES_INLINE void
139 save_text(const char *fmt, const char *s, int len)
141 size_t s_len = strlen(s);
142 if (len > (int) s_len)
145 get_space(s_len + 1);
147 (void) sprintf(TPS(out_buff) + TPS(out_used), fmt, s);
148 TPS(out_used) += strlen(TPS(out_buff) + TPS(out_used));
151 static NCURSES_INLINE void
152 save_number(const char *fmt, int number, int len)
155 len = 30; /* actually log10(MAX_INT)+1 */
157 get_space((unsigned) len + 1);
159 (void) sprintf(TPS(out_buff) + TPS(out_used), fmt, number);
160 TPS(out_used) += strlen(TPS(out_buff) + TPS(out_used));
163 static NCURSES_INLINE void
169 TPS(out_buff)[TPS(out_used)++] = (char) c;
172 static NCURSES_INLINE void
175 if (TPS(stack_ptr) < STACKSIZE) {
176 TPS(stack)[TPS(stack_ptr)].num_type = TRUE;
177 TPS(stack)[TPS(stack_ptr)].data.num = x;
180 DEBUG(2, ("npush: stack overflow: %s", _nc_visbuf(TPS(tparam_base))));
185 static NCURSES_INLINE int
189 if (TPS(stack_ptr) > 0) {
191 if (TPS(stack)[TPS(stack_ptr)].num_type)
192 result = TPS(stack)[TPS(stack_ptr)].data.num;
194 DEBUG(2, ("npop: stack underflow: %s", _nc_visbuf(TPS(tparam_base))));
200 static NCURSES_INLINE void
203 if (TPS(stack_ptr) < STACKSIZE) {
204 TPS(stack)[TPS(stack_ptr)].num_type = FALSE;
205 TPS(stack)[TPS(stack_ptr)].data.str = x;
208 DEBUG(2, ("spush: stack overflow: %s", _nc_visbuf(TPS(tparam_base))));
213 static NCURSES_INLINE char *
216 static char dummy[] = ""; /* avoid const-cast */
217 char *result = dummy;
218 if (TPS(stack_ptr) > 0) {
220 if (!TPS(stack)[TPS(stack_ptr)].num_type
221 && TPS(stack)[TPS(stack_ptr)].data.str != 0)
222 result = TPS(stack)[TPS(stack_ptr)].data.str;
224 DEBUG(2, ("spop: stack underflow: %s", _nc_visbuf(TPS(tparam_base))));
230 static NCURSES_INLINE const char *
231 parse_format(const char *s, char *format, int *len)
236 bool allowminus = FALSE;
246 while (*s != '\0' && !done) {
248 case 'c': /* FALLTHRU */
249 case 'd': /* FALLTHRU */
250 case 'o': /* FALLTHRU */
251 case 'x': /* FALLTHRU */
252 case 'X': /* FALLTHRU */
261 } else { /* value before '.' is the width */
285 if (isdigit(UChar(*s))) {
286 value = (value * 10) + (*s - '0');
297 * If we found an error, ignore (and remove) the flags.
300 my_width = my_prec = value = 0;
307 * Any value after '.' is the precision. If we did not see '.', then
308 * the value is the width.
316 /* return maximum string length in print */
317 *len = (my_width > my_prec) ? my_width : my_prec;
322 #define isUPPER(c) ((c) >= 'A' && (c) <= 'Z')
323 #define isLOWER(c) ((c) >= 'a' && (c) <= 'z')
326 * Analyze the string to see how many parameters we need from the varargs list,
327 * and what their types are. We will only accept string parameters if they
328 * appear as a %l or %s format following an explicit parameter reference (e.g.,
329 * %p2%s). All other parameters are numbers.
331 * 'number' counts coarsely the number of pop's we see in the string, and
332 * 'popcount' shows the highest parameter number in the string. We would like
333 * to simply use the latter count, but if we are reading termcap strings, there
334 * may be cases that we cannot see the explicit parameter numbers.
337 _nc_tparm_analyze(const char *string, char *p_is_s[NUM_PARM], int *popcount)
344 const char *cp = string;
345 static char dummy[] = "";
350 if ((len2 = strlen(cp)) > TPS(fmt_size)) {
351 TPS(fmt_size) = len2 + TPS(fmt_size) + 2;
352 TPS(fmt_buff) = typeRealloc(char, TPS(fmt_size), TPS(fmt_buff));
353 if (TPS(fmt_buff) == 0)
357 memset(p_is_s, 0, sizeof(p_is_s[0]) * NUM_PARM);
360 while ((cp - string) < (int) len2) {
363 cp = parse_format(cp, TPS(fmt_buff), &len);
368 case 'd': /* FALLTHRU */
369 case 'o': /* FALLTHRU */
370 case 'x': /* FALLTHRU */
371 case 'X': /* FALLTHRU */
372 case 'c': /* FALLTHRU */
381 p_is_s[lastpop - 1] = dummy;
387 i = (UChar(*cp) - '0');
388 if (i >= 0 && i <= NUM_PARM) {
390 if (lastpop > *popcount)
411 while (isdigit(UChar(*cp))) {
440 /* will add 1 to first (usually two) parameters */
448 if (number > NUM_PARM)
453 static NCURSES_INLINE char *
454 tparam_internal(const char *string, va_list ap)
456 char *p_is_s[NUM_PARM];
457 TPARM_ARG param[NUM_PARM];
464 const char *cp = string;
474 * Find the highest parameter-number referred to in the format string.
475 * Use this value to limit the number of arguments copied from the
476 * variable-length argument list.
478 number = _nc_tparm_analyze(cp, p_is_s, &popcount);
479 if (TPS(fmt_buff) == 0)
482 for (i = 0; i < max(popcount, number); i++) {
484 * A few caps (such as plab_norm) have string-valued parms.
485 * We'll have to assume that the caller knows the difference, since
486 * a char* and an int may not be the same size on the stack. The
487 * normal prototype for this uses 9 long's, which is consistent with
488 * our va_arg() usage.
490 if (p_is_s[i] != 0) {
491 p_is_s[i] = va_arg(ap, char *);
493 param[i] = va_arg(ap, TPARM_ARG);
498 * This is a termcap compatibility hack. If there are no explicit pop
499 * operations in the string, load the stack in such a way that
500 * successive pops will grab successive parameters. That will make
501 * the expansion of (for example) \E[%d;%dH work correctly in termcap
502 * style, which means tparam() will expand termcap strings OK.
507 for (i = number - 1; i >= 0; i--) {
515 if (USE_TRACEF(TRACE_CALLS)) {
516 for (i = 0; i < popcount; i++) {
518 save_text(", %s", _nc_visbuf(p_is_s[i]), 0);
520 save_number(", %d", param[i], 0);
522 _tracef(T_CALLED("%s(%s%s)"), TPS(tname), _nc_visbuf(cp), TPS(out_buff));
524 _nc_unlock_global(tracef);
528 while ((cp - string) < (int) len2) {
530 save_char(UChar(*cp));
532 TPS(tparam_base) = cp++;
533 cp = parse_format(cp, TPS(fmt_buff), &len);
541 case 'd': /* FALLTHRU */
542 case 'o': /* FALLTHRU */
543 case 'x': /* FALLTHRU */
544 case 'X': /* FALLTHRU */
545 save_number(TPS(fmt_buff), npop(), len);
548 case 'c': /* FALLTHRU */
553 save_number("%d", (int) strlen(spop()), 0);
557 save_text(TPS(fmt_buff), spop(), len);
562 i = (UChar(*cp) - '1');
563 if (i >= 0 && i < NUM_PARM) {
574 i = (UChar(*cp) - 'A');
575 TPS(static_vars)[i] = npop();
576 } else if (isLOWER(*cp)) {
577 i = (UChar(*cp) - 'a');
578 TPS(dynamic_var)[i] = npop();
585 i = (UChar(*cp) - 'A');
586 npush(TPS(static_vars)[i]);
587 } else if (isLOWER(*cp)) {
588 i = (UChar(*cp) - 'a');
589 npush(TPS(dynamic_var)[i]);
602 while (isdigit(UChar(*cp))) {
603 number = (number * 10) + (UChar(*cp) - '0');
610 npush(npop() + npop());
620 npush(npop() * npop());
626 npush(y ? (x / y) : 0);
632 npush(y ? (x % y) : 0);
636 npush(npop() && npop());
640 npush(npop() || npop());
644 npush(npop() & npop());
648 npush(npop() | npop());
652 npush(npop() ^ npop());
694 /* scan forward for %e or %; at level zero */
702 else if (*cp == ';') {
707 } else if (*cp == 'e' && level == 0)
718 /* scan forward for a %; at level zero */
726 else if (*cp == ';') {
742 } /* endswitch (*cp) */
743 } /* endelse (*cp == '%') */
749 } /* endwhile (*cp) */
752 TPS(out_buff)[TPS(out_used)] = '\0';
754 T((T_RETURN("%s"), _nc_visbuf(TPS(out_buff))));
755 return (TPS(out_buff));
758 #if NCURSES_TPARM_VARARGS
759 #define tparm_varargs tparm
761 #define tparm_proto tparm
764 NCURSES_EXPORT(char *)
765 tparm_varargs(NCURSES_CONST char *string,...)
771 va_start(ap, string);
773 TPS(tname) = "tparm";
775 result = tparam_internal(string, ap);
780 #if !NCURSES_TPARM_VARARGS
781 NCURSES_EXPORT(char *)
782 tparm_proto(NCURSES_CONST char *string,
793 return tparm_varargs(string, a1, a2, a3, a4, a5, a6, a7, a8, a9);
795 #endif /* NCURSES_TPARM_VARARGS */