Home | History | Annotate | Line # | Download | only in libterminfo
tparm.c revision 1.11
      1  1.11      roy /* $NetBSD: tparm.c,v 1.11 2013/01/24 10:28:28 roy Exp $ */
      2   1.1      roy 
      3   1.1      roy /*
      4   1.5      roy  * Copyright (c) 2009, 2011 The NetBSD Foundation, Inc.
      5   1.1      roy  *
      6   1.1      roy  * This code is derived from software contributed to The NetBSD Foundation
      7   1.1      roy  * by Roy Marples.
      8   1.1      roy  *
      9   1.1      roy  * Redistribution and use in source and binary forms, with or without
     10   1.1      roy  * modification, are permitted provided that the following conditions
     11   1.1      roy  * are met:
     12   1.1      roy  * 1. Redistributions of source code must retain the above copyright
     13   1.1      roy  *    notice, this list of conditions and the following disclaimer.
     14   1.1      roy  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.1      roy  *    notice, this list of conditions and the following disclaimer in the
     16   1.1      roy  *    documentation and/or other materials provided with the distribution.
     17   1.1      roy  *
     18   1.1      roy  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19   1.1      roy  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20   1.1      roy  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21   1.1      roy  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22   1.1      roy  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23   1.1      roy  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24   1.1      roy  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25   1.1      roy  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26   1.1      roy  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27   1.1      roy  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28   1.1      roy  */
     29   1.1      roy 
     30   1.1      roy #include <sys/cdefs.h>
     31  1.11      roy __RCSID("$NetBSD: tparm.c,v 1.11 2013/01/24 10:28:28 roy Exp $");
     32   1.8      roy #include <sys/param.h>
     33   1.1      roy 
     34   1.1      roy #include <assert.h>
     35   1.1      roy #include <ctype.h>
     36   1.1      roy #include <errno.h>
     37   1.1      roy #include <stdarg.h>
     38   1.1      roy #include <stdio.h>
     39   1.1      roy #include <stdlib.h>
     40   1.1      roy #include <string.h>
     41   1.1      roy #include <term_private.h>
     42   1.1      roy #include <term.h>
     43   1.1      roy 
     44  1.11      roy #define LONG_STR_MAX ((CHAR_BIT * sizeof(long)) / 3)
     45  1.11      roy #define BUFINC 128 /* Size to increament the terminal buffer by */
     46  1.11      roy 
     47   1.1      roy static TERMINAL *dumbterm; /* For non thread safe functions */
     48   1.1      roy 
     49   1.2      roy typedef struct {
     50  1.11      roy 	long nums[20];
     51   1.1      roy 	char *strings[20];
     52   1.1      roy 	size_t offset;
     53   1.1      roy } TPSTACK;
     54   1.1      roy 
     55   1.2      roy typedef struct {
     56  1.11      roy 	long num;
     57   1.1      roy 	char *string;
     58   1.1      roy } TPVAR;
     59   1.1      roy 
     60   1.1      roy static int
     61  1.11      roy push(long num, char *string, TPSTACK *stack)
     62   1.1      roy {
     63   1.9  msaitoh 	if (stack->offset >= sizeof(stack->nums)) {
     64   1.1      roy 		errno = E2BIG;
     65   1.1      roy 		return -1;
     66   1.1      roy 	}
     67   1.1      roy 	stack->nums[stack->offset] = num;
     68   1.1      roy 	stack->strings[stack->offset] = string;
     69   1.1      roy 	stack->offset++;
     70   1.1      roy 	return 0;
     71   1.1      roy }
     72   1.1      roy 
     73   1.1      roy static int
     74  1.11      roy pop(long *num, char **string, TPSTACK *stack)
     75   1.1      roy {
     76   1.1      roy 	if (stack->offset == 0) {
     77   1.5      roy 		if (num)
     78   1.5      roy 			*num = 0;
     79   1.5      roy 		if (string)
     80   1.5      roy 			*string = NULL;
     81   1.1      roy 		errno = E2BIG;
     82   1.1      roy 		return -1;
     83   1.1      roy 	}
     84   1.1      roy 	stack->offset--;
     85   1.1      roy 	if (num)
     86   1.1      roy 		*num = stack->nums[stack->offset];
     87   1.1      roy 	if (string)
     88   1.1      roy 		*string = stack->strings[stack->offset];
     89   1.1      roy 	return 0;
     90   1.1      roy }
     91   1.1      roy 
     92   1.1      roy static char *
     93   1.1      roy checkbuf(TERMINAL *term, size_t len)
     94   1.1      roy {
     95   1.1      roy 	char *buf;
     96   1.1      roy 
     97   1.1      roy 	if (term->_bufpos + len >= term->_buflen) {
     98  1.11      roy 		len = term->_buflen + MAX(len, BUFINC);
     99   1.1      roy 		buf = realloc(term->_buf, len);
    100   1.1      roy 		if (buf == NULL)
    101   1.8      roy 			return NULL;
    102   1.1      roy 		term->_buf = buf;
    103   1.1      roy 		term->_buflen = len;
    104   1.1      roy 	}
    105   1.1      roy 	return term->_buf;
    106   1.1      roy }
    107   1.1      roy 
    108   1.1      roy static size_t
    109   1.1      roy ochar(TERMINAL *term, int c)
    110   1.1      roy {
    111   1.1      roy 	if (c == 0)
    112   1.1      roy 		c = 0200;
    113   1.1      roy 	/* Check we have space and a terminator */
    114   1.1      roy 	if (checkbuf(term, 2) == NULL)
    115   1.1      roy 		return 0;
    116   1.1      roy 	term->_buf[term->_bufpos++] = (char)c;
    117   1.1      roy 	return 1;
    118   1.1      roy }
    119   1.1      roy 
    120   1.1      roy static size_t
    121  1.11      roy onum(TERMINAL *term, const char *fmt, int num, unsigned int len)
    122   1.1      roy {
    123   1.1      roy 	size_t l;
    124   1.1      roy 
    125  1.11      roy 	if (len < LONG_STR_MAX)
    126  1.11      roy 		len = LONG_STR_MAX;
    127  1.11      roy 	if (checkbuf(term, len + 2) == NULL)
    128   1.1      roy 		return 0;
    129   1.1      roy 	l = sprintf(term->_buf + term->_bufpos, fmt, num);
    130   1.1      roy 	term->_bufpos += l;
    131   1.1      roy 	return l;
    132   1.1      roy }
    133   1.1      roy 
    134   1.1      roy static char *
    135  1.11      roy _ti_tiparm(TERMINAL *term, const char *str, int va_long, va_list parms)
    136   1.1      roy {
    137   1.1      roy 	const char *sp;
    138   1.1      roy 	char c, fmt[64], *fp, *ostr;
    139  1.11      roy 	long val, val2;
    140  1.11      roy 	long dnums[26]; /* dynamic variables a-z, not preserved */
    141   1.1      roy 	size_t l, max;
    142   1.1      roy 	TPSTACK stack;
    143   1.1      roy 	TPVAR params[9];
    144  1.11      roy 	unsigned int done, dot, minus, width, precision, olen;
    145   1.1      roy 	int piss[9]; /* Parameter IS String - piss ;) */
    146   1.1      roy 
    147   1.1      roy 	if (str == NULL)
    148   1.1      roy 		return NULL;
    149   1.1      roy 
    150   1.1      roy 	/*
    151   1.1      roy 	  If not passed a terminal, malloc a dummy one.
    152   1.1      roy 	  This means we can preserve buffers and variables per terminal and
    153   1.1      roy 	  still work with non thread safe functions (which sadly are still the
    154   1.1      roy 	  norm and standard).
    155   1.1      roy 	*/
    156   1.1      roy 	if (term == NULL) {
    157   1.1      roy 		if (dumbterm == NULL) {
    158   1.1      roy 			dumbterm = malloc(sizeof(*dumbterm));
    159   1.1      roy 			if (dumbterm == NULL)
    160   1.1      roy 				return NULL;
    161   1.1      roy 			dumbterm->_buflen = 0;
    162   1.1      roy 		}
    163   1.1      roy 		term = dumbterm;
    164   1.1      roy 	}
    165   1.1      roy 
    166   1.1      roy 	term->_bufpos = 0;
    167   1.1      roy 	/* Ensure we have an initial buffer */
    168   1.1      roy 	if (term->_buflen == 0) {
    169  1.11      roy 		term->_buf = malloc(BUFINC);
    170   1.1      roy 		if (term->_buf == NULL)
    171   1.1      roy 			return NULL;
    172  1.11      roy 		term->_buflen = BUFINC;
    173   1.1      roy 	}
    174   1.1      roy 
    175   1.1      roy 	/*
    176   1.1      roy 	  Make a first pass through the string so we can work out
    177   1.7      roy 	  which parameters are ints and which are char *.
    178   1.1      roy 	  Basically we only use char * if %p[1-9] is followed by %l or %s.
    179   1.1      roy 	*/
    180   1.1      roy 	memset(&piss, 0, sizeof(piss));
    181   1.1      roy 	max = 0;
    182   1.1      roy 	sp = str;
    183   1.1      roy 	while ((c = *sp++) != '\0') {
    184   1.1      roy 		if (c != '%')
    185   1.1      roy 			continue;
    186   1.1      roy 		c = *sp++;
    187   1.1      roy 		if (c == '\0')
    188   1.1      roy 			break;
    189   1.1      roy 		if (c != 'p')
    190   1.1      roy 			continue;
    191   1.1      roy 		c = *sp++;
    192   1.1      roy 		if (c < '1' || c > '9') {
    193   1.1      roy 			errno = EINVAL;
    194   1.5      roy 			continue;
    195   1.1      roy 		}
    196   1.1      roy 		l = c - '0';
    197   1.1      roy 		if (l > max)
    198   1.1      roy 			max = l;
    199   1.1      roy 		if (*sp != '%')
    200   1.1      roy 			continue;
    201   1.1      roy 		/* Skip formatting */
    202   1.1      roy 		sp++;
    203   1.1      roy 		while (*sp == '.' || *sp == '#' || *sp == ' ' || *sp == ':' ||
    204   1.1      roy 		    *sp == '-' || isdigit((unsigned char)*sp))
    205   1.1      roy 			sp++;
    206   1.1      roy 		if (*sp == 'l' || *sp == 's')
    207   1.1      roy 			piss[l - 1] = 1;
    208   1.1      roy 	}
    209   1.1      roy 
    210   1.1      roy 	/* Put our parameters into variables */
    211   1.1      roy 	memset(&params, 0, sizeof(params));
    212   1.1      roy 	for (l = 0; l < max; l++) {
    213  1.11      roy 		if (piss[l])
    214  1.11      roy 			params[l].string = va_arg(parms, char *);
    215  1.11      roy 		else if (va_long)
    216  1.11      roy 			params[l].num = va_arg(parms, long);
    217   1.1      roy 		else
    218  1.11      roy 			params[l].num = (long)va_arg(parms, int);
    219   1.1      roy 	}
    220   1.1      roy 
    221   1.1      roy 	memset(&stack, 0, sizeof(stack));
    222   1.1      roy 	while ((c = *str++) != '\0') {
    223   1.1      roy 		if (c != '%' || (c = *str++) == '%') {
    224   1.1      roy 			if (c == '\0')
    225   1.1      roy 				break;
    226   1.1      roy 			if (ochar(term, c) == 0)
    227   1.1      roy 				return NULL;
    228   1.1      roy 			continue;
    229   1.1      roy 		}
    230   1.1      roy 
    231   1.1      roy 		/* Handle formatting. */
    232   1.1      roy 		fp = fmt;
    233   1.1      roy 		*fp++ = '%';
    234   1.1      roy 		done = dot = minus = width = precision = 0;
    235   1.1      roy 		val = 0;
    236   1.1      roy 		while (done == 0 && (size_t)(fp - fmt) < sizeof(fmt)) {
    237   1.1      roy 			switch (c) {
    238   1.1      roy 			case 'c': /* FALLTHROUGH */
    239  1.11      roy 			case 's':
    240  1.11      roy 				*fp++ = c;
    241  1.11      roy 				done = 1;
    242  1.11      roy 				break;
    243   1.1      roy 			case 'd': /* FALLTHROUGH */
    244   1.1      roy 			case 'o': /* FALLTHROUGH */
    245   1.1      roy 			case 'x': /* FALLTHROUGH */
    246   1.1      roy 			case 'X': /* FALLTHROUGH */
    247  1.11      roy 				*fp++ = 'l';
    248   1.1      roy 				*fp++ = c;
    249   1.1      roy 				done = 1;
    250   1.1      roy 				break;
    251   1.1      roy 			case '#': /* FALLTHROUGH */
    252   1.1      roy 			case ' ':
    253   1.1      roy 				*fp++ = c;
    254   1.1      roy 				break;
    255   1.1      roy 			case '.':
    256   1.1      roy 				*fp++ = c;
    257   1.1      roy 				if (dot == 0) {
    258   1.1      roy 					dot = 1;
    259   1.1      roy 					width = val;
    260   1.1      roy 				} else
    261   1.1      roy 					done = 2;
    262   1.1      roy 				val = 0;
    263   1.1      roy 				break;
    264   1.1      roy 			case ':':
    265   1.1      roy 				minus = 1;
    266   1.1      roy 				break;
    267   1.1      roy 			case '-':
    268   1.1      roy 				if (minus)
    269   1.1      roy 					*fp++ = c;
    270   1.1      roy 				else
    271   1.1      roy 					done = 1;
    272   1.1      roy 				break;
    273   1.1      roy 			default:
    274   1.1      roy 				if (isdigit((unsigned char)c)) {
    275   1.1      roy 					val = (val * 10) + (c - '0');
    276   1.1      roy 					if (val > 10000)
    277   1.1      roy 						done = 2;
    278   1.1      roy 					else
    279   1.1      roy 						*fp++ = c;
    280   1.1      roy 				} else
    281   1.1      roy 					done = 1;
    282   1.1      roy 			}
    283   1.1      roy 			if (done == 0)
    284   1.1      roy 				c = *str++;
    285   1.1      roy 		}
    286   1.1      roy 		if (done == 2) {
    287   1.1      roy 			/* Found an error in the format */
    288   1.1      roy 			fp = fmt + 1;
    289   1.1      roy 			*fp = *str;
    290   1.1      roy 			olen = 0;
    291   1.1      roy 		} else {
    292   1.1      roy 			if (dot == 0)
    293   1.1      roy 				width = val;
    294   1.1      roy 			else
    295   1.1      roy 				precision = val;
    296  1.11      roy 			olen = MAX(width, precision);
    297   1.1      roy 		}
    298   1.1      roy 		*fp++ = '\0';
    299   1.1      roy 
    300   1.1      roy 		/* Handle commands */
    301   1.1      roy 		switch (c) {
    302   1.1      roy 		case 'c':
    303   1.5      roy 			pop(&val, NULL, &stack);
    304   1.1      roy 			if (ochar(term, (unsigned char)val) == 0)
    305   1.1      roy 				return NULL;
    306   1.1      roy 			break;
    307   1.1      roy 		case 's':
    308   1.5      roy 			pop(NULL, &ostr, &stack);
    309   1.1      roy 			if (ostr != NULL) {
    310   1.1      roy 				l = strlen(ostr);
    311   1.1      roy 				if (l < (size_t)olen)
    312   1.1      roy 					l = olen;
    313   1.1      roy 				if (checkbuf(term, (size_t)(l + 1)) == NULL)
    314   1.1      roy 					return NULL;
    315   1.1      roy 				l = sprintf(term->_buf + term->_bufpos,
    316   1.1      roy 				    fmt, ostr);
    317   1.1      roy 				term->_bufpos += l;
    318   1.1      roy 			}
    319   1.1      roy 			break;
    320   1.1      roy 		case 'l':
    321   1.5      roy 			pop(NULL, &ostr, &stack);
    322   1.1      roy 			if (ostr == NULL)
    323   1.1      roy 				l = 0;
    324   1.1      roy 			else
    325   1.1      roy 				l = strlen(ostr);
    326  1.11      roy #ifdef NCURSES_COMPAT_57
    327  1.11      roy 			if (onum(term, "%ld", (long)l, 0) == 0)
    328   1.1      roy 				return NULL;
    329  1.11      roy #else
    330  1.11      roy 			push((long)l, NULL, &stack);
    331  1.11      roy #endif
    332   1.1      roy 			break;
    333   1.1      roy 		case 'd': /* FALLTHROUGH */
    334   1.1      roy 		case 'o': /* FALLTHROUGH */
    335   1.1      roy 		case 'x': /* FALLTHROUGH */
    336   1.1      roy 		case 'X':
    337   1.5      roy 			pop(&val, NULL, &stack);
    338  1.11      roy 			if (onum(term, fmt, (int)val, olen) == 0)
    339   1.1      roy 				return NULL;
    340   1.1      roy 			break;
    341   1.1      roy 		case 'p':
    342   1.5      roy 			if (*str < '1' || *str > '9')
    343   1.5      roy 				break;
    344   1.1      roy 			l = *str++ - '1';
    345   1.1      roy 			if (push(params[l].num, params[l].string, &stack))
    346   1.1      roy 				return NULL;
    347   1.1      roy 			break;
    348   1.1      roy 		case 'P':
    349   1.5      roy 			pop(&val, NULL, &stack);
    350   1.1      roy 			if (*str >= 'a' && *str <= 'z')
    351   1.1      roy 				dnums[*str - 'a'] = val;
    352   1.1      roy 			else if (*str >= 'A' && *str <= 'Z')
    353   1.1      roy 				term->_snums[*str - 'A'] = val;
    354   1.1      roy 			break;
    355   1.1      roy 		case 'g':
    356   1.1      roy 			if (*str >= 'a' && *str <= 'z') {
    357   1.1      roy 				if (push(dnums[*str - 'a'], NULL, &stack))
    358   1.1      roy 					return NULL;
    359   1.1      roy 			} else if (*str >= 'A' && *str <= 'Z') {
    360   1.1      roy 				if (push(term->_snums[*str - 'A'],
    361   1.1      roy 					NULL, &stack))
    362   1.1      roy 					return NULL;
    363   1.1      roy 			}
    364   1.1      roy 			break;
    365   1.1      roy 		case 'i':
    366   1.1      roy 			if (piss[0] == 0)
    367   1.1      roy 				params[0].num++;
    368   1.1      roy 			if (piss[1] == 0)
    369   1.1      roy 				params[1].num++;
    370   1.1      roy 			break;
    371   1.1      roy 		case '\'':
    372  1.11      roy 			if (push((long)(unsigned char)*str++, NULL, &stack))
    373   1.1      roy 				return NULL;
    374   1.1      roy 			while (*str != '\0' && *str != '\'')
    375   1.1      roy 				str++;
    376   1.1      roy 			if (*str == '\'')
    377   1.1      roy 				str++;
    378   1.1      roy 			break;
    379   1.1      roy 		case '{':
    380   1.1      roy 			val = 0;
    381   1.3      roy 			for (; isdigit((unsigned char)*str);  str++)
    382   1.1      roy 				val = (val * 10) + (*str - '0');
    383   1.1      roy 			if (push(val, NULL, &stack))
    384   1.1      roy 				return NULL;
    385   1.1      roy 			while (*str != '\0' && *str != '}')
    386   1.1      roy 				str++;
    387   1.1      roy 			if (*str == '}')
    388   1.1      roy 				str++;
    389   1.1      roy 			break;
    390   1.1      roy 		case '+': /* FALLTHROUGH */
    391   1.1      roy 		case '-': /* FALLTHROUGH */
    392   1.1      roy 		case '*': /* FALLTHROUGH */
    393   1.1      roy 		case '/': /* FALLTHROUGH */
    394   1.1      roy 		case 'm': /* FALLTHROUGH */
    395   1.1      roy 		case 'A': /* FALLTHROUGH */
    396   1.1      roy 		case 'O': /* FALLTHROUGH */
    397   1.1      roy 		case '&': /* FALLTHROUGH */
    398   1.1      roy 		case '|': /* FALLTHROUGH */
    399   1.1      roy 		case '^': /* FALLTHROUGH */
    400   1.1      roy 		case '=': /* FALLTHROUGH */
    401   1.1      roy 		case '<': /* FALLTHROUGH */
    402   1.1      roy 		case '>':
    403   1.5      roy 			pop(&val, NULL, &stack);
    404   1.5      roy 			pop(&val2, NULL, &stack);
    405   1.1      roy 			switch (c) {
    406   1.1      roy 			case '+':
    407   1.1      roy 				val = val + val2;
    408   1.1      roy 				break;
    409   1.1      roy 			case '-':
    410   1.1      roy 				val = val2 - val;
    411   1.1      roy 				break;
    412   1.1      roy 			case '*':
    413   1.1      roy 				val = val * val2;
    414   1.1      roy 				break;
    415   1.1      roy 			case '/':
    416   1.1      roy 				val = val ? val2 / val : 0;
    417   1.1      roy 				break;
    418   1.1      roy 			case 'm':
    419   1.1      roy 				val = val ? val2 % val : 0;
    420   1.1      roy 				break;
    421   1.1      roy 			case 'A':
    422   1.1      roy 				val = val && val2;
    423   1.1      roy 				break;
    424   1.1      roy 			case 'O':
    425   1.1      roy 				val = val || val2;
    426   1.1      roy 				break;
    427   1.1      roy 			case '&':
    428   1.1      roy 				val = val & val2;
    429   1.1      roy 				break;
    430   1.1      roy 			case '|':
    431   1.1      roy 				val = val | val2;
    432   1.1      roy 				break;
    433   1.1      roy 			case '^':
    434   1.1      roy 				val = val ^ val2;
    435   1.1      roy 				break;
    436   1.1      roy 			case '=':
    437   1.1      roy 				val = val == val2;
    438   1.1      roy 				break;
    439   1.1      roy 			case '<':
    440   1.1      roy 				val = val2 < val;
    441   1.1      roy 				break;
    442   1.1      roy 			case '>':
    443   1.1      roy 				val = val2 > val;
    444   1.1      roy 				break;
    445   1.1      roy 			}
    446   1.1      roy 			if (push(val, NULL, &stack))
    447   1.1      roy 				return NULL;
    448   1.1      roy 			break;
    449   1.1      roy 		case '!':
    450   1.1      roy 		case '~':
    451   1.5      roy 			pop(&val, NULL, &stack);
    452  1.11      roy 			switch (c) {
    453   1.1      roy 			case '!':
    454   1.1      roy 				val = !val;
    455   1.1      roy 				break;
    456   1.1      roy 			case '~':
    457   1.1      roy 				val = ~val;
    458   1.1      roy 				break;
    459   1.1      roy 			}
    460   1.1      roy 			if (push(val, NULL, &stack))
    461   1.1      roy 				return NULL;
    462   1.1      roy 			break;
    463   1.1      roy 		case '?': /* if */
    464   1.1      roy 			break;
    465   1.1      roy 		case 't': /* then */
    466   1.5      roy 			pop(&val, NULL, &stack);
    467  1.10      roy 			if (val == 0) {
    468   1.1      roy 				l = 0;
    469   1.1      roy 				for (; *str != '\0'; str++) {
    470   1.1      roy 					if (*str != '%')
    471   1.1      roy 						continue;
    472   1.1      roy 					str++;
    473   1.1      roy 					if (*str == '?')
    474   1.1      roy 						l++;
    475   1.1      roy 					else if (*str == ';') {
    476   1.1      roy 						if (l > 0)
    477   1.1      roy 							l--;
    478  1.10      roy 						else {
    479  1.10      roy 							str++;
    480   1.1      roy 							break;
    481  1.10      roy 						}
    482  1.10      roy 					} else if (*str == 'e' && l == 0) {
    483  1.10      roy 						str++;
    484   1.1      roy 						break;
    485  1.10      roy 					}
    486   1.1      roy 				}
    487   1.1      roy 			}
    488   1.1      roy 			break;
    489   1.1      roy 		case 'e': /* else */
    490   1.1      roy 			l = 0;
    491   1.1      roy 			for (; *str != '\0'; str++) {
    492   1.1      roy 				if (*str != '%')
    493   1.1      roy 					continue;
    494   1.1      roy 				str++;
    495   1.1      roy 				if (*str == '?')
    496   1.1      roy 					l++;
    497   1.1      roy 				else if (*str == ';') {
    498   1.1      roy 					if (l > 0)
    499   1.1      roy 						l--;
    500  1.10      roy 					else {
    501  1.10      roy 						str++;
    502   1.1      roy 						break;
    503  1.10      roy 					}
    504   1.1      roy 				}
    505   1.1      roy 			}
    506   1.1      roy 			break;
    507   1.1      roy 		case ';': /* fi */
    508   1.1      roy 			break;
    509   1.1      roy 		}
    510   1.1      roy 	}
    511   1.1      roy 	term->_buf[term->_bufpos] = '\0';
    512   1.1      roy 	return term->_buf;
    513   1.1      roy }
    514   1.1      roy 
    515   1.1      roy char *
    516   1.6      roy ti_tiparm(TERMINAL *term, const char *str, ...)
    517   1.1      roy {
    518   1.1      roy 	va_list va;
    519   1.1      roy 	char *ret;
    520   1.1      roy 
    521   1.1      roy 	_DIAGASSERT(term != NULL);
    522   1.1      roy 	_DIAGASSERT(str != NULL);
    523   1.1      roy 
    524   1.1      roy 	va_start(va, str);
    525  1.11      roy 	ret = _ti_tiparm(term, str, 0, va);
    526   1.1      roy 	va_end(va);
    527   1.1      roy 	return ret;
    528   1.1      roy }
    529   1.1      roy 
    530   1.1      roy char *
    531   1.6      roy tiparm(const char *str, ...)
    532   1.1      roy {
    533   1.1      roy 	va_list va;
    534   1.1      roy 	char *ret;
    535   1.1      roy 
    536   1.1      roy 	_DIAGASSERT(str != NULL);
    537   1.1      roy 
    538   1.1      roy 	va_start(va, str);
    539  1.11      roy 	ret = _ti_tiparm(NULL, str, 0, va);
    540  1.11      roy 	va_end(va);
    541  1.11      roy 	return ret;
    542  1.11      roy }
    543  1.11      roy 
    544  1.11      roy /* Same as tiparm, but accepts long instead of int for the numeric params.
    545  1.11      roy  * Currently there is no need for this to be a public interface and is only
    546  1.11      roy  * consumed by tparm. If we need this to be public, and I really cannot
    547  1.11      roy  * imagine why, then we would need ti_tlparm() as well. */
    548  1.11      roy static char *
    549  1.11      roy tlparm(const char *str, ...)
    550  1.11      roy {
    551  1.11      roy 	va_list va;
    552  1.11      roy 	char *ret;
    553  1.11      roy 
    554  1.11      roy 	_DIAGASSERT(str != NULL);
    555  1.11      roy 
    556  1.11      roy 	va_start(va, str);
    557  1.11      roy 	ret = _ti_tiparm(NULL, str, 1, va);
    558   1.1      roy 	va_end(va);
    559   1.1      roy 	return ret;
    560   1.1      roy }
    561   1.1      roy 
    562   1.1      roy char *
    563   1.1      roy tparm(const char *str,
    564  1.11      roy     long p1, long p2, long p3, long p4, long p5,
    565  1.11      roy     long p6, long p7, long p8, long p9)
    566   1.1      roy {
    567   1.7      roy 
    568  1.11      roy 	return tlparm(str, p1, p2, p3, p4, p5, p6, p7, p8, p9);
    569   1.1      roy }
    570