Thu Dec 17 13:34:07 2009

Asterisk developer's documentation


term.c File Reference

Terminal Routines. More...

#include "asterisk.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <signal.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "asterisk/term.h"
#include "asterisk/options.h"
#include "asterisk/lock.h"
#include "asterisk/utils.h"

Go to the source code of this file.

Functions

int ast_term_init (void)
static short convshort (char *s)
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)
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, char *inbuf, int maxout)

Variables

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


Detailed Description

Terminal Routines.

Author:
Mark Spencer <markster@digium.com>

Definition in file term.c.


Function Documentation

int ast_term_init ( void   ) 

Provided by term.c

Definition at line 75 of file term.c.

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

Referenced by main().

00076 {
00077    char *term = getenv("TERM");
00078    char termfile[256] = "";
00079    char buffer[512] = "";
00080    int termfd = -1, parseokay = 0, i;
00081 
00082    if (!term)
00083       return 0;
00084    if (!ast_opt_console || ast_opt_no_color || !ast_opt_no_fork)
00085       return 0;
00086 
00087    for (i=0 ;; i++) {
00088       if (termpath[i] == NULL) {
00089          break;
00090       }
00091       snprintf(termfile, sizeof(termfile), "%s/%c/%s", termpath[i], *term, term);
00092       termfd = open(termfile, O_RDONLY);
00093       if (termfd > -1) {
00094          break;
00095       }
00096    }
00097    if (termfd > -1) {
00098       int actsize = read(termfd, buffer, sizeof(buffer) - 1);
00099       short sz_names = convshort(buffer + 2);
00100       short sz_bools = convshort(buffer + 4);
00101       short n_nums   = convshort(buffer + 6);
00102 
00103       /* if ((sz_names + sz_bools) & 1)
00104          sz_bools++; */
00105 
00106       if (sz_names + sz_bools + n_nums < actsize) {
00107          /* Offset 13 is defined in /usr/include/term.h, though we do not
00108           * include it here, as it conflicts with include/asterisk/term.h */
00109          short max_colors = convshort(buffer + 12 + sz_names + sz_bools + 13 * 2);
00110          if (max_colors > 0) {
00111             vt100compat = 1;
00112          }
00113          parseokay = 1;
00114       }
00115       close(termfd);
00116    }
00117 
00118    if (!parseokay) {
00119       /* These comparisons should not be substrings nor case-insensitive, as
00120        * terminal types are very particular about how they treat suffixes and
00121        * capitalization.  For example, terminal type 'linux-m' does NOT
00122        * support color, while 'linux' does.  Not even all vt100* terminals
00123        * support color, either (e.g. 'vt100+fnkeys'). */
00124       if (!strcmp(term, "linux")) {
00125          vt100compat = 1;
00126       } else if (!strcmp(term, "xterm")) {
00127          vt100compat = 1;
00128       } else if (!strcmp(term, "xterm-color")) {
00129          vt100compat = 1;
00130       } else if (!strncmp(term, "Eterm", 5)) {
00131          /* Both entries which start with Eterm support color */
00132          vt100compat = 1;
00133       } else if (!strcmp(term, "vt100")) {
00134          vt100compat = 1;
00135       } else if (!strncmp(term, "crt", 3)) {
00136          /* Both crt terminals support color */
00137          vt100compat = 1;
00138       }
00139    }
00140 
00141    if (vt100compat) {
00142       /* Make commands show up in nice colors */
00143       snprintf(prepdata, sizeof(prepdata), "%c[%d;%d;%dm", ESC, ATTR_BRIGHT, COLOR_BROWN, COLOR_BLACK + 10);
00144       snprintf(enddata, sizeof(enddata), "%c[%d;%d;%dm", ESC, ATTR_RESET, COLOR_WHITE, COLOR_BLACK + 10);
00145       snprintf(quitdata, sizeof(quitdata), "%c[0m", ESC);
00146    }
00147    return 0;
00148 }

static short convshort ( char *  s  )  [static]

Definition at line 60 of file term.c.

Referenced by ast_term_init().

00061 {
00062    register int a,b;
00063 
00064    a = (int) s[0] & 0377;
00065    b = (int) s[1] & 0377;
00066 
00067    if (a == 0377 && b == 0377)
00068       return -1;
00069    if (a == 0376 && b == 0377)
00070       return -2;
00071 
00072    return a + b * 256;
00073 }

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

Definition at line 150 of file term.c.

References ast_copy_string(), ATTR_BRIGHT, COLOR_BLACK, COLOR_WHITE, and ESC.

Referenced by __ast_register_translator(), ast_frame_dump(), ast_log(), ast_register_application(), ast_unregister_translator(), fix_header(), handle_dahdi_show_cadences(), handle_show_application(), handle_show_application_deprecated(), handle_show_function(), handle_show_function_deprecated(), load_resource(), main(), pbx_extension_helper(), realtime_exec(), and show_config_description().

