1 /****************************************************************************
2 * Copyright 2018-2020,2021 Thomas E. Dickey *
3 * Copyright 1998-2016,2017 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 /****************************************************************************
31 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
32 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
33 * and: Thomas E. Dickey, 1996 on *
34 ****************************************************************************/
41 #define entry _ncu_entry
42 #define ENTRY _ncu_ENTRY
44 #include <curses.priv.h>
56 MODULE_ID("$Id: lib_tparm.c,v 1.131 2021/04/03 22:05:59 tom Exp $")
62 * Substitute the given parameters into the given string by the following
63 * rules (taken from terminfo(5)):
65 * Cursor addressing and other strings requiring parame-
66 * ters in the terminal are described by a parameterized string
67 * capability, with escapes like %x in it. For example, to
68 * address the cursor, the cup capability is given, using two
69 * parameters: the row and column to address to. (Rows and
70 * columns are numbered from zero and refer to the physical
71 * screen visible to the user, not to any unseen memory.) If
72 * the terminal has memory relative cursor addressing, that can
75 * The parameter mechanism uses a stack and special %
76 * codes to manipulate it. Typically a sequence will push one
77 * of the parameters onto the stack and then print it in some
78 * format. Often more complex operations are necessary.
80 * The % encodings have the following meanings:
83 * %c print pop() like %c in printf()
84 * %s print pop() like %s in printf()
85 * %[[:]flags][width[.precision]][doxXs]
86 * as in printf, flags are [-+#] and space
87 * The ':' is used to avoid making %+ or %-
88 * patterns (see below).
90 * %p[1-9] push ith parm
91 * %P[a-z] set dynamic variable [a-z] to pop()
92 * %g[a-z] get dynamic variable [a-z] and push it
93 * %P[A-Z] set static variable [A-Z] to pop()
94 * %g[A-Z] get static variable [A-Z] and push it
96 * %'c' push char constant c
97 * %{nn} push integer constant nn
100 * arithmetic (%m is mod): push(pop() op pop())
101 * %& %| %^ bit operations: push(pop() op pop())
102 * %= %> %< logical operations: push(pop() op pop())
103 * %A %O logical and & or operations for conditionals
104 * %! %~ unary operations push(op pop())
105 * %i add 1 to first two parms (for ANSI terminals)
107 * %? expr %t thenpart %e elsepart %;
108 * if-then-else, %e elsepart is optional.
109 * else-if's are possible ala Algol 68:
110 * %? c1 %t b1 %e c2 %t b2 %e c3 %t b3 %e c4 %t b4 %e b5 %;
112 * For those of the above operators which are binary and not commutative,
113 * the stack works in the usual way, with
115 * resulting in x mod y, not the reverse.
118 NCURSES_EXPORT_VAR(int) _nc_tparm_err = 0;
120 #define TPS(var) _nc_prescreen.tparm_state.var
121 #define popcount _nc_popcount /* workaround for NetBSD 6.0 defect */
123 #define isUPPER(c) ((c) >= 'A' && (c) <= 'Z')
124 #define isLOWER(c) ((c) >= 'a' && (c) <= 'z')
125 #define tc_BUMP() if (level < 0 && number < 2) number++
128 const char *format; /* format-string can be used as cache-key */
129 int tparm_type; /* bit-set for each string-parameter */
133 TPARM_ARG param[NUM_PARM];
134 char *p_is_s[NUM_PARM];
138 #define MyCache _nc_globals.cached_tparm
139 #define MyCount _nc_globals.count_tparm
141 static int which_tparm;
142 static TPARM_DATA **delete_tparm;
144 #endif /* HAVE_TSEARCH */
146 static char dummy[] = ""; /* avoid const-cast */
150 cmp_format(const void *p, const void *q)
152 const char *a = *(char *const *) p;
153 const char *b = *(char *const *) q;
161 visit_nodes(const void *nodep, const VISIT which, const int depth)
164 if (which == preorder || which == leaf) {
165 delete_tparm[which_tparm] = *(TPARM_DATA **) nodep;
176 delete_tparm = typeCalloc(TPARM_DATA *, MyCount);
178 twalk(MyCache, visit_nodes);
179 for (which_tparm = 0; which_tparm < MyCount; ++which_tparm) {
180 TPARM_DATA *ptr = delete_tparm[which_tparm];
182 tdelete(ptr, &MyCache, cmp_format);
183 free((char *) ptr->format);
188 twalk(MyCache, visit_nodes);
189 FreeAndNull(delete_tparm);
194 FreeAndNull(TPS(out_buff));
198 FreeAndNull(TPS(fmt_buff));
203 static NCURSES_INLINE void
204 get_space(size_t need)
206 need += TPS(out_used);
207 if (need > TPS(out_size)) {
208 TPS(out_size) = need * 2;
209 TYPE_REALLOC(char, TPS(out_size), TPS(out_buff));
213 static NCURSES_INLINE void
214 save_text(const char *fmt, const char *s, int len)
216 size_t s_len = (size_t) len + strlen(s) + strlen(fmt);
217 get_space(s_len + 1);
219 _nc_SPRINTF(TPS(out_buff) + TPS(out_used),
220 _nc_SLIMIT(TPS(out_size) - TPS(out_used))
222 TPS(out_used) += strlen(TPS(out_buff) + TPS(out_used));
225 static NCURSES_INLINE void
226 save_number(const char *fmt, int number, int len)
228 size_t s_len = (size_t) len + 30 + strlen(fmt);
229 get_space(s_len + 1);
231 _nc_SPRINTF(TPS(out_buff) + TPS(out_used),
232 _nc_SLIMIT(TPS(out_size) - TPS(out_used))
234 TPS(out_used) += strlen(TPS(out_buff) + TPS(out_used));
237 static NCURSES_INLINE void
242 get_space((size_t) 1);
243 TPS(out_buff)[TPS(out_used)++] = (char) c;
246 static NCURSES_INLINE void
249 if (TPS(stack_ptr) < STACKSIZE) {
250 TPS(stack)[TPS(stack_ptr)].num_type = TRUE;
251 TPS(stack)[TPS(stack_ptr)].data.num = x;
254 DEBUG(2, ("npush: stack overflow: %s", _nc_visbuf(TPS(tparam_base))));
259 static NCURSES_INLINE int
263 if (TPS(stack_ptr) > 0) {
265 if (TPS(stack)[TPS(stack_ptr)].num_type)
266 result = TPS(stack)[TPS(stack_ptr)].data.num;
268 DEBUG(2, ("npop: stack underflow: %s", _nc_visbuf(TPS(tparam_base))));
274 static NCURSES_INLINE void
277 if (TPS(stack_ptr) < STACKSIZE) {
278 TPS(stack)[TPS(stack_ptr)].num_type = FALSE;
279 TPS(stack)[TPS(stack_ptr)].data.str = x;
282 DEBUG(2, ("spush: stack overflow: %s", _nc_visbuf(TPS(tparam_base))));
287 static NCURSES_INLINE char *
290 char *result = dummy;
291 if (TPS(stack_ptr) > 0) {
293 if (!TPS(stack)[TPS(stack_ptr)].num_type
294 && TPS(stack)[TPS(stack_ptr)].data.str != 0)
295 result = TPS(stack)[TPS(stack_ptr)].data.str;
297 DEBUG(2, ("spop: stack underflow: %s", _nc_visbuf(TPS(tparam_base))));
303 static NCURSES_INLINE const char *
304 parse_format(const char *s, char *format, int *len)
309 bool allowminus = FALSE;
319 while (*s != '\0' && !done) {
321 case 'c': /* FALLTHRU */
322 case 'd': /* FALLTHRU */
323 case 'o': /* FALLTHRU */
324 case 'x': /* FALLTHRU */
325 case 'X': /* FALLTHRU */
327 #ifdef EXP_XTERM_1005
337 } else { /* value before '.' is the width */
361 if (isdigit(UChar(*s))) {
362 value = (value * 10) + (*s - '0');
373 * If we found an error, ignore (and remove) the flags.
376 my_width = my_prec = value = 0;
383 * Any value after '.' is the precision. If we did not see '.', then
384 * the value is the width.
392 /* return maximum string length in print */
393 *len = (my_width > my_prec) ? my_width : my_prec;
399 * Analyze the string to see how many parameters we need from the varargs list,
400 * and what their types are. We will only accept string parameters if they
401 * appear as a %l or %s format following an explicit parameter reference (e.g.,
402 * %p2%s). All other parameters are numbers.
404 * 'number' counts coarsely the number of pop's we see in the string, and
405 * 'popcount' shows the highest parameter number in the string. We would like
406 * to simply use the latter count, but if we are reading termcap strings, there
407 * may be cases that we cannot see the explicit parameter numbers.
410 _nc_tparm_analyze(const char *string, char **p_is_s, int *popcount)
418 const char *cp = string;
423 if ((len2 = strlen(cp)) + 2 > TPS(fmt_size)) {
424 TPS(fmt_size) += len2 + 2;
425 TPS(fmt_buff) = typeRealloc(char, TPS(fmt_size), TPS(fmt_buff));
426 if (TPS(fmt_buff) == 0)
430 memset(p_is_s, 0, sizeof(p_is_s[0]) * NUM_PARM);
433 while ((cp - string) < (int) len2) {
436 cp = parse_format(cp, TPS(fmt_buff), &len);
441 case 'd': /* FALLTHRU */
442 case 'o': /* FALLTHRU */
443 case 'x': /* FALLTHRU */
444 case 'X': /* FALLTHRU */
445 case 'c': /* FALLTHRU */
446 #ifdef EXP_XTERM_1005
460 p_is_s[lastpop - 1] = dummy;
467 i = (UChar(*cp) - '0');
468 if (i >= 0 && i <= NUM_PARM) {
471 if (lastpop > *popcount)
494 while (isdigit(UChar(*cp))) {
513 level -= 1; /* pop 2, operate, push 1 */
524 /* will add 1 to first (usually two) parameters */
532 if (number > NUM_PARM)
538 * Analyze the capability string, finding the number of parameters and their
541 * TODO: cache the result so that this is done once per capability per term.
544 tparm_setup(const char *string, TPARM_DATA * result)
549 memset(result, 0, sizeof(*result));
551 if (string == NULL) {
552 TR(TRACE_CALLS, ("%s: format is null", TPS(tname)));
559 result->format = string;
560 if ((ft = tfind(result, &MyCache, cmp_format)) != 0) {
561 fs = *(TPARM_DATA **) ft;
567 * Find the highest parameter-number referred to in the format
568 * string. Use this value to limit the number of arguments copied
569 * from the variable-length argument list.
571 result->num_parsed = _nc_tparm_analyze(string,
573 &(result->num_popped));
574 if (TPS(fmt_buff) == 0) {
575 TR(TRACE_CALLS, ("%s: error in analysis", TPS(tname)));
580 if (result->num_parsed > NUM_PARM)
581 result->num_parsed = NUM_PARM;
582 if (result->num_popped > NUM_PARM)
583 result->num_popped = NUM_PARM;
584 result->num_actual = max(result->num_popped, result->num_parsed);
586 for (n = 0; n < result->num_actual; ++n) {
587 if (result->p_is_s[n])
588 result->tparm_type |= (1 << n);
591 if ((fs = typeCalloc(TPARM_DATA, 1)) != 0) {
593 if ((fs->format = strdup(string)) != 0) {
594 if (tsearch(fs, &MyCache, cmp_format) != 0) {
616 * A few caps (such as plab_norm) have string-valued parms. We'll have to
617 * assume that the caller knows the difference, since a char* and an int may
618 * not be the same size on the stack. The normal prototype for tparm uses 9
619 * long's, which is consistent with our va_arg() usage.
622 tparm_copy_valist(TPARM_DATA * data, int use_TPARM_ARG, va_list ap)
626 for (i = 0; i < data->num_actual; i++) {
627 if (data->p_is_s[i] != 0) {
628 char *value = va_arg(ap, char *);
631 data->p_is_s[i] = value;
633 } else if (use_TPARM_ARG) {
634 data->param[i] = va_arg(ap, TPARM_ARG);
636 data->param[i] = (TPARM_ARG) va_arg(ap, int);
642 * This is a termcap compatibility hack. If there are no explicit pop
643 * operations in the string, load the stack in such a way that successive pops
644 * will grab successive parameters. That will make the expansion of (for
645 * example) \E[%d;%dH work correctly in termcap style, which means tparam()
646 * will expand termcap strings OK.
649 tparm_tc_compat(TPARM_DATA * data)
651 bool termcap_hack = FALSE;
655 if (data->num_popped == 0) {
659 for (i = data->num_parsed - 1; i >= 0; i--) {
661 spush(data->p_is_s[i]);
663 npush((int) data->param[i]);
671 tparm_trace_call(const char *string, TPARM_DATA * data)
673 if (USE_TRACEF(TRACE_CALLS)) {
675 for (i = 0; i < data->num_actual; i++) {
676 if (data->p_is_s[i] != 0) {
677 save_text(", %s", _nc_visbuf(data->p_is_s[i]), 0);
678 } else if ((long) data->param[i] > MAX_OF_TYPE(NCURSES_INT2) ||
679 (long) data->param[i] < 0) {
680 _tracef("BUG: problem with tparm parameter #%d of %d",
681 i + 1, data->num_actual);
684 save_number(", %d", (int) data->param[i], 0);
687 _tracef(T_CALLED("%s(%s%s)"), TPS(tname), _nc_visbuf(string), TPS(out_buff));
689 _nc_unlock_global(tracef);
694 #define tparm_trace_call(string, data) /* nothing */
697 static NCURSES_INLINE char *
698 tparam_internal(const char *string, TPARM_DATA * data)
705 const char *cp = string;
706 size_t len2 = strlen(cp);
707 bool incremented_two = FALSE;
708 bool termcap_hack = tparm_tc_compat(data);
710 tparm_trace_call(string, data);
712 while ((cp - string) < (int) len2) {
714 save_char(UChar(*cp));
716 TPS(tparam_base) = cp++;
717 cp = parse_format(cp, TPS(fmt_buff), &len);
725 case 'd': /* FALLTHRU */
726 case 'o': /* FALLTHRU */
727 case 'x': /* FALLTHRU */
728 case 'X': /* FALLTHRU */
729 save_number(TPS(fmt_buff), npop(), len);
732 case 'c': /* FALLTHRU */
736 #ifdef EXP_XTERM_1005
739 unsigned char target[10];
740 unsigned source = (unsigned) npop();
741 int rc = _nc_conv_to_utf8(target, source, (unsigned)
744 for (n = 0; n < rc; ++n) {
745 save_char(target[n]);
751 npush((int) strlen(spop()));
755 save_text(TPS(fmt_buff), spop(), len);
760 i = (UChar(*cp) - '1');
761 if (i >= 0 && i < NUM_PARM) {
762 if (data->p_is_s[i]) {
763 spush(data->p_is_s[i]);
765 npush((int) data->param[i]);
773 i = (UChar(*cp) - 'A');
774 TPS(static_vars)[i] = npop();
775 } else if (isLOWER(*cp)) {
776 i = (UChar(*cp) - 'a');
777 TPS(dynamic_var)[i] = npop();
784 i = (UChar(*cp) - 'A');
785 npush(TPS(static_vars)[i]);
786 } else if (isLOWER(*cp)) {
787 i = (UChar(*cp) - 'a');
788 npush(TPS(dynamic_var)[i]);
801 while (isdigit(UChar(*cp))) {
802 number = (number * 10) + (UChar(*cp) - '0');
809 npush(npop() + npop());
819 npush(npop() * npop());
825 npush(y ? (x / y) : 0);
831 npush(y ? (x % y) : 0);
847 npush(npop() & npop());
851 npush(npop() | npop());
855 npush(npop() ^ npop());
886 * Increment the first two parameters -- if they are numbers
887 * rather than strings. As a side effect, assign into the
888 * stack; if this is termcap, then the stack was populated
889 * using the termcap hack above rather than via the terminfo
892 if (!incremented_two) {
893 incremented_two = TRUE;
894 if (data->p_is_s[0] == 0) {
897 TPS(stack)[0].data.num = (int) data->param[0];
899 if (data->p_is_s[1] == 0) {
902 TPS(stack)[1].data.num = (int) data->param[1];
913 /* scan forward for %e or %; at level zero */
921 else if (*cp == ';') {
926 } else if (*cp == 'e' && level == 0)
937 /* scan forward for a %; at level zero */
945 else if (*cp == ';') {
961 } /* endswitch (*cp) */
962 } /* endelse (*cp == '%') */
968 } /* endwhile (*cp) */
970 get_space((size_t) 1);
971 TPS(out_buff)[TPS(out_used)] = '\0';
973 if (TPS(stack_ptr) && !_nc_tparm_err) {
974 DEBUG(2, ("tparm: stack has %d item%s on return",
976 TPS(stack_ptr) == 1 ? "" : "s"));
980 T((T_RETURN("%s"), _nc_visbuf(TPS(out_buff))));
981 return (TPS(out_buff));
984 #if NCURSES_TPARM_VARARGS
986 NCURSES_EXPORT(char *)
987 tparm(const char *string, ...)
994 TPS(tname) = "tparm";
997 if (tparm_setup(string, &myData) == OK) {
1000 va_start(ap, string);
1001 tparm_copy_valist(&myData, TRUE, ap);
1004 result = tparam_internal(string, &myData);
1009 #else /* !NCURSES_TPARM_VARARGS */
1011 NCURSES_EXPORT(char *)
1012 tparm(const char *string,
1024 char *result = NULL;
1028 TPS(tname) = "tparm";
1031 if (tparm_setup(string, &myData) == OK) {
1033 myData.param[0] = a1;
1034 myData.param[1] = a2;
1035 myData.param[2] = a3;
1036 myData.param[3] = a4;
1037 myData.param[4] = a5;
1038 myData.param[5] = a6;
1039 myData.param[6] = a7;
1040 myData.param[7] = a8;
1041 myData.param[8] = a9;
1043 result = tparam_internal(string, &myData);
1048 #endif /* NCURSES_TPARM_VARARGS */
1050 NCURSES_EXPORT(char *)
1051 tiparm(const char *string, ...)
1054 char *result = NULL;
1058 TPS(tname) = "tiparm";
1061 if (tparm_setup(string, &myData) == OK) {
1064 va_start(ap, string);
1065 tparm_copy_valist(&myData, FALSE, ap);
1068 result = tparam_internal(string, &myData);
1074 * The internal-use flavor ensures that the parameters are numbers, not strings
1076 NCURSES_EXPORT(char *)
1077 _nc_tiparm(int expected, const char *string, ...)
1080 char *result = NULL;
1084 TPS(tname) = "_nc_tiparm";
1087 if (tparm_setup(string, &myData) == OK
1088 && myData.num_actual <= expected
1089 && myData.tparm_type == 0) {
1092 va_start(ap, string);
1093 tparm_copy_valist(&myData, FALSE, ap);
1096 result = tparam_internal(string, &myData);