1 /****************************************************************************
2 * Copyright 2018-2020,2021 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.102 2021/09/04 10:29:15 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;
223 } else if (c == '\0') {
231 len = (c != '\0') ? 1 : 0;
233 if (isgraph(c) && c != ',' && c != '\'' && c != '\\' && c != ':') {
234 dp = save_string(dp, "%\'");
235 dp = save_char(dp, c);
236 dp = save_char(dp, '\'');
237 } else if (c != '\0') {
238 dp = save_string(dp, "%{");
240 dp = save_char(dp, c / 100 + '0');
242 dp = save_char(dp, ((int) (c / 10)) % 10 + '0');
243 dp = save_char(dp, c % 10 + '0');
244 dp = save_char(dp, '}');
250 getparm(int parm, int n)
251 /* push n copies of param on the terminfo stack if not already there */
262 for (nn = 0; nn < n; ++nn) {
263 dp = save_string(dp, "%p");
264 dp = save_char(dp, '0' + parm);
267 if (onstack == parm) {
269 _nc_warning("string may not be optimal");
270 dp = save_string(dp, "%Pa");
272 dp = save_string(dp, "%ga");
282 if (seenn && parm < 3) {
283 dp = save_string(dp, "%{96}%^");
286 if (seenm && parm < 3) {
287 dp = save_string(dp, "%{127}%^");
292 * Convert a termcap string to terminfo format.
293 * 'cap' is the relevant terminfo capability index.
294 * 's' is the string value of the capability.
295 * 'parameterized' tells what type of translations to do:
296 * % translations if 1
297 * pad translations if >=0
299 NCURSES_EXPORT(char *)
300 _nc_captoinfo(const char *cap, const char *s, int const parameterized)
302 const char *capstart;
311 DEBUG_THIS(("_nc_captoinfo params %d, %s", parameterized, s));
315 /* skip the initial padding (if we haven't been told not to) */
319 if (parameterized >= 0 && isdigit(UChar(*s)))
320 for (capstart = s; *s != '\0'; s++)
321 if (!(isdigit(UChar(*s)) || *s == '*' || *s == '.'))
328 if (parameterized < 1) {
329 dp = save_char(dp, '%');
334 dp = save_string(dp, "%%");
338 _nc_warning("saw %%r twice in %s", cap);
343 _nc_warning("saw %%m twice in %s", cap);
348 _nc_warning("saw %%n twice in %s", cap);
352 dp = save_string(dp, "%i");
357 dp = save_string(dp, "%{10}%/%{16}%*");
359 dp = save_string(dp, "%{10}%m%+");
364 dp = save_string(dp, "%{2}%*%-");
367 /* %?%{x}%>%t%{y}%+%; */
370 dp = save_string(dp, "%?");
372 dp = save_string(dp, "%>%t");
374 dp = save_string(dp, "%+%;");
376 _nc_warning("expected two characters after %%>");
377 dp = save_string(dp, "%>");
381 if ((*s == '=' || *s == '+' || *s == '-'
382 || *s == '*' || *s == '/')
383 && (s[1] == 'p' || s[1] == 'c')
390 getparm(param + s[2] - '@', 1);
391 if (param != onstack) {
400 dp = save_string(dp, "%+");
403 dp = save_string(dp, "%-");
406 dp = save_string(dp, "%*");
409 dp = save_string(dp, "%/");
428 dp = save_string(dp, "%+");
433 dp = save_string(dp, "%+%c");
440 dp = save_string(dp, "%-");
443 dp = save_string(dp, "%s");
445 #endif /* WATERLOO */
450 dp = save_string(dp, "%-%c");
455 dp = save_string(dp, "%c");
458 case '0': /* not clear any of the historical termcaps did this */
471 dp = save_string(dp, "%2d");
477 dp = save_string(dp, "%3d");
482 dp = save_string(dp, "%d");
492 dp = save_string(dp, "%\\");
496 dp = save_char(dp, '%');
498 _nc_warning("unknown %% code %s (%#x) in %s",
499 unctrl((chtype) *s), UChar(*s), cap);
505 dp = save_char(dp, *s++);
511 * Now, if we stripped off some leading padding, add it at the end
512 * of the string as mandatory padding.
515 dp = save_string(dp, "$<");
516 for (s = capstart; *s != '\0'; s++)
517 if (isdigit(UChar(*s)) || *s == '*' || *s == '.')
518 dp = save_char(dp, *s);
521 dp = save_string(dp, "/>");
524 (void) save_char(dp, '\0');
526 DEBUG_THIS(("... _nc_captoinfo %s", NonNull(my_string)));
532 * Check for an expression that corresponds to "%B" (BCD):
533 * (parameter / 10) * 16 + (parameter % 10)
536 bcd_expression(const char *str)
538 /* leave this non-const for HPUX */
539 static char fmt[] = "%%p%c%%{10}%%/%%{16}%%*%%p%c%%{10}%%m%%+";
543 if (sscanf(str, fmt, &ch1, &ch2) == 2
544 && isdigit(UChar(ch1))
545 && isdigit(UChar(ch2))
552 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer)) fmt, ch1, ch2);
553 tst = strlen(buffer) - 1;
562 save_tc_char(char *bufptr, int c1)
564 if (is7bits(c1) && isprint(c1)) {
565 if (c1 == ':' || c1 == '\\')
566 bufptr = save_char(bufptr, '\\');
567 bufptr = save_char(bufptr, c1);
571 if (c1 == (c1 & 0x1f)) { /* iscntrl() returns T on 255 */
572 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
573 "%.20s", unctrl((chtype) c1));
575 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
578 bufptr = save_string(bufptr, temp);
584 save_tc_inequality(char *bufptr, int c1, int c2)
586 bufptr = save_string(bufptr, "%>");
587 bufptr = save_tc_char(bufptr, c1);
588 bufptr = save_tc_char(bufptr, c2);
593 * info-to-cap --- conversion between terminfo and termcap formats
595 * Here are the capabilities infotocap assumes it can translate to:
598 * %d output value as in printf %d
599 * %2 output value as in printf %2d
600 * %3 output value as in printf %3d
601 * %. output value as in printf %c
602 * %+c add character c to value, then do %.
603 * %>xy if value > x then add y, no output
604 * %r reverse order of two parameters, no output
605 * %i increment by one, no output
606 * %n exclusive-or all parameters with 0140 (Datamedia 2500)
607 * %B BCD (16*(value/10)) + (value%10), no output
608 * %D Reverse coding (value - 2*(value%16)), no output (Delta Data).
609 * %m exclusive-or all parameters with 0177 (not in 4.4BSD)
612 #define octal_fixup(n, c) fixups[n].ch = ((fixups[n].ch << 3) | ((c) - '0'))
615 * Convert a terminfo string to termcap format. Parameters are as in
618 NCURSES_EXPORT(char *)
619 _nc_infotocap(const char *cap GCC_UNUSED, const char *str, int const parameterized)
621 int seenone = 0, seentwo = 0, saw_m = 0, saw_n = 0;
623 const char *trimmed = 0;
625 char ch1 = 0, ch2 = 0;
626 char *bufptr = init_string();
630 bool syntax_error = FALSE;
635 } fixups[MAX_TC_FIXUPS];
637 DEBUG_THIS(("_nc_infotocap %s params %d, %s",
638 _nc_strict_bsd ? "strict" : "loose",
642 /* we may have to move some trailing mandatory padding up front */
643 padding = str + strlen(str) - 1;
644 if (padding > str && *padding == '>') {
645 if (padding > (str + 1) && *--padding == '/')
647 while (isdigit(UChar(*padding)) || *padding == '.' || *padding == '*')
649 if (padding > str && *padding == '<' && *--padding == '$')
653 while (isdigit(UChar(*padding)) || *padding == '.' || *padding == '*')
654 bufptr = save_char(bufptr, *padding++);
657 for (; !syntax_error &&
659 ((trimmed == 0) || (str < trimmed)); str++) {
664 if (str[1] == '\0' || (str + 1) == trimmed) {
665 bufptr = save_string(bufptr, "\\136");
667 } else if (str[1] == '?') {
669 * Although the 4.3BSD termcap file has an instance of "kb=^?",
670 * that appears to be just cut/paste since neither 4.3BSD nor
671 * 4.4BSD termcap interprets "^?" as DEL.
673 bufptr = save_string(bufptr, "\\177");
676 bufptr = save_char(bufptr, *str++);
677 bufptr = save_char(bufptr, *str);
679 } else if (str[0] == ':') {
680 bufptr = save_char(bufptr, '\\');
681 bufptr = save_char(bufptr, '0');
682 bufptr = save_char(bufptr, '7');
683 bufptr = save_char(bufptr, '2');
684 } else if (str[0] == '\\') {
685 if (str[1] == '\0' || (str + 1) == trimmed) {
686 bufptr = save_string(bufptr, "\\134");
688 } else if (str[1] == '^') {
689 bufptr = save_string(bufptr, "\\136");
691 } else if (str[1] == ',') {
692 bufptr = save_char(bufptr, *++str);
696 bufptr = save_char(bufptr, *str++);
698 if (_nc_strict_bsd) {
700 if (isoctal(UChar(xx1))) {
705 if (!isoctal(UChar(str[1])))
707 else if (str[1] && !isoctal(UChar(str[2])))
711 * Test for "\0", "\00" or "\000" and transform those
715 && ((pad == 2) || (str[1] == '0'))
716 && ((pad >= 1) || (str[2] == '0'))) {
720 pad = 0; /* FIXME - optionally pad to 3 digits */
722 if (myfix < MAX_TC_FIXUPS) {
724 fixups[myfix].ch = 0;
725 fixups[myfix].offset = (int) (bufptr
730 bufptr = save_char(bufptr, xx2);
731 if (myfix < MAX_TC_FIXUPS) {
732 fixups[myfix].ch <<= 3;
733 fixups[myfix].ch |= (xx2 - '0');
737 if (myfix < MAX_TC_FIXUPS) {
739 for (n = 0; n < fix; ++n) {
740 fixups[myfix].ch <<= 3;
741 fixups[myfix].ch |= (str[n] - '0');
743 if (fixups[myfix].ch < 32) {
747 } else if (strchr("E\\nrtbf", xx1) == 0) {
756 bufptr = save_char(bufptr, '0');
757 bufptr = save_char(bufptr, '4');
762 * Note: termcap documentation claims that ":"
763 * must be escaped as "\072", however the
764 * documentation is incorrect - read the code.
765 * The replacement does not work reliably,
766 * so the advice is not helpful.
768 bufptr = save_char(bufptr, '0');
769 bufptr = save_char(bufptr, '7');
773 /* should not happen, but handle this anyway */
774 _nc_SPRINTF(octal, _nc_SLIMIT(sizeof(octal))
776 bufptr = save_char(bufptr, octal[0]);
777 bufptr = save_char(bufptr, octal[1]);
783 if (myfix < MAX_TC_FIXUPS && isoctal(UChar(xx1))) {
784 bool will_fix = TRUE;
787 fixups[myfix].ch = 0;
788 fixups[myfix].offset = (int) (bufptr - my_string - 1);
789 for (n = 0; n < 3; ++n) {
790 if (isoctal(str[n])) {
791 octal_fixup(myfix, str[n]);
797 if (will_fix && (fixups[myfix].ch < 32))
801 bufptr = save_char(bufptr, xx1);
803 } else if (str[0] == '$' && str[1] == '<') { /* discard padding */
805 while (isdigit(UChar(*str))
812 } else if (sscanf(str,
813 "[%%?%%p1%%{8}%%<%%t%d%%p1%%d%%e%%p1%%{16}%%<%%t%d%%p1%%{8}%%-%%d%%e%d;5;%%p1%%d%%;m",
814 &in0, &in1, &in2) == 3
815 && ((in0 == 4 && in1 == 10 && in2 == 48)
816 || (in0 == 3 && in1 == 9 && in2 == 38))) {
817 /* dumb-down an optimized case from xterm-256color for termcap */
818 if ((str = strstr(str, ";m")) == 0)
819 break; /* cannot happen */
822 bufptr = save_string(bufptr, "[48;5;%dm");
824 bufptr = save_string(bufptr, "[38;5;%dm");
826 } else if (str[0] == '%' && str[1] == '%') { /* escaped '%' */
827 bufptr = save_string(bufptr, "%%");
829 } else if (*str != '%' || (parameterized < 1)) {
830 bufptr = save_char(bufptr, *str);
831 } else if (sscanf(str, "%%?%%{%d}%%>%%t%%{%d}%%+%%;", &c1, &c2) == 2) {
832 str = strchr(str, ';');
833 bufptr = save_tc_inequality(bufptr, c1, c2);
834 } else if (sscanf(str, "%%?%%{%d}%%>%%t%%'%c'%%+%%;", &c1, &ch2) == 2) {
835 str = strchr(str, ';');
836 bufptr = save_tc_inequality(bufptr, c1, ch2);
837 } else if (sscanf(str, "%%?%%'%c'%%>%%t%%{%d}%%+%%;", &ch1, &c2) == 2) {
838 str = strchr(str, ';');
839 bufptr = save_tc_inequality(bufptr, ch1, c2);
840 } else if (sscanf(str, "%%?%%'%c'%%>%%t%%'%c'%%+%%;", &ch1, &ch2) == 2) {
841 str = strchr(str, ';');
842 bufptr = save_tc_inequality(bufptr, ch1, ch2);
843 } else if ((len = bcd_expression(str)) != 0) {
845 bufptr = save_string(bufptr, "%B");
846 } else if ((sscanf(str, "%%{%d}%%+%%%c", &c1, &ch2) == 2
847 || sscanf(str, "%%'%c'%%+%%%c", &ch1, &ch2) == 2)
849 && (cp = strchr(str, '+'))) {
851 bufptr = save_string(bufptr, "%+");
855 bufptr = save_tc_char(bufptr, c1);
857 /* FIXME: this "works" for 'delta' */
858 else if (strncmp(str, "%{2}%*%-", (size_t) 8) == 0) {
860 bufptr = save_string(bufptr, "%D");
861 } else if (strncmp(str, "%{96}%^", (size_t) 7) == 0) {
864 bufptr = save_string(bufptr, "%n");
866 } else if (strncmp(str, "%{127}%^", (size_t) 8) == 0) {
869 bufptr = save_string(bufptr, "%m");
871 } else { /* cm-style format element */
875 bufptr = save_char(bufptr, '%');
888 bufptr = save_char(bufptr, '%');
892 while (isdigit(UChar(*str))) {
899 if (digits == 2 && ch2 != '0') {
902 } else if (_nc_strict_bsd) {
908 bufptr = save_char(bufptr, ch1);
914 * Convert %02 to %2 and %03 to %3
916 if (ch2 == '0' && !_nc_strict_bsd) {
918 bufptr[-2] = bufptr[-1];
921 if (_nc_strict_bsd) {
922 if (ch2 != 0 && ch2 != '0') {
924 } else if (ch1 < '2') {
927 bufptr = save_char(bufptr, ch1);
929 if (strchr("oxX.", *str)) {
930 syntax_error = TRUE; /* termcap doesn't have octal, hex */
935 bufptr = save_string(bufptr, "%d");
939 bufptr = save_string(bufptr, "%.");
943 * %s isn't in termcap, but it is convenient to pass it through
944 * so we can represent things like terminfo pfkey strings in
948 if (_nc_strict_bsd) {
951 bufptr = save_string(bufptr, "%s");
959 else if (*str == '2') {
960 if (!seenone && !seentwo) {
961 bufptr = save_string(bufptr, "%r");
964 } else if (*str >= '3') {
970 bufptr = save_string(bufptr, "%i");
974 bufptr = save_char(bufptr, *str);
977 } /* endswitch (*str) */
978 } /* endelse (*str == '%') */
981 * 'str' always points to the end of what was scanned in this step,
982 * but that may not be the end of the string.
985 if (str == 0 || *str == '\0')
988 } /* endwhile (*str) */
992 ((int) strlen(my_string) - (4 * myfix)) < MIN_TC_FIXUPS) {
993 while (--myfix >= 0) {
994 char *p = fixups[myfix].offset + my_string;
996 *p++ = (char) (fixups[myfix].ch | '@');
997 while ((p[0] = p[2]) != '\0') {
1003 DEBUG_THIS(("... _nc_infotocap %s",
1006 : _nc_visbuf(my_string)));
1008 return (syntax_error ? NULL : my_string);
1016 main(int argc, char *argv[])
1020 while ((c = getopt(argc, argv, "c")) != EOF)
1032 if (fgets(buf, sizeof(buf), stdin) == 0)
1034 buf[strlen(buf) - 1] = '\0';
1035 _nc_set_source(buf);
1038 char *cp = _nc_infotocap("to termcap", buf, 1);
1041 (void) fputs(cp, stdout);
1043 (void) fputs(_nc_captoinfo("to terminfo", buf, 1), stdout);
1044 (void) putchar('\n');
1051 NCURSES_EXPORT(void)
1052 _nc_captoinfo_leaks(void)
1054 if (my_string != 0) {
1055 FreeAndNull(my_string);