~ubuntu-branches/ubuntu/trusty/bash/trusty-security

« back to all changes in this revision

Viewing changes to builtins/read.def

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2014-03-03 22:52:05 UTC
  • mfrom: (1.3.5) (2.2.6 experimental)
  • Revision ID: package-import@ubuntu.com-20140303225205-87ltrt5kspeq0g1b
Tags: 4.3-1ubuntu1
* Merge with Debian; remaining changes:
  - skel.bashrc:
    - Run lesspipe.
    - Enable ls aliases.
    - Set options in ll alias to -alF.
    - Define an alert alias.
    - Enabled colored grep aliases.
  - etc.bash.bashrc:
    - Add sudo hint.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
This file is read.def, from which is created read.c.
 
2
It implements the builtin "read" in Bash.
 
3
 
 
4
Copyright (C) 1987-2012 Free Software Foundation, Inc.
 
5
 
 
6
This file is part of GNU Bash, the Bourne Again SHell.
 
7
 
 
8
Bash is free software: you can redistribute it and/or modify
 
9
it under the terms of the GNU General Public License as published by
 
10
the Free Software Foundation, either version 3 of the License, or
 
11
(at your option) any later version.
 
12
 
 
13
Bash is distributed in the hope that it will be useful,
 
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
GNU General Public License for more details.
 
17
 
 
18
You should have received a copy of the GNU General Public License
 
19
along with Bash.  If not, see <http://www.gnu.org/licenses/>.
 
20
 
 
21
$PRODUCES read.c
 
22
 
 
23
$BUILTIN read
 
24
$FUNCTION read_builtin
 
25
$SHORT_DOC read [-ers] [-a array] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...]
 
26
Read a line from the standard input and split it into fields.
 
27
 
 
28
Reads a single line from the standard input, or from file descriptor FD
 
29
if the -u option is supplied.  The line is split into fields as with word
 
30
splitting, and the first word is assigned to the first NAME, the second
 
31
word to the second NAME, and so on, with any leftover words assigned to
 
32
the last NAME.  Only the characters found in $IFS are recognized as word
 
33
delimiters.
 
34
 
 
35
If no NAMEs are supplied, the line read is stored in the REPLY variable.
 
36
 
 
37
Options:
 
38
  -a array      assign the words read to sequential indices of the array
 
39
                variable ARRAY, starting at zero
 
40
  -d delim      continue until the first character of DELIM is read, rather
 
41
                than newline
 
42
  -e            use Readline to obtain the line in an interactive shell
 
43
  -i text       Use TEXT as the initial text for Readline
 
44
  -n nchars     return after reading NCHARS characters rather than waiting
 
45
                for a newline, but honor a delimiter if fewer than NCHARS
 
46
                characters are read before the delimiter
 
47
  -N nchars     return only after reading exactly NCHARS characters, unless
 
48
                EOF is encountered or read times out, ignoring any delimiter
 
49
  -p prompt     output the string PROMPT without a trailing newline before
 
50
                attempting to read
 
51
  -r            do not allow backslashes to escape any characters
 
52
  -s            do not echo input coming from a terminal
 
53
  -t timeout    time out and return failure if a complete line of input is
 
54
                not read within TIMEOUT seconds.  The value of the TMOUT
 
55
                variable is the default timeout.  TIMEOUT may be a
 
56
                fractional number.  If TIMEOUT is 0, read returns immediately,
 
57
                without trying to read any data, returning success only if
 
58
                input is available on the specified file descriptor.  The
 
59
                exit status is greater than 128 if the timeout is exceeded
 
60
  -u fd         read from file descriptor FD instead of the standard input
 
61
 
 
62
Exit Status:
 
63
The return code is zero, unless end-of-file is encountered, read times out
 
64
(in which case it's greater than 128), a variable assignment error occurs,
 
65
or an invalid file descriptor is supplied as the argument to -u.
 
66
$END
 
67
 
 
68
#include <config.h>
 
69
 
 
70
#include "bashtypes.h"
 
71
#include "posixstat.h"
 
72
 
 
73
#include <stdio.h>
 
74
 
 
75
#include "bashansi.h"
 
76
 
 
77
#if defined (HAVE_UNISTD_H)
 
78
#  include <unistd.h>
 
79
#endif
 
80
 
 
81
#include <signal.h>
 
82
#include <errno.h>
 
83
 
 
84
#ifdef __CYGWIN__
 
85
#  include <fcntl.h>
 
86
#  include <io.h>
 
87
#endif
 
88
 
 
89
#include "../bashintl.h"
 
90
 
 
91
#include "../shell.h"
 
92
#include "common.h"
 
93
#include "bashgetopt.h"
 
94
 
 
95
#include <shtty.h>
 
96
 
 
97
#if defined (READLINE)
 
98
#include "../bashline.h"
 
99
#include <readline/readline.h>
 
100
#endif
 
101
 
 
102
#if defined (BUFFERED_INPUT)
 
103
#  include "input.h"
 
104
#endif
 
105
 
 
106
#include "shmbutil.h"
 
107
 
 
108
#if !defined(errno)
 
109
extern int errno;
 
110
#endif
 
111
 
 
112
extern void run_pending_traps __P((void));
 
113
 
 
114
extern int posixly_correct;
 
115
extern int trapped_signal_received;
 
116
 
 
117
struct ttsave
 
118
{
 
119
  int fd;
 
120
  TTYSTRUCT *attrs;
 
121
};
 
122
 
 
123
#if defined (READLINE)
 
124
static void reset_attempted_completion_function __P((char *));
 
125
static int set_itext __P((void));
 
126
static char *edit_line __P((char *, char *));
 
127
static void set_eol_delim __P((int));
 
128
static void reset_eol_delim __P((char *));
 
129
#endif
 
130
static SHELL_VAR *bind_read_variable __P((char *, char *));
 
131
#if defined (HANDLE_MULTIBYTE)
 
132
static int read_mbchar __P((int, char *, int, int, int));
 
133
#endif
 
134
static void ttyrestore __P((struct ttsave *));
 
135
 
 
136
static sighandler sigalrm __P((int));
 
137
static void reset_alarm __P((void));
 
138
 
 
139
/* Try this to see what the rest of the shell can do with the information. */
 
140
procenv_t alrmbuf;
 
141
int sigalrm_seen;
 
142
 
 
143
static int reading;
 
144
static SigHandler *old_alrm;
 
145
static unsigned char delim;
 
146
 
 
147
/* In all cases, SIGALRM just sets a flag that we check periodically.  This
 
148
   avoids problems with the semi-tricky stuff we do with the xfree of
 
149
   input_string at the top of the unwind-protect list (see below). */
 
150
 
 
151
/* Set a flag that CHECK_ALRM can check.  This relies on zread calling
 
152
   trap.c:check_signals_and_traps(), which knows about sigalrm_seen and
 
153
   alrmbuf. */
 
154
static sighandler
 
155
sigalrm (s)
 
156
     int s;
 
157
{
 
158
  sigalrm_seen = 1;
 
159
}
 
160
 
 
161
static void
 
162
reset_alarm ()
 
163
{
 
164
  set_signal_handler (SIGALRM, old_alrm);
 
165
  falarm (0, 0);
 
166
}
 
167
 
 
168
/* Read the value of the shell variables whose names follow.
 
169
   The reading is done from the current input stream, whatever
 
170
   that may be.  Successive words of the input line are assigned
 
171
   to the variables mentioned in LIST.  The last variable in LIST
 
172
   gets the remainder of the words on the line.  If no variables
 
173
   are mentioned in LIST, then the default variable is $REPLY. */
 
174
int
 
175
read_builtin (list)
 
176
     WORD_LIST *list;
 
177
{
 
178
  register char *varname;
 
179
  int size, i, nr, pass_next, saw_escape, eof, opt, retval, code, print_ps2;
 
180
  int input_is_tty, input_is_pipe, unbuffered_read, skip_ctlesc, skip_ctlnul;
 
181
  int raw, edit, nchars, silent, have_timeout, ignore_delim, fd, lastsig, t_errno;
 
182
  unsigned int tmsec, tmusec;
 
183
  long ival, uval;
 
184
  intmax_t intval;
 
185
  char c;
 
186
  char *input_string, *orig_input_string, *ifs_chars, *prompt, *arrayname;
 
187
  char *e, *t, *t1, *ps2, *tofree;
 
188
  struct stat tsb;
 
189
  SHELL_VAR *var;
 
190
  TTYSTRUCT ttattrs, ttset;
 
191
  struct ttsave termsave;
 
192
#if defined (ARRAY_VARS)
 
193
  WORD_LIST *alist;
 
194
#endif
 
195
#if defined (READLINE)
 
196
  char *rlbuf, *itext;
 
197
  int rlind;
 
198
#endif
 
199
 
 
200
  USE_VAR(size);
 
201
  USE_VAR(i);
 
202
  USE_VAR(pass_next);
 
203
  USE_VAR(print_ps2);
 
204
  USE_VAR(saw_escape);
 
205
  USE_VAR(input_is_pipe);
 
206
/*  USE_VAR(raw); */
 
207
  USE_VAR(edit);
 
208
  USE_VAR(tmsec);
 
209
  USE_VAR(tmusec);
 
210
  USE_VAR(nchars);
 
211
  USE_VAR(silent);
 
212
  USE_VAR(ifs_chars);
 
213
  USE_VAR(prompt);
 
214
  USE_VAR(arrayname);
 
215
#if defined (READLINE)
 
216
  USE_VAR(rlbuf);
 
217
  USE_VAR(rlind);
 
218
  USE_VAR(itext);
 
219
#endif
 
220
  USE_VAR(list);
 
221
  USE_VAR(ps2);
 
222
  USE_VAR(lastsig);
 
223
 
 
224
  sigalrm_seen = reading = 0;
 
225
 
 
226
  i = 0;                /* Index into the string that we are reading. */
 
227
  raw = edit = 0;       /* Not reading raw input by default. */
 
228
  silent = 0;
 
229
  arrayname = prompt = (char *)NULL;
 
230
  fd = 0;               /* file descriptor to read from */
 
231
 
 
232
#if defined (READLINE)
 
233
  rlbuf = itext = (char *)0;
 
234
  rlind = 0;
 
235
#endif
 
236
 
 
237
  tmsec = tmusec = 0;           /* no timeout */
 
238
  nr = nchars = input_is_tty = input_is_pipe = unbuffered_read = have_timeout = 0;
 
239
  delim = '\n';         /* read until newline */
 
240
  ignore_delim = 0;
 
241
 
 
242
  reset_internal_getopt ();
 
243
  while ((opt = internal_getopt (list, "ersa:d:i:n:p:t:u:N:")) != -1)
 
244
    {
 
245
      switch (opt)
 
246
        {
 
247
        case 'r':
 
248
          raw = 1;
 
249
          break;
 
250
        case 'p':
 
251
          prompt = list_optarg;
 
252
          break;
 
253
        case 's':
 
254
          silent = 1;
 
255
          break;
 
256
        case 'e':
 
257
#if defined (READLINE)
 
258
          edit = 1;
 
259
#endif
 
260
          break;
 
261
        case 'i':
 
262
#if defined (READLINE)
 
263
          itext = list_optarg;
 
264
#endif
 
265
          break;
 
266
#if defined (ARRAY_VARS)
 
267
        case 'a':
 
268
          arrayname = list_optarg;
 
269
          break;
 
270
#endif
 
271
        case 't':
 
272
          code = uconvert (list_optarg, &ival, &uval);
 
273
          if (code == 0 || ival < 0 || uval < 0)
 
274
            {
 
275
              builtin_error (_("%s: invalid timeout specification"), list_optarg);
 
276
              return (EXECUTION_FAILURE);
 
277
            }
 
278
          else
 
279
            {
 
280
              have_timeout = 1;
 
281
              tmsec = ival;
 
282
              tmusec = uval;
 
283
            }
 
284
          break;
 
285
        case 'N':
 
286
          ignore_delim = 1;
 
287
          delim = -1;
 
288
        case 'n':
 
289
          code = legal_number (list_optarg, &intval);
 
290
          if (code == 0 || intval < 0 || intval != (int)intval)
 
291
            {
 
292
              sh_invalidnum (list_optarg);
 
293
              return (EXECUTION_FAILURE);
 
294
            }
 
295
          else
 
296
            nchars = intval;
 
297
          break;
 
298
        case 'u':
 
299
          code = legal_number (list_optarg, &intval);
 
300
          if (code == 0 || intval < 0 || intval != (int)intval)
 
301
            {
 
302
              builtin_error (_("%s: invalid file descriptor specification"), list_optarg);
 
303
              return (EXECUTION_FAILURE);
 
304
            }
 
305
          else
 
306
            fd = intval;
 
307
          if (sh_validfd (fd) == 0)
 
308
            {
 
309
              builtin_error (_("%d: invalid file descriptor: %s"), fd, strerror (errno));
 
310
              return (EXECUTION_FAILURE);
 
311
            }
 
312
          break;
 
313
        case 'd':
 
314
          delim = *list_optarg;
 
315
          break;
 
316
        default:
 
317
          builtin_usage ();
 
318
          return (EX_USAGE);
 
319
        }
 
320
    }
 
321
  list = loptend;
 
322
 
 
323
  /* `read -t 0 var' tests whether input is available with select/FIONREAD,
 
324
     and fails if those are unavailable */
 
325
  if (have_timeout && tmsec == 0 && tmusec == 0)
 
326
#if 0
 
327
    return (EXECUTION_FAILURE);
 
328
#else
 
329
    return (input_avail (fd) ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
 
330
#endif
 
331
 
 
332
  /* Convenience: check early whether or not the first of possibly several
 
333
     variable names is a valid identifier, and bail early if so. */
 
334
#if defined (ARRAY_VARS)
 
335
  if (list && legal_identifier (list->word->word) == 0 && valid_array_reference (list->word->word) == 0)
 
336
#else
 
337
  if (list && legal_identifier (list->word->word) == 0)
 
338
#endif
 
339
    {
 
340
      sh_invalidid (list->word->word);
 
341
      return (EXECUTION_FAILURE);
 
342
    }
 
343
 
 
344
  /* If we're asked to ignore the delimiter, make sure we do. */
 
345
  if (ignore_delim)
 
346
    delim = -1;
 
347
 
 
348
  /* IF IFS is unset, we use the default of " \t\n". */
 
349
  ifs_chars = getifs ();
 
350
  if (ifs_chars == 0)           /* XXX - shouldn't happen */
 
351
    ifs_chars = "";
 
352
  /* If we want to read exactly NCHARS chars, don't split on IFS */
 
353
  if (ignore_delim)
 
354
    ifs_chars = "";
 
355
  for (skip_ctlesc = skip_ctlnul = 0, e = ifs_chars; *e; e++)
 
356
    skip_ctlesc |= *e == CTLESC, skip_ctlnul |= *e == CTLNUL;
 
357
 
 
358
  input_string = (char *)xmalloc (size = 112);  /* XXX was 128 */
 
359
  input_string[0] = '\0';
 
360
 
 
361
  /* $TMOUT, if set, is the default timeout for read. */
 
362
  if (have_timeout == 0 && (e = get_string_value ("TMOUT")))
 
363
    {
 
364
      code = uconvert (e, &ival, &uval);
 
365
      if (code == 0 || ival < 0 || uval < 0)
 
366
        tmsec = tmusec = 0;
 
367
      else
 
368
        {
 
369
          tmsec = ival;
 
370
          tmusec = uval;
 
371
        }
 
372
    }
 
373
 
 
374
  begin_unwind_frame ("read_builtin");
 
375
 
 
376
#if defined (BUFFERED_INPUT)
 
377
  if (interactive == 0 && default_buffered_input >= 0 && fd_is_bash_input (fd))
 
378
    sync_buffered_stream (default_buffered_input);
 
379
#endif
 
380
 
 
381
  input_is_tty = isatty (fd);
 
382
  if (input_is_tty == 0)
 
383
#ifndef __CYGWIN__
 
384
    input_is_pipe = (lseek (fd, 0L, SEEK_CUR) < 0) && (errno == ESPIPE);
 
385
#else
 
386
    input_is_pipe = 1;
 
387
#endif
 
388
 
 
389
  /* If the -p, -e or -s flags were given, but input is not coming from the
 
390
     terminal, turn them off. */
 
391
  if ((prompt || edit || silent) && input_is_tty == 0)
 
392
    {
 
393
      prompt = (char *)NULL;
 
394
#if defined (READLINE)
 
395
      itext = (char *)NULL;
 
396
#endif
 
397
      edit = silent = 0;
 
398
    }
 
399
 
 
400
#if defined (READLINE)
 
401
  if (edit)
 
402
    add_unwind_protect (xfree, rlbuf);
 
403
#endif
 
404
 
 
405
  pass_next = 0;        /* Non-zero signifies last char was backslash. */
 
406
  saw_escape = 0;       /* Non-zero signifies that we saw an escape char */
 
407
 
 
408
  if (tmsec > 0 || tmusec > 0)
 
409
    {
 
410
      /* Turn off the timeout if stdin is a regular file (e.g. from
 
411
         input redirection). */
 
412
      if ((fstat (fd, &tsb) < 0) || S_ISREG (tsb.st_mode))
 
413
        tmsec = tmusec = 0;
 
414
    }
 
415
 
 
416
  if (tmsec > 0 || tmusec > 0)
 
417
    {
 
418
      code = setjmp_nosigs (alrmbuf);
 
419
      if (code)
 
420
        {
 
421
          sigalrm_seen = 0;
 
422
          /* Tricky.  The top of the unwind-protect stack is the free of
 
423
             input_string.  We want to run all the rest and use input_string,
 
424
             so we have to save input_string temporarily, run the unwind-
 
425
             protects, then restore input_string so we can use it later */
 
426
          orig_input_string = 0;
 
427
          input_string[i] = '\0';       /* make sure it's terminated */
 
428
          if (i == 0)
 
429
            {
 
430
              t = (char *)xmalloc (1);
 
431
              t[0] = 0;
 
432
            }
 
433
          else
 
434
            t = savestring (input_string);
 
435
 
 
436
          run_unwind_frame ("read_builtin");
 
437
          input_string = t;
 
438
          retval = 128+SIGALRM;
 
439
          goto assign_vars;
 
440
        }
 
441
      old_alrm = set_signal_handler (SIGALRM, sigalrm);
 
442
      add_unwind_protect (reset_alarm, (char *)NULL);
 
443
#if defined (READLINE)
 
444
      if (edit)
 
445
        add_unwind_protect (reset_attempted_completion_function, (char *)NULL);
 
446
#endif
 
447
      falarm (tmsec, tmusec);
 
448
    }
 
449
 
 
450
  /* If we've been asked to read only NCHARS chars, or we're using some
 
451
     character other than newline to terminate the line, do the right
 
452
     thing to readline or the tty. */
 
453
  if (nchars > 0 || delim != '\n')
 
454
    {
 
455
#if defined (READLINE)
 
456
      if (edit)
 
457
        {
 
458
          if (nchars > 0)
 
459
            {
 
460
              unwind_protect_int (rl_num_chars_to_read);
 
461
              rl_num_chars_to_read = nchars;
 
462
            }
 
463
          if (delim != '\n')
 
464
            {
 
465
              set_eol_delim (delim);
 
466
              add_unwind_protect (reset_eol_delim, (char *)NULL);
 
467
            }
 
468
        }
 
469
      else
 
470
#endif
 
471
      if (input_is_tty)
 
472
        {
 
473
          /* ttsave() */
 
474
          termsave.fd = fd;
 
475
          ttgetattr (fd, &ttattrs);
 
476
          termsave.attrs = &ttattrs;
 
477
 
 
478
          ttset = ttattrs;        
 
479
          i = silent ? ttfd_cbreak (fd, &ttset) : ttfd_onechar (fd, &ttset);
 
480
          if (i < 0)
 
481
            sh_ttyerror (1);
 
482
          add_unwind_protect ((Function *)ttyrestore, (char *)&termsave);
 
483
        }
 
484
    }
 
485
  else if (silent)      /* turn off echo but leave term in canonical mode */
 
486
    {
 
487
      /* ttsave (); */
 
488
      termsave.fd = fd;
 
489
      ttgetattr (fd, &ttattrs);
 
490
      termsave.attrs = &ttattrs;
 
491
 
 
492
      ttset = ttattrs;
 
493
      i = ttfd_noecho (fd, &ttset);                     /* ttnoecho (); */
 
494
      if (i < 0)
 
495
        sh_ttyerror (1);
 
496
 
 
497
      add_unwind_protect ((Function *)ttyrestore, (char *)&termsave);
 
498
    }
 
499
 
 
500
  /* This *must* be the top unwind-protect on the stack, so the manipulation
 
501
     of the unwind-protect stack after the realloc() works right. */
 
502
  add_unwind_protect (xfree, input_string);
 
503
 
 
504
  CHECK_ALRM;
 
505
  if ((nchars > 0) && (input_is_tty == 0) && ignore_delim)      /* read -N */
 
506
    unbuffered_read = 2;
 
507
  else if ((nchars > 0) || (delim != '\n') || input_is_pipe)
 
508
    unbuffered_read = 1;
 
509
 
 
510
  if (prompt && edit == 0)
 
511
    {
 
512
      fprintf (stderr, "%s", prompt);
 
513
      fflush (stderr);
 
514
    }
 
515
 
 
516
#if defined (__CYGWIN__) && defined (O_TEXT)
 
517
  setmode (0, O_TEXT);
 
518
#endif
 
519
 
 
520
  ps2 = 0;
 
521
  for (print_ps2 = eof = retval = 0;;)
 
522
    {
 
523
      CHECK_ALRM;
 
524
 
 
525
#if defined (READLINE)
 
526
      if (edit)
 
527
        {
 
528
          if (rlbuf && rlbuf[rlind] == '\0')
 
529
            {
 
530
              xfree (rlbuf);
 
531
              rlbuf = (char *)0;
 
532
            }
 
533
          if (rlbuf == 0)
 
534
            {
 
535
              reading = 1;
 
536
              rlbuf = edit_line (prompt ? prompt : "", itext);
 
537
              reading = 0;
 
538
              rlind = 0;
 
539
            }
 
540
          if (rlbuf == 0)
 
541
            {
 
542
              eof = 1;
 
543
              break;
 
544
            }
 
545
          c = rlbuf[rlind++];
 
546
        }
 
547
      else
 
548
        {
 
549
#endif
 
550
 
 
551
      if (print_ps2)
 
552
        {
 
553
          if (ps2 == 0)
 
554
            ps2 = get_string_value ("PS2");
 
555
          fprintf (stderr, "%s", ps2 ? ps2 : "");
 
556
          fflush (stderr);
 
557
          print_ps2 = 0;
 
558
        }
 
559
 
 
560
#if 0
 
561
      if (posixly_correct == 0)
 
562
        interrupt_immediately++;
 
563
#endif
 
564
      reading = 1;
 
565
      if (unbuffered_read == 2)
 
566
        retval = posixly_correct ? zreadintr (fd, &c, 1) : zreadn (fd, &c, nchars - nr);
 
567
      else if (unbuffered_read)
 
568
        retval = posixly_correct ? zreadintr (fd, &c, 1) : zread (fd, &c, 1);
 
569
      else
 
570
        retval = posixly_correct ? zreadcintr (fd, &c) : zreadc (fd, &c);
 
571
      reading = 0;
 
572
#if 0
 
573
      if (posixly_correct == 0)
 
574
        interrupt_immediately--;
 
575
#endif
 
576
 
 
577
      if (retval <= 0)
 
578
        {
 
579
          if (retval < 0 && errno == EINTR)
 
580
            {
 
581
              lastsig = LASTSIG();
 
582
              if (lastsig == 0)
 
583
                lastsig = trapped_signal_received;
 
584
              run_pending_traps ();     /* because interrupt_immediately is not set */
 
585
            }
 
586
          else
 
587
            lastsig = 0;
 
588
          CHECK_TERMSIG;
 
589
          eof = 1;
 
590
          break;
 
591
        }
 
592
 
 
593
      CHECK_ALRM;
 
594
 
 
595
#if defined (READLINE)
 
596
        }
 
597
#endif
 
598
 
 
599
      CHECK_ALRM;
 
600
      if (i + 4 >= size)        /* XXX was i + 2; use i + 4 for multibyte/read_mbchar */
 
601
        {
 
602
          char *t;
 
603
          t = (char *)xrealloc (input_string, size += 128);
 
604
 
 
605
          /* Only need to change unwind-protect if input_string changes */
 
606
          if (t != input_string)
 
607
            {
 
608
              input_string = t;
 
609
              remove_unwind_protect ();
 
610
              add_unwind_protect (xfree, input_string);
 
611
            }
 
612
        }
 
613
 
 
614
      /* If the next character is to be accepted verbatim, a backslash
 
615
         newline pair still disappears from the input. */
 
616
      if (pass_next)
 
617
        {
 
618
          pass_next = 0;
 
619
          if (c == '\n')
 
620
            {
 
621
              i--;              /* back up over the CTLESC */
 
622
              if (interactive && input_is_tty && raw == 0)
 
623
                print_ps2 = 1;
 
624
            }
 
625
          else
 
626
            goto add_char;
 
627
          continue;
 
628
        }
 
629
 
 
630
      /* This may cause problems if IFS contains CTLESC */
 
631
      if (c == '\\' && raw == 0)
 
632
        {
 
633
          pass_next++;
 
634
          if (skip_ctlesc == 0)
 
635
            {
 
636
              saw_escape++;
 
637
              input_string[i++] = CTLESC;
 
638
            }
 
639
          continue;
 
640
        }
 
641
 
 
642
      if (ignore_delim == 0 && (unsigned char)c == delim)
 
643
        break;
 
644
 
 
645
      if (c == '\0' && delim != '\0')
 
646
        continue;               /* skip NUL bytes in input */
 
647
 
 
648
      if ((skip_ctlesc == 0 && c == CTLESC) || (skip_ctlnul == 0 && c == CTLNUL))
 
649
        {
 
650
          saw_escape++;
 
651
          input_string[i++] = CTLESC;
 
652
        }
 
653
 
 
654
add_char:
 
655
      input_string[i++] = c;
 
656
      CHECK_ALRM;
 
657
 
 
658
#if defined (HANDLE_MULTIBYTE)
 
659
      if (nchars > 0 && MB_CUR_MAX > 1 && is_basic (c) == 0)
 
660
        {
 
661
          input_string[i] = '\0';       /* for simplicity and debugging */
 
662
          i += read_mbchar (fd, input_string, i, c, unbuffered_read);
 
663
        }
 
664
#endif
 
665
 
 
666
      nr++;
 
667
 
 
668
      if (nchars > 0 && nr >= nchars)
 
669
        break;
 
670
    }
 
671
  input_string[i] = '\0';
 
672
  CHECK_ALRM;
 
673
 
 
674
  if (retval < 0)
 
675
    {
 
676
      t_errno = errno;
 
677
      if (errno != EINTR)
 
678
        builtin_error (_("read error: %d: %s"), fd, strerror (errno));
 
679
      run_unwind_frame ("read_builtin");
 
680
      return ((t_errno != EINTR) ? EXECUTION_FAILURE : 128+lastsig);
 
681
    }
 
682
 
 
683
  if (tmsec > 0 || tmusec > 0)
 
684
    reset_alarm ();
 
685
 
 
686
  if (nchars > 0 || delim != '\n')
 
687
    {
 
688
#if defined (READLINE)
 
689
      if (edit)
 
690
        {
 
691
          if (nchars > 0)
 
692
            rl_num_chars_to_read = 0;
 
693
          if (delim != '\n')
 
694
            reset_eol_delim ((char *)NULL);
 
695
        }
 
696
      else
 
697
#endif
 
698
      if (input_is_tty)
 
699
        ttyrestore (&termsave);
 
700
    }
 
701
  else if (silent)
 
702
    ttyrestore (&termsave);
 
703
 
 
704
  if (unbuffered_read == 0)
 
705
    zsyncfd (fd);
 
706
 
 
707
  discard_unwind_frame ("read_builtin");
 
708
 
 
709
  retval = eof ? EXECUTION_FAILURE : EXECUTION_SUCCESS;
 
710
 
 
711
assign_vars:
 
712
 
 
713
#if defined (ARRAY_VARS)
 
714
  /* If -a was given, take the string read, break it into a list of words,
 
715
     an assign them to `arrayname' in turn. */
 
716
  if (arrayname)
 
717
    {
 
718
      if (legal_identifier (arrayname) == 0)
 
719
        {
 
720
          sh_invalidid (arrayname);
 
721
          xfree (input_string);
 
722
          return (EXECUTION_FAILURE);
 
723
        }
 
724
 
 
725
      var = find_or_make_array_variable (arrayname, 1);
 
726
      if (var == 0)
 
727
        {
 
728
          xfree (input_string);
 
729
          return EXECUTION_FAILURE;     /* readonly or noassign */
 
730
        }
 
731
      if (assoc_p (var))
 
732
        {
 
733
          builtin_error (_("%s: cannot convert associative to indexed array"), arrayname);
 
734
          xfree (input_string);
 
735
          return EXECUTION_FAILURE;     /* existing associative array */
 
736
        }
 
737
      else if (invisible_p (var))
 
738
        VUNSETATTR (var, att_invisible);
 
739
      array_flush (array_cell (var));
 
740
 
 
741
      alist = list_string (input_string, ifs_chars, 0);
 
742
      if (alist)
 
743
        {
 
744
          if (saw_escape)
 
745
            dequote_list (alist);
 
746
          else
 
747
            word_list_remove_quoted_nulls (alist);
 
748
          assign_array_var_from_word_list (var, alist, 0);
 
749
          dispose_words (alist);
 
750
        }
 
751
      xfree (input_string);
 
752
      return (retval);
 
753
    }
 
754
#endif /* ARRAY_VARS */ 
 
755
 
 
756
  /* If there are no variables, save the text of the line read to the
 
757
     variable $REPLY.  ksh93 strips leading and trailing IFS whitespace,
 
758
     so that `read x ; echo "$x"' and `read ; echo "$REPLY"' behave the
 
759
     same way, but I believe that the difference in behaviors is useful
 
760
     enough to not do it.  Without the bash behavior, there is no way
 
761
     to read a line completely without interpretation or modification
 
762
     unless you mess with $IFS (e.g., setting it to the empty string).
 
763
     If you disagree, change the occurrences of `#if 0' to `#if 1' below. */
 
764
  if (list == 0)
 
765
    {
 
766
#if 0
 
767
      orig_input_string = input_string;
 
768
      for (t = input_string; ifs_chars && *ifs_chars && spctabnl(*t) && isifs(*t); t++)
 
769
        ;
 
770
      input_string = t;
 
771
      input_string = strip_trailing_ifs_whitespace (input_string, ifs_chars, saw_escape);
 
772
#endif
 
773
 
 
774
      if (saw_escape)
 
775
        {
 
776
          t = dequote_string (input_string);
 
777
          var = bind_variable ("REPLY", t, 0);
 
778
          free (t);
 
779
        }
 
780
      else
 
781
        var = bind_variable ("REPLY", input_string, 0);
 
782
      VUNSETATTR (var, att_invisible);
 
783
 
 
784
      xfree (input_string);
 
785
      return (retval);
 
786
    }
 
787
 
 
788
  /* This code implements the Posix.2 spec for splitting the words
 
789
     read and assigning them to variables. */
 
790
  orig_input_string = input_string;
 
791
 
 
792
  /* Remove IFS white space at the beginning of the input string.  If
 
793
     $IFS is null, no field splitting is performed. */
 
794
  for (t = input_string; ifs_chars && *ifs_chars && spctabnl(*t) && isifs(*t); t++)
 
795
    ;
 
796
  input_string = t;
 
797
  for (; list->next; list = list->next)
 
798
    {
 
799
      varname = list->word->word;
 
800
#if defined (ARRAY_VARS)
 
801
      if (legal_identifier (varname) == 0 && valid_array_reference (varname) == 0)
 
802
#else
 
803
      if (legal_identifier (varname) == 0)
 
804
#endif
 
805
        {
 
806
          sh_invalidid (varname);
 
807
          xfree (orig_input_string);
 
808
          return (EXECUTION_FAILURE);
 
809
        }
 
810
 
 
811
      /* If there are more variables than words read from the input,
 
812
         the remaining variables are set to the empty string. */
 
813
      if (*input_string)
 
814
        {
 
815
          /* This call updates INPUT_STRING. */
 
816
          t = get_word_from_string (&input_string, ifs_chars, &e);
 
817
          if (t)
 
818
            *e = '\0';
 
819
          /* Don't bother to remove the CTLESC unless we added one
 
820
             somewhere while reading the string. */
 
821
          if (t && saw_escape)
 
822
            {
 
823
              t1 = dequote_string (t);
 
824
              var = bind_read_variable (varname, t1);
 
825
              xfree (t1);
 
826
            }
 
827
          else
 
828
            var = bind_read_variable (varname, t ? t : "");
 
829
        }
 
830
      else
 
831
        {
 
832
          t = (char *)0;
 
833
          var = bind_read_variable (varname, "");
 
834
        }
 
835
 
 
836
      FREE (t);
 
837
      if (var == 0)
 
838
        {
 
839
          xfree (orig_input_string);
 
840
          return (EXECUTION_FAILURE);
 
841
        }
 
842
 
 
843
      stupidly_hack_special_variables (varname);
 
844
      VUNSETATTR (var, att_invisible);
 
845
    }
 
846
 
 
847
  /* Now assign the rest of the line to the last variable argument. */
 
848
#if defined (ARRAY_VARS)
 
849
  if (legal_identifier (list->word->word) == 0 && valid_array_reference (list->word->word) == 0)
 
850
#else
 
851
  if (legal_identifier (list->word->word) == 0)
 
852
#endif
 
853
    {
 
854
      sh_invalidid (list->word->word);
 
855
      xfree (orig_input_string);
 
856
      return (EXECUTION_FAILURE);
 
857
    }
 
858
 
 
859
#if 0
 
860
  /* This has to be done this way rather than using string_list
 
861
     and list_string because Posix.2 says that the last variable gets the
 
862
     remaining words and their intervening separators. */
 
863
  input_string = strip_trailing_ifs_whitespace (input_string, ifs_chars, saw_escape);
 
864
#else
 
865
  /* Check whether or not the number of fields is exactly the same as the
 
866
     number of variables. */
 
867
  tofree = NULL;
 
868
  if (*input_string)
 
869
    {
 
870
      t1 = input_string;
 
871
      t = get_word_from_string (&input_string, ifs_chars, &e);
 
872
      if (*input_string == 0)
 
873
        tofree = input_string = t;
 
874
      else
 
875
        {
 
876
          input_string = strip_trailing_ifs_whitespace (t1, ifs_chars, saw_escape);
 
877
          tofree = t;
 
878
        }
 
879
    }
 
880
#endif
 
881
 
 
882
  if (saw_escape && input_string && *input_string)
 
883
    {
 
884
      t = dequote_string (input_string);
 
885
      var = bind_read_variable (list->word->word, t);
 
886
      xfree (t);
 
887
    }
 
888
  else
 
889
    var = bind_read_variable (list->word->word, input_string ? input_string : "");
 
890
 
 
891
  if (var)
 
892
    {
 
893
      stupidly_hack_special_variables (list->word->word);
 
894
      VUNSETATTR (var, att_invisible);
 
895
    }
 
896
  else
 
897
    retval = EXECUTION_FAILURE;
 
898
 
 
899
  FREE (tofree);
 
900
  xfree (orig_input_string);
 
901
 
 
902
  return (retval);
 
903
}
 
904
 
 
905
static SHELL_VAR *
 
906
bind_read_variable (name, value)
 
907
     char *name, *value;
 
908
{
 
909
  SHELL_VAR *v;
 
910
 
 
911
#if defined (ARRAY_VARS)
 
912
  if (valid_array_reference (name) == 0)
 
913
    v = bind_variable (name, value, 0);
 
914
  else
 
915
    v = assign_array_element (name, value, 0);
 
916
#else /* !ARRAY_VARS */
 
917
  v = bind_variable (name, value, 0);
 
918
#endif /* !ARRAY_VARS */
 
919
  return (v == 0 ? v
 
920
                 : ((readonly_p (v) || noassign_p (v)) ? (SHELL_VAR *)NULL : v));
 
921
}
 
922
 
 
923
#if defined (HANDLE_MULTIBYTE)
 
924
static int
 
925
read_mbchar (fd, string, ind, ch, unbuffered)
 
926
     int fd;
 
927
     char *string;
 
928
     int ind, ch, unbuffered;
 
929
{
 
930
  char mbchar[MB_LEN_MAX + 1];
 
931
  int i, n, r;
 
932
  char c;
 
933
  size_t ret;
 
934
  mbstate_t ps, ps_back;
 
935
  wchar_t wc;
 
936
 
 
937
  memset (&ps, '\0', sizeof (mbstate_t));
 
938
  memset (&ps_back, '\0', sizeof (mbstate_t));
 
939
  
 
940
  mbchar[0] = ch;
 
941
  i = 1;
 
942
  for (n = 0; n <= MB_LEN_MAX; n++)
 
943
    {
 
944
      ps_back = ps;
 
945
      ret = mbrtowc (&wc, mbchar, i, &ps);
 
946
      if (ret == (size_t)-2)
 
947
        {
 
948
          ps = ps_back;
 
949
          /* We don't want to be interrupted during a multibyte char read */
 
950
          if (unbuffered)
 
951
            r = zread (fd, &c, 1);
 
952
          else
 
953
            r = zreadc (fd, &c);
 
954
          if (r < 0)
 
955
            goto mbchar_return;
 
956
          mbchar[i++] = c;      
 
957
          continue;
 
958
        }
 
959
      else if (ret == (size_t)-1 || ret == (size_t)0 || ret > (size_t)0)
 
960
        break;
 
961
    }
 
962
 
 
963
mbchar_return:
 
964
  if (i > 1)    /* read a multibyte char */
 
965
    /* mbchar[0] is already string[ind-1] */
 
966
    for (r = 1; r < i; r++)
 
967
      string[ind+r-1] = mbchar[r];
 
968
  return i - 1;
 
969
}
 
970
#endif
 
971
 
 
972
 
 
973
static void
 
974
ttyrestore (ttp)
 
975
     struct ttsave *ttp;
 
976
{
 
977
  ttsetattr (ttp->fd, ttp->attrs);
 
978
}
 
979
 
 
980
#if defined (READLINE)
 
981
static rl_completion_func_t *old_attempted_completion_function = 0;
 
982
static rl_hook_func_t *old_startup_hook;
 
983
static char *deftext;
 
984
 
 
985
static void
 
986
reset_attempted_completion_function (cp)
 
987
     char *cp;
 
988
{
 
989
  if (rl_attempted_completion_function == 0 && old_attempted_completion_function)
 
990
    rl_attempted_completion_function = old_attempted_completion_function;
 
991
}
 
992
 
 
993
static int
 
994
set_itext ()
 
995
{
 
996
  int r1, r2;
 
997
 
 
998
  r1 = r2 = 0;
 
999
  if (old_startup_hook)
 
1000
    r1 = (*old_startup_hook) ();
 
1001
  if (deftext)
 
1002
    {
 
1003
      r2 = rl_insert_text (deftext);
 
1004
      deftext = (char *)NULL;
 
1005
      rl_startup_hook = old_startup_hook;
 
1006
      old_startup_hook = (rl_hook_func_t *)NULL;
 
1007
    }
 
1008
  return (r1 || r2);
 
1009
}
 
1010
 
 
1011
static char *
 
1012
edit_line (p, itext)
 
1013
     char *p;
 
1014
     char *itext;
 
1015
{
 
1016
  char *ret;
 
1017
  int len;
 
1018
 
 
1019
  if (bash_readline_initialized == 0)
 
1020
    initialize_readline ();
 
1021
 
 
1022
  old_attempted_completion_function = rl_attempted_completion_function;
 
1023
  rl_attempted_completion_function = (rl_completion_func_t *)NULL;
 
1024
  if (itext)
 
1025
    {
 
1026
      old_startup_hook = rl_startup_hook;
 
1027
      rl_startup_hook = set_itext;
 
1028
      deftext = itext;
 
1029
    }
 
1030
 
 
1031
  ret = readline (p);
 
1032
 
 
1033
  rl_attempted_completion_function = old_attempted_completion_function;
 
1034
  old_attempted_completion_function = (rl_completion_func_t *)NULL;
 
1035
 
 
1036
  if (ret == 0)
 
1037
    return ret;
 
1038
  len = strlen (ret);
 
1039
  ret = (char *)xrealloc (ret, len + 2);
 
1040
  ret[len++] = delim;
 
1041
  ret[len] = '\0';
 
1042
  return ret;
 
1043
}
 
1044
 
 
1045
static int old_delim_ctype;
 
1046
static rl_command_func_t *old_delim_func;
 
1047
static int old_newline_ctype;
 
1048
static rl_command_func_t *old_newline_func;
 
1049
 
 
1050
static unsigned char delim_char;
 
1051
 
 
1052
static void
 
1053
set_eol_delim (c)
 
1054
     int c;
 
1055
{
 
1056
  Keymap cmap;
 
1057
 
 
1058
  if (bash_readline_initialized == 0)
 
1059
    initialize_readline ();
 
1060
  cmap = rl_get_keymap ();
 
1061
 
 
1062
  /* Change newline to self-insert */
 
1063
  old_newline_ctype = cmap[RETURN].type;
 
1064
  old_newline_func =  cmap[RETURN].function;
 
1065
  cmap[RETURN].type = ISFUNC;
 
1066
  cmap[RETURN].function = rl_insert;
 
1067
 
 
1068
  /* Bind the delimiter character to accept-line. */
 
1069
  old_delim_ctype = cmap[c].type;
 
1070
  old_delim_func = cmap[c].function;
 
1071
  cmap[c].type = ISFUNC;
 
1072
  cmap[c].function = rl_newline;
 
1073
 
 
1074
  delim_char = c;
 
1075
}
 
1076
 
 
1077
static void
 
1078
reset_eol_delim (cp)
 
1079
     char *cp;
 
1080
{
 
1081
  Keymap cmap;
 
1082
 
 
1083
  cmap = rl_get_keymap ();
 
1084
 
 
1085
  cmap[RETURN].type = old_newline_ctype;
 
1086
  cmap[RETURN].function = old_newline_func;
 
1087
 
 
1088
  cmap[delim_char].type = old_delim_ctype;
 
1089
  cmap[delim_char].function = old_delim_func;
 
1090
}
 
1091
#endif