mylsh

my own shell, based on brenns10's
Log | Files | Refs | Feed

commit 965d60a7ba1b3e2c75723e507e6494738bdefa68
parent d5dd8b2c74c84fb25700123a891dffbb1e9ea0a3
Author: Jenny Doe <tng@soykaf.me>
Date:   Tue, 19 Mar 2019 19:28:31 +0100

added: exit and a bunch of tweaks

Diffstat:
Mlsh.c | 89+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------
1 file changed, 69 insertions(+), 20 deletions(-)

diff --git a/lsh.c b/lsh.c @@ -30,6 +30,8 @@ wchar_t **lsh_split_line(wchar_t *); int lsh_launch(wchar_t * *); int lsh_execute(wchar_t * *); +int lsh_loop(void); + void lsh_siginfo(int); /* @@ -55,11 +57,14 @@ lsh_execute(wchar_t * *words) #define sc(s) wcscmp(words[0], s) == 0 if (sc(L"echo")) { - for (int i = 1; words[i] != NULL; i++) - printf("%ls\n", words[i]); - - if (words[1] == NULL) /* to mimick classic 'echo' */ - putchar('\n'); + int i = 1; + while (words[i] != NULL) { + printf("%ls", words[i]); + if (words[i + 1] != NULL) + putchar(' '); + ++i; + } + putchar('\n'); } else if (sc(L"pwd")) { if (words[1] != NULL) @@ -77,17 +82,53 @@ lsh_execute(wchar_t * *words) fprintf(stderr, "cd: too much arguments!\n"); } else { - syslog(LOG_DEBUG, "chdir to %ls", words[1]); + /* + * i clearly don't know what size it should be, any + * hint would be appreciated: mailto tng@soykaf.me + */ + size_t l = 2 + 2 * sizeof(char) * wcslen(words[1]); + char *t = malloc(l); + if (t == NULL) { + fprintf(stderr, "out of memory\n"); + exit(1); + } - char *t = malloc(sizeof(char) * wcslen(words[1])); + wcsrtombs(&t[0], &words[1], l, NULL); + syslog(LOG_DEBUG, "chdir to %s\n", t); - wcsrtombs(&t[0], &words[1], sizeof(t), NULL); if (chdir(t) == -1) perror("cd"); free(t); } + } else if (sc(L"exit")) { + + if (words[1] == NULL) + fprintf(stderr, "cd: missing argument!\n"); + else if (words[2] != NULL) { + fprintf(stderr, "cd: too much arguments!\n"); + + } else { + int l = 2 + 2 * sizeof(char) * wcslen(words[1]); + char *t = malloc(l); + if (t == NULL) { + fprintf(stderr, "out of memory\n"); + exit(1); + } + + wcsrtombs(&t[0], &words[1], l, NULL); + int code = atoi(t); + + free(t); + + if (code == 0) + fprintf(stderr, "exit: use something else than 0\n"); + else + syslog(LOG_DEBUG, "exiting with code %d", code); + return (code); + } + } else { fprintf(stderr, "command not found!\n"); } @@ -99,19 +140,19 @@ lsh_execute(wchar_t * *words) wchar_t * lsh_read_line(void) { - wchar_t *line = malloc(sizeof(wchar_t) * 1); - if (line == NULL) { - fprintf(stderr, "out of memory"); - exit(1); - } - wchar_t c; int bufsize = 1; int pos = 0; if (feof(stdin)) { putchar('\n'); - exit(0); + return NULL; + } + + wchar_t *line = malloc(sizeof(wchar_t) * 1); + if (line == NULL) { + fprintf(stderr, "out of memory"); + exit(1); } if (isatty(fileno(stdin))) @@ -160,7 +201,6 @@ lsh_split_line(wchar_t * line) words[pos] = word; if (word == NULL) break; - //fprintf(stderr, "%ls\n", word); ++pos; if (pos >= bufsize) { bufsize += 10; @@ -178,7 +218,7 @@ lsh_split_line(wchar_t * line) return words; } -void +int lsh_loop(void) { wchar_t *line; @@ -186,7 +226,11 @@ lsh_loop(void) int status; while (1) { + line = lsh_read_line(); + if (line == NULL) + return (0); + words = lsh_split_line(line); status = lsh_execute(words); @@ -194,13 +238,15 @@ lsh_loop(void) free(words); if (status != 0) - break; + return status; } + + return (0); } int -main(void) +main(int argc, char *argv[]) { (void)setlocale(LC_ALL, ""); @@ -217,7 +263,10 @@ main(void) syslog(LOG_DEBUG, "just started!"); - lsh_loop(); + int status = lsh_loop(); + syslog(LOG_DEBUG, "main loop returned %d", status); + syslog(LOG_DEBUG, "exiting with code 0"); + /* return status; */ return (0); }