From af241224c93d3e9e8c714f30195a22c4808b05b8 Mon Sep 17 00:00:00 2001 From: Santiago Lo Coco Date: Tue, 26 Oct 2021 22:18:19 -0300 Subject: [PATCH] Make a lot of changes in shell and fix some bugs Co-authored-by: Ezequiel Bellver Co-authored-by: Juan Barmasch --- Kernel/asm/interrupts.asm | 103 +++++++----- Kernel/drivers/video.c | 47 +----- Kernel/include/lib.h | 1 + Kernel/include/pipe.h | 18 ++ Kernel/include/pipeLib.h | 26 +++ Kernel/include/scheduler.h | 21 +-- Kernel/include/schedulerLib.h | 19 +++ Kernel/include/sem.h | 18 ++ Kernel/include/semLib.h | 23 +++ Kernel/include/semaphore.h | 9 - Kernel/interruptions/systemCalls.c | 38 +++-- Kernel/lib.c | 8 + Kernel/utils/memManagerFRT4.c | 14 +- Kernel/utils/pipe.c | 88 +++------- Kernel/utils/scheduler.c | 45 +++-- Kernel/utils/{semaphore.c => sem.c} | 41 +---- Userland/SampleCodeModule/include/libc.h | 2 + Userland/SampleCodeModule/libc.c | 6 +- .../SampleCodeModule/shell/commands/change.c | 6 - .../SampleCodeModule/shell/commands/clear.c | 4 +- .../SampleCodeModule/shell/commands/cpuid.c | 8 +- .../SampleCodeModule/shell/commands/help.c | 11 +- .../SampleCodeModule/shell/commands/inforeg.c | 15 +- .../shell/commands/printmem.c | 9 +- Userland/SampleCodeModule/shell/commands/ps.c | 9 +- .../shell/commands/quadratic.c | 33 ++-- .../SampleCodeModule/shell/commands/sem.c | 10 -- .../SampleCodeModule/shell/commands/semCom.c | 9 + .../SampleCodeModule/shell/commands/switch.c | 0 .../SampleCodeModule/shell/commands/time.c | 28 ++-- .../SampleCodeModule/shell/include/change.h | 6 - .../SampleCodeModule/shell/include/clear.h | 10 +- .../SampleCodeModule/shell/include/cpu_id.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 +- Userland/SampleCodeModule/shell/include/sem.h | 6 - .../SampleCodeModule/shell/include/semCom.h | 6 + .../SampleCodeModule/shell/include/shell.h | 8 +- .../SampleCodeModule/shell/include/time.h | 2 +- Userland/SampleCodeModule/shell/oldShell.c | 158 ++++++++++++++++++ Userland/SampleCodeModule/shell/shell.c | 107 +++--------- 44 files changed, 570 insertions(+), 421 deletions(-) create mode 100644 Kernel/include/pipe.h create mode 100644 Kernel/include/pipeLib.h create mode 100644 Kernel/include/schedulerLib.h create mode 100644 Kernel/include/sem.h create mode 100644 Kernel/include/semLib.h delete mode 100644 Kernel/include/semaphore.h rename Kernel/utils/{semaphore.c => sem.c} (83%) delete mode 100644 Userland/SampleCodeModule/shell/commands/change.c delete mode 100644 Userland/SampleCodeModule/shell/commands/sem.c create mode 100644 Userland/SampleCodeModule/shell/commands/semCom.c delete mode 100644 Userland/SampleCodeModule/shell/commands/switch.c delete mode 100644 Userland/SampleCodeModule/shell/include/change.h delete mode 100644 Userland/SampleCodeModule/shell/include/sem.h create mode 100644 Userland/SampleCodeModule/shell/include/semCom.h create mode 100644 Userland/SampleCodeModule/shell/oldShell.c diff --git a/Kernel/asm/interrupts.asm b/Kernel/asm/interrupts.asm index 2d9368d..ddf5f71 100755 --- a/Kernel/asm/interrupts.asm +++ b/Kernel/asm/interrupts.asm @@ -30,6 +30,8 @@ GLOBAL loadProcess GLOBAL _initialize_stack_frame GLOBAL _switchContext +EXTERN getFPUaddress, getSSEaddress + SECTION .text %macro pushState 0 @@ -136,9 +138,44 @@ picSlaveMask: pop rbp retn +; getFPUaddress: +; push rcx +; mov ecx, [auxi] +; mov ecx, 1 +; xor ecx, [auxi] +; cmp ecx, 0 +; jne .getSecond +; mov rax, bytesForFPU1 +; jmp .finish +; .getSecond: +; mov rax, bytesForFPU2 +; .finish: +; mov dword [auxi], ecx +; pop rcx +; ret + +; getSSEaddress: +; push rcx +; mov ecx, [auxi] +; cmp ecx, 0 +; jne .getSecond +; mov rax, bytesForSSEAligned1 +; jmp .finish +; .getSecond: +; mov rax, bytesForSSEAligned2 +; .finish: +; pop rcx +; ret + ;8254 Timer (Timer Tick) _irq00Handler: pushState + ; push rax + ; call getFPUaddress + ; fsave [rax] + ; call getSSEaddress + ; fxsave [rax] + ; pop rax ; fsave [bytesForFPU] ; fxsave [bytesForSSEAligned] @@ -156,9 +193,13 @@ _irq00Handler: ; fxrstor [bytesForSSEAligned] ; frstor [bytesForFPU] + ; push rax + ; call getFPUaddress + ; frstor [rax] + ; call getSSEaddress + ; fxrstor [rax] + ; pop rax popState - ; pop rsi ; argv - ; pop rdi ; argc 6004d8 iretq ;Keyboard @@ -236,16 +277,24 @@ _initialize_stack_frame: push 0x0 ; ss push rsi ; sp push 0x202 ; rflags - push 0x08 ; cs -- offset de la GDT + push 0x08 ; cs push rdi ; IP - ; push rdx ; argc - ; push rcx ; argv + + mov rsi, rcx + mov rdi, rdx pushState - ; mov rdi, rsp - ; call newStack - - ; mov rax, rsp + ; fsave [bytesForFPU] + ; push rax + ; mov dword [auxi], 1 + ; call getFPUaddress + ; call getSSEaddress + ; pop rax + ; fxsave [bytesForSSEAligned] + + ; fsave [r8] + ; fxsave [r9] + mov rsp, r10 ret @@ -262,41 +311,19 @@ _systemCallsHandler: popStateNoRax iretq -; switch Context (int 81h) -; _switchContext: -; pushState -; -; call changeWindow -; mov rdi, rsp -; call preserveStack -; -; mov rsp, rax -; popState -; pop rsi ; argv -; pop rdi ; argc -; iretq - -; _switchContext: -; pushState - -; mov rdi, rsp -; call updateRSP - -; call nextProcess - -; mov rsp, rax -; popState -; pop rsi ; argv -; pop rdi ; argc -; iretq - SECTION .data align 16 bytesForSSEAligned times 512 db 0 + ; bytesForSSEAligned1 times 512 db 0 + ; bytesForSSEAligned2 times 512 db 0 + ; counter dd 1 SECTION .bss aux resq 1 bytesForSSE resb 512 bytesForFPU resb 108 + ; bytesForFPU1 resb 108 + ; bytesForFPU2 resb 108 insPointer resb 8 - rspPointer resb 8 \ No newline at end of file + rspPointer resb 8 + ; auxi resb 4 \ No newline at end of file diff --git a/Kernel/drivers/video.c b/Kernel/drivers/video.c index 1cf280d..49ba6ae 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; @@ -11,39 +11,6 @@ static int currentY = 0; int limitX[2] = {0, 80}; int limitY[2] = {0, 25}; -// char windowVideo = 1; -// -// void changeWindow() { -// windowVideo = 1 - windowVideo; -// moveToWindowVideo(windowVideo); -// } -// -// void moveToWindowVideo(char window) { -// if (window == -1) { -// windowVideo = -1; -// limitX[0] = 0; -// limitX[1] = 80; -// limitY[0] = 0; -// limitY[1] = 25; -// } -// if (window == 1) { -// windowVideo = 1; -// limitX[0] = 40; -// limitX[1] = 80; -// limitY[0] = 0; -// limitY[1] = 25; -// } -// if (window == 0) { -// windowVideo = 0; -// limitX[0] = 0; -// limitX[1] = 40; -// limitY[0] = 0; -// limitY[1] = 25; -// } -// currentX = limitX[0]; -// currentY = limitY[0]; -// } - void increment() { currentX++; if (currentX >= limitX[1]) { @@ -89,7 +56,10 @@ int printStringLen(int color, const char * string, int maxLen) { if (*string == '\n') { new_line(); - return i; + // return i; + string++; + i++; + continue; } else if (*string == '\b') { backspace(); @@ -109,9 +79,7 @@ int printStringLen(int color, const char * string, int maxLen) { } void backspace() { - if (currentVideo > video) { - currentVideo -= 2; - *currentVideo = ' '; + if (video + currentY * width * 2 + currentX * 2 > video) { currentX--; if (currentX < limitX[0]) { currentX = limitX[1]; @@ -119,6 +87,7 @@ void backspace() { currentY--; } } + *(video + currentY * width * 2 + currentX * 2) = ' '; } void new_line() { diff --git a/Kernel/include/lib.h b/Kernel/include/lib.h index 8a3e7ba..db6c7e9 100644 --- a/Kernel/include/lib.h +++ b/Kernel/include/lib.h @@ -12,5 +12,6 @@ int abs(int value); char * itoa(int value, char * buffer, int base, int length); void strlen(const char *str, int *len); int strcpy(char * strDest, const char * strSrc); +char addSpaces(char * str, char qty); #endif \ No newline at end of file diff --git a/Kernel/include/pipe.h b/Kernel/include/pipe.h new file mode 100644 index 0000000..ffb756d --- /dev/null +++ b/Kernel/include/pipe.h @@ -0,0 +1,18 @@ +#ifndef PIPE_H +#define PIPE_H + +#include +#include +#include +#include "lib.h" +#include "scheduler.h" +#include "memManager.h" +#include "pipeLib.h" + +#define MAX_SEM 100 +#define SEM_NAME "Pipes" + +int exists(pipe_t * pipe); +node_t * searchPipe(node_t ** previous, int fd); + +#endif \ No newline at end of file diff --git a/Kernel/include/pipeLib.h b/Kernel/include/pipeLib.h new file mode 100644 index 0000000..dbd8b66 --- /dev/null +++ b/Kernel/include/pipeLib.h @@ -0,0 +1,26 @@ +#ifndef PIPELIB_H +#define PIPELIB_H + +#include "semLib.h" + +#define MAX_NAME 100 +#define PIPE_MAX_SIZE 1024 + +typedef struct pipe_t { + int fd; + char buffer[PIPE_MAX_SIZE]; + int current; + char name[MAX_NAME]; + sem_t * sem; +} pipe_t; + +typedef struct node_t { + pipe_t * pipe; + struct node_t * next; +} node_t; + +pipe_t * openPipe(char * name); +void writePipe(int fd, char c); +char readPipe(int fd); + +#endif \ No newline at end of file diff --git a/Kernel/include/scheduler.h b/Kernel/include/scheduler.h index a2c17f7..799084e 100644 --- a/Kernel/include/scheduler.h +++ b/Kernel/include/scheduler.h @@ -1,19 +1,18 @@ #ifndef SCHEDULER_H #define SCHEDULER_H -#include "queue.h" -#include -#include -#include #include "memManager.h" +#include #include "lib.h" +#include +#include "schedulerLib.h" void forceTimer(); void haltcpu(); #define STACK_SIZE 1024 * 512 #define MAX_PRIORITY 40 // Maximum number for a priority -#define MIN_PRIORITY 0 // Minimum number for a priority (yet maximum level of priority) // me gusta +#define MIN_PRIORITY 0 // Minimum number for a priority (yet maximum level of priority) #define DEF_PRIORITY 0 #define PROCESS_DATA_MAX_SIZE 100 #define MAX_ATTR_SIZE 6 @@ -21,20 +20,10 @@ void haltcpu(); typedef struct processCDT * processADT; -uint64_t nextProcess(); void idle(); -void initScheduler(); -void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char *argv[]); void newProcess(processADT process, char * name, char priority, char foreground, uint64_t rsp, uint64_t rbp); -char block(int pid); -char unblock(int pid); -char kill(int pid); -void exitProcess(); -char nice(char offset); +uint64_t nextProcess(); void updateRSP(uint64_t newRsp); -int getPid(); -char quitCPU(); char getProcessData(char * out, processADT proc); -char * processes(); #endif \ No newline at end of file diff --git a/Kernel/include/schedulerLib.h b/Kernel/include/schedulerLib.h new file mode 100644 index 0000000..c19a5fd --- /dev/null +++ b/Kernel/include/schedulerLib.h @@ -0,0 +1,19 @@ +#ifndef SCHEDULERLIB_H +#define SCHEDULERLIB_H + +#include + +void initScheduler(); +void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char *argv[]); +char block(int pid); +char unblock(int pid); +char kill(int pid); +void exitProcess(); +char nice(char offset); +int getPid(); +char quitCPU(); +char * processes(); +int getFdOut(); +int getFdIn(); + +#endif \ No newline at end of file diff --git a/Kernel/include/sem.h b/Kernel/include/sem.h new file mode 100644 index 0000000..a0f1f22 --- /dev/null +++ b/Kernel/include/sem.h @@ -0,0 +1,18 @@ +#ifndef SEM_H +#define SEM_H + +#include +#include +#include +#include "lib.h" +#include "scheduler.h" +#include "memManager.h" +#include "semLib.h" + +#define MAX_SEM 100 +#define SEM_DATA_MAX_SIZE 100 + +void enter_region(uint32_t * lock); +void leave_region(uint32_t * lock); + +#endif \ No newline at end of file diff --git a/Kernel/include/semLib.h b/Kernel/include/semLib.h new file mode 100644 index 0000000..c2277b5 --- /dev/null +++ b/Kernel/include/semLib.h @@ -0,0 +1,23 @@ +#ifndef SEMLIB_H +#define SEMLIB_H + +#define MAX_NAME 100 + +typedef struct pid_t { + int pid; + struct pid_t * next; +} pid_t; + +typedef struct sem_t { + unsigned int value; + char name[MAX_NAME]; + pid_t * entering; + pid_t * last; +} sem_t; + +sem_t * semOpen(char * name, unsigned int value); +int semClose(sem_t * sem); +int semWait(sem_t * sem); +int semPost(sem_t * sem); + +#endif \ No newline at end of file diff --git a/Kernel/include/semaphore.h b/Kernel/include/semaphore.h deleted file mode 100644 index 3031668..0000000 --- a/Kernel/include/semaphore.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef SEM_H -#define SEM_H - -sem_t * semOpen(char * name, unsigned int value); -int semClose(sem_t * sem); -int semWait(sem_t * sem); -int semPost(sem_t * sem); - -#endif \ No newline at end of file diff --git a/Kernel/interruptions/systemCalls.c b/Kernel/interruptions/systemCalls.c index 669260d..e8b0cba 100644 --- a/Kernel/interruptions/systemCalls.c +++ b/Kernel/interruptions/systemCalls.c @@ -3,7 +3,10 @@ #include "keyboard.h" #include "time.h" #include "pcb.h" +#include "pipeLib.h" +#include "schedulerLib.h" +#define STDIN 0 #define STDOUT 1 #define STDERR 2 @@ -16,8 +19,14 @@ uint64_t write(uint64_t fd, uint64_t buffer, uint64_t length) { if (fd == STDOUT) color = STDOUT_COLOR; - else if (fd == STDERR) - color = STDERR_COLOR; + else { + fd = getFdOut(); + int i = 0; + while (bufferAux[i] != '\0' && i++ <= length) { + writePipe(fd, bufferAux[i]); + } + return i; + } return printStringLen(color, bufferAux, (int) length); } @@ -26,17 +35,24 @@ uint64_t read(uint64_t fd, uint64_t buffer, uint64_t length) { char * bufferAux = (char *) buffer; int readBytes = 0; - while (length-- > 0) { - *bufferAux = getKeyFromBuffer(); - if (*bufferAux == 0) - break; - readBytes++; - bufferAux++; + + if (fd == STDIN) { + while (length-- > 0) { + *bufferAux = getKeyFromBuffer(); + if (*bufferAux == 0) + break; + readBytes++; + bufferAux++; + } + } + else { + fd = getFdIn(); + readBytes += strcpy(buffer, readPipe(fd)); } return readBytes; } -void createProcess(void (*fn)) { - newProcess(fn); -} \ No newline at end of file +// void createProcess(void (*fn)) { +// newProcess(fn); +// } \ No newline at end of file diff --git a/Kernel/lib.c b/Kernel/lib.c index 5adbbc0..8b03e0e 100644 --- a/Kernel/lib.c +++ b/Kernel/lib.c @@ -66,6 +66,14 @@ char * reverse(char * buffer, int i, int j) { int abs(int value) { return value < 0 ? -value : value; } + +char addSpaces(char * str, char qty) { + char aux = qty; + while (qty-- > 0) { + *str++ = ' '; + } + return aux; +} char * itoa(int value, char * buffer, int base, int length) { if (base < 2 || base > 32) { diff --git a/Kernel/utils/memManagerFRT4.c b/Kernel/utils/memManagerFRT4.c index 367b3a4..f581fae 100644 --- a/Kernel/utils/memManagerFRT4.c +++ b/Kernel/utils/memManagerFRT4.c @@ -25,7 +25,9 @@ typedef unsigned long UBaseType_t; #define configTOTAL_HEAP_SIZE 1024 * 1024 * 100 #define configAPPLICATION_ALLOCATED_HEAP 0 #define portBYTE_ALIGNMENT 8 +// #define portBYTE_ALIGNMENT 16 #define portBYTE_ALIGNMENT_MASK ( 0x0007 ) // 8 +// #define portBYTE_ALIGNMENT_MASK ( 0x000F ) // 16 #define pdFALSE ( ( BaseType_t ) 0 ) #define pdTRUE ( ( BaseType_t ) 1 ) #define portPOINTER_SIZE_TYPE uint32_t @@ -198,12 +200,12 @@ void *pvReturn = NULL; else { ncNewline(); - ncPrint("MALLOC: "); - ncPrintDec(xFreeBytesRemaining); - ncPrint(" "); - ncPrintDec(xWantedSize); - ncPrint(" "); - ncPrintDec(configADJUSTED_HEAP_SIZE); + // ncPrint("MALLOC: "); + // ncPrintDec(xFreeBytesRemaining); + // ncPrint(" "); + // ncPrintDec(xWantedSize); + // ncPrint(" "); + // ncPrintDec(configADJUSTED_HEAP_SIZE); ncNewline(); } } diff --git a/Kernel/utils/pipe.c b/Kernel/utils/pipe.c index e490902..96a7820 100644 --- a/Kernel/utils/pipe.c +++ b/Kernel/utils/pipe.c @@ -1,99 +1,65 @@ -#include -#include -#include -#include "lib.h" -#include "scheduler.h" -#include "memManager.h" -#include "semaphore.h" +#include "pipe.h" -#define MAX_SEM 100 -#define MAX_NAME 100 -#define PIPE_MAX_SIZE 1024 -#define SEM_NAME "Pipes" +int fds = 2; -typedef struct pipe_t { - char buffer[PIPE_MAX_SIZE]; - int current; - char name[MAX_NAME]; - sem_t * sem; -} pipe_t; - -typedef struct node_t { - pipe_t * pipe; - struct node_t * next; -} node_t; - -node_t * first; - -// char initPipes() { - // if ((sem = semOpen(SEM_NAME)) == NULL) - // return EXIT_FAILURE; - // return EXIT_SUCCESS; -// } +node_t * firstPipe; pipe_t * openPipe(char * name) { - // semWait(sem); - pipe_t * pipe = pvPortMalloc(sizeof(pipe_t)); strcpy(pipe->name, name); - if ((pipe->sem = semOpen(SEM_NAME)) == NULL) + pipe->fd = fds++; + if ((pipe->sem = semOpen(SEM_NAME, 1)) == NULL) return NULL; return pipe; - // semPost(sem); } -void writePipe(pipe_t * pipe, char c) { - if (!exists(pipe)) - return; +void writePipe(int fd, char c) { + node_t * prev = NULL; + node_t * node = searchPipe(&prev, fd); - semWait(pipe->sem); + semWait(node->pipe->sem); - pipe->buffer[pipe->current++ % PIPE_MAX_SIZE] = c; + node->pipe->buffer[node->pipe->current++ % PIPE_MAX_SIZE] = c; - semPost(pipe->sem); + semPost(node->pipe->sem); } -char readPipe(pipe_t * pipe) { - if (!exists(pipe)) - return -1; +char readPipe(int fd) { + node_t * prev = NULL; + node_t * node = searchPipe(&prev, fd); - semWait(pipe->sem); + semWait(node->pipe->sem); - char c = pipe->buffer[pipe->current-- % PIPE_MAX_SIZE]; + char c = node->pipe->buffer[node->pipe->current-- % PIPE_MAX_SIZE]; - semPost(pipe->sem); + semPost(node->pipe->sem); return c; } -void closePipe(pipe_t * pipe) { +void closePipe(int fd) { node_t * prev = NULL; - node_t * del = search(&prev, pipe); + node_t * del = searchPipe(&prev, fd); if (del == NULL) return; - semWait(pipe->sem); + semWait(del->pipe->sem); if (prev != NULL) prev->next = del->next; - else first->next = del->next; + else firstPipe->next = del->next; - vPortFree(pipe); + vPortFree(del->pipe); vPortFree(del); - semPost(pipe->sem); + semPost(del->pipe->sem); } -int exists(pipe_t * pipe) { - node_t * prev = NULL; - return search(&prev, pipe) != NULL; -} - -node_t * search(node_t ** previous, pipe_t * pipe) { - node_t * curr = first; +node_t * searchPipe(node_t ** previous, int fd) { + node_t * curr = firstPipe; * previous = NULL; while (curr != NULL) { - if (curr->pipe == pipe) { + if (curr->pipe->fd == fd) { break; } * previous = curr; @@ -103,7 +69,7 @@ node_t * search(node_t ** previous, pipe_t * pipe) { * previous = NULL; return NULL; } - if (curr == first) { + if (curr == firstPipe) { * previous = NULL; return curr; } diff --git a/Kernel/utils/scheduler.c b/Kernel/utils/scheduler.c index 16a6920..9448c6d 100644 --- a/Kernel/utils/scheduler.c +++ b/Kernel/utils/scheduler.c @@ -1,6 +1,7 @@ #include "scheduler.h" #define IDLE_PID 1 +// void _initialize_stack_frame(void *, void *, int, char**, void **, void **); void _initialize_stack_frame(void *, void *, int, char**); enum states {READY = 0, BLOCKED}; @@ -17,6 +18,8 @@ typedef struct processCDT { char foreground; enum states state; int * fd; + void * sseBytes; + void * fpuBytes; } processCDT; processCDT * firstReady = NULL; @@ -67,6 +70,7 @@ 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[2]) { if (firstReady != NULL && firstReady->pid == IDLE_PID) block(IDLE_PID); @@ -95,7 +99,10 @@ void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char * process->executions = 0; process->foreground = foreground; process->state = READY; + // process->sseBytes = pvPortMalloc(64); + // process->fpuBytes = pvPortMalloc(14); + // _initialize_stack_frame(fn, rbp, argc, argv, &(process->fpuBytes), &(process->sseBytes)); _initialize_stack_frame(fn, rbp, argc, argv); if (firstReady == NULL) @@ -115,6 +122,14 @@ void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char * return; } +void * getSSEaddress() { + return currentProcess->sseBytes; +} + +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; @@ -139,7 +154,7 @@ void newProcess(processADT process, char * name, char priority, char foreground, // exit(); // } -processADT search(processADT * previous, int pid, processADT first) { +processADT searchProcess(processADT * previous, int pid, processADT first) { processADT curr = first; * previous = NULL; while (curr != NULL) { @@ -162,7 +177,7 @@ processADT search(processADT * previous, int pid, processADT first) { char block(int pid) { processADT prev = NULL; - processADT del = search(&prev, pid, firstReady); + processADT del = searchProcess(&prev, pid, firstReady); if (del == NULL) return EXIT_FAILURE; else { @@ -192,7 +207,7 @@ char block(int pid) { char unblock(int pid) { processADT prev = NULL; - processADT del = search(&prev, pid, firstBlocked); + processADT del = searchProcess(&prev, pid, firstBlocked); if (del == NULL) return EXIT_FAILURE; else { @@ -213,9 +228,9 @@ char unblock(int pid) { char kill(int pid) { processADT prev = NULL; - processADT del = search(&prev, pid, firstReady); + processADT del = searchProcess(&prev, pid, firstReady); if (del == NULL) { - del = search(&prev, pid, firstBlocked); + del = searchProcess(&prev, pid, firstBlocked); if (del == NULL) return EXIT_FAILURE; else { @@ -246,6 +261,18 @@ char kill(int pid) { return EXIT_SUCCESS; } +int getFdOut() { + if (currentProcess == NULL) + return NULL; + return currentProcess->fd[1]; +} + +int getFdIn() { + if (currentProcess == NULL) + return NULL; + return currentProcess->fd[0]; +} + void exitProcess() { kill(currentProcess->pid); } @@ -280,14 +307,6 @@ char quitCPU() { return EXIT_SUCCESS; } -char addSpaces(char * str, char qty) { - char aux = qty; - while (qty-- > 0) { - *str++ = ' '; - } - return aux; -} - char getGenProcessData(char ** out, char * written, char toAdd, char * in, char isLast) { char copied = strcpy(*out, in); *out += copied; diff --git a/Kernel/utils/semaphore.c b/Kernel/utils/sem.c similarity index 83% rename from Kernel/utils/semaphore.c rename to Kernel/utils/sem.c index f15b79b..0c128aa 100644 --- a/Kernel/utils/semaphore.c +++ b/Kernel/utils/sem.c @@ -1,37 +1,14 @@ -#include -#include -#include -#include "lib.h" -#include "scheduler.h" -#include "memManager.h" +#include "sem.h" -#define MAX_SEM 100 -#define MAX_NAME 100 -#define SEM_DATA_MAX_SIZE 100 - -void enter_region(uint32_t * lock); -void leave_region(uint32_t * lock); static uint32_t semLock; -typedef struct pid_t { - int pid; - struct pid_t * next; -} pid_t; - -typedef struct sem_t { - unsigned int value; - char name[MAX_NAME]; - pid_t * entering; - pid_t * last; -} sem_t; - typedef struct node_t { sem_t * sem; struct node_t * next; } node_t; static sem_t semaphores[MAX_SEM]; -node_t * first = NULL; +node_t * firstSem = NULL; static char counter = 0; sem_t * semOpen(char * name, unsigned int value) { @@ -40,8 +17,8 @@ sem_t * semOpen(char * name, unsigned int value) { sem_t * sem = pvPortMalloc(sizeof(sem_t)); node_t * node = pvPortMalloc(sizeof(node_t)); node->sem = sem; - node->next = first; - first = node; + node->next = firstSem; + firstSem = node; strcpy(sem->name, name); sem->value = value; counter++; @@ -57,12 +34,12 @@ int semClose(sem_t * sem) { node_t * del = NULL; - if (first == sem) { - del = first; - first = first->next; + if (firstSem == sem) { + del = firstSem; + firstSem = firstSem->next; } else { - node_t * aux = first; + node_t * aux = firstSem; while (aux != NULL) { if (aux->next != NULL) if (aux->next->sem == sem) { @@ -176,7 +153,7 @@ char * getSems() { ans += strcpy(ans, info); // ans += 56; - node_t * aux = first; + node_t * aux = firstSem; while (aux != NULL) { char writtenChars = getSemaphoresData(ans, aux); if (writtenChars == EXIT_FAILURE) diff --git a/Userland/SampleCodeModule/include/libc.h b/Userland/SampleCodeModule/include/libc.h index 1e4596a..f7999ba 100644 --- a/Userland/SampleCodeModule/include/libc.h +++ b/Userland/SampleCodeModule/include/libc.h @@ -8,7 +8,9 @@ void printString(char * string); void printStringError(char * string); void printStringLen(char * string, int len); void new_line(); +void backspace(); char getChar(); +void putChar(char c); int abs(); int atoi(char * buffer, int len); int strlen(const char * s); diff --git a/Userland/SampleCodeModule/libc.c b/Userland/SampleCodeModule/libc.c index 1786bd1..85b3740 100644 --- a/Userland/SampleCodeModule/libc.c +++ b/Userland/SampleCodeModule/libc.c @@ -30,13 +30,17 @@ int strcmp(const char * s1, const char * s2) { void putChar(char c){ char buffer = c; - sys_write(1, &buffer, 1); + sys_write(1, &buffer, 0); } void new_line() { putChar('\n'); } +void backspace() { + putChar('\b'); +} + void winClear() { sys_write(1, "\e\f", 2); } diff --git a/Userland/SampleCodeModule/shell/commands/change.c b/Userland/SampleCodeModule/shell/commands/change.c deleted file mode 100644 index f7cccca..0000000 --- a/Userland/SampleCodeModule/shell/commands/change.c +++ /dev/null @@ -1,6 +0,0 @@ -#include "system.h" -#include "change.h" - -void change() { - sys_switchContext(); -} \ No newline at end of file diff --git a/Userland/SampleCodeModule/shell/commands/clear.c b/Userland/SampleCodeModule/shell/commands/clear.c index 8523675..027e93e 100644 --- a/Userland/SampleCodeModule/shell/commands/clear.c +++ b/Userland/SampleCodeModule/shell/commands/clear.c @@ -2,6 +2,6 @@ #include "shell.h" #include "clear.h" -void clear(char * window, int * offset) { - clearWindow(window, offset); +void clear() { + winClear(); } \ No newline at end of file diff --git a/Userland/SampleCodeModule/shell/commands/cpuid.c b/Userland/SampleCodeModule/shell/commands/cpuid.c index 7f2ecbb..54f0f4e 100644 --- a/Userland/SampleCodeModule/shell/commands/cpuid.c +++ b/Userland/SampleCodeModule/shell/commands/cpuid.c @@ -1,5 +1,4 @@ #include "cpu_id.h" -#include "shell.h" int cpu_id(int *, int *, int *, int *); @@ -101,12 +100,11 @@ 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(char * window, int * offset) { +void cpufeatures() { for (int i = 0; i < len; i++) { if (checks[i]()) { - addText(supports[i], window, offset); - substractLine(window, offset); + printString(supports[i]); + new_line(); } } - printWindow(window); } diff --git a/Userland/SampleCodeModule/shell/commands/help.c b/Userland/SampleCodeModule/shell/commands/help.c index 4dfe88d..801dfe2 100644 --- a/Userland/SampleCodeModule/shell/commands/help.c +++ b/Userland/SampleCodeModule/shell/commands/help.c @@ -1,13 +1,12 @@ #include "libc.h" #include "shell.h" -static char * info[] = {"change: Change the current working terminal (alternatively, press ALT+F1)", "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 = 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(char * window, int * offset) { +void help() { for (int i = 0; i < len; i++) { - addText(info[i], window, offset); - substractLine(window, offset); + printString(info[i]); + new_line(); } - printWindow(window); } \ No newline at end of file diff --git a/Userland/SampleCodeModule/shell/commands/inforeg.c b/Userland/SampleCodeModule/shell/commands/inforeg.c index fed08ee..1f6f5e9 100644 --- a/Userland/SampleCodeModule/shell/commands/inforeg.c +++ b/Userland/SampleCodeModule/shell/commands/inforeg.c @@ -10,20 +10,19 @@ static char * regsNames[] = { "R8: ", "R9: ", "R10: ", "R11: ", "R12: ", "R13: ", "R14: ", "R15: ", "RSP: ", "RIP: "}; -void inforeg(char * window, int * offset) { +void inforeg() { uint64_t * regs = (uint64_t *) _getRegs(); char bufferAux[20]; for (int i = 0; i < 17; i++) { if (i % 5 == 0 && i != 0) - substractLine(window, offset); - addText(regsNames[i], window, offset); - addText("0x", window, offset); - addText(gtoa(regs[i], bufferAux, 16, 20), window, offset); + new_line(); + printString(regsNames[i]); + printStringLen("0x", 2); + printString(gtoa(regs[i], bufferAux, 16, 20)); if (i != 14 && i % 5 != 4) - addText(" - ", window, offset); + printStringLen(" - ", 3); } - printWindow(window); - substractLine(window, offset); + new_line(); } \ No newline at end of file diff --git a/Userland/SampleCodeModule/shell/commands/printmem.c b/Userland/SampleCodeModule/shell/commands/printmem.c index 44b59c3..cfedf31 100644 --- a/Userland/SampleCodeModule/shell/commands/printmem.c +++ b/Userland/SampleCodeModule/shell/commands/printmem.c @@ -13,16 +13,15 @@ void getMem(int * buffer, int initialAddress) { } } -void printmem(char * window, int * offset, long dir) { +void printmem(long dir) { int buffer[8]; getMem(buffer, dir); for (int i = 0; i < 8; i++) { char bufferAux[8]; - addText("0x", window, offset); - addText(itoa(buffer[i], bufferAux, 16), window, offset); - substractLine(window, offset); + printStringLen("0x", 2); + printString(itoa(buffer[i], bufferAux, 16)); + new_line(); } - printWindow(window); } \ No newline at end of file diff --git a/Userland/SampleCodeModule/shell/commands/ps.c b/Userland/SampleCodeModule/shell/commands/ps.c index 6e4eb79..3ad71ad 100644 --- a/Userland/SampleCodeModule/shell/commands/ps.c +++ b/Userland/SampleCodeModule/shell/commands/ps.c @@ -1,10 +1,9 @@ #include "libc.h" #include "shell.h" -void ps(char * window, int * offset) { +void ps() { char * output = sys_ps(); - addText(output, window, offset); - printWindow(window); - substractLine(window, offset); - sys_free(output); + printString(output); + new_line(); + // 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 d009b58..fc0214b 100644 --- a/Userland/SampleCodeModule/shell/commands/quadratic.c +++ b/Userland/SampleCodeModule/shell/commands/quadratic.c @@ -2,41 +2,36 @@ #include "quadratic.h" #include "shell.h" -void quadratic(char * window, int * offset, double a, double b, double c) { +void quadratic(double a, double b, double c) { double sol1, sol2; if (a == 0) { - addText("First argument cannot be 0", window, offset); - printWindow(window); - substractLine(window, offset); + printStringLen("First argument cannot be 0\n", 28); return; } int cond = quadSolver(a, b, c, &sol1, &sol2); if (cond == 0) { - addText("Not real!", window, offset); - printWindow(window); - substractLine(window, offset); + printStringLen("Not real!\n", 11); return; } char buffer[30] = {0}; - addText("Roots for ", window, offset); + printStringLen("Roots for ", 11); ftoa(a, buffer, 10); - addText(buffer, window, offset); - addText("x^2 + ", window, offset); + printString(buffer); + printStringLen("x^2 + ", 7); ftoa(b, buffer, 10); - addText(buffer, window, offset); - addText("x + ", window, offset); + printString(buffer); + printString("x + "); ftoa(c, buffer, 10); - addText(buffer, window, offset); - substractLine(window, offset); + printString(buffer); + new_line(); ftoa(sol1, buffer, 10); - addText(buffer, window, offset); - addText(" and ", window, offset); + printString(buffer); + printStringLen(" and ", 6); char buffer2[30] = {0}; ftoa(sol2, buffer2, 10); - addText(buffer2, window, offset); - printWindow(window); - substractLine(window, offset); + printString(buffer2); + new_line(); } \ No newline at end of file diff --git a/Userland/SampleCodeModule/shell/commands/sem.c b/Userland/SampleCodeModule/shell/commands/sem.c deleted file mode 100644 index fbdda01..0000000 --- a/Userland/SampleCodeModule/shell/commands/sem.c +++ /dev/null @@ -1,10 +0,0 @@ -#include "libc.h" -#include "shell.h" - -void sem(char * window, int * offset) { - char * output = sys_sem(); - addText(output, window, offset); - printWindow(window); - substractLine(window, offset); - sys_free(output); -} \ No newline at end of file diff --git a/Userland/SampleCodeModule/shell/commands/semCom.c b/Userland/SampleCodeModule/shell/commands/semCom.c new file mode 100644 index 0000000..c58a8d7 --- /dev/null +++ b/Userland/SampleCodeModule/shell/commands/semCom.c @@ -0,0 +1,9 @@ +#include "libc.h" +#include "shell.h" + +void sem() { + char * output = sys_sem(); + printString(output); + new_line(); + // sys_free(output); +} \ No newline at end of file diff --git a/Userland/SampleCodeModule/shell/commands/switch.c b/Userland/SampleCodeModule/shell/commands/switch.c deleted file mode 100644 index e69de29..0000000 diff --git a/Userland/SampleCodeModule/shell/commands/time.c b/Userland/SampleCodeModule/shell/commands/time.c index dc249b3..83adfc4 100644 --- a/Userland/SampleCodeModule/shell/commands/time.c +++ b/Userland/SampleCodeModule/shell/commands/time.c @@ -27,26 +27,24 @@ int getYear() { return sys_time(YEAR); } -void printTime(int number, char * window, int * offset) { +void printTime(int number) { char buffer[3] = {0}; buffer[0] = number / 10 + '0'; buffer[1] = number % 10 + '0'; buffer[2] = '\0'; - addText(buffer, window, offset); + printString(buffer); } void time(char * window, int * offset) { - printTime(getDay(), window, offset); - addText("/", window, offset); - printTime(getMonth(), window, offset); - addText("/", window, offset); - printTime(getYear(), window, offset); - addText(" ", window, offset); - printTime(getHours(), window, offset); - addText(":", window, offset); - printTime(getMinutes(), window, offset); - addText(":", window, offset); - printTime(getSeconds(), window, offset); - printWindow(window); - substractLine(window, offset); + printTime(getDay()); + putChar('/'); + printTime(getMonth()); + putChar('/'); + printTime(getYear()); + putChar(' '); + printTime(getHours()); + putChar(':'); + printTime(getMinutes()); + putChar(':'); + printTime(getSeconds()); } \ No newline at end of file diff --git a/Userland/SampleCodeModule/shell/include/change.h b/Userland/SampleCodeModule/shell/include/change.h deleted file mode 100644 index 6f58a2a..0000000 --- a/Userland/SampleCodeModule/shell/include/change.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef CHANGE_H -#define CHANGE_H - -void change(); - -#endif \ No newline at end of file diff --git a/Userland/SampleCodeModule/shell/include/clear.h b/Userland/SampleCodeModule/shell/include/clear.h index 2767810..22df688 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(char * window, int * offset); - +#ifndef CLEAR_LIB +#define CLEAR_LIB + +void clear(); + #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 7256d9a..1417b1a 100644 --- a/Userland/SampleCodeModule/shell/include/cpu_id.h +++ b/Userland/SampleCodeModule/shell/include/cpu_id.h @@ -1,5 +1,8 @@ -#ifndef CPUID -#define CPUID +#ifndef CPUID_H +#define CPUID_H + +#include "libc.h" +#include "shell.h" // Tabla obtenidad de: https://wiki.osdev.org/CPUID enum { @@ -68,7 +71,7 @@ enum { }; -void cpufeatures(char * window, int * offset); +void cpufeatures(); int cpu_id_support(void); #endif \ No newline at end of file diff --git a/Userland/SampleCodeModule/shell/include/help.h b/Userland/SampleCodeModule/shell/include/help.h index fde789e..6c9cc93 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(char * window, int * offset); +void help(); #endif \ No newline at end of file diff --git a/Userland/SampleCodeModule/shell/include/inforeg.h b/Userland/SampleCodeModule/shell/include/inforeg.h index febfdab..107b568 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(char*, int*); +void inforeg(); #endif \ No newline at end of file diff --git a/Userland/SampleCodeModule/shell/include/printmem.h b/Userland/SampleCodeModule/shell/include/printmem.h index f47a5ae..11afa3d 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(char* window, int* offset, long); +void printmem(long); #endif \ No newline at end of file diff --git a/Userland/SampleCodeModule/shell/include/ps.h b/Userland/SampleCodeModule/shell/include/ps.h index 0b1df69..44cb239 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(char * window, int * offset); +void ps(); #endif \ No newline at end of file diff --git a/Userland/SampleCodeModule/shell/include/quadratic.h b/Userland/SampleCodeModule/shell/include/quadratic.h index f22ba78..0e28c4f 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(char*, int*, double, double, double); +void quadratic(double, double, double); #endif \ No newline at end of file diff --git a/Userland/SampleCodeModule/shell/include/sem.h b/Userland/SampleCodeModule/shell/include/sem.h deleted file mode 100644 index 0f9d5ed..0000000 --- a/Userland/SampleCodeModule/shell/include/sem.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef SEM_LIB -#define SEM_LIB - -void sem(char * window, int * offset); - -#endif \ No newline at end of file diff --git a/Userland/SampleCodeModule/shell/include/semCom.h b/Userland/SampleCodeModule/shell/include/semCom.h new file mode 100644 index 0000000..d2b9e81 --- /dev/null +++ b/Userland/SampleCodeModule/shell/include/semCom.h @@ -0,0 +1,6 @@ +#ifndef SEM_LIB +#define SEM_LIB + +void sem(); + +#endif \ No newline at end of file diff --git a/Userland/SampleCodeModule/shell/include/shell.h b/Userland/SampleCodeModule/shell/include/shell.h index ba19291..1091a75 100644 --- a/Userland/SampleCodeModule/shell/include/shell.h +++ b/Userland/SampleCodeModule/shell/include/shell.h @@ -4,11 +4,7 @@ #include "system.h" void shell(int argc, char *argv[]); -void printWindow(char * window); -void addText(char * buffer, char * window, int * offset); -void incorrect_comm(char * buffer, char * window, int * offset); -void incorrect_arg(char * command, char * window, int * offset); -void clearWindow(char * window, int * offset); -void substractLine(char * window, int * offset); +void incorrect_comm(char * buffer); +void incorrect_arg(char * command); #endif \ No newline at end of file diff --git a/Userland/SampleCodeModule/shell/include/time.h b/Userland/SampleCodeModule/shell/include/time.h index 1479e8a..bf073e0 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(char * window, int * offset); +void time(); #define SECONDS 0 #define MINUTES 2 diff --git a/Userland/SampleCodeModule/shell/oldShell.c b/Userland/SampleCodeModule/shell/oldShell.c new file mode 100644 index 0000000..89ae1a3 --- /dev/null +++ b/Userland/SampleCodeModule/shell/oldShell.c @@ -0,0 +1,158 @@ +/* +#include "libc.h" +#include "help.h" +#include "clear.h" +#include "time.h" +#include "shell.h" +#include "inforeg.h" +#include "printmem.h" +#include "excDiv.h" +#include "excOP.h" +#include "quadratic.h" +#include "cpu_id.h" +#include "change.h" +#include "ps.h" +#include "semCom.h" + +#define SIZE 100 +#define MAX_ARGS 5 + +#define COLS 80 +#define ROWS 25 + +const int len = 9; +char *commands_void[] = {"help", "time", "inforeg", "excdiv", "excop", "clear", "cpufeatures", "ps", "sem"}; +void (*func []) (char *, int *) = {help, time, inforeg, excdiv, excop, clear, cpufeatures, ps, semCom}; + +void substractLine(char * window, int * offset) { + for (int i = 0; i < ROWS - 1; i++) { + for (int j = 0; j < COLS; j++) { + window[i * COLS + j] = window[(i + 1) * COLS + j]; + } + } + for (int i = 0; i < COLS; i++) { + window[(ROWS - 1) * COLS + i - 1] = ' '; + window[ROWS * COLS] = 0; + } + *offset = (ROWS - 1) * COLS; +} + +void addText(char * buffer, char * window, int * offset) { + while (*buffer != 0) { + if (*offset == ROWS * COLS - 1) substractLine(window, offset); + if (*buffer == '\n') { + buffer++; + substractLine(window, offset); + } + window[(*offset)++] = *buffer++; + } +} + +void printWindow(char * window) { + printStringLen(window, ROWS * COLS); +} + +void scanfNoPrint(char * buffer, int maxSize, char * window, int * offset) { + char c; + int i = 0; + while ((c = getChar()) != '\n' && i < maxSize - 1) { + if (c != -1) { + // if (c == '\v') + // sys_switchContext(); + // else if (c == '\b' && i > 0) { + if (c == '\b' && i > 0) { + buffer[--i] = ' '; + window[--(*offset)] = ' '; + printWindow(window); + } + else if (c != 0 && c != '\b') { + buffer[i++] = c; + if (*offset == ROWS * COLS - 1) substractLine(window, offset); + window[(*offset)++] = c; + printWindow(window); + } + } + } + buffer[i] = '\0'; + if (*offset == ROWS * COLS - 1) substractLine(window, offset); + window[*offset] = ' '; +} + +void clearWindow(char * window, int * offset) { + for (int i = 0; i <= ROWS * COLS; i++) { + window[i] = ' '; + } + window[ROWS * COLS] = 0; + *offset = (ROWS - 1) * COLS; + printWindow(window); +} + +// static char window[ROWS * COLS + 1] = {[0 ... ROWS * COLS - 1] = ' ', 0}; +// static char buffer[SIZE] = {0}; +// static char *tokens[SIZE] = {0}; +void shell(int argc, char *argv[]) { + char window[ROWS * COLS + 1] = {[0 ... ROWS * COLS - 1] = ' ', 0}; + int offset = (ROWS - 1) * COLS; + + printWindow(window); + while (1) { + int comm_flag = 0; + addText("$> ", window, &offset); + printWindow(window); + char buffer[SIZE] = {0}; + scanfNoPrint(buffer, SIZE, window, &offset); + substractLine(window, &offset); + 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], window, &offset); + else + (*func[i])(window, &offset); + comm_flag = 1; + } + } + if (!strcmp(tokens[0], "quadratic")) { + if (*tokens[4] != 0 || *tokens[3] == 0) + incorrect_arg(tokens[0], window, &offset); + else if (!isFloat(tokens[1]) || !isFloat(tokens[2]) || !isFloat(tokens[3])) + incorrect_arg(tokens[0], window, &offset); + else + quadratic(window, &offset, 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], window, &offset); + else { + int length = strlen(tokens[1]); + printmem(window, &offset, atoi(tokens[1], length)); + } + comm_flag = 1; + } + + if (!comm_flag) { + if (*tokens[0] != 0) + incorrect_comm(tokens[0], window, &offset); + } + } +} + +void incorrect_comm(char * buffer, char* window, int * offset) { + addText(buffer, window, offset); + addText(" is not a BottlerShell command", window, offset); + printWindow(window); + substractLine(window, offset); +} + +void incorrect_arg(char * command, char* window, int * offset) { + addText("Incorrect arguments for command ", window, offset); + addText(command, window, offset); + printWindow(window); + substractLine(window, offset); +} +*/ \ No newline at end of file diff --git a/Userland/SampleCodeModule/shell/shell.c b/Userland/SampleCodeModule/shell/shell.c index 85ca57a..f416031 100644 --- a/Userland/SampleCodeModule/shell/shell.c +++ b/Userland/SampleCodeModule/shell/shell.c @@ -1,17 +1,16 @@ #include "libc.h" #include "help.h" -#include "clear.h" #include "time.h" #include "shell.h" +#include "clear.h" #include "inforeg.h" #include "printmem.h" #include "excDiv.h" #include "excOP.h" #include "quadratic.h" #include "cpu_id.h" -#include "change.h" #include "ps.h" -#include "sem.h" +#include "semCom.h" #define SIZE 100 #define MAX_ARGS 5 @@ -21,86 +20,33 @@ const int len = 9; char *commands_void[] = {"help", "time", "inforeg", "excdiv", "excop", "clear", "cpufeatures", "ps", "sem"}; -void (*func []) (char *, int *) = {help, time, inforeg, excdiv, excop, clear, cpufeatures, ps, sem}; +void (*func []) () = {help, time, inforeg, excdiv, excop, clear, cpufeatures, ps, sem}; -void substractLine(char * window, int * offset) { - for (int i = 0; i < ROWS - 1; i++) { - for (int j = 0; j < COLS; j++) { - window[i * COLS + j] = window[(i + 1) * COLS + j]; - } - } - for (int i = 0; i < COLS; i++) { - window[(ROWS - 1) * COLS + i - 1] = ' '; - window[ROWS * COLS] = 0; - } - *offset = (ROWS - 1) * COLS; -} - -void addText(char * buffer, char * window, int * offset) { - while (*buffer != 0) { - if (*offset == ROWS * COLS - 1) substractLine(window, offset); - if (*buffer == '\n') { - buffer++; - substractLine(window, offset); - } - window[(*offset)++] = *buffer++; - } -} - -void printWindow(char * window) { - printStringLen(window, ROWS * COLS); -} - -void scanfNoPrint(char * buffer, int maxSize, char * window, int * offset) { +void scanfNoPrint(char * buffer) { char c; int i = 0; - while ((c = getChar()) != '\n' && i < maxSize - 1) { + while ((c = getChar()) != '\n' && i < SIZE - 1) { if (c != -1) { - // if (c == '\v') - // sys_switchContext(); - // else if (c == '\b' && i > 0) { if (c == '\b' && i > 0) { buffer[--i] = ' '; - window[--(*offset)] = ' '; - printWindow(window); + backspace(); } else if (c != 0 && c != '\b') { buffer[i++] = c; - if (*offset == ROWS * COLS - 1) substractLine(window, offset); - window[(*offset)++] = c; - printWindow(window); + putChar(c); } } } buffer[i] = '\0'; - if (*offset == ROWS * COLS - 1) substractLine(window, offset); - window[*offset] = ' '; } -void clearWindow(char * window, int * offset) { - for (int i = 0; i <= ROWS * COLS; i++) { - window[i] = ' '; - } - window[ROWS * COLS] = 0; - *offset = (ROWS - 1) * COLS; - printWindow(window); -} - -// static char window[ROWS * COLS + 1] = {[0 ... ROWS * COLS - 1] = ' ', 0}; -// static char buffer[SIZE] = {0}; -// static char *tokens[SIZE] = {0}; void shell(int argc, char *argv[]) { - char window[ROWS * COLS + 1] = {[0 ... ROWS * COLS - 1] = ' ', 0}; - int offset = (ROWS - 1) * COLS; - - printWindow(window); while (1) { int comm_flag = 0; - addText("$> ", window, &offset); - printWindow(window); + printStringLen("$> ", 3); char buffer[SIZE] = {0}; - scanfNoPrint(buffer, SIZE, window, &offset); - substractLine(window, &offset); + scanfNoPrint(buffer); + new_line(); char* tokens[SIZE] = {0}; tokens[0] = strstrip(buffer, ' '); for (int i = 1; i < MAX_ARGS; i++) { @@ -109,48 +55,45 @@ void shell(int argc, char *argv[]) { for (int i = 0; i < len; i++) { if (!strcmp(tokens[0], commands_void[i])) { if (*tokens[1] != 0) - incorrect_arg(tokens[0], window, &offset); + incorrect_arg(tokens[0]); else - (*func[i])(window, &offset); + (*func[i])(); comm_flag = 1; } } if (!strcmp(tokens[0], "quadratic")) { if (*tokens[4] != 0 || *tokens[3] == 0) - incorrect_arg(tokens[0], window, &offset); + incorrect_arg(tokens[0]); else if (!isFloat(tokens[1]) || !isFloat(tokens[2]) || !isFloat(tokens[3])) - incorrect_arg(tokens[0], window, &offset); + incorrect_arg(tokens[0]); else - quadratic(window, &offset, atof(tokens[1]), atof(tokens[2]), atof(tokens[3])); + 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], window, &offset); + incorrect_arg(tokens[0]); else { int length = strlen(tokens[1]); - printmem(window, &offset, atoi(tokens[1], length)); + printmem(atoi(tokens[1], length)); } comm_flag = 1; } if (!comm_flag) { if (*tokens[0] != 0) - incorrect_comm(tokens[0], window, &offset); + incorrect_comm(tokens[0]); } } } -void incorrect_comm(char * buffer, char* window, int * offset) { - addText(buffer, window, offset); - addText(" is not a BottlerShell command", window, offset); - printWindow(window); - substractLine(window, offset); +void incorrect_comm(char * buffer) { + printString(buffer); + printStringLen(" is not a BottlerShell command\n", 32); } -void incorrect_arg(char * command, char* window, int * offset) { - addText("Incorrect arguments for command ", window, offset); - addText(command, window, offset); - printWindow(window); - substractLine(window, offset); +void incorrect_arg(char * command) { + printStringLen("Incorrect arguments for command ", 33); + printString(command); + new_line(); } \ No newline at end of file