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.72 2012/02/22 22:40:24 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 my_string = typeMalloc(char, my_length = 256);
119 _nc_err_abort(MSG_NO_MEMORY);
126 save_string(char *d, const char *const s)
128 size_t have = (size_t) (d - my_string);
129 size_t need = have + strlen(s) + 2;
130 if (need > my_length) {
131 my_string = (char *) _nc_doalloc(my_string, my_length = (need + need));
133 _nc_err_abort(MSG_NO_MEMORY);
134 d = my_string + have;
136 _nc_STRCPY(d, s, my_length - have);
137 return d + strlen(d);
140 static NCURSES_INLINE char *
141 save_char(char *s, int c)
145 return save_string(s, temp);
150 /* push onstack on to the stack */
152 if (stackptr >= MAX_PUSHED)
153 _nc_warning("string too complex to convert");
155 stack[stackptr++] = onstack;
160 /* pop the top of the stack into onstack */
164 _nc_warning("I'm confused");
168 onstack = stack[--stackptr];
173 cvtchar(register const char *sp)
174 /* convert a character to a terminfo push */
186 c = (unsigned char) (*sp);
198 while (isdigit(UChar(*sp))) {
199 c = (unsigned char) (8 * c + (*sp++ - '0'));
204 c = (unsigned char) (*sp);
210 c = (unsigned char) (*++sp & 0x1f);
214 c = (unsigned char) (*sp);
217 if (isgraph(c) && c != ',' && c != '\'' && c != '\\' && c != ':') {
218 dp = save_string(dp, "%\'");
219 dp = save_char(dp, c);
220 dp = save_char(dp, '\'');
222 dp = save_string(dp, "%{");
224 dp = save_char(dp, c / 100 + '0');
226 dp = save_char(dp, ((int) (c / 10)) % 10 + '0');
227 dp = save_char(dp, c % 10 + '0');
228 dp = save_char(dp, '}');
234 getparm(int parm, int n)
235 /* push n copies of param on the terminfo stack if not already there */
245 dp = save_string(dp, "%p");
246 dp = save_char(dp, '0' + parm);
249 if (onstack == parm) {
251 _nc_warning("string may not be optimal");
252 dp = save_string(dp, "%Pa");
254 dp = save_string(dp, "%ga");
264 if (seenn && parm < 3) {
265 dp = save_string(dp, "%{96}%^");
268 if (seenm && parm < 3) {
269 dp = save_string(dp, "%{127}%^");
274 * Convert a termcap string to terminfo format.
275 * 'cap' is the relevant terminfo capability index.
276 * 's' is the string value of the capability.
277 * 'parameterized' tells what type of translations to do:
278 * % translations if 1
279 * pad translations if >=0
281 NCURSES_EXPORT(char *)
282 _nc_captoinfo(const char *cap, const char *s, int const parameterized)
284 const char *capstart;
295 /* skip the initial padding (if we haven't been told not to) */
299 if (parameterized >= 0 && isdigit(UChar(*s)))
300 for (capstart = s;; s++)
301 if (!(isdigit(UChar(*s)) || *s == '*' || *s == '.'))
308 if (parameterized < 1) {
309 dp = save_char(dp, '%');
314 dp = save_char(dp, '%');
318 _nc_warning("saw %%r twice in %s", cap);
323 _nc_warning("saw %%m twice in %s", cap);
328 _nc_warning("saw %%n twice in %s", cap);
332 dp = save_string(dp, "%i");
337 dp = save_string(dp, "%{10}%/%{16}%*");
339 dp = save_string(dp, "%{10}%m%+");
344 dp = save_string(dp, "%{2}%*%-");
348 /* %?%{x}%>%t%{y}%+%; */
349 dp = save_string(dp, "%?");
351 dp = save_string(dp, "%>%t");
353 dp = save_string(dp, "%+%;");
356 if ((*s == '=' || *s == '+' || *s == '-'
357 || *s == '*' || *s == '/')
358 && (s[1] == 'p' || s[1] == 'c')
365 getparm(param + s[2] - '@', 1);
366 if (param != onstack) {
375 dp = save_string(dp, "%+");
378 dp = save_string(dp, "%-");
381 dp = save_string(dp, "%*");
384 dp = save_string(dp, "%/");
403 dp = save_string(dp, "%+");
408 dp = save_string(dp, "%+%c");
415 dp = save_string(dp, "%-");
418 dp = save_string(dp, "%s");
420 #endif /* WATERLOO */
425 dp = save_string(dp, "%-%c");
430 dp = save_string(dp, "%c");
433 case '0': /* not clear any of the historical termcaps did this */
441 dp = save_string(dp, "%2d");
447 dp = save_string(dp, "%3d");
452 dp = save_string(dp, "%d");
462 dp = save_string(dp, "%\\");
466 dp = save_char(dp, '%');
468 _nc_warning("unknown %% code %s (%#x) in %s",
469 unctrl((chtype) *s), UChar(*s), cap);
474 dp = save_char(dp, *s++);
480 * Now, if we stripped off some leading padding, add it at the end
481 * of the string as mandatory padding.
484 dp = save_string(dp, "$<");
485 for (s = capstart;; s++)
486 if (isdigit(UChar(*s)) || *s == '*' || *s == '.')
487 dp = save_char(dp, *s);
490 dp = save_string(dp, "/>");
493 (void) save_char(dp, '\0');
498 * Check for an expression that corresponds to "%B" (BCD):
499 * (parameter / 10) * 16 + (parameter % 10)
502 bcd_expression(const char *str)
504 /* leave this non-const for HPUX */
505 static char fmt[] = "%%p%c%%{10}%%/%%{16}%%*%%p%c%%{10}%%m%%+";
509 if (sscanf(str, fmt, &ch1, &ch2) == 2
510 && isdigit(UChar(ch1))
511 && isdigit(UChar(ch2))
518 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer)) fmt, ch1, ch2);
519 tst = strlen(buffer) - 1;
528 save_tc_char(char *bufptr, int c1)
532 if (is7bits(c1) && isprint(c1)) {
533 if (c1 == ':' || c1 == '\\')
534 bufptr = save_char(bufptr, '\\');
535 bufptr = save_char(bufptr, c1);
537 if (c1 == (c1 & 0x1f)) /* iscntrl() returns T on 255 */
538 _nc_STRCPY(temp, unctrl((chtype) c1), sizeof(temp));
540 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp)) "\\%03o", c1);
541 bufptr = save_string(bufptr, temp);
547 save_tc_inequality(char *bufptr, int c1, int c2)
549 bufptr = save_string(bufptr, "%>");
550 bufptr = save_tc_char(bufptr, c1);
551 bufptr = save_tc_char(bufptr, c2);
556 * Here are the capabilities infotocap assumes it can translate to:
559 * %d output value as in printf %d
560 * %2 output value as in printf %2d
561 * %3 output value as in printf %3d
562 * %. output value as in printf %c
563 * %+c add character c to value, then do %.
564 * %>xy if value > x then add y, no output
565 * %r reverse order of two parameters, no output
566 * %i increment by one, no output
567 * %n exclusive-or all parameters with 0140 (Datamedia 2500)
568 * %B BCD (16*(value/10)) + (value%10), no output
569 * %D Reverse coding (value - 2*(value%16)), no output (Delta Data).
570 * %m exclusive-or all parameters with 0177 (not in 4.4BSD)
574 * Convert a terminfo string to termcap format. Parameters are as in
577 NCURSES_EXPORT(char *)
578 _nc_infotocap(const char *cap GCC_UNUSED, const char *str, int const parameterized)
580 int seenone = 0, seentwo = 0, saw_m = 0, saw_n = 0;
582 const char *trimmed = 0;
584 char ch1 = 0, ch2 = 0;
585 char *bufptr = init_string();
588 bool syntax_error = FALSE;
590 /* we may have to move some trailing mandatory padding up front */
591 padding = str + strlen(str) - 1;
592 if (padding > str && *padding == '>') {
593 if (*--padding == '/')
595 while (isdigit(UChar(*padding)) || *padding == '.' || *padding == '*')
597 if (padding > str && *padding == '<' && *--padding == '$')
601 while (isdigit(UChar(*padding)) || *padding == '.' || *padding == '*')
602 bufptr = save_char(bufptr, *padding++);
605 for (; *str && ((trimmed == 0) || (str < trimmed)); str++) {
610 if (str[1] == '\0' || (str + 1) == trimmed) {
611 bufptr = save_string(bufptr, "\\136");
614 bufptr = save_char(bufptr, *str++);
615 bufptr = save_char(bufptr, *str);
617 } else if (str[0] == '\\') {
618 if (str[1] == '\0' || (str + 1) == trimmed) {
619 bufptr = save_string(bufptr, "\\134");
621 } else if (str[1] == '^') {
622 bufptr = save_string(bufptr, "\\136");
624 } else if (str[1] == ',') {
625 bufptr = save_char(bufptr, *++str);
629 bufptr = save_char(bufptr, *str++);
631 if (_nc_strict_bsd) {
632 if (isdigit(UChar(xx1))) {
635 if (!isdigit(UChar(str[1])))
637 else if (str[1] && !isdigit(UChar(str[2])))
641 * Test for "\0", "\00" or "\000" and transform those
645 && ((pad == 2) || (str[1] == '0'))
646 && ((pad >= 1) || (str[2] == '0'))) {
650 pad = 0; /* FIXME - optionally pad to 3 digits */
653 bufptr = save_char(bufptr, xx2);
656 } else if (strchr("E\\nrtbf", xx1) == 0) {
665 bufptr = save_char(bufptr, '0');
666 bufptr = save_char(bufptr, '4');
671 * Note: termcap documentation claims that ":"
672 * must be escaped as "\072", however the
673 * documentation is incorrect - read the code.
674 * The replacement does not work reliably,
675 * so the advice is not helpful.
677 bufptr = save_char(bufptr, '0');
678 bufptr = save_char(bufptr, '7');
682 /* should not happen, but handle this anyway */
683 _nc_SPRINTF(octal, _nc_SLIMIT(sizeof(octal))
685 bufptr = save_char(bufptr, octal[0]);
686 bufptr = save_char(bufptr, octal[1]);
692 bufptr = save_char(bufptr, xx1);
694 } else if (str[0] == '$' && str[1] == '<') { /* discard padding */
696 while (isdigit(UChar(*str))
703 } else if (sscanf(str,
704 "[%%?%%p1%%{8}%%<%%t%d%%p1%%d%%e%%p1%%{16}%%<%%t%d%%p1%%{8}%%-%%d%%e%d;5;%%p1%%d%%;m",
705 &in0, &in1, &in2) == 3
706 && ((in0 == 4 && in1 == 10 && in2 == 48)
707 || (in0 == 3 && in1 == 9 && in2 == 38))) {
708 /* dumb-down an optimized case from xterm-256color for termcap */
709 str = strstr(str, ";m");
712 bufptr = save_string(bufptr, "[48;5;%dm");
714 bufptr = save_string(bufptr, "[38;5;%dm");
716 } else if (str[0] == '%' && str[1] == '%') { /* escaped '%' */
717 bufptr = save_string(bufptr, "%%");
719 } else if (*str != '%' || (parameterized < 1)) {
720 bufptr = save_char(bufptr, *str);
721 } else if (sscanf(str, "%%?%%{%d}%%>%%t%%{%d}%%+%%;", &c1, &c2) == 2) {
722 str = strchr(str, ';');
723 bufptr = save_tc_inequality(bufptr, c1, c2);
724 } else if (sscanf(str, "%%?%%{%d}%%>%%t%%'%c'%%+%%;", &c1, &ch2) == 2) {
725 str = strchr(str, ';');
726 bufptr = save_tc_inequality(bufptr, c1, ch2);
727 } else if (sscanf(str, "%%?%%'%c'%%>%%t%%{%d}%%+%%;", &ch1, &c2) == 2) {
728 str = strchr(str, ';');
729 bufptr = save_tc_inequality(bufptr, ch1, c2);
730 } else if (sscanf(str, "%%?%%'%c'%%>%%t%%'%c'%%+%%;", &ch1, &ch2) == 2) {
731 str = strchr(str, ';');
732 bufptr = save_tc_inequality(bufptr, ch1, ch2);
733 } else if ((len = bcd_expression(str)) != 0) {
735 bufptr = save_string(bufptr, "%B");
736 } else if ((sscanf(str, "%%{%d}%%+%%c", &c1) == 1
737 || sscanf(str, "%%'%c'%%+%%c", &ch1) == 1)
738 && (cp = strchr(str, '+'))) {
740 bufptr = save_string(bufptr, "%+");
744 bufptr = save_tc_char(bufptr, c1);
746 /* FIXME: this "works" for 'delta' */
747 else if (strncmp(str, "%{2}%*%-", (size_t) 8) == 0) {
749 bufptr = save_string(bufptr, "%D");
750 } else if (strncmp(str, "%{96}%^", (size_t) 7) == 0) {
753 bufptr = save_string(bufptr, "%n");
755 } else if (strncmp(str, "%{127}%^", (size_t) 8) == 0) {
758 bufptr = save_string(bufptr, "%m");
760 } else { /* cm-style format element */
764 bufptr = save_char(bufptr, '%');
777 bufptr = save_char(bufptr, '%');
780 while (isdigit(UChar(*str))) {
783 if (_nc_strict_bsd) {
787 bufptr = save_char(bufptr, ch1);
790 if (_nc_strict_bsd) {
791 if (ch2 != 0 && ch2 != '0')
795 bufptr = save_char(bufptr, ch1);
797 if (strchr("doxX.", *str)) {
798 if (*str != 'd') /* termcap doesn't have octal, hex */
804 bufptr = save_string(bufptr, "%d");
808 bufptr = save_string(bufptr, "%.");
812 * %s isn't in termcap, but it's convenient to pass it through
813 * so we can represent things like terminfo pfkey strings in
819 bufptr = save_string(bufptr, "%s");
826 else if (*str == '2') {
827 if (!seenone && !seentwo) {
828 bufptr = save_string(bufptr, "%r");
831 } else if (*str >= '3')
836 bufptr = save_string(bufptr, "%i");
840 bufptr = save_char(bufptr, *str);
843 } /* endswitch (*str) */
844 } /* endelse (*str == '%') */
847 * 'str' always points to the end of what was scanned in this step,
848 * but that may not be the end of the string.
854 } /* endwhile (*str) */
856 return (syntax_error ? NULL : my_string);
864 main(int argc, char *argv[])
868 while ((c = getopt(argc, argv, "c")) != EOF)
880 if (fgets(buf, sizeof(buf), stdin) == 0)
882 buf[strlen(buf) - 1] = '\0';
886 char *cp = _nc_infotocap("to termcap", buf, 1);
889 (void) fputs(cp, stdout);
891 (void) fputs(_nc_captoinfo("to terminfo", buf, 1), stdout);
892 (void) putchar('\n');
900 _nc_captoinfo_leaks(void)
902 if (my_string != 0) {
903 FreeAndNull(my_string);