source: subversion/applications/utils/export/osm2pgsql/sprompt.c @ 26588

Last change on this file since 26588 was 7976, checked in by martinvoosterhout, 12 years ago

Add some changes from Edgemaster for MinGW support, see #926

File size: 4.6 KB
Line 
1/*-------------------------------------------------------------------------
2 *
3 * sprompt.c
4 *        simple_prompt() routine
5 *
6 * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 *
10 * IDENTIFICATION
11 *        $PostgreSQL: pgsql/src/port/sprompt.c,v 1.18 2006/10/04 00:30:14 momjian Exp $
12 *
13 *-------------------------------------------------------------------------
14 *
15 * PostgreSQL Database Management System
16 * (formerly known as Postgres, then as Postgres95)
17 *
18 * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
19 *
20 * Portions Copyright (c) 1994, The Regents of the University of California
21 *
22 * Permission to use, copy, modify, and distribute this software and its
23 * documentation for any purpose, without fee, and without a written agreement
24 * is hereby granted, provided that the above copyright notice and this
25 * paragraph and the following two paragraphs appear in all copies.
26 *
27 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
28 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
29 * LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
30 * DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 *
33 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
34 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
35 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
36 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO
37 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
38 *
39 */
40
41
42/*
43 * simple_prompt
44 *
45 * Generalized function especially intended for reading in usernames and
46 * password interactively. Reads from /dev/tty or stdin/stderr.
47 *
48 * prompt:              The prompt to print
49 * maxlen:              How many characters to accept
50 * echo:                Set to false if you want to hide what is entered (for passwords)
51 *
52 * Returns a malloc()'ed string with the input (w/o trailing newline).
53 */
54
55#define DEVTTY "/dev/tty"
56
57#include <stdio.h>
58#include <unistd.h>
59#include <stdlib.h>
60#include <string.h>
61#include <assert.h>
62
63#include <libpq-fe.h>
64
65#ifdef __MINGW_H
66# include <windows.h>
67#else
68# define HAVE_TERMIOS_H
69# include <termios.h>
70#endif
71
72extern char *simple_prompt(const char *prompt, int maxlen, int echo);
73
74char *
75simple_prompt(const char *prompt, int maxlen, int echo)
76{
77        int                     length;
78        char       *destination;
79        FILE       *termin,
80                           *termout;
81
82#ifdef HAVE_TERMIOS_H
83        struct termios t_orig,
84                                t;
85#else
86#ifdef WIN32
87        HANDLE          t = NULL;
88        LPDWORD         t_orig = NULL;
89#endif
90#endif
91
92        destination = (char *) malloc(maxlen + 1);
93        if (!destination)
94                return NULL;
95
96        /*
97         * Do not try to collapse these into one "w+" mode file. Doesn't work on
98         * some platforms (eg, HPUX 10.20).
99         */
100        termin = fopen(DEVTTY, "r");
101        termout = fopen(DEVTTY, "w");
102        if (!termin || !termout
103#ifdef WIN32
104        /* See DEVTTY comment for msys */
105                || (getenv("OSTYPE") && strcmp(getenv("OSTYPE"), "msys") == 0)
106#endif
107                )
108        {
109                if (termin)
110                        fclose(termin);
111                if (termout)
112                        fclose(termout);
113                termin = stdin;
114                termout = stderr;
115        }
116
117#ifdef HAVE_TERMIOS_H
118        if (!echo)
119        {
120                tcgetattr(fileno(termin), &t);
121                t_orig = t;
122                t.c_lflag &= ~ECHO;
123                tcsetattr(fileno(termin), TCSAFLUSH, &t);
124        }
125#else
126#ifdef WIN32
127        if (!echo)
128        {
129                /* get a new handle to turn echo off */
130                t_orig = (LPDWORD) malloc(sizeof(DWORD));
131                t = GetStdHandle(STD_INPUT_HANDLE);
132
133                /* save the old configuration first */
134                GetConsoleMode(t, t_orig);
135
136                /* set to the new mode */
137                SetConsoleMode(t, ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT);
138        }
139#endif
140#endif
141
142        if (prompt)
143        {
144                fputs(prompt, termout);
145                fflush(termout);
146        }
147
148        if (fgets(destination, maxlen + 1, termin) == NULL)
149                destination[0] = '\0';
150
151        length = strlen(destination);
152        if (length > 0 && destination[length - 1] != '\n')
153        {
154                /* eat rest of the line */
155                char            buf[128];
156                int                     buflen;
157
158                do
159                {
160                        if (fgets(buf, sizeof(buf), termin) == NULL)
161                                break;
162                        buflen = strlen(buf);
163                } while (buflen > 0 && buf[buflen - 1] != '\n');
164        }
165
166        if (length > 0 && destination[length - 1] == '\n')
167                /* remove trailing newline */
168                destination[length - 1] = '\0';
169
170#ifdef HAVE_TERMIOS_H
171        if (!echo)
172        {
173                tcsetattr(fileno(termin), TCSAFLUSH, &t_orig);
174                fputs("\n", termout);
175                fflush(termout);
176        }
177#else
178#ifdef WIN32
179        if (!echo)
180        {
181                /* reset to the original console mode */
182                SetConsoleMode(t, *t_orig);
183                fputs("\n", termout);
184                fflush(termout);
185                free(t_orig);
186        }
187#endif
188#endif
189
190        if (termin != stdin)
191        {
192                fclose(termin);
193                fclose(termout);
194        }
195
196        return destination;
197}
Note: See TracBrowser for help on using the repository browser.