kon2 (0.3.9b-20) src/term.c

Summary

 src/term.c |   69 ++++++++++++++++++++++++++++++++++++-------------------------
 1 file changed, 41 insertions(+), 28 deletions(-)

    
download this patch

Patch contents

--- kon2-0.3.9b.orig/src/term.c
+++ kon2-0.3.9b/src/term.c
@@ -25,6 +25,9 @@
  * 
  */
 
+/* #define ErrorF(x,y)  fprintf(stderr,x,y); */
+#define ErrorF(x,y)  
+
 #include	<stdio.h>
 #include	<stdlib.h>
 #include	<unistd.h>
@@ -58,6 +61,7 @@
 #include	<child.h>
 #include	<interface.h>
 #include	<sock.h>
+#include	<pty.h>
 #ifdef	MINI_KON
 static int mouseFd=-1;
 #else
@@ -81,6 +85,9 @@
 static char	ptyName[MAX_TTYNAME + 1];
 static int orgVtNum = -1;
 
+static void ChangeNewConsole ();
+void RunStartupCmd ();
+
 struct initInfo {
     bool display;			 /* display initialized */
     bool utmp;			 /* utmp set */
@@ -180,10 +187,11 @@
     numFds = masterPty + 1;
 #else
     FD_SET(sockFd, &orgReadFds);
-    if (mInfo.has_mouse && mouseFd > 0)
+    if (mInfo.has_mouse)
 	MouseSetRfd(mouseFd);
     else
 	MouseResetRfd(mouseFd);
+    numFds = (masterPty < numFds) ? numFds : masterPty + 1;
 #endif
     /* Note: we use timeout on select call even if cursor blink is off
        because of screen saver and mouse cursor timeout. */
@@ -197,8 +205,16 @@
 	    tv.tv_sec = 0;
 	    tv.tv_usec = 100000; /* 0.1 sec */
 	    v = select(numFds, &readFds, NULL, NULL, &tv);
-	} while (v == 0 || (v < 0 && (errno == EINTR || mouseFd < 0)));
+	} while (v == 0 || (v < 0 && 
+			(errno == EINTR || mouseFd < 0 ||
+				(errno == EBADF && mInfo.has_mouse))));
 	if (v < 0) {
+		ErrorF("v: %d\r\n", v);
+		ErrorF("numFds: %d\r\n", numFds);
+		ErrorF("masterPty: %d\r\n", masterPty);
+		ErrorF("sockFd: %d\r\n", sockFd);
+		ErrorF("mouseFd: %d\r\n", mouseFd);
+		ErrorF("errno: %d\r\n", errno);
 	    PerrorExit("select");
 	}
 	if (FD_ISSET(masterPty, &readFds)) {
@@ -235,7 +251,10 @@
 	if (FD_ISSET(sockFd, &readFds)) SocketInterface(sockFd);
 	if (mInfo.has_mouse && mouseFd > 0) {
 	    if (FD_ISSET(mouseFd, &readFds) && con.active) {
-		i = read(mouseFd, buff, BUFSIZ);
+                i = -1;
+                if(mouseFd > 0) {
+                        i = read(mouseFd, buff, BUFSIZ);
+                }
 		if (i > 0) MouseGetPacket(buff, i);
 		PollCursor(TRUE);
 	    }
@@ -248,7 +267,7 @@
 {
     int	i = 0;
     const	char *video = "NORMAL";
-    extern int ConfigExecProg(const char *);
+    extern int ConfigExecProg(const char *,char *const args[]);
     
     if (argc > 0 && argv[0][0] != '-') {
 	video = argv[0];
@@ -268,8 +287,10 @@
 	    KonError("no value for `%s'\r\n", arg);
 	    break;
 	}
-	if (!strcasecmp(arg, "e"))
-	    ConfigExecProg(argv[i]);
+	if (!strcasecmp(arg, "e")){
+	    ConfigExecProg(argv[i],&(argv[i]));
+	    break;
+	}
 	else if (SetCapArg(arg, argv[i]) < 0)
 	    KonWarn("invalid capability `%s' ignored\r\n", arg);
 	i++;
@@ -279,6 +300,8 @@
 static int	savedArgc;		 /* argc of startup time */
 static const char	**savedArgv;	 /* argv of startup time */
 
+static void ChangeNewConsole();
+
 /* Do initialization before reading config file */
 void
 KonInit(int argc, const char *argv[])
@@ -288,7 +311,6 @@
     init.display = init.utmp = init.socket = init.termios = FALSE;
     /* Initialize subsystems. */
     
-    ChangeNewConsole();
     CapInit();
 
 #ifndef	MINI_KON
@@ -321,8 +343,9 @@
     CapInit();
     ConsoleCleanup();
 #ifndef	MINI_KON
-    if (mInfo.has_mouse)
+    if (mInfo.has_mouse && mouseFd > 0) {
 	MouseCleanup();
+    }
     MouseInit();
 #endif
 #if 0
@@ -357,8 +380,9 @@
 	TryTermReset(0, (const char **)NULL) < 0)
 	KonFatal("giving up\r\n");
 #ifndef	MINI_KON
-    if (mInfo.has_mouse)
+    if (mInfo.has_mouse) {
 	mouseFd = MouseStart();
+    }
 #endif
     VtStart();
     ConsoleStart();
@@ -388,21 +412,15 @@
     char	ls, ln;
 #ifdef	MINI_KON
     extern void LoadMiniFont();
+#else
+    RunStartupCmd();
 #endif
     
-    /* Open PTY(master) */
-    for (ls = 'p'; ls <= 's'; ls ++) {
-	for (ln = 0; ln <= 0xF; ln ++) {
-	    sprintf(ptyName, "/dev/pty%1c%1x", ls, ln);
-	    if ((masterPty = open(ptyName, O_RDWR)) >= 0) break;
-	}
-	if (masterPty >= 0) break;
-    }
-    if (masterPty < 0) {
-	KonMessage("can not get master pty\r\n");
-	PerrorExit(ptyName);
+    /* Open PTY */
+    if (openpty (&masterPty, &slavePty, ptyName, NULL, NULL) == -1) {
+	KonMessage ("cannot open PTY\r\n");
+	PerrorExit ("openpty");
     }
-    ptyName[5] = 't';
 
 #ifndef	MINI_KON
     if (mInfo.has_mouse) {
@@ -411,7 +429,6 @@
 #endif
 
 #ifndef	MINI_KON
-    chown("/dev/tty0", getuid(), getgid());
 #if defined(linux)
     sockFd = SocketInit(TermName() + 8);
 #elif defined(__FreeBSD__)
@@ -495,10 +512,6 @@
 	  }
 	}
 #endif
-	/* Open TTY(slave) */
-	if ((slavePty = open(ptyName, O_RDWR)) < 0) {
-	    PerrorExit(ptyName);
-	}
 	close(masterPty);
 	/* Set old tio to TTY */
 	tcsetattr(slavePty, TCSAFLUSH, &oldTio);
@@ -571,9 +584,9 @@
     }
     setsid();
 #if defined(linux)
-    sprintf(vtty, "/dev/tty%d", vtNum);
+    snprintf(vtty, sizeof(vtty), "/dev/tty%d", vtNum);
 #elif defined(__FreeBSD__)
-    sprintf(vtty, "/dev/ttyv%d", vtNum);
+    snprintf(vtty, sizeof(vtty), "/dev/ttyv%d", vtNum);
 #endif
     if ((vfd = open(vtty, O_RDWR)) < 0)
 	KonFatal("can't open %s", vtty);