Wed Jan 8 2020 09:50:21

Asterisk developer's documentation


term.c File Reference

Terminal Routines. More...

#include "asterisk.h"
#include "asterisk/_private.h"
#include <sys/time.h>
#include <signal.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "asterisk/term.h"
#include "asterisk/lock.h"
#include "asterisk/utils.h"

Go to the source code of this file.

Functions

int ast_term_color_code (struct ast_str **str, int fgcolor, int bgcolor)
 Append a color sequence to an ast_str. More...
 
int ast_term_init (void)
 
static void check_bgcolor (int *bgcolor)
 
static int check_colors_allowed (int fgcolor)
 
static void check_fgcolor (int *fgcolor, int *attr)
 
static short convshort (char *s)
 
static int opposite (int color)
 
char * term_color (char *outbuf, const char *inbuf, int fgcolor, int bgcolor, int maxout)
 
char * term_color_code (char *outbuf, int fgcolor, int bgcolor, int maxout)
 Write a color sequence to a string. More...
 
char * term_end (void)
 
void term_filter_escapes (char *line)
 
char * term_prep (void)
 
char * term_prompt (char *outbuf, const char *inbuf, int maxout)
 
char * term_quit (void)
 
char * term_strip (char *outbuf, const char *inbuf, int maxout)
 

Variables

static char enddata [80] = ""
 
static char prepdata [80] = ""
 
static char quitdata [80] = ""
 
static const char *const termpath []
 
static int vt100compat
 

Detailed Description

Terminal Routines.

Author
Mark Spencer marks.nosp@m.ter@.nosp@m.digiu.nosp@m.m.co.nosp@m.m

Definition in file term.c.

Function Documentation

int ast_term_color_code ( struct ast_str **  str,
int  fgcolor,
int  bgcolor 
)

Append a color sequence to an ast_str.

Parameters
strThe string to append to
fgcolorforeground color
bgcolorbackground color
Return values
0success
-1failure

Definition at line 242 of file term.c.

References ast_opt_force_black_background, ast_str_append(), check_bgcolor(), check_colors_allowed(), check_fgcolor(), COLOR_BLACK, and ESC.

Referenced by ast_xmldoc_printable(), data_result_print_cli(), and data_result_print_cli_node().

243 {
244  int attr = 0;
245 
246  if (!check_colors_allowed(fgcolor)) {
247  return -1;
248  }
249 
250  check_fgcolor(&fgcolor, &attr);
251  check_bgcolor(&bgcolor);
252 
254  ast_str_append(str, 0, "%c[%d;%d;%dm", ESC, attr, fgcolor, COLOR_BLACK + 10);
255  } else if (bgcolor) {
256  ast_str_append(str, 0, "%c[%d;%d;%dm", ESC, attr, fgcolor, bgcolor + 10);
257  } else {
258  ast_str_append(str, 0, "%c[%d;%dm", ESC, attr, fgcolor);
259  }
260 
261  return 0;
262 }
static void check_fgcolor(int *fgcolor, int *attr)
Definition: term.c:218
#define ast_opt_force_black_background
Definition: options.h:129
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definition: strings.h:900
#define COLOR_BLACK
Definition: term.h:47
static void check_bgcolor(int *bgcolor)
Definition: term.c:230
#define ESC
Definition: term.h:30
static int check_colors_allowed(int fgcolor)
Definition: term.c:237
int ast_term_init ( void  )

Provided by term.c

Definition at line 87 of file term.c.

References ast_opt_console, ast_opt_force_black_background, ast_opt_light_background, ast_opt_no_color, ATTR_BRIGHT, ATTR_RESET, COLOR_BLACK, COLOR_BROWN, COLOR_WHITE, convshort(), and ESC.

Referenced by main().

