term.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2010, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Terminal Routines
  21. *
  22. * \author Mark Spencer <markster@digium.com>
  23. */
  24. /*** MODULEINFO
  25. <support_level>core</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  29. #include "asterisk/_private.h"
  30. #include <sys/time.h>
  31. #include <signal.h>
  32. #include <sys/stat.h>
  33. #include <fcntl.h>
  34. #include "asterisk/term.h"
  35. #include "asterisk/lock.h"
  36. #include "asterisk/utils.h"
  37. #include "asterisk/threadstorage.h"
  38. static int vt100compat;
  39. static char prepdata[80] = "";
  40. static char enddata[80] = "";
  41. static char quitdata[80] = "";
  42. static const char * const termpath[] = {
  43. "/usr/share/terminfo",
  44. "/usr/local/share/misc/terminfo",
  45. "/usr/lib/terminfo",
  46. NULL
  47. };
  48. AST_THREADSTORAGE(commonbuf);
  49. struct commonbuf {
  50. short which;
  51. char buffer[AST_TERM_MAX_ROTATING_BUFFERS][AST_TERM_MAX_ESCAPE_CHARS];
  52. };
  53. static int opposite(int color)
  54. {
  55. int lookup[] = {
  56. /* BLACK */ COLOR_BLACK,
  57. /* RED */ COLOR_MAGENTA,
  58. /* GREEN */ COLOR_GREEN,
  59. /* BROWN */ COLOR_BROWN,
  60. /* BLUE */ COLOR_CYAN,
  61. /* MAGENTA */ COLOR_RED,
  62. /* CYAN */ COLOR_BLUE,
  63. /* WHITE */ COLOR_BLACK };
  64. return color ? lookup[color - 30] : 0;
  65. }
  66. /* Ripped off from Ross Ridge, but it's public domain code (libmytinfo) */
  67. static short convshort(char *s)
  68. {
  69. register int a, b;
  70. a = (int) s[0] & 0377;
  71. b = (int) s[1] & 0377;
  72. if (a == 0377 && b == 0377)
  73. return -1;
  74. if (a == 0376 && b == 0377)
  75. return -2;
  76. return a + b * 256;
  77. }
  78. int ast_term_init(void)
  79. {
  80. char *term = getenv("TERM");
  81. char termfile[256] = "";
  82. char buffer[512] = "";
  83. int termfd = -1, parseokay = 0, i;
  84. if (ast_opt_no_color) {
  85. return 0;
  86. }
  87. if (!ast_opt_console) {
  88. /* If any remote console is not compatible, we'll strip the color codes at that point */
  89. vt100compat = 1;
  90. goto end;
  91. }
  92. if (!term) {
  93. return 0;
  94. }
  95. for (i = 0;; i++) {
  96. if (termpath[i] == NULL) {
  97. break;
  98. }
  99. snprintf(termfile, sizeof(termfile), "%s/%c/%s", termpath[i], *term, term);
  100. termfd = open(termfile, O_RDONLY);
  101. if (termfd > -1) {
  102. break;
  103. }
  104. }
  105. if (termfd > -1) {
  106. int actsize = read(termfd, buffer, sizeof(buffer) - 1);
  107. short sz_names = convshort(buffer + 2);
  108. short sz_bools = convshort(buffer + 4);
  109. short n_nums = convshort(buffer + 6);
  110. /* if ((sz_names + sz_bools) & 1)
  111. sz_bools++; */
  112. if (sz_names + sz_bools + n_nums < actsize) {
  113. /* Offset 13 is defined in /usr/include/term.h, though we do not
  114. * include it here, as it conflicts with include/asterisk/term.h */
  115. short max_colors = convshort(buffer + 12 + sz_names + sz_bools + 13 * 2);
  116. if (max_colors > 0) {
  117. vt100compat = 1;
  118. }
  119. parseokay = 1;
  120. }
  121. close(termfd);
  122. }
  123. if (!parseokay) {
  124. /* These comparisons should not be substrings nor case-insensitive, as
  125. * terminal types are very particular about how they treat suffixes and
  126. * capitalization. For example, terminal type 'linux-m' does NOT
  127. * support color, while 'linux' does. Not even all vt100* terminals
  128. * support color, either (e.g. 'vt100+fnkeys'). */
  129. if (!strcmp(term, "linux")) {
  130. vt100compat = 1;
  131. } else if (!strcmp(term, "xterm")) {
  132. vt100compat = 1;
  133. } else if (!strcmp(term, "xterm-color")) {
  134. vt100compat = 1;
  135. } else if (!strcmp(term, "xterm-256color")) {
  136. vt100compat = 1;
  137. } else if (!strncmp(term, "Eterm", 5)) {
  138. /* Both entries which start with Eterm support color */
  139. vt100compat = 1;
  140. } else if (!strcmp(term, "vt100")) {
  141. vt100compat = 1;
  142. } else if (!strncmp(term, "crt", 3)) {
  143. /* Both crt terminals support color */
  144. vt100compat = 1;
  145. }
  146. }
  147. end:
  148. if (vt100compat) {
  149. /* Make commands show up in nice colors */
  150. if (ast_opt_light_background) {
  151. snprintf(prepdata, sizeof(prepdata), "%c[%dm", ESC, COLOR_BROWN);
  152. snprintf(enddata, sizeof(enddata), "%c[%dm", ESC, COLOR_BLACK);
  153. } else if (ast_opt_force_black_background) {
  154. snprintf(prepdata, sizeof(prepdata), "%c[%d;%d;%dm", ESC, ATTR_BRIGHT, COLOR_BROWN, COLOR_BLACK + 10);
  155. snprintf(enddata, sizeof(enddata), "%c[%d;%d;%dm", ESC, ATTR_RESET, COLOR_WHITE, COLOR_BLACK + 10);
  156. } else {
  157. snprintf(prepdata, sizeof(prepdata), "%c[%d;%dm", ESC, ATTR_BRIGHT, COLOR_BROWN);
  158. snprintf(enddata, sizeof(enddata), "%c[%dm", ESC, ATTR_RESET);
  159. }
  160. snprintf(quitdata, sizeof(quitdata), "%c[%dm", ESC, ATTR_RESET);
  161. }
  162. return 0;
  163. }
  164. char *term_color(char *outbuf, const char *inbuf, int fgcolor, int bgcolor, int maxout)
  165. {
  166. int attr = 0;
  167. if (!vt100compat) {
  168. ast_copy_string(outbuf, inbuf, maxout);
  169. return outbuf;
  170. }
  171. if (!fgcolor) {
  172. ast_copy_string(outbuf, inbuf, maxout);
  173. return outbuf;
  174. }
  175. if (fgcolor & 128) {
  176. attr = ast_opt_light_background ? 0 : ATTR_BRIGHT;
  177. fgcolor &= ~128;
  178. }
  179. if (bgcolor) {
  180. bgcolor &= ~128;
  181. }
  182. if (ast_opt_light_background) {
  183. fgcolor = opposite(fgcolor);
  184. }
  185. if (ast_opt_force_black_background) {
  186. if (!bgcolor) {
  187. bgcolor = COLOR_BLACK;
  188. }
  189. snprintf(outbuf, maxout, "%c[%d;%d;%dm%s%s", ESC, attr, fgcolor, bgcolor + 10, inbuf, term_end());
  190. } else {
  191. snprintf(outbuf, maxout, "%c[%d;%dm%s%s", ESC, attr, fgcolor, inbuf, term_end());
  192. }
  193. return outbuf;
  194. }
  195. static void check_fgcolor(int *fgcolor, int *attr)
  196. {
  197. *attr = ast_opt_light_background ? 0 : ATTR_BRIGHT;
  198. if (*fgcolor & 128) {
  199. *fgcolor &= ~128;
  200. }
  201. if (ast_opt_light_background) {
  202. *fgcolor = opposite(*fgcolor);
  203. }
  204. }
  205. static void check_bgcolor(int *bgcolor)
  206. {
  207. if (*bgcolor) {
  208. *bgcolor &= ~128;
  209. }
  210. }
  211. static int check_colors_allowed(void)
  212. {
  213. return vt100compat;
  214. }
  215. int ast_term_color_code(struct ast_str **str, int fgcolor, int bgcolor)
  216. {
  217. int attr = 0;
  218. if (!check_colors_allowed()) {
  219. return -1;
  220. }
  221. check_fgcolor(&fgcolor, &attr);
  222. check_bgcolor(&bgcolor);
  223. if (ast_opt_force_black_background) {
  224. ast_str_append(str, 0, "%c[%d;%d;%dm", ESC, attr, fgcolor, COLOR_BLACK + 10);
  225. } else if (bgcolor) {
  226. ast_str_append(str, 0, "%c[%d;%d;%dm", ESC, attr, fgcolor, bgcolor + 10);
  227. } else {
  228. ast_str_append(str, 0, "%c[%d;%dm", ESC, attr, fgcolor);
  229. }
  230. return 0;
  231. }
  232. char *term_color_code(char *outbuf, int fgcolor, int bgcolor, int maxout)
  233. {
  234. int attr = 0;
  235. if (!check_colors_allowed()) {
  236. *outbuf = '\0';
  237. return outbuf;
  238. }
  239. check_fgcolor(&fgcolor, &attr);
  240. check_bgcolor(&bgcolor);
  241. if (ast_opt_force_black_background) {
  242. snprintf(outbuf, maxout, "%c[%d;%d;%dm", ESC, attr, fgcolor, COLOR_BLACK + 10);
  243. } else if (bgcolor) {
  244. snprintf(outbuf, maxout, "%c[%d;%d;%dm", ESC, attr, fgcolor, bgcolor + 10);
  245. } else {
  246. snprintf(outbuf, maxout, "%c[%d;%dm", ESC, attr, fgcolor);
  247. }
  248. return outbuf;
  249. }
  250. const char *ast_term_color(int fgcolor, int bgcolor)
  251. {
  252. struct commonbuf *cb = ast_threadstorage_get(&commonbuf, sizeof(*cb));
  253. char *buf;
  254. if (!cb) {
  255. return "";
  256. }
  257. buf = cb->buffer[cb->which++];
  258. if (cb->which == AST_TERM_MAX_ROTATING_BUFFERS) {
  259. cb->which = 0;
  260. }
  261. return term_color_code(buf, fgcolor, bgcolor, AST_TERM_MAX_ESCAPE_CHARS);
  262. }
  263. const char *ast_term_reset(void)
  264. {
  265. return term_end();
  266. }
  267. char *term_strip(char *outbuf, const char *inbuf, int maxout)
  268. {
  269. char *outbuf_ptr = outbuf;
  270. const char *inbuf_ptr = inbuf;
  271. while (outbuf_ptr < outbuf + maxout) {
  272. switch (*inbuf_ptr) {
  273. case ESC:
  274. while (*inbuf_ptr && (*inbuf_ptr != 'm'))
  275. inbuf_ptr++;
  276. break;
  277. default:
  278. *outbuf_ptr = *inbuf_ptr;
  279. outbuf_ptr++;
  280. }
  281. if (! *inbuf_ptr)
  282. break;
  283. inbuf_ptr++;
  284. }
  285. return outbuf;
  286. }
  287. char *term_prompt(char *outbuf, const char *inbuf, int maxout)
  288. {
  289. if (!vt100compat) {
  290. ast_copy_string(outbuf, inbuf, maxout);
  291. return outbuf;
  292. }
  293. if (ast_opt_force_black_background) {
  294. snprintf(outbuf, maxout, "%c[%d;%d;%dm%c%c[%d;%dm%s",
  295. ESC, ATTR_BRIGHT, COLOR_BLUE, COLOR_BLACK + 10,
  296. inbuf[0],
  297. ESC, COLOR_WHITE, COLOR_BLACK + 10,
  298. inbuf + 1);
  299. } else if (ast_opt_light_background) {
  300. snprintf(outbuf, maxout, "%c[%d;0m%c%c[0m%s",
  301. ESC, COLOR_BLUE,
  302. inbuf[0],
  303. ESC,
  304. inbuf + 1);
  305. } else {
  306. snprintf(outbuf, maxout, "%c[%d;%d;0m%c%c[0m%s",
  307. ESC, ATTR_BRIGHT, COLOR_BLUE,
  308. inbuf[0],
  309. ESC,
  310. inbuf + 1);
  311. }
  312. return outbuf;
  313. }
  314. /* filter escape sequences */
  315. void term_filter_escapes(char *line)
  316. {
  317. int i;
  318. int len = strlen(line);
  319. for (i = 0; i < len; i++) {
  320. if (line[i] != ESC)
  321. continue;
  322. if ((i < (len - 2)) &&
  323. (line[i + 1] == 0x5B)) {
  324. switch (line[i + 2]) {
  325. case 0x30:
  326. case 0x31:
  327. case 0x33:
  328. continue;
  329. }
  330. }
  331. /* replace ESC with a space */
  332. line[i] = ' ';
  333. }
  334. }
  335. const char *term_prep(void)
  336. {
  337. return prepdata;
  338. }
  339. const char *term_end(void)
  340. {
  341. return enddata;
  342. }
  343. const char *term_quit(void)
  344. {
  345. return quitdata;
  346. }