1 /****************************************************************************
2 * Copyright (c) 1998-2010,2011 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.64 2011/07/23 20:36:28 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;
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 */
243 if (onstack == parm) {
245 _nc_warning("string may not be optimal");
246 dp = save_string(dp, "%Pa");
248 dp = save_string(dp, "%ga");
259 dp = save_string(dp, "%p");
260 dp = save_char(dp, '0' + parm);
263 if (seenn && parm < 3) {
264 dp = save_string(dp, "%{96}%^");
267 if (seenm && parm < 3) {
268 dp = save_string(dp, "%{127}%^");
273 * Convert a termcap string to terminfo format.
274 * 'cap' is the relevant terminfo capability index.
275 * 's' is the string value of the capability.
276 * 'parameterized' tells what type of translations to do:
277 * % translations if 1
278 * pad translations if >=0
280 NCURSES_EXPORT(char *)
281 _nc_captoinfo(const char *cap, const char *s, int const parameterized)
283 const char *capstart;
294 /* skip the initial padding (if we haven't been told not to) */
298 if (parameterized >= 0 && isdigit(UChar(*s)))
299 for (capstart = s;; s++)
300 if (!(isdigit(UChar(*s)) || *s == '*' || *s == '.'))
307 if (parameterized < 1) {
308 dp = save_char(dp, '%');
313 dp = save_char(dp, '%');
317 _nc_warning("saw %%r twice in %s", cap);
322 _nc_warning("saw %%m twice in %s", cap);
327 _nc_warning("saw %%n twice in %s", cap);
331 dp = save_string(dp, "%i");
336 dp = save_string(dp, "%{10}%/%{16}%*");
338 dp = save_string(dp, "%{10}%m%+");
343 dp = save_string(dp, "%{2}%*%-");
347 /* %?%{x}%>%t%{y}%+%; */
348 dp = save_string(dp, "%?");
350 dp = save_string(dp, "%>%t");
352 dp = save_string(dp, "%+%;");
355 if ((*s == '=' || *s == '+' || *s == '-'
356 || *s == '*' || *s == '/')
357 && (s[1] == 'p' || s[1] == 'c')
364 getparm(param + s[2] - '@', 1);
365 if (param != onstack) {
374 dp = save_string(dp, "%+");
377 dp = save_string(dp, "%-");
380 dp = save_string(dp, "%*");
383 dp = save_string(dp, "%/");
402 dp = save_string(dp, "%+");
407 dp = save_string(dp, "%+%c");
414 dp = save_string(dp, "%-");
417 dp = save_string(dp, "%s");
419 #endif /* WATERLOO */
424 dp = save_string(dp, "%-%c");
429 dp = save_string(dp, "%c");
432 case '0': /* not clear any of the historical termcaps did this */
440 dp = save_string(dp, "%2d");
446 dp = save_string(dp, "%3d");
451 dp = save_string(dp, "%d");
461 dp = save_string(dp, "%\\");
465 dp = save_char(dp, '%');
467 _nc_warning("unknown %% code %s (%#x) in %s",
468 unctrl((chtype) *s), UChar(*s), cap);
473 dp = save_char(dp, *s++);
479 * Now, if we stripped off some leading padding, add it at the end
480 * of the string as mandatory padding.
483 dp = save_string(dp, "$<");
484 for (s = capstart;; s++)
485 if (isdigit(UChar(*s)) || *s == '*' || *s == '.')
486 dp = save_char(dp, *s);
489 dp = save_string(dp, "/>");
492 (void) save_char(dp, '\0');
497 * Check for an expression that corresponds to "%B" (BCD):
498 * (parameter / 10) * 16 + (parameter % 10)
501 bcd_expression(const char *str)
503 /* leave this non-const for HPUX */
504 static char fmt[] = "%%p%c%%{10}%%/%%{16}%%*%%p%c%%{10}%%m%%+";
508 if (sscanf(str, fmt, &ch1, &ch2) == 2
509 && isdigit(UChar(ch1))
510 && isdigit(UChar(ch2))
517 sprintf(buffer, fmt, ch1, ch2);
518 tst = strlen(buffer) - 1;
527 save_tc_char(char *bufptr, int c1)
531 if (is7bits(c1) && isprint(c1)) {
532 if (c1 == ':' || c1 == '\\')
533 bufptr = save_char(bufptr, '\\');
534 bufptr = save_char(bufptr, c1);
536 if (c1 == (c1 & 0x1f)) /* iscntrl() returns T on 255 */
537 (void) strcpy(temp, unctrl((chtype) c1));
539 (void) sprintf(temp, "\\%03o", c1);
540 bufptr = save_string(bufptr, temp);
546 save_tc_inequality(char *bufptr, int c1, int c2)
548 bufptr = save_string(bufptr, "%>");
549 bufptr = save_tc_char(bufptr, c1);
550 bufptr = save_tc_char(bufptr, c2);
555 * Here are the capabilities infotocap assumes it can translate to:
558 * %d output value as in printf %d
559 * %2 output value as in printf %2d
560 * %3 output value as in printf %3d
561 * %. output value as in printf %c
562 * %+c add character c to value, then do %.
563 * %>xy if value > x then add y, no output
564 * %r reverse order of two parameters, no output
565 * %i increment by one, no output
566 * %n exclusive-or all parameters with 0140 (Datamedia 2500)
567 * %B BCD (16*(value/10)) + (value%10), no output
568 * %D Reverse coding (value - 2*(value%16)), no output (Delta Data).
569 * %m exclusive-or all parameters with 0177 (not in 4.4BSD)
573 * Convert a terminfo string to termcap format. Parameters are as in
576 NCURSES_EXPORT(char *)
577 _nc_infotocap(const char *cap GCC_UNUSED, const char *str, int const parameterized)
579 int strict_bsd = 1; /* FIXME - consider making this an option */
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 == '>' && *--padding == '/') {
594 while (isdigit(UChar(*padding)) || *padding == '.' || *padding == '*')
596 if (padding > str && *padding == '<' && *--padding == '$')
600 while (isdigit(UChar(*padding)) || *padding == '.' || *padding == '*')
601 bufptr = save_char(bufptr, *padding++);
604 for (; *str && str != trimmed; str++) {
609 if (str[1] == '\0' || (str + 1) == trimmed) {
610 bufptr = save_string(bufptr, "\\136");
613 bufptr = save_char(bufptr, *str++);
614 bufptr = save_char(bufptr, *str);
616 } else if (str[0] == '\\') {
617 if (str[1] == '\0' || (str + 1) == trimmed) {
618 bufptr = save_string(bufptr, "\\134");
620 } else if (str[1] == '^') {
621 bufptr = save_string(bufptr, "\\136");
623 } else if (str[1] == ',') {
624 bufptr = save_char(bufptr, *++str);
628 bufptr = save_char(bufptr, *str++);
631 if (isdigit(UChar(xx1))) {
634 if (!isdigit(UChar(str[1])))
636 else if (str[1] && !isdigit(UChar(str[2])))
640 * Test for "\0", "\00" or "\000" and transform those
644 && ((pad == 2) || (str[1] == '0'))
645 && ((pad >= 1) || (str[2] == '0'))) {
649 pad = 0; /* FIXME - optionally pad to 3 digits */
652 bufptr = save_char(bufptr, xx2);
655 } else if (strchr("E\\:nrtbf", xx1) == 0) {
657 * Note: termcap documentation claims that ":" must be
658 * escaped as "\072", however the documentation is
659 * incorrect - read the code.
666 bufptr = save_char(bufptr, '0');
667 bufptr = save_char(bufptr, '4');
671 /* should not happen, but handle this anyway */
672 sprintf(octal, "%03o", UChar(xx1));
673 bufptr = save_char(bufptr, octal[0]);
674 bufptr = save_char(bufptr, octal[1]);
680 bufptr = save_char(bufptr, xx1);
682 } else if (str[0] == '$' && str[1] == '<') { /* discard padding */
684 while (isdigit(UChar(*str))
691 } else if (sscanf(str,
692 "[%%?%%p1%%{8}%%<%%t%d%%p1%%d%%e%%p1%%{16}%%<%%t%d%%p1%%{8}%%-%%d%%e%d;5;%%p1%%d%%;m",
693 &in0, &in1, &in2) == 3
694 && ((in0 == 4 && in1 == 10 && in2 == 48)
695 || (in0 == 3 && in1 == 9 && in2 == 38))) {
696 /* dumb-down an optimized case from xterm-256color for termcap */
697 str = strstr(str, ";m");
700 bufptr = save_string(bufptr, "[48;5;%dm");
702 bufptr = save_string(bufptr, "[38;5;%dm");
704 } else if (str[0] == '%' && str[1] == '%') { /* escaped '%' */
705 bufptr = save_string(bufptr, "%%");
707 } else if (*str != '%' || (parameterized < 1)) {
708 bufptr = save_char(bufptr, *str);
709 } else if (sscanf(str, "%%?%%{%d}%%>%%t%%{%d}%%+%%;", &c1, &c2) == 2) {
710 str = strchr(str, ';');
711 bufptr = save_tc_inequality(bufptr, c1, c2);
712 } else if (sscanf(str, "%%?%%{%d}%%>%%t%%'%c'%%+%%;", &c1, &ch2) == 2) {
713 str = strchr(str, ';');
714 bufptr = save_tc_inequality(bufptr, c1, c2);
715 } else if (sscanf(str, "%%?%%'%c'%%>%%t%%{%d}%%+%%;", &ch1, &c2) == 2) {
716 str = strchr(str, ';');
717 bufptr = save_tc_inequality(bufptr, c1, c2);
718 } else if (sscanf(str, "%%?%%'%c'%%>%%t%%'%c'%%+%%;", &ch1, &ch2) == 2) {
719 str = strchr(str, ';');
720 bufptr = save_tc_inequality(bufptr, c1, c2);
721 } else if ((len = bcd_expression(str)) != 0) {
723 bufptr = save_string(bufptr, "%B");
724 } else if ((sscanf(str, "%%{%d}%%+%%c", &c1) == 1
725 || sscanf(str, "%%'%c'%%+%%c", &ch1) == 1)
726 && (cp = strchr(str, '+'))) {
728 bufptr = save_string(bufptr, "%+");
732 bufptr = save_tc_char(bufptr, c1);
734 /* FIXME: this "works" for 'delta' */
735 else if (strncmp(str, "%{2}%*%-", 8) == 0) {
737 bufptr = save_string(bufptr, "%D");
738 } else if (strncmp(str, "%{96}%^", 7) == 0) {
741 bufptr = save_string(bufptr, "%n");
743 } else if (strncmp(str, "%{127}%^", 8) == 0) {
746 bufptr = save_string(bufptr, "%m");
748 } else { /* cm-style format element */
752 bufptr = save_char(bufptr, '%');
765 bufptr = save_char(bufptr, '%');
768 while (isdigit(UChar(*str))) {
775 bufptr = save_char(bufptr, ch1);
779 if (ch2 != 0 && ch2 != '0')
783 bufptr = save_char(bufptr, ch1);
785 if (strchr("doxX.", *str)) {
786 if (*str != 'd') /* termcap doesn't have octal, hex */
792 bufptr = save_string(bufptr, "%d");
796 bufptr = save_string(bufptr, "%.");
800 * %s isn't in termcap, but it's convenient to pass it through
801 * so we can represent things like terminfo pfkey strings in
807 bufptr = save_string(bufptr, "%s");
814 else if (*str == '2') {
815 if (!seenone && !seentwo) {
816 bufptr = save_string(bufptr, "%r");
819 } else if (*str >= '3')
824 bufptr = save_string(bufptr, "%i");
828 bufptr = save_char(bufptr, *str);
831 } /* endswitch (*str) */
832 } /* endelse (*str == '%') */
835 * 'str' always points to the end of what was scanned in this step,
836 * but that may not be the end of the string.
842 } /* endwhile (*str) */
844 return (syntax_error ? NULL : my_string);
852 main(int argc, char *argv[])
856 while ((c = getopt(argc, argv, "c")) != EOF)
868 if (fgets(buf, sizeof(buf), stdin) == 0)
870 buf[strlen(buf) - 1] = '\0';
874 char *cp = _nc_infotocap("to termcap", buf, 1);
877 (void) fputs(cp, stdout);
879 (void) fputs(_nc_captoinfo("to terminfo", buf, 1), stdout);
880 (void) putchar('\n');
888 _nc_captoinfo_leaks(void)
890 if (my_string != 0) {
891 FreeAndNull(my_string);