Make a lot of changes in shell and fix some bugs

Co-authored-by: Ezequiel Bellver <ebellver@itba.edu.ar>
Co-authored-by: Juan Barmasch <jbarmasch@itba.edu.ar>
This commit is contained in:
Santiago Lo Coco 2021-10-26 22:18:19 -03:00
parent 86f6f72574
commit af241224c9
44 changed files with 570 additions and 421 deletions

View File

@ -30,6 +30,8 @@ GLOBAL loadProcess
GLOBAL _initialize_stack_frame GLOBAL _initialize_stack_frame
GLOBAL _switchContext GLOBAL _switchContext
EXTERN getFPUaddress, getSSEaddress
SECTION .text SECTION .text
%macro pushState 0 %macro pushState 0
@ -136,9 +138,44 @@ picSlaveMask:
pop rbp pop rbp
retn 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) ;8254 Timer (Timer Tick)
_irq00Handler: _irq00Handler:
pushState pushState
; push rax
; call getFPUaddress
; fsave [rax]
; call getSSEaddress
; fxsave [rax]
; pop rax
; fsave [bytesForFPU] ; fsave [bytesForFPU]
; fxsave [bytesForSSEAligned] ; fxsave [bytesForSSEAligned]
@ -156,9 +193,13 @@ _irq00Handler:
; fxrstor [bytesForSSEAligned] ; fxrstor [bytesForSSEAligned]
; frstor [bytesForFPU] ; frstor [bytesForFPU]
; push rax
; call getFPUaddress
; frstor [rax]
; call getSSEaddress
; fxrstor [rax]
; pop rax
popState popState
; pop rsi ; argv
; pop rdi ; argc 6004d8
iretq iretq
;Keyboard ;Keyboard
@ -236,16 +277,24 @@ _initialize_stack_frame:
push 0x0 ; ss push 0x0 ; ss
push rsi ; sp push rsi ; sp
push 0x202 ; rflags push 0x202 ; rflags
push 0x08 ; cs -- offset de la GDT push 0x08 ; cs
push rdi ; IP push rdi ; IP
; push rdx ; argc
; push rcx ; argv mov rsi, rcx
mov rdi, rdx
pushState pushState
; mov rdi, rsp ; fsave [bytesForFPU]
; call newStack ; push rax
; mov dword [auxi], 1
; call getFPUaddress
; call getSSEaddress
; pop rax
; fxsave [bytesForSSEAligned]
; fsave [r8]
; fxsave [r9]
; mov rax, rsp
mov rsp, r10 mov rsp, r10
ret ret
@ -262,41 +311,19 @@ _systemCallsHandler:
popStateNoRax popStateNoRax
iretq 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 SECTION .data
align 16 align 16
bytesForSSEAligned times 512 db 0 bytesForSSEAligned times 512 db 0
; bytesForSSEAligned1 times 512 db 0
; bytesForSSEAligned2 times 512 db 0
; counter dd 1
SECTION .bss SECTION .bss
aux resq 1 aux resq 1
bytesForSSE resb 512 bytesForSSE resb 512
bytesForFPU resb 108 bytesForFPU resb 108
; bytesForFPU1 resb 108
; bytesForFPU2 resb 108
insPointer resb 8 insPointer resb 8
rspPointer resb 8 rspPointer resb 8
; auxi resb 4

View File

