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

@@ -67,8 +67,8 @@ extern "C" {
#define COLORIZE_FMT "%s%s%s" #define COLORIZE_FMT "%s%s%s"
#define COLORIZE(fg, bg, str) ast_term_color(fg,bg),str,ast_term_reset() #define COLORIZE(fg, bg, str) ast_term_color(fg,bg),str,ast_term_reset()
/*! \brief Maximum number of characters needed for a color escape sequence, /*! \brief Maximum number of characters needed for a color escape sequence,
* plus a null char */ * and another one for a trailing reset, plus a null char */
#define AST_TERM_MAX_ESCAPE_CHARS 12 #define AST_TERM_MAX_ESCAPE_CHARS 23
#define AST_TERM_MAX_ROTATING_BUFFERS 15 #define AST_TERM_MAX_ROTATING_BUFFERS 15
/*! \brief Colorize a specified string by adding terminal color codes /*! \brief Colorize a specified string by adding terminal color codes
@@ -137,10 +137,6 @@ char *term_strip(char *outbuf, const char *inbuf, int maxout);
void term_filter_escapes(char *line); void term_filter_escapes(char *line);
char *term_prompt(char *outbuf, const char *inbuf, int maxout);
const char *term_prep(void);
const char *term_end(void); const char *term_end(void);
const char *term_quit(void); const char *term_quit(void);

View File

@@ -373,9 +373,14 @@ struct console consoles[AST_MAX_CONNECTS];
char ast_defaultlanguage[MAX_LANGUAGE] = DEFAULT_LANGUAGE; char ast_defaultlanguage[MAX_LANGUAGE] = DEFAULT_LANGUAGE;
static int ast_el_add_history(char *); static int ast_el_add_history(const char *);
static int ast_el_read_history(char *); static int ast_el_read_history(const char *);
static int ast_el_write_history(char *); static int ast_el_write_history(const char *);
static void ast_el_read_default_histfile(void);
static void ast_el_write_default_histfile(void);
static void asterisk_daemon(int isroot, const char *runuser, const char *rungroup);
struct _cfg_paths { struct _cfg_paths {
char config_dir[PATH_MAX]; char config_dir[PATH_MAX];
@@ -1965,13 +1970,7 @@ static void really_quit(int num, shutdown_nice_t niceness, int restart)
} }
if (ast_opt_console || (ast_opt_remote && !ast_opt_exec)) { if (ast_opt_console || (ast_opt_remote && !ast_opt_exec)) {
char filename[80] = ""; ast_el_write_default_histfile();
if (getenv("HOME")) {
snprintf(filename, sizeof(filename), "%s/.asterisk_history", getenv("HOME"));
}
if (!ast_strlen_zero(filename)) {
ast_el_write_history(filename);
}
if (consolethread == AST_PTHREADT_NULL || consolethread == pthread_self()) { if (consolethread == AST_PTHREADT_NULL || consolethread == pthread_self()) {
/* Only end if we are the consolethread, otherwise there's a race with that thread. */ /* Only end if we are the consolethread, otherwise there's a race with that thread. */
if (el != NULL) { if (el != NULL) {
@@ -2202,7 +2201,7 @@ static void console_verboser(const char *s)
} }
} }
static int ast_all_zeros(char *s) static int ast_all_zeros(const char *s)
{ {
while (*s) { while (*s) {
if (*s > 32) if (*s > 32)
@@ -2213,7 +2212,7 @@ static int ast_all_zeros(char *s)
} }
/* This is the main console CLI command handler. Run by the main() thread. */ /* This is the main console CLI command handler. Run by the main() thread. */
static void consolehandler(char *s) static void consolehandler(const char *s)
{ {
printf("%s", term_end()); printf("%s", term_end());
fflush(stdout); fflush(stdout);
@@ -2231,7 +2230,7 @@ static void consolehandler(char *s)
ast_cli_command(STDOUT_FILENO, s); ast_cli_command(STDOUT_FILENO, s);
} }
static int remoteconsolehandler(char *s) static int remoteconsolehandler(const char *s)
{ {
int ret = 0; int ret = 0;
@@ -3147,7 +3146,7 @@ static int ast_el_initialize(void)
#define MAX_HISTORY_COMMAND_LENGTH 256 #define MAX_HISTORY_COMMAND_LENGTH 256
static int ast_el_add_history(char *buf) static int ast_el_add_history(const char *buf)
{ {
HistEvent ev; HistEvent ev;
char *stripped_buf; char *stripped_buf;
@@ -3169,7 +3168,7 @@ static int ast_el_add_history(char *buf)
return history(el_hist, &ev, H_ENTER, stripped_buf); return history(el_hist, &ev, H_ENTER, stripped_buf);
} }
static int ast_el_write_history(char *filename) static int ast_el_write_history(const char *filename)
{ {
HistEvent ev; HistEvent ev;
@@ -3179,7 +3178,7 @@ static int ast_el_write_history(char *filename)
return (history(el_hist, &ev, H_SAVE, filename)); return (history(el_hist, &ev, H_SAVE, filename));
} }
static int ast_el_read_history(char *filename) static int ast_el_read_history(const char *filename)
{ {
HistEvent ev; HistEvent ev;
@@ -3190,11 +3189,32 @@ static int ast_el_read_history(char *filename)
return history(el_hist, &ev, H_LOAD, filename); return history(el_hist, &ev, H_LOAD, filename);
} }
static void ast_el_read_default_histfile(void)
{
char histfile[80] = "";
const char *home = getenv("HOME");
if (!ast_strlen_zero(home)) {
snprintf(histfile, sizeof(histfile), "%s/.asterisk_history", home);
ast_el_read_history(histfile);
}
}
static void ast_el_write_default_histfile(void)
{
char histfile[80] = "";
const char *home = getenv("HOME");
if (!ast_strlen_zero(home)) {
snprintf(histfile, sizeof(histfile), "%s/.asterisk_history", home);
ast_el_write_history(histfile);
}
}
static void ast_remotecontrol(char *data) static void ast_remotecontrol(char *data)
{ {
char buf[256] = ""; char buf[256] = "";
int res; int res;
char filename[80] = "";
char *hostname; char *hostname;
char *cpid; char *cpid;
char *version; char *version;
@@ -3204,6 +3224,10 @@ static void ast_remotecontrol(char *data)
char *ebuf; char *ebuf;
int num = 0; int num = 0;
ast_term_init();
printf("%s", term_end());
fflush(stdout);
memset(&sig_flags, 0, sizeof(sig_flags)); memset(&sig_flags, 0, sizeof(sig_flags));
signal(SIGINT, __remote_quit_handler); signal(SIGINT, __remote_quit_handler);
signal(SIGTERM, __remote_quit_handler); signal(SIGTERM, __remote_quit_handler);
@@ -3302,16 +3326,12 @@ static void ast_remotecontrol(char *data)
ast_verbose("Connected to Asterisk %s currently running on %s (pid = %d)\n", version, hostname, pid); ast_verbose("Connected to Asterisk %s currently running on %s (pid = %d)\n", version, hostname, pid);
remotehostname = hostname; remotehostname = hostname;
if (getenv("HOME"))
snprintf(filename, sizeof(filename), "%s/.asterisk_history", getenv("HOME"));
if (el_hist == NULL || el == NULL) if (el_hist == NULL || el == NULL)
ast_el_initialize(); ast_el_initialize();
ast_el_read_default_histfile();
el_set(el, EL_GETCFN, ast_el_read_char); el_set(el, EL_GETCFN, ast_el_read_char);
if (!ast_strlen_zero(filename))
ast_el_read_history(filename);
for (;;) { for (;;) {
ebuf = (char *)el_gets(el, &num); ebuf = (char *)el_gets(el, &num);
@@ -3780,17 +3800,10 @@ static void main_atexit(void)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int c; int c;
char filename[80] = "";
char hostname[MAXHOSTNAMELEN] = "";
char * xarg = NULL; char * xarg = NULL;
int x; int x;
FILE *f;
sigset_t sigs;
int num;
int isroot = 1, rundir_exists = 0; int isroot = 1, rundir_exists = 0;
char *buf;
const char *runuser = NULL, *rungroup = NULL; const char *runuser = NULL, *rungroup = NULL;
int moduleresult; /*!< Result from the module load subsystem */
struct rlimit l; struct rlimit l;
static const char *getopt_settings = "BC:cde:FfG:ghIiL:M:mnpqRrs:TtU:VvWXx:"; static const char *getopt_settings = "BC:cde:FfG:ghIiL:M:mnpqRrs:TtU:VvWXx:";
@@ -3810,13 +3823,8 @@ int main(int argc, char *argv[])
if (argv[0] && (strstr(argv[0], "rasterisk")) != NULL) { if (argv[0] && (strstr(argv[0], "rasterisk")) != NULL) {
ast_set_flag(&ast_options, AST_OPT_FLAG_NO_FORK | AST_OPT_FLAG_REMOTE); ast_set_flag(&ast_options, AST_OPT_FLAG_NO_FORK | AST_OPT_FLAG_REMOTE);
} }
if (gethostname(hostname, sizeof(hostname)-1))
ast_copy_string(hostname, "<Unknown>", sizeof(hostname));
ast_mainpid = getpid(); ast_mainpid = getpid();
if (getenv("HOME"))
snprintf(filename, sizeof(filename), "%s/.asterisk_history", getenv("HOME"));
/* Set config file to default before checking arguments for override. */ /* Set config file to default before checking arguments for override. */
ast_copy_string(cfg_paths.config_file, DEFAULT_CONFIG_FILE, sizeof(cfg_paths.config_file)); ast_copy_string(cfg_paths.config_file, DEFAULT_CONFIG_FILE, sizeof(cfg_paths.config_file));
@@ -4197,6 +4205,10 @@ int main(int argc, char *argv[])
quit_handler(0, SHUTDOWN_FAST, 0); quit_handler(0, SHUTDOWN_FAST, 0);
exit(0); exit(0);
} }
ast_term_init();
printf("%s", term_end());
fflush(stdout);
print_intro_message(runuser, rungroup); print_intro_message(runuser, rungroup);
printf("%s", term_quit()); printf("%s", term_quit());
ast_remotecontrol(NULL); ast_remotecontrol(NULL);
@@ -4213,6 +4225,19 @@ int main(int argc, char *argv[])
exit(1); exit(1);
} }
/* Not a remote console? Start the daemon. */
asterisk_daemon(isroot, runuser, rungroup);
return 0;
}
static void asterisk_daemon(int isroot, const char *runuser, const char *rungroup)
{
FILE *f;
sigset_t sigs;
int num;
char *buf;
int moduleresult; /*!< Result from the module load subsystem */
/* This needs to remain as high up in the initial start up as possible. /* This needs to remain as high up in the initial start up as possible.
* daemon causes a fork to occur, which has all sorts of unintended * daemon causes a fork to occur, which has all sorts of unintended
* consequences for things that interact with threads. This call *must* * consequences for things that interact with threads. This call *must*
@@ -4309,9 +4334,7 @@ int main(int argc, char *argv[])
if (ast_opt_console) { if (ast_opt_console) {
if (el_hist == NULL || el == NULL) if (el_hist == NULL || el == NULL)
ast_el_initialize(); ast_el_initialize();
ast_el_read_default_histfile();
if (!ast_strlen_zero(filename))
ast_el_read_history(filename);
} }
ast_json_init(); ast_json_init();
@@ -4476,7 +4499,7 @@ int main(int argc, char *argv[])
/* initialize the data retrieval API */ /* initialize the data retrieval API */
if (ast_data_init()) { if (ast_data_init()) {
printf ("Failed: ast_data_init\n%s", term_quit()); printf("Failed: ast_data_init\n%s", term_quit());
exit(1); exit(1);
} }
@@ -4658,6 +4681,11 @@ int main(int argc, char *argv[])
/* Console stuff now... */ /* Console stuff now... */
/* Register our quit function */ /* Register our quit function */
char title[256]; char title[256];
char hostname[MAXHOSTNAMELEN] = "";
if (gethostname(hostname, sizeof(hostname) - 1)) {
ast_copy_string(hostname, "<Unknown>", sizeof(hostname));
}
ast_pthread_create_detached(&mon_sig_flags, NULL, monitor_sig_flags, NULL); ast_pthread_create_detached(&mon_sig_flags, NULL, monitor_sig_flags, NULL);
@@ -4675,30 +4703,17 @@ int main(int argc, char *argv[])
buf = (char *) el_gets(el, &num); buf = (char *) el_gets(el, &num);
if (!buf && write(1, "", 1) < 0) if (!buf && write(1, "", 1) < 0)
goto lostterm; return; /* quit */
if (buf) { if (buf) {
if (buf[strlen(buf)-1] == '\n') if (buf[strlen(buf)-1] == '\n')
buf[strlen(buf)-1] = '\0'; buf[strlen(buf)-1] = '\0';
consolehandler((char *)buf); consolehandler(buf);
} else if (ast_opt_remote && (write(STDOUT_FILENO, "\nUse EXIT or QUIT to exit the asterisk console\n",
strlen("\nUse EXIT or QUIT to exit the asterisk console\n")) < 0)) {
/* Whoa, stdout disappeared from under us... Make /dev/null's */
int fd;
fd = open("/dev/null", O_RDWR);
if (fd > -1) {
dup2(fd, STDOUT_FILENO);
dup2(fd, STDIN_FILENO);
} else
ast_log(LOG_WARNING, "Failed to open /dev/null to recover from dead console. Bad things will happen!\n");
break;
} }
} }
} }
/* Stall until a quit signal is given */
monitor_sig_flags(NULL); monitor_sig_flags(NULL);
lostterm:
return 0;
} }

