Compare commits
12 Commits
7dc85ea994
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e9cf02e428 | ||
|
|
cfe3d40fc6 | ||
|
|
913449e500 | ||
|
|
6672ecac58 | ||
|
|
c8b5621df1 | ||
|
|
37aab9b79b | ||
|
|
ea1d6f617a | ||
|
|
0794ed12ed | ||
|
|
a1df9fca4f | ||
|
|
a7cbe7b32c | ||
|
|
28d6123c4c | ||
|
|
2396e4c869 |
7
Makefile
7
Makefile
@@ -7,9 +7,11 @@ CFLAGS ?= -Wall
|
|||||||
|
|
||||||
ifeq ($(RELEASE),true)
|
ifeq ($(RELEASE),true)
|
||||||
CFLAGS += -O3
|
CFLAGS += -O3
|
||||||
|
else
|
||||||
|
CFLAGS += --debug
|
||||||
endif
|
endif
|
||||||
|
|
||||||
.PHONY: clean release-$(RELEASE)
|
.PHONY: clean grind release-$(RELEASE)
|
||||||
|
|
||||||
all: dist/msh
|
all: dist/msh
|
||||||
|
|
||||||
@@ -21,3 +23,6 @@ dist:
|
|||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -rf dist
|
rm -rf dist
|
||||||
|
|
||||||
|
grind:
|
||||||
|
valgrind --leak-check=full --show-leak-kinds=all ./dist/msh
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
# MSH
|
# MSH
|
||||||
- a toy shell implementation
|
- a toy shell implementation in C
|
||||||
|
|
||||||
## What it can do
|
## What it can do
|
||||||
- handle text input
|
- handle text input
|
||||||
- run binaries
|
- run binaries
|
||||||
- change directory
|
- change directory
|
||||||
|
- command history
|
||||||
|
|
||||||
## What it can't do
|
## What it can't do
|
||||||
- any sort of scripting, variables
|
- any sort of scripting, variables
|
||||||
- no fatal memory corruption bugs
|
- memory safety
|
||||||
- pretty colors and themes :(
|
|
||||||
|
|||||||
70
src/ansi.h
Normal file
70
src/ansi.h
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
|
||||||
|
#define ANSI_ESC "\x1b"
|
||||||
|
|
||||||
|
#define ANSI_FG_BLACK "\x1b[30m"
|
||||||
|
#define ANSI_FG_RED "\x1b[31m"
|
||||||
|
#define ANSI_FG_GREEN "\x1b[32m"
|
||||||
|
#define ANSI_FG_YELLOW "\x1b[33m"
|
||||||
|
#define ANSI_FG_BLUE "\x1b[34m"
|
||||||
|
#define ANSI_FG_PURPLE "\x1b[35m"
|
||||||
|
#define ANSI_FG_CYAN "\x1b[36m"
|
||||||
|
#define ANSI_FG_WHITE "\x1b[37m"
|
||||||
|
|
||||||
|
#define ANSI_FG_BLACK_BRIGHT "\x1b[90m"
|
||||||
|
#define ANSI_FG_RED_BRIGHT "\x1b[91m"
|
||||||
|
#define ANSI_FG_GREEN_BRIGHT "\x1b[92m"
|
||||||
|
#define ANSI_FG_YELLOW_BRIGHT "\x1b[93m"
|
||||||
|
#define ANSI_FG_BLUE_BRIGHT "\x1b[94m"
|
||||||
|
#define ANSI_FG_PURPLE_BRIGHT "\x1b[95m"
|
||||||
|
#define ANSI_FG_CYAN_BRIGHT "\x1b[96m"
|
||||||
|
#define ANSI_FG_WHITE_BRIGHT "\x1b[97m"
|
||||||
|
|
||||||
|
#define ANSI_FG_BLACK_BOLD "\x1b[1;30m"
|
||||||
|
#define ANSI_FG_RED_BOLD "\x1b[1;31m"
|
||||||
|
#define ANSI_FG_GREEN_BOLD "\x1b[1;32m"
|
||||||
|
#define ANSI_FG_YELLOW_BOLD "\x1b[1;33m"
|
||||||
|
#define ANSI_FG_BLUE_BOLD "\x1b[1;34m"
|
||||||
|
#define ANSI_FG_PURPLE_BOLD "\x1b[1;35m"
|
||||||
|
#define ANSI_FG_CYAN_BOLD "\x1b[1;36m"
|
||||||
|
#define ANSI_FG_WHITE_BOLD "\x1b[1;37m"
|
||||||
|
|
||||||
|
#define ANSI_FG_BLACK_BRIGHT_BOLD "\x1b[1;90m"
|
||||||
|
#define ANSI_FG_RED_BRIGHT_BOLD "\x1b[1;91m"
|
||||||
|
#define ANSI_FG_GREEN_BRIGHT_BOLD "\x1b[1;92m"
|
||||||
|
#define ANSI_FG_YELLOW_BRIGHT_BOLD "\x1b[1;93m"
|
||||||
|
#define ANSI_FG_BLUE_BRIGHT_BOLD "\x1b[1;94m"
|
||||||
|
#define ANSI_FG_PURPLE_BRIGHT_BOLD "\x1b[1;95m"
|
||||||
|
#define ANSI_FG_CYAN_BRIGHT_BOLD "\x1b[1;96m"
|
||||||
|
#define ANSI_FG_WHITE_BRIGHT_BOLD "\x1b[1;97m"
|
||||||
|
|
||||||
|
#define ANSI_BG_BLACK "\x1b[0;40m"
|
||||||
|
#define ANSI_BG_RED "\x1b[0;41m"
|
||||||
|
#define ANSI_BG_GREEN "\x1b[0;42m"
|
||||||
|
#define ANSI_BG_YELLOW "\x1b[0;43m"
|
||||||
|
#define ANSI_BG_BLUE "\x1b[0;44m"
|
||||||
|
#define ANSI_BG_PURPLE "\x1b[0;45m"
|
||||||
|
#define ANSI_BG_CYAN "\x1b[0;46m"
|
||||||
|
#define ANSI_BG_WHITE "\x1b[0;47m"
|
||||||
|
|
||||||
|
#define ANSI_BG_BLACK_BRIGHT "\x1b[0;100m"
|
||||||
|
#define ANSI_BG_RED_BRIGHT "\x1b[0;101m"
|
||||||
|
#define ANSI_BG_GREEN_BRIGHT "\x1b[0;102m"
|
||||||
|
#define ANSI_BG_YELLOW_BRIGHT "\x1b[0;103m"
|
||||||
|
#define ANSI_BG_BLUE_BRIGHT "\x1b[0;104m"
|
||||||
|
#define ANSI_BG_PURPLE_BRIGHT "\x1b[0;105m"
|
||||||
|
#define ANSI_BG_CYAN_BRIGHT "\x1b[0;106m"
|
||||||
|
#define ANSI_BG_WHITE_BRIGHT "\x1b[0;107m"
|
||||||
|
|
||||||
|
#define ANSI_COLOR_RESET "\x1b[0m"
|
||||||
|
|
||||||
|
#define ANSI_UNKNOWN -1
|
||||||
|
#define ANSI_CURSOR_LEFT 0
|
||||||
|
#define ANSI_CURSOR_RIGHT 1
|
||||||
|
#define ANSI_CURSOR_UP 2
|
||||||
|
#define ANSI_CURSOR_DOWN 3
|
||||||
|
#define ANSI_DELETE_WORD 4
|
||||||
|
#define ANSI_CURSOR_LEFT_WORD 5
|
||||||
|
#define ANSI_CURSOR_RIGHT_WORD 6
|
||||||
|
#define ANSI_CURSOR_UP_WORD 7
|
||||||
|
#define ANSI_CURSOR_DOWN_WORD 8
|
||||||
|
|
||||||
400
src/msh.c
400
src/msh.c
@@ -8,12 +8,19 @@
|
|||||||
#include <sys/utsname.h>
|
#include <sys/utsname.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <termios.h>
|
#include <termios.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
|
#include "ansi.h"
|
||||||
|
|
||||||
|
|
||||||
#define ESC \x1B
|
#define HISTORY_FILE_NAME "/history.log"
|
||||||
|
#define LOCAL_SHARE_DIRNAME "/.local/share/msh"
|
||||||
|
|
||||||
|
#define DEFAULTLINECAPACITY 256
|
||||||
|
#define DEFAULTTOKENSCAPACITY 64
|
||||||
|
#define DEFAULTHISTORYCAPACITY 128
|
||||||
|
|
||||||
const int LINESIZE = 512;
|
|
||||||
|
|
||||||
int cd(int argc, char** argv) {
|
int cd(int argc, char** argv) {
|
||||||
if (argc != 1) {
|
if (argc != 1) {
|
||||||
@@ -36,18 +43,6 @@ void pretty_path_home(char** path) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#define ANSI_UNKNOWN -1
|
|
||||||
#define ANSI_CURSOR_LEFT 0
|
|
||||||
#define ANSI_CURSOR_RIGHT 1
|
|
||||||
#define ANSI_CURSOR_UP 2
|
|
||||||
#define ANSI_CURSOR_DOWN 3
|
|
||||||
#define ANSI_DELETE_WORD 4
|
|
||||||
#define ANSI_CURSOR_LEFT_WORD 5
|
|
||||||
#define ANSI_CURSOR_RIGHT_WORD 6
|
|
||||||
#define ANSI_CURSOR_UP_WORD 7
|
|
||||||
#define ANSI_CURSOR_DOWN_WORD 8
|
|
||||||
|
|
||||||
|
|
||||||
int read_ansi() {
|
int read_ansi() {
|
||||||
switch (getchar()) {
|
switch (getchar()) {
|
||||||
case '[':
|
case '[':
|
||||||
@@ -133,30 +128,48 @@ size_t strnocspn(char* str, size_t n, size_t o, char* reject) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
char* status_string(int status) {
|
||||||
|
switch (status) {
|
||||||
|
case 0:
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
return "❗";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
int retval = 0;
|
int retval = 0;
|
||||||
char* tokens[LINESIZE] = {};
|
char* line = NULL;
|
||||||
char** args;
|
size_t linecapacity = 0;
|
||||||
char line[LINESIZE] = {};
|
char prompt[DEFAULTLINECAPACITY] = {};
|
||||||
char prompt[LINESIZE] = {};
|
size_t linelen = 0;
|
||||||
int linelen = 0;
|
char* tokenwork = NULL;
|
||||||
char* tokenwork;
|
size_t tokenworkcapacity = 0;
|
||||||
char* token;
|
char** tokens = NULL;
|
||||||
int ntokens = 0;
|
size_t tokenscapacity = 0;
|
||||||
|
size_t ntokens = 0;
|
||||||
|
char** history = NULL;
|
||||||
|
size_t historycapacity = 0;
|
||||||
|
size_t historyi = 0;
|
||||||
|
size_t historyoffset = 0;
|
||||||
|
char* oldline = NULL;
|
||||||
|
|
||||||
int i;
|
|
||||||
|
|
||||||
int pid;
|
int pid;
|
||||||
int laststatus = 0;
|
int laststatus = 0;
|
||||||
|
|
||||||
int wstatus;
|
int wstatus;
|
||||||
char pwd[LINESIZE] = {};
|
char pwd[DEFAULTLINECAPACITY] = {};
|
||||||
char* pwd_disp;
|
char* pwd_disp;
|
||||||
|
|
||||||
char* user;
|
char* user;
|
||||||
char* host;
|
char* host;
|
||||||
|
|
||||||
struct utsname uts = {};
|
struct utsname uts = {};
|
||||||
|
char* sharedir = NULL;
|
||||||
|
char* historyfilepath = NULL;
|
||||||
|
FILE* historyfile = NULL;
|
||||||
|
|
||||||
user = secure_getenv("USER");
|
user = secure_getenv("USER");
|
||||||
|
|
||||||
@@ -177,10 +190,96 @@ int main(int argc, char** argv) {
|
|||||||
// disable echo and icanon (don't wait for newline)
|
// disable echo and icanon (don't wait for newline)
|
||||||
term.c_lflag &= ~(ECHO | ICANON);
|
term.c_lflag &= ~(ECHO | ICANON);
|
||||||
|
|
||||||
|
char* homedir = secure_getenv("HOME");
|
||||||
|
|
||||||
|
if (homedir == NULL) {
|
||||||
|
fprintf(stderr, "Error: $HOME is not specified\n");
|
||||||
|
retval = 1;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
sharedir = malloc(strlen(homedir) + strlen(LOCAL_SHARE_DIRNAME) + 1);
|
||||||
|
if (sharedir == NULL) {
|
||||||
|
retval = 1;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
sharedir = strcpy(sharedir, homedir);
|
||||||
|
if (sharedir == NULL) {
|
||||||
|
retval = 1;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
sharedir = strcat(sharedir, LOCAL_SHARE_DIRNAME);
|
||||||
|
if (sharedir == NULL) {
|
||||||
|
retval = 1;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
result = mkdir(sharedir, S_IRWXU);
|
||||||
|
if (result != 0 && errno != EEXIST) {
|
||||||
|
fprintf(stderr, "Error: %s\n", strerror(errno));
|
||||||
|
retval = 1;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
historyfilepath = malloc(strlen(sharedir) + strlen(HISTORY_FILE_NAME) + 1);
|
||||||
|
if (historyfilepath == NULL) {
|
||||||
|
retval = 1;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
historyfilepath = strcpy(historyfilepath, sharedir);
|
||||||
|
if (historyfilepath == NULL) {
|
||||||
|
retval = 1;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
historyfilepath = strcat(historyfilepath, HISTORY_FILE_NAME);
|
||||||
|
if (historyfilepath == NULL) {
|
||||||
|
retval = 1;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
int historyfilefd = open(historyfilepath, O_CREAT | O_APPEND | O_WRONLY, S_IRUSR | S_IWUSR);
|
||||||
|
if (historyfilefd == -1) {
|
||||||
|
fprintf(stderr, "Error: could not open history file: %s\n", strerror(errno));
|
||||||
|
retval = 1;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
historyfile = fdopen(historyfilefd, "w");
|
||||||
|
if (historyfile == NULL) {
|
||||||
|
fprintf(stderr, "Error: could not open history file fd: %s\n", strerror(errno));
|
||||||
|
retval = 1;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Initialize memory
|
||||||
|
tokens = calloc(DEFAULTTOKENSCAPACITY, sizeof(char*));
|
||||||
|
if (tokens == NULL) {
|
||||||
|
retval = 1;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
memset(tokens, 0, DEFAULTTOKENSCAPACITY * sizeof(char*));
|
||||||
|
tokenscapacity = DEFAULTTOKENSCAPACITY;
|
||||||
|
|
||||||
|
tokenwork = malloc(DEFAULTLINECAPACITY);
|
||||||
|
if (tokenwork == NULL) {
|
||||||
|
retval = 1;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
tokenworkcapacity = DEFAULTLINECAPACITY;
|
||||||
|
|
||||||
|
historycapacity = DEFAULTHISTORYCAPACITY;
|
||||||
|
history = calloc(historycapacity, sizeof(char*));
|
||||||
|
if (history == NULL) {
|
||||||
|
retval = 1;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
memset(history, 0, historycapacity);
|
||||||
|
|
||||||
// Main loop
|
// Main loop
|
||||||
while(1) {
|
while(1) {
|
||||||
char* result = getcwd(pwd, LINESIZE - 1);
|
char* result = getcwd(pwd, DEFAULTLINECAPACITY - 1);
|
||||||
if (result == NULL) {
|
if (result == NULL) {
|
||||||
retval = 1;
|
retval = 1;
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
@@ -190,10 +289,27 @@ int main(int argc, char** argv) {
|
|||||||
|
|
||||||
pretty_path_home(&pwd_disp);
|
pretty_path_home(&pwd_disp);
|
||||||
|
|
||||||
if (laststatus != 0) {
|
|
||||||
fprintf(stdout, "❌");
|
snprintf(
|
||||||
}
|
prompt,
|
||||||
snprintf(prompt, LINESIZE, "%s@%s %s$ ", user, host, pwd_disp);
|
DEFAULTLINECAPACITY,
|
||||||
|
|
||||||
|
ANSI_BG_BLUE ANSI_FG_BLACK
|
||||||
|
"%s" // status
|
||||||
|
" %s" // user
|
||||||
|
"@"
|
||||||
|
ANSI_FG_BLACK
|
||||||
|
"%s " // host
|
||||||
|
ANSI_COLOR_RESET ANSI_FG_BLUE
|
||||||
|
" "
|
||||||
|
ANSI_FG_BLUE
|
||||||
|
"%s" // directory
|
||||||
|
ANSI_COLOR_RESET
|
||||||
|
"$ "
|
||||||
|
ANSI_COLOR_RESET,
|
||||||
|
|
||||||
|
status_string(laststatus), user, host, pwd_disp
|
||||||
|
);
|
||||||
printf("%s", prompt);
|
printf("%s", prompt);
|
||||||
|
|
||||||
|
|
||||||
@@ -204,7 +320,12 @@ int main(int argc, char** argv) {
|
|||||||
|
|
||||||
tcsetattr(fileno(stdin), 0, &term);
|
tcsetattr(fileno(stdin), 0, &term);
|
||||||
|
|
||||||
memset(line, 0, LINESIZE);
|
line = malloc(DEFAULTLINECAPACITY);
|
||||||
|
linecapacity = DEFAULTLINECAPACITY;
|
||||||
|
|
||||||
|
memset(line, 0, linecapacity);
|
||||||
|
|
||||||
|
historyoffset = 0;
|
||||||
|
|
||||||
while ((readc = getchar()) != '\n') {
|
while ((readc = getchar()) != '\n') {
|
||||||
if (readc == EOF) {
|
if (readc == EOF) {
|
||||||
@@ -223,10 +344,71 @@ int main(int argc, char** argv) {
|
|||||||
if (cursor < linelen) {
|
if (cursor < linelen) {
|
||||||
cursor++;
|
cursor++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} else if (ansi == ANSI_CURSOR_UP || ansi == ANSI_CURSOR_DOWN) {
|
||||||
|
int load_from_hist = 1;
|
||||||
|
|
||||||
|
if (ansi == ANSI_CURSOR_UP && historyoffset < historyi) {
|
||||||
|
historyoffset++;
|
||||||
|
|
||||||
|
if (historyoffset == 1) {
|
||||||
|
if (oldline != NULL) {
|
||||||
|
free(oldline);
|
||||||
|
}
|
||||||
|
|
||||||
|
oldline = line;
|
||||||
|
line = NULL;
|
||||||
|
}
|
||||||
|
} else if (ansi == ANSI_CURSOR_DOWN && historyoffset > 0) {
|
||||||
|
historyoffset--;
|
||||||
|
|
||||||
|
// We just went back to the last line, load from oldline
|
||||||
|
if (historyoffset == 0) {
|
||||||
|
free(line);
|
||||||
|
line = oldline;
|
||||||
|
linelen = strlen(line);
|
||||||
|
linecapacity = linelen + 1;
|
||||||
|
cursor = linelen;
|
||||||
|
|
||||||
|
oldline = NULL;
|
||||||
|
|
||||||
|
// Don't look up from history
|
||||||
|
load_from_hist = 0;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// We didn't change our line (already at top or bottom)
|
||||||
|
load_from_hist = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (load_from_hist == 1) {
|
||||||
|
char* historyline = history[historyi - (historyoffset)];
|
||||||
|
size_t historylinelen = strlen(historyline);
|
||||||
|
|
||||||
|
if (line != NULL) {
|
||||||
|
free(line);
|
||||||
|
}
|
||||||
|
|
||||||
|
line = malloc(historylinelen + 1);
|
||||||
|
if (line == NULL) {
|
||||||
|
retval = 1;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
line = strcpy(line, historyline);
|
||||||
|
if (line == NULL) {
|
||||||
|
retval = 1;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
cursor = historylinelen;
|
||||||
|
linecapacity = historylinelen + 1;
|
||||||
|
linelen = historylinelen;
|
||||||
|
}
|
||||||
} else if (ansi == ANSI_DELETE_WORD) {
|
} else if (ansi == ANSI_DELETE_WORD) {
|
||||||
if (cursor > 0) {
|
if (cursor > 0) {
|
||||||
|
// 0123 56
|
||||||
size_t index = strnrcspn(line, cursor, " \t");
|
size_t index = strnrcspn(line, cursor, " \t");
|
||||||
memmove(line + index, line + cursor, LINESIZE - cursor);
|
memmove(line + index, line + cursor, linecapacity - cursor);
|
||||||
linelen -= cursor - index;
|
linelen -= cursor - index;
|
||||||
cursor = index;
|
cursor = index;
|
||||||
}
|
}
|
||||||
@@ -237,12 +419,8 @@ int main(int argc, char** argv) {
|
|||||||
size_t index = strnocspn(line, linelen, cursor, " \t");
|
size_t index = strnocspn(line, linelen, cursor, " \t");
|
||||||
cursor = index;
|
cursor = index;
|
||||||
}
|
}
|
||||||
|
} else if (readc == 127) {
|
||||||
goto print_prompt;
|
|
||||||
}
|
|
||||||
|
|
||||||
// backspace
|
// backspace
|
||||||
if (readc == 127) {
|
|
||||||
if (cursor > 0) {
|
if (cursor > 0) {
|
||||||
cursor--;
|
cursor--;
|
||||||
linelen--;
|
linelen--;
|
||||||
@@ -251,49 +429,113 @@ int main(int argc, char** argv) {
|
|||||||
if (cursor == linelen) {
|
if (cursor == linelen) {
|
||||||
line[cursor] = 0;
|
line[cursor] = 0;
|
||||||
} else {
|
} else {
|
||||||
memmove(line + cursor, line + cursor + 1, LINESIZE - (cursor));
|
memmove(line + cursor, line + cursor + 1, linecapacity - (cursor));
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
// We don't want to change the line
|
// We are typing
|
||||||
goto print_prompt;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (cursor != linelen) {
|
if (cursor != linelen) {
|
||||||
memmove(line + cursor + 1, line + cursor, LINESIZE - cursor - 1);
|
memmove(line + cursor + 1, line + cursor, linecapacity - cursor - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
line[cursor] = readc;
|
line[cursor] = readc;
|
||||||
cursor++;
|
cursor++;
|
||||||
linelen++;
|
linelen++;
|
||||||
|
|
||||||
print_prompt:
|
// Are we out of capacity? Double line length
|
||||||
|
while (linelen >= linecapacity - 1) {
|
||||||
|
linecapacity *= 2;
|
||||||
|
line = realloc(line, linecapacity);
|
||||||
|
if (line == NULL) {
|
||||||
|
fprintf(stderr, "Error: Failed to re-allocated line buffer");
|
||||||
|
retval = 1;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(line + linecapacity / 2, 0, linecapacity / 2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Manual echo
|
// Manual echo
|
||||||
printf("\x1b[2K\x1b[0G%s%s", prompt, line);
|
printf(ANSI_ESC "[2K" ANSI_ESC "[0G" "%s%s", prompt, line);
|
||||||
for (int j = 0; j < linelen - cursor; j++) {
|
for (int j = 0; j < linelen - cursor; j++) {
|
||||||
printf("\x1b[D");
|
printf(ANSI_ESC "[D");
|
||||||
}
|
}
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// oldline is only valid while reading
|
||||||
|
if (oldline != NULL) {
|
||||||
|
free(oldline);
|
||||||
|
oldline = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
putc('\n', stdout);
|
putc('\n', stdout);
|
||||||
|
|
||||||
tcsetattr(fileno(stdin), 0, &orig_term);
|
tcsetattr(fileno(stdin), 0, &orig_term);
|
||||||
|
|
||||||
line[linelen] = 0;
|
line[linelen] = 0;
|
||||||
|
|
||||||
|
if (linelen == 0) {
|
||||||
|
free(line);
|
||||||
|
line = NULL;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
fprintf(historyfile, "%s\n", line);
|
||||||
|
fflush(historyfile);
|
||||||
|
|
||||||
|
while (tokenworkcapacity + 1 < linelen) {
|
||||||
|
tokenworkcapacity *= 2;
|
||||||
|
tokenwork = realloc(tokenwork, tokenworkcapacity);
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(tokenwork, 0, tokenworkcapacity);
|
||||||
|
tokenwork = strncpy(tokenwork, line, linelen);
|
||||||
|
if (tokenwork == NULL) {
|
||||||
|
retval = 1;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
while (historyi >= historycapacity) {
|
||||||
|
historycapacity *= 2;
|
||||||
|
history = reallocarray(history, historycapacity, sizeof(char*));
|
||||||
|
|
||||||
|
if (history == NULL) {
|
||||||
|
retval = 1;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
memset(history + historyi, 0, (historycapacity - historyi) * sizeof(char*));
|
||||||
|
}
|
||||||
|
// change ownership of line to history
|
||||||
|
history[historyi++] = line;
|
||||||
|
line = NULL;
|
||||||
|
|
||||||
|
char* tokensarg = tokenwork;
|
||||||
|
ntokens = 0;
|
||||||
|
|
||||||
// Split into tokens
|
// Split into tokens
|
||||||
for (tokenwork = line, i = 0; ; tokenwork = NULL, i++) {
|
for (;; ntokens++) {
|
||||||
token = strtok(tokenwork, " \t");
|
char* token = strtok(tokensarg, " \t");
|
||||||
if (token == NULL) {
|
if (token == NULL) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
tokens[i] = token;
|
tokens[ntokens] = token;
|
||||||
|
|
||||||
|
while (ntokens >= tokenscapacity - 1) {
|
||||||
|
tokenscapacity *= 2;
|
||||||
|
tokens = reallocarray(tokens, tokenscapacity, sizeof(char*));
|
||||||
|
}
|
||||||
|
|
||||||
|
// required to be NULL for second strtok invocation
|
||||||
|
if (tokensarg != NULL) {
|
||||||
|
tokensarg = NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ntokens = i;
|
|
||||||
tokens[ntokens] = NULL;
|
tokens[ntokens] = NULL;
|
||||||
args = &tokens[1];
|
char** args = &tokens[1];
|
||||||
|
|
||||||
if (ntokens == 0) {
|
if (ntokens == 0) {
|
||||||
continue;
|
continue;
|
||||||
@@ -311,21 +553,75 @@ print_prompt:
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (strcmp("history", tokens[0]) == 0) {
|
||||||
|
for (int i = 0; i < historycapacity; i++) {
|
||||||
|
if (history[i] == NULL) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("%.3d: %s\n", i, history[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
laststatus = 0;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// Execute command
|
// Execute command
|
||||||
pid = fork();
|
pid = fork();
|
||||||
|
|
||||||
if(pid == 0) {
|
if(pid == 0) {
|
||||||
return execvpe(tokens[0], tokens, environ);
|
retval = execvpe(tokens[0], tokens, environ);
|
||||||
|
goto cleanup;
|
||||||
} else {
|
} else {
|
||||||
waitpid(pid, &wstatus, 0);
|
waitpid(pid, &wstatus, 0);
|
||||||
|
|
||||||
laststatus = WEXITSTATUS(wstatus);
|
laststatus = WEXITSTATUS(wstatus);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
|
if (sharedir != NULL) {
|
||||||
|
free(sharedir);
|
||||||
|
}
|
||||||
|
if (historyfilepath != NULL) {
|
||||||
|
free(historyfilepath);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (historyfile != NULL) {
|
||||||
|
fflush(historyfile);
|
||||||
|
fclose(historyfile);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (line != NULL) {
|
||||||
|
free(line);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (oldline != NULL) {
|
||||||
|
free(oldline);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tokenwork != NULL) {
|
||||||
|
free(tokenwork);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tokens != NULL) {
|
||||||
|
free(tokens);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (history != NULL) {
|
||||||
|
for (int i = 0; i < historycapacity; i++) {
|
||||||
|
if (history[i] != NULL) {
|
||||||
|
free(history[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
free(history);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
tcsetattr(fileno(stdin), 0, &orig_term);
|
tcsetattr(fileno(stdin), 0, &orig_term);
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user