diff -ruN expect5.45.orig/Dbg.c expect5.45/Dbg.c
--- expect5.45.orig/Dbg.c	2010-08-31 13:30:29.000000000 -0500
+++ expect5.45/Dbg.c	2020-11-16 20:32:15.000000000 -0600
@@ -177,7 +177,7 @@
 static int
 breakpoint_test(interp,cmd,bp)
 Tcl_Interp *interp;
-char *cmd;		/* command about to be executed */
+CONST char *cmd;		/* command about to be executed */
 struct breakpoint *bp;	/* breakpoint to test */
 {
     if (bp->re) {
@@ -472,7 +472,7 @@
 		PrintStackBelow(interp,curf->callerVarPtr,viewf);
 		print(interp,"%c%d: %s\n",ptr,curf->level,
 #if TCL_MAJOR_VERSION >= 8
-	      print_objv(interp,curf->objc,curf->objv)
+	      print_objv(interp,curf->objc,(struct Tcl_Obj **)curf->objv)
 #else
 	      print_argv(interp,curf->argc,curf->argv)
 #endif
@@ -491,7 +491,7 @@
 char *level;
 {
 	PrintStackBelow(interp,curf,viewf);
-    print(interp," %s: %s\n",level,print_objv(interp,objc,objv));
+    print(interp," %s: %s\n",level,print_objv(interp,objc,(struct Tcl_Obj **)objv));
 }
 
 /* return 0 if goal matches current frame or goal can't be found */
@@ -663,7 +663,7 @@
 start_interact:
 	if (print_command_first_time) {
 		print(interp,"%s: %s\n",
-				level_text,print_argv(interp,1,&command));
+				level_text,print_argv(interp,1,(char **)&command));
 		print_command_first_time = FALSE;
 	}
 	/* since user is typing a command, don't interrupt it immediately */
diff -ruN expect5.45.orig/configure expect5.45/configure
--- expect5.45.orig/configure	2010-09-16 15:46:47.000000000 -0500
+++ expect5.45/configure	2020-11-16 20:46:57.000000000 -0600
@@ -6288,7 +6288,7 @@
     echo "$as_me:$LINENO: checking for Tcl private include files" >&5
 echo $ECHO_N "checking for Tcl private include files... $ECHO_C" >&6
 
-    TCL_SRC_DIR_NATIVE=`${CYGPATH} ${TCL_SRC_DIR}`
+    TCL_SRC_DIR_NATIVE=`${CYGPATH} ${TCL_SRC_DIR}|sed s:/private::g`
     TCL_TOP_DIR_NATIVE=\"${TCL_SRC_DIR_NATIVE}\"
 
     # Check to see if tcl<Plat>Port.h isn't already with the public headers
@@ -11928,6 +11928,7 @@
 cat >>conftest.$ac_ext <<_ACEOF
 /* end confdefs.h.  */
 #include <time.h>
+#include <stdlib.h>
 int
 main ()
 {
@@ -11990,6 +11991,7 @@
 cat >>conftest.$ac_ext <<_ACEOF
 /* end confdefs.h.  */
 #include <time.h>
+#include <stdlib.h>
 int
 main ()
 {
@@ -14204,6 +14206,7 @@
 cat >>conftest.$ac_ext <<_ACEOF
 /* end confdefs.h.  */
 
+#include <string.h>
 int
 main ()
 {
@@ -14423,8 +14426,7 @@
 /* end confdefs.h.  */
 
 #include <signal.h>
-#define RETSIGTYPE $retsigtype
-
+#include <stdlib.h>
 int signal_rearms = 0;
 
 RETSIGTYPE
@@ -15060,6 +15062,7 @@
 /* end confdefs.h.  */
 
 #include <sgtty.h>
+#include <stdlib.h>
 main()
 {
   struct sgttyb tmp;
@@ -15180,6 +15183,7 @@
 #  include <inttypes.h>
 #  endif
 #  include <termios.h>
+#include <stdlib.h>
   main()
   {
     struct termios tmp;
@@ -15899,6 +15903,7 @@
 cat >>conftest.$ac_ext <<_ACEOF
 /* end confdefs.h.  */
 
+#include <stdlib.h>
 extern char *tzname[2];
 extern int daylight;
 main()
@@ -16026,7 +16031,7 @@
     else
         EXP_LIB_FLAG="-lexpect`echo ${EXP_LIB_VERSION} | tr -d .`"
     fi
-    EXP_BUILD_LIB_SPEC="-L`pwd` ${EXP_LIB_FLAG}"
+    EXP_BUILD_LIB_SPEC=`echo ${EXP_LIB_FLAG} | sed "s/-l/lib/; s/$/${SHLIB_SUFFIX}/"`
     EXP_LIB_SPEC="-L${libdir} ${EXP_LIB_FLAG}"
 fi
 
diff -ruN expect5.45.orig/exp_chan.c expect5.45/exp_chan.c
--- expect5.45.orig/exp_chan.c	2010-06-30 19:53:49.000000000 -0500
+++ expect5.45/exp_chan.c	2020-11-16 20:32:15.000000000 -0600
@@ -34,7 +34,9 @@
 #include "exp_rename.h"
 #include "exp_prog.h"
 #include "exp_command.h"
+#include "exp_event.h"
 #include "exp_log.h"
+#include "exp_event.h"
 #include "tcldbg.h" /* Dbg_StdinMode */
 
 extern int		expSetBlockModeProc _ANSI_ARGS_((int fd, int mode));
@@ -565,7 +567,7 @@
 	if (esPtr->user_waited) continue;	/* one wait only! */
 	if (esPtr->sys_waited) break;
       restart:
-	result = waitpid(esPtr->pid,&esPtr->wait,WNOHANG);
+	result = waitpid(esPtr->pid,(int *)&esPtr->wait,WNOHANG);
 	if (result == esPtr->pid) break;
 	if (result == 0) continue;	/* busy, try next */
 	if (result == -1) {
@@ -584,7 +586,7 @@
     /* should really be recoded using the common wait code in command.c */
     WAIT_STATUS_TYPE status;
 
-    pid = wait(&status);
+    pid = wait((int *)&status);
     for (esPtr = tsdPtr->firstExpPtr;esPtr;esPtr = esPtr->nextPtr) {
 	if (esPtr->pid == pid) {
 	    esPtr->sys_waited = TRUE;
diff -ruN expect5.45.orig/exp_clib.c expect5.45/exp_clib.c
--- expect5.45.orig/exp_clib.c	2010-08-31 17:20:27.000000000 -0500
+++ expect5.45/exp_clib.c	2020-11-16 20:32:15.000000000 -0600
@@ -7,7 +7,12 @@
 would appreciate credit if this program or parts of it are used.
 */
 
+#include <unistd.h>
+void exp_init_tty();
+
 #include "expect_cf.h"
+#include "exp_command.h"
+#include <unistd.h>
 #include <stdio.h>
 #include <setjmp.h>
 #ifdef HAVE_INTTYPES_H
@@ -1476,8 +1481,8 @@
      char *str;
 {
   if (exp_is_debugging) {
-    fprintf(stderr,str);
-    if (exp_logfile) fprintf(exp_logfile,str);
+    fprintf(stderr,"%s",str);
+    if (exp_logfile) fprintf(exp_logfile,"%s",str);
   }
 }
 
diff -ruN expect5.45.orig/exp_command.c expect5.45/exp_command.c
--- expect5.45.orig/exp_command.c	2010-06-30 19:53:49.000000000 -0500
+++ expect5.45/exp_command.c	2020-11-16 20:32:15.000000000 -0600
@@ -315,7 +315,15 @@
 	fcntl(x,F_DUPFD,esPtr->fdin);
 	close(x);
     }
-    expCloseOnExec(esPtr->fdin);
+    if (esPtr->fdout != EXP_NOFD &&
+	esPtr->fdout != esPtr->fdin) {
+	fcntl(esPtr->fdin,F_DUPFD,esPtr->fdout);
+    }
+    if (esPtr->fdin > 2)
+	expCloseOnExec(esPtr->fdin);
+    if (esPtr->fdout != EXP_NOFD &&
+	esPtr->fdout > 2)
+	expCloseOnExec(esPtr->fdout);
     esPtr->fdBusy = TRUE;
 }
 
@@ -1161,7 +1169,7 @@
     /* if stty finds dev(stderr) != dev(stdout) */
 
     /* save error fd while we're setting up new one */
-    errorfd = fcntl(2,F_DUPFD,3);
+    errorfd = fcntl(2,F_DUPFD_CLOEXEC,3);
     /* and here is the macro to restore it */
 #define restore_error_fd {close(2);fcntl(errorfd,F_DUPFD,2);}
 
diff -ruN expect5.45.orig/exp_inter.c expect5.45/exp_inter.c
--- expect5.45.orig/exp_inter.c	2010-08-31 17:20:27.000000000 -0500
+++ expect5.45/exp_inter.c	2020-11-16 20:32:15.000000000 -0600
@@ -404,8 +404,8 @@
     int matchBytes)
 {
     int seenBytes;	/* either printed or echoed */
-    int echoBytes;
-    int offsetBytes;
+    int echoBytes = 0;
+    int offsetBytes = 0;
 
     /* write is unlikely to fail, since we just read from same descriptor */
     seenBytes = esPtr->printed + esPtr->echoed;
@@ -437,7 +437,7 @@
     int key)
 {
     Tcl_UniChar *eobOld;  /* old end of buffer */
-    int cc;
+    int cc = 0;
     int numchars;
     Tcl_UniChar *str;
 
diff -ruN expect5.45.orig/exp_log.c expect5.45/exp_log.c
--- expect5.45.orig/exp_log.c	2010-06-30 19:53:49.000000000 -0500
+++ expect5.45/exp_log.c	2020-11-16 20:32:15.000000000 -0600
@@ -85,7 +85,7 @@
     Tcl_UniChar *buf;
     int lenChars;
 {
-    int wc;
+    int wc = 0;
     ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
 
     if (esPtr->valid)
@@ -176,7 +176,7 @@
 
     if ((!tsdPtr->logUser) && (!force_stdout) && (!tsdPtr->logAll)) return;
 
-    (void) vsprintf(bigbuf,fmt,args);
+    (void) vsnprintf(bigbuf,sizeof(bigbuf),fmt,args);
     expDiagWriteBytes(bigbuf,-1);
     if (tsdPtr->logAll || (LOGUSER && tsdPtr->logChannel)) Tcl_WriteChars(tsdPtr->logChannel,bigbuf,-1);
     if (LOGUSER) fprintf(stdout,"%s",bigbuf);
@@ -222,7 +222,7 @@
     va_list args;
 
     fmt = TCL_VARARGS_START(char *,arg1,args);
-    (void) vsprintf(bigbuf,fmt,args);
+    (void) vsnprintf(bigbuf,sizeof(bigbuf),fmt,args);
 
     expDiagWriteChars(bigbuf,-1);
     fprintf(stderr,"%s",bigbuf);
@@ -264,7 +264,7 @@
 
     fmt = TCL_VARARGS_START(char *,arg1,args);
 
-    (void) vsprintf(bigbuf,fmt,args);
+    (void) vsnprintf(bigbuf,sizeof(bigbuf),fmt,args);
 
     expDiagWriteBytes(bigbuf,-1);
     if (tsdPtr->diagToStderr) {
@@ -307,7 +307,7 @@
   int len, rc;
 
   fmt = TCL_VARARGS_START(char *,arg1,args);
-  len = vsprintf(bigbuf,arg1,args);
+  len = vsnprintf(bigbuf,sizeof(bigbuf),arg1,args);
  retry:
   rc = write(2,bigbuf,len);
   if ((rc == -1) && (errno == EAGAIN)) goto retry;
diff -ruN expect5.45.orig/exp_trap.c expect5.45/exp_trap.c
--- expect5.45.orig/exp_trap.c	2010-08-27 16:51:53.000000000 -0500
+++ expect5.45/exp_trap.c	2020-11-16 20:32:15.000000000 -0600
@@ -264,6 +264,18 @@
 #if defined(SIGSTOP)
 	traps[SIGSTOP].reserved = TRUE;
 #endif
+#if defined(SIGSEGV)
+	traps[SIGSEGV].reserved = TRUE;
+#endif
+#if defined(SIGBUS)
+	traps[SIGBUS].reserved = TRUE;
+#endif
+#if defined(SIGILL)
+	traps[SIGILL].reserved = TRUE;
+#endif
+#if defined(SIGFPE)
+	traps[SIGFPE].reserved = TRUE;
+#endif
 
 	async_handler = Tcl_AsyncCreate(tophalf,(ClientData)0);
 
diff -ruN expect5.45.orig/exp_tty.h expect5.45/exp_tty.h
--- expect5.45.orig/exp_tty.h	2008-04-03 14:19:41.000000000 -0500
+++ expect5.45/exp_tty.h	2020-11-16 20:32:15.000000000 -0600
@@ -18,9 +18,11 @@
 void exp_tty_raw(int set);
 void exp_tty_echo(int set);
 void exp_tty_break(Tcl_Interp *interp, int fd);
+int exp_tty_cooked_echo(Tcl_Interp *interp, exp_tty *tty_old, int *was_raw, int *was_echo);
 int exp_tty_raw_noecho(Tcl_Interp *interp, exp_tty *tty_old, int *was_raw, int *was_echo);
 int exp_israw(void);
 int exp_isecho(void);
+EXTERN int exp_tty_cooked_echo _ANSI_ARGS_((Tcl_Interp *interp, exp_tty *tty_old, int *was_raw, int *was_echo));
 
 void exp_tty_set(Tcl_Interp *interp, exp_tty *tty, int raw, int echo);
 int exp_tty_set_simple(exp_tty *tty);
diff -ruN expect5.45.orig/exp_win.c expect5.45/exp_win.c
--- expect5.45.orig/exp_win.c	2010-06-30 19:53:49.000000000 -0500
+++ expect5.45/exp_win.c	2020-11-16 20:32:15.000000000 -0600
@@ -36,12 +36,14 @@
 #  include <sys/ioctl.h>
 #endif
 
+#include <sys/ioctl.h>
+
 /* Sigh.  On AIX 2.3, termios.h exists but does not define TIOCGWINSZ */
 /* Instead, it has to come from ioctl.h.  However, As I said above, this */
 /* can't be cavalierly included on all machines, even when it exists. */
-#if defined(HAVE_TERMIOS) && !defined(HAVE_TIOCGWINSZ_IN_TERMIOS_H)
+/*  #if defined(HAVE_TERMIOS) && !defined(HAVE_TIOCGWINSZ_IN_TERMIOS_H) */
 #  include <sys/ioctl.h>
-#endif
+/*  #endif
 
 /* SCO defines window size structure in PTEM and TIOCGWINSZ in termio.h */
 /* Sigh... */
@@ -87,6 +89,7 @@
 #if defined(TIOCSSIZE) && !defined(TIOCSWINSZ)
 	ioctl(fd,TIOCSSIZE,&winsize);
 #endif
+return 0;
 }
 
 int exp_window_size_get(fd)
@@ -102,6 +105,7 @@
 	winsize.rows = 0;
 	winsize.columns = 0;
 #endif
+return 0;
 }
 
 void
@@ -151,6 +155,7 @@
 #if defined(TIOCSSIZE) && !defined(TIOCSWINSZ)
 			ioctl(fd,TIOCSSIZE,&win2size);
 #endif
+return 0;
 }
 
 int exp_win2_size_get(fd)
@@ -162,6 +167,7 @@
 #if defined(TIOCGSIZE) && !defined(TIOCGWINSZ)
 	ioctl(fd,TIOCGSIZE,&win2size);
 #endif
+return 0;
 }
 
 void
diff -ruN expect5.45.orig/expect.c expect5.45/expect.c
--- expect5.45.orig/expect.c	2010-10-26 17:09:36.000000000 -0500
+++ expect5.45/expect.c	2020-11-16 20:32:15.000000000 -0600
@@ -185,9 +185,9 @@
 {
     if (ec->i_list->duration == EXP_PERMANENT) {
 	if (ec->pat)  { Tcl_DecrRefCount(ec->pat); }
-	if (ec->gate) { Tcl_DecrRefCount(ec->gate); }
 	if (ec->body) { Tcl_DecrRefCount(ec->body); }
     }
+    if (ec->gate) { Tcl_DecrRefCount(ec->gate); }
 
     if (free_ilist) {
 	ec->i_list->ecount--;
@@ -1094,7 +1094,7 @@
 			/* shift remaining elements down */
 			/* but only if there are any left */
 			if (i+1 != ecmd->ecd.count) {
-				memcpy(&ecmd->ecd.cases[i],
+				memmove(&ecmd->ecd.cases[i],
 				       &ecmd->ecd.cases[i+1],
 					((ecmd->ecd.count - i) - 1) * 
 					sizeof(struct exp_cmd_descriptor *));
@@ -2363,7 +2363,12 @@
 
 	/* "!e" means no case matched - transfer by default */
 	if (!e || e->transfer) {
-	    int remainder = numchars-match;
+	    int remainder;
+	    if (match > numchars) {
+		match = numchars;
+		eo->matchlen = match;
+	    }
+	    remainder = numchars-match;
 	    /* delete matched chars from input buffer */
 	    esPtr->printed -= match;
 	    if (numchars != 0) {
@@ -2899,7 +2904,7 @@
     int def = FALSE;
     char* chan = NULL;
     int i;
-    ExpState *esPtr;
+    ExpState *esPtr = NULL;
 
     for (i=1; i<objc; i++) {
 	char *name;
diff -ruN expect5.45.orig/pty_termios.c expect5.45/pty_termios.c
--- expect5.45.orig/pty_termios.c	2010-08-27 16:51:53.000000000 -0500
+++ expect5.45/pty_termios.c	2020-11-16 20:33:57.000000000 -0600
@@ -9,12 +9,17 @@
 
 #include <stdio.h>
 #include <signal.h>
+#include <util.h>
 
 #if defined(SIGCLD) && !defined(SIGCHLD)
 #define SIGCHLD SIGCLD
 #endif
 
 #include "expect_cf.h"
+#include <tcl.h>
+#include "expect.h"
+#include "exp_tty_in.h"
+#include "exp_int.h"  /* expErrnoMsg() prototype */
 
 /*
    The following functions are linked from the Tcl library.  They
@@ -673,7 +680,7 @@
 		fcntl(0,F_DUPFD,2);
 	}
 
-	ttytype(SET_TTYTYPE,slave,ttycopy,ttyinit,stty_args);
+	ttytype(SET_TTYTYPE,slave,ttycopy,ttyinit,(char *)stty_args);
 
 #if 0
 #ifdef HAVE_PTYTRAP