include/asterisk/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.

Defines

#define AST_TERM_MAX_ESCAPE_CHARS   12
 Maximum number of characters needed for a color escape sequence, 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_BLINK   5
#define ATTR_BRIGHT   1
#define ATTR_DIM   2
#define ATTR_HIDDEN   8
#define ATTR_RESET   0
#define ATTR_REVER   7
#define ATTR_UNDER   4
Terminal Colors
#define COLOR_BLACK   30
#define COLOR_BLUE   34
#define COLOR_BRBLUE   (34 | 128)
#define COLOR_BRCYAN   (36 | 128)
#define COLOR_BRGREEN   (32 | 128)
#define COLOR_BRMAGENTA   (35 | 128)
#define COLOR_BROWN   33
#define COLOR_BRRED   (31 | 128)
#define COLOR_BRWHITE   (37 | 128)
#define COLOR_CYAN   36
#define COLOR_GRAY   (30 | 128)
#define COLOR_GREEN   32
#define COLOR_MAGENTA   35
#define COLOR_RED   31
#define COLOR_WHITE   37
#define COLOR_YELLOW   (33 | 128)

Functions

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


Detailed Description

Handy terminal functions for vt* terms.

Definition in file include/asterisk/term.h.


Define Documentation

#define AST_TERM_MAX_ESCAPE_CHARS   12

Maximum number of characters needed for a color escape sequence, plus a null char.

Definition at line 71 of file include/asterisk/term.h.

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

#define AST_TERM_MAX_ROTATING_BUFFERS   15

Definition at line 72 of file include/asterisk/term.h.

Referenced by ast_term_color().

#define ATTR_BLINK   5

Definition at line 39 of file include/asterisk/term.h.

#define ATTR_BRIGHT   1

Definition at line 36 of file include/asterisk/term.h.

Referenced by ast_term_init(), check_fgcolor(), term_color(), and term_prompt().

#define ATTR_DIM   2

Definition at line 37 of file include/asterisk/term.h.

#define ATTR_HIDDEN   8

Definition at line 41 of file include/asterisk/term.h.

#define ATTR_RESET   0

Definition at line 35 of file include/asterisk/term.h.

Referenced by ast_term_init().

#define ATTR_REVER   7

Definition at line 40 of file include/asterisk/term.h.

#define ATTR_UNDER   4

Definition at line 38 of file include/asterisk/term.h.

#define COLOR_BLACK   30

#define COLOR_BLUE   34

Definition at line 55 of file include/asterisk/term.h.

Referenced by data_result_get_color(), opposite(), and term_prompt().

#define COLOR_BRBLUE   (34 | 128)

Definition at line 56 of file include/asterisk/term.h.

#define COLOR_BRCYAN   (36 | 128)

#define COLOR_BRGREEN   (32 | 128)

Definition at line 52 of file include/asterisk/term.h.

Referenced by ast_frame_dump(), and main().

#define COLOR_BRMAGENTA   (35 | 128)

#define COLOR_BROWN   33

Definition at line 53 of file include/asterisk/term.h.

Referenced by __queues_show(), ast_term_init(), opposite(), and start_resource().

#define COLOR_BRRED   (31 | 128)

Definition at line 50 of file include/asterisk/term.h.

Referenced by ast_frame_dump().

#define COLOR_BRWHITE   (37 | 128)

Definition at line 62 of file include/asterisk/term.h.

Referenced by logger_print_normal(), and show_config_description().

#define COLOR_CYAN   36

#define COLOR_GRAY   (30 | 128)

Definition at line 48 of file include/asterisk/term.h.

Referenced by set_header().

#define COLOR_GREEN   32

Definition at line 51 of file include/asterisk/term.h.

Referenced by __queues_show(), handle_dahdi_show_cadences(), and opposite().

#define COLOR_MAGENTA   35

#define COLOR_RED   31

Definition at line 49 of file include/asterisk/term.h.

Referenced by __queues_show(), and opposite().

#define COLOR_WHITE   37

Definition at line 61 of file include/asterisk/term.h.

Referenced by ast_term_init(), cli_prompt(), term_color(), and term_prompt().

#define COLOR_YELLOW   (33 | 128)

Definition at line 54 of file include/asterisk/term.h.

Referenced by ast_frame_dump(), and show_config_description().

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

