]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/lib_tparm.c
ncurses 6.2 - patch 20210821
[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.134 2021/08/21 21:52:08 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) tps->var
121 #define popcount _nc_popcount   /* workaround for NetBSD 6.0 defect */
122
123 #define get_tparm_state(term) \
124             (term != NULL \
125               ? &(term->tparm_state) \
126               : &(_nc_prescreen.tparm_state))
127
128 #define isUPPER(c) ((c) >= 'A' && (c) <= 'Z')
129 #define isLOWER(c) ((c) >= 'a' && (c) <= 'z')
130 #define tc_BUMP()  if (level < 0 && number < 2) number++
131
132 typedef struct {
133     const char *format;         /* format-string can be used as cache-key */
134     int tparm_type;             /* bit-set for each string-parameter */
135     int num_actual;
136     int num_parsed;
137     int num_popped;
138     TPARM_ARG param[NUM_PARM];
139     char *p_is_s[NUM_PARM];
140 } TPARM_DATA;
141
142 #if HAVE_TSEARCH
143 #define MyCache _nc_globals.cached_tparm
144 #define MyCount _nc_globals.count_tparm
145 #if NO_LEAKS
146 static int which_tparm;
147 static TPARM_DATA **delete_tparm;
148 #endif
149 #endif /* HAVE_TSEARCH */
150
151 static char dummy[] = "";       /* avoid const-cast */
152
153 #if HAVE_TSEARCH
154 static int
155 cmp_format(const void *p, const void *q)
156 {
157     const char *a = *(char *const *) p;
158     const char *b = *(char *const *) q;
159     return strcmp(a, b);
160 }
161 #endif
162
163 #if NO_LEAKS
164 #if HAVE_TSEARCH
165 static void
166 visit_nodes(const void *nodep, const VISIT which, const int depth)
167 {
168     (void) depth;
169     if (which == preorder || which == leaf) {
170         delete_tparm[which_tparm] = *(TPARM_DATA **) nodep;
171         which_tparm++;
172     }
173 }
174 #endif
175
176 NCURSES_EXPORT(void)
177 _nc_free_tparm(void)
178 {
179     TPARM_STATE *tps = get_tparm_state(cur_term);       /* FIXME */
180 #if HAVE_TSEARCH
181     if (MyCount != 0) {
182         delete_tparm = typeCalloc(TPARM_DATA *, MyCount);
183         which_tparm = 0;
184         twalk(MyCache, visit_nodes);
185         for (which_tparm = 0; which_tparm < MyCount; ++which_tparm) {
186             TPARM_DATA *ptr = delete_tparm[which_tparm];
187             if (ptr != NULL) {
188                 tdelete(ptr, &MyCache, cmp_format);
189                 free((char *) ptr->format);
190                 free(ptr);
191             }
192         }
193         which_tparm = 0;
194         twalk(MyCache, visit_nodes);
195         FreeAndNull(delete_tparm);
196         MyCount = 0;
197         which_tparm = 0;
198     }
199 #endif
200     FreeAndNull(TPS(out_buff));
201     TPS(out_size) = 0;
202     TPS(out_used) = 0;
203
204     FreeAndNull(TPS(fmt_buff));
205     TPS(fmt_size) = 0;
206 }
207 #endif
208
209 static int
210 tparm_error(TPARM_STATE *tps, const char *message)
211 {
212     DEBUG(2, ("%s: %s", message, _nc_visbuf(TPS(tparam_base))));
213     return ++_nc_tparm_err;
214 }
215
216 #define get_space(tps, need) \
217 { \
218     size_t need2get = need + TPS(out_used); \
219     if (need2get > TPS(out_size)) { \
220         TPS(out_size) = need2get * 2; \
221         TYPE_REALLOC(char, TPS(out_size), TPS(out_buff)); \
222     } \
223 }
224
225 #if NCURSES_EXPANDED
226 static NCURSES_INLINE void
227   (get_space) (TPARM_STATE *tps, size_t need) {
228     get_space(tps, need);
229 }
230
231 #undef get_space
232 #endif
233
234 #define save_text(tps, fmt, s, len) \
235 { \
236     size_t s_len = (size_t) len + strlen(s) + strlen(fmt); \
237     get_space(tps, s_len + 1); \
238     _nc_SPRINTF(TPS(out_buff) + TPS(out_used), \
239                 _nc_SLIMIT(TPS(out_size) - TPS(out_used)) \
240                 fmt, s); \
241     TPS(out_used) += strlen(TPS(out_buff) + TPS(out_used)); \
242 }
243
244 #if NCURSES_EXPANDED
245 static NCURSES_INLINE void
246   (save_text) (TPARM_STATE *tps, const char *fmt, const char *s, int len) {
247     save_text(tps, fmt, s, len);
248 }
249
250 #undef save_text
251 #endif
252
253 #define save_number(tps, fmt, number, len) \
254 { \
255     size_t s_len = (size_t) len + 30 + strlen(fmt); \
256     get_space(tps, s_len + 1); \
257     _nc_SPRINTF(TPS(out_buff) + TPS(out_used), \
258                 _nc_SLIMIT(TPS(out_size) - TPS(out_used)) \
259                 fmt, number); \
260     TPS(out_used) += strlen(TPS(out_buff) + TPS(out_used)); \
261 }
262
263 #if NCURSES_EXPANDED
264 static NCURSES_INLINE void
265   (save_number) (TPARM_STATE *tps, const char *fmt, int number, int len) {
266     save_number(tps, fmt, number, len);
267 }
268
269 #undef save_number
270 #endif
271
272 #define save_char(tps, c) \
273 { \
274     get_space(tps, (size_t) 1); \
275     TPS(out_buff)[TPS(out_used)++] = (char) ((c == 0) ? 0200 : c); \
276 }
277
278 #if NCURSES_EXPANDED
279 static NCURSES_INLINE void
280   (save_char) (TPARM_STATE *tps, int c) {
281     save_char(tps, c);
282 }
283
284 #undef save_char
285 #endif
286
287 #define npush(tps, x) \
288 { \
289     if (TPS(stack_ptr) < STACKSIZE) { \
290         TPS(stack)[TPS(stack_ptr)].num_type = TRUE; \
291         TPS(stack)[TPS(stack_ptr)].data.num = x; \
292         TPS(stack_ptr)++; \
293     } else { \
294         (void) tparm_error(tps, "npush: stack overflow"); \
295     } \
296 }
297
298 #if NCURSES_EXPANDED
299 static NCURSES_INLINE void
300   (npush) (TPARM_STATE *tps, int x) {
301     npush(tps, x);
302 }
303
304 #undef npush
305 #endif
306
307 #define spush(tps, x) \
308 { \
309     if (TPS(stack_ptr) < STACKSIZE) { \
310         TPS(stack)[TPS(stack_ptr)].num_type = FALSE; \
311         TPS(stack)[TPS(stack_ptr)].data.str = x; \
312         TPS(stack_ptr)++; \
313     } else { \
314         (void) tparm_error(tps, "spush: stack overflow"); \
315     } \
316 }
317
318 #if NCURSES_EXPANDED
319 static NCURSES_INLINE void
320   (spush) (TPARM_STATE *tps, char *x) {
321     spush(tps, x);
322 }
323
324 #undef spush
325 #endif
326
327 #define npop(tps) \
328     ((TPS(stack_ptr)-- > 0) \
329      ? ((TPS(stack)[TPS(stack_ptr)].num_type) \
330          ? TPS(stack)[TPS(stack_ptr)].data.num \
331          : 0) \
332      : (tparm_error(tps, "npop: stack underflow"), \
333         TPS(stack_ptr) = 0))
334
335 #if NCURSES_EXPANDED
336 static NCURSES_INLINE int
337   (npop) (TPARM_STATE *tps) {
338     return npop(tps);
339 }
340 #undef npop
341 #endif
342
343 #define spop(tps) \
344     ((TPS(stack_ptr)-- > 0) \
345      ? ((!TPS(stack)[TPS(stack_ptr)].num_type \
346         && TPS(stack)[TPS(stack_ptr)].data.str != 0) \
347          ? TPS(stack)[TPS(stack_ptr)].data.str \
348          : dummy) \
349      : (tparm_error(tps, "spop: stack underflow"), \
350         dummy))
351
352 #if NCURSES_EXPANDED
353 static NCURSES_INLINE char *
354   (spop) (TPARM_STATE *tps) {
355     return spop(tps);
356 }
357 #undef spop
358 #endif
359
360 static NCURSES_INLINE const char *
361 parse_format(const char *s, char *format, int *len)
362 {
363     *len = 0;
364     if (format != 0) {
365         bool done = FALSE;
366         bool allowminus = FALSE;
367         bool dot = FALSE;
368         bool err = FALSE;
369         char *fmt = format;
370         int my_width = 0;
371         int my_prec = 0;
372         int value = 0;
373
374         *len = 0;
375         *format++ = '%';
376         while (*s != '\0' && !done) {
377             switch (*s) {
378             case 'c':           /* FALLTHRU */
379             case 'd':           /* FALLTHRU */
380             case 'o':           /* FALLTHRU */
381             case 'x':           /* FALLTHRU */
382             case 'X':           /* FALLTHRU */
383             case 's':
384 #ifdef EXP_XTERM_1005
385             case 'u':
386 #endif
387                 *format++ = *s;
388                 done = TRUE;
389                 break;
390             case '.':
391                 *format++ = *s++;
392                 if (dot) {
393                     err = TRUE;
394                 } else {        /* value before '.' is the width */
395                     dot = TRUE;
396                     my_width = value;
397                 }
398                 value = 0;
399                 break;
400             case '#':
401                 *format++ = *s++;
402                 break;
403             case ' ':
404                 *format++ = *s++;
405                 break;
406             case ':':
407                 s++;
408                 allowminus = TRUE;
409                 break;
410             case '-':
411                 if (allowminus) {
412                     *format++ = *s++;
413                 } else {
414                     done = TRUE;
415                 }
416                 break;
417             default:
418                 if (isdigit(UChar(*s))) {
419                     value = (value * 10) + (*s - '0');
420                     if (value > 10000)
421                         err = TRUE;
422                     *format++ = *s++;
423                 } else {
424                     done = TRUE;
425                 }
426             }
427         }
428
429         /*
430          * If we found an error, ignore (and remove) the flags.
431          */
432         if (err) {
433             my_width = my_prec = value = 0;
434             format = fmt;
435             *format++ = '%';
436             *format++ = *s;
437         }
438
439         /*
440          * Any value after '.' is the precision.  If we did not see '.', then
441          * the value is the width.
442          */
443         if (dot)
444             my_prec = value;
445         else
446             my_width = value;
447
448         *format = '\0';
449         /* return maximum string length in print */
450         *len = (my_width > my_prec) ? my_width : my_prec;
451     }
452     return s;
453 }
454
455 /*
456  * Analyze the string to see how many parameters we need from the varargs list,
457  * and what their types are.  We will only accept string parameters if they
458  * appear as a %l or %s format following an explicit parameter reference (e.g.,
459  * %p2%s).  All other parameters are numbers.
460  *
461  * 'number' counts coarsely the number of pop's we see in the string, and
462  * 'popcount' shows the highest parameter number in the string.  We would like
463  * to simply use the latter count, but if we are reading termcap strings, there
464  * may be cases that we cannot see the explicit parameter numbers.
465  */
466 NCURSES_EXPORT(int)
467 _nc_tparm_analyze(TERMINAL *term, const char *string, char **p_is_s, int *popcount)
468 {
469     TPARM_STATE *tps = get_tparm_state(term);
470     size_t len2;
471     int i;
472     int lastpop = -1;
473     int len;
474     int number = 0;
475     int level = -1;
476     const char *cp = string;
477
478     if (cp == 0)
479         return 0;
480
481     if ((len2 = strlen(cp)) + 2 > TPS(fmt_size)) {
482         TPS(fmt_size) += len2 + 2;
483         TPS(fmt_buff) = typeRealloc(char, TPS(fmt_size), TPS(fmt_buff));
484         if (TPS(fmt_buff) == 0)
485             return 0;
486     }
487
488     memset(p_is_s, 0, sizeof(p_is_s[0]) * NUM_PARM);
489     *popcount = 0;
490
491     while ((cp - string) < (int) len2) {
492         if (*cp == '%') {
493             cp++;
494             cp = parse_format(cp, TPS(fmt_buff), &len);
495             switch (*cp) {
496             default:
497                 break;
498
499             case 'd':           /* FALLTHRU */
500             case 'o':           /* FALLTHRU */
501             case 'x':           /* FALLTHRU */
502             case 'X':           /* FALLTHRU */
503             case 'c':           /* FALLTHRU */
504 #ifdef EXP_XTERM_1005
505             case 'u':
506 #endif
507                 if (lastpop <= 0) {
508                     tc_BUMP();
509                 }
510                 level -= 1;
511                 lastpop = -1;
512                 break;
513
514             case 'l':
515             case 's':
516                 if (lastpop > 0) {
517                     level -= 1;
518                     p_is_s[lastpop - 1] = dummy;
519                 }
520                 tc_BUMP();
521                 break;
522
523             case 'p':
524                 cp++;
525                 i = (UChar(*cp) - '0');
526                 if (i >= 0 && i <= NUM_PARM) {
527                     ++level;
528                     lastpop = i;
529                     if (lastpop > *popcount)
530                         *popcount = lastpop;
531                 }
532                 break;
533
534             case 'P':
535                 ++cp;
536                 break;
537
538             case 'g':
539                 ++level;
540                 cp++;
541                 break;
542
543             case S_QUOTE:
544                 ++level;
545                 cp += 2;
546                 lastpop = -1;
547                 break;
548
549             case L_BRACE:
550                 ++level;
551                 cp++;
552                 while (isdigit(UChar(*cp))) {
553                     cp++;
554                 }
555                 break;
556
557             case '+':
558             case '-':
559             case '*':
560             case '/':
561             case 'm':
562             case 'A':
563             case 'O':
564             case '&':
565             case '|':
566             case '^':
567             case '=':
568             case '<':
569             case '>':
570                 tc_BUMP();
571                 level -= 1;     /* pop 2, operate, push 1 */
572                 lastpop = -1;
573                 break;
574
575             case '!':
576             case '~':
577                 tc_BUMP();
578                 lastpop = -1;
579                 break;
580
581             case 'i':
582                 /* will add 1 to first (usually two) parameters */
583                 break;
584             }
585         }
586         if (*cp != '\0')
587             cp++;
588     }
589
590     if (number > NUM_PARM)
591         number = NUM_PARM;
592     return number;
593 }
594
595 /*
596  * Analyze the capability string, finding the number of parameters and their
597  * types.
598  *
599  * TODO: cache the result so that this is done once per capability per term.
600  */
601 static int
602 tparm_setup(TERMINAL *term, const char *string, TPARM_DATA *result)
603 {
604     TPARM_STATE *tps = get_tparm_state(term);
605     int rc = OK;
606
607     TPS(out_used) = 0;
608     memset(result, 0, sizeof(*result));
609
610     if (string == NULL) {
611         TR(TRACE_CALLS, ("%s: format is null", TPS(tname)));
612         rc = ERR;
613     } else {
614 #if HAVE_TSEARCH
615         TPARM_DATA *fs;
616         void *ft;
617
618         result->format = string;
619         if ((ft = tfind(result, &MyCache, cmp_format)) != 0) {
620             size_t len2;
621             fs = *(TPARM_DATA **) ft;
622             *result = *fs;
623             if ((len2 = strlen(string)) + 2 > TPS(fmt_size)) {
624                 TPS(fmt_size) += len2 + 2;
625                 TPS(fmt_buff) = typeRealloc(char, TPS(fmt_size), TPS(fmt_buff));
626                 if (TPS(fmt_buff) == 0)
627                     return ERR;
628             }
629         } else
630 #endif
631         {
632             /*
633              * Find the highest parameter-number referred to in the format
634              * string.  Use this value to limit the number of arguments copied
635              * from the variable-length argument list.
636              */
637             result->num_parsed = _nc_tparm_analyze(term, string,
638                                                    result->p_is_s,
639                                                    &(result->num_popped));
640             if (TPS(fmt_buff) == 0) {
641                 TR(TRACE_CALLS, ("%s: error in analysis", TPS(tname)));
642                 rc = ERR;
643             } else {
644                 int n;
645
646                 if (result->num_parsed > NUM_PARM)
647                     result->num_parsed = NUM_PARM;
648                 if (result->num_popped > NUM_PARM)
649                     result->num_popped = NUM_PARM;
650                 result->num_actual = max(result->num_popped, result->num_parsed);
651
652                 for (n = 0; n < result->num_actual; ++n) {
653                     if (result->p_is_s[n])
654                         result->tparm_type |= (1 << n);
655                 }
656 #if HAVE_TSEARCH
657                 if ((fs = typeCalloc(TPARM_DATA, 1)) != 0) {
658                     *fs = *result;
659                     if ((fs->format = strdup(string)) != 0) {
660                         if (tsearch(fs, &MyCache, cmp_format) != 0) {
661                             ++MyCount;
662                         } else {
663                             free(fs);
664                             rc = ERR;
665                         }
666                     } else {
667                         free(fs);
668                         rc = ERR;
669                     }
670                 } else {
671                     rc = ERR;
672                 }
673 #endif
674             }
675         }
676     }
677
678     return rc;
679 }
680
681 /*
682  * A few caps (such as plab_norm) have string-valued parms.  We'll have to
683  * assume that the caller knows the difference, since a char* and an int may
684  * not be the same size on the stack.  The normal prototype for tparm uses 9
685  * long's, which is consistent with our va_arg() usage.
686  */
687 static void
688 tparm_copy_valist(TPARM_DATA *data, int use_TPARM_ARG, va_list ap)
689 {
690     int i;
691
692     for (i = 0; i < data->num_actual; i++) {
693         if (data->p_is_s[i] != 0) {
694             char *value = va_arg(ap, char *);
695             if (value == 0)
696                 value = dummy;
697             data->p_is_s[i] = value;
698             data->param[i] = 0;
699         } else if (use_TPARM_ARG) {
700             data->param[i] = va_arg(ap, TPARM_ARG);
701         } else {
702             data->param[i] = (TPARM_ARG) va_arg(ap, int);
703         }
704     }
705 }
706
707 /*
708  * This is a termcap compatibility hack.  If there are no explicit pop
709  * operations in the string, load the stack in such a way that successive pops
710  * will grab successive parameters.  That will make the expansion of (for
711  * example) \E[%d;%dH work correctly in termcap style, which means tparam()
712  * will expand termcap strings OK.
713  */
714 static bool
715 tparm_tc_compat(TPARM_STATE *tps, TPARM_DATA *data)
716 {
717     bool termcap_hack = FALSE;
718
719     TPS(stack_ptr) = 0;
720
721     if (data->num_popped == 0) {
722         int i;
723
724         termcap_hack = TRUE;
725         for (i = data->num_parsed - 1; i >= 0; i--) {
726             if (data->p_is_s[i]) {
727                 spush(tps, data->p_is_s[i]);
728             } else {
729                 npush(tps, (int) data->param[i]);
730             }
731         }
732     }
733     return termcap_hack;
734 }
735
736 #ifdef TRACE
737 static void
738 tparm_trace_call(TPARM_STATE *tps, const char *string, TPARM_DATA *data)
739 {
740     if (USE_TRACEF(TRACE_CALLS)) {
741         int i;
742         for (i = 0; i < data->num_actual; i++) {
743             if (data->p_is_s[i] != 0) {
744                 save_text(tps, ", %s", _nc_visbuf(data->p_is_s[i]), 0);
745             } else if ((long) data->param[i] > MAX_OF_TYPE(NCURSES_INT2) ||
746                        (long) data->param[i] < 0) {
747                 _tracef("BUG: problem with tparm parameter #%d of %d",
748                         i + 1, data->num_actual);
749                 break;
750             } else {
751                 save_number(tps, ", %d", (int) data->param[i], 0);
752             }
753         }
754         _tracef(T_CALLED("%s(%s%s)"), TPS(tname), _nc_visbuf(string), TPS(out_buff));
755         TPS(out_used) = 0;
756         _nc_unlock_global(tracef);
757     }
758 }
759
760 #else
761 #define tparm_trace_call(tps, string, data)     /* nothing */
762 #endif /* TRACE */
763
764 #define init_vars(name) \
765         if (!name##_used) { \
766             name##_used = TRUE; \
767             memset(name##_vars, 0, sizeof(name##_vars)); \
768         }
769
770 static NCURSES_INLINE char *
771 tparam_internal(TPARM_STATE *tps, const char *string, TPARM_DATA *data)
772 {
773     int number;
774     int len;
775     int level;
776     int x, y;
777     int i;
778     const char *s;
779     const char *cp = string;
780     size_t len2 = strlen(cp);
781     bool incremented_two = FALSE;
782     bool termcap_hack = tparm_tc_compat(tps, data);
783     /*
784      * SVr4 curses stores variables 'A' to 'Z' in the TERMINAL structure (so
785      * they are initialized once to zero), and variables 'a' to 'z' on the
786      * stack in tparm, referring to the former as "static" and the latter as
787      * "dynamic".  However, it makes no check to ensure that the "dynamic"
788      * variables are initialized.
789      *
790      * Solaris xpg4 curses makes no distinction between the upper/lower, and
791      * stores the common set of 26 variables on the stack, without initializing
792      * them.
793      *
794      * In ncurses, both sets of variables are initialized on the first use.
795      */
796     bool dynamic_used = FALSE;
797     int dynamic_vars[NUM_VARS];
798
799     tparm_trace_call(tps, string, data);
800
801     while ((cp - string) < (int) len2) {
802         if (*cp != '%') {
803             save_char(tps, UChar(*cp));
804         } else {
805             TPS(tparam_base) = cp++;
806             cp = parse_format(cp, TPS(fmt_buff), &len);
807             switch (*cp) {
808             default:
809                 break;
810             case '%':
811                 save_char(tps, '%');
812                 break;
813
814             case 'd':           /* FALLTHRU */
815             case 'o':           /* FALLTHRU */
816             case 'x':           /* FALLTHRU */
817             case 'X':           /* FALLTHRU */
818                 x = npop(tps);
819                 save_number(tps, TPS(fmt_buff), x, len);
820                 break;
821
822             case 'c':           /* FALLTHRU */
823                 x = npop(tps);
824                 save_char(tps, x);
825                 break;
826
827 #ifdef EXP_XTERM_1005
828             case 'u':
829                 {
830                     unsigned char target[10];
831                     unsigned source = (unsigned) npop(tps);
832                     int rc = _nc_conv_to_utf8(target, source, (unsigned)
833                                               sizeof(target));
834                     int n;
835                     for (n = 0; n < rc; ++n) {
836                         save_char(tps, target[n]);
837                     }
838                 }
839                 break;
840 #endif
841             case 'l':
842                 s = spop(tps);
843                 npush(tps, (int) strlen(s));
844                 break;
845
846             case 's':
847                 s = spop(tps);
848                 save_text(tps, TPS(fmt_buff), s, len);
849                 break;
850
851             case 'p':
852                 cp++;
853                 i = (UChar(*cp) - '1');
854                 if (i >= 0 && i < NUM_PARM) {
855                     if (data->p_is_s[i]) {
856                         spush(tps, data->p_is_s[i]);
857                     } else {
858                         npush(tps, (int) data->param[i]);
859                     }
860                 }
861                 break;
862
863             case 'P':
864                 cp++;
865                 if (isUPPER(*cp)) {
866                     i = (UChar(*cp) - 'A');
867                     TPS(static_vars)[i] = npop(tps);
868                 } else if (isLOWER(*cp)) {
869                     i = (UChar(*cp) - 'a');
870                     init_vars(dynamic);
871                     dynamic_vars[i] = npop(tps);
872                 }
873                 break;
874
875             case 'g':
876                 cp++;
877                 if (isUPPER(*cp)) {
878                     i = (UChar(*cp) - 'A');
879                     npush(tps, TPS(static_vars)[i]);
880                 } else if (isLOWER(*cp)) {
881                     i = (UChar(*cp) - 'a');
882                     init_vars(dynamic);
883                     npush(tps, dynamic_vars[i]);
884                 }
885                 break;
886
887             case S_QUOTE:
888                 cp++;
889                 npush(tps, UChar(*cp));
890                 cp++;
891                 break;
892
893             case L_BRACE:
894                 number = 0;
895                 cp++;
896                 while (isdigit(UChar(*cp))) {
897                     number = (number * 10) + (UChar(*cp) - '0');
898                     cp++;
899                 }
900                 npush(tps, number);
901                 break;
902
903             case '+':
904                 y = npop(tps);
905                 x = npop(tps);
906                 npush(tps, x + y);
907                 break;
908
909             case '-':
910                 y = npop(tps);
911                 x = npop(tps);
912                 npush(tps, x - y);
913                 break;
914
915             case '*':
916                 y = npop(tps);
917                 x = npop(tps);
918                 npush(tps, x * y);
919                 break;
920
921             case '/':
922                 y = npop(tps);
923                 x = npop(tps);
924                 npush(tps, y ? (x / y) : 0);
925                 break;
926
927             case 'm':
928                 y = npop(tps);
929                 x = npop(tps);
930                 npush(tps, y ? (x % y) : 0);
931                 break;
932
933             case 'A':
934                 y = npop(tps);
935                 x = npop(tps);
936                 npush(tps, y && x);
937                 break;
938
939             case 'O':
940                 y = npop(tps);
941                 x = npop(tps);
942                 npush(tps, y || x);
943                 break;
944
945             case '&':
946                 y = npop(tps);
947                 x = npop(tps);
948                 npush(tps, x & y);
949                 break;
950
951             case '|':
952                 y = npop(tps);
953                 x = npop(tps);
954                 npush(tps, x | y);
955                 break;
956
957             case '^':
958                 y = npop(tps);
959                 x = npop(tps);
960                 npush(tps, x ^ y);
961                 break;
962
963             case '=':
964                 y = npop(tps);
965                 x = npop(tps);
966                 npush(tps, x == y);
967                 break;
968
969             case '<':
970                 y = npop(tps);
971                 x = npop(tps);
972                 npush(tps, x < y);
973                 break;
974
975             case '>':
976                 y = npop(tps);
977                 x = npop(tps);
978                 npush(tps, x > y);
979                 break;
980
981             case '!':
982                 x = npop(tps);
983                 npush(tps, !x);
984                 break;
985
986             case '~':
987                 x = npop(tps);
988                 npush(tps, ~x);
989                 break;
990
991             case 'i':
992                 /*
993                  * Increment the first two parameters -- if they are numbers
994                  * rather than strings.  As a side effect, assign into the
995                  * stack; if this is termcap, then the stack was populated
996                  * using the termcap hack above rather than via the terminfo
997                  * 'p' case.
998                  */
999                 if (!incremented_two) {
1000                     incremented_two = TRUE;
1001                     if (data->p_is_s[0] == 0) {
1002                         data->param[0]++;
1003                         if (termcap_hack)
1004                             TPS(stack)[0].data.num = (int) data->param[0];
1005                     }
1006                     if (data->p_is_s[1] == 0) {
1007                         data->param[1]++;
1008                         if (termcap_hack)
1009                             TPS(stack)[1].data.num = (int) data->param[1];
1010                     }
1011                 }
1012                 break;
1013
1014             case '?':
1015                 break;
1016
1017             case 't':
1018                 x = npop(tps);
1019                 if (!x) {
1020                     /* scan forward for %e or %; at level zero */
1021                     cp++;
1022                     level = 0;
1023                     while (*cp) {
1024                         if (*cp == '%') {
1025                             cp++;
1026                             if (*cp == '?')
1027                                 level++;
1028                             else if (*cp == ';') {
1029                                 if (level > 0)
1030                                     level--;
1031                                 else
1032                                     break;
1033                             } else if (*cp == 'e' && level == 0)
1034                                 break;
1035                         }
1036
1037                         if (*cp)
1038                             cp++;
1039                     }
1040                 }
1041                 break;
1042
1043             case 'e':
1044                 /* scan forward for a %; at level zero */
1045                 cp++;
1046                 level = 0;
1047                 while (*cp) {
1048                     if (*cp == '%') {
1049                         cp++;
1050                         if (*cp == '?')
1051                             level++;
1052                         else if (*cp == ';') {
1053                             if (level > 0)
1054                                 level--;
1055                             else
1056                                 break;
1057                         }
1058                     }
1059
1060                     if (*cp)
1061                         cp++;
1062                 }
1063                 break;
1064
1065             case ';':
1066                 break;
1067
1068             }                   /* endswitch (*cp) */
1069         }                       /* endelse (*cp == '%') */
1070
1071         if (*cp == '\0')
1072             break;
1073
1074         cp++;
1075     }                           /* endwhile (*cp) */
1076
1077     get_space(tps, (size_t) 1);
1078     TPS(out_buff)[TPS(out_used)] = '\0';
1079
1080     if (TPS(stack_ptr) && !_nc_tparm_err) {
1081         DEBUG(2, ("tparm: stack has %d item%s on return",
1082                   TPS(stack_ptr),
1083                   TPS(stack_ptr) == 1 ? "" : "s"));
1084         _nc_tparm_err++;
1085     }
1086
1087     T((T_RETURN("%s"), _nc_visbuf(TPS(out_buff))));
1088     return (TPS(out_buff));
1089 }
1090
1091 #if NCURSES_TPARM_VARARGS
1092
1093 NCURSES_EXPORT(char *)
1094 tparm(const char *string, ...)
1095 {
1096     TPARM_STATE *tps = get_tparm_state(cur_term);
1097     TPARM_DATA myData;
1098     char *result = NULL;
1099
1100     _nc_tparm_err = 0;
1101 #ifdef TRACE
1102     tps->tname = "tparm";
1103 #endif /* TRACE */
1104
1105     if (tparm_setup(cur_term, string, &myData) == OK) {
1106         va_list ap;
1107
1108         va_start(ap, string);
1109         tparm_copy_valist(&myData, TRUE, ap);
1110         va_end(ap);
1111
1112         result = tparam_internal(tps, string, &myData);
1113     }
1114     return result;
1115 }
1116
1117 #else /* !NCURSES_TPARM_VARARGS */
1118
1119 NCURSES_EXPORT(char *)
1120 tparm(const char *string,
1121       TPARM_ARG a1,
1122       TPARM_ARG a2,
1123       TPARM_ARG a3,
1124       TPARM_ARG a4,
1125       TPARM_ARG a5,
1126       TPARM_ARG a6,
1127       TPARM_ARG a7,
1128       TPARM_ARG a8,
1129       TPARM_ARG a9)
1130 {
1131     TPARM_STATE *tps = get_tparm_state(cur_term);
1132     TPARM_DATA myData;
1133     char *result = NULL;
1134
1135     _nc_tparm_err = 0;
1136 #ifdef TRACE
1137     tps->tname = "tparm";
1138 #endif /* TRACE */
1139
1140     if (tparm_setup(cur_term, string, &myData) == OK) {
1141
1142         myData.param[0] = a1;
1143         myData.param[1] = a2;
1144         myData.param[2] = a3;
1145         myData.param[3] = a4;
1146         myData.param[4] = a5;
1147         myData.param[5] = a6;
1148         myData.param[6] = a7;
1149         myData.param[7] = a8;
1150         myData.param[8] = a9;
1151
1152         result = tparam_internal(tps, string, &myData);
1153     }
1154     return result;
1155 }
1156
1157 #endif /* NCURSES_TPARM_VARARGS */
1158
1159 NCURSES_EXPORT(char *)
1160 tiparm(const char *string, ...)
1161 {
1162     TPARM_STATE *tps = get_tparm_state(cur_term);
1163     TPARM_DATA myData;
1164     char *result = NULL;
1165
1166     _nc_tparm_err = 0;
1167 #ifdef TRACE
1168     tps->tname = "tiparm";
1169 #endif /* TRACE */
1170
1171     if (tparm_setup(cur_term, string, &myData) == OK) {
1172         va_list ap;
1173
1174         va_start(ap, string);
1175         tparm_copy_valist(&myData, FALSE, ap);
1176         va_end(ap);
1177
1178         result = tparam_internal(tps, string, &myData);
1179     }
1180     return result;
1181 }
1182
1183 /*
1184  * The internal-use flavor ensures that the parameters are numbers, not strings
1185  */
1186 NCURSES_EXPORT(char *)
1187 _nc_tiparm(int expected, const char *string, ...)
1188 {
1189     TPARM_STATE *tps = get_tparm_state(cur_term);
1190     TPARM_DATA myData;
1191     char *result = NULL;
1192
1193     _nc_tparm_err = 0;
1194 #ifdef TRACE
1195     tps->tname = "_nc_tiparm";
1196 #endif /* TRACE */
1197
1198     if (tparm_setup(cur_term, string, &myData) == OK
1199         && myData.num_actual <= expected
1200         && myData.tparm_type == 0) {
1201         va_list ap;
1202
1203         va_start(ap, string);
1204         tparm_copy_valist(&myData, FALSE, ap);
1205         va_end(ap);
1206
1207         result = tparam_internal(tps, string, &myData);
1208     }
1209     return result;
1210 }
1211
1212 /*
1213  * Improve tic's checks by resetting the terminfo "static variables" before
1214  * calling functions which may update them.
1215  */
1216 NCURSES_EXPORT(void)
1217 _nc_reset_tparm(TERMINAL *term)
1218 {
1219     TPARM_STATE *tps = get_tparm_state(term);
1220     memset(TPS(static_vars), 0, sizeof(TPS(static_vars)));
1221 }