Better parsing for the "join" command ##shell

This commit is contained in:
pancake 2024-07-12 23:11:11 +02:00
parent 7b435a6b8b
commit bda4eb0832
2 changed files with 108 additions and 98 deletions

View File

@ -1678,6 +1678,57 @@ static int cmd_l(void *data, const char *input) { // "l"
static int cmd_join(void *data, const char *input) { // "join"
RCore *core = (RCore *)data;
char *tmp = strdup (input);
const char *arg1 = strchr (tmp, ' ');
if (!arg1) {
goto beach;
}
arg1 = r_str_trim_head_ro (arg1);
if (!arg1) {
goto beach;
}
char *end = strchr (arg1, ' ');
if (!end) {
goto beach;
}
*end = '\0';
const char *arg2 = end + 1;
if (!arg2) {
goto beach;
}
arg2 = r_str_trim_head_ro (arg2);
switch (*input) {
case '?': // "join?"
goto beach;
default: // "join"
if (!arg1) {
arg1 = "";
}
if (!arg2) {
arg2 = "";
}
if (!r_fs_check (core->fs, arg1) && !r_fs_check (core->fs, arg2)) {
char *res = r_syscmd_join (arg1, arg2);
if (res) {
r_cons_print (res);
free (res);
}
}
break;
}
free (tmp);
return R_CMD_RC_SUCCESS;
beach:
r_core_cmd_help (core, help_msg_j);
free (tmp);
return R_CMD_RC_SUCCESS;
}
static int cmd_j(void *data, const char *input) { // "j"
RCore *core = (RCore *)data;
if (r_str_startswith (input, "oin")) {
return cmd_join (data, input);
}
if (r_str_startswith (input, "i:")) {
char *res = r_core_cmd_str (core, input + 2);
char *indented = r_print_json_indent (res, true, " ", NULL);
@ -1689,7 +1740,8 @@ static int cmd_join(void *data, const char *input) { // "join"
if (input[0] == 'q') { // "jq"
r_core_cmd_callf (core, "!jq%s", input + 1);
return R_CMD_RC_SUCCESS;
} else if (input[0] == 's') { // "js"
}
if (input[0] == 's') { // "js"
if (input[1] == ':' || input[1] == '.') { // "js:"
if (input[2]) {
if (r_lang_use (core->lang, "qjs")) {
@ -1758,50 +1810,7 @@ static int cmd_join(void *data, const char *input) { // "join"
free (s);
return R_CMD_RC_SUCCESS;
}
char *tmp = strdup (input);
const char *arg1 = strchr (tmp, ' ');
if (!arg1) {
goto beach;
}
arg1 = r_str_trim_head_ro (arg1);
if (!arg1) {
goto beach;
}
char *end = strchr (arg1, ' ');
if (!end) {
goto beach;
}
*end = '\0';
const char *arg2 = end + 1;
if (!arg2) {
goto beach;
}
arg2 = r_str_trim_head_ro (arg2);
switch (*input) {
case '?': // "join?"
goto beach;
default: // "join"
if (!arg1) {
arg1 = "";
}
if (!arg2) {
arg2 = "";
}
if (!r_fs_check (core->fs, arg1) && !r_fs_check (core->fs, arg2)) {
char *res = r_syscmd_join (arg1, arg2);
if (res) {
r_cons_print (res);
free (res);
}
}
break;
}
free (tmp);
return R_CMD_RC_SUCCESS;
beach:
r_core_cmd_help (core, help_msg_j);
free (tmp);
return R_CMD_RC_SUCCESS;
return R_CMD_RC_FAILURE;
}
static int cmd_plus(void *data, const char *input) {
@ -6687,7 +6696,7 @@ R_API void r_core_cmd_init(RCore *core) {
{ "i", "get file info", cmd_info },
{ "k", "perform sdb query", cmd_kuery },
{ "l", "list files and directories", cmd_l },
{ "j", "join the contents of the two files", cmd_join },
{ "j", "join the contents of the two files", cmd_j },
{ "h", "show the top n number of line in file", cmd_h },
{ "L", "manage dynamically loaded plugins", cmd_plugins },
{ "m", "mount filesystem", cmd_mount },

View File

@ -434,7 +434,7 @@ R_API char *r_syscmd_uniq(const char *file) {
R_API char *r_syscmd_join(const char *file1, const char *file2) {
const char *p1 = NULL, *p2 = NULL;
RList *list1, *list2, *list = r_list_newf (free);
RList *list1, *list2, *list;
if (!list) {
return NULL;
}
@ -447,62 +447,63 @@ R_API char *r_syscmd_join(const char *file1, const char *file2) {
}
if (file2) {
if ((p2 = strchr (file2, ' '))) {
p2 = p2 + 1;
p2++;
} else {
p2 = file2;
}
}
if (R_STR_ISNOTEMPTY (p1) && R_STR_ISNOTEMPTY (p2)) {
char *filename1 = strdup (p1);
char *filename2 = strdup (p2);
r_str_trim (filename1);
r_str_trim (filename2);
char *data1 = r_file_slurp (filename1, NULL);
char *data2 = r_file_slurp (filename2, NULL);
char *data = NULL;
RListIter *iter1, *iter2;
if (!data1 || !data2) {
R_LOG_ERROR ("No such files or directory");
} else {
list1 = r_str_split_list (data1, "\n", 0);
list2 = r_str_split_list (data2, "\n", 0);
char *str1, *str2;
r_list_foreach (list1, iter1, str1) {
char *field = strdup (str1); // extract comman field
char *end = strchr (field, ' ');
if (end) {
*end = '\0';
} else {
free (field);
continue;
}
r_list_foreach (list2, iter2, str2) {
if (r_str_startswith (str2, field)) {
char *out = r_str_new (field);
char *first = strchr (str1, ' ');
char *second = strchr (str2, ' ');
out = r_str_append (out, r_str_get_fail (first, " "));
out = r_str_append (out, r_str_get_fail (second, " "));
r_list_append (list, out);
}
}
free (field);
}
data = r_list_to_str (list, '\n');
r_list_free (list1);
r_list_free (list2);
}
r_list_free (list);
free (filename1);
free (filename2);
free (data1);
free (data2);
return data;
} else {
eprintf ("Usage: join file1 file2\n");
if (R_STR_ISEMPTY (p1) || R_STR_ISEMPTY (p2)) {
R_LOG_INFO ("Usage: join file1 file2\n");
return NULL;
}
return NULL;
list = r_list_newf (free);
char *filename1 = strdup (p1);
char *filename2 = strdup (p2);
r_str_trim (filename1);
r_str_trim (filename2);
char *data1 = r_file_slurp (filename1, NULL);
char *data2 = r_file_slurp (filename2, NULL);
char *data = NULL;
RListIter *iter1, *iter2;
if (!data1 || !data2) {
R_LOG_ERROR ("No such files or directory");
} else {
list1 = r_str_split_list (data1, "\n", 0);
list2 = r_str_split_list (data2, "\n", 0);
char *str1, *str2;
r_list_foreach (list1, iter1, str1) {
char *field = strdup (str1); // extract command field
char *end = strchr (field, ' ');
if (end) {
*end = '\0';
} else {
free (field);
continue;
}
r_list_foreach (list2, iter2, str2) {
if (r_str_startswith (str2, field)) {
char *out = r_str_new (field);
char *first = strchr (str1, ' ');
char *second = strchr (str2, ' ');
out = r_str_append (out, r_str_get_fail (first, " "));
out = r_str_append (out, r_str_get_fail (second, " "));
r_list_append (list, out);
}
}
free (field);
}
data = r_list_to_str (list, '\n');
r_list_free (list1);
r_list_free (list2);
}
r_list_free (list);
free (filename1);
free (filename2);
free (data1);
free (data2);
return data;
}
R_API char *r_syscmd_cat(const char *file) {