Adding support for multiple PATH

This commit is contained in:
Lluciocc 2026-04-20 10:09:39 +02:00
parent 0e32f35d91
commit 750880dcb5

View file

@ -430,30 +430,70 @@ static void get_tab_title(TerminalSession *s, char *out, int max_len) {
static int read_config_value(const char *key, char *out, int max_len) { static int read_config_value(const char *key, char *out, int max_len) {
if (!key || !out || max_len <= 0) return -1; if (!key || !out || max_len <= 0) return -1;
int fd = sys_open("/Library/bsh/bshrc", "r"); int fd = sys_open("/Library/bsh/bshrc", "r");
if (fd < 0) return -1; if (fd < 0) return -1;
char buf[4096]; char buf[4096];
int bytes = sys_read(fd, buf, sizeof(buf) - 1); int bytes = sys_read(fd, buf, sizeof(buf) - 1);
sys_close(fd); sys_close(fd);
if (bytes <= 0) return -1; if (bytes <= 0) return -1;
buf[bytes] = 0; buf[bytes] = 0;
char *line = buf; char *line = buf;
while (*line) { while (*line) {
char *end = line; char *end = line;
while (*end && *end != '\n' && *end != '\r') end++; while (*end && *end != '\n' && *end != '\r') end++;
char saved = *end; char saved = *end;
*end = 0; *end = 0;
trim_end(line); trim_end(line);
if (line[0] != '#' && line[0] != 0) { if (line[0] != '#' && line[0] != 0) {
char *sep = line; char *sep = line;
while (*sep && *sep != '=') sep++; while (*sep && *sep != '=') sep++;
if (*sep == '=') { if (*sep == '=') {
*sep = 0; *sep = 0;
if (strcmp(line, key) == 0) {
str_copy(out, sep + 1, max_len); char *k = line;
// skip leading spaces
while (*k == ' ' || *k == '\t') k++;
// skip "export "
if (strncmp(k, "export ", 7) == 0) {
k += 7;
}
// skip spaces again after export
while (*k == ' ' || *k == '\t') k++;
// trim end of key
char *kend = k + strlen(k) - 1;
while (kend > k && (*kend == ' ' || *kend == '\t')) {
*kend = 0;
kend--;
}
if (strcmp(k, key) == 0) {
char *val = sep + 1;
// skip leading spaces
while (*val == ' ' || *val == '\t') val++;
// remove "
if (*val == '"') {
val++;
char *vend = strchr(val, '"');
if (vend) *vend = 0;
}
str_copy(out, val, max_len);
return 0; return 0;
} }
} }
@ -826,24 +866,69 @@ static int create_tab(void) {
return g_tab_count - 1; return g_tab_count - 1;
} }
// checks if a command exist in /bin static int parse_path(char paths[][128], int max_paths) {
char path_line[256];
if (read_config_value("PATH", path_line, sizeof(path_line)) != 0) {
str_copy(path_line, "/bin", sizeof(path_line));
}
if (path_line[0] == '"') {
memmove(path_line, path_line + 1, strlen(path_line));
char *end = strchr(path_line, '"');
if (end) *end = 0;
}
int count = 0;
int start = 0;
for (int i = 0;; i++) {
if (path_line[i] == ':' || path_line[i] == ';' || path_line[i] == 0) {
int len = i - start;
if (len > 0 && count < max_paths) {
if (len >= 128) len = 127;
memcpy(paths[count], &path_line[start], len);
paths[count][len] = 0;
count++;
}
start = i + 1;
}
if (path_line[i] == 0) break;
}
return count;
}
static bool command_exists(const char *cmd) { static bool command_exists(const char *cmd) {
char path[256]; if (!cmd || !cmd[0]) return false;
// test /bin/cmd
path[0] = 0;
str_append(path, "/bin/", sizeof(path));
str_append(path, cmd, sizeof(path));
if (sys_exists(path)) return true; char paths[16][128];
int path_count = parse_path(paths, 16);
// test /bin/cmd.elf char full[256];
path[0] = 0;
str_append(path, "/bin/", sizeof(path));
str_append(path, cmd, sizeof(path));
str_append(path, ".elf", sizeof(path));
if (sys_exists(path)) return true; for (int i = 0; i < path_count; i++) {
// folder/cmd
full[0] = 0;
str_append(full, paths[i], sizeof(full));
if (full[strlen(full) - 1] != '/') str_append(full, "/", sizeof(full));
str_append(full, cmd, sizeof(full));
if (sys_exists(full)) return true;
// folder/cmd.elf
full[0] = 0;
str_append(full, paths[i], sizeof(full));
if (full[strlen(full) - 1] != '/') str_append(full, "/", sizeof(full));
str_append(full, cmd, sizeof(full));
str_append(full, ".elf", sizeof(full));
if (sys_exists(full)) return true;
}
return false; return false;
} }
@ -851,30 +936,36 @@ static bool command_exists(const char *cmd) {
static bool command_starts_with(const char *prefix) { static bool command_starts_with(const char *prefix) {
if (!prefix || !prefix[0]) return false; if (!prefix || !prefix[0]) return false;
char paths[16][128];
int path_count = parse_path(paths, 16);
FAT32_FileInfo entries[128]; FAT32_FileInfo entries[128];
int count = sys_list("/bin", entries, 128);
if (count <= 0) return false;
for (int i = 0; i < count; i++) { for (int p = 0; p < path_count; p++) {
if (entries[i].is_directory) { int count = sys_list(paths[p], entries, 128);
continue; if (count <= 0) continue;
}
char name[256]; for (int i = 0; i < count; i++) {
str_copy(name, entries[i].name, sizeof(name)); if (entries[i].is_directory) continue;
int len = (int)strlen(name); char name[256];
if (len > 4 && strcmp(name + len - 4, ".elf") == 0) { str_copy(name, entries[i].name, sizeof(name));
name[len - 4] = 0;
}
int j = 0; int len = strlen(name);
while (prefix[j] && name[j] && prefix[j] == name[j]) {
j++;
}
if (prefix[j] == 0) { // remove .elf
return true; if (len > 4 && strcmp(name + len - 4, ".elf") == 0) {
name[len - 4] = 0;
}
int j = 0;
while (prefix[j] && name[j] && prefix[j] == name[j]) {
j++;
}
if (prefix[j] == 0) {
return true;
}
} }
} }