Asterisk - The Open Source Telephony Project  18.5.0
Macros | Functions
term.h File Reference

Handy terminal functions for vt* terms. More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define AST_TERM_MAX_ESCAPE_CHARS   23
 Maximum number of characters needed for a color escape sequence, and another one for a trailing reset, plus a null char. More...
 
#define AST_TERM_MAX_ROTATING_BUFFERS   15
 
#define COLORIZE(fg, bg, str)   ast_term_color(fg,bg),str,ast_term_reset()
 
#define COLORIZE_FMT   "%s%s%s"
 Shortcut macros for coloring a set of text. More...
 
#define ESC   0x1b
 
Terminal Attributes
#define ATTR_RESET   0
 
#define ATTR_BRIGHT   1
 
#define ATTR_DIM   2
 
#define ATTR_UNDER   4
 
#define ATTR_BLINK   5
 
#define ATTR_REVER   7
 
#define ATTR_HIDDEN   8
 
Terminal Colors
#define COLOR_BLACK   30
 
#define COLOR_GRAY   (30 | 128)
 
#define COLOR_RED   31
 
#define COLOR_BRRED   (31 | 128)
 
#define COLOR_GREEN   32
 
#define COLOR_BRGREEN   (32 | 128)
 
#define COLOR_BROWN   33
 
#define COLOR_YELLOW   (33 | 128)
 
#define COLOR_BLUE   34
 
#define COLOR_BRBLUE   (34 | 128)
 
#define COLOR_MAGENTA   35
 
#define COLOR_BRMAGENTA   (35 | 128)
 
#define COLOR_CYAN   36
 
#define COLOR_BRCYAN   (36 | 128)
 
#define COLOR_WHITE   37
 
#define COLOR_BRWHITE   (37 | 128)
 

Functions

const char * ast_term_color (int fgcolor, int bgcolor)
 Return a color sequence string. More...
 
int ast_term_color_code (struct ast_str **str, int fgcolor, int bgcolor)
 Append a color sequence to an ast_str. More...
 
const char * ast_term_reset (void)
 Returns the terminal reset code. More...
 
char * term_color (char *outbuf, const char *inbuf, int fgcolor, int bgcolor, int maxout)
 Colorize a specified string by adding terminal color codes. More...
 
char * term_color_code (char *outbuf, int fgcolor, int bgcolor, int maxout)
 Write a color sequence to a string. More...
 
const char * term_end (void)
 
void term_filter_escapes (char *line)
 
const char * term_quit (void)
 
char * term_strip (char *outbuf, const char *inbuf, int maxout)
 Remove colorings from a specified string. More...
 

Detailed Description

Handy terminal functions for vt* terms.

Definition in file term.h.

Macro Definition Documentation

◆ AST_TERM_MAX_ESCAPE_CHARS

#define AST_TERM_MAX_ESCAPE_CHARS   23

Maximum number of characters needed for a color escape sequence, and another one for a trailing reset, plus a null char.

Definition at line 71 of file term.h.

Referenced by ast_term_color(), handle_cli_agi_show(), and handle_show_function().

◆ AST_TERM_MAX_ROTATING_BUFFERS

#define AST_TERM_MAX_ROTATING_BUFFERS   15

Definition at line 72 of file term.h.

Referenced by ast_term_color().

◆ ATTR_BLINK

#define ATTR_BLINK   5

Definition at line 39 of file term.h.

◆ ATTR_BRIGHT

#define ATTR_BRIGHT   1

Definition at line 36 of file term.h.

Referenced by check_fgcolor(), and term_color().

◆ ATTR_DIM

#define ATTR_DIM   2

Definition at line 37 of file term.h.

◆ ATTR_HIDDEN

#define ATTR_HIDDEN   8

Definition at line 41 of file term.h.

◆ ATTR_RESET

#define ATTR_RESET   0

Definition at line 35 of file term.h.

Referenced by ast_term_init().

◆ ATTR_REVER

#define ATTR_REVER   7

Definition at line 40 of file term.h.

◆ ATTR_UNDER

#define ATTR_UNDER   4

Definition at line 38 of file term.h.

◆ COLOR_BLACK

#define COLOR_BLACK   30

◆ COLOR_BLUE

#define COLOR_BLUE   34

Definition at line 55 of file term.h.

Referenced by opposite().

◆ COLOR_BRBLUE

#define COLOR_BRBLUE   (34 | 128)

Definition at line 56 of file term.h.

◆ COLOR_BRCYAN

#define COLOR_BRCYAN   (36 | 128)

◆ COLOR_BRGREEN

#define COLOR_BRGREEN   (32 | 128)

Definition at line 52 of file term.h.

Referenced by ast_frame_dump(), and asterisk_daemon().

◆ COLOR_BRMAGENTA

#define COLOR_BRMAGENTA   (35 | 128)

◆ COLOR_BROWN

#define COLOR_BROWN   33

Definition at line 53 of file term.h.

Referenced by opposite(), print_queue(), and start_resource().

