Skip to content

Commit

Permalink
Merge pull request #25 from ianchen-tw/refactor-shell
Browse files Browse the repository at this point in the history
Refactor shell
  • Loading branch information
ianchen-tw committed Apr 16, 2021
2 parents 7f88915 + 0144de5 commit aa09b3f
Show file tree
Hide file tree
Showing 10 changed files with 290 additions and 197 deletions.
8 changes: 6 additions & 2 deletions impl-c/Makefile
Expand Up @@ -9,7 +9,8 @@ LDSCRIPT = kernel/linker.ld
LDFLAGS = -nostdlib

SRC = $(wildcard **/*.S) $(wildcard **/*.c)
OBJS = $(wildcard *.o)
OBJS = $(wildcard ./*.o)
BUILD_OBJS = $(wildcard build/*.o)

CFLAGS = -Wall -Iinclude -nostartfiles -ffreestanding
CFLAGS += -g -ggdb
Expand All @@ -20,15 +21,18 @@ TARGET_ELF = kernel8.elf
.PHONY: all
all:
$(CC) $(CFLAGS) -c $(SRC)
mkdir -p build
mv ./*.o build
make kernel

.PHONY: kernel
kernel:
$(LD) $(LDFLAGS) $(OBJS) -T $(LDSCRIPT) -o $(TARGET_ELF)
$(LD) $(LDFLAGS) $(BUILD_OBJS) -T $(LDSCRIPT) -o $(TARGET_ELF)
$(OBJCOPY) -O binary $(TARGET_ELF) $(TARGET)

clean:
$(RM) $(OBJS)
$(RM) -rf build
$(RM) $(TARGET) $(TARGET_ELF)


1 change: 1 addition & 0 deletions impl-c/include/cfg.h
Expand Up @@ -15,6 +15,7 @@

// Shell
#define CFG_RUN_SHELL_BUFFER_TEST
#define CFG_RUN_SHELL_CMD_TEST

// Memory management
// #define CFG_RUN_STATUP_ALLOC_TEST
16 changes: 8 additions & 8 deletions impl-c/include/shell/buffer.h
@@ -1,19 +1,19 @@
#pragma once

#include "cfg.h"

struct InputBuffer {
char *data;
int mx_size;
int write_head;
int cur_input_size;

void (*pop)(struct InputBuffer *self);
void (*push)(struct InputBuffer *self, char c);
void (*cursor_mov_left)(struct InputBuffer *self);
void (*cursor_mov_right)(struct InputBuffer *self);
void (*clear)(struct InputBuffer *self);
};
void bfr_init(struct InputBuffer *bfr, char *data, int mx_size);
void bfr_pop(struct InputBuffer *bfr);
void bfr_push(struct InputBuffer *bfr, char c);
void bfr_cursor_mov_left(struct InputBuffer *bfr);
void bfr_cursor_mov_right(struct InputBuffer *bfr);
void bfr_clear(struct InputBuffer *bfr);

void InputBuffer_init(struct InputBuffer *bfr, char *data, int mx_size);

// Only used for running tests
void test_shell_buffer();
12 changes: 12 additions & 0 deletions impl-c/include/shell/cmd.h
@@ -0,0 +1,12 @@
#pragma once

typedef struct {
char *name;
char *help;
void (*func)(void);
} Cmd;

Cmd *getCmd(char *name);

// Only used for running tests
void test_shell_cmd();
31 changes: 10 additions & 21 deletions impl-c/include/shell/shell.h
@@ -1,26 +1,15 @@
#pragma once

#define MX_CMD_BFRSIZE 64
#include "buffer.h"
#include <stdint.h>

typedef enum AnsiEscType {
Unknown,
CursorForward,
CursorBackward,
} AnsiEscType;

enum KeyboardInput {
KI_BackSpace = '\b', // 8
KI_LineFeed = '\n', // 10
KI_CarrageReturn = '\r', // 13
KI_Esc = '\e', // 27
KI_ANSI_ESCAPE_SEQ_START = '\e', // 27

KI_PRINTABLE_START = 32,
KI_PRINTABLE_END = 126,
KI_Delete = 127,
struct Shell {
char *data;
uint32_t bfr_size;
struct InputBuffer bfr;
};

void shellPrintPrompt();
void shellInputLine();
void shellProcessCommand();
void shellInit();
void shell_show_prompt(struct Shell *sh);
void shell_input_line(struct Shell *sh);
void shell_process_command(struct Shell *sh);
void shell_init(struct Shell *sh, char *data, uint32_t size);
11 changes: 7 additions & 4 deletions impl-c/kernel/main.c
Expand Up @@ -6,6 +6,7 @@
#include "test.h"
#include "uart.h"

#define MX_CMD_BFRSIZE 64
extern unsigned char __kernel_start, __kernel_end;

void svc_test() {
Expand Down Expand Up @@ -45,10 +46,12 @@ int main() {
uart_println("-------------------------------");
uart_println(" input filename to see file content");

shellInit();
struct Shell sh;
char shell_buffer[MX_CMD_BFRSIZE + 1];
shell_init(&sh, shell_buffer, MX_CMD_BFRSIZE);
while (1) {
shellPrintPrompt();
shellInputLine();
shellProcessCommand();
shell_show_prompt(&sh);
shell_input_line(&sh);
shell_process_command(&sh);
}
}
2 changes: 2 additions & 0 deletions impl-c/kernel/test.c
Expand Up @@ -2,8 +2,10 @@
#include "cfg.h"
#include "mm/startup.h"
#include "shell/buffer.h"
#include "shell/cmd.h"

void run_tests() {
test_startup_alloc();
test_shell_buffer();
test_shell_cmd();
}
67 changes: 36 additions & 31 deletions impl-c/shell/buffer.c
Expand Up @@ -3,27 +3,19 @@
#include "cfg.h"
#include "test.h"

void bfr_init(struct InputBuffer *bfr, char *data, int mx_size) {
bfr->data = data;
bfr->mx_size = mx_size;
bfr->write_head = 0;
bfr->cur_input_size = 0;
bfr->data[bfr->cur_input_size] = 0;
}

void bfr_cursor_mov_left(struct InputBuffer *bfr) {
static void bfr_cursor_mov_left(struct InputBuffer *bfr) {
if (bfr->write_head > 0) {
bfr->write_head--;
}
}

void bfr_cursor_mov_right(struct InputBuffer *bfr) {
static void bfr_cursor_mov_right(struct InputBuffer *bfr) {
if (bfr->write_head < bfr->cur_input_size) {
bfr->write_head++;
}
}

void bfr_push(struct InputBuffer *bfr, char c) {
static void bfr_push(struct InputBuffer *bfr, char c) {
if (bfr->cur_input_size >= bfr->mx_size) {
// buffer is full
return;
Expand All @@ -39,7 +31,7 @@ void bfr_push(struct InputBuffer *bfr, char c) {
}
}

void bfr_pop(struct InputBuffer *bfr) {
static void bfr_pop(struct InputBuffer *bfr) {
if (bfr->write_head > 0) {
bfr->write_head--;
// left shift the whole buffer
Expand All @@ -50,31 +42,46 @@ void bfr_pop(struct InputBuffer *bfr) {
}
}

void bfr_clear(struct InputBuffer *bfr) {
static void bfr_clear(struct InputBuffer *bfr) {
bfr->cur_input_size = 0;
bfr->write_head = 0;
bfr->data[0] = 0;
}

void InputBuffer_init(struct InputBuffer *bfr, char *data, int mx_size) {
bfr->data = data;
bfr->mx_size = mx_size;
bfr->write_head = 0;
bfr->cur_input_size = 0;
bfr->data[bfr->cur_input_size] = 0;

// method binding
bfr->pop = bfr_pop;
bfr->push = bfr_push;
bfr->cursor_mov_left = bfr_cursor_mov_left;
bfr->cursor_mov_right = bfr_cursor_mov_right;
bfr->clear = bfr_clear;
}

//============== TEST START =================
#ifdef CFG_RUN_SHELL_BUFFER_TEST
bool test_bfr_init() {
char data[10] = {'a', 'b', 'c'};
struct InputBuffer bfr;
bfr_init(&bfr, data, 10);
InputBuffer_init(&bfr, data, 10);
assert(data[0] == 0);
return true;
}

bool test_bfr_write() {
char data[10];
struct InputBuffer bfr;
bfr_init(&bfr, data, 10);
bfr_push(&bfr, 'b');
bfr_push(&bfr, 'c');
bfr_cursor_mov_left(&bfr);
bfr_cursor_mov_left(&bfr);
bfr_push(&bfr, 'a');
InputBuffer_init(&bfr, data, 10);
bfr.push(&bfr, 'b');
bfr.push(&bfr, 'c');
bfr.cursor_mov_left(&bfr);
bfr.cursor_mov_left(&bfr);
bfr.push(&bfr, 'a');
assert(data[0] == 'a');
assert(data[1] == 'b');
assert(data[2] == 'c');
Expand All @@ -84,10 +91,10 @@ bool test_bfr_write() {
bool test_bfr_clear() {
char data[10];
struct InputBuffer bfr;
bfr_init(&bfr, data, 10);
bfr_push(&bfr, 'b');
bfr_push(&bfr, 'c');
bfr_clear(&bfr);
InputBuffer_init(&bfr, data, 10);
bfr.push(&bfr, 'b');
bfr.push(&bfr, 'c');
bfr.clear(&bfr);
assert(data[0] == 0);
assert(bfr.write_head == 0);
return true;
Expand All @@ -96,15 +103,14 @@ bool test_bfr_clear() {
bool test_bfr_pop() {
char data[10];
struct InputBuffer bfr;
bfr_init(&bfr, data, 10);
bfr_push(&bfr, 'b');
bfr_push(&bfr, 'c');
bfr_cursor_mov_left(&bfr);
bfr_pop(&bfr);
InputBuffer_init(&bfr, data, 10);
bfr.push(&bfr, 'b');
bfr.push(&bfr, 'c');
bfr.cursor_mov_left(&bfr);
bfr.pop(&bfr);
assert(data[0] == 'c');
return true;
}

#endif

void test_shell_buffer() {
Expand All @@ -113,6 +119,5 @@ void test_shell_buffer() {
unittest(test_bfr_write, "shell", "buffer write");
unittest(test_bfr_clear, "shell", "buffer clear");
unittest(test_bfr_pop, "shell", "buffer delete");

#endif
}

0 comments on commit aa09b3f

Please sign in to comment.