88 {
89  char *term = getenv("TERM");
90  char termfile[256] = "";
91  char buffer[512] = "";
92  int termfd = -1, parseokay = 0, i;
93 
94  if (ast_opt_no_color) {
95  return 0;
96  }
97 
98  if (!ast_opt_console) {
99  /* If any remote console is not compatible, we'll strip the color codes at that point */
100  vt100compat = 1;
101  goto end;
102  }
103 
104  if (!term) {
105  return 0;
106  }
107 
108  for (i = 0;; i++) {
109  if (termpath[i] == NULL) {
110  break;
111  }
112  snprintf(termfile, sizeof(termfile), "%s/%c/%s", termpath[i], *term, term);
113  termfd = open(termfile, O_RDONLY);
114  if (termfd > -1) {
115  break;
116  }
117  }
118  if (termfd > -1) {
119  int actsize = read(termfd, buffer, sizeof(buffer) - 1);
120  short sz_names = convshort(buffer + 2);
121  short sz_bools = convshort(buffer + 4);
122  short n_nums = convshort(buffer + 6);
123 
124  /* if ((sz_names + sz_bools) & 1)
125  sz_bools++; */
126 
127  if (sz_names + sz_bools + n_nums < actsize) {
128  /* Offset 13 is defined in /usr/include/term.h, though we do not
129  * include it here, as it conflicts with include/asterisk/term.h */
130  short max_colors = convshort(buffer + 12 + sz_names + sz_bools + 13 * 2);
131  if (max_colors > 0) {
132  vt100compat = 1;
133  }
134  parseokay = 1;
135  }
136  close(termfd);
137  }
138 
139  if (!parseokay) {
140  /* These comparisons should not be substrings nor case-insensitive, as
141  * terminal types are very particular about how they treat suffixes and
142  * capitalization. For example, terminal type 'linux-m' does NOT
143  * support color, while 'linux' does. Not even all vt100* terminals
144  * support color, either (e.g. 'vt100+fnkeys'). */
145  if (!strcmp(term, "linux")) {
146  vt100compat = 1;
147  } else if (!strcmp(term, "xterm")) {
148  vt100compat = 1;
149  } else if (!strcmp(term, "xterm-color")) {
150  vt100compat = 1;
151  } else if (!strcmp(term, "xterm-256color")) {
152  vt100compat = 1;
153  } else if (!strncmp(term, "Eterm", 5)) {
154  /* Both entries which start with Eterm support color */
155  vt100compat = 1;
156  } else if (!strcmp(term, "vt100")) {
157  vt100compat = 1;
158  } else if (!strncmp(term, "crt", 3)) {
159  /* Both crt terminals support color */
160  vt100compat = 1;
161  }
162  }
163 
164 end:
165  if (vt100compat) {
166  /* Make commands show up in nice colors */
168  snprintf(prepdata, sizeof(prepdata), "%c[%dm", ESC, COLOR_BROWN);
169  snprintf(enddata, sizeof(enddata), "%c[%dm", ESC, COLOR_BLACK);
170  snprintf(quitdata, sizeof(quitdata), "%c[0m", ESC);
171  } else if (ast_opt_force_black_background) {
172  snprintf(prepdata, sizeof(prepdata), "%c[%d;%d;%dm", ESC, ATTR_BRIGHT, COLOR_BROWN, COLOR_BLACK + 10);
173  snprintf(enddata, sizeof(enddata), "%c[%d;%d;%dm", ESC, ATTR_RESET, COLOR_WHITE, COLOR_BLACK + 10);
174  snprintf(quitdata, sizeof(quitdata), "%c[0m", ESC);
175  } else {
176  snprintf(prepdata, sizeof(prepdata), "%c[%d;%dm", ESC, ATTR_BRIGHT, COLOR_BROWN);
177  snprintf(enddata, sizeof(enddata), "%c[%d;%dm", ESC, ATTR_RESET, COLOR_WHITE);
178  snprintf(quitdata, sizeof(quitdata), "%c[0m", ESC);
179  }
180  }
181  return 0;
182 }
#define ast_opt_force_black_background
Definition: options.h:129
static int vt100compat
Definition: term.c:44
static short convshort(char *s)
Definition: term.c:72
#define COLOR_WHITE
Definition: term.h:61
static char enddata[80]
Definition: term.c:47
#define ast_opt_console
Definition: options.h:107
static char prepdata[80]
Definition: term.c:46
#define ATTR_BRIGHT
Definition: term.h:36
#define ast_opt_no_color
Definition: options.h:112
static char quitdata[80]
Definition: term.c:48
#define COLOR_BLACK
Definition: term.h:47
#define ATTR_RESET
Definition: term.h:35
#define ast_opt_light_background
Definition: options.h:128
static const char *const termpath[]
Definition: term.c:50
#define ESC
Definition: term.h:30
#define COLOR_BROWN
Definition: term.h:53
static void check_bgcolor ( int *  bgcolor)
static

