Rizin
unix-like reverse engineering framework and cli tools
canvas.c File Reference
#include <math.h>
#include <rz_cons.h>
#include <rz_util/rz_assert.h>

Go to the source code of this file.

Macros

#define USE_UTF8   (rz_cons_singleton()->use_utf8)
 
#define USE_UTF8_CURVY   (rz_cons_singleton()->use_utf8_curvy)
 
#define W(y)   rz_cons_canvas_write(c, y)
 
#define G(x, y)   rz_cons_canvas_gotoxy(c, x, y)
 

Functions

static bool __isAnsiSequence (const char *s)
 
static int __getAnsiPiece (const char *p, char *chr)
 
static void attribute_free_kv (HtUPKv *kv)
 
static const char * __attributeAt (RzConsCanvas *c, int loc)
 
static void __stampAttribute (RzConsCanvas *c, int loc, int length)
 
static const char * set_attr (RzConsCanvas *c, const char *s)
 
static int __getUtf8Length (const char *s, int n)
 
static int __getUtf8Length2 (const char *s, int n, int left)
 
static bool __expandLine (RzConsCanvas *c, int real_len, int utf8_len)
 
RZ_API void rz_cons_canvas_free (RzConsCanvas *c)
 
static bool attribute_delete_cb (void *user, const ut64 key, const void *value)
 
RZ_API void rz_cons_canvas_clear (RzConsCanvas *c)
 
RZ_API bool rz_cons_canvas_gotoxy (RzConsCanvas *c, int x, int y)
 
RZ_API RzConsCanvasrz_cons_canvas_new (int w, int h)
 
RZ_API void rz_cons_canvas_write (RzConsCanvas *c, const char *s)
 
RZ_API RZ_OWN char * rz_cons_canvas_to_string (RzConsCanvas *c)
 
RZ_API void rz_cons_canvas_print_region (RzConsCanvas *c)
 
RZ_API void rz_cons_canvas_print (RzConsCanvas *c)
 
RZ_API int rz_cons_canvas_resize (RzConsCanvas *c, int w, int h)
 
RZ_API void rz_cons_canvas_box (RzConsCanvas *c, int x, int y, int w, int h, const char *color)
 
RZ_API void rz_cons_canvas_fill (RzConsCanvas *c, int x, int y, int w, int h, char ch)
 
RZ_API void rz_cons_canvas_line (RzConsCanvas *c, int x, int y, int x2, int y2, RzCanvasLineStyle *style)
 

Macro Definition Documentation

◆ G

#define G (   x,
 
)    rz_cons_canvas_gotoxy(c, x, y)

Definition at line 12 of file canvas.c.

◆ USE_UTF8

#define USE_UTF8   (rz_cons_singleton()->use_utf8)

Definition at line 8 of file canvas.c.

◆ USE_UTF8_CURVY

#define USE_UTF8_CURVY   (rz_cons_singleton()->use_utf8_curvy)

Definition at line 9 of file canvas.c.

◆ W

#define W (   y)    rz_cons_canvas_write(c, y)

Definition at line 11 of file canvas.c.

Function Documentation

◆ __attributeAt()

static const char* __attributeAt ( RzConsCanvas c,
int  loc 
)
static

Definition at line 36 of file canvas.c.

36  {
37  if (!c->color) {
38  return NULL;
39  }
40  return ht_up_find(c->attrs, loc, NULL);
41 }
#define NULL
Definition: cris-opc.c:27
#define c(i)
Definition: sha256.c:43

References c, and NULL.

Referenced by rz_cons_canvas_to_string().

◆ __expandLine()

static bool __expandLine ( RzConsCanvas c,
int  real_len,
int  utf8_len 
)
static

Definition at line 110 of file canvas.c.

