many many updates
[apps/madtty.git] / demo / boxshell.c
index 43c007a..a1d746e 100644 (file)
@@ -6,9 +6,12 @@
  */
 
 #include <ncurses.h>
+#include <locale.h>
 #include <stdio.h>
 #include <signal.h>
 #include <string.h>
+#include <sys/time.h>
+#include <time.h>
 
 #include <madtty/madtty.h>
 
@@ -23,13 +26,15 @@ void sigchld(int signo __attribute__((unused)))
 
 int main(int argc, char *argv[])
 {
+    struct timeval next = { 0, 0 };
     RoteTerm *rt;
-    int i, j, ch, w, h;
+    int i, j, ch, w, h, pos;
+    char buf[BUFSIZ];
 
     signal(SIGCHLD, sigchld);
 
     w = 80;
-    h = 50;
+    h = 40;
     if (argc > 1) {
         char *p = argv[1];
         w = strtol(p, &p, 10);
@@ -37,6 +42,7 @@ int main(int argc, char *argv[])
             h = strtol(p, &p, 10);
     }
 
+    setlocale(LC_ALL, "");
     initscr();
     noecho();
     start_color();
@@ -70,17 +76,15 @@ int main(int argc, char *argv[])
     refresh();
 
     /* create a window with a frame */
-    term_win = newwin(h + 2, w + 2, 1, 2);
-    wattrset(term_win, COLOR_PAIR(7*8+7-0)); /* black over white */
+    term_win = newwin(h + 2, w + 2, 2, 3);
     wborder(term_win, '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0');
     mvwprintw(term_win, 0, 27, " Term In a Box ");
     wrefresh(term_win);
 
-    /* create the terminal and have it run bash */
     rt = rote_vt_create(h, w);
     {
-        const char *path = "/bin/bash";
-        const char *args[] = {"/bin/bash", "--login", NULL};
+        const char *path = getenv("SHELL") ?: "/bin/sh";
+        const char *args[] = { path, "--login", NULL};
 
         rote_vt_forkpty(rt, path, args);
     }
@@ -88,29 +92,44 @@ int main(int argc, char *argv[])
     /* 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;
     while (!getout) {
         fd_set rfds;
-        struct timeval tv = { 0 , 1000 };
+        struct timeval tv = { 0 , 1000 }, t;
 
         FD_ZERO(&rfds);
         FD_SET(rt->pty, &rfds);
 
         if (select(rt->pty + 1, &rfds, NULL, NULL, &tv) > 0) {
-            char buf[512];
-            int nbread;
-
-            nbread = rote_vt_read(rt, buf, sizeof(buf));
-            if (nbread > 0)
-                rote_vt_inject(rt, buf, nbread);
+            int nb;
+
+            nb = rote_vt_read(rt, buf + pos, sizeof(buf) - pos);
+            if (nb <= 0)
+                continue;
+            pos += nb;
+
+            nb = rote_vt_inject(rt, buf, pos);
+            if (nb <= 0)
+                continue;
+            memmove(buf, buf + nb, pos - nb);
+            pos -= nb;
         }
 
-
         while ((ch = getch()) != ERR) {
             rote_vt_keypress(rt, ch); /* pass the keypress for handling */
         }
 
-        rote_vt_draw(rt, term_win, 1, 1, NULL);
-        wrefresh(term_win);
+        gettimeofday(&t, NULL);
+        if (timercmp(&t, &next, >=)) {
+            rote_vt_draw(rt, term_win, 1, 1);
+            wrefresh(term_win);
+            gettimeofday(&next, NULL);
+            next.tv_usec += 1000 * 1000 / 100;
+            if (next.tv_usec > 1000 * 1000) {
+                next.tv_usec -= 1000 * 1000;
+                next.tv_sec++;
+            }
+        }
     }
 
     endwin();