1 /****************************************************************************
2 * Copyright (c) 1998-2017,2018 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.96 2018/05/12 16:46:55 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 */
258 for (nn = 0; nn < n; ++nn) {
259 dp = save_string(dp, "%p");
260 dp = save_char(dp, '0' + parm);
263 if (onstack == parm) {
265 _nc_warning("string may not be optimal");
266 dp = save_string(dp, "%Pa");
268 dp = save_string(dp, "%ga");
278 if (seenn && parm < 3) {
279 dp = save_string(dp, "%{96}%^");
282 if (seenm && parm < 3) {
283 dp = save_string(dp, "%{127}%^");
288 * Convert a termcap string to terminfo format.
289 * 'cap' is the relevant terminfo capability index.
290 * 's' is the string value of the capability.
291 * 'parameterized' tells what type of translations to do:
292 * % translations if 1
293 * pad translations if >=0
295 NCURSES_EXPORT(char *)
296 _nc_captoinfo(const char *cap, const char *s, int const parameterized)
298 const char *capstart;
307 DEBUG_THIS(("_nc_captoinfo params %d, %s", parameterized, s));
311 /* skip the initial padding (if we haven't been told not to) */
315 if (parameterized >= 0 && isdigit(UChar(*s)))
316 for (capstart = s;; s++)
317 if (!(isdigit(UChar(*s)) || *s == '*' || *s == '.'))
324 if (parameterized < 1) {
325 dp = save_char(dp, '%');
330 dp = save_string(dp, "%%");
334 _nc_warning("saw %%r twice in %s", cap);
339 _nc_warning("saw %%m twice in %s", cap);
344 _nc_warning("saw %%n twice in %s", cap);
348 dp = save_string(dp, "%i");
353 dp = save_string(dp, "%{10}%/%{16}%*");
355 dp = save_string(dp, "%{10}%m%+");
360 dp = save_string(dp, "%{2}%*%-");
364 /* %?%{x}%>%t%{y}%+%; */
365 dp = save_string(dp, "%?");
367 dp = save_string(dp, "%>%t");
369 dp = save_string(dp, "%+%;");
372 if ((*s == '=' || *s == '+' || *s == '-'
373 || *s == '*' || *s == '/')
374 && (s[1] == 'p' || s[1] == 'c')
381 getparm(param + s[2] - '@', 1);
382 if (param != onstack) {
391 dp = save_string(dp, "%+");
394 dp = save_string(dp, "%-");
397 dp = save_string(dp, "%*");
400 dp = save_string(dp, "%/");
419 dp = save_string(dp, "%+");
424 dp = save_string(dp, "%+%c");
431 dp = save_string(dp, "%-");
434 dp = save_string(dp, "%s");
436 #endif /* WATERLOO */
441 dp = save_string(dp, "%-%c");
446 dp = save_string(dp, "%c");
449 case '0': /* not clear any of the historical termcaps did this */
462 dp = save_string(dp, "%2d");
468 dp = save_string(dp, "%3d");
473 dp = save_string(dp, "%d");
483 dp = save_string(dp, "%\\");
487 dp = save_char(dp, '%');
489 _nc_warning("unknown %% code %s (%#x) in %s",
490 unctrl((chtype) *s), UChar(*s), cap);
495 dp = save_char(dp, *s++);
501 * Now, if we stripped off some leading padding, add it at the end
502 * of the string as mandatory padding.
505 dp = save_string(dp, "$<");
506 for (s = capstart;; s++)
507 if (isdigit(UChar(*s)) || *s == '*' || *s == '.')
508 dp = save_char(dp, *s);
511 dp = save_string(dp, "/>");
514 (void) save_char(dp, '\0');
516 DEBUG_THIS(("... _nc_captoinfo %s", NonNull(my_string)));
522 * Check for an expression that corresponds to "%B" (BCD):
523 * (parameter / 10) * 16 + (parameter % 10)
526 bcd_expression(const char *str)
528 /* leave this non-const for HPUX */
529 static char fmt[] = "%%p%c%%{10}%%/%%{16}%%*%%p%c%%{10}%%m%%+";
533 if (sscanf(str, fmt, &ch1, &ch2) == 2
534 && isdigit(UChar(ch1))
535 && isdigit(UChar(ch2))
542 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer)) fmt, ch1, ch2);
543 tst = strlen(buffer) - 1;
552 save_tc_char(char *bufptr, int c1)
554 if (is7bits(c1) && isprint(c1)) {
555 if (c1 == ':' || c1 == '\\')
556 bufptr = save_char(bufptr, '\\');
557 bufptr = save_char(bufptr, c1);
561 if (c1 == (c1 & 0x1f)) { /* iscntrl() returns T on 255 */
562 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
563 "%.20s", unctrl((chtype) c1));
565 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
568 bufptr = save_string(bufptr, temp);
574 save_tc_inequality(char *bufptr, int c1, int c2)
576 bufptr = save_string(bufptr, "%>");
577 bufptr = save_tc_char(bufptr, c1);
578 bufptr = save_tc_char(bufptr, c2);
583 * info-to-cap --- conversion between terminfo and termcap formats
585 * Here are the capabilities infotocap assumes it can translate to:
588 * %d output value as in printf %d
589 * %2 output value as in printf %2d
590 * %3 output value as in printf %3d
591 * %. output value as in printf %c
592 * %+c add character c to value, then do %.
593 * %>xy if value > x then add y, no output
594 * %r reverse order of two parameters, no output
595 * %i increment by one, no output
596 * %n exclusive-or all parameters with 0140 (Datamedia 2500)
597 * %B BCD (16*(value/10)) + (value%10), no output
598 * %D Reverse coding (value - 2*(value%16)), no output (Delta Data).
599 * %m exclusive-or all parameters with 0177 (not in 4.4BSD)
602 #define octal_fixup(n, c) fixups[n].ch = ((fixups[n].ch << 3) | ((c) - '0'))
605 * Convert a terminfo string to termcap format. Parameters are as in
608 NCURSES_EXPORT(char *)
609 _nc_infotocap(const char *cap GCC_UNUSED, const char *str, int const parameterized)
611 int seenone = 0, seentwo = 0, saw_m = 0, saw_n = 0;
613 const char *trimmed = 0;
615 char ch1 = 0, ch2 = 0;
616 char *bufptr = init_string();
620 bool syntax_error = FALSE;
625 } fixups[MAX_TC_FIXUPS];
627 DEBUG_THIS(("_nc_infotocap params %d, %s", parameterized, str));
629 /* we may have to move some trailing mandatory padding up front */
630 padding = str + strlen(str) - 1;
631 if (padding > str && *padding == '>') {
632 if (*--padding == '/')
634 while (isdigit(UChar(*padding)) || *padding == '.' || *padding == '*')
636 if (padding > str && *padding == '<' && *--padding == '$')
640 while (isdigit(UChar(*padding)) || *padding == '.' || *padding == '*')
641 bufptr = save_char(bufptr, *padding++);
644 for (; !syntax_error &&
646 ((trimmed == 0) || (str < trimmed)); str++) {
651 if (str[1] == '\0' || (str + 1) == trimmed) {
652 bufptr = save_string(bufptr, "\\136");
654 } else if (str[1] == '?') {
656 * Although the 4.3BSD termcap file has an instance of "kb=^?",
657 * that appears to be just cut/paste since neither 4.3BSD nor
658 * 4.4BSD termcap interprets "^?" as DEL.
660 bufptr = save_string(bufptr, "\\177");
663 bufptr = save_char(bufptr, *str++);
664 bufptr = save_char(bufptr, *str);
666 } else if (str[0] == '\\') {
667 if (str[1] == '\0' || (str + 1) == trimmed) {
668 bufptr = save_string(bufptr, "\\134");
670 } else if (str[1] == '^') {
671 bufptr = save_string(bufptr, "\\136");
673 } else if (str[1] == ',') {
674 bufptr = save_char(bufptr, *++str);
678 bufptr = save_char(bufptr, *str++);
680 if (_nc_strict_bsd) {
682 if (isoctal(UChar(xx1))) {
687 if (!isoctal(UChar(str[1])))
689 else if (str[1] && !isoctal(UChar(str[2])))
693 * Test for "\0", "\00" or "\000" and transform those
697 && ((pad == 2) || (str[1] == '0'))
698 && ((pad >= 1) || (str[2] == '0'))) {
702 pad = 0; /* FIXME - optionally pad to 3 digits */
704 if (myfix < MAX_TC_FIXUPS) {
706 fixups[myfix].ch = 0;
707 fixups[myfix].offset = (int) (bufptr
712 bufptr = save_char(bufptr, xx2);
713 if (myfix < MAX_TC_FIXUPS) {
714 fixups[myfix].ch <<= 3;
715 fixups[myfix].ch |= (xx2 - '0');
719 if (myfix < MAX_TC_FIXUPS) {
721 for (n = 0; n < fix; ++n) {
722 fixups[myfix].ch <<= 3;
723 fixups[myfix].ch |= (str[n] - '0');
725 if (fixups[myfix].ch < 32) {
729 } else if (strchr("E\\nrtbf", xx1) == 0) {
738 bufptr = save_char(bufptr, '0');
739 bufptr = save_char(bufptr, '4');
744 * Note: termcap documentation claims that ":"
745 * must be escaped as "\072", however the
746 * documentation is incorrect - read the code.
747 * The replacement does not work reliably,
748 * so the advice is not helpful.
750 bufptr = save_char(bufptr, '0');
751 bufptr = save_char(bufptr, '7');
755 /* should not happen, but handle this anyway */
756 _nc_SPRINTF(octal, _nc_SLIMIT(sizeof(octal))
758 bufptr = save_char(bufptr, octal[0]);
759 bufptr = save_char(bufptr, octal[1]);
765 if (myfix < MAX_TC_FIXUPS && isoctal(UChar(xx1))) {
766 bool will_fix = TRUE;
769 fixups[myfix].ch = 0;
770 fixups[myfix].offset = (int) (bufptr - my_string - 1);
771 for (n = 0; n < 3; ++n) {
772 if (isoctal(str[n])) {
773 octal_fixup(myfix, str[n]);
779 if (will_fix && (fixups[myfix].ch < 32))
783 bufptr = save_char(bufptr, xx1);
785 } else if (str[0] == '$' && str[1] == '<') { /* discard padding */
787 while (isdigit(UChar(*str))
794 } else if (sscanf(str,
795 "[%%?%%p1%%{8}%%<%%t%d%%p1%%d%%e%%p1%%{16}%%<%%t%d%%p1%%{8}%%-%%d%%e%d;5;%%p1%%d%%;m",
796 &in0, &in1, &in2) == 3
797 && ((in0 == 4 && in1 == 10 && in2 == 48)
798 || (in0 == 3 && in1 == 9 && in2 == 38))) {
799 /* dumb-down an optimized case from xterm-256color for termcap */
800 if ((str = strstr(str, ";m")) == 0)
801 break; /* cannot happen */
804 bufptr = save_string(bufptr, "[48;5;%dm");
806 bufptr = save_string(bufptr, "[38;5;%dm");
808 } else if (str[0] == '%' && str[1] == '%') { /* escaped '%' */
809 bufptr = save_string(bufptr, "%%");
811 } else if (*str != '%' || (parameterized < 1)) {
812 bufptr = save_char(bufptr, *str);
813 } else if (sscanf(str, "%%?%%{%d}%%>%%t%%{%d}%%+%%;", &c1, &c2) == 2) {
814 str = strchr(str, ';');
815 bufptr = save_tc_inequality(bufptr, c1, c2);
816 } else if (sscanf(str, "%%?%%{%d}%%>%%t%%'%c'%%+%%;", &c1, &ch2) == 2) {
817 str = strchr(str, ';');
818 bufptr = save_tc_inequality(bufptr, c1, ch2);
819 } else if (sscanf(str, "%%?%%'%c'%%>%%t%%{%d}%%+%%;", &ch1, &c2) == 2) {
820 str = strchr(str, ';');
821 bufptr = save_tc_inequality(bufptr, ch1, c2);
822 } else if (sscanf(str, "%%?%%'%c'%%>%%t%%'%c'%%+%%;", &ch1, &ch2) == 2) {
823 str = strchr(str, ';');
824 bufptr = save_tc_inequality(bufptr, ch1, ch2);
825 } else if ((len = bcd_expression(str)) != 0) {
827 bufptr = save_string(bufptr, "%B");
828 } else if ((sscanf(str, "%%{%d}%%+%%%c", &c1, &ch2) == 2
829 || sscanf(str, "%%'%c'%%+%%%c", &ch1, &ch2) == 2)
831 && (cp = strchr(str, '+'))) {
833 bufptr = save_string(bufptr, "%+");
837 bufptr = save_tc_char(bufptr, c1);
839 /* FIXME: this "works" for 'delta' */
840 else if (strncmp(str, "%{2}%*%-", (size_t) 8) == 0) {
842 bufptr = save_string(bufptr, "%D");
843 } else if (strncmp(str, "%{96}%^", (size_t) 7) == 0) {
846 bufptr = save_string(bufptr, "%n");
848 } else if (strncmp(str, "%{127}%^", (size_t) 8) == 0) {
851 bufptr = save_string(bufptr, "%m");
853 } else { /* cm-style format element */
857 bufptr = save_char(bufptr, '%');
870 bufptr = save_char(bufptr, '%');
874 while (isdigit(UChar(*str))) {
881 if (digits == 2 && ch2 != '0') {
884 } else if (_nc_strict_bsd) {
890 bufptr = save_char(bufptr, ch1);
896 * Convert %02 to %2 and %03 to %3
898 if (ch2 == '0' && !_nc_strict_bsd) {
900 bufptr[-2] = bufptr[-1];
903 if (_nc_strict_bsd) {
904 if (ch2 != 0 && ch2 != '0') {
906 } else if (ch1 < '2') {
909 bufptr = save_char(bufptr, ch1);
911 if (strchr("oxX.", *str)) {
912 syntax_error = TRUE; /* termcap doesn't have octal, hex */
917 bufptr = save_string(bufptr, "%d");
921 bufptr = save_string(bufptr, "%.");
925 * %s isn't in termcap, but it's convenient to pass it through
926 * so we can represent things like terminfo pfkey strings in
930 if (_nc_strict_bsd) {
933 bufptr = save_string(bufptr, "%s");
941 else if (*str == '2') {
942 if (!seenone && !seentwo) {
943 bufptr = save_string(bufptr, "%r");
946 } else if (*str >= '3') {
952 bufptr = save_string(bufptr, "%i");
956 bufptr = save_char(bufptr, *str);
959 } /* endswitch (*str) */
960 } /* endelse (*str == '%') */
963 * 'str' always points to the end of what was scanned in this step,
964 * but that may not be the end of the string.
967 if (str == 0 || *str == '\0')
970 } /* endwhile (*str) */
974 ((int) strlen(my_string) - (4 * myfix)) < MIN_TC_FIXUPS) {
975 while (--myfix >= 0) {
976 char *p = fixups[myfix].offset + my_string;
978 *p++ = (char) (fixups[myfix].ch | '@');
979 while ((p[0] = p[2]) != '\0') {
985 DEBUG_THIS(("... _nc_infotocap %s",
988 : _nc_visbuf(my_string)));
990 return (syntax_error ? NULL : my_string);
998 main(int argc, char *argv[])
1002 while ((c = getopt(argc, argv, "c")) != EOF)
1014 if (fgets(buf, sizeof(buf), stdin) == 0)
1016 buf[strlen(buf) - 1] = '\0';
1017 _nc_set_source(buf);
1020 char *cp = _nc_infotocap("to termcap", buf, 1);
1023 (void) fputs(cp, stdout);
1025 (void) fputs(_nc_captoinfo("to terminfo", buf, 1), stdout);
1026 (void) putchar('\n');
1033 NCURSES_EXPORT(void)
1034 _nc_captoinfo_leaks(void)
1036 if (my_string != 0) {
1037 FreeAndNull(my_string);