Asterisk - The Open Source Telephony Project GIT-master-a358458
Data Structures | Macros | Functions | Variables
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"
#include "asterisk/threadstorage.h"
Include dependency graph for term.c:

Go to the source code of this file.

Data Structures

struct  commonbuf
 

Macros

#define HEADER_LEN   (12)
 
#define MAGIC_EXTNUM   (01036)
 
#define MAGIC_LEGACY   (00432)
 
#define MAX_COLORS_INDEX   (13)
 

Functions

static void __init_commonbuf (void)
 
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...
 
int ast_term_init (void)
 
const char * ast_term_reset (void)
 Returns the terminal reset code. More...
 
static void check_bgcolor (int *bgcolor)
 
static int check_colors_allowed (void)
 
static void check_fgcolor (int *fgcolor, int *attr)
 
static int convint (unsigned char *s)
 
static short convshort (unsigned char *s)
 
static int opposite (int color)
 
static int parse_terminfo_file (int fd)
 
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...
 

Variables

static struct ast_threadstorage commonbuf = { .once = PTHREAD_ONCE_INIT , .key_init = __init_commonbuf , .custom_init = NULL , }
 
static char enddata [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.

Macro Definition Documentation

◆ HEADER_LEN

#define HEADER_LEN   (12)

Definition at line 103 of file term.c.

◆ MAGIC_EXTNUM

#define MAGIC_EXTNUM   (01036)

Definition at line 101 of file term.c.

◆ MAGIC_LEGACY

#define MAGIC_LEGACY   (00432)

Definition at line 100 of file term.c.

◆ MAX_COLORS_INDEX

#define MAX_COLORS_INDEX   (13)

Definition at line 104 of file term.c.

Function Documentation

◆ __init_commonbuf()

static void __init_commonbuf ( void  )
static

Definition at line 55 of file term.c.

57{

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

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++];
351 cb->which = 0;
352 }
353
354 return term_color_code(buf, fgcolor, bgcolor, AST_TERM_MAX_ESCAPE_CHARS);
355}
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
Definition: term.c:57
short which
Definition: term.c:58
char buffer[AST_TERM_MAX_ROTATING_BUFFERS][AST_TERM_MAX_ESCAPE_CHARS]
Definition: term.c:59
char * term_color_code(char *outbuf, int fgcolor, int bgcolor, int maxout)
Write a color sequence to a string.
Definition: term.c:318
#define AST_TERM_MAX_ROTATING_BUFFERS
Definition: term.h:76
#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
void * ast_threadstorage_get(struct ast_threadstorage *ts, size_t init_size)
Retrieve thread storage.

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().

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

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
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}
const char * str
Definition: app_jack.c:147
#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:1139
static int check_colors_allowed(void)
Definition: term.c:291
static void check_bgcolor(int *bgcolor)
Definition: term.c:284
static void check_fgcolor(int *fgcolor, int *attr)
Definition: term.c:272
#define ESC
Definition: term.h:30
#define COLOR_BLACK
Definition: term.h:50

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

Referenced by ast_xmldoc_printable(), and cli_prompt().

◆ ast_term_init()

int ast_term_init ( void  )

Provided by term.c

Definition at line 165 of file term.c.

166{
167 char *term = getenv("TERM");
168 char termfile[256] = "";
169 int termfd = -1, parseokay = 0, i;
170
171 if (ast_opt_no_color) {
172 return 0;
173 }
174
175 if (!ast_opt_console) {
176 /* If any remote console is not compatible, we'll strip the color codes at that point */
177 vt100compat = 1;
178 goto end;
179 }
180
181 if (!term) {
182 return 0;
183 }
184
185 for (i = 0; !parseokay && termpath[i]; i++) {
186 snprintf(termfile, sizeof(termfile), "%s/%c/%s", termpath[i], *term, term);
187
188 termfd = open(termfile, O_RDONLY);
189 if (termfd > -1) {
190 parseokay = parse_terminfo_file(termfd);
191 close(termfd);
192 }
193 }
194
195 if (!parseokay) {
196 /* These comparisons should not be substrings nor case-insensitive, as
197 * terminal types are very particular about how they treat suffixes and
198 * capitalization. For example, terminal type 'linux-m' does NOT
199 * support color, while 'linux' does. Not even all vt100* terminals
200 * support color, either (e.g. 'vt100+fnkeys'). */
201 if (!strcmp(term, "linux")) {
202 vt100compat = 1;
203 } else if (!strcmp(term, "xterm")) {
204 vt100compat = 1;
205 } else if (!strcmp(term, "xterm-color")) {
206 vt100compat = 1;
207 } else if (!strcmp(term, "xterm-256color")) {
208 vt100compat = 1;
209 } else if (!strncmp(term, "Eterm", 5)) {
210 /* Both entries which start with Eterm support color */
211 vt100compat = 1;
212 } else if (!strcmp(term, "vt100")) {
213 vt100compat = 1;
214 } else if (!strncmp(term, "crt", 3)) {
215 /* Both crt terminals support color */
216 vt100compat = 1;
217 }
218 }
219
220end:
221 if (vt100compat) {
222 /* Make commands show up in nice colors */
224 snprintf(enddata, sizeof(enddata), "%c[%dm", ESC, COLOR_BLACK);
226 snprintf(enddata, sizeof(enddata), "%c[%d;%d;%dm", ESC, ATTR_RESET, COLOR_WHITE, COLOR_BLACK + 10);
227 } else {
228 snprintf(enddata, sizeof(enddata), "%c[%dm", ESC, ATTR_RESET);
229 }
230 snprintf(quitdata, sizeof(quitdata), "%c[%dm", ESC, ATTR_RESET);
231 }
232 return 0;
233}
char * end
Definition: eagi_proxy.c:73
#define ast_opt_console
Definition: options.h:111
#define ast_opt_no_color
Definition: options.h:116
#define ast_opt_light_background
Definition: options.h:130
static int vt100compat
Definition: term.c:43
static char enddata[80]
Definition: term.c:45
static int parse_terminfo_file(int fd)
Definition: term.c:106
static char quitdata[80]
Definition: term.c:46
static const char *const termpath[]
Definition: term.c:48
#define COLOR_WHITE
Definition: term.h:64
#define ATTR_RESET
Definition: term.h:36

