summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJean-Philippe Andre <jp.andre@samsung.com>2015-06-24 13:46:48 +0900
committerJean-Philippe Andre <jp.andre@samsung.com>2015-06-24 13:46:48 +0900
commit09b4fddf84ad2d106e7f038cde35ead33af47a2f (patch)
tree6b869ab792cb89ebaaf929028481ea10da2af025 /src
parent0fa0a5d9c8bc389c58af33f93784e839bd173711 (diff)
escapes: pretty-print some escapse and avoid ESC in debug logs
ERR("Hello '%c'", ESC) would result in "Hello '\033'" which means the parent terminal (another terminology, maybe) would then try to interpret ' as an escape char (ESC is \033 or 0x1b), and so it would then forward more escapes to the parent. Also, avoid printing BEL (and, well, beeping) when we are just printing an error log. I believe all those "Unhandled escape" logs should go to WRN or higher log levels. Any thoughts, @billiob ? See T2506 (for the test file)
Diffstat (limited to 'src')
-rw-r--r--src/bin/termptyesc.c78
1 files changed, 66 insertions, 12 deletions
diff --git a/src/bin/termptyesc.c b/src/bin/termptyesc.c
index 94831c9..1d9adc4 100644
--- a/src/bin/termptyesc.c
+++ b/src/bin/termptyesc.c
@@ -25,11 +25,67 @@
25#define ST 0x9c // String Terminator 25#define ST 0x9c // String Terminator
26#define BEL 0x07 // Bell 26#define BEL 0x07 // Bell
27#define ESC 033 // Escape 27#define ESC 033 // Escape
28#define DEL 127
28 29
29/* XXX: all handle_ functions return the number of bytes successfully read, 0 30/* XXX: all handle_ functions return the number of bytes successfully read, 0
30 * if not enough bytes could be read 31 * if not enough bytes could be read
31 */ 32 */
32 33
34static const char *ASCII_CHARS_TABLE[] =
35{
36 "NUL", // '\0'
37 "SOH", // '\001'
38 "STX", // '\002'
39 "ETX", // '\003'
40 "EOT", // '\004'
41 "ENQ", // '\005'
42 "ACK", // '\006'
43 "BEL", // '\007'
44 "BS", // '\010'
45 "HT", // '\011'
46 "LF", // '\012'
47 "VT", // '\013'
48 "FF", // '\014'
49 "CR" , // '\015'
50 "SO", // '\016'
51 "SI", // '\017'
52 "DLE", // '\020'
53 "DC1", // '\021'
54 "DC2", // '\022'
55 "DC3", // '\023'
56 "DC4", // '\024'
57 "NAK", // '\025'
58 "SYN", // '\026'
59 "ETB", // '\027'
60 "CAN", // '\030'
61 "EM", // '\031'
62 "SUB", // '\032'
63 "ESC", // '\033'
64 "FS", // '\034'
65 "GS", // '\035'
66 "RS", // '\036'
67 "US" // '\037'
68};
69
70static const char *
71_safechar(unsigned int c)
72{
73 static char _str[9];
74
75 // This should avoid 'BEL' and 'ESC' in particular, which would
76 // have side effects in the parent terminal (esp. ESC).
77 if (c < (sizeof(ASCII_CHARS_TABLE) / sizeof(ASCII_CHARS_TABLE[0])))
78 return ASCII_CHARS_TABLE[c];
79
80 if (c == DEL)
81 return "DEL";
82
83 // The rest should be safe (?)
84 snprintf(_str, 9, "%c", c);
85 _str[8] = '\0';
86 return _str;
87}
88
33static int 89static int
34_csi_arg_get(Eina_Unicode **ptr) 90_csi_arg_get(Eina_Unicode **ptr)
35{ 91{
@@ -660,7 +716,7 @@ _handle_esc_csi(Termpty *ty, const Eina_Unicode *c, Eina_Unicode *ce)
660 if (cc == ce) return 0; 716 if (cc == ce) return 0;
661 *b = 0; 717 *b = 0;
662 b = buf; 718 b = buf;
663 DBG(" CSI: '%c' args '%s'", (int) *cc, (char *) buf); 719 DBG(" CSI: '%s' args '%s'", _safechar(*cc), (char *) buf);
664 switch (*cc) 720 switch (*cc)
665 { 721 {
666 case 'm': // color set 722 case 'm': // color set
@@ -1052,7 +1108,7 @@ unhandled:
1052 else 1108 else
1053 eina_strbuf_append_char(bf, c[i]); 1109 eina_strbuf_append_char(bf, c[i]);
1054 } 1110 }
1055 ERR("unhandled CSI '%c': %s", (int) *cc, eina_strbuf_string_get(bf)); 1111 ERR("unhandled CSI '%s': %s", _safechar(*cc), eina_strbuf_string_get(bf));
1056 eina_strbuf_free(bf); 1112 eina_strbuf_free(bf);
1057 } 1113 }
1058 cc++; 1114 cc++;
@@ -1491,8 +1547,7 @@ _handle_esc_dcs(Termpty *ty EINA_UNUSED, const Eina_Unicode *c, const Eina_Unico
1491 /* Request status string */ 1547 /* Request status string */
1492 if (len > 1 && buf[1] != 'q') 1548 if (len > 1 && buf[1] != 'q')
1493 { 1549 {
1494 ERR("invalid/unhandled dsc esc '$%c' (expected '$q')", 1550 ERR("invalid/unhandled dsc esc '$%s' (expected '$q')", _safechar(buf[1]));
1495 (int) buf[1]);
1496 goto end; 1551 goto end;
1497 } 1552 }
1498 if (len < 4) 1553 if (len < 4)
@@ -1513,7 +1568,7 @@ _handle_esc_dcs(Termpty *ty EINA_UNUSED, const Eina_Unicode *c, const Eina_Unico
1513 } 1568 }
1514 else 1569 else
1515 { 1570 {
1516 ERR("invalid/unhandled dsc esc '$q\"%c'", (int) buf[3]); 1571 ERR("invalid/unhandled dsc esc '$q\"%s'", _safechar(buf[3]));
1517 goto end; 1572 goto end;
1518 } 1573 }
1519 break; 1574 break;
@@ -1522,15 +1577,14 @@ _handle_esc_dcs(Termpty *ty EINA_UNUSED, const Eina_Unicode *c, const Eina_Unico
1522 case 'r': /* DECSTBM */ 1577 case 'r': /* DECSTBM */
1523 /* TODO: */ 1578 /* TODO: */
1524 default: 1579 default:
1525 ERR("unhandled dsc request status string '$q%c'", 1580 ERR("unhandled dsc request status string '$q%s'", _safechar(buf[2]));
1526 (int) buf[2]);
1527 goto end; 1581 goto end;
1528 } 1582 }
1529 /* TODO */ 1583 /* TODO */
1530 break; 1584 break;
1531 default: 1585 default:
1532 // many others 1586 // many others
1533 ERR("Unhandled DCS escape '%c'", (int) buf[0]); 1587 ERR("Unhandled DCS escape '%s'", _safechar(buf[0]));
1534 break; 1588 break;
1535 } 1589 }
1536end: 1590end:
@@ -1543,7 +1597,7 @@ _handle_esc(Termpty *ty, const Eina_Unicode *c, Eina_Unicode *ce)
1543 int len = ce - c; 1597 int len = ce - c;
1544 1598
1545 if (len < 1) return 0; 1599 if (len < 1) return 0;
1546 DBG("ESC: '%c'", (int) c[0]); 1600 DBG("ESC: '%s'", _safechar(c[0]));
1547 switch (c[0]) 1601 switch (c[0])
1548 { 1602 {
1549 case '[': 1603 case '[':
@@ -1664,7 +1718,7 @@ _handle_esc(Termpty *ty, const Eina_Unicode *c, Eina_Unicode *ce)
1664 return 1; 1718 return 1;
1665 */ 1719 */
1666 default: 1720 default:
1667 ERR("Unhandled escape '%c' (0x%02x)", (int) c[0], (unsigned int) c[0]); 1721 ERR("Unhandled escape '%s' (0x%02x)", _safechar(c[0]), (unsigned int) c[0]);
1668 return 1; 1722 return 1;
1669 } 1723 }
1670 return 0; 1724 return 0;
@@ -1685,7 +1739,7 @@ termpty_handle_seq(Termpty *ty, Eina_Unicode *c, Eina_Unicode *ce)
1685 if ((c[j] < ' ') || (c[j] >= 0x7f)) 1739 if ((c[j] < ' ') || (c[j] >= 0x7f))
1686 printf("\033[35m%08x\033[0m", c[j]); 1740 printf("\033[35m%08x\033[0m", c[j]);
1687 else 1741 else
1688 printf("%c", c[j]); 1742 printf("%s", _safechar(c[j]));
1689 } 1743 }
1690 printf("\n"); 1744 printf("\n");
1691 */ 1745 */
@@ -1839,7 +1893,7 @@ termpty_handle_seq(Termpty *ty, Eina_Unicode *c, Eina_Unicode *ce)
1839 DBG("txt: ["); 1893 DBG("txt: [");
1840 while ((cc < ce) && (*cc >= 0x20) && (*cc != 0x7f)) 1894 while ((cc < ce) && (*cc >= 0x20) && (*cc != 0x7f))
1841 { 1895 {
1842 DBG("%c", (int) *cc); 1896 DBG("%s", _safechar(*cc));
1843 cc++; 1897 cc++;
1844 len++; 1898 len++;
1845 } 1899 }