From: Pierre Habouzit Date: Sat, 11 Nov 2006 11:58:32 +0000 (+0100) Subject: coding rules X-Git-Url: http://git.madism.org/?a=commitdiff_plain;h=8f56ed8cb875b860ded79f630d439c953bc4aa4d;p=apps%2Fmadtty.git coding rules Signed-off-by: Pierre Habouzit --- diff --git a/madtty/madtty.c b/madtty/madtty.c index 80db4c6..ed810d6 100644 --- a/madtty/madtty.c +++ b/madtty/madtty.c @@ -36,10 +36,12 @@ RoteTerm *rote_vt_create(int rows, int cols) RoteTerm *rt; int i, j; - if (rows <= 0 || cols <= 0) return NULL; + if (rows <= 0 || cols <= 0) + return NULL; - if (! (rt = (RoteTerm*) malloc(sizeof(RoteTerm))) ) return NULL; - memset(rt, 0, sizeof(RoteTerm)); + rt = (RoteTerm*)calloc(sizeof(RoteTerm), 1); + if (!rt) + return NULL; /* record dimensions */ rt->rows = rows; @@ -56,21 +58,20 @@ RoteTerm *rote_vt_create(int rows, int cols) /* fill row with spaces */ for (j = 0; j < rt->cols; j++) { - rt->cells[i][j].ch = 0x20; /* a space */ + rt->cells[i][j].ch = 0x20; /* a space */ rt->cells[i][j].attr = 0x70; /* white text, black background */ } } /* allocate dirtiness array */ - rt->line_dirty = (bool*) malloc(sizeof(bool) * rt->rows); + rt->line_dirty = (bool*)calloc(sizeof(bool), rt->rows); /* initialization of other public fields */ rt->crow = rt->ccol = 0; rt->curattr = 0x70; /* white text over black background */ /* allocate private data */ - rt->pd = (RoteTermPrivate*) malloc(sizeof(RoteTermPrivate)); - memset(rt->pd, 0, sizeof(RoteTermPrivate)); + rt->pd = (RoteTermPrivate*)calloc(sizeof(RoteTermPrivate), 1); rt->pd->pty = -1; /* no pty for now */ @@ -84,11 +85,14 @@ RoteTerm *rote_vt_create(int rows, int cols) void rote_vt_destroy(RoteTerm *rt) { int i; - if (!rt) return; + if (!rt) + return; free(rt->pd); free(rt->line_dirty); - for (i = 0; i < rt->rows; i++) free(rt->cells[i]); + for (i = 0; i < rt->rows; i++) { + free(rt->cells[i]); + } free(rt->cells); free(rt); } @@ -142,7 +146,8 @@ pid_t rote_vt_forkpty(RoteTerm *rt, const char *command) ws.ws_xpixel = ws.ws_ypixel = 0; childpid = forkpty(&rt->pd->pty, NULL, NULL, &ws); - if (childpid < 0) return -1; + if (childpid < 0) + return -1; if (childpid == 0) { /* we are the child, running under the slave side of the pty. */ @@ -159,14 +164,14 @@ pid_t rote_vt_forkpty(RoteTerm *rt, const char *command) } /* if we got here we are the parent process */ - rt->childpid = childpid; - return childpid; + return (rt->childpid = childpid); } void rote_vt_forsake_child(RoteTerm *rt) { - if (rt->pd->pty >= 0) close(rt->pd->pty); - rt->pd->pty = -1; + if (rt->pd->pty >= 0) + close(rt->pd->pty); + rt->pd->pty = -1; rt->childpid = 0; } @@ -179,7 +184,8 @@ void rote_vt_update(RoteTerm *rt) char buf[512]; int bytesread; int n = ROTE_VT_UPDATE_ITERATIONS; - if (rt->pd->pty < 0) return; /* nothing to pump */ + if (rt->pd->pty < 0) + return; /* nothing to pump */ while (n--) { /* iterate at most ROVE_VT_UPDATE_ITERATIONS times. * As Phil Endecott pointed out, if we don't restrict this, @@ -237,10 +243,10 @@ void rote_vt_install_handler(RoteTerm *rt, rote_es_handler_t handler) void *rote_vt_take_snapshot(RoteTerm *rt) { - int i; - int bytes_per_row = sizeof(RoteCell) * rt->cols; + const int bytes_per_row = sizeof(RoteCell) * rt->cols; void *buf = malloc(bytes_per_row * rt->rows); void *ptr = buf; + int i; for (i = 0; i < rt->rows; i++, ptr += bytes_per_row) memcpy(ptr, rt->cells[i], bytes_per_row); @@ -250,8 +256,9 @@ void *rote_vt_take_snapshot(RoteTerm *rt) void rote_vt_restore_snapshot(RoteTerm *rt, void *snapbuf) { + const int bytes_per_row = sizeof(RoteCell) * rt->cols; + int i; - int bytes_per_row = sizeof(RoteCell) * rt->cols; for (i = 0; i < rt->rows; i++, snapbuf += bytes_per_row) { rt->line_dirty[i] = true; @@ -265,25 +272,9 @@ int rote_vt_get_pty_fd(RoteTerm *rt) } static const char *keytable[KEY_MAX+1]; -static int initialized = 0; - -static void keytable_init(); - -void rote_vt_keypress(RoteTerm *rt, int keycode) -{ - char c = (char) keycode; - - if (!initialized) keytable_init(); - - if (keycode >= 0 && keycode < KEY_MAX && keytable[keycode]) - rote_vt_write(rt, keytable[keycode], strlen(keytable[keycode])); - else - rote_vt_write(rt, &c, 1); /* not special, just write it */ -} static void keytable_init() { - initialized = 1; memset(keytable, 0, KEY_MAX+1 * sizeof(const char*)); keytable['\n'] = "\r"; @@ -311,4 +302,17 @@ static void keytable_init() keytable[KEY_F(10)] = "\e[21~"; } +void rote_vt_keypress(RoteTerm *rt, int keycode) +{ + char c = (char) keycode; + + if (keytable['\n'] != NULL) + keytable_init(); + + if (keycode >= 0 && keycode < KEY_MAX && keytable[keycode]) { + rote_vt_write(rt, keytable[keycode], strlen(keytable[keycode])); + } else { + rote_vt_write(rt, &c, 1); /* not special, just write it */ + } +}