Definition at line 230 of file term.c.

Referenced by ast_term_color_code(), and term_color_code().

231 {
232  if (*bgcolor) {
233  *bgcolor &= ~128;
234  }
235 }
static int check_colors_allowed ( int  fgcolor)
static

Definition at line 237 of file term.c.

Referenced by ast_term_color_code(), and term_color_code().

238 {
239  return (!vt100compat || !fgcolor) ? 0 : 1;
240 }
static int vt100compat
Definition: term.c:44
static void check_fgcolor ( int *  fgcolor,
int *  attr 
)
static

Definition at line 218 of file term.c.

References ast_opt_light_background, ATTR_BRIGHT, and opposite().

Referenced by ast_term_color_code(), and term_color_code().

219 {
220  if (*fgcolor & 128) {
222  *fgcolor &= ~128;
223  }
224 
226  *fgcolor = opposite(*fgcolor);
227  }
228 }
#define ATTR_BRIGHT
Definition: term.h:36
#define ast_opt_light_background
Definition: options.h:128
static int opposite(int color)
Definition: term.c:57
static short convshort ( char *  s)
static

Definition at line 72 of file term.c.

Referenced by ast_term_init().

73 {
74  register int a, b;
75 
76  a = (int) s[0] & 0377;
77  b = (int) s[1] & 0377;
78 
79  if (a == 0377 && b == 0377)
80  return -1;
81  if (a == 0376 && b == 0377)
82  return -2;
83 
84  return a + b * 256;
85 }
static int opposite ( int  color)
static

Definition at line 57 of file term.c.

References COLOR_BLACK, COLOR_BLUE, COLOR_BROWN, COLOR_CYAN, COLOR_GREEN, COLOR_MAGENTA, and COLOR_RED.

Referenced by check_fgcolor(), and term_color().

58 {
59  int lookup[] = {
60  /* BLACK */ COLOR_BLACK,
61  /* RED */ COLOR_MAGENTA,
62  /* GREEN */ COLOR_GREEN,
63  /* BROWN */ COLOR_BROWN,
64  /* BLUE */ COLOR_CYAN,
65  /* MAGENTA */ COLOR_RED,
66  /* CYAN */ COLOR_BLUE,
67  /* WHITE */ COLOR_BLACK };
68  return color ? lookup[color - 30] : 0;
69 }
#define COLOR_CYAN
Definition: term.h:59
#define COLOR_GREEN
Definition: term.h:51
#define COLOR_RED
Definition: term.h:49
#define COLOR_BLACK
Definition: term.h:47
int color
Definition: data.c:2664
#define COLOR_BLUE
Definition: term.h:55
#define COLOR_BROWN
Definition: term.h:53
#define COLOR_MAGENTA
Definition: term.h:57
char* term_color ( char *  outbuf,
const char *  inbuf,
int  fgcolor,
int  bgcolor,
int  maxout 
)

Definition at line 184 of file term.c.

References ast_copy_string(), ast_opt_force_black_background, ast_opt_light_background, ATTR_BRIGHT, COLOR_BLACK, COLOR_WHITE, ESC, and opposite().

Referenced by __ast_custom_function_register(), __ast_register_translator(), ast_frame_dump(), ast_register_application2(), ast_unregister_translator(), fix_header(), handle_cli_agi_show(), handle_dahdi_show_cadences(), handle_show_function(), handle_showmancmd(), logger_print_normal(), lua_pbx_exec(), main(), pbx_extension_helper(), print_app_docs(), realtime_exec(), show_config_description(), and start_resource().

