gcc says those values are out of bounds
[apps/madtty.git] / demo / boxshell.c
index 44a9d9c..15fbc41 100644 (file)
  */
 
 #include <ncurses.h>
-#include <stdio.h>
+
+#include <fcntl.h>
+#include <locale.h>
 #include <signal.h>
+#include <stdio.h>
 #include <string.h>
+#include <sys/ioctl.h>
 #include <sys/time.h>
+#include <termios.h>
 #include <time.h>
 
 #include <madtty/madtty.h>
 
-static unsigned char getout = 0;
+static int getout = 0, sigwinch = 0;
 static int screen_w, screen_h;
 static WINDOW *term_win;
+static struct timeval const slice = { 0, 1000 * 1000 / 100 };
 
-void sigchld(int signo __attribute__((unused)))
+void handler(int signo)
 {
-    getout = 1;
+    switch (signo) {
+      case SIGCHLD:
+        getout = 1;
+        break;
+      case SIGWINCH:
+        sigwinch = 1;
+        break;
+    }
 }
 
-int main(int argc, char *argv[])
+static struct timeval timeval_add(struct timeval a, struct timeval b)
 {
-    struct timeval next = { 0, 0 };
-    madtty_t *rt;
-    int i, j, ch, w, h, pos;
-    char buf[BUFSIZ];
-
-    signal(SIGCHLD, sigchld);
-
-    w = 80;
-    h = 40;
-    if (argc > 1) {
-        char *p = argv[1];
-        w = strtol(p, &p, 10);
-        if (*p++ == 'x')
-            h = strtol(p, &p, 10);
+    int usec = a.tv_usec + b.tv_usec;
+    a.tv_sec += b.tv_sec;
+    while (usec > 1000 * 1000) {
+        a.tv_sec += 1;
+        usec -= 1000 * 1000;
     }
+    a.tv_usec = usec;
+    return a;
+}
 
-    madtty_initialize();
-    getmaxyx(stdscr, screen_h, screen_w);
+static int is_expired(struct timeval now, struct timeval expiry)
+{
+    return now.tv_sec > expiry.tv_sec
+        || (now.tv_sec == expiry.tv_sec && now.tv_usec > expiry.tv_usec);
+}
 
-    /* paint the screen blue */
-    attrset(COLOR_PAIR(004));
-    for (i = 0; i < screen_h; i++)
-        for (j = 0; j < screen_w; j++)
-            addch(' ');
-    refresh();
+int main(void)
+{
+    madtty_t *rt;
+    int dirty = 0, pty;
+    struct timeval next;
+
+    signal(SIGCHLD, handler);
+    signal(SIGWINCH, handler);
+
+    setlocale(LC_ALL, "");
+    initscr();
+    start_color();
+    noecho();
+    raw();
+    nodelay(stdscr, TRUE);
+    keypad(stdscr, TRUE);
+    curs_set(0);
+    ESCDELAY=50;
+    madtty_init_vt100_graphics();
+    madtty_init_colors();
+    getmaxyx(stdscr, screen_h, screen_w);
 
     /* create a window with a frame */
-    term_win = newwin(h, w, 2, 3);
-    mvwprintw(term_win, 0, 27, " Term In a Box ");
-    wrefresh(term_win);
-
-    rt = madtty_create(h, w);
+    term_win = newwin(screen_h - 2, screen_w - 2, 1, 1);
+    rt = madtty_create(screen_h - 2, screen_w -2);
     {
         const char *path = getenv("SHELL") ?: "/bin/sh";
         const char *args[] = { path, "--login", NULL};
 
-        madtty_forkpty(rt, path, args);
+        madtty_forkpty(rt, path, args, &pty);
     }
 
     /* keep reading keypresses from the user and passing them to the terminal;
      * also, redraw the terminal to the window at each iteration */
-    ch = '\0';
-    pos = 0;
+    gettimeofday(&next, NULL);
     while (!getout) {
+        struct timeval tv = { 0, 1000 * 1000 / 100 };
         fd_set rfds;
-        struct timeval tv = { 0 , 1000 }, t;
+        int ch;
 
         FD_ZERO(&rfds);
-        FD_SET(rt->pty, &rfds);
+        FD_SET(0, &rfds);
+        FD_SET(pty, &rfds);
+
+        if (select(pty + 1, &rfds, NULL, NULL, &tv) > 0) {
+            if (FD_ISSET(pty, &rfds)) {
+                madtty_process(rt);
+                dirty = 1;
+            }
+        }
 
-        if (select(rt->pty + 1, &rfds, NULL, NULL, &tv) > 0) {
-            int nb;
+        if (sigwinch) {
+            int fd, cols = -1, rows = -1;
+            struct winsize w;
 
-            nb = madtty_read(rt, buf + pos, sizeof(buf) - pos);
-            if (nb <= 0)
-                continue;
-            pos += nb;
+            if ((fd = open("/dev/tty", O_RDONLY)) != -1) {
+                if (ioctl(fd, TIOCGWINSZ, &w) != -1) {
+                    rows = w.ws_row;
+                    cols = w.ws_col;
+                }
+                close(fd);
+            }
+            if (rows <= 0) {
+                rows = atoi(getenv("LINES") ?: "24");
+            }
+            if (cols <= 0) {
+                cols = atoi(getenv("COLUMNS") ?: "80");
+            }
 
-            nb = madtty_inject(rt, buf, pos);
-            if (nb <= 0)
-                continue;
-            memmove(buf, buf + nb, pos - nb);
-            pos -= nb;
+            resizeterm(rows, cols);
+            madtty_resize(rt, rows - 2, cols - 2);
+            wresize(term_win, rows - 2, cols - 2);
+            sigwinch = 0;
+            erase();
         }
 
         while ((ch = getch()) != ERR) {
             madtty_keypress(rt, ch); /* pass the keypress for handling */
+            dirty = 1;
         }
 
-        gettimeofday(&t, NULL);
-        if (timercmp(&t, &next, >=)) {
+        gettimeofday(&tv, NULL);
+        if (dirty && is_expired(tv, next)) {
             madtty_draw(rt, term_win, 0, 0);
             wrefresh(term_win);
-            gettimeofday(&next, NULL);
-            next.tv_usec += 1000 * 1000 / 50;
-            if (next.tv_usec > 1000 * 1000) {
-                next.tv_usec -= 1000 * 1000;
-                next.tv_sec++;
-            }
+            dirty = 0;
+            next = timeval_add(tv, slice);
         }
     }