1 #include <lunaix/ioctl.h>
2 #include <lunaix/status.h>
5 #include <usr/signal.h>
6 #include <usr/sys/dirent.h>
7 #include <usr/sys/lunaix.h>
8 #include <usr/sys/mann.h>
9 #include <usr/unistd.h>
11 #include <klibc/string.h>
12 #include <ulibc/stdio.h>
18 Simple shell - (actually this is not even a shell)
19 It just to make the testing more easy.
23 parse_cmdline(char* line, char** cmd, char** arg_part)
26 line = strltrim_safe(line);
29 while ((c = line[l])) {
37 *arg_part = strltrim_safe(line + l);
43 int errno = geterrno();
46 printf("Error: Not a directory\n");
49 printf("Error: No such file or directory\n");
52 printf("Error: Invalid parameter or operation\n");
55 printf("Error: Not supported\n");
58 printf("Error: File system is read only\n");
61 printf("Error: Out of memory\n");
64 printf("Error: This is a directory\n");
67 printf("Error: (%d)\n", errno);
73 sigint_handle(int signum)
79 do_cat(const char* file)
81 int fd = open(file, 0);
86 while ((sz = read(fd, cat_buf, 1024)) > 0) {
87 write(stdout, cat_buf, sz);
98 do_ls(const char* path)
100 int fd = open(path, 0);
104 struct lx_dirent ent = { .d_offset = 0 };
106 while ((status = sys_readdir(fd, &ent)) == 1) {
107 if (ent.d_type == DT_DIR) {
108 printf(" \033[3m%s\033[39;49m\n", ent.d_name);
110 printf(" %s\n", ent.d_name);
122 do_mcat(const char* file)
124 int fd = open(file, 0);
128 void* p = mmap(NULL, 2048, 0, 0, fd, 0);
146 signal(SIGINT, sigint_handle);
148 // set our shell as foreground process
149 // (unistd.h:tcsetpgrp is essentially a wrapper of this)
150 // stdout (by default, unless user did smth) is the tty we are currently at
151 ioctl(stdout, TIOCSPGRP, getpgid());
155 printf("[\033[2m%s\033[39;49m]$ ", pwd);
156 size_t sz = read(stdin, buf, 511);
158 printf("fail to read user input (%d)\n", geterrno());
162 parse_cmdline(buf, &cmd, &argpart);
167 if (streq(cmd, "cd")) {
168 if (chdir(argpart) < 0) {
172 } else if (streq(cmd, "clear")) {
173 ioctl(stdout, TIOCCLSBUF);
175 } else if (streq(cmd, "ls")) {
180 } else if (streq(cmd, "cat")) {
185 } else if (streq(cmd, "mcat")) {
191 printf("unknow command\n");
194 setpgid(p, getpgid());
204 printf("\n Simple shell. Use <PG_UP> or <PG_DOWN> to scroll.\n\n");