Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:49:21 2009

## Asterisk developer's documentation

# term.c File Reference

Terminal Routines. More...

`#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.h"`
`#include "asterisk/term.h"`
`#include "asterisk/options.h"`
`#include "asterisk/lock.h"`
`#include "asterisk/utils.h"`

Include dependency graph for term.c:

Go to the source code of this file.

## Functions

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)
int | term_init (void)
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 = 0

## Detailed Description

Terminal Routines.

Definition in file term.c.

## Function Documentation

static short convshort | ( | char * | s | ) | `[static]`

Definition at line 59 of file term.c.

Referenced by term_init().

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

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

Definition at line 149 of file term.c.

References ATTR_BRIGHT, COLOR_BLACK, COLOR_WHITE, and ESC.

Referenced by __load_resource(), ast_frame_dump(), ast_log(), ast_register_application(), ast_register_translator(), ast_unregister_translator(), fix_header(), handle_show_application(), handle_show_function(), handle_zap_show_cadences(), load_modules(), main(), pbx_extension_helper(), and realtime_exec().

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

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

Definition at line 192 of file term.c.

References ATTR_BRIGHT, COLOR_BLACK, and ESC.

Referenced by cli_prompt().

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

char* term_end | ( | void |  | ) |

Definition at line 271 of file term.c.

Referenced by consolehandler(), and main().

    00272 {
    00273    return enddata;
    00274 }

int term_init | ( | void |  | ) |

Definition at line 74 of file term.c.

References ATTR_BRIGHT, ATTR_RESET, COLOR_BLACK, COLOR_BROWN, COLOR_WHITE, convshort(), ESC, option_console, option_nocolor, and option_nofork.

Referenced by main().

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

char* term_prep | ( | void |  | ) |

Definition at line 266 of file term.c.

    00267 {
    00268    return prepdata;
    00269 }

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

Definition at line 252 of file term.c.

References ATTR_BRIGHT, COLOR_BLACK, COLOR_BLUE, COLOR_WHITE, and ESC.

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

char* term_quit | ( | void |  | ) |

Definition at line 276 of file term.c.

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

    00277 {
    00278    return quitdata;
    00279 }

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

Definition at line 231 of file term.c.

References ESC.

Referenced by ast_log(), and ast_log_vsyslog().

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

## Variable Documentation

char enddata[80] = ""`[static]`

Definition at line 48 of file term.c.

char prepdata[80] = ""`[static]`

Definition at line 47 of file term.c.

char quitdata[80] = ""`[static]`

Definition at line 49 of file term.c.

const char* termpath[]`[static]`

Definition at line 51 of file term.c.

int vt100compat = 0`[static]`

Definition at line 45 of file term.c.