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>
45 MODULE_ID("$Id: lib_tparm.c,v 1.77 2008/11/16 00:19:59 juergen Exp $")
51 * Substitute the given parameters into the given string by the following
52 * rules (taken from terminfo(5)):
54 * Cursor addressing and other strings requiring parame-
55 * ters in the terminal are described by a parameterized string
56 * capability, with like escapes %x in it. For example, to
57 * address the cursor, the cup capability is given, using two
58 * parameters: the row and column to address to. (Rows and
59 * columns are numbered from zero and refer to the physical
60 * screen visible to the user, not to any unseen memory.) If
61 * the terminal has memory relative cursor addressing, that can
64 * The parameter mechanism uses a stack and special %
65 * codes to manipulate it. Typically a sequence will push one
66 * of the parameters onto the stack and then print it in some
67 * format. Often more complex operations are necessary.
69 * The % encodings have the following meanings:
72 * %c print pop() like %c in printf()
73 * %s print pop() like %s in printf()
74 * %[[:]flags][width[.precision]][doxXs]
75 * as in printf, flags are [-+#] and space
76 * The ':' is used to avoid making %+ or %-
77 * patterns (see below).
79 * %p[1-9] push ith parm
80 * %P[a-z] set dynamic variable [a-z] to pop()
81 * %g[a-z] get dynamic variable [a-z] and push it
82 * %P[A-Z] set static variable [A-Z] to pop()
83 * %g[A-Z] get static variable [A-Z] and push it
85 * %'c' push char constant c
86 * %{nn} push integer constant nn
89 * arithmetic (%m is mod): push(pop() op pop())
90 * %& %| %^ bit operations: push(pop() op pop())
91 * %= %> %< logical operations: push(pop() op pop())
92 * %A %O logical and & or operations for conditionals
93 * %! %~ unary operations push(op pop())
94 * %i add 1 to first two parms (for ANSI terminals)
96 * %? expr %t thenpart %e elsepart %;
97 * if-then-else, %e elsepart is optional.
98 * else-if's are possible ala Algol 68:
99 * %? c1 %t b1 %e c2 %t b2 %e c3 %t b3 %e c4 %t b4 %e b5 %;
101 * For those of the above operators which are binary and not commutative,
102 * the stack works in the usual way, with
104 * resulting in x mod y, not the reverse.
107 NCURSES_EXPORT_VAR(int) _nc_tparm_err = 0;
109 #define TPS(var) _nc_prescreen.tparm_state.var
115 if (TPS(out_buff) != 0) {
116 FreeAndNull(TPS(out_buff));
119 FreeAndNull(TPS(fmt_buff));
125 static NCURSES_INLINE void
126 get_space(size_t need)
128 need += TPS(out_used);
129 if (need > TPS(out_size)) {
130 TPS(out_size) = need * 2;
131 TPS(out_buff) = typeRealloc(char, TPS(out_size), TPS(out_buff));
132 if (TPS(out_buff) == 0)
133 _nc_err_abort(MSG_NO_MEMORY);
137 static NCURSES_INLINE void
138 save_text(const char *fmt, const char *s, int len)
140 size_t s_len = strlen(s);
141 if (len > (int) s_len)
144 get_space(s_len + 1);
146 (void) sprintf(TPS(out_buff) + TPS(out_used), fmt, s);
147 TPS(out_used) += strlen(TPS(out_buff) + TPS(out_used));
150 static NCURSES_INLINE void
151 save_number(const char *fmt, int number, int len)
154 len = 30; /* actually log10(MAX_INT)+1 */
156 get_space((unsigned) len + 1);
158 (void) sprintf(TPS(out_buff) + TPS(out_used), fmt, number);
159 TPS(out_used) += strlen(TPS(out_buff) + TPS(out_used));
162 static NCURSES_INLINE void
168 TPS(out_buff)[TPS(out_used)++] = (char) c;
171 static NCURSES_INLINE void
174 if (TPS(stack_ptr) < STACKSIZE) {
175 TPS(stack)[TPS(stack_ptr)].num_type = TRUE;
176 TPS(stack)[TPS(stack_ptr)].data.num = x;
179 DEBUG(2, ("npush: stack overflow: %s", _nc_visbuf(TPS(tparam_base))));
184 static NCURSES_INLINE int
188 if (TPS(stack_ptr) > 0) {
190 if (TPS(stack)[TPS(stack_ptr)].num_type)
191 result = TPS(stack)[TPS(stack_ptr)].data.num;
193 DEBUG(2, ("npop: stack underflow: %s", _nc_visbuf(TPS(tparam_base))));
199 static NCURSES_INLINE void
202 if (TPS(stack_ptr) < STACKSIZE) {
203 TPS(stack)[TPS(stack_ptr)].num_type = FALSE;
204 TPS(stack)[TPS(stack_ptr)].data.str = x;
207 DEBUG(2, ("spush: stack overflow: %s", _nc_visbuf(TPS(tparam_base))));
212 static NCURSES_INLINE char *
215 static char dummy[] = ""; /* avoid const-cast */
216 char *result = dummy;
217 if (TPS(stack_ptr) > 0) {
219 if (!TPS(stack)[TPS(stack_ptr)].num_type
220 && TPS(stack)[TPS(stack_ptr)].data.str != 0)
221 result = TPS(stack)[TPS(stack_ptr)].data.str;
223 DEBUG(2, ("spop: stack underflow: %s", _nc_visbuf(TPS(tparam_base))));
229 static NCURSES_INLINE const char *
230 parse_format(const char *s, char *format, int *len)
235 bool allowminus = FALSE;
245 while (*s != '\0' && !done) {
247 case 'c': /* FALLTHRU */
248 case 'd': /* FALLTHRU */
249 case 'o': /* FALLTHRU */
250 case 'x': /* FALLTHRU */
251 case 'X': /* FALLTHRU */
260 } else { /* value before '.' is the width */
284 if (isdigit(UChar(*s))) {
285 value = (value * 10) + (*s - '0');
296 * If we found an error, ignore (and remove) the flags.
299 my_width = my_prec = value = 0;
306 * Any value after '.' is the precision. If we did not see '.', then
307 * the value is the width.
315 /* return maximum string length in print */
316 *len = (my_width > my_prec) ? my_width : my_prec;
321 #define isUPPER(c) ((c) >= 'A' && (c) <= 'Z')
322 #define isLOWER(c) ((c) >= 'a' && (c) <= 'z')
325 * Analyze the string to see how many parameters we need from the varargs list,
326 * and what their types are. We will only accept string parameters if they
327 * appear as a %l or %s format following an explicit parameter reference (e.g.,
328 * %p2%s). All other parameters are numbers.
330 * 'number' counts coarsely the number of pop's we see in the string, and
331 * 'popcount' shows the highest parameter number in the string. We would like
332 * to simply use the latter count, but if we are reading termcap strings, there
333 * may be cases that we cannot see the explicit parameter numbers.
336 _nc_tparm_analyze(const char *string, char *p_is_s[NUM_PARM], int *popcount)
343 const char *cp = string;
344 static char dummy[] = "";
349 if ((len2 = strlen(cp)) > TPS(fmt_size)) {
350 TPS(fmt_size) = len2 + TPS(fmt_size) + 2;
351 TPS(fmt_buff) = typeRealloc(char, TPS(fmt_size), TPS(fmt_buff));
352 if (TPS(fmt_buff) == 0)
356 memset(p_is_s, 0, sizeof(p_is_s[0]) * NUM_PARM);
359 while ((cp - string) < (int) len2) {
362 cp = parse_format(cp, TPS(fmt_buff), &len);
367 case 'd': /* FALLTHRU */
368 case 'o': /* FALLTHRU */
369 case 'x': /* FALLTHRU */
370 case 'X': /* FALLTHRU */
371 case 'c': /* FALLTHRU */
380 p_is_s[lastpop - 1] = dummy;
386 i = (UChar(*cp) - '0');
387 if (i >= 0 && i <= NUM_PARM) {
389 if (lastpop > *popcount)
410 while (isdigit(UChar(*cp))) {
439 /* will add 1 to first (usually two) parameters */
447 if (number > NUM_PARM)
452 static NCURSES_INLINE char *
453 tparam_internal(const char *string, va_list ap)
455 char *p_is_s[NUM_PARM];
456 TPARM_ARG param[NUM_PARM];
463 const char *cp = string;
473 * Find the highest parameter-number referred to in the format string.
474 * Use this value to limit the number of arguments copied from the
475 * variable-length argument list.
477 number = _nc_tparm_analyze(cp, p_is_s, &popcount);
478 if (TPS(fmt_buff) == 0)
481 for (i = 0; i < max(popcount, number); i++) {
483 * A few caps (such as plab_norm) have string-valued parms.
484 * We'll have to assume that the caller knows the difference, since
485 * a char* and an int may not be the same size on the stack. The
486 * normal prototype for this uses 9 long's, which is consistent with
487 * our va_arg() usage.
489 if (p_is_s[i] != 0) {
490 p_is_s[i] = va_arg(ap, char *);
492 param[i] = va_arg(ap, TPARM_ARG);
497 * This is a termcap compatibility hack. If there are no explicit pop
498 * operations in the string, load the stack in such a way that
499 * successive pops will grab successive parameters. That will make
500 * the expansion of (for example) \E[%d;%dH work correctly in termcap
501 * style, which means tparam() will expand termcap strings OK.
506 for (i = number - 1; i >= 0; i--) {
514 if (USE_TRACEF(TRACE_CALLS)) {
515 for (i = 0; i < popcount; i++) {
517 save_text(", %s", _nc_visbuf(p_is_s[i]), 0);
519 save_number(", %d", param[i], 0);
521 _tracef(T_CALLED("%s(%s%s)"), TPS(tname), _nc_visbuf(cp), TPS(out_buff));
523 _nc_unlock_global(tracef);
527 while ((cp - string) < (int) len2) {
529 save_char(UChar(*cp));
531 TPS(tparam_base) = cp++;
532 cp = parse_format(cp, TPS(fmt_buff), &len);
540 case 'd': /* FALLTHRU */
541 case 'o': /* FALLTHRU */
542 case 'x': /* FALLTHRU */
543 case 'X': /* FALLTHRU */
544 save_number(TPS(fmt_buff), npop(), len);
547 case 'c': /* FALLTHRU */
552 save_number("%d", (int) strlen(spop()), 0);
556 save_text(TPS(fmt_buff), spop(), len);
561 i = (UChar(*cp) - '1');
562 if (i >= 0 && i < NUM_PARM) {
573 i = (UChar(*cp) - 'A');
574 TPS(static_vars)[i] = npop();
575 } else if (isLOWER(*cp)) {
576 i = (UChar(*cp) - 'a');
577 TPS(dynamic_var)[i] = npop();
584 i = (UChar(*cp) - 'A');
585 npush(TPS(static_vars)[i]);
586 } else if (isLOWER(*cp)) {
587 i = (UChar(*cp) - 'a');
588 npush(TPS(dynamic_var)[i]);
601 while (isdigit(UChar(*cp))) {
602 number = (number * 10) + (UChar(*cp) - '0');
609 npush(npop() + npop());
619 npush(npop() * npop());
625 npush(y ? (x / y) : 0);
631 npush(y ? (x % y) : 0);
635 npush(npop() && npop());
639 npush(npop() || npop());
643 npush(npop() & npop());
647 npush(npop() | npop());
651 npush(npop() ^ npop());
693 /* scan forward for %e or %; at level zero */
701 else if (*cp == ';') {
706 } else if (*cp == 'e' && level == 0)
717 /* scan forward for a %; at level zero */
725 else if (*cp == ';') {
741 } /* endswitch (*cp) */
742 } /* endelse (*cp == '%') */
748 } /* endwhile (*cp) */
751 TPS(out_buff)[TPS(out_used)] = '\0';
753 T((T_RETURN("%s"), _nc_visbuf(TPS(out_buff))));
754 return (TPS(out_buff));
757 #if NCURSES_TPARM_VARARGS
758 #define tparm_varargs tparm
760 #define tparm_proto tparm
763 NCURSES_EXPORT(char *)
764 tparm_varargs(NCURSES_CONST char *string,...)
770 va_start(ap, string);
772 TPS(tname) = "tparm";
774 result = tparam_internal(string, ap);
779 #if !NCURSES_TPARM_VARARGS
780 NCURSES_EXPORT(char *)
781 tparm_proto(NCURSES_CONST char *string,
792 return tparm_varargs(string, a1, a2, a3, a4, a5, a6, a7, a8, a9);
794 #endif /* NCURSES_TPARM_VARARGS */