shell/shelf.c (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
#include "cedos.h"
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include <stdint.h>
struct hist_item {
char* buffer;
struct hist_item* next;
};
struct hist_item* hist_first = NULL;
char* read_line() {
int i = 0;
char c;
char* buffer = malloc(512);
buffer[0] = 0;
struct hist_item* history = hist_first;
while (1) {
c = getchar();
if (c == '\n') {
break;
} else if (c == 0) {
continue;
} else if (c == 0x08 && i <= 0) {
continue;
} else if (c == '^' && getchar() == '[' && getchar() == 'A') {
if (history == NULL) { continue; }
strcpy(buffer, history->buffer);
while (i) { putchar(0x08); i--; }
i = strlen(buffer);
puts(buffer);
history = history->next;
continue;
} else if (c == 0x08) {
buffer[--i] = 0;
putchar(c);
} else {
buffer[i++] = c;
putchar(c);
}
}
buffer[i] = 0;
putchar(c);
if (i > 0) {
// append command history
struct hist_item* next = malloc(sizeof(struct hist_item));
next->buffer = buffer;
next->next = hist_first;
hist_first = next;
}
char *ret_buf = malloc(512);
strcpy(ret_buf, buffer);
return ret_buf;
}
int async[32];
int async_index;
void main(char *args) {
(void)args;
printf("\n");
printf("\e[94mShELF shell interface for CeDOS\e[97m\n");
printf("Version: " VERSION "\n");
while (1) {
printf("/> ");
char* buffer = read_line();
if (strlen(buffer) == 0) { continue; }
char *file = buffer;
const char *first_space = strchr(file, ' ');
const char *amp = strchr(file, ',');
char *args = NULL;
if (first_space != NULL) {
size_t offset = first_space - file;
file[offset] = 0;
args = &(file[offset + 1]);
}
if (strcmp(file, "exit") == 0) {
printf("Thank you for using ShELF!\n");
break;
} else if (strcmp(file, "history") == 0) {
struct hist_item* item = hist_first;
while (item) {
printf("%s\n", item->buffer);
item = item->next;
}
continue;
} else if (strcmp(file, "clear") == 0) {
printf("\e[2J");
continue;
}
int pid = process_spawn(file, args);
//printf("Child process %i spawned, waiting for termination...\n", pid);
if (pid == -1) {
printf("File not found: %s\n", buffer);
} else if (amp != NULL) {
printf("[%i] %i\n", async_index, pid);
async[async_index++] = pid;
} else {
process_wait(pid);
}
}
}