Add cat, filter and wc

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-27 17:12:01 -03:00
parent 85ab947672
commit 6c805191d0
44 changed files with 353 additions and 177 deletions

View File

@ -72,7 +72,7 @@ void testKeyboardInterrupt(unsigned char c) {
unsigned char getKeyFromBuffer() {
if (current - buffer == SIZE) {
current = buffer;
}
}
char aux = *current;
*current++ = 0;
@ -212,7 +212,7 @@ static unsigned char kbdus_sh[250] =
0, /* 29 - Control */
'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', /* 39 */
'\'', '>', 0, /* Left shift */
'*', 'Z', 'X', 'C', 'V', 'B', 'N', /* 49 */
'|', 'Z', 'X', 'C', 'V', 'B', 'N', /* 49 */
'M', ';', ':', '_', 0, /* Right shift */
'*',
0, /* Alt */

View File

@ -1,8 +1,8 @@
#include <stdint.h>
#include "video.h"
static uint8_t * const video = (uint8_t*) 0xB8000;
static uint8_t * currentVideo = (uint8_t*) 0xB8000;
static uint8_t * const video = (uint8_t *) 0xB8000;
static uint8_t * currentVideo = (uint8_t *) 0xB8000;
static const int width = 80;
static const int height = 25;
static int currentX = 0;
@ -13,23 +13,16 @@ int limitY[2] = {0, 25};
void increment() {
currentX++;
if (currentX >= limitX[1]) {
if (currentX == limitX[1]) {
currentY++;
currentX = limitX[0];
if (currentY >= limitY[1])
currentY = limitY[0];
if (currentY >= limitY[1]) {
scroll();
currentY = limitY[1] - 1;
}
}
}
int atoi(char * string, int length) {
int res = 0, i = 0;
while (i < length) {
res = res * 10 + string[i++] - '0';
}
return res;
}
// "\e\f" para clear
char checkIfEscapeSequence(const char * bufferAux) {
if (*bufferAux == '\e') {
bufferAux++;
@ -42,21 +35,24 @@ char checkIfEscapeSequence(const char * bufferAux) {
return 0;
}
void scroll() {
for (int i = limitY[0]; i < limitY[1] - 1; i++) {
for (int j = limitX[0]; j < limitX[1]; j++) {
*(video + i * width * 2 + j * 2) = *(video + (i + 1) * width * 2 + j * 2);
*(video + i * width * 2 + j * 2 + 1) = *(video + (i + 1) * width * 2 + j * 2 + 1);
}
}
for (int j = limitX[0]; j < limitX[1]; j++) {
*(video + (limitY[1] - 1) * width * 2 + j * 2) = ' ';
}
}
int printStringLen(int color, const char * string, int maxLen) {
int i = 0;
while (*string != '\0' && i <= maxLen) {
if (currentX >= limitX[1]) {
currentX = limitX[0];
currentY++;
}
if (currentY >= limitY[1]) {
currentY = limitY[0];
}
if (*string == '\n') {
new_line();
// return i;
string++;
i++;
continue;
@ -93,15 +89,18 @@ void backspace() {
void new_line() {
currentX = limitX[0];
currentY++;
if (currentY == limitY[1])
currentY = limitY[0];
if (currentY == limitY[1]) {
scroll();
currentY = limitY[1] - 1;
}
}
void clear() {
for (int i = limitX[0]; i < (limitX[1] - limitX[0]) * 2 * (limitY[1] - limitY[0]); i++) {
printStringLen(15, " ", 1);
}
currentX = limitX[0];
currentY = limitY[0];
for (int i = limitY[0]; i < limitY[1]; i++) {
for (int j = limitX[0]; j < limitX[1]; j++) {
*(video + i * width * 2 + j * 2) = ' ';
}
}
}

View File

@ -7,7 +7,7 @@
#define PIPE_MAX_SIZE 1024
typedef struct pipe_t {
int fd;
int fd[2];
char buffer[PIPE_MAX_SIZE];
int current;
char name[MAX_NAME];
@ -19,7 +19,7 @@ typedef struct node_t {
struct node_t * next;
} node_t;
pipe_t * openPipe(char * name);
char openPipe(int * fds, char * name);
void writePipe(int fd, char c);
char readPipe(int fd);

View File

@ -4,7 +4,7 @@
#include <stdint.h>
void initScheduler();
void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char *argv[]);
void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char *argv[], int * fd);
char block(int pid);
char unblock(int pid);
char kill(int pid);

View File

@ -10,5 +10,6 @@ void backspace();
void clear();
void increment();
void changeWindow();
void scroll();
#endif

View File

@ -15,12 +15,11 @@
uint64_t write(uint64_t fd, uint64_t buffer, uint64_t length) {
char * bufferAux = (char *) buffer;
int color;
int color = STDOUT_COLOR;
if (fd == STDOUT)
color = STDOUT_COLOR;
else {
fd = getFdOut();
fd = getFdOut();
if (fd != STDOUT) {
int i = 0;
while (bufferAux[i] != '\0' && i++ <= length) {
writePipe(fd, bufferAux[i]);
@ -35,6 +34,7 @@ uint64_t read(uint64_t fd, uint64_t buffer, uint64_t length) {
char * bufferAux = (char *) buffer;
int readBytes = 0;
fd = getFdIn();
if (fd == STDIN) {
while (length-- > 0) {

View File

@ -5,9 +5,10 @@
void exitProcess();
// void setFn(uint64_t, uint64_t, uint64_t);
char * processes();
void enqueueProcess(uint64_t, uint64_t, uint64_t, uint64_t);
void enqueueProcess(uint64_t, uint64_t, uint64_t, uint64_t, uint64_t);
char openPipe(int *, char *);
uint64_t systemCallsDispatcher(uint64_t rdi, uint64_t rsi, uint64_t rdx, uint64_t rcx, uint64_t r8) {
uint64_t systemCallsDispatcher(uint64_t rdi, uint64_t rsi, uint64_t rdx, uint64_t rcx, uint64_t r8, uint64_t r9) {
switch (rdi) {
case 0:
return write(rsi, rdx, rcx);
@ -17,7 +18,7 @@ uint64_t systemCallsDispatcher(uint64_t rdi, uint64_t rsi, uint64_t rdx, uint64_
return getTime(rsi, rdx, rcx);
case 3:
// createProcess(rsi);
enqueueProcess(rsi, rdx, rcx, r8);
enqueueProcess(rsi, rdx, rcx, r8, r9);
break;
case 4:
exitProcess();
@ -34,6 +35,8 @@ uint64_t systemCallsDispatcher(uint64_t rdi, uint64_t rsi, uint64_t rdx, uint64_
case 9:
vPortFree(rsi);
break;
case 10:
return (uint64_t) openPipe(rsi, rdx);
default:
return -1;
}

View File

@ -141,7 +141,7 @@ void test_mm(){
}
}
void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char *argv[]);
void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char *argv[], int * fd);
void initScheduler();
void _cli();
@ -177,7 +177,7 @@ int main() {
// ((EntryPoint)sampleCodeModuleAddress)();
char * argv[] = {"SampleCode"};
enqueueProcess(sampleCodeModuleAddress, 1, 1, argv);
enqueueProcess(sampleCodeModuleAddress, 1, 1, argv, NULL);
clear();
// haltcpu();
_sti();

View File

@ -199,14 +199,14 @@ void *pvReturn = NULL;
}
else {
ncNewline();
// ncNewline();
// ncPrint("MALLOC: ");
// ncPrintDec(xFreeBytesRemaining);
// ncPrint(" ");
// ncPrintDec(xWantedSize);
// ncPrint(" ");
// ncPrintDec(configADJUSTED_HEAP_SIZE);
ncNewline();
// ncNewline();
}
}

View File

@ -4,14 +4,19 @@ int fds = 2;
node_t * firstPipe;
pipe_t * openPipe(char * name) {
char openPipe(int fds[2] ,char * name) {
pipe_t * pipe = pvPortMalloc(sizeof(pipe_t));
strcpy(pipe->name, name);
pipe->fd = fds++;
if ((pipe->sem = semOpen(SEM_NAME, 1)) == NULL)
return NULL;
return EXIT_FAILURE;
return pipe;
pipe->fd[0] = fds++;
pipe->fd[1] = fds++;
fds[0] = pipe->fd[0];
fds[1] = pipe->fd[1];
return EXIT_SUCCESS;
}
void writePipe(int fd, char c) {
@ -47,7 +52,7 @@ void closePipe(int fd) {
semWait(del->pipe->sem);
if (prev != NULL)
prev->next = del->next;
else firstPipe->next = del->next;
else firstPipe = del->next;
vPortFree(del->pipe);
vPortFree(del);
@ -69,9 +74,5 @@ node_t * searchPipe(node_t ** previous, int fd) {
* previous = NULL;
return NULL;
}
if (curr == firstPipe) {
* previous = NULL;
return curr;
}
return curr;
}

View File

@ -18,8 +18,8 @@ typedef struct processCDT {
char foreground;
enum states state;
int * fd;
void * sseBytes;
void * fpuBytes;
// void * sseBytes;
// void * fpuBytes;
} processCDT;
processCDT * firstReady = NULL;
@ -61,7 +61,7 @@ void idle() {
void initScheduler() {
char * argv[] = {"Idle"};
enqueueProcess(idle, 0, 1, argv);
enqueueProcess(idle, 0, 1, argv, NULL);
}
// void setFn(void (*fn) (int, char **), int argc, char *argv[]) {
@ -69,8 +69,14 @@ void initScheduler() {
// _initialize_stack_frame(fn, currentProcess->rbp, argc, argv);
// }
void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char *argv[]) {
// void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char *argv[], int * fd[2]) {
// void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char *argv[]) {
void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char *argv[], int * fd) {
if (fd == NULL) {
int * aux = pvPortMalloc(2);
aux[0] = 0;
aux[1] = 1;
fd = aux;
}
if (firstReady != NULL && firstReady->pid == IDLE_PID)
block(IDLE_PID);
@ -81,7 +87,6 @@ void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char *
char priority = (foreground == 1) ? DEF_PRIORITY : MAX_PRIORITY/2;
// newProcess(process, argv[0], priority, foreground, (uint64_t) rsp, (uint64_t) rbp);
// char aux[MAX_NAME_SIZE];
char * aux = pvPortMalloc(10);
int j;
@ -99,6 +104,7 @@ void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char *
process->executions = 0;
process->foreground = foreground;
process->state = READY;
process->fd = fd;
// process->sseBytes = pvPortMalloc(64);
// process->fpuBytes = pvPortMalloc(14);
@ -111,49 +117,20 @@ void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char *
lastReady->next = process;
lastReady = process;
// ncClear();
// ncPrint(argv[0]);
// // proc->name = argv[0];
// ncPrint(process->name);
// ncPrintDec(process->pid);
// ncPrintHex(process->rsp);
// ncPrintHex(process->rbp);
// wait(3);
return;
}
void * getSSEaddress() {
return currentProcess->sseBytes;
}
// void * getSSEaddress() {
// return currentProcess->sseBytes;
// }
void * getFPUaddress() {
return currentProcess->fpuBytes;
}
// void * getFPUaddress() {
// return currentProcess->fpuBytes;
// }
void newProcess(processADT process, char * name, char priority, char foreground, uint64_t rsp, uint64_t rbp) {
// char aux[MAX_NAME_SIZE];
// int j;
// for (j = 0; j < MAX_NAME_SIZE - 1 && name[j] != 0; j++) {
// aux[j] = name[j];
// }
// aux[j] = '\0';
// process->name = aux;
// process->pid = pids++;
// process->ppid = currentProcess->pid;
// process->priority = priority;
// process->rsp = rsp;
// process->rbp = rbp;
// process->executions = 0;
// process->foreground = foreground;
// process->state = READY;
}
// void loader(int argc, char * argv[], void (*fn) (int, char **)) {
// fn(argc, argv);
// exit();
// }
processADT searchProcess(processADT * previous, int pid, processADT first) {
processADT curr = first;
* previous = NULL;
@ -168,10 +145,6 @@ processADT searchProcess(processADT * previous, int pid, processADT first) {
* previous = NULL;
return NULL;
}
if (curr == first) {
* previous = NULL;
return curr;
}
return curr;
}
@ -263,13 +236,13 @@ char kill(int pid) {
int getFdOut() {
if (currentProcess == NULL)
return NULL;
return EXIT_FAILURE;
return currentProcess->fd[1];
}
int getFdIn() {
if (currentProcess == NULL)
return NULL;
return EXIT_FAILURE;
return currentProcess->fd[0];
}

View File

@ -3,7 +3,7 @@ GLOBAL _getMem, sys_loadProcess
GLOBAL raiseOpcodeExc
GLOBAL _getRegs, sys_switchContext
GLOBAL cpu_id, cpu_id_support
GLOBAL sys_exit, sys_ps, sys_free, sys_malloc, sys_sem
GLOBAL sys_exit, sys_ps, sys_free, sys_malloc, sys_sem, sys_openPipe
section .text
@ -55,6 +55,27 @@ sys_read:
pop rbp
ret
sys_openPipe:
push rbp
mov rbp, rsp
push rdi
push rsi
push rdx
mov rdx, rsi
mov rsi, rdi
mov rdi, 10
int 80h
pop rdx
pop rsi
pop rdi
mov rsp, rbp
pop rbp
ret
sys_exit:
push rbp
mov rbp, rsp
@ -274,7 +295,9 @@ sys_loadProcess:
push rdx
push rcx
push r8
push r9
mov r9, r8
mov r8, rcx
mov rcx, rdx
mov rdx, rsi
@ -282,6 +305,7 @@ sys_loadProcess:
mov rdi, 3
int 80h
pop r9
pop r8
pop rcx
pop rdx

View File

@ -2,13 +2,15 @@
#define SYSTEM_H
// void sys_switchContext();
// void sys_loadProcess();
void sys_loadProcess(void (*fn) (int, char **), char foreground, int argc, char *argv[], int * fd);
int sys_time(char);
void sys_exit();
void sys_write(int, char *, int);
char sys_read(int, char *, int);
char * sys_ps();
char * sys_sem();
void * sys_malloc(int);
void * sys_free(void *);
void * sys_openPipe(int *, char *);
#endif

View File

@ -28,6 +28,15 @@ int strcmp(const char * s1, const char * s2) {
return *s1 - *s2;
}
char * strcat(char * dest, const char * src) {
char * rdest = dest;
while (*dest)
dest++;
while (*dest++ = *src++);
return rdest;
}
void putChar(char c){
char buffer = c;
sys_write(1, &buffer, 0);

View File

@ -1,8 +1,9 @@
/* sampleCodeModule.c */
#include "libc.h"
#include "shell/include/shell.h"
#include <stddef.h>
void sys_loadProcess(void (*fn) (int, char **), char foreground, int argc, char *argv[]);
void sys_loadProcess(void (*fn) (int, char **), char foreground, int argc, char *argv[], int * fd);
void sys_exit();
// void sys_switchContext();
@ -10,7 +11,7 @@ int main(int argc, char *argv[]) {
winClear();
char * argv2[] = {"BottlerSh"};
sys_loadProcess(shell, 1, 1, argv2);
sys_loadProcess(shell, 1, 1, argv2, NULL);
// sys_loadProcess(shell);
// sys_switchContext();

View File

@ -0,0 +1,17 @@
#include "cat.h"
#define SIZE 1000
void cat(int argc, char ** argv) {
char c;
int i = 0;
char buffer[SIZE] = {0};
while ((c = getChar()) != 0 && c != -1) {
if (i >= SIZE)
break;
buffer[i++] = c;
}
printStringLen(buffer, i);
sys_exit();
}

View File

@ -2,6 +2,7 @@
#include "shell.h"
#include "clear.h"
void clear() {
void clear(int argc, char *argv[]) {
winClear();
sys_exit();
}

View File

@ -100,11 +100,12 @@ static const int len = 16;
int (* checks[])() = {check_cpuid, check_mmx, check_sse, check_sse2, check_sse3, check_sse41, check_sse42, check_aes, check_pclmulqdq, check_avx, check_vaesni, check_vpclmulqdq, check_f16c, check_fma, check_avx2, check_fpu};
char * supports[] = {"cpuid_support", "mx_support", "sse_support", "sse2_support", "sse3_support", "sse41_support", "sse42_support", "aes_support", "pclmulqdq_support", "avx_support", "vaesni_support", "vpclmulqdq_support", "f16c_support", "fma_support", "avx2_support", "fpu_support"};
void cpufeatures() {
void cpufeatures(int argc, char *argv[]) {
for (int i = 0; i < len; i++) {
if (checks[i]()) {
printString(supports[i]);
new_line();
}
}
sys_exit();
}

View File

@ -1,6 +1,6 @@
#include "excDiv.h"
void excdiv() {
void excdiv(int argc, char *argv[]) {
int cero = 0;
int res = 1/cero;
res = res + 1;

View File

@ -2,6 +2,6 @@
void raiseOpcodeExc();
void excop() {
void excop(int argc, char *argv[]) {
raiseOpcodeExc();
}

View File

@ -0,0 +1,22 @@
#include "filter.h"
#define SIZE 1000
int isVocal(char c) {
return c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U' || c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}
void filter(int argc, char ** argv) {
char c;
int i = 0;
char buffer[SIZE] = {0};
while ((c = getChar()) != 0 && c != -1) {
if (i >= SIZE)
break;
if (isVocal(c))
buffer[i++] = c;
}
printStringLen(buffer, i);
sys_exit();
}

View File

@ -4,9 +4,10 @@
static char * info[] = {"clear: Clear the shell window", "cpufeatures: Show the features the cpu supports", "excdiv: Throw exception 0 (zero division)", "excop: Throw exception 6 (opcode error)", "help: Print information about commands", "inforeg: Print values of registers","printmem: Prints 32 bytes of memory, starting at a given direction", "quadratic: Receives 3 floats, quadratic coefficients, prints roots if real", "time: Prints date and time in UTC" };
static const int len = 9;
void help() {
void help(int argc, char *argv[]) {
for (int i = 0; i < len; i++) {
printString(info[i]);
new_line();
}
sys_exit();
}

View File

@ -10,7 +10,7 @@ static char * regsNames[] = {
"R8: ", "R9: ", "R10: ", "R11: ", "R12: ", "R13: ", "R14: ", "R15: ",
"RSP: ", "RIP: "};
void inforeg() {
void inforeg(int argc, char *argv[]) {
uint64_t * regs = (uint64_t *) _getRegs();
char bufferAux[20];
@ -25,4 +25,5 @@ void inforeg() {
printStringLen(" - ", 3);
}
new_line();
sys_exit();
}

View File

@ -13,10 +13,16 @@ void getMem(int * buffer, int initialAddress) {
}
}
void printmem(long dir) {
// void printmem(long dir) {
void printmem(int argc, char *argv[]) {
if (argc != 2) {
printStringLen("printmem receives a memory position\n", 37);
return;
}
int buffer[8];
getMem(buffer, dir);
getMem(buffer, atoi(argv[1], strlen(argv[1])));
for (int i = 0; i < 8; i++) {
char bufferAux[8];

View File

@ -1,9 +1,9 @@
#include "libc.h"
#include "shell.h"
void ps() {
void ps(int argc, char *argv[]) {
char * output = sys_ps();
printString(output);
new_line();
// sys_free(output);
sys_free(output);
}

View File

@ -2,8 +2,14 @@
#include "quadratic.h"
#include "shell.h"
void quadratic(double a, double b, double c) {
double sol1, sol2;
// void quadratic(double a, double b, double c) {
void quadratic(int argc, char *argv[]) {
if (argc != 4) {
printStringLen("quadratic receives 3 doubles\n", 30);
return;
}
double sol1, sol2, a = atof(argv[1]), b = atof(argv[2]), c = atof(argv[3]);
if (a == 0) {
printStringLen("First argument cannot be 0\n", 28);
return;

View File

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

View File

@ -35,7 +35,7 @@ void printTime(int number) {
printString(buffer);
}
void time(char * window, int * offset) {
void time(int argc, char *argv[]) {
printTime(getDay());
putChar('/');
printTime(getMonth());

View File

@ -0,0 +1,18 @@
#include "wc.h"
#define SIZE 1000
void wc(int argc, char ** argv) {
char c;
int i = 0;
char buffer[SIZE] = {0};
while ((c = getChar()) != 0 && c != -1) {
if (i >= SIZE)
break;
if (c == '\n')
i++;
}
printStringLen(gtoa(i, buffer, 10, SIZE), SIZE);
sys_exit();
}

View File

@ -0,0 +1,9 @@
#ifndef CAT_LIB
#define CAT_LIB
#include "libc.h"
#include "system.h"
void cat(int argc, char *argv[]);
#endif

View File

@ -1,6 +1,6 @@
#ifndef CLEAR_LIB
#define CLEAR_LIB
void clear();
void clear(int argc, char *argv[]);
#endif

View File

@ -71,7 +71,7 @@ enum {
};
void cpufeatures();
void cpufeatures(int argc, char *argv[]);
int cpu_id_support(void);
#endif

View File

@ -1,6 +1,6 @@
#ifndef EXCDIV_LIB
#define EXCDIV_LIB
void excdiv();
void excdiv(int argc, char *argv[]);
#endif

View File

@ -1,6 +1,6 @@
#ifndef EXCOP_LIB
#define EXCOP_LIB
void excop();
void excop(int argc, char *argv[]);
#endif

View File

@ -0,0 +1,9 @@
#ifndef FILTER_LIB
#define FILTER_LIB
#include "libc.h"
#include "system.h"
void filter(int argc, char *argv[]);
#endif

View File

@ -1,6 +1,6 @@
#ifndef HELP_LIB
#define HELP_LIB
void help();
void help(int argc, char *argv[]);
#endif

View File

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

View File

@ -1,6 +1,6 @@
#ifndef MEM_LIB
#define MEM_LIB
void printmem(long);
void printmem(int argc, char *argv[]);
#endif

View File

@ -1,6 +1,6 @@
#ifndef PS_LIB
#define PS_LIB
void ps();
void ps(int argc, char *argv[]);
#endif

View File

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

View File

@ -1,6 +1,6 @@
#ifndef SEM_LIB
#define SEM_LIB
void sem();
void sem(int argc, char *argv[]);
#endif

View File

@ -7,7 +7,7 @@ int getHours();
int getDays();
int getMonths();
int getYears();
void time();
void time(int argc, char *argv[]);
#define SECONDS 0
#define MINUTES 2

View File

@ -0,0 +1,9 @@
#ifndef WC_LIB
#define WC_LIB
#include "libc.h"
#include "system.h"
void wc(int argc, char *argv[]);
#endif

View File

@ -10,19 +10,45 @@
#include "quadratic.h"
#include "cpu_id.h"
#include "ps.h"
#include "wc.h"
#include "filter.h"
#include "cat.h"
#include "semCom.h"
#include "stddef.h"
#define EXIT_FAILURE 1
#define EXIT_SUCCESS 0
#define SIZE 100
#define MAX_ARGS 5
#define MAX_ARGS 11
#define COLS 80
#define ROWS 25
const int len = 9;
char *commands_void[] = {"help", "time", "inforeg", "excdiv", "excop", "clear", "cpufeatures", "ps", "sem"};
void (*func []) () = {help, time, inforeg, excdiv, excop, clear, cpufeatures, ps, sem};
typedef struct cmd_t {
char * name;
void (*func) (int argc, char * argv[]);
char isBuiltIn;
} cmd_t;
void scanfNoPrint(char * buffer) {
const int len = 14;
cmd_t commands[] = {
{ "help", help, 1 },
{ "cat", cat, 0 },
{ "time", time, 1 },
{ "inforeg", inforeg, 1 },
{ "excdiv", excdiv, 1 },
{ "excop", excop, 1 },
{ "filter", filter, 0 },
{ "clear", clear, 1 },
{ "cpufeatures", cpufeatures, 1 },
{ "ps", ps, 1 },
{ "sem", sem, 1 },
{ "quadratic", quadratic, 0 },
{ "printmem", printmem, 0 },
{ "wc", wc, 0 },
};
int scanfNoPrint(char * buffer) {
char c;
int i = 0;
while ((c = getChar()) != '\n' && i < SIZE - 1) {
@ -38,52 +64,89 @@ void scanfNoPrint(char * buffer) {
}
}
buffer[i] = '\0';
return i;
}
void processInput(char * input) {
int comm_flag0 = 0, comm_flag1 = 0, pipe = -1, end = -1;
char* tokens[SIZE] = {0};
tokens[0] = strstrip(input, ' ');
for (int i = 1; i < MAX_ARGS; i++) {
tokens[i] = strtok(tokens[i - 1], ' ');
if (i > 1) {
if (!strcmp(tokens[i - 1], "|"))
pipe = i - 1;
}
if (tokens[i][0] == 0) {
end = i;
break;
}
}
int fd[2];
if (pipe != -1) {
if (sys_openPipe(fd, "pipe"))
return;
// return EXIT_FAILURE;
}
char ** argv0 = NULL;
char ** argv1 = NULL;
if (pipe != -1) {
argv0 = sys_malloc(sizeof(char *) * pipe);
for (int i = 0; i < pipe; i++)
argv0[i] = tokens[i];
argv1 = sys_malloc(sizeof(char *) * (end - pipe - 1));
for (int i = pipe + 1; i < end - pipe - 1; i++)
argv1[i] = tokens[i];
}
else {
*argv0 = sys_malloc(sizeof(char *) * end);
for (int i = 0; i < end; i++)
argv0[i] = tokens[i];
}
for (int i = 0; i < len; i++) {
if (!strcmp(tokens[0], commands[i].name)) {
comm_flag0 = 1;
if (pipe != -1)
sys_loadProcess(commands[i].func, 1, pipe, argv0, fd);
else
sys_loadProcess(commands[i].func, 1, end, argv0, NULL);
break;
}
}
if (comm_flag0 && pipe != -1) {
for (int i = 0; i < len; i++) {
if (!strcmp(tokens[pipe + 1], commands[i].name)) {
comm_flag1 = 1;
sys_loadProcess(commands[i].func, 1, end - pipe - 1, argv1, fd);
break;
}
}
}
if (!comm_flag0) {
if (*tokens[0] != 0)
incorrect_comm(tokens[0]);
}
else if (!comm_flag1 && pipe != -1) {
if (*tokens[pipe + 1] != 0)
incorrect_comm(tokens[pipe + 1]);
}
}
void loader(void (*fn) (int, char **), int argc, char * argv[]) {
fn(argc, argv);
sys_exit();
}
void shell(int argc, char *argv[]) {
printStringLen("$> ", 3);
char buffer[SIZE] = {0};
while (1) {
int comm_flag = 0;
printStringLen("$> ", 3);
char buffer[SIZE] = {0};
scanfNoPrint(buffer);
new_line();
char* tokens[SIZE] = {0};
tokens[0] = strstrip(buffer, ' ');
for (int i = 1; i < MAX_ARGS; i++) {
tokens[i] = strtok(tokens[i - 1], ' ');
}
for (int i = 0; i < len; i++) {
if (!strcmp(tokens[0], commands_void[i])) {
if (*tokens[1] != 0)
incorrect_arg(tokens[0]);
else
(*func[i])();
comm_flag = 1;
}
}
if (!strcmp(tokens[0], "quadratic")) {
if (*tokens[4] != 0 || *tokens[3] == 0)
incorrect_arg(tokens[0]);
else if (!isFloat(tokens[1]) || !isFloat(tokens[2]) || !isFloat(tokens[3]))
incorrect_arg(tokens[0]);
else
quadratic(atof(tokens[1]), atof(tokens[2]), atof(tokens[3]));
comm_flag = 1;
}
if (!strcmp(tokens[0], "printmem")) {
if (*tokens[2] != 0 || *tokens[1] == 0)
incorrect_arg(tokens[0]);
else {
int length = strlen(tokens[1]);
printmem(atoi(tokens[1], length));
}
comm_flag = 1;
}
if (!comm_flag) {
if (*tokens[0] != 0)
incorrect_comm(tokens[0]);
}
processInput(buffer);
printStringLen("$> ", 3);
}
}