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** args)
53 line = strltrim_safe(line);
56 while ((c = line[l])) {
66 args[1] = strltrim_safe(line + l);
81 printf("Error: Not a directory\n");
84 printf("Error: No such file or directory\n");
87 printf("Error: Invalid parameter or operation\n");
90 printf("Error: Not supported\n");
93 printf("Error: File system is read only\n");
96 printf("Error: Out of memory\n");
99 printf("Error: This is a directory\n");
102 printf("Error: (%d)\n", errno);
108 sigint_handle(int signum)
114 sh_exec(const char** argv)
116 static int prev_exit;
117 const char* envp[] = { 0 };
118 char* name = argv[0];
119 if (!strcmp(name, "cd")) {
120 chdir(argv[1] ? argv[1] : ".");
125 if (!strcmp(name, "?")) {
126 printf("%d\n", prev_exit);
131 strcpy(buffer, "/bin/");
132 strcpy(&buffer[5], name);
137 if (execve(buffer, argv, envp)) {
142 setpgid(p, getpgid());
145 prev_exit = WEXITSTATUS(res);
149 sanity_filter(char* buf)
156 if ((32 <= c && c <= 126) || !c) {
174 signal(SIGINT, sigint_handle);
176 // set our shell as foreground process
177 // (unistd.h:tcsetpgrp is essentially a wrapper of this)
178 // stdout (by default, unless user did smth) is the tty we are currently at
179 ioctl(stdout, TIOCSPGRP, getpgid());
181 char* argv[] = {0, 0, 0};
185 printf("[%s]$ ", pwd);
186 int sz = read(stdin, buf, 511);
189 printf("fail to read user input (%d)\n", geterrno());
196 // currently, this shell only support single argument
197 if (!parse_cmdline(buf, argv)) {
203 if (*(unsigned int*)argv[0] == 0x74697865U) {
207 sh_exec((const char**)argv);