View File

@@ -44,7 +44,6 @@ ASTERISK_REGISTER_FILE()
static int vt100compat; static int vt100compat;
static char prepdata[80] = "";
static char enddata[80] = ""; static char enddata[80] = "";
static char quitdata[80] = ""; static char quitdata[80] = "";
@@ -173,18 +172,13 @@ end:
if (vt100compat) { if (vt100compat) {
/* Make commands show up in nice colors */ /* Make commands show up in nice colors */
if (ast_opt_light_background) { if (ast_opt_light_background) {
snprintf(prepdata, sizeof(prepdata), "%c[%dm", ESC, COLOR_BROWN);
snprintf(enddata, sizeof(enddata), "%c[%dm", ESC, COLOR_BLACK); snprintf(enddata, sizeof(enddata), "%c[%dm", ESC, COLOR_BLACK);
snprintf(quitdata, sizeof(quitdata), "%c[0m", ESC);
} else if (ast_opt_force_black_background) { } 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(enddata, sizeof(enddata), "%c[%d;%d;%dm", ESC, ATTR_RESET, COLOR_WHITE, COLOR_BLACK + 10);
snprintf(quitdata, sizeof(quitdata), "%c[0m", ESC);
} else { } else {
snprintf(prepdata, sizeof(prepdata), "%c[%d;%dm", ESC, ATTR_BRIGHT, COLOR_BROWN); snprintf(enddata, sizeof(enddata), "%c[%dm", ESC, ATTR_RESET);
snprintf(enddata, sizeof(enddata), "%c[%d;%dm", ESC, ATTR_RESET, COLOR_WHITE);
snprintf(quitdata, sizeof(quitdata), "%c[0m", ESC);
} }
snprintf(quitdata, sizeof(quitdata), "%c[%dm", ESC, ATTR_RESET);
} }
return 0; 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) { 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 { } 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; 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 ast_term_color_code(struct ast_str **str, int fgcolor, int bgcolor)
{ {
int attr = 0; int attr = 0;
if (!check_colors_allowed(fgcolor)) { if (!check_colors_allowed()) {
return -1; return -1;
} }
@@ -273,7 +270,7 @@ char *term_color_code(char *outbuf, int fgcolor, int bgcolor, int maxout)
{ {
int attr = 0; int attr = 0;
if (!check_colors_allowed(fgcolor)) { if (!check_colors_allowed()) {
*outbuf = '\0'; *outbuf = '\0';
return outbuf; return outbuf;
} }
@@ -310,11 +307,7 @@ const char *ast_term_color(int fgcolor, int bgcolor)
const char *ast_term_reset(void) const char *ast_term_reset(void)
{ {
if (ast_opt_force_black_background) { return term_end();
return enddata;
} else {
return quitdata;
}
} }
char *term_strip(char *outbuf, const char *inbuf, int maxout) 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; 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 */ /* filter escape sequences */
void term_filter_escapes(char *line) 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) const char *term_end(void)
{ {
return enddata; return enddata;