term.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. snprintf(quitdata, sizeof(quitdata), "%c[0m", ESC);
  154. } else if (ast_opt_force_black_background) {
  155. snprintf(prepdata, sizeof(prepdata), "%c[%d;%d;%dm", ESC, ATTR_BRIGHT, COLOR_BROWN, COLOR_BLACK + 10);
  156. snprintf(enddata, sizeof(enddata), "%c[%d;%d;%dm", ESC, ATTR_RESET, COLOR_WHITE, COLOR_BLACK + 10);
  157. snprintf(quitdata, sizeof(quitdata), "%c[0m", ESC);
  158. } else {
  159. snprintf(prepdata, sizeof(prepdata), "%c[%d;%dm", ESC, ATTR_BRIGHT, COLOR_BROWN);
  160. snprintf(enddata, sizeof(enddata), "%c[%d;%dm", ESC, ATTR_RESET, COLOR_WHITE);
  161. snprintf(quitdata, sizeof(quitdata), "%c[0m", ESC);
  162. }
  163. }
  164. return 0;
  165. }
  166. char *term_color(char *outbuf, const char *inbuf, int fgcolor, int bgcolor, int maxout)
  167. {
  168. int attr = 0;
  169. if (!vt100compat) {
  170. ast_copy_string(outbuf, inbuf, maxout);
  171. return outbuf;
  172. }
  173. if (!fgcolor) {
  174. ast_copy_string(outbuf, inbuf, maxout);
  175. return outbuf;
  176. }
  177. if (fgcolor & 128) {
  178. attr = ast_opt_light_background ? 0 : ATTR_BRIGHT;
  179. fgcolor &= ~128;
  180. }
  181. if (bgcolor) {
  182. bgcolor &= ~128;
  183. }
  184. if (ast_opt_light_background) {
  185. fgcolor = opposite(fgcolor);
  186. }
  187. if (ast_opt_force_black_background) {
  188. snprintf(outbuf, maxout, "%c[%d;%d;%dm%s%c[%d;%dm", ESC, attr, fgcolor, bgcolor + 10, inbuf, ESC, COLOR_WHITE, COLOR_BLACK + 10);
  189. } else {
  190. snprintf(outbuf, maxout, "%c[%d;%dm%s%c[0m", ESC, attr, fgcolor, inbuf, ESC);
  191. }
  192. return outbuf;
  193. }
  194. static void check_fgcolor(int *fgcolor, int *attr)
  195. {
  196. *attr = ast_opt_light_background ? 0 : ATTR_BRIGHT;
  197. if (*fgcolor & 128) {
  198. *fgcolor &= ~128;
  199. }
  200. if (ast_opt_light_background) {
  201. *fgcolor = opposite(*fgcolor);
  202. }
  203. }
  204. static void check_bgcolor(int *bgcolor)
  205. {
  206. if (*bgcolor) {
  207. *bgcolor &= ~128;
  208. }
  209. }
  210. static int check_colors_allowed(int fgcolor)
  211. {
  212. return (!vt100compat || !fgcolor) ? 0 : 1;
  213. }
  214. int ast_term_color_code(struct ast_str **str, int fgcolor, int bgcolor)
  215. {
  216. int attr = 0;
  217. if (!check_colors_allowed(fgcolor)) {
  218. return -1;
  219. }
  220. check_fgcolor(&fgcolor, &attr);
  221. check_bgcolor(&bgcolor);
  222. if (ast_opt_force_black_background) {
  223. ast_str_append(str, 0, "%c[%d;%d;%dm", ESC, attr, fgcolor, COLOR_BLACK + 10);
  224. } else if (bgcolor) {
  225. ast_str_append(str, 0, "%c[%d;%d;%dm", ESC, attr, fgcolor, bgcolor + 10);
  226. } else {
  227. ast_str_append(str, 0, "%c[%d;%dm", ESC, attr, fgcolor);
  228. }
  229. return 0;
  230. }
  231. char *term_color_code(char *outbuf, int fgcolor, int bgcolor, int maxout)
  232. {
  233. int attr = 0;
  234. if (!check_colors_allowed(fgcolor)) {
  235. *outbuf = '\0';
  236. return outbuf;
  237. }
  238. check_fgcolor(&fgcolor, &attr);
  239. check_bgcolor(&bgcolor);
  240. if (ast_opt_force_black_background) {
  241. snprintf(outbuf, maxout, "%c[%d;%d;%dm", ESC, attr, fgcolor, COLOR_BLACK + 10);
  242. } else if (bgcolor) {
  243. snprintf(outbuf, maxout, "%c[%d;%d;%dm", ESC, attr, fgcolor, bgcolor + 10);
  244. } else {
  245. snprintf(outbuf, maxout, "%c[%d;%dm", ESC, attr, fgcolor);
  246. }
  247. return outbuf;
  248. }
  249. const char *ast_term_color(int fgcolor, int bgcolor)
  250. {
  251. struct commonbuf *cb = ast_threadstorage_get(&commonbuf, sizeof(*cb));
  252. char *buf;
  253. if (!cb) {
  254. return "";
  255. }
  256. buf = cb->buffer[cb->which++];
  257. if (cb->which == AST_TERM_MAX_ROTATING_BUFFERS) {
  258. cb->which = 0;
  259. }
  260. return term_color_code(buf, fgcolor, bgcolor, AST_TERM_MAX_ESCAPE_CHARS);
  261. }
  262. const char *ast_term_reset(void)
  263. {
  264. if (ast_opt_force_black_background) {
  265. static const char reset[] = { ESC, '[', COLOR_BLACK + 10, 'm', 0 };
  266. return reset;
  267. } else {
  268. return quitdata;
  269. }
  270. }
  271. char *term_strip(char *outbuf, const char *inbuf, int maxout)
  272. {
  273. char *outbuf_ptr = outbuf;
  274. const char *inbuf_ptr = inbuf;
  275. while (outbuf_ptr < outbuf + maxout) {
  276. switch (*inbuf_ptr) {
  277. case ESC:
  278. while (*inbuf_ptr && (*inbuf_ptr != 'm'))
  279. inbuf_ptr++;
  280. break;
  281. default:
  282. *outbuf_ptr = *inbuf_ptr;
  283. outbuf_ptr++;
  284. }
  285. if (! *inbuf_ptr)
  286. break;
  287. inbuf_ptr++;
  288. }
  289. return outbuf;
  290. }
  291. char *term_prompt(char *outbuf, const char *inbuf, int maxout)
  292. {
  293. if (!vt100compat) {
  294. ast_copy_string(outbuf, inbuf, maxout);
  295. return outbuf;
  296. }
  297. if (ast_opt_force_black_background) {
  298. snprintf(outbuf, maxout, "%c[%d;%d;%dm%c%c[%d;%dm%s",
  299. ESC, ATTR_BRIGHT, COLOR_BLUE, COLOR_BLACK + 10,
  300. inbuf[0],
  301. ESC, COLOR_WHITE, COLOR_BLACK + 10,
  302. inbuf + 1);
  303. } else if (ast_opt_light_background) {
  304. snprintf(outbuf, maxout, "%c[%d;0m%c%c[0m%s",
  305. ESC, COLOR_BLUE,
  306. inbuf[0],
  307. ESC,
  308. inbuf + 1);
  309. } else {
  310. snprintf(outbuf, maxout, "%c[%d;%d;0m%c%c[0m%s",
  311. ESC, ATTR_BRIGHT, COLOR_BLUE,
  312. inbuf[0],
  313. ESC,
  314. inbuf + 1);
  315. }
  316. return outbuf;
  317. }
  318. /* filter escape sequences */
  319. void term_filter_escapes(char *line)
  320. {
  321. int i;
  322. int len = strlen(line);
  323. for (i = 0; i < len; i++) {
  324. if (line[i] != ESC)
  325. continue;
  326. if ((i < (len - 2)) &&
  327. (line[i + 1] == 0x5B)) {
  328. switch (line[i + 2]) {
  329. case 0x30:
  330. case 0x31:
  331. case 0x33:
  332. continue;
  333. }
  334. }
  335. /* replace ESC with a space */
  336. line[i] = ' ';
  337. }
  338. }
  339. const char *term_prep(void)
  340. {
  341. return prepdata;
  342. }
  343. const char *term_end(void)
  344. {
  345. return enddata;
  346. }
  347. const char *term_quit(void)
  348. {
  349. return quitdata;
  350. }