1 /****************************************************************************
2 * Copyright (c) 1998-2011,2012 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
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 ****************************************************************************/
36 * captoinfo.c --- conversion between termcap and terminfo formats
38 * The captoinfo() code was swiped from Ross Ridge's mytinfo package,
39 * adapted to fit ncurses by Eric S. Raymond <esr@snark.thyrsus.com>.
41 * There is just one entry point:
43 * char *_nc_captoinfo(n, s, parameterized)
45 * Convert value s for termcap string capability named n into terminfo
48 * This code recognizes all the standard 4.4BSD %-escapes:
51 * %d output value as in printf %d
52 * %2 output value as in printf %2d
53 * %3 output value as in printf %3d
54 * %. output value as in printf %c
55 * %+x add x to value, then do %.
56 * %>xy if value > x then add y, no output
57 * %r reverse order of two parameters, no output
58 * %i increment by one, no output
59 * %n exclusive-or all parameters with 0140 (Datamedia 2500)
60 * %B BCD (16*(value/10)) + (value%10), no output
61 * %D Reverse coding (value - 2*(value%16)), no output (Delta Data).
63 * Also, %02 and %03 are accepted as synonyms for %2 and %3.
65 * Besides all the standard termcap escapes, this translator understands
66 * the following extended escapes:
68 * used by GNU Emacs termcap libraries
69 * %a[+*-/=][cp]x GNU arithmetic.
70 * %m xor the first two parameters by 0177
71 * %b backup to previous parameter
72 * %f skip this parameter
74 * used by the University of Waterloo (MFCF) termcap libraries
75 * %-x subtract parameter FROM char x and output it as a char
76 * %ax add the character x to parameter
78 * If #define WATERLOO is on, also enable these translations:
80 * %sx subtract parameter FROM the character x
82 * By default, this Waterloo translations are not compiled in, because
83 * the Waterloo %s conflicts with the way terminfo uses %s in strings for
84 * function programming.
86 * Note the two definitions of %a: the GNU definition is translated if the
87 * characters after the 'a' are valid for it, otherwise the UW definition
91 #include <curses.priv.h>
96 MODULE_ID("$Id: captoinfo.c,v 1.77 2012/12/30 00:50:40 tom Exp $")
98 #define MAX_PUSHED 16 /* max # args we can push onto the stack */
100 static int stack[MAX_PUSHED]; /* the stack */
101 static int stackptr; /* the next empty place on the stack */
102 static int onstack; /* the top of stack */
103 static int seenm; /* seen a %m */
104 static int seenn; /* seen a %n */
105 static int seenr; /* seen a %r */
106 static int param; /* current parameter */
107 static char *dp; /* pointer to end of the converted string */
109 static char *my_string;
110 static size_t my_length;
114 /* initialize 'my_string', 'my_length' */
117 TYPE_MALLOC(char, my_length = 256, my_string);
124 save_string(char *d, const char *const s)
126 size_t have = (size_t) (d - my_string);
127 size_t need = have + strlen(s) + 2;
128 if (need > my_length) {
129 my_string = (char *) _nc_doalloc(my_string, my_length = (need + need));
131 _nc_err_abort(MSG_NO_MEMORY);
132 d = my_string + have;
134 _nc_STRCPY(d, s, my_length - have);
135 return d + strlen(d);
138 static NCURSES_INLINE char *
139 save_char(char *s, int c)
143 return save_string(s, temp);
148 /* push onstack on to the stack */
150 if (stackptr >= MAX_PUSHED)
151 _nc_warning("string too complex to convert");
153 stack[stackptr++] = onstack;
158 /* pop the top of the stack into onstack */
162 _nc_warning("I'm confused");
166 onstack = stack[--stackptr];
171 cvtchar(register const char *sp)
172 /* convert a character to a terminfo push */
184 c = (unsigned char) (*sp);
196 while (isdigit(UChar(*sp))) {
197 c = (unsigned char) (8 * c + (*sp++ - '0'));
202 c = (unsigned char) (*sp);
208 c = (unsigned char) (*++sp & 0x1f);
212 c = (unsigned char) (*sp);
215 if (isgraph(c) && c != ',' && c != '\'' && c != '\\' && c != ':') {
216 dp = save_string(dp, "%\'");
217 dp = save_char(dp, c);
218 dp = save_char(dp, '\'');
220 dp = save_string(dp, "%{");
222 dp = save_char(dp, c / 100 + '0');
224 dp = save_char(dp, ((int) (c / 10)) % 10 + '0');
225 dp = save_char(dp, c % 10 + '0');
226 dp = save_char(dp, '}');
232 getparm(int parm, int n)
233 /* push n copies of param on the terminfo stack if not already there */
243 dp = save_string(dp, "%p");
244 dp = save_char(dp, '0' + parm);
247 if (onstack == parm) {
249 _nc_warning("string may not be optimal");
250 dp = save_string(dp, "%Pa");
252 dp = save_string(dp, "%ga");
262 if (seenn && parm < 3) {
263 dp = save_string(dp, "%{96}%^");
266 if (seenm && parm < 3) {
267 dp = save_string(dp, "%{127}%^");
272 * Convert a termcap string to terminfo format.
273 * 'cap' is the relevant terminfo capability index.
274 * 's' is the string value of the capability.
275 * 'parameterized' tells what type of translations to do:
276 * % translations if 1
277 * pad translations if >=0
279 NCURSES_EXPORT(char *)
280 _nc_captoinfo(const char *cap, const char *s, int const parameterized)
282 const char *capstart;
293 /* skip the initial padding (if we haven't been told not to) */
297 if (parameterized >= 0 && isdigit(UChar(*s)))
298 for (capstart = s;; s++)
299 if (!(isdigit(UChar(*s)) || *s == '*' || *s == '.'))
306 if (parameterized < 1) {
307 dp = save_char(dp, '%');
312 dp = save_char(dp, '%');
316 _nc_warning("saw %%r twice in %s", cap);
321 _nc_warning("saw %%m twice in %s", cap);
326 _nc_warning("saw %%n twice in %s", cap);
330 dp = save_string(dp, "%i");
335 dp = save_string(dp, "%{10}%/%{16}%*");
337 dp = save_string(dp, "%{10}%m%+");
342 dp = save_string(dp, "%{2}%*%-");
346 /* %?%{x}%>%t%{y}%+%; */
347 dp = save_string(dp, "%?");
349 dp = save_string(dp, "%>%t");
351 dp = save_string(dp, "%+%;");
354 if ((*s == '=' || *s == '+' || *s == '-'
355 || *s == '*' || *s == '/')
356 && (s[1] == 'p' || s[1] == 'c')
363 getparm(param + s[2] - '@', 1);
364 if (param != onstack) {
373 dp = save_string(dp, "%+");
376 dp = save_string(dp, "%-");
379 dp = save_string(dp, "%*");
382 dp = save_string(dp, "%/");
401 dp = save_string(dp, "%+");
406 dp = save_string(dp, "%+%c");
413 dp = save_string(dp, "%-");
416 dp = save_string(dp, "%s");
418 #endif /* WATERLOO */
423 dp = save_string(dp, "%-%c");
428 dp = save_string(dp, "%c");
431 case '0': /* not clear any of the historical termcaps did this */
439 dp = save_string(dp, "%2d");
445 dp = save_string(dp, "%3d");
450 dp = save_string(dp, "%d");
460 dp = save_string(dp, "%\\");
464 dp = save_char(dp, '%');
466 _nc_warning("unknown %% code %s (%#x) in %s",
467 unctrl((chtype) *s), UChar(*s), cap);
472 dp = save_char(dp, *s++);
478 * Now, if we stripped off some leading padding, add it at the end
479 * of the string as mandatory padding.
482 dp = save_string(dp, "$<");
483 for (s = capstart;; s++)
484 if (isdigit(UChar(*s)) || *s == '*' || *s == '.')
485 dp = save_char(dp, *s);
488 dp = save_string(dp, "/>");
491 (void) save_char(dp, '\0');
496 * Check for an expression that corresponds to "%B" (BCD):
497 * (parameter / 10) * 16 + (parameter % 10)
500 bcd_expression(const char *str)
502 /* leave this non-const for HPUX */
503 static char fmt[] = "%%p%c%%{10}%%/%%{16}%%*%%p%c%%{10}%%m%%+";
507 if (sscanf(str, fmt, &ch1, &ch2) == 2
508 && isdigit(UChar(ch1))
509 && isdigit(UChar(ch2))
516 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer)) fmt, ch1, ch2);
517 tst = strlen(buffer) - 1;
526 save_tc_char(char *bufptr, int c1)
530 if (is7bits(c1) && isprint(c1)) {
531 if (c1 == ':' || c1 == '\\')
532 bufptr = save_char(bufptr, '\\');
533 bufptr = save_char(bufptr, c1);
535 if (c1 == (c1 & 0x1f)) { /* iscntrl() returns T on 255 */
536 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
537 "%.20s", unctrl((chtype) c1));
539 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
542 bufptr = save_string(bufptr, temp);
548 save_tc_inequality(char *bufptr, int c1, int c2)
550 bufptr = save_string(bufptr, "%>");
551 bufptr = save_tc_char(bufptr, c1);
552 bufptr = save_tc_char(bufptr, c2);
557 * Here are the capabilities infotocap assumes it can translate to:
560 * %d output value as in printf %d
561 * %2 output value as in printf %2d
562 * %3 output value as in printf %3d
563 * %. output value as in printf %c
564 * %+c add character c to value, then do %.
565 * %>xy if value > x then add y, no output
566 * %r reverse order of two parameters, no output
567 * %i increment by one, no output
568 * %n exclusive-or all parameters with 0140 (Datamedia 2500)
569 * %B BCD (16*(value/10)) + (value%10), no output
570 * %D Reverse coding (value - 2*(value%16)), no output (Delta Data).
571 * %m exclusive-or all parameters with 0177 (not in 4.4BSD)
575 * Convert a terminfo string to termcap format. Parameters are as in
578 NCURSES_EXPORT(char *)
579 _nc_infotocap(const char *cap GCC_UNUSED, const char *str, int const parameterized)
581 int seenone = 0, seentwo = 0, saw_m = 0, saw_n = 0;
583 const char *trimmed = 0;
585 char ch1 = 0, ch2 = 0;
586 char *bufptr = init_string();
589 bool syntax_error = FALSE;
591 /* we may have to move some trailing mandatory padding up front */
592 padding = str + strlen(str) - 1;
593 if (padding > str && *padding == '>') {
594 if (*--padding == '/')
596 while (isdigit(UChar(*padding)) || *padding == '.' || *padding == '*')
598 if (padding > str && *padding == '<' && *--padding == '$')
602 while (isdigit(UChar(*padding)) || *padding == '.' || *padding == '*')
603 bufptr = save_char(bufptr, *padding++);
606 for (; *str && ((trimmed == 0) || (str < trimmed)); str++) {
611 if (str[1] == '\0' || (str + 1) == trimmed) {
612 bufptr = save_string(bufptr, "\\136");
615 bufptr = save_char(bufptr, *str++);
616 bufptr = save_char(bufptr, *str);
618 } else if (str[0] == '\\') {
619 if (str[1] == '\0' || (str + 1) == trimmed) {
620 bufptr = save_string(bufptr, "\\134");
622 } else if (str[1] == '^') {
623 bufptr = save_string(bufptr, "\\136");
625 } else if (str[1] == ',') {
626 bufptr = save_char(bufptr, *++str);
630 bufptr = save_char(bufptr, *str++);
632 if (_nc_strict_bsd) {
633 if (isdigit(UChar(xx1))) {
636 if (!isdigit(UChar(str[1])))
638 else if (str[1] && !isdigit(UChar(str[2])))
642 * Test for "\0", "\00" or "\000" and transform those
646 && ((pad == 2) || (str[1] == '0'))
647 && ((pad >= 1) || (str[2] == '0'))) {
651 pad = 0; /* FIXME - optionally pad to 3 digits */
654 bufptr = save_char(bufptr, xx2);
657 } else if (strchr("E\\nrtbf", xx1) == 0) {
666 bufptr = save_char(bufptr, '0');
667 bufptr = save_char(bufptr, '4');
672 * Note: termcap documentation claims that ":"
673 * must be escaped as "\072", however the
674 * documentation is incorrect - read the code.
675 * The replacement does not work reliably,
676 * so the advice is not helpful.
678 bufptr = save_char(bufptr, '0');
679 bufptr = save_char(bufptr, '7');
683 /* should not happen, but handle this anyway */
684 _nc_SPRINTF(octal, _nc_SLIMIT(sizeof(octal))
686 bufptr = save_char(bufptr, octal[0]);
687 bufptr = save_char(bufptr, octal[1]);
693 bufptr = save_char(bufptr, xx1);
695 } else if (str[0] == '$' && str[1] == '<') { /* discard padding */
697 while (isdigit(UChar(*str))
704 } else if (sscanf(str,
705 "[%%?%%p1%%{8}%%<%%t%d%%p1%%d%%e%%p1%%{16}%%<%%t%d%%p1%%{8}%%-%%d%%e%d;5;%%p1%%d%%;m",
706 &in0, &in1, &in2) == 3
707 && ((in0 == 4 && in1 == 10 && in2 == 48)
708 || (in0 == 3 && in1 == 9 && in2 == 38))) {
709 /* dumb-down an optimized case from xterm-256color for termcap */
710 if ((str = strstr(str, ";m")) == 0)
711 break; /* cannot happen */
714 bufptr = save_string(bufptr, "[48;5;%dm");
716 bufptr = save_string(bufptr, "[38;5;%dm");
718 } else if (str[0] == '%' && str[1] == '%') { /* escaped '%' */
719 bufptr = save_string(bufptr, "%%");
721 } else if (*str != '%' || (parameterized < 1)) {
722 bufptr = save_char(bufptr, *str);
723 } else if (sscanf(str, "%%?%%{%d}%%>%%t%%{%d}%%+%%;", &c1, &c2) == 2) {
724 str = strchr(str, ';');
725 bufptr = save_tc_inequality(bufptr, c1, c2);
726 } else if (sscanf(str, "%%?%%{%d}%%>%%t%%'%c'%%+%%;", &c1, &ch2) == 2) {
727 str = strchr(str, ';');
728 bufptr = save_tc_inequality(bufptr, c1, ch2);
729 } else if (sscanf(str, "%%?%%'%c'%%>%%t%%{%d}%%+%%;", &ch1, &c2) == 2) {
730 str = strchr(str, ';');
731 bufptr = save_tc_inequality(bufptr, ch1, c2);
732 } else if (sscanf(str, "%%?%%'%c'%%>%%t%%'%c'%%+%%;", &ch1, &ch2) == 2) {
733 str = strchr(str, ';');
734 bufptr = save_tc_inequality(bufptr, ch1, ch2);
735 } else if ((len = bcd_expression(str)) != 0) {
737 bufptr = save_string(bufptr, "%B");
738 } else if ((sscanf(str, "%%{%d}%%+%%c", &c1) == 1
739 || sscanf(str, "%%'%c'%%+%%c", &ch1) == 1)
740 && (cp = strchr(str, '+'))) {
742 bufptr = save_string(bufptr, "%+");
746 bufptr = save_tc_char(bufptr, c1);
748 /* FIXME: this "works" for 'delta' */
749 else if (strncmp(str, "%{2}%*%-", (size_t) 8) == 0) {
751 bufptr = save_string(bufptr, "%D");
752 } else if (strncmp(str, "%{96}%^", (size_t) 7) == 0) {
755 bufptr = save_string(bufptr, "%n");
757 } else if (strncmp(str, "%{127}%^", (size_t) 8) == 0) {
760 bufptr = save_string(bufptr, "%m");
762 } else { /* cm-style format element */
766 bufptr = save_char(bufptr, '%');
779 bufptr = save_char(bufptr, '%');
782 while (isdigit(UChar(*str))) {
785 if (_nc_strict_bsd) {
789 bufptr = save_char(bufptr, ch1);
792 if (_nc_strict_bsd) {
793 if (ch2 != 0 && ch2 != '0')
797 bufptr = save_char(bufptr, ch1);
799 if (strchr("doxX.", *str)) {
800 if (*str != 'd') /* termcap doesn't have octal, hex */
806 bufptr = save_string(bufptr, "%d");
810 bufptr = save_string(bufptr, "%.");
814 * %s isn't in termcap, but it's convenient to pass it through
815 * so we can represent things like terminfo pfkey strings in
821 bufptr = save_string(bufptr, "%s");
828 else if (*str == '2') {
829 if (!seenone && !seentwo) {
830 bufptr = save_string(bufptr, "%r");
833 } else if (*str >= '3')
838 bufptr = save_string(bufptr, "%i");
842 bufptr = save_char(bufptr, *str);
845 } /* endswitch (*str) */
846 } /* endelse (*str == '%') */
849 * 'str' always points to the end of what was scanned in this step,
850 * but that may not be the end of the string.
853 if (str == 0 || *str == '\0')
856 } /* endwhile (*str) */
858 return (syntax_error ? NULL : my_string);
866 main(int argc, char *argv[])
870 while ((c = getopt(argc, argv, "c")) != EOF)
882 if (fgets(buf, sizeof(buf), stdin) == 0)
884 buf[strlen(buf) - 1] = '\0';
888 char *cp = _nc_infotocap("to termcap", buf, 1);
891 (void) fputs(cp, stdout);
893 (void) fputs(_nc_captoinfo("to terminfo", buf, 1), stdout);
894 (void) putchar('\n');
902 _nc_captoinfo_leaks(void)
904 if (my_string != 0) {
905 FreeAndNull(my_string);