]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/lib_tparm.c
970114107c9b4ab3b59bb696c134a535fbf438a5
[ncurses.git] / ncurses / tinfo / lib_tparm.c
1 /****************************************************************************
2  * Copyright (c) 1998-2016,2017 Free Software Foundation, Inc.              *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28
29 /****************************************************************************
30  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
31  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
32  *     and: Thomas E. Dickey, 1996 on                                       *
33  ****************************************************************************/
34
35 /*
36  *      tparm.c
37  *
38  */
39
40 #include <curses.priv.h>
41
42 #include <ctype.h>
43 #include <tic.h>
44
45 MODULE_ID("$Id: lib_tparm.c,v 1.104 2017/08/02 01:23:51 tom Exp $")
46
47 /*
48  *      char *
49  *      tparm(string, ...)
50  *
51  *      Substitute the given parameters into the given string by the following
52  *      rules (taken from terminfo(5)):
53  *
54  *           Cursor addressing and other strings  requiring  parame-
55  *      ters in the terminal are described by a parameterized string
56  *      capability, with escapes like %x in  it.   For  example,  to
57  *      address  the  cursor, the cup capability is given, using two
58  *      parameters: the row and column to  address  to.   (Rows  and
59  *      columns  are  numbered  from  zero and refer to the physical
60  *      screen visible to the user, not to any  unseen  memory.)  If
61  *      the terminal has memory relative cursor addressing, that can
62  *      be indicated by
63  *
64  *           The parameter mechanism uses  a  stack  and  special  %
65  *      codes  to manipulate it.  Typically a sequence will push one
66  *      of the parameters onto the stack and then print it  in  some
67  *      format.  Often more complex operations are necessary.
68  *
69  *           The % encodings have the following meanings:
70  *
71  *           %%        outputs `%'
72  *           %c        print pop() like %c in printf()
73  *           %s        print pop() like %s in printf()
74  *           %[[:]flags][width[.precision]][doxXs]
75  *                     as in printf, flags are [-+#] and space
76  *                     The ':' is used to avoid making %+ or %-
77  *                     patterns (see below).
78  *
79  *           %p[1-9]   push ith parm
80  *           %P[a-z]   set dynamic variable [a-z] to pop()
81  *           %g[a-z]   get dynamic variable [a-z] and push it
82  *           %P[A-Z]   set static variable [A-Z] to pop()
83  *           %g[A-Z]   get static variable [A-Z] and push it
84  *           %l        push strlen(pop)
85  *           %'c'      push char constant c
86  *           %{nn}     push integer constant nn
87  *
88  *           %+ %- %* %/ %m
89  *                     arithmetic (%m is mod): push(pop() op pop())
90  *           %& %| %^  bit operations: push(pop() op pop())
91  *           %= %> %<  logical operations: push(pop() op pop())
92  *           %A %O     logical and & or operations for conditionals
93  *           %! %~     unary operations push(op pop())
94  *           %i        add 1 to first two parms (for ANSI terminals)
95  *
96  *           %? expr %t thenpart %e elsepart %;
97  *                     if-then-else, %e elsepart is optional.
98  *                     else-if's are possible ala Algol 68:
99  *                     %? c1 %t b1 %e c2 %t b2 %e c3 %t b3 %e c4 %t b4 %e b5 %;
100  *
101  *      For those of the above operators which are binary and not commutative,
102  *      the stack works in the usual way, with
103  *                      %gx %gy %m
104  *      resulting in x mod y, not the reverse.
105  */
106
107 NCURSES_EXPORT_VAR(int) _nc_tparm_err = 0;
108
109 #define TPS(var) _nc_prescreen.tparm_state.var
110 #define popcount _nc_popcount   /* workaround for NetBSD 6.0 defect */
111
112 #if NO_LEAKS
113 NCURSES_EXPORT(void)
114 _nc_free_tparm(void)
115 {
116     if (TPS(out_buff) != 0) {
117         FreeAndNull(TPS(out_buff));
118         TPS(out_size) = 0;
119         TPS(out_used) = 0;
120         FreeAndNull(TPS(fmt_buff));
121         TPS(fmt_size) = 0;
122     }
123 }
124 #endif
125
126 static NCURSES_INLINE void
127 get_space(size_t need)
128 {
129     need += TPS(out_used);
130     if (need > TPS(out_size)) {
131         TPS(out_size) = need * 2;
132         TYPE_REALLOC(char, TPS(out_size), TPS(out_buff));
133     }
134 }
135
136 static NCURSES_INLINE void
137 save_text(const char *fmt, const char *s, int len)
138 {
139     size_t s_len = strlen(s);
140     if (len > (int) s_len)
141         s_len = (size_t) len;
142
143     get_space(s_len + 1);
144
145     _nc_SPRINTF(TPS(out_buff) + TPS(out_used),
146                 _nc_SLIMIT(TPS(out_size) - TPS(out_used))
147                 fmt, s);
148     TPS(out_used) += strlen(TPS(out_buff) + TPS(out_used));
149 }
150
151 static NCURSES_INLINE void
152 save_number(const char *fmt, int number, int len)
153 {
154     if (len < 30)
155         len = 30;               /* actually log10(MAX_INT)+1 */
156
157     get_space((size_t) len + 1);
158
159     _nc_SPRINTF(TPS(out_buff) + TPS(out_used),
160                 _nc_SLIMIT(TPS(out_size) - TPS(out_used))
161                 fmt, number);
162     TPS(out_used) += strlen(TPS(out_buff) + TPS(out_used));
163 }
164
165 static NCURSES_INLINE void
166 save_char(int c)
167 {
168     if (c == 0)
169         c = 0200;
170     get_space((size_t) 1);
171     TPS(out_buff)[TPS(out_used)++] = (char) c;
172 }
173
174 static NCURSES_INLINE void
175 npush(int x)
176 {
177     if (TPS(stack_ptr) < STACKSIZE) {
178         TPS(stack)[TPS(stack_ptr)].num_type = TRUE;
179         TPS(stack)[TPS(stack_ptr)].data.num = x;
180         TPS(stack_ptr)++;
181     } else {
182         DEBUG(2, ("npush: stack overflow: %s", _nc_visbuf(TPS(tparam_base))));
183         _nc_tparm_err++;
184     }
185 }
186
187 static NCURSES_INLINE int
188 npop(void)
189 {
190     int result = 0;
191     if (TPS(stack_ptr) > 0) {
192         TPS(stack_ptr)--;
193         if (TPS(stack)[TPS(stack_ptr)].num_type)
194             result = TPS(stack)[TPS(stack_ptr)].data.num;
195     } else {
196         DEBUG(2, ("npop: stack underflow: %s", _nc_visbuf(TPS(tparam_base))));
197         _nc_tparm_err++;
198     }
199     return result;
200 }
201
202 static NCURSES_INLINE void
203 spush(char *x)
204 {
205     if (TPS(stack_ptr) < STACKSIZE) {
206         TPS(stack)[TPS(stack_ptr)].num_type = FALSE;
207         TPS(stack)[TPS(stack_ptr)].data.str = x;
208         TPS(stack_ptr)++;
209     } else {
210         DEBUG(2, ("spush: stack overflow: %s", _nc_visbuf(TPS(tparam_base))));
211         _nc_tparm_err++;
212     }
213 }
214
215 static NCURSES_INLINE char *
216 spop(void)
217 {
218     static char dummy[] = "";   /* avoid const-cast */
219     char *result = dummy;
220     if (TPS(stack_ptr) > 0) {
221         TPS(stack_ptr)--;
222         if (!TPS(stack)[TPS(stack_ptr)].num_type
223             && TPS(stack)[TPS(stack_ptr)].data.str != 0)
224             result = TPS(stack)[TPS(stack_ptr)].data.str;
225     } else {
226         DEBUG(2, ("spop: stack underflow: %s", _nc_visbuf(TPS(tparam_base))));
227         _nc_tparm_err++;
228     }
229     return result;
230 }
231
232 static NCURSES_INLINE const char *
233 parse_format(const char *s, char *format, int *len)
234 {
235     *len = 0;
236     if (format != 0) {
237         bool done = FALSE;
238         bool allowminus = FALSE;
239         bool dot = FALSE;
240         bool err = FALSE;
241         char *fmt = format;
242         int my_width = 0;
243         int my_prec = 0;
244         int value = 0;
245
246         *len = 0;
247         *format++ = '%';
248         while (*s != '\0' && !done) {
249             switch (*s) {
250             case 'c':           /* FALLTHRU */
251             case 'd':           /* FALLTHRU */
252             case 'o':           /* FALLTHRU */
253             case 'x':           /* FALLTHRU */
254             case 'X':           /* FALLTHRU */
255             case 's':
256 #ifdef EXP_XTERM_1005
257             case 'u':
258 #endif
259                 *format++ = *s;
260                 done = TRUE;
261                 break;
262             case '.':
263                 *format++ = *s++;
264                 if (dot) {
265                     err = TRUE;
266                 } else {        /* value before '.' is the width */
267                     dot = TRUE;
268                     my_width = value;
269                 }
270                 value = 0;
271                 break;
272             case '#':
273                 *format++ = *s++;
274                 break;
275             case ' ':
276                 *format++ = *s++;
277                 break;
278             case ':':
279                 s++;
280                 allowminus = TRUE;
281                 break;
282             case '-':
283                 if (allowminus) {
284                     *format++ = *s++;
285                 } else {
286                     done = TRUE;
287                 }
288                 break;
289             default:
290                 if (isdigit(UChar(*s))) {
291                     value = (value * 10) + (*s - '0');
292                     if (value > 10000)
293                         err = TRUE;
294                     *format++ = *s++;
295                 } else {
296                     done = TRUE;
297                 }
298             }
299         }
300
301         /*
302          * If we found an error, ignore (and remove) the flags.
303          */
304         if (err) {
305             my_width = my_prec = value = 0;
306             format = fmt;
307             *format++ = '%';
308             *format++ = *s;
309         }
310
311         /*
312          * Any value after '.' is the precision.  If we did not see '.', then
313          * the value is the width.
314          */
315         if (dot)
316             my_prec = value;
317         else
318             my_width = value;
319
320         *format = '\0';
321         /* return maximum string length in print */
322         *len = (my_width > my_prec) ? my_width : my_prec;
323     }
324     return s;
325 }
326
327 #define isUPPER(c) ((c) >= 'A' && (c) <= 'Z')
328 #define isLOWER(c) ((c) >= 'a' && (c) <= 'z')
329 #define tc_BUMP()  if (level < 0 && number < 2) number++
330
331 /*
332  * Analyze the string to see how many parameters we need from the varargs list,
333  * and what their types are.  We will only accept string parameters if they
334  * appear as a %l or %s format following an explicit parameter reference (e.g.,
335  * %p2%s).  All other parameters are numbers.
336  *
337  * 'number' counts coarsely the number of pop's we see in the string, and
338  * 'popcount' shows the highest parameter number in the string.  We would like
339  * to simply use the latter count, but if we are reading termcap strings, there
340  * may be cases that we cannot see the explicit parameter numbers.
341  */
342 NCURSES_EXPORT(int)
343 _nc_tparm_analyze(const char *string, char *p_is_s[NUM_PARM], int *popcount)
344 {
345     size_t len2;
346     int i;
347     int lastpop = -1;
348     int len;
349     int number = 0;
350     int level = -1;
351     const char *cp = string;
352     static char dummy[] = "";
353
354     if (cp == 0)
355         return 0;
356
357     if ((len2 = strlen(cp)) > TPS(fmt_size)) {
358         TPS(fmt_size) = len2 + TPS(fmt_size) + 2;
359         TPS(fmt_buff) = typeRealloc(char, TPS(fmt_size), TPS(fmt_buff));
360         if (TPS(fmt_buff) == 0)
361             return 0;
362     }
363
364     memset(p_is_s, 0, sizeof(p_is_s[0]) * NUM_PARM);
365     *popcount = 0;
366
367     while ((cp - string) < (int) len2) {
368         if (*cp == '%') {
369             cp++;
370             cp = parse_format(cp, TPS(fmt_buff), &len);
371             switch (*cp) {
372             default:
373                 break;
374
375             case 'd':           /* FALLTHRU */
376             case 'o':           /* FALLTHRU */
377             case 'x':           /* FALLTHRU */
378             case 'X':           /* FALLTHRU */
379             case 'c':           /* FALLTHRU */
380 #ifdef EXP_XTERM_1005
381             case 'u':
382 #endif
383                 if (lastpop <= 0) {
384                     tc_BUMP();
385                 }
386                 level -= 1;
387                 lastpop = -1;
388                 break;
389
390             case 'l':
391             case 's':
392                 if (lastpop > 0) {
393                     level -= 1;
394                     p_is_s[lastpop - 1] = dummy;
395                 }
396                 tc_BUMP();
397                 break;
398
399             case 'p':
400                 cp++;
401                 i = (UChar(*cp) - '0');
402                 if (i >= 0 && i <= NUM_PARM) {
403                     ++level;
404                     lastpop = i;
405                     if (lastpop > *popcount)
406                         *popcount = lastpop;
407                 }
408                 break;
409
410             case 'P':
411                 ++cp;
412                 break;
413
414             case 'g':
415                 ++level;
416                 cp++;
417                 break;
418
419             case S_QUOTE:
420                 ++level;
421                 cp += 2;
422                 lastpop = -1;
423                 break;
424
425             case L_BRACE:
426                 ++level;
427                 cp++;
428                 while (isdigit(UChar(*cp))) {
429                     cp++;
430                 }
431                 break;
432
433             case '+':
434             case '-':
435             case '*':
436             case '/':
437             case 'm':
438             case 'A':
439             case 'O':
440             case '&':
441             case '|':
442             case '^':
443             case '=':
444             case '<':
445             case '>':
446                 tc_BUMP();
447                 level -= 1;     /* pop 2, operate, push 1 */
448                 lastpop = -1;
449                 break;
450
451             case '!':
452             case '~':
453                 tc_BUMP();
454                 lastpop = -1;
455                 break;
456
457             case 'i':
458                 /* will add 1 to first (usually two) parameters */
459                 break;
460             }
461         }
462         if (*cp != '\0')
463             cp++;
464     }
465
466     if (number > NUM_PARM)
467         number = NUM_PARM;
468     return number;
469 }
470
471 static NCURSES_INLINE char *
472 tparam_internal(int use_TPARM_ARG, const char *string, va_list ap)
473 {
474     char *p_is_s[NUM_PARM];
475     TPARM_ARG param[NUM_PARM];
476     int popcount = 0;
477     int number;
478     int num_args;
479     int len;
480     int level;
481     int x, y;
482     int i;
483     const char *cp = string;
484     size_t len2;
485     bool termcap_hack;
486     bool incremented_two;
487
488     if (cp == NULL)
489         return NULL;
490
491     TPS(out_used) = 0;
492     len2 = strlen(cp);
493
494     /*
495      * Find the highest parameter-number referred to in the format string.
496      * Use this value to limit the number of arguments copied from the
497      * variable-length argument list.
498      */
499     number = _nc_tparm_analyze(cp, p_is_s, &popcount);
500     if (TPS(fmt_buff) == 0)
501         return NULL;
502
503     incremented_two = FALSE;
504
505     if (number > NUM_PARM)
506         number = NUM_PARM;
507     if (popcount > NUM_PARM)
508         popcount = NUM_PARM;
509     num_args = max(popcount, number);
510
511     for (i = 0; i < num_args; i++) {
512         /*
513          * A few caps (such as plab_norm) have string-valued parms.
514          * We'll have to assume that the caller knows the difference, since
515          * a char* and an int may not be the same size on the stack.  The
516          * normal prototype for this uses 9 long's, which is consistent with
517          * our va_arg() usage.
518          */
519         if (p_is_s[i] != 0) {
520             p_is_s[i] = va_arg(ap, char *);
521             param[i] = 0;
522         } else if (use_TPARM_ARG) {
523             param[i] = va_arg(ap, TPARM_ARG);
524         } else {
525             param[i] = (TPARM_ARG) va_arg(ap, int);
526         }
527     }
528
529     /*
530      * This is a termcap compatibility hack.  If there are no explicit pop
531      * operations in the string, load the stack in such a way that
532      * successive pops will grab successive parameters.  That will make
533      * the expansion of (for example) \E[%d;%dH work correctly in termcap
534      * style, which means tparam() will expand termcap strings OK.
535      */
536     TPS(stack_ptr) = 0;
537     termcap_hack = FALSE;
538     if (popcount == 0) {
539         termcap_hack = TRUE;
540         for (i = number - 1; i >= 0; i--) {
541             if (p_is_s[i])
542                 spush(p_is_s[i]);
543             else
544                 npush((int) param[i]);
545         }
546     }
547 #ifdef TRACE
548     if (USE_TRACEF(TRACE_CALLS)) {
549         for (i = 0; i < num_args; i++) {
550             if (p_is_s[i] != 0) {
551                 save_text(", %s", _nc_visbuf(p_is_s[i]), 0);
552             } else if ((long) param[i] > MAX_OF_TYPE(NCURSES_INT2) ||
553                        (long) param[i] < 0) {
554                 _tracef("BUG: problem with tparm parameter #%d of %d",
555                         i + 1, num_args);
556                 break;
557             } else {
558                 save_number(", %d", (int) param[i], 0);
559             }
560         }
561         _tracef(T_CALLED("%s(%s%s)"), TPS(tname), _nc_visbuf(cp), TPS(out_buff));
562         TPS(out_used) = 0;
563         _nc_unlock_global(tracef);
564     }
565 #endif /* TRACE */
566
567     while ((cp - string) < (int) len2) {
568         if (*cp != '%') {
569             save_char(UChar(*cp));
570         } else {
571             TPS(tparam_base) = cp++;
572             cp = parse_format(cp, TPS(fmt_buff), &len);
573             switch (*cp) {
574             default:
575                 break;
576             case '%':
577                 save_char('%');
578                 break;
579
580             case 'd':           /* FALLTHRU */
581             case 'o':           /* FALLTHRU */
582             case 'x':           /* FALLTHRU */
583             case 'X':           /* FALLTHRU */
584                 save_number(TPS(fmt_buff), npop(), len);
585                 break;
586
587             case 'c':           /* FALLTHRU */
588                 save_char(npop());
589                 break;
590
591 #ifdef EXP_XTERM_1005
592             case 'u':
593                 {
594                     unsigned char target[10];
595                     unsigned source = (unsigned) npop();
596                     int rc = _nc_conv_to_utf8(target, source, (unsigned)
597                                               sizeof(target));
598                     int n;
599                     for (n = 0; n < rc; ++n) {
600                         save_char(target[n]);
601                     }
602                 }
603                 break;
604 #endif
605             case 'l':
606                 npush((int) strlen(spop()));
607                 break;
608
609             case 's':
610                 save_text(TPS(fmt_buff), spop(), len);
611                 break;
612
613             case 'p':
614                 cp++;
615                 i = (UChar(*cp) - '1');
616                 if (i >= 0 && i < NUM_PARM) {
617                     if (p_is_s[i]) {
618                         spush(p_is_s[i]);
619                     } else {
620                         npush((int) param[i]);
621                     }
622                 }
623                 break;
624
625             case 'P':
626                 cp++;
627                 if (isUPPER(*cp)) {
628                     i = (UChar(*cp) - 'A');
629                     TPS(static_vars)[i] = npop();
630                 } else if (isLOWER(*cp)) {
631                     i = (UChar(*cp) - 'a');
632                     TPS(dynamic_var)[i] = npop();
633                 }
634                 break;
635
636             case 'g':
637                 cp++;
638                 if (isUPPER(*cp)) {
639                     i = (UChar(*cp) - 'A');
640                     npush(TPS(static_vars)[i]);
641                 } else if (isLOWER(*cp)) {
642                     i = (UChar(*cp) - 'a');
643                     npush(TPS(dynamic_var)[i]);
644                 }
645                 break;
646
647             case S_QUOTE:
648                 cp++;
649                 npush(UChar(*cp));
650                 cp++;
651                 break;
652
653             case L_BRACE:
654                 number = 0;
655                 cp++;
656                 while (isdigit(UChar(*cp))) {
657                     number = (number * 10) + (UChar(*cp) - '0');
658                     cp++;
659                 }
660                 npush(number);
661                 break;
662
663             case '+':
664                 npush(npop() + npop());
665                 break;
666
667             case '-':
668                 y = npop();
669                 x = npop();
670                 npush(x - y);
671                 break;
672
673             case '*':
674                 npush(npop() * npop());
675                 break;
676
677             case '/':
678                 y = npop();
679                 x = npop();
680                 npush(y ? (x / y) : 0);
681                 break;
682
683             case 'm':
684                 y = npop();
685                 x = npop();
686                 npush(y ? (x % y) : 0);
687                 break;
688
689             case 'A':
690                 y = npop();
691                 x = npop();
692                 npush(y && x);
693                 break;
694
695             case 'O':
696                 y = npop();
697                 x = npop();
698                 npush(y || x);
699                 break;
700
701             case '&':
702                 npush(npop() & npop());
703                 break;
704
705             case '|':
706                 npush(npop() | npop());
707                 break;
708
709             case '^':
710                 npush(npop() ^ npop());
711                 break;
712
713             case '=':
714                 y = npop();
715                 x = npop();
716                 npush(x == y);
717                 break;
718
719             case '<':
720                 y = npop();
721                 x = npop();
722                 npush(x < y);
723                 break;
724
725             case '>':
726                 y = npop();
727                 x = npop();
728                 npush(x > y);
729                 break;
730
731             case '!':
732                 npush(!npop());
733                 break;
734
735             case '~':
736                 npush(~npop());
737                 break;
738
739             case 'i':
740                 /*
741                  * Increment the first two parameters -- if they are numbers
742                  * rather than strings.  As a side effect, assign into the
743                  * stack; if this is termcap, then the stack was populated
744                  * using the termcap hack above rather than via the terminfo
745                  * 'p' case.
746                  */
747                 if (!incremented_two) {
748                     incremented_two = TRUE;
749                     if (p_is_s[0] == 0) {
750                         param[0]++;
751                         if (termcap_hack)
752                             TPS(stack)[0].data.num = (int) param[0];
753                     }
754                     if (p_is_s[1] == 0) {
755                         param[1]++;
756                         if (termcap_hack)
757                             TPS(stack)[1].data.num = (int) param[1];
758                     }
759                 }
760                 break;
761
762             case '?':
763                 break;
764
765             case 't':
766                 x = npop();
767                 if (!x) {
768                     /* scan forward for %e or %; at level zero */
769                     cp++;
770                     level = 0;
771                     while (*cp) {
772                         if (*cp == '%') {
773                             cp++;
774                             if (*cp == '?')
775                                 level++;
776                             else if (*cp == ';') {
777                                 if (level > 0)
778                                     level--;
779                                 else
780                                     break;
781                             } else if (*cp == 'e' && level == 0)
782                                 break;
783                         }
784
785                         if (*cp)
786                             cp++;
787                     }
788                 }
789                 break;
790
791             case 'e':
792                 /* scan forward for a %; at level zero */
793                 cp++;
794                 level = 0;
795                 while (*cp) {
796                     if (*cp == '%') {
797                         cp++;
798                         if (*cp == '?')
799                             level++;
800                         else if (*cp == ';') {
801                             if (level > 0)
802                                 level--;
803                             else
804                                 break;
805                         }
806                     }
807
808                     if (*cp)
809                         cp++;
810                 }
811                 break;
812
813             case ';':
814                 break;
815
816             }                   /* endswitch (*cp) */
817         }                       /* endelse (*cp == '%') */
818
819         if (*cp == '\0')
820             break;
821
822         cp++;
823     }                           /* endwhile (*cp) */
824
825     get_space((size_t) 1);
826     TPS(out_buff)[TPS(out_used)] = '\0';
827
828     T((T_RETURN("%s"), _nc_visbuf(TPS(out_buff))));
829     return (TPS(out_buff));
830 }
831
832 #if NCURSES_TPARM_VARARGS
833 #define tparm_varargs tparm
834 #else
835 #define tparm_proto tparm
836 #endif
837
838 NCURSES_EXPORT(char *)
839 tparm_varargs(NCURSES_CONST char *string,...)
840 {
841     va_list ap;
842     char *result;
843
844     _nc_tparm_err = 0;
845     va_start(ap, string);
846 #ifdef TRACE
847     TPS(tname) = "tparm";
848 #endif /* TRACE */
849     result = tparam_internal(TRUE, string, ap);
850     va_end(ap);
851     return result;
852 }
853
854 #if !NCURSES_TPARM_VARARGS
855 NCURSES_EXPORT(char *)
856 tparm_proto(NCURSES_CONST char *string,
857             TPARM_ARG a1,
858             TPARM_ARG a2,
859             TPARM_ARG a3,
860             TPARM_ARG a4,
861             TPARM_ARG a5,
862             TPARM_ARG a6,
863             TPARM_ARG a7,
864             TPARM_ARG a8,
865             TPARM_ARG a9)
866 {
867     return tparm_varargs(string, a1, a2, a3, a4, a5, a6, a7, a8, a9);
868 }
869 #endif /* NCURSES_TPARM_VARARGS */
870
871 NCURSES_EXPORT(char *)
872 tiparm(const char *string,...)
873 {
874     va_list ap;
875     char *result;
876
877     _nc_tparm_err = 0;
878     va_start(ap, string);
879 #ifdef TRACE
880     TPS(tname) = "tiparm";
881 #endif /* TRACE */
882     result = tparam_internal(FALSE, string, ap);
883     va_end(ap);
884     return result;
885 }