@ -1,8 +1,8 @@
#include <stdint.h> #include <stdint.h>
#include "video.h" #include "video.h"
static uint8_t * const video = (uint8_t*)0xB8000; static uint8_t * const video = (uint8_t*) 0xB8000;
static uint8_t * currentVideo = (uint8_t*)0xB8000; static uint8_t * currentVideo = (uint8_t*) 0xB8000;
static const int width = 80; static const int width = 80;
static const int height = 25; static const int height = 25;
static int currentX = 0; static int currentX = 0;
@ -11,39 +11,6 @@ static int currentY = 0;
int limitX[2] = {0, 80}; int limitX[2] = {0, 80};
int limitY[2] = {0, 25}; 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() { void increment() {
currentX++; currentX++;
if (currentX >= limitX[1]) { if (currentX >= limitX[1]) {
@ -89,7 +56,10 @@ int printStringLen(int color, const char * string, int maxLen) {
if (*string == '\n') { if (*string == '\n') {
new_line(); new_line();
return i; // return i;
string++;
i++;
continue;
} }
else if (*string == '\b') { else if (*string == '\b') {
backspace(); backspace();
@ -109,9 +79,7 @@ int printStringLen(int color, const char * string, int maxLen) {
} }
void backspace() { void backspace() {
if (currentVideo > video) { if (video + currentY * width * 2 + currentX * 2 > video) {
currentVideo -= 2;
*currentVideo = ' ';
currentX--; currentX--;
if (currentX < limitX[0]) { if (currentX < limitX[0]) {
currentX = limitX[1]; currentX = limitX[1];
@ -119,6 +87,7 @@ void backspace() {
currentY--; currentY--;
} }
} }
*(video + currentY * width * 2 + currentX * 2) = ' ';
} }
void new_line() { void new_line() {

View File

@ -12,5 +12,6 @@ int abs(int value);
char * itoa(int value, char * buffer, int base, int length); char * itoa(int value, char * buffer, int base, int length);
void strlen(const char *str, int *len); void strlen(const char *str, int *len);
int strcpy(char * strDest, const char * strSrc); int strcpy(char * strDest, const char * strSrc);
char addSpaces(char * str, char qty);
#endif #endif

18
Kernel/include/pipe.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef PIPE_H
#define PIPE_H
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#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

26
Kernel/include/pipeLib.h Normal file
View File

@ -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

View File

@ -1,19 +1,18 @@
#ifndef SCHEDULER_H #ifndef SCHEDULER_H
#define SCHEDULER_H #define SCHEDULER_H
#include "queue.h"
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#include "memManager.h" #include "memManager.h"
#include <stdlib.h>
#include "lib.h" #include "lib.h"
#include <stddef.h>
#include "schedulerLib.h"
void forceTimer(); void forceTimer();
void haltcpu(); void haltcpu();
#define STACK_SIZE 1024 * 512 #define STACK_SIZE 1024 * 512
#define MAX_PRIORITY 40 // Maximum number for a priority #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 DEF_PRIORITY 0
#define PROCESS_DATA_MAX_SIZE 100 #define PROCESS_DATA_MAX_SIZE 100
#define MAX_ATTR_SIZE 6 #define MAX_ATTR_SIZE 6
@ -21,20 +20,10 @@ void haltcpu();
typedef struct processCDT * processADT; typedef struct processCDT * processADT;
uint64_t nextProcess();
void idle(); 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); void newProcess(processADT process, char * name, char priority, char foreground, uint64_t rsp, uint64_t rbp);
char block(int pid); uint64_t nextProcess();
char unblock(int pid);
char kill(int pid);
void exitProcess();
char nice(char offset);
void updateRSP(uint64_t newRsp); void updateRSP(uint64_t newRsp);
int getPid();
char quitCPU();
char getProcessData(char * out, processADT proc); char getProcessData(char * out, processADT proc);
char * processes();
#endif #endif

View File

@ -0,0 +1,19 @@
#ifndef SCHEDULERLIB_H
#define SCHEDULERLIB_H
#include <stdint.h>
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

18
Kernel/include/sem.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef SEM_H
#define SEM_H
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#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

23
Kernel/include/semLib.h Normal file
View File

@ -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

View File

@ -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

View File

@ -3,7 +3,10 @@
#include "keyboard.h" #include "keyboard.h"
#include "time.h" #include "time.h"
#include "pcb.h" #include "pcb.h"
#include "pipeLib.h"
#include "schedulerLib.h"
#define STDIN 0
#define STDOUT 1 #define STDOUT 1
#define STDERR 2 #define STDERR 2
@ -16,8 +19,14 @@ uint64_t write(uint64_t fd, uint64_t buffer, uint64_t length) {
if (fd == STDOUT) if (fd == STDOUT)
color = STDOUT_COLOR; color = STDOUT_COLOR;
else if (fd == STDERR) else {
color = STDERR_COLOR; fd = getFdOut();
int i = 0;
while (bufferAux[i] != '\0' && i++ <= length) {
writePipe(fd, bufferAux[i]);
}
return i;
}
return printStringLen(color, bufferAux, (int) length); return printStringLen(color, bufferAux, (int) length);
} }
@ -26,6 +35,8 @@ uint64_t read(uint64_t fd, uint64_t buffer, uint64_t length) {
char * bufferAux = (char *) buffer; char * bufferAux = (char *) buffer;
int readBytes = 0; int readBytes = 0;
if (fd == STDIN) {
while (length-- > 0) { while (length-- > 0) {
*bufferAux = getKeyFromBuffer(); *bufferAux = getKeyFromBuffer();
if (*bufferAux == 0) if (*bufferAux == 0)
@ -33,10 +44,15 @@ uint64_t read(uint64_t fd, uint64_t buffer, uint64_t length) {
readBytes++; readBytes++;
bufferAux++; bufferAux++;
} }
}
else {
fd = getFdIn();
readBytes += strcpy(buffer, readPipe(fd));
}
return readBytes; return readBytes;
} }
void createProcess(void (*fn)) { // void createProcess(void (*fn)) {
newProcess(fn); // newProcess(fn);
} // }

View File

@ -67,6 +67,14 @@ int abs(int value) {
return value < 0 ? -value : 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) { char * itoa(int value, char * buffer, int base, int length) {
if (base < 2 || base > 32) { if (base < 2 || base > 32) {
return buffer; return buffer;

View File

@ -25,7 +25,9 @@ typedef unsigned long UBaseType_t;
#define configTOTAL_HEAP_SIZE 1024 * 1024 * 100 #define configTOTAL_HEAP_SIZE 1024 * 1024 * 100
#define configAPPLICATION_ALLOCATED_HEAP 0 #define configAPPLICATION_ALLOCATED_HEAP 0
#define portBYTE_ALIGNMENT 8 #define portBYTE_ALIGNMENT 8
// #define portBYTE_ALIGNMENT 16
#define portBYTE_ALIGNMENT_MASK ( 0x0007 ) // 8 #define portBYTE_ALIGNMENT_MASK ( 0x0007 ) // 8
// #define portBYTE_ALIGNMENT_MASK ( 0x000F ) // 16
#define pdFALSE ( ( BaseType_t ) 0 ) #define pdFALSE ( ( BaseType_t ) 0 )
#define pdTRUE ( ( BaseType_t ) 1 ) #define pdTRUE ( ( BaseType_t ) 1 )
#define portPOINTER_SIZE_TYPE uint32_t #define portPOINTER_SIZE_TYPE uint32_t
@ -198,12 +200,12 @@ void *pvReturn = NULL;
else { else {
ncNewline(); ncNewline();
ncPrint("MALLOC: "); // ncPrint("MALLOC: ");
ncPrintDec(xFreeBytesRemaining); // ncPrintDec(xFreeBytesRemaining);
ncPrint(" "); // ncPrint(" ");
ncPrintDec(xWantedSize); // ncPrintDec(xWantedSize);
ncPrint(" "); // ncPrint(" ");
ncPrintDec(configADJUSTED_HEAP_SIZE); // ncPrintDec(configADJUSTED_HEAP_SIZE);
ncNewline(); ncNewline();
} }
} }

View File

@ -1,99 +1,65 @@
#include <stdint.h> #include "pipe.h"
#include <stddef.h>
#include <stdlib.h>
#include "lib.h"
#include "scheduler.h"
#include "memManager.h"
#include "semaphore.h"
#define MAX_SEM 100 int fds = 2;
#define MAX_NAME 100
#define PIPE_MAX_SIZE 1024
#define SEM_NAME "Pipes"
typedef struct pipe_t { node_t * firstPipe;
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;
// }
pipe_t * openPipe(char * name) { pipe_t * openPipe(char * name) {
// semWait(sem);
pipe_t * pipe = pvPortMalloc(sizeof(pipe_t)); pipe_t * pipe = pvPortMalloc(sizeof(pipe_t));
strcpy(pipe->name, name); 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 NULL;
return pipe; return pipe;
// semPost(sem);
} }
void writePipe(pipe_t * pipe, char c) { void writePipe(int fd, char c) {
if (!exists(pipe)) node_t * prev = NULL;
return; 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) { char readPipe(int fd) {
if (!exists(pipe)) node_t * prev = NULL;
return -1; 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; return c;
} }
void closePipe(pipe_t * pipe) { void closePipe(int fd) {
node_t * prev = NULL; node_t * prev = NULL;
node_t * del = search(&prev, pipe); node_t * del = searchPipe(&prev, fd);
if (del == NULL) if (del == NULL)
return; return;
semWait(pipe->sem); semWait(del->pipe->sem);
if (prev != NULL) if (prev != NULL)
prev->next = del->next; prev->next = del->next;
else first->next = del->next; else firstPipe->next = del->next;
vPortFree(pipe); vPortFree(del->pipe);
vPortFree(del); vPortFree(del);
semPost(pipe->sem); semPost(del->pipe->sem);
} }
int exists(pipe_t * pipe) { node_t * searchPipe(node_t ** previous, int fd) {
node_t * prev = NULL; node_t * curr = firstPipe;
return search(&prev, pipe) != NULL;
}
node_t * search(node_t ** previous, pipe_t * pipe) {
node_t * curr = first;
* previous = NULL; * previous = NULL;
while (curr != NULL) { while (curr != NULL) {
if (curr->pipe == pipe) { if (curr->pipe->fd == fd) {
break; break;
} }
* previous = curr; * previous = curr;
@ -103,7 +69,7 @@ node_t * search(node_t ** previous, pipe_t * pipe) {
* previous = NULL; * previous = NULL;
return NULL; return NULL;
} }
if (curr == first) { if (curr == firstPipe) {
* previous = NULL; * previous = NULL;
return curr; return curr;
} }

View File

@ -1,6 +1,7 @@
#include "scheduler.h" #include "scheduler.h"
#define IDLE_PID 1 #define IDLE_PID 1
// void _initialize_stack_frame(void *, void *, int, char**, void **, void **);
void _initialize_stack_frame(void *, void *, int, char**); void _initialize_stack_frame(void *, void *, int, char**);
enum states {READY = 0, BLOCKED}; enum states {READY = 0, BLOCKED};
@ -17,6 +18,8 @@ typedef struct processCDT {
char foreground; char foreground;
enum states state; enum states state;
int * fd; int * fd;
void * sseBytes;
void * fpuBytes;
} processCDT; } processCDT;
processCDT * firstReady = NULL; 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[]) {
// void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char *argv[], int * fd[2]) {
if (firstReady != NULL && firstReady->pid == IDLE_PID) if (firstReady != NULL && firstReady->pid == IDLE_PID)
block(IDLE_PID); block(IDLE_PID);
@ -95,7 +99,10 @@ void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char *
process->executions = 0; process->executions = 0;
process->foreground = foreground; process->foreground = foreground;
process->state = READY; 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); _initialize_stack_frame(fn, rbp, argc, argv);
if (firstReady == NULL) if (firstReady == NULL)
@ -115,6 +122,14 @@ void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char *
return; 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) { void newProcess(processADT process, char * name, char priority, char foreground, uint64_t rsp, uint64_t rbp) {
// char aux[MAX_NAME_SIZE]; // char aux[MAX_NAME_SIZE];
// int j; // int j;
@ -139,7 +154,7 @@ void newProcess(processADT process, char * name, char priority, char foreground,
// exit(); // exit();
// } // }
processADT search(processADT * previous, int pid, processADT first) { processADT searchProcess(processADT * previous, int pid, processADT first) {
processADT curr = first; processADT curr = first;
* previous = NULL; * previous = NULL;
while (curr != NULL) { while (curr != NULL) {
@ -162,7 +177,7 @@ processADT search(processADT * previous, int pid, processADT first) {
char block(int pid) { char block(int pid) {
processADT prev = NULL; processADT prev = NULL;
processADT del = search(&prev, pid, firstReady); processADT del = searchProcess(&prev, pid, firstReady);
if (del == NULL) if (del == NULL)
return EXIT_FAILURE; return EXIT_FAILURE;
else { else {
@ -192,7 +207,7 @@ char block(int pid) {
char unblock(int pid) { char unblock(int pid) {
processADT prev = NULL; processADT prev = NULL;
processADT del = search(&prev, pid, firstBlocked); processADT del = searchProcess(&prev, pid, firstBlocked);
if (del == NULL) if (del == NULL)
return EXIT_FAILURE; return EXIT_FAILURE;
else { else {
@ -213,9 +228,9 @@ char unblock(int pid) {
char kill(int pid) { char kill(int pid) {
processADT prev = NULL; processADT prev = NULL;
processADT del = search(&prev, pid, firstReady); processADT del = searchProcess(&prev, pid, firstReady);
if (del == NULL) { if (del == NULL) {
del = search(&prev, pid, firstBlocked); del = searchProcess(&prev, pid, firstBlocked);
if (del == NULL) if (del == NULL)
return EXIT_FAILURE; return EXIT_FAILURE;
else { else {
@ -246,6 +261,18 @@ char kill(int pid) {
return EXIT_SUCCESS; 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() { void exitProcess() {
kill(currentProcess->pid); kill(currentProcess->pid);
} }
@ -280,14 +307,6 @@ char quitCPU() {
return EXIT_SUCCESS; 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 getGenProcessData(char ** out, char * written, char toAdd, char * in, char isLast) {
char copied = strcpy(*out, in); char copied = strcpy(*out, in);
*out += copied; *out += copied;

View File

@ -1,37 +1,14 @@
#include <stdint.h> #include "sem.h"
#include <stddef.h>
#include <stdlib.h>
#include "lib.h"
#include "scheduler.h"
#include "memManager.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; 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 { typedef struct node_t {
sem_t * sem; sem_t * sem;
struct node_t * next; struct node_t * next;
} node_t; } node_t;
static sem_t semaphores[MAX_SEM]; static sem_t semaphores[MAX_SEM];
node_t * first = NULL; node_t * firstSem = NULL;
static char counter = 0; static char counter = 0;
sem_t * semOpen(char * name, unsigned int value) { 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)); sem_t * sem = pvPortMalloc(sizeof(sem_t));
node_t * node = pvPortMalloc(sizeof(node_t)); node_t * node = pvPortMalloc(sizeof(node_t));
node->sem = sem; node->sem = sem;
node->next = first; node->next = firstSem;
first = node; firstSem = node;
strcpy(sem->name, name); strcpy(sem->name, name);
sem->value = value; sem->value = value;
counter++; counter++;
@ -57,12 +34,12 @@ int semClose(sem_t * sem) {
node_t * del = NULL; node_t * del = NULL;
if (first == sem) { if (firstSem == sem) {
del = first; del = firstSem;
first = first->next; firstSem = firstSem->next;
} }
else { else {
node_t * aux = first; node_t * aux = firstSem;
while (aux != NULL) { while (aux != NULL) {
if (aux->next != NULL) if (aux->next != NULL)
if (aux->next->sem == sem) { if (aux->next->sem == sem) {
@ -176,7 +153,7 @@ char * getSems() {
ans += strcpy(ans, info); ans += strcpy(ans, info);
// ans += 56; // ans += 56;
node_t * aux = first; node_t * aux = firstSem;
while (aux != NULL) { while (aux != NULL) {
char writtenChars = getSemaphoresData(ans, aux); char writtenChars = getSemaphoresData(ans, aux);
if (writtenChars == EXIT_FAILURE) if (writtenChars == EXIT_FAILURE)

View File

@ -8,7 +8,9 @@ void printString(char * string);
void printStringError(char * string); void printStringError(char * string);
void printStringLen(char * string, int len); void printStringLen(char * string, int len);
void new_line(); void new_line();
void backspace();
char getChar(); char getChar();
void putChar(char c);
int abs(); int abs();
int atoi(char * buffer, int len); int atoi(char * buffer, int len);
int strlen(const char * s); int strlen(const char * s);

View File

@ -30,13 +30,17 @@ int strcmp(const char * s1, const char * s2) {
void putChar(char c){ void putChar(char c){
char buffer = c; char buffer = c;
sys_write(1, &buffer, 1); sys_write(1, &buffer, 0);
} }
void new_line() { void new_line() {
putChar('\n'); putChar('\n');
} }
void backspace() {
putChar('\b');
}
void winClear() { void winClear() {
sys_write(1, "\e\f", 2); sys_write(1, "\e\f", 2);
} }

View File

@ -1,6 +0,0 @@
#include "system.h"
#include "change.h"
void change() {
sys_switchContext();
}

View File

@ -2,6 +2,6 @@
#include "shell.h" #include "shell.h"
#include "clear.h" #include "clear.h"
void clear(char * window, int * offset) { void clear() {
clearWindow(window, offset); winClear();
} }

View File

@ -1,5 +1,4 @@
#include "cpu_id.h" #include "cpu_id.h"
#include "shell.h"
int cpu_id(int *, int *, int *, int *); 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}; 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"}; 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++) { for (int i = 0; i < len; i++) {
if (checks[i]()) { if (checks[i]()) {
addText(supports[i], window, offset); printString(supports[i]);
substractLine(window, offset); new_line();
} }
} }
printWindow(window);
} }

View File

@ -1,13 +1,12 @@
#include "libc.h" #include "libc.h"
#include "shell.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 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 = 10; static const int len = 9;
void help(char * window, int * offset) { void help() {
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
addText(info[i], window, offset); printString(info[i]);
substractLine(window, offset); new_line();
} }
printWindow(window);
} }

View File

@ -10,20 +10,19 @@ static char * regsNames[] = {
"R8: ", "R9: ", "R10: ", "R11: ", "R12: ", "R13: ", "R14: ", "R15: ", "R8: ", "R9: ", "R10: ", "R11: ", "R12: ", "R13: ", "R14: ", "R15: ",
"RSP: ", "RIP: "}; "RSP: ", "RIP: "};
void inforeg(char * window, int * offset) { void inforeg() {
uint64_t * regs = (uint64_t *) _getRegs(); uint64_t * regs = (uint64_t *) _getRegs();
char bufferAux[20]; char bufferAux[20];
for (int i = 0; i < 17; i++) { for (int i = 0; i < 17; i++) {
if (i % 5 == 0 && i != 0) if (i % 5 == 0 && i != 0)
substractLine(window, offset); new_line();
addText(regsNames[i], window, offset); printString(regsNames[i]);
addText("0x", window, offset); printStringLen("0x", 2);
addText(gtoa(regs[i], bufferAux, 16, 20), window, offset); printString(gtoa(regs[i], bufferAux, 16, 20));
if (i != 14 && i % 5 != 4) if (i != 14 && i % 5 != 4)
addText(" - ", window, offset); printStringLen(" - ", 3);
} }
printWindow(window); new_line();
substractLine(window, offset);
} }

View File

@ -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]; int buffer[8];
getMem(buffer, dir); getMem(buffer, dir);
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
char bufferAux[8]; char bufferAux[8];
addText("0x", window, offset); printStringLen("0x", 2);
addText(itoa(buffer[i], bufferAux, 16), window, offset); printString(itoa(buffer[i], bufferAux, 16));
substractLine(window, offset); new_line();
} }
printWindow(window);
} }

View File

@ -1,10 +1,9 @@
#include "libc.h" #include "libc.h"
#include "shell.h" #include "shell.h"
void ps(char * window, int * offset) { void ps() {
char * output = sys_ps(); char * output = sys_ps();
addText(output, window, offset); printString(output);
printWindow(window); new_line();
substractLine(window, offset); // sys_free(output);
sys_free(output);
} }

View File

@ -2,41 +2,36 @@
#include "quadratic.h" #include "quadratic.h"
#include "shell.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; double sol1, sol2;
if (a == 0) { if (a == 0) {
addText("First argument cannot be 0", window, offset); printStringLen("First argument cannot be 0\n", 28);
printWindow(window);
substractLine(window, offset);
return; return;
} }
int cond = quadSolver(a, b, c, &sol1, &sol2); int cond = quadSolver(a, b, c, &sol1, &sol2);
if (cond == 0) { if (cond == 0) {
addText("Not real!", window, offset); printStringLen("Not real!\n", 11);
printWindow(window);
substractLine(window, offset);
return; return;
} }
char buffer[30] = {0}; char buffer[30] = {0};
addText("Roots for ", window, offset); printStringLen("Roots for ", 11);
ftoa(a, buffer, 10); ftoa(a, buffer, 10);
addText(buffer, window, offset); printString(buffer);
addText("x^2 + ", window, offset); printStringLen("x^2 + ", 7);
ftoa(b, buffer, 10); ftoa(b, buffer, 10);
addText(buffer, window, offset); printString(buffer);
addText("x + ", window, offset); printString("x + ");
ftoa(c, buffer, 10); ftoa(c, buffer, 10);
addText(buffer, window, offset); printString(buffer);
substractLine(window, offset); new_line();
ftoa(sol1, buffer, 10); ftoa(sol1, buffer, 10);
addText(buffer, window, offset); printString(buffer);
addText(" and ", window, offset); printStringLen(" and ", 6);
char buffer2[30] = {0}; char buffer2[30] = {0};
ftoa(sol2, buffer2, 10); ftoa(sol2, buffer2, 10);
addText(buffer2, window, offset); printString(buffer2);
printWindow(window); new_line();
substractLine(window, offset);
} }

View File

@ -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);
}

View File

@ -0,0 +1,9 @@
#include "libc.h"
#include "shell.h"
void sem() {
char * output = sys_sem();
printString(output);
new_line();
// sys_free(output);
}

View File

@ -27,26 +27,24 @@ int getYear() {
return sys_time(YEAR); return sys_time(YEAR);
} }
void printTime(int number, char * window, int * offset) { void printTime(int number) {
char buffer[3] = {0}; char buffer[3] = {0};
buffer[0] = number / 10 + '0'; buffer[0] = number / 10 + '0';
buffer[1] = number % 10 + '0'; buffer[1] = number % 10 + '0';
buffer[2] = '\0'; buffer[2] = '\0';
addText(buffer, window, offset); printString(buffer);
} }
void time(char * window, int * offset) { void time(char * window, int * offset) {
printTime(getDay(), window, offset); printTime(getDay());
addText("/", window, offset); putChar('/');
printTime(getMonth(), window, offset); printTime(getMonth());
addText("/", window, offset); putChar('/');
printTime(getYear(), window, offset); printTime(getYear());
addText(" ", window, offset); putChar(' ');
printTime(getHours(), window, offset); printTime(getHours());
addText(":", window, offset); putChar(':');
printTime(getMinutes(), window, offset); printTime(getMinutes());
addText(":", window, offset); putChar(':');
printTime(getSeconds(), window, offset); printTime(getSeconds());
printWindow(window);
substractLine(window, offset);
} }

View File

@ -1,6 +0,0 @@
#ifndef CHANGE_H
#define CHANGE_H
void change();
#endif

View File

@ -1,6 +1,6 @@
#ifndef CLEAR_LIB #ifndef CLEAR_LIB
#define CLEAR_LIB #define CLEAR_LIB
void clear(char * window, int * offset); void clear();
#endif #endif

View File

@ -1,5 +1,8 @@
#ifndef CPUID #ifndef CPUID_H
#define CPUID #define CPUID_H
#include "libc.h"
#include "shell.h"
// Tabla obtenidad de: https://wiki.osdev.org/CPUID // Tabla obtenidad de: https://wiki.osdev.org/CPUID
enum { enum {
@ -68,7 +71,7 @@ enum {
}; };
void cpufeatures(char * window, int * offset); void cpufeatures();
int cpu_id_support(void); int cpu_id_support(void);
#endif #endif

View File

@ -1,6 +1,6 @@
#ifndef HELP_LIB #ifndef HELP_LIB
#define HELP_LIB #define HELP_LIB
void help(char * window, int * offset); void help();
#endif #endif

View File

@ -1,6 +1,6 @@
#ifndef REG_LIB #ifndef REG_LIB
#define REG_LIB #define REG_LIB
void inforeg(char*, int*); void inforeg();
#endif #endif

View File

@ -1,6 +1,6 @@
#ifndef MEM_LIB #ifndef MEM_LIB
#define MEM_LIB #define MEM_LIB
void printmem(char* window, int* offset, long); void printmem(long);
#endif #endif

View File

@ -1,6 +1,6 @@
#ifndef PS_LIB #ifndef PS_LIB
#define PS_LIB #define PS_LIB
void ps(char * window, int * offset); void ps();
#endif #endif

View File

@ -2,6 +2,6 @@
#define QUAD_LIB #define QUAD_LIB
int quadSolver(double, double, double, double *, double *); int quadSolver(double, double, double, double *, double *);
void quadratic(char*, int*, double, double, double); void quadratic(double, double, double);
#endif #endif

View File

@ -1,6 +0,0 @@
#ifndef SEM_LIB
#define SEM_LIB
void sem(char * window, int * offset);
#endif

View File

@ -0,0 +1,6 @@
#ifndef SEM_LIB
#define SEM_LIB
void sem();
#endif

View File

@ -4,11 +4,7 @@
#include "system.h" #include "system.h"
void shell(int argc, char *argv[]); void shell(int argc, char *argv[]);
void printWindow(char * window); void incorrect_comm(char * buffer);
void addText(char * buffer, char * window, int * offset); void incorrect_arg(char * command);
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);
#endif #endif

View File

@ -7,7 +7,7 @@ int getHours();
int getDays(); int getDays();
int getMonths(); int getMonths();
int getYears(); int getYears();
void time(char * window, int * offset); void time();
#define SECONDS 0 #define SECONDS 0
#define MINUTES 2 #define MINUTES 2

View File

@ -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);
}
*/

View File

@ -1,17 +1,16 @@
#include "libc.h" #include "libc.h"
#include "help.h" #include "help.h"
#include "clear.h"
#include "time.h" #include "time.h"
#include "shell.h" #include "shell.h"
#include "clear.h"
#include "inforeg.h" #include "inforeg.h"
#include "printmem.h" #include "printmem.h"
#include "excDiv.h" #include "excDiv.h"
#include "excOP.h" #include "excOP.h"
#include "quadratic.h" #include "quadratic.h"
#include "cpu_id.h" #include "cpu_id.h"
#include "change.h"
#include "ps.h" #include "ps.h"
#include "sem.h" #include "semCom.h"
#define SIZE 100 #define SIZE 100
#define MAX_ARGS 5 #define MAX_ARGS 5
@ -21,86 +20,33 @@
const int len = 9; const int len = 9;
char *commands_void[] = {"help", "time", "inforeg", "excdiv", "excop", "clear", "cpufeatures", "ps", "sem"}; 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) { void scanfNoPrint(char * buffer) {
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; char c;
int i = 0; int i = 0;
while ((c = getChar()) != '\n' && i < maxSize - 1) { while ((c = getChar()) != '\n' && i < SIZE - 1) {
if (c != -1) { if (c != -1) {
// if (c == '\v')
// sys_switchContext();
// else if (c == '\b' && i > 0) {
if (c == '\b' && i > 0) { if (c == '\b' && i > 0) {
buffer[--i] = ' '; buffer[--i] = ' ';
window[--(*offset)] = ' '; backspace();
printWindow(window);
} }
else if (c != 0 && c != '\b') { else if (c != 0 && c != '\b') {
buffer[i++] = c; buffer[i++] = c;
if (*offset == ROWS * COLS - 1) substractLine(window, offset); putChar(c);
window[(*offset)++] = c;
printWindow(window);
} }
} }
} }
buffer[i] = '\0'; 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[]) { 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) { while (1) {
int comm_flag = 0; int comm_flag = 0;
addText("$> ", window, &offset); printStringLen("$> ", 3);
printWindow(window);
char buffer[SIZE] = {0}; char buffer[SIZE] = {0};
scanfNoPrint(buffer, SIZE, window, &offset); scanfNoPrint(buffer);
substractLine(window, &offset); new_line();
char* tokens[SIZE] = {0}; char* tokens[SIZE] = {0};
tokens[0] = strstrip(buffer, ' '); tokens[0] = strstrip(buffer, ' ');
for (int i = 1; i < MAX_ARGS; i++) { 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++) { for (int i = 0; i < len; i++) {
if (!strcmp(tokens[0], commands_void[i])) { if (!strcmp(tokens[0], commands_void[i])) {
if (*tokens[1] != 0) if (*tokens[1] != 0)
incorrect_arg(tokens[0], window, &offset); incorrect_arg(tokens[0]);
else else
(*func[i])(window, &offset); (*func[i])();
comm_flag = 1; comm_flag = 1;
} }
} }
if (!strcmp(tokens[0], "quadratic")) { if (!strcmp(tokens[0], "quadratic")) {
if (*tokens[4] != 0 || *tokens[3] == 0) 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])) else if (!isFloat(tokens[1]) || !isFloat(tokens[2]) || !isFloat(tokens[3]))
incorrect_arg(tokens[0], window, &offset); incorrect_arg(tokens[0]);
else 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; comm_flag = 1;
} }
if (!strcmp(tokens[0], "printmem")) { if (!strcmp(tokens[0], "printmem")) {
if (*tokens[2] != 0 || *tokens[1] == 0) if (*tokens[2] != 0 || *tokens[1] == 0)
incorrect_arg(tokens[0], window, &offset); incorrect_arg(tokens[0]);
else { else {
int length = strlen(tokens[1]); int length = strlen(tokens[1]);
printmem(window, &offset, atoi(tokens[1], length)); printmem(atoi(tokens[1], length));
} }
comm_flag = 1; comm_flag = 1;
} }
if (!comm_flag) { if (!comm_flag) {
if (*tokens[0] != 0) if (*tokens[0] != 0)
incorrect_comm(tokens[0], window, &offset); incorrect_comm(tokens[0]);
} }
} }
} }
void incorrect_comm(char * buffer, char* window, int * offset) { void incorrect_comm(char * buffer) {
addText(buffer, window, offset); printString(buffer);
addText(" is not a BottlerShell command", window, offset); printStringLen(" is not a BottlerShell command\n", 32);
printWindow(window);
substractLine(window, offset);
} }
void incorrect_arg(char * command, char* window, int * offset) { void incorrect_arg(char * command) {
addText("Incorrect arguments for command ", window, offset); printStringLen("Incorrect arguments for command ", 33);
addText(command, window, offset); printString(command);
printWindow(window); new_line();
substractLine(window, offset);
} }