Skip site navigation (1)Skip section navigation (2)

ports/127321: japanese/kon2-16dot: buffer overflow and mouse bugs

From:Arai Toshihiko <double_star@scorpio.plala.or.jp>
Date:Fri, 12 Sep 2008 13:19:21 GMT
Subject:buffer overflow and mouse bugs
Send-pr version:www-3.1

Number:127321
Category:ports
Synopsis:japanese/kon2-16dot: buffer overflow and mouse bugs
Severity:serious
Priority:medium
Responsible:freebsd-ports-bugs@FreeBSD.org
State:open
Class:sw-bug
Arrival-Date:Fri Sep 12 13:20:01 UTC 2008
Closed-Date:
Last-Modified:Mon Oct 27 17:05:55 UTC 2008
Originator:Arai Toshihiko
Release:7.0-RELEASE

Organization:
 
Environment:
FreeBSD kkk 7.0-RELEASE FreeBSD 7.0-RELEASE #4: Thu Sep 11 23:10:06 JST 2008 root@kkk:/usr/src/sys/i386/compile/KKK i386
Description:
ports/japanese/kon2-16dot
ports/japanese/kon2-14dot

The following problem exists.
1. The mouse cannot use it.
2. buffer overflow
How-To-Repeat:
 
Fix:
The patch is sent. However, I do not think it is complete.
Release-Note:
 
Audit-Trail:
Reply via E-mail
From:Stefan Walter <stefan@freebsd.org>
Date:Tue, 21 Oct 2008 09:37:51 +0200
Hi Arai,

in your PR, you mention a patch, but a patch is not included. Can you send
a followup to this PR with your patch attached?

Regards,
Stefan

Reply via E-mail
From:Toshihiko ARAI <double_star@scorpio.plala.or.jp>
Date:Wed, 22 Oct 2008 17:54:14 +0900
At 21 Oct 2008 09:37:51 +0200 Stefan Walter wrote:
> Hi Arai,
>
> in your PR, you mention a patch, but a patch is not included. Can you send
> a followup to this PR with your patch attached?
>
> Regards,
> Stefan

I have enhanced some functions based on KON2. I assumed it is KON2b.
The configuration file is incompatible.

The main additional function as follows.

- History backscroll
- Add new charset
- Rewrite bdf font loader
- VGAGM display driver for small size font
- Add emacs support
- Add option variables and bug fixes

http://www8.plala.or.jp/double_star/kon2b/index-e.html

--
ARAI toshihiko

Reply via E-mail
From:Stefan Walter <stefan@freebsd.org>
Date:Wed, 22 Oct 2008 11:25:00 +0200
Toshihiko ARAI, 22.10.08, 10:54h CEST:

> I have enhanced some functions based on KON2. I assumed it is KON2b.
> The configuration file is incompatible.
>
> The main additional function as follows.
>
> - History backscroll
> - Add new charset
> - Rewrite bdf font loader
> - VGAGM display driver for small size font
> - Add emacs support
> - Add option variables and bug fixes
>
> http://www8.plala.or.jp/double_star/kon2b/index-e.html

This looks like a new software, then, not a patch to the existing port. If
you want to add it to the ports collection, please feel free to submit a
separate PR for that.

The description of your problem report is a bit vague. You reported that
there is a buffer overflow, but detailed information about it (and a patch
to correct it, if you have one) is missing.

Regards,
Stefan

Reply via E-mail
From:Toshihiko ARAI <double_star@scorpio.plala.or.jp>
Date:Thu, 23 Oct 2008 19:13:42 +0900


At 22 Oct 2008 11:25:00 +0200 Stefan Walter wrote:
> The description of your problem report is a bit vague. You reported that
> there is a buffer overflow, but detailed information about it (and a patch
> to correct it, if you have one) is missing.
>
I seem to have failed in send-pr.

I make the patch for KON2 ports. It is a patch that I tried to send before.
The change point in this patch as follows.

- Enable of cut and paste with mouse.
- Check of buffer overflow.
- Review related free().
- The check on syscons is added.

--
ARAI Toshihiko


Download kon2.diff.bz2
(Binary attachment not viewable.)



Reply via E-mail
From:Stefan Walter <stefan@freebsd.org>
Date:Sat, 25 Oct 2008 13:43:58 +0200
Uncompressed version of the patch attached.

Download patch-2.diff
Index: font/fld.c
===================================================================
RCS file: /home/toshi/cvs/kon2/font/fld.c,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 fld.c
--- font/fld.c   30 Sep 2008 08:02:59 -0000      1.1.1.1
+++ font/fld.c   23 Oct 2008 09:24:33 -0000
@@ -52,36 +52,13 @@
     {NULL, NULL}
 };
 
