Make sure the sigaction structure and fileno function are properly declared with...
[openssl.git] / crypto / ui / ui_openssl.c
index a958a5b87849a819b40cd6ba0a734a3db3148109..ce1cb1dfcd13d217a60928f1dccb9a78c1082989 100644 (file)
 
 #include <openssl/e_os2.h>
 
+#define _POSIX_C_SOURCE 1
+#include <signal.h>
+#include <stdio.h>
+#undef _POSIX_C_SOURCE
+#include <string.h>
+#include <errno.h>
+
 #if !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS)
 # ifdef OPENSSL_UNISTD
 #  include OPENSSL_UNISTD
 /* 06-Apr-92 Luke Brennan    Support for VMS */
 #include "ui_locl.h"
 #include "cryptlib.h"
-#include <signal.h>
-#include <stdio.h>
-#include <string.h>
-#include <setjmp.h>
-#include <errno.h>
 
 #ifdef OPENSSL_SYS_VMS         /* prototypes for sys$whatever */
 # include <starlet.h>
 
 #ifdef WIN_CONSOLE_BUG
 # include <windows.h>
+#ifndef OPENSSL_SYS_WINCE
 # include <wincon.h>
 #endif
+#endif
 
 
 /* There are 5 types of terminal interface supported,
 # define SGTTY
 #endif
 
+#if defined(OPENSSL_SYS_VXWORKS)
+#undef TERMIOS
+#undef TERMIO
+#undef SGTTY
+#endif
+
 #ifdef TERMIOS
 # include <termios.h>
 # define TTY_STRUCT            struct termios
 # define TTY_set(tty,data)     ioctl(tty,TIOCSETP,data)
 #endif
 
-#if !defined(_LIBC) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS) && !defined(OPENSSL_SYS_MACINTOSH_CLASSIC)
+#if !defined(_LIBC) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS) && !defined(OPENSSL_SYS_MACINTOSH_CLASSIC) && !defined(OPENSSL_SYS_SUNOS)
 # include <sys/ioctl.h>
 #endif
 
@@ -236,6 +246,10 @@ struct IOSB {
        };
 #endif
 
+#ifdef OPENSSL_SYS_SUNOS
+       typedef int sig_atomic_t;
+#endif
+
 #if defined(OPENSSL_SYS_MACINTOSH_CLASSIC) || defined(MAC_OS_GUSI_SOURCE)
 /*
  * This one needs work. As a matter of fact the code is unoperational
@@ -256,7 +270,6 @@ static struct sigaction savsig[NX509_SIG];
 #else
 static void (*savsig[NX509_SIG])(int );
 #endif
-static jmp_buf save;
 
 #ifdef OPENSSL_SYS_VMS
 static struct IOSB iosb;
@@ -265,7 +278,7 @@ static long tty_orig[3], tty_new[3]; /* XXX   Is there any guarantee that this w
 static long status;
 static unsigned short channel = 0;
 #else
-#ifndef OPENSSL_SYS_MSDOS
+#if !defined(OPENSSL_SYS_MSDOS) || defined(__DJGPP__)
 static TTY_STRUCT tty_orig,tty_new;
 #endif
 #endif
@@ -273,10 +286,12 @@ static FILE *tty_in, *tty_out;
 static int is_a_tty;
 
 /* Declare static functions */
+#if !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE)
 static void read_till_nl(FILE *);
 static void recsig(int);
 static void pushsig(void);
 static void popsig(void);
+#endif
 #if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN16)
 static int noecho_fgets(char *buf, int size, FILE *tty);
 #endif
@@ -363,6 +378,7 @@ static int read_string(UI *ui, UI_STRING *uis)
        }
 
 
+#if !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE)
 /* Internal functions to read a string without echoing */
 static void read_till_nl(FILE *in)
        {
@@ -374,38 +390,43 @@ static void read_till_nl(FILE *in)
                } while (strchr(buf,'\n') == NULL);
        }
 