References ast_opt_console, ast_opt_force_black_background, ast_opt_light_background, ast_opt_no_color, ATTR_RESET, COLOR_BLACK, COLOR_WHITE, end, enddata, ESC, parse_terminfo_file(), quitdata, termpath, and vt100compat.

Referenced by ast_remotecontrol(), asterisk_daemon(), and main().

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

358{
359 return term_end();
360}
const char * term_end(void)
Definition: term.c:407

References term_end().

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

◆ check_bgcolor()

static void check_bgcolor ( int *  bgcolor)
static

Definition at line 284 of file term.c.

285{
286 if (*bgcolor) {
287 *bgcolor &= ~128;
288 }
289}

Referenced by ast_term_color_code(), and term_color_code().

◆ check_colors_allowed()

static int check_colors_allowed ( void  )
static

Definition at line 291 of file term.c.

292{
293 return vt100compat;
294}

References vt100compat.

Referenced by ast_term_color_code(), and term_color_code().

◆ check_fgcolor()

static void check_fgcolor ( int *  fgcolor,
int *  attr 
)
static

Definition at line 272 of file term.c.

273{
275 if (*fgcolor & 128) {
276 *fgcolor &= ~128;
277 }
278
280 *fgcolor = opposite(*fgcolor);
281 }
282}
static int opposite(int color)
Definition: term.c:62
#define ATTR_BRIGHT
Definition: term.h:37

References ast_opt_light_background, ATTR_BRIGHT, and opposite().

Referenced by ast_term_color_code(), and term_color_code().

◆ convint()

static int convint ( unsigned char *  s)
inlinestatic

Definition at line 92 of file term.c.

93{
94 return s[0]
95 | s[1] << 8
96 | s[2] << 16
97 | s[3] << 24;
98}

Referenced by parse_terminfo_file().

◆ convshort()

static short convshort ( unsigned char *  s)
static

Definition at line 77 of file term.c.

78{
79 register int a, b;
80
81 a = (int) s[0] & 0377;
82 b = (int) s[1] & 0377;
83
84 if (a == 0377 && b == 0377)
85 return -1;
86 if (a == 0376 && b == 0377)
87 return -2;
88
89 return a + b * 256;
90}
static struct test_val b
static struct test_val a

References a, and b.

Referenced by parse_terminfo_file().

◆ opposite()

static int opposite ( int  color)
static

Definition at line 62 of file term.c.

63{
64 int lookup[] = {
65 /* BLACK */ COLOR_BLACK,
66 /* RED */ COLOR_MAGENTA,
67 /* GREEN */ COLOR_GREEN,
68 /* BROWN */ COLOR_BROWN,
69 /* BLUE */ COLOR_CYAN,
70 /* MAGENTA */ COLOR_RED,
71 /* CYAN */ COLOR_BLUE,
72 /* WHITE */ COLOR_BLACK };
73 return color ? lookup[color - 30] : 0;
74}
#define COLOR_BLUE
Definition: term.h:58
#define COLOR_CYAN
Definition: term.h:62
#define COLOR_MAGENTA
Definition: term.h:60
#define COLOR_BROWN
Definition: term.h:56
#define COLOR_RED
Definition: term.h:52
#define COLOR_GREEN
Definition: term.h:54

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

Referenced by check_fgcolor(), and term_color().

◆ parse_terminfo_file()

static int parse_terminfo_file ( int  fd)
static