-void UnloadShmem(char fnum)
-{
-    key_t shmkey;
-    int shmid;
-    struct shmid_ds shmseg;
-
-#if defined(linux)
-    shmkey = ftok(CONFIG_NAME, fnum);
-#elif defined(__FreeBSD__)
-    shmkey = 5000 + (fnum & 0x7F);
-#endif
-    if ((shmid = shmget(shmkey, sizeof(struct fontInfo), 0444)) < 0)
-        return;
-    shmctl(shmid, IPC_STAT, &shmseg);
-    if (shmseg.shm_nattch < 1) {
-        shmctl(shmid, IPC_RMID, 0);
-    }
-}
-
 int CheckLoadedFont(char fnum)
 {
     key_t shmkey;
     extern int forceLoad;
 
     if (forceLoad) return(EOF);
-#if defined(linux)
-    shmkey = ftok(SHMEM_NAME, fnum);
-#elif defined(__FreeBSD__)
-    shmkey = 5000 + (fnum & 0x7F);
-#endif
+    shmkey = ftok(CONFIG_NAME, fnum);
     if (shmget(shmkey, 1, 0444) == EOF) return(EOF);
     return(0);
 }
@@ -93,11 +70,7 @@
     int shmid;
     u_char *shmbuff;
 
-#if defined(linux)
-    shmkey = ftok(SHMEM_NAME, fi->type);
-#elif defined(__FreeBSD__)
-    shmkey = 5000 + (fi->type & 0x0000007F);
-#endif
+    shmkey = ftok(CONFIG_NAME, fi->type);
     shmid = shmget(shmkey, fi->size+sizeof(struct fontInfo),
                    IPC_CREAT|0666);
     shmbuff = shmat(shmid, 0, 0);
@@ -134,11 +107,7 @@
     int shmid;
     struct fontInfo *fi;
 
-#if defined(linux)
     shmkey = ftok(CONFIG_NAME, fnum);
-#elif defined(__FreeBSD__)
-    shmkey = 5000 + (fnum & 0x7F);
-#endif
     if ((shmid = shmget(shmkey, sizeof(struct fontInfo), 0444)) < 0)
         return(0);
     fi = (struct fontInfo*)shmat(shmid, 0, SHM_RDONLY);
@@ -194,7 +163,7 @@
                         (n & CHR_DBC) ?
                         fDRegs[n&~CHR_DFLD].registry:
                         fSRegs[n&~CHR_SFLD].registry);
-                UnloadShmem(n | CHR_SFLD);
+                DownShmem(n | CHR_SFLD);
                 break;
             }
             st = ST_ARG;
Index: font/fontx2.c
===================================================================
RCS file: /home/toshi/cvs/kon2/font/fontx2.c,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 fontx2.c
--- font/fontx2.c        30 Sep 2008 08:02:59 -0000      1.1.1.1
+++ font/fontx2.c        23 Oct 2008 09:24:33 -0000
@@ -37,6 +37,7 @@
 #include        <string.h>
 #include        <sys/socket.h>
 
+#include        <defs.h>
 #include        <interface.h>
 #include        <fnld.h>
 
@@ -170,7 +171,7 @@
             font = FontLoadSFontx(fp, header);
         else exit(0);
     }
-    free(header);
+    SafeFree(header);
     return(font);
 }
 
Index: include/defs.h
===================================================================
RCS file: /home/toshi/cvs/kon2/include/defs.h,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 defs.h
--- include/defs.h       30 Sep 2008 08:02:59 -0000      1.1.1.1
+++ include/defs.h       23 Oct 2008 09:27:32 -0000
@@ -37,4 +37,6 @@
 #define FAILURE (-1)
 #define SUCCESS (0)
 
+#define SafeFree(x)   do { if ((x) != NULL) free(x), (x) = NULL; } while (0)
+
 #endif
Index: include/fnld.h
===================================================================
RCS file: /home/toshi/cvs/kon2/include/fnld.h,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 fnld.h
--- include/fnld.h       30 Sep 2008 08:02:59 -0000      1.1.1.1
+++ include/fnld.h       23 Oct 2008 09:24:33 -0000
@@ -51,6 +51,10 @@
 #define FR_ATTACH       1
 #define FR_PROXY        2
 
+#if defined(__FreeBSD__)
+#define ftok(p, n) (5000 + ((n) & 0x7F))
+#endif
+
 extern struct fontRegs fSRegs[], fDRegs[];
 extern struct fontRegs *sbFReg, *dbFReg;
 
Index: include/mem.h
===================================================================
RCS file: /home/toshi/cvs/kon2/include/mem.h,v
retrieving revision 1.1.1.2
diff -u -r1.1.1.2 mem.h
--- include/mem.h        30 Sep 2008 08:09:55 -0000      1.1.1.2
+++ include/mem.h        23 Oct 2008 09:24:33 -0000
@@ -111,6 +111,5 @@
 extern void wmove(void *, void *, int);
 extern void lmove(void *, void *, int);
 #endif
-extern void SafeFree(void **);
 
 #endif
