~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to src/port/sprompt.c

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*-------------------------------------------------------------------------
 
2
 *
 
3
 * sprompt.c
 
4
 *        simple_prompt() routine
 
5
 *
 
6
 * Portions Copyright (c) 1996-2005, 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.10 2004-12-31 22:03:53 pgsql Exp $
 
12
 *
 
13
 *-------------------------------------------------------------------------
 
14
 */
 
15
 
 
16
 
 
17
/*
 
18
 * simple_prompt
 
19
 *
 
20
 * Generalized function especially intended for reading in usernames and
 
21
 * password interactively. Reads from /dev/tty or stdin/stderr.
 
22
 *
 
23
 * prompt:              The prompt to print
 
24
 * maxlen:              How many characters to accept
 
25
 * echo:                Set to false if you want to hide what is entered (for passwords)
 
26
 *
 
27
 * Returns a malloc()'ed string with the input (w/o trailing newline).
 
28
 */
 
29
#include "c.h"
 
30
 
 
31
#ifdef HAVE_TERMIOS_H
 
32
#include <termios.h>
 
33
#endif
 
34
 
 
35
bool            prompt_state = false;
 
36
extern char *simple_prompt(const char *prompt, int maxlen, bool echo);
 
37
 
 
38
char *
 
39
simple_prompt(const char *prompt, int maxlen, bool echo)
 
40
{
 
41
        int                     length;
 
42
        char       *destination;
 
43
        FILE       *termin,
 
44
                           *termout;
 
45
 
 
46
#ifdef HAVE_TERMIOS_H
 
47
        struct termios t_orig,
 
48
                                t;
 
49
 
 
50
#else
 
51
#ifdef WIN32
 
52
        HANDLE          t = NULL;
 
53
        LPDWORD         t_orig = NULL;
 
54
#endif
 
55
#endif
 
56
 
 
57
        destination = (char *) malloc(maxlen + 1);
 
58
        if (!destination)
 
59
                return NULL;
 
60
 
 
61
        prompt_state = true;            /* disable SIGINT */
 
62
 
 
63
        /*
 
64
         * Do not try to collapse these into one "w+" mode file. Doesn't work
 
65
         * on some platforms (eg, HPUX 10.20).
 
66
         */
 
67
        termin = fopen("/dev/tty", "r");
 
68
        termout = fopen("/dev/tty", "w");
 
69
        if (!termin || !termout)
 
70
        {
 
71
                if (termin)
 
72
                        fclose(termin);
 
73
                if (termout)
 
74
                        fclose(termout);
 
75
                termin = stdin;
 
76
                termout = stderr;
 
77
        }
 
78
 
 
79
#ifdef HAVE_TERMIOS_H
 
80
        if (!echo)
 
81
        {
 
82
                tcgetattr(fileno(termin), &t);
 
83
                t_orig = t;
 
84
                t.c_lflag &= ~ECHO;
 
85
                tcsetattr(fileno(termin), TCSAFLUSH, &t);
 
86
        }
 
87
#else
 
88
#ifdef WIN32
 
89
        if (!echo)
 
90
        {
 
91
                /* get a new handle to turn echo off */
 
92
                t_orig = (LPDWORD) malloc(sizeof(DWORD));
 
93
                t = GetStdHandle(STD_INPUT_HANDLE);
 
94
 
 
95
                /* save the old configuration first */
 
96
                GetConsoleMode(t, t_orig);
 
97
 
 
98
                /* set to the new mode */
 
99
                SetConsoleMode(t, ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT);
 
100
        }
 
101
#endif
 
102
#endif
 
103
 
 
104
        if (prompt)
 
105
        {
 
106
                fputs(gettext(prompt), termout);
 
107
                fflush(termout);
 
108
        }
 
109
 
 
110
        if (fgets(destination, maxlen + 1, termin) == NULL)
 
111
                destination[0] = '\0';
 
112
 
 
113
        length = strlen(destination);
 
114
        if (length > 0 && destination[length - 1] != '\n')
 
115
        {
 
116
                /* eat rest of the line */
 
117
                char            buf[128];
 
118
                int                     buflen;
 
119
 
 
120
                do
 
121
                {
 
122
                        if (fgets(buf, sizeof(buf), termin) == NULL)
 
123
                                break;
 
124
                        buflen = strlen(buf);
 
125
                } while (buflen > 0 && buf[buflen - 1] != '\n');
 
126
        }
 
127
 
 
128
        if (length > 0 && destination[length - 1] == '\n')
 
129
                /* remove trailing newline */
 
130
                destination[length - 1] = '\0';
 
131
 
 
132
#ifdef HAVE_TERMIOS_H
 
133
        if (!echo)
 
134
        {
 
135
                tcsetattr(fileno(termin), TCSAFLUSH, &t_orig);
 
136
                fputs("\n", termout);
 
137
                fflush(termout);
 
138
        }
 
139
#else
 
140
#ifdef WIN32
 
141
        if (!echo)
 
142
        {
 
143
                /* reset to the original console mode */
 
144
                SetConsoleMode(t, *t_orig);
 
145
                fputs("\n", termout);
 
146
                fflush(termout);
 
147
                free(t_orig);
 
148
        }
 
149
#endif
 
150
#endif
 
151
 
 
152
        if (termin != stdin)
 
153
        {
 
154
                fclose(termin);
 
155
                fclose(termout);
 
156
        }
 
157
 
 
158
        prompt_state = false;           /* SIGINT okay again */
 
159
 
 
160
        return destination;
 
161
}