mylsh

my own shell, based on brenns10's
git clone https://github.com/jennydoe/mylsh.git
Log | Files | Refs | Feed | README

commit 3ef8fae1d95a894618ac14b12882992cc6c98496
parent 4b7efd2201446b196f864b07cf780e2e7c2adce5
Author: Jenny Doe <tng@soykaf.me>
Date:   Wed, 20 Mar 2019 15:07:56 +0100

changed: now using bitfields because it's cool and tweaked return values

Diffstat:
Mlsh.c | 49+++++++++++++++++++++++++------------------------
1 file changed, 25 insertions(+), 24 deletions(-)

diff --git a/lsh.c b/lsh.c @@ -23,8 +23,8 @@ #include <locale.h> typedef struct lsh_error_ { - int command_status; - int flag; /* 0: success, 1: oom, 2: args, 3: exit */ + unsigned int command_status:6; /* 0 to 63 */ + unsigned int flag:2; /* 0: success, 1: oom, 2: args, 3: exit */ } lsh_error; const wchar_t prompt[] = L"λ "; @@ -35,7 +35,7 @@ wchar_t **lsh_split_line(wchar_t *); int lsh_launch(wchar_t * *); lsh_error lsh_execute(wchar_t * *); -int lsh_loop(void); +lsh_error lsh_loop(void); void lsh_siginfo(int); @@ -110,7 +110,7 @@ lsh_execute(wchar_t * *words) { syslog(LOG_DEBUG, "chdir to %s\n", t); if (chdir(t) == -1) { - status.command_status = -1; + status.command_status = 1; perror("cd"); } @@ -156,7 +156,7 @@ end: syslog(LOG_DEBUG, "exiting with code %d", status.command_status); syslog(LOG_DEBUG, "setting status flag to %d", status.flag); - return (status); + return status; } wchar_t * @@ -243,19 +243,17 @@ lsh_split_line(wchar_t * line) return words; } -int -lsh_loop(void) -{ +lsh_error +lsh_loop(void){ wchar_t *line; wchar_t **words; - lsh_error status; - int last_status = 0; + lsh_error status = {0,0}; while (1) { line = lsh_read_line(); if (line == NULL) - return (0); + return status; words = lsh_split_line(line); status = lsh_execute(words); @@ -263,20 +261,21 @@ lsh_loop(void) free(line); free(words); - last_status = status.command_status; - - switch (status.flag){ - case 0: /* everything when right */ - case 1: /* out of memory (for builtins) */ - case 2: /* bad argument count (for builtins) */ + switch (status.flag) { + case 0: /* everything when right */ + case 1: /* out of memory (for builtins) */ + case 2: /* bad argument count (for builtins) */ break; - case 3: /* set by exit */ - return last_status; + case 3: /* set by exit */ + return status; } + status.command_status = 0; + status.flag = 0; + } - return (0); + return status; } int @@ -297,10 +296,12 @@ main(int argc, char *argv[]) syslog(LOG_DEBUG, "just started!"); - int status = lsh_loop(); - syslog(LOG_DEBUG, "main loop returned %d", status); - syslog(LOG_DEBUG, "exiting lsh with code %d", status); + lsh_error status = lsh_loop(); + syslog(LOG_DEBUG, "main loop returned %d", + status.command_status); + syslog(LOG_DEBUG, "exiting lsh with code %d", + status.command_status); /* return status; */ - return (status); + return status.command_status; }