Refactor to avoid unnecessary preprocessor logic
authorRichard Levitte <levitte@openssl.org>
Thu, 15 Sep 2016 09:20:18 +0000 (11:20 +0200)
committerRichard Levitte <levitte@openssl.org>
Thu, 15 Sep 2016 21:24:06 +0000 (23:24 +0200)
Reviewed-by: Andy Polyakov <appro@openssl.org>
Reviewed-by: Rich Salz <rsalz@openssl.org>
apps/apps.c
apps/apps.h
apps/s_client.c
apps/s_server.c

index 522db71322ef050f79ec6e09c6c35a0ddfe51540..6ca0f2b1bc5538f6f083ac3067f85a2a9854fc2d 100644 (file)
@@ -2300,6 +2300,36 @@ int app_isdir(const char *name)
 #endif
 
 /* raw_read|write section */
+#if defined(__VMS)
+# include "vms_term_sock.h"
+static int stdin_sock = -1;
+
+static void close_stdin_sock(void)
+{
+    TerminalSocket (TERM_SOCK_DELETE, &stdin_sock);
+}
+
+int fileno_stdin(void)
+{
+    if (stdin_sock == -1) {
+        TerminalSocket(TERM_SOCK_CREATE, &stdin_sock);
+        atexit(close_stdin_sock);
+    }
+
+    return stdin_sock;
+}
+#else
+int fileno_stdin(void)
+{
+    return fileno(stdin);
+}
+#endif
+
+int fileno_stdout(void)
+{
+    return fileno(stdout);
+}
+
 #if defined(_WIN32) && defined(STD_INPUT_HANDLE)
 int raw_read_stdin(void *buf, int siz)
 {
@@ -2309,10 +2339,15 @@ int raw_read_stdin(void *buf, int siz)
     else
         return (-1);
 }
+#elif defined(__VMS)
+int raw_read_stdin(void *buf, int siz)
+{
+    return recv(fileno_stdin(), buf, siz, 0);
+}
 #else
 int raw_read_stdin(void *buf, int siz)
 {
-    return read(fileno(stdin), buf, siz);
+    return read(fileno_stdin(), buf, siz);
 }
 #endif
 
@@ -2328,7 +2363,7 @@ int raw_write_stdout(const void *buf, int siz)
 #else
 int raw_write_stdout(const void *buf, int siz)
 {
-    return write(fileno(stdout), buf, siz);
+    return write(fileno_stdout(), buf, siz);
 }
 #endif
 
index 176150164d6bdab2f9f68836ac7eabe629a50347..6083780a7873fe827bb34c51ff46eff8f59aa7af 100644 (file)
@@ -549,6 +549,8 @@ void store_setup_crl_download(X509_STORE *st);
 
 int app_isdir(const char *);
 int app_access(const char *, int flag);
+int fileno_stdin(void);
+int fileno_stdout(void);
 int raw_read_stdin(void *, int);
 int raw_write_stdout(const void *, int);
 
index 820bffe8b4842ab0cbbc0f9113dc7d4917769878..0627ae8bf6b00e1c4c9cea5edf93aab26604ac9c 100644 (file)
@@ -43,9 +43,6 @@
 
 #ifndef OPENSSL_NO_SOCK
 
-#ifdef OPENSSL_SYS_VMS
-# include "vms_term_sock.h"
-#endif
 /*
  * With IPv6, it looks like Digital has mixed up the proper order of
  * recursive header file inclusion, resulting in the compiler complaining
@@ -861,10 +858,6 @@ int s_client_main(int argc, char **argv)
     int c_nbio = 0, c_msg = 0, c_ign_eof = 0, c_brief = 0;
     int c_tlsextdebug = 0, c_status_req = 0;
     BIO *bio_c_msg = NULL;
-#if defined(OPENSSL_SYS_VMS)
-    int stdin_sock;
-    TerminalSocket(TERM_SOCK_CREATE, &stdin_sock);
-#endif
 
     FD_ZERO(&readfds);
     FD_ZERO(&writefds);
@@ -1828,14 +1821,11 @@ int s_client_main(int argc, char **argv)
     SSL_set_connect_state(con);
 
     /* ok, lets connect */
