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;
/* 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 */
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);
}
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. */
}
/* 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;
}
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,
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);
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;
}
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";
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 */
+ }
+}