◆ COLOR_BRRED

#define COLOR_BRRED   (31 | 128)

Definition at line 50 of file term.h.

Referenced by ast_frame_dump().

◆ COLOR_BRWHITE

#define COLOR_BRWHITE   (37 | 128)

Definition at line 62 of file term.h.

Referenced by format_log_default(), and show_config_description().

◆ COLOR_CYAN

#define COLOR_CYAN   36

◆ COLOR_GRAY

#define COLOR_GRAY   (30 | 128)

Definition at line 48 of file term.h.

Referenced by set_header().

◆ COLOR_GREEN

#define COLOR_GREEN   32

◆ COLOR_MAGENTA

#define COLOR_MAGENTA   35

◆ COLOR_RED

#define COLOR_RED   31

Definition at line 49 of file term.h.

Referenced by opposite(), print_queue(), test_cli_show_results(), and test_execute_multiple().

◆ COLOR_WHITE

#define COLOR_WHITE   37

Definition at line 61 of file term.h.

Referenced by ast_term_init(), and cli_prompt().

◆ COLOR_YELLOW

#define COLOR_YELLOW   (33 | 128)

Definition at line 54 of file term.h.

Referenced by ast_frame_dump(), and show_config_description().

◆ COLORIZE

#define COLORIZE (   fg,
  bg,
  str 
)    ast_term_color(fg,bg),str,ast_term_reset()

◆ COLORIZE_FMT

#define COLORIZE_FMT   "%s%s%s"

◆ ESC

#define ESC   0x1b

Function Documentation

◆ ast_term_color()

const char* ast_term_color ( int  fgcolor,
int  bgcolor 
)

Return a color sequence string.

Parameters
fgcolorforeground color
bgcolorbackground color
Note
This function may be called up to 15 times within the arguments to a single function without the danger of overwriting a common buffer.
Returns
A color sequence string, or the empty string, on error

Definition at line 290 of file term.c.

References AST_TERM_MAX_ESCAPE_CHARS, AST_TERM_MAX_ROTATING_BUFFERS, ast_threadstorage_get(), buf, commonbuf::buffer, term_color_code(), and commonbuf::which.

Referenced by print_app_docs(), print_queue(), and set_header().

291 {
292  struct commonbuf *cb = ast_threadstorage_get(&commonbuf, sizeof(*cb));
293  char *buf;
294 
295  if (!cb) {
296  return "";
297  }
298  buf = cb->buffer[cb->which++];
300  cb->which = 0;
301  }
302 
303  return term_color_code(buf, fgcolor, bgcolor, AST_TERM_MAX_ESCAPE_CHARS);
304 }
#define AST_TERM_MAX_ROTATING_BUFFERS
Definition: term.h:72
void * ast_threadstorage_get(struct ast_threadstorage *ts, size_t init_size)
Retrieve thread storage.
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define AST_TERM_MAX_ESCAPE_CHARS
Maximum number of characters needed for a color escape sequence, and another one for a trailing reset...
Definition: term.h:71
Definition: term.c:57
char * term_color_code(char *outbuf, int fgcolor, int bgcolor, int maxout)
Write a color sequence to a string.
Definition: term.c:267
char buffer[AST_TERM_MAX_ROTATING_BUFFERS][AST_TERM_MAX_ESCAPE_CHARS]
Definition: term.c:59
short which
Definition: term.c:58

◆ ast_term_color_code()

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 245 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(), and cli_prompt().

246 {
247  int attr = 0;
248 
249  if (!check_colors_allowed()) {
250  return -1;
251  }
252 
253  check_fgcolor(&fgcolor, &attr);
254  check_bgcolor(&bgcolor);
255 
257  ast_str_append(str, 0, "%c[%d;%d;%dm", ESC, attr, fgcolor, COLOR_BLACK + 10);
258  } else if (bgcolor) {
259  ast_str_append(str, 0, "%c[%d;%d;%dm", ESC, attr, fgcolor, bgcolor + 10);
260  } else {
261  ast_str_append(str, 0, "%c[%d;%dm", ESC, attr, fgcolor);
262  }
263 
264  return 0;
265 }
static void check_fgcolor(int *fgcolor, int *attr)
Definition: term.c:221
#define ast_opt_force_black_background
Definition: options.h:131
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:1091
static int check_colors_allowed(void)
Definition: term.c:240
#define COLOR_BLACK
Definition: term.h:47
static void check_bgcolor(int *bgcolor)
Definition: term.c:233
#define ESC
Definition: term.h:30

◆ ast_term_reset()

const char* ast_term_reset ( void  )

Returns the terminal reset code.

Returns
String which, when sent to the screen, resets the terminal colors

Definition at line 306 of file term.c.

References term_end().

Referenced by ast_xmldoc_printable(), cli_show_module_options(), print_app_docs(), print_queue(), and set_header().

307 {
308  return term_end();
309 }
const char * term_end(void)
Definition: term.c:356

◆ term_color()

char* term_color ( char *  outbuf,
const char *  inbuf,
int  fgcolor,
int  bgcolor,
int  maxout 
)

