$NetBSD: patch-ae,v 1.5 2005/12/10 22:10:47 christos Exp $

--- fep_edit.c.orig	1995-01-20 03:16:37.000000000 -0500
+++ fep_edit.c	2005-12-10 16:59:27.000000000 -0500
@@ -3,13 +3,25 @@
 #ifndef lint
 static char rcsid[]=
 "$Id: patch-ae,v 1.5 2005/12/10 22:10:47 christos Exp $ (SRA)";
-#endif lint
+#endif /* lint */
 
 #include <stdio.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/ioctl.h>
+#include <inttypes.h>
+#ifdef TERMIOS
+#include <termios.h>
+#ifdef __linux__
+#ifndef _POSIX_VDISABLE
+#define _POSIX_VDISABLE '\0'
+#endif
+#endif
+#else
 #include <sgtty.h>
+#endif
 #include <sys/time.h>
+#include <stdlib.h>
 #include <ctype.h>
 #include <sys/dir.h>
 #include <sys/file.h>
@@ -25,7 +37,7 @@
 EDITMODE	editmode = NOTYET;	/* edtimode EMACS, VI */
 EDITSTATUS	editstatus = NOTEDITING;/* EDITING, NOTEDITING */
 
-int	maxline = MAXCOMLEN;		/* maximum length of command line */
+int	maxline = MAXCMDLEN;		/* maximum length of command line */
 int	NeedNewLine;			/* add new line flag */
 int	NeedSave;			/* need to save to history */
 int	Transparency = OFF;		/* transparent flag */
@@ -86,7 +98,7 @@
 	/* ^X-^V	*/	{"\\^X\\^V",	view_buffer},
 	/* ^X-^K	*/	{"\\^X\\^K",	kill_to_top_of_line},
 	/* ^X-^L	*/	{"\\^X\\^L",	fep_repaint},
-	/* ^X-^C	*/	{"\\^X\\^C",	terminate},
+	/* ^X-^C	*/	{"\\^X\\^C",	(FUNC)terminate},
 	/* ^X-^D	*/	{"\\^X\\^D",	send_eof},
 	/* ^X-(		*/	{"\\^X(",	fep_start_script},
 	/* ^X-)		*/	{"\\^X)",	fep_end_script},
