1 /****************************************************************************
2 * Copyright 2018-2019,2020 Thomas E. Dickey *
3 * Copyright 1998-2016,2017 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 /****************************************************************************
31 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
32 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
33 * and: Thomas E. Dickey 1996-on *
34 ****************************************************************************/
39 * Provide conversion in both directions between termcap and terminfo.
41 * cap-to-info --- conversion between termcap and terminfo formats
43 * The captoinfo() code was swiped from Ross Ridge's mytinfo package,
44 * adapted to fit ncurses by Eric S. Raymond <esr@snark.thyrsus.com>.
46 * It has just one entry point:
48 * char *_nc_captoinfo(n, s, parameterized)
50 * Convert value s for termcap string capability named n into terminfo
53 * This code recognizes all the standard 4.4BSD %-escapes:
56 * %d output value as in printf %d
57 * %2 output value as in printf %2d
58 * %3 output value as in printf %3d
59 * %. output value as in printf %c
60 * %+x add x to value, then do %.
61 * %>xy if value > x then add y, no output
62 * %r reverse order of two parameters, no output
63 * %i increment by one, no output
64 * %n exclusive-or all parameters with 0140 (Datamedia 2500)
65 * %B BCD (16*(value/10)) + (value%10), no output
66 * %D Reverse coding (value - 2*(value%16)), no output (Delta Data).
68 * Also, %02 and %03 are accepted as synonyms for %2 and %3.
70 * Besides all the standard termcap escapes, this translator understands
71 * the following extended escapes:
73 * used by GNU Emacs termcap libraries
74 * %a[+*-/=][cp]x GNU arithmetic.
75 * %m xor the first two parameters by 0177
76 * %b backup to previous parameter
77 * %f skip this parameter
79 * used by the University of Waterloo (MFCF) termcap libraries
80 * %-x subtract parameter FROM char x and output it as a char
81 * %ax add the character x to parameter
83 * If #define WATERLOO is on, also enable these translations:
85 * %sx subtract parameter FROM the character x
87 * By default, this Waterloo translations are not compiled in, because
88 * the Waterloo %s conflicts with the way terminfo uses %s in strings for
89 * function programming.
91 * Note the two definitions of %a: the GNU definition is translated if the
92 * characters after the 'a' are valid for it, otherwise the UW definition
96 #include <curses.priv.h>
101 MODULE_ID("$Id: captoinfo.c,v 1.98 2020/02/02 23:34:34 tom Exp $")
104 #define DEBUG_THIS(p) DEBUG(9, p)
106 #define DEBUG_THIS(p) /* nothing */
109 #define MAX_PUSHED 16 /* max # args we can push onto the stack */
111 static int stack[MAX_PUSHED]; /* the stack */
112 static int stackptr; /* the next empty place on the stack */
113 static int onstack; /* the top of stack */
114 static int seenm; /* seen a %m */
115 static int seenn; /* seen a %n */
116 static int seenr; /* seen a %r */
117 static int param; /* current parameter */
118 static char *dp; /* pointer to end of the converted string */
120 static char *my_string;
121 static size_t my_length;
125 /* initialize 'my_string', 'my_length' */
128 TYPE_MALLOC(char, my_length = 256, my_string);
135 save_string(char *d, const char *const s)
137 size_t have = (size_t) (d - my_string);
138 size_t need = have + strlen(s) + 2;
139 if (need > my_length) {
140 my_string = (char *) _nc_doalloc(my_string, my_length = (need + need));
142 _nc_err_abort(MSG_NO_MEMORY);
143 d = my_string + have;
145 _nc_STRCPY(d, s, my_length - have);
146 return d + strlen(d);
149 static NCURSES_INLINE char *
150 save_char(char *s, int c)
154 return save_string(s, temp);
159 /* push onstack on to the stack */
161 if (stackptr >= MAX_PUSHED)
162 _nc_warning("string too complex to convert");
164 stack[stackptr++] = onstack;
169 /* pop the top of the stack into onstack */
173 _nc_warning("I'm confused");
177 onstack = stack[--stackptr];
182 cvtchar(register const char *sp)
183 /* convert a character to a terminfo push */
207 while (isdigit(UChar(*sp))) {
208 c = UChar(8 * c + (*sp++ - '0'));
214 len = (c != '\0') ? 2 : 1;
228 len = (c != '\0') ? 1 : 0;
230 if (isgraph(c) && c != ',' && c != '\'' && c != '\\' && c != ':') {
231 dp = save_string(dp, "%\'");
232 dp = save_char(dp, c);
233 dp = save_char(dp, '\'');
234 } else if (c != '\0') {
235 dp = save_string(dp, "%{");
237 dp = save_char(dp, c / 100 + '0');
239 dp = save_char(dp, ((int) (c / 10)) % 10 + '0');
240 dp = save_char(dp, c % 10 + '0');
241 dp = save_char(dp, '}');
247 getparm(int parm, int n)
248 /* push n copies of param on the terminfo stack if not already there */
259 for (nn = 0; nn < n; ++nn) {
260 dp = save_string(dp, "%p");
261 dp = save_char(dp, '0' + parm);
264 if (onstack == parm) {
266 _nc_warning("string may not be optimal");
267 dp = save_string(dp, "%Pa");
269 dp = save_string(dp, "%ga");
279 if (seenn && parm < 3) {
280 dp = save_string(dp, "%{96}%^");
283 if (seenm && parm < 3) {
284 dp = save_string(dp, "%{127}%^");
289 * Convert a termcap string to terminfo format.
290 * 'cap' is the relevant terminfo capability index.
291 * 's' is the string value of the capability.
292 * 'parameterized' tells what type of translations to do:
293 * % translations if 1
294 * pad translations if >=0
296 NCURSES_EXPORT(char *)
297 _nc_captoinfo(const char *cap, const char *s, int const parameterized)
299 const char *capstart;
308 DEBUG_THIS(("_nc_captoinfo params %d, %s", parameterized, s));
312 /* skip the initial padding (if we haven't been told not to) */
316 if (parameterized >= 0 && isdigit(UChar(*s)))
317 for (capstart = s; *s != '\0'; s++)
318 if (!(isdigit(UChar(*s)) || *s == '*' || *s == '.'))
325 if (parameterized < 1) {
326 dp = save_char(dp, '%');
331 dp = save_string(dp, "%%");
335 _nc_warning("saw %%r twice in %s", cap);
340 _nc_warning("saw %%m twice in %s", cap);
345 _nc_warning("saw %%n twice in %s", cap);
349 dp = save_string(dp, "%i");
354 dp = save_string(dp, "%{10}%/%{16}%*");
356 dp = save_string(dp, "%{10}%m%+");
361 dp = save_string(dp, "%{2}%*%-");
364 /* %?%{x}%>%t%{y}%+%; */
367 dp = save_string(dp, "%?");
369 dp = save_string(dp, "%>%t");
371 dp = save_string(dp, "%+%;");
373 _nc_warning("expected two characters after %%>");
374 dp = save_string(dp, "%>");
378 if ((*s == '=' || *s == '+' || *s == '-'
379 || *s == '*' || *s == '/')
380 && (s[1] == 'p' || s[1] == 'c')
387 getparm(param + s[2] - '@', 1);
388 if (param != onstack) {
397 dp = save_string(dp, "%+");
400 dp = save_string(dp, "%-");
403 dp = save_string(dp, "%*");
406 dp = save_string(dp, "%/");
425 dp = save_string(dp, "%+");
430 dp = save_string(dp, "%+%c");
437 dp = save_string(dp, "%-");
440 dp = save_string(dp, "%s");
442 #endif /* WATERLOO */
447 dp = save_string(dp, "%-%c");
452 dp = save_string(dp, "%c");
455 case '0': /* not clear any of the historical termcaps did this */
468 dp = save_string(dp, "%2d");
474 dp = save_string(dp, "%3d");
479 dp = save_string(dp, "%d");
489 dp = save_string(dp, "%\\");
493 dp = save_char(dp, '%');
495 _nc_warning("unknown %% code %s (%#x) in %s",
496 unctrl((chtype) *s), UChar(*s), cap);
502 dp = save_char(dp, *s++);
508 * Now, if we stripped off some leading padding, add it at the end
509 * of the string as mandatory padding.
512 dp = save_string(dp, "$<");
513 for (s = capstart; *s != '\0'; s++)
514 if (isdigit(UChar(*s)) || *s == '*' || *s == '.')
515 dp = save_char(dp, *s);
518 dp = save_string(dp, "/>");
521 (void) save_char(dp, '\0');
523 DEBUG_THIS(("... _nc_captoinfo %s", NonNull(my_string)));
529 * Check for an expression that corresponds to "%B" (BCD):
530 * (parameter / 10) * 16 + (parameter % 10)
533 bcd_expression(const char *str)
535 /* leave this non-const for HPUX */
536 static char fmt[] = "%%p%c%%{10}%%/%%{16}%%*%%p%c%%{10}%%m%%+";
540 if (sscanf(str, fmt, &ch1, &ch2) == 2
541 && isdigit(UChar(ch1))
542 && isdigit(UChar(ch2))
549 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer)) fmt, ch1, ch2);
550 tst = strlen(buffer) - 1;
559 save_tc_char(char *bufptr, int c1)
561 if (is7bits(c1) && isprint(c1)) {
562 if (c1 == ':' || c1 == '\\')
563 bufptr = save_char(bufptr, '\\');
564 bufptr = save_char(bufptr, c1);
568 if (c1 == (c1 & 0x1f)) { /* iscntrl() returns T on 255 */
569 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
570 "%.20s", unctrl((chtype) c1));
572 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
575 bufptr = save_string(bufptr, temp);
581 save_tc_inequality(char *bufptr, int c1, int c2)
583 bufptr = save_string(bufptr, "%>");
584 bufptr = save_tc_char(bufptr, c1);
585 bufptr = save_tc_char(bufptr, c2);
590 * info-to-cap --- conversion between terminfo and termcap formats
592 * Here are the capabilities infotocap assumes it can translate to:
595 * %d output value as in printf %d
596 * %2 output value as in printf %2d
597 * %3 output value as in printf %3d
598 * %. output value as in printf %c
599 * %+c add character c to value, then do %.
600 * %>xy if value > x then add y, no output
601 * %r reverse order of two parameters, no output
602 * %i increment by one, no output
603 * %n exclusive-or all parameters with 0140 (Datamedia 2500)
604 * %B BCD (16*(value/10)) + (value%10), no output
605 * %D Reverse coding (value - 2*(value%16)), no output (Delta Data).
606 * %m exclusive-or all parameters with 0177 (not in 4.4BSD)
609 #define octal_fixup(n, c) fixups[n].ch = ((fixups[n].ch << 3) | ((c) - '0'))
612 * Convert a terminfo string to termcap format. Parameters are as in
615 NCURSES_EXPORT(char *)
616 _nc_infotocap(const char *cap GCC_UNUSED, const char *str, int const parameterized)
618 int seenone = 0, seentwo = 0, saw_m = 0, saw_n = 0;
620 const char *trimmed = 0;
622 char ch1 = 0, ch2 = 0;
623 char *bufptr = init_string();
627 bool syntax_error = FALSE;
632 } fixups[MAX_TC_FIXUPS];
634 DEBUG_THIS(("_nc_infotocap params %d, %s", parameterized, str));
636 /* we may have to move some trailing mandatory padding up front */
637 padding = str + strlen(str) - 1;
638 if (padding > str && *padding == '>') {
639 if (*--padding == '/')
641 while (isdigit(UChar(*padding)) || *padding == '.' || *padding == '*')
643 if (padding > str && *padding == '<' && *--padding == '$')
647 while (isdigit(UChar(*padding)) || *padding == '.' || *padding == '*')
648 bufptr = save_char(bufptr, *padding++);
651 for (; !syntax_error &&
653 ((trimmed == 0) || (str < trimmed)); str++) {
658 if (str[1] == '\0' || (str + 1) == trimmed) {
659 bufptr = save_string(bufptr, "\\136");
661 } else if (str[1] == '?') {
663 * Although the 4.3BSD termcap file has an instance of "kb=^?",
664 * that appears to be just cut/paste since neither 4.3BSD nor
665 * 4.4BSD termcap interprets "^?" as DEL.
667 bufptr = save_string(bufptr, "\\177");
670 bufptr = save_char(bufptr, *str++);
671 bufptr = save_char(bufptr, *str);
673 } else if (str[0] == '\\') {
674 if (str[1] == '\0' || (str + 1) == trimmed) {
675 bufptr = save_string(bufptr, "\\134");
677 } else if (str[1] == '^') {
678 bufptr = save_string(bufptr, "\\136");
680 } else if (str[1] == ',') {
681 bufptr = save_char(bufptr, *++str);
685 bufptr = save_char(bufptr, *str++);
687 if (_nc_strict_bsd) {
689 if (isoctal(UChar(xx1))) {
694 if (!isoctal(UChar(str[1])))
696 else if (str[1] && !isoctal(UChar(str[2])))
700 * Test for "\0", "\00" or "\000" and transform those
704 && ((pad == 2) || (str[1] == '0'))
705 && ((pad >= 1) || (str[2] == '0'))) {
709 pad = 0; /* FIXME - optionally pad to 3 digits */
711 if (myfix < MAX_TC_FIXUPS) {
713 fixups[myfix].ch = 0;
714 fixups[myfix].offset = (int) (bufptr
719 bufptr = save_char(bufptr, xx2);
720 if (myfix < MAX_TC_FIXUPS) {
721 fixups[myfix].ch <<= 3;
722 fixups[myfix].ch |= (xx2 - '0');
726 if (myfix < MAX_TC_FIXUPS) {
728 for (n = 0; n < fix; ++n) {
729 fixups[myfix].ch <<= 3;
730 fixups[myfix].ch |= (str[n] - '0');
732 if (fixups[myfix].ch < 32) {
736 } else if (strchr("E\\nrtbf", xx1) == 0) {
745 bufptr = save_char(bufptr, '0');
746 bufptr = save_char(bufptr, '4');
751 * Note: termcap documentation claims that ":"
752 * must be escaped as "\072", however the
753 * documentation is incorrect - read the code.
754 * The replacement does not work reliably,
755 * so the advice is not helpful.
757 bufptr = save_char(bufptr, '0');
758 bufptr = save_char(bufptr, '7');
762 /* should not happen, but handle this anyway */
763 _nc_SPRINTF(octal, _nc_SLIMIT(sizeof(octal))
765 bufptr = save_char(bufptr, octal[0]);
766 bufptr = save_char(bufptr, octal[1]);
772 if (myfix < MAX_TC_FIXUPS && isoctal(UChar(xx1))) {
773 bool will_fix = TRUE;
776 fixups[myfix].ch = 0;
777 fixups[myfix].offset = (int) (bufptr - my_string - 1);
778 for (n = 0; n < 3; ++n) {
779 if (isoctal(str[n])) {
780 octal_fixup(myfix, str[n]);
786 if (will_fix && (fixups[myfix].ch < 32))
790 bufptr = save_char(bufptr, xx1);
792 } else if (str[0] == '$' && str[1] == '<') { /* discard padding */
794 while (isdigit(UChar(*str))
801 } else if (sscanf(str,
802 "[%%?%%p1%%{8}%%<%%t%d%%p1%%d%%e%%p1%%{16}%%<%%t%d%%p1%%{8}%%-%%d%%e%d;5;%%p1%%d%%;m",
803 &in0, &in1, &in2) == 3
804 && ((in0 == 4 && in1 == 10 && in2 == 48)
805 || (in0 == 3 && in1 == 9 && in2 == 38))) {
806 /* dumb-down an optimized case from xterm-256color for termcap */
807 if ((str = strstr(str, ";m")) == 0)
808 break; /* cannot happen */
811 bufptr = save_string(bufptr, "[48;5;%dm");
813 bufptr = save_string(bufptr, "[38;5;%dm");
815 } else if (str[0] == '%' && str[1] == '%') { /* escaped '%' */
816 bufptr = save_string(bufptr, "%%");
818 } else if (*str != '%' || (parameterized < 1)) {
819 bufptr = save_char(bufptr, *str);
820 } else if (sscanf(str, "%%?%%{%d}%%>%%t%%{%d}%%+%%;", &c1, &c2) == 2) {
821 str = strchr(str, ';');
822 bufptr = save_tc_inequality(bufptr, c1, c2);
823 } else if (sscanf(str, "%%?%%{%d}%%>%%t%%'%c'%%+%%;", &c1, &ch2) == 2) {
824 str = strchr(str, ';');
825 bufptr = save_tc_inequality(bufptr, c1, ch2);
826 } else if (sscanf(str, "%%?%%'%c'%%>%%t%%{%d}%%+%%;", &ch1, &c2) == 2) {
827 str = strchr(str, ';');
828 bufptr = save_tc_inequality(bufptr, ch1, c2);
829 } else if (sscanf(str, "%%?%%'%c'%%>%%t%%'%c'%%+%%;", &ch1, &ch2) == 2) {
830 str = strchr(str, ';');
831 bufptr = save_tc_inequality(bufptr, ch1, ch2);
832 } else if ((len = bcd_expression(str)) != 0) {
834 bufptr = save_string(bufptr, "%B");
835 } else if ((sscanf(str, "%%{%d}%%+%%%c", &c1, &ch2) == 2
836 || sscanf(str, "%%'%c'%%+%%%c", &ch1, &ch2) == 2)
838 && (cp = strchr(str, '+'))) {
840 bufptr = save_string(bufptr, "%+");
844 bufptr = save_tc_char(bufptr, c1);
846 /* FIXME: this "works" for 'delta' */
847 else if (strncmp(str, "%{2}%*%-", (size_t) 8) == 0) {
849 bufptr = save_string(bufptr, "%D");
850 } else if (strncmp(str, "%{96}%^", (size_t) 7) == 0) {
853 bufptr = save_string(bufptr, "%n");
855 } else if (strncmp(str, "%{127}%^", (size_t) 8) == 0) {
858 bufptr = save_string(bufptr, "%m");
860 } else { /* cm-style format element */
864 bufptr = save_char(bufptr, '%');
877 bufptr = save_char(bufptr, '%');
881 while (isdigit(UChar(*str))) {
888 if (digits == 2 && ch2 != '0') {
891 } else if (_nc_strict_bsd) {
897 bufptr = save_char(bufptr, ch1);
903 * Convert %02 to %2 and %03 to %3
905 if (ch2 == '0' && !_nc_strict_bsd) {
907 bufptr[-2] = bufptr[-1];
910 if (_nc_strict_bsd) {
911 if (ch2 != 0 && ch2 != '0') {
913 } else if (ch1 < '2') {
916 bufptr = save_char(bufptr, ch1);
918 if (strchr("oxX.", *str)) {
919 syntax_error = TRUE; /* termcap doesn't have octal, hex */
924 bufptr = save_string(bufptr, "%d");
928 bufptr = save_string(bufptr, "%.");
932 * %s isn't in termcap, but it's convenient to pass it through
933 * so we can represent things like terminfo pfkey strings in
937 if (_nc_strict_bsd) {
940 bufptr = save_string(bufptr, "%s");
948 else if (*str == '2') {
949 if (!seenone && !seentwo) {
950 bufptr = save_string(bufptr, "%r");
953 } else if (*str >= '3') {
959 bufptr = save_string(bufptr, "%i");
963 bufptr = save_char(bufptr, *str);
966 } /* endswitch (*str) */
967 } /* endelse (*str == '%') */
970 * 'str' always points to the end of what was scanned in this step,
971 * but that may not be the end of the string.
974 if (str == 0 || *str == '\0')
977 } /* endwhile (*str) */
981 ((int) strlen(my_string) - (4 * myfix)) < MIN_TC_FIXUPS) {
982 while (--myfix >= 0) {
983 char *p = fixups[myfix].offset + my_string;
985 *p++ = (char) (fixups[myfix].ch | '@');
986 while ((p[0] = p[2]) != '\0') {
992 DEBUG_THIS(("... _nc_infotocap %s",
995 : _nc_visbuf(my_string)));
997 return (syntax_error ? NULL : my_string);
1005 main(int argc, char *argv[])
1009 while ((c = getopt(argc, argv, "c")) != EOF)
1021 if (fgets(buf, sizeof(buf), stdin) == 0)
1023 buf[strlen(buf) - 1] = '\0';
1024 _nc_set_source(buf);
1027 char *cp = _nc_infotocap("to termcap", buf, 1);
1030 (void) fputs(cp, stdout);
1032 (void) fputs(_nc_captoinfo("to terminfo", buf, 1), stdout);
1033 (void) putchar('\n');
1040 NCURSES_EXPORT(void)
1041 _nc_captoinfo_leaks(void)
1043 if (my_string != 0) {
1044 FreeAndNull(my_string);