Thu Apr 28 2011 16:57:19

Asterisk developer's documentation


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 dependency graph for term.c:

Go to the source code of this file.

Functions

int ast_term_color_code (struct ast_str **str, int fgcolor, int bgcolor)
 Append a color sequence to an ast_str.
int ast_term_init (void)
static void check_bgcolor (int *bgcolor)
static int check_colors_allowed (int fgcolor)
static void check_fgcolor (int *fgcolor, int *attr)
static short convshort (char *s)
static int opposite (int color)
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)
 Write a color sequence to a string.
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_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 236 of file term.c.

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

{
   int attr = 0;

   if (!check_colors_allowed(fgcolor)) {
      return -1;
   }

   check_fgcolor(&fgcolor, &attr);
   check_bgcolor(&bgcolor);
   
   if (ast_opt_force_black_background) {
      ast_str_append(str, 0, "%c[%d;%d;%dm", ESC, attr, fgcolor, COLOR_BLACK + 10);
   } else if (bgcolor) {
      ast_str_append(str, 0, "%c[%d;%d;%dm", ESC, attr, fgcolor, bgcolor + 10);
   } else {
      ast_str_append(str, 0, "%c[%d;%dm", ESC, attr, fgcolor);
   }

   return 0;
}
int ast_term_init ( void  )

Provided by term.c

Definition at line 83 of file term.c.

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

Referenced by main().

{
   char *term = getenv("TERM");
   char termfile[256] = "";
   char buffer[512] = "";
   int termfd = -1, parseokay = 0, i;

   if (ast_opt_no_color) {
      return 0;
   }

   if (!ast_opt_console) {
      /* If any remote console is not compatible, we'll strip the color codes at that point */
      vt100compat = 1;
      goto end;
   }

   if (!term) {
      return 0;
   }

   for (i = 0;; i++) {
      if (termpath[i] == NULL) {
         break;
      }
      snprintf(termfile, sizeof(termfile), "%s/%c/%s", termpath[i], *term, term);
      termfd = open(termfile, O_RDONLY);
      if (termfd > -1) {
         break;
      }
   }
   if (termfd > -1) {
      int actsize = read(termfd, buffer, sizeof(buffer) - 1);
      short sz_names = convshort(buffer + 2);
      short sz_bools = convshort(buffer + 4);
      short n_nums   = convshort(buffer + 6);

      /* if ((sz_names + sz_bools) & 1)
         sz_bools++; */

      if (sz_names + sz_bools + n_nums < actsize) {
         /* Offset 13 is defined in /usr/include/term.h, though we do not
          * include it here, as it conflicts with include/asterisk/term.h */
         short max_colors = convshort(buffer + 12 + sz_names + sz_bools + 13 * 2);
         if (max_colors > 0) {
            vt100compat = 1;
         }
         parseokay = 1;
      }
      close(termfd);
   }

   if (!parseokay) {
      /* These comparisons should not be substrings nor case-insensitive, as
       * terminal types are very particular about how they treat suffixes and
       * capitalization.  For example, terminal type 'linux-m' does NOT
       * support color, while 'linux' does.  Not even all vt100* terminals
       * support color, either (e.g. 'vt100+fnkeys'). */
      if (!strcmp(term, "linux")) {
         vt100compat = 1;
      } else if (!strcmp(term, "xterm")) {
         vt100compat = 1;
      } else if (!strcmp(term, "xterm-color")) {
         vt100compat = 1;
      } else if (!strncmp(term, "Eterm", 5)) {
         /* Both entries which start with Eterm support color */
         vt100compat = 1;
      } else if (!strcmp(term, "vt100")) {
         vt100compat = 1;
      } else if (!strncmp(term, "crt", 3)) {
         /* Both crt terminals support color */
         vt100compat = 1;
      }
   }

end:
   if (vt100compat) {
      /* Make commands show up in nice colors */
      if (ast_opt_light_background) {
         snprintf(prepdata, sizeof(prepdata), "%c[%dm", ESC, COLOR_BROWN);
         snprintf(enddata, sizeof(enddata), "%c[%dm", ESC, COLOR_BLACK);
         snprintf(quitdata, sizeof(quitdata), "%c[0m", ESC);
      } else if (ast_opt_force_black_background) {
         snprintf(prepdata, sizeof(prepdata), "%c[%d;%d;%dm", ESC, ATTR_BRIGHT, COLOR_BROWN, COLOR_BLACK + 10);
         snprintf(enddata, sizeof(enddata), "%c[%d;%d;%dm", ESC, ATTR_RESET, COLOR_WHITE, COLOR_BLACK + 10);
         snprintf(quitdata, sizeof(quitdata), "%c[0m", ESC);
      } else {
         snprintf(prepdata, sizeof(prepdata), "%c[%d;%dm", ESC, ATTR_BRIGHT, COLOR_BROWN);
         snprintf(enddata, sizeof(enddata), "%c[%d;%dm", ESC, ATTR_RESET, COLOR_WHITE);
         snprintf(quitdata, sizeof(quitdata), "%c[0m", ESC);
      }
   }
   return 0;
}
static void check_bgcolor ( int *  bgcolor) [static]

