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>
#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) {
{
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;
}
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;
} 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);
} 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;
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");
int fl = fcntl(h->ttyin, F_GETFL);
fcntl(0, F_SETFL, fl | O_ASYNC);
+
history_display(h);
+
+ return h;
}
int
#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_ */
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;
}
#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);
}
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);
}
#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 *
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
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
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);
void * ignore)
{
while (1) {
- avr_run(avr);
+ int state = avr_run(avr);
+ if (state == cpu_Done || state == cpu_Crashed)
+ break;
}
return NULL;
}
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)) {
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) {
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)