#include <stdio.h> #include <string.h> #include <ctype.h> #include "externs.h" #define COLOR_CHAR '|' typedef struct color_def { char ch; char code; } colortype; colortype colors[] = { { 'n', 0 }, /* NORMAL */ { '+', 1 }, /* BRIGHT */ { 'b', 5 }, /* BLINK */ { 'r', 7 }, /* REVERSE */ { 'N', 30 }, /* BLACK */ { 'R', 31 }, /* RED */ { 'G', 32 }, /* GREEN */ { 'Y', 33 }, /* YELLOW */ { 'B', 34 }, /* BLUE */ { 'M', 35 }, /* MAGENTA */ { 'C', 36 }, /* CYAN */ { 'W', 37 }, /* WHITE */ { '0', 40 }, /* BLACKB */ { '1', 41 }, /* REDB */ { '2', 42 }, /* GREENB */ { '3', 43 }, /* YELLOWB */ { '4', 44 }, /* BLUEB */ { '5', 45 }, /* MAGENTAB */ { '6', 46 }, /* CYANB */ { '7', 47 }, /* WHITEB */ { '\0', -1} }; static char get_color(char ch) { colortype *c; for(c = colors;c->ch;c++) if(c->ch == ch) return(c->code); return(-1); } static char *translate_color(char *str, bool add) { char buffer[4096]; char newstr[4096]; char *p; int c; strcpy(newstr, str); if(add) strcpy(buffer, "["); else strcpy(buffer, ""); for(p = newstr;*p;p++) { if((c = get_color(*p)) != -1) { if(add) { if(!c) sprintf(buffer, "%s;37;%d", buffer, c); else sprintf(buffer, "%s;%d", buffer, c); } } else { sprintf(buffer, "%c%s%c", COLOR_CHAR, str, COLOR_CHAR); return(stack_string_alloc(buffer, 0)); } } if(add) sprintf(buffer, "%sm", buffer); return(stack_string_alloc(buffer, 0)); } static bool is_color(char *str) { char buffer[BUFFER_LEN]; char *tmpstr, *p; strcpy(buffer, str); tmpstr = buffer; if(!(p = strchr(tmpstr, COLOR_CHAR))) return(0); if(p == tmpstr) return(0); *p = '\0'; for(p = tmpstr;*p;p++) if(get_color(*p) == -1) return(0); return(1); } static char *addcolor(char **strptr, bool flag) { char *p = parse_up(strptr, COLOR_CHAR); return(translate_color(p, flag)); } char *color(char *str, bool flag) { char *newstr = NULL, *p = NULL, *k = NULL; char buffer[BUFFER_LEN], buffer2[BUFFER_LEN]; strcpy(buffer, ""); strcpy(buffer2, str); newstr = buffer2; k = newstr; for(p = strchr(newstr, COLOR_CHAR);p;p = strchr(p, COLOR_CHAR)) { if(is_color(p+1)) { *p++ = '\0'; sprintf(buffer, "%s%s%s", buffer, k, addcolor(&p, flag)); k = p; } else p++; } sprintf(buffer, "%s%s", buffer, k); return(stack_string_alloc(buffer, 0)); } int my_is_color(char *str) { char *p; if(!*str) return(0); for(p = str;*p;p++) if(get_color(*p) == -1) return(0); return(1); }