Definition at line 224 of file term.c.

Referenced by ast_term_color_code(), and term_color_code().

{
   if (*bgcolor) {
      *bgcolor &= ~128;
   }
}
static int check_colors_allowed ( int  fgcolor) [static]

Definition at line 231 of file term.c.

Referenced by ast_term_color_code(), and term_color_code().

{
   return (!vt100compat || !fgcolor) ? 0 : 1;
}
static void check_fgcolor ( int *  fgcolor,
int *  attr 
) [static]

Definition at line 212 of file term.c.

References ast_opt_light_background, ATTR_BRIGHT, and opposite().

Referenced by ast_term_color_code(), and term_color_code().

{
   if (*fgcolor & 128) {
      *attr = ast_opt_light_background ? 0 : ATTR_BRIGHT;
      *fgcolor &= ~128;
   }
   
   if (ast_opt_light_background) {
      *fgcolor = opposite(*fgcolor);
   }
}
static short convshort ( char *  s) [static]

Definition at line 68 of file term.c.

Referenced by ast_term_init().

{
   register int a, b;

   a = (int) s[0] & 0377;
   b = (int) s[1] & 0377;

   if (a == 0377 && b == 0377)
      return -1;
   if (a == 0376 && b == 0377)
      return -2;

   return a + b * 256;
}
static int opposite ( int  color) [static]

Definition at line 53 of file term.c.

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

Referenced by check_fgcolor(), and term_color().

{
   int lookup[] = {
      /* BLACK */ COLOR_BLACK,
      /* RED */ COLOR_MAGENTA,
      /* GREEN */ COLOR_GREEN,
      /* BROWN */ COLOR_BROWN,
      /* BLUE */ COLOR_CYAN,
      /* MAGENTA */ COLOR_RED,
      /* CYAN */ COLOR_BLUE,
      /* WHITE */ COLOR_BLACK };
   return color ? lookup[color - 30] : 0;
}
char* term_color ( char *  outbuf,
const char *  inbuf,
int  fgcolor,
int  bgcolor,
int  maxout 
)

Definition at line 178 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_custom_function_register(), __ast_register_translator(), ast_frame_dump(), ast_register_application2(), ast_unregister_translator(), fix_header(), handle_cli_agi_show(), handle_dahdi_show_cadences(), handle_show_function(), logger_print_normal(), lua_pbx_exec(), main(), pbx_extension_helper(), print_app_docs(), realtime_exec(), show_config_description(), and start_resource().

{
   int attr = 0;

   if (!vt100compat) {
      ast_copy_string(outbuf, inbuf, maxout);
      return outbuf;
   }
   if (!fgcolor) {
      ast_copy_string(outbuf, inbuf, maxout);
      return outbuf;
   }

   if (fgcolor & 128) {
      attr = ast_opt_light_background ? 0 : ATTR_BRIGHT;
      fgcolor &= ~128;
   }

   if (bgcolor) {
      bgcolor &= ~128;
   }

   if (ast_opt_light_background) {
      fgcolor = opposite(fgcolor);
   }

   if (ast_opt_force_black_background) {
      snprintf(outbuf, maxout, "%c[%d;%d;%dm%s%c[%d;%dm", ESC, attr, fgcolor, bgcolor + 10, inbuf, ESC, COLOR_WHITE, COLOR_BLACK + 10);
   } else {
      snprintf(outbuf, maxout, "%c[%d;%dm%s%c[0m", ESC, attr, fgcolor, inbuf, ESC);
   }
   return outbuf;
}
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
Returns:
outbuf

