From 53f7c374cf436f17e8912c5cc73fbebd6c7f29e0 Mon Sep 17 00:00:00 2001 From: Kevin Easton Date: Fri, 19 Jun 2015 23:12:10 +1000 Subject: [PATCH] Convert strip_ansi() from unsigned char to char This required a few careful changes to ensure that the result does the same thing. Also included a few little cleanups in this function. --- include/modval.h | 2 +- include/screen.h | 6 +- source/input.c | 2 +- source/screen.c | 256 +++++++++++++++++++++++------------------------ 4 files changed, 129 insertions(+), 137 deletions(-) diff --git a/include/modval.h b/include/modval.h index ec8cdd7..cc9ff91 100644 --- a/include/modval.h +++ b/include/modval.h @@ -296,7 +296,7 @@ extern Function_ptr *global; #define xterm_settitle (*(void (*)(void))global[XTERM_SETTITLE]) #define add_wait_prompt (*(void (*)(char *, void (*)(char *, char *), char *, int , int ))global[ADD_WAIT_PROMPT]) #define skip_ctl_c_seq (*(char *(*)(const char *, int *, int *, int ))global[SKIP_CTL_C_SEQ]) -#define strip_ansi (*(unsigned char *(*)(const unsigned char *))global[STRIP_ANSI]) +#define strip_ansi (*(char *(*)(const char *))global[STRIP_ANSI]) #define create_new_screen ((Screen * (*)(void))global[CREATE_NEW_SCREEN]) #define create_additional_screen ((Window * (*)(void))global[CREATE_ADDITIONAL_SCREEN]) diff --git a/include/screen.h b/include/screen.h index 48fdfe0..c36dc43 100644 --- a/include/screen.h +++ b/include/screen.h @@ -59,12 +59,12 @@ Screen * BX_create_new_screen(void); void refresh_window_screen(Window *); #endif - u_char *BX_strip_ansi (const u_char *); - char *normalize_color (int, int, int, int); +char *BX_strip_ansi(const char *); +char *normalize_color(int, int, int, int); char *BX_skip_ctl_c_seq(const char *, int *, int *, int); char **BX_prepare_display(const char *, int, int *, int); int BX_output_with_count(const char *, int, int); -char *BX_skip_incoming_mirc (char *); +char *BX_skip_incoming_mirc(char *); /* Dont do any word-wrapping, just truncate each line at its place. */ #define PREPARE_NOWRAP 0x01 diff --git a/source/input.c b/source/input.c index 86f8564..69742af 100644 --- a/source/input.c +++ b/source/input.c @@ -243,7 +243,7 @@ extern void BX_update_input (int update) term_echo(last_input_screen->promptlist->echo); ptr_free = ptr; - ptr = (char *)strip_ansi(ptr); + ptr = strip_ansi(ptr); strcat(ptr, ALL_OFF_STR); /* Yes, we can do this */ if (free_it) new_free(&ptr_free); diff --git a/source/screen.c b/source/screen.c index 66316c9..b3295d5 100644 --- a/source/screen.c +++ b/source/screen.c @@ -2191,95 +2191,6 @@ char *BX_skip_ctl_c_seq(const char *start, int *lhs, int *rhs, int proper) return (char *)after; } -/* - * Used as a translation table when we cant display graphics characters - * or we have been told to do translation. A no-brainer, with little attempt - * at being smart. - * (JKJ: perhaps we should allow a user to /set this?) - */ -static u_char gcxlate[256] = { - '*', '*', '*', '*', '*', '*', '*', '*', - '#', '*', '#', '*', '*', '*', '*', '*', - '>', '<', '|', '!', '|', '$', '_', '|', - '^', 'v', '>', '<', '*', '=', '^', 'v', - ' ', '!', '"', '#', '$', '%', '&', '\'', - '(', ')', '*', '+', ',', '_', '.', '/', - '0', '1', '2', '3', '4', '5', '6', '7', - '8', '9', ':', ';', '<', '=', '>', '?', - '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', - 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', - 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', - 'Z', 'Y', 'X', '[', '\\', ']', '^', '_', - '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', - 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', - 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', - 'x', 'y', 'z', '{', '|', '}', '~', '?', - 'C', 'u', 'e', 'a', 'a', 'a', 'a', 'c', - 'e', 'e', 'e', 'i', 'i', 'i', 'A', 'A', - 'e', 'e', 'e', 'o', 'o', 'o', 'u', 'u', - 'y', 'O', 'U', 'C', '#', 'Y', 'P', 'f', - 'a', 'i', 'o', 'u', 'n', 'N', '^', '^', - '?', '<', '>', '2', '4', '!', '<', '>', - '#', '#', '#', '|', '|', '|', '|', '+', - '+', '+', '+', '|', '+', '+', '+', '+', - '+', '+', '+', '+', '-', '+', '+', '+', - '+', '+', '+', '+', '+', '=', '+', '+', - '+', '+', '+', '+', '+', '+', '+', '+', - '+', '+', '+', '#', '-', '|', '|', '-', - 'a', 'b', 'P', 'p', 'Z', 'o', 'u', 't', - '#', 'O', '0', 'O', '-', 'o', 'e', 'U', - '*', '+', '>', '<', '|', '|', '/', '=', - '*', '*', '*', '*', 'n', '2', '*', '*' -}; - -/* - * State 0 is "normal, printable character" - * State 1 is "nonprintable character" - * State 2 is "Escape character" (033) - * State 3 is "Color code" (003) - * State 4 is "Special highlight character" - * State 5 is "ROM character" (022) - * State 6 is a character that should never be printed - */ -static u_char ansi_state[256] = { -/* ^@ ^A ^B ^C ^D ^E ^F ^G */ - 6, 6, 4, 3, 6, 4, 4, 4, /* 000 */ -/* ^H ^I ^J ^K ^D ^M ^N ^O */ - 6, 4, 4, 6, 0, 6, 6, 4, /* 010 */ -/* ^P ^Q ^R ^S ^T ^U ^V ^W */ - 6, 6, 5, 4, 4, 6, 4, 6, /* 020 */ -/* ^X ^Y ^Z ^[ ^\ ^] ^^ ^_ */ - 6, 6, 6, 2, 6, 6, 6, 4, /* 030 */ - 0, 0, 0, 0, 0, 0, 0, 0, /* 040 */ - 0, 0, 0, 0, 0, 0, 0, 0, /* 050 */ - 0, 0, 0, 0, 0, 0, 0, 0, /* 060 */ - 0, 0, 0, 0, 0, 0, 0, 0, /* 070 */ - 0, 0, 0, 0, 0, 0, 0, 0, /* 100 */ - 0, 0, 0, 0, 0, 0, 0, 0, /* 110 */ - 0, 0, 0, 0, 0, 0, 0, 0, /* 120 */ - 0, 0, 0, 0, 0, 0, 0, 0, /* 130 */ - 0, 0, 0, 0, 0, 0, 0, 0, /* 140 */ - 0, 0, 0, 0, 0, 0, 0, 0, /* 150 */ - 0, 0, 0, 0, 0, 0, 0, 0, /* 160 */ - 0, 0, 0, 0, 0, 0, 0, 0, /* 170 */ - 1, 1, 1, 1, 1, 1, 1, 1, /* 200 */ - 1, 1, 1, 1, 1, 1, 1, 1, /* 210 */ - 1, 1, 1, 1, 1, 1, 1, 1, /* 220 */ - 1, 1, 1, 1, 1, 1, 1, 1, /* 230 */ - 1, 1, 1, 1, 1, 1, 1, 1, /* 240 */ - 1, 1, 1, 1, 1, 1, 1, 1, /* 250 */ - 1, 1, 1, 1, 1, 1, 1, 1, /* 260 */ - 1, 1, 1, 1, 1, 1, 1, 1, /* 270 */ - 1, 1, 1, 1, 1, 1, 1, 1, /* 300 */ - 1, 1, 1, 1, 1, 1, 1, 1, /* 310 */ - 1, 1, 1, 1, 1, 1, 1, 1, /* 320 */ - 1, 1, 1, 1, 1, 1, 1, 1, /* 330 */ - 1, 1, 1, 1, 1, 1, 1, 1, /* 340 */ - 1, 1, 1, 1, 1, 1, 1, 1, /* 350 */ - 1, 1, 1, 1, 1, 1, 1, 1, /* 360 */ - 1, 1, 1, 1, 1, 1, 1, 1 /* 370 */ -}; - /* * This started off as a general ansi parser, and it worked for stuff that * was going out to the display, but it couldnt deal properly with ansi codes, @@ -2295,56 +2206,140 @@ static u_char ansi_state[256] = { * These macros help keep 8 bit chars from sneaking into the output stream * where they might be stripped out. */ -#if 1 #define this_char() (eightbit ? *str : (*str) & 0x7f) #define next_char() (eightbit ? *str++ : (*str++) & 0x7f) #define put_back() (str--) -#else -#define this_char() (*str) -#define next_char() (*str++) -#define put_back() (str--) -#endif - -unsigned char *BX_strip_ansi (const unsigned char *str) +char *BX_strip_ansi(const char *str) { - u_char *output; - u_char chr, chr1; - int pos, maxpos; - int args[10], nargs, i; + /* + * Used as a translation table when we cant display graphics characters + * or we have been told to do translation. A no-brainer, with little attempt + * at being smart. + * (JKJ: perhaps we should allow a user to /set this?) + */ + const static char gcxlate[256] = { + '*', '*', '*', '*', '*', '*', '*', '*', + '#', '*', '#', '*', '*', '*', '*', '*', + '>', '<', '|', '!', '|', '$', '_', '|', + '^', 'v', '>', '<', '*', '=', '^', 'v', + ' ', '!', '"', '#', '$', '%', '&', '\'', + '(', ')', '*', '+', ',', '_', '.', '/', + '0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', ':', ';', '<', '=', '>', '?', + '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', + 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', + 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', + 'Z', 'Y', 'X', '[', '\\', ']', '^', '_', + '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', + 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', + 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', + 'x', 'y', 'z', '{', '|', '}', '~', '?', + 'C', 'u', 'e', 'a', 'a', 'a', 'a', 'c', + 'e', 'e', 'e', 'i', 'i', 'i', 'A', 'A', + 'e', 'e', 'e', 'o', 'o', 'o', 'u', 'u', + 'y', 'O', 'U', 'C', '#', 'Y', 'P', 'f', + 'a', 'i', 'o', 'u', 'n', 'N', '^', '^', + '?', '<', '>', '2', '4', '!', '<', '>', + '#', '#', '#', '|', '|', '|', '|', '+', + '+', '+', '+', '|', '+', '+', '+', '+', + '+', '+', '+', '+', '-', '+', '+', '+', + '+', '+', '+', '+', '+', '=', '+', '+', + '+', '+', '+', '+', '+', '+', '+', '+', + '+', '+', '+', '#', '-', '|', '|', '-', + 'a', 'b', 'P', 'p', 'Z', 'o', 'u', 't', + '#', 'O', '0', 'O', '-', 'o', 'e', 'U', + '*', '+', '>', '<', '|', '|', '/', '=', + '*', '*', '*', '*', 'n', '2', '*', '*' + }; + + /* + * State 0 is "normal, printable character" + * State 1 is "nonprintable character" + * State 2 is "Escape character" (033) + * State 3 is "Color code" (003) + * State 4 is "Special highlight character" + * State 5 is "ROM character" (022) + * State 6 is a character that should never be printed + */ + const static char ansi_state[256] = { + /* ^@ ^A ^B ^C ^D ^E ^F ^G */ + 6, 6, 4, 3, 6, 4, 4, 4, /* 000 */ + /* ^H ^I ^J ^K ^D ^M ^N ^O */ + 6, 4, 4, 6, 0, 6, 6, 4, /* 010 */ + /* ^P ^Q ^R ^S ^T ^U ^V ^W */ + 6, 6, 5, 4, 4, 6, 4, 6, /* 020 */ + /* ^X ^Y ^Z ^[ ^\ ^] ^^ ^_ */ + 6, 6, 6, 2, 6, 6, 6, 4, /* 030 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 040 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 050 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 060 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 070 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 100 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 110 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 120 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 130 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 140 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 150 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 160 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 170 */ + 1, 1, 1, 1, 1, 1, 1, 1, /* 200 */ + 1, 1, 1, 1, 1, 1, 1, 1, /* 210 */ + 1, 1, 1, 1, 1, 1, 1, 1, /* 220 */ + 1, 1, 1, 1, 1, 1, 1, 1, /* 230 */ + 1, 1, 1, 1, 1, 1, 1, 1, /* 240 */ + 1, 1, 1, 1, 1, 1, 1, 1, /* 250 */ + 1, 1, 1, 1, 1, 1, 1, 1, /* 260 */ + 1, 1, 1, 1, 1, 1, 1, 1, /* 270 */ + 1, 1, 1, 1, 1, 1, 1, 1, /* 300 */ + 1, 1, 1, 1, 1, 1, 1, 1, /* 310 */ + 1, 1, 1, 1, 1, 1, 1, 1, /* 320 */ + 1, 1, 1, 1, 1, 1, 1, 1, /* 330 */ + 1, 1, 1, 1, 1, 1, 1, 1, /* 340 */ + 1, 1, 1, 1, 1, 1, 1, 1, /* 350 */ + 1, 1, 1, 1, 1, 1, 1, 1, /* 360 */ + 1, 1, 1, 1, 1, 1, 1, 1 /* 370 */ + }; + + char *output; + char chr; + int pos, maxpos; + int args[10], nargs, i; int bold = 0; int underline = 0; int reverse = 0; int blink = 0; int alt_mode = 0; - int fg_color = 0; - int bg_color = 1; + int fg_color = 0; + int bg_color = 1; - int ansi = get_int_var(DISPLAY_ANSI_VAR); - int gcmode = get_int_var(DISPLAY_PC_CHARACTERS_VAR); - int n; - int eightbit = term_eight_bit(); + const int ansi = get_int_var(DISPLAY_ANSI_VAR); + const int gcmode = get_int_var(DISPLAY_PC_CHARACTERS_VAR); + const int eightbit = term_eight_bit(); + int n; /* * The output string has a few extra chars on the end just * in case you need to tack something else onto it. */ maxpos = strlen(str); - output = (u_char *)new_malloc(maxpos + 64); + output = new_malloc(maxpos + 64); pos = 0; while ((chr = next_char())) { - chr1 = *(str - 1); - if (pos > maxpos) - { - maxpos += 64; /* Extend 64 chars at a time */ - RESIZE(output, unsigned char, maxpos + 64); - } + /* chr1 is the same as chr, but without the top bit masked. */ + const char chr1 = *(str - 1); - switch (ansi_state[chr1]) - { + if (pos > maxpos) + { + maxpos += 64; /* Extend 64 chars at a time */ + RESIZE(output, char, maxpos + 64); + } + + switch (ansi_state[(unsigned char)chr1]) + { /* * State 0 is a normal, printable ascii character */ @@ -2364,14 +2359,14 @@ unsigned char *BX_strip_ansi (const unsigned char *str) * This is a very paranoid check to make sure that * the 8-bit escape code doesnt elude us. */ - if (chr == 27 + 128) + if (chr == '\x9b') { output[pos++] = REV_TOG; output[pos++] = '['; output[pos++] = REV_TOG; continue; } - if (ansi_state[chr] == 6) + if (ansi_state[(unsigned char)chr] == 6) my_gcmode = 1; if (strip_ansi_never_xlate) my_gcmode = 4; @@ -2383,7 +2378,7 @@ unsigned char *BX_strip_ansi (const unsigned char *str) */ case 5: { - output[pos++] = gcxlate[chr]; + output[pos++] = gcxlate[(unsigned char)chr]; break; } @@ -2404,7 +2399,7 @@ unsigned char *BX_strip_ansi (const unsigned char *str) if (termfeatures & TERM_CAN_GCHAR) output[pos++] = chr; else - output[pos++] = gcxlate[chr]; + output[pos++] = gcxlate[(unsigned char)chr]; break; } @@ -2418,7 +2413,7 @@ unsigned char *BX_strip_ansi (const unsigned char *str) else { output[pos++] = REV_TOG; - output[pos++] = gcxlate[chr]; + output[pos++] = gcxlate[(unsigned char)chr]; output[pos++] = REV_TOG; } break; @@ -2433,15 +2428,12 @@ unsigned char *BX_strip_ansi (const unsigned char *str) */ case 1: { - if (termfeatures & TERM_CAN_GCHAR) - output[pos++] = chr; - else if ((chr & 0x80) && eightbit) + if ((termfeatures & TERM_CAN_GCHAR) || (chr & 0x80)) output[pos++] = chr; else { output[pos++] = REV_TOG; - output[pos++] = - (chr | 0x40) & 0x7f; + output[pos++] = chr | 0x40; output[pos++] = REV_TOG; } break; @@ -2599,7 +2591,7 @@ unsigned char *BX_strip_ansi (const unsigned char *str) { n = 0; for (n = 0; - isdigit(this_char()); + isdigit((unsigned char)this_char()); next_char()) { n = n * 10 + (this_char() - '0'); @@ -2647,7 +2639,7 @@ unsigned char *BX_strip_ansi (const unsigned char *str) if (pos + args[0] > maxpos) { maxpos += args[0]; - RESIZE(output, u_char, maxpos + 64); + RESIZE(output, char, maxpos + 64); } while (args[0]-- > 0) output[pos++] = ND_SPACE; @@ -2760,7 +2752,7 @@ unsigned char *BX_strip_ansi (const unsigned char *str) } else if (args[i] >= 30 && args[i] <= 37) { - output[pos++] = 003; + output[pos++] = COLOR_CHAR; if (bold) output[pos++] = '5'; else @@ -2776,7 +2768,7 @@ unsigned char *BX_strip_ansi (const unsigned char *str) } else if (args[i] >= 40 && args[i] <= 47) { - output[pos++] = 003; + output[pos++] = COLOR_CHAR; output[pos++] = ','; if (blink) output[pos++] = '5'; @@ -2800,8 +2792,8 @@ unsigned char *BX_strip_ansi (const unsigned char *str) */ case 3: { - const u_char *end; - int lhs, rhs; + const char *end; + int lhs, rhs; put_back(); end = skip_ctl_c_seq (str, &lhs, &rhs, 0);