1 /****************************************************************************
2 * Copyright (c) 1998,2000 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 ****************************************************************************/
39 #include <curses.priv.h>
45 MODULE_ID("$Id: lib_tparm.c,v 1.48 2000/10/14 17:45:00 Sergei.Ivanov 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.
117 static stack_frame stack[STACKSIZE];
118 static int stack_ptr;
121 static const char *tname;
124 static char *out_buff;
125 static size_t out_size;
126 static size_t out_used;
133 FreeAndNull(out_buff);
141 really_get_space(size_t need)
144 out_buff = typeRealloc(char, out_size, out_buff);
146 _nc_err_abort("Out of memory");
150 get_space(size_t need)
154 really_get_space(need);
158 save_text(const char *fmt, const char *s, int len)
160 size_t s_len = strlen(s);
161 if (len > (int) s_len)
164 get_space(s_len + 1);
166 (void) sprintf(out_buff + out_used, fmt, s);
167 out_used += strlen(out_buff + out_used);
171 save_number(const char *fmt, int number, int len)
174 len = 30; /* actually log10(MAX_INT)+1 */
178 (void) sprintf(out_buff + out_used, fmt, number);
179 out_used += strlen(out_buff + out_used);
188 out_buff[out_used++] = c;
194 if (stack_ptr < STACKSIZE) {
195 stack[stack_ptr].num_type = TRUE;
196 stack[stack_ptr].data.num = x;
207 if (stack[stack_ptr].num_type)
208 result = stack[stack_ptr].data.num;
216 if (stack_ptr < STACKSIZE) {
217 stack[stack_ptr].num_type = FALSE;
218 stack[stack_ptr].data.str = x;
226 static char dummy[] = ""; /* avoid const-cast */
227 char *result = dummy;
230 if (!stack[stack_ptr].num_type && stack[stack_ptr].data.str != 0)
231 result = stack[stack_ptr].data.str;
236 static inline const char *
237 parse_format(const char *s, char *format, int *len)
240 bool allowminus = FALSE;
250 while (*s != '\0' && !done) {
252 case 'c': /* FALLTHRU */
253 case 'd': /* FALLTHRU */
254 case 'o': /* FALLTHRU */
255 case 'x': /* FALLTHRU */
256 case 'X': /* FALLTHRU */
290 value = (value * 10) + (*s - '0');
301 * If we found an error, ignore (and remove) the flags.
304 prec = width = value = 0;
316 /* return maximum string length in print */
317 *len = (prec > width) ? prec : width;
321 #define isUPPER(c) ((c) >= 'A' && (c) <= 'Z')
322 #define isLOWER(c) ((c) >= 'a' && (c) <= 'z')
325 tparam_internal(const char *string, va_list ap)
338 register const char *cp;
339 static size_t len_fmt;
340 static char dummy[] = "";
342 static int dynamic_var[NUM_VARS];
343 static int static_vars[NUM_VARS];
349 if ((len2 = strlen(string)) > len_fmt) {
350 len_fmt = len2 + len_fmt + 2;
351 if ((format = typeRealloc(char, len_fmt, format)) == 0)
356 * Find the highest parameter-number referred to in the format string.
357 * Use this value to limit the number of arguments copied from the
358 * variable-length argument list.
364 memset(p_is_s, 0, sizeof(p_is_s));
367 * Analyze the string to see how many parameters we need from the varargs
368 * list, and what their types are. We will only accept string parameters
369 * if they appear as a %l or %s format following an explicit parameter
370 * reference (e.g., %p2%s). All other parameters are numbers.
372 * 'number' counts coarsely the number of pop's we see in the string, and
373 * 'popcount' shows the highest parameter number in the string. We would
374 * like to simply use the latter count, but if we are reading termcap
375 * strings, there may be cases that we cannot see the explicit parameter
378 for (cp = string; (cp - string) < (int) len2;) {
381 cp = parse_format(cp, format, &len);
386 case 'd': /* FALLTHRU */
387 case 'o': /* FALLTHRU */
388 case 'x': /* FALLTHRU */
389 case 'X': /* FALLTHRU */
390 case 'c': /* FALLTHRU */
398 p_is_s[lastpop - 1] = dummy;
405 if (i >= 0 && i <= 9) {
407 if (lastpop > popcount)
424 while (*cp >= '0' && *cp <= '9') {
461 for (i = 0; i < max(popcount, number); i++) {
463 * A few caps (such as plab_norm) have string-valued parms.
464 * We'll have to assume that the caller knows the difference, since
465 * a char* and an int may not be the same size on the stack.
467 if (p_is_s[i] != 0) {
468 p_is_s[i] = va_arg(ap, char *);
470 param[i] = va_arg(ap, int);
475 * This is a termcap compatibility hack. If there are no explicit pop
476 * operations in the string, load the stack in such a way that
477 * successive pops will grab successive parameters. That will make
478 * the expansion of (for example) \E[%d;%dH work correctly in termcap
479 * style, which means tparam() will expand termcap strings OK.
484 for (i = number - 1; i >= 0; i--)
488 if (_nc_tracing & TRACE_CALLS) {
489 for (i = 0; i < popcount; i++) {
491 save_text(", %s", _nc_visbuf(p_is_s[i]), 0);
493 save_number(", %d", param[i], 0);
495 _tracef(T_CALLED("%s(%s%s)"), tname, _nc_visbuf(string), out_buff);
501 if (*string != '%') {
505 string = parse_format(string, format, &len);
513 case 'd': /* FALLTHRU */
514 case 'o': /* FALLTHRU */
515 case 'x': /* FALLTHRU */
516 case 'X': /* FALLTHRU */
517 case 'c': /* FALLTHRU */
518 save_number(format, npop(), len);
522 save_number("%d", strlen(spop()), 0);
526 save_text(format, spop(), len);
532 if (i >= 0 && i < 9) {
542 if (isUPPER(*string)) {
544 static_vars[i] = npop();
545 } else if (isLOWER(*string)) {
547 dynamic_var[i] = npop();
553 if (isUPPER(*string)) {
555 npush(static_vars[i]);
556 } else if (isLOWER(*string)) {
558 npush(dynamic_var[i]);
571 while (*string >= '0' && *string <= '9') {
572 number = number * 10 + *string - '0';
579 npush(npop() + npop());
589 npush(npop() * npop());
595 npush(y ? (x / y) : 0);
601 npush(y ? (x % y) : 0);
605 npush(npop() && npop());
609 npush(npop() || npop());
613 npush(npop() & npop());
617 npush(npop() | npop());
621 npush(npop() ^ npop());
663 /* scan forward for %e or %; at level zero */
667 if (*string == '%') {
671 else if (*string == ';') {
676 } else if (*string == 'e' && level == 0)
687 /* scan forward for a %; at level zero */
691 if (*string == '%') {
695 else if (*string == ';') {
711 } /* endswitch (*string) */
712 } /* endelse (*string == '%') */
718 } /* endwhile (*string) */
721 out_buff[out_used] = '\0';
723 T((T_RETURN("%s"), _nc_visbuf(out_buff)));
728 tparm(NCURSES_CONST char *string,...)
733 va_start(ap, string);
737 result = tparam_internal(string, ap);