1 /****************************************************************************
2 * Copyright (c) 1998-2016,2017 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 ****************************************************************************/
38 * Provide conversion in both directions between termcap and terminfo.
40 * cap-to-info --- conversion between termcap and terminfo formats
42 * The captoinfo() code was swiped from Ross Ridge's mytinfo package,
43 * adapted to fit ncurses by Eric S. Raymond <esr@snark.thyrsus.com>.
45 * It has just one entry point:
47 * char *_nc_captoinfo(n, s, parameterized)
49 * Convert value s for termcap string capability named n into terminfo
52 * This code recognizes all the standard 4.4BSD %-escapes:
55 * %d output value as in printf %d
56 * %2 output value as in printf %2d
57 * %3 output value as in printf %3d
58 * %. output value as in printf %c
59 * %+x add x to value, then do %.
60 * %>xy if value > x then add y, no output
61 * %r reverse order of two parameters, no output
62 * %i increment by one, no output
63 * %n exclusive-or all parameters with 0140 (Datamedia 2500)
64 * %B BCD (16*(value/10)) + (value%10), no output
65 * %D Reverse coding (value - 2*(value%16)), no output (Delta Data).
67 * Also, %02 and %03 are accepted as synonyms for %2 and %3.
69 * Besides all the standard termcap escapes, this translator understands
70 * the following extended escapes:
72 * used by GNU Emacs termcap libraries
73 * %a[+*-/=][cp]x GNU arithmetic.
74 * %m xor the first two parameters by 0177
75 * %b backup to previous parameter
76 * %f skip this parameter
78 * used by the University of Waterloo (MFCF) termcap libraries
79 * %-x subtract parameter FROM char x and output it as a char
80 * %ax add the character x to parameter
82 * If #define WATERLOO is on, also enable these translations:
84 * %sx subtract parameter FROM the character x
86 * By default, this Waterloo translations are not compiled in, because
87 * the Waterloo %s conflicts with the way terminfo uses %s in strings for
88 * function programming.
90 * Note the two definitions of %a: the GNU definition is translated if the
91 * characters after the 'a' are valid for it, otherwise the UW definition
95 #include <curses.priv.h>
100 MODULE_ID("$Id: captoinfo.c,v 1.94 2017/04/21 23:26:38 tom Exp $")
103 #define DEBUG_THIS(p) DEBUG(9, p)
105 #define DEBUG_THIS(p) /* nothing */
108 #define MAX_PUSHED 16 /* max # args we can push onto the stack */
110 static int stack[MAX_PUSHED]; /* the stack */
111 static int stackptr; /* the next empty place on the stack */
112 static int onstack; /* the top of stack */
113 static int seenm; /* seen a %m */
114 static int seenn; /* seen a %n */
115 static int seenr; /* seen a %r */
116 static int param; /* current parameter */
117 static char *dp; /* pointer to end of the converted string */
119 static char *my_string;
120 static size_t my_length;
124 /* initialize 'my_string', 'my_length' */
127 TYPE_MALLOC(char, my_length = 256, my_string);
134 save_string(char *d, const char *const s)
136 size_t have = (size_t) (d - my_string);
137 size_t need = have + strlen(s) + 2;
138 if (need > my_length) {
139 my_string = (char *) _nc_doalloc(my_string, my_length = (need + need));
141 _nc_err_abort(MSG_NO_MEMORY);
142 d = my_string + have;
144 _nc_STRCPY(d, s, my_length - have);
145 return d + strlen(d);
148 static NCURSES_INLINE char *
149 save_char(char *s, int c)
153 return save_string(s, temp);
158 /* push onstack on to the stack */
160 if (stackptr >= MAX_PUSHED)
161 _nc_warning("string too complex to convert");
163 stack[stackptr++] = onstack;
168 /* pop the top of the stack into onstack */
172 _nc_warning("I'm confused");
176 onstack = stack[--stackptr];
181 cvtchar(register const char *sp)
182 /* convert a character to a terminfo push */
206 while (isdigit(UChar(*sp))) {
207 c = UChar(8 * c + (*sp++ - '0'));
229 if (isgraph(c) && c != ',' && c != '\'' && c != '\\' && c != ':') {
230 dp = save_string(dp, "%\'");
231 dp = save_char(dp, c);
232 dp = save_char(dp, '\'');
234 dp = save_string(dp, "%{");
236 dp = save_char(dp, c / 100 + '0');
238 dp = save_char(dp, ((int) (c / 10)) % 10 + '0');
239 dp = save_char(dp, c % 10 + '0');
240 dp = save_char(dp, '}');
246 getparm(int parm, int n)
247 /* push n copies of param on the terminfo stack if not already there */
257 dp = save_string(dp, "%p");
258 dp = save_char(dp, '0' + parm);
261 if (onstack == parm) {
263 _nc_warning("string may not be optimal");
264 dp = save_string(dp, "%Pa");
266 dp = save_string(dp, "%ga");
276 if (seenn && parm < 3) {
277 dp = save_string(dp, "%{96}%^");
280 if (seenm && parm < 3) {
281 dp = save_string(dp, "%{127}%^");
286 * Convert a termcap string to terminfo format.
287 * 'cap' is the relevant terminfo capability index.
288 * 's' is the string value of the capability.
289 * 'parameterized' tells what type of translations to do:
290 * % translations if 1
291 * pad translations if >=0
293 NCURSES_EXPORT(char *)
294 _nc_captoinfo(const char *cap, const char *s, int const parameterized)
296 const char *capstart;
305 DEBUG_THIS(("_nc_captoinfo params %d, %s", parameterized, s));
309 /* skip the initial padding (if we haven't been told not to) */
313 if (parameterized >= 0 && isdigit(UChar(*s)))
314 for (capstart = s;; s++)
315 if (!(isdigit(UChar(*s)) || *s == '*' || *s == '.'))
322 if (parameterized < 1) {
323 dp = save_char(dp, '%');
328 dp = save_string(dp, "%%");
332 _nc_warning("saw %%r twice in %s", cap);
337 _nc_warning("saw %%m twice in %s", cap);
342 _nc_warning("saw %%n twice in %s", cap);
346 dp = save_string(dp, "%i");
351 dp = save_string(dp, "%{10}%/%{16}%*");
353 dp = save_string(dp, "%{10}%m%+");
358 dp = save_string(dp, "%{2}%*%-");
362 /* %?%{x}%>%t%{y}%+%; */
363 dp = save_string(dp, "%?");
365 dp = save_string(dp, "%>%t");
367 dp = save_string(dp, "%+%;");
370 if ((*s == '=' || *s == '+' || *s == '-'
371 || *s == '*' || *s == '/')
372 && (s[1] == 'p' || s[1] == 'c')
379 getparm(param + s[2] - '@', 1);
380 if (param != onstack) {
389 dp = save_string(dp, "%+");
392 dp = save_string(dp, "%-");
395 dp = save_string(dp, "%*");
398 dp = save_string(dp, "%/");
417 dp = save_string(dp, "%+");
422 dp = save_string(dp, "%+%c");
429 dp = save_string(dp, "%-");
432 dp = save_string(dp, "%s");
434 #endif /* WATERLOO */
439 dp = save_string(dp, "%-%c");
444 dp = save_string(dp, "%c");
447 case '0': /* not clear any of the historical termcaps did this */
460 dp = save_string(dp, "%2d");
466 dp = save_string(dp, "%3d");
471 dp = save_string(dp, "%d");
481 dp = save_string(dp, "%\\");
485 dp = save_char(dp, '%');
487 _nc_warning("unknown %% code %s (%#x) in %s",
488 unctrl((chtype) *s), UChar(*s), cap);
493 dp = save_char(dp, *s++);
499 * Now, if we stripped off some leading padding, add it at the end
500 * of the string as mandatory padding.
503 dp = save_string(dp, "$<");
504 for (s = capstart;; s++)
505 if (isdigit(UChar(*s)) || *s == '*' || *s == '.')
506 dp = save_char(dp, *s);
509 dp = save_string(dp, "/>");
512 (void) save_char(dp, '\0');
514 DEBUG_THIS(("... _nc_captoinfo %s", NonNull(my_string)));
520 * Check for an expression that corresponds to "%B" (BCD):
521 * (parameter / 10) * 16 + (parameter % 10)
524 bcd_expression(const char *str)
526 /* leave this non-const for HPUX */
527 static char fmt[] = "%%p%c%%{10}%%/%%{16}%%*%%p%c%%{10}%%m%%+";
531 if (sscanf(str, fmt, &ch1, &ch2) == 2
532 && isdigit(UChar(ch1))
533 && isdigit(UChar(ch2))
540 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer)) fmt, ch1, ch2);
541 tst = strlen(buffer) - 1;
550 save_tc_char(char *bufptr, int c1)
552 if (is7bits(c1) && isprint(c1)) {
553 if (c1 == ':' || c1 == '\\')
554 bufptr = save_char(bufptr, '\\');
555 bufptr = save_char(bufptr, c1);
559 if (c1 == (c1 & 0x1f)) { /* iscntrl() returns T on 255 */
560 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
561 "%.20s", unctrl((chtype) c1));
563 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
566 bufptr = save_string(bufptr, temp);
572 save_tc_inequality(char *bufptr, int c1, int c2)
574 bufptr = save_string(bufptr, "%>");
575 bufptr = save_tc_char(bufptr, c1);
576 bufptr = save_tc_char(bufptr, c2);
581 * info-to-cap --- conversion between terminfo and termcap formats
583 * Here are the capabilities infotocap assumes it can translate to:
586 * %d output value as in printf %d
587 * %2 output value as in printf %2d
588 * %3 output value as in printf %3d
589 * %. output value as in printf %c
590 * %+c add character c to value, then do %.
591 * %>xy if value > x then add y, no output
592 * %r reverse order of two parameters, no output
593 * %i increment by one, no output
594 * %n exclusive-or all parameters with 0140 (Datamedia 2500)
595 * %B BCD (16*(value/10)) + (value%10), no output
596 * %D Reverse coding (value - 2*(value%16)), no output (Delta Data).
597 * %m exclusive-or all parameters with 0177 (not in 4.4BSD)
600 #define octal_fixup(n, c) fixups[n].ch = ((fixups[n].ch << 3) | ((c) - '0'))
603 * Convert a terminfo string to termcap format. Parameters are as in
606 NCURSES_EXPORT(char *)
607 _nc_infotocap(const char *cap GCC_UNUSED, const char *str, int const parameterized)
609 int seenone = 0, seentwo = 0, saw_m = 0, saw_n = 0;
611 const char *trimmed = 0;
613 char ch1 = 0, ch2 = 0;
614 char *bufptr = init_string();
618 bool syntax_error = FALSE;
623 } fixups[MAX_TC_FIXUPS];
625 DEBUG_THIS(("_nc_infotocap params %d, %s", parameterized, str));
627 /* we may have to move some trailing mandatory padding up front */
628 padding = str + strlen(str) - 1;
629 if (padding > str && *padding == '>') {
630 if (*--padding == '/')
632 while (isdigit(UChar(*padding)) || *padding == '.' || *padding == '*')
634 if (padding > str && *padding == '<' && *--padding == '$')
638 while (isdigit(UChar(*padding)) || *padding == '.' || *padding == '*')
639 bufptr = save_char(bufptr, *padding++);
642 for (; !syntax_error &&
644 ((trimmed == 0) || (str < trimmed)); str++) {
649 if (str[1] == '\0' || (str + 1) == trimmed) {
650 bufptr = save_string(bufptr, "\\136");
652 } else if (str[1] == '?') {
654 * Although the 4.3BSD termcap file has an instance of "kb=^?",
655 * that appears to be just cut/paste since neither 4.3BSD nor
656 * 4.4BSD termcap interprets "^?" as DEL.
658 bufptr = save_string(bufptr, "\\177");
661 bufptr = save_char(bufptr, *str++);
662 bufptr = save_char(bufptr, *str);
664 } else if (str[0] == '\\') {
665 if (str[1] == '\0' || (str + 1) == trimmed) {
666 bufptr = save_string(bufptr, "\\134");
668 } else if (str[1] == '^') {
669 bufptr = save_string(bufptr, "\\136");
671 } else if (str[1] == ',') {
672 bufptr = save_char(bufptr, *++str);
676 bufptr = save_char(bufptr, *str++);
678 if (_nc_strict_bsd) {
680 if (isoctal(UChar(xx1))) {
685 if (!isoctal(UChar(str[1])))
687 else if (str[1] && !isoctal(UChar(str[2])))
691 * Test for "\0", "\00" or "\000" and transform those
695 && ((pad == 2) || (str[1] == '0'))
696 && ((pad >= 1) || (str[2] == '0'))) {
700 pad = 0; /* FIXME - optionally pad to 3 digits */
702 if (myfix < MAX_TC_FIXUPS) {
704 fixups[myfix].ch = 0;
705 fixups[myfix].offset = (int) (bufptr
710 bufptr = save_char(bufptr, xx2);
711 if (myfix < MAX_TC_FIXUPS) {
712 fixups[myfix].ch <<= 3;
713 fixups[myfix].ch |= (xx2 - '0');
717 if (myfix < MAX_TC_FIXUPS) {
719 for (n = 0; n < fix; ++n) {
720 fixups[myfix].ch <<= 3;
721 fixups[myfix].ch |= (str[n] - '0');
723 if (fixups[myfix].ch < 32) {
727 } else if (strchr("E\\nrtbf", xx1) == 0) {
736 bufptr = save_char(bufptr, '0');
737 bufptr = save_char(bufptr, '4');
742 * Note: termcap documentation claims that ":"
743 * must be escaped as "\072", however the
744 * documentation is incorrect - read the code.
745 * The replacement does not work reliably,
746 * so the advice is not helpful.
748 bufptr = save_char(bufptr, '0');
749 bufptr = save_char(bufptr, '7');
753 /* should not happen, but handle this anyway */
754 _nc_SPRINTF(octal, _nc_SLIMIT(sizeof(octal))
756 bufptr = save_char(bufptr, octal[0]);
757 bufptr = save_char(bufptr, octal[1]);
763 if (myfix < MAX_TC_FIXUPS && isoctal(UChar(xx1))) {
764 bool will_fix = TRUE;
767 fixups[myfix].ch = 0;
768 fixups[myfix].offset = (int) (bufptr - my_string - 1);
769 for (n = 0; n < 3; ++n) {
770 if (isoctal(str[n])) {
771 octal_fixup(myfix, str[n]);
777 if (will_fix && (fixups[myfix].ch < 32))
781 bufptr = save_char(bufptr, xx1);
783 } else if (str[0] == '$' && str[1] == '<') { /* discard padding */
785 while (isdigit(UChar(*str))
792 } else if (sscanf(str,
793 "[%%?%%p1%%{8}%%<%%t%d%%p1%%d%%e%%p1%%{16}%%<%%t%d%%p1%%{8}%%-%%d%%e%d;5;%%p1%%d%%;m",
794 &in0, &in1, &in2) == 3
795 && ((in0 == 4 && in1 == 10 && in2 == 48)
796 || (in0 == 3 && in1 == 9 && in2 == 38))) {
797 /* dumb-down an optimized case from xterm-256color for termcap */
798 if ((str = strstr(str, ";m")) == 0)
799 break; /* cannot happen */
802 bufptr = save_string(bufptr, "[48;5;%dm");
804 bufptr = save_string(bufptr, "[38;5;%dm");
806 } else if (str[0] == '%' && str[1] == '%') { /* escaped '%' */
807 bufptr = save_string(bufptr, "%%");
809 } else if (*str != '%' || (parameterized < 1)) {
810 bufptr = save_char(bufptr, *str);
811 } else if (sscanf(str, "%%?%%{%d}%%>%%t%%{%d}%%+%%;", &c1, &c2) == 2) {
812 str = strchr(str, ';');
813 bufptr = save_tc_inequality(bufptr, c1, c2);
814 } else if (sscanf(str, "%%?%%{%d}%%>%%t%%'%c'%%+%%;", &c1, &ch2) == 2) {
815 str = strchr(str, ';');
816 bufptr = save_tc_inequality(bufptr, c1, ch2);
817 } else if (sscanf(str, "%%?%%'%c'%%>%%t%%{%d}%%+%%;", &ch1, &c2) == 2) {
818 str = strchr(str, ';');
819 bufptr = save_tc_inequality(bufptr, ch1, c2);
820 } else if (sscanf(str, "%%?%%'%c'%%>%%t%%'%c'%%+%%;", &ch1, &ch2) == 2) {
821 str = strchr(str, ';');
822 bufptr = save_tc_inequality(bufptr, ch1, ch2);
823 } else if ((len = bcd_expression(str)) != 0) {
825 bufptr = save_string(bufptr, "%B");
826 } else if ((sscanf(str, "%%{%d}%%+%%%c", &c1, &ch2) == 2
827 || sscanf(str, "%%'%c'%%+%%%c", &ch1, &ch2) == 2)
829 && (cp = strchr(str, '+'))) {
831 bufptr = save_string(bufptr, "%+");
835 bufptr = save_tc_char(bufptr, c1);
837 /* FIXME: this "works" for 'delta' */
838 else if (strncmp(str, "%{2}%*%-", (size_t) 8) == 0) {
840 bufptr = save_string(bufptr, "%D");
841 } else if (strncmp(str, "%{96}%^", (size_t) 7) == 0) {
844 bufptr = save_string(bufptr, "%n");
846 } else if (strncmp(str, "%{127}%^", (size_t) 8) == 0) {
849 bufptr = save_string(bufptr, "%m");
851 } else { /* cm-style format element */
855 bufptr = save_char(bufptr, '%');
868 bufptr = save_char(bufptr, '%');
872 while (isdigit(UChar(*str))) {
879 if (digits == 2 && ch2 != '0') {
882 } else if (_nc_strict_bsd) {
888 bufptr = save_char(bufptr, ch1);
894 * Convert %02 to %2 and %03 to %3
896 if (ch2 == '0' && !_nc_strict_bsd) {
898 bufptr[-2] = bufptr[-1];
901 if (_nc_strict_bsd) {
902 if (ch2 != 0 && ch2 != '0') {
904 } else if (ch1 < '2') {
907 bufptr = save_char(bufptr, ch1);
909 if (strchr("oxX.", *str)) {
910 syntax_error = TRUE; /* termcap doesn't have octal, hex */
915 bufptr = save_string(bufptr, "%d");
919 bufptr = save_string(bufptr, "%.");
923 * %s isn't in termcap, but it's convenient to pass it through
924 * so we can represent things like terminfo pfkey strings in
928 if (_nc_strict_bsd) {
931 bufptr = save_string(bufptr, "%s");
939 else if (*str == '2') {
940 if (!seenone && !seentwo) {
941 bufptr = save_string(bufptr, "%r");
944 } else if (*str >= '3') {
950 bufptr = save_string(bufptr, "%i");
954 bufptr = save_char(bufptr, *str);
957 } /* endswitch (*str) */
958 } /* endelse (*str == '%') */
961 * 'str' always points to the end of what was scanned in this step,
962 * but that may not be the end of the string.
965 if (str == 0 || *str == '\0')
968 } /* endwhile (*str) */
972 ((int) strlen(my_string) - (4 * myfix)) < MIN_TC_FIXUPS) {
973 while (--myfix >= 0) {
974 char *p = fixups[myfix].offset + my_string;
976 *p++ = (char) (fixups[myfix].ch | '@');
977 while ((p[0] = p[2]) != '\0') {
983 DEBUG_THIS(("... _nc_infotocap %s",
986 : _nc_visbuf(my_string)));
988 return (syntax_error ? NULL : my_string);
996 main(int argc, char *argv[])
1000 while ((c = getopt(argc, argv, "c")) != EOF)
1012 if (fgets(buf, sizeof(buf), stdin) == 0)
1014 buf[strlen(buf) - 1] = '\0';
1015 _nc_set_source(buf);
1018 char *cp = _nc_infotocap("to termcap", buf, 1);
1021 (void) fputs(cp, stdout);
1023 (void) fputs(_nc_captoinfo("to terminfo", buf, 1), stdout);
1024 (void) putchar('\n');
1031 NCURSES_EXPORT(void)
1032 _nc_captoinfo_leaks(void)
1034 if (my_string != 0) {
1035 FreeAndNull(my_string);