This commit is contained in:
2026-04-16 22:29:08 +08:00
parent 2d843a100e
commit 589a83cb07
29 changed files with 1397 additions and 49 deletions

View File

@@ -0,0 +1,61 @@
#include "cmd_runtime.h"
static void ush_args_write_u64_dec(u64 value) {
char tmp[32];
u64 len = 0ULL;
if (value == 0ULL) {
ush_write_char('0');
return;
}
while (value > 0ULL && len < (u64)sizeof(tmp)) {
tmp[len++] = (char)('0' + (value % 10ULL));
value /= 10ULL;
}
while (len > 0ULL) {
len--;
ush_write_char(tmp[len]);
}
}
int cleonos_app_main(int argc, char **argv, char **envp) {
int i;
int env_count = 0;
ush_write("argc=");
ush_args_write_u64_dec((u64)((argc >= 0) ? argc : 0));
ush_write_char('\n');
for (i = 0; i < argc; i++) {
ush_write("argv[");
ush_args_write_u64_dec((u64)i);
ush_write("]=");
if (argv != (char **)0 && argv[i] != (char *)0) {
ush_writeln(argv[i]);
} else {
ush_writeln("(null)");
}
}
if (envp != (char **)0) {
while (envp[env_count] != (char *)0 && env_count < 64) {
env_count++;
}
}
ush_write("envc=");
ush_args_write_u64_dec((u64)env_count);
ush_write_char('\n');
for (i = 0; i < env_count; i++) {
ush_write("env[");
ush_args_write_u64_dec((u64)i);
ush_write("]=");
ush_writeln(envp[i]);
}
return 0;
}

View File

