term.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 <pthread.h>
  16. #include <string.h>
  17. #include <sys/time.h>
  18. #include <signal.h>
  19. #include <errno.h>
  20. #include <unistd.h>
  21. #include <asterisk/term.h>
  22. #include <asterisk/options.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, "crt", 3))
  40. vt100compat = 1; else
  41. if (!strncasecmp(term, "vt", 2))
  42. vt100compat = 1;
  43. if (vt100compat) {
  44. /* Make commands show up in nice colors */
  45. snprintf(prepdata, sizeof(prepdata), "%c[%d;%d;%dm", ESC, ATTR_BRIGHT, COLOR_BROWN, COLOR_BLACK + 10);
  46. snprintf(enddata, sizeof(enddata), "%c[%d;%d;%dm", ESC, ATTR_RESET, COLOR_WHITE, COLOR_BLACK + 10);
  47. snprintf(quitdata, sizeof(quitdata), "%c[0m", ESC);
  48. }
  49. return 0;
  50. }
  51. char *term_color(char *outbuf, const char *inbuf, int fgcolor, int bgcolor, int maxout)
  52. {
  53. int attr=0;
  54. char tmp[40];
  55. if (!vt100compat) {
  56. strncpy(outbuf, inbuf, maxout -1);
  57. return outbuf;
  58. }
  59. if (!fgcolor && !bgcolor) {
  60. strncpy(outbuf, inbuf, maxout - 1);
  61. return outbuf;
  62. }
  63. if ((fgcolor & 128) && (bgcolor & 128)) {
  64. /* Can't both be highlighted */
  65. strncpy(outbuf, inbuf, maxout - 1);
  66. return outbuf;
  67. }
  68. if (!bgcolor)
  69. bgcolor = COLOR_BLACK;
  70. if (bgcolor) {
  71. bgcolor &= ~128;
  72. bgcolor += 10;
  73. }
  74. if (fgcolor & 128) {
  75. attr = ATTR_BRIGHT;
  76. fgcolor &= ~128;
  77. }
  78. if (fgcolor && bgcolor) {
  79. snprintf(tmp, sizeof(tmp), "%d;%d", fgcolor, bgcolor);
  80. } else if (bgcolor) {
  81. snprintf(tmp, sizeof(tmp), "%d", bgcolor);
  82. } else if (fgcolor) {
  83. snprintf(tmp, sizeof(tmp), "%d", fgcolor);
  84. }
  85. if (attr) {
  86. snprintf(outbuf, maxout, "%c[%d;%sm%s%c[0;%d;%dm", ESC, attr, tmp, inbuf, ESC, COLOR_WHITE, COLOR_BLACK + 10);
  87. } else {
  88. snprintf(outbuf, maxout, "%c[%sm%s%c[0;%d;%dm", ESC, tmp, inbuf, ESC, COLOR_WHITE, COLOR_BLACK + 10);
  89. }
  90. return outbuf;
  91. }
  92. char *term_prompt(char *outbuf, const char *inbuf, int maxout)
  93. {
  94. if (!vt100compat) {
  95. strncpy(outbuf, inbuf, maxout -1);
  96. return outbuf;
  97. }
  98. snprintf(outbuf, maxout, "%c[%d;%d;%dm%c%c[%d;%d;%dm%s",
  99. ESC, ATTR_BRIGHT, COLOR_BLUE, COLOR_BLACK + 10,
  100. inbuf[0],
  101. ESC, 0, COLOR_WHITE, COLOR_BLACK + 10,
  102. inbuf + 1);
  103. return outbuf;
  104. }
  105. char *term_prep(void)
  106. {
  107. return prepdata;
  108. }
  109. char *term_end(void)
  110. {
  111. return enddata;
  112. }
  113. char *term_quit(void)
  114. {
  115. return quitdata;
  116. }