term.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Channel Management
  5. *
  6. * Copyright (C) 1999, Mark Spencer
  7. *
  8. * Mark Spencer <markster@linux-support.net>
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU General Public License
  12. */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <sys/time.h>
  17. #include <signal.h>
  18. #include <errno.h>
  19. #include <unistd.h>
  20. #include <asterisk/term.h>
  21. #include <asterisk/options.h>
  22. #include <asterisk/lock.h>
  23. #include "asterisk.h"
  24. static int vt100compat = 0;
  25. static char prepdata[80] = "";
  26. static char enddata[80] = "";
  27. static char quitdata[80] = "";
  28. int term_init(void)
  29. {
  30. char *term = getenv("TERM");
  31. if (!term)
  32. return 0;
  33. if (!option_console || option_nocolor || !option_nofork)
  34. return 0;
  35. if (!strncasecmp(term, "linux", 5))
  36. vt100compat = 1; else
  37. if (!strncasecmp(term, "xterm", 5))
  38. vt100compat = 1; else
  39. if (!strncasecmp(term, "Eterm", 5))
  40. vt100compat = 1; else
  41. if (!strncasecmp(term, "crt", 3))
  42. vt100compat = 1; else
  43. if (!strncasecmp(term, "vt", 2))
  44. vt100compat = 1;
  45. if (vt100compat) {
  46. /* Make commands show up in nice colors */
  47. snprintf(prepdata, sizeof(prepdata), "%c[%d;%d;%dm", ESC, ATTR_BRIGHT, COLOR_BROWN, COLOR_BLACK + 10);
  48. snprintf(enddata, sizeof(enddata), "%c[%d;%d;%dm", ESC, ATTR_RESET, COLOR_WHITE, COLOR_BLACK + 10);
  49. snprintf(quitdata, sizeof(quitdata), "%c[0m", ESC);
  50. }
  51. return 0;
  52. }
  53. char *term_color(char *outbuf, const char *inbuf, int fgcolor, int bgcolor, int maxout)
  54. {
  55. int attr=0;
  56. char tmp[40];
  57. if (!vt100compat) {
  58. strncpy(outbuf, inbuf, maxout -1);
  59. return outbuf;
  60. }
  61. if (!fgcolor && !bgcolor) {
  62. strncpy(outbuf, inbuf, maxout - 1);
  63. return outbuf;
  64. }
  65. if ((fgcolor & 128) && (bgcolor & 128)) {
  66. /* Can't both be highlighted */
  67. strncpy(outbuf, inbuf, maxout - 1);
  68. return outbuf;
  69. }
  70. if (!bgcolor)
  71. bgcolor = COLOR_BLACK;
  72. if (bgcolor) {
  73. bgcolor &= ~128;
  74. bgcolor += 10;
  75. }
  76. if (fgcolor & 128) {
  77. attr = ATTR_BRIGHT;
  78. fgcolor &= ~128;
  79. }
  80. if (fgcolor && bgcolor) {
  81. snprintf(tmp, sizeof(tmp), "%d;%d", fgcolor, bgcolor);
  82. } else if (bgcolor) {
  83. snprintf(tmp, sizeof(tmp), "%d", bgcolor);
  84. } else if (fgcolor) {
  85. snprintf(tmp, sizeof(tmp), "%d", fgcolor);
  86. }
  87. if (attr) {
  88. snprintf(outbuf, maxout, "%c[%d;%sm%s%c[0;%d;%dm", ESC, attr, tmp, inbuf, ESC, COLOR_WHITE, COLOR_BLACK + 10);
  89. } else {
  90. snprintf(outbuf, maxout, "%c[%sm%s%c[0;%d;%dm", ESC, tmp, inbuf, ESC, COLOR_WHITE, COLOR_BLACK + 10);
  91. }
  92. return outbuf;
  93. }
  94. char *term_color_code(char *outbuf, int fgcolor, int bgcolor, int maxout)
  95. {
  96. int attr=0;
  97. char tmp[40];
  98. if ((!vt100compat) || (!fgcolor && !bgcolor)) {
  99. *outbuf = '\0';
  100. return outbuf;
  101. }
  102. if ((fgcolor & 128) && (bgcolor & 128)) {
  103. /* Can't both be highlighted */
  104. *outbuf = '\0';
  105. return outbuf;
  106. }
  107. if (!bgcolor)
  108. bgcolor = COLOR_BLACK;
  109. if (bgcolor) {
  110. bgcolor &= ~128;
  111. bgcolor += 10;
  112. }
  113. if (fgcolor & 128) {
  114. attr = ATTR_BRIGHT;
  115. fgcolor &= ~128;
  116. }
  117. if (fgcolor && bgcolor) {
  118. snprintf(tmp, sizeof(tmp), "%d;%d", fgcolor, bgcolor);
  119. } else if (bgcolor) {
  120. snprintf(tmp, sizeof(tmp), "%d", bgcolor);
  121. } else if (fgcolor) {
  122. snprintf(tmp, sizeof(tmp), "%d", fgcolor);
  123. }
  124. if (attr) {
  125. snprintf(outbuf, maxout, "%c[%d;%sm", ESC, attr, tmp);
  126. } else {
  127. snprintf(outbuf, maxout, "%c[%sm", ESC, tmp);
  128. }
  129. return outbuf;
  130. }
  131. char *term_strip(char *outbuf, char *inbuf, int maxout)
  132. {
  133. char *outbuf_ptr = outbuf, *inbuf_ptr = inbuf;
  134. while (outbuf_ptr < outbuf + maxout) {
  135. switch (*inbuf_ptr) {
  136. case ESC:
  137. while (*inbuf_ptr && (*inbuf_ptr != 'm'))
  138. inbuf_ptr++;
  139. break;
  140. default:
  141. *outbuf_ptr = *inbuf_ptr;
  142. outbuf_ptr++;
  143. }
  144. if (! *inbuf_ptr)
  145. break;
  146. inbuf_ptr++;
  147. }
  148. return outbuf;
  149. }
  150. char *term_prompt(char *outbuf, const char *inbuf, int maxout)
  151. {
  152. if (!vt100compat) {
  153. strncpy(outbuf, inbuf, maxout -1);
  154. return outbuf;
  155. }
  156. snprintf(outbuf, maxout, "%c[%d;%d;%dm%c%c[%d;%d;%dm%s",
  157. ESC, ATTR_BRIGHT, COLOR_BLUE, COLOR_BLACK + 10,
  158. inbuf[0],
  159. ESC, 0, COLOR_WHITE, COLOR_BLACK + 10,
  160. inbuf + 1);
  161. return outbuf;
  162. }
  163. char *term_prep(void)
  164. {
  165. return prepdata;
  166. }
  167. char *term_end(void)
  168. {
  169. return enddata;
  170. }
  171. char *term_quit(void)
  172. {
  173. return quitdata;
  174. }