From 7a88322f19016606cf0b370433a816b224e6c761 Mon Sep 17 00:00:00 2001 From: g1n Date: Tue, 28 Sep 2021 09:34:58 +0300 Subject: [PATCH] Add prompt generating (now should be less buggy) --- src/main.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/main.c b/src/main.c index 9992ac0..df70b63 100644 --- a/src/main.c +++ b/src/main.c @@ -17,6 +17,7 @@ char *username; // Current username char *homedir; // HOME directory of current user pid_t shell_pid; // pid of current shell (gets only on start) pid_t pid; // FIXME: this shouldn't be global i think +char prompt[2048]; char **split_line(char *line, char *delim) { int bufsize = 64; @@ -146,23 +147,19 @@ void init() { // Some initial tasks shell_pid = getpid(); } -void print_ps() { +void gen_prompt() { // TODO: add parsing config file - // FIXME: add proper PS1 (this is commented because of readline currently) - /* if (!strcmp(pwd, homedir)) { - printf("%s@%s %s> ", username, hostname, "~"); + sprintf(prompt, "%s@%s %s> ", username, hostname, "~"); } else { - printf("%s@%s %s> ", username, hostname, basename(pwd)); - }*/ - printf("orsh> "); + sprintf(prompt, "%s@%s %s> ", username, hostname, basename(pwd)); + } } void signal_handler(int sig) { - if (pid == 0) { + if (pid == 0 || shell_pid == getpid()) { printf("\n"); - print_ps(); - } else if (shell_pid == getpid()) { // Do nothing because readline prints the prompt + printf("%s", prompt); } else raise(sig); @@ -190,7 +187,8 @@ int main(int argc, char *argv[]) { } else { while(1) { - line = readline("orsh> "); // FIXME: add prompt + gen_prompt(); + line = readline(prompt); // FIXME: add prompt if (line == NULL) { printf("exit\n"); break;