/* Simple shell to run on SOS */ /* Written by Gernot Heiser */ #include #include #include #include #include #include "sos.h" #include "printf.h" extern l4_threadid_t syscall_server_id; /* testing only */ #define BUF_SIZ 128 #define N_STACK 128 #define MAX_PROCESSES 10 static process_t process[MAX_PROCESSES]; static int n_processes = 0; static void ps(int print) { int i; n_processes = process_status(process, MAX_PROCESSES); if (!print) { return; } printf ("TID SIZE STIME CTIME COMMAND\n"); for (i=0; i= 0); bp = buf; done = 0; new = 1; while (!done) { if (new) { printf("$ "); } new = 0; found = 0; while (!found && !done) { r = read(in, bp, BUF_SIZ-1+buf-bp); if (r<0) { printf("Console read failed!\n"); done=1; break; } else if (!r) { continue; } bp[r] = 0; /* terminate */ for (p=bp; pbuf) { printf("\010 \010",*p); p--; r--; } p--; r--; } else if (*p == '\n') { /* ^J */ printf("%c",*p); *p = 0; found = p>buf; p = buf; new = 1; break; } else { printf("%c",*p); } } bp = p; if (bp == buf) { break; } } if (!found) { continue; } p = buf; while (*p == ' ') { p++; } if (!*p) { found = 0; continue; } if (strcmp(p, "dir") == 0 || strcmp(p, "ls") == 0) { dir(0); } else if (strcmp(p, "ps") == 0) { ps(1); } else if (strcmp(p, "exit") == 0 || strcmp(p, "\04") == 0) {/* ^D */ done=1; break; } else if (stat(p, &sbuf) != 0) { printf("Command \"%s\" not found\n", p); } else if (!(sbuf.st_fmode & FM_EXEC)) { printf("File \"%s\" not executable\n", p); } else { r = close(in); assert(!r); tid = process_create(p); if (tid>=0) { printf("Child tid=%d\n", tid); do { ps(0); } while (n_processes>1); } else { printf("Failed!\n"); } in = open("console", FM_READ); assert(in>=0); } new = 1; } printf("Exit\n", buf); }