Index: include/term.h
===================================================================
RCS file: /home/toshi/cvs/kon2/include/term.h,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 term.h
--- include/term.h       30 Sep 2008 08:02:59 -0000      1.1.1.1
+++ include/term.h       23 Oct 2008 09:24:33 -0000
@@ -36,5 +36,6 @@
 extern void     TermStart(void);         /* start procesing */
 extern void     TermRestart(int fd);     /* restart kon (args are read from fd) */
 extern int      masterPty;               /* master pseudo-tty file descriptor */
+extern int      TermGetNumber(void);
 
 #endif
Index: include/vt.h
===================================================================
RCS file: /home/toshi/cvs/kon2/include/vt.h,v
retrieving revision 1.1.1.2
diff -u -r1.1.1.2 vt.h
--- include/vt.h 30 Sep 2008 08:09:55 -0000      1.1.1.2
+++ include/vt.h 23 Oct 2008 09:24:33 -0000
@@ -59,12 +59,10 @@
         ins,
         active,
         wrap,
-#if defined(__FreeBSD__)
-        text_mode,
-        cursor_key_mode;
-#else
         text_mode;
-#endif
+#if defined(__FreeBSD__)
+    bool cursor_key_mode;
+#endif  /* __FreeBSD__ */
 };
 
 extern struct   _con_info con;
Index: lib/coding.c
===================================================================
RCS file: /home/toshi/cvs/kon2/lib/coding.c,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 coding.c
--- lib/coding.c 30 Sep 2008 08:02:59 -0000      1.1.1.1
+++ lib/coding.c 23 Oct 2008 09:24:33 -0000
@@ -28,6 +28,7 @@
 #include        <config.h>
 
 #include        <stdio.h>
+#include        <string.h>
 #include        <errno.h>
 
 #include        <interface.h>
Index: lib/font.c
===================================================================
RCS file: /home/toshi/cvs/kon2/lib/font.c,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 font.c
--- lib/font.c   30 Sep 2008 08:02:59 -0000      1.1.1.1
+++ lib/font.c   23 Oct 2008 09:24:33 -0000
@@ -44,11 +44,7 @@
     int shmid;
     struct shmid_ds shmseg;
 
-#if defined(linux)
     shmkey = ftok(CONFIG_NAME, fnum);
-#elif defined(__FreeBSD__)
-    shmkey = 5000 + (fnum & 0x7F);
-#endif
     if ((shmid = shmget(shmkey, sizeof(struct fontInfo), 0444)) < 0)
         return;
     shmctl(shmid, IPC_STAT, &shmseg);
@@ -63,11 +59,7 @@
     key_t shmkey;
     int shmid;
 
-#if defined(linux)
     shmkey = ftok(CONFIG_NAME, fnum);
-#elif defined(__FreeBSD__)
-    shmkey = 5000 + (fnum & 0x7F);
-#endif
     if ((shmid = shmget(shmkey, sizeof(struct fontInfo), 0444)) < 0) return(0);
     return((u_char*)shmat(shmid, 0, SHM_RDONLY));
 }
Index: lib/getcap.c
===================================================================
RCS file: /home/toshi/cvs/kon2/lib/getcap.c,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 getcap.c
--- lib/getcap.c 30 Sep 2008 08:02:59 -0000      1.1.1.1
+++ lib/getcap.c 23 Oct 2008 09:24:33 -0000
@@ -73,8 +73,10 @@
                 fprintf(stderr, "cap %s redefined (default %s)\r\n", name,
                         def_value ? def_value : "None");
 #endif
+                SafeFree(cp->name);
                 cp->name = strdup(name);
                 cp->func = func;
+                SafeFree(cp->def_value);
                 if (def_value)
                         cp->def_value = strdup(def_value);
                 return;
@@ -86,8 +88,10 @@
                         fprintf(stderr, "cap %s defined (default %s)\r\n", name,
                                 def_value ? def_value : "None");
 #endif
+                        SafeFree(cp->name);
                         cp->name = strdup(name);
                         cp->func = func;
+                        SafeFree(cp->def_value);
                         if (def_value)
                                 cp->def_value = strdup(def_value);
                         return;
@@ -110,12 +114,9 @@
                         fprintf(stderr, "cap %s deleted\r\n", cp->name);
 #endif
                 cp->initialized = 0;
-                if (cp->name)
-                        free(cp->name);
-                if (cp->arg)
-                        free(cp->arg);
-                if (cp->def_value)
-                        free(cp->def_value);
+                SafeFree(cp->name);
+                SafeFree(cp->arg);
+                SafeFree(cp->def_value);
                 cp->name = cp->arg = cp->def_value = NULL;
         }
 }
