coding rules
authorPierre Habouzit <madcoder@debian.org>
Sat, 11 Nov 2006 11:58:32 +0000 (12:58 +0100)
committerPierre Habouzit <madcoder@debian.org>
Sat, 11 Nov 2006 11:58:32 +0000 (12:58 +0100)
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
madtty/madtty.c

index 80db4c6..ed810d6 100644 (file)
@@ -36,10 +36,12 @@ RoteTerm *rote_vt_create(int rows, int cols)
     RoteTerm *rt;
     int i, j;
 
     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;
 
     /* 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++) {
 
         /* 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->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 */
 
     /* 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 */
 
 
     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;
 void rote_vt_destroy(RoteTerm *rt)
 {
     int i;
-    if (!rt) return;
+    if (!rt)
+        return;
 
     free(rt->pd);
     free(rt->line_dirty);
 
     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);
 }
     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);
     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 (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 */
     }
 
     /* 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)
 {
 }
 
 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;
 }
 
     rt->childpid = 0;
 }
 
@@ -179,7 +184,8 @@ void rote_vt_update(RoteTerm *rt)
     char buf[512];
     int bytesread;
     int n = ROTE_VT_UPDATE_ITERATIONS;
     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,
 
     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)
 {
 
 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;
     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);
 
     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)
 {
 
 void rote_vt_restore_snapshot(RoteTerm *rt, void *snapbuf)
 {
+    const int bytes_per_row = sizeof(RoteCell) * rt->cols;
+
     int i;
     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;
 
     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 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()
 {
 
 static void keytable_init()
 {
-    initialized = 1;
     memset(keytable, 0, KEY_MAX+1 * sizeof(const char*));
 
     keytable['\n']          = "\r";
     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~";
 }
 
     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 */
+    }
+}