@@ -99,9 +111,9 @@
 init_bind_table ()
 {
 
-    curFuncTab = (FUNC *) calloc (sizeof (FUNC), 256);
-    altFuncTab = (FUNC *) calloc (sizeof (FUNC), 256);
-    if (curFuncTab == 0 || altFuncTab == 0) {
+    curFuncTab = calloc (sizeof (FUNC), 256);
+    altFuncTab = calloc (sizeof (FUNC), 256);
+    if (curFuncTab == NULL || altFuncTab == NULL) {
 	printf ("Can't allocate space for function table\n");
 	exit (1);
     }
@@ -167,9 +179,9 @@
     /*
      * Initialize command line buffer
      */
-    CommandLine = (CHAR *) calloc (maxline, 1);
-    KillBuffer = (CHAR *) calloc (maxline, 1);
-    if (CommandLine == 0 || KillBuffer == 0) {
+    CommandLine = calloc (maxline, 1);
+    KillBuffer = calloc (maxline, 1);
+    if (CommandLine == NULL || KillBuffer == NULL) {
 	perror ("Edit line buffer");
 	exit (1);
     }
@@ -192,6 +204,30 @@
 
 #define import(table,key,fn) if((int)key>0)table[(int)key]=fn
 
+#ifdef TERMIOS
+    /* Now, using cbreak mode
+    import (cft, initial_ttymode.c_cc[VSTART], ignore);
+    import (cft, initial_ttymode.c_cc[VSTOP], ignore);
+    */
+    import (cft, initial_ttymode.c_cc[VINTR], insert_and_flush);
+    import (cft, initial_ttymode.c_cc[VQUIT], insert_and_flush);
+    /* Now, EOF will be sent on empty line.
+    import (cft, initial_ttymode.c_cc[VEOF], send_eof);
+    */
+#ifdef VSWTC
+    import (cft, initial_ttymode.c_cc[VSWTC], insert_and_flush);
+#endif
+    import (cft, initial_ttymode.c_cc[VSUSP], insert_and_flush);
+    /* ^Y is used for yank-from-kill-buffer
+    import (cft, initial_ttymode.c_cc[VDSUSP], self_insert);
+    */
+    import (cft, initial_ttymode.c_cc[VREPRINT], reprint);
+    import (cft, initial_ttymode.c_cc[VDISCARD], self_insert);
+    import (cft, initial_ttymode.c_cc[VWERASE], delete_previous_word);
+    import (cft, initial_ttymode.c_cc[VLNEXT], literal_next);
+    import (cft, initial_ttymode.c_cc[VERASE], delete_previous_character);
+    import (cft, initial_ttymode.c_cc[VKILL], delete_line);
+#else
     /* Now, using cbreak mode
     import (cft, tchars_buf.t_startc, ignore);
     import (cft, tchars_buf.t_stopc, ignore);
@@ -212,6 +248,7 @@
     import (cft, ltchars_buf.t_lnextc, literal_next);
     import (cft, initial_ttymode.sg_erase, delete_previous_character);
     import (cft, initial_ttymode.sg_kill, delete_line);
+#endif
 
 #undef import
 
@@ -257,7 +294,7 @@
 	 */
 	swallow_output();
 
-	if (fgets (CommandLine, MAXCOMLEN, redirect_fp)) {
+	if (fgets (CommandLine, MAXCMDLEN, redirect_fp)) {
 	    ++redirect_line;
 	    execute_command = CommandLine;
 	    goto RETURN;	    
@@ -301,7 +338,11 @@
 	 * call send_eof
 	 */
 	if (
+#ifdef TERMIOS
+	    c == initial_ttymode.c_cc[VEOF]
+#else
 	    c == tchars_buf.t_eofc
+#endif
 	    && curFuncTab[c] != send_eof
 	    && ! look_var ("ignore-eof")
 	    && CommandLine [0] == '\0'
@@ -422,11 +463,11 @@
     register int i, nbyte = 1, currentNull;
 #ifdef KANJI
     CHAR byte2;
-#endif KANJI
+#endif /* KANJI */
 
     currentNull = strlen (CommandLine);
 
-    if (currentNull >= maxline) {
+    if (currentNull + 1 >= maxline) {
         errorBell();
 	return (0);
     }
@@ -441,7 +482,7 @@
 	nbyte = 2;
     }
     else
-#endif KANJI
+#endif /* KANJI */
 	putChar (c);
     reverse_strcpy (
 	&CommandLine[CurrentPosition] + nbyte,
@@ -453,7 +494,7 @@
     if (nbyte > 1) {
 	CommandLine[CurrentPosition - 1] = byte2;
     }
-#endif KANJI
+#endif /* KANJI */
     printS (&CommandLine [CurrentPosition]);
 
     if (CommandLine[CurrentPosition] != '\0') {
@@ -557,7 +598,7 @@
 #ifdef KANJI
 	    && !(CurrentPosition + 1 == position
 		    && iskanji (CommandLine[CurrentPosition]))
-#endif KANJI
+#endif /* KANJI */
 	)
 	    (void) forward_n_character (1);
 }
@@ -581,10 +622,10 @@
 #ifdef KANJI
 #define INC(i) if(iskanji(CommandLine[i])) i+=2; else i++;
 #define DEC(i) if(i>=2 && iskanji_in_string(CommandLine, i-2)) i-=2; else i--;
-#else KANJI
+#else /* KANJI */
 #define INC(i) i++
 #define DEC(i) i--
-#endif KANJI
+#endif /* KANJI */
 
 /*
  * Move cursor to end of line
@@ -642,7 +683,7 @@
 	    i--;
 	}
 	else
-#endif KANJI
+#endif /* KANJI */
 	putchar (BS);
 	i--;
     }
@@ -697,7 +738,7 @@
 	    nchars++;
 	}
     }
-#else KANJI
+#else /* KANJI */
     while (n--) {
 	i--, nchars++;
 	while (i > 0 && !iswordchar (CommandLine [i])) {
@@ -707,7 +748,7 @@
 	    i--, nchars++;
 	}
     }
-#endif KANJI
+#endif /* KANJI */
     return (backward_n_character (nchars));
 }
 
@@ -758,7 +799,7 @@
 	    nchars++;
 	}
     }
-#else KANJI
+#else /* KANJI */
     while (n--) {
 	i--, nchars++;
 	while (i > 0 && !isWordchar (CommandLine [i]))
@@ -766,7 +807,7 @@
 	while (i > 0 && isWordchar (CommandLine [i - 1]))
 	    i--, nchars++;
     }
-#endif KANJI
+#endif /* KANJI */
     return (backward_n_character (nchars));
 }
 
@@ -804,7 +845,7 @@
 	(void) putchar (CommandLine[i++]);
     }
     else
-#endif KANJI
+#endif /* KANJI */
     if (isctlchar(CommandLine[i])) {
 	(void) putchar (unctl (CommandLine [i]));
 	i++;
@@ -819,7 +860,7 @@
 	    (void) putchar (CommandLine[i++]);
 	}
 	else
-#endif KANJI
+#endif /* KANJI */
 	putChar (CommandLine [i++]);
     }
 
@@ -1000,9 +1041,9 @@
 	    i -= 2, nbyte += 2;
 	else
 	    i -= 1, nbyte += 1;
-#else KANJI
+#else /* KANJI */
     nbyte = n;
-#endif KANJI
+#endif /* KANJI */
 
     deleteArea = howlong (&CommandLine[CurrentPosition - nbyte], nbyte);
     restArea = howlong (&CommandLine[CurrentPosition], 0);
@@ -1075,7 +1116,7 @@
 	    nchars++;
 	}
     }
-#else KANJI
+#else /* KANJI */
     while (n--) {
 	i--, nchars++;
 	while (i > 0 && !iswordchar (CommandLine [i]))
@@ -1083,7 +1124,7 @@
 	while (i > 0 && iswordchar (CommandLine [i - 1]))
 	    i--, nchars++;
     }