@@ -149,10 +150,13 @@
                         if ((c = FindCap(p)) != NULL) {
                                 /* Found matching capability.  Get body from file. */
                                 char *l = buf;
+                                int bnum = 0;
                                 while (fgets(line, MAX_COLS, capFp) != NULL
                                        && line[0] == '\t') {
                                         char *l2 = line;
                                         while (*l2 != '\n' && *l2 != '#') {
+                                                if (++bnum > BUF_SIZE - 2)
+                                                        fatal("buffer overflow at configuration file\n");
                                                 *l++ = *l2++;
                                         }
                                         *l++ = '\n';
@@ -223,6 +227,7 @@
                 /* Protected capability. */
                 return FAILURE;
         }
+        SafeFree(cp->arg);
         cp->arg = strdup(value);
 #ifdef  DEBUG
         fprintf(stderr, "Setting arg for %s to %s\r\n", capName, value);
@@ -234,8 +239,12 @@
 
 bool BoolConf(const char *confstr)
 {
-        char name[MAX_COLS];
-        sscanf(confstr, "%s", name);
+        char *name, *last, *sep = " \t\r\n";
+
+        name = strtok_r((char *)confstr, sep, &last);
+        if (name == NULL)
+            return FALSE;
+
         if (strcasecmp(name, "On") == 0 ||
             strcasecmp(name, "True") == 0) {
                 return TRUE;
Index: lib/mem.c
===================================================================
RCS file: /home/toshi/cvs/kon2/lib/mem.c,v
retrieving revision 1.1.1.2
diff -u -r1.1.1.2 mem.c
--- lib/mem.c    30 Sep 2008 08:09:56 -0000      1.1.1.2
+++ lib/mem.c    23 Oct 2008 09:24:33 -0000
@@ -79,10 +79,3 @@
 }
 #endif
 
-void    SafeFree(void **p)
-{
-        if (*p) {
-                free(*p);
-                *p = NULL;
-        }
-}
Index: src/child.c
===================================================================
RCS file: /home/toshi/cvs/kon2/src/child.c,v
retrieving revision 1.1.1.2
diff -u -r1.1.1.2 child.c
--- src/child.c  30 Sep 2008 08:09:56 -0000      1.1.1.2
+++ src/child.c  23 Oct 2008 09:24:33 -0000
@@ -40,16 +40,18 @@
 #include        <version.h>
 #include        <vc.h>
 
-static char *startupStr, *execProg;
+static char *startupStr = NULL, *execProg = NULL;
 
 int     ConfigExecProg(const char *string)
 {
+        SafeFree(execProg);
         execProg = strdup(string);
         return SUCCESS;
 }
 
 static int      ConfigStartup(const char *string)
 {
+        SafeFree(startupStr);
         startupStr = strdup(string);
         return SUCCESS;
 }
@@ -81,7 +83,8 @@
 
 void    ChildCleanup(void)
 {
-        free(startupStr);
+        SafeFree(startupStr);
+        SafeFree(execProg);
 }
 
 void    ChildStart(FILE *errfp)
@@ -105,46 +108,44 @@
         win.ws_col = dInfo.txmax + 1;
         win.ws_xpixel = win.ws_ypixel = 0;
         ioctl(STDIN_FILENO, TIOCSWINSZ, &win);
-        sprintf(buff,"TERM=vt100");
-#endif
+        strcpy(buff, "TERM=vt100-color");
+#endif  /* __FreeBSD__ */
 
         tcap = strdup(buff);
         putenv(tcap);
 
-        if (startupMessage)
+        if (startupMessage) {
             printf("\rKON2 Kanji On Console " VERSION
-                   " using VT number %c\r\n"
+                   " using VT number %d\n"
                    "%*s\r\n"
-#if defined(__FreeBSD__)
-                   "%*s\r\n", *(ttyname(fileno(errfp))+9),
-#else   /* linux */
-                   "%*s\r\n", *(ttyname(fileno(errfp))+8),
-#endif
+                   "%*s\r\n",
+                   TermGetNumber(),
                    dInfo.txmax,
                    "Copyright (C) "
                    "1993-1996  Takashi MANABE",
                    dInfo.txmax,
                    "1993, 1994 MAEDA Atusi   ");
 #if defined(__FreeBSD__)
-        printf("\rKON for FreeBSD-2.x ver0.01 Takashi OGURA\r\n");
+            printf("\rKON for FreeBSD-2.x ver0.01 Takashi OGURA\r\n");
 #endif
-
+        }
 
 /*
-        printf("KON using VT number %c.\n\n",
-               *(ttyname(fileno(errfp))+8));
+        printf("KON using VT number %d.\n\n", TermGetNumber());
 */
         fflush(stdout);
 
         if (execProg)
             execlp(execProg, execProg, 0);
         else {
+            char *ptr;
             if ((execProg = getenv("SHELL")) == NULL)
                 execProg = "/bin/sh";
             if ((tail = rindex(execProg, '/')) == NULL)
                 tail = " sh";
-            sprintf(buff, "-%s", tail + 1);
-            execl(execProg, buff, 0);
+            ptr = strdup(tail);
+            *ptr = '-';
+            execl(execProg, ptr, 0);
         }
         fprintf(errfp, "KON> couldn't exec shell\r\n");
         fprintf(errfp, "%s: %s\r\n", execProg, strerror(errno));
Index: src/errors.c
===================================================================
RCS file: /home/toshi/cvs/kon2/src/errors.c,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 errors.c
--- src/errors.c 30 Sep 2008 08:02:59 -0000      1.1.1.1
+++ src/errors.c 23 Oct 2008 09:24:33 -0000
@@ -48,7 +48,7 @@
                 vfprintf(stderr, format, args);
         } else {
                 VtEmu(head, strlen(head));
-                vsprintf(buf, format, args);
+                vsnprintf(buf, sizeof(buf), format, args);
                 VtEmu(buf, strlen(buf));
         }
 }