+static volatile sig_atomic_t intr_signal;
+#endif
+
 static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl)
        {
        static int ps;
        int ok;
        char result[BUFSIZ];
        int maxsize = BUFSIZ-1;
+#if !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE)
        char *p;
 
-#ifndef OPENSSL_SYS_WIN16
-       if ((ok = setjmp(save)))
-               {
-               if (ok == 1) ok=0;
-               goto error;
-               }
+       intr_signal=0;
        ok=0;
        ps=0;
 
        pushsig();
        ps=1;
 
-       if (!echo) noecho_console(ui);
+       if (!echo && !noecho_console(ui))
+               goto error;
        ps=2;
 
        result[0]='\0';
 #ifdef OPENSSL_SYS_MSDOS
        if (!echo)
+               {
                noecho_fgets(result,maxsize,tty_in);
+               p=result; /* FIXME: noecho_fgets doesn't return errors */
+               }
        else
-               fgets(result,maxsize,tty_in);
+               p=fgets(result,maxsize,tty_in);
 #else
-       fgets(result,maxsize,tty_in);
+       p=fgets(result,maxsize,tty_in);
 #endif
+       if(!p)
+               goto error;
        if (feof(tty_in)) goto error;
        if (ferror(tty_in)) goto error;
        if ((p=(char *)strchr(result,'\n')) != NULL)
@@ -419,9 +440,11 @@ static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl)
                ok=1;
 
 error:
+       if (intr_signal == SIGINT)
+               ok=-1;
        if (!echo) fprintf(tty_out,"\n");
-       if (ps >= 2 && !echo)
-               echo_console(ui);
+       if (ps >= 2 && !echo && !echo_console(ui))
+               ok=0;
 
        if (ps >= 1)
                popsig();
@@ -429,7 +452,7 @@ error:
        ok=1;
 #endif
 
-       memset(result,0,BUFSIZ);
+       OPENSSL_cleanse(result,BUFSIZ);
        return ok;
        }
 
@@ -440,7 +463,7 @@ static int open_console(UI *ui)
        CRYPTO_w_lock(CRYPTO_LOCK_UI);
        is_a_tty = 1;
 
-#if defined(OPENSSL_SYS_MACINTOSH_CLASSIC)
+#if defined(OPENSSL_SYS_MACINTOSH_CLASSIC) || defined(OPENSSL_SYS_VXWORKS)
        tty_in=stdin;
        tty_out=stderr;
 #else
@@ -455,8 +478,8 @@ static int open_console(UI *ui)
                tty_out=stderr;
 #endif
 
-#if defined(TTY_get) && !defined(VMS)
-       if (TTY_get(fileno(tty_in),&tty_orig) == -1)
+#if defined(TTY_get) && !defined(OPENSSL_SYS_VMS)
+       if (TTY_get(fileno(tty_in),&tty_orig) == -1)
                {
 #ifdef ENOTTY
                if (errno == ENOTTY)
@@ -530,7 +553,7 @@ static int echo_console(UI *ui)
 
 static int close_console(UI *ui)
        {
-       if (tty_in != stderr) fclose(tty_in);
+       if (tty_in != stdin) fclose(tty_in);
        if (tty_out != stderr) fclose(tty_out);
 #ifdef OPENSSL_SYS_VMS
        status = sys$dassgn(channel);
@@ -541,6 +564,7 @@ static int close_console(UI *ui)
        }
 
 
+#if !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE)
 /* Internal functions to handle signals and act on them */
 static void pushsig(void)
        {
@@ -602,20 +626,12 @@ static void popsig(void)
 
 static void recsig(int i)
        {
-       switch(i)
-               {
-       case SIGINT:
-               longjmp(save,-1);
-               break;
-       default:
-               break;
-               }
-       longjmp(save,1);
+       intr_signal=i;
        }
-
+#endif
 
 /* Internal functions specific for Windows */
-#if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN16)
+#if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE)
 static int noecho_fgets(char *buf, int size, FILE *tty)
        {
        int i;