coding rules
[apps/madtty.git] / madtty / madtty.c
index 3121367..ed810d6 100644 (file)
@@ -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 */
 
@@ -78,21 +79,20 @@ RoteTerm *rote_vt_create(int rows, int cols)
     rt->pd->scrolltop = 0;
     rt->pd->scrollbottom = rt->rows - 1;
 
-#ifdef DEBUG
-    fprintf(stderr, "Created a %d x %d terminal.\n", rt->rows, rt->cols);
-#endif
-
     return rt;
 }
 
 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);
 }
@@ -109,12 +109,10 @@ static void default_cur_set_attr(WINDOW *win, unsigned char attr)
     if (ROTE_ATTR_BLINK(attr))    wattron(win, A_BLINK);
 }
 
-#endif
-
-static inline unsigned char ensure_printable(unsigned char ch) 
-{ return ch >= 32 ? ch : 32; }
-
-#ifdef USE_NCURSES
+static inline unsigned char ensure_printable(unsigned char ch)
+{
+    return ch >= 32 ? ch : 32;
+}
 
 void rote_vt_draw(RoteTerm *rt, WINDOW *win, int srow, int scol, 
                   void (*cur_set_attr)(WINDOW*,unsigned char)) {
@@ -148,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. */
@@ -165,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;
 }
 
@@ -185,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,
@@ -243,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);
@@ -256,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;
@@ -271,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";
@@ -317,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 */
+    }
+}