110  {
111  if (real_len == 0) {
112  return true;
113  }
114  int buf_utf8_len = __getUtf8Length2(c->b[c->y] + c->x, utf8_len, c->blen[c->y] - c->x);
115  int goback = RZ_MAX(0, (buf_utf8_len - utf8_len));
116  int padding = (real_len - utf8_len) - goback;
117 
118  if (padding) {
119  if (padding > 0 && c->blen[c->y] + padding > c->bsize[c->y]) {
120  int newsize = RZ_MAX(c->bsize[c->y] * 1.5, c->blen[c->y] + padding);
121  char *newline = realloc(c->b[c->y], sizeof(*c->b[c->y]) * (newsize));
122  if (!newline) {
123  return false;
124  }
125  memset(newline + c->bsize[c->y], 0, newsize - c->bsize[c->y]);
126  c->b[c->y] = newline;
127  c->bsize[c->y] = newsize;
128  }
129  int size = RZ_MAX(c->blen[c->y] - c->x - goback, 0);
130  char *start = c->b[c->y] + c->x + goback;
131  char *tmp = malloc(size);
132  if (!tmp) {
133  return false;
134  }
135  memcpy(tmp, start, size);
136  if (padding < 0) {
137  int lap = RZ_MAX(0, c->b[c->y] - (start + padding));
138  memcpy(start + padding + lap, tmp + lap, size - lap);
139  free(tmp);
140  c->blen[c->y] += padding;
141  return true;
142  }
143  memcpy(start + padding, tmp, size);
144  free(tmp);
145  c->blen[c->y] += padding;
146  }
147  return true;
148 }
static int __getUtf8Length2(const char *s, int n, int left)
Definition: canvas.c:95
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void start
Definition: sflib.h:133
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void uLong size
Definition: ioapi.h:138
return memset(p, 0, total)
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
void * realloc(void *ptr, size_t size)
Definition: malloc.c:144
void * malloc(size_t size)
Definition: malloc.c:123
#define RZ_MAX(x, y)

References __getUtf8Length2(), c, free(), malloc(), memcpy(), memset(), realloc(), RZ_MAX, start, and autogen_x86imm::tmp.

Referenced by rz_cons_canvas_write().

◆ __getAnsiPiece()

static int __getAnsiPiece ( const char *  p,
char *  chr 
)
static

Definition at line 18 of file canvas.c.

18  {
19  const char *q = p;
20  if (!p) {
21  return 0;
22  }
23  while (p && *p && *p != '\n' && !__isAnsiSequence(p)) {
24  p++;
25  }
26  if (chr) {
27  *chr = *p;
28  }
29  return p - q;
30 }
static bool __isAnsiSequence(const char *s)
Definition: canvas.c:14
void * p
Definition: libc.cpp:67

References __isAnsiSequence(), and p.

Referenced by rz_cons_canvas_write().

◆ __getUtf8Length()

static int __getUtf8Length ( const char *  s,
int  n 
)
static

Definition at line 80 of file canvas.c.

80  {
81  int i = 0, j = 0, fullwidths = 0;
82  while (s[i] && n > 0) {
83  if ((s[i] & 0xc0) != 0x80) {
84  j++;
85  if (rz_str_char_fullwidth(s + i, n)) {
86  fullwidths++;
87  }
88  }
89  n--;
90  i++;
91  }
92  return j + fullwidths;
93 }
lzma_index ** i
Definition: index.h:629
int n
Definition: mipsasm.c:19
static RzSocket * s
Definition: rtr.c:28
RZ_API bool rz_str_char_fullwidth(const char *s, size_t left)
Definition: str.c:2277

References i, n, rz_str_char_fullwidth(), and s.

Referenced by rz_cons_canvas_write().

◆ __getUtf8Length2()

static int __getUtf8Length2 ( const char *  s,
int  n,
int  left 
)
static

Definition at line 95 of file canvas.c.

95  {
96  int i = 0, fullwidths = 0;
97  while (n > -1 && i < left && s[i]) {
98  if (rz_str_char_fullwidth(s + i, left - i)) {
99  fullwidths++;
100  }
101  if ((s[i] & 0xc0) != 0x80) {
102  n--;
103  }
104  i++;
105  }
106  i -= fullwidths;
107  return n == -1 ? i - 1 : i;
108 }

References i, n, rz_str_char_fullwidth(), and s.

Referenced by __expandLine(), and rz_cons_canvas_write().

◆ __isAnsiSequence()

static bool __isAnsiSequence ( const char *  s)
inlinestatic

Definition at line 14 of file canvas.c.

14  {
15  return s && s[0] == 033 && s[1] == '[';
16 }

References s.

Referenced by __getAnsiPiece(), and set_attr().

◆ __stampAttribute()

static void __stampAttribute ( RzConsCanvas c,
int  loc,
int  length 
)
static

Definition at line 43 of file canvas.c.

43  {
44  if (!c->color) {
45  return;
46  }
47  int i;
48  ht_up_update(c->attrs, loc, (void *)c->attr);
49  for (i = 1; i < length; i++) {
50  ht_up_delete(c->attrs, loc + i);
51  }
52 }
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void length
Definition: sflib.h:133

References c, i, and length.

Referenced by rz_cons_canvas_write().

◆ attribute_delete_cb()

static bool attribute_delete_cb ( void *  user,
const ut64  key,
const void *  value 
)
static

Definition at line 168 of file canvas.c.

168  {
169  HtUP *ht = (HtUP *)user;
170  ht_up_delete(ht, key);
171  return true;
172 }
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len key
Definition: sflib.h:118

References key.