#define COLORIZE_FMT   "%s%s%s"

#define ESC   0x1b


Function Documentation

const char* ast_term_color ( int  fgcolor,
int  bgcolor 
)

Return a color sequence string.

Parameters:
fgcolor foreground color
bgcolor background 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 295 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 __queues_show(), print_app_docs(), and set_header().

00296 {
00297    struct commonbuf *cb = ast_threadstorage_get(&commonbuf, sizeof(*cb));
00298    char *buf;
00299 
00300    if (!cb) {
00301       return "";
00302    }
00303    buf = cb->buffer[cb->which++];
00304    if (cb->which == AST_TERM_MAX_ROTATING_BUFFERS) {
00305       cb->which = 0;
00306    }
00307 
00308    return term_color_code(buf, fgcolor, bgcolor, AST_TERM_MAX_ESCAPE_CHARS);
00309 }

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

Append a color sequence to an ast_str.

Parameters:
str The string to append to
fgcolor foreground color
bgcolor background color
Return values:
0 success
-1 failure

Definition at line 250 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 cli_prompt(), and data_result_print_cli_node().

00251 {
00252    int attr = 0;
00253 
00254    if (!check_colors_allowed(fgcolor)) {
00255       return -1;
00256    }
00257 
00258    check_fgcolor(&fgcolor, &attr);
00259    check_bgcolor(&bgcolor);
00260 
00261    if (ast_opt_force_black_background) {
00262       ast_str_append(str, 0, "%c[%d;%d;%dm", ESC, attr, fgcolor, COLOR_BLACK + 10);
00263    } else if (bgcolor) {
00264       ast_str_append(str, 0, "%c[%d;%d;%dm", ESC, attr, fgcolor, bgcolor + 10);
00265    } else {
00266       ast_str_append(str, 0, "%c[%d;%dm", ESC, attr, fgcolor);
00267    }
00268 
00269    return 0;
00270 }

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

References ast_opt_force_black_background, COLOR_BLACK, and ESC.

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

00312 {
00313    if (ast_opt_force_black_background) {
00314       static const char reset[] = { ESC, '[', COLOR_BLACK + 10, 'm', 0 };
00315       return reset;
00316    } else {
00317       return quitdata;
00318    }
00319 }

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

Colorize a specified string by adding terminal color codes.

Parameters:
outbuf Result buffer
inbuf Starting string
fgcolor Foreground color, specified as one of the constants in include/asterisk/term.h. Use '0' if the want the normal terminal foreground color.
bgcolor Background color, specified as one of the constants in include/asterisk/term.h. Use '0' if you want the normal terminal background color.
maxout Maximum 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 192 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_register_translator(), ast_frame_dump(), ast_unregister_translator(), handle_cli_agi_show(), handle_dahdi_show_cadences(), handle_show_function(), handle_showmancmd(), lua_pbx_exec(), realtime_exec(), show_config_description(), and start_resource().

00193 {
00194    int attr = 0;
00195 
00196    if (!vt100compat) {
00197       ast_copy_string(outbuf, inbuf, maxout);
00198       return outbuf;
00199    }
00200    if (!fgcolor) {
00201       ast_copy_string(outbuf, inbuf, maxout);
00202       return outbuf;
00203    }
00204 
00205    if (fgcolor & 128) {
00206       attr = ast_opt_light_background ? 0 : ATTR_BRIGHT;
00207       fgcolor &= ~128;
00208    }
00209 
00210    if (bgcolor) {
00211       bgcolor &= ~128;
00212    }
00213 
00214    if (ast_opt_light_background) {
00215       fgcolor = opposite(fgcolor);
00216    }
00217 
00218    if (ast_opt_force_black_background) {
00219       snprintf(outbuf, maxout, "%c[%d;%d;%dm%s%c[%d;%dm", ESC, attr, fgcolor, bgcolor + 10, inbuf, ESC, COLOR_WHITE, COLOR_BLACK + 10);
00220    } else {
00221       snprintf(outbuf, maxout, "%c[%d;%dm%s%c[0m", ESC, attr, fgcolor, inbuf, ESC);
00222    }
00223    return outbuf;
00224 }

char* term_color_code ( char *  outbuf,
int  fgcolor,
int  bgcolor,
int  maxout 
)

