1 #include <lunaix/fctrl.h>
2 #include <lunaix/foptions.h>
3 #include <lunaix/ioctl.h>
4 #include <lunaix/lunaix.h>
5 #include <lunaix/lunistd.h>
6 #include <lunaix/signal.h>
7 #include <lunaix/status.h>
9 #include <klibc/string.h>
10 #include <ulibc/stdio.h>
16 Simple shell - (actually this is not even a shell)
17 It just to make the testing more easy.
21 parse_cmdline(char* line, char** cmd, char** arg_part)
24 line = strltrim_safe(line);
27 while ((c = line[l])) {
35 *arg_part = strltrim_safe(line + l);
41 int errno = geterrno();
44 printf("Error: Not a directory\n");
47 printf("Error: No such file or directory\n");
50 printf("Error: Invalid parameter or operation\n");
53 printf("Error: Not supported\n");
56 printf("Error: File system is read only\n");
59 printf("Error: Out of memory\n");
62 printf("Error: This is a directory\n");
65 printf("Error: (%d)\n", errno);
71 sigint_handle(int signum)
77 do_cat(const char* file)
79 int fd = open(file, 0);
84 while ((sz = read(fd, cat_buf, 1024)) > 0) {
85 write(stdout, cat_buf, sz);
96 do_ls(const char* path)
98 int fd = open(path, 0);
102 struct dirent ent = { .d_offset = 0 };
104 while ((status = readdir(fd, &ent)) == 1) {
105 if (ent.d_type == DT_DIR) {
106 printf(" \033[3m%s\033[39;49m\n", ent.d_name);
108 printf(" %s\n", ent.d_name);
125 signal(_SIGINT, sigint_handle);
127 // set our shell as foreground process
128 // (unistd.h:tcsetpgrp is essentially a wrapper of this)
129 // stdout (by default, unless user did smth) is the tty we are currently at
130 ioctl(stdout, TIOCSPGRP, getpgid());
134 printf("[\033[2m%s\033[39;49m]$ ", pwd);
135 size_t sz = read(stdin, buf, 511);
137 printf("fail to read user input (%d)\n", geterrno());
141 parse_cmdline(buf, &cmd, &argpart);
146 if (streq(cmd, "cd")) {
147 if (chdir(argpart) < 0) {
151 } else if (streq(cmd, "clear")) {
152 ioctl(stdout, TIOCCLSBUF);
154 } else if (streq(cmd, "ls")) {
159 } else if (streq(cmd, "cat")) {
165 printf("unknow command\n");
168 setpgid(p, getpgid());
178 printf("\n Simple shell. Use <PG_UP> or <PG_DOWN> to scroll.\n\n");