void get_color_codes(char *old, char *str, char *buf) { char *pti, *pto, col[100], tmp[BUFFER_SIZE]; int len, vtc, fgc, bgc, cnt; pto = tmp; pti = old; while (*pti) { while ((len = find_non_color_codes(pti)) != 0) { memcpy(pto, pti, len); pti += len; pto += len; } if (*pti) { pti++; } } pti = str; while (*pti) { while ((len = find_non_color_codes(pti)) != 0) { memcpy(pto, pti, len); pti += len; pto += len; } if (*pti) { pti++; } } *pto = 0; if (strlen(tmp) == 0) { buf[0] = 0; return; } vtc = 0; fgc = -1; bgc = -1; pti = tmp; while (*pti) { switch (*pti) { case 27: pti += 2; if (pti[-1] == 'm') { vtc = 0; fgc = -1; bgc = -1; break; } for (cnt = 0 ; pti[cnt] ; cnt++) { col[cnt] = pti[cnt]; if (pti[cnt] == ';' || pti[cnt] == 'm') { col[cnt] = 0; cnt = -1; pti += 1 + strlen(col); if (HAS_BIT(vtc, COL_256) && (HAS_BIT(vtc, COL_XTF) || HAS_BIT(vtc, COL_XTB))) { if (HAS_BIT(vtc, COL_XTF)) { fgc = URANGE(0, atoi(col), 255); } if (HAS_BIT(vtc, COL_XTB)) { bgc = URANGE(0, atoi(col), 255); } DEL_BIT(vtc, COL_XTF|COL_XTB); } else { switch (atoi(col)) { case 0: vtc = 0; fgc = -1; bgc = -1; break; case 1: SET_BIT(vtc, COL_BLD); break; case 4: SET_BIT(vtc, COL_UND); break; case 5: if (HAS_BIT(vtc, COL_XTF) || HAS_BIT(vtc, COL_XTB)) { SET_BIT(vtc, COL_256); } else { SET_BIT(vtc, COL_BLK); } break; case 7: SET_BIT(vtc, COL_REV); break; case 2: case 21: case 22: DEL_BIT(vtc, COL_BLD); break; case 24: DEL_BIT(vtc, COL_UND); break; case 25: DEL_BIT(vtc, COL_BLK); break; case 27: DEL_BIT(vtc, COL_REV); break; case 38: DEL_BIT(vtc, COL_XTB); DEL_BIT(vtc, COL_256); SET_BIT(vtc, COL_XTF); fgc = -1; break; case 39: DEL_BIT(vtc, COL_UND); fgc = -1; break; case 48: DEL_BIT(vtc, COL_XTF); DEL_BIT(vtc, COL_256); SET_BIT(vtc, COL_XTB); bgc = -1; break; default: DEL_BIT(vtc, COL_256); /* Use 256 color's 16 color notation */ if (atoi(col) / 10 == 4) { bgc = atoi(col) % 10; } if (atoi(col) / 10 == 9) { bgc = atoi(col) % 10 + 8; } if (atoi(col) / 10 == 3) { fgc = atoi(col) % 10; } if (atoi(col) / 10 == 10) { fgc = atoi(col) % 10 + 8; } break; } } } if (pti[-1] == 'm') { break; } } break; default: pti++; break; } } strcpy(buf, "\033[0"); if (HAS_BIT(vtc, COL_BLD)) { strcat(buf, ";1"); } if (HAS_BIT(vtc, COL_UND)) { strcat(buf, ";4"); } if (HAS_BIT(vtc, COL_BLK)) { strcat(buf, ";5"); } if (HAS_BIT(vtc, COL_REV)) { strcat(buf, ";7"); } if (fgc >= 16) { cat_sprintf(buf, ";38;5;%d", fgc); } else if (fgc >= 8) { cat_sprintf(buf, ";%d", fgc + 100); } else if (fgc >= 0) { cat_sprintf(buf, ";%d", fgc + 30); } if (bgc >= 16) { cat_sprintf(buf, ";48;5;%d", bgc); } else if (bgc >= 8) { cat_sprintf(buf, ";%d", fgc + 90); } else if (bgc >= 0) { cat_sprintf(buf, ";%d", bgc + 40); } strcat(buf, "m"); }
void check_all_highlights(struct session *ses, char *original, char *line) { struct listroot *root = ses->list[LIST_HIGHLIGHT]; struct listnode *node; char *pto, *ptl, *ptm; char match[BUFFER_SIZE], color[BUFFER_SIZE], reset[BUFFER_SIZE], output[BUFFER_SIZE], plain[BUFFER_SIZE]; for (root->update = 0 ; root->update < root->used ; root->update++) { if (check_one_regexp(ses, root->list[root->update], line, original, 0)) { node = root->list[root->update]; get_highlight_codes(ses, node->right, color); *output = *reset = 0; pto = original; ptl = line; do { if (*gtd->vars[0] == 0) { break; } strcpy(match, gtd->vars[0]); strip_vt102_codes(match, plain); ptm = strstr(pto, match); if (!HAS_BIT(node->flags, NODE_FLAG_META)) { if (ptm == NULL) { break; } ptl = strstr(ptl, match); ptl = ptl + strlen(match); } *ptm = 0; get_color_codes(reset, pto, reset); cat_sprintf(output, "%s%s%s\033[0m%s", pto, color, plain, reset); pto = ptm + strlen(match); show_debug(ses, LIST_HIGHLIGHT, "#DEBUG HIGHLIGHT {%s}", node->left); } while (check_one_regexp(ses, node, ptl, pto, 0)); strcat(output, pto); strcpy(original, output); } } }