Write a color sequence to a string.

Parameters:
outbuf the location to write to
fgcolor foreground color
bgcolor background color
maxout maximum number of characters to write
Deprecated:
You should use ast_term_color_code or ast_term_color, instead.
Returns:
outbuf

Definition at line 272 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().

00273 {
00274    int attr = 0;
00275 
00276    if (!check_colors_allowed(fgcolor)) {
00277       *outbuf = '\0';
00278       return outbuf;
00279    }
00280 
00281    check_fgcolor(&fgcolor, &attr);
00282    check_bgcolor(&bgcolor);
00283 
00284    if (ast_opt_force_black_background) {
00285       snprintf(outbuf, maxout, "%c[%d;%d;%dm", ESC, attr, fgcolor, COLOR_BLACK + 10);
00286    } else if (bgcolor) {
00287       snprintf(outbuf, maxout, "%c[%d;%d;%dm", ESC, attr, fgcolor, bgcolor + 10);
00288    } else {
00289       snprintf(outbuf, maxout, "%c[%d;%dm", ESC, attr, fgcolor);
00290    }
00291 
00292    return outbuf;
00293 }

const char* term_end ( void   ) 

Definition at line 399 of file term.c.

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

00400 {
00401    return enddata;
00402 }

void term_filter_escapes ( char *  line  ) 

Definition at line 372 of file term.c.

References ESC, and len().

Referenced by ast_log_full().

00373 {
00374    int i;
00375    int len = strlen(line);
00376 
00377    for (i = 0; i < len; i++) {
00378       if (line[i] != ESC)
00379          continue;
00380       if ((i < (len - 2)) &&
00381           (line[i + 1] == 0x5B)) {
00382          switch (line[i + 2]) {
00383          case 0x30:
00384          case 0x31:
00385          case 0x33:
00386             continue;
00387          }
00388       }
00389       /* replace ESC with a space */
00390       line[i] = ' ';
00391    }
00392 }

const char* term_prep ( void   ) 

Definition at line 394 of file term.c.

00395 {
00396    return prepdata;
00397 }

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

Definition at line 343 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.

00344 {
00345    if (!vt100compat) {
00346       ast_copy_string(outbuf, inbuf, maxout);
00347       return outbuf;
00348    }
00349    if (ast_opt_force_black_background) {
00350       snprintf(outbuf, maxout, "%c[%d;%d;%dm%c%c[%d;%dm%s",
00351          ESC, ATTR_BRIGHT, COLOR_BLUE, COLOR_BLACK + 10,
00352          inbuf[0],
00353          ESC, COLOR_WHITE, COLOR_BLACK + 10,
00354          inbuf + 1);
00355    } else if (ast_opt_light_background) {
00356       snprintf(outbuf, maxout, "%c[%d;0m%c%c[0m%s",
00357          ESC, COLOR_BLUE,
00358          inbuf[0],
00359          ESC,
00360          inbuf + 1);
00361    } else {
00362       snprintf(outbuf, maxout, "%c[%d;%d;0m%c%c[0m%s",
00363          ESC, ATTR_BRIGHT, COLOR_BLUE,
00364          inbuf[0],
00365          ESC,
00366          inbuf + 1);
00367    }
00368    return outbuf;
00369 }

const char* term_quit ( void   ) 

Definition at line 404 of file term.c.

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

00405 {
00406    return quitdata;
00407 }

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

Remove colorings from a specified string.

Parameters:
outbuf the location to write to
inbuf the original string
maxout the available size of outbuf
Returns:
outbuf

Definition at line 321 of file term.c.

References ESC.

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

00322 {
00323    char *outbuf_ptr = outbuf;
00324    const char *inbuf_ptr = inbuf;
00325 
00326    while (outbuf_ptr < outbuf + maxout) {
00327       switch (*inbuf_ptr) {
00328          case ESC:
00329             while (*inbuf_ptr && (*inbuf_ptr != 'm'))
00330                inbuf_ptr++;
00331             break;
00332          default:
00333             *outbuf_ptr = *inbuf_ptr;
00334             outbuf_ptr++;
00335       }
00336       if (! *inbuf_ptr)
00337          break;
00338       inbuf_ptr++;
00339    }
00340    return outbuf;
00341 }


Generated on Sun Sep 21 06:33:56 2014 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6