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

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

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.
 
#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.
 
#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.

Function Documentation

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 341 of file term.c.

References AST_TERM_MAX_ESCAPE_CHARS, ast_threadstorage_get(), and term_color_code().

Referenced by print_queue().

342 {
343  struct commonbuf *cb = ast_threadstorage_get(&commonbuf, sizeof(*cb));
344  char *buf;
345 
346  if (!cb) {
347  return "";
348  }
349  buf = cb->buffer[cb->which++];
350  if (cb->which == AST_TERM_MAX_ROTATING_BUFFERS) {
351  cb->which = 0;
352  }
353 
354  return term_color_code(buf, fgcolor, bgcolor, AST_TERM_MAX_ESCAPE_CHARS);
355 }
void * ast_threadstorage_get(struct ast_threadstorage *ts, size_t init_size)
Retrieve thread storage.
#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:75
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:318
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 296 of file term.c.

References ast_str_append().

Referenced by ast_xmldoc_printable().

297 {
298  int attr = 0;
299 
300  if (!check_colors_allowed()) {
301  return -1;
302  }
303 
304  check_fgcolor(&fgcolor, &attr);
305  check_bgcolor(&bgcolor);
306 
307  if (ast_opt_force_black_background) {
308  ast_str_append(str, 0, "%c[%d;%d;%dm", ESC, attr, fgcolor, COLOR_BLACK + 10);
309  } else if (bgcolor) {
310  ast_str_append(str, 0, "%c[%d;%d;%dm", ESC, attr, fgcolor, bgcolor + 10);
311  } else {
312  ast_str_append(str, 0, "%c[%d;%dm", ESC, attr, fgcolor);
313  }
314 
315  return 0;
316 }
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:1139
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 357 of file term.c.

Referenced by ast_xmldoc_printable(), and print_queue().

358 {
359  return term_end();
360 }
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 235 of file term.c.

References ast_copy_string().

Referenced by __ast_register_translator(), ast_frame_dump(), ast_unregister_translator(), and lua_pbx_exec().

236 {
237  int attr = 0;
238 
239  if (!vt100compat) {
240  ast_copy_string(outbuf, inbuf, maxout);
241  return outbuf;
242  }
243  if (!fgcolor) {
244  ast_copy_string(outbuf, inbuf, maxout);
245  return outbuf;
246  }
247 
248  if (fgcolor & 128) {
249  attr = ast_opt_light_background ? 0 : ATTR_BRIGHT;
250  fgcolor &= ~128;
251  }
252 
253  if (bgcolor) {
254  bgcolor &= ~128;
255  }
256 
257  if (ast_opt_light_background) {
258  fgcolor = opposite(fgcolor);
259  }
260 
261  if (ast_opt_force_black_background) {
262  if (!bgcolor) {
263  bgcolor = COLOR_BLACK;
264  }
265  snprintf(outbuf, maxout, "%c[%d;%d;%dm%s%s", ESC, attr, fgcolor, bgcolor + 10, inbuf, term_end());
266  } else {
267  snprintf(outbuf, maxout, "%c[%d;%dm%s%s", ESC, attr, fgcolor, inbuf, term_end());
268  }
269  return outbuf;
270 }
static int inbuf(struct baseio *bio, FILE *fi)
utility used by inchar(), for base_encode()
Definition: utils.c:590
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425
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 318 of file term.c.

Referenced by ast_term_color().

319 {
320  int attr = 0;
321 
322  if (!check_colors_allowed()) {
323  *outbuf = '\0';
324  return outbuf;
325  }
326 
327  check_fgcolor(&fgcolor, &attr);
328  check_bgcolor(&bgcolor);
329 
330  if (ast_opt_force_black_background) {
331  snprintf(outbuf, maxout, "%c[%d;%d;%dm", ESC, attr, fgcolor, COLOR_BLACK + 10);
332  } else if (bgcolor) {
333  snprintf(outbuf, maxout, "%c[%d;%d;%dm", ESC, attr, fgcolor, bgcolor + 10);
334  } else {
335  snprintf(outbuf, maxout, "%c[%d;%dm", ESC, attr, fgcolor);
336  }
337 
338  return outbuf;
339 }
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 362 of file term.c.

References inbuf().

Referenced by action_command().

363 {
364  char *outbuf_ptr = outbuf;
365  const char *inbuf_ptr = inbuf;
366 
367  while (outbuf_ptr < outbuf + maxout) {
368  switch (*inbuf_ptr) {
369  case ESC:
370  while (*inbuf_ptr && (*inbuf_ptr != 'm'))
371  inbuf_ptr++;
372  break;
373  default:
374  *outbuf_ptr = *inbuf_ptr;
375  outbuf_ptr++;
376  }
377  if (! *inbuf_ptr)
378  break;
379  inbuf_ptr++;
380  }
381  return outbuf;
382 }
static int inbuf(struct baseio *bio, FILE *fi)
utility used by inchar(), for base_encode()
Definition: utils.c:590