Index: src/main.c
===================================================================
RCS file: /home/toshi/cvs/kon2/src/main.c,v
retrieving revision 1.1.1.2
diff -u -r1.1.1.2 main.c
--- src/main.c   30 Sep 2008 08:09:56 -0000      1.1.1.2
+++ src/main.c   23 Oct 2008 09:24:33 -0000
@@ -29,11 +29,45 @@
 #include <stdlib.h>
 #include <unistd.h>
 
+#if defined(__FreeBSD__)
+#include <err.h>
+#include <osreldate.h>
+#if __FreeBSD_version >= 410000
+#  include      <sys/consio.h>
+#  include      <sys/kbio.h>
+#else
+#  include      <machine/console.h>
+#endif
+#endif  /* __FreeBSD__ */
+
 #include <getcap.h>
 
 #include <version.h>
 #include <term.h>
 
+/* handler for atexit() */
+static void
+reset_text_mode(void)
+{
+    int mode;
+
+#if defined(__FreeBSD__)
+    if (ioctl(0, KDGETMODE, &mode) == -1)
+        warn("ioctl(0, KDGETMODE)");
+    else if (mode != KD_TEXT) {
+        ioctl(0, KDSETMODE, KD_TEXT);
+    }
+
+#elif defined(__NetBSD__)
+    if (ioctl(0, WSDISPLAYIO_GMODE, &mode) == -1)
+        warn("ioctl(0, WSDISPLAYIO_GMODE)");
+    else if (mode != WSDISPLAYIO_MODE_EMUL) {
+        mode = WSDISPLAYIO_MODE_EMUL;
+        ioctl(0, WSDISPLAYIO_SMODE, &mode);
+    }
+#endif /* __NetBSD__ */
+}
+
 int main(int argc, const char *argv[])
 {
     char        *p;
@@ -43,6 +77,7 @@
         fprintf(stderr, "can not get I/O permissions.\n");
         exit(EXIT_FAILURE);
     }
+    atexit(reset_text_mode);
     ChangeNewConsole();
     TermInit(argc - 1, argv + 1);
     if (ReadConfig(CONFIG_NAME) < 0) {
Index: src/mouse.c
===================================================================
RCS file: /home/toshi/cvs/kon2/src/mouse.c,v
retrieving revision 1.1.1.2
diff -u -r1.1.1.2 mouse.c
--- src/mouse.c  30 Sep 2008 08:09:56 -0000      1.1.1.2
+++ src/mouse.c  23 Oct 2008 09:24:33 -0000
@@ -122,11 +122,11 @@
                 }
 };
 
-static int mouseBaud;
+static int mouseBaud = B1200;
 
 static int      ConfigMouseBaud(const char *config)
 {
-        int baud;
+        int baud = 0;
 
         sscanf(config, "%d", &baud);
 
@@ -149,19 +149,23 @@
         return SUCCESS;
 }
 
-static char *mouseDev;
+static char *mouseDev = NULL;
 
 static int      ConfigMouseDev(const char *config)
 {
-        char name[MAX_COLS];
-        sscanf(config, "%s", name);
+        char *name, *last, *sep = " \t\r\n";
 
-        if (mouseDev) free(mouseDev);
+        name = strtok_r((char *)config, sep, &last);
+        if (name == NULL) {
+                warn("unknown mouse type ignored; assuming no mouse\r\n");
+                return SUCCESS;
+        }
+        SafeFree(mouseDev);
         mouseDev = strdup(name);
         return SUCCESS;
 }
 
