Separation of term and vt100, season 1 episode 3

This commit is contained in:
Julien Palard 2011-09-19 22:41:37 +02:00
parent c29a9f7900
commit 1254172ff6
3 changed files with 40 additions and 41 deletions

20
term.c
View File

@ -57,13 +57,13 @@ void term_parse_params(struct term_emul *term)
void term_call_CSI(struct term_emul *term, char c)
{
term_parse_params(term);
if (((term_action *)&term->callbacks->csi)[c - '0'] == NULL)
if (((term_action *)&term->callbacks.csi)[c - '0'] == NULL)
{
if (term->unimplemented != NULL)
term->unimplemented(term, "CSI", c);
goto leave;
}
((term_action *)&term->callbacks->csi)[c - '0'](term);
((term_action *)&term->callbacks.csi)[c - '0'](term);
leave:
term->state = INIT;
term->flag = '\0';
@ -73,13 +73,13 @@ leave:
void term_call_ESC(struct term_emul *term, char c)
{
if (((term_action *)&term->callbacks->esc)[c - '0'] == NULL)
if (((term_action *)&term->callbacks.esc)[c - '0'] == NULL)
{
if (term->unimplemented != NULL)
term->unimplemented(term, "ESC", c);
goto leave;
}
((term_action *)&term->callbacks->esc)[c - '0'](term);
((term_action *)&term->callbacks.esc)[c - '0'](term);
leave:
term->state = INIT;
term->stack_ptr = 0;
@ -88,13 +88,13 @@ leave:
void term_call_HASH(struct term_emul *term, char c)
{
if (((term_action *)&term->callbacks->hash)[c - '0'] == NULL)
if (((term_action *)&term->callbacks.hash)[c - '0'] == NULL)
{
if (term->unimplemented != NULL)
term->unimplemented(term, "HASH", c);
goto leave;
}
((term_action *)&term->callbacks->hash)[c - '0'](term);
((term_action *)&term->callbacks.hash)[c - '0'](term);
leave:
term->state = INIT;
term->stack_ptr = 0;
@ -104,13 +104,13 @@ leave:
void term_call_GSET(struct term_emul *term, char c)
{
if (c < '0' || c > 'B'
|| ((term_action *)&term->callbacks->scs)[c - '0'] == NULL)
|| ((term_action *)&term->callbacks.scs)[c - '0'] == NULL)
{
if (term->unimplemented != NULL)
term->unimplemented(term, "GSET", c);
goto leave;
}
((term_action *)&term->callbacks->scs)[c - '0'](term);
((term_action *)&term->callbacks.scs)[c - '0'](term);
leave:
term->state = INIT;
term->stack_ptr = 0;
@ -186,18 +186,16 @@ void term_read_str(struct term_emul *term, char *c)
}
struct term_emul *term_init(unsigned int width, unsigned int height,
struct term_callbacks *callbacks,
void (*vtwrite)(struct term_emul *, char))
{
struct term_emul *term;
term = malloc(sizeof(*term));
term = calloc(1, sizeof(*term));
term->width = width;
term->height = height;
term->cursor_pos_x = 0;
term->cursor_pos_y = 0;
term->stack_ptr = 0;
term->callbacks = callbacks;
term->state = INIT;
term->write = vtwrite;
return term;

3
term.h
View File

@ -120,7 +120,7 @@ struct term_emul
void (*write)(struct term_emul *, char c);
char stack[TERM_STACK_SIZE];
unsigned int stack_ptr;
struct term_callbacks *callbacks;
struct term_callbacks callbacks;
char flag;
void *user_data;
void (*unimplemented)(struct term_emul*,
@ -129,7 +129,6 @@ struct term_emul
};
struct term_emul *term_init(unsigned int width, unsigned int height,
struct term_callbacks *callbacks,
void (*write)(struct term_emul *, char));
void term_read(struct term_emul *term, char c);

58
test.c
View File

@ -1026,7 +1026,7 @@ void TBC(struct term_emul *term_emul)
vt100->tabulations[i] = '-';
}
dump("TBC", term_emul, vt100);
my_putstr(vt100->tabulations);
write(1, vt100->tabulations, 132);
my_putchar('\n');
}
@ -1200,13 +1200,11 @@ void unimplemented(struct term_emul* term_emul, char *seq, char chr)
struct term_emul *vt100(void)
{
struct term_emul *term;
struct term_callbacks callbacks;
struct vt100_term *vt100;
vt100 = malloc(sizeof(*vt100));
vt100 = calloc(1, sizeof(*vt100));
if (vt100 == NULL)
return NULL;
memset(&callbacks, 0, sizeof(callbacks));
vt100->height = 24;
vt100->width = 80;
vt100->screen = calloc(132 * SCROLLBACK * vt100->height,
@ -1214,6 +1212,8 @@ struct term_emul *vt100(void)
vt100->frozen_screen = calloc(132 * vt100->height,
sizeof(*vt100->frozen_screen));
vt100->tabulations = malloc(132);
if (vt100->tabulations == NULL)
return NULL; /* Need to free before returning ... */
vt100->margin_top = 0;
vt100->margin_bottom = vt100->height - 1;
vt100->selected_charset = 0;
@ -1221,31 +1221,33 @@ struct term_emul *vt100(void)
vt100->y = 0;
vt100->modes = MASK_DECANM;
vt100->top_line = 0;
callbacks.csi.f = HVP;
callbacks.csi.K = EL;
callbacks.csi.c = DA;
callbacks.csi.h = SM;
callbacks.csi.l = RM;
callbacks.csi.J = ED;
callbacks.csi.H = CUP;
callbacks.csi.C = CUF;
callbacks.csi.B = CUD;
callbacks.csi.r = DECSTBM;
callbacks.csi.m = SGR;
callbacks.csi.A = CUU;
callbacks.csi.g = TBC;
callbacks.esc.H = HTS;
callbacks.csi.D = CUB;
callbacks.esc.E = NEL;
callbacks.esc.D = IND;
callbacks.esc.M = RI;
callbacks.esc.n8 = DECRC;
callbacks.esc.n7 = DECSC;
callbacks.hash.n8 = DECALN;
term = term_init(80, 24, &callbacks, vt100_write);
term->user_data = &vt100;
term = term_init(80, 24, vt100_write);
term->callbacks.csi.f = HVP;
term->callbacks.csi.K = EL;
term->callbacks.csi.c = DA;
term->callbacks.csi.h = SM;
term->callbacks.csi.l = RM;
term->callbacks.csi.J = ED;
term->callbacks.csi.H = CUP;
term->callbacks.csi.C = CUF;
term->callbacks.csi.B = CUD;
term->callbacks.csi.r = DECSTBM;
term->callbacks.csi.m = SGR;
term->callbacks.csi.A = CUU;
term->callbacks.csi.g = TBC;
term->callbacks.esc.H = HTS;
term->callbacks.csi.D = CUB;
term->callbacks.esc.E = NEL;
term->callbacks.esc.D = IND;
term->callbacks.esc.M = RI;
term->callbacks.esc.n8 = DECRC;
term->callbacks.esc.n7 = DECSC;
term->callbacks.hash.n8 = DECALN;
term->user_data = vt100;
term->argc = 1;
term->argv[0] = 3;
TBC(term);
term->argc = 0;
term->unimplemented = unimplemented;
return term;
}