-#if defined(OPENSSL_SYS_VMS)
-    if (stdin_sock > SSL_get_fd(con))
-        width = stdin_sock + 1;
+    if (fileno_stdin() > SSL_get_fd(con))
+        width = fileno_stdin() + 1;
     else
         width = SSL_get_fd(con) + 1;
-#else
-    width = SSL_get_fd(con) + 1;
-#endif
+
     read_tty = 1;
     write_tty = 0;
     tty_on = 0;
@@ -2162,14 +2152,11 @@ int s_client_main(int argc, char **argv)
                  * and EOF satisfies that.  To avoid a CPU-hogging loop,
                  * set the flag so we exit.
                  */
-#if defined(OPENSSL_SYS_VMS)
-                if (read_tty && !at_eof)
-                    openssl_fdset(stdin_sock, &readfds);
-#else
                 if (read_tty && !at_eof)
-                    openssl_fdset(fileno(stdin), &readfds);
+                    openssl_fdset(fileno_stdin(), &readfds);
+#if !defined(OPENSSL_SYS_VMS)
                 if (write_tty)
-                    openssl_fdset(fileno(stdout), &writefds);
+                    openssl_fdset(fileno_stdout(), &writefds);
 #endif
             }
             if (read_ssl)
@@ -2300,7 +2287,7 @@ int s_client_main(int argc, char **argv)
         /* Assume Windows/DOS/BeOS can always write */
         else if (!ssl_pending && write_tty)
 #else
-        else if (!ssl_pending && FD_ISSET(fileno(stdout), &writefds))
+        else if (!ssl_pending && FD_ISSET(fileno_stdout(), &writefds))
 #endif
         {
 #ifdef CHARSET_EBCDIC
@@ -2388,21 +2375,14 @@ int s_client_main(int argc, char **argv)
 /* OPENSSL_SYS_MSDOS includes OPENSSL_SYS_WINDOWS */
 #if defined(OPENSSL_SYS_MSDOS)
         else if (has_stdin_waiting())
-#elif defined(OPENSSL_SYS_VMS)
-        else if (FD_ISSET(stdin_sock, &readfds))
 #else
-        else if (FD_ISSET(fileno(stdin), &readfds))
+        else if (FD_ISSET(fileno_stdin(), &readfds))
 #endif
         {
             if (crlf) {
                 int j, lf_num;
 
-#if defined(OPENSSL_SYS_VMS)
-                i = recv(stdin_sock, cbuf, BUFSIZZ / 2, 0);
-#else
                 i = raw_read_stdin(cbuf, BUFSIZZ / 2);
-#endif
-
                 lf_num = 0;
                 /* both loops are skipped when i <= 0 */
                 for (j = 0; j < i; j++)
@@ -2417,13 +2397,8 @@ int s_client_main(int argc, char **argv)
                     }
                 }
                 assert(lf_num == 0);
-            } else {
-#if defined(OPENSSL_SYS_VMS)
-                i = recv(stdin_sock, cbuf, BUFSIZZ, 0);
-#else
+            } else
                 i = raw_read_stdin(cbuf, BUFSIZZ);
-#endif
-            }
 #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS)
             if (i == 0)
                 at_eof = 1;
@@ -2508,9 +2483,6 @@ int s_client_main(int argc, char **argv)
     bio_c_out = NULL;
     BIO_free(bio_c_msg);
     bio_c_msg = NULL;
-#if defined(OPENSSL_SYS_VMS)
-    TerminalSocket(TERM_SOCK_DELETE, &stdin_sock);
-#endif
     return (ret);
 }
 
index 060a0bb24fe5513589ae7d88e198c6717f483ee7..ebab7a2137a83136113276e17f1331c0cc258045 100644 (file)
@@ -90,11 +90,6 @@ typedef unsigned int u_int;
 #include <openssl/ebcdic.h>
 #endif
 