00151 {
00152    int attr=0;
00153    char tmp[40];
00154    if (!vt100compat) {
00155       ast_copy_string(outbuf, inbuf, maxout);
00156       return outbuf;
00157    }
00158    if (!fgcolor && !bgcolor) {
00159       ast_copy_string(outbuf, inbuf, maxout);
00160       return outbuf;
00161    }
00162    if ((fgcolor & 128) && (bgcolor & 128)) {
00163       /* Can't both be highlighted */
00164       ast_copy_string(outbuf, inbuf, maxout);
00165       return outbuf;
00166    }
00167    if (!bgcolor)
00168       bgcolor = COLOR_BLACK;
00169 
00170    if (bgcolor) {
00171       bgcolor &= ~128;
00172       bgcolor += 10;
00173    }
00174    if (fgcolor & 128) {
00175       attr = ATTR_BRIGHT;
00176       fgcolor &= ~128;
00177    }
00178    if (fgcolor && bgcolor) {
00179       snprintf(tmp, sizeof(tmp), "%d;%d", fgcolor, bgcolor);
00180    } else if (bgcolor) {
00181       snprintf(tmp, sizeof(tmp), "%d", bgcolor);
00182    } else if (fgcolor) {
00183       snprintf(tmp, sizeof(tmp), "%d", fgcolor);
00184    }
00185    if (attr) {
00186       snprintf(outbuf, maxout, "%c[%d;%sm%s%c[0;%d;%dm", ESC, attr, tmp, inbuf, ESC, COLOR_WHITE, COLOR_BLACK + 10);
00187    } else {
00188       snprintf(outbuf, maxout, "%c[%sm%s%c[0;%d;%dm", ESC, tmp, inbuf, ESC, COLOR_WHITE, COLOR_BLACK + 10);
00189    }
00190    return outbuf;
00191 }

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

Definition at line 193 of file term.c.

References ATTR_BRIGHT, COLOR_BLACK, and ESC.

Referenced by cli_prompt().

00194 {
00195    int attr=0;
00196    char tmp[40];
00197    if ((!vt100compat) || (!fgcolor && !bgcolor)) {
00198       *outbuf = '\0';
00199       return outbuf;
00200    }
00201    if ((fgcolor & 128) && (bgcolor & 128)) {
00202       /* Can't both be highlighted */
00203       *outbuf = '\0';
00204       return outbuf;
00205    }
00206    if (!bgcolor)
00207       bgcolor = COLOR_BLACK;
00208 
00209    if (bgcolor) {
00210       bgcolor &= ~128;
00211       bgcolor += 10;
00212    }
00213    if (fgcolor & 128) {
00214       attr = ATTR_BRIGHT;
00215       fgcolor &= ~128;
00216    }
00217    if (fgcolor && bgcolor) {
00218       snprintf(tmp, sizeof(tmp), "%d;%d", fgcolor, bgcolor);
00219    } else if (bgcolor) {
00220       snprintf(tmp, sizeof(tmp), "%d", bgcolor);
00221    } else if (fgcolor) {
00222       snprintf(tmp, sizeof(tmp), "%d", fgcolor);
00223    }
00224    if (attr) {
00225       snprintf(outbuf, maxout, "%c[%d;%sm", ESC, attr, tmp);
00226    } else {
00227       snprintf(outbuf, maxout, "%c[%sm", ESC, tmp);
00228    }
00229    return outbuf;
00230 }

char* term_end ( void   ) 

Definition at line 295 of file term.c.

Referenced by consolehandler(), and main().

00296 {
00297    return enddata;
00298 }

void term_filter_escapes ( char *  line  ) 

Definition at line 268 of file term.c.

References ESC, and len().

Referenced by ast_log(), and ast_verbose().

00269 {
00270    int i;
00271    int len = strlen(line);
00272 
00273    for (i = 0; i < len; i++) {
00274       if (line[i] != ESC)
00275          continue;
00276       if ((i < (len - 2)) &&
00277           (line[i + 1] == 0x5B)) {
00278          switch (line[i + 2]) {
00279          case 0x30:
00280          case 0x31:
00281          case 0x33:
00282             continue;
00283          }
00284       }
00285       /* replace ESC with a space */
00286       line[i] = ' ';
00287    }
00288 }

char* term_prep ( void   ) 

Definition at line 290 of file term.c.

00291 {
00292    return prepdata;
00293 }

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

Definition at line 253 of file term.c.

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

00254 {
00255    if (!vt100compat) {
00256       ast_copy_string(outbuf, inbuf, maxout);
00257       return outbuf;
00258    }
00259    snprintf(outbuf, maxout, "%c[%d;%d;%dm%c%c[%d;%d;%dm%s",
00260       ESC, ATTR_BRIGHT, COLOR_BLUE, COLOR_BLACK + 10,
00261       inbuf[0],
00262       ESC, 0, COLOR_WHITE, COLOR_BLACK + 10,
00263       inbuf + 1);
00264    return outbuf;
00265 }

char* term_quit ( void   ) 

Definition at line 300 of file term.c.

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

00301 {
00302    return quitdata;
00303 }

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

Definition at line 232 of file term.c.

References ESC.

Referenced by __verboser(), action_command(), ast_log(), and ast_log_vsyslog().

00233 {
00234    char *outbuf_ptr = outbuf, *inbuf_ptr = inbuf;
00235 
00236    while (outbuf_ptr < outbuf + maxout) {
00237       switch (*inbuf_ptr) {
00238          case ESC:
00239             while (*inbuf_ptr && (*inbuf_ptr != 'm'))
00240                inbuf_ptr++;
00241             break;
00242          default:
00243             *outbuf_ptr = *inbuf_ptr;
00244             outbuf_ptr++;
00245       }
00246       if (! *inbuf_ptr)
00247          break;
00248       inbuf_ptr++;
00249    }
00250    return outbuf;
00251 }


Variable Documentation

char enddata[80] = "" [static]

Definition at line 49 of file term.c.

char prepdata[80] = "" [static]

Definition at line 48 of file term.c.

char quitdata[80] = "" [static]

Definition at line 50 of file term.c.

const char* termpath[] [static]

Definition at line 52 of file term.c.

int vt100compat [static]

Definition at line 46 of file term.c.


Generated on Thu Dec 17 13:34:07 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7