1 /****************************************************************************
2 * Copyright (c) 1998-2003,2004 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.68 2004/02/07 20:52:51 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.
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;
132 static char *fmt_buff;
133 static size_t fmt_size;
140 FreeAndNull(out_buff);
143 FreeAndNull(fmt_buff);
150 get_space(size_t need)
153 if (need > out_size) {
155 out_buff = typeRealloc(char, out_size, out_buff);
157 _nc_err_abort(MSG_NO_MEMORY);
162 save_text(const char *fmt, const char *s, int len)
164 size_t s_len = strlen(s);
165 if (len > (int) s_len)
168 get_space(s_len + 1);
170 (void) sprintf(out_buff + out_used, fmt, s);
171 out_used += strlen(out_buff + out_used);
175 save_number(const char *fmt, int number, int len)
178 len = 30; /* actually log10(MAX_INT)+1 */
180 get_space((unsigned) len + 1);
182 (void) sprintf(out_buff + out_used, fmt, number);
183 out_used += strlen(out_buff + out_used);
192 out_buff[out_used++] = c;
198 if (stack_ptr < STACKSIZE) {
199 stack[stack_ptr].num_type = TRUE;
200 stack[stack_ptr].data.num = x;
203 DEBUG(2, ("npush: stack overflow: %s", _nc_visbuf(tparam_base)));
214 if (stack[stack_ptr].num_type)
215 result = stack[stack_ptr].data.num;
217 DEBUG(2, ("npop: stack underflow: %s", _nc_visbuf(tparam_base)));
226 if (stack_ptr < STACKSIZE) {
227 stack[stack_ptr].num_type = FALSE;
228 stack[stack_ptr].data.str = x;
231 DEBUG(2, ("spush: stack overflow: %s", _nc_visbuf(tparam_base)));
239 static char dummy[] = ""; /* avoid const-cast */
240 char *result = dummy;
243 if (!stack[stack_ptr].num_type && stack[stack_ptr].data.str != 0)
244 result = stack[stack_ptr].data.str;
246 DEBUG(2, ("spop: stack underflow: %s", _nc_visbuf(tparam_base)));
252 static inline const char *
253 parse_format(const char *s, char *format, int *len)
258 bool allowminus = FALSE;
268 while (*s != '\0' && !done) {
270 case 'c': /* FALLTHRU */
271 case 'd': /* FALLTHRU */
272 case 'o': /* FALLTHRU */
273 case 'x': /* FALLTHRU */
274 case 'X': /* FALLTHRU */
283 } else { /* value before '.' is the width */
307 if (isdigit(UChar(*s))) {
308 value = (value * 10) + (*s - '0');
319 * If we found an error, ignore (and remove) the flags.
322 my_width = my_prec = value = 0;
329 * Any value after '.' is the precision. If we did not see '.', then
330 * the value is the width.
338 /* return maximum string length in print */
339 *len = (my_width > my_prec) ? my_width : my_prec;
344 #define isUPPER(c) ((c) >= 'A' && (c) <= 'Z')
345 #define isLOWER(c) ((c) >= 'a' && (c) <= 'z')
348 * Analyze the string to see how many parameters we need from the varargs list,
349 * and what their types are. We will only accept string parameters if they
350 * appear as a %l or %s format following an explicit parameter reference (e.g.,
351 * %p2%s). All other parameters are numbers.
353 * 'number' counts coarsely the number of pop's we see in the string, and
354 * 'popcount' shows the highest parameter number in the string. We would like
355 * to simply use the latter count, but if we are reading termcap strings, there
356 * may be cases that we cannot see the explicit parameter numbers.
359 _nc_tparm_analyze(const char *string, char *p_is_s[NUM_PARM], int *popcount)
366 const char *cp = string;
367 static char dummy[] = "";
372 if ((len2 = strlen(cp)) > fmt_size) {
373 fmt_size = len2 + fmt_size + 2;
374 if ((fmt_buff = typeRealloc(char, fmt_size, fmt_buff)) == 0)
378 memset(p_is_s, 0, sizeof(p_is_s[0]) * NUM_PARM);
381 while ((cp - string) < (int) len2) {
384 cp = parse_format(cp, fmt_buff, &len);
389 case 'd': /* FALLTHRU */
390 case 'o': /* FALLTHRU */
391 case 'x': /* FALLTHRU */
392 case 'X': /* FALLTHRU */
393 case 'c': /* FALLTHRU */
402 p_is_s[lastpop - 1] = dummy;
408 i = (UChar(*cp) - '0');
409 if (i >= 0 && i <= NUM_PARM) {
411 if (lastpop > *popcount)
432 while (isdigit(UChar(*cp))) {
461 /* will add 1 to first (usually two) parameters */
469 if (number > NUM_PARM)
475 tparam_internal(const char *string, va_list ap)
478 char *p_is_s[NUM_PARM];
479 long param[NUM_PARM];
486 const char *cp = string;
488 static int dynamic_var[NUM_VARS];
489 static int static_vars[NUM_VARS];
498 * Find the highest parameter-number referred to in the format string.
499 * Use this value to limit the number of arguments copied from the
500 * variable-length argument list.
502 number = _nc_tparm_analyze(cp, p_is_s, &popcount);
506 for (i = 0; i < max(popcount, number); i++) {
508 * A few caps (such as plab_norm) have string-valued parms.
509 * We'll have to assume that the caller knows the difference, since
510 * a char* and an int may not be the same size on the stack. The
511 * normal prototype for this uses 9 long's, which is consistent with
512 * our va_arg() usage.
514 if (p_is_s[i] != 0) {
515 p_is_s[i] = va_arg(ap, char *);
517 param[i] = va_arg(ap, long int);
522 * This is a termcap compatibility hack. If there are no explicit pop
523 * operations in the string, load the stack in such a way that
524 * successive pops will grab successive parameters. That will make
525 * the expansion of (for example) \E[%d;%dH work correctly in termcap
526 * style, which means tparam() will expand termcap strings OK.
531 for (i = number - 1; i >= 0; i--)
535 if (_nc_tracing & TRACE_CALLS) {
536 for (i = 0; i < popcount; i++) {
538 save_text(", %s", _nc_visbuf(p_is_s[i]), 0);
540 save_number(", %d", param[i], 0);
542 _tracef(T_CALLED("%s(%s%s)"), tname, _nc_visbuf(cp), out_buff);
547 while ((cp - string) < (int) len2) {
549 save_char(UChar(*cp));
552 cp = parse_format(cp, fmt_buff, &len);
560 case 'd': /* FALLTHRU */
561 case 'o': /* FALLTHRU */
562 case 'x': /* FALLTHRU */
563 case 'X': /* FALLTHRU */
564 save_number(fmt_buff, npop(), len);
567 case 'c': /* FALLTHRU */
572 save_number("%d", (int) strlen(spop()), 0);
576 save_text(fmt_buff, spop(), len);
581 i = (UChar(*cp) - '1');
582 if (i >= 0 && i < NUM_PARM) {
593 i = (UChar(*cp) - 'A');
594 static_vars[i] = npop();
595 } else if (isLOWER(*cp)) {
596 i = (UChar(*cp) - 'a');
597 dynamic_var[i] = npop();
604 i = (UChar(*cp) - 'A');
605 npush(static_vars[i]);
606 } else if (isLOWER(*cp)) {
607 i = (UChar(*cp) - 'a');
608 npush(dynamic_var[i]);
621 while (isdigit(UChar(*cp))) {
622 number = (number * 10) + (UChar(*cp) - '0');
629 npush(npop() + npop());
639 npush(npop() * npop());
645 npush(y ? (x / y) : 0);
651 npush(y ? (x % y) : 0);
655 npush(npop() && npop());
659 npush(npop() || npop());
663 npush(npop() & npop());
667 npush(npop() | npop());
671 npush(npop() ^ npop());
713 /* scan forward for %e or %; at level zero */
721 else if (*cp == ';') {
726 } else if (*cp == 'e' && level == 0)
737 /* scan forward for a %; at level zero */
745 else if (*cp == ';') {
761 } /* endswitch (*cp) */
762 } /* endelse (*cp == '%') */
768 } /* endwhile (*cp) */
771 out_buff[out_used] = '\0';
773 T((T_RETURN("%s"), _nc_visbuf(out_buff)));
777 NCURSES_EXPORT(char *)
778 tparm(NCURSES_CONST char *string,...)
784 va_start(ap, string);
788 result = tparam_internal(string, ap);