diff options
author | Singustromo <singustromo@disroot.org> | 2024-05-19 18:51:28 +0200 |
---|---|---|
committer | Singustromo <singustromo@disroot.org> | 2024-05-19 18:51:28 +0200 |
commit | ae433f7515f4503e75a9be673b7c194e7101d552 (patch) | |
tree | 83f8dd95f897d79018a0f76b3c4f01e51d1d7cd1 | |
parent | 49ff6cdff74cb46d2cd585ee6c97a3bba8262a86 (diff) | |
download | operating-systems_3_clash-ae433f7515f4503e75a9be673b7c194e7101d552.tar.gz operating-systems_3_clash-ae433f7515f4503e75a9be673b7c194e7101d552.zip |
minor changes
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | src/clash.c | 50 | ||||
-rw-r--r-- | src/plist.c | 2 |
3 files changed, 31 insertions, 23 deletions
diff --git a/Makefile b/Makefile index 9e6f02c..182b8b8 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ VPATH=src # search for a prerequisite in those directories (delimiter is :) BUILD_TARGET=clash BUILD_DIR=bin -OBJS = $(patsubst %.o, $(BUILD_DIR)/%.o, plist.o strictmem.o $(BUILD_TARGET).o) +OBJS = $(patsubst %.o, $(BUILD_DIR)/%.o, plist.o $(BUILD_TARGET).o) $(BUILD_TARGET): $(OBJS) $(CC) -o $@ $(CFLAGS) $(OBJS) diff --git a/src/clash.c b/src/clash.c index 705ca2a..be1bd2f 100644 --- a/src/clash.c +++ b/src/clash.c @@ -10,53 +10,60 @@ #include "plist.h" #define PROMPTSIZE 1337 -#define MAX_PARAMETERS 50 -static int getline_skew(char *buffer, int limit); +#define TOKEN_BUFFERSIZE 50 +#define TOKEN_DELIMITER "\n\t " // do not include new line + +static int getline_skew(char *buffer, size_t limit); static bool update_current_path(char *path); static void print_prompt(char *current_path, int child_exitcode, int job_count); -static int parse_commands(char *prompt, char *parameters[]); +static int tokenize_line(char *prompt, char *parameters[]); int main(int argc, char *argv[]) { char current_path[PATH_MAX]; char prompt[PROMPTSIZE]; - if (! update_current_path(current_path)) - exit(EXIT_FAILURE); + char *args[TOKEN_BUFFERSIZE]; + int args_length, args_token_count; + args_length = args_token_count = 0; - int prompt_length, cmdline_arg_count; - prompt_length = cmdline_arg_count = 0; - - char *cmdline[MAX_PARAMETERS]; int background_jobs = 0; + if (! update_current_path(current_path)) + exit(EXIT_FAILURE); + print_prompt(current_path, 0, 0); - while ((prompt_length = getline_skew(prompt, PROMPTSIZE)) > 0) { - if (prompt_length == 1) { + while ((args_length = getline_skew(prompt, PROMPTSIZE)) > 0) { + if (args_length == 1) { print_prompt(current_path, 0, 0); continue; - } else if (prompt_length > PROMPTSIZE) { + } else if (args_length > PROMPTSIZE) { fprintf(stderr, "[!] input line is too long!\n"); print_prompt(current_path, 0, 0); continue; } - if ((cmdline_arg_count = parse_commands(prompt, cmdline)) < 1) { + if ((args_token_count = tokenize_line(prompt, args)) < 1) { printf("[!] "); print_prompt(current_path, 0, 0); continue; } +//////////////////////////////// Execution //////////////////////////////////// +/* + * 1. int execute_command(char *args) + * 2. void collect_background_jobs(void) + */ bool background_job = false; // Determine, if it should be executed in the background - if (strcmp(cmdline[cmdline_arg_count -1], "&") == 0) { // we only check last token - cmdline[cmdline_arg_count -1] = NULL; // for exec + if (strcmp(args[args_token_count -1], "&") == 0) { // we only check last token + args[args_token_count -1] = NULL; // don't include that in call background_job = true; } else - cmdline[cmdline_arg_count] = NULL; + args[args_token_count] = NULL; int wstatus = 0; // status information of child process pid_t child_pid = fork(); @@ -65,7 +72,7 @@ int main(int argc, char *argv[]) { print_prompt(current_path, 0, background_jobs); continue; } else if (child_pid == 0) // child process - if (execvp(cmdline[0], cmdline) == -1) // command failed? + if (execvp(args[0], args) == -1) // command failed? exit(EXIT_FAILURE); if (! background_job) { @@ -76,6 +83,7 @@ int main(int argc, char *argv[]) { insertElement(child_pid, prompt); printf("[%d] %d\n", ++background_jobs, child_pid); } +/////////////////////////////////////////////////////////////////////////////// print_prompt(current_path, WEXITSTATUS(wstatus), background_jobs); } @@ -89,18 +97,18 @@ int main(int argc, char *argv[]) { * * @returns token count (0 on error) */ -static int parse_commands(char *prompt, char *parameters[]) { +static int tokenize_line(char *prompt, char *parameters[]) { char *previous_token, *current_token; int token_index = 0; - char *tmp = strtok(prompt, "\n\t "); // do not include new line + char *tmp = strtok(prompt, TOKEN_DELIMITER); if (! tmp) return token_index; // according to specification argv[0] parameters[token_index++] = tmp; previous_token = prompt; - while ((current_token = strtok(NULL, "\n\t ")) != NULL) { + while ((current_token = strtok(NULL, TOKEN_DELIMITER)) != NULL) { if (previous_token > prompt) parameters[token_index++] = previous_token; @@ -136,7 +144,7 @@ static bool update_current_path(char *path) { * * @returns character count (counts new line) */ -static int getline_skew(char *buffer, int limit) { +static int getline_skew(char *buffer, size_t limit) { int c, charcount; for (charcount = 0; (c = getchar()) != EOF && c != '\n'; ++charcount) diff --git a/src/plist.c b/src/plist.c index 9a60330..2e3af2a 100644 --- a/src/plist.c +++ b/src/plist.c @@ -20,7 +20,7 @@ static struct queue_element *head; void walkList(bool (*callback) (pid_t, const char *)) { struct queue_element* current = head; - while (current && (callback(current->pid, current->cmdLine) == false)) + while (current && (! callback(current->pid, current->cmdLine))) current = current->next; } |