#include "common/common.h" #include typedef struct { char* buffer; int x; int y; uint32_t process; uint32_t stdin; uint32_t stdout; } procbuffer; void scrollBuffer(char* buf) { for (int y=0; y<56; y++) for (int x=0; x<43; x++) if (y != 55) buf[y*43+x] = buf[(y+1)*43+x]; else buf[y*43+x] = ' '; } void writeToBuf(char c, procbuffer* buf) { switch (c) { case '\n': buf->x = 0; buf->y++; break; case '\b': if (buf->x > 0) buf->x--; else if (buf->y > 0) { buf->x = 42; buf->y--; } buf->buffer[buf->y*43+buf->x] = ' '; break; default: buf->buffer[buf->y*43+buf->x++] = c; } if (buf->x == 43) { buf->x = 0; buf->y++; } if (buf->y == 56) { buf->y--; scrollBuffer(buf->buffer); } } void writeCountToBuf(int count, char* c, procbuffer* buf) { while (count--) { writeToBuf(*(c++), buf); } } void clearBuf(procbuffer* buf) { for (int i=0; i<56*43;i++) { buf->buffer[i] = ' '; } buf->x = 0; buf->y = 0; } void writeStrToBuf(char* c, procbuffer* b) { char* s = c; while(*s) writeToBuf(*(s++), b); } void displayBuf(procbuffer* b, int dx, int dy) { char pset[9] = "\x1b[00;00H"; for (int i=0; ibuffer+(43*i)); pset[3]++; if (pset[3] == 0x3a) { pset[3] = 0x30; pset[2]++; } } } void setCurPos(int x, int y) { char pset[9] = "\x1b[00;00H"; for (int i=0; i\n", selectedBuf); writeStrToBuf(" Loads and executes the program \n", selectedBuf); writeStrToBuf("--------\n", selectedBuf); } else if (strcmpcnt(4, buf, "kill")) { if (selectedBuf->process) { kill(selectedBuf->process); clearBuf(selectedBuf); selectedBuf->process = 0; selectedBuf->stdin = 0; selectedBuf->stdout = 0; if (selectedBuf == &b1) { selectedBuf = &b2; } else { selectedBuf = &b1; } } } else if (strcmpcnt(4, buf, "load")) { if (!selectedBuf->process) { char* potFn = buf+5; uint32_t fh = fopen(potFn); selectedBuf->process = fork(fh); selectedBuf->stdout = bindStdout(selectedBuf->process); selectedBuf->stdin = bindStdin(selectedBuf->process); fclose(fh); } } } else { if (selectedBuf->process) write(1, selectedBuf->stdin, c); } } displayBuf(&b1, 2, 2); displayBuf(&b2, 47, 2); } }