Repository navigation
Expand file tree
/
Copy pathsys_sh.c
More file actions
196 lines (190 loc) · 4 KB
/
Copy pathsys_sh.c
File metadata and controls
196 lines (190 loc) · 4 KB
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
/* radare - LGPL - Copyright 2009-2021 - pancake */
#include <r_userconf.h>
#include <r_util.h>
// typedef struct { int rc; } RSystem;
// sh
char *get_argv(const char *l, int *argpos, int pos) {
int a0 = argpos[(pos * 2)];
int a1 = argpos[(pos * 2) + 1];
return r_str_ndup (l + a0, a1 - a0);
}
R_API int r_sys_tem_statement(const char *l) {
#define MAXARG 16
l = r_str_trim_head_ro (l);
int argc = 0;
int argv[32] = {0}; // max 16 args
int i, pos = 0;
int inquote = 0;
bool escaped = false;
int cmdpos = 0;
for (pos = cmdpos; l[pos]; pos++) {
char lc = l[pos];
if (lc == '\\') {
escaped = !escaped;
} else if (lc == '"' || lc == '\'') {
if (inquote) {
inquote = 0;
}
inquote = lc;
} else if (lc == ';') {
if (!inquote) {
// XXX shouldnt have this char
R_LOG_ERROR ("Too many arguments");
return -1;
}
} else if (lc == ' ') {
argv[1+(argc * 2)] = pos;
argv[2+(argc * 2)] = pos + 1;
argc++;
if (argc + 2 >= MAXARG) {
R_LOG_ERROR ("Too many arguments");
return -1;
}
}
}
argv[1+(argc*2)] = pos;
argc++;
char *progname = NULL;
RStrBuf *sb = r_strbuf_new ("");
for (i = 0; i < argc; i++) {
char *s = get_argv (l, argv, i);
// unescape and remove heading/tail quotes if any
// eprintf ("ARG %d %s \n", i, s);
if (i == 0) {
progname = s;
} else {
r_strbuf_append (sb, " ");
// TODO: replace env vars
// s = r_str_replace (s, "$FOO", "TMP", 1);
r_strbuf_append (sb, s);
free (s);
}
}
char *sbs = r_strbuf_drain (sb);
r_str_trim (sbs);
// run command and return rc
if (R_STR_ISNOTEMPTY (progname)) {
if (!strcmp (progname, "false")) {
free (sbs);
return 1;
}
if (!strcmp (progname, "true")) {
free (sbs);
return 0;
}
if (!strcmp (progname, "ls")) {
char *s = r_syscmd_ls (sbs, 80);
eprintf ("%s\n", s);
free (s);
} else if (!strcmp (progname, "mktemp")) {
char *d = r_syscmd_mktemp (sbs);
printf ("%s\n", d);
free (d);
return d? 0: 1;
} else if (!strcmp (progname, "mkdir")) {
return r_syscmd_mkdir (sbs);
} else if (!strcmp (progname, "pwd")) {
char *wd = r_sys_getdir ();
eprintf ("%s\n", wd);
free (wd);
} else if (!strcmp (progname, "echo")) {
eprintf ("%s\n", sbs);
} else {
free (sbs);
return r_sys_cmd (l);
}
}
free (sbs);
return 0;
}
R_API int r_sys_tem_line(const char *l) {
int pos = 0;
int inquote = 0;
bool escaped = false;
int rc = 0;
int cmdpos = 0;
int nextpos = 0;
repeat:
for (pos = cmdpos; l[pos]; pos++) {
char lc = l[pos];
char ld = l[pos + 1];
if (lc == '\\') {
escaped = !escaped;
} else if (lc == '"' || lc == '\'') {
if (inquote) {
if (lc == inquote) {
inquote = 0;
}
} else {
inquote = lc;
}
} else if (lc == '|' && ld == '|') {
if (!inquote) {
if (pos == cmdpos) {
if (rc) {
nextpos = pos + 2;
cmdpos += 2;
break;
} else {
// eprintf ("op || failed\n");
return 1;
}
} else {
nextpos = pos - 1;
break;
}
}
} else if (lc == '&' && ld == '&') {
if (!inquote) {
if (pos == cmdpos) {
if (rc) {
// eprintf ("op && failed\n");
return 1;
} else {
cmdpos += 2;
nextpos = pos + 2;
break;
}
} else {
nextpos = pos - 1;
break;
}
}
} else if (lc == ';') {
if (!inquote) {
// end of command
nextpos = pos;
break;
}
}
}
if (nextpos) {
char *s = r_str_ndup (l + cmdpos, nextpos - cmdpos);
rc = r_sys_tem_statement (s);
free (s);
cmdpos = nextpos + 1;
nextpos = 0;
goto repeat;
} else {
// eprintf ("RUN (%s)\n", l + cmdpos);
rc = r_sys_tem_statement (l + cmdpos);
}
//r_sys_setenv ("?", rc);
// separate by ; honor quotes
return rc;
}
R_API int r_sys_tem(const char *s) {
int rc = 0;
char *ss = strdup (s);
char *line;
RListIter *iter;
RList *lines = r_str_split_list (ss, "\n", 0);
r_list_foreach (lines, iter, line) {
char *l = r_str_trim_dup (line);
rc = r_sys_tem_line (l);
free (l);
}
r_list_free (lines);
free (ss);
return rc;
}