Merge slepp's sub-second expiration timer although hopefully it will never have to...
[asterisk/asterisk.git] / asterisk.c
index 449c1dd..d1b0dad 100755 (executable)
@@ -884,18 +884,40 @@ static char *cli_prompt(EditLine *el)
 {
        static char prompt[200];
        char *pfmt;
 {
        static char prompt[200];
        char *pfmt;
+       int color_used=0;
+       char term_code[20];
 
        if ((pfmt = getenv("ASTERISK_PROMPT"))) {
                char *t = pfmt, *p = prompt;
                memset(prompt, 0, sizeof(prompt));
                while (*t != '\0' && *p < sizeof(prompt)) {
                        if (*t == '%') {
 
        if ((pfmt = getenv("ASTERISK_PROMPT"))) {
                char *t = pfmt, *p = prompt;
                memset(prompt, 0, sizeof(prompt));
                while (*t != '\0' && *p < sizeof(prompt)) {
                        if (*t == '%') {
+                               char hostname[256];
+                               int i;
+                               struct timeval tv;
+                               struct tm tm;
+                               FILE *LOADAVG;
+                               int fgcolor = COLOR_WHITE, bgcolor = COLOR_BLACK;
+
                                t++;
                                switch (*t) {
                                t++;
                                switch (*t) {
-                                       char hostname[256];
-                                       struct timeval tv;
-                                       struct tm tm;
-                                       FILE *LOADAVG;
+                                       case 'C': /* color */
+                                               t++;
+                                               if (sscanf(t, "%d;%d%n", &fgcolor, &bgcolor, &i) == 2) {
+                                                       strncat(p, term_color_code(term_code, fgcolor, bgcolor, sizeof(term_code)),sizeof(prompt) - strlen(prompt));
+                                                       t += i - 1;
+                                               } else if (sscanf(t, "%d%n", &fgcolor, &i) == 1) {
+                                                       strncat(p, term_color_code(term_code, fgcolor, 0, sizeof(term_code)),sizeof(prompt) - strlen(prompt));
+                                                       t += i - 1;
+                                               }
+
+                                               /* If the color has been reset correctly, then there's no need to reset it later */
+                                               if ((fgcolor == COLOR_WHITE) && (bgcolor == COLOR_BLACK)) {
+                                                       color_used = 0;
+                                               } else {
+                                                       color_used = 1;
+                                               }
+                                               break;
                                        case 'd': /* date */
                                                memset(&tm, 0, sizeof(struct tm));
                                                gettimeofday(&tv, NULL);
                                        case 'd': /* date */
                                                memset(&tm, 0, sizeof(struct tm));
                                                gettimeofday(&tv, NULL);
@@ -910,6 +932,19 @@ static char *cli_prompt(EditLine *el)
                                                        strncat(p, "localhost", sizeof(prompt) - strlen(prompt));
                                                }
                                                break;
                                                        strncat(p, "localhost", sizeof(prompt) - strlen(prompt));
                                                }
                                                break;
+                                       case 'H': /* short hostname */
+                                               if (!gethostname(hostname, sizeof(hostname) - 1)) {
+                                                       for (i=0;i<sizeof(hostname);i++) {
+                                                               if (hostname[i] == '.') {
+                                                                       hostname[i] = '\0';
+                                                                       break;
+                                                               }
+                                                       }
+                                                       strncat(p, hostname, sizeof(prompt) - strlen(prompt));
+                                               } else {
+                                                       strncat(p, "localhost", sizeof(prompt) - strlen(prompt));
+                                               }
+                                               break;
 #ifdef linux
                                        case 'l': /* load avg */
                                                t++;
 #ifdef linux
                                        case 'l': /* load avg */
                                                t++;
@@ -971,6 +1006,15 @@ static char *cli_prompt(EditLine *el)
                                t++;
                        }
                }
                                t++;
                        }
                }
+               if (color_used) {
+                       /* Force colors back to normal at end */
+                       term_color_code(term_code, COLOR_WHITE, COLOR_BLACK, sizeof(term_code));
+                       if (strlen(term_code) > sizeof(prompt) - strlen(prompt)) {
+                               strncat(prompt + sizeof(prompt) - strlen(term_code) - 1, term_code, strlen(term_code));
+                       } else {
+                               strncat(p, term_code, sizeof(term_code));
+                       }
+               }
        } else if (remotehostname)
                snprintf(prompt, sizeof(prompt), ASTERISK_PROMPT2, remotehostname);
        else
        } else if (remotehostname)
                snprintf(prompt, sizeof(prompt), ASTERISK_PROMPT2, remotehostname);
        else
@@ -1667,14 +1711,13 @@ int main(int argc, char *argv[])
                        } else {
                                if (option_remote)
                                        ast_cli(STDOUT_FILENO, "\nUse EXIT or QUIT to exit the asterisk console\n");
                        } else {
                                if (option_remote)
                                        ast_cli(STDOUT_FILENO, "\nUse EXIT or QUIT to exit the asterisk console\n");
-                               else
-                                       ast_cli(STDOUT_FILENO, "\nUse STOP NOW to shutdown Asterisk\n");
                        }
                }
 
        } else {
                /* Do nothing */
                        }
                }
 
        } else {
                /* Do nothing */
-               poll(NULL,0, -1);
+               for(;;) 
+                       poll(NULL,0, -1);
        }
        return 0;
 }
        }
        return 0;
 }
@@ -1702,8 +1745,8 @@ int gethostbyname_r (const char *name,
 
   int hsave;
   struct hostent *ph;
 
   int hsave;
   struct hostent *ph;
-  static pthread_mutex_t __mutex = PTHREAD_MUTEX_INITIALIZER;
-  pthread_mutex_lock(&__mutex); /* begin critical area */
+  static ast_mutex_t __mutex = AST_MUTEX_INITIALIZER;
+  ast_mutex_lock(&__mutex); /* begin critical area */
   hsave = h_errno;
 
   ph = gethostbyname(name);
   hsave = h_errno;
 
   ph = gethostbyname(name);
@@ -1789,9 +1832,9 @@ int gethostbyname_r (const char *name,
   }
   h_errno = hsave;  /* restore h_errno */
 
   }
   h_errno = hsave;  /* restore h_errno */
 
-  pthread_mutex_unlock(&__mutex); /* end critical area */
+  ast_mutex_unlock(&__mutex); /* end critical area */
 
 
-  return (*result == NULL);
+  return (*result != NULL);
 
 }
 
 
 }