[Asterisk-code-review] main: Slight refactor for in main. Improve color situation. (asterisk[11])
Walter Doekes
asteriskteam at digium.com
Mon Nov 23 11:35:55 CST 2015
Walter Doekes has uploaded a new change for review.
https://gerrit.asterisk.org/1698
Change subject: main: Slight refactor for in main. Improve color situation.
......................................................................
main: Slight refactor for in 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.
- Certain unused term_* functions are removed. (This will have to be
reverted for non-trunk.)
- 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.
ASTERISK-25585 #close
Change-Id: Ib641a0964c59ef9fe6f59efa8ccb481a9580c52f
---
M include/asterisk/term.h
M main/asterisk.c
M main/term.c
3 files changed, 81 insertions(+), 105 deletions(-)
git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/98/1698/1
diff --git a/include/asterisk/term.h b/include/asterisk/term.h
index bbdc753..a7d6db5 100644
--- a/include/asterisk/term.h
+++ b/include/asterisk/term.h
@@ -96,10 +96,6 @@
void term_filter_escapes(char *line);
-char *term_prompt(char *outbuf, const char *inbuf, int maxout);
-
-char *term_prep(void);
-
char *term_end(void);
char *term_quit(void);
diff --git a/main/asterisk.c b/main/asterisk.c
index 45b5b90..b19b469 100644
--- a/main/asterisk.c
+++ b/main/asterisk.c
@@ -243,9 +243,12 @@
char ast_defaultlanguage[MAX_LANGUAGE] = DEFAULT_LANGUAGE;
-static int ast_el_add_history(char *);
-static int ast_el_read_history(char *);
-static int ast_el_write_history(char *);
+static int ast_el_add_history(const char *);
+static int ast_el_read_history(const 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);
struct _cfg_paths {
char config_dir[PATH_MAX];
@@ -1813,13 +1816,7 @@
}
if (ast_opt_console || (ast_opt_remote && !ast_opt_exec)) {
- char filename[80] = "";
- if (getenv("HOME")) {
- snprintf(filename, sizeof(filename), "%s/.asterisk_history", getenv("HOME"));
- }
- if (!ast_strlen_zero(filename)) {
- ast_el_write_history(filename);
- }
+ ast_el_write_default_histfile();
if (consolethread == AST_PTHREADT_NULL || consolethread == pthread_self()) {
/* Only end if we are the consolethread, otherwise there's a race with that thread. */
if (el != NULL) {
@@ -2058,7 +2055,7 @@
}
}
-static int ast_all_zeros(char *s)
+static int ast_all_zeros(const char *s)
{
while (*s) {
if (*s > 32)
@@ -2069,7 +2066,7 @@
}
/* 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());
fflush(stdout);
@@ -2087,7 +2084,7 @@
ast_cli_command(STDOUT_FILENO, s);
}
-static int remoteconsolehandler(char *s)
+static int remoteconsolehandler(const char *s)
{
int ret = 0;
@@ -3017,7 +3014,7 @@
#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;
@@ -3028,7 +3025,7 @@
return (history(el_hist, &ev, H_ENTER, ast_strip(ast_strdupa(buf))));
}
-static int ast_el_write_history(char *filename)
+static int ast_el_write_history(const char *filename)
{
HistEvent ev;
@@ -3038,7 +3035,7 @@
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;
@@ -3049,11 +3046,32 @@
return history(el_hist, &ev, H_LOAD, filename);
}
+static void ast_el_read_default_histfile(void)
+{
+ char histfile[80] = "";
+ if (getenv("HOME")) {
+ snprintf(histfile, sizeof(histfile), "%s/.asterisk_history", getenv("HOME"));
+ }
+ if (!ast_strlen_zero(histfile)) {
+ ast_el_read_history(histfile);
+ }
+}
+
+static void ast_el_write_default_histfile(void)
+{
+ char histfile[80] = "";
+ if (getenv("HOME")) {
+ snprintf(histfile, sizeof(histfile), "%s/.asterisk_history", getenv("HOME"));
+ }
+ if (!ast_strlen_zero(histfile)) {
+ ast_el_write_history(histfile);
+ }
+}
+
static void ast_remotecontrol(char *data)
{
char buf[256] = "";
int res;
- char filename[80] = "";
char *hostname;
char *cpid;
char *version;
@@ -3062,6 +3080,10 @@
char *ebuf;
int num = 0;
+
+ ast_term_init();
+ printf("%s", term_end());
+ fflush(stdout);
memset(&sig_flags, 0, sizeof(sig_flags));
signal(SIGINT, __remote_quit_handler);
@@ -3161,15 +3183,11 @@
ast_verbose("Connected to Asterisk %s currently running on %s (pid = %d)\n", version, hostname, pid);
remotehostname = hostname;
- if (getenv("HOME"))
- snprintf(filename, sizeof(filename), "%s/.asterisk_history", getenv("HOME"));
if (el_hist == NULL || el == NULL)
ast_el_initialize();
+ ast_el_read_default_histfile();
el_set(el, EL_GETCFN, ast_el_read_char);
-
- if (!ast_strlen_zero(filename))
- ast_el_read_history(filename);
for (;;) {
ebuf = (char *)el_gets(el, &num);
@@ -3637,22 +3655,16 @@
ast_cli_unregister_multiple(cli_asterisk, ARRAY_LEN(cli_asterisk));
}
+void asterisk_daemon(int isroot, const char *runuser, const char *rungroup);
+
int main(int argc, char *argv[])
{
int c;
- char filename[80] = "";
- char hostname[MAXHOSTNAMELEN] = "";
- char tmp[80];
char * xarg = NULL;
int x;
- FILE *f;
- sigset_t sigs;
- int num;
int isroot = 1, rundir_exists = 0;
- char *buf;
const char *runuser = NULL, *rungroup = NULL;
char *remotesock = NULL;
- int moduleresult; /*!< Result from the module load subsystem */
struct rlimit l;
/* Remember original args for restart */
@@ -3671,12 +3683,8 @@
if (argv[0] && (strstr(argv[0], "rasterisk")) != NULL) {
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();
- if (getenv("HOME"))
- snprintf(filename, sizeof(filename), "%s/.asterisk_history", getenv("HOME"));
/* Check for options */
while ((c = getopt(argc, argv, "BC:cde:FfG:ghIiL:M:mnpqRrs:TtU:VvWXx:")) != -1) {
/*!\note Please keep the ordering here to alphabetical, capital letters
@@ -4017,6 +4025,10 @@
quit_handler(0, SHUTDOWN_FAST, 0);
exit(0);
}
+ ast_term_init();
+ printf("%s", term_end());
+ fflush(stdout);
+
print_intro_message(runuser, rungroup);
printf("%s", term_quit());
ast_remotecontrol(NULL);
@@ -4032,6 +4044,20 @@
printf("%s", term_quit());
exit(1);
}
+
+ /* Not a remote console? Start the daemon. */
+ asterisk_daemon(isroot, runuser, rungroup);
+ return 0;
+}
+
+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 */
+ char tmp[80];
/* 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
@@ -4126,9 +4152,7 @@
if (ast_opt_console) {
if (el_hist == NULL || el == NULL)
ast_el_initialize();
-
- if (!ast_strlen_zero(filename))
- ast_el_read_history(filename);
+ ast_el_read_default_histfile();
}
ast_ulaw_init();
@@ -4236,7 +4260,7 @@
/* initialize the data retrieval API */
if (ast_data_init()) {
- printf ("Failed: ast_data_init\n%s", term_quit());
+ printf("Failed: ast_data_init\n%s", term_quit());
exit(1);
}
@@ -4379,6 +4403,11 @@
/* Console stuff now... */
/* Register our quit function */
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);
@@ -4396,30 +4425,17 @@
buf = (char *) el_gets(el, &num);
if (!buf && write(1, "", 1) < 0)
- goto lostterm;
+ return; /* quit */
if (buf) {
if (buf[strlen(buf)-1] == '\n')
buf[strlen(buf)-1] = '\0';
- consolehandler((char *)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;
+ consolehandler(buf);
}
}
}
+ /* Stall until a quit signal is given */
monitor_sig_flags(NULL);
-
-lostterm:
- return 0;
}
diff --git a/main/term.c b/main/term.c
index 132b7fa..86bce55 100644
--- a/main/term.c
+++ b/main/term.c
@@ -43,7 +43,6 @@
static int vt100compat;
-static char prepdata[80] = "";
static char enddata[80] = "";
static char quitdata[80] = "";
@@ -165,18 +164,13 @@
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;
}
@@ -208,9 +202,12 @@
}
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;
}
@@ -234,16 +231,16 @@
}
}
-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;
}
@@ -265,7 +262,7 @@
{
int attr = 0;
- if (!check_colors_allowed(fgcolor)) {
+ if (!check_colors_allowed()) {
*outbuf = '\0';
return outbuf;
}
@@ -306,34 +303,6 @@
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;%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;
-}
-
/* filter escape sequences */
void term_filter_escapes(char *line)
{
@@ -355,11 +324,6 @@
/* replace ESC with a space */
line[i] = ' ';
}
-}
-
-char *term_prep(void)
-{
- return prepdata;
}
char *term_end(void)
--
To view, visit https://gerrit.asterisk.org/1698
To unsubscribe, visit https://gerrit.asterisk.org/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib641a0964c59ef9fe6f59efa8ccb481a9580c52f
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: 11
Gerrit-Owner: Walter Doekes <walter+asterisk at wjd.nu>
More information about the asterisk-code-review
mailing list