@@ -1,9 +1,28 @@
#include "cmd_runtime.h"
static int ush_cmd_exec(const ush_state *sh, const char *arg) {
char target[USH_PATH_MAX];
char argv_line[USH_ARG_MAX];
char env_line[USH_PATH_MAX + 32ULL];
const char *rest = "";
char path[USH_PATH_MAX];
u64 status;
if (ush_resolve_exec_path(sh, arg, path, (u64)sizeof(path)) == 0) {
if (sh == (const ush_state *)0 || arg == (const char *)0 || arg[0] == '\0') {
ush_writeln("exec: usage exec <path|name> [args...]");
return 0;
}
if (ush_split_first_and_rest(arg, target, (u64)sizeof(target), &rest) == 0) {
ush_writeln("exec: usage exec <path|name> [args...]");
return 0;
}
argv_line[0] = '\0';
if (rest != (const char *)0 && rest[0] != '\0') {
ush_copy(argv_line, (u64)sizeof(argv_line), rest);
}
if (ush_resolve_exec_path(sh, target, path, (u64)sizeof(path)) == 0) {
ush_writeln("exec: invalid target");
return 0;
}
@@ -13,7 +32,11 @@ static int ush_cmd_exec(const ush_state *sh, const char *arg) {
return 0;
}
status = cleonos_sys_exec_path(path);
env_line[0] = '\0';
ush_copy(env_line, (u64)sizeof(env_line), "PWD=");
ush_copy(env_line + 4, (u64)(sizeof(env_line) - 4ULL), sh->cwd);
status = cleonos_sys_exec_pathv(path, argv_line, env_line);
if (status == (u64)-1) {
ush_writeln("exec: request failed");
@@ -25,7 +48,16 @@ static int ush_cmd_exec(const ush_state *sh, const char *arg) {
return 1;
}
ush_writeln("exec: returned non-zero status");
if ((status & (1ULL << 63)) != 0ULL) {
ush_writeln("exec: terminated by signal");
ush_print_kv_hex(" SIGNAL", status & 0xFFULL);
ush_print_kv_hex(" VECTOR", (status >> 8) & 0xFFULL);
ush_print_kv_hex(" ERROR", (status >> 16) & 0xFFFFULL);
} else {
ush_writeln("exec: returned non-zero status");
ush_print_kv_hex(" STATUS", status);
}
return 0;
}

View File

@@ -2,6 +2,7 @@
static int ush_cmd_help(void) {
ush_writeln("commands:");
ush_writeln(" help");
ush_writeln(" args [a b c] (print argc/argv/envp)");
ush_writeln(" ls [-l] [-R] [path]");
ush_writeln(" cat [file] (reads pipeline input when file omitted)");
ush_writeln(" grep [-n] <pattern> [file]");
@@ -9,7 +10,7 @@ static int ush_cmd_help(void) {
ush_writeln(" wc [file] / cut -d <char> -f <N> [file] / uniq [file] / sort [file]");
ush_writeln(" pwd");
ush_writeln(" cd [dir]");
ush_writeln(" exec|run <path|name>");
ush_writeln(" exec|run <path|name> [args...]");
ush_writeln(" clear");
ush_writeln(" ansi / ansitest / color");
ush_writeln(" wavplay <file.wav> [steps] [ticks] / wavplay --stop");
@@ -26,7 +27,7 @@ static int ush_cmd_help(void) {
ush_writeln(" mv <src> <dst> (/temp only)");
ush_writeln(" rm <path> (/temp only)");
ush_writeln(" pid");
ush_writeln(" spawn <path|name>");
ush_writeln(" spawn <path|name> [args...]");
ush_writeln(" wait <pid>");
ush_writeln(" sleep <ticks>");
ush_writeln(" yield");

View File

@@ -127,6 +127,7 @@ static int ush_split_two_args(const char *arg,
static int ush_cmd_help(void) {
ush_writeln("commands:");
ush_writeln(" help");
ush_writeln(" args [a b c] (print argc/argv/envp)");
ush_writeln(" ls [-l] [-R] [path]");
ush_writeln(" cat [file] (reads pipeline input when file omitted)");
ush_writeln(" grep [-n] <pattern> [file]");
@@ -134,7 +135,7 @@ static int ush_cmd_help(void) {
ush_writeln(" wc [file] / cut -d <char> -f <N> [file] / uniq [file] / sort [file]");
ush_writeln(" pwd");
ush_writeln(" cd [dir]");
ush_writeln(" exec|run <path|name>");
ush_writeln(" exec|run <path|name> [args...]");
ush_writeln(" clear");
ush_writeln(" ansi / ansitest / color");
ush_writeln(" wavplay <file.wav> [steps] [ticks] / wavplay --stop");
@@ -151,7 +152,7 @@ static int ush_cmd_help(void) {
ush_writeln(" mv <src> <dst> (/temp only)");
ush_writeln(" rm <path> (/temp only)");
ush_writeln(" pid");
ush_writeln(" spawn <path|name>");
ush_writeln(" spawn <path|name> [args...]");
ush_writeln(" wait <pid>");
ush_writeln(" sleep <ticks>");
ush_writeln(" yield");
@@ -1485,10 +1486,29 @@ static int ush_cmd_cd(ush_state *sh, const char *arg) {
}
static int ush_cmd_exec(const ush_state *sh, const char *arg) {
char target[USH_PATH_MAX];
char argv_line[USH_ARG_MAX];
char env_line[USH_PATH_MAX + 32ULL];
const char *rest = "";
char path[USH_PATH_MAX];
u64 status;
if (ush_resolve_exec_path(sh, arg, path, (u64)sizeof(path)) == 0) {
if (sh == (const ush_state *)0 || arg == (const char *)0 || arg[0] == '\0') {
ush_writeln("exec: usage exec <path|name> [args...]");
return 0;
}
if (ush_split_first_and_rest(arg, target, (u64)sizeof(target), &rest) == 0) {
ush_writeln("exec: usage exec <path|name> [args...]");
return 0;
}
argv_line[0] = '\0';
if (rest != (const char *)0 && rest[0] != '\0') {
ush_copy(argv_line, (u64)sizeof(argv_line), rest);
}
if (ush_resolve_exec_path(sh, target, path, (u64)sizeof(path)) == 0) {
ush_writeln("exec: invalid target");
return 0;
}
@@ -1498,7 +1518,11 @@ static int ush_cmd_exec(const ush_state *sh, const char *arg) {
return 0;
}
status = cleonos_sys_exec_path(path);
env_line[0] = '\0';
ush_copy(env_line, (u64)sizeof(env_line), "PWD=");
ush_copy(env_line + 4, (u64)(sizeof(env_line) - 4ULL), sh->cwd);
status = cleonos_sys_exec_pathv(path, argv_line, env_line);
if (status == (u64)-1) {
ush_writeln("exec: request failed");
@@ -1510,7 +1534,16 @@ static int ush_cmd_exec(const ush_state *sh, const char *arg) {
return 1;
}
ush_writeln("exec: returned non-zero status");
if ((status & (1ULL << 63)) != 0ULL) {
ush_writeln("exec: terminated by signal");
ush_print_kv_hex(" SIGNAL", status & 0xFFULL);
ush_print_kv_hex(" VECTOR", (status >> 8) & 0xFFULL);
ush_print_kv_hex(" ERROR", (status >> 16) & 0xFFFFULL);
} else {
ush_writeln("exec: returned non-zero status");
ush_print_kv_hex(" STATUS", status);
}
return 0;
}
@@ -1520,10 +1553,29 @@ static int ush_cmd_pid(void) {
}
static int ush_cmd_spawn(const ush_state *sh, const char *arg) {
char target[USH_PATH_MAX];
char argv_line[USH_ARG_MAX];
char env_line[USH_PATH_MAX + 32ULL];
const char *rest = "";
char path[USH_PATH_MAX];
u64 pid;
if (ush_resolve_exec_path(sh, arg, path, (u64)sizeof(path)) == 0) {
if (sh == (const ush_state *)0 || arg == (const char *)0 || arg[0] == '\0') {
ush_writeln("spawn: usage spawn <path|name> [args...]");
return 0;
}
if (ush_split_first_and_rest(arg, target, (u64)sizeof(target), &rest) == 0) {
ush_writeln("spawn: usage spawn <path|name> [args...]");
return 0;
}
argv_line[0] = '\0';
if (rest != (const char *)0 && rest[0] != '\0') {
ush_copy(argv_line, (u64)sizeof(argv_line), rest);
}
if (ush_resolve_exec_path(sh, target, path, (u64)sizeof(path)) == 0) {
ush_writeln("spawn: invalid target");
return 0;
}
@@ -1533,7 +1585,11 @@ static int ush_cmd_spawn(const ush_state *sh, const char *arg) {
return 0;
}
pid = cleonos_sys_spawn_path(path);
env_line[0] = '\0';
ush_copy(env_line, (u64)sizeof(env_line), "PWD=");
ush_copy(env_line + 4, (u64)(sizeof(env_line) - 4ULL), sh->cwd);
pid = cleonos_sys_spawn_pathv(path, argv_line, env_line);
if (pid == (u64)-1) {
ush_writeln("spawn: request failed");
@@ -1573,7 +1629,13 @@ static int ush_cmd_wait(const char *arg) {
}
ush_writeln("wait: exited");
ush_print_kv_hex(" STATUS", status);
if ((status & (1ULL << 63)) != 0ULL) {
ush_print_kv_hex(" SIGNAL", status & 0xFFULL);
ush_print_kv_hex(" VECTOR", (status >> 8) & 0xFFULL);
ush_print_kv_hex(" ERROR", (status >> 16) & 0xFFFFULL);
} else {
ush_print_kv_hex(" STATUS", status);
}
return 1;
}

View File

@@ -13,6 +13,25 @@ static void ush_zero(void *ptr, u64 size) {
}
}
static void ush_append_text(char *dst, u64 dst_size, const char *text) {
u64 p = 0ULL;
u64 i = 0ULL;
if (dst == (char *)0 || dst_size == 0ULL || text == (const char *)0) {
return;
}
while (dst[p] != '\0' && p + 1ULL < dst_size) {
p++;
}
while (text[i] != '\0' && p + 1ULL < dst_size) {
dst[p++] = text[i++];
}
dst[p] = '\0';
}
static const char *ush_alias_command(const char *cmd) {
if (cmd == (const char *)0) {
return (const char *)0;
@@ -121,6 +140,7 @@ int ush_command_ret_read(ush_cmd_ret *out_ret) {
int ush_try_exec_external(ush_state *sh, const char *cmd, const char *arg, int *out_success) {
const char *canonical;
char path[USH_PATH_MAX];
char env_line[USH_PATH_MAX + USH_CMD_MAX + 32ULL];
u64 status;
ush_cmd_ret ret;
@@ -153,7 +173,13 @@ int ush_try_exec_external(ush_state *sh, const char *cmd, const char *arg, int *
return 1;
}
status = cleonos_sys_exec_path(path);
env_line[0] = '\0';
ush_append_text(env_line, (u64)sizeof(env_line), "PWD=");
ush_append_text(env_line, (u64)sizeof(env_line), sh->cwd);
ush_append_text(env_line, (u64)sizeof(env_line), ";CMD=");
ush_append_text(env_line, (u64)sizeof(env_line), canonical);
status = cleonos_sys_exec_pathv(path, arg, env_line);
if (status == (u64)-1) {
ush_writeln("exec: request failed");
@@ -169,7 +195,15 @@ int ush_try_exec_external(ush_state *sh, const char *cmd, const char *arg, int *
(void)cleonos_sys_fs_remove(USH_CMD_RET_PATH);
if (status != 0ULL) {
ush_writeln("exec: returned non-zero status");
if ((status & (1ULL << 63)) != 0ULL) {
ush_writeln("exec: terminated by signal");
ush_print_kv_hex(" SIGNAL", status & 0xFFULL);
ush_print_kv_hex(" VECTOR", (status >> 8) & 0xFFULL);
ush_print_kv_hex(" ERROR", (status >> 16) & 0xFFFFULL);
} else {
ush_writeln("exec: returned non-zero status");
ush_print_kv_hex(" STATUS", status);
}
if (out_success != (int *)0) {
*out_success = 0;

View File

@@ -1,9 +1,28 @@
#include "cmd_runtime.h"
static int ush_cmd_spawn(const ush_state *sh, const char *arg) {
char target[USH_PATH_MAX];
char argv_line[USH_ARG_MAX];
char env_line[USH_PATH_MAX + 32ULL];
const char *rest = "";
char path[USH_PATH_MAX];
u64 pid;
if (ush_resolve_exec_path(sh, arg, path, (u64)sizeof(path)) == 0) {
if (sh == (const ush_state *)0 || arg == (const char *)0 || arg[0] == '\0') {
ush_writeln("spawn: usage spawn <path|name> [args...]");
return 0;
}
if (ush_split_first_and_rest(arg, target, (u64)sizeof(target), &rest) == 0) {
ush_writeln("spawn: usage spawn <path|name> [args...]");
return 0;
}
argv_line[0] = '\0';
if (rest != (const char *)0 && rest[0] != '\0') {
ush_copy(argv_line, (u64)sizeof(argv_line), rest);
}
if (ush_resolve_exec_path(sh, target, path, (u64)sizeof(path)) == 0) {
ush_writeln("spawn: invalid target");
return 0;
}
@@ -13,7 +32,11 @@ static int ush_cmd_spawn(const ush_state *sh, const char *arg) {
return 0;
}
pid = cleonos_sys_spawn_path(path);
env_line[0] = '\0';
ush_copy(env_line, (u64)sizeof(env_line), "PWD=");
ush_copy(env_line + 4, (u64)(sizeof(env_line) - 4ULL), sh->cwd);
pid = cleonos_sys_spawn_pathv(path, argv_line, env_line);
if (pid == (u64)-1) {
ush_writeln("spawn: request failed");

View File

@@ -27,7 +27,13 @@ static int ush_cmd_wait(const char *arg) {
}
ush_writeln("wait: exited");
ush_print_kv_hex(" STATUS", status);
if ((status & (1ULL << 63)) != 0ULL) {
ush_print_kv_hex(" SIGNAL", status & 0xFFULL);
ush_print_kv_hex(" VECTOR", (status >> 8) & 0xFFULL);
ush_print_kv_hex(" ERROR", (status >> 16) & 0xFFFFULL);
} else {
ush_print_kv_hex(" STATUS", status);
}
return 1;
}