185 {
186  int attr = 0;
187 
188  if (!vt100compat) {
189  ast_copy_string(outbuf, inbuf, maxout);
190  return outbuf;
191  }
192  if (!fgcolor) {
193  ast_copy_string(outbuf, inbuf, maxout);
194  return outbuf;
195  }
196 
197  if (fgcolor & 128) {
199  fgcolor &= ~128;
200  }
201 
202  if (bgcolor) {
203  bgcolor &= ~128;
204  }
205 
207  fgcolor = opposite(fgcolor);
208  }
209 
211  snprintf(outbuf, maxout, "%c[%d;%d;%dm%s%c[%d;%dm", ESC, attr, fgcolor, bgcolor + 10, inbuf, ESC, COLOR_WHITE, COLOR_BLACK + 10);
212  } else {
213  snprintf(outbuf, maxout, "%c[%d;%dm%s%c[0m", ESC, attr, fgcolor, inbuf, ESC);
214  }
215  return outbuf;
216 }
#define ast_opt_force_black_background
Definition: options.h:129
static int vt100compat
Definition: term.c:44
#define COLOR_WHITE
Definition: term.h:61
static int inbuf(struct baseio *bio, FILE *fi)
utility used by inchar(), for base_encode()
#define ATTR_BRIGHT
Definition: term.h:36
#define COLOR_BLACK
Definition: term.h:47
#define ast_opt_light_background
Definition: options.h:128
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:223
#define ESC
Definition: term.h:30
static int opposite(int color)
Definition: term.c:57
char* term_color_code ( char *  outbuf,
int  fgcolor,
int  bgcolor,
int  maxout 
)

Write a color sequence to a string.

Parameters
outbufthe location to write to
fgcolorforeground color
bgcolorbackground color
maxoutmaximum number of characters to write
Returns
outbuf

Definition at line 264 of file term.c.

References ast_opt_force_black_background, check_bgcolor(), check_colors_allowed(), check_fgcolor(), COLOR_BLACK, and ESC.

Referenced by cli_prompt().

265 {
266  int attr = 0;
267 
268  if (!check_colors_allowed(fgcolor)) {
269  *outbuf = '\0';
270  return outbuf;
271  }
272 
273  check_fgcolor(&fgcolor, &attr);
274  check_bgcolor(&bgcolor);
275 
277  snprintf(outbuf, maxout, "%c[%d;%d;%dm", ESC, attr, fgcolor, COLOR_BLACK + 10);
278  } else if (bgcolor) {
279  snprintf(outbuf, maxout, "%c[%d;%d;%dm", ESC, attr, fgcolor, bgcolor + 10);
280  } else {
281  snprintf(outbuf, maxout, "%c[%d;%dm", ESC, attr, fgcolor);
282  }
283 
284  return outbuf;
285 }
static void check_fgcolor(int *fgcolor, int *attr)
Definition: term.c:218
#define ast_opt_force_black_background
Definition: options.h:129
#define COLOR_BLACK
Definition: term.h:47
static void check_bgcolor(int *bgcolor)
Definition: term.c:230
#define ESC
Definition: term.h:30
static int check_colors_allowed(int fgcolor)
Definition: term.c:237
char* term_end ( void  )

Definition at line 365 of file term.c.

References enddata.

Referenced by ast_xmldoc_printable(), consolehandler(), and main().

366 {
367  return enddata;
368 }
static char enddata[80]
Definition: term.c:47
void term_filter_escapes ( char *  line)

Definition at line 338 of file term.c.

References ESC, and len().

Referenced by ast_log().