-#ifdef OPENSSL_SYS_VMS
-# include "vms_term_sock.h"
-#endif
-
-
 static int not_resumable_sess_cb(SSL *s, int is_forward_secure);
 static int sv_body(int s, int stype, unsigned char *context);
 static int www_body(int s, int stype, unsigned char *context);
@@ -2012,10 +2007,6 @@ static int sv_body(int s, int stype, unsigned char *context)
 #else
     struct timeval *timeoutp;
 #endif
-#if defined(OPENSSL_SYS_VMS)
-    int stdin_sock;
-    TerminalSocket (TERM_SOCK_CREATE, &stdin_sock);
-#endif
 
     buf = app_malloc(bufsize, "server buffer");
     if (s_nbio) {
@@ -2116,15 +2107,10 @@ static int sv_body(int s, int stype, unsigned char *context)
         SSL_set_tlsext_debug_arg(con, bio_s_out);
     }
 
-
-#if defined(OPENSSL_SYS_VMS)
-    if (stdin_sock > s)
-        width = stdin_sock + 1;
+    if (fileno_stdin() > s)
+        width = fileno_stdin() + 1;
     else
         width = s + 1;
-#else
-    width = s + 1;
-#endif
     for (;;) {
         int read_from_terminal;
         int read_from_sslcon;
@@ -2136,11 +2122,7 @@ static int sv_body(int s, int stype, unsigned char *context)
         if (!read_from_sslcon) {
             FD_ZERO(&readfds);
 #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS)
-# if defined(OPENSSL_SYS_VMS)
-            openssl_fdset(stdin_sock, &readfds);
-# else
-            openssl_fdset(fileno(stdin), &readfds);
-# endif
+            openssl_fdset(fileno_stdin(), &readfds);
 #endif
             openssl_fdset(s, &readfds);
             /*
@@ -2180,11 +2162,7 @@ static int sv_body(int s, int stype, unsigned char *context)
 
             if (i <= 0)
                 continue;
-# if defined(OPENSSL_SYS_VMS)
-            if (FD_ISSET(stdin_sock, &readfds))
-# else
-            if (FD_ISSET(fileno(stdin), &readfds))
-# endif
+            if (FD_ISSET(fileno_stdin(), &readfds))
                 read_from_terminal = 1;
 #endif
             if (FD_ISSET(s, &readfds))
@@ -2194,11 +2172,7 @@ static int sv_body(int s, int stype, unsigned char *context)
             if (s_crlf) {
                 int j, lf_num;
 
-#if defined(OPENSSL_SYS_VMS)
-                i=recv(stdin_sock, buf, bufsize / 2, 0);
-#else
                 i = raw_read_stdin(buf, bufsize / 2);
-#endif
                 lf_num = 0;
                 /* both loops are skipped when i <= 0 */
                 for (j = 0; j < i; j++)
@@ -2213,13 +2187,9 @@ static int sv_body(int s, int stype, unsigned char *context)
                     }
                 }
                 assert(lf_num == 0);
-            } else {
-#if defined(OPENSSL_SYS_VMS)
-                i = recv(stdin_sock, buf, bufsize, 0);
-#else
+            } else
                 i = raw_read_stdin(buf, bufsize);
-#endif
-            }
+
             if (!s_quiet && !s_brief) {
                 if ((i <= 0) || (buf[0] == 'Q')) {
                     BIO_printf(bio_s_out, "DONE\n");
@@ -2433,9 +2403,6 @@ static int sv_body(int s, int stype, unsigned char *context)
     OPENSSL_clear_free(buf, bufsize);
     if (ret >= 0)
         BIO_printf(bio_s_out, "ACCEPT\n");
-#if defined(OPENSSL_SYS_VMS)
-    TerminalSocket (TERM_SOCK_DELETE, &stdin_sock);
-#endif
     (void)BIO_flush(bio_s_out);
     return (ret);
 }