-static int      pasteButton;
+static int      pasteButton = MOUSE_RGT;
 
 static int      Config3Buttons(const char *config)
 { 
@@ -172,12 +176,15 @@
 static int      ConfigMouse(const char *config)
 {
         struct mouseconf *p;
-        char name[MAX_COLS];
+        char *name, *last, *sep = " \t\r\n";
 
         mouseType = MOUSE_NONE;
         mInfo.has_mouse = FALSE;
-        strncpy(name, config, MAX_COLS - 1);
-        name[MAX_COLS - 1] = '\0';
+        name = strtok_r((char *)config, sep, &last);
+        if (name == NULL) {
+                warn("unknown mouse type ignored; assuming no mouse\r\n");
+                return SUCCESS;
+        }
         for (p = mice; p->name != NULL; p++) {
                 if (strcasecmp(name, p->name) == 0) {
                         mouseType = p->type;
@@ -254,7 +261,7 @@
     if ((mfd = open(mouseDev, O_RDWR|O_NONBLOCK)) < 0) {
         warn("couldn't open mouse device; mouse disabled\n");
         Perror(mouseDev);
-        free(mouseDev);
+        SafeFree(mouseDev);
         mouseDev = NULL;
         mInfo.has_mouse = FALSE;
         return -1;
Index: src/term.c
===================================================================
RCS file: /home/toshi/cvs/kon2/src/term.c,v
retrieving revision 1.1.1.2
diff -u -r1.1.1.2 term.c
--- src/term.c   30 Sep 2008 08:09:56 -0000      1.1.1.2
+++ src/term.c   23 Oct 2008 09:24:33 -0000
@@ -76,7 +76,7 @@
 
 int     masterPty;                       /* master pseudo-tty file descriptor */
 
-#define MAX_TTYNAME     10
+#define MAX_TTYNAME     20
 
 static int      childPid, sockFd, slavePty;
 static struct termio    oldTio;
@@ -235,13 +235,16 @@
     }
     ConsoleInit(video);
     while (i < argc) {
-        const char      *arg;
+        const char      *arg = argv[i];
 
-        if (argv[i][0] != '-') {
-            warn("bad arg `%s'; assumed `-%s'\r\n", argv[i]);
-            arg = (char *) argv[i];
-        } else
-            arg = (char *) argv[i] + 1;
+        if (*arg != '-')
+            warn("bad arg `%s'; assumed `-%s'\n", arg, arg);
+        else
+            ++arg;
+        if (!strcasecmp(arg, "help")) {
+            message("Usage: kon2 [video] [-option value] ...\n");
+            exit(EXIT_FAILURE);
+        }
         i++;
         if (i >= argc) {
             error("no value for `%s'\r\n", arg);
@@ -258,6 +261,25 @@
 static int      savedArgc;               /* argc of startup time */
 static const char       **savedArgv;     /* argv of startup time */
 
+static int ttyNumber = 0;
+
+int
+set_vt_number(void)
+{
+#if defined(__FreeBSD__)
+    ttyNumber = strtol(ttyname(0) + 9, NULL, 16);
+#elif defined(linux)
+    ttyNumber = atoi(ttyname(0) + 8);
+#endif  /* linux */
+    return ttyNumber;
+}
+
+int
+TermGetNumber(void)
+{
+    return ttyNumber;
+}
+
 /* Do initialization before reading config file */
 void    TermInit(int argc, const char *argv[])
 {
@@ -266,6 +288,7 @@
     init.display = init.utmp = init.socket = init.termio = FALSE;
     /* Initialize subsystems. */
     
+    set_vt_number();
     CapInit();
     ChildInit();
     MouseInit();
@@ -355,8 +378,8 @@
         mouseFd = MouseStart();
     }
 
-    chown("/dev/tty0", getuid(), getgid());
 #if defined(linux)
+    chown("/dev/tty0", getuid(), getgid());
     sockFd = SocketInit(ttyname(0) + 8);
 #elif defined(__FreeBSD__)
     sockFd = SocketInit(ttyname(0) + 9);
@@ -381,6 +404,7 @@
 
         /* Signal Setting */
         signal(SIGCHLD, ExitPty);
+        signal(SIGPIPE, SIG_IGN);
         signal(SIGHUP, ExitTerm);
         signal(SIGTERM, ExitTerm);
         signal(SIGSEGV, ExitTerm);
@@ -458,18 +482,13 @@
         PerrorExit("/dev/console");
     }
 #elif defined(__FreeBSD__)
-#if __FreeBSD_version >= 501100
-    cfd = open("/dev/ttyv0", O_WRONLY);
-    if (cfd < 0 && (cfd = open("/dev/ttyv0", O_RDONLY)) < 0) {
-        PerrorExit("/dev/ttyv0");
-    }
-#else
-    cfd = open("/dev/vga", O_WRONLY);
-    if (cfd < 0 && (cfd = open("/dev/vga", O_RDONLY)) < 0) {
-        PerrorExit("/dev/vga");
+    cfd = open("/dev/vga", O_RDONLY);
+    if (cfd == -1) {
+        cfd = open("/dev/console", O_RDONLY);
+        if (cfd == -1)
+            PerrorExit("/dev/console");
     }
-#endif
-#endif
+#endif  /* __FreeBSD__ */
     ioctl(cfd, VT_ACTIVATE, orgVtNum);
     close(cfd);
 }
@@ -486,28 +505,36 @@
     cfd = open("/dev/console", O_WRONLY);
     if (cfd < 0 && (cfd = open("/dev/console", O_RDONLY)) < 0)
         fatal("can't open /dev/console");
-#elif defined(__FreeBSD__)
-#if __FreeBSD_version >= 501100
-    cfd = open("/dev/ttyv0", O_WRONLY);
-    if (cfd < 0 && (cfd = open("/dev/ttyv0", O_RDONLY)) < 0)
-        fatal("can't open /dev/ttyv0");
-#else
-    cfd = open("/dev/vga", O_WRONLY);
-    if (cfd < 0 && (cfd = open("/dev/vga", O_RDONLY)) < 0)
-        fatal("can't open /dev/vga");
-#endif
-#endif
+
     ioctl(cfd, KDGETMODE, &mode);
     if (mode == KD_TEXT) {
         close(cfd);
         return;
     }
-#if defined(linux)
     ioctl(cfd, VT_GETSTATE, &vts);
     orgVtNum = vts.v_active;
-#endif
-    ioctl(cfd, VT_OPENQRY, &vtNum);
-    if (vtNum < 0)
+
+#elif defined(__FreeBSD__)
+    /* check for syscons mode */
+    if (ioctl(0, KDGETMODE, &mode) == -1)
+        fatal("KON is require the syscons(4)");
+    else if (mode != KD_TEXT)
+        fatal("unknown terminal mode.");
+    else if (strcmp(ttyname(0), "/dev/console") != 0)
+        return;
+
+    cfd = open("/dev/vga", O_RDONLY);
+    if (cfd == -1) {
+        cfd = open("/dev/console", O_RDONLY);
+        if (cfd == -1)
+            fatal("can't open /dev/console");
+    }
+    if (ioctl(cfd, VT_GETINDEX, &orgVtNum) == -1)
+        fatal("ioctl(VT_GETINDEX)");
+#endif /* __FreeBSD__ */
+
+    errno = 0;
+    if (ioctl(cfd, VT_OPENQRY, &vtNum) == -1 || vtNum < 0)
         fatal("can't get free VC");
     parent = getpid();
     if ((child = fork()) == -1)
@@ -515,12 +542,13 @@
     if (child) {
         signal(SIGHUP, ExitPty);
         pause();
+        fatal("internal error.");
     }
     setsid();
 #if defined(linux)
     sprintf(vtty, "/dev/tty%d", vtNum);
 #elif defined(__FreeBSD__)
-    sprintf(vtty, "/dev/ttyv%d", vtNum);
+    sprintf(vtty, "/dev/ttyv%d", vtNum - 1);
 #endif
     if ((vfd = open(vtty, O_RDWR)) < 0)
         fatal("can't open %s", vtty);
@@ -531,5 +559,6 @@
     dup2(vfd, 0);
     dup2(vfd, 1);
     dup2(vfd, 2);
+    close(vfd);
     kill(parent, SIGHUP);
 }
Index: src/vc.c
===================================================================
RCS file: /home/toshi/cvs/kon2/src/vc.c,v
retrieving revision 1.1.1.2
diff -u -r1.1.1.2 vc.c
--- src/vc.c     30 Sep 2008 08:09:56 -0000      1.1.1.2
+++ src/vc.c     23 Oct 2008 09:24:33 -0000
@@ -792,22 +792,21 @@
     return SUCCESS;
 }
 
-static  char    *videoName;
+static  char    *videoName = NULL;
 
 static int      ConfigDisplay(const char *config)
 {
     struct videoconf *v;
-    char name[MAX_COLS];
-    
-    sscanf(config, "%s", name);
+    char *name, *last, *sep = " \t\r\n";
+
+    name = strtok_r((char *)config, sep, &last);
+    if (name == NULL) {
+        error("invalid entry for %s\r\n", videoName);
+        return FAILURE;
+    } 
     for (v = videos; v->name != NULL; v++) {
         if (strcasecmp(name, v->name) == 0) {
-            config = strchr(config, '\n');
-            if (config == NULL) {
-                error("invalid entry for %s\r\n", videoName);
-                return FAILURE;
-            }
-            if (v->set(&vInfo, config) == FAILURE)
+            if (v->set(&vInfo, last) == FAILURE)
                 return FAILURE;
             message("video type `%s' selected\r\n", name);
             if (vInfo.has_hard_scroll) {
@@ -873,6 +872,7 @@
 
 void    ConsoleInit(const char *video)
 {
+    SafeFree(videoName);
     videoName = strdup(video);
     DefineCap(videoName, ConfigDisplay, NULL);
     DefineCap("BeepCounter", ConfigBeep, "5");
@@ -900,12 +900,12 @@
 {
     scrollLine = textHead = 0;
     vInfo.detatch();
-    SafeFree((void **)&textBuff);
-    SafeFree((void **)&attrBuff);
-    SafeFree((void **)&flagBuff);
+    SafeFree(textBuff);
+    SafeFree(attrBuff);
+    SafeFree(flagBuff);
 #ifdef linux
     ioperm(COUNTER_ADDR, 1, FALSE);
 #endif
     
-    SafeFree((void **)&videoName);
+    SafeFree(videoName);
 }
Index: src/vt.c
===================================================================
RCS file: /home/toshi/cvs/kon2/src/vt.c,v
retrieving revision 1.1.1.2
diff -u -r1.1.1.2 vt.c
--- src/vt.c     30 Sep 2008 08:09:56 -0000      1.1.1.2
+++ src/vt.c     23 Oct 2008 09:24:33 -0000
@@ -107,7 +107,7 @@
         con.bcol = saveAttr->bcol;
         tmp = saveAttr;
         saveAttr = tmp->prev;
-        free(tmp);
+        SafeFree(tmp);
     }
 }
 
@@ -606,20 +606,23 @@
 
 static int      ConfigCoding(const char *confstr)
 {
-    char reg[3][MAX_COLS];
+    char *name, *last, *sep = " \t\r\n";
     int n, i;
 
-    *reg[0] = *reg[1] = *reg[2] = '\0';
-    sscanf(confstr, "%s %s %s", reg[0], reg[1], reg[2]);
-    for (i = 0; i < 3 && *reg[i]; i ++) {
-        n = (int)CodingByRegistry(reg[i]);
+    name = strtok_r((char *)confstr, sep, &last);
+    for (i = 0; i < 3; i++) {
+        if (name == NULL) {
+            error("invalid coding system\n");
+            return FAILURE;
+        }
+        n = (int)CodingByRegistry(name);
         if (n < 0) {
-            if (!strcasecmp(reg[i], "EUC"))
+            if (!strcasecmp(name, "EUC"))
                 lInfo.sc = CODE_EUC;
-            else if (!strcasecmp(reg[i], "SJIS"))
+            else if (!strcasecmp(name, "SJIS"))
                 lInfo.sc = CODE_SJIS;
 /*
-            else if (!strcasecmp(reg[i], "BIG5"))
+            else if (!strcasecmp(name, "BIG5"))
                 lInfo.sc = CODE_BIG5;
 */
             else
@@ -633,6 +636,7 @@
 fprintf(fp,"[<%s> %d %d %d %d]\n", reg[i], n, lInfo.sb, lInfo.db, lInfo.sc);
 fclose(fp);}
 #endif
+        name = strtok_r(NULL, sep, &last);
     }
     return SUCCESS;
 }
Index: src/display/j31sx.c
===================================================================
RCS file: /home/toshi/cvs/kon2/src/display/j31sx.c,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 j31sx.c
--- src/display/j31sx.c  30 Sep 2008 08:02:59 -0000      1.1.1.1
+++ src/display/j31sx.c  23 Oct 2008 09:24:33 -0000
@@ -49,7 +49,6 @@
 #include        <sys/mman.h>
 #include        <linux/mm.h>
 #include        <sys/kd.h>
-#undef free
 #include        <stdlib.h>
 
 #include        <mem.h>
@@ -365,8 +364,6 @@
         ioperm(CGA_COLOR, 1, 0);
 
         munmap(gram, GRAPH_SIZE);
-
-        SafeFree((void **)&gram);
 }
 
 static struct videoInfo J31SXInfo =
Index: src/display/s3.c
===================================================================
RCS file: /home/toshi/cvs/kon2/src/display/s3.c,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 s3.c
--- src/display/s3.c     30 Sep 2008 08:02:59 -0000      1.1.1.1
+++ src/display/s3.c     23 Oct 2008 09:24:33 -0000
@@ -43,7 +43,6 @@
 #include        <sys/mman.h>
 #include        <linux/mm.h>
 #include        <sys/kd.h>
-#undef free
 #include        <stdlib.h>
 
 #include        <mem.h>
Index: src/display/svga.c
===================================================================
RCS file: /home/toshi/cvs/kon2/src/display/svga.c,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 svga.c
--- src/display/svga.c   30 Sep 2008 08:02:59 -0000      1.1.1.1
+++ src/display/svga.c   23 Oct 2008 09:24:33 -0000
@@ -45,7 +45,6 @@
 /* #include     <linux/mm.h> */
 #include        <sys/kd.h>
 #endif
-#undef free
 #include        <stdlib.h>
 
 #include        <mem.h>
Index: src/display/vga.c
===================================================================
RCS file: /home/toshi/cvs/kon2/src/display/vga.c,v
retrieving revision 1.1.1.2
diff -u -r1.1.1.2 vga.c
--- src/display/vga.c    30 Sep 2008 08:09:56 -0000      1.1.1.2
+++ src/display/vga.c    23 Oct 2008 09:24:33 -0000
@@ -56,7 +56,6 @@
 #endif
 vm_size_t page_size;
 #endif
-#undef free
 #include        <stdlib.h>
 
 #include        <mem.h>
@@ -691,10 +690,9 @@
 
     munmap(gramMem, dInfo.gsize);
     
-    SafeFree((void **)&gramMem);
-    SafeFree((void **)&fontBuff1);
+    SafeFree(fontBuff1);
     if (savePlane3)
-        SafeFree((void **)&fontBuff2);
+        SafeFree(fontBuff2);
 }
 
 /* Configure */




Unformatted:
 
Submit Followup | Raw PR | Find another PR