Referenced by rz_cons_canvas_clear().

◆ attribute_free_kv()

static void attribute_free_kv ( HtUPKv *  kv)
static

Definition at line 32 of file canvas.c.

32  {
33  free(kv->value);
34 }

References free().

Referenced by rz_cons_canvas_new().

◆ rz_cons_canvas_box()

RZ_API void rz_cons_canvas_box ( RzConsCanvas c,
int  x,
int  y,
int  w,
int  h,
const char *  color 
)

Definition at line 486 of file canvas.c.

486  {
487  rz_return_if_fail(c && w && h);
488 
489  if (color) {
490  c->attr = color;
491  }
492  if (!c->color) {
493  c->attr = Color_RESET;
494  }
495  char *row = malloc(w + 1);
496  if (!row) {
497  return;
498  }
499 
500  const char *hline = USE_UTF8 ? RUNECODESTR_LINE_HORIZ : "-";
501  const char *vtmp = USE_UTF8 ? RUNECODESTR_LINE_VERT : "|";
502  const char *tl_corner = USE_UTF8 ? (USE_UTF8_CURVY ? RUNECODESTR_CURVE_CORNER_TL : RUNECODESTR_CORNER_TL) : ".";
503  const char *tr_corner = USE_UTF8 ? (USE_UTF8_CURVY ? RUNECODESTR_CURVE_CORNER_TR : RUNECODESTR_CORNER_TR) : ".";
504  const char *bl_corner = USE_UTF8 ? (USE_UTF8_CURVY ? RUNECODESTR_CURVE_CORNER_BL : RUNECODESTR_CORNER_BL) : "`";
505  const char *br_corner = USE_UTF8 ? (USE_UTF8_CURVY ? RUNECODESTR_CURVE_CORNER_BR : RUNECODESTR_CORNER_BR) : "'";
506  int i, x_mod;
507  int roundcorners = 0;
508  char *row_ptr;
509 
510  RzStrBuf *vline = rz_strbuf_new(NULL);
511  rz_strbuf_appendf(vline, Color_RESET "%s%s", color, vtmp);
512 
513  row[0] = roundcorners ? '.' : tl_corner[0];
514  if (w > 2) {
515  memset(row + 1, hline[0], w - 2);
516  }
517  if (w > 1) {
518  row[w - 1] = roundcorners ? '.' : tr_corner[0];
519  }
520  row[w] = 0;
521 
522  row_ptr = row;
523  x_mod = x;
524  if (x < -c->sx) {
525  x_mod = RZ_MIN(-c->sx, x_mod + w);
526  row_ptr += x_mod - x;
527  }
528  if (G(x_mod, y)) {
529  W(row_ptr);
530  }
531  if (G(x_mod, y + h - 1)) {
532  row[0] = roundcorners ? '\'' : bl_corner[0];
533  row[w - 1] = roundcorners ? '\'' : br_corner[0];
534  W(row_ptr);
535  }
536  for (i = 1; i < h - 1; i++) {
537  if (G(x, y + i)) {
538  W(rz_strbuf_get(vline));
539  }
540  if (G(x + w - 1, y + i)) {
541  W(rz_strbuf_get(vline));
542  }
543  }
544  free(row);
545  rz_strbuf_free(vline);
546  if (color) {
547  c->attr = Color_RESET;
548  }
549 }
#define USE_UTF8_CURVY
Definition: canvas.c:9
#define USE_UTF8
Definition: canvas.c:8
#define W(y)
Definition: canvas.c:11
#define G(x, y)
Definition: canvas.c:12
#define w
Definition: crypto_rc6.c:13
int x
Definition: mipsasm.c:20
#define rz_return_if_fail(expr)
Definition: rz_assert.h:100
#define Color_RESET
Definition: rz_cons.h:617
#define RUNECODESTR_CURVE_CORNER_TL
Definition: rz_cons.h:395
#define RUNECODESTR_LINE_HORIZ
Definition: rz_cons.h:391
#define RUNECODESTR_CORNER_BL
Definition: rz_cons.h:388
#define RUNECODESTR_CORNER_TL
Definition: rz_cons.h:392
#define RUNECODESTR_CORNER_BR
Definition: rz_cons.h:387
#define RUNECODESTR_CURVE_CORNER_BL
Definition: rz_cons.h:398
#define RUNECODESTR_CORNER_TR
Definition: rz_cons.h:393
#define RUNECODESTR_CURVE_CORNER_TR
Definition: rz_cons.h:396
#define RUNECODESTR_CURVE_CORNER_BR
Definition: rz_cons.h:397
#define RUNECODESTR_LINE_VERT
Definition: rz_cons.h:385
RZ_API char * rz_strbuf_get(RzStrBuf *sb)
Definition: strbuf.c:321
RZ_API RzStrBuf * rz_strbuf_new(const char *s)
Definition: strbuf.c:8
RZ_API void rz_strbuf_free(RzStrBuf *sb)
Definition: strbuf.c:358
RZ_API bool rz_strbuf_appendf(RzStrBuf *sb, const char *fmt,...) RZ_PRINTF_CHECK(2
#define RZ_MIN(x, y)
#define h(i)
Definition: sha256.c:48
static int color
Definition: visual.c:20

References c, color, Color_RESET, free(), G, h, i, malloc(), memset(), NULL, RUNECODESTR_CORNER_BL, RUNECODESTR_CORNER_BR, RUNECODESTR_CORNER_TL, RUNECODESTR_CORNER_TR, RUNECODESTR_CURVE_CORNER_BL, RUNECODESTR_CURVE_CORNER_BR, RUNECODESTR_CURVE_CORNER_TL, RUNECODESTR_CURVE_CORNER_TR, RUNECODESTR_LINE_HORIZ, RUNECODESTR_LINE_VERT, RZ_MIN, rz_return_if_fail, rz_strbuf_appendf(), rz_strbuf_free(), rz_strbuf_get(), rz_strbuf_new(), USE_UTF8, USE_UTF8_CURVY, W, w, and x.

Referenced by __panel_print(), __update_modal(), normal_RzANode_print(), and rz_diff_draw_tui().

◆ rz_cons_canvas_clear()

RZ_API void rz_cons_canvas_clear ( RzConsCanvas c)

Definition at line 174 of file canvas.c.

174  {
175  rz_return_if_fail(c && c->b);
176  int y;
177  for (y = 0; y < c->h; y++) {
178  memset(c->b[y], '\n', c->bsize[y]);
179  }
180 
181  ht_up_foreach(c->attrs, attribute_delete_cb, c->attrs);
182 }
static bool attribute_delete_cb(void *user, const ut64 key, const void *value)
Definition: canvas.c:168

References attribute_delete_cb(), c, memset(), and rz_return_if_fail.

Referenced by rz_cons_canvas_new(), rz_cons_canvas_resize(), and rz_diff_draw_tui().

◆ rz_cons_canvas_fill()

RZ_API void rz_cons_canvas_fill ( RzConsCanvas c,
int  x,
int  y,
int  w,
int  h,
char  ch 
)

Definition at line 551 of file canvas.c.

551  {
552  int i;
553  if (w < 0) {
554  return;
555  }
556  char *row = malloc(w + 1);
557  if (!row) {
558  return;
559  }
560  memset(row, ch, w);
561  row[w] = 0;
562  for (i = 0; i < h; i++) {
563  if (G(x, y + i)) {
564  W(row);
565  }
566  }
567  free(row);
568 }

References free(), G, h, i, malloc(), memset(), W, w, and x.

Referenced by __create_new_canvas(), __panel_all_clear(), __panel_print(), __panels_refresh(), __update_modal(), agraph_print(), and rz_diff_draw_tui().

◆ rz_cons_canvas_free()

RZ_API void rz_cons_canvas_free ( RzConsCanvas c)

Definition at line 150 of file canvas.c.

150  {
151  if (!c) {
152  return;
153  }
154  if (c->b) {
155  int y;
156  for (y = 0; y < c->h; y++) {
157  free(c->b[y]);
158  }
159  free(c->b);
160  }
161  free(c->bsize);
162  free(c->blen);
163  ht_up_free(c->attrs);
164  rz_str_constpool_fini(&c->constpool);
165  free(c);
166 }
RZ_API void rz_str_constpool_fini(RzStrConstPool *pool)
Definition: str_constpool.c:15

References c, free(), and rz_str_constpool_fini().

Referenced by __call_visual_graph(), __panels_process(), cmd_pCd(), cmd_pCD(), cmd_pCx(), rz_agraph_free(), rz_cmd_heap_chunks_print_handler(), rz_cmd_print(), rz_cons_canvas_resize(), rz_core_visual_graph(), rz_diff_hex_visual(), and rz_diff_resize_buffer().

◆ rz_cons_canvas_gotoxy()

RZ_API bool rz_cons_canvas_gotoxy ( RzConsCanvas c,
int  x,
int  y 
)

Definition at line 184 of file canvas.c.

184  {
185  bool ret = true;
186  if (!c) {
187  return 0;
188  }
189  y += c->sy;
190  x += c->sx;
191 
192  if (y > c->h * 2) {
193  return false;
194  }
195  if (y >= c->h) {
196  y = c->h - 1;
197  ret = false;
198  }
199  if (y < 0) {
200  y = 0;
201  ret = false;
202  }
203  if (x < 0) {
204  // c->x = 0;
205  ret = false;
206  }
207  if (x > c->blen[y] * 2) {
208  return false;
209  }
210  if (x >= c->blen[y]) {
211  c->x = c->blen[y];
212  ret = false;
213  }
214  if (x < c->blen[y] && x >= 0) {
215  c->x = x;
216  }
217  if (y < c->h) {
218  c->y = y;
219  }
220  return ret;
221 }

References c, h, and x.

Referenced by __menu_panel_print(), __panels_refresh(), __update_help_contents(), __update_help_title(), __update_modal(), __update_panel_contents(), __update_panel_title(), cmd_pCd(), cmd_pCD(), cmd_pCx(), rz_cmd_print(), and rz_diff_draw_tui().

◆ rz_cons_canvas_line()

RZ_API void rz_cons_canvas_line ( RzConsCanvas c,
int  x,
int  y,
int  x2,
int  y2,
RzCanvasLineStyle style 
)

Definition at line 570 of file canvas.c.

570  {
571  if (c->linemode) {
572  rz_cons_canvas_line_square(c, x, y, x2, y2, style);
573  } else {
574  rz_cons_canvas_line_diagonal(c, x, y, x2, y2, style);
575  }
576 }
RZ_API void rz_cons_canvas_line_square(RzConsCanvas *c, int x, int y, int x2, int y2, RzCanvasLineStyle *style)
Definition: canvas_line.c:371
RZ_API void rz_cons_canvas_line_diagonal(RzConsCanvas *c, int x, int y, int x2, int y2, RzCanvasLineStyle *style)
Definition: canvas_line.c:285

References c, rz_cons_canvas_line_diagonal(), rz_cons_canvas_line_square(), x, and x2.

Referenced by agraph_print_edges(), agraph_print_edges_simple(), rz_cons_canvas_line_back_edge(), and rz_cons_canvas_line_square_defined().

◆ rz_cons_canvas_new()

RZ_API RzConsCanvas* rz_cons_canvas_new ( int  w,
int  h 
)

Definition at line 223 of file canvas.c.

223  {
224  if (w < 1 || h < 1) {
225  return NULL;
226  }
228  if (!c) {
229  return NULL;
230  }
231  c->bsize = NULL;
232  c->blen = NULL;
233  int i = 0;
234  c->color = 0;
235  c->sx = 0;
236  c->sy = 0;
237  c->b = malloc(sizeof *c->b * h);
238  if (!c->b) {
239  goto beach;
240  }
241  c->blen = malloc(sizeof *c->blen * h);
242  if (!c->blen) {
243  goto beach;
244  }
245  c->bsize = malloc(sizeof *c->bsize * h);
246  if (!c->bsize) {
247  goto beach;
248  }
249  for (i = 0; i < h; i++) {
250  c->b[i] = malloc(w + 1);
251  c->blen[i] = w;
252  c->bsize[i] = w + 1;
253  if (!c->b[i]) {
254  goto beach;
255  }
256  }
257  c->w = w;
258  c->h = h;
259  c->x = c->y = 0;
260  if (!rz_str_constpool_init(&c->constpool)) {
261  goto beach;
262  }
263  c->attrs = ht_up_new((HtUPDupValue)strdup, attribute_free_kv, NULL);
264  if (!c->attrs) {
265  goto beach;
266  }
267  c->attr = Color_RESET;
269  return c;
270 beach:
271  rz_str_constpool_fini(&c->constpool);
272  int j;
273  for (j = 0; j < i; j++) {
274  free(c->b[j]);
275  }
276  free(c->bsize);
277  free(c->blen);
278  free(c->b);
279  free(c);
280  return NULL;
281 }
static void attribute_free_kv(HtUPKv *kv)
Definition: canvas.c:32
RZ_API void rz_cons_canvas_clear(RzConsCanvas *c)
Definition: canvas.c:174
return strdup("=SP r13\n" "=LR r14\n" "=PC r15\n" "=A0 r0\n" "=A1 r1\n" "=A2 r2\n" "=A3 r3\n" "=ZF zf\n" "=SF nf\n" "=OF vf\n" "=CF cf\n" "=SN or0\n" "gpr lr .32 56 0\n" "gpr pc .32 60 0\n" "gpr cpsr .32 64 0 ____tfiae_________________qvczn\n" "gpr or0 .32 68 0\n" "gpr tf .1 64.5 0 thumb\n" "gpr ef .1 64.9 0 endian\n" "gpr jf .1 64.24 0 java\n" "gpr qf .1 64.27 0 sticky_overflow\n" "gpr vf .1 64.28 0 overflow\n" "gpr cf .1 64.29 0 carry\n" "gpr zf .1 64.30 0 zero\n" "gpr nf .1 64.31 0 negative\n" "gpr itc .4 64.10 0 if_then_count\n" "gpr gef .4 64.16 0 great_or_equal\n" "gpr r0 .32 0 0\n" "gpr r1 .32 4 0\n" "gpr r2 .32 8 0\n" "gpr r3 .32 12 0\n" "gpr r4 .32 16 0\n" "gpr r5 .32 20 0\n" "gpr r6 .32 24 0\n" "gpr r7 .32 28 0\n" "gpr r8 .32 32 0\n" "gpr r9 .32 36 0\n" "gpr r10 .32 40 0\n" "gpr r11 .32 44 0\n" "gpr r12 .32 48 0\n" "gpr r13 .32 52 0\n" "gpr r14 .32 56 0\n" "gpr r15 .32 60 0\n" "gpr r16 .32 64 0\n" "gpr r17 .32 68 0\n")
RZ_API bool rz_str_constpool_init(RzStrConstPool *pool)
Definition: str_constpool.c:10
#define RZ_NEW0(x)
Definition: rz_types.h:284

References attribute_free_kv(), c, Color_RESET, free(), h, i, malloc(), NULL, rz_cons_canvas_clear(), RZ_NEW0, rz_str_constpool_fini(), rz_str_constpool_init(), strdup(), and w.

Referenced by __create_new_canvas(), cmd_pCd(), cmd_pCD(), cmd_pCx(), create_agraph_from_graph(), print_double_linked_list_bin_graph(), rz_cmd_heap_chunks_print_handler(), rz_cmd_print(), rz_core_init(), rz_core_visual_graph(), rz_diff_hex_visual(), and rz_diff_resize_buffer().

◆ rz_cons_canvas_print()

RZ_API void rz_cons_canvas_print ( RzConsCanvas c)

Definition at line 423 of file canvas.c.

423  {
424  char *o = rz_cons_canvas_to_string(c);
425  if (!o) {
426  return;
427  }
428  rz_cons_strcat(o);
429  free(o);
430 }
RZ_API RZ_OWN char * rz_cons_canvas_to_string(RzConsCanvas *c)
Definition: canvas.c:351
RZ_API void rz_cons_strcat(const char *str)
Definition: cons.c:1263

References c, free(), rz_cons_canvas_to_string(), and rz_cons_strcat().

Referenced by __panel_all_clear(), __panels_refresh(), __update_modal(), cmd_pCd(), cmd_pCD(), cmd_pCx(), rz_cmd_print(), and rz_diff_draw_tui().

◆ rz_cons_canvas_print_region()

RZ_API void rz_cons_canvas_print_region ( RzConsCanvas c)

Definition at line 410 of file canvas.c.

410  {
411  char *o = rz_cons_canvas_to_string(c);
412  if (RZ_STR_ISEMPTY(o)) {
413  free(o);
414  return;
415  }
416  rz_str_trim_tail(o);
417  if (RZ_STR_ISNOTEMPTY(o)) {
418  rz_cons_strcat(o);
419  }
420  free(o);
421 }
#define RZ_STR_ISNOTEMPTY(x)
Definition: rz_str.h:68
RZ_API RZ_BORROW char * rz_str_trim_tail(RZ_NONNULL char *str)
Removes whitespace characters (space, tab, newline etc.) from the end of a string and replaces them w...
Definition: str_trim.c:125
#define RZ_STR_ISEMPTY(x)
Definition: rz_str.h:67

References c, free(), rz_cons_canvas_to_string(), rz_cons_strcat(), RZ_STR_ISEMPTY, RZ_STR_ISNOTEMPTY, and rz_str_trim_tail().

Referenced by agraph_print().

◆ rz_cons_canvas_resize()

RZ_API int rz_cons_canvas_resize ( RzConsCanvas c,
int  w,
int  h 
)

Definition at line 432 of file canvas.c.

432  {
433  if (!c || w < 0 || h <= 0) {
434  return false;
435  }
436  int *newblen = realloc(c->blen, sizeof *c->blen * h);
437  if (!newblen) {
439  return false;
440  }
441  c->blen = newblen;
442  int *newbsize = realloc(c->bsize, sizeof *c->bsize * h);
443  if (!newbsize) {
445  return false;
446  }
447  c->bsize = newbsize;
448  char **newb = realloc(c->b, sizeof *c->b * h);
449  if (!newb) {
451  return false;
452  }
453  c->b = newb;
454  int i;
455  char *newline = NULL;
456  for (i = 0; i < h; i++) {
457  if (i < c->h) {
458  newline = realloc(c->b[i], sizeof *c->b[i] * (w + 1));
459  } else {
460  newline = malloc(w + 1);
461  }
462  c->blen[i] = w;
463  c->bsize[i] = w + 1;
464  if (!newline) {
465  size_t j;
466  for (j = 0; j <= i; j++) {
467  free(c->b[i]);
468  }
469  ht_up_free(c->attrs);
470  free(c->blen);
471  free(c->bsize);
472  free(c->b);
473  free(c);
474  return false;
475  }
476  c->b[i] = newline;
477  }
478  c->w = w;
479  c->h = h;
480  c->x = 0;
481  c->y = 0;
483  return true;
484 }
RZ_API void rz_cons_canvas_free(RzConsCanvas *c)
Definition: canvas.c:150

References c, free(), h, i, malloc(), NULL, realloc(), rz_cons_canvas_clear(), rz_cons_canvas_free(), and w.

Referenced by __panels_refresh(), and agraph_print().

◆ rz_cons_canvas_to_string()

RZ_API RZ_OWN char* rz_cons_canvas_to_string ( RzConsCanvas c)

Definition at line 351 of file canvas.c.

351  {
353 
354  int x, y, olen = 0, attr_x = 0;
355  bool is_first = true;
356 
357  for (y = 0; y < c->h; y++) {
358  olen += c->blen[y] + 1;
359  }
360  char *o = calloc(1, olen * 4 * CONS_MAX_ATTR_SZ);
361  if (!o) {
362  return NULL;
363  }
364  if (!olen) {
365  free(o);
366  return NULL;
367  }
368 
369  olen = 0;
370  for (y = 0; y < c->h; y++) {
371  if (!is_first) {
372  o[olen++] = '\n';
373  }
374  is_first = false;
375  attr_x = 0;
376  for (x = 0; x < c->blen[y]; x++) {
377  if ((c->b[y][x] & 0xc0) != 0x80) {
378  const char *atr = __attributeAt(c, y * c->w + attr_x);
379  if (atr) {
380  size_t len = strlen(atr);
381  memcpy(o + olen, atr, len);
382  olen += len;
383  }
384  attr_x++;
385  if (rz_str_char_fullwidth(c->b[y] + x, c->blen[y] - x)) {
386  attr_x++;
387  }
388  }
389  if (!c->b[y][x] || c->b[y][x] == '\n') {
390  o[olen++] = ' ';
391  continue;
392  }
393  const char *rune = rz_cons_get_rune((const ut8)c->b[y][x]);
394  if (rune) {
395  size_t rune_len = strlen(rune);
396  memcpy(o + olen, rune, rune_len + 1);
397  olen += rune_len;
398  } else {
399  o[olen++] = c->b[y][x];
400  }
401  }
402  while (olen > 0 && o[olen - 1] == ' ') {
403  o[--olen] = '\0';
404  }
405  }
406  o[olen] = '\0';
407  return o;
408 }
size_t len
Definition: 6502dis.c:15
static const char * __attributeAt(RzConsCanvas *c, int loc)
Definition: canvas.c:36
RZ_API const char * rz_cons_get_rune(const ut8 ch)
Definition: cons.c:1922
uint8_t ut8
Definition: lh5801.h:11
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
#define CONS_MAX_ATTR_SZ
Definition: rz_cons.h:348

References __attributeAt(), c, calloc(), CONS_MAX_ATTR_SZ, free(), len, memcpy(), NULL, rz_cons_get_rune(), rz_return_val_if_fail, rz_str_char_fullwidth(), and x.

Referenced by get_word_from_canvas(), get_word_from_canvas_for_menu(), rz_cons_canvas_print(), and rz_cons_canvas_print_region().

◆ rz_cons_canvas_write()

RZ_API void rz_cons_canvas_write ( RzConsCanvas c,
const char *  s 
)

Definition at line 283 of file canvas.c.

283  {
284  if (!c || !s || !*s || !RZ_BETWEEN(0, c->y, c->h - 1) || !RZ_BETWEEN(0, c->x, c->w - 1)) {
285  return;
286  }
287 
288  char ch;
289  int left, slen, attr_len, piece_len;
290  int orig_x = c->x, attr_x = c->x;
291 
292  c->x = __getUtf8Length2(c->b[c->y], c->x, c->blen[c->y]);
293 
294  /* split the string into pieces of non-ANSI chars and print them normally,
295  ** using the ANSI chars to set the attr of the canvas */
297  do {
298  const char *s_part = set_attr(c, s);
299  ch = 0;
300  piece_len = __getAnsiPiece(s_part, &ch);
301  if (piece_len == 0 && ch == '\0' && s_part == s) {
302  break;
303  }
304  left = c->blen[c->y] - c->x;
305  slen = piece_len;
306 
307  if (piece_len > left) {
308  int utf8_piece_len = __getUtf8Length(s_part, piece_len);
309  if (utf8_piece_len > c->w - attr_x) {
310  slen = left;
311  }
312  }
313 
314  int real_len = rz_str_nlen(s_part, slen);
315  int utf8_len = __getUtf8Length(s_part, slen);
316 
317  if (!__expandLine(c, real_len, utf8_len)) {
318  break;
319  }
320 
321  if (G(c->x - c->sx, c->y - c->sy)) {
322  memcpy(c->b[c->y] + c->x, s_part, slen);
323  }
324 
325  attr_len = slen <= 0 && s_part != s ? 1 : utf8_len;
326  if (attr_len > 0 && attr_x < c->blen[c->y]) {
327  __stampAttribute(c, c->y * c->w + attr_x, attr_len);
328  }
329 
330  s = s_part;
331  if (ch == '\n') {
332  c->attr = Color_RESET;
333  __stampAttribute(c, c->y * c->w + attr_x, 0);
334  c->y++;
335  s++;
336  if (*s == '\0' || c->y >= c->h) {
337  break;
338  }
339  c->x = __getUtf8Length2(c->b[c->y], orig_x, c->blen[c->y]);
340  attr_x = orig_x;
341  } else {
342  c->x += slen;
343  attr_x += utf8_len;
344  }
345  s += piece_len;
346  } while (*s && !rz_cons_is_breaked());
348  c->x = orig_x;
349 }
static const char * set_attr(RzConsCanvas *c, const char *s)
Definition: canvas.c:55
static int __getAnsiPiece(const char *p, char *chr)
Definition: canvas.c:18
static bool __expandLine(RzConsCanvas *c, int real_len, int utf8_len)
Definition: canvas.c:110
static void __stampAttribute(RzConsCanvas *c, int loc, int length)
Definition: canvas.c:43
static int __getUtf8Length(const char *s, int n)
Definition: canvas.c:80
RZ_API void rz_cons_break_pop(void)
Definition: cons.c:361
RZ_API void rz_cons_break_push(RzConsBreak cb, void *user)
Definition: cons.c:357
RZ_API bool rz_cons_is_breaked(void)
Definition: cons.c:373
RZ_API size_t rz_str_nlen(const char *s, size_t n)
Definition: str.c:1949
#define RZ_BETWEEN(x, y, z)

References __expandLine(), __getAnsiPiece(), __getUtf8Length(), __getUtf8Length2(), __stampAttribute(), c, Color_RESET, G, memcpy(), NULL, RZ_BETWEEN, rz_cons_break_pop(), rz_cons_break_push(), rz_cons_is_breaked(), rz_str_nlen(), s, and set_attr().

Referenced by __menu_panel_print(), __panels_refresh(), __update_help_contents(), __update_help_title(), __update_modal(), __update_panel_contents(), __update_panel_title(), cmd_pCd(), cmd_pCD(), cmd_pCx(), rz_cmd_print(), and rz_diff_draw_tui().

◆ set_attr()

static const char* set_attr ( RzConsCanvas c,
const char *  s 
)
static

Definition at line 55 of file canvas.c.

55  {
56  if (!c || !s) {
57  return NULL;
58  }
59  const char *p = s;
60 
61  while (__isAnsiSequence(p)) {
62  p += 2;
63  while (*p && *p != 'J' && *p != 'm' && *p != 'H') {
64  p++;
65  }
66  p++;
67  }
68 
69  const int slen = p - s;
70  if (slen > 0) {
71  RzStrBuf tmp;
73  rz_strbuf_append_n(&tmp, s, slen);
74  c->attr = rz_str_constpool_get(&c->constpool, rz_strbuf_get(&tmp));
76  }
77  return p;
78 }
RZ_API const char * rz_str_constpool_get(RzStrConstPool *pool, const char *str)
Definition: str_constpool.c:19
RZ_API void rz_strbuf_fini(RzStrBuf *sb)
Definition: strbuf.c:365
RZ_API void rz_strbuf_init(RzStrBuf *sb)
Definition: strbuf.c:33
RZ_API bool rz_strbuf_append_n(RzStrBuf *sb, const char *s, size_t l)
Definition: strbuf.c:229

References __isAnsiSequence(), c, NULL, p, rz_str_constpool_get(), rz_strbuf_append_n(), rz_strbuf_fini(), rz_strbuf_get(), rz_strbuf_init(), s, and autogen_x86imm::tmp.

Referenced by rz_cons_canvas_write().