339 {
340  int i;
341  int len = strlen(line);
342 
343  for (i = 0; i < len; i++) {
344  if (line[i] != ESC)
345  continue;
346  if ((i < (len - 2)) &&
347  (line[i + 1] == 0x5B)) {
348  switch (line[i + 2]) {
349  case 0x30:
350  case 0x31:
351  case 0x33:
352  continue;
353  }
354  }
355  /* replace ESC with a space */
356  line[i] = ' ';
357  }
358 }
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
#define ESC
Definition: term.h:30
char* term_prep ( void  )

Definition at line 360 of file term.c.

References prepdata.

361 {
362  return prepdata;
363 }
static char prepdata[80]
Definition: term.c:46
char* term_prompt ( char *  outbuf,
const char *  inbuf,
int  maxout 
)

Definition at line 309 of file term.c.

References ast_copy_string(), ast_opt_force_black_background, ast_opt_light_background, ATTR_BRIGHT, COLOR_BLACK, COLOR_BLUE, COLOR_WHITE, and ESC.

310 {
311  if (!vt100compat) {
312  ast_copy_string(outbuf, inbuf, maxout);
313  return outbuf;
314  }
316  snprintf(outbuf, maxout, "%c[%d;%dm%c%c[%d;%dm%s",
317  ESC, COLOR_BLUE, COLOR_BLACK + 10,
318  inbuf[0],
319  ESC, COLOR_WHITE, COLOR_BLACK + 10,
320  inbuf + 1);
321  } else if (ast_opt_light_background) {
322  snprintf(outbuf, maxout, "%c[%d;0m%c%c[%d;0m%s",
323  ESC, COLOR_BLUE,
324  inbuf[0],
325  ESC, COLOR_BLACK,
326  inbuf + 1);
327  } else {
328  snprintf(outbuf, maxout, "%c[%d;%d;0m%c%c[%d;%d;0m%s",
330  inbuf[0],
331  ESC, 0, COLOR_WHITE,
332  inbuf + 1);
333  }
334  return outbuf;
335 }
#define ast_opt_force_black_background
Definition: options.h:129
static int vt100compat
Definition: term.c:44
#define COLOR_WHITE
Definition: term.h:61
static int inbuf(struct baseio *bio, FILE *fi)
utility used by inchar(), for base_encode()
#define ATTR_BRIGHT
Definition: term.h:36
#define COLOR_BLACK
Definition: term.h:47
#define ast_opt_light_background
Definition: options.h:128
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:223
#define COLOR_BLUE
Definition: term.h:55
#define ESC
Definition: term.h:30
char* term_quit ( void  )

Definition at line 370 of file term.c.

References quitdata.

Referenced by ast_el_read_char(), main(), and really_quit().

371 {
372  return quitdata;
373 }
static char quitdata[80]
Definition: term.c:48
char* term_strip ( char *  outbuf,
const char *  inbuf,
int  maxout 
)

Definition at line 287 of file term.c.

References ESC, and inbuf().

Referenced by action_command(), ast_log_vsyslog(), and logger_print_normal().

288 {
289  char *outbuf_ptr = outbuf;
290  const char *inbuf_ptr = inbuf;
291 
292  while (outbuf_ptr < outbuf + maxout) {
293  switch (*inbuf_ptr) {
294  case ESC:
295  while (*inbuf_ptr && (*inbuf_ptr != 'm'))
296  inbuf_ptr++;
297  break;
298  default:
299  *outbuf_ptr = *inbuf_ptr;
300  outbuf_ptr++;
301  }
302  if (! *inbuf_ptr)
303  break;
304  inbuf_ptr++;
305  }
306  return outbuf;
307 }
static int inbuf(struct baseio *bio, FILE *fi)
utility used by inchar(), for base_encode()
#define ESC
Definition: term.h:30

Variable Documentation

char enddata[80] = ""
static

Definition at line 47 of file term.c.

Referenced by term_end().

char prepdata[80] = ""
static

Definition at line 46 of file term.c.

Referenced by term_prep().

char quitdata[80] = ""
static

Definition at line 48 of file term.c.

Referenced by term_quit().

const char* const termpath[]
static

Definition at line 50 of file term.c.

int vt100compat
static

Definition at line 44 of file term.c.