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.73 2012/10/27 21:27:02 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 TYPE_MALLOC(char, my_length = 256, my_string);
124 save_string(char *d, const char *const s)
126 size_t have = (size_t) (d - my_string);
127 size_t need = have + strlen(s) + 2;
128 if (need > my_length) {
129 my_string = (char *) _nc_doalloc(my_string, my_length = (need + need));
131 _nc_err_abort(MSG_NO_MEMORY);
132 d = my_string + have;
134 _nc_STRCPY(d, s, my_length - have);
135 return d + strlen(d);
138 static NCURSES_INLINE char *
139 save_char(char *s, int c)
143 return save_string(s, temp);
148 /* push onstack on to the stack */
150 if (stackptr >= MAX_PUSHED)
151 _nc_warning("string too complex to convert");
153 stack[stackptr++] = onstack;
158 /* pop the top of the stack into onstack */
162 _nc_warning("I'm confused");
166 onstack = stack[--stackptr];
171 cvtchar(register const char *sp)
172 /* convert a character to a terminfo push */
184 c = (unsigned char) (*sp);
196 while (isdigit(UChar(*sp))) {
197 c = (unsigned char) (8 * c + (*sp++ - '0'));
202 c = (unsigned char) (*sp);
208 c = (unsigned char) (*++sp & 0x1f);
212 c = (unsigned char) (*sp);
215 if (isgraph(c) && c != ',' && c != '\'' && c != '\\' && c != ':') {
216 dp = save_string(dp, "%\'");
217 dp = save_char(dp, c);
218 dp = save_char(dp, '\'');
220 dp = save_string(dp, "%{");
222 dp = save_char(dp, c / 100 + '0');
224 dp = save_char(dp, ((int) (c / 10)) % 10 + '0');
225 dp = save_char(dp, c % 10 + '0');
226 dp = save_char(dp, '}');
232 getparm(int parm, int n)
233 /* push n copies of param on the terminfo stack if not already there */
243 dp = save_string(dp, "%p");
244 dp = save_char(dp, '0' + parm);
247 if (onstack == parm) {
249 _nc_warning("string may not be optimal");
250 dp = save_string(dp, "%Pa");
252 dp = save_string(dp, "%ga");
262 if (seenn && parm < 3) {
263 dp = save_string(dp, "%{96}%^");
266 if (seenm && parm < 3) {
267 dp = save_string(dp, "%{127}%^");
272 * Convert a termcap string to terminfo format.
273 * 'cap' is the relevant terminfo capability index.
274 * 's' is the string value of the capability.
275 * 'parameterized' tells what type of translations to do:
276 * % translations if 1
277 * pad translations if >=0
279 NCURSES_EXPORT(char *)
280 _nc_captoinfo(const char *cap, const char *s, int const parameterized)
282 const char *capstart;
293 /* skip the initial padding (if we haven't been told not to) */
297 if (parameterized >= 0 && isdigit(UChar(*s)))
298 for (capstart = s;; s++)
299 if (!(isdigit(UChar(*s)) || *s == '*' || *s == '.'))
306 if (parameterized < 1) {
307 dp = save_char(dp, '%');
312 dp = save_char(dp, '%');
316 _nc_warning("saw %%r twice in %s", cap);
321 _nc_warning("saw %%m twice in %s", cap);
326 _nc_warning("saw %%n twice in %s", cap);
330 dp = save_string(dp, "%i");
335 dp = save_string(dp, "%{10}%/%{16}%*");
337 dp = save_string(dp, "%{10}%m%+");
342 dp = save_string(dp, "%{2}%*%-");
346 /* %?%{x}%>%t%{y}%+%; */
347 dp = save_string(dp, "%?");
349 dp = save_string(dp, "%>%t");
351 dp = save_string(dp, "%+%;");
354 if ((*s == '=' || *s == '+' || *s == '-'
355 || *s == '*' || *s == '/')
356 && (s[1] == 'p' || s[1] == 'c')
363 getparm(param + s[2] - '@', 1);
364 if (param != onstack) {
373 dp = save_string(dp, "%+");
376 dp = save_string(dp, "%-");
379 dp = save_string(dp, "%*");
382 dp = save_string(dp, "%/");
401 dp = save_string(dp, "%+");
406 dp = save_string(dp, "%+%c");
413 dp = save_string(dp, "%-");
416 dp = save_string(dp, "%s");
418 #endif /* WATERLOO */
423 dp = save_string(dp, "%-%c");
428 dp = save_string(dp, "%c");
431 case '0': /* not clear any of the historical termcaps did this */
439 dp = save_string(dp, "%2d");
445 dp = save_string(dp, "%3d");
450 dp = save_string(dp, "%d");
460 dp = save_string(dp, "%\\");
464 dp = save_char(dp, '%');
466 _nc_warning("unknown %% code %s (%#x) in %s",
467 unctrl((chtype) *s), UChar(*s), cap);
472 dp = save_char(dp, *s++);
478 * Now, if we stripped off some leading padding, add it at the end
479 * of the string as mandatory padding.
482 dp = save_string(dp, "$<");
483 for (s = capstart;; s++)
484 if (isdigit(UChar(*s)) || *s == '*' || *s == '.')
485 dp = save_char(dp, *s);
488 dp = save_string(dp, "/>");
491 (void) save_char(dp, '\0');
496 * Check for an expression that corresponds to "%B" (BCD):
497 * (parameter / 10) * 16 + (parameter % 10)
500 bcd_expression(const char *str)
502 /* leave this non-const for HPUX */
503 static char fmt[] = "%%p%c%%{10}%%/%%{16}%%*%%p%c%%{10}%%m%%+";
507 if (sscanf(str, fmt, &ch1, &ch2) == 2
508 && isdigit(UChar(ch1))
509 && isdigit(UChar(ch2))
516 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer)) fmt, ch1, ch2);
517 tst = strlen(buffer) - 1;
526 save_tc_char(char *bufptr, int c1)
530 if (is7bits(c1) && isprint(c1)) {
531 if (c1 == ':' || c1 == '\\')
532 bufptr = save_char(bufptr, '\\');
533 bufptr = save_char(bufptr, c1);
535 if (c1 == (c1 & 0x1f)) /* iscntrl() returns T on 255 */
536 _nc_STRCPY(temp, unctrl((chtype) c1), sizeof(temp));
538 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp)) "\\%03o", c1);
539 bufptr = save_string(bufptr, temp);
545 save_tc_inequality(char *bufptr, int c1, int c2)
547 bufptr = save_string(bufptr, "%>");
548 bufptr = save_tc_char(bufptr, c1);
549 bufptr = save_tc_char(bufptr, c2);
554 * Here are the capabilities infotocap assumes it can translate to:
557 * %d output value as in printf %d
558 * %2 output value as in printf %2d
559 * %3 output value as in printf %3d
560 * %. output value as in printf %c
561 * %+c add character c to value, then do %.
562 * %>xy if value > x then add y, no output
563 * %r reverse order of two parameters, no output
564 * %i increment by one, no output
565 * %n exclusive-or all parameters with 0140 (Datamedia 2500)
566 * %B BCD (16*(value/10)) + (value%10), no output
567 * %D Reverse coding (value - 2*(value%16)), no output (Delta Data).
568 * %m exclusive-or all parameters with 0177 (not in 4.4BSD)
572 * Convert a terminfo string to termcap format. Parameters are as in
575 NCURSES_EXPORT(char *)
576 _nc_infotocap(const char *cap GCC_UNUSED, const char *str, int const parameterized)
578 int seenone = 0, seentwo = 0, saw_m = 0, saw_n = 0;
580 const char *trimmed = 0;
582 char ch1 = 0, ch2 = 0;
583 char *bufptr = init_string();
586 bool syntax_error = FALSE;
588 /* we may have to move some trailing mandatory padding up front */
589 padding = str + strlen(str) - 1;
590 if (padding > str && *padding == '>') {
591 if (*--padding == '/')
593 while (isdigit(UChar(*padding)) || *padding == '.' || *padding == '*')
595 if (padding > str && *padding == '<' && *--padding == '$')
599 while (isdigit(UChar(*padding)) || *padding == '.' || *padding == '*')
600 bufptr = save_char(bufptr, *padding++);
603 for (; *str && ((trimmed == 0) || (str < trimmed)); str++) {
608 if (str[1] == '\0' || (str + 1) == trimmed) {
609 bufptr = save_string(bufptr, "\\136");
612 bufptr = save_char(bufptr, *str++);
613 bufptr = save_char(bufptr, *str);
615 } else if (str[0] == '\\') {
616 if (str[1] == '\0' || (str + 1) == trimmed) {
617 bufptr = save_string(bufptr, "\\134");
619 } else if (str[1] == '^') {
620 bufptr = save_string(bufptr, "\\136");
622 } else if (str[1] == ',') {
623 bufptr = save_char(bufptr, *++str);
627 bufptr = save_char(bufptr, *str++);
629 if (_nc_strict_bsd) {
630 if (isdigit(UChar(xx1))) {
633 if (!isdigit(UChar(str[1])))
635 else if (str[1] && !isdigit(UChar(str[2])))
639 * Test for "\0", "\00" or "\000" and transform those
643 && ((pad == 2) || (str[1] == '0'))
644 && ((pad >= 1) || (str[2] == '0'))) {
648 pad = 0; /* FIXME - optionally pad to 3 digits */
651 bufptr = save_char(bufptr, xx2);
654 } else if (strchr("E\\nrtbf", xx1) == 0) {
663 bufptr = save_char(bufptr, '0');
664 bufptr = save_char(bufptr, '4');
669 * Note: termcap documentation claims that ":"
670 * must be escaped as "\072", however the
671 * documentation is incorrect - read the code.
672 * The replacement does not work reliably,
673 * so the advice is not helpful.
675 bufptr = save_char(bufptr, '0');
676 bufptr = save_char(bufptr, '7');
680 /* should not happen, but handle this anyway */
681 _nc_SPRINTF(octal, _nc_SLIMIT(sizeof(octal))
683 bufptr = save_char(bufptr, octal[0]);
684 bufptr = save_char(bufptr, octal[1]);
690 bufptr = save_char(bufptr, xx1);
692 } else if (str[0] == '$' && str[1] == '<') { /* discard padding */
694 while (isdigit(UChar(*str))
701 } else if (sscanf(str,
702 "[%%?%%p1%%{8}%%<%%t%d%%p1%%d%%e%%p1%%{16}%%<%%t%d%%p1%%{8}%%-%%d%%e%d;5;%%p1%%d%%;m",
703 &in0, &in1, &in2) == 3
704 && ((in0 == 4 && in1 == 10 && in2 == 48)
705 || (in0 == 3 && in1 == 9 && in2 == 38))) {
706 /* dumb-down an optimized case from xterm-256color for termcap */
707 str = strstr(str, ";m");
710 bufptr = save_string(bufptr, "[48;5;%dm");
712 bufptr = save_string(bufptr, "[38;5;%dm");
714 } else if (str[0] == '%' && str[1] == '%') { /* escaped '%' */
715 bufptr = save_string(bufptr, "%%");
717 } else if (*str != '%' || (parameterized < 1)) {
718 bufptr = save_char(bufptr, *str);
719 } else if (sscanf(str, "%%?%%{%d}%%>%%t%%{%d}%%+%%;", &c1, &c2) == 2) {
720 str = strchr(str, ';');
721 bufptr = save_tc_inequality(bufptr, c1, c2);
722 } else if (sscanf(str, "%%?%%{%d}%%>%%t%%'%c'%%+%%;", &c1, &ch2) == 2) {
723 str = strchr(str, ';');
724 bufptr = save_tc_inequality(bufptr, c1, ch2);
725 } else if (sscanf(str, "%%?%%'%c'%%>%%t%%{%d}%%+%%;", &ch1, &c2) == 2) {
726 str = strchr(str, ';');
727 bufptr = save_tc_inequality(bufptr, ch1, c2);
728 } else if (sscanf(str, "%%?%%'%c'%%>%%t%%'%c'%%+%%;", &ch1, &ch2) == 2) {
729 str = strchr(str, ';');
730 bufptr = save_tc_inequality(bufptr, ch1, ch2);
731 } else if ((len = bcd_expression(str)) != 0) {
733 bufptr = save_string(bufptr, "%B");
734 } else if ((sscanf(str, "%%{%d}%%+%%c", &c1) == 1
735 || sscanf(str, "%%'%c'%%+%%c", &ch1) == 1)
736 && (cp = strchr(str, '+'))) {
738 bufptr = save_string(bufptr, "%+");
742 bufptr = save_tc_char(bufptr, c1);
744 /* FIXME: this "works" for 'delta' */
745 else if (strncmp(str, "%{2}%*%-", (size_t) 8) == 0) {
747 bufptr = save_string(bufptr, "%D");
748 } else if (strncmp(str, "%{96}%^", (size_t) 7) == 0) {
751 bufptr = save_string(bufptr, "%n");
753 } else if (strncmp(str, "%{127}%^", (size_t) 8) == 0) {
756 bufptr = save_string(bufptr, "%m");
758 } else { /* cm-style format element */
762 bufptr = save_char(bufptr, '%');
775 bufptr = save_char(bufptr, '%');
778 while (isdigit(UChar(*str))) {
781 if (_nc_strict_bsd) {
785 bufptr = save_char(bufptr, ch1);
788 if (_nc_strict_bsd) {
789 if (ch2 != 0 && ch2 != '0')
793 bufptr = save_char(bufptr, ch1);
795 if (strchr("doxX.", *str)) {
796 if (*str != 'd') /* termcap doesn't have octal, hex */
802 bufptr = save_string(bufptr, "%d");
806 bufptr = save_string(bufptr, "%.");
810 * %s isn't in termcap, but it's convenient to pass it through
811 * so we can represent things like terminfo pfkey strings in
817 bufptr = save_string(bufptr, "%s");
824 else if (*str == '2') {
825 if (!seenone && !seentwo) {
826 bufptr = save_string(bufptr, "%r");
829 } else if (*str >= '3')
834 bufptr = save_string(bufptr, "%i");
838 bufptr = save_char(bufptr, *str);
841 } /* endswitch (*str) */
842 } /* endelse (*str == '%') */
845 * 'str' always points to the end of what was scanned in this step,
846 * but that may not be the end of the string.
852 } /* endwhile (*str) */
854 return (syntax_error ? NULL : my_string);
862 main(int argc, char *argv[])
866 while ((c = getopt(argc, argv, "c")) != EOF)
878 if (fgets(buf, sizeof(buf), stdin) == 0)
880 buf[strlen(buf) - 1] = '\0';
884 char *cp = _nc_infotocap("to termcap", buf, 1);
887 (void) fputs(cp, stdout);
889 (void) fputs(_nc_captoinfo("to terminfo", buf, 1), stdout);
890 (void) putchar('\n');
898 _nc_captoinfo_leaks(void)
900 if (my_string != 0) {
901 FreeAndNull(my_string);