Skip to content

Commit c95b955

Browse files
radaretrufae
authored andcommitted
Implement e subcommand for all types using cfg.editor ##types
1 parent 43111b7 commit c95b955

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

libr/core/cmd_type.inc.c

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ static RCoreHelpMessage help_msg_tf = {
7070
"tf", " <name>", "show function signature",
7171
"tfc", " [name]", "list all/given function signatures in C output format with newlines",
7272
"tfcj", " <name>", "same as above but in JSON",
73+
"tfe", " <name>", "edit function signature with cfg.editor",
7374
"tfj", "", "list all function definitions in JSON",
7475
"tfj", " <name>", "show function signature in JSON",
7576
NULL
@@ -113,6 +114,7 @@ static RCoreHelpMessage help_msg_te = {
113114
"tec", "", "list all loaded enums in C output format with newlines",
114115
"tec", " <name>", "list given loaded enums in C output format with newlines",
115116
"ted", "", "list all loaded enums in C output format without newlines",
117+
"tee", " <name>", "edit enum with cfg.editor",
116118
"tej", "", "list all loaded enums in json",
117119
"tej", " <enum>", "show enum in json",
118120
"test", " [-x,f,d] [path]", "test if executable, file or directory exists",
@@ -124,6 +126,7 @@ static RCoreHelpMessage help_msg_tt = {
124126
"Usage: tt[...]", "", "Type typedef commands",
125127
"tt", "", "list all loaded typedefs",
126128
"tt", " <typename>", "show name for given type alias",
129+
"tte", " <name>", "edit typedef with cfg.editor",
127130
"ttj", "", "show typename and type alias in json",
128131
"ttc", "<name>", "show typename and type alias in C output format",
129132
NULL
@@ -161,6 +164,7 @@ static RCoreHelpMessage help_msg_ts = {
161164
"ts", " [type]", "show pf format string for given struct",
162165
"tu.", "[type]", "show struct contents mapped in current offset (same as .ts)",
163166
"ts-", " <type> [name2..]", "delete struct type(s) (supports glob with *)",
167+
"tse", " <name>", "edit struct with cfg.editor",
164168
"tsj", "", "list all loaded structs in json",
165169
"tsj", " [type]", "show pf format string for given struct in json",
166170
"ts*", "", "show pf.<name> format string for all loaded structs",
@@ -178,6 +182,7 @@ static RCoreHelpMessage help_msg_tu = {
178182
"tu", " [type]", "show pf format string for given union",
179183
"tu.", "[type]", "show union contents mapped in current offset (same as .tu)",
180184
"tu-", " <type> [name2..]", "delete union type(s) (supports glob with *)",
185+
"tue", " <name>", "edit union with cfg.editor",
181186
"tuj", "", "list all loaded unions in json",
182187
"tuj", " [type]", "show pf format string for given union in json",
183188
"tu*", "", "show pf.<name> format string for all loaded unions",
@@ -562,6 +567,41 @@ static void cmd_type_noreturn(RCore *core, const char *input) {
562567
}
563568
}
564569

570+
static void cmd_type_edit(RCore *core, const char *typename, const char *c_cmd) {
571+
if (R_STR_ISEMPTY (typename)) {
572+
R_LOG_ERROR ("Type name required");
573+
return;
574+
}
575+
char *str = r_core_cmd_strf (core, "%s %s", c_cmd, typename);
576+
if (R_STR_ISEMPTY (str)) {
577+
R_LOG_ERROR ("Type '%s' not found", typename);
578+
free (str);
579+
return;
580+
}
581+
char *tmp = r_core_editor (core, "*.h", str);
582+
if (tmp) {
583+
r_str_trim (tmp);
584+
char *str_trimmed = strdup (str);
585+
r_str_trim (str_trimmed);
586+
if (strcmp (tmp, str_trimmed)) {
587+
r_core_cmdf (core, "t- %s", typename);
588+
char *errmsg = NULL;
589+
char *out = r_anal_cparse (core->anal, tmp, &errmsg);
590+
if (out) {
591+
r_anal_save_parsed_type (core->anal, out);
592+
free (out);
593+
}
594+
if (errmsg) {
595+
R_LOG_ERROR ("%s", errmsg);
596+
free (errmsg);
597+
}
598+
}
599+
free (str_trimmed);
600+
free (tmp);
601+
}
602+
free (str);
603+
}
604+
565605
static bool stdifstruct(void *user, const char *k, const char *v) {
566606
Sdb *TDB = (Sdb *)user;
567607
if (!strcmp (v, "struct") && !r_str_startswith (k, "typedef")) {
@@ -1777,6 +1817,13 @@ static int cmd_type(void *data, const char *input) {
17771817
case 'd':
17781818
print_struct_union_in_c_format (core, TDB, stdifunion, r_str_trim_head_ro (input + 2), false);
17791819
break;
1820+
case 'e': // "tue"
1821+
if (input[2] == '?') {
1822+
r_core_cmd_help_match (core, help_msg_tu, "tue");
1823+
} else {
1824+
cmd_type_edit (core, r_str_trim_head_ro (input + 2), "tuc");
1825+
}
1826+
break;
17801827
case 'v': // "tuv"
17811828
print_struct_union_with_offsets (core, TDB, stdifunion, r_str_trim_head_ro (input + 2), true);
17821829
break;
@@ -1912,6 +1959,13 @@ static int cmd_type(void *data, const char *input) {
19121959
case 'd': // "tsd"
19131960
print_struct_union_in_c_format (core, TDB, stdifstruct, r_str_trim_head_ro (input + 2), false);
19141961
break;
1962+
case 'e': // "tse"
1963+
if (input[2] == '?') {
1964+
r_core_cmd_help_match (core, help_msg_ts, "tse");
1965+
} else {
1966+
cmd_type_edit (core, r_str_trim_head_ro (input + 2), "tsc");
1967+
}
1968+
break;
19151969
case 'j': // "tsj"
19161970
if (input[2]) {
19171971
showFormat (core, r_str_trim_head_ro (input + 2), 'j');
@@ -2045,6 +2099,13 @@ static int cmd_type(void *data, const char *input) {
20452099
print_enum_in_c_format (core, TDB, r_str_trim_head_ro (input + 2), false);
20462100
}
20472101
break;
2102+
case 'e': // "tee"
2103+
if (input[2] == '?') {
2104+
r_core_cmd_help_match (core, help_msg_te, "tee");
2105+
} else {
2106+
cmd_type_edit (core, r_str_trim_head_ro (input + 2), "tec");
2107+
}
2108+
break;
20482109
case 'v': // "tev"
20492110
if (input[2] == '?') {
20502111
r_core_cmd_help_match (core, help_msg_te, "tev");
@@ -2562,6 +2623,13 @@ static int cmd_type(void *data, const char *input) {
25622623
ls_free (l);
25632624
}
25642625
break;
2626+
case 'e': // "tfe"
2627+
if (input[2] == '?') {
2628+
r_core_cmd_help_match (core, help_msg_tf, "tfe");
2629+
} else {
2630+
cmd_type_edit (core, r_str_trim_head_ro (input + 2), "tfc");
2631+
}
2632+
break;
25652633
case 'j': // "tfj"
25662634
if (input[2] == ' ') {
25672635
printFunctionType (core, input + 2);
@@ -2661,6 +2729,14 @@ static int cmd_type(void *data, const char *input) {
26612729
ls_free (l);
26622730
break;
26632731
}
2732+
if (input[1] == 'e') { // "tte"
2733+
if (input[2] == '?') {
2734+
r_core_cmd_help_match (core, help_msg_tt, "tte");
2735+
} else {
2736+
cmd_type_edit (core, r_str_trim_head_ro (input + 2), "ttc");
2737+
}
2738+
break;
2739+
}
26642740
if (input[1] == '?') {
26652741
r_core_cmd_help (core, help_msg_tt);
26662742
break;

0 commit comments

Comments
 (0)