Skip to content

Commit

Permalink
add CSD$ and CHDIR
Browse files Browse the repository at this point in the history
  • Loading branch information
braindigitalis committed May 2, 2023
1 parent 6673157 commit d3fc16b
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 19 deletions.
45 changes: 29 additions & 16 deletions include/taskswitch.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,22 @@ typedef uint32_t gid_t; // Group ID
typedef uint8_t cpu_id_t; // CPU ID

typedef struct process_t {
pid_t pid; /* Process ID */
pid_t ppid; /* Parent Process ID */
uid_t uid; /* User id - Future use */
gid_t gid; /* Group id - Future use */
process_state_t state; /* Running state */
time_t start_time; /* Start time (UNIX epoch)*/
pid_t waitpid; /* PID we are waiting on compltion of */
cpu_id_t cpu; /* CPU ID */
const char* directory; /* Directory of program */
const char* name; /* Filename of program */
uint64_t size; /* Size of program in bytes */
struct console* cons; /* Program's console */
struct ubasic_ctx* code; /* BASIC context */
struct process_t* prev; /* Prev process in doubly linked list */
struct process_t* next; /* Next process in doubly linked list */
pid_t pid; /* Process ID */
pid_t ppid; /* Parent Process ID */
uid_t uid; /* User id - Future use */
gid_t gid; /* Group id - Future use */
process_state_t state; /* Running state */
time_t start_time; /* Start time (UNIX epoch)*/
pid_t waitpid; /* PID we are waiting on compltion of */
cpu_id_t cpu; /* CPU ID */
const char* directory; /* Directory of program */
const char* name; /* Filename of program */
uint64_t size; /* Size of program in bytes */
const char* csd; /* Current selected directory */
struct console* cons; /* Program's console */
struct ubasic_ctx* code; /* BASIC context */
struct process_t* prev; /* Prev process in doubly linked list */
struct process_t* next; /* Next process in doubly linked list */
} process_t;

/**
Expand Down Expand Up @@ -73,9 +74,10 @@ typedef struct idle_timer {
* @param fullpath fully qualified path to file
* @param cons console
* @param parent_pid parent PID, or 0
* @param csd Currently selected directory
* @return process_t* new process details
*/
process_t* proc_load(const char* fullpath, struct console* cons, pid_t parent_pid);
process_t* proc_load(const char* fullpath, struct console* cons, pid_t parent_pid, const char* csd);

/**
* @brief Find a process by ID
Expand Down Expand Up @@ -171,5 +173,16 @@ pid_t proc_id(int64_t index);
*/
void proc_register_idle(proc_idle_timer_t handler, idle_type_t type);

/**
* @brief Change CSD (currently selected directory) of process
*
* @note No validation of the path is peformed, this must be done
* extnerally to this function by validating the file information on VFS.
* @param proc Process struct
* @param csd current directory
* @return const char* new current directory
*/
const char* proc_set_csd(process_t* proc, const char* csd);

void init_process();

1 change: 1 addition & 0 deletions include/tokenizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
T(PUSH) \
T(POP) \
T(LOCAL) \
T(CHDIR) \
T(MOUNT)

/*
Expand Down
2 changes: 2 additions & 0 deletions include/ubasic.h
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@ char* ubasic_insocket(struct ubasic_ctx* ctx);
char* ubasic_upper(struct ubasic_ctx* ctx);
char* ubasic_lower(struct ubasic_ctx* ctx);
char* ubasic_tokenize(struct ubasic_ctx* ctx);
char* ubasic_csd(struct ubasic_ctx* ctx);

/*
* File I/O functions
Expand All @@ -565,6 +566,7 @@ void mkdir_statement(struct ubasic_ctx* ctx);
void mount_statement(struct ubasic_ctx* ctx);
void rmdir_statement(struct ubasic_ctx* ctx);
void write_statement(struct ubasic_ctx* ctx);
void chdir_statement(struct ubasic_ctx* ctx);

/*
* Builtin real (double) functions
Expand Down
39 changes: 37 additions & 2 deletions src/taskswitch.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ uint32_t process_count = 0;
*/
idle_timer_t* task_idles = NULL, *timer_idles = NULL;