-#endif KANJI
+#endif /* KANJI */
 
     return (delete_previous_n_character (nchars));
 }
@@ -1136,7 +1177,7 @@
 	    nchars++;
 	}
     }
-#else KANJI
+#else /* KANJI */
     while (n--) {
 	i--, nchars++;
 	while (i > 0 && !isWordchar (CommandLine [i]))
@@ -1144,7 +1185,7 @@
 	while (i > 0 && isWordchar (CommandLine [i - 1]))
 	    i--, nchars++;
     }
-#endif KANJI
+#endif /* KANJI */
 
     return (delete_previous_n_character (nchars));
 }
@@ -1183,9 +1224,9 @@
 	    else
 		cp++, nbyte++;
     }
-#else KANJI
+#else /* KANJI */
     nbyte = n;
-#endif KANJI
+#endif /* KANJI */
 
     deleteArea = howlong (&CommandLine[CurrentPosition], nbyte);
     restArea = howlong (&CommandLine[CurrentPosition + nbyte], 0);
@@ -1481,7 +1522,11 @@
  */
 send_eof()
 {
+#ifdef TERMIOS
+    char c = initial_ttymode.c_cc[VEOF];
+#else
     char c = tchars_buf.t_eofc;
+#endif
 
     (void) self_insert (c);
     if (isctlchar (c))
@@ -1737,11 +1782,11 @@
 	    char *fcp;
 
 # ifdef ALLOCA
-  	    fcp = (char *) alloca (strlen (dp->d_name) + 1);
-# else ALLOCA
-  	    fcp = (char *) malloc (strlen (dp->d_name) + 1);
-# endif ALLOCA
-	    if (fcp == 0) {
+  	    fcp = alloca (strlen (dp->d_name) + 1);
+# else /* ALLOCA */
+  	    fcp = malloc (strlen (dp->d_name) + 1);
+# endif /* ALLOCA */
+	    if (fcp == NULL) {
 		fputs ("\r\n", stdout);
 		perror ("alloca:");
 		reprint ();
@@ -1754,7 +1799,7 @@
     fileList [i] = (char *) 0;
 
     if (*start_expand == '~' && look_var ("expand-tilde")) {
-	char *buf [256], *p;
+	char buf [256], *p;
 
 	strcpy (buf, start_expand);
 	p = x_dirname (buf);
@@ -1801,7 +1846,7 @@
 # ifndef ALLOCA
     for (i = 0; fileList [i]; i++)
 	free (fileList [i]);
-# endif ALLOCA
+# endif /* ALLOCA */
 
     closedir(dirp);
     return (0);
@@ -1928,11 +1973,11 @@
 	    continue;
 	if (prefix (prefixstring, dp->d_name)) {
 # ifdef ALLOCA
-  	    fcp = (char *) alloca (strlen (dp->d_name) + 1);
-# else ALLOCA
-  	    fcp = (char *) malloc (strlen (dp->d_name) + 1);
-# endif ALLOCA
-	    if (fcp == 0) {
+  	    fcp = alloca (strlen (dp->d_name) + 1);
+# else /* ALLOCA */
+  	    fcp = malloc (strlen (dp->d_name) + 1);
+# endif /* ALLOCA */
+	    if (fcp == NULL) {
 		fputs ("\r\n", stdout);
 		perror ("alloca:");
 		reprint ();
@@ -1975,7 +2020,7 @@
 # ifndef ALLOCA
     for (i = 0; fileList [i]; i++)
 	free (fileList [i]);
-# endif ALLOCA
+# endif /* ALLOCA */
     return;
 }
 
@@ -2022,7 +2067,7 @@
 	fputs ("\n", stdout);
     }
 }
-#endif RINFO
+#endif /* RINFO */
 
 bind_key (ft, func, s, dfunc)
     FUNC ft[];		/* Function table */
@@ -2032,7 +2077,7 @@
 {
     char tmps[16];
 
-    if (s[0] == '\\' && s[1] == '^' && s[2] != NULL) {
+    if (s[0] == '\\' && s[1] == '^' && s[2] != '\0') {
 	tmps[0] = toctrl (s[2]);
 	strcpy (&tmps[1], &s[3]);
 	s = tmps;
@@ -2042,7 +2087,7 @@
      * If the string contain only one character, put the function to
      * appropriate position in the table.
      */
-    if (*(s+1) == NULL) {
+    if (*(s+1) == '\0') {
 	if (isIndirect (ft[(int) *s]))
 	    free (maskIndirect (ft[(int) *s]));
 
@@ -2059,11 +2104,11 @@
 	if (! (isIndirect (ft[(int) *s]))) {
 	    register int i;
 
-	    nft = (FUNC *) calloc (sizeof (FUNC), 256);
+	    nft = calloc (sizeof (FUNC), 256);
 	    /*
 	     * If failed in allocating, return 0.
 	     */
-	    if (nft == 0)
+	    if (nft == NULL)
 		return (0);
 
 	    /*
