Commit refs/heads/wip/shell wip/shell
authorMichel Pollet <buserror@gmail.com>
Wed, 17 Jan 2018 10:18:42 +0000 (10:18 +0000)
committerMichel Pollet <buserror@gmail.com>
Wed, 17 Jan 2018 10:54:24 +0000 (10:54 +0000)
Reworked the command buffer and that sort of things.
Will work toward passing a context all the way down, so hanlde possibly
multiple instances of the core.

Signed-off-by: Michel Pollet <buserror@gmail.com>
5 files changed:
shavr/src/history.c
shavr/src/history.h
shavr/src/history_avr.c
shavr/src/shavr.c
shavr/src/sim_prepare.c

index 4b11fe5cefdb6c9a8344aa1acade5c20d9e43baf..92c5258aa4259e6eda1209e9e18401f834d4f326 100644 (file)
 
 #include "history.h"
 
+typedef struct line_t {
+       TAILQ_ENTRY(line_t)     queue;
+
+       int size, len, pos;
+       char * line;
+} line_t, *line_p;
+
+typedef struct history_t {
+       int ttyin;
+       int ttyout;
+       void * private_context;
+       struct history_params_t *p;
+
+       TAILQ_HEAD(cmd_queue_t,line_t)  cmd;
+       line_p current;
+       void * state;
+       int temp;
+} history_t, *history_p;
+
 static struct termios orig_termios;  /* TERMinal I/O Structure */
 
 void fatal(char *message) {
@@ -105,12 +124,8 @@ line_new(
 {
        line_p res = malloc(sizeof(line_t));
        memset(res, 0, sizeof(*res));
-       res->prev = h->tail;
-       if (!h->tail)
-               h->head = res;
-       else
-               h->tail->next = res;
-       h->tail = res;
+
+       TAILQ_INSERT_TAIL(&h->cmd, res, queue);
        h->current = res;
        return res;
 }
@@ -136,9 +151,9 @@ history_display(
                history_p h)
 {
        char s[16];
-       int pl = strlen(h->prompt);
+       int pl = strlen(h->p->prompt);
        write(h->ttyout, "\033[0m\r", 5);
-       write(h->ttyout, h->prompt, pl);
+       write(h->ttyout, h->p->prompt, pl);
        if (h->current) {
                write(h->ttyout, h->current->line, h->current->len);
                pl += h->current->pos;
@@ -228,24 +243,28 @@ line_key(
                                }       break;
                                case 13: { // return!!
                                        if (h->current->len) {
-                                               if (!h->process_line || h->process_line(h, h->current)) {
+                                               if (!h->p->process_line ||
+                                                               h->p->process_line(h->private_context,
+                                                                                                       h->current->line)) {
                                                        res++;
-                                                       if (h->current != h->tail)
+                                                       if (h->current != TAILQ_LAST(&h->cmd, cmd_queue_t))
                                                                line_dup(h, h->current);
                                                        line_new(h);
                                                }
                                        } else
                                                write(1, "\012", 1);    // next line
                                }       break;
-                               case 16: { // control-P -- previous line
-                                       previous_line:
-                                       if (h->current->prev)
-                                               h->current = h->current->prev;
+                               case 16: {// control-P -- previous line
+                                       previous_line: ;
+                                       line_p p = TAILQ_PREV(h->current, cmd_queue_t, queue);
+                                       if (p)
+                                               h->current = p;
                                }       break;
                                case 14: {      // control-N -- next line
-                                       next_line:
-                                       if (h->current->next)
-                                               h->current = h->current->next;
+                                       next_line: ;
+                                       line_p n = TAILQ_NEXT(h->current, queue);
+                                       if (n)
+                                               h->current = n;
                                }       break;
                                case 27: {
                                        PT_YIELD(h->state);
@@ -281,7 +300,7 @@ line_key(
                }       break;
        }
 
-       if ((insert || delete) && h->current != h->tail) {
+       if ((insert || delete) && h->current != TAILQ_LAST(&h->cmd, cmd_queue_t)) {
                line_dup(h, h->current);
        }
        line_p l = h->current;
@@ -321,13 +340,23 @@ line_key(
        return res;
 }
 
-
-void
-history_init(
-               history_p h)
+struct history_t *
+history_new(
+               int ttyin, int ttyout,
+               struct history_params_t * params,
+               void * private_context)
 {
-       atexit(tty_atexit);
+       struct history_t * h = malloc(sizeof(*h));
+
+       memset(h, 0, sizeof(*h));
+
+       h->ttyin = ttyin;
+       h->ttyout = ttyout;
+       h->p = params;
+       h->private_context = private_context;
 
+       atexit(tty_atexit);
+       TAILQ_INIT(&h->cmd);
        /* store current tty settings in orig_termios */
        if (tcgetattr(h->ttyout, &orig_termios) < 0)
                fatal("can't get tty settings");
@@ -336,7 +365,10 @@ history_init(
 
        int fl = fcntl(h->ttyin, F_GETFL);
        fcntl(0, F_SETFL, fl | O_ASYNC);
+
        history_display(h);
+
+       return h;
 }
 
 int
index bcb211dfa4809b9867b7bffcb889f0d472df3e24..30158f3249bb87f58aef8bc76d80f9484bc2c742 100644 (file)
 #ifndef HISTORY_H_
 #define HISTORY_H_
 
+#include <sys/queue.h>
 
-typedef struct line_t {
-       struct line_t * next;
-       struct line_t * prev;
+struct history_t;
 
-       int size, len, pos;
-       char * line;
-} line_t, *line_p;
-
-
-typedef struct history_t {
-       int ttyin;
-       int ttyout;
-
-       line_p head, tail;
-       line_p current;
+typedef struct history_params_t {
        char prompt[32];
-       void * state;
-       int temp;
-
-       void *param;
-       int (*process_line)(struct history_t *h, line_p l);
-
-       //struct history_cmd_list_t * cmd_list;
-} history_t, *history_p;
-
-
-void
-history_init(
-               history_p h);
-
+       int (*process_line)(
+                       struct history_params_t *p,
+                       const char *cmd_line);
+} history_params_t;
+
+struct history_t *
+history_new(
+               int ttyin, int ttyout,
+               struct history_params_t * params,
+               void * private_context);
 int
 history_idle(
-               history_p h);
-
+               struct history_t * h);
 void
 history_display(
-               history_p h);
+               struct history_t * h);
 
 #endif /* HISTORY_H_ */
index b3aad918c3a015acde8870a5756119780603d0a3..382058305aba66b94c62805b4c3ccda0192ddfc1 100644 (file)
 
 int history_redisplay = 0;
 
-
+/*
+ * Called when the user type 'return' on the shell
+ */
 static int
 _history_process_line(
-               struct history_t *h,
-               line_p l )
+               void * param,
+               const char *cmd_line )
 {
        printf("\r\n");
-       history_cmd_execute(NULL, l->line);
+       history_cmd_execute(NULL, cmd_line);
 
        return 1;
 }
@@ -103,17 +105,16 @@ callback_sleep_prompt(
 #endif
 }
 
-history_t history = {
-       .ttyin = 0,
-       .ttyout = 1,
+history_params_t history_avr_params = {
        .prompt = "avr: ",
        .process_line = _history_process_line,
 };
 
+static struct history_t *history = NULL;
 void history_avr_init()
 {
-       history_init(&history);
-       prompt_fd = history.ttyin;
+       history = history_new(0, 1, &history_avr_params, NULL);
+       prompt_fd = 0; /* ttyin */
 //     avr->sleep = callback_sleep_prompt;
     avr_global_logger_set(raw_std_logger);
 }
@@ -128,9 +129,9 @@ void history_avr_idle()
                prompt_event++;
        if (history_redisplay) {
                history_redisplay = 0;
-               history_display(&history);
+               history_display(history);
                prompt_event++;
        }
        if (prompt_event)
-               history_idle(&history);
+               history_idle(history);
 }
index ac29e9a509a930d750bd4862335818d67fb3315f..a978c4c9c6c8183d5f611f922689a0beda65b4e9 100644 (file)
 #include "sim_hex.h"
 #include "sim_gdb.h"
 #include "uart_pty.h"
-#include "sim_vcd_file.h"
 
 #include "sim_args.h"
 #include "history_avr.h"
 
 uart_pty_t uart_pty;
 avr_t * avr = NULL;
-avr_vcd_t vcd_file;
 elf_firmware_t *code = NULL;
 
 avr_t *
@@ -50,10 +48,10 @@ sim_prepare(
        sim_args_t * a ); // TODO: Move to a header
 
 
-typedef struct avr_flash_desc_t {
-       char avr_flash_path[1024];
-       int avr_flash_fd;
-} avr_flash_desc_t;
+typedef struct shavr_runtime_t {
+       char    avr_flash_path[1024];
+       int             avr_flash_fd;
+} shavr_runtime_t;
 
 // avr special flash initalization
 // here: open and map a file to enable a persistent storage for the flash memory
@@ -62,7 +60,7 @@ avr_special_init(
                avr_t * avr,
                void * data)
 {
-       avr_flash_desc_t *flash_data = (avr_flash_desc_t *)data;
+       shavr_runtime_t *flash_data = (shavr_runtime_t *)data;
 
        printf("%s\n", __func__);
        // open the file
@@ -89,7 +87,7 @@ avr_special_deinit(
                avr_t* avr,
                void * data)
 {
-       avr_flash_desc_t *flash_data = (avr_flash_desc_t *)data;
+       shavr_runtime_t *flash_data = (shavr_runtime_t *)data;
 
        printf("%s\n", __func__);
        lseek(flash_data->avr_flash_fd, SEEK_SET, 0);
@@ -107,7 +105,9 @@ avr_run_thread(
                void * ignore)
 {
        while (1) {
-               avr_run(avr);
+               int state = avr_run(avr);
+               if (state == cpu_Done || state == cpu_Crashed)
+                       break;
        }
        return NULL;
 }
@@ -117,7 +117,7 @@ main(
                int argc,
                const char *argv[])
 {
-       avr_flash_desc_t flash_data;
+       shavr_runtime_t flash_data;
        sim_args_t args;
 
        if (sim_args_parse(&args, argc, argv, NULL)) {
@@ -148,7 +148,7 @@ main(
        history_avr_init();
 
 //     pthread_t run;
-       //pthread_create(&run, NULL, avr_run_thread, NULL);
+       //pthread_create(&run, NULL, avr_run_thread, avr);
 
        printf("Running...\n");
        while (1) {
index 650d46b21faffe3b1d6065d578a90d4829ae9e1e..36614c098eb7ebaaa196fd9daa929fba11995c78 100644 (file)
@@ -38,6 +38,7 @@
 static avr_t * avr = NULL;
 static avr_vcd_t input;
 
+// TODO: Signal the run thread and let it terminate
 static void
 sig_int(
                int sign)