Definition at line 106 of file term.c.

107{
108 int bytes_read, bytes_needed, num_size;
109 short magic, sz_names, sz_bools;
110 unsigned char buffer[1024];
111
112 bytes_read = read(fd, buffer, sizeof(buffer));
113 if (bytes_read < HEADER_LEN) {
114 return 0;
115 }
116
117 magic = convshort(buffer);
118
119 if (magic == MAGIC_LEGACY) {
120 num_size = 2;
121 } else if (magic == MAGIC_EXTNUM) {
122 /* Extended number format (ncurses 6.1) */
123 num_size = 4;
124 } else {
125 /* We don't know how to parse this file */
126 return 0;
127 }
128
129 sz_names = convshort(buffer + 2);
130 sz_bools = convshort(buffer + 4);
131
132 /* From term(5):
133 * Between the boolean section and the number section, a null byte will be
134 * inserted, if necessary, to ensure that the number section begins on an
135 * even byte. */
136 if ((sz_names + sz_bools) & 1) {
137 sz_bools++;
138 }
139
140 bytes_needed = HEADER_LEN + sz_names + sz_bools + ((MAX_COLORS_INDEX + 1) * num_size);
141 if (bytes_needed <= bytes_read) {
142 /* Offset 13 is defined in /usr/include/term.h, though we do not
143 * include it here, as it conflicts with include/asterisk/term.h */
144 int max_colors;
145 int offset = HEADER_LEN + sz_names + sz_bools + MAX_COLORS_INDEX * num_size;
146
147 if (num_size == 2) {
148 /* In the legacy terminfo format, numbers are signed shorts */
149 max_colors = convshort(buffer + offset);
150 } else {
151 /* Extended number format makes them signed ints */
152 max_colors = convint(buffer + offset);
153 }
154
155 if (max_colors > 0) {
156 vt100compat = 1;
157 }
158
159 return 1;
160 }
161
162 return 0;
163}
#define MAGIC_LEGACY
Definition: term.c:100
static short convshort(unsigned char *s)
Definition: term.c:77
static int convint(unsigned char *s)
Definition: term.c:92
#define HEADER_LEN
Definition: term.c:103
#define MAGIC_EXTNUM
Definition: term.c:101
#define MAX_COLORS_INDEX
Definition: term.c:104

References convint(), convshort(), HEADER_LEN, MAGIC_EXTNUM, MAGIC_LEGACY, MAX_COLORS_INDEX, and vt100compat.

Referenced by ast_term_init().

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

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) {
250 fgcolor &= ~128;
251 }
252
253 if (bgcolor) {
254 bgcolor &= ~128;
255 }
256
258 fgcolor = opposite(fgcolor);
259 }
260
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}
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425
static int inbuf(struct baseio *bio, FILE *fi)
utility used by inchar(), for base_encode()
Definition: utils.c:590

References ast_copy_string(), ast_opt_force_black_background, ast_opt_light_background, ATTR_BRIGHT, COLOR_BLACK, ESC, inbuf(), 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(), and start_resource().

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

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
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}

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

Referenced by ast_term_color().

◆ term_end()

const char * term_end ( void  )

Definition at line 407 of file term.c.

408{
409 return enddata;
410}

References enddata.

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

◆ term_filter_escapes()

void term_filter_escapes ( char *  line)

Definition at line 385 of file term.c.

386{
387 int i;
388 int len = strlen(line);
389
390 for (i = 0; i < len; i++) {
391 if (line[i] != ESC)
392 continue;
393 if ((i < (len - 2)) &&
394 (line[i + 1] == 0x5B)) {
395 switch (line[i + 2]) {
396 case 0x30:
397 case 0x31:
398 case 0x33:
399 continue;
400 }
401 }
402 /* replace ESC with a space */
403 line[i] = ' ';
404 }
405}
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)

References ESC, and len().

◆ term_quit()

const char * term_quit ( void  )

Definition at line 412 of file term.c.

413{
414 return quitdata;
415}

References quitdata.

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

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

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}

References ESC, and inbuf().

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

Variable Documentation

◆ commonbuf

struct ast_threadstorage commonbuf = { .once = PTHREAD_ONCE_INIT , .key_init = __init_commonbuf , .custom_init = NULL , }
static

Definition at line 55 of file term.c.

◆ enddata

char enddata[80] = ""
static

Definition at line 45 of file term.c.

Referenced by ast_term_init(), and term_end().

◆ quitdata

char quitdata[80] = ""
static

Definition at line 46 of file term.c.

Referenced by ast_term_init(), and term_quit().

◆ termpath

const char* const termpath[]
static

Definition at line 48 of file term.c.

Referenced by ast_term_init().

◆ vt100compat

int vt100compat
static

Definition at line 43 of file term.c.

Referenced by ast_term_init(), check_colors_allowed(), parse_terminfo_file(), and term_color().