xref: /openbsd-src/lib/libcurses/tinfo/captoinfo.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: captoinfo.c,v 1.15 2006/10/24 17:25:48 moritz Exp $	*/
2 
3 /****************************************************************************
4  * Copyright (c) 1998,1999,2000 Free Software Foundation, Inc.              *
5  *                                                                          *
6  * Permission is hereby granted, free of charge, to any person obtaining a  *
7  * copy of this software and associated documentation files (the            *
8  * "Software"), to deal in the Software without restriction, including      *
9  * without limitation the rights to use, copy, modify, merge, publish,      *
10  * distribute, distribute with modifications, sublicense, and/or sell       *
11  * copies of the Software, and to permit persons to whom the Software is    *
12  * furnished to do so, subject to the following conditions:                 *
13  *                                                                          *
14  * The above copyright notice and this permission notice shall be included  *
15  * in all copies or substantial portions of the Software.                   *
16  *                                                                          *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
20  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
21  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
22  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
23  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
24  *                                                                          *
25  * Except as contained in this notice, the name(s) of the above copyright   *
26  * holders shall not be used in advertising or otherwise to promote the     *
27  * sale, use or other dealings in this Software without prior written       *
28  * authorization.                                                           *
29  ****************************************************************************/
30 
31 /****************************************************************************
32  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
33  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
34  ****************************************************************************/
35 
36 /*
37  *	captoinfo.c --- conversion between termcap and terminfo formats
38  *
39  *	The captoinfo() code was swiped from Ross Ridge's mytinfo package,
40  *	adapted to fit ncurses by Eric S. Raymond <esr@snark.thyrsus.com>.
41  *
42  *	There is just one entry point:
43  *
44  *	char *_nc_captoinfo(n, s, parametrized)
45  *
46  *	Convert value s for termcap string capability named n into terminfo
47  *	format.
48  *
49  *	This code recognizes all the standard 4.4BSD %-escapes:
50  *
51  *	%%       output `%'
52  *	%d       output value as in printf %d
53  *	%2       output value as in printf %2d
54  *	%3       output value as in printf %3d
55  *	%.       output value as in printf %c
56  *	%+x      add x to value, then do %.
57  *	%>xy     if value > x then add y, no output
58  *	%r       reverse order of two parameters, no output
59  *	%i       increment by one, no output
60  *	%n       exclusive-or all parameters with 0140 (Datamedia 2500)
61  *	%B       BCD (16*(value/10)) + (value%10), no output
62  *	%D       Reverse coding (value - 2*(value%16)), no output (Delta Data).
63  *
64  *	Also, %02 and %03 are accepted as synonyms for %2 and %3.
65  *
66  *	Besides all the standard termcap escapes, this translator understands
67  *	the following extended escapes:
68  *
69  *	used by GNU Emacs termcap libraries
70  *		%a[+*-/=][cp]x	GNU arithmetic.
71  *		%m		xor the first two parameters by 0177
72  *		%b		backup to previous parameter
73  *		%f		skip this parameter
74  *
75  *	used by the University of Waterloo (MFCF) termcap libraries
76  *		%-x	 subtract parameter FROM char x and output it as a char
77  *		%ax	 add the character x to parameter
78  *
79  *	If #define WATERLOO is on, also enable these translations:
80  *
81  *		%sx	 subtract parameter FROM the character x
82  *
83  *	By default, this Waterloo translations are not compiled in, because
84  *	the Waterloo %s conflicts with the way terminfo uses %s in strings for
85  *	function programming.
86  *
87  *	Note the two definitions of %a: the GNU definition is translated if the
88  *	characters after the 'a' are valid for it, otherwise the UW definition
89  *	is translated.
90  */
91 
92 #include <curses.priv.h>
93 
94 #include <ctype.h>
95 #include <tic.h>
96 
97 MODULE_ID("$From: captoinfo.c,v 1.40 2000/11/05 00:22:36 tom Exp $")
98 
99 #define MAX_PUSHED	16	/* max # args we can push onto the stack */
100 
101 static int stack[MAX_PUSHED];	/* the stack */
102 static int stackptr;		/* the next empty place on the stack */
103 static int onstack;		/* the top of stack */
104 static int seenm;		/* seen a %m */
105 static int seenn;		/* seen a %n */
106 static int seenr;		/* seen a %r */
107 static int param;		/* current parameter */
108 static char *dp;		/* pointer to end of the converted string */
109 
110 static char *my_string;
111 static size_t my_length;
112 
113 static char *
114 init_string(void)
115 /* initialize 'my_string', 'my_length' */
116 {
117     if (my_string == 0)
118 	my_string = typeMalloc(char, my_length = 256);
119     if (my_string == 0)
120 	_nc_err_abort("Out of memory");
121 
122     *my_string = '\0';
123     return my_string;
124 }
125 
126 static char *
127 save_string(char *d, const char *const s)
128 {
129     size_t have = (d - my_string);
130     size_t need = have + strlen(s) + 2;
131     size_t copied;
132     if (need > my_length) {
133 	my_string = (char *) realloc(my_string, my_length = (need + need));
134 	if (my_string == 0)
135 	    _nc_err_abort("Out of memory");
136 	d = my_string + have;
137     }
138     if ((copied = strlcpy(d, s, my_length - have)) >= my_length - have)
139 	_nc_err_abort("Buffer overflow");
140     return d + copied;
141 }
142 
143 static inline char *
144 save_char(char *s, char c)
145 {
146     static char temp[2];
147     temp[0] = c;
148     return save_string(s, temp);
149 }
150 
151 static void
152 push(void)
153 /* push onstack on to the stack */
154 {
155     if (stackptr > MAX_PUSHED)
156 	_nc_warning("string too complex to convert");
157     else
158 	stack[stackptr++] = onstack;
159 }
160 
161 static void
162 pop(void)
163 /* pop the top of the stack into onstack */
164 {
165     if (stackptr == 0) {
166 	if (onstack == 0)
167 	    _nc_warning("I'm confused");
168 	else
169 	    onstack = 0;
170     } else
171 	onstack = stack[--stackptr];
172     param++;
173 }
174 
175 static int
176 cvtchar(register const char *sp)
177 /* convert a character to a terminfo push */
178 {
179     unsigned char c = 0;
180     int len;
181 
182     switch (*sp) {
183     case '\\':
184 	switch (*++sp) {
185 	case '\'':
186 	case '$':
187 	case '\\':
188 	case '%':
189 	    c = *sp;
190 	    len = 2;
191 	    break;
192 	case '\0':
193 	    c = '\\';
194 	    len = 1;
195 	    break;
196 	case '0':
197 	case '1':
198 	case '2':
199 	case '3':
200 	    len = 1;
201 	    while (isdigit(CharOf(*sp))) {
202 		c = 8 * c + (*sp++ - '0');
203 		len++;
204 	    }
205 	    break;
206 	default:
207 	    c = *sp;
208 	    len = 2;
209 	    break;
210 	}
211 	break;
212     case '^':
213 	c = (*++sp & 0x1f);
214 	len = 2;
215 	break;
216     default:
217 	c = *sp;
218 	len = 1;
219     }
220     if (isgraph(c) && c != ',' && c != '\'' && c != '\\' && c != ':') {
221 	dp = save_string(dp, "%\'");
222 	dp = save_char(dp, c);
223 	dp = save_char(dp, '\'');
224     } else {
225 	dp = save_string(dp, "%{");
226 	if (c > 99)
227 	    dp = save_char(dp, c / 100 + '0');
228 	if (c > 9)
229 	    dp = save_char(dp, ((int) (c / 10)) % 10 + '0');
230 	dp = save_char(dp, c % 10 + '0');
231 	dp = save_char(dp, '}');
232     }
233     return len;
234 }
235 
236 static void
237 getparm(int parm, int n)
238 /* push n copies of param on the terminfo stack if not already there */
239 {
240     if (seenr) {
241 	if (parm == 1)
242 	    parm = 2;
243 	else if (parm == 2)
244 	    parm = 1;
245     }
246     if (onstack == parm) {
247 	if (n > 1) {
248 	    _nc_warning("string may not be optimal");
249 	    dp = save_string(dp, "%Pa");
250 	    while (n--) {
251 		dp = save_string(dp, "%ga");
252 	    }
253 	}
254 	return;
255     }
256     if (onstack != 0)
257 	push();
258 
259     onstack = parm;
260 
261     while (n--) {
262 	dp = save_string(dp, "%p");
263 	dp = save_char(dp, '0' + parm);
264     }
265 
266     if (seenn && parm < 3) {
267 	dp = save_string(dp, "%{96}%^");
268     }
269 
270     if (seenm && parm < 3) {
271 	dp = save_string(dp, "%{127}%^");
272     }
273 }
274 
275 /*
276  * Convert a termcap string to terminfo format.
277  * 'cap' is the relevant terminfo capability index.
278  * 's' is the string value of the capability.
279  * 'parametrized' tells what type of translations to do:
280  *	% translations if 1
281  *	pad translations if >=0
282  */
283 char *
284 _nc_captoinfo(const char *cap, const char *s, int const parametrized)
285 {
286     const char *capstart;
287 
288     stackptr = 0;
289     onstack = 0;
290     seenm = 0;
291     seenn = 0;
292     seenr = 0;
293     param = 1;
294 
295     dp = init_string();
296 
297     /* skip the initial padding (if we haven't been told not to) */
298     capstart = 0;
299     if (s == 0)
300 	s = "";
301     if (parametrized >= 0 && isdigit(CharOf(*s)))
302 	for (capstart = s;; s++)
303 	    if (!(isdigit(CharOf(*s)) || *s == '*' || *s == '.'))
304 		break;
305 
306     while (*s != '\0') {
307 	switch (*s) {
308 	case '%':
309 	    s++;
310 	    if (parametrized < 1) {
311 		dp = save_char(dp, '%');
312 		break;
313 	    }
314 	    switch (*s++) {
315 	    case '%':
316 		dp = save_char(dp, '%');
317 		break;
318 	    case 'r':
319 		if (seenr++ == 1) {
320 		    _nc_warning("saw %%r twice in %s", cap);
321 		}
322 		break;
323 	    case 'm':
324 		if (seenm++ == 1) {
325 		    _nc_warning("saw %%m twice in %s", cap);
326 		}
327 		break;
328 	    case 'n':
329 		if (seenn++ == 1) {
330 		    _nc_warning("saw %%n twice in %s", cap);
331 		}
332 		break;
333 	    case 'i':
334 		dp = save_string(dp, "%i");
335 		break;
336 	    case '6':
337 	    case 'B':
338 		getparm(param, 1);
339 		dp = save_string(dp, "%{10}%/%{16}%*");
340 		getparm(param, 1);
341 		dp = save_string(dp, "%{10}%m%+");
342 		break;
343 	    case '8':
344 	    case 'D':
345 		getparm(param, 2);
346 		dp = save_string(dp, "%{2}%*%-");
347 		break;
348 	    case '>':
349 		getparm(param, 2);
350 		/* %?%{x}%>%t%{y}%+%; */
351 		dp = save_string(dp, "%?");
352 		s += cvtchar(s);
353 		dp = save_string(dp, "%>%t");
354 		s += cvtchar(s);
355 		dp = save_string(dp, "%+%;");
356 		break;
357 	    case 'a':
358 		if ((*s == '=' || *s == '+' || *s == '-'
359 		     || *s == '*' || *s == '/')
360 		    && (s[1] == 'p' || s[1] == 'c')
361 		    && s[2] != '\0') {
362 		    int l;
363 		    l = 2;
364 		    if (*s != '=')
365 			getparm(param, 1);
366 		    if (s[1] == 'p') {
367 			getparm(param + s[2] - '@', 1);
368 			if (param != onstack) {
369 			    pop();
370 			    param--;
371 			}
372 			l++;
373 		    } else
374 			l += cvtchar(s + 2);
375 		    switch (*s) {
376 		    case '+':
377 			dp = save_string(dp, "%+");
378 			break;
379 		    case '-':
380 			dp = save_string(dp, "%-");
381 			break;
382 		    case '*':
383 			dp = save_string(dp, "%*");
384 			break;
385 		    case '/':
386 			dp = save_string(dp, "%/");
387 			break;
388 		    case '=':
389 			if (seenr) {
390 			    if (param == 1)
391 				onstack = 2;
392 			    else if (param == 2)
393 				onstack = 1;
394 			    else
395 				onstack = param;
396 			} else
397 			    onstack = param;
398 			break;
399 		    }
400 		    s += l;
401 		    break;
402 		}
403 		getparm(param, 1);
404 		s += cvtchar(s);
405 		dp = save_string(dp, "%+");
406 		break;
407 	    case '+':
408 		getparm(param, 1);
409 		s += cvtchar(s);
410 		dp = save_string(dp, "%+%c");
411 		pop();
412 		break;
413 	    case 's':
414 #ifdef WATERLOO
415 		s += cvtchar(s);
416 		getparm(param, 1);
417 		dp = save_string(dp, "%-");
418 #else
419 		getparm(param, 1);
420 		dp = save_string(dp, "%s");
421 		pop();
422 #endif /* WATERLOO */
423 		break;
424 	    case '-':
425 		s += cvtchar(s);
426 		getparm(param, 1);
427 		dp = save_string(dp, "%-%c");
428 		pop();
429 		break;
430 	    case '.':
431 		getparm(param, 1);
432 		dp = save_string(dp, "%c");
433 		pop();
434 		break;
435 	    case '0':		/* not clear any of the historical termcaps did this */
436 		if (*s == '3')
437 		    goto see03;
438 		else if (*s != '2')
439 		    goto invalid;
440 		/* FALLTHRU */
441 	    case '2':
442 		getparm(param, 1);
443 		dp = save_string(dp, "%2d");
444 		pop();
445 		break;
446 	    case '3':
447 	      see03:
448 		getparm(param, 1);
449 		dp = save_string(dp, "%3d");
450 		pop();
451 		break;
452 	    case 'd':
453 		getparm(param, 1);
454 		dp = save_string(dp, "%d");
455 		pop();
456 		break;
457 	    case 'f':
458 		param++;
459 		break;
460 	    case 'b':
461 		param--;
462 		break;
463 	    case '\\':
464 		dp = save_string(dp, "%\\");
465 		break;
466 	    default:
467 	      invalid:
468 		dp = save_char(dp, '%');
469 		s--;
470 		_nc_warning("unknown %% code %s (%#x) in %s",
471 			    unctrl((chtype) * s), CharOf(*s), cap);
472 		break;
473 	    }
474 	    break;
475 #ifdef REVISIBILIZE
476 	case '\\':
477 	    dp = save_char(dp, *s++);
478 	    dp = save_char(dp, *s++);
479 	    break;
480 	case '\n':
481 	    dp = save_string(dp, "\\n");
482 	    s++;
483 	    break;
484 	case '\t':
485 	    dp = save_string(dp, "\\t");
486 	    s++;
487 	    break;
488 	case '\r':
489 	    dp = save_string(dp, "\\r");
490 	    s++;
491 	    break;
492 	case '\200':
493 	    dp = save_string(dp, "\\0");
494 	    s++;
495 	    break;
496 	case '\f':
497 	    dp = save_string(dp, "\\f");
498 	    s++;
499 	    break;
500 	case '\b':
501 	    dp = save_string(dp, "\\b");
502 	    s++;
503 	    break;
504 	case ' ':
505 	    dp = save_string(dp, "\\s");
506 	    s++;
507 	    break;
508 	case '^':
509 	    dp = save_string(dp, "\\^");
510 	    s++;
511 	    break;
512 	case ':':
513 	    dp = save_string(dp, "\\:");
514 	    s++;
515 	    break;
516 	case ',':
517 	    dp = save_string(dp, "\\,");
518 	    s++;
519 	    break;
520 	default:
521 	    if (*s == '\033') {
522 		dp = save_string(dp, "\\E");
523 		s++;
524 	    } else if (*s > 0 && *s < 32) {
525 		dp = save_char(dp, '^');
526 		dp = save_char(dp, *s + '@');
527 		s++;
528 	    } else if (*s <= 0 || *s >= 127) {
529 		dp = save_char(dp, '\\');
530 		dp = save_char(dp, ((*s & 0300) >> 6) + '0');
531 		dp = save_char(dp, ((*s & 0070) >> 3) + '0');
532 		dp = save_char(dp, (*s & 0007) + '0');
533 		s++;
534 	    } else
535 		dp = save_char(dp, *s++);
536 	    break;
537 #else
538 	default:
539 	    dp = save_char(dp, *s++);
540 	    break;
541 #endif
542 	}
543     }
544 
545     /*
546      * Now, if we stripped off some leading padding, add it at the end
547      * of the string as mandatory padding.
548      */
549     if (capstart) {
550 	dp = save_string(dp, "$<");
551 	for (s = capstart;; s++)
552 	    if (isdigit(CharOf(*s)) || *s == '*' || *s == '.')
553 		dp = save_char(dp, *s);
554 	    else
555 		break;
556 	dp = save_string(dp, "/>");
557     }
558 
559     (void) save_char(dp, '\0');
560     return (my_string);
561 }
562 
563 /*
564  * Check for an expression that corresponds to "%B" (BCD):
565  *	(parameter / 10) * 16 + (parameter % 10)
566  */
567 static int
568 bcd_expression(const char *str)
569 {
570     /* leave this non-const for HPUX */
571     static char fmt[] = "%%p%c%%{10}%%/%%{16}%%*%%p%c%%{10}%%m%%+";
572     int len = 0;
573     char ch1, ch2;
574 
575     if (sscanf(str, fmt, &ch1, &ch2) == 2
576 	&& isdigit(CharOf(ch1))
577 	&& isdigit(CharOf(ch2))
578 	&& (ch1 == ch2)) {
579 	len = 28;
580 #ifndef NDEBUG
581 	{
582 	    char buffer[80];
583 	    int tst;
584 	    snprintf(buffer, sizeof(buffer), fmt, ch1, ch2);
585 	    tst = strlen(buffer) - 1;
586 	    assert(len == tst);
587 	}
588 #endif
589     }
590     return len;
591 }
592 
593 static char *
594 save_tc_char(char *bufptr, int c1)
595 {
596     char temp[80];
597 
598     if (is7bits(c1) && isprint(c1)) {
599 	if (c1 == ':' || c1 == '\\')
600 	    bufptr = save_char(bufptr, '\\');
601 	bufptr = save_char(bufptr, c1);
602     } else {
603 	if (c1 == (c1 & 0x1f))	/* iscntrl() returns T on 255 */
604 	    (void) strlcpy(temp, unctrl((chtype) c1), sizeof(temp));
605 	else
606 	    (void) snprintf(temp, sizeof(temp), "\\%03o", c1);
607 	bufptr = save_string(bufptr, temp);
608     }
609     return bufptr;
610 }
611 
612 static char *
613 save_tc_inequality(char *bufptr, int c1, int c2)
614 {
615     bufptr = save_string(bufptr, "%>");
616     bufptr = save_tc_char(bufptr, c1);
617     bufptr = save_tc_char(bufptr, c2);
618     return bufptr;
619 }
620 
621 /*
622  * Here are the capabilities infotocap assumes it can translate to:
623  *
624  *     %%       output `%'
625  *     %d       output value as in printf %d
626  *     %2       output value as in printf %2d
627  *     %3       output value as in printf %3d
628  *     %.       output value as in printf %c
629  *     %+c      add character c to value, then do %.
630  *     %>xy     if value > x then add y, no output
631  *     %r       reverse order of two parameters, no output
632  *     %i       increment by one, no output
633  *     %n       exclusive-or all parameters with 0140 (Datamedia 2500)
634  *     %B       BCD (16*(value/10)) + (value%10), no output
635  *     %D       Reverse coding (value - 2*(value%16)), no output (Delta Data).
636  *     %m       exclusive-or all parameters with 0177 (not in 4.4BSD)
637  */
638 
639 /*
640  * Convert a terminfo string to termcap format.  Parameters are as in
641  * _nc_captoinfo().
642  */
643 char *
644 _nc_infotocap(const char *cap GCC_UNUSED, const char *str, int const parametrized)
645 {
646     int seenone = 0, seentwo = 0, saw_m = 0, saw_n = 0;
647     const char *padding;
648     const char *trimmed = 0;
649     char ch1 = 0, ch2 = 0;
650     char *bufptr = init_string();
651     int len;
652     bool syntax_error = FALSE;
653 
654     /* we may have to move some trailing mandatory padding up front */
655     padding = str + strlen(str) - 1;
656     if (padding > str && *padding == '>' && *--padding == '/') {
657 	--padding;
658 	while (isdigit(CharOf(*padding)) || *padding == '.' || *padding == '*')
659 	    padding--;
660 	if (padding > str && *padding == '<' && *--padding == '$')
661 	    trimmed = padding;
662 	padding += 2;
663 
664 	while (isdigit(CharOf(*padding)) || *padding == '.' || *padding == '*')
665 	    bufptr = save_char(bufptr, *padding++);
666     }
667 
668     for (; *str && str != trimmed; str++) {
669 	int c1, c2;
670 	char *cp = 0;
671 
672 	if (str[0] == '\\' && (str[1] == '^' || str[1] == ',')) {
673 	    bufptr = save_char(bufptr, *++str);
674 	} else if (str[0] == '$' && str[1] == '<') {	/* discard padding */
675 	    str += 2;
676 	    while (isdigit(CharOf(*str))
677 		   || *str == '.'
678 		   || *str == '*'
679 		   || *str == '/'
680 		   || *str == '>')
681 		str++;
682 	    --str;
683 	} else if (str[0] == '%' && str[1] == '%') {	/* escaped '%' */
684 	    bufptr = save_string(bufptr, "%%");
685 	} else if (*str != '%' || (parametrized < 1)) {
686 	    bufptr = save_char(bufptr, *str);
687 	} else if (sscanf(str, "%%?%%{%d}%%>%%t%%{%d}%%+%%;", &c1, &c2) == 2) {
688 	    str = strchr(str, ';');
689 	    bufptr = save_tc_inequality(bufptr, c1, c2);
690 	} else if (sscanf(str, "%%?%%{%d}%%>%%t%%'%c'%%+%%;", &c1, &ch2) == 2) {
691 	    str = strchr(str, ';');
692 	    bufptr = save_tc_inequality(bufptr, c1, c2);
693 	} else if (sscanf(str, "%%?%%'%c'%%>%%t%%{%d}%%+%%;", &ch1, &c2) == 2) {
694 	    str = strchr(str, ';');
695 	    bufptr = save_tc_inequality(bufptr, c1, c2);
696 	} else if (sscanf(str, "%%?%%'%c'%%>%%t%%'%c'%%+%%;", &ch1, &ch2) == 2) {
697 	    str = strchr(str, ';');
698 	    bufptr = save_tc_inequality(bufptr, c1, c2);
699 	} else if ((len = bcd_expression(str)) != 0) {
700 	    str += len;
701 	    bufptr = save_string(bufptr, "%B");
702 	} else if ((sscanf(str, "%%{%d}%%+%%c", &c1) == 1
703 		    || sscanf(str, "%%'%c'%%+%%c", &ch1) == 1)
704 		   && (cp = strchr(str, '+'))) {
705 	    str = cp + 2;
706 	    bufptr = save_string(bufptr, "%+");
707 
708 	    if (ch1)
709 		c1 = ch1;
710 	    bufptr = save_tc_char(bufptr, c1);
711 	}
712 	/* FIXME: this "works" for 'delta' */
713 	else if (strncmp(str, "%{2}%*%-", 8) == 0) {
714 	    str += 7;
715 	    bufptr = save_string(bufptr, "%D");
716 	} else if (strncmp(str, "%{96}%^", 7) == 0) {
717 	    str += 6;
718 	    if (saw_m++ == 0) {
719 		bufptr = save_string(bufptr, "%n");
720 	    }
721 	} else if (strncmp(str, "%{127}%^", 8) == 0) {
722 	    str += 7;
723 	    if (saw_n++ == 0) {
724 		bufptr = save_string(bufptr, "%m");
725 	    }
726 	} else {		/* cm-style format element */
727 	    str++;
728 	    switch (*str) {
729 	    case '%':
730 		bufptr = save_char(bufptr, '%');
731 		break;
732 
733 	    case '0':
734 	    case '1':
735 	    case '2':
736 	    case '3':
737 	    case '4':
738 	    case '5':
739 	    case '6':
740 	    case '7':
741 	    case '8':
742 	    case '9':
743 		bufptr = save_char(bufptr, '%');
744 		while (isdigit(CharOf(*str)))
745 		    bufptr = save_char(bufptr, *str++);
746 		if (strchr("doxX.", *str)) {
747 		    if (*str != 'd')	/* termcap doesn't have octal, hex */
748 			return 0;
749 		}
750 		break;
751 
752 	    case 'd':
753 		bufptr = save_string(bufptr, "%d");
754 		break;
755 
756 	    case 'c':
757 		bufptr = save_string(bufptr, "%.");
758 		break;
759 
760 		/*
761 		 * %s isn't in termcap, but it's convenient to pass it through
762 		 * so we can represent things like terminfo pfkey strings in
763 		 * termcap notation.
764 		 */
765 	    case 's':
766 		bufptr = save_string(bufptr, "%s");
767 		break;
768 
769 	    case 'p':
770 		str++;
771 		if (*str == '1')
772 		    seenone = 1;
773 		else if (*str == '2') {
774 		    if (!seenone && !seentwo) {
775 			bufptr = save_string(bufptr, "%r");
776 			seentwo++;
777 		    }
778 		} else if (*str >= '3')
779 		    return (0);
780 		break;
781 
782 	    case 'i':
783 		bufptr = save_string(bufptr, "%i");
784 		break;
785 
786 	    default:
787 		bufptr = save_char(bufptr, *str);
788 		syntax_error = TRUE;
789 		break;
790 	    }			/* endswitch (*str) */
791 	}			/* endelse (*str == '%') */
792 
793 	if (*str == '\0')
794 	    break;
795 
796     }				/* endwhile (*str) */
797 
798     return (syntax_error ? NULL : my_string);
799 }
800 
801 #ifdef MAIN
802 
803 int curr_line;
804 
805 int
806 main(int argc, char *argv[])
807 {
808     int c, tc = FALSE;
809 
810     while ((c = getopt(argc, argv, "c")) != -1)
811 	switch (c) {
812 	case 'c':
813 	    tc = TRUE;
814 	    break;
815 	}
816 
817     curr_line = 0;
818     for (;;) {
819 	char buf[BUFSIZ];
820 	size_t buflen;
821 
822 	++curr_line;
823 	if (fgets(buf, sizeof(buf), stdin) == NULL)
824 	    break;
825 	buflen = strlen(buf);
826 	if (buflen > 0 && buf[buflen - 1] == '\n')
827 		buf[buflen - 1] = '\0';
828 	_nc_set_source(buf);
829 
830 	if (tc) {
831 	    char *cp = _nc_infotocap("to termcap", buf, 1);
832 
833 	    if (cp)
834 		(void) fputs(cp, stdout);
835 	} else
836 	    (void) fputs(_nc_captoinfo("to terminfo", buf, 1), stdout);
837 	(void) putchar('\n');
838     }
839     return (0);
840 }
841 #endif /* MAIN */
842 
843 /* captoinfo.c ends here */
844