2 #include <lunaix/ioctl.h>
3 #include <lunaix/lunaix.h>
13 Simple shell - (actually this is not even a shell)
14 It just to make the testing more easy.
18 (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\v' || c == '\r')
23 size_t l = strlen(str);
24 while (l < (size_t)-1) {
26 if (!c || WS_CHAR(c)) {
36 strltrim_safe(char* str)
40 while ((c = str[l]) && WS_CHAR(c)) {
46 return strcpy(str, str + l);
50 parse_cmdline(char* line, char** cmd, char** arg_part)
53 line = strltrim_safe(line);
56 while ((c = line[l])) {
65 *arg_part = strltrim_safe(line + l);
78 printf("Error: Not a directory\n");
81 printf("Error: No such file or directory\n");
84 printf("Error: Invalid parameter or operation\n");
87 printf("Error: Not supported\n");
90 printf("Error: File system is read only\n");
93 printf("Error: Out of memory\n");
96 printf("Error: This is a directory\n");
99 printf("Error: (%d)\n", errno);
105 sigint_handle(int signum)
111 sh_exec(const char* name, const char** argv)
113 if (!strcmp(name, "cd")) {
121 if (execve(name, argv, NULL)) {
126 setpgid(p, getpgid());
135 signal(SIGINT, sigint_handle);
137 // set our shell as foreground process
138 // (unistd.h:tcsetpgrp is essentially a wrapper of this)
139 // stdout (by default, unless user did smth) is the tty we are currently at
140 ioctl(stdout, TIOCSPGRP, getpgid());
142 char* argv[] = { 0, 0 };
146 printf("[\033[2m%s\033[39;49m]$ ", pwd);
147 int sz = read(stdin, buf, 511);
150 printf("fail to read user input (%d)\n", geterrno());
156 // currently, this shell only support single argument
157 parse_cmdline(buf, &cmd, &argv[0]);
165 if (*(unsigned int*)cmd == 0x74697865U) {
169 sh_exec(cmd, (const char**)&argv);
178 printf("\n Simple shell. Use <PG_UP> or <PG_DOWN> to scroll.\n\n");