forked from radareorg/radare2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_project.c
243 lines (237 loc) · 5.93 KB
/
cmd_project.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/* radare - LGPL - Copyright 2009-2017 - pancake */
#include "r_config.h"
#include "r_core.h"
#include "r_print.h"
static const char *help_msg_P[] = {
"Usage:", "P[?osi] [file]", "Project management",
"Pc", " [file]", "show project script to console",
"Pd", " [file]", "delete project",
"Pi", " [file]", "show project information",
"Pl", "", "list all projects",
"Pn", "[j]", "show project notes (Pnj for json)",
"Pn", " [base64]", "set notes text",
"Pn", " -", "edit notes with cfg.editor",
"Po", " [file]", "open project",
"Ps", " [file]", "save project",
"PS", " [file]", "save script file",
"P-", " [file]", "delete project (alias for Pd)",
"NOTE:", "", "See 'e??prj.'",
"NOTE:", "", "project are stored in ~/.config/radare2/projects",
NULL
};
static const char *help_msg_Pn[] = {
"Usage:", "Pn[j-?] [...]", "Project Notes",
"Pn", "", "show project notes",
"Pn", " -", "edit notes with cfg.editor",
"Pn-", "", "delete notes",
"Pn-", "str", "delete lines matching /str/ in notes",
"Pn+", "str", "append one line to the notes",
"Pnj", "", "show notes in base64",
"Pnj", " [base64]", "set notes in base64",
"Pnx", "", "run project note commands",
NULL
};
static void cmd_project_init(RCore *core) {
DEFINE_CMD_DESCRIPTOR (core, P);
DEFINE_CMD_DESCRIPTOR (core, Pn);
}
static int cmd_project(void *data, const char *input) {
RCore *core = (RCore *) data;
const char *file, *arg = (input && *input)? input + 1: NULL;
const char *fileproject = r_config_get (core->config, "prj.name");
char *str = NULL;
if (!input) {
return false;
}
str = strdup (fileproject);
arg = strchr (input, ' ');
if (arg) {
arg++;
} else {
if (*input) {
arg = input + 1;
if (*arg == '&') {
arg++;
}
}
}
file = arg;
switch (input[0]) {
case 'c':
if (input[1] == ' ') {
r_core_project_cat (core, input + 2);
} else {
eprintf ("Usage: Pc [prjname]\n");
}
break;
case 'o':
// if (r_file_is_regular (file))
if (input[1] == '&') {
r_core_project_open (core, file, true);
} else if (input[1]) {
r_core_project_open (core, file, false);
} else {
if (file && *file) {
r_cons_println (file);
}
}
break;
case 'l':
r_core_project_list (core, input[1]);
break;
case 'd':
case '-':
r_core_project_delete (core, file);
break;
case 's':
if (!file || !file[0]) { /* if no argument specified use current project */
file = str;
}
if (r_core_project_save (core, file)) {
r_cons_println (file);
}
break;
case 'S':
if (input[1] == ' ') {
r_core_project_save_rdb (core, input + 2, R_CORE_PRJ_ALL);
} else {
eprintf ("Usage: PS [file]\n");
}
break;
case 'n': // "Pn"
if (input[1] == '?') {
r_core_cmd_help (core, help_msg_Pn);
} else if (!fileproject || !*fileproject) {
eprintf ("No project\n");
} else {
switch (input[1]) {
case '-': // "Pn-"
/* remove lines containing specific words */
{
FILE *fd = r_sandbox_fopen (str, "w");
if (!fd) {
eprintf ("Cannot open %s\n", str);
} else {
char *str = r_core_project_notes_file (core, fileproject);
char *data = r_file_slurp (str, NULL);
int del = 0;
if (data) {
char *ptr, *nl;
for (ptr = data; ptr; ptr = nl) {
nl = strchr (ptr, '\n');
if (nl) {
*nl++ = 0;
if (strstr (ptr, input + 2)) {
del++;
} else {
fprintf (fd, "%s\n", ptr);
}
}
}
free (data);
}
if (del > 0) {
eprintf ("Deleted %d lines\n", del);
}
free (str);
fclose (fd);
}
}
break;
case ' ': // "Pn "
if (input[2] == '-') {
char *str = r_core_project_notes_file (core, fileproject);
// edit with cfg.editor
const char *editor = r_config_get (core->config, "cfg.editor");
if (str && *str && editor && *editor) {
r_sys_cmdf ("%s %s", editor, str);
} else {
eprintf ("No cfg.editor configured\n");
}
free (str);
} else {
// char *str = r_core_project_notes_file (core, fileproject);
// append line to project notes
char *str = r_core_project_notes_file (core, fileproject);
char *data = r_file_slurp (str, NULL);
FILE *fd = r_sandbox_fopen (str, "a");
if (fd) {
fprintf (fd, "%s\n", input + 2);
fclose (fd);
}
free (str);
free (data);
}
break;
case '+': // "Pn+"
{
char *str = r_core_project_notes_file (core, fileproject);
char *data = r_file_slurp (str, NULL);
data = r_str_append (data, input + 2);
data = r_str_append (data, "\n");
r_file_dump (str, (const ut8*)data, strlen (data), false);
free (data);
free (str);
}
break;
case 'j': // "Pnj"
if (!input[2]) {
int len = 0;
/* get base64 string */
char *str = r_core_project_notes_file (core, fileproject);
if (str) {
char *data = r_file_slurp (str, &len);
char *res = r_base64_encode_dyn (data, len);
if (res) {
r_cons_println (res);
free (res);
}
free (data);
free (str);
}
} else if (input[2] == ' ') {
/* set base64 string */
ut8 *data = r_base64_decode_dyn (input + 3, -1);
if (data) {
char *str = r_core_project_notes_file (core, fileproject);
if (str) {
r_file_dump (str, data, strlen ((const char *) data), 0);
free (str);
}
free (data);
}
} else {
eprintf ("Usage: `Pnj` or `Pnj ...`\n");
}
break;
case 'x': // "Pnx"
r_core_project_execute_cmds (core, fileproject);
break;
case 0: // "Pn"
{
char *str = r_core_project_notes_file (core, fileproject);
char *data = r_file_slurp (str, NULL);
if (data) {
r_cons_println (data);
free (data);
}
free (str);
}
break;
}
}
break;
case 'i':
if (file && *file) {
char *prjName = r_core_project_info (core, file);
r_cons_println (prjName);
free (prjName);
}
break;
default:
r_core_cmd_help (core, help_msg_P);
break;
}
free (str);
return true;
}