process_t* proc_load(const char* fullpath, struct console* cons, pid_t parent_pid)
process_t* proc_load(const char* fullpath, struct console* cons, pid_t parent_pid, const char* csd)
{
fs_directory_entry_t* fsi = fs_get_file_info(fullpath);
if (fsi != NULL && !(fsi->flags & FS_DIRECTORY)) {
Expand All @@ -61,6 +61,7 @@ process_t* proc_load(const char* fullpath, struct console* cons, pid_t parent_pi
newproc->name = strdup(fsi->filename);
newproc->pid = nextid++;
newproc->directory = strdup(fullpath);
newproc->csd = strdup(csd);
newproc->size = fsi->size;
newproc->start_time = time(NULL);
newproc->state = PROC_RUNNING;
Expand Down Expand Up @@ -145,6 +146,39 @@ void proc_wait(process_t* proc, pid_t otherpid)
proc->waitpid = otherpid;
}

const char* proc_set_csd(process_t* proc, const char* csd)
{
if (!csd || !*csd || !proc) {
return NULL;
}

size_t len = strlen(proc->csd), csdlen = strlen(csd);

if (*csd == '/') {
kfree(proc->csd);
proc->csd = strdup(csd);
dprintf("Process %d CSD set to: '%s'\n", proc->pid, proc->csd);
return proc->csd;
}

proc->csd = krealloc((void*)proc->csd, len + csdlen + 1);
strlcpy((char*)(proc->csd + len), csd, len + csdlen + 1);
char* end = (char*)proc->csd + csdlen;
if (*end == '/') {
*end = 0;
}
dprintf("Process %d CSD set to: '%s'\n", proc->pid, proc->csd);
return proc->csd;
}

const char* proc_get_csd(process_t* proc)
{
if (!proc) {
return "";
}
return proc->csd;
}

void proc_kill(process_t* proc)
{
for (process_t* cur = proc_list; cur; cur = cur->next) {
Expand Down Expand Up @@ -174,6 +208,7 @@ void proc_kill(process_t* proc)
ubasic_destroy(proc->code);
kfree(proc->name);
kfree(proc->directory);
kfree(proc->csd);
hashmap_delete(process_by_pid, &(proc_id_t){ .id = proc->pid });
kfree(proc);
process_count--;
Expand Down Expand Up @@ -239,7 +274,7 @@ int process_compare(const void *a, const void *b, void *udata) {
void init_process()
{
process_by_pid = hashmap_new(sizeof(proc_id_t), 0, 704830503, 487304583058, process_hash, process_compare, NULL, NULL);
process_t* init = proc_load("/programs/init", (struct console*)current_console, 0);
process_t* init = proc_load("/programs/init", (struct console*)current_console, 0, "/");
if (!init) {
preboot_fail("/programs/init missing or invalid!\n");
}
Expand Down
1 change: 1 addition & 0 deletions src/tokenizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const int keywords[] = {
BACKGROUND,
CALL,
CHAIN,
CHDIR,
CIRCLE,
CLOSE,
CLS,
Expand Down
33 changes: 32 additions & 1 deletion src/ubasic.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ struct ubasic_str_fn builtin_str[] =
{ ubasic_upper, "UPPER$" },
{ ubasic_lower, "LOWER$" },
{ ubasic_tokenize, "TOKENIZE$" },
{ ubasic_csd, "CSD$" },
{ NULL, NULL }
};

Expand Down Expand Up @@ -865,7 +866,7 @@ void chain_statement(struct ubasic_ctx* ctx)
{
accept(CHAIN, ctx);
const char* pn = str_expr(ctx);
process_t* p = proc_load(pn, ctx->cons, proc_cur()->pid);
process_t* p = proc_load(pn, ctx->cons, proc_cur()->pid, proc_cur()->csd);
if (p == NULL) {
accept(NEWLINE, ctx);
return;
Expand Down Expand Up @@ -1585,6 +1586,8 @@ void statement(struct ubasic_ctx* ctx)
return push_statement(ctx);
case POP:
return pop_statement(ctx);
case CHDIR:
return chdir_statement(ctx);
case CIRCLE:
return circle_statement(ctx);
case LET:
Expand All @@ -1605,6 +1608,29 @@ void statement(struct ubasic_ctx* ctx)
}
}

void chdir_statement(struct ubasic_ctx* ctx)
{
accept(CHDIR, ctx);
const char* csd = str_expr(ctx);
accept(NEWLINE, ctx);
const char* old = strdup(proc_cur()->csd);
const char* new = proc_set_csd(proc_cur(), csd);
if (new && fs_is_directory(new)) {
kfree(old);
return;
}

if (!new) {
tokenizer_error_print(ctx, "Invalid directory");
}
if (!fs_is_directory(new)) {
tokenizer_error_print(ctx, "Not a directory");
}

proc_set_csd(proc_cur(), old);
kfree(old);
}

void line_statement(struct ubasic_ctx* ctx)
{
ctx->current_linenum = tokenizer_num(ctx, NUMBER);
Expand Down Expand Up @@ -2265,6 +2291,11 @@ char* ubasic_lower(struct ubasic_ctx* ctx)
return modified;
}

char* ubasic_csd(struct ubasic_ctx* ctx)
{
return gc_strdup(proc_cur()->csd);
}

char* ubasic_tokenize(struct ubasic_ctx* ctx)
{
char* varname, *split;
Expand Down

0 comments on commit d3fc16b

Please sign in to comment.