12 Simple shell - (actually this is not even a shell)
13 It just to make the testing more easy.
17 (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\v' || c == '\r')
22 size_t l = strlen(str);
23 while (l < (size_t)-1) {
25 if (!c || WS_CHAR(c)) {
35 strltrim_safe(char* str)
39 while ((c = str[l]) && WS_CHAR(c)) {
45 return strcpy(str, str + l);
49 parse_cmdline(char* line, char** cmd, char** arg_part)
52 line = strltrim_safe(line);
55 while ((c = line[l])) {
64 *arg_part = strltrim_safe(line + l);
77 printf("Error: Not a directory\n");
80 printf("Error: No such file or directory\n");
83 printf("Error: Invalid parameter or operation\n");
86 printf("Error: Not supported\n");
89 printf("Error: File system is read only\n");
92 printf("Error: Out of memory\n");
95 printf("Error: This is a directory\n");
98 printf("Error: (%d)\n", errno);
104 sigint_handle(int signum)
110 sh_exec(const char* name, const char** argv)
112 if (!strcmp(name, "cd")) {
120 if (execve(name, argv, NULL)) {
125 setpgid(p, getpgid());
134 signal(SIGINT, sigint_handle);
136 // set our shell as foreground process
137 // (unistd.h:tcsetpgrp is essentially a wrapper of this)
138 // stdout (by default, unless user did smth) is the tty we are currently at
139 ioctl(stdout, TIOCSPGRP, getpgid());
141 char* argv[] = { 0, 0 };
145 printf("[\033[2m%s\033[39;49m]$ ", pwd);
146 size_t sz = read(stdin, buf, 511);
149 printf("fail to read user input (%d)\n", geterrno());
155 // currently, this shell only support single argument
156 parse_cmdline(buf, &cmd, &argv[0]);
164 if (*(unsigned int*)cmd == 0x74697865U) {
168 sh_exec(cmd, (const char**)&argv);
177 printf("\n Simple shell. Use <PG_UP> or <PG_DOWN> to scroll.\n\n");