a
[apps/madtty.git] / rote.c
diff --git a/rote.c b/rote.c
index e6b6bda..442e1d0 100644 (file)
--- a/rote.c
+++ b/rote.c
@@ -1,10 +1,35 @@
+/*
+LICENSE INFORMATION:
+This program is free software; you can redistribute it and/or
+modify it under the terms of the GNU Lesser General Public
+License (LGPL) as published by the Free Software Foundation.
+
+Please refer to the COPYING file for more information.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public
+License along with this program; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+
+Copyright (c) 2004 Bruno T. C. de Oliveira
+*/
+
+
 #include "rote.h"
 #include "roteprivate.h"
 #include <stdlib.h>
+#ifdef USE_PTY
 #include <pty.h>
+#endif
 #include <stdio.h>
 #include <string.h>
 
+#define ROTE_VT_UPDATE_ITERATIONS 5
+
 RoteTerm *rote_vt_create(int rows, int cols) {
    RoteTerm *rt;
    int i, j;
@@ -18,6 +43,9 @@ RoteTerm *rote_vt_create(int rows, int cols) {
    rt->rows = rows;
    rt->cols = cols;
 
+   /* default mode is replace */
+   rt->insert = false; 
+
    /* create the cell matrix */
    rt->cells = (RoteCell**) malloc(sizeof(RoteCell*) * rt->rows);
    for (i = 0; i < rt->rows; i++) {
@@ -66,6 +94,8 @@ void rote_vt_destroy(RoteTerm *rt) {
    free(rt);
 }
 
+#ifdef USE_NCURSES
+
 static void default_cur_set_attr(WINDOW *win, unsigned char attr) {
    int cp = ROTE_ATTR_BG(attr) * 8 + 7 - ROTE_ATTR_FG(attr);
    if (!cp) wattrset(win, A_NORMAL);
@@ -75,9 +105,13 @@ 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
+
 void rote_vt_draw(RoteTerm *rt, WINDOW *win, int srow, int scol, 
                                 void (*cur_set_attr)(WINDOW*,unsigned char)) {
 
@@ -96,6 +130,10 @@ void rote_vt_draw(RoteTerm *rt, WINDOW *win, int srow, int scol,
    wmove(win, srow + rt->crow, scol + rt->ccol);
 }
 
+#endif
+
+#ifdef USE_PTY
+
 pid_t rote_vt_forkpty(RoteTerm *rt, const char *command) {
    struct winsize ws;
    pid_t childpid;
@@ -132,14 +170,24 @@ void rote_vt_forsake_child(RoteTerm *rt) {
    rt->childpid = 0;
 }
 
+#endif
+
 void rote_vt_update(RoteTerm *rt) {
    fd_set ifs;
    struct timeval tvzero;
    char buf[512];
    int bytesread;
+   int n = ROTE_VT_UPDATE_ITERATIONS;
    if (rt->pd->pty < 0) return;  /* nothing to pump */
 
-   while (1) {
+   while (n--) { /* iterate at most ROVE_VT_UPDATE_ITERATIONS times.
+                  * As Phil Endecott pointed out, if we don't restrict this,
+                  * a program that floods the terminal with output
+                  * could cause this loop to iterate forever, never
+                  * being able to catch up. So we'll rely on the client
+                  * calling rote_vt_update often, as the documentation
+                  * recommends :-) */
+
       /* check if pty has something to say */
       FD_ZERO(&ifs); FD_SET(rt->pd->pty, &ifs);
       tvzero.tv_sec = 0; tvzero.tv_usec = 0;
@@ -206,4 +254,7 @@ void rote_vt_restore_snapshot(RoteTerm *rt, void *snapbuf) {
    }
 }
 
+int rote_vt_get_pty_fd(RoteTerm *rt) {
+   return rt->pd->pty;
+}