diff options
author | Jari Aalto <jari.aalto@cante.net> | 2009-02-19 22:21:29 +0000 |
---|---|---|
committer | Jari Aalto <jari.aalto@cante.net> | 2009-09-12 16:47:02 +0000 |
commit | 17345e5ad288f7543b77b23a25aa380eacc279f2 (patch) | |
tree | 8ac954624147c08ebe1f72f38e0fa5e0416ce87d /builtins | |
parent | 3185942a5234e26ab13fa02f9c51d340cec514f8 (diff) | |
download | bash-17345e5ad288f7543b77b23a25aa380eacc279f2.tar.gz |
Imported from ../bash-4.0.tar.gz.
Diffstat (limited to 'builtins')
-rw-r--r-- | builtins/common.c | 10 | ||||
-rw-r--r-- | builtins/common.h | 1 | ||||
-rw-r--r-- | builtins/evalstring.c | 3 | ||||
-rw-r--r-- | builtins/mapfile.def | 46 | ||||
-rw-r--r-- | builtins/printf.def | 4 | ||||
-rw-r--r-- | builtins/read.def | 16 | ||||
-rw-r--r-- | builtins/reserved.def | 13 |
7 files changed, 71 insertions, 22 deletions
diff --git a/builtins/common.c b/builtins/common.c index f03a655d..6ba641b8 100644 --- a/builtins/common.c +++ b/builtins/common.c @@ -321,6 +321,16 @@ sh_wrerror () builtin_error (_("write error: %s"), strerror (errno)); } +void +sh_ttyerror (set) + int set; +{ + if (set) + builtin_error (_("error setting terminal attributes: %s"), strerror (errno)); + else + builtin_error (_("error getting terminal attributes: %s"), strerror (errno)); +} + int sh_chkwrite (s) int s; diff --git a/builtins/common.h b/builtins/common.h index a3835ca2..ecf9d1b4 100644 --- a/builtins/common.h +++ b/builtins/common.h @@ -81,6 +81,7 @@ extern void sh_nojobs __P((char *)); extern void sh_restricted __P((char *)); extern void sh_notbuiltin __P((char *)); extern void sh_wrerror __P((void)); +extern void sh_ttyerror __P((int)); extern int sh_chkwrite __P((int)); extern char **make_builtin_argv __P((WORD_LIST *, int *)); diff --git a/builtins/evalstring.c b/builtins/evalstring.c index 8f172de7..a0fb3e26 100644 --- a/builtins/evalstring.c +++ b/builtins/evalstring.c @@ -266,10 +266,7 @@ parse_and_execute (string, from_file, flags) global_command = (COMMAND *)NULL; if ((subshell_environment & SUBSHELL_COMSUB) && comsub_ignore_return) -{ command->flags |= CMD_IGNORE_RETURN; -itrace("parse_and_execute: turned on CMD_IGNORE_RETURN from comsub_ignore_return"); -} #if defined (ONESHOT) /* diff --git a/builtins/mapfile.def b/builtins/mapfile.def index 32742ee1..e37cd227 100644 --- a/builtins/mapfile.def +++ b/builtins/mapfile.def @@ -24,7 +24,7 @@ $PRODUCES mapfile.c $BUILTIN mapfile $FUNCTION mapfile_builtin $SHORT_DOC mapfile [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array] -Read lines from a file into an array variable. +Read lines from the standard input into an array variable. Read lines from the standard input into the array variable ARRAY, or from file descriptor FD if the -u option is supplied. The variable MAPFILE is @@ -42,7 +42,9 @@ Options: Arguments: ARRAY Array variable name to use for file data. -If -C is supplied without -c, the default quantum is 5000. +If -C is supplied without -c, the default quantum is 5000. When +CALLBACK is evaluated, it is supplied the index of the next array +element to be assigned as an additional argument. If not supplied with an explicit origin, mapfile will clear ARRAY before assigning to it. @@ -51,6 +53,14 @@ Exit Status: Returns success unless an invalid option is given or ARRAY is readonly. $END +$BUILTIN readarray +$FUNCTION mapfile_builtin +$SHORT_DOC readarray [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array] +Read lines from a file into an array variable. + +A synonym for `mapfile'. +$END + #include <config.h> #include "builtins.h" @@ -70,7 +80,6 @@ $END #include "common.h" #include "bashgetopt.h" - #if !defined (errno) extern int errno; #endif @@ -93,6 +102,7 @@ run_callback(callback, current_index) { unsigned int execlen; char *execstr; + int flags; execlen = strlen (callback) + 10; /* 1 for space between %s and %d, @@ -100,8 +110,13 @@ run_callback(callback, current_index) execlen += 2; execstr = xmalloc (execlen); + flags = 0; +#if 0 + if (interactive) + flags |= SEVAL_NOHIST|SEVAL_INTERACT; +#endif snprintf (execstr, execlen, "%s %d", callback, current_index); - return parse_and_execute(execstr, NULL, 0); + return parse_and_execute(execstr, NULL, flags); } static void @@ -136,8 +151,13 @@ mapfile (fd, line_count_goal, origin, nskip, callback_quantum, callback, array_n here allows us to call bind_array_element instead of bind_array_variable and skip the variable lookup on every call. */ entry = find_or_make_array_variable (array_name, 1); - if (entry == 0) - return (EXECUTION_FAILURE); + if (entry == 0 || readonly_p (entry) || noassign_p (entry)) + { + if (readonly_p (entry)) + err_readonly (array_name); + + return (EXECUTION_FAILURE); + } if (flags & MAPF_CLEARARRAY) array_flush (array_cell (entry)); @@ -147,19 +167,24 @@ mapfile (fd, line_count_goal, origin, nskip, callback_quantum, callback, array_n unbuffered_read = 1; #endif + zreset (); + /* Skip any lines at beginning of file? */ for (line_count = 0; line_count < nskip; line_count++) - zgetline(fd, &line, &line_length, unbuffered_read); + if (zgetline (fd, &line, &line_length, unbuffered_read) < 0) + break; + line = 0; line_length = 0; /* Reset the buffer for bash own stream */ - for (array_index = origin, line_count = 0; - zgetline(fd, &line, &line_length, unbuffered_read) != -1; + interrupt_immediately++; + for (array_index = origin, line_count = 1; + zgetline (fd, &line, &line_length, unbuffered_read) != -1; array_index++, line_count++) { /* Have we exceeded # of lines to store? */ - if (line_count_goal != 0 && line_count >= line_count_goal) + if (line_count_goal != 0 && line_count > line_count_goal) break; /* Remove trailing newlines? */ @@ -184,6 +209,7 @@ mapfile (fd, line_count_goal, origin, nskip, callback_quantum, callback, array_n if (unbuffered_read == 0) zsyncfd (fd); + interrupt_immediately--; return EXECUTION_SUCCESS; } diff --git a/builtins/printf.def b/builtins/printf.def index 7f07d156..757fcea2 100644 --- a/builtins/printf.def +++ b/builtins/printf.def @@ -879,16 +879,18 @@ vbprintf (format, va_alist) SH_VA_START (args, format); blen = vsnprintf (vbuf + vblen, vbsize - vblen, format, args); + va_end (args); nlen = vblen + blen + 1; if (nlen >= vbsize) { vbsize = ((nlen + 63) >> 6) << 6; vbuf = (char *)xrealloc (vbuf, vbsize); + SH_VA_START (args, format); blen = vsnprintf (vbuf + vblen, vbsize - vblen, format, args); + va_end (args); } - va_end (args); vblen += blen; vbuf[vblen] = '\0'; diff --git a/builtins/read.def b/builtins/read.def index c93681ed..fb4366fe 100644 --- a/builtins/read.def +++ b/builtins/read.def @@ -290,9 +290,8 @@ read_builtin (list) } list = loptend; - /* `read -t 0 var' returns failure immediately. XXX - should it test - whether input is available with select/FIONREAD, and fail if those - are unavailable? */ + /* `read -t 0 var' tests whether input is available with select/FIONREAD, + and fails if those are unavailable */ if (have_timeout && tmsec == 0 && tmusec == 0) #if 0 return (EXECUTION_FAILURE); @@ -417,10 +416,9 @@ read_builtin (list) termsave.attrs = &ttattrs; ttset = ttattrs; - if (silent) - ttfd_cbreak (fd, &ttset); /* ttcbreak () */ - else - ttfd_onechar (fd, &ttset); /* ttonechar () */ + i = silent ? ttfd_cbreak (fd, &ttset) : ttfd_onechar (fd, &ttset); + if (i < 0) + sh_ttyerror (1); add_unwind_protect ((Function *)ttyrestore, (char *)&termsave); } } @@ -432,7 +430,9 @@ read_builtin (list) termsave.attrs = &ttattrs; ttset = ttattrs; - ttfd_noecho (fd, &ttset); /* ttnoecho (); */ + i = ttfd_noecho (fd, &ttset); /* ttnoecho (); */ + if (i < 0) + sh_ttyerror (1); add_unwind_protect ((Function *)ttyrestore, (char *)&termsave); } diff --git a/builtins/reserved.def b/builtins/reserved.def index 04c59b01..2478f163 100644 --- a/builtins/reserved.def +++ b/builtins/reserved.def @@ -135,6 +135,19 @@ Exit Status: Returns the status of the last command executed. $END +$BUILTIN coproc +$SHORT_DOC coproc [NAME] command [redirections] +Create a coprocess named NAME. + +Execute COMMAND asynchronously, with the standard output and standard +input of the command connected via a pipe to file descriptors assigned +to indices 0 and 1 of an array variable NAME in the executing shell. +The default NAME is "COPROC". + +Exit Status: +Returns the exit status of COMMAND. +$END + $BUILTIN function $SHORT_DOC function name { COMMANDS ; } or name () { COMMANDS ; } Define shell function. |