From 6c805191d0392eb9e7376294b87993613618b5e9 Mon Sep 17 00:00:00 2001 From: Santiago Lo Coco Date: Wed, 27 Oct 2021 17:12:01 -0300 Subject: [PATCH] Add cat, filter and wc Co-authored-by: Ezequiel Bellver Co-authored-by: Juan Barmasch --- Kernel/drivers/keyboard.c | 4 +- Kernel/drivers/video.c | 57 ++++--- Kernel/include/pipeLib.h | 4 +- Kernel/include/schedulerLib.h | 2 +- Kernel/include/video.h | 1 + Kernel/interruptions/systemCalls.c | 10 +- Kernel/interruptions/systemCallsDispatcher.c | 9 +- Kernel/kernel.c | 4 +- Kernel/utils/memManagerFRT4.c | 4 +- Kernel/utils/pipe.c | 19 +-- Kernel/utils/scheduler.c | 67 +++----- Userland/SampleCodeModule/asm/libasm.asm | 26 ++- Userland/SampleCodeModule/include/system.h | 4 +- Userland/SampleCodeModule/libc.c | 9 ++ Userland/SampleCodeModule/sampleCodeModule.c | 5 +- .../SampleCodeModule/shell/commands/cat.c | 17 ++ .../SampleCodeModule/shell/commands/clear.c | 3 +- .../SampleCodeModule/shell/commands/cpuid.c | 3 +- .../SampleCodeModule/shell/commands/excDiv.c | 2 +- .../SampleCodeModule/shell/commands/excOP.c | 2 +- .../SampleCodeModule/shell/commands/filter.c | 22 +++ .../SampleCodeModule/shell/commands/help.c | 3 +- .../SampleCodeModule/shell/commands/inforeg.c | 3 +- .../shell/commands/printmem.c | 10 +- Userland/SampleCodeModule/shell/commands/ps.c | 4 +- .../shell/commands/quadratic.c | 10 +- .../SampleCodeModule/shell/commands/semCom.c | 4 +- .../SampleCodeModule/shell/commands/time.c | 2 +- Userland/SampleCodeModule/shell/commands/wc.c | 18 +++ Userland/SampleCodeModule/shell/include/cat.h | 9 ++ .../SampleCodeModule/shell/include/clear.h | 2 +- .../SampleCodeModule/shell/include/cpu_id.h | 2 +- .../SampleCodeModule/shell/include/excDiv.h | 2 +- .../SampleCodeModule/shell/include/excOP.h | 2 +- .../SampleCodeModule/shell/include/filter.h | 9 ++ .../SampleCodeModule/shell/include/help.h | 2 +- .../SampleCodeModule/shell/include/inforeg.h | 2 +- .../SampleCodeModule/shell/include/printmem.h | 2 +- Userland/SampleCodeModule/shell/include/ps.h | 2 +- .../shell/include/quadratic.h | 2 +- .../SampleCodeModule/shell/include/semCom.h | 2 +- .../SampleCodeModule/shell/include/time.h | 2 +- Userland/SampleCodeModule/shell/include/wc.h | 9 ++ Userland/SampleCodeModule/shell/shell.c | 153 ++++++++++++------ 44 files changed, 353 insertions(+), 177 deletions(-) create mode 100644 Userland/SampleCodeModule/shell/commands/cat.c create mode 100644 Userland/SampleCodeModule/shell/commands/filter.c create mode 100644 Userland/SampleCodeModule/shell/commands/wc.c create mode 100644 Userland/SampleCodeModule/shell/include/cat.h create mode 100644 Userland/SampleCodeModule/shell/include/filter.h create mode 100644 Userland/SampleCodeModule/shell/include/wc.h diff --git a/Kernel/drivers/keyboard.c b/Kernel/drivers/keyboard.c index 9624187..90d88af 100644 --- a/Kernel/drivers/keyboard.c +++ b/Kernel/drivers/keyboard.c @@ -72,7 +72,7 @@ void testKeyboardInterrupt(unsigned char c) { unsigned char getKeyFromBuffer() { if (current - buffer == SIZE) { current = buffer; - } + } char aux = *current; *current++ = 0; @@ -212,7 +212,7 @@ static unsigned char kbdus_sh[250] = 0, /* 29 - Control */ 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', /* 39 */ '\'', '>', 0, /* Left shift */ - '*', 'Z', 'X', 'C', 'V', 'B', 'N', /* 49 */ + '|', 'Z', 'X', 'C', 'V', 'B', 'N', /* 49 */ 'M', ';', ':', '_', 0, /* Right shift */ '*', 0, /* Alt */ diff --git a/Kernel/drivers/video.c b/Kernel/drivers/video.c index 49ba6ae..e40d27c 100644 --- a/Kernel/drivers/video.c +++ b/Kernel/drivers/video.c @@ -1,8 +1,8 @@ #include #include "video.h" -static uint8_t * const video = (uint8_t*) 0xB8000; -static uint8_t * currentVideo = (uint8_t*) 0xB8000; +static uint8_t * const video = (uint8_t *) 0xB8000; +static uint8_t * currentVideo = (uint8_t *) 0xB8000; static const int width = 80; static const int height = 25; static int currentX = 0; @@ -13,23 +13,16 @@ int limitY[2] = {0, 25}; void increment() { currentX++; - if (currentX >= limitX[1]) { + if (currentX == limitX[1]) { currentY++; currentX = limitX[0]; - if (currentY >= limitY[1]) - currentY = limitY[0]; + if (currentY >= limitY[1]) { + scroll(); + currentY = limitY[1] - 1; + } } } -int atoi(char * string, int length) { - int res = 0, i = 0; - while (i < length) { - res = res * 10 + string[i++] - '0'; - } - return res; -} - -// "\e\f" para clear char checkIfEscapeSequence(const char * bufferAux) { if (*bufferAux == '\e') { bufferAux++; @@ -42,21 +35,24 @@ char checkIfEscapeSequence(const char * bufferAux) { return 0; } +void scroll() { + for (int i = limitY[0]; i < limitY[1] - 1; i++) { + for (int j = limitX[0]; j < limitX[1]; j++) { + *(video + i * width * 2 + j * 2) = *(video + (i + 1) * width * 2 + j * 2); + *(video + i * width * 2 + j * 2 + 1) = *(video + (i + 1) * width * 2 + j * 2 + 1); + } + } + for (int j = limitX[0]; j < limitX[1]; j++) { + *(video + (limitY[1] - 1) * width * 2 + j * 2) = ' '; + } +} + int printStringLen(int color, const char * string, int maxLen) { int i = 0; while (*string != '\0' && i <= maxLen) { - if (currentX >= limitX[1]) { - currentX = limitX[0]; - currentY++; - } - if (currentY >= limitY[1]) { - currentY = limitY[0]; - } - if (*string == '\n') { new_line(); - // return i; string++; i++; continue; @@ -93,15 +89,18 @@ void backspace() { void new_line() { currentX = limitX[0]; currentY++; - if (currentY == limitY[1]) - currentY = limitY[0]; + if (currentY == limitY[1]) { + scroll(); + currentY = limitY[1] - 1; + } } void clear() { - for (int i = limitX[0]; i < (limitX[1] - limitX[0]) * 2 * (limitY[1] - limitY[0]); i++) { - printStringLen(15, " ", 1); - } - currentX = limitX[0]; currentY = limitY[0]; + for (int i = limitY[0]; i < limitY[1]; i++) { + for (int j = limitX[0]; j < limitX[1]; j++) { + *(video + i * width * 2 + j * 2) = ' '; + } + } } \ No newline at end of file diff --git a/Kernel/include/pipeLib.h b/Kernel/include/pipeLib.h index dbd8b66..2f62fd4 100644 --- a/Kernel/include/pipeLib.h +++ b/Kernel/include/pipeLib.h @@ -7,7 +7,7 @@ #define PIPE_MAX_SIZE 1024 typedef struct pipe_t { - int fd; + int fd[2]; char buffer[PIPE_MAX_SIZE]; int current; char name[MAX_NAME]; @@ -19,7 +19,7 @@ typedef struct node_t { struct node_t * next; } node_t; -pipe_t * openPipe(char * name); +char openPipe(int * fds, char * name); void writePipe(int fd, char c); char readPipe(int fd); diff --git a/Kernel/include/schedulerLib.h b/Kernel/include/schedulerLib.h index c19a5fd..d7f7efc 100644 --- a/Kernel/include/schedulerLib.h +++ b/Kernel/include/schedulerLib.h @@ -4,7 +4,7 @@ #include void initScheduler(); -void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char *argv[]); +void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char *argv[], int * fd); char block(int pid); char unblock(int pid); char kill(int pid); diff --git a/Kernel/include/video.h b/Kernel/include/video.h index 541979a..51aef60 100644 --- a/Kernel/include/video.h +++ b/Kernel/include/video.h @@ -10,5 +10,6 @@ void backspace(); void clear(); void increment(); void changeWindow(); +void scroll(); #endif \ No newline at end of file diff --git a/Kernel/interruptions/systemCalls.c b/Kernel/interruptions/systemCalls.c index e8b0cba..5ab3015 100644 --- a/Kernel/interruptions/systemCalls.c +++ b/Kernel/interruptions/systemCalls.c @@ -15,12 +15,11 @@ uint64_t write(uint64_t fd, uint64_t buffer, uint64_t length) { char * bufferAux = (char *) buffer; - int color; + int color = STDOUT_COLOR; - if (fd == STDOUT) - color = STDOUT_COLOR; - else { - fd = getFdOut(); + fd = getFdOut(); + + if (fd != STDOUT) { int i = 0; while (bufferAux[i] != '\0' && i++ <= length) { writePipe(fd, bufferAux[i]); @@ -35,6 +34,7 @@ uint64_t read(uint64_t fd, uint64_t buffer, uint64_t length) { char * bufferAux = (char *) buffer; int readBytes = 0; + fd = getFdIn(); if (fd == STDIN) { while (length-- > 0) { diff --git a/Kernel/interruptions/systemCallsDispatcher.c b/Kernel/interruptions/systemCallsDispatcher.c index d211781..a426369 100644 --- a/Kernel/interruptions/systemCallsDispatcher.c +++ b/Kernel/interruptions/systemCallsDispatcher.c @@ -5,9 +5,10 @@ void exitProcess(); // void setFn(uint64_t, uint64_t, uint64_t); char * processes(); -void enqueueProcess(uint64_t, uint64_t, uint64_t, uint64_t); +void enqueueProcess(uint64_t, uint64_t, uint64_t, uint64_t, uint64_t); +char openPipe(int *, char *); -uint64_t systemCallsDispatcher(uint64_t rdi, uint64_t rsi, uint64_t rdx, uint64_t rcx, uint64_t r8) { +uint64_t systemCallsDispatcher(uint64_t rdi, uint64_t rsi, uint64_t rdx, uint64_t rcx, uint64_t r8, uint64_t r9) { switch (rdi) { case 0: return write(rsi, rdx, rcx); @@ -17,7 +18,7 @@ uint64_t systemCallsDispatcher(uint64_t rdi, uint64_t rsi, uint64_t rdx, uint64_ return getTime(rsi, rdx, rcx); case 3: // createProcess(rsi); - enqueueProcess(rsi, rdx, rcx, r8); + enqueueProcess(rsi, rdx, rcx, r8, r9); break; case 4: exitProcess(); @@ -34,6 +35,8 @@ uint64_t systemCallsDispatcher(uint64_t rdi, uint64_t rsi, uint64_t rdx, uint64_ case 9: vPortFree(rsi); break; + case 10: + return (uint64_t) openPipe(rsi, rdx); default: return -1; } diff --git a/Kernel/kernel.c b/Kernel/kernel.c index f15e98e..a640d08 100644 --- a/Kernel/kernel.c +++ b/Kernel/kernel.c @@ -141,7 +141,7 @@ void test_mm(){ } } -void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char *argv[]); +void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char *argv[], int * fd); void initScheduler(); void _cli(); @@ -177,7 +177,7 @@ int main() { // ((EntryPoint)sampleCodeModuleAddress)(); char * argv[] = {"SampleCode"}; - enqueueProcess(sampleCodeModuleAddress, 1, 1, argv); + enqueueProcess(sampleCodeModuleAddress, 1, 1, argv, NULL); clear(); // haltcpu(); _sti(); diff --git a/Kernel/utils/memManagerFRT4.c b/Kernel/utils/memManagerFRT4.c index f581fae..0c15cc0 100644 --- a/Kernel/utils/memManagerFRT4.c +++ b/Kernel/utils/memManagerFRT4.c @@ -199,14 +199,14 @@ void *pvReturn = NULL; } else { - ncNewline(); + // ncNewline(); // ncPrint("MALLOC: "); // ncPrintDec(xFreeBytesRemaining); // ncPrint(" "); // ncPrintDec(xWantedSize); // ncPrint(" "); // ncPrintDec(configADJUSTED_HEAP_SIZE); - ncNewline(); + // ncNewline(); } } diff --git a/Kernel/utils/pipe.c b/Kernel/utils/pipe.c index 96a7820..c488454 100644 --- a/Kernel/utils/pipe.c +++ b/Kernel/utils/pipe.c @@ -4,14 +4,19 @@ int fds = 2; node_t * firstPipe; -pipe_t * openPipe(char * name) { +char openPipe(int fds[2] ,char * name) { pipe_t * pipe = pvPortMalloc(sizeof(pipe_t)); strcpy(pipe->name, name); - pipe->fd = fds++; + if ((pipe->sem = semOpen(SEM_NAME, 1)) == NULL) - return NULL; + return EXIT_FAILURE; - return pipe; + pipe->fd[0] = fds++; + pipe->fd[1] = fds++; + fds[0] = pipe->fd[0]; + fds[1] = pipe->fd[1]; + + return EXIT_SUCCESS; } void writePipe(int fd, char c) { @@ -47,7 +52,7 @@ void closePipe(int fd) { semWait(del->pipe->sem); if (prev != NULL) prev->next = del->next; - else firstPipe->next = del->next; + else firstPipe = del->next; vPortFree(del->pipe); vPortFree(del); @@ -69,9 +74,5 @@ node_t * searchPipe(node_t ** previous, int fd) { * previous = NULL; return NULL; } - if (curr == firstPipe) { - * previous = NULL; - return curr; - } return curr; } \ No newline at end of file diff --git a/Kernel/utils/scheduler.c b/Kernel/utils/scheduler.c index 9448c6d..2b15fda 100644 --- a/Kernel/utils/scheduler.c +++ b/Kernel/utils/scheduler.c @@ -18,8 +18,8 @@ typedef struct processCDT { char foreground; enum states state; int * fd; - void * sseBytes; - void * fpuBytes; + // void * sseBytes; + // void * fpuBytes; } processCDT; processCDT * firstReady = NULL; @@ -61,7 +61,7 @@ void idle() { void initScheduler() { char * argv[] = {"Idle"}; - enqueueProcess(idle, 0, 1, argv); + enqueueProcess(idle, 0, 1, argv, NULL); } // void setFn(void (*fn) (int, char **), int argc, char *argv[]) { @@ -69,8 +69,14 @@ void initScheduler() { // _initialize_stack_frame(fn, currentProcess->rbp, argc, argv); // } -void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char *argv[]) { -// void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char *argv[], int * fd[2]) { +// void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char *argv[]) { +void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char *argv[], int * fd) { + if (fd == NULL) { + int * aux = pvPortMalloc(2); + aux[0] = 0; + aux[1] = 1; + fd = aux; + } if (firstReady != NULL && firstReady->pid == IDLE_PID) block(IDLE_PID); @@ -81,7 +87,6 @@ void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char * char priority = (foreground == 1) ? DEF_PRIORITY : MAX_PRIORITY/2; - // newProcess(process, argv[0], priority, foreground, (uint64_t) rsp, (uint64_t) rbp); // char aux[MAX_NAME_SIZE]; char * aux = pvPortMalloc(10); int j; @@ -99,6 +104,7 @@ void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char * process->executions = 0; process->foreground = foreground; process->state = READY; + process->fd = fd; // process->sseBytes = pvPortMalloc(64); // process->fpuBytes = pvPortMalloc(14); @@ -111,49 +117,20 @@ void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char * lastReady->next = process; lastReady = process; - // ncClear(); - // ncPrint(argv[0]); - // // proc->name = argv[0]; - // ncPrint(process->name); - // ncPrintDec(process->pid); - // ncPrintHex(process->rsp); - // ncPrintHex(process->rbp); - // wait(3); return; } -void * getSSEaddress() { - return currentProcess->sseBytes; -} +// void * getSSEaddress() { +// return currentProcess->sseBytes; +// } -void * getFPUaddress() { - return currentProcess->fpuBytes; -} +// void * getFPUaddress() { +// return currentProcess->fpuBytes; +// } void newProcess(processADT process, char * name, char priority, char foreground, uint64_t rsp, uint64_t rbp) { - // char aux[MAX_NAME_SIZE]; - // int j; - // for (j = 0; j < MAX_NAME_SIZE - 1 && name[j] != 0; j++) { - // aux[j] = name[j]; - // } - // aux[j] = '\0'; - - // process->name = aux; - // process->pid = pids++; - // process->ppid = currentProcess->pid; - // process->priority = priority; - // process->rsp = rsp; - // process->rbp = rbp; - // process->executions = 0; - // process->foreground = foreground; - // process->state = READY; } -// void loader(int argc, char * argv[], void (*fn) (int, char **)) { -// fn(argc, argv); -// exit(); -// } - processADT searchProcess(processADT * previous, int pid, processADT first) { processADT curr = first; * previous = NULL; @@ -168,10 +145,6 @@ processADT searchProcess(processADT * previous, int pid, processADT first) { * previous = NULL; return NULL; } - if (curr == first) { - * previous = NULL; - return curr; - } return curr; } @@ -263,13 +236,13 @@ char kill(int pid) { int getFdOut() { if (currentProcess == NULL) - return NULL; + return EXIT_FAILURE; return currentProcess->fd[1]; } int getFdIn() { if (currentProcess == NULL) - return NULL; + return EXIT_FAILURE; return currentProcess->fd[0]; } diff --git a/Userland/SampleCodeModule/asm/libasm.asm b/Userland/SampleCodeModule/asm/libasm.asm index 82ee7c0..3bd6bbe 100644 --- a/Userland/SampleCodeModule/asm/libasm.asm +++ b/Userland/SampleCodeModule/asm/libasm.asm @@ -3,7 +3,7 @@ GLOBAL _getMem, sys_loadProcess GLOBAL raiseOpcodeExc GLOBAL _getRegs, sys_switchContext GLOBAL cpu_id, cpu_id_support -GLOBAL sys_exit, sys_ps, sys_free, sys_malloc, sys_sem +GLOBAL sys_exit, sys_ps, sys_free, sys_malloc, sys_sem, sys_openPipe section .text @@ -55,6 +55,27 @@ sys_read: pop rbp ret +sys_openPipe: + push rbp + mov rbp, rsp + + push rdi + push rsi + push rdx + + mov rdx, rsi + mov rsi, rdi + mov rdi, 10 + int 80h + + pop rdx + pop rsi + pop rdi + + mov rsp, rbp + pop rbp + ret + sys_exit: push rbp mov rbp, rsp @@ -274,7 +295,9 @@ sys_loadProcess: push rdx push rcx push r8 + push r9 + mov r9, r8 mov r8, rcx mov rcx, rdx mov rdx, rsi @@ -282,6 +305,7 @@ sys_loadProcess: mov rdi, 3 int 80h + pop r9 pop r8 pop rcx pop rdx diff --git a/Userland/SampleCodeModule/include/system.h b/Userland/SampleCodeModule/include/system.h index 0fac742..815d3db 100644 --- a/Userland/SampleCodeModule/include/system.h +++ b/Userland/SampleCodeModule/include/system.h @@ -2,13 +2,15 @@ #define SYSTEM_H // void sys_switchContext(); -// void sys_loadProcess(); +void sys_loadProcess(void (*fn) (int, char **), char foreground, int argc, char *argv[], int * fd); int sys_time(char); +void sys_exit(); void sys_write(int, char *, int); char sys_read(int, char *, int); char * sys_ps(); char * sys_sem(); void * sys_malloc(int); void * sys_free(void *); +void * sys_openPipe(int *, char *); #endif \ No newline at end of file diff --git a/Userland/SampleCodeModule/libc.c b/Userland/SampleCodeModule/libc.c index 85b3740..de703bd 100644 --- a/Userland/SampleCodeModule/libc.c +++ b/Userland/SampleCodeModule/libc.c @@ -28,6 +28,15 @@ int strcmp(const char * s1, const char * s2) { return *s1 - *s2; } +char * strcat(char * dest, const char * src) { + char * rdest = dest; + + while (*dest) + dest++; + while (*dest++ = *src++); + return rdest; +} + void putChar(char c){ char buffer = c; sys_write(1, &buffer, 0); diff --git a/Userland/SampleCodeModule/sampleCodeModule.c b/Userland/SampleCodeModule/sampleCodeModule.c index 5ebbbc9..d1b35a7 100644 --- a/Userland/SampleCodeModule/sampleCodeModule.c +++ b/Userland/SampleCodeModule/sampleCodeModule.c @@ -1,8 +1,9 @@ /* sampleCodeModule.c */ #include "libc.h" #include "shell/include/shell.h" +#include -void sys_loadProcess(void (*fn) (int, char **), char foreground, int argc, char *argv[]); +void sys_loadProcess(void (*fn) (int, char **), char foreground, int argc, char *argv[], int * fd); void sys_exit(); // void sys_switchContext(); @@ -10,7 +11,7 @@ int main(int argc, char *argv[]) { winClear(); char * argv2[] = {"BottlerSh"}; - sys_loadProcess(shell, 1, 1, argv2); + sys_loadProcess(shell, 1, 1, argv2, NULL); // sys_loadProcess(shell); // sys_switchContext(); diff --git a/Userland/SampleCodeModule/shell/commands/cat.c b/Userland/SampleCodeModule/shell/commands/cat.c new file mode 100644 index 0000000..73c64e5 --- /dev/null +++ b/Userland/SampleCodeModule/shell/commands/cat.c @@ -0,0 +1,17 @@ +#include "cat.h" + +#define SIZE 1000 + +void cat(int argc, char ** argv) { + char c; + int i = 0; + char buffer[SIZE] = {0}; + while ((c = getChar()) != 0 && c != -1) { + if (i >= SIZE) + break; + buffer[i++] = c; + } + + printStringLen(buffer, i); + sys_exit(); +} \ No newline at end of file diff --git a/Userland/SampleCodeModule/shell/commands/clear.c b/Userland/SampleCodeModule/shell/commands/clear.c index 027e93e..97d81b8 100644 --- a/Userland/SampleCodeModule/shell/commands/clear.c +++ b/Userland/SampleCodeModule/shell/commands/clear.c @@ -2,6 +2,7 @@ #include "shell.h" #include "clear.h" -void clear() { +void clear(int argc, char *argv[]) { winClear(); + sys_exit(); } \ No newline at end of file diff --git a/Userland/SampleCodeModule/shell/commands/cpuid.c b/Userland/SampleCodeModule/shell/commands/cpuid.c index 54f0f4e..4e81e3d 100644 --- a/Userland/SampleCodeModule/shell/commands/cpuid.c +++ b/Userland/SampleCodeModule/shell/commands/cpuid.c @@ -100,11 +100,12 @@ static const int len = 16; int (* checks[])() = {check_cpuid, check_mmx, check_sse, check_sse2, check_sse3, check_sse41, check_sse42, check_aes, check_pclmulqdq, check_avx, check_vaesni, check_vpclmulqdq, check_f16c, check_fma, check_avx2, check_fpu}; char * supports[] = {"cpuid_support", "mx_support", "sse_support", "sse2_support", "sse3_support", "sse41_support", "sse42_support", "aes_support", "pclmulqdq_support", "avx_support", "vaesni_support", "vpclmulqdq_support", "f16c_support", "fma_support", "avx2_support", "fpu_support"}; -void cpufeatures() { +void cpufeatures(int argc, char *argv[]) { for (int i = 0; i < len; i++) { if (checks[i]()) { printString(supports[i]); new_line(); } } + sys_exit(); } diff --git a/Userland/SampleCodeModule/shell/commands/excDiv.c b/Userland/SampleCodeModule/shell/commands/excDiv.c index afa4d7f..bc07068 100644 --- a/Userland/SampleCodeModule/shell/commands/excDiv.c +++ b/Userland/SampleCodeModule/shell/commands/excDiv.c @@ -1,6 +1,6 @@ #include "excDiv.h" -void excdiv() { +void excdiv(int argc, char *argv[]) { int cero = 0; int res = 1/cero; res = res + 1; diff --git a/Userland/SampleCodeModule/shell/commands/excOP.c b/Userland/SampleCodeModule/shell/commands/excOP.c index 1aa4020..0a4f297 100644 --- a/Userland/SampleCodeModule/shell/commands/excOP.c +++ b/Userland/SampleCodeModule/shell/commands/excOP.c @@ -2,6 +2,6 @@ void raiseOpcodeExc(); -void excop() { +void excop(int argc, char *argv[]) { raiseOpcodeExc(); } \ No newline at end of file diff --git a/Userland/SampleCodeModule/shell/commands/filter.c b/Userland/SampleCodeModule/shell/commands/filter.c new file mode 100644 index 0000000..fc505b0 --- /dev/null +++ b/Userland/SampleCodeModule/shell/commands/filter.c @@ -0,0 +1,22 @@ +#include "filter.h" + +#define SIZE 1000 + +int isVocal(char c) { + return c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U' || c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; +} + +void filter(int argc, char ** argv) { + char c; + int i = 0; + char buffer[SIZE] = {0}; + while ((c = getChar()) != 0 && c != -1) { + if (i >= SIZE) + break; + if (isVocal(c)) + buffer[i++] = c; + } + + printStringLen(buffer, i); + sys_exit(); +} \ No newline at end of file diff --git a/Userland/SampleCodeModule/shell/commands/help.c b/Userland/SampleCodeModule/shell/commands/help.c index 801dfe2..0fd7c73 100644 --- a/Userland/SampleCodeModule/shell/commands/help.c +++ b/Userland/SampleCodeModule/shell/commands/help.c @@ -4,9 +4,10 @@ static char * info[] = {"clear: Clear the shell window", "cpufeatures: Show the features the cpu supports", "excdiv: Throw exception 0 (zero division)", "excop: Throw exception 6 (opcode error)", "help: Print information about commands", "inforeg: Print values of registers","printmem: Prints 32 bytes of memory, starting at a given direction", "quadratic: Receives 3 floats, quadratic coefficients, prints roots if real", "time: Prints date and time in UTC" }; static const int len = 9; -void help() { +void help(int argc, char *argv[]) { for (int i = 0; i < len; i++) { printString(info[i]); new_line(); } + sys_exit(); } \ No newline at end of file diff --git a/Userland/SampleCodeModule/shell/commands/inforeg.c b/Userland/SampleCodeModule/shell/commands/inforeg.c index 1f6f5e9..9c14028 100644 --- a/Userland/SampleCodeModule/shell/commands/inforeg.c +++ b/Userland/SampleCodeModule/shell/commands/inforeg.c @@ -10,7 +10,7 @@ static char * regsNames[] = { "R8: ", "R9: ", "R10: ", "R11: ", "R12: ", "R13: ", "R14: ", "R15: ", "RSP: ", "RIP: "}; -void inforeg() { +void inforeg(int argc, char *argv[]) { uint64_t * regs = (uint64_t *) _getRegs(); char bufferAux[20]; @@ -25,4 +25,5 @@ void inforeg() { printStringLen(" - ", 3); } new_line(); + sys_exit(); } \ No newline at end of file diff --git a/Userland/SampleCodeModule/shell/commands/printmem.c b/Userland/SampleCodeModule/shell/commands/printmem.c index cfedf31..4846f83 100644 --- a/Userland/SampleCodeModule/shell/commands/printmem.c +++ b/Userland/SampleCodeModule/shell/commands/printmem.c @@ -13,10 +13,16 @@ void getMem(int * buffer, int initialAddress) { } } -void printmem(long dir) { +// void printmem(long dir) { +void printmem(int argc, char *argv[]) { + if (argc != 2) { + printStringLen("printmem receives a memory position\n", 37); + return; + } + int buffer[8]; - getMem(buffer, dir); + getMem(buffer, atoi(argv[1], strlen(argv[1]))); for (int i = 0; i < 8; i++) { char bufferAux[8]; diff --git a/Userland/SampleCodeModule/shell/commands/ps.c b/Userland/SampleCodeModule/shell/commands/ps.c index 3ad71ad..5221e29 100644 --- a/Userland/SampleCodeModule/shell/commands/ps.c +++ b/Userland/SampleCodeModule/shell/commands/ps.c @@ -1,9 +1,9 @@ #include "libc.h" #include "shell.h" -void ps() { +void ps(int argc, char *argv[]) { char * output = sys_ps(); printString(output); new_line(); - // sys_free(output); + sys_free(output); } \ No newline at end of file diff --git a/Userland/SampleCodeModule/shell/commands/quadratic.c b/Userland/SampleCodeModule/shell/commands/quadratic.c index fc0214b..0e93e7c 100644 --- a/Userland/SampleCodeModule/shell/commands/quadratic.c +++ b/Userland/SampleCodeModule/shell/commands/quadratic.c @@ -2,8 +2,14 @@ #include "quadratic.h" #include "shell.h" -void quadratic(double a, double b, double c) { - double sol1, sol2; +// void quadratic(double a, double b, double c) { +void quadratic(int argc, char *argv[]) { + if (argc != 4) { + printStringLen("quadratic receives 3 doubles\n", 30); + return; + } + + double sol1, sol2, a = atof(argv[1]), b = atof(argv[2]), c = atof(argv[3]); if (a == 0) { printStringLen("First argument cannot be 0\n", 28); return; diff --git a/Userland/SampleCodeModule/shell/commands/semCom.c b/Userland/SampleCodeModule/shell/commands/semCom.c index c58a8d7..f19764b 100644 --- a/Userland/SampleCodeModule/shell/commands/semCom.c +++ b/Userland/SampleCodeModule/shell/commands/semCom.c @@ -1,9 +1,9 @@ #include "libc.h" #include "shell.h" -void sem() { +void sem(int argc, char *argv[]) { char * output = sys_sem(); printString(output); new_line(); - // sys_free(output); + sys_free(output); } \ No newline at end of file diff --git a/Userland/SampleCodeModule/shell/commands/time.c b/Userland/SampleCodeModule/shell/commands/time.c index 83adfc4..74d2d2a 100644 --- a/Userland/SampleCodeModule/shell/commands/time.c +++ b/Userland/SampleCodeModule/shell/commands/time.c @@ -35,7 +35,7 @@ void printTime(int number) { printString(buffer); } -void time(char * window, int * offset) { +void time(int argc, char *argv[]) { printTime(getDay()); putChar('/'); printTime(getMonth()); diff --git a/Userland/SampleCodeModule/shell/commands/wc.c b/Userland/SampleCodeModule/shell/commands/wc.c new file mode 100644 index 0000000..0f32f3f --- /dev/null +++ b/Userland/SampleCodeModule/shell/commands/wc.c @@ -0,0 +1,18 @@ +#include "wc.h" + +#define SIZE 1000 + +void wc(int argc, char ** argv) { + char c; + int i = 0; + char buffer[SIZE] = {0}; + while ((c = getChar()) != 0 && c != -1) { + if (i >= SIZE) + break; + if (c == '\n') + i++; + } + + printStringLen(gtoa(i, buffer, 10, SIZE), SIZE); + sys_exit(); +} \ No newline at end of file diff --git a/Userland/SampleCodeModule/shell/include/cat.h b/Userland/SampleCodeModule/shell/include/cat.h new file mode 100644 index 0000000..a4f2475 --- /dev/null +++ b/Userland/SampleCodeModule/shell/include/cat.h @@ -0,0 +1,9 @@ +#ifndef CAT_LIB +#define CAT_LIB + +#include "libc.h" +#include "system.h" + +void cat(int argc, char *argv[]); + +#endif \ No newline at end of file diff --git a/Userland/SampleCodeModule/shell/include/clear.h b/Userland/SampleCodeModule/shell/include/clear.h index 22df688..de08d0e 100644 --- a/Userland/SampleCodeModule/shell/include/clear.h +++ b/Userland/SampleCodeModule/shell/include/clear.h @@ -1,6 +1,6 @@ #ifndef CLEAR_LIB #define CLEAR_LIB -void clear(); +void clear(int argc, char *argv[]); #endif \ No newline at end of file diff --git a/Userland/SampleCodeModule/shell/include/cpu_id.h b/Userland/SampleCodeModule/shell/include/cpu_id.h index 1417b1a..fddbd1f 100644 --- a/Userland/SampleCodeModule/shell/include/cpu_id.h +++ b/Userland/SampleCodeModule/shell/include/cpu_id.h @@ -71,7 +71,7 @@ enum { }; -void cpufeatures(); +void cpufeatures(int argc, char *argv[]); int cpu_id_support(void); #endif \ No newline at end of file diff --git a/Userland/SampleCodeModule/shell/include/excDiv.h b/Userland/SampleCodeModule/shell/include/excDiv.h index 80b55ca..b9d0e6c 100644 --- a/Userland/SampleCodeModule/shell/include/excDiv.h +++ b/Userland/SampleCodeModule/shell/include/excDiv.h @@ -1,6 +1,6 @@ #ifndef EXCDIV_LIB #define EXCDIV_LIB -void excdiv(); +void excdiv(int argc, char *argv[]); #endif \ No newline at end of file diff --git a/Userland/SampleCodeModule/shell/include/excOP.h b/Userland/SampleCodeModule/shell/include/excOP.h index 0457153..a774c79 100644 --- a/Userland/SampleCodeModule/shell/include/excOP.h +++ b/Userland/SampleCodeModule/shell/include/excOP.h @@ -1,6 +1,6 @@ #ifndef EXCOP_LIB #define EXCOP_LIB -void excop(); +void excop(int argc, char *argv[]); #endif \ No newline at end of file diff --git a/Userland/SampleCodeModule/shell/include/filter.h b/Userland/SampleCodeModule/shell/include/filter.h new file mode 100644 index 0000000..f09ee2d --- /dev/null +++ b/Userland/SampleCodeModule/shell/include/filter.h @@ -0,0 +1,9 @@ +#ifndef FILTER_LIB +#define FILTER_LIB + +#include "libc.h" +#include "system.h" + +void filter(int argc, char *argv[]); + +#endif \ No newline at end of file diff --git a/Userland/SampleCodeModule/shell/include/help.h b/Userland/SampleCodeModule/shell/include/help.h index 6c9cc93..ad29b80 100644 --- a/Userland/SampleCodeModule/shell/include/help.h +++ b/Userland/SampleCodeModule/shell/include/help.h @@ -1,6 +1,6 @@ #ifndef HELP_LIB #define HELP_LIB -void help(); +void help(int argc, char *argv[]); #endif \ No newline at end of file diff --git a/Userland/SampleCodeModule/shell/include/inforeg.h b/Userland/SampleCodeModule/shell/include/inforeg.h index 107b568..4198c68 100644 --- a/Userland/SampleCodeModule/shell/include/inforeg.h +++ b/Userland/SampleCodeModule/shell/include/inforeg.h @@ -1,6 +1,6 @@ #ifndef REG_LIB #define REG_LIB -void inforeg(); +void inforeg(int argc, char *argv[]); #endif \ No newline at end of file diff --git a/Userland/SampleCodeModule/shell/include/printmem.h b/Userland/SampleCodeModule/shell/include/printmem.h index 11afa3d..675daab 100644 --- a/Userland/SampleCodeModule/shell/include/printmem.h +++ b/Userland/SampleCodeModule/shell/include/printmem.h @@ -1,6 +1,6 @@ #ifndef MEM_LIB #define MEM_LIB -void printmem(long); +void printmem(int argc, char *argv[]); #endif \ No newline at end of file diff --git a/Userland/SampleCodeModule/shell/include/ps.h b/Userland/SampleCodeModule/shell/include/ps.h index 44cb239..c3126d6 100644 --- a/Userland/SampleCodeModule/shell/include/ps.h +++ b/Userland/SampleCodeModule/shell/include/ps.h @@ -1,6 +1,6 @@ #ifndef PS_LIB #define PS_LIB -void ps(); +void ps(int argc, char *argv[]); #endif \ No newline at end of file diff --git a/Userland/SampleCodeModule/shell/include/quadratic.h b/Userland/SampleCodeModule/shell/include/quadratic.h index 0e28c4f..7b4700d 100644 --- a/Userland/SampleCodeModule/shell/include/quadratic.h +++ b/Userland/SampleCodeModule/shell/include/quadratic.h @@ -2,6 +2,6 @@ #define QUAD_LIB int quadSolver(double, double, double, double *, double *); -void quadratic(double, double, double); +void quadratic(int argc, char *argv[]); #endif \ No newline at end of file diff --git a/Userland/SampleCodeModule/shell/include/semCom.h b/Userland/SampleCodeModule/shell/include/semCom.h index d2b9e81..74d0e98 100644 --- a/Userland/SampleCodeModule/shell/include/semCom.h +++ b/Userland/SampleCodeModule/shell/include/semCom.h @@ -1,6 +1,6 @@ #ifndef SEM_LIB #define SEM_LIB -void sem(); +void sem(int argc, char *argv[]); #endif \ No newline at end of file diff --git a/Userland/SampleCodeModule/shell/include/time.h b/Userland/SampleCodeModule/shell/include/time.h index bf073e0..f932900 100644 --- a/Userland/SampleCodeModule/shell/include/time.h +++ b/Userland/SampleCodeModule/shell/include/time.h @@ -7,7 +7,7 @@ int getHours(); int getDays(); int getMonths(); int getYears(); -void time(); +void time(int argc, char *argv[]); #define SECONDS 0 #define MINUTES 2 diff --git a/Userland/SampleCodeModule/shell/include/wc.h b/Userland/SampleCodeModule/shell/include/wc.h new file mode 100644 index 0000000..63c9583 --- /dev/null +++ b/Userland/SampleCodeModule/shell/include/wc.h @@ -0,0 +1,9 @@ +#ifndef WC_LIB +#define WC_LIB + +#include "libc.h" +#include "system.h" + +void wc(int argc, char *argv[]); + +#endif \ No newline at end of file diff --git a/Userland/SampleCodeModule/shell/shell.c b/Userland/SampleCodeModule/shell/shell.c index f416031..2d9f91f 100644 --- a/Userland/SampleCodeModule/shell/shell.c +++ b/Userland/SampleCodeModule/shell/shell.c @@ -10,19 +10,45 @@ #include "quadratic.h" #include "cpu_id.h" #include "ps.h" +#include "wc.h" +#include "filter.h" +#include "cat.h" #include "semCom.h" +#include "stddef.h" +#define EXIT_FAILURE 1 +#define EXIT_SUCCESS 0 #define SIZE 100 -#define MAX_ARGS 5 +#define MAX_ARGS 11 #define COLS 80 #define ROWS 25 -const int len = 9; -char *commands_void[] = {"help", "time", "inforeg", "excdiv", "excop", "clear", "cpufeatures", "ps", "sem"}; -void (*func []) () = {help, time, inforeg, excdiv, excop, clear, cpufeatures, ps, sem}; +typedef struct cmd_t { + char * name; + void (*func) (int argc, char * argv[]); + char isBuiltIn; +} cmd_t; -void scanfNoPrint(char * buffer) { +const int len = 14; +cmd_t commands[] = { + { "help", help, 1 }, + { "cat", cat, 0 }, + { "time", time, 1 }, + { "inforeg", inforeg, 1 }, + { "excdiv", excdiv, 1 }, + { "excop", excop, 1 }, + { "filter", filter, 0 }, + { "clear", clear, 1 }, + { "cpufeatures", cpufeatures, 1 }, + { "ps", ps, 1 }, + { "sem", sem, 1 }, + { "quadratic", quadratic, 0 }, + { "printmem", printmem, 0 }, + { "wc", wc, 0 }, +}; + +int scanfNoPrint(char * buffer) { char c; int i = 0; while ((c = getChar()) != '\n' && i < SIZE - 1) { @@ -38,52 +64,89 @@ void scanfNoPrint(char * buffer) { } } buffer[i] = '\0'; + return i; +} + +void processInput(char * input) { + int comm_flag0 = 0, comm_flag1 = 0, pipe = -1, end = -1; + char* tokens[SIZE] = {0}; + tokens[0] = strstrip(input, ' '); + for (int i = 1; i < MAX_ARGS; i++) { + tokens[i] = strtok(tokens[i - 1], ' '); + if (i > 1) { + if (!strcmp(tokens[i - 1], "|")) + pipe = i - 1; + } + if (tokens[i][0] == 0) { + end = i; + break; + } + } + int fd[2]; + if (pipe != -1) { + if (sys_openPipe(fd, "pipe")) + return; + // return EXIT_FAILURE; + } + char ** argv0 = NULL; + char ** argv1 = NULL; + if (pipe != -1) { + argv0 = sys_malloc(sizeof(char *) * pipe); + for (int i = 0; i < pipe; i++) + argv0[i] = tokens[i]; + argv1 = sys_malloc(sizeof(char *) * (end - pipe - 1)); + for (int i = pipe + 1; i < end - pipe - 1; i++) + argv1[i] = tokens[i]; + } + else { + *argv0 = sys_malloc(sizeof(char *) * end); + for (int i = 0; i < end; i++) + argv0[i] = tokens[i]; + } + for (int i = 0; i < len; i++) { + if (!strcmp(tokens[0], commands[i].name)) { + comm_flag0 = 1; + if (pipe != -1) + sys_loadProcess(commands[i].func, 1, pipe, argv0, fd); + else + sys_loadProcess(commands[i].func, 1, end, argv0, NULL); + break; + } + } + if (comm_flag0 && pipe != -1) { + for (int i = 0; i < len; i++) { + if (!strcmp(tokens[pipe + 1], commands[i].name)) { + comm_flag1 = 1; + sys_loadProcess(commands[i].func, 1, end - pipe - 1, argv1, fd); + break; + } + } + } + + if (!comm_flag0) { + if (*tokens[0] != 0) + incorrect_comm(tokens[0]); + } + else if (!comm_flag1 && pipe != -1) { + if (*tokens[pipe + 1] != 0) + incorrect_comm(tokens[pipe + 1]); + } +} + +void loader(void (*fn) (int, char **), int argc, char * argv[]) { + fn(argc, argv); + sys_exit(); } void shell(int argc, char *argv[]) { + printStringLen("$> ", 3); + char buffer[SIZE] = {0}; + while (1) { - int comm_flag = 0; - printStringLen("$> ", 3); - char buffer[SIZE] = {0}; scanfNoPrint(buffer); new_line(); - char* tokens[SIZE] = {0}; - tokens[0] = strstrip(buffer, ' '); - for (int i = 1; i < MAX_ARGS; i++) { - tokens[i] = strtok(tokens[i - 1], ' '); - } - for (int i = 0; i < len; i++) { - if (!strcmp(tokens[0], commands_void[i])) { - if (*tokens[1] != 0) - incorrect_arg(tokens[0]); - else - (*func[i])(); - comm_flag = 1; - } - } - if (!strcmp(tokens[0], "quadratic")) { - if (*tokens[4] != 0 || *tokens[3] == 0) - incorrect_arg(tokens[0]); - else if (!isFloat(tokens[1]) || !isFloat(tokens[2]) || !isFloat(tokens[3])) - incorrect_arg(tokens[0]); - else - quadratic(atof(tokens[1]), atof(tokens[2]), atof(tokens[3])); - comm_flag = 1; - } - if (!strcmp(tokens[0], "printmem")) { - if (*tokens[2] != 0 || *tokens[1] == 0) - incorrect_arg(tokens[0]); - else { - int length = strlen(tokens[1]); - printmem(atoi(tokens[1], length)); - } - comm_flag = 1; - } - - if (!comm_flag) { - if (*tokens[0] != 0) - incorrect_comm(tokens[0]); - } + processInput(buffer); + printStringLen("$> ", 3); } }