Rizin
unix-like reverse engineering framework and cli tools
line.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2007-2019 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_util.h>
5 #include <rz_cons.h>
6 
8 #define I rz_line_instance
9 
11  c->run = NULL;
12  c->run_user = NULL;
13 }
14 
16  return &rz_line_instance;
17 }
18 
20  I.hist_up = NULL;
21  I.hist_down = NULL;
22  I.prompt = strdup("> ");
23  I.contents = NULL;
24  I.enable_vi_mode = false;
25  I.clipboard = NULL;
26  I.kill_ring = rz_list_newf(free);
27  I.kill_ring_ptr = -1;
28 #if __WINDOWS__
29  I.vtmode = rz_cons_detect_vt_mode();
30 #else
32 #endif
33  if (!rz_line_dietline_init()) {
34  eprintf("error: rz_line_dietline_init\n");
35  }
36  rz_line_completion_init(&I.completion, 4096);
37  rz_line_nscompletion_init(&I.ns_completion);
38  return &I;
39 }
40 
41 RZ_API void rz_line_free(void) {
42  // XXX: prompt out of the heap?
43  free((void *)I.prompt);
44  I.prompt = NULL;
45  rz_list_free(I.kill_ring);
47  rz_line_completion_fini(&I.completion);
48 }
49 
50 RZ_API void rz_line_clipboard_push(const char *str) {
51  I.kill_ring_ptr += 1;
52  rz_list_insert(I.kill_ring, I.kill_ring_ptr, strdup(str));
53 }
54 
55 // handle const or dynamic prompts?
56 RZ_API void rz_line_set_prompt(const char *prompt) {
57  free(I.prompt);
58  I.prompt = strdup(prompt);
59  RzCons *cons = rz_cons_singleton();
60  I.cb_fkey = cons->cb_fkey;
61 }
62 
63 // handle const or dynamic prompts?
65  return strdup(I.prompt);
66 }
67 
68 RZ_API void rz_line_completion_init(RzLineCompletion *completion, size_t args_limit) {
69  completion->run = NULL;
70  completion->run_user = NULL;
71  completion->args_limit = args_limit;
72  rz_pvector_init(&completion->args, free);
73 }
74 
76  rz_line_completion_clear(completion);
77 }
78 
79 RZ_API void rz_line_completion_push(RzLineCompletion *completion, const char *str) {
80  rz_return_if_fail(completion && str);
81  if (completion->quit) {
82  return;
83  }
84  if (rz_pvector_len(&completion->args) < completion->args_limit) {
85  char *s = strdup(str);
86  if (s) {
87  rz_pvector_push(&completion->args, (void *)s);
88  }
89  } else {
90  completion->quit = true;
91  eprintf("WARNING: Maximum completion capacity reached, increase scr.maxtab");
92  }
93 }
94 
95 RZ_API void rz_line_completion_set(RzLineCompletion *completion, int argc, const char **argv) {
96  rz_return_if_fail(completion && (argc >= 0));
97  rz_line_completion_clear(completion);
98  if (argc > completion->args_limit) {
99  eprintf("WARNING: Maximum completion capacity reached, increase scr.maxtab");
100  }
101  size_t count = RZ_MIN(argc, completion->args_limit);
102  rz_pvector_reserve(&completion->args, count);
103  int i;
104  for (i = 0; i < count; i++) {
105  rz_line_completion_push(completion, argv[i]);
106  }
107 }
108 
110  rz_return_if_fail(completion);
111  completion->quit = false;
112  rz_pvector_clear(&completion->args);
113 }
114 
124  if (!res) {
125  return NULL;
126  }
128  HtPPOptions opt = { 0 };
129  opt.cmp = (HtPPListComparator)strcmp;
130  opt.hashfn = (HtPPHashFunction)sdb_hash;
131  res->options_ht = ht_pp_new_opt(&opt);
132  res->start = start;
133  res->end = end;
134  if (!end_string) {
135  end_string = " ";
136  }
137  res->end_string = end_string;
138  return res;
139 }
140 
145  if (!res) {
146  return;
147  }
148  ht_pp_free(res->options_ht);
149  rz_pvector_fini(&res->options);
150  free(res);
151 }
152 
157  if (ht_pp_find(res->options_ht, option, NULL)) {
158  return;
159  }
160  char *dup = strdup(option);
161  rz_pvector_push(&res->options, dup);
162  ht_pp_insert(res->options_ht, dup, dup);
163 }
164 
170 RZ_API void rz_line_ns_completion_result_propose(RzLineNSCompletionResult *res, const char *option, const char *cur, size_t cur_len) {
171  if (strncmp(option, cur, cur_len)) {
172  return;
173  }
175 }
176 
177 #include "dietline.c"
lzma_index ** i
Definition: index.h:629
RZ_API RzCons * rz_cons_singleton(void)
Definition: cons.c:300
#define RZ_API
#define NULL
Definition: cris-opc.c:27
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 count
Definition: sflib.h:98
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 int rz_line_dietline_init(void)
Definition: dietline.c:147
RZ_API void rz_line_hist_free(void)
Definition: dietline.c:455
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
RZ_API RZ_BORROW RzListIter * rz_list_insert(RZ_NONNULL RzList *list, ut32 n, void *data)
Inserts a new element at the N-th position.
Definition: list.c:342
RZ_API RZ_OWN RzList * rz_list_newf(RzListFree f)
Returns a new initialized RzList pointer and sets the free method.
Definition: list.c:248
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137
static void rz_line_nscompletion_init(RzLineNSCompletion *c)
Definition: line.c:10
RZ_API RzLine * rz_line_singleton(void)
Definition: line.c:15
RZ_API void rz_line_set_prompt(const char *prompt)
Definition: line.c:56
RZ_API void rz_line_completion_push(RzLineCompletion *completion, const char *str)
Definition: line.c:79
RZ_API RZ_OWN char * rz_line_get_prompt(void)
Definition: line.c:64
RZ_API void rz_line_clipboard_push(const char *str)
Definition: line.c:50
RZ_API void rz_line_completion_set(RzLineCompletion *completion, int argc, const char **argv)
Definition: line.c:95
RZ_API void rz_line_ns_completion_result_free(RzLineNSCompletionResult *res)
Definition: line.c:144
RZ_API RzLine * rz_line_new(void)
Definition: line.c:19
#define I
Definition: line.c:8
RZ_API RzLineNSCompletionResult * rz_line_ns_completion_result_new(size_t start, size_t end, const char *end_string)
Definition: line.c:122
RZ_API void rz_line_completion_fini(RzLineCompletion *completion)
Definition: line.c:75
RZ_API void rz_line_ns_completion_result_add(RzLineNSCompletionResult *res, const char *option)
Definition: line.c:156
RZ_API void rz_line_completion_clear(RzLineCompletion *completion)
Definition: line.c:109
RZ_API void rz_line_completion_init(RzLineCompletion *completion, size_t args_limit)
Definition: line.c:68
static RzLine rz_line_instance
Definition: line.c:7
RZ_API void rz_line_ns_completion_result_propose(RzLineNSCompletionResult *res, const char *option, const char *cur, size_t cur_len)
Add a new option to the list of possible autocomplete-able value if it matches the given string.
Definition: line.c:170
RZ_API void rz_line_free(void)
Definition: line.c:41
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds const char struct utimbuf static buf static inc static sig const char static mode dup
Definition: sflib.h:68
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
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")
#define eprintf(x, y...)
Definition: rlcc.c:7
static RzSocket * s
Definition: rtr.c:28
#define rz_return_if_fail(expr)
Definition: rz_assert.h:100
@ RZ_VIRT_TERM_MODE_COMPLETE
All the sequences goes through VT (Windows Terminal, mintty, all OSs)
Definition: rz_cons.h:451
#define RZ_OWN
Definition: rz_types.h:62
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define RZ_MIN(x, y)
static void ** rz_pvector_reserve(RzPVector *vec, size_t capacity)
Definition: rz_vector.h:312
RZ_API void rz_pvector_init(RzPVector *vec, RzPVectorFree free)
Definition: vector.c:298
RZ_API void rz_pvector_fini(RzPVector *vec)
Definition: vector.c:331
static size_t rz_pvector_len(const RzPVector *vec)
Definition: rz_vector.h:231
void(* RzPVectorFree)(void *e)
Definition: rz_vector.h:43
static void ** rz_pvector_push(RzPVector *vec, void *x)
Definition: rz_vector.h:300
RZ_API void rz_pvector_clear(RzPVector *vec)
Definition: vector.c:326
RZ_API ut32 sdb_hash(const char *key)
Definition: util.c:22
#define c(i)
Definition: sha256.c:43
Definition: getopt.h:84
RzConsFunctionKey cb_fkey
Definition: rz_cons.h:532
size_t args_limit
Definition: rz_cons.h:1045
RzLineCompletionCb run
Definition: rz_cons.h:1048
void * run_user
Definition: rz_cons.h:1049
RzPVector args
Definition: rz_cons.h:1047
size_t start
First byte that was considered for autocompletion. Everything before this will be left intact.
Definition: rz_cons.h:1062
const char * end_string
String to place after the only option available is autocompleted. By default a space is used.
Definition: rz_cons.h:1064
HtPP * options_ht
Hash table to keep track of duplicated autocompletion suggestions.
Definition: rz_cons.h:1061
size_t end
Last byte that was considered for autocompletion. Everything after this will be left intact.
Definition: rz_cons.h:1063
RzPVector options
Vector of options that can be used for autocompletion.
Definition: rz_cons.h:1060
static char * prompt(const char *str, const char *txt)
Definition: vmenus.c:30