1ebfedea0SLionel Sambuc /* crypto/ui/ui_openssl.c -*- mode:C; c-file-style: "eay" -*- */
2*0a6a1f1dSLionel Sambuc /*
3*0a6a1f1dSLionel Sambuc * Written by Richard Levitte (richard@levitte.org) and others for the
4*0a6a1f1dSLionel Sambuc * OpenSSL project 2001.
5ebfedea0SLionel Sambuc */
6ebfedea0SLionel Sambuc /* ====================================================================
7ebfedea0SLionel Sambuc * Copyright (c) 2001 The OpenSSL Project. All rights reserved.
8ebfedea0SLionel Sambuc *
9ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
10ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
11ebfedea0SLionel Sambuc * are met:
12ebfedea0SLionel Sambuc *
13ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
14ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
15ebfedea0SLionel Sambuc *
16ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
17ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in
18ebfedea0SLionel Sambuc * the documentation and/or other materials provided with the
19ebfedea0SLionel Sambuc * distribution.
20ebfedea0SLionel Sambuc *
21ebfedea0SLionel Sambuc * 3. All advertising materials mentioning features or use of this
22ebfedea0SLionel Sambuc * software must display the following acknowledgment:
23ebfedea0SLionel Sambuc * "This product includes software developed by the OpenSSL Project
24ebfedea0SLionel Sambuc * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
25ebfedea0SLionel Sambuc *
26ebfedea0SLionel Sambuc * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27ebfedea0SLionel Sambuc * endorse or promote products derived from this software without
28ebfedea0SLionel Sambuc * prior written permission. For written permission, please contact
29ebfedea0SLionel Sambuc * openssl-core@openssl.org.
30ebfedea0SLionel Sambuc *
31ebfedea0SLionel Sambuc * 5. Products derived from this software may not be called "OpenSSL"
32ebfedea0SLionel Sambuc * nor may "OpenSSL" appear in their names without prior written
33ebfedea0SLionel Sambuc * permission of the OpenSSL Project.
34ebfedea0SLionel Sambuc *
35ebfedea0SLionel Sambuc * 6. Redistributions of any form whatsoever must retain the following
36ebfedea0SLionel Sambuc * acknowledgment:
37ebfedea0SLionel Sambuc * "This product includes software developed by the OpenSSL Project
38ebfedea0SLionel Sambuc * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
39ebfedea0SLionel Sambuc *
40ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41ebfedea0SLionel Sambuc * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43ebfedea0SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44ebfedea0SLionel Sambuc * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45ebfedea0SLionel Sambuc * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46ebfedea0SLionel Sambuc * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47ebfedea0SLionel Sambuc * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49ebfedea0SLionel Sambuc * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50ebfedea0SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51ebfedea0SLionel Sambuc * OF THE POSSIBILITY OF SUCH DAMAGE.
52ebfedea0SLionel Sambuc * ====================================================================
53ebfedea0SLionel Sambuc *
54ebfedea0SLionel Sambuc * This product includes cryptographic software written by Eric Young
55ebfedea0SLionel Sambuc * (eay@cryptsoft.com). This product includes software written by Tim
56ebfedea0SLionel Sambuc * Hudson (tjh@cryptsoft.com).
57ebfedea0SLionel Sambuc *
58ebfedea0SLionel Sambuc */
59ebfedea0SLionel Sambuc
60*0a6a1f1dSLionel Sambuc /*-
61*0a6a1f1dSLionel Sambuc * The lowest level part of this file was previously in crypto/des/read_pwd.c,
62ebfedea0SLionel Sambuc * Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
63ebfedea0SLionel Sambuc * All rights reserved.
64ebfedea0SLionel Sambuc *
65ebfedea0SLionel Sambuc * This package is an SSL implementation written
66ebfedea0SLionel Sambuc * by Eric Young (eay@cryptsoft.com).
67ebfedea0SLionel Sambuc * The implementation was written so as to conform with Netscapes SSL.
68ebfedea0SLionel Sambuc *
69ebfedea0SLionel Sambuc * This library is free for commercial and non-commercial use as long as
70ebfedea0SLionel Sambuc * the following conditions are aheared to. The following conditions
71ebfedea0SLionel Sambuc * apply to all code found in this distribution, be it the RC4, RSA,
72ebfedea0SLionel Sambuc * lhash, DES, etc., code; not just the SSL code. The SSL documentation
73ebfedea0SLionel Sambuc * included with this distribution is covered by the same copyright terms
74ebfedea0SLionel Sambuc * except that the holder is Tim Hudson (tjh@cryptsoft.com).
75ebfedea0SLionel Sambuc *
76ebfedea0SLionel Sambuc * Copyright remains Eric Young's, and as such any Copyright notices in
77ebfedea0SLionel Sambuc * the code are not to be removed.
78ebfedea0SLionel Sambuc * If this package is used in a product, Eric Young should be given attribution
79ebfedea0SLionel Sambuc * as the author of the parts of the library used.
80ebfedea0SLionel Sambuc * This can be in the form of a textual message at program startup or
81ebfedea0SLionel Sambuc * in documentation (online or textual) provided with the package.
82ebfedea0SLionel Sambuc *
83ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
84ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
85ebfedea0SLionel Sambuc * are met:
86ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the copyright
87ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
88ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
89ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
90ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
91ebfedea0SLionel Sambuc * 3. All advertising materials mentioning features or use of this software
92ebfedea0SLionel Sambuc * must display the following acknowledgement:
93ebfedea0SLionel Sambuc * "This product includes cryptographic software written by
94ebfedea0SLionel Sambuc * Eric Young (eay@cryptsoft.com)"
95ebfedea0SLionel Sambuc * The word 'cryptographic' can be left out if the rouines from the library
96ebfedea0SLionel Sambuc * being used are not cryptographic related :-).
97ebfedea0SLionel Sambuc * 4. If you include any Windows specific code (or a derivative thereof) from
98ebfedea0SLionel Sambuc * the apps directory (application code) you must include an acknowledgement:
99ebfedea0SLionel Sambuc * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
100ebfedea0SLionel Sambuc *
101ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
102ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
103ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
104ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
105ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
106ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
107ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
108ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
109ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
110ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
111ebfedea0SLionel Sambuc * SUCH DAMAGE.
112ebfedea0SLionel Sambuc *
113ebfedea0SLionel Sambuc * The licence and distribution terms for any publically available version or
114ebfedea0SLionel Sambuc * derivative of this code cannot be changed. i.e. this code cannot simply be
115ebfedea0SLionel Sambuc * copied and put under another distribution licence
116ebfedea0SLionel Sambuc * [including the GNU Public Licence.]
117ebfedea0SLionel Sambuc */
118ebfedea0SLionel Sambuc
119ebfedea0SLionel Sambuc #include <openssl/e_os2.h>
120ebfedea0SLionel Sambuc
121*0a6a1f1dSLionel Sambuc /*
122*0a6a1f1dSLionel Sambuc * need for #define _POSIX_C_SOURCE arises whenever you pass -ansi to gcc
123ebfedea0SLionel Sambuc * [maybe others?], because it masks interfaces not discussed in standard,
124*0a6a1f1dSLionel Sambuc * sigaction and fileno included. -pedantic would be more appropriate for the
125*0a6a1f1dSLionel Sambuc * intended purposes, but we can't prevent users from adding -ansi.
126ebfedea0SLionel Sambuc */
127ebfedea0SLionel Sambuc #if defined(OPENSSL_SYSNAME_VXWORKS)
128ebfedea0SLionel Sambuc # include <sys/types.h>
129ebfedea0SLionel Sambuc #endif
130ebfedea0SLionel Sambuc
131ebfedea0SLionel Sambuc #if !defined(_POSIX_C_SOURCE) && defined(OPENSSL_SYS_VMS)
132ebfedea0SLionel Sambuc # ifndef _POSIX_C_SOURCE
133ebfedea0SLionel Sambuc # define _POSIX_C_SOURCE 2
134ebfedea0SLionel Sambuc # endif
135ebfedea0SLionel Sambuc #endif
136ebfedea0SLionel Sambuc #include <signal.h>
137ebfedea0SLionel Sambuc #include <stdio.h>
138ebfedea0SLionel Sambuc #include <string.h>
139ebfedea0SLionel Sambuc #include <errno.h>
140ebfedea0SLionel Sambuc
141ebfedea0SLionel Sambuc #if !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS)
142*0a6a1f1dSLionel Sambuc /*
143*0a6a1f1dSLionel Sambuc * If unistd.h defines _POSIX_VERSION, we conclude that we are on a POSIX
144*0a6a1f1dSLionel Sambuc * system and have sigaction and termios.
145*0a6a1f1dSLionel Sambuc */
146ebfedea0SLionel Sambuc # if defined(_POSIX_VERSION)
147ebfedea0SLionel Sambuc
148ebfedea0SLionel Sambuc # define SIGACTION
149ebfedea0SLionel Sambuc # if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY)
150ebfedea0SLionel Sambuc # define TERMIOS
151ebfedea0SLionel Sambuc # endif
152ebfedea0SLionel Sambuc
153ebfedea0SLionel Sambuc # endif
154ebfedea0SLionel Sambuc #endif
155ebfedea0SLionel Sambuc
156ebfedea0SLionel Sambuc #ifdef WIN16TTY
157ebfedea0SLionel Sambuc # undef OPENSSL_SYS_WIN16
158ebfedea0SLionel Sambuc # undef WIN16
159ebfedea0SLionel Sambuc # undef _WINDOWS
160ebfedea0SLionel Sambuc # include <graph.h>
161ebfedea0SLionel Sambuc #endif
162ebfedea0SLionel Sambuc
163ebfedea0SLionel Sambuc /* 06-Apr-92 Luke Brennan Support for VMS */
164ebfedea0SLionel Sambuc #include "ui_locl.h"
165ebfedea0SLionel Sambuc #include "cryptlib.h"
166ebfedea0SLionel Sambuc
167ebfedea0SLionel Sambuc #ifdef OPENSSL_SYS_VMS /* prototypes for sys$whatever */
168ebfedea0SLionel Sambuc # include <starlet.h>
169ebfedea0SLionel Sambuc # ifdef __DECC
170ebfedea0SLionel Sambuc # pragma message disable DOLLARID
171ebfedea0SLionel Sambuc # endif
172ebfedea0SLionel Sambuc #endif
173ebfedea0SLionel Sambuc
174ebfedea0SLionel Sambuc #ifdef WIN_CONSOLE_BUG
175ebfedea0SLionel Sambuc # include <windows.h>
176ebfedea0SLionel Sambuc # ifndef OPENSSL_SYS_WINCE
177ebfedea0SLionel Sambuc # include <wincon.h>
178ebfedea0SLionel Sambuc # endif
179ebfedea0SLionel Sambuc #endif
180ebfedea0SLionel Sambuc
181*0a6a1f1dSLionel Sambuc /*
182*0a6a1f1dSLionel Sambuc * There are 5 types of terminal interface supported, TERMIO, TERMIOS, VMS,
183*0a6a1f1dSLionel Sambuc * MSDOS and SGTTY.
184*0a6a1f1dSLionel Sambuc *
185*0a6a1f1dSLionel Sambuc * If someone defines one of the macros TERMIO, TERMIOS or SGTTY, it will
186*0a6a1f1dSLionel Sambuc * remain respected. Otherwise, we default to TERMIOS except for a few
187*0a6a1f1dSLionel Sambuc * systems that require something different.
188*0a6a1f1dSLionel Sambuc *
189*0a6a1f1dSLionel Sambuc * Note: we do not use SGTTY unless it's defined by the configuration. We
190*0a6a1f1dSLionel Sambuc * may eventually opt to remove it's use entirely.
191ebfedea0SLionel Sambuc */
192ebfedea0SLionel Sambuc
193*0a6a1f1dSLionel Sambuc #if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY)
194*0a6a1f1dSLionel Sambuc
195*0a6a1f1dSLionel Sambuc # if defined(_LIBC)
196*0a6a1f1dSLionel Sambuc # undef TERMIOS
197*0a6a1f1dSLionel Sambuc # define TERMIO
198*0a6a1f1dSLionel Sambuc # undef SGTTY
199*0a6a1f1dSLionel Sambuc /*
200*0a6a1f1dSLionel Sambuc * We know that VMS, MSDOS, VXWORKS, NETWARE use entirely other mechanisms.
201*0a6a1f1dSLionel Sambuc * MAC_OS_GUSI_SOURCE should probably go away, but that needs to be confirmed.
202*0a6a1f1dSLionel Sambuc */
203*0a6a1f1dSLionel Sambuc # elif !defined(OPENSSL_SYS_VMS) \
204*0a6a1f1dSLionel Sambuc && !defined(OPENSSL_SYS_MSDOS) \
205*0a6a1f1dSLionel Sambuc && !defined(OPENSSL_SYS_MACINTOSH_CLASSIC) \
206*0a6a1f1dSLionel Sambuc && !defined(MAC_OS_GUSI_SOURCE) \
207*0a6a1f1dSLionel Sambuc && !defined(OPENSSL_SYS_VXWORKS) \
208*0a6a1f1dSLionel Sambuc && !defined(OPENSSL_SYS_NETWARE)
209ebfedea0SLionel Sambuc # define TERMIOS
210ebfedea0SLionel Sambuc # undef TERMIO
211ebfedea0SLionel Sambuc # undef SGTTY
212ebfedea0SLionel Sambuc # endif
213ebfedea0SLionel Sambuc
214ebfedea0SLionel Sambuc #endif
215ebfedea0SLionel Sambuc
216ebfedea0SLionel Sambuc #ifdef TERMIOS
217ebfedea0SLionel Sambuc # include <termios.h>
218ebfedea0SLionel Sambuc # define TTY_STRUCT struct termios
219ebfedea0SLionel Sambuc # define TTY_FLAGS c_lflag
220ebfedea0SLionel Sambuc # define TTY_get(tty,data) tcgetattr(tty,data)
221ebfedea0SLionel Sambuc # define TTY_set(tty,data) tcsetattr(tty,TCSANOW,data)
222ebfedea0SLionel Sambuc #endif
223ebfedea0SLionel Sambuc
224ebfedea0SLionel Sambuc #ifdef TERMIO
225ebfedea0SLionel Sambuc # include <termio.h>
226ebfedea0SLionel Sambuc # define TTY_STRUCT struct termio
227ebfedea0SLionel Sambuc # define TTY_FLAGS c_lflag
228ebfedea0SLionel Sambuc # define TTY_get(tty,data) ioctl(tty,TCGETA,data)
229ebfedea0SLionel Sambuc # define TTY_set(tty,data) ioctl(tty,TCSETA,data)
230ebfedea0SLionel Sambuc #endif
231ebfedea0SLionel Sambuc
232ebfedea0SLionel Sambuc #ifdef SGTTY
233ebfedea0SLionel Sambuc # include <sgtty.h>
234ebfedea0SLionel Sambuc # define TTY_STRUCT struct sgttyb
235ebfedea0SLionel Sambuc # define TTY_FLAGS sg_flags
236ebfedea0SLionel Sambuc # define TTY_get(tty,data) ioctl(tty,TIOCGETP,data)
237ebfedea0SLionel Sambuc # define TTY_set(tty,data) ioctl(tty,TIOCSETP,data)
238ebfedea0SLionel Sambuc #endif
239ebfedea0SLionel Sambuc
240ebfedea0SLionel Sambuc #if !defined(_LIBC) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS) && !defined(OPENSSL_SYS_MACINTOSH_CLASSIC) && !defined(OPENSSL_SYS_SUNOS)
241ebfedea0SLionel Sambuc # include <sys/ioctl.h>
242ebfedea0SLionel Sambuc #endif
243ebfedea0SLionel Sambuc
244ebfedea0SLionel Sambuc #ifdef OPENSSL_SYS_MSDOS
245ebfedea0SLionel Sambuc # include <conio.h>
246ebfedea0SLionel Sambuc #endif
247ebfedea0SLionel Sambuc
248ebfedea0SLionel Sambuc #ifdef OPENSSL_SYS_VMS
249ebfedea0SLionel Sambuc # include <ssdef.h>
250ebfedea0SLionel Sambuc # include <iodef.h>
251ebfedea0SLionel Sambuc # include <ttdef.h>
252ebfedea0SLionel Sambuc # include <descrip.h>
253ebfedea0SLionel Sambuc struct IOSB {
254ebfedea0SLionel Sambuc short iosb$w_value;
255ebfedea0SLionel Sambuc short iosb$w_count;
256ebfedea0SLionel Sambuc long iosb$l_info;
257ebfedea0SLionel Sambuc };
258ebfedea0SLionel Sambuc #endif
259ebfedea0SLionel Sambuc
260ebfedea0SLionel Sambuc #ifdef OPENSSL_SYS_SUNOS
261ebfedea0SLionel Sambuc typedef int sig_atomic_t;
262ebfedea0SLionel Sambuc #endif
263ebfedea0SLionel Sambuc
264ebfedea0SLionel Sambuc #if defined(OPENSSL_SYS_MACINTOSH_CLASSIC) || defined(MAC_OS_GUSI_SOURCE) || defined(OPENSSL_SYS_NETWARE)
265ebfedea0SLionel Sambuc /*
266ebfedea0SLionel Sambuc * This one needs work. As a matter of fact the code is unoperational
267ebfedea0SLionel Sambuc * and this is only a trick to get it compiled.
268ebfedea0SLionel Sambuc * <appro@fy.chalmers.se>
269ebfedea0SLionel Sambuc */
270ebfedea0SLionel Sambuc # define TTY_STRUCT int
271ebfedea0SLionel Sambuc #endif
272ebfedea0SLionel Sambuc
273ebfedea0SLionel Sambuc #ifndef NX509_SIG
274ebfedea0SLionel Sambuc # define NX509_SIG 32
275ebfedea0SLionel Sambuc #endif
276ebfedea0SLionel Sambuc
277ebfedea0SLionel Sambuc /* Define globals. They are protected by a lock */
278ebfedea0SLionel Sambuc #ifdef SIGACTION
279ebfedea0SLionel Sambuc static struct sigaction savsig[NX509_SIG];
280ebfedea0SLionel Sambuc #else
281ebfedea0SLionel Sambuc static void (*savsig[NX509_SIG]) (int);
282ebfedea0SLionel Sambuc #endif
283ebfedea0SLionel Sambuc
284ebfedea0SLionel Sambuc #ifdef OPENSSL_SYS_VMS
285ebfedea0SLionel Sambuc static struct IOSB iosb;
286ebfedea0SLionel Sambuc static $DESCRIPTOR(terminal, "TT");
287*0a6a1f1dSLionel Sambuc static long tty_orig[3], tty_new[3]; /* XXX Is there any guarantee that this
288*0a6a1f1dSLionel Sambuc * will always suffice for the actual
289*0a6a1f1dSLionel Sambuc * structures? */
290ebfedea0SLionel Sambuc static long status;
291ebfedea0SLionel Sambuc static unsigned short channel = 0;
292ebfedea0SLionel Sambuc #else
293ebfedea0SLionel Sambuc # if !defined(OPENSSL_SYS_MSDOS) || defined(__DJGPP__)
294ebfedea0SLionel Sambuc static TTY_STRUCT tty_orig, tty_new;
295ebfedea0SLionel Sambuc # endif
296ebfedea0SLionel Sambuc #endif
297ebfedea0SLionel Sambuc static FILE *tty_in, *tty_out;
298ebfedea0SLionel Sambuc static int is_a_tty;
299ebfedea0SLionel Sambuc
300ebfedea0SLionel Sambuc /* Declare static functions */
301ebfedea0SLionel Sambuc #if !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE)
302ebfedea0SLionel Sambuc static int read_till_nl(FILE *);
303ebfedea0SLionel Sambuc static void recsig(int);
304ebfedea0SLionel Sambuc static void pushsig(void);
305ebfedea0SLionel Sambuc static void popsig(void);
306ebfedea0SLionel Sambuc #endif
307ebfedea0SLionel Sambuc #if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN16)
308ebfedea0SLionel Sambuc static int noecho_fgets(char *buf, int size, FILE *tty);
309ebfedea0SLionel Sambuc #endif
310ebfedea0SLionel Sambuc static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl);
311ebfedea0SLionel Sambuc
312ebfedea0SLionel Sambuc static int read_string(UI *ui, UI_STRING *uis);
313ebfedea0SLionel Sambuc static int write_string(UI *ui, UI_STRING *uis);
314ebfedea0SLionel Sambuc
315ebfedea0SLionel Sambuc static int open_console(UI *ui);
316ebfedea0SLionel Sambuc static int echo_console(UI *ui);
317ebfedea0SLionel Sambuc static int noecho_console(UI *ui);
318ebfedea0SLionel Sambuc static int close_console(UI *ui);
319ebfedea0SLionel Sambuc
320*0a6a1f1dSLionel Sambuc static UI_METHOD ui_openssl = {
321ebfedea0SLionel Sambuc "OpenSSL default user interface",
322ebfedea0SLionel Sambuc open_console,
323ebfedea0SLionel Sambuc write_string,
324ebfedea0SLionel Sambuc NULL, /* No flusher is needed for command lines */
325ebfedea0SLionel Sambuc read_string,
326ebfedea0SLionel Sambuc close_console,
327ebfedea0SLionel Sambuc NULL
328ebfedea0SLionel Sambuc };
329ebfedea0SLionel Sambuc
330ebfedea0SLionel Sambuc /* The method with all the built-in thingies */
UI_OpenSSL(void)331ebfedea0SLionel Sambuc UI_METHOD *UI_OpenSSL(void)
332ebfedea0SLionel Sambuc {
333ebfedea0SLionel Sambuc return &ui_openssl;
334ebfedea0SLionel Sambuc }
335ebfedea0SLionel Sambuc
336*0a6a1f1dSLionel Sambuc /*
337*0a6a1f1dSLionel Sambuc * The following function makes sure that info and error strings are printed
338*0a6a1f1dSLionel Sambuc * before any prompt.
339*0a6a1f1dSLionel Sambuc */
write_string(UI * ui,UI_STRING * uis)340ebfedea0SLionel Sambuc static int write_string(UI *ui, UI_STRING *uis)
341ebfedea0SLionel Sambuc {
342*0a6a1f1dSLionel Sambuc switch (UI_get_string_type(uis)) {
343ebfedea0SLionel Sambuc case UIT_ERROR:
344ebfedea0SLionel Sambuc case UIT_INFO:
345ebfedea0SLionel Sambuc fputs(UI_get0_output_string(uis), tty_out);
346ebfedea0SLionel Sambuc fflush(tty_out);
347ebfedea0SLionel Sambuc break;
348ebfedea0SLionel Sambuc default:
349ebfedea0SLionel Sambuc break;
350ebfedea0SLionel Sambuc }
351ebfedea0SLionel Sambuc return 1;
352ebfedea0SLionel Sambuc }
353ebfedea0SLionel Sambuc
read_string(UI * ui,UI_STRING * uis)354ebfedea0SLionel Sambuc static int read_string(UI *ui, UI_STRING *uis)
355ebfedea0SLionel Sambuc {
356ebfedea0SLionel Sambuc int ok = 0;
357ebfedea0SLionel Sambuc
358*0a6a1f1dSLionel Sambuc switch (UI_get_string_type(uis)) {
359ebfedea0SLionel Sambuc case UIT_BOOLEAN:
360ebfedea0SLionel Sambuc fputs(UI_get0_output_string(uis), tty_out);
361ebfedea0SLionel Sambuc fputs(UI_get0_action_string(uis), tty_out);
362ebfedea0SLionel Sambuc fflush(tty_out);
363ebfedea0SLionel Sambuc return read_string_inner(ui, uis,
364*0a6a1f1dSLionel Sambuc UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO,
365*0a6a1f1dSLionel Sambuc 0);
366ebfedea0SLionel Sambuc case UIT_PROMPT:
367ebfedea0SLionel Sambuc fputs(UI_get0_output_string(uis), tty_out);
368ebfedea0SLionel Sambuc fflush(tty_out);
369ebfedea0SLionel Sambuc return read_string_inner(ui, uis,
370*0a6a1f1dSLionel Sambuc UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO,
371*0a6a1f1dSLionel Sambuc 1);
372ebfedea0SLionel Sambuc case UIT_VERIFY:
373*0a6a1f1dSLionel Sambuc fprintf(tty_out, "Verifying - %s", UI_get0_output_string(uis));
374ebfedea0SLionel Sambuc fflush(tty_out);
375ebfedea0SLionel Sambuc if ((ok = read_string_inner(ui, uis,
376*0a6a1f1dSLionel Sambuc UI_get_input_flags(uis) &
377*0a6a1f1dSLionel Sambuc UI_INPUT_FLAG_ECHO, 1)) <= 0)
378ebfedea0SLionel Sambuc return ok;
379*0a6a1f1dSLionel Sambuc if (strcmp(UI_get0_result_string(uis), UI_get0_test_string(uis)) != 0) {
380ebfedea0SLionel Sambuc fprintf(tty_out, "Verify failure\n");
381ebfedea0SLionel Sambuc fflush(tty_out);
382ebfedea0SLionel Sambuc return 0;
383ebfedea0SLionel Sambuc }
384ebfedea0SLionel Sambuc break;
385ebfedea0SLionel Sambuc default:
386ebfedea0SLionel Sambuc break;
387ebfedea0SLionel Sambuc }
388ebfedea0SLionel Sambuc return 1;
389ebfedea0SLionel Sambuc }
390ebfedea0SLionel Sambuc
391ebfedea0SLionel Sambuc #if !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE)
392ebfedea0SLionel Sambuc /* Internal functions to read a string without echoing */
read_till_nl(FILE * in)393ebfedea0SLionel Sambuc static int read_till_nl(FILE *in)
394ebfedea0SLionel Sambuc {
395ebfedea0SLionel Sambuc # define SIZE 4
396ebfedea0SLionel Sambuc char buf[SIZE + 1];
397ebfedea0SLionel Sambuc
398ebfedea0SLionel Sambuc do {
399ebfedea0SLionel Sambuc if (!fgets(buf, SIZE, in))
400ebfedea0SLionel Sambuc return 0;
401ebfedea0SLionel Sambuc } while (strchr(buf, '\n') == NULL);
402ebfedea0SLionel Sambuc return 1;
403ebfedea0SLionel Sambuc }
404ebfedea0SLionel Sambuc
405ebfedea0SLionel Sambuc static volatile sig_atomic_t intr_signal;
406ebfedea0SLionel Sambuc #endif
407ebfedea0SLionel Sambuc
read_string_inner(UI * ui,UI_STRING * uis,int echo,int strip_nl)408ebfedea0SLionel Sambuc static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl)
409ebfedea0SLionel Sambuc {
410ebfedea0SLionel Sambuc static int ps;
411ebfedea0SLionel Sambuc int ok;
412ebfedea0SLionel Sambuc char result[BUFSIZ];
413ebfedea0SLionel Sambuc int maxsize = BUFSIZ - 1;
414ebfedea0SLionel Sambuc #if !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE)
415ebfedea0SLionel Sambuc char *p;
416ebfedea0SLionel Sambuc
417ebfedea0SLionel Sambuc intr_signal = 0;
418ebfedea0SLionel Sambuc ok = 0;
419ebfedea0SLionel Sambuc ps = 0;
420ebfedea0SLionel Sambuc
421ebfedea0SLionel Sambuc pushsig();
422ebfedea0SLionel Sambuc ps = 1;
423ebfedea0SLionel Sambuc
424ebfedea0SLionel Sambuc if (!echo && !noecho_console(ui))
425ebfedea0SLionel Sambuc goto error;
426ebfedea0SLionel Sambuc ps = 2;
427ebfedea0SLionel Sambuc
428ebfedea0SLionel Sambuc result[0] = '\0';
429ebfedea0SLionel Sambuc # ifdef OPENSSL_SYS_MSDOS
430*0a6a1f1dSLionel Sambuc if (!echo) {
431ebfedea0SLionel Sambuc noecho_fgets(result, maxsize, tty_in);
432ebfedea0SLionel Sambuc p = result; /* FIXME: noecho_fgets doesn't return errors */
433*0a6a1f1dSLionel Sambuc } else
434ebfedea0SLionel Sambuc p = fgets(result, maxsize, tty_in);
435ebfedea0SLionel Sambuc # else
436ebfedea0SLionel Sambuc p = fgets(result, maxsize, tty_in);
437ebfedea0SLionel Sambuc # endif
438ebfedea0SLionel Sambuc if (!p)
439ebfedea0SLionel Sambuc goto error;
440*0a6a1f1dSLionel Sambuc if (feof(tty_in))
441*0a6a1f1dSLionel Sambuc goto error;
442*0a6a1f1dSLionel Sambuc if (ferror(tty_in))
443*0a6a1f1dSLionel Sambuc goto error;
444*0a6a1f1dSLionel Sambuc if ((p = (char *)strchr(result, '\n')) != NULL) {
445ebfedea0SLionel Sambuc if (strip_nl)
446ebfedea0SLionel Sambuc *p = '\0';
447*0a6a1f1dSLionel Sambuc } else if (!read_till_nl(tty_in))
448ebfedea0SLionel Sambuc goto error;
449ebfedea0SLionel Sambuc if (UI_set_result(ui, uis, result) >= 0)
450ebfedea0SLionel Sambuc ok = 1;
451ebfedea0SLionel Sambuc
452ebfedea0SLionel Sambuc error:
453ebfedea0SLionel Sambuc if (intr_signal == SIGINT)
454ebfedea0SLionel Sambuc ok = -1;
455*0a6a1f1dSLionel Sambuc if (!echo)
456*0a6a1f1dSLionel Sambuc fprintf(tty_out, "\n");
457ebfedea0SLionel Sambuc if (ps >= 2 && !echo && !echo_console(ui))
458ebfedea0SLionel Sambuc ok = 0;
459ebfedea0SLionel Sambuc
460ebfedea0SLionel Sambuc if (ps >= 1)
461ebfedea0SLionel Sambuc popsig();
462ebfedea0SLionel Sambuc #else
463ebfedea0SLionel Sambuc ok = 1;
464ebfedea0SLionel Sambuc #endif
465ebfedea0SLionel Sambuc
466ebfedea0SLionel Sambuc OPENSSL_cleanse(result, BUFSIZ);
467ebfedea0SLionel Sambuc return ok;
468ebfedea0SLionel Sambuc }
469ebfedea0SLionel Sambuc
470ebfedea0SLionel Sambuc /* Internal functions to open, handle and close a channel to the console. */
open_console(UI * ui)471ebfedea0SLionel Sambuc static int open_console(UI *ui)
472ebfedea0SLionel Sambuc {
473ebfedea0SLionel Sambuc CRYPTO_w_lock(CRYPTO_LOCK_UI);
474ebfedea0SLionel Sambuc is_a_tty = 1;
475ebfedea0SLionel Sambuc
476ebfedea0SLionel Sambuc #if defined(OPENSSL_SYS_MACINTOSH_CLASSIC) || defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_NETWARE) || defined(OPENSSL_SYS_BEOS)
477ebfedea0SLionel Sambuc tty_in = stdin;
478ebfedea0SLionel Sambuc tty_out = stderr;
479ebfedea0SLionel Sambuc #else
480ebfedea0SLionel Sambuc # ifdef OPENSSL_SYS_MSDOS
481ebfedea0SLionel Sambuc # define DEV_TTY "con"
482ebfedea0SLionel Sambuc # else
483ebfedea0SLionel Sambuc # define DEV_TTY "/dev/tty"
484ebfedea0SLionel Sambuc # endif
485ebfedea0SLionel Sambuc if ((tty_in = fopen(DEV_TTY, "r")) == NULL)
486ebfedea0SLionel Sambuc tty_in = stdin;
487ebfedea0SLionel Sambuc if ((tty_out = fopen(DEV_TTY, "w")) == NULL)
488ebfedea0SLionel Sambuc tty_out = stderr;
489ebfedea0SLionel Sambuc #endif
490ebfedea0SLionel Sambuc
491ebfedea0SLionel Sambuc #if defined(TTY_get) && !defined(OPENSSL_SYS_VMS)
492*0a6a1f1dSLionel Sambuc if (TTY_get(fileno(tty_in), &tty_orig) == -1) {
493ebfedea0SLionel Sambuc # ifdef ENOTTY
494ebfedea0SLionel Sambuc if (errno == ENOTTY)
495ebfedea0SLionel Sambuc is_a_tty = 0;
496ebfedea0SLionel Sambuc else
497ebfedea0SLionel Sambuc # endif
498ebfedea0SLionel Sambuc # ifdef EINVAL
499*0a6a1f1dSLionel Sambuc /*
500*0a6a1f1dSLionel Sambuc * Ariel Glenn ariel@columbia.edu reports that solaris can return
501*0a6a1f1dSLionel Sambuc * EINVAL instead. This should be ok
502*0a6a1f1dSLionel Sambuc */
503ebfedea0SLionel Sambuc if (errno == EINVAL)
504ebfedea0SLionel Sambuc is_a_tty = 0;
505ebfedea0SLionel Sambuc else
506ebfedea0SLionel Sambuc # endif
507ebfedea0SLionel Sambuc return 0;
508ebfedea0SLionel Sambuc }
509ebfedea0SLionel Sambuc #endif
510ebfedea0SLionel Sambuc #ifdef OPENSSL_SYS_VMS
511ebfedea0SLionel Sambuc status = sys$assign(&terminal, &channel, 0, 0);
512ebfedea0SLionel Sambuc if (status != SS$_NORMAL)
513ebfedea0SLionel Sambuc return 0;
514*0a6a1f1dSLionel Sambuc status =
515*0a6a1f1dSLionel Sambuc sys$qiow(0, channel, IO$_SENSEMODE, &iosb, 0, 0, tty_orig, 12, 0, 0,
516*0a6a1f1dSLionel Sambuc 0, 0);
517ebfedea0SLionel Sambuc if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
518ebfedea0SLionel Sambuc return 0;
519ebfedea0SLionel Sambuc #endif
520ebfedea0SLionel Sambuc return 1;
521ebfedea0SLionel Sambuc }
522ebfedea0SLionel Sambuc
noecho_console(UI * ui)523ebfedea0SLionel Sambuc static int noecho_console(UI *ui)
524ebfedea0SLionel Sambuc {
525ebfedea0SLionel Sambuc #ifdef TTY_FLAGS
526ebfedea0SLionel Sambuc memcpy(&(tty_new), &(tty_orig), sizeof(tty_orig));
527ebfedea0SLionel Sambuc tty_new.TTY_FLAGS &= ~ECHO;
528ebfedea0SLionel Sambuc #endif
529ebfedea0SLionel Sambuc
530ebfedea0SLionel Sambuc #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
531ebfedea0SLionel Sambuc if (is_a_tty && (TTY_set(fileno(tty_in), &tty_new) == -1))
532ebfedea0SLionel Sambuc return 0;
533ebfedea0SLionel Sambuc #endif
534ebfedea0SLionel Sambuc #ifdef OPENSSL_SYS_VMS
535ebfedea0SLionel Sambuc tty_new[0] = tty_orig[0];
536ebfedea0SLionel Sambuc tty_new[1] = tty_orig[1] | TT$M_NOECHO;
537ebfedea0SLionel Sambuc tty_new[2] = tty_orig[2];
538*0a6a1f1dSLionel Sambuc status =
539*0a6a1f1dSLionel Sambuc sys$qiow(0, channel, IO$_SETMODE, &iosb, 0, 0, tty_new, 12, 0, 0, 0,
540*0a6a1f1dSLionel Sambuc 0);
541ebfedea0SLionel Sambuc if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
542ebfedea0SLionel Sambuc return 0;
543ebfedea0SLionel Sambuc #endif
544ebfedea0SLionel Sambuc return 1;
545ebfedea0SLionel Sambuc }
546ebfedea0SLionel Sambuc
echo_console(UI * ui)547ebfedea0SLionel Sambuc static int echo_console(UI *ui)
548ebfedea0SLionel Sambuc {
549ebfedea0SLionel Sambuc #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
550ebfedea0SLionel Sambuc memcpy(&(tty_new), &(tty_orig), sizeof(tty_orig));
551ebfedea0SLionel Sambuc tty_new.TTY_FLAGS |= ECHO;
552ebfedea0SLionel Sambuc #endif
553ebfedea0SLionel Sambuc
554ebfedea0SLionel Sambuc #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
555ebfedea0SLionel Sambuc if (is_a_tty && (TTY_set(fileno(tty_in), &tty_new) == -1))
556ebfedea0SLionel Sambuc return 0;
557ebfedea0SLionel Sambuc #endif
558ebfedea0SLionel Sambuc #ifdef OPENSSL_SYS_VMS
559ebfedea0SLionel Sambuc tty_new[0] = tty_orig[0];
560ebfedea0SLionel Sambuc tty_new[1] = tty_orig[1] & ~TT$M_NOECHO;
561ebfedea0SLionel Sambuc tty_new[2] = tty_orig[2];
562*0a6a1f1dSLionel Sambuc status =
563*0a6a1f1dSLionel Sambuc sys$qiow(0, channel, IO$_SETMODE, &iosb, 0, 0, tty_new, 12, 0, 0, 0,
564*0a6a1f1dSLionel Sambuc 0);
565ebfedea0SLionel Sambuc if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
566ebfedea0SLionel Sambuc return 0;
567ebfedea0SLionel Sambuc #endif
568ebfedea0SLionel Sambuc return 1;
569ebfedea0SLionel Sambuc }
570ebfedea0SLionel Sambuc
close_console(UI * ui)571ebfedea0SLionel Sambuc static int close_console(UI *ui)
572ebfedea0SLionel Sambuc {
573*0a6a1f1dSLionel Sambuc if (tty_in != stdin)
574*0a6a1f1dSLionel Sambuc fclose(tty_in);
575*0a6a1f1dSLionel Sambuc if (tty_out != stderr)
576*0a6a1f1dSLionel Sambuc fclose(tty_out);
577ebfedea0SLionel Sambuc #ifdef OPENSSL_SYS_VMS
578ebfedea0SLionel Sambuc status = sys$dassgn(channel);
579ebfedea0SLionel Sambuc #endif
580ebfedea0SLionel Sambuc CRYPTO_w_unlock(CRYPTO_LOCK_UI);
581ebfedea0SLionel Sambuc
582ebfedea0SLionel Sambuc return 1;
583ebfedea0SLionel Sambuc }
584ebfedea0SLionel Sambuc
585ebfedea0SLionel Sambuc #if !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE)
586ebfedea0SLionel Sambuc /* Internal functions to handle signals and act on them */
pushsig(void)587ebfedea0SLionel Sambuc static void pushsig(void)
588ebfedea0SLionel Sambuc {
589ebfedea0SLionel Sambuc # ifndef OPENSSL_SYS_WIN32
590ebfedea0SLionel Sambuc int i;
591ebfedea0SLionel Sambuc # endif
592ebfedea0SLionel Sambuc # ifdef SIGACTION
593ebfedea0SLionel Sambuc struct sigaction sa;
594ebfedea0SLionel Sambuc
595ebfedea0SLionel Sambuc memset(&sa, 0, sizeof sa);
596ebfedea0SLionel Sambuc sa.sa_handler = recsig;
597ebfedea0SLionel Sambuc # endif
598ebfedea0SLionel Sambuc
599ebfedea0SLionel Sambuc # ifdef OPENSSL_SYS_WIN32
600ebfedea0SLionel Sambuc savsig[SIGABRT] = signal(SIGABRT, recsig);
601ebfedea0SLionel Sambuc savsig[SIGFPE] = signal(SIGFPE, recsig);
602ebfedea0SLionel Sambuc savsig[SIGILL] = signal(SIGILL, recsig);
603ebfedea0SLionel Sambuc savsig[SIGINT] = signal(SIGINT, recsig);
604ebfedea0SLionel Sambuc savsig[SIGSEGV] = signal(SIGSEGV, recsig);
605ebfedea0SLionel Sambuc savsig[SIGTERM] = signal(SIGTERM, recsig);
606ebfedea0SLionel Sambuc # else
607*0a6a1f1dSLionel Sambuc for (i = 1; i < NX509_SIG; i++) {
608ebfedea0SLionel Sambuc # ifdef SIGUSR1
609ebfedea0SLionel Sambuc if (i == SIGUSR1)
610ebfedea0SLionel Sambuc continue;
611ebfedea0SLionel Sambuc # endif
612ebfedea0SLionel Sambuc # ifdef SIGUSR2
613ebfedea0SLionel Sambuc if (i == SIGUSR2)
614ebfedea0SLionel Sambuc continue;
615ebfedea0SLionel Sambuc # endif
616ebfedea0SLionel Sambuc # ifdef SIGKILL
617ebfedea0SLionel Sambuc if (i == SIGKILL) /* We can't make any action on that. */
618ebfedea0SLionel Sambuc continue;
619ebfedea0SLionel Sambuc # endif
620ebfedea0SLionel Sambuc # ifdef SIGACTION
621ebfedea0SLionel Sambuc sigaction(i, &sa, &savsig[i]);
622ebfedea0SLionel Sambuc # else
623ebfedea0SLionel Sambuc savsig[i] = signal(i, recsig);
624ebfedea0SLionel Sambuc # endif
625ebfedea0SLionel Sambuc }
626ebfedea0SLionel Sambuc # endif
627ebfedea0SLionel Sambuc
628ebfedea0SLionel Sambuc # ifdef SIGWINCH
629ebfedea0SLionel Sambuc signal(SIGWINCH, SIG_DFL);
630ebfedea0SLionel Sambuc # endif
631ebfedea0SLionel Sambuc }
632ebfedea0SLionel Sambuc
popsig(void)633ebfedea0SLionel Sambuc static void popsig(void)
634ebfedea0SLionel Sambuc {
635ebfedea0SLionel Sambuc # ifdef OPENSSL_SYS_WIN32
636ebfedea0SLionel Sambuc signal(SIGABRT, savsig[SIGABRT]);
637ebfedea0SLionel Sambuc signal(SIGFPE, savsig[SIGFPE]);
638ebfedea0SLionel Sambuc signal(SIGILL, savsig[SIGILL]);
639ebfedea0SLionel Sambuc signal(SIGINT, savsig[SIGINT]);
640ebfedea0SLionel Sambuc signal(SIGSEGV, savsig[SIGSEGV]);
641ebfedea0SLionel Sambuc signal(SIGTERM, savsig[SIGTERM]);
642ebfedea0SLionel Sambuc # else
643ebfedea0SLionel Sambuc int i;
644*0a6a1f1dSLionel Sambuc for (i = 1; i < NX509_SIG; i++) {
645ebfedea0SLionel Sambuc # ifdef SIGUSR1
646ebfedea0SLionel Sambuc if (i == SIGUSR1)
647ebfedea0SLionel Sambuc continue;
648ebfedea0SLionel Sambuc # endif
649ebfedea0SLionel Sambuc # ifdef SIGUSR2
650ebfedea0SLionel Sambuc if (i == SIGUSR2)
651ebfedea0SLionel Sambuc continue;
652ebfedea0SLionel Sambuc # endif
653ebfedea0SLionel Sambuc # ifdef SIGACTION
654ebfedea0SLionel Sambuc sigaction(i, &savsig[i], NULL);
655ebfedea0SLionel Sambuc # else
656ebfedea0SLionel Sambuc signal(i, savsig[i]);
657ebfedea0SLionel Sambuc # endif
658ebfedea0SLionel Sambuc }
659ebfedea0SLionel Sambuc # endif
660ebfedea0SLionel Sambuc }
661ebfedea0SLionel Sambuc
recsig(int i)662ebfedea0SLionel Sambuc static void recsig(int i)
663ebfedea0SLionel Sambuc {
664ebfedea0SLionel Sambuc intr_signal = i;
665ebfedea0SLionel Sambuc }
666ebfedea0SLionel Sambuc #endif
667ebfedea0SLionel Sambuc
668ebfedea0SLionel Sambuc /* Internal functions specific for Windows */
669ebfedea0SLionel Sambuc #if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE)
noecho_fgets(char * buf,int size,FILE * tty)670ebfedea0SLionel Sambuc static int noecho_fgets(char *buf, int size, FILE *tty)
671ebfedea0SLionel Sambuc {
672ebfedea0SLionel Sambuc int i;
673ebfedea0SLionel Sambuc char *p;
674ebfedea0SLionel Sambuc
675ebfedea0SLionel Sambuc p = buf;
676*0a6a1f1dSLionel Sambuc for (;;) {
677*0a6a1f1dSLionel Sambuc if (size == 0) {
678ebfedea0SLionel Sambuc *p = '\0';
679ebfedea0SLionel Sambuc break;
680ebfedea0SLionel Sambuc }
681ebfedea0SLionel Sambuc size--;
682ebfedea0SLionel Sambuc # ifdef WIN16TTY
683ebfedea0SLionel Sambuc i = _inchar();
684ebfedea0SLionel Sambuc # elif defined(_WIN32)
685ebfedea0SLionel Sambuc i = _getch();
686ebfedea0SLionel Sambuc # else
687ebfedea0SLionel Sambuc i = getch();
688ebfedea0SLionel Sambuc # endif
689*0a6a1f1dSLionel Sambuc if (i == '\r')
690*0a6a1f1dSLionel Sambuc i = '\n';
691ebfedea0SLionel Sambuc *(p++) = i;
692*0a6a1f1dSLionel Sambuc if (i == '\n') {
693ebfedea0SLionel Sambuc *p = '\0';
694ebfedea0SLionel Sambuc break;
695ebfedea0SLionel Sambuc }
696ebfedea0SLionel Sambuc }
697ebfedea0SLionel Sambuc # ifdef WIN_CONSOLE_BUG
698*0a6a1f1dSLionel Sambuc /*
699*0a6a1f1dSLionel Sambuc * Win95 has several evil console bugs: one of these is that the last
700*0a6a1f1dSLionel Sambuc * character read using getch() is passed to the next read: this is
701ebfedea0SLionel Sambuc * usually a CR so this can be trouble. No STDIO fix seems to work but
702ebfedea0SLionel Sambuc * flushing the console appears to do the trick.
703ebfedea0SLionel Sambuc */
704ebfedea0SLionel Sambuc {
705ebfedea0SLionel Sambuc HANDLE inh;
706ebfedea0SLionel Sambuc inh = GetStdHandle(STD_INPUT_HANDLE);
707ebfedea0SLionel Sambuc FlushConsoleInputBuffer(inh);
708ebfedea0SLionel Sambuc }
709ebfedea0SLionel Sambuc # endif
710ebfedea0SLionel Sambuc return (strlen(buf));
711ebfedea0SLionel Sambuc }
712ebfedea0SLionel Sambuc #endif
713