[Asterisk-cvs] asterisk asterisk.c,1.78,1.79 term.c,1.3,1.4

markster at lists.digium.com markster at lists.digium.com
Sun May 2 15:05:32 CDT 2004


Update of /usr/cvsroot/asterisk
In directory mongoose.digium.com:/tmp/cvs-serv16429

Modified Files:
	asterisk.c term.c 
Log Message:
Merge Tilghman's color patches for the asterisk prompt (bug #1535)


Index: asterisk.c
===================================================================
RCS file: /usr/cvsroot/asterisk/asterisk.c,v
retrieving revision 1.78
retrieving revision 1.79
diff -u -d -r1.78 -r1.79
--- asterisk.c	1 May 2004 03:14:02 -0000	1.78
+++ asterisk.c	2 May 2004 19:13:16 -0000	1.79
@@ -884,18 +884,41 @@
 {
 	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 == '%') {
+				char hostname[256];
+				int i;
+				struct timeval tv;
+				struct tm tm;
+				time_t curtime;
+				FILE *LOADAVG;
+				int fgcolor = COLOR_WHITE, bgcolor = COLOR_BLACK;
+
 				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);
@@ -910,6 +933,19 @@
 							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++;
@@ -969,6 +1005,15 @@
 				*p = *t;
 				p++;
 				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)

Index: term.c
===================================================================
RCS file: /usr/cvsroot/asterisk/term.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- term.c	22 Oct 2003 03:04:45 -0000	1.3
+++ term.c	2 May 2004 19:13:16 -0000	1.4
@@ -96,6 +96,66 @@
 	return outbuf;
 }
 
+char *term_color_code(char *outbuf, int fgcolor, int bgcolor, int maxout)
+{
+	int attr=0;
+	char tmp[40];
+	if ((!vt100compat) || (!fgcolor && !bgcolor)) {
+		*outbuf = '\0';
+		return outbuf;
+	}
+	if ((fgcolor & 128) && (bgcolor & 128)) {
+		/* Can't both be highlighted */
+		*outbuf = '\0';
+		return outbuf;
+	}
+	if (!bgcolor)
+		bgcolor = COLOR_BLACK;
+
+	if (bgcolor) {
+		bgcolor &= ~128;
+		bgcolor += 10;
+	}
+	if (fgcolor & 128) {
+		attr = ATTR_BRIGHT;
+		fgcolor &= ~128;
+	}
+	if (fgcolor && bgcolor) {
+		snprintf(tmp, sizeof(tmp), "%d;%d", fgcolor, bgcolor);
+	} else if (bgcolor) {
+		snprintf(tmp, sizeof(tmp), "%d", bgcolor);
+	} else if (fgcolor) {
+		snprintf(tmp, sizeof(tmp), "%d", fgcolor);
+	}
+	if (attr) {
+		snprintf(outbuf, maxout, "%c[%d;%sm", ESC, attr, tmp);
+	} else {
+		snprintf(outbuf, maxout, "%c[%sm", ESC, tmp);
+	}
+	return outbuf;
+}
+
+char *term_strip(char *outbuf, char *inbuf, int maxout)
+{
+	char *outbuf_ptr = outbuf, *inbuf_ptr = inbuf;
+
+	while (outbuf_ptr < outbuf + maxout) {
+		switch (*inbuf_ptr) {
+			case ESC:
+				while (*inbuf_ptr && (*inbuf_ptr != 'm'))
+					inbuf_ptr++;
+				break;
+			default:
+				*outbuf_ptr = *inbuf_ptr;
+				outbuf_ptr++;
+		}
+		if (! *inbuf_ptr)
+			break;
+		inbuf_ptr++;
+	}
+	return outbuf;
+}
+
 char *term_prompt(char *outbuf, const char *inbuf, int maxout)
 {
 	if (!vt100compat) {




More information about the svn-commits mailing list