Colorize a specified string by adding terminal color codes.

Parameters
outbufResult buffer
inbufStarting string
fgcolorForeground color, specified as one of the constants in include/asterisk/term.h. Use '0' if the want the normal terminal foreground color.
bgcolorBackground color, specified as one of the constants in include/asterisk/term.h. Use '0' if you want the normal terminal background color.
maxoutMaximum size of outbuf
Returns
outbuf
Deprecated:
Due to the necessity of pre-sizing a result buffer, new code should avoid using this function in preference to ast_term_color_code() or ast_term_color().

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, ESC, opposite(), term_end(), and vt100compat.

Referenced by __ast_register_translator(), ast_frame_dump(), ast_unregister_translator(), cli_show_module_options(), cli_show_module_type(), handle_cli_agi_show(), handle_dahdi_show_cadences(), handle_show_function(), handle_showmancmd(), lua_pbx_exec(), print_event_instance(), realtime_exec(), show_config_description(), start_resource(), test_cli_show_results(), and test_execute_multiple().

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  if (!bgcolor) {
212  bgcolor = COLOR_BLACK;
213  }
214  snprintf(outbuf, maxout, "%c[%d;%d;%dm%s%s", ESC, attr, fgcolor, bgcolor + 10, inbuf, term_end());
215  } else {
216  snprintf(outbuf, maxout, "%c[%d;%dm%s%s", ESC, attr, fgcolor, inbuf, term_end());
217  }
218  return outbuf;
219 }
#define ast_opt_force_black_background
Definition: options.h:131
static int vt100compat
Definition: term.c:43
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
const char * term_end(void)
Definition: term.c:356
#define ast_opt_light_background
Definition: options.h:130
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:401
#define ESC
Definition: term.h:30
static int opposite(int color)
Definition: term.c:62

◆ term_color_code()

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
Deprecated:
You should use ast_term_color_code or ast_term_color, instead.
Returns
outbuf

Definition at line 267 of file term.c.

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

Referenced by ast_term_color().

268 {
269  int attr = 0;
270 
271  if (!check_colors_allowed()) {
272  *outbuf = '\0';
273  return outbuf;
274  }
275 
276  check_fgcolor(&fgcolor, &attr);
277  check_bgcolor(&bgcolor);
278 
280  snprintf(outbuf, maxout, "%c[%d;%d;%dm", ESC, attr, fgcolor, COLOR_BLACK + 10);
281  } else if (bgcolor) {
282  snprintf(outbuf, maxout, "%c[%d;%d;%dm", ESC, attr, fgcolor, bgcolor + 10);
283  } else {
284  snprintf(outbuf, maxout, "%c[%d;%dm", ESC, attr, fgcolor);
285  }
286 
287  return outbuf;
288 }
static void check_fgcolor(int *fgcolor, int *attr)
Definition: term.c:221
#define ast_opt_force_black_background
Definition: options.h:131
static int check_colors_allowed(void)
Definition: term.c:240
#define COLOR_BLACK
Definition: term.h:47
static void check_bgcolor(int *bgcolor)
Definition: term.c:233
#define ESC
Definition: term.h:30

◆ term_end()

const char* term_end ( void  )

Definition at line 356 of file term.c.

References enddata.

Referenced by ast_remotecontrol(), ast_term_reset(), asterisk_daemon(), consolehandler(), main(), and term_color().

357 {
358  return enddata;
359 }
static char enddata[80]
Definition: term.c:45

◆ term_filter_escapes()

void term_filter_escapes ( char *  line)

Definition at line 334 of file term.c.

References ESC, and len().

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

◆ term_quit()

const char* term_quit ( void  )

Definition at line 361 of file term.c.

References quitdata.

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

362 {
363  return quitdata;
364 }
static char quitdata[80]
Definition: term.c:46

◆ term_strip()

char* term_strip ( char *  outbuf,
const char *  inbuf,
int  maxout 
)

Remove colorings from a specified string.

Parameters
outbufthe location to write to
inbufthe original string
maxoutthe available size of outbuf
Returns
outbuf

Definition at line 311 of file term.c.

References ESC, and inbuf().

Referenced by action_command(), format_log_default(), format_log_json(), and format_log_plain().

312 {
313  char *outbuf_ptr = outbuf;
314  const char *inbuf_ptr = inbuf;
315 
316  while (outbuf_ptr < outbuf + maxout) {
317  switch (*inbuf_ptr) {
318  case ESC:
319  while (*inbuf_ptr && (*inbuf_ptr != 'm'))
320  inbuf_ptr++;
321  break;
322  default:
323  *outbuf_ptr = *inbuf_ptr;
324  outbuf_ptr++;
325  }
326  if (! *inbuf_ptr)
327  break;
328  inbuf_ptr++;
329  }
330  return outbuf;
331 }
static int inbuf(struct baseio *bio, FILE *fi)
utility used by inchar(), for base_encode()
#define ESC
Definition: term.h:30