Definition at line 258 of file term.c.

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

Referenced by cli_prompt().

{
   int attr = 0;

   if (!check_colors_allowed(fgcolor)) {
      *outbuf = '\0';
      return outbuf;
   }

   check_fgcolor(&fgcolor, &attr);
   check_bgcolor(&bgcolor);

   if (ast_opt_force_black_background) {
      snprintf(outbuf, maxout, "%c[%d;%d;%dm", ESC, attr, fgcolor, COLOR_BLACK + 10);
   } else if (bgcolor) {
      snprintf(outbuf, maxout, "%c[%d;%d;%dm", ESC, attr, fgcolor, bgcolor + 10);
   } else {
      snprintf(outbuf, maxout, "%c[%d;%dm", ESC, attr, fgcolor);
   }

   return outbuf;
}
char* term_end ( void  )

Definition at line 358 of file term.c.

References enddata.

Referenced by consolehandler(), and main().

{
   return enddata;
}
void term_filter_escapes ( char *  line)

Definition at line 331 of file term.c.

References ESC, and len().

Referenced by ast_log().

{
   int i;
   int len = strlen(line);

   for (i = 0; i < len; i++) {
      if (line[i] != ESC)
         continue;
      if ((i < (len - 2)) &&
          (line[i + 1] == 0x5B)) {
         switch (line[i + 2]) {
         case 0x30:
         case 0x31:
         case 0x33:
            continue;
         }
      }
      /* replace ESC with a space */
      line[i] = ' ';
   }
}
char* term_prep ( void  )

Definition at line 353 of file term.c.

References prepdata.

{
   return prepdata;
}
char* term_prompt ( char *  outbuf,
const char *  inbuf,
int  maxout 
)

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

{
   if (!vt100compat) {
      ast_copy_string(outbuf, inbuf, maxout);
      return outbuf;
   }
   if (ast_opt_force_black_background) {
      snprintf(outbuf, maxout, "%c[%d;%dm%c%c[%d;%dm%s",
         ESC, COLOR_BLUE, COLOR_BLACK + 10,
         inbuf[0],
         ESC, COLOR_WHITE, COLOR_BLACK + 10,
         inbuf + 1);
   } else if (ast_opt_light_background) {
      snprintf(outbuf, maxout, "%c[%d;0m%c%c[%d;0m%s",
         ESC, COLOR_BLUE,
         inbuf[0],
         ESC, COLOR_BLACK,
         inbuf + 1);
   } else {
      snprintf(outbuf, maxout, "%c[%d;%d;0m%c%c[%d;%d;0m%s",
         ESC, ATTR_BRIGHT, COLOR_BLUE,
         inbuf[0],
         ESC, 0, COLOR_WHITE,
         inbuf + 1);
   }
   return outbuf;
}
char* term_quit ( void  )

Definition at line 363 of file term.c.

References quitdata.

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

{
   return quitdata;
}
char* term_strip ( char *  outbuf,
char *  inbuf,
int  maxout 
)

Definition at line 281 of file term.c.

References ESC, and inbuf().

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

{
   char *outbuf_ptr = outbuf, *inbuf_ptr = inbuf;

   while (outbuf_ptr < outbuf + maxout) {
      switch (*inbuf_ptr) {
         case ESC:
            while (*inbuf_ptr && (*inbuf_ptr != 'm'))
               inbuf_ptr++;
            break;
         default:
            *outbuf_ptr = *inbuf_ptr;
            outbuf_ptr++;
      }
      if (! *inbuf_ptr)
         break;
      inbuf_ptr++;
   }
   return outbuf;
}

Variable Documentation

char enddata[80] = "" [static]

Definition at line 43 of file term.c.

Referenced by term_end().

char prepdata[80] = "" [static]

Definition at line 42 of file term.c.

Referenced by term_prep().

char quitdata[80] = "" [static]

Definition at line 44 of file term.c.

Referenced by term_quit().

const char* termpath[] [static]

Definition at line 46 of file term.c.

int vt100compat [static]

Definition at line 40 of file term.c.