R_API void r_cons_visual_write (char *buffer) { char white[1024]; int cols = I.columns; int alen, plen, lines = I.rows; const char *endptr; char *nl, *ptr = buffer, *pptr; if (I.null) return; memset (&white, ' ', sizeof (white)); while ((nl = strchr (ptr, '\n'))) { int len = ((int)(size_t)(nl-ptr))+1; *nl = 0; alen = real_strlen (ptr, len); *nl = '\n'; pptr = ptr > buffer ? ptr - 1 : ptr; plen = ptr > buffer ? len : len - 1; if (alen > cols) { int olen = len; endptr = r_str_ansi_chrn (ptr, cols); endptr++; len = endptr - ptr; plen = ptr > buffer ? len : len - 1; if (lines > 0) { r_cons_write (pptr, plen); if (len != olen) { r_cons_write (Color_RESET, strlen (Color_RESET)); } } } else { if (lines > 0) { int w = cols - alen; r_cons_write (pptr, plen); if (I.blankline && w>0) { if (w > sizeof (white) - 1) w = sizeof (white) - 1; r_cons_write (white, w); } } // TRICK to empty columns.. maybe buggy in w32 if (r_mem_mem ((const ut8*)ptr, len, (const ut8*)"\x1b[0;0H", 6)) { lines = I.rows; r_cons_write (pptr, plen); } } lines--; // do not use last line ptr = nl + 1; } /* fill the rest of screen */ if (lines > 0) { if (cols > sizeof (white)) { cols = sizeof (white); } while (--lines >= 0) { r_cons_write (white, cols); } } }
R_API void r_cons_visual_write (char *buffer) { char white[1024]; int cols = I.columns; int alen, lines = I.rows; const char *endptr; char *nl, *ptr = buffer; memset (&white, ' ', sizeof (white)); while ((nl = strchr (ptr, '\n'))) { int len = ((int)(size_t)(nl-ptr))+1; *nl = 0; //alen = r_str_ansi_len (ptr); // handle ansi chars { int utf8len = r_str_len_utf8 (ptr); int ansilen = r_str_ansi_len (ptr); int diff = len-utf8len; if (diff) diff--; alen = ansilen - diff; } *nl = '\n'; if (alen>cols) { endptr = r_str_ansi_chrn (ptr, cols); endptr++; len = (endptr-ptr); if (lines>0) { r_cons_write (ptr, len); } } else { if (lines>0) { int w = cols-alen; if (ptr>buffer) r_cons_write (ptr-1, len); else r_cons_write (ptr, len-1); if (I.blankline && w>0) { if (w>sizeof (white)-1) w = sizeof (white)-1; r_cons_write (white, w); } } // TRICK to empty columns.. maybe buggy in w32 if (r_mem_mem ((const ut8*)ptr, len, (const ut8*)"\x1b[0;0H", 6)) { lines = I.rows; r_cons_write (ptr, len); } } lines--; // do not use last line ptr = nl+1; } /* fill the rest of screen */ if (lines>0) { if (cols>sizeof (white)) cols = sizeof (white); while (lines-->0) r_cons_write (white, cols); } }
R_API char* r_str_replace_thunked(char *str, char *clean, int *thunk, int clen, const char *key, const char *val, int g) { int i, klen, vlen, slen, delta = 0, bias; char *newstr, *scnd, *p = clean, *str_p; if (!str || !key || !val || !clean || !thunk) return NULL; klen = strlen (key); vlen = strlen (val); if (klen == vlen && !strcmp (key, val)) return str; slen = strlen (str) + 1; for (i = 0; i < clen; ) { bias = 0; p = (char *)r_mem_mem ( (const ut8*)clean + i, clen - i, (const ut8*)key, klen); if (!p) break; i = (int)(size_t)(p - clean); /* as the original string changes size during replacement * we need delta to keep track of it*/ str_p = str + thunk[i] + delta; if (r_str_ansi_chrn(str_p, klen) - str_p > klen) { /* f**k, we're trying to highlight * a string with a CSI in the middle * avoid color breakage and disable this particular * CSIs */ int newo = thunk[i + klen] - thunk[i]; r_str_ansi_filter(str_p, NULL, NULL, newo); scnd = strdup (str_p + newo); bias = vlen - newo; } else { scnd = strdup (str_p + klen); bias = vlen - klen; } slen += bias; // HACK: this 32 avoids overwrites wtf newstr = realloc (str, slen + klen); if (!newstr) { eprintf ("realloc fail\n"); free (str); free (scnd); str = NULL; break; } str = newstr; str_p = str + thunk[i] + delta; memcpy (str_p, val, vlen); memcpy (str_p + vlen, scnd, strlen (scnd) + 1); i += klen; delta += bias; free (scnd); if (!g) break; } return str; }
static void color_line(const char *line, RStrpool *p, RRegexMatch *ms){ int i, m_len; int offset = 0; char *m_addr; char *inv[2] = {R_CONS_INVERT(R_TRUE, R_TRUE), R_CONS_INVERT(R_FALSE, R_TRUE)}; int linv[2] = {strlen(inv[0]), strlen(inv[1])}; r_strpool_empty(p); for (i = 0; i < NMATCHES; i++) { if (ms[i].rm_eo && (i < NMATCHES - 1)) { /* highlight a match */ r_strpool_memcat (p, line + offset, ms[i].rm_so - offset); r_strpool_memcat (p, inv[0], linv[0]); m_len = ms[i].rm_eo - ms[i].rm_so; m_addr = r_str_ndup (line + ms[i].rm_so, m_len); if (m_addr) { if(r_str_ansi_chrn (m_addr, m_len) - m_addr < m_len ){ /* there's a CSI in the middle of this match*/ m_len = r_str_ansi_filter(m_addr, NULL, NULL, m_len); } r_strpool_memcat (p, m_addr, m_len); r_strpool_memcat (p, inv[1], linv[1]); offset = ms[i].rm_eo; free(m_addr); } } else { /* append final part of string w/o matches */ r_strpool_append(p, line + offset); break; } } }