彻底拆分

This commit is contained in:
2026-04-16 19:29:42 +08:00
parent af4ad36f6a
commit 643b26cfb2
96 changed files with 5239 additions and 1737 deletions

View File

@@ -1,7 +1,5 @@
#include "shell_internal.h"
static int ush_dispatch_external_enabled = 1;
static void ush_zero(void *ptr, u64 size) {
u64 i;
char *bytes = (char *)ptr;
@@ -47,44 +45,6 @@ static const char *ush_alias_command(const char *cmd) {
return cmd;
}
static int ush_build_command_line(const char *cmd, const char *arg, char *out_line, u64 out_size) {
u64 p = 0ULL;
u64 i;
if (cmd == (const char *)0 || out_line == (char *)0 || out_size == 0ULL) {
return 0;
}
out_line[0] = '\0';
for (i = 0ULL; cmd[i] != '\0'; i++) {
if (p + 1ULL >= out_size) {
return 0;
}
out_line[p++] = cmd[i];
}
if (arg != (const char *)0 && arg[0] != '\0') {
if (p + 1ULL >= out_size) {
return 0;
}
out_line[p++] = ' ';
for (i = 0ULL; arg[i] != '\0'; i++) {
if (p + 1ULL >= out_size) {
return 0;
}
out_line[p++] = arg[i];
}
}
out_line[p] = '\0';
return 1;
}
static int ush_cmd_ret_apply(ush_state *sh, const ush_cmd_ret *ret) {
if (sh == (ush_state *)0 || ret == (const ush_cmd_ret *)0) {
return 0;
@@ -102,10 +62,6 @@ static int ush_cmd_ret_apply(ush_state *sh, const ush_cmd_ret *ret) {
return 1;
}
void ush_set_external_dispatch(int enabled) {
ush_dispatch_external_enabled = (enabled != 0) ? 1 : 0;
}
int ush_command_ctx_write(const char *cmd, const char *arg, const char *cwd) {
ush_cmd_ctx ctx;
@@ -176,10 +132,6 @@ int ush_try_exec_external(ush_state *sh, const char *cmd, const char *arg, int *
return 0;
}
if (ush_dispatch_external_enabled == 0) {
return 0;
}
canonical = ush_alias_command(cmd);
if (canonical == (const char *)0) {
@@ -232,66 +184,3 @@ int ush_try_exec_external(ush_state *sh, const char *cmd, const char *arg, int *
return 1;
}
int ush_command_program_main(const char *command_name) {
ush_cmd_ctx ctx;
ush_state sh;
ush_cmd_ret ret;
char line[USH_LINE_MAX];
u64 ok_before;
int success;
int has_context = 0;
int use_ctx_cwd = 0;
if (command_name == (const char *)0 || command_name[0] == '\0') {
return 1;
}
ush_zero(&ctx, (u64)sizeof(ctx));
if (ush_command_ctx_read(&ctx) != 0) {
if (ush_streq(ctx.cmd, command_name) != 0) {
has_context = 1;
use_ctx_cwd = 1;
} else if (ctx.cwd[0] == '/') {
use_ctx_cwd = 1;
ctx.arg[0] = '\0';
}
}
ush_init_state(&sh);
ush_set_external_dispatch(0);
if (use_ctx_cwd != 0 && ctx.cwd[0] == '/') {
ush_copy(sh.cwd, (u64)sizeof(sh.cwd), ctx.cwd);
}
if (ush_build_command_line(command_name, ctx.arg, line, (u64)sizeof(line)) == 0) {
ush_set_external_dispatch(1);
ush_writeln("command line too long");
return 1;
}
ok_before = sh.cmd_ok;
ush_execute_line(&sh, line);
success = (sh.cmd_ok > ok_before) ? 1 : 0;
if (has_context != 0) {
ush_zero(&ret, (u64)sizeof(ret));
if (ush_streq(sh.cwd, ctx.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);
}
ush_set_external_dispatch(1);
return (success != 0) ? 0 : 1;
}