interact.c 4.37 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/* interact.c
 *
 * Interact with the user.
 *
 * $Id$ */

/* lsh, an implementation of the ssh protocol
 *
 * Copyright (C) 1999 Niels Mller
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "interact.h"

#include "io.h"
#include "werror.h"
#include "xalloc.h"

#include <string.h>

#if HAVE_UNISTD_H
#include <unistd.h>
#endif

#include <errno.h>
#include <fcntl.h>

41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#if 0
/* GABA:
   (class
     (name interact_unix)
     (super abstract_interact)
     (vars
       (tty_fd . int)
       (dims . "struct terminal_dimensions")
       (changed . sig_atomic_t)))
*/

static int
unix_is_tty(struct abstract_interact *s)
{
  CAST(interact_unix, self, s);

  return self->tty_fd >= 0;
}


/* FIXME: Rewrite to operate on tty_fd. */
static struct lsh_string *
unix_read_password(struct abstract_interact *s UNUSED,
		   int max_length UNUSED,
		   struct lsh_string *prompt, int free)
{
  /* NOTE: Ignores max_length; instead getpass()'s limit applies. */

  char *password;
  
  prompt = make_cstring(prompt, free);

  if (!prompt)
    return 0;

  /* NOTE: This function uses a static buffer. */
  password = getpass(prompt->data);

  lsh_string_free(prompt);
  
  if (!password)
    return 0;

  return format_cstring(password);
}

static int
unix_yes_or_no(struct abstract_interact *s,
	       struct lsh_string *s, int def, int free)
{
  CAST(interact_unix, self, s);
  UINT8 buffer[TTY_BUFSIZE];
  const struct exception *e;
  
  if ( (self->tty_fd < 0) || quiet_flag)
    {
      if (free)
	lsh_string_free(s);
      return def;
    }
  
  e = write_raw(self->tty_fd, s->length, s->data);

  if (free)
    lsh_string_free(s);

  if (e)
    return def;

  if (!tty_read_line(TTY_BUFSIZE, buffer))
    return def;

  switch (buffer[0])
    {
    case 'y':
    case 'Y':
      return 1;
    default:
      return 0;
    }
}	       

#endif
Niels Möller's avatar
Niels Möller committed
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242

int tty_fd = -1;

int lsh_open_tty(void)
{
#if HAVE_STDTTY_FILENO

  tty_fd = STDTTY_FILENO;
  return 1;

#else /* !HAVE_STDTTY_FILENO */

  int fd = open("/dev/tty", O_RDWR);
  if (fd < 0)
    return 0;
  tty_fd = fd;
  return 1;
  
#endif /* !HAVE_STDTTY_FILENO */
}

/* Depends on the tty being line buffered */
int tty_read_line(UINT32 size, UINT8 *buffer)
{
  UINT32 i = 0;

  while (i < size)
    {
      int res = read(tty_fd, buffer + i, size - i);
      if (!res)
	/* User pressed EOF (^D) */
	return i;
      else if (res < 0)
	switch(errno)
	  {
	  case EAGAIN:
	  case EINTR:
	    break;
	  default:
	    /* I/O error */
	    werror("tty_read_line: %z (errno = %i)\n",
		   errno, STRERROR(errno));
	    return 0;
	  }
      else
	{
	  UINT32 j;
	  for (j = 0; j < (unsigned) res; j++, i++)
	    if (buffer[i] == '\n')
	      return i;
	}
    }
  /* We have filled our buffer already; continue reading until end of line */
#define BUFSIZE 512
  for (;;)
    {
      UINT8 b[BUFSIZE];
      int res = read(tty_fd, b, BUFSIZE);
      if (!res)
	/* EOF */
	return size;
      else if (res < 0)
	switch(errno)
	  {
	  case EAGAIN:
	  case EINTR:
	    break;
	  default:
	    /* I/O error */
	    werror("tty_read_line: %z (errno = %i)\n",
		   errno, strerror(errno));
	    return 0;
	  }
      else
	{
	  UINT32 j;
	  for (j = 0; j < (unsigned) res; j++)
	    if (b[j] == '\n')
	      return res;
	}
    }
#undef BUFSIZE
}

#define TTY_BUFSIZE 10

int yes_or_no(struct lsh_string *s, int def, int free)
{
  UINT8 buffer[TTY_BUFSIZE];
  const struct exception *e;
  
  if ( (tty_fd < 0) || quiet_flag)
    {
      if (free)
	lsh_string_free(s);
      return def;
    }

  e = write_raw(tty_fd, s->length, s->data);

  if (free)
    lsh_string_free(s);

  if (e)
    return def;

  if (!tty_read_line(TTY_BUFSIZE, buffer))
    return def;

  switch (buffer[0])
    {
    case 'y':
    case 'Y':
      return 1;
    default:
      return 0;
    }
}