]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/lib_tparm.c
ncurses 6.2 - patch 20210320
[ncurses.git] / ncurses / tinfo / lib_tparm.c
1 /****************************************************************************
2  * Copyright 2018-2020,2021 Thomas E. Dickey                                *
3  * Copyright 1998-2016,2017 Free Software Foundation, Inc.                  *
4  *                                                                          *
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:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
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.                               *
23  *                                                                          *
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       *
27  * authorization.                                                           *
28  ****************************************************************************/
29
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  ****************************************************************************/
35
36 /*
37  *      tparm.c
38  *
39  */
40
41 #define entry _ncu_entry
42 #define ENTRY _ncu_ENTRY
43
44 #include <curses.priv.h>
45
46 #undef entry
47 #undef ENTRY
48
49 #if HAVE_TSEARCH
50 #include <search.h>
51 #endif
52
53 #include <ctype.h>
54 #include <tic.h>
55
56 MODULE_ID("$Id: lib_tparm.c,v 1.130 2021/03/20 12:36:34 tom Exp $")
57
58 /*
59  *      char *
60  *      tparm(string, ...)
61  *
62  *      Substitute the given parameters into the given string by the following
63  *      rules (taken from terminfo(5)):
64  *
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
73  *      be indicated by
74  *
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.
79  *
80  *           The % encodings have the following meanings:
81  *
82  *           %%        outputs `%'
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).
89  *
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
95  *           %l        push strlen(pop)
96  *           %'c'      push char constant c
97  *           %{nn}     push integer constant nn
98  *
99  *           %+ %- %* %/ %m
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)
106  *
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 %;
111  *
112  *      For those of the above operators which are binary and not commutative,
113  *      the stack works in the usual way, with
114  *                      %gx %gy %m
115  *      resulting in x mod y, not the reverse.
116  */
117
118 NCURSES_EXPORT_VAR(int) _nc_tparm_err = 0;
119
120 #define TPS(var) _nc_prescreen.tparm_state.var
121 #define popcount _nc_popcount   /* workaround for NetBSD 6.0 defect */
122
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++
126
127 typedef struct {
128     const char *format;         /* format-string can be used as cache-key */
129     int tparm_type;             /* bit-set for each string-parameter */
130     int num_actual;
131     int num_parsed;
132     int num_popped;
133     TPARM_ARG param[NUM_PARM];
134     char *p_is_s[NUM_PARM];
135 } TPARM_DATA;
136
137 #if HAVE_TSEARCH
138 #define MyCache _nc_globals.cached_tparm
139 #define MyCount _nc_globals.count_tparm
140 #if NO_LEAKS
141 static int which_tparm;
142 static TPARM_DATA **delete_tparm;
143 #endif
144 #endif /* HAVE_TSEARCH */
145
146 static char dummy[] = "";       /* avoid const-cast */
147
148 #if HAVE_TSEARCH
149 static int
150 cmp_format(const void *p, const void *q)
151 {
152     const char *a = *(char *const *) p;
153     const char *b = *(char *const *) q;
154     return strcmp(a, b);
155 }
156 #endif
157
158 #if NO_LEAKS
159 #if HAVE_TSEARCH
160 static void
161 visit_nodes(const void *nodep, const VISIT which, const int depth)
162 {
163     (void) depth;
164     if (which == preorder || which == leaf) {
165         delete_tparm[which_tparm] = *(TPARM_DATA **) nodep;
166         which_tparm++;
167     }
168 }
169 #endif
170
171 NCURSES_EXPORT(void)
172 _nc_free_tparm(void)
173 {
174 #if HAVE_TSEARCH
175     if (MyCount != 0) {
176         delete_tparm = typeCalloc(TPARM_DATA *, MyCount);
177         which_tparm = 0;
178         twalk(MyCache, visit_nodes);
179         for (which_tparm = 0; which_tparm < MyCount; ++which_tparm) {
180             TPARM_DATA *ptr = delete_tparm[which_tparm];
181             if (ptr != NULL) {
182                 tdelete(ptr, &MyCache, cmp_format);
183                 free((char *) ptr->format);
184                 free(ptr);
185             }
186         }
187         which_tparm = 0;
188         twalk(MyCache, visit_nodes);
189         FreeAndNull(delete_tparm);
190         MyCount = 0;
191         which_tparm = 0;
192     }
193 #endif
194     FreeAndNull(TPS(out_buff));
195     TPS(out_size) = 0;
196     TPS(out_used) = 0;
197
198     FreeAndNull(TPS(fmt_buff));
199     TPS(fmt_size) = 0;
200 }
201 #endif
202
203 static NCURSES_INLINE void
204 get_space(size_t need)
205 {
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));
210     }
211 }
212
213 static NCURSES_INLINE void
214 save_text(const char *fmt, const char *s, int len)
215 {
216     size_t s_len = (size_t) len + strlen(s) + strlen(fmt);
217     get_space(s_len + 1);
218
219     _nc_SPRINTF(TPS(out_buff) + TPS(out_used),
220                 _nc_SLIMIT(TPS(out_size) - TPS(out_used))
221                 fmt, s);
222     TPS(out_used) += strlen(TPS(out_buff) + TPS(out_used));
223 }
224
225 static NCURSES_INLINE void
226 save_number(const char *fmt, int number, int len)
227 {
228     size_t s_len = (size_t) len + 30 + strlen(fmt);
229     get_space(s_len + 1);
230
231     _nc_SPRINTF(TPS(out_buff) + TPS(out_used),
232                 _nc_SLIMIT(TPS(out_size) - TPS(out_used))
233                 fmt, number);
234     TPS(out_used) += strlen(TPS(out_buff) + TPS(out_used));
235 }
236
237 static NCURSES_INLINE void
238 save_char(int c)
239 {
240     if (c == 0)
241         c = 0200;
242     get_space((size_t) 1);
243     TPS(out_buff)[TPS(out_used)++] = (char) c;
244 }
245
246 static NCURSES_INLINE void
247 npush(int x)
248 {
249     if (TPS(stack_ptr) < STACKSIZE) {
250         TPS(stack)[TPS(stack_ptr)].num_type = TRUE;
251         TPS(stack)[TPS(stack_ptr)].data.num = x;
252         TPS(stack_ptr)++;
253     } else {
254         DEBUG(2, ("npush: stack overflow: %s", _nc_visbuf(TPS(tparam_base))));
255         _nc_tparm_err++;
256     }
257 }
258
259 static NCURSES_INLINE int
260 npop(void)
261 {
262     int result = 0;
263     if (TPS(stack_ptr) > 0) {
264         TPS(stack_ptr)--;
265         if (TPS(stack)[TPS(stack_ptr)].num_type)
266             result = TPS(stack)[TPS(stack_ptr)].data.num;
267     } else {
268         DEBUG(2, ("npop: stack underflow: %s", _nc_visbuf(TPS(tparam_base))));
269         _nc_tparm_err++;
270     }
271     return result;
272 }
273
274 static NCURSES_INLINE void
275 spush(char *x)
276 {
277     if (TPS(stack_ptr) < STACKSIZE) {
278         TPS(stack)[TPS(stack_ptr)].num_type = FALSE;
279         TPS(stack)[TPS(stack_ptr)].data.str = x;
280         TPS(stack_ptr)++;
281     } else {
282         DEBUG(2, ("spush: stack overflow: %s", _nc_visbuf(TPS(tparam_base))));
283         _nc_tparm_err++;
284     }
285 }
286
287 static NCURSES_INLINE char *
288 spop(void)
289 {
290     char *result = dummy;
291     if (TPS(stack_ptr) > 0) {
292         TPS(stack_ptr)--;
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;
296     } else {
297         DEBUG(2, ("spop: stack underflow: %s", _nc_visbuf(TPS(tparam_base))));
298         _nc_tparm_err++;
299     }
300     return result;
301 }
302
303 static NCURSES_INLINE const char *
304 parse_format(const char *s, char *format, int *len)
305 {
306     *len = 0;
307     if (format != 0) {
308         bool done = FALSE;
309         bool allowminus = FALSE;
310         bool dot = FALSE;
311         bool err = FALSE;
312         char *fmt = format;
313         int my_width = 0;
314         int my_prec = 0;
315         int value = 0;
316
317         *len = 0;
318         *format++ = '%';
319         while (*s != '\0' && !done) {
320             switch (*s) {
321             case 'c':           /* FALLTHRU */
322             case 'd':           /* FALLTHRU */
323             case 'o':           /* FALLTHRU */
324             case 'x':           /* FALLTHRU */
325             case 'X':           /* FALLTHRU */
326             case 's':
327 #ifdef EXP_XTERM_1005
328             case 'u':
329 #endif
330                 *format++ = *s;
331                 done = TRUE;
332                 break;
333             case '.':
334                 *format++ = *s++;
335                 if (dot) {
336                     err = TRUE;
337                 } else {        /* value before '.' is the width */
338                     dot = TRUE;
339                     my_width = value;
340                 }
341                 value = 0;
342                 break;
343             case '#':
344                 *format++ = *s++;
345                 break;
346             case ' ':
347                 *format++ = *s++;
348                 break;
349             case ':':
350                 s++;
351                 allowminus = TRUE;
352                 break;
353             case '-':
354                 if (allowminus) {
355                     *format++ = *s++;
356                 } else {
357                     done = TRUE;
358                 }
359                 break;
360             default:
361                 if (isdigit(UChar(*s))) {
362                     value = (value * 10) + (*s - '0');
363                     if (value > 10000)
364                         err = TRUE;
365                     *format++ = *s++;
366                 } else {
367                     done = TRUE;
368                 }
369             }
370         }
371
372         /*
373          * If we found an error, ignore (and remove) the flags.
374          */
375         if (err) {
376             my_width = my_prec = value = 0;
377             format = fmt;
378             *format++ = '%';
379             *format++ = *s;
380         }
381
382         /*
383          * Any value after '.' is the precision.  If we did not see '.', then
384          * the value is the width.
385          */
386         if (dot)
387             my_prec = value;
388         else
389             my_width = value;
390
391         *format = '\0';
392         /* return maximum string length in print */
393         *len = (my_width > my_prec) ? my_width : my_prec;
394     }
395     return s;
396 }
397
398 /*
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.
403  *
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.
408  */
409 NCURSES_EXPORT(int)
410 _nc_tparm_analyze(const char *string, char **p_is_s, int *popcount)
411 {
412     size_t len2;
413     int i;
414     int lastpop = -1;
415     int len;
416     int number = 0;
417     int level = -1;
418     const char *cp = string;
419
420     if (cp == 0)
421         return 0;
422
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)
427             return 0;
428     }
429
430     memset(p_is_s, 0, sizeof(p_is_s[0]) * NUM_PARM);
431     *popcount = 0;
432
433     while ((cp - string) < (int) len2) {
434         if (*cp == '%') {
435             cp++;
436             cp = parse_format(cp, TPS(fmt_buff), &len);
437             switch (*cp) {
438             default:
439                 break;
440
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
447             case 'u':
448 #endif
449                 if (lastpop <= 0) {
450                     tc_BUMP();
451                 }
452                 level -= 1;
453                 lastpop = -1;
454                 break;
455
456             case 'l':
457             case 's':
458                 if (lastpop > 0) {
459                     level -= 1;
460                     p_is_s[lastpop - 1] = dummy;
461                 }
462                 tc_BUMP();
463                 break;
464
465             case 'p':
466                 cp++;
467                 i = (UChar(*cp) - '0');
468                 if (i >= 0 && i <= NUM_PARM) {
469                     ++level;
470                     lastpop = i;
471                     if (lastpop > *popcount)
472                         *popcount = lastpop;
473                 }
474                 break;
475
476             case 'P':
477                 ++cp;
478                 break;
479
480             case 'g':
481                 ++level;
482                 cp++;
483                 break;
484
485             case S_QUOTE:
486                 ++level;
487                 cp += 2;
488                 lastpop = -1;
489                 break;
490
491             case L_BRACE:
492                 ++level;
493                 cp++;
494                 while (isdigit(UChar(*cp))) {
495                     cp++;
496                 }
497                 break;
498
499             case '+':
500             case '-':
501             case '*':
502             case '/':
503             case 'm':
504             case 'A':
505             case 'O':
506             case '&':
507             case '|':
508             case '^':
509             case '=':
510             case '<':
511             case '>':
512                 tc_BUMP();
513                 level -= 1;     /* pop 2, operate, push 1 */
514                 lastpop = -1;
515                 break;
516
517             case '!':
518             case '~':
519                 tc_BUMP();
520                 lastpop = -1;
521                 break;
522
523             case 'i':
524                 /* will add 1 to first (usually two) parameters */
525                 break;
526             }
527         }
528         if (*cp != '\0')
529             cp++;
530     }
531
532     if (number > NUM_PARM)
533         number = NUM_PARM;
534     return number;
535 }
536
537 /*
538  * Analyze the capability string, finding the number of parameters and their
539  * types.
540  *
541  * TODO: cache the result so that this is done once per capability per term.
542  */
543 static int
544 tparm_setup(const char *string, TPARM_DATA * result)
545 {
546     int rc = OK;
547
548     TPS(out_used) = 0;
549     memset(result, 0, sizeof(*result));
550
551     if (string == NULL) {
552         TR(TRACE_CALLS, ("%s: format is null", TPS(tname)));
553         rc = ERR;
554     } else {
555 #if HAVE_TSEARCH
556         TPARM_DATA *fs;
557         void *ft;
558
559         result->format = string;
560         if ((ft = tfind(result, &MyCache, cmp_format)) != 0) {
561             fs = *(TPARM_DATA **) ft;
562             *result = *fs;
563         } else
564 #endif
565         {
566             /*
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.
570              */
571             result->num_parsed = _nc_tparm_analyze(string,
572                                                    result->p_is_s,
573                                                    &(result->num_popped));
574             if (TPS(fmt_buff) == 0) {
575                 TR(TRACE_CALLS, ("%s: error in analysis", TPS(tname)));
576                 rc = ERR;
577             } else {
578                 int n;
579
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);
585
586                 for (n = 0; n < result->num_actual; ++n) {
587                     if (result->p_is_s[n])
588                         result->tparm_type |= (1 << n);
589                 }
590 #if HAVE_TSEARCH
591                 if ((fs = typeCalloc(TPARM_DATA, 1)) != 0) {
592                     *fs = *result;
593                     if ((fs->format = strdup(string)) != 0) {
594                         if (tsearch(fs, &MyCache, cmp_format) != 0) {
595                             ++MyCount;
596                         } else {
597                             free(fs);
598                             rc = ERR;
599                         }
600                     } else {
601                         free(fs);
602                         rc = ERR;
603                     }
604                 } else {
605                     rc = ERR;
606                 }
607 #endif
608             }
609         }
610     }
611
612     return rc;
613 }
614
615 /*
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.
620  */
621 static void
622 tparm_copy_valist(TPARM_DATA * data, int use_TPARM_ARG, va_list ap)
623 {
624     int i;
625
626     for (i = 0; i < data->num_actual; i++) {
627         if (data->p_is_s[i] != 0) {
628             char *value = va_arg(ap, char *);
629             if (value == 0)
630                 value = dummy;
631             data->p_is_s[i] = value;
632             data->param[i] = 0;
633         } else if (use_TPARM_ARG) {
634             data->param[i] = va_arg(ap, TPARM_ARG);
635         } else {
636             data->param[i] = (TPARM_ARG) va_arg(ap, int);
637         }
638     }
639 }
640
641 /*
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.
647  */
648 static bool
649 tparm_tc_compat(TPARM_DATA * data)
650 {
651     bool termcap_hack = FALSE;
652
653     TPS(stack_ptr) = 0;
654
655     if (data->num_popped == 0) {
656         int i;
657
658         termcap_hack = TRUE;
659         for (i = data->num_parsed - 1; i >= 0; i--) {
660             if (data->p_is_s[i])
661                 spush(data->p_is_s[i]);
662             else
663                 npush((int) data->param[i]);
664         }
665     }
666     return termcap_hack;
667 }
668
669 #ifdef TRACE
670 static void
671 tparm_trace_call(const char *string, TPARM_DATA * data)
672 {
673     if (USE_TRACEF(TRACE_CALLS)) {
674         int i;
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);
682                 break;
683             } else {
684                 save_number(", %d", (int) data->param[i], 0);
685             }
686         }
687         _tracef(T_CALLED("%s(%s%s)"), TPS(tname), _nc_visbuf(string), TPS(out_buff));
688         TPS(out_used) = 0;
689         _nc_unlock_global(tracef);
690     }
691 }
692
693 #else
694 #define tparm_trace_call(string, data)  /* nothing */
695 #endif /* TRACE */
696
697 static NCURSES_INLINE char *
698 tparam_internal(const char *string, TPARM_DATA * data)
699 {
700     int number;
701     int len;
702     int level;
703     int x, y;
704     int i;
705     const char *cp = string;
706     size_t len2 = strlen(cp);
707     bool incremented_two = FALSE;
708     bool termcap_hack = tparm_tc_compat(data);
709
710     tparm_trace_call(string, data);
711
712     while ((cp - string) < (int) len2) {
713         if (*cp != '%') {
714             save_char(UChar(*cp));
715         } else {
716             TPS(tparam_base) = cp++;
717             cp = parse_format(cp, TPS(fmt_buff), &len);
718             switch (*cp) {
719             default:
720                 break;
721             case '%':
722                 save_char('%');
723                 break;
724
725             case 'd':           /* FALLTHRU */
726             case 'o':           /* FALLTHRU */
727             case 'x':           /* FALLTHRU */
728             case 'X':           /* FALLTHRU */
729                 save_number(TPS(fmt_buff), npop(), len);
730                 break;
731
732             case 'c':           /* FALLTHRU */
733                 save_char(npop());
734                 break;
735
736 #ifdef EXP_XTERM_1005
737             case 'u':
738                 {
739                     unsigned char target[10];
740                     unsigned source = (unsigned) npop();
741                     int rc = _nc_conv_to_utf8(target, source, (unsigned)
742                                               sizeof(target));
743                     int n;
744                     for (n = 0; n < rc; ++n) {
745                         save_char(target[n]);
746                     }
747                 }
748                 break;
749 #endif
750             case 'l':
751                 npush((int) strlen(spop()));
752                 break;
753
754             case 's':
755                 save_text(TPS(fmt_buff), spop(), len);
756                 break;
757
758             case 'p':
759                 cp++;
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]);
764                     } else {
765                         npush((int) data->param[i]);
766                     }
767                 }
768                 break;
769
770             case 'P':
771                 cp++;
772                 if (isUPPER(*cp)) {
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();
778                 }
779                 break;
780
781             case 'g':
782                 cp++;
783                 if (isUPPER(*cp)) {
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]);
789                 }
790                 break;
791
792             case S_QUOTE:
793                 cp++;
794                 npush(UChar(*cp));
795                 cp++;
796                 break;
797
798             case L_BRACE:
799                 number = 0;
800                 cp++;
801                 while (isdigit(UChar(*cp))) {
802                     number = (number * 10) + (UChar(*cp) - '0');
803                     cp++;
804                 }
805                 npush(number);
806                 break;
807
808             case '+':
809                 npush(npop() + npop());
810                 break;
811
812             case '-':
813                 y = npop();
814                 x = npop();
815                 npush(x - y);
816                 break;
817
818             case '*':
819                 npush(npop() * npop());
820                 break;
821
822             case '/':
823                 y = npop();
824                 x = npop();
825                 npush(y ? (x / y) : 0);
826                 break;
827
828             case 'm':
829                 y = npop();
830                 x = npop();
831                 npush(y ? (x % y) : 0);
832                 break;
833
834             case 'A':
835                 y = npop();
836                 x = npop();
837                 npush(y && x);
838                 break;
839
840             case 'O':
841                 y = npop();
842                 x = npop();
843                 npush(y || x);
844                 break;
845
846             case '&':
847                 npush(npop() & npop());
848                 break;
849
850             case '|':
851                 npush(npop() | npop());
852                 break;
853
854             case '^':
855                 npush(npop() ^ npop());
856                 break;
857
858             case '=':
859                 y = npop();
860                 x = npop();
861                 npush(x == y);
862                 break;
863
864             case '<':
865                 y = npop();
866                 x = npop();
867                 npush(x < y);
868                 break;
869
870             case '>':
871                 y = npop();
872                 x = npop();
873                 npush(x > y);
874                 break;
875
876             case '!':
877                 npush(!npop());
878                 break;
879
880             case '~':
881                 npush(~npop());
882                 break;
883
884             case 'i':
885                 /*
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
890                  * 'p' case.
891                  */
892                 if (!incremented_two) {
893                     incremented_two = TRUE;
894                     if (data->p_is_s[0] == 0) {
895                         data->param[0]++;
896                         if (termcap_hack)
897                             TPS(stack)[0].data.num = (int) data->param[0];
898                     }
899                     if (data->p_is_s[1] == 0) {
900                         data->param[1]++;
901                         if (termcap_hack)
902                             TPS(stack)[1].data.num = (int) data->param[1];
903                     }
904                 }
905                 break;
906
907             case '?':
908                 break;
909
910             case 't':
911                 x = npop();
912                 if (!x) {
913                     /* scan forward for %e or %; at level zero */
914                     cp++;
915                     level = 0;
916                     while (*cp) {
917                         if (*cp == '%') {
918                             cp++;
919                             if (*cp == '?')
920                                 level++;
921                             else if (*cp == ';') {
922                                 if (level > 0)
923                                     level--;
924                                 else
925                                     break;
926                             } else if (*cp == 'e' && level == 0)
927                                 break;
928                         }
929
930                         if (*cp)
931                             cp++;
932                     }
933                 }
934                 break;
935
936             case 'e':
937                 /* scan forward for a %; at level zero */
938                 cp++;
939                 level = 0;
940                 while (*cp) {
941                     if (*cp == '%') {
942                         cp++;
943                         if (*cp == '?')
944                             level++;
945                         else if (*cp == ';') {
946                             if (level > 0)
947                                 level--;
948                             else
949                                 break;
950                         }
951                     }
952
953                     if (*cp)
954                         cp++;
955                 }
956                 break;
957
958             case ';':
959                 break;
960
961             }                   /* endswitch (*cp) */
962         }                       /* endelse (*cp == '%') */
963
964         if (*cp == '\0')
965             break;
966
967         cp++;
968     }                           /* endwhile (*cp) */
969
970     get_space((size_t) 1);
971     TPS(out_buff)[TPS(out_used)] = '\0';
972
973     if (TPS(stack_ptr) && !_nc_tparm_err) {
974         DEBUG(2, ("tparm: stack has %d item%s on return",
975                   TPS(stack_ptr),
976                   TPS(stack_ptr) == 1 ? "" : "s"));
977         _nc_tparm_err++;
978     }
979
980     T((T_RETURN("%s"), _nc_visbuf(TPS(out_buff))));
981     return (TPS(out_buff));
982 }
983
984 #if NCURSES_TPARM_VARARGS
985
986 NCURSES_EXPORT(char *)
987 tparm(const char *string, ...)
988 {
989     TPARM_DATA myData;
990     va_list ap;
991     char *result = NULL;
992
993     _nc_tparm_err = 0;
994 #ifdef TRACE
995     TPS(tname) = "tparm";
996 #endif /* TRACE */
997
998     if (tparm_setup(string, &myData) == OK) {
999
1000         va_start(ap, string);
1001         tparm_copy_valist(&myData, TRUE, ap);
1002         va_end(ap);
1003
1004         result = tparam_internal(string, &myData);
1005     }
1006     return result;
1007 }
1008
1009 #else /* !NCURSES_TPARM_VARARGS */
1010
1011 NCURSES_EXPORT(char *)
1012 tparm(const char *string,
1013       TPARM_ARG a1,
1014       TPARM_ARG a2,
1015       TPARM_ARG a3,
1016       TPARM_ARG a4,
1017       TPARM_ARG a5,
1018       TPARM_ARG a6,
1019       TPARM_ARG a7,
1020       TPARM_ARG a8,
1021       TPARM_ARG a9)
1022 {
1023     TPARM_DATA myData;
1024     char *result = NULL;
1025
1026     _nc_tparm_err = 0;
1027 #ifdef TRACE
1028     TPS(tname) = "tparm";
1029 #endif /* TRACE */
1030
1031     if (tparm_setup(string, &myData) == OK) {
1032
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;
1042
1043         result = tparam_internal(string, &myData);
1044     }
1045     return result;
1046 }
1047
1048 #endif /* NCURSES_TPARM_VARARGS */
1049
1050 NCURSES_EXPORT(char *)
1051 tiparm(const char *string, ...)
1052 {
1053     TPARM_DATA myData;
1054     va_list ap;
1055     char *result = NULL;
1056
1057     _nc_tparm_err = 0;
1058 #ifdef TRACE
1059     TPS(tname) = "tiparm";
1060 #endif /* TRACE */
1061
1062     if (tparm_setup(string, &myData) == OK) {
1063
1064         va_start(ap, string);
1065         tparm_copy_valist(&myData, FALSE, ap);
1066         va_end(ap);
1067
1068         result = tparam_internal(string, &myData);
1069     }
1070     return result;
1071 }
1072
1073 /*
1074  * The internal-use flavor ensures that the parameters are numbers, not strings
1075  */
1076 NCURSES_EXPORT(char *)
1077 _nc_tiparm(int expected, const char *string, ...)
1078 {
1079     TPARM_DATA myData;
1080     va_list ap;
1081     char *result = NULL;
1082
1083     _nc_tparm_err = 0;
1084 #ifdef TRACE
1085     TPS(tname) = "_nc_tiparm";
1086 #endif /* TRACE */
1087
1088     if (tparm_setup(string, &myData) == OK
1089         && myData.num_actual <= expected
1090         && myData.tparm_type == 0) {
1091
1092         va_start(ap, string);
1093         tparm_copy_valist(&myData, FALSE, ap);
1094         va_end(ap);
1095
1096         result = tparam_internal(string, &myData);
1097     }
1098     return result;
1099 }