main: Slight refactor of main. Improve color situation.

Several issues are addressed here:
- main() is large, and half of it is only used if we're not rasterisk;
  fixed by spliting up the daemon part into a separate function.
- Call ast_term_init from rasterisk as well.
- Remove duplicate code reading/writing asterisk history file.
- Attempt to tackle background color issues and color changes that
  occur. Tested by starting asterisk -c until the colors stopped
  changing at odd locations.
- Remove unused term_prep() and term_prompt() functions.

ASTERISK-25585 #close

Change-Id: Ib641a0964c59ef9fe6f59efa8ccb481a9580c52f
This commit is contained in:
Walter Doekes
2015-11-25 20:29:55 +01:00
parent fb45130476
commit 03759c5587
3 changed files with 83 additions and 112 deletions

View File

@@ -44,7 +44,6 @@ ASTERISK_REGISTER_FILE()
static int vt100compat;
static char prepdata[80] = "";
static char enddata[80] = "";
static char quitdata[80] = "";
@@ -173,18 +172,13 @@ 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);
snprintf(enddata, sizeof(enddata), "%c[%dm", ESC, ATTR_RESET);
}
snprintf(quitdata, sizeof(quitdata), "%c[%dm", ESC, ATTR_RESET);
}
return 0;
}
@@ -216,9 +210,12 @@ char *term_color(char *outbuf, const char *inbuf, int fgcolor, int bgcolor, int
}
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);
if (!bgcolor) {
bgcolor = COLOR_BLACK;
}
snprintf(outbuf, maxout, "%c[%d;%d;%dm%s%s", ESC, attr, fgcolor, bgcolor + 10, inbuf, term_end());
} else {
snprintf(outbuf, maxout, "%c[%d;%dm%s%c[0m", ESC, attr, fgcolor, inbuf, ESC);
snprintf(outbuf, maxout, "%c[%d;%dm%s%s", ESC, attr, fgcolor, inbuf, term_end());
}
return outbuf;
}
@@ -242,16 +239,16 @@ static void check_bgcolor(int *bgcolor)
}
}
static int check_colors_allowed(int fgcolor)
static int check_colors_allowed(void)
{
return (!vt100compat || !fgcolor) ? 0 : 1;
return vt100compat;
}
int ast_term_color_code(struct ast_str **str, int fgcolor, int bgcolor)
{
int attr = 0;
if (!check_colors_allowed(fgcolor)) {
if (!check_colors_allowed()) {
return -1;
}
@@ -273,7 +270,7 @@ char *term_color_code(char *outbuf, int fgcolor, int bgcolor, int maxout)
{
int attr = 0;
if (!check_colors_allowed(fgcolor)) {
if (!check_colors_allowed()) {
*outbuf = '\0';
return outbuf;
}
@@ -310,11 +307,7 @@ const char *ast_term_color(int fgcolor, int bgcolor)
const char *ast_term_reset(void)
{
if (ast_opt_force_black_background) {
return enddata;
} else {
return quitdata;
}
return term_end();
}
char *term_strip(char *outbuf, const char *inbuf, int maxout)
@@ -339,34 +332,6 @@ char *term_strip(char *outbuf, const char *inbuf, int maxout)
return outbuf;
}
char *term_prompt(char *outbuf, const char *inbuf, int maxout)
{
if (!vt100compat) {
ast_copy_string(outbuf, inbuf, maxout);
return outbuf;
}
if (ast_opt_force_black_background) {
snprintf(outbuf, maxout, "%c[%d;%d;%dm%c%c[%d;%dm%s",
ESC, ATTR_BRIGHT, 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[0m%s",
ESC, COLOR_BLUE,
inbuf[0],
ESC,
inbuf + 1);
} else {
snprintf(outbuf, maxout, "%c[%d;%d;0m%c%c[0m%s",
ESC, ATTR_BRIGHT, COLOR_BLUE,
inbuf[0],
ESC,
inbuf + 1);
}
return outbuf;
}
/* filter escape sequences */
void term_filter_escapes(char *line)
{
@@ -390,11 +355,6 @@ void term_filter_escapes(char *line)
}
}
const char *term_prep(void)
{
return prepdata;
}
const char *term_end(void)
{
return enddata;