-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpkg_client.c
More file actions
76 lines (66 loc) · 2.05 KB
/
pkg_client.c
File metadata and controls
76 lines (66 loc) · 2.05 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
#include "pkg_internal.h"
void pkg_reconstruct_argv(char *out, u64 out_size) {
u64 argc;
u64 i;
u64 used = 0ULL;
if (out == (char *)0 || out_size == 0ULL) {
return;
}
out[0] = '\0';
argc = cleonos_sys_proc_argc();
for (i = 1ULL; i < argc; i++) {
char item[128];
if (cleonos_sys_proc_argv(i, item, (u64)sizeof(item)) == 0ULL || item[0] == '\0') {
continue;
}
if (used != 0ULL && pkg_append_char(out, out_size, &used, ' ') == 0) {
return;
}
if (pkg_append_text(out, out_size, &used, item) == 0) {
return;
}
}
}
int pkg_client_app_main(void) {
ush_cmd_ctx ctx;
ush_cmd_ret ret;
ush_state sh;
char initial_cwd[USH_PATH_MAX];
char arg_line[PKG_ARG_MAX];
int has_context = 0;
int success;
const char *arg = "";
ush_zero(&ctx, (u64)sizeof(ctx));
ush_zero(&ret, (u64)sizeof(ret));
ush_zero(arg_line, (u64)sizeof(arg_line));
ush_init_state(&sh);
ush_copy(initial_cwd, (u64)sizeof(initial_cwd), sh.cwd);
if (ush_command_ctx_read(&ctx) != 0) {
if (ctx.cmd[0] != '\0' && ush_streq(ctx.cmd, "pkg") != 0) {
has_context = 1;
ush_copy(arg_line, (u64)sizeof(arg_line), ctx.arg);
arg = arg_line;
if (ctx.cwd[0] == '/') {
ush_copy(sh.cwd, (u64)sizeof(sh.cwd), ctx.cwd);
ush_copy(initial_cwd, (u64)sizeof(initial_cwd), sh.cwd);
}
}
}
if (has_context == 0) {
pkg_reconstruct_argv(arg_line, (u64)sizeof(arg_line));
arg = arg_line;
}
success = pkg_run(&sh, arg);
if (has_context != 0) {
if (ush_streq(sh.cwd, initial_cwd) == 0) {
ret.flags |= USH_CMD_RET_FLAG_CWD;
ush_copy(ret.cwd, (u64)sizeof(ret.cwd), sh.cwd);
}
if (sh.exit_requested != 0) {
ret.flags |= USH_CMD_RET_FLAG_EXIT;
ret.exit_code = sh.exit_code;
}
(void)ush_command_ret_write(&ret);
}
return (success != 0) ? 0 : 1;
}