diff options
160 files changed, 12713 insertions, 570 deletions
diff --git a/CWRU/CWRU.chlog b/CWRU/CWRU.chlog index 67207366..f487faac 100644 --- a/CWRU/CWRU.chlog +++ b/CWRU/CWRU.chlog @@ -9831,3 +9831,217 @@ lib/sh/strtrans.c doc/{bash.1,bashref.texi} - document new \u and \U escape sequences for $'...' and echo (printf defers to the system's man page or Posix) + + 5/24 + ---- +execute_cmd.c + - change execute_disk_command to return a status, instead of just + leaving it in `last_command_exit_value', since the parent's return + value is sometimes uses (e.g., when a restricted shell refuses to + run a command with a `/'). Fixes bug reported by David Pitt + <David.Pitt@anz.com> + + 5/25 + ---- +bashline.c + - change bash completion functions to save and restore the value of + rl_ignore_some_completions_function, and set it to the bash default + of filename_completion_ignore where appropriate. Fixes bug + reported by Henning Bekel <h.bekel@googlemail.com> + +variables.c + - new convenience function: find_global_variable (name). Looks for + NAME in the global variables table, skipping any local and + temporary environment variables + +builtins/declare.def + - add new -g option to declare/typeset/local, forces variables to be + created or modified at the global scope when executing inside a + shell function. Requested by many, most recently by + konsolebox@gmail.com + + 5/27 + ---- +test.c + - added new `-v var' unary test operator; returns TRUE if var is set + (i.e., has been assigned a value). Works in both test builtin and + [[ conditional command + +doc/{bash.1,bashref.texi} + - documented new `-v var' unary conditional operator + +tests/test.tests + - added tests for new -v var operator + +builtins/kill.def + - change kill builtin so -PID (pgrp specification) following a + -s sig or -n sig option is not interpreted as a signal specification. + Fixes bug reported by Roman Rakus <rrakus@redhat.com> + +builtins/evalstring.c + - in parse_and_execute, if parse_command() returns non-zero, + indicating a parse error, exit the shell if the conditions require + a posix-mode non-interactive shell to abort (parse error in a `.' + script or eval string). Bash-4.1 only printed a warning. This is + from Austin Group interp 114 + +doc/bashref.texi + - add note to the posix mode section of the texinfo manual noting + the changed behavior for `.' and `eval' + +parse.y + - change time_command_acceptable to allow TIME token to appear after + BANG token (to allow `! time foo', which is supposed to be valid) + - change pipeline_command production to allow multiple instances of + `!' (which toggle inverting the return status) and `time' (which + have no effect) + +execute_cmd.c + - In posix mode, `time' without a following pipeline prints the + elapsed user, system, and real time for the shell and its + children since the shell was invoked. + It's like `times' but obeys the setting of TIMEFORMAT. A future + revision of Posix will require this + +doc/{bashref.texi,bash.1} + - document new posix mode use of `time' + +parse.y + - add production to pipeline_command that permits `!' by itself to + be equivalent to `false' (and, with the changes above, permits + `! !' to be roughly equivalent to `true'). A future revision of + Posix will require this + + 5/28 + ---- +parse.y + - fix \W prompt expansion to use memmove instead of strcpy, since the + source and target strings overlap (though you think it wouldn't + matter, since the overlapping regions are never touched at the same + time). Fixes bug reported by Stéphane Jourdoi + <sjourdois@gmail.com> + +parse.y + - Posix interp 217 states that $(( must be parsed first as an + arithmetic expansion, so avoid attempting to parse it as a nested + command substitution. Fixes bug reported by several, most recently + <jwm@horde.net> + +subst.c + - change extract_delimited_string to process nested $( as a possible + command substitution, but only if already parsing an arithmetic + expansion. Rest of fix for Posix interp 217 + - change parameter_brace_expand_rhs to make the := expansion operator + perform quote removal and both assign the result to the variable and + return it as the result of the expansion, rather than assign the + value after quote removal but return the value before quote removal. + Posix interp 221 + - introduce new internal quoting flag: Q_DOLBRACE. Denotes a double- + quoted ${...} expansion. In this case, Posix interp 221 requires + that a backslash quoting an embedded `}' be removed, even though it's + not one of the characters marked as special inside double quotes. + Set in parameter_brace_expand, used by expand_word_internal. + +parse.y + - introduce new parsing state, P_DOLBRACE, set when parsing a ${...} + expansion + - set a "dolbrace operator state" in parse_matched_pair to decide + whether the lexer is reading the param, op, or word in + ${paramOPword}. Will be used to decide whether or not to treat + single quotes specially in a double-quoted "${...} + + 5/29 + ---- +parse.y + - change parse_matched_pair so that a single quote appearing in a + double-quoted ${...} expansion is not special unless the expansion + operator is `#[#]' or `%[%]'. Posix interp 221 + +subst.c + - change string_extract_double_quoted so that a single quote appearing + in a double-quoted ${...} expansion is not special unless the + expansion operator is `#[#]' or `%[%]'. Posix interp 221 + +doc/bashref.texi + - document posix-mode effects of Posix interp 221 + - add section describing GNU parallel as requested by Stallman + +lib/readline/complete.c + - broke code that compares filenames read from the file system (and + possibly converted) to words being completed out into a separate + function: complete_fncmp + - augment complete_fncmp to treat hyphen and underscore as equivalent + when comparing filenames if _rl_completion_case_map is set + +lib/readline/rlprivate.h + - new extern declaration for _rl_completion_case_map + +lib/readline/util.c + - change _rl_strnicmp to return the difference between the characters, + like strcasecmp, and not modify the pointers it is passed + - change _rl_stricmp to not modify the pointers it is passed + +lib/readline/bind.c + - new bindable variable, "completion-case-map", toggles value of + _rl_completion_case_map + +lib/readline/doc/{rluser.texi,readline.3} + - document new bindable readline variable "completion-case-map" + +execute_cmd.c + - change execute_function to reset funcnest and jump back to top level + if funcnest exceeds funcnest_max + - use funcnest_max as a max function nesting level, if set to numeric + value greater than 0 (defaults to 0, so inactive) + +variables.c + - new variable FUNCNEST, controls funcnest_max value if set to numeric + value > 0 +sig.c + - reset funcnest to 0 when throw_to_top_level occurs + +doc/{bash.1,bashref.texi} + - document FUNCNEST variable and its effect on function execution + +lib/readline/funmap.c + - add new bindable command names to avoid case-insensitive matching + problems between, for instance, vi-fword and vi-fWord: + + vi-forward-word + vi-forward-bigword + vi-backward-word + vi-backward-bigword + vi-end-word + vi-end-bigword + + Suggested in a different form in 2006 (!) by Servatius Brandt + <servatius.brandt@arcor.de> + +builtins/mapfile.def + - run_callback now takes a new third argument: curline, the line + currently being read and about to be assigned + - the callback function/command now takes an additional argument: + the line to be assigned to the array index. Feature suggested by + Dennis Williamson <dennistwilliamson@gmail.com> + +doc/{bash.1,bashref.texi} + - document new additional `line' argument to mapfile callback + + 5/30 + ---- +builtins/printf.def + - add new %(fmt)T format specifier, where FMT is a strftime format. + Argument is number of seconds since the epoch, with -1 meaning + current time (roughly date +%s) and -2 meaning shell start time + (roughly $SECONDS, unless it's been assigned a value or unset). + Fieldwidth and precision are preserved, strftime result is printed + as with %[-][[fieldwidth][.[precision]]]s + +doc/{bash.1,bashref.texi} + - document new %(datefmt)T printf format specifier and special + arguments + +builtins/hash.def + - don't permit programs with slashes to be entered into the hash table + at all, even with the -p option. Inconsistency pointed out by + Jan Schampera <jan.schampera@web.de> diff --git a/CWRU/CWRU.chlog~ b/CWRU/CWRU.chlog~ index 74dd43c3..eeb49aa4 100644 --- a/CWRU/CWRU.chlog~ +++ b/CWRU/CWRU.chlog~ @@ -9827,3 +9827,217 @@ builtins/printf.def lib/sh/strtrans.c - add code to handle \u and \U escapes as unicode characters, works for both `echo -e' and $'...' + +doc/{bash.1,bashref.texi} + - document new \u and \U escape sequences for $'...' and echo (printf + defers to the system's man page or Posix) + + 5/24 + ---- +execute_cmd.c + - change execute_disk_command to return a status, instead of just + leaving it in `last_command_exit_value', since the parent's return + value is sometimes uses (e.g., when a restricted shell refuses to + run a command with a `/'). Fixes bug reported by David Pitt + <David.Pitt@anz.com> + + 5/25 + ---- +bashline.c + - change bash completion functions to save and restore the value of + rl_ignore_some_completions_function, and set it to the bash default + of filename_completion_ignore where appropriate. Fixes bug + reported by Henning Bekel <h.bekel@googlemail.com> + +variables.c + - new convenience function: find_global_variable (name). Looks for + NAME in the global variables table, skipping any local and + temporary environment variables + +builtins/declare.def + - add new -g option to declare/typeset/local, forces variables to be + created or modified at the global scope when executing inside a + shell function. Requested by many, most recently by + konsolebox@gmail.com + + 5/27 + ---- +test.c + - added new `-v var' unary test operator; returns TRUE if var is set + (i.e., has been assigned a value). Works in both test builtin and + [[ conditional command + +doc/{bash.1,bashref.texi} + - documented new `-v var' unary conditional operator + +tests/test.tests + - added tests for new -v var operator + +builtins/kill.def + - change kill builtin so -PID (pgrp specification) following a + -s sig or -n sig option is not interpreted as a signal specification. + Fixes bug reported by Roman Rakus <rrakus@redhat.com> + +builtins/evalstring.c + - in parse_and_execute, if parse_command() returns non-zero, + indicating a parse error, exit the shell if the conditions require + a posix-mode non-interactive shell to abort (parse error in a `.' + script or eval string). Bash-4.1 only printed a warning. This is + from Austin Group interp 114 + +doc/bashref.texi + - add note to the posix mode section of the texinfo manual noting + the changed behavior for `.' and `eval' + +parse.y + - change time_command_acceptable to allow TIME token to appear after + BANG token (to allow `! time foo', which is supposed to be valid) + - change pipeline_command production to allow multiple instances of + `!' (which toggle inverting the return status) and `time' (which + have no effect) + +execute_cmd.c + - In posix mode, `time' without a following pipeline prints the + elapsed user, system, and real time for the shell and its + children since the shell was invoked. + It's like `times' but obeys the setting of TIMEFORMAT. A future + revision of Posix will require this + +doc/{bashref.texi,bash.1} + - document new posix mode use of `time' + +parse.y + - add production to pipeline_command that permits `!' by itself to + be equivalent to `false' (and, with the changes above, permits + `! !' to be roughly equivalent to `true'). A future revision of + Posix will require this + + 5/28 + ---- +parse.y + - fix \W prompt expansion to use memmove instead of strcpy, since the + source and target strings overlap (though you think it wouldn't + matter, since the overlapping regions are never touched at the same + time). Fixes bug reported by Stéphane Jourdoi + <sjourdois@gmail.com> + +parse.y + - Posix interp 217 states that $(( must be parsed first as an + arithmetic expansion, so avoid attempting to parse it as a nested + command substitution. Fixes bug reported by several, most recently + <jwm@horde.net> + +subst.c + - change extract_delimited_string to process nested $( as a possible + command substitution, but only if already parsing an arithmetic + expansion. Rest of fix for Posix interp 217 + - change parameter_brace_expand_rhs to make the := expansion operator + perform quote removal and both assign the result to the variable and + return it as the result of the expansion, rather than assign the + value after quote removal but return the value before quote removal. + Posix interp 221 + - introduce new internal quoting flag: Q_DOLBRACE. Denotes a double- + quoted ${...} expansion. In this case, Posix interp 221 requires + that a backslash quoting an embedded `}' be removed, even though it's + not one of the characters marked as special inside double quotes. + Set in parameter_brace_expand, used by expand_word_internal. + +parse.y + - introduce new parsing state, P_DOLBRACE, set when parsing a ${...} + expansion + - set a "dolbrace operator state" in parse_matched_pair to decide + whether the lexer is reading the param, op, or word in + ${paramOPword}. Will be used to decide whether or not to treat + single quotes specially in a double-quoted "${...} + + 5/29 + ---- +parse.y + - change parse_matched_pair so that a single quote appearing in a + double-quoted ${...} expansion is not special unless the expansion + operator is `#[#]' or `%[%]'. Posix interp 221 + +subst.c + - change string_extract_double_quoted so that a single quote appearing + in a double-quoted ${...} expansion is not special unless the + expansion operator is `#[#]' or `%[%]'. Posix interp 221 + +doc/bashref.texi + - document posix-mode effects of Posix interp 221 + - add section describing GNU parallel as requested by Stallman + +lib/readline/complete.c + - broke code that compares filenames read from the file system (and + possibly converted) to words being completed out into a separate + function: complete_fncmp + - augment complete_fncmp to treat hyphen and underscore as equivalent + when comparing filenames if _rl_completion_case_map is set + +lib/readline/rlprivate.h + - new extern declaration for _rl_completion_case_map + +lib/readline/util.c + - change _rl_strnicmp to return the difference between the characters, + like strcasecmp, and not modify the pointers it is passed + - change _rl_stricmp to not modify the pointers it is passed + +lib/readline/bind.c + - new bindable variable, "completion-case-map", toggles value of + _rl_completion_case_map + +lib/readline/doc/{rluser.texi,readline.3} + - document new bindable readline variable "completion-case-map" + +execute_cmd.c + - change execute_function to reset funcnest and jump back to top level + if funcnest exceeds funcnest_max + - use funcnest_max as a max function nesting level, if set to numeric + value greater than 0 (defaults to 0, so inactive) + +variables.c + - new variable FUNCNEST, controls funcnest_max value if set to numeric + value > 0 +sig.c + - reset funcnest to 0 when throw_to_top_level occurs + +doc/{bash.1,bashref.texi} + - document FUNCNEST variable and its effect on function execution + +lib/readline/funmap.c + - add new bindable command names to avoid case-insensitive matching + problems between, for instance, vi-fword and vi-fWord: + + vi-forward-word + vi-forward-bigword + vi-backward-word + vi-backward-bigword + vi-end-word + vi-end-bigword + + Suggested in a different form in 2006 (!) by Servatius Brandt + <servatius.brandt@arcor.de> + +builtins/mapfile.def + - run_callback now takes a new third argument: curline, the line + currently being read and about to be assigned + - the callback function/command now takes an additional argument: + the line to be assigned to the array index. Feature suggested by + Dennis Williamson <dennistwilliamson@gmail.com> + +doc/{bash.1,bashref.texi} + - document new additional `line' argument to mapfile callback + + 5/30 + ---- +builtins/printf.def + - add new %(fmt)T format specifier, where FMT is a strftime format. + Argument is number of seconds since the epoch, with -1 meaning + current time (roughly date +%s) and -2 meaning shell start time + (roughly $SECONDS, unless it's been assigned a value or unset). + Fieldwidth and precision are preserved, strftime result is printed + as with %[-][[fieldwidth][.[precision]]]s + +doc/{bash.1,bashref.texi} + - document new %(datefmt)T printf format specifier and special + arguments + diff --git a/CWRU/misc/sigs.c b/CWRU/misc/sigs.c index 6389ff2c..bae93f8a 100644 --- a/CWRU/misc/sigs.c +++ b/CWRU/misc/sigs.c @@ -1,6 +1,6 @@ /* sigs - print signal dispositions for a process */ -/* Copyright (C) 1990-2009 Free Software Foundation, Inc. +/* Copyright (C) 1990-2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. diff --git a/CWRU/misc/sigs.c~ b/CWRU/misc/sigs.c~ index 097ab317..6389ff2c 100644 --- a/CWRU/misc/sigs.c~ +++ b/CWRU/misc/sigs.c~ @@ -21,7 +21,7 @@ #include <signal.h> #include <stdio.h> -extern char *sys_siglist[]; +extern const char * const sys_siglist[]; typedef void sighandler(); @@ -799,6 +799,7 @@ tests/comsub-eof.right f tests/comsub-posix.tests f tests/comsub-posix.right f tests/comsub-posix1.sub f +tests/comsub-posix2.sub f tests/cond.tests f tests/cond.right f tests/cond-regexp.sub f @@ -855,6 +856,7 @@ tests/func.right f tests/func1.sub f tests/func2.sub f tests/func3.sub f +tests/func4.sub f tests/getopts.tests f tests/getopts.right f tests/getopts1.sub f @@ -932,13 +934,17 @@ tests/nquote5.tests f tests/nquote5.right f tests/posix2.tests f tests/posix2.right f +tests/posixexp.tests f +tests/posixexp.right f tests/posixpat.tests f tests/posixpat.right f tests/prec.right f tests/precedence f tests/printf.tests f tests/printf.right f +tests/printf1.sub f tests/printf2.sub f +tests/printf3.sub f tests/quote.tests f tests/quote.right f tests/read.tests f @@ -1021,6 +1027,7 @@ tests/run-nquote3 f tests/run-nquote4 f tests/run-nquote5 f tests/run-posix2 f +tests/run-posixexp f tests/run-posixpat f tests/run-precedence f tests/run-printf f @@ -436,6 +436,7 @@ lib/sh/timeval.c f lib/sh/tmpfile.c f lib/sh/uconvert.c f lib/sh/ufuncs.c f +lib/sh/unicode.c f lib/sh/vprint.c f lib/sh/wcsdup.c f lib/sh/winsize.c f @@ -798,6 +799,7 @@ tests/comsub-eof.right f tests/comsub-posix.tests f tests/comsub-posix.right f tests/comsub-posix1.sub f +tests/comsub-posix2.sub f tests/cond.tests f tests/cond.right f tests/cond-regexp.sub f @@ -854,6 +856,7 @@ tests/func.right f tests/func1.sub f tests/func2.sub f tests/func3.sub f +tests/func4.sub f tests/getopts.tests f tests/getopts.right f tests/getopts1.sub f @@ -931,6 +934,8 @@ tests/nquote5.tests f tests/nquote5.right f tests/posix2.tests f tests/posix2.right f +tests/posixexp.tests f +tests/posixexp.right f tests/posixpat.tests f tests/posixpat.right f tests/prec.right f @@ -938,6 +943,7 @@ tests/precedence f tests/printf.tests f tests/printf.right f tests/printf2.sub f +tests/printf3.sub f tests/quote.tests f tests/quote.right f tests/read.tests f @@ -1020,6 +1026,7 @@ tests/run-nquote3 f tests/run-nquote4 f tests/run-nquote5 f tests/run-posix2 f +tests/run-posixexp f tests/run-posixpat f tests/run-precedence f tests/run-printf f @@ -1066,6 +1073,7 @@ tests/type.tests f tests/type.right f tests/type1.sub f tests/type2.sub f +tests/type3.sub f tests/varenv.right f tests/varenv.sh f tests/varenv1.sub f diff --git a/arrayfunc.c b/arrayfunc.c index ad5b2bdd..f7c7fd9f 100644 --- a/arrayfunc.c +++ b/arrayfunc.c @@ -1,6 +1,6 @@ /* arrayfunc.c -- High-level array functions used by other parts of the shell. */ -/* Copyright (C) 2001-2009 Free Software Foundation, Inc. +/* Copyright (C) 2001-2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. diff --git a/arrayfunc.c~ b/arrayfunc.c~ index c5c926e7..ad5b2bdd 100644 --- a/arrayfunc.c~ +++ b/arrayfunc.c~ @@ -883,7 +883,7 @@ array_value_internal (s, quoted, allow_all, rtype, indp) err_badarraysub (s); return ((char *)NULL); } - else if (var == 0 || value_cell (var) == 0) + else if (var == 0 || value_cell (var) == 0) /* XXX - check for invisible_p(var) ? */ return ((char *)NULL); else if (array_p (var) == 0 && assoc_p (var) == 0) l = add_string_to_list (value_cell (var), (WORD_LIST *)NULL); @@ -943,7 +943,7 @@ index_error: goto index_error; } - if (var == 0) + if (var == 0 || value_cell (var) == 0) /* XXX - check invisible_p(var) ? */ return ((char *)NULL); if (array_p (var) == 0 && assoc_p (var) == 0) return (ind == 0 ? value_cell (var) : (char *)NULL); diff --git a/arrayfunc.h b/arrayfunc.h index 9add1189..286dd8c4 100644 --- a/arrayfunc.h +++ b/arrayfunc.h @@ -1,6 +1,6 @@ /* arrayfunc.h -- declarations for miscellaneous array functions in arrayfunc.c */ -/* Copyright (C) 2001-2009 Free Software Foundation, Inc. +/* Copyright (C) 2001-2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. diff --git a/arrayfunc.h~ b/arrayfunc.h~ index 85ebb483..9add1189 100644 --- a/arrayfunc.h~ +++ b/arrayfunc.h~ @@ -51,7 +51,7 @@ extern void print_assoc_assignment __P((SHELL_VAR *, int)); extern arrayind_t array_expand_index __P((char *, int)); extern int valid_array_reference __P((char *)); -extern char *array_value __P((char *, int, int *)); +extern char *array_value __P((char *, int, int *, arrayind_t *)); extern char *get_array_value __P((char *, int, int *, arrayind_t *)); extern char *array_keys __P((char *, int)); @@ -1,6 +1,6 @@ /* bashhist.c -- bash interface to the GNU history library. */ -/* Copyright (C) 1993-2009 Free Software Foundation, Inc. +/* Copyright (C) 1993-2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. diff --git a/bashhist.c~ b/bashhist.c~ index f12fdac5..aad8e23b 100644 --- a/bashhist.c~ +++ b/bashhist.c~ @@ -733,7 +733,6 @@ bash_add_history (line) if (command_oriented_history && current_command_line_count > 1) { chars_to_add = literal_history ? "\n" : history_delimiting_chars (line); -itrace("bash_add_history: `%s' PST_HEREDOC = %d chars_to_add = `%s'", line, (parser_state&PST_HEREDOC), chars_to_add); using_history (); current = previous_history (); @@ -753,14 +752,12 @@ itrace("bash_add_history: `%s' PST_HEREDOC = %d chars_to_add = `%s'", line, (par chars_to_add = ""; } -#if 1 /* XXX - bash-4.2 */ /* If we're not in some kind of quoted construct, the current history entry ends with a newline, and we're going to add a semicolon, don't. In some cases, it results in a syntax error (e.g., before a close brace), and it should not be needed. */ if (dstack.delimiter_depth == 0 && current->line[curlen - 1] == '\n' && *chars_to_add == ';') chars_to_add++; -#endif new_line = (char *)xmalloc (1 + curlen @@ -1,6 +1,6 @@ /* bashline.c -- Bash's interface to the readline library. */ -/* Copyright (C) 1987-2009 Free Software Foundation, Inc. +/* Copyright (C) 1987-2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. @@ -2876,12 +2876,15 @@ dynamic_complete_history (count, key) int r; rl_compentry_func_t *orig_func; rl_completion_func_t *orig_attempt_func; + rl_compignore_func_t *orig_ignore_func; orig_func = rl_completion_entry_function; orig_attempt_func = rl_attempted_completion_function; + orig_ignore_func = rl_ignore_some_completions_function; rl_completion_entry_function = history_completion_generator; rl_attempted_completion_function = (rl_completion_func_t *)NULL; + rl_ignore_some_completions_function = filename_completion_ignore; /* XXX - use rl_completion_mode here? */ if (rl_last_func == dynamic_complete_history) @@ -2891,6 +2894,8 @@ dynamic_complete_history (count, key) rl_completion_entry_function = orig_func; rl_attempted_completion_function = orig_attempt_func; + rl_ignore_some_completions_function = orig_ignore_func; + return r; } @@ -2901,14 +2906,17 @@ bash_dabbrev_expand (count, key) int r, orig_suppress, orig_sort; rl_compentry_func_t *orig_func; rl_completion_func_t *orig_attempt_func; + rl_compignore_func_t *orig_ignore_func; orig_func = rl_menu_completion_entry_function; orig_attempt_func = rl_attempted_completion_function; + orig_ignore_func = rl_ignore_some_completions_function; orig_suppress = rl_completion_suppress_append; orig_sort = rl_sort_completion_matches; rl_menu_completion_entry_function = history_completion_generator; rl_attempted_completion_function = (rl_completion_func_t *)NULL; + rl_ignore_some_completions_function = filename_completion_ignore; rl_filename_completion_desired = 0; rl_completion_suppress_append = 1; rl_sort_completion_matches = 0; @@ -2923,6 +2931,7 @@ bash_dabbrev_expand (count, key) rl_last_func = bash_dabbrev_expand; rl_menu_completion_entry_function = orig_func; rl_attempted_completion_function = orig_attempt_func; + rl_ignore_some_completions_function = orig_ignore_func; rl_completion_suppress_append = orig_suppress; rl_sort_completion_matches = orig_sort; @@ -2972,16 +2981,19 @@ bash_complete_filename_internal (what_to_do) rl_compentry_func_t *orig_func; rl_completion_func_t *orig_attempt_func; rl_icppfunc_t *orig_dir_func; + rl_compignore_func_t *orig_ignore_func; /*const*/ char *orig_rl_completer_word_break_characters; int r; orig_func = rl_completion_entry_function; orig_attempt_func = rl_attempted_completion_function; orig_dir_func = rl_directory_completion_hook; + orig_ignore_func = rl_ignore_some_completions_function; orig_rl_completer_word_break_characters = rl_completer_word_break_characters; rl_completion_entry_function = rl_filename_completion_function; rl_attempted_completion_function = (rl_completion_func_t *)NULL; rl_directory_completion_hook = (rl_icppfunc_t *)NULL; + rl_ignore_some_completions_function = filename_completion_ignore; rl_completer_word_break_characters = " \t\n\"\'"; r = rl_complete_internal (what_to_do); @@ -2989,6 +3001,7 @@ bash_complete_filename_internal (what_to_do) rl_completion_entry_function = orig_func; rl_attempted_completion_function = orig_attempt_func; rl_directory_completion_hook = orig_dir_func; + rl_ignore_some_completions_function = orig_ignore_func; rl_completer_word_break_characters = orig_rl_completer_word_break_characters; return r; @@ -3166,17 +3179,21 @@ bash_specific_completion (what_to_do, generator) { rl_compentry_func_t *orig_func; rl_completion_func_t *orig_attempt_func; + rl_compignore_func_t *orig_ignore_func; int r; orig_func = rl_completion_entry_function; orig_attempt_func = rl_attempted_completion_function; + orig_ignore_func = rl_ignore_some_completions_function; rl_completion_entry_function = generator; rl_attempted_completion_function = NULL; + rl_ignore_some_completions_function = orig_ignore_func; r = rl_complete_internal (what_to_do); rl_completion_entry_function = orig_func; rl_attempted_completion_function = orig_attempt_func; + rl_ignore_some_completions_function = orig_ignore_func; return r; } diff --git a/bashline.c~ b/bashline.c~ index da3353d4..eb86b3b7 100644 --- a/bashline.c~ +++ b/bashline.c~ @@ -175,7 +175,7 @@ static char **prog_complete_matches; extern int hist_verify; #endif -extern int current_command_line_count, fc_command_line_count; +extern int current_command_line_count, saved_command_line_count; extern int last_command_exit_value; extern int array_needs_making; extern int posixly_correct, no_symbolic_links; @@ -863,11 +863,11 @@ edit_and_execute_command (count, c, editing_mode, edit_command) char *edit_command; { char *command, *metaval; - int r, cclc, rrs, metaflag; + int r, rrs, metaflag; sh_parser_state_t ps; rrs = rl_readline_state; - cclc = current_command_line_count; + saved_command_line_count = current_command_line_count; /* Accept the current line. */ rl_newline (1, c); @@ -907,7 +907,7 @@ edit_and_execute_command (count, c, editing_mode, edit_command) if (rl_prep_term_function) (*rl_prep_term_function) (metaflag); - current_command_line_count = cclc; + current_command_line_count = saved_command_line_count; /* Now erase the contents of the current line and undo the effects of the rl_accept_line() above. We don't even want to make the text we just @@ -2876,12 +2876,15 @@ dynamic_complete_history (count, key) int r; rl_compentry_func_t *orig_func; rl_completion_func_t *orig_attempt_func; + rl_compignore_func_t *orig_ignore_func; orig_func = rl_completion_entry_function; orig_attempt_func = rl_attempted_completion_function; + orig_ignore_func = rl_ignore_some_completions_function; rl_completion_entry_function = history_completion_generator; rl_attempted_completion_function = (rl_completion_func_t *)NULL; + rl_ignore_some_completions_function = filename_completion_ignore; /* XXX - use rl_completion_mode here? */ if (rl_last_func == dynamic_complete_history) @@ -2891,6 +2894,8 @@ dynamic_complete_history (count, key) rl_completion_entry_function = orig_func; rl_attempted_completion_function = orig_attempt_func; + rl_ignore_some_completions_function = orig_ignore_func; + return r; } @@ -2901,14 +2906,17 @@ bash_dabbrev_expand (count, key) int r, orig_suppress, orig_sort; rl_compentry_func_t *orig_func; rl_completion_func_t *orig_attempt_func; + rl_compignore_func_t *orig_ignore_func; orig_func = rl_menu_completion_entry_function; orig_attempt_func = rl_attempted_completion_function; + orig_ignore_func = rl_ignore_some_completions_function; orig_suppress = rl_completion_suppress_append; orig_sort = rl_sort_completion_matches; rl_menu_completion_entry_function = history_completion_generator; rl_attempted_completion_function = (rl_completion_func_t *)NULL; + rl_ignore_some_completions_function = filename_completion_ignore; rl_filename_completion_desired = 0; rl_completion_suppress_append = 1; rl_sort_completion_matches = 0; @@ -2923,6 +2931,7 @@ bash_dabbrev_expand (count, key) rl_last_func = bash_dabbrev_expand; rl_menu_completion_entry_function = orig_func; rl_attempted_completion_function = orig_attempt_func; + rl_ignore_some_completions_function = orig_ignore_func; rl_completion_suppress_append = orig_suppress; rl_sort_completion_matches = orig_sort; @@ -2972,16 +2981,19 @@ bash_complete_filename_internal (what_to_do) rl_compentry_func_t *orig_func; rl_completion_func_t *orig_attempt_func; rl_icppfunc_t *orig_dir_func; + rl_compignore_func_t *orig_ignore_func; /*const*/ char *orig_rl_completer_word_break_characters; int r; orig_func = rl_completion_entry_function; orig_attempt_func = rl_attempted_completion_function; orig_dir_func = rl_directory_completion_hook; + orig_ignore_func = rl_ignore_some_completions_function; orig_rl_completer_word_break_characters = rl_completer_word_break_characters; rl_completion_entry_function = rl_filename_completion_function; rl_attempted_completion_function = (rl_completion_func_t *)NULL; rl_directory_completion_hook = (rl_icppfunc_t *)NULL; + rl_ignore_some_completions_function = filename_completion_ignore; rl_completer_word_break_characters = " \t\n\"\'"; r = rl_complete_internal (what_to_do); @@ -2989,6 +3001,7 @@ bash_complete_filename_internal (what_to_do) rl_completion_entry_function = orig_func; rl_attempted_completion_function = orig_attempt_func; rl_directory_completion_hook = orig_dir_func; + rl_ignore_some_completions_function = orig_ignore_func; rl_completer_word_break_characters = orig_rl_completer_word_break_characters; return r; @@ -3166,17 +3179,21 @@ bash_specific_completion (what_to_do, generator) { rl_compentry_func_t *orig_func; rl_completion_func_t *orig_attempt_func; + rl_compignore_func_t *orig_ignore_func; int r; orig_func = rl_completion_entry_function; orig_attempt_func = rl_attempted_completion_function; + orig_ignore_func = rl_ignore_some_completions_function; rl_completion_entry_function = generator; rl_attempted_completion_function = NULL; + rl_ignore_some_completions_function = orig_ignore_func; r = rl_complete_internal (what_to_do); rl_completion_entry_function = orig_func; rl_attempted_completion_function = orig_attempt_func; + rl_ignore_some_completions_function = orig_ignore_func; return r; } diff --git a/builtins/cd.def b/builtins/cd.def index 109e97e2..d558fa56 100644 --- a/builtins/cd.def +++ b/builtins/cd.def @@ -1,7 +1,7 @@ This file is cd.def, from which is created cd.c. It implements the builtins "cd" and "pwd" in Bash. -Copyright (C) 1987-2009 Free Software Foundation, Inc. +Copyright (C) 1987-2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. diff --git a/builtins/cd.def~ b/builtins/cd.def~ new file mode 100644 index 00000000..109e97e2 --- /dev/null +++ b/builtins/cd.def~ @@ -0,0 +1,513 @@ +This file is cd.def, from which is created cd.c. It implements the +builtins "cd" and "pwd" in Bash. + +Copyright (C) 1987-2009 Free Software Foundation, Inc. + +This file is part of GNU Bash, the Bourne Again SHell. + +Bash 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 3 of the License, or +(at your option) any later version. + +Bash 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 Bash. If not, see <http://www.gnu.org/licenses/>. + +$PRODUCES cd.c +#include <config.h> + +#if defined (HAVE_UNISTD_H) +# ifdef _MINIX +# include <sys/types.h> +# endif +# include <unistd.h> +#endif + +#include "../bashtypes.h" +#include "posixdir.h" +#include "posixstat.h" +#ifndef _MINIX +#include <sys/param.h> +#endif + +#include <stdio.h> + +#include "../bashansi.h" +#include "../bashintl.h" + +#include <errno.h> +#include <tilde/tilde.h> + +#include "../shell.h" +#include "../flags.h" +#include "maxpath.h" +#include "common.h" +#include "bashgetopt.h" + +#if !defined (errno) +extern int errno; +#endif /* !errno */ + +extern int posixly_correct; +extern int array_needs_making; +extern const char * const bash_getcwd_errstr; + +static int bindpwd __P((int)); +static void setpwd __P((char *)); +static char *resetpwd __P((char *)); +static int change_to_directory __P((char *, int)); + +/* Change this to 1 to get cd spelling correction by default. */ +int cdspelling = 0; + +int cdable_vars; + +$BUILTIN cd +$FUNCTION cd_builtin +$SHORT_DOC cd [-L|-P] [dir] +Change the shell working directory. + +Change the current directory to DIR. The default DIR is the value of the +HOME shell variable. + +The variable CDPATH defines the search path for the directory containing +DIR. Alternative directory names in CDPATH are separated by a colon (:). +A null directory name is the same as the current directory. If DIR begins +with a slash (/), then CDPATH is not used. + +If the directory is not found, and the shell option `cdable_vars' is set, +the word is assumed to be a variable name. If that variable has a value, +its value is used for DIR. + +Options: + -L force symbolic links to be followed + -P use the physical directory structure without following symbolic + links + +The default is to follow symbolic links, as if `-L' were specified. + +Exit Status: +Returns 0 if the directory is changed; non-zero otherwise. +$END + +/* Just set $PWD, don't change OLDPWD. Used by `pwd -P' in posix mode. */ +static void +setpwd (dirname) + char *dirname; +{ + int old_anm; + SHELL_VAR *tvar; + + old_anm = array_needs_making; + tvar = bind_variable ("PWD", dirname ? dirname : "", 0); + if (old_anm == 0 && array_needs_making && exported_p (tvar)) + { + update_export_env_inplace ("PWD=", 4, dirname ? dirname : ""); + array_needs_making = 0; + } +} + +static int +bindpwd (no_symlinks) + int no_symlinks; +{ + char *dirname, *pwdvar; + int old_anm, r; + SHELL_VAR *tvar; + + r = sh_chkwrite (EXECUTION_SUCCESS); + +#define tcwd the_current_working_directory + dirname = tcwd ? (no_symlinks ? sh_physpath (tcwd, 0) : tcwd) + : get_working_directory ("cd"); +#undef tcwd + + old_anm = array_needs_making; + pwdvar = get_string_value ("PWD"); + + tvar = bind_variable ("OLDPWD", pwdvar, 0); + if (old_anm == 0 && array_needs_making && exported_p (tvar)) + { + update_export_env_inplace ("OLDPWD=", 7, pwdvar); + array_needs_making = 0; + } + + setpwd (dirname); + + if (dirname && dirname != the_current_working_directory) + free (dirname); + + return (r); +} + +/* Call get_working_directory to reset the value of + the_current_working_directory () */ +static char * +resetpwd (caller) + char *caller; +{ + char *tdir; + + FREE (the_current_working_directory); + the_current_working_directory = (char *)NULL; + tdir = get_working_directory (caller); + return (tdir); +} + +#define LCD_DOVARS 0x001 +#define LCD_DOSPELL 0x002 +#define LCD_PRINTPATH 0x004 +#define LCD_FREEDIRNAME 0x008 + +/* This builtin is ultimately the way that all user-visible commands should + change the current working directory. It is called by cd_to_string (), + so the programming interface is simple, and it handles errors and + restrictions properly. */ +int +cd_builtin (list) + WORD_LIST *list; +{ + char *dirname, *cdpath, *path, *temp; + int path_index, no_symlinks, opt, lflag; + +#if defined (RESTRICTED_SHELL) + if (restricted) + { + sh_restricted ((char *)NULL); + return (EXECUTION_FAILURE); + } +#endif /* RESTRICTED_SHELL */ + + no_symlinks = no_symbolic_links; + reset_internal_getopt (); + while ((opt = internal_getopt (list, "LP")) != -1) + { + switch (opt) + { + case 'P': + no_symlinks = 1; + break; + case 'L': + no_symlinks = 0; + break; + default: + builtin_usage (); + return (EXECUTION_FAILURE); + } + } + list = loptend; + + lflag = (cdable_vars ? LCD_DOVARS : 0) | + ((interactive && cdspelling) ? LCD_DOSPELL : 0); + + if (list == 0) + { + /* `cd' without arguments is equivalent to `cd $HOME' */ + dirname = get_string_value ("HOME"); + + if (dirname == 0) + { + builtin_error (_("HOME not set")); + return (EXECUTION_FAILURE); + } + lflag = 0; + } + else if (list->word->word[0] == '-' && list->word->word[1] == '\0') + { + /* This is `cd -', equivalent to `cd $OLDPWD' */ + dirname = get_string_value ("OLDPWD"); + + if (dirname == 0) + { + builtin_error (_("OLDPWD not set")); + return (EXECUTION_FAILURE); + } +#if 0 + lflag = interactive ? LCD_PRINTPATH : 0; +#else + lflag = LCD_PRINTPATH; /* According to SUSv3 */ +#endif + } + else if (absolute_pathname (list->word->word)) + dirname = list->word->word; + else if (privileged_mode == 0 && (cdpath = get_string_value ("CDPATH"))) + { + dirname = list->word->word; + + /* Find directory in $CDPATH. */ + path_index = 0; + while (path = extract_colon_unit (cdpath, &path_index)) + { + /* OPT is 1 if the path element is non-empty */ + opt = path[0] != '\0'; + temp = sh_makepath (path, dirname, MP_DOTILDE); + free (path); + + if (change_to_directory (temp, no_symlinks)) + { + /* POSIX.2 says that if a nonempty directory from CDPATH + is used to find the directory to change to, the new + directory name is echoed to stdout, whether or not + the shell is interactive. */ + if (opt && (path = no_symlinks ? temp : the_current_working_directory)) + printf ("%s\n", path); + + free (temp); +#if 0 + /* Posix.2 says that after using CDPATH, the resultant + value of $PWD will not contain `.' or `..'. */ + return (bindpwd (posixly_correct || no_symlinks)); +#else + return (bindpwd (no_symlinks)); +#endif + } + else + free (temp); + } + + /* POSIX.2 says that if `.' does not appear in $CDPATH, we don't + try the current directory, so we just punt now with an error + message if POSIXLY_CORRECT is non-zero. The check for cdpath[0] + is so we don't mistakenly treat a CDPATH value of "" as not + specifying the current directory. */ + if (posixly_correct && cdpath[0]) + { + builtin_error ("%s: %s", dirname, strerror (ENOENT)); + return (EXECUTION_FAILURE); + } + } + else + dirname = list->word->word; + + /* When we get here, DIRNAME is the directory to change to. If we + chdir successfully, just return. */ + if (change_to_directory (dirname, no_symlinks)) + { + if (lflag & LCD_PRINTPATH) + printf ("%s\n", dirname); + return (bindpwd (no_symlinks)); + } + + /* If the user requests it, then perhaps this is the name of + a shell variable, whose value contains the directory to + change to. */ + if (lflag & LCD_DOVARS) + { + temp = get_string_value (dirname); + if (temp && change_to_directory (temp, no_symlinks)) + { + printf ("%s\n", temp); + return (bindpwd (no_symlinks)); + } + } + + /* If the user requests it, try to find a directory name similar in + spelling to the one requested, in case the user made a simple + typo. This is similar to the UNIX 8th and 9th Edition shells. */ + if (lflag & LCD_DOSPELL) + { + temp = dirspell (dirname); + if (temp && change_to_directory (temp, no_symlinks)) + { + printf ("%s\n", temp); + return (bindpwd (no_symlinks)); + } + else + FREE (temp); + } + + builtin_error ("%s: %s", dirname, strerror (errno)); + return (EXECUTION_FAILURE); +} + +$BUILTIN pwd +$FUNCTION pwd_builtin +$SHORT_DOC pwd [-LP] +Print the name of the current working directory. + +Options: + -L print the value of $PWD if it names the current working + directory + -P print the physical directory, without any symbolic links + +By default, `pwd' behaves as if `-L' were specified. + +Exit Status: +Returns 0 unless an invalid option is given or the current directory +cannot be read. +$END + +/* Non-zero means that pwd always prints the physical directory, without + symbolic links. */ +static int verbatim_pwd; + +/* Print the name of the current working directory. */ +int +pwd_builtin (list) + WORD_LIST *list; +{ + char *directory; + int opt, pflag; + + verbatim_pwd = no_symbolic_links; + pflag = 0; + reset_internal_getopt (); + while ((opt = internal_getopt (list, "LP")) != -1) + { + switch (opt) + { + case 'P': + verbatim_pwd = pflag = 1; + break; + case 'L': + verbatim_pwd = 0; + break; + default: + builtin_usage (); + return (EXECUTION_FAILURE); + } + } + list = loptend; + +#define tcwd the_current_working_directory + + directory = tcwd ? (verbatim_pwd ? sh_physpath (tcwd, 0) : tcwd) + : get_working_directory ("pwd"); + + /* Try again using getcwd() if canonicalization fails (for instance, if + the file system has changed state underneath bash). */ + if ((tcwd && directory == 0) || + (posixly_correct && same_file (".", tcwd, (struct stat *)0, (struct stat *)0) == 0)) + directory = resetpwd ("pwd"); + +#undef tcwd + + if (directory) + { + printf ("%s\n", directory); + /* This is dumb but posix-mandated. */ + if (posixly_correct && pflag) + setpwd (directory); + if (directory != the_current_working_directory) + free (directory); + return (sh_chkwrite (EXECUTION_SUCCESS)); + } + else + return (EXECUTION_FAILURE); +} + +/* Do the work of changing to the directory NEWDIR. Handle symbolic + link following, etc. This function *must* return with + the_current_working_directory either set to NULL (in which case + getcwd() will eventually be called), or set to a string corresponding + to the working directory. Return 1 on success, 0 on failure. */ + +static int +change_to_directory (newdir, nolinks) + char *newdir; + int nolinks; +{ + char *t, *tdir; + int err, canon_failed, r, ndlen, dlen; + + tdir = (char *)NULL; + + if (the_current_working_directory == 0) + { + t = get_working_directory ("chdir"); + FREE (t); + } + + t = make_absolute (newdir, the_current_working_directory); + + /* TDIR is either the canonicalized absolute pathname of NEWDIR + (nolinks == 0) or the absolute physical pathname of NEWDIR + (nolinks != 0). */ + tdir = nolinks ? sh_physpath (t, 0) + : sh_canonpath (t, PATH_CHECKDOTDOT|PATH_CHECKEXISTS); + + ndlen = strlen (newdir); + dlen = strlen (t); + + /* Use the canonicalized version of NEWDIR, or, if canonicalization + failed, use the non-canonical form. */ + canon_failed = 0; + if (tdir && *tdir) + free (t); + else + { + FREE (tdir); + tdir = t; + canon_failed = 1; + } + + /* In POSIX mode, if we're resolving symlinks logically and sh_canonpath + returns NULL (because it checks the path, it will return NULL if the + resolved path doesn't exist), fail immediately. */ + if (posixly_correct && nolinks == 0 && canon_failed && (errno != ENAMETOOLONG || ndlen > PATH_MAX)) + { +#if defined ENAMETOOLONG + if (errno != ENOENT && errno != ENAMETOOLONG) +#else + if (errno != ENOENT) +#endif + errno = ENOTDIR; + free (tdir); + return (0); + } + + /* If the chdir succeeds, update the_current_working_directory. */ + if (chdir (nolinks ? newdir : tdir) == 0) + { + /* If canonicalization failed, but the chdir succeeded, reset the + shell's idea of the_current_working_directory. */ + if (canon_failed) + { + t = resetpwd ("cd"); + if (t == 0) + set_working_directory (tdir); + } + else + set_working_directory (tdir); + + free (tdir); + return (1); + } + + /* We failed to change to the appropriate directory name. If we tried + what the user passed (nolinks != 0), punt now. */ + if (nolinks) + { + free (tdir); + return (0); + } + + err = errno; + + /* We're not in physical mode (nolinks == 0), but we failed to change to + the canonicalized directory name (TDIR). Try what the user passed + verbatim. If we succeed, reinitialize the_current_working_directory. */ + if (chdir (newdir) == 0) + { + t = resetpwd ("cd"); + if (t == 0) + set_working_directory (tdir); + else + free (t); + + r = 1; + } + else + { + errno = err; + r = 0; + } + + free (tdir); + return r; +} diff --git a/builtins/common.c b/builtins/common.c index fbfb92a5..8adb1923 100644 --- a/builtins/common.c +++ b/builtins/common.c @@ -1,6 +1,6 @@ /* common.c - utility functions for all builtins */ -/* Copyright (C) 1987-2009 Free Software Foundation, Inc. +/* Copyright (C) 1987-2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. diff --git a/builtins/common.c~ b/builtins/common.c~ index f02e99c8..fbfb92a5 100644 --- a/builtins/common.c~ +++ b/builtins/common.c~ @@ -367,7 +367,7 @@ make_builtin_argv (list, ip) return argv; } -/* Remember LIST in $0 ... $9, and REST_OF_ARGS. If DESTRUCTIVE is +/* Remember LIST in $1 ... $9, and REST_OF_ARGS. If DESTRUCTIVE is non-zero, then discard whatever the existing arguments are, else only discard the ones that are to be replaced. */ void diff --git a/builtins/common.h b/builtins/common.h index efbb0785..caeefeab 100644 --- a/builtins/common.h +++ b/builtins/common.h @@ -1,6 +1,6 @@ /* common.h -- extern declarations for functions defined in common.c. */ -/* Copyright (C) 1993-2004 Free Software Foundation, Inc. +/* Copyright (C) 1993-2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. diff --git a/builtins/common.h~ b/builtins/common.h~ new file mode 100644 index 00000000..efbb0785 --- /dev/null +++ b/builtins/common.h~ @@ -0,0 +1,174 @@ +/* common.h -- extern declarations for functions defined in common.c. */ + +/* Copyright (C) 1993-2004 Free Software Foundation, Inc. + + This file is part of GNU Bash, the Bourne Again SHell. + + Bash 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 3 of the License, or + (at your option) any later version. + + Bash 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 Bash. If not, see <http://www.gnu.org/licenses/>. +*/ + +#if !defined (__COMMON_H) +# define __COMMON_H + +#include "stdc.h" + +#define ISOPTION(s, c) (s[0] == '-' && !s[2] && s[1] == c) + +/* Flag values for parse_and_execute () */ +#define SEVAL_NONINT 0x001 +#define SEVAL_INTERACT 0x002 +#define SEVAL_NOHIST 0x004 +#define SEVAL_NOFREE 0x008 +#define SEVAL_RESETLINE 0x010 +#define SEVAL_PARSEONLY 0x020 +#define SEVAL_NOLONGJMP 0x040 + +/* Flags for describe_command, shared between type.def and command.def */ +#define CDESC_ALL 0x001 /* type -a */ +#define CDESC_SHORTDESC 0x002 /* command -V */ +#define CDESC_REUSABLE 0x004 /* command -v */ +#define CDESC_TYPE 0x008 /* type -t */ +#define CDESC_PATH_ONLY 0x010 /* type -p */ +#define CDESC_FORCE_PATH 0x020 /* type -ap or type -P */ +#define CDESC_NOFUNCS 0x040 /* type -f */ +#define CDESC_ABSPATH 0x080 /* convert to absolute path, no ./ */ + +/* Flags for get_job_by_name */ +#define JM_PREFIX 0x01 /* prefix of job name */ +#define JM_SUBSTRING 0x02 /* substring of job name */ +#define JM_EXACT 0x04 /* match job name exactly */ +#define JM_STOPPED 0x08 /* match stopped jobs only */ +#define JM_FIRSTMATCH 0x10 /* return first matching job */ + +/* Flags for remember_args and value of changed_dollar_vars */ +#define ARGS_NONE 0x0 +#define ARGS_INVOC 0x01 +#define ARGS_FUNC 0x02 +#define ARGS_SETBLTIN 0x04 + +/* Functions from common.c */ +extern void builtin_error __P((const char *, ...)) __attribute__((__format__ (printf, 1, 2))); +extern void builtin_warning __P((const char *, ...)) __attribute__((__format__ (printf, 1, 2))); +extern void builtin_usage __P((void)); +extern void no_args __P((WORD_LIST *)); +extern int no_options __P((WORD_LIST *)); + +/* common error message functions */ +extern void sh_needarg __P((char *)); +extern void sh_neednumarg __P((char *)); +extern void sh_notfound __P((char *)); +extern void sh_invalidopt __P((char *)); +extern void sh_invalidoptname __P((char *)); +extern void sh_invalidid __P((char *)); +extern void sh_invalidnum __P((char *)); +extern void sh_invalidsig __P((char *)); +extern void sh_erange __P((char *, char *)); +extern void sh_badpid __P((char *)); +extern void sh_badjob __P((char *)); +extern void sh_readonly __P((const char *)); +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 *)); +extern void remember_args __P((WORD_LIST *, int)); + +extern int dollar_vars_changed __P((void)); +extern void set_dollar_vars_unchanged __P((void)); +extern void set_dollar_vars_changed __P((void)); + +extern int get_numeric_arg __P((WORD_LIST *, int, intmax_t *)); +extern int get_exitstat __P((WORD_LIST *)); +extern int read_octal __P((char *)); + +/* Keeps track of the current working directory. */ +extern char *the_current_working_directory; +extern char *get_working_directory __P((char *)); +extern void set_working_directory __P((char *)); + +#if defined (JOB_CONTROL) +extern int get_job_by_name __P((const char *, int)); +extern int get_job_spec __P((WORD_LIST *)); +#endif +extern int display_signal_list __P((WORD_LIST *, int)); + +/* It's OK to declare a function as returning a Function * without + providing a definition of what a `Function' is. */ +extern struct builtin *builtin_address_internal __P((char *, int)); +extern sh_builtin_func_t *find_shell_builtin __P((char *)); +extern sh_builtin_func_t *builtin_address __P((char *)); +extern sh_builtin_func_t *find_special_builtin __P((char *)); +extern void initialize_shell_builtins __P((void)); + +/* Functions from exit.def */ +extern void bash_logout __P((void)); + +/* Functions from getopts.def */ +extern void getopts_reset __P((int)); + +/* Functions from set.def */ +extern int minus_o_option_value __P((char *)); +extern void list_minus_o_opts __P((int, int)); +extern char **get_minus_o_opts __P((void)); +extern int set_minus_o_option __P((int, char *)); + +extern void set_shellopts __P((void)); +extern void parse_shellopts __P((char *)); +extern void initialize_shell_options __P((int)); + +extern void reset_shell_options __P((void)); + +/* Functions from shopt.def */ +extern void reset_shopt_options __P((void)); +extern char **get_shopt_options __P((void)); + +extern int shopt_setopt __P((char *, int)); +extern int shopt_listopt __P((char *, int)); + +extern int set_login_shell __P((char *, int)); + +extern void set_bashopts __P((void)); +extern void parse_bashopts __P((char *)); +extern void initialize_bashopts __P((int)); + +/* Functions from type.def */ +extern int describe_command __P((char *, int)); + +/* Functions from setattr.def */ +extern int set_or_show_attributes __P((WORD_LIST *, int, int)); +extern int show_all_var_attributes __P((int, int)); +extern int show_var_attributes __P((SHELL_VAR *, int, int)); +extern int show_name_attributes __P((char *, int)); +extern void set_var_attribute __P((char *, int, int)); + +/* Functions from pushd.def */ +extern char *get_dirstack_from_string __P((char *)); +extern char *get_dirstack_element __P((intmax_t, int)); +extern void set_dirstack_element __P((intmax_t, int, char *)); +extern WORD_LIST *get_directory_stack __P((int)); + +/* Functions from evalstring.c */ +extern int parse_and_execute __P((char *, const char *, int)); +extern void parse_and_execute_cleanup __P((void)); +extern int parse_string __P((char *, const char *, int, char **)); + +/* Functions from evalfile.c */ +extern int maybe_execute_file __P((const char *, int)); +extern int source_file __P((const char *, int)); +extern int fc_execute_file __P((const char *)); + +#endif /* !__COMMON_H */ diff --git a/builtins/complete.def b/builtins/complete.def index d7d6a460..b9f0a134 100644 --- a/builtins/complete.def +++ b/builtins/complete.def @@ -1,7 +1,7 @@ This file is complete.def, from which is created complete.c. It implements the builtins "complete", "compgen", and "compopt" in Bash. -Copyright (C) 1999-2009 Free Software Foundation, Inc. +Copyright (C) 1999-2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. diff --git a/builtins/complete.def~ b/builtins/complete.def~ index 2267794d..d7d6a460 100644 --- a/builtins/complete.def~ +++ b/builtins/complete.def~ @@ -753,7 +753,7 @@ $SHORT_DOC compopt [-o|+o option] [-DE] [name ...] Modify or display completion options. Modify the completion options for each NAME, or, if no NAMEs are supplied, -the completion currently begin executed. If no OPTIONs are givenm, print +the completion currently being executed. If no OPTIONs are given, print the completion options for each NAME or the current completion specification. Options: diff --git a/builtins/declare.def b/builtins/declare.def index 811d8e64..55206fa7 100644 --- a/builtins/declare.def +++ b/builtins/declare.def @@ -1,7 +1,7 @@ This file is declare.def, from which is created declare.c. It implements the builtins "declare" and "local" in Bash. -Copyright (C) 1987-2009 Free Software Foundation, Inc. +Copyright (C) 1987-2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. @@ -22,7 +22,7 @@ $PRODUCES declare.c $BUILTIN declare $FUNCTION declare_builtin -$SHORT_DOC declare [-aAfFilrtux] [-p] [name[=value] ...] +$SHORT_DOC declare [-aAfFgilrtux] [-p] [name[=value] ...] Set variable values and attributes. Declare variables and give them attributes. If no NAMEs are given, @@ -32,6 +32,8 @@ Options: -f restrict action or display to function names and definitions -F restrict display to function names only (plus line number and source file when debugging) + -g create global variables when used in a shell function; otherwise + ignored -p display the attributes and value of each NAME Options which set attributes: @@ -50,7 +52,7 @@ Variables with the integer attribute have arithmetic evaluation (see the `let' command) performed when the variable is assigned a value. When used in a function, `declare' makes NAMEs local, as with the `local' -command. +command. The `-g' option suppresses this behavior. Exit Status: Returns success unless an invalid option is supplied or an error occurs. @@ -58,7 +60,7 @@ $END $BUILTIN typeset $FUNCTION declare_builtin -$SHORT_DOC typeset [-aAfFilrtux] [-p] name[=value] ... +$SHORT_DOC typeset [-aAfFgilrtux] [-p] name[=value] ... Set variable values and attributes. Obsolete. See `help declare'. @@ -125,9 +127,9 @@ local_builtin (list) } #if defined (ARRAY_VARS) -# define DECLARE_OPTS "+acfilprtuxAF" +# define DECLARE_OPTS "+acfgilprtuxAF" #else -# define DECLARE_OPTS "+cfilprtuxF" +# define DECLARE_OPTS "+cfgilprtuxF" #endif /* The workhorse function. */ @@ -137,12 +139,12 @@ declare_internal (list, local_var) int local_var; { int flags_on, flags_off, *flags; - int any_failed, assign_error, pflag, nodefs, opt; + int any_failed, assign_error, pflag, nodefs, opt, mkglobal; char *t, *subscript_start; SHELL_VAR *var; FUNCTION_DEF *shell_fn; - flags_on = flags_off = any_failed = assign_error = pflag = nodefs = 0; + flags_on = flags_off = any_failed = assign_error = pflag = nodefs = mkglobal = 0; reset_internal_getopt (); while ((opt = internal_getopt (list, DECLARE_OPTS)) != EOF) { @@ -177,6 +179,10 @@ declare_internal (list, local_var) case 'f': *flags |= att_function; break; + case 'g': + if (flags == &flags_on) + mkglobal = 1; + break; case 'i': *flags |= att_integer; break; @@ -328,7 +334,7 @@ declare_internal (list, local_var) /* XXX - this has consequences when we're making a local copy of a variable that was in the temporary environment. Watch out for this. */ - if (variable_context && ((flags_on & att_function) == 0)) + if (variable_context && mkglobal == 0 && ((flags_on & att_function) == 0)) { #if defined (ARRAY_VARS) if (flags_on & att_assoc) @@ -410,7 +416,7 @@ declare_internal (list, local_var) { /* Non-null if we just created or fetched a local variable. */ if (var == 0) - var = find_variable (name); + var = mkglobal ? find_global_variable (name) : find_variable (name); if (var == 0) { diff --git a/builtins/declare.def~ b/builtins/declare.def~ index a0ce6054..5a16c19e 100644 --- a/builtins/declare.def~ +++ b/builtins/declare.def~ @@ -22,7 +22,7 @@ $PRODUCES declare.c $BUILTIN declare $FUNCTION declare_builtin -$SHORT_DOC declare [-aAfFilrtux] [-p] [name[=value] ...] +$SHORT_DOC declare [-aAfFgilrtux] [-p] [name[=value] ...] Set variable values and attributes. Declare variables and give them attributes. If no NAMEs are given, @@ -32,6 +32,8 @@ Options: -f restrict action or display to function names and definitions -F restrict display to function names only (plus line number and source file when debugging) + -g create global variables when used in a shell function; otherwise + ignored -p display the attributes and value of each NAME Options which set attributes: @@ -50,7 +52,7 @@ Variables with the integer attribute have arithmetic evaluation (see the `let' command) performed when the variable is assigned a value. When used in a function, `declare' makes NAMEs local, as with the `local' -command. +command. The `-g' option suppresses this behavior. Exit Status: Returns success unless an invalid option is supplied or an error occurs. @@ -58,7 +60,7 @@ $END $BUILTIN typeset $FUNCTION declare_builtin -$SHORT_DOC typeset [-aAfFilrtux] [-p] name[=value] ... +$SHORT_DOC typeset [-aAfFgilrtux] [-p] name[=value] ... Set variable values and attributes. Obsolete. See `help declare'. @@ -125,9 +127,9 @@ local_builtin (list) } #if defined (ARRAY_VARS) -# define DECLARE_OPTS "+acfilprtuxAF" +# define DECLARE_OPTS "+acfgilprtuxAF" #else -# define DECLARE_OPTS "+cfilprtuxF" +# define DECLARE_OPTS "+cfgilprtuxF" #endif /* The workhorse function. */ @@ -137,12 +139,12 @@ declare_internal (list, local_var) int local_var; { int flags_on, flags_off, *flags; - int any_failed, assign_error, pflag, nodefs, opt; + int any_failed, assign_error, pflag, nodefs, opt, mkglobal; char *t, *subscript_start; SHELL_VAR *var; FUNCTION_DEF *shell_fn; - flags_on = flags_off = any_failed = assign_error = pflag = nodefs = 0; + flags_on = flags_off = any_failed = assign_error = pflag = nodefs = mkglobal = 0; reset_internal_getopt (); while ((opt = internal_getopt (list, DECLARE_OPTS)) != EOF) { @@ -177,6 +179,10 @@ declare_internal (list, local_var) case 'f': *flags |= att_function; break; + case 'g': + if (flags == &flags_on) + mkglobal = 1; + break; case 'i': *flags |= att_integer; break; @@ -328,7 +334,7 @@ declare_internal (list, local_var) /* XXX - this has consequences when we're making a local copy of a variable that was in the temporary environment. Watch out for this. */ - if (variable_context && ((flags_on & att_function) == 0)) + if (variable_context && mkglobal == 0 && ((flags_on & att_function) == 0)) { #if defined (ARRAY_VARS) if (flags_on & att_assoc) @@ -410,7 +416,7 @@ declare_internal (list, local_var) { /* Non-null if we just created or fetched a local variable. */ if (var == 0) - var = find_variable (name); + var = mkglobal ? find_global_variable (name) : find_variable (name); if (var == 0) { @@ -512,7 +518,7 @@ declare_internal (list, local_var) { /* let bind_{array,assoc}_variable take care of this. */ if (assoc_p (var)) - bind_assoc_variable (var, name, "0", value, aflags); + bind_assoc_variable (var, name, savestring ("0"), value, aflags); else bind_array_variable (name, 0, value, aflags); } diff --git a/builtins/evalstring.c b/builtins/evalstring.c index 40abe009..8c9833b7 100644 --- a/builtins/evalstring.c +++ b/builtins/evalstring.c @@ -1,6 +1,6 @@ /* evalstring.c - evaluate a string as one or more shell commands. -/* Copyright (C) 1996-2009 Free Software Foundation, Inc. +/* Copyright (C) 1996-2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. @@ -331,13 +331,9 @@ parse_and_execute (string, from_file, flags) (this_shell_builtin == source_builtin || this_shell_builtin == eval_builtin) && last_command_exit_value == EX_BADSYNTAX && posixly_correct) { -#if 0 /* XXX - for bash-4.2 */ should_jump_to_top_level = 1; code = ERREXIT; last_command_exit_value = EX_BADUSAGE; -#else - internal_warning (_("syntax errors in . or eval will cause future versions of the shell to abort as Posix requires")); -#endif } /* Since we are shell compatible, syntax errors in a script diff --git a/builtins/evalstring.c~ b/builtins/evalstring.c~ new file mode 100644 index 00000000..82c0d96a --- /dev/null +++ b/builtins/evalstring.c~ @@ -0,0 +1,508 @@ +/* evalstring.c - evaluate a string as one or more shell commands. + +/* Copyright (C) 1996-2009 Free Software Foundation, Inc. + + This file is part of GNU Bash, the Bourne Again SHell. + + Bash 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 3 of the License, or + (at your option) any later version. + + Bash 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 Bash. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <config.h> + +#if defined (HAVE_UNISTD_H) +# ifdef _MINIX +# include <sys/types.h> +# endif +# include <unistd.h> +#endif + +#include <stdio.h> +#include <signal.h> + +#include <errno.h> + +#include "filecntl.h" +#include "../bashansi.h" + +#include "../shell.h" +#include "../jobs.h" +#include "../builtins.h" +#include "../flags.h" +#include "../input.h" +#include "../execute_cmd.h" +#include "../redir.h" +#include "../trap.h" +#include "../bashintl.h" + +#include <y.tab.h> + +#if defined (HISTORY) +# include "../bashhist.h" +#endif + +#include "common.h" +#include "builtext.h" + +#if !defined (errno) +extern int errno; +#endif + +#define IS_BUILTIN(s) (builtin_address_internal(s, 0) != (struct builtin *)NULL) + +extern int indirection_level, subshell_environment; +extern int line_number; +extern int current_token, shell_eof_token; +extern int last_command_exit_value; +extern int running_trap; +extern int loop_level; +extern int executing_list; +extern int comsub_ignore_return; +extern int posixly_correct; +extern sh_builtin_func_t *this_shell_builtin; + +int parse_and_execute_level = 0; + +static int cat_file __P((REDIRECT *)); + +#define PE_TAG "parse_and_execute top" +#define PS_TAG "parse_string top" + +#if defined (HISTORY) +static void +set_history_remembering () +{ + remember_on_history = enable_history_list; +} +#endif + +/* How to force parse_and_execute () to clean up after itself. */ +void +parse_and_execute_cleanup () +{ + if (running_trap) + { + run_trap_cleanup (running_trap - 1); + unfreeze_jobs_list (); + } + + if (have_unwind_protects ()) + run_unwind_frame (PE_TAG); + else + parse_and_execute_level = 0; /* XXX */ +} + +static void +parse_prologue (string, flags, tag) + char *string; + int flags; + char *tag; +{ + char *orig_string; + int x; + + orig_string = string; + /* Unwind protect this invocation of parse_and_execute (). */ + begin_unwind_frame (tag); + unwind_protect_int (parse_and_execute_level); + unwind_protect_jmp_buf (top_level); + unwind_protect_int (indirection_level); + unwind_protect_int (line_number); + unwind_protect_int (loop_level); + unwind_protect_int (executing_list); + unwind_protect_int (comsub_ignore_return); + if (flags & (SEVAL_NONINT|SEVAL_INTERACT)) + unwind_protect_int (interactive); + +#if defined (HISTORY) + if (parse_and_execute_level == 0) + add_unwind_protect (set_history_remembering, (char *)NULL); + else + unwind_protect_int (remember_on_history); /* can be used in scripts */ +# if defined (BANG_HISTORY) + if (interactive_shell) + unwind_protect_int (history_expansion_inhibited); +# endif /* BANG_HISTORY */ +#endif /* HISTORY */ + + if (interactive_shell) + { + x = get_current_prompt_level (); + add_unwind_protect (set_current_prompt_level, x); + } + + add_unwind_protect (pop_stream, (char *)NULL); + if (orig_string && ((flags & SEVAL_NOFREE) == 0)) + add_unwind_protect (xfree, orig_string); + end_unwind_frame (); + + if (flags & (SEVAL_NONINT|SEVAL_INTERACT)) + interactive = (flags & SEVAL_NONINT) ? 0 : 1; + +#if defined (HISTORY) + if (flags & SEVAL_NOHIST) + bash_history_disable (); +#endif /* HISTORY */ +} + +/* Parse and execute the commands in STRING. Returns whatever + execute_command () returns. This frees STRING. FLAGS is a + flags word; look in common.h for the possible values. Actions + are: + (flags & SEVAL_NONINT) -> interactive = 0; + (flags & SEVAL_INTERACT) -> interactive = 1; + (flags & SEVAL_NOHIST) -> call bash_history_disable () + (flags & SEVAL_NOFREE) -> don't free STRING when finished + (flags & SEVAL_RESETLINE) -> reset line_number to 1 +*/ + +int +parse_and_execute (string, from_file, flags) + char *string; + const char *from_file; + int flags; +{ + int code, lreset; + volatile int should_jump_to_top_level, last_result; + COMMAND *volatile command; + + parse_prologue (string, flags, PE_TAG); + + parse_and_execute_level++; + + lreset = flags & SEVAL_RESETLINE; + + /* Reset the line number if the caller wants us to. If we don't reset the + line number, we have to subtract one, because we will add one just + before executing the next command (resetting the line number sets it to + 0; the first line number is 1). */ + push_stream (lreset); + if (lreset == 0) + line_number--; + + indirection_level++; + + code = should_jump_to_top_level = 0; + last_result = EXECUTION_SUCCESS; + + with_input_from_string (string, from_file); + while (*(bash_input.location.string)) + { + command = (COMMAND *)NULL; + + if (interrupt_state) + { + last_result = EXECUTION_FAILURE; + break; + } + + /* Provide a location for functions which `longjmp (top_level)' to + jump to. This prevents errors in substitution from restarting + the reader loop directly, for example. */ + code = setjmp (top_level); + + if (code) + { + should_jump_to_top_level = 0; + switch (code) + { + case FORCE_EOF: + case ERREXIT: + case EXITPROG: + if (command) + run_unwind_frame ("pe_dispose"); + /* Remember to call longjmp (top_level) after the old + value for it is restored. */ + should_jump_to_top_level = 1; + goto out; + + case DISCARD: + if (command) + run_unwind_frame ("pe_dispose"); + last_result = last_command_exit_value = EXECUTION_FAILURE; /* XXX */ + if (subshell_environment) + { + should_jump_to_top_level = 1; + goto out; + } + else + { +#if 0 + dispose_command (command); /* pe_dispose does this */ +#endif + continue; + } + + default: + command_error ("parse_and_execute", CMDERR_BADJUMP, code, 0); + break; + } + } + + if (parse_command () == 0) + { + if ((flags & SEVAL_PARSEONLY) || (interactive_shell == 0 && read_but_dont_execute)) + { + last_result = EXECUTION_SUCCESS; + dispose_command (global_command); + global_command = (COMMAND *)NULL; + } + else if (command = global_command) + { + struct fd_bitmap *bitmap; + + bitmap = new_fd_bitmap (FD_BITMAP_SIZE); + begin_unwind_frame ("pe_dispose"); + add_unwind_protect (dispose_fd_bitmap, bitmap); + add_unwind_protect (dispose_command, command); /* XXX */ + + global_command = (COMMAND *)NULL; + + if ((subshell_environment & SUBSHELL_COMSUB) && comsub_ignore_return) + command->flags |= CMD_IGNORE_RETURN; + +#if defined (ONESHOT) + /* + * IF + * we were invoked as `bash -c' (startup_state == 2) AND + * parse_and_execute has not been called recursively AND + * we're not running a trap AND + * we have parsed the full command (string == '\0') AND + * we're not going to run the exit trap AND + * we have a simple command without redirections AND + * the command is not being timed AND + * the command's return status is not being inverted + * THEN + * tell the execution code that we don't need to fork + */ + if (startup_state == 2 && parse_and_execute_level == 1 && + running_trap == 0 && + *bash_input.location.string == '\0' && + command->type == cm_simple && + signal_is_trapped (EXIT_TRAP) == 0 && + command->redirects == 0 && command->value.Simple->redirects == 0 && + ((command->flags & CMD_TIME_PIPELINE) == 0) && + ((command->flags & CMD_INVERT_RETURN) == 0)) + { + command->flags |= CMD_NO_FORK; + command->value.Simple->flags |= CMD_NO_FORK; + } +#endif /* ONESHOT */ + + /* See if this is a candidate for $( <file ). */ + if (startup_state == 2 && + (subshell_environment & SUBSHELL_COMSUB) && + *bash_input.location.string == '\0' && + command->type == cm_simple && !command->redirects && + (command->flags & CMD_TIME_PIPELINE) == 0 && + command->value.Simple->words == 0 && + command->value.Simple->redirects && + command->value.Simple->redirects->next == 0 && + command->value.Simple->redirects->instruction == r_input_direction) + { + int r; + r = cat_file (command->value.Simple->redirects); + last_result = (r < 0) ? EXECUTION_FAILURE : EXECUTION_SUCCESS; + } + else + last_result = execute_command_internal + (command, 0, NO_PIPE, NO_PIPE, bitmap); + + dispose_command (command); + dispose_fd_bitmap (bitmap); + discard_unwind_frame ("pe_dispose"); + } + } + else + { + last_result = EXECUTION_FAILURE; + + if (interactive_shell == 0 && this_shell_builtin && + (this_shell_builtin == source_builtin || this_shell_builtin == eval_builtin) && + last_command_exit_value == EX_BADSYNTAX && posixly_correct) + { + should_jump_to_top_level = 1; + code = ERREXIT; + last_command_exit_value = EX_BADUSAGE; + } + + /* Since we are shell compatible, syntax errors in a script + abort the execution of the script. Right? */ + break; + } + } + + out: + + run_unwind_frame (PE_TAG); + + if (interrupt_state && parse_and_execute_level == 0) + { + /* An interrupt during non-interactive execution in an + interactive shell (e.g. via $PROMPT_COMMAND) should + not cause the shell to exit. */ + interactive = interactive_shell; + throw_to_top_level (); + } + + if (should_jump_to_top_level) + jump_to_top_level (code); + + return (last_result); +} + +/* Parse a command contained in STRING according to FLAGS and return the + number of characters consumed from the string. If non-NULL, set *ENDP + to the position in the string where the parse ended. Used to validate + command substitutions during parsing to obey Posix rules about finding + the end of the command and balancing parens. */ +int +parse_string (string, from_file, flags, endp) + char *string; + const char *from_file; + int flags; + char **endp; +{ + int code, nc; + volatile int should_jump_to_top_level; + COMMAND *volatile command, *oglobal; + char *ostring; + + parse_prologue (string, flags, PS_TAG); + + /* Reset the line number if the caller wants us to. If we don't reset the + line number, we have to subtract one, because we will add one just + before executing the next command (resetting the line number sets it to + 0; the first line number is 1). */ + push_stream (0); + + code = should_jump_to_top_level = 0; + oglobal = global_command; + ostring = string; + + with_input_from_string (string, from_file); + while (*(bash_input.location.string)) + { + command = (COMMAND *)NULL; + +#if 0 + if (interrupt_state) + break; +#endif + + /* Provide a location for functions which `longjmp (top_level)' to + jump to. */ + code = setjmp (top_level); + + if (code) + { +#if defined (DEBUG) +itrace("parse_string: longjmp executed: code = %d", code); +#endif + should_jump_to_top_level = 0; + switch (code) + { + case FORCE_EOF: + case ERREXIT: + case EXITPROG: + case DISCARD: /* XXX */ + if (command) + dispose_command (command); + /* Remember to call longjmp (top_level) after the old + value for it is restored. */ + should_jump_to_top_level = 1; + goto out; + + default: + command_error ("parse_string", CMDERR_BADJUMP, code, 0); + break; + } + } + + if (parse_command () == 0) + { + dispose_command (global_command); + global_command = (COMMAND *)NULL; + } + else + { + if ((flags & SEVAL_NOLONGJMP) == 0) + { + should_jump_to_top_level = 1; + code = DISCARD; + } + else + reset_parser (); /* XXX - sets token_to_read */ + break; + } + + if (current_token == yacc_EOF || current_token == shell_eof_token) + break; + } + + out: + + global_command = oglobal; + nc = bash_input.location.string - ostring; + if (endp) + *endp = bash_input.location.string; + + run_unwind_frame (PS_TAG); + + if (should_jump_to_top_level) + jump_to_top_level (code); + + return (nc); +} + +/* Handle a $( < file ) command substitution. This expands the filename, + returning errors as appropriate, then just cats the file to the standard + output. */ +static int +cat_file (r) + REDIRECT *r; +{ + char *fn; + int fd, rval; + + if (r->instruction != r_input_direction) + return -1; + + /* Get the filename. */ + if (posixly_correct && !interactive_shell) + disallow_filename_globbing++; + fn = redirection_expand (r->redirectee.filename); + if (posixly_correct && !interactive_shell) + disallow_filename_globbing--; + + if (fn == 0) + { + redirection_error (r, AMBIGUOUS_REDIRECT); + return -1; + } + + fd = open(fn, O_RDONLY); + if (fd < 0) + { + file_error (fn); + free (fn); + return -1; + } + + rval = zcatfd (fd, 1, fn); + + free (fn); + close (fd); + + return (rval); +} diff --git a/builtins/exec.def b/builtins/exec.def index 9a1185e8..140407e4 100644 --- a/builtins/exec.def +++ b/builtins/exec.def @@ -1,7 +1,7 @@ This file is exec.def, from which is created exec.c. It implements the builtin "exec" in Bash. -Copyright (C) 1987-2009 Free Software Foundation, Inc. +Copyright (C) 1987-2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. diff --git a/builtins/exec.def~ b/builtins/exec.def~ index 434bfcb0..9a1185e8 100644 --- a/builtins/exec.def~ +++ b/builtins/exec.def~ @@ -164,7 +164,8 @@ exec_builtin (list) if (argv0) { free (args[0]); - exec_argv0 = args[0] = login ? mkdashname (argv0) : savestring (argv0); + args[0] = login ? mkdashname (argv0) : savestring (argv0); + exec_argv0 = savestring (args[0]); } else if (login) { diff --git a/builtins/fc.def b/builtins/fc.def index a9b67030..257029df 100644 --- a/builtins/fc.def +++ b/builtins/fc.def @@ -1,7 +1,7 @@ This file is fc.def, from which is created fc.c. It implements the builtin "fc" in Bash. -Copyright (C) 1987-2009 Free Software Foundation, Inc. +Copyright (C) 1987-2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. diff --git a/builtins/fc.def~ b/builtins/fc.def~ index c01f6c99..a9b67030 100644 --- a/builtins/fc.def~ +++ b/builtins/fc.def~ @@ -304,8 +304,8 @@ fc_builtin (list) last_hist = i - rh - hist_last_line_added; /* XXX */ - if (i == last_hist && hlist[last_hist] == 0) - while (saved_command_line_count > 0 && last_hist >= 0 && hlist[last_hist] == 0) + if (saved_command_line_count > 0 && i == last_hist && hlist[last_hist] == 0) + while (last_hist >= 0 && hlist[last_hist] == 0) last_hist--; if (last_hist < 0) { diff --git a/builtins/hash.def b/builtins/hash.def index 0cba874b..6724ad17 100644 --- a/builtins/hash.def +++ b/builtins/hash.def @@ -1,7 +1,7 @@ This file is hash.def, from which is created hash.c. It implements the builtin "hash" in Bash. -Copyright (C) 1987-2009 Free Software Foundation, Inc. +Copyright (C) 1987-2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. @@ -159,7 +159,9 @@ hash_builtin (list) { /* Add, remove or rehash the specified commands. */ w = list->word->word; - if (pathname) + if (absolute_program (w)) + continue; + else if (pathname) { if (is_directory (pathname)) { @@ -173,8 +175,6 @@ hash_builtin (list) else phash_insert (w, pathname, 0, 0); } - else if (absolute_program (w)) - continue; else if (delete) { if (phash_remove (w)) diff --git a/builtins/hash.def~ b/builtins/hash.def~ new file mode 100644 index 00000000..154b465a --- /dev/null +++ b/builtins/hash.def~ @@ -0,0 +1,282 @@ +This file is hash.def, from which is created hash.c. +It implements the builtin "hash" in Bash. + +Copyright (C) 1987-2009 Free Software Foundation, Inc. + +This file is part of GNU Bash, the Bourne Again SHell. + +Bash 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 3 of the License, or +(at your option) any later version. + +Bash 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 Bash. If not, see <http://www.gnu.org/licenses/>. + +$PRODUCES hash.c + +$BUILTIN hash +$FUNCTION hash_builtin +$SHORT_DOC hash [-lr] [-p pathname] [-dt] [name ...] +Remember or display program locations. + +Determine and remember the full pathname of each command NAME. If +no arguments are given, information about remembered commands is displayed. + +Options: + -d forget the remembered location of each NAME + -l display in a format that may be reused as input + -p pathname use PATHNAME is the full pathname of NAME + -r forget all remembered locations + -t print the remembered location of each NAME, preceding + each location with the corresponding NAME if multiple + NAMEs are given +Arguments: + NAME Each NAME is searched for in $PATH and added to the list + of remembered commands. + +Exit Status: +Returns success unless NAME is not found or an invalid option is given. +$END + +#include <config.h> + +#include <stdio.h> + +#include "../bashtypes.h" + +#if defined (HAVE_UNISTD_H) +# include <unistd.h> +#endif + +#include <errno.h> + +#include "../bashansi.h" +#include "../bashintl.h" + +#include "../shell.h" +#include "../builtins.h" +#include "../flags.h" +#include "../findcmd.h" +#include "../hashcmd.h" +#include "common.h" +#include "bashgetopt.h" + +extern int posixly_correct; +extern int dot_found_in_search; +extern char *this_command_name; + +static int add_hashed_command __P((char *, int)); +static int print_hash_info __P((BUCKET_CONTENTS *)); +static int print_portable_hash_info __P((BUCKET_CONTENTS *)); +static int print_hashed_commands __P((int)); +static int list_hashed_filename_targets __P((WORD_LIST *, int)); + +/* Print statistics on the current state of hashed commands. If LIST is + not empty, then rehash (or hash in the first place) the specified + commands. */ +int +hash_builtin (list) + WORD_LIST *list; +{ + int expunge_hash_table, list_targets, list_portably, delete, opt; + char *w, *pathname; + + if (hashing_enabled == 0) + { + builtin_error (_("hashing disabled")); + return (EXECUTION_FAILURE); + } + + expunge_hash_table = list_targets = list_portably = delete = 0; + pathname = (char *)NULL; + reset_internal_getopt (); + while ((opt = internal_getopt (list, "dlp:rt")) != -1) + { + switch (opt) + { + case 'd': + delete = 1; + break; + case 'l': + list_portably = 1; + break; + case 'p': + pathname = list_optarg; + break; + case 'r': + expunge_hash_table = 1; + break; + case 't': + list_targets = 1; + break; + default: + builtin_usage (); + return (EX_USAGE); + } + } + list = loptend; + + /* hash -t requires at least one argument. */ + if (list == 0 && list_targets) + { + sh_needarg ("-t"); + return (EXECUTION_FAILURE); + } + + /* We want hash -r to be silent, but hash -- to print hashing info, so + we test expunge_hash_table. */ + if (list == 0 && expunge_hash_table == 0) + { + opt = print_hashed_commands (list_portably); + if (opt == 0 && posixly_correct == 0) + printf (_("%s: hash table empty\n"), this_command_name); + + return (EXECUTION_SUCCESS); + } + + if (expunge_hash_table) + phash_flush (); + + /* If someone runs `hash -r -t xyz' he will be disappointed. */ + if (list_targets) + return (list_hashed_filename_targets (list, list_portably)); + +#if defined (RESTRICTED_SHELL) + if (restricted && pathname && strchr (pathname, '/')) + { + sh_restricted (pathname); + return (EXECUTION_FAILURE); + } +#endif + + for (opt = EXECUTION_SUCCESS; list; list = list->next) + { + /* Add, remove or rehash the specified commands. */ + w = list->word->word; + if (absolute_program (w)) + continue; + else if (pathname) + { + if (is_directory (pathname)) + { +#ifdef EISDIR + builtin_error ("%s: %s", pathname, strerror (EISDIR)); +#else + builtin_error (_("%s: is a directory"), pathname); +#endif + opt = EXECUTION_FAILURE; + } + else + phash_insert (w, pathname, 0, 0); + } + else if (delete) + { + if (phash_remove (w)) + { + sh_notfound (w); + opt = EXECUTION_FAILURE; + } + } + else if (add_hashed_command (w, 0)) + opt = EXECUTION_FAILURE; + } + + fflush (stdout); + return (opt); +} + +static int +add_hashed_command (w, quiet) + char *w; + int quiet; +{ + int rv; + char *full_path; + + rv = 0; + if (find_function (w) == 0 && find_shell_builtin (w) == 0) + { + phash_remove (w); + full_path = find_user_command (w); + if (full_path && executable_file (full_path)) + phash_insert (w, full_path, dot_found_in_search, 0); + else + { + if (quiet == 0) + sh_notfound (w); + rv++; + } + FREE (full_path); + } + return (rv); +} + +/* Print information about current hashed info. */ +static int +print_hash_info (item) + BUCKET_CONTENTS *item; +{ + printf ("%4d\t%s\n", item->times_found, pathdata(item)->path); + return 0; +} + +static int +print_portable_hash_info (item) + BUCKET_CONTENTS *item; +{ + printf ("builtin hash -p %s %s\n", pathdata(item)->path, item->key); + return 0; +} + +static int +print_hashed_commands (fmt) + int fmt; +{ + if (hashed_filenames == 0 || HASH_ENTRIES (hashed_filenames) == 0) + return (0); + + if (fmt == 0) + printf (_("hits\tcommand\n")); + hash_walk (hashed_filenames, fmt ? print_portable_hash_info : print_hash_info); + return (1); +} + +static int +list_hashed_filename_targets (list, fmt) + WORD_LIST *list; + int fmt; +{ + int all_found, multiple; + char *target; + WORD_LIST *l; + + all_found = 1; + multiple = list->next != 0; + + for (l = list; l; l = l->next) + { + target = phash_search (l->word->word); + if (target == 0) + { + all_found = 0; + sh_notfound (l->word->word); + continue; + } + if (fmt) + printf ("builtin hash -p %s %s\n", target, l->word->word); + else + { + if (multiple) + printf ("%s\t", l->word->word); + printf ("%s\n", target); + } + } + + return (all_found ? EXECUTION_SUCCESS : EXECUTION_FAILURE); +} diff --git a/builtins/kill.def b/builtins/kill.def index 18c3667f..adf022c9 100644 --- a/builtins/kill.def +++ b/builtins/kill.def @@ -1,7 +1,7 @@ This file is kill.def, from which is created kill.c. It implements the builtin "kill" in Bash. -Copyright (C) 1987-2009 Free Software Foundation, Inc. +Copyright (C) 1987-2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. @@ -121,9 +121,7 @@ kill_builtin (list) else sig = decode_signal (sigspec, dflags); list = list->next; -#if 0 - saw_signal++; /* XXX - for bash-4.2 */ -#endif + saw_signal++; } else { diff --git a/builtins/kill.def~ b/builtins/kill.def~ new file mode 100644 index 00000000..cfb06739 --- /dev/null +++ b/builtins/kill.def~ @@ -0,0 +1,263 @@ +This file is kill.def, from which is created kill.c. +It implements the builtin "kill" in Bash. + +Copyright (C) 1987-2009 Free Software Foundation, Inc. + +This file is part of GNU Bash, the Bourne Again SHell. + +Bash 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 3 of the License, or +(at your option) any later version. + +Bash 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 Bash. If not, see <http://www.gnu.org/licenses/>. + +$PRODUCES kill.c + +$BUILTIN kill +$FUNCTION kill_builtin +$SHORT_DOC kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec] +Send a signal to a job. + +Send the processes identified by PID or JOBSPEC the signal named by +SIGSPEC or SIGNUM. If neither SIGSPEC nor SIGNUM is present, then +SIGTERM is assumed. + +Options: + -s sig SIG is a signal name + -n sig SIG is a signal number + -l list the signal names; if arguments follow `-l' they are + assumed to be signal numbers for which names should be listed + +Kill is a shell builtin for two reasons: it allows job IDs to be used +instead of process IDs, and allows processes to be killed if the limit +on processes that you can create is reached. + +Exit Status: +Returns success unless an invalid option is given or an error occurs. +$END + +#include <config.h> + +#include <stdio.h> +#include <errno.h> +#if defined (HAVE_UNISTD_H) +# ifdef _MINIX +# include <sys/types.h> +# endif +# include <unistd.h> +#endif + +#include "../bashansi.h" +#include "../bashintl.h" + +#include "../shell.h" +#include "../trap.h" +#include "../jobs.h" +#include "common.h" + +/* Not all systems declare ERRNO in errno.h... and some systems #define it! */ +#if !defined (errno) +extern int errno; +#endif /* !errno */ + +extern int posixly_correct; + +static void kill_error __P((pid_t, int)); + +#if !defined (CONTINUE_AFTER_KILL_ERROR) +# define CONTINUE_OR_FAIL return (EXECUTION_FAILURE) +#else +# define CONTINUE_OR_FAIL goto continue_killing +#endif /* CONTINUE_AFTER_KILL_ERROR */ + +/* Here is the kill builtin. We only have it so that people can type + kill -KILL %1? No, if you fill up the process table this way you + can still kill some. */ +int +kill_builtin (list) + WORD_LIST *list; +{ + int sig, any_succeeded, listing, saw_signal, dflags; + char *sigspec, *word; + pid_t pid; + intmax_t pid_value; + + if (list == 0) + { + builtin_usage (); + return (EXECUTION_FAILURE); + } + + any_succeeded = listing = saw_signal = 0; + sig = SIGTERM; + sigspec = "TERM"; + + dflags = DSIG_NOCASE | ((posixly_correct == 0) ? DSIG_SIGPREFIX : 0); + /* Process options. */ + while (list) + { + word = list->word->word; + + if (ISOPTION (word, 'l')) + { + listing++; + list = list->next; + } + else if (ISOPTION (word, 's') || ISOPTION (word, 'n')) + { + list = list->next; + if (list) + { + sigspec = list->word->word; + if (sigspec[0] == '0' && sigspec[1] == '\0') + sig = 0; + else + sig = decode_signal (sigspec, dflags); + list = list->next; + saw_signal++; + } + else + { + sh_needarg (word); + return (EXECUTION_FAILURE); + } + } + else if (ISOPTION (word, '-')) + { + list = list->next; + break; + } + else if (ISOPTION (word, '?')) + { + builtin_usage (); + return (EXECUTION_SUCCESS); + } + /* If this is a signal specification then process it. We only process + the first one seen; other arguments may signify process groups (e.g, + -num == process group num). */ + else if (*word == '-' && saw_signal == 0) + { + sigspec = word + 1; + sig = decode_signal (sigspec, dflags); + saw_signal++; + list = list->next; + } + else + break; + } + + if (listing) + return (display_signal_list (list, 0)); + + /* OK, we are killing processes. */ + if (sig == NO_SIG) + { + sh_invalidsig (sigspec); + return (EXECUTION_FAILURE); + } + + if (list == 0) + { + builtin_usage (); + return (EXECUTION_FAILURE); + } + + while (list) + { + word = list->word->word; + + if (*word == '-') + word++; + + /* Use the entire argument in case of minus sign presence. */ + if (*word && legal_number (list->word->word, &pid_value) && (pid_value == (pid_t)pid_value)) + { + pid = (pid_t) pid_value; + + if (kill_pid (pid, sig, pid < -1) < 0) + { + if (errno == EINVAL) + sh_invalidsig (sigspec); + else + kill_error (pid, errno); + CONTINUE_OR_FAIL; + } + else + any_succeeded++; + } +#if defined (JOB_CONTROL) + else if (*list->word->word && *list->word->word != '%') + { + builtin_error (_("%s: arguments must be process or job IDs"), list->word->word); + CONTINUE_OR_FAIL; + } + else if (*word) + /* Posix.2 says you can kill without job control active (4.32.4) */ + { /* Must be a job spec. Check it out. */ + int job; + sigset_t set, oset; + JOB *j; + + BLOCK_CHILD (set, oset); + job = get_job_spec (list); + + if (INVALID_JOB (job)) + { + if (job != DUP_JOB) + sh_badjob (list->word->word); + UNBLOCK_CHILD (oset); + CONTINUE_OR_FAIL; + } + + j = get_job_by_jid (job); + /* Job spec used. Kill the process group. If the job was started + without job control, then its pgrp == shell_pgrp, so we have + to be careful. We take the pid of the first job in the pipeline + in that case. */ + pid = IS_JOBCONTROL (job) ? j->pgrp : j->pipe->pid; + + UNBLOCK_CHILD (oset); + + if (kill_pid (pid, sig, 1) < 0) + { + if (errno == EINVAL) + sh_invalidsig (sigspec); + else + kill_error (pid, errno); + CONTINUE_OR_FAIL; + } + else + any_succeeded++; + } +#endif /* !JOB_CONTROL */ + else + { + sh_badpid (list->word->word); + CONTINUE_OR_FAIL; + } + continue_killing: + list = list->next; + } + + return (any_succeeded ? EXECUTION_SUCCESS : EXECUTION_FAILURE); +} + +static void +kill_error (pid, e) + pid_t pid; + int e; +{ + char *x; + + x = strerror (e); + if (x == 0) + x = _("Unknown error"); + builtin_error ("(%ld) - %s", (long)pid, x); +} diff --git a/builtins/mapfile.def b/builtins/mapfile.def index 0946de3e..ec1e32e5 100644 --- a/builtins/mapfile.def +++ b/builtins/mapfile.def @@ -2,7 +2,7 @@ This file is mapfile.def, from which is created mapfile.c. It implements the builtin "mapfile" in Bash. Copyright (C) 2005-2006 Rocky Bernstein for Free Software Foundation, Inc. -Copyright (C) 2008,2009 Free Software Foundation, Inc. +Copyright (C) 2008-2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. @@ -44,7 +44,8 @@ Arguments: 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. +element to be assigned and the line to be assigned to that element +as additional arguments. If not supplied with an explicit origin, mapfile will clear ARRAY before assigning to it. @@ -88,7 +89,10 @@ extern int errno; #if defined (ARRAY_VARS) +static int run_callback __P((const char *, unsigned int, const char *)); + #define DEFAULT_ARRAY_NAME "MAPFILE" +#define DEFAULT_VARIABLE_NAME "MAPLINE" /* not used right now */ /* The value specifying how frequently `mapfile' calls the callback. */ #define DEFAULT_QUANTUM 5000 @@ -98,18 +102,20 @@ extern int errno; #define MAPF_CHOP 0x02 static int -run_callback(callback, current_index) +run_callback (callback, curindex, curline) const char *callback; - unsigned int current_index; + unsigned int curindex; + const char *curline; { unsigned int execlen; - char *execstr; + char *execstr, *qline; int flags; - execlen = strlen (callback) + 10; - /* 1 for space between %s and %d, + qline = sh_single_quote (curline); + execlen = strlen (callback) + strlen (qline) + 10; + /* 1 for each space between %s and %d, another 1 for the last nul char for C string. */ - execlen += 2; + execlen += 3; execstr = xmalloc (execlen); flags = SEVAL_NOHIST; @@ -117,8 +123,9 @@ run_callback(callback, current_index) if (interactive) flags |= SEVAL_INTERACT; #endif - snprintf (execstr, execlen, "%s %d", callback, current_index); - return parse_and_execute(execstr, NULL, flags); + snprintf (execstr, execlen, "%s %d %s", callback, curindex, qline); + free (qline); + return parse_and_execute (execstr, NULL, flags); } static void @@ -202,7 +209,7 @@ mapfile (fd, line_count_goal, origin, nskip, callback_quantum, callback, array_n /* Has a callback been registered and if so is it time to call it? */ if (callback && line_count && (line_count % callback_quantum) == 0) { - run_callback (callback, array_index); + run_callback (callback, array_index, line); /* Reset the buffer for bash own stream. */ if (unbuffered_read == 0) diff --git a/builtins/mapfile.def~ b/builtins/mapfile.def~ new file mode 100644 index 00000000..aff1d855 --- /dev/null +++ b/builtins/mapfile.def~ @@ -0,0 +1,357 @@ +This file is mapfile.def, from which is created mapfile.c. +It implements the builtin "mapfile" in Bash. + +Copyright (C) 2005-2006 Rocky Bernstein for Free Software Foundation, Inc. +Copyright (C) 2008-2010 Free Software Foundation, Inc. + +This file is part of GNU Bash, the Bourne Again SHell. + +Bash 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 3 of the License, or +(at your option) any later version. + +Bash 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 Bash. If not, see <http://www.gnu.org/licenses/>. + +$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 the standard input into an indexed array variable. + +Read lines from the standard input into the indexed array variable ARRAY, or +from file descriptor FD if the -u option is supplied. The variable MAPFILE +is the default ARRAY. + +Options: + -n count Copy at most COUNT lines. If COUNT is 0, all lines are copied. + -O origin Begin assigning to ARRAY at index ORIGIN. The default index is 0. + -s count Discard the first COUNT lines read. + -t Remove a trailing newline from each line read. + -u fd Read lines from file descriptor FD instead of the standard input. + -C callback Evaluate CALLBACK each time QUANTUM lines are read. + -c quantum Specify the number of lines read between each call to CALLBACK. + +Arguments: + ARRAY Array variable name to use for file data. + +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. + +Exit Status: +Returns success unless an invalid option is given or ARRAY is readonly or +not an indexed array. +$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" +#include "posixstat.h" + +#if defined (HAVE_UNISTD_H) +# include <unistd.h> +#endif + +#include "bashansi.h" +#include "bashintl.h" + +#include <stdio.h> +#include <errno.h> + +#include "../bashintl.h" +#include "../shell.h" +#include "common.h" +#include "bashgetopt.h" + +#if !defined (errno) +extern int errno; +#endif + +#if defined (ARRAY_VARS) + +static int run_callback __P((const char *, unsigned int, const char *)); + +#define DEFAULT_ARRAY_NAME "MAPFILE" +#define DEFAULT_VARIABLE_NAME "MAPLINE" /* not used right now */ + +/* The value specifying how frequently `mapfile' calls the callback. */ +#define DEFAULT_QUANTUM 5000 + +/* Values for FLAGS */ +#define MAPF_CLEARARRAY 0x01 +#define MAPF_CHOP 0x02 + +static int +run_callback (callback, curindex, curline) + const char *callback; + unsigned int curindex; + const char *curline; +{ + unsigned int execlen; + char *execstr, *qline; + int flags; + + qline = sh_single_quote (curline); + execlen = strlen (callback) + strlen (qline) + 10; + /* 1 for each space between %s and %d, + another 1 for the last nul char for C string. */ + execlen += 3; + execstr = xmalloc (execlen); + + flags = SEVAL_NOHIST; +#if 0 + if (interactive) + flags |= SEVAL_INTERACT; +#endif + snprintf (execstr, execlen, "%s %d %s", callback, curindex, qline); + free (qline); + return parse_and_execute (execstr, NULL, flags); +} + +static void +do_chop(line) + char * line; +{ + int length; + + length = strlen (line); + if (length && line[length-1] == '\n') + line[length-1] = '\0'; +} + +static int +mapfile (fd, line_count_goal, origin, nskip, callback_quantum, callback, array_name, flags) + int fd; + long line_count_goal, origin, nskip, callback_quantum; + char *callback, *array_name; + int flags; +{ + char *line; + size_t line_length; + unsigned int array_index, line_count; + SHELL_VAR *entry; + int unbuffered_read; + + line = NULL; + line_length = 0; + unbuffered_read = 0; + + /* The following check should be done before reading any lines. Doing it + 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 || readonly_p (entry) || noassign_p (entry)) + { + if (entry && readonly_p (entry)) + err_readonly (array_name); + + return (EXECUTION_FAILURE); + } + else if (array_p (entry) == 0) + { + builtin_error (_("%s: not an indexed array"), array_name); + return (EXECUTION_FAILURE); + } + + if (flags & MAPF_CLEARARRAY) + array_flush (array_cell (entry)); + +#ifndef __CYGWIN__ + unbuffered_read = (lseek (fd, 0L, SEEK_CUR) < 0) && (errno == ESPIPE); +#else + unbuffered_read = 1; +#endif + + zreset (); + + /* Skip any lines at beginning of file? */ + for (line_count = 0; line_count < nskip; line_count++) + if (zgetline (fd, &line, &line_length, unbuffered_read) < 0) + break; + + line = 0; + line_length = 0; + + /* Reset the buffer for bash own stream */ + 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) + break; + + /* Remove trailing newlines? */ + if (flags & MAPF_CHOP) + do_chop (line); + + /* Has a callback been registered and if so is it time to call it? */ + if (callback && line_count && (line_count % callback_quantum) == 0) + { + run_callback (callback, array_index, line); + + /* Reset the buffer for bash own stream. */ + if (unbuffered_read == 0) + zsyncfd (fd); + } + + bind_array_element (entry, array_index, line, 0); + } + + xfree (line); + + if (unbuffered_read == 0) + zsyncfd (fd); + + interrupt_immediately--; + return EXECUTION_SUCCESS; +} + +int +mapfile_builtin (list) + WORD_LIST *list; +{ + int opt, code, fd, clear_array, flags; + intmax_t intval; + long lines, origin, nskip, callback_quantum; + char *array_name, *callback; + + clear_array = 1; + fd = 0; + lines = origin = nskip = 0; + flags = MAPF_CLEARARRAY; + callback_quantum = DEFAULT_QUANTUM; + callback = 0; + + reset_internal_getopt (); + while ((opt = internal_getopt (list, "u:n:O:tC:c:s:")) != -1) + { + switch (opt) + { + case 'u': + code = legal_number (list_optarg, &intval); + if (code == 0 || intval < 0 || intval != (int)intval) + { + builtin_error (_("%s: invalid file descriptor specification"), list_optarg); + return (EXECUTION_FAILURE); + } + else + fd = intval; + + if (sh_validfd (fd) == 0) + { + builtin_error (_("%d: invalid file descriptor: %s"), fd, strerror (errno)); + return (EXECUTION_FAILURE); + } + break; + + case 'n': + code = legal_number (list_optarg, &intval); + if (code == 0 || intval < 0 || intval != (unsigned)intval) + { + builtin_error (_("%s: invalid line count"), list_optarg); + return (EXECUTION_FAILURE); + } + else + lines = intval; + break; + + case 'O': + code = legal_number (list_optarg, &intval); + if (code == 0 || intval < 0 || intval != (unsigned)intval) + { + builtin_error (_("%s: invalid array origin"), list_optarg); + return (EXECUTION_FAILURE); + } + else + origin = intval; + flags &= ~MAPF_CLEARARRAY; + break; + case 't': + flags |= MAPF_CHOP; + break; + case 'C': + callback = list_optarg; + break; + case 'c': + code = legal_number (list_optarg, &intval); + if (code == 0 || intval <= 0 || intval != (unsigned)intval) + { + builtin_error (_("%s: invalid callback quantum"), list_optarg); + return (EXECUTION_FAILURE); + } + else + callback_quantum = intval; + break; + case 's': + code = legal_number (list_optarg, &intval); + if (code == 0 || intval < 0 || intval != (unsigned)intval) + { + builtin_error (_("%s: invalid line count"), list_optarg); + return (EXECUTION_FAILURE); + } + else + nskip = intval; + break; + default: + builtin_usage (); + return (EX_USAGE); + } + } + list = loptend; + + if (list == 0) + array_name = DEFAULT_ARRAY_NAME; + else if (list->word == 0 || list->word->word == 0) + { + builtin_error ("internal error: getting variable name"); + return (EXECUTION_FAILURE); + } + else if (list->word->word[0] == '\0') + { + builtin_error (_("empty array variable name")); + return (EX_USAGE); + } + else + array_name = list->word->word; + + if (legal_identifier (array_name) == 0 && valid_array_reference (array_name) == 0) + { + sh_invalidid (array_name); + return (EXECUTION_FAILURE); + } + + return mapfile (fd, lines, origin, nskip, callback_quantum, callback, array_name, flags); +} + +#else + +int +mapfile_builtin (list) + WORD_LIST *list; +{ + builtin_error (_("array variable support required")); + return (EXECUTION_FAILURE); +} + +#endif /* ARRAY_VARS */ diff --git a/builtins/mkbuiltins.c b/builtins/mkbuiltins.c index 071a28ce..10a28583 100644 --- a/builtins/mkbuiltins.c +++ b/builtins/mkbuiltins.c @@ -1,7 +1,7 @@ /* mkbuiltins.c - Create builtins.c, builtext.h, and builtdoc.c from a single source file called builtins.def. */ -/* Copyright (C) 1987-2009 Free Software Foundation, Inc. +/* Copyright (C) 1987-2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. diff --git a/builtins/mkbuiltins.c~ b/builtins/mkbuiltins.c~ index cdd45e40..071a28ce 100644 --- a/builtins/mkbuiltins.c~ +++ b/builtins/mkbuiltins.c~ @@ -1380,7 +1380,7 @@ write_documentation (stream, documentation, indentation, flags) { register int i, j; register char *line; - int string_array, texinfo, base_indent, last_cpp, filename_p; + int string_array, texinfo, base_indent, filename_p; if (stream == 0) return; @@ -1407,7 +1407,7 @@ write_documentation (stream, documentation, indentation, flags) base_indent = (string_array && single_longdoc_strings && filename_p == 0) ? BASE_INDENT : 0; - for (i = last_cpp = 0, texinfo = (flags & TEXINFO); line = documentation[i]; i++) + for (i = 0, texinfo = (flags & TEXINFO); line = documentation[i]; i++) { /* Allow #ifdef's to be written out verbatim, but don't put them into separate help files. */ @@ -1415,11 +1415,8 @@ write_documentation (stream, documentation, indentation, flags) { if (string_array && filename_p == 0 && single_longdoc_strings == 0) fprintf (stream, "%s\n", line); - last_cpp = 1; continue; } - else - last_cpp = 0; /* prefix with N_( for gettext */ if (string_array && single_longdoc_strings == 0) diff --git a/builtins/printf.def b/builtins/printf.def index 496da9b6..ec2d575e 100644 --- a/builtins/printf.def +++ b/builtins/printf.def @@ -1,7 +1,7 @@ This file is printf.def, from which is created printf.c. It implements the builtin "printf" in Bash. -Copyright (C) 1997-2009 Free Software Foundation, Inc. +Copyright (C) 1997-2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. @@ -72,9 +72,12 @@ $END # include <inttypes.h> #endif +#include "posixtime.h" #include "../bashansi.h" #include "../bashintl.h" +#define NEED_STRFTIME_DECL + #include "../shell.h" #include "shmbutil.h" #include "stdc.h" @@ -167,6 +170,8 @@ extern int errno; #define SKIP1 "#'-+ 0" #define LENMODS "hjlLtz" +extern time_t shell_start_time; + #if !HAVE_ASPRINTF extern int asprintf __P((char **, const char *, ...)) __attribute__((__format__ (printf, 2, 3))); #endif @@ -177,7 +182,7 @@ extern int vsnprintf __P((char *, size_t, const char *, va_list)) __attribute__( static void printf_erange __P((char *)); static int printstr __P((char *, char *, int, int, int)); -static int tescape __P((char *, char *, int *)); +static int tescape __P((char *, char *, int *, int *)); static char *bexpand __P((char *, int, int *, int *)); static char *vbadd __P((char *, int)); static int vbprintf __P((const char *, ...)) __attribute__((__format__ (printf, 1, 2))); @@ -226,7 +231,7 @@ printf_builtin (list) char convch, thisch, nextch, *format, *modstart, *fmt, *start; #if defined (HANDLE_MULTIBYTE) char mbch[25]; /* 25 > MB_LEN_MAX, plus can handle 4-byte UTF-8 and large Unicode characters*/ - int mbind; + int mbind, mblen; #endif conversion_error = 0; @@ -309,11 +314,11 @@ printf_builtin (list) /* Accommodate possible use of \u or \U, which can result in multibyte characters */ memset (mbch, '\0', sizeof (mbch)); - fmt += tescape (fmt, mbch, (int *)NULL); - for (mbind = 0; mbch[mbind]; mbind++) + fmt += tescape (fmt, mbch, &mblen, (int *)NULL); + for (mbind = 0; mbind < mblen; mbind++) PC (mbch[mbind]); #else - fmt += tescape (fmt, &nextch, (int *)NULL); + fmt += tescape (fmt, &nextch, (int *)NULL, (int *)NULL); PC (nextch); #endif fmt--; /* for loop will increment it for us again */ @@ -414,6 +419,70 @@ printf_builtin (list) break; } + case '(': + { + char *timefmt, timebuf[128], *t; + int n; + intmax_t arg; + time_t secs; + struct tm *tm; + + modstart[1] = nextch; /* restore char after left paren */ + timefmt = xmalloc (strlen (fmt) + 3); + fmt++; /* skip over left paren */ + for (t = timefmt, n = 1; *fmt; ) + { + if (*fmt == '(') + n++; + else if (*fmt == ')') + n--; + if (n == 0) + break; + *t++ = *fmt++; + } + *t = '\0'; + if (*++fmt != 'T') + { + builtin_warning (_("`%c': invalid time format specification"), *fmt); + fmt = start; + free (timefmt); + PC (*fmt); + continue; + } + if (timefmt[0] == '\0') + { + timefmt[0] = '%'; + timefmt[1] = 'X'; /* locale-specific current time - should we use `+'? */ + timefmt[2] = '\0'; + } + /* argument is seconds since the epoch with special -1 and -2 */ + arg = getintmax (); + if (arg == -1) + secs = NOW; /* roughly date +%s */ + else if (arg == -2) + secs = shell_start_time; /* roughly $SECONDS */ + else + secs = arg; + tm = localtime (&secs); + n = strftime (timebuf, sizeof (timebuf), timefmt, tm); + free (timefmt); + if (n == 0) + timebuf[0] = '\0'; + else + timebuf[sizeof(timebuf) - 1] = '\0'; + /* convert to %s format that preserves fieldwidth and precision */ + modstart[0] = 's'; + modstart[1] = '\0'; + n = printstr (start, timebuf, strlen (timebuf), fieldwidth, precision); /* XXX - %s for now */ + if (n < 0) + { + sh_wrerror (); + clearerr (stdout); + PRETURN (EXECUTION_FAILURE); + } + break; + } + case 'n': { char *var; @@ -712,15 +781,17 @@ printstr (fmt, string, len, fieldwidth, precision) do the \c short-circuiting, and \c is treated as an unrecognized escape sequence; we also bypass the other processing specific to %b arguments. */ static int -tescape (estart, cp, sawc) +tescape (estart, cp, lenp, sawc) char *estart; char *cp; - int *sawc; + int *lenp, *sawc; { register char *p; int temp, c, evalue; p = estart; + if (lenp) + *lenp = 1; switch (c = *p++) { @@ -788,6 +859,8 @@ tescape (estart, cp, sawc) { temp = u32cconv (evalue, cp); cp[temp] = '\0'; + if (lenp) + *lenp = temp; } break; #endif @@ -832,7 +905,7 @@ bexpand (string, len, sawc, lenp) char *ret, *r, *s, c; #if defined (HANDLE_MULTIBYTE) char mbch[25]; - int mbind; + int mbind, mblen; #endif if (string == 0 || len == 0) @@ -856,9 +929,9 @@ bexpand (string, len, sawc, lenp) temp = 0; #if defined (HANDLE_MULTIBYTE) memset (mbch, '\0', sizeof (mbch)); - s += tescape (s, mbch, &temp); + s += tescape (s, mbch, &mblen, &temp); #else - s += tescape (s, &c, &temp); + s += tescape (s, &c, (int *)NULL, &temp); #endif if (temp) { @@ -868,7 +941,7 @@ bexpand (string, len, sawc, lenp) } #if defined (HANDLE_MULTIBYTE) - for (mbind = 0; mbch[mbind]; mbind++) + for (mbind = 0; mbind < mblen; mbind++) *r++ = mbch[mbind]; #else *r++ = c; diff --git a/builtins/printf.def~ b/builtins/printf.def~ index bccd1aa9..bc70d506 100644 --- a/builtins/printf.def~ +++ b/builtins/printf.def~ @@ -72,9 +72,12 @@ $END # include <inttypes.h> #endif +#include "posixtime.h" #include "../bashansi.h" #include "../bashintl.h" +#define NEED_STRFTIME_DECL + #include "../shell.h" #include "shmbutil.h" #include "stdc.h" @@ -167,6 +170,8 @@ extern int errno; #define SKIP1 "#'-+ 0" #define LENMODS "hjlLtz" +extern time_t shell_start_time; + #if !HAVE_ASPRINTF extern int asprintf __P((char **, const char *, ...)) __attribute__((__format__ (printf, 2, 3))); #endif @@ -177,7 +182,7 @@ extern int vsnprintf __P((char *, size_t, const char *, va_list)) __attribute__( static void printf_erange __P((char *)); static int printstr __P((char *, char *, int, int, int)); -static int tescape __P((char *, char *, int *)); +static int tescape __P((char *, char *, int *, int *)); static char *bexpand __P((char *, int, int *, int *)); static char *vbadd __P((char *, int)); static int vbprintf __P((const char *, ...)) __attribute__((__format__ (printf, 1, 2))); @@ -226,7 +231,7 @@ printf_builtin (list) char convch, thisch, nextch, *format, *modstart, *fmt, *start; #if defined (HANDLE_MULTIBYTE) char mbch[25]; /* 25 > MB_LEN_MAX, plus can handle 4-byte UTF-8 and large Unicode characters*/ - int mbind; + int mbind, mblen; #endif conversion_error = 0; @@ -309,11 +314,11 @@ printf_builtin (list) /* Accommodate possible use of \u or \U, which can result in multibyte characters */ memset (mbch, '\0', sizeof (mbch)); - fmt += tescape (fmt, mbch, (int *)NULL); - for (mbind = 0; mbch[mbind]; mbind++) + fmt += tescape (fmt, mbch, &mblen, (int *)NULL); + for (mbind = 0; mbind < mblen; mbind++) PC (mbch[mbind]); #else - fmt += tescape (fmt, &nextch, (int *)NULL); + fmt += tescape (fmt, &nextch, (int *)NULL, (int *)NULL); PC (nextch); #endif fmt--; /* for loop will increment it for us again */ @@ -414,6 +419,70 @@ printf_builtin (list) break; } + case '(': + { + char *timefmt, timebuf[128], *t; + int n; + intmax_t arg; + time_t secs; + struct tm *tm; + + modstart[1] = nextch; /* restore char after left paren */ + timefmt = xmalloc (strlen (fmt) + 3); + fmt++; /* skip over left paren */ + for (t = timefmt, n = 1; *fmt; ) + { + if (*fmt == '(') + n++; + else if (*fmt == ')') + n--; + if (n == 0) + break; + *t++ = *fmt++; + } + *t = '\0'; + if (*++fmt != 'T') + { + builtin_warning (_("`%c': invalid time format specification"), *fmt); + fmt = start; + free (timefmt); + PC (*fmt); + continue; + } + if (timefmt[0] == '\0') + { + timefmt[0] = '%'; + timefmt[1] = 'X'; /* locale-specific current time - should we use `+'? */ + timefmt[2] = '\0'; + } + /* argument is seconds since the epoch with special -1 and -2 */ + arg = getintmax (); + if (arg == -1) + secs = NOW; /* roughly date +%s */ + else if (arg == -2) + secs = shell_start_time; /* roughly $SECONDS */ + else + secs = arg; + tm = localtime (&secs); + n = strftime (timebuf, sizeof (timebuf), timefmt, tm); + free (timefmt); + if (n == 0) + timebuf[0] = '\0'; + else + timebuf[sizeof(timebuf) - 1] = '\0'; + /* convert to %s format that preserves fieldwidth and precision */ + modstart[0] = 's'; + modstart[1] = '\0'; + n = printstr (start, timebuf, strlen (timebuf), fieldwidth, precision); /* XXX - %s for now */ + if (n < 0) + { + sh_wrerror (); + clearerr (stdout); + PRETURN (EXECUTION_FAILURE); + } + break; + } + case 'n': { char *var; @@ -712,15 +781,17 @@ printstr (fmt, string, len, fieldwidth, precision) do the \c short-circuiting, and \c is treated as an unrecognized escape sequence; we also bypass the other processing specific to %b arguments. */ static int -tescape (estart, cp, sawc) +tescape (estart, cp, lenp, sawc) char *estart; char *cp; - int *sawc; + int *lenp, *sawc; { register char *p; int temp, c, evalue; p = estart; + if (lenp) + *lenp = 1; switch (c = *p++) { @@ -782,8 +853,15 @@ tescape (estart, cp, sawc) *cp = '\\'; return 0; } - temp = u32cconv (evalue, cp); - cp[temp] = '\0'; + if (evalue <= UCHAR_MAX) + *cp = evalue; + else + { + temp = u32cconv (evalue, cp); + cp[temp] = '\0'; + if (lenp) + *lenp = temp; + } break; #endif @@ -827,7 +905,7 @@ bexpand (string, len, sawc, lenp) char *ret, *r, *s, c; #if defined (HANDLE_MULTIBYTE) char mbch[25]; - int mbind; + int mbind, mblen; #endif if (string == 0 || len == 0) @@ -851,9 +929,9 @@ bexpand (string, len, sawc, lenp) temp = 0; #if defined (HANDLE_MULTIBYTE) memset (mbch, '\0', sizeof (mbch)); - s += tescape (s, mbch, &temp); + s += tescape (s, mbch, &mblen, &temp); #else - s += tescape (s, &c, &temp); + s += tescape (s, &c, (int *)NULL, &temp); #endif if (temp) { @@ -863,7 +941,7 @@ bexpand (string, len, sawc, lenp) } #if defined (HANDLE_MULTIBYTE) - for (mbind = 0; mbch[mbind]; mbind++) + for (mbind = 0; mbind < mblen; mbind++) *r++ = mbch[mbind]; #else *r++ = c; diff --git a/builtins/read.def b/builtins/read.def index 20860bef..d9431d35 100644 --- a/builtins/read.def +++ b/builtins/read.def @@ -1,7 +1,7 @@ This file is read.def, from which is created read.c. It implements the builtin "read" in Bash. -Copyright (C) 1987-2009 Free Software Foundation, Inc. +Copyright (C) 1987-2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. diff --git a/builtins/read.def~ b/builtins/read.def~ index e03982c9..20860bef 100644 --- a/builtins/read.def~ +++ b/builtins/read.def~ @@ -390,7 +390,6 @@ read_builtin (list) run_unwind_frame ("read_builtin"); input_string[i] = '\0'; /* make sure it's terminated */ retval = 128+SIGALRM; -itrace("read_builtin: before goto assign_vars: interrupt_immediately = %d", interrupt_immediately); goto assign_vars; } old_alrm = set_signal_handler (SIGALRM, sigalrm); @@ -616,13 +615,14 @@ add_char: if (unbuffered_read == 0) zsyncfd (fd); + discard_unwind_frame ("read_builtin"); + retval = eof ? EXECUTION_FAILURE : EXECUTION_SUCCESS; assign_vars: interrupt_immediately--; terminate_immediately--; - discard_unwind_frame ("read_builtin"); #if defined (ARRAY_VARS) /* If -a was given, take the string read, break it into a list of words, diff --git a/builtins/setattr.def b/builtins/setattr.def index f549d05f..a21c4fb0 100644 --- a/builtins/setattr.def +++ b/builtins/setattr.def @@ -1,7 +1,7 @@ This file is setattr.def, from which is created setattr.c. It implements the builtins "export" and "readonly", in Bash. -Copyright (C) 1987-2009 Free Software Foundation, Inc. +Copyright (C) 1987-2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. diff --git a/builtins/setattr.def~ b/builtins/setattr.def~ index 29a835e6..f549d05f 100644 --- a/builtins/setattr.def~ +++ b/builtins/setattr.def~ @@ -433,7 +433,7 @@ show_var_attributes (var, pattr, nodefs) printf ("%s\n", var->name); else if (function_p (var)) printf ("%s\n", named_function_string (var->name, function_cell (var), FUNC_MULTILINE|FUNC_EXTERNAL)); - else if (invisible_p (var) || var_isnull (var)) + else if (invisible_p (var) || var_isset (var) == 0) printf ("%s\n", var->name); else { diff --git a/builtins/shopt.def b/builtins/shopt.def index 68c7245b..b610a266 100644 --- a/builtins/shopt.def +++ b/builtins/shopt.def @@ -1,7 +1,7 @@ This file is shopt.def, from which is created shopt.c. It implements the Bash `shopt' builtin. -Copyright (C) 1994-2009 Free Software Foundation, Inc. +Copyright (C) 1994-2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. @@ -124,6 +124,7 @@ static int shopt_login_shell; static int shopt_compat31; static int shopt_compat32; static int shopt_compat40; +static int shopt_compat41; typedef int shopt_set_func_t __P((char *, int)); @@ -146,6 +147,7 @@ static struct { { "compat31", &shopt_compat31, set_compatibility_level }, { "compat32", &shopt_compat32, set_compatibility_level }, { "compat40", &shopt_compat40, set_compatibility_level }, + { "compat41", &shopt_compat41, set_compatibility_level }, #if defined (READLINE) { "dirspell", &dircomplete_spelling, (shopt_set_func_t *)NULL }, #endif diff --git a/builtins/shopt.def~ b/builtins/shopt.def~ new file mode 100644 index 00000000..47d7f12d --- /dev/null +++ b/builtins/shopt.def~ @@ -0,0 +1,717 @@ +This file is shopt.def, from which is created shopt.c. +It implements the Bash `shopt' builtin. + +Copyright (C) 1994-2009 Free Software Foundation, Inc. + +This file is part of GNU Bash, the Bourne Again SHell. + +Bash 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 3 of the License, or +(at your option) any later version. + +Bash 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 Bash. If not, see <http://www.gnu.org/licenses/>. + +$PRODUCES shopt.c + +$BUILTIN shopt +$FUNCTION shopt_builtin +$SHORT_DOC shopt [-pqsu] [-o] [optname ...] +Set and unset shell options. + +Change the setting of each shell option OPTNAME. Without any option +arguments, list all shell options with an indication of whether or not each +is set. + +Options: + -o restrict OPTNAMEs to those defined for use with `set -o' + -p print each shell option with an indication of its status + -q suppress output + -s enable (set) each OPTNAME + -u disable (unset) each OPTNAME + +Exit Status: +Returns success if OPTNAME is enabled; fails if an invalid option is +given or OPTNAME is disabled. +$END + +#include <config.h> + +#if defined (HAVE_UNISTD_H) +# ifdef _MINIX +# include <sys/types.h> +# endif +# include <unistd.h> +#endif + +#include <stdio.h> + +#include "version.h" + +#include "../bashintl.h" + +#include "../shell.h" +#include "../flags.h" +#include "common.h" +#include "bashgetopt.h" + +#if defined (HISTORY) +# include "../bashhist.h" +#endif + +#define UNSETOPT 0 +#define SETOPT 1 + +#define OPTFMT "%-15s\t%s\n" + +extern int allow_null_glob_expansion, fail_glob_expansion, glob_dot_filenames; +extern int cdable_vars, mail_warning, source_uses_path; +extern int no_exit_on_failed_exec, print_shift_error; +extern int check_hashed_filenames, promptvars; +extern int cdspelling, expand_aliases; +extern int extended_quote; +extern int check_window_size; +extern int glob_ignore_case, match_ignore_case; +extern int hup_on_exit; +extern int xpg_echo; +extern int gnu_error_format; +extern int check_jobs_at_exit; +extern int autocd; +extern int glob_star; + +#if defined (EXTENDED_GLOB) +extern int extended_glob; +#endif + +#if defined (READLINE) +extern int hist_verify, history_reediting, perform_hostname_completion; +extern int no_empty_command_completion; +extern int force_fignore; +extern int dircomplete_spelling; + +extern int enable_hostname_completion __P((int)); +#endif + +#if defined (PROGRAMMABLE_COMPLETION) +extern int prog_completion_enabled; +#endif + +#if defined (RESTRICTED_SHELL) +extern char *shell_name; +#endif + +#if defined (DEBUGGER) +extern int debugging_mode; +#endif + +static void shopt_error __P((char *)); + +static int set_shellopts_after_change __P((char *, int)); +static int shopt_enable_hostname_completion __P((char *, int)); +static int set_compatibility_level __P((char *, int)); + +#if defined (RESTRICTED_SHELL) +static int set_restricted_shell __P((char *, int)); +#endif + +static int shopt_login_shell; +static int shopt_compat31; +static int shopt_compat32; +static int shopt_compat40; +static int shopt_compat41; + +typedef int shopt_set_func_t __P((char *, int)); + +static struct { + char *name; + int *value; + shopt_set_func_t *set_func; +} shopt_vars[] = { + { "autocd", &autocd, (shopt_set_func_t *)NULL }, + { "cdable_vars", &cdable_vars, (shopt_set_func_t *)NULL }, + { "cdspell", &cdspelling, (shopt_set_func_t *)NULL }, + { "checkhash", &check_hashed_filenames, (shopt_set_func_t *)NULL }, +#if defined (JOB_CONTROL) + { "checkjobs", &check_jobs_at_exit, (shopt_set_func_t *)NULL }, +#endif + { "checkwinsize", &check_window_size, (shopt_set_func_t *)NULL }, +#if defined (HISTORY) + { "cmdhist", &command_oriented_history, (shopt_set_func_t *)NULL }, +#endif + { "compat31", &shopt_compat31, set_compatibility_level }, + { "compat32", &shopt_compat32, set_compatibility_level }, + { "compat40", &shopt_compat40, set_compatibility_level }, + { "compat41", &shopt_compat41, set_compatibility_level }, +#if defined (READLINE) + { "dirspell", &dircomplete_spelling, (shopt_set_func_t *)NULL }, +#endif + { "dotglob", &glob_dot_filenames, (shopt_set_func_t *)NULL }, + { "execfail", &no_exit_on_failed_exec, (shopt_set_func_t *)NULL }, + { "expand_aliases", &expand_aliases, (shopt_set_func_t *)NULL }, +#if defined (DEBUGGER) + { "extdebug", &debugging_mode, (shopt_set_func_t *)NULL }, +#endif +#if defined (EXTENDED_GLOB) + { "extglob", &extended_glob, (shopt_set_func_t *)NULL }, +#endif + { "extquote", &extended_quote, (shopt_set_func_t *)NULL }, + { "failglob", &fail_glob_expansion, (shopt_set_func_t *)NULL }, +#if defined (READLINE) + { "force_fignore", &force_fignore, (shopt_set_func_t *)NULL }, +#endif + { "globstar", &glob_star, (shopt_set_func_t *)NULL }, + { "gnu_errfmt", &gnu_error_format, (shopt_set_func_t *)NULL }, +#if defined (HISTORY) + { "histappend", &force_append_history, (shopt_set_func_t *)NULL }, +#endif +#if defined (READLINE) + { "histreedit", &history_reediting, (shopt_set_func_t *)NULL }, + { "histverify", &hist_verify, (shopt_set_func_t *)NULL }, + { "hostcomplete", &perform_hostname_completion, shopt_enable_hostname_completion }, +#endif + { "huponexit", &hup_on_exit, (shopt_set_func_t *)NULL }, + { "interactive_comments", &interactive_comments, set_shellopts_after_change }, +#if defined (HISTORY) + { "lithist", &literal_history, (shopt_set_func_t *)NULL }, +#endif + { "login_shell", &shopt_login_shell, set_login_shell }, + { "mailwarn", &mail_warning, (shopt_set_func_t *)NULL }, +#if defined (READLINE) + { "no_empty_cmd_completion", &no_empty_command_completion, (shopt_set_func_t *)NULL }, +#endif + { "nocaseglob", &glob_ignore_case, (shopt_set_func_t *)NULL }, + { "nocasematch", &match_ignore_case, (shopt_set_func_t *)NULL }, + { "nullglob", &allow_null_glob_expansion, (shopt_set_func_t *)NULL }, +#if defined (PROGRAMMABLE_COMPLETION) + { "progcomp", &prog_completion_enabled, (shopt_set_func_t *)NULL }, +#endif + { "promptvars", &promptvars, (shopt_set_func_t *)NULL }, +#if defined (RESTRICTED_SHELL) + { "restricted_shell", &restricted_shell, set_restricted_shell }, +#endif + { "shift_verbose", &print_shift_error, (shopt_set_func_t *)NULL }, + { "sourcepath", &source_uses_path, (shopt_set_func_t *)NULL }, + { "xpg_echo", &xpg_echo, (shopt_set_func_t *)NULL }, + { (char *)0, (int *)0, (shopt_set_func_t *)NULL } +}; + +#define N_SHOPT_OPTIONS (sizeof (shopt_vars) / sizeof (shopt_vars[0])) + +#define GET_SHOPT_OPTION_VALUE(i) (*shopt_vars[i].value) + +static const char * const on = "on"; +static const char * const off = "off"; + +static int find_shopt __P((char *)); +static int toggle_shopts __P((int, WORD_LIST *, int)); +static void print_shopt __P((char *, int, int)); +static int list_shopts __P((WORD_LIST *, int)); +static int list_some_shopts __P((int, int)); +static int list_shopt_o_options __P((WORD_LIST *, int)); +static int list_some_o_options __P((int, int)); +static int set_shopt_o_options __P((int, WORD_LIST *, int)); + +#define SFLAG 0x01 +#define UFLAG 0x02 +#define QFLAG 0x04 +#define OFLAG 0x08 +#define PFLAG 0x10 + +int +shopt_builtin (list) + WORD_LIST *list; +{ + int opt, flags, rval; + + flags = 0; + reset_internal_getopt (); + while ((opt = internal_getopt (list, "psuoq")) != -1) + { + switch (opt) + { + case 's': + flags |= SFLAG; + break; + case 'u': + flags |= UFLAG; + break; + case 'q': + flags |= QFLAG; + break; + case 'o': + flags |= OFLAG; + break; + case 'p': + flags |= PFLAG; + break; + default: + builtin_usage (); + return (EX_USAGE); + } + } + list = loptend; + + if ((flags & (SFLAG|UFLAG)) == (SFLAG|UFLAG)) + { + builtin_error (_("cannot set and unset shell options simultaneously")); + return (EXECUTION_FAILURE); + } + + rval = EXECUTION_SUCCESS; + if ((flags & OFLAG) && ((flags & (SFLAG|UFLAG)) == 0)) /* shopt -o */ + rval = list_shopt_o_options (list, flags); + else if (list && (flags & OFLAG)) /* shopt -so args */ + rval = set_shopt_o_options ((flags & SFLAG) ? FLAG_ON : FLAG_OFF, list, flags & QFLAG); + else if (flags & OFLAG) /* shopt -so */ + rval = list_some_o_options ((flags & SFLAG) ? 1 : 0, flags); + else if (list && (flags & (SFLAG|UFLAG))) /* shopt -su args */ + rval = toggle_shopts ((flags & SFLAG) ? SETOPT : UNSETOPT, list, flags & QFLAG); + else if ((flags & (SFLAG|UFLAG)) == 0) /* shopt [args] */ + rval = list_shopts (list, flags); + else /* shopt -su */ + rval = list_some_shopts ((flags & SFLAG) ? SETOPT : UNSETOPT, flags); + return (rval); +} + +/* Reset the options managed by `shopt' to the values they would have at + shell startup. */ +void +reset_shopt_options () +{ + allow_null_glob_expansion = glob_dot_filenames = 0; + cdable_vars = mail_warning = 0; + no_exit_on_failed_exec = print_shift_error = 0; + check_hashed_filenames = cdspelling = expand_aliases = check_window_size = 0; + + source_uses_path = promptvars = 1; + +#if defined (EXTENDED_GLOB) + extended_glob = 0; +#endif + +#if defined (HISTORY) + literal_history = force_append_history = 0; + command_oriented_history = 1; +#endif + +#if defined (READLINE) + hist_verify = history_reediting = 0; + perform_hostname_completion = 1; +#endif + + shopt_login_shell = login_shell; +} + +static int +find_shopt (name) + char *name; +{ + int i; + + for (i = 0; shopt_vars[i].name; i++) + if (STREQ (name, shopt_vars[i].name)) + return i; + return -1; +} + +static void +shopt_error (s) + char *s; +{ + builtin_error (_("%s: invalid shell option name"), s); +} + +static int +toggle_shopts (mode, list, quiet) + int mode; + WORD_LIST *list; + int quiet; +{ + WORD_LIST *l; + int ind, rval; + + for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next) + { + ind = find_shopt (l->word->word); + if (ind < 0) + { + shopt_error (l->word->word); + rval = EXECUTION_FAILURE; + } + else + { + *shopt_vars[ind].value = mode; /* 1 for set, 0 for unset */ + if (shopt_vars[ind].set_func) + (*shopt_vars[ind].set_func) (shopt_vars[ind].name, mode); + } + } + + set_bashopts (); + return (rval); +} + +static void +print_shopt (name, val, flags) + char *name; + int val, flags; +{ + if (flags & PFLAG) + printf ("shopt %s %s\n", val ? "-s" : "-u", name); + else + printf (OPTFMT, name, val ? on : off); +} + +/* List the values of all or any of the `shopt' options. Returns 0 if + all were listed or all variables queried were on; 1 otherwise. */ +static int +list_shopts (list, flags) + WORD_LIST *list; + int flags; +{ + WORD_LIST *l; + int i, val, rval; + + if (list == 0) + { + for (i = 0; shopt_vars[i].name; i++) + { + val = *shopt_vars[i].value; + if ((flags & QFLAG) == 0) + print_shopt (shopt_vars[i].name, val, flags); + } + return (sh_chkwrite (EXECUTION_SUCCESS)); + } + + for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next) + { + i = find_shopt (l->word->word); + if (i < 0) + { + shopt_error (l->word->word); + rval = EXECUTION_FAILURE; + continue; + } + val = *shopt_vars[i].value; + if (val == 0) + rval = EXECUTION_FAILURE; + if ((flags & QFLAG) == 0) + print_shopt (l->word->word, val, flags); + } + + return (sh_chkwrite (rval)); +} + +static int +list_some_shopts (mode, flags) + int mode, flags; +{ + int val, i; + + for (i = 0; shopt_vars[i].name; i++) + { + val = *shopt_vars[i].value; + if (((flags & QFLAG) == 0) && mode == val) + print_shopt (shopt_vars[i].name, val, flags); + } + return (sh_chkwrite (EXECUTION_SUCCESS)); +} + +static int +list_shopt_o_options (list, flags) + WORD_LIST *list; + int flags; +{ + WORD_LIST *l; + int val, rval; + + if (list == 0) + { + if ((flags & QFLAG) == 0) + list_minus_o_opts (-1, (flags & PFLAG)); + return (sh_chkwrite (EXECUTION_SUCCESS)); + } + + for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next) + { + val = minus_o_option_value (l->word->word); + if (val == -1) + { + sh_invalidoptname (l->word->word); + rval = EXECUTION_FAILURE; + continue; + } + if (val == 0) + rval = EXECUTION_FAILURE; + if ((flags & QFLAG) == 0) + { + if (flags & PFLAG) + printf ("set %co %s\n", val ? '-' : '+', l->word->word); + else + printf (OPTFMT, l->word->word, val ? on : off); + } + } + return (sh_chkwrite (rval)); +} + +static int +list_some_o_options (mode, flags) + int mode, flags; +{ + if ((flags & QFLAG) == 0) + list_minus_o_opts (mode, (flags & PFLAG)); + return (sh_chkwrite (EXECUTION_SUCCESS)); +} + +static int +set_shopt_o_options (mode, list, quiet) + int mode; + WORD_LIST *list; + int quiet; +{ + WORD_LIST *l; + int rval; + + for (l = list, rval = EXECUTION_SUCCESS; l; l = l->next) + { + if (set_minus_o_option (mode, l->word->word) == EXECUTION_FAILURE) + rval = EXECUTION_FAILURE; + } + set_shellopts (); + return rval; +} + +/* If we set or unset interactive_comments with shopt, make sure the + change is reflected in $SHELLOPTS. */ +static int +set_shellopts_after_change (option_name, mode) + char *option_name; + int mode; +{ + set_shellopts (); + return (0); +} + +static int +shopt_enable_hostname_completion (option_name, mode) + char *option_name; + int mode; +{ + return (enable_hostname_completion (mode)); +} + +static int +set_compatibility_level (option_name, mode) + char *option_name; + int mode; +{ + /* Need to change logic here as we add more compatibility levels */ + + /* First, check option_name so we can turn off other compat options when + one is set. */ + if (mode && option_name[6] == '3' && option_name[7] == '1') + shopt_compat32 = shopt_compat40 = 0; + else if (mode && option_name[6] == '3' && option_name[7] == '2') + shopt_compat31 = shopt_compat40 = 0; + else if (mode && option_name[6] == '4' && option_name[7] == '0') + shopt_compat31 = shopt_compat32 = 0; + + /* Then set shell_compatibility_level based on what remains */ + if (shopt_compat31) + shell_compatibility_level = 31; + else if (shopt_compat32) + shell_compatibility_level = 32; + else if (shopt_compat40) + shell_compatibility_level = 40; + else + shell_compatibility_level = DEFAULT_COMPAT_LEVEL; + return 0; +} + +#if defined (RESTRICTED_SHELL) +/* Don't allow the value of restricted_shell to be modified. */ + +static int +set_restricted_shell (option_name, mode) + char *option_name; + int mode; +{ + static int save_restricted = -1; + + if (save_restricted == -1) + save_restricted = shell_is_restricted (shell_name); + + restricted_shell = save_restricted; + return (0); +} +#endif /* RESTRICTED_SHELL */ + +/* Not static so shell.c can call it to initialize shopt_login_shell */ +int +set_login_shell (option_name, mode) + char *option_name; + int mode; +{ + shopt_login_shell = login_shell != 0; + return (0); +} + +char ** +get_shopt_options () +{ + char **ret; + int n, i; + + n = sizeof (shopt_vars) / sizeof (shopt_vars[0]); + ret = strvec_create (n + 1); + for (i = 0; shopt_vars[i].name; i++) + ret[i] = savestring (shopt_vars[i].name); + ret[i] = (char *)NULL; + return ret; +} + +/* + * External interface for other parts of the shell. NAME is a string option; + * MODE is 0 if we want to unset an option; 1 if we want to set an option. + * REUSABLE is 1 if we want to print output in a form that may be reused. + */ +int +shopt_setopt (name, mode) + char *name; + int mode; +{ + WORD_LIST *wl; + int r; + + wl = add_string_to_list (name, (WORD_LIST *)NULL); + r = toggle_shopts (mode, wl, 0); + dispose_words (wl); + return r; +} + +int +shopt_listopt (name, reusable) + char *name; + int reusable; +{ + int i; + + if (name == 0) + return (list_shopts ((WORD_LIST *)NULL, reusable ? PFLAG : 0)); + + i = find_shopt (name); + if (i < 0) + { + shopt_error (name); + return (EXECUTION_FAILURE); + } + + print_shopt (name, *shopt_vars[i].value, reusable ? PFLAG : 0); + return (sh_chkwrite (EXECUTION_SUCCESS)); +} + +void +set_bashopts () +{ + char *value; + char tflag[N_SHOPT_OPTIONS]; + int vsize, i, vptr, *ip, exported; + SHELL_VAR *v; + + for (vsize = i = 0; shopt_vars[i].name; i++) + { + tflag[i] = 0; + if (GET_SHOPT_OPTION_VALUE (i)) + { + vsize += strlen (shopt_vars[i].name) + 1; + tflag[i] = 1; + } + } + + value = (char *)xmalloc (vsize + 1); + + for (i = vptr = 0; shopt_vars[i].name; i++) + { + if (tflag[i]) + { + strcpy (value + vptr, shopt_vars[i].name); + vptr += strlen (shopt_vars[i].name); + value[vptr++] = ':'; + } + } + + if (vptr) + vptr--; /* cut off trailing colon */ + value[vptr] = '\0'; + + v = find_variable ("BASHOPTS"); + + /* Turn off the read-only attribute so we can bind the new value, and + note whether or not the variable was exported. */ + if (v) + { + VUNSETATTR (v, att_readonly); + exported = exported_p (v); + } + else + exported = 0; + + v = bind_variable ("BASHOPTS", value, 0); + + /* Turn the read-only attribute back on, and turn off the export attribute + if it was set implicitly by mark_modified_vars and SHELLOPTS was not + exported before we bound the new value. */ + VSETATTR (v, att_readonly); + if (mark_modified_vars && exported == 0 && exported_p (v)) + VUNSETATTR (v, att_exported); + + free (value); +} + +void +parse_bashopts (value) + char *value; +{ + char *vname; + int vptr, ind; + + vptr = 0; + while (vname = extract_colon_unit (value, &vptr)) + { + ind = find_shopt (vname); + if (ind >= 0) + *shopt_vars[ind].value = 1; + free (vname); + } +} + +void +initialize_bashopts (no_bashopts) + int no_bashopts; +{ + char *temp; + SHELL_VAR *var; + + if (no_bashopts == 0) + { + var = find_variable ("BASHOPTS"); + /* set up any shell options we may have inherited. */ + if (var && imported_p (var)) + { + temp = (array_p (var) || assoc_p (var)) ? (char *)NULL : savestring (value_cell (var)); + if (temp) + { + parse_bashopts (temp); + free (temp); + } + } + } + + /* Set up the $BASHOPTS variable. */ + set_bashopts (); +} diff --git a/builtins/test.def b/builtins/test.def index 4adff93b..2eea2f50 100644 --- a/builtins/test.def +++ b/builtins/test.def @@ -1,7 +1,7 @@ This file is test.def, from which is created test.c. It implements the builtin "test" in Bash. -Copyright (C) 1987-2009 Free Software Foundation, Inc. +Copyright (C) 1987-2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. @@ -80,6 +80,7 @@ String operators: Other operators: -o OPTION True if the shell option OPTION is enabled. + -v VAR True if the shell variable VAR is set ! EXPR True if expr is false. EXPR1 -a EXPR2 True if both expr1 AND expr2 are true. EXPR1 -o EXPR2 True if either expr1 OR expr2 is true. diff --git a/builtins/test.def~ b/builtins/test.def~ new file mode 100644 index 00000000..d3552974 --- /dev/null +++ b/builtins/test.def~ @@ -0,0 +1,155 @@ +This file is test.def, from which is created test.c. +It implements the builtin "test" in Bash. + +Copyright (C) 1987-2009 Free Software Foundation, Inc. + +This file is part of GNU Bash, the Bourne Again SHell. + +Bash 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 3 of the License, or +(at your option) any later version. + +Bash 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 Bash. If not, see <http://www.gnu.org/licenses/>. + +$PRODUCES test.c + +$BUILTIN test +$FUNCTION test_builtin +$SHORT_DOC test [expr] +Evaluate conditional expression. + +Exits with a status of 0 (true) or 1 (false) depending on +the evaluation of EXPR. Expressions may be unary or binary. Unary +expressions are often used to examine the status of a file. There +are string operators as well, and numeric comparison operators. + +File operators: + + -a FILE True if file exists. + -b FILE True if file is block special. + -c FILE True if file is character special. + -d FILE True if file is a directory. + -e FILE True if file exists. + -f FILE True if file exists and is a regular file. + -g FILE True if file is set-group-id. + -h FILE True if file is a symbolic link. + -L FILE True if file is a symbolic link. + -k FILE True if file has its `sticky' bit set. + -p FILE True if file is a named pipe. + -r FILE True if file is readable by you. + -s FILE True if file exists and is not empty. + -S FILE True if file is a socket. + -t FD True if FD is opened on a terminal. + -u FILE True if the file is set-user-id. + -w FILE True if the file is writable by you. + -x FILE True if the file is executable by you. + -O FILE True if the file is effectively owned by you. + -G FILE True if the file is effectively owned by your group. + -N FILE True if the file has been modified since it was last read. + + FILE1 -nt FILE2 True if file1 is newer than file2 (according to + modification date). + + FILE1 -ot FILE2 True if file1 is older than file2. + + FILE1 -ef FILE2 True if file1 is a hard link to file2. + +String operators: + + -z STRING True if string is empty. + + -n STRING + STRING True if string is not empty. + + STRING1 = STRING2 + True if the strings are equal. + STRING1 != STRING2 + True if the strings are not equal. + STRING1 < STRING2 + True if STRING1 sorts before STRING2 lexicographically. + STRING1 > STRING2 + True if STRING1 sorts after STRING2 lexicographically. + +Other operators: + + -o OPTION True if the shell option OPTION is enabled. + -v VAR True if the shell variable VAR is set + ! EXPR True if expr is false. + EXPR1 -a EXPR2 True if both expr1 AND expr2 are true. + EXPR1 -o EXPR2 True if either expr1 OR expr2 is true. + + arg1 OP arg2 Arithmetic tests. OP is one of -eq, -ne, + -lt, -le, -gt, or -ge. + +Arithmetic binary operators return true if ARG1 is equal, not-equal, +less-than, less-than-or-equal, greater-than, or greater-than-or-equal +than ARG2. + +Exit Status: +Returns success if EXPR evaluates to true; fails if EXPR evaluates to +false or an invalid argument is given. +$END + +$BUILTIN [ +$DOCNAME test_bracket +$FUNCTION test_builtin +$SHORT_DOC [ arg... ] +Evaluate conditional expression. + +This is a synonym for the "test" builtin, but the last argument must +be a literal `]', to match the opening `['. +$END + +#include <config.h> + +#if defined (HAVE_UNISTD_H) +# ifdef _MINIX +# include <sys/types.h> +# endif +# include <unistd.h> +#endif + +#include "../bashansi.h" +#include "../bashintl.h" + +#include "../shell.h" +#include "../test.h" +#include "common.h" + +extern char *this_command_name; + +/* TEST/[ builtin. */ +int +test_builtin (list) + WORD_LIST *list; +{ + char **argv; + int argc, result; + + /* We let Matthew Bradburn and Kevin Braunsdorf's code do the + actual test command. So turn the list of args into an array + of strings, since that is what their code wants. */ + if (list == 0) + { + if (this_command_name[0] == '[' && !this_command_name[1]) + { + builtin_error (_("missing `]'")); + return (EX_BADUSAGE); + } + + return (EXECUTION_FAILURE); + } + + argv = make_builtin_argv (list, &argc); + result = test_command (argc, argv); + free ((char *)argv); + + return (result); +} diff --git a/builtins/trap.def b/builtins/trap.def index 4de3990d..9523cfc3 100644 --- a/builtins/trap.def +++ b/builtins/trap.def @@ -1,7 +1,7 @@ This file is trap.def, from which is created trap.c. It implements the builtin "trap" in Bash. -Copyright (C) 1987-2009 Free Software Foundation, Inc. +Copyright (C) 1987-2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. diff --git a/builtins/trap.def~ b/builtins/trap.def~ index 1c66f82c..4de3990d 100644 --- a/builtins/trap.def~ +++ b/builtins/trap.def~ @@ -129,6 +129,7 @@ trap_builtin (list) else if (display || list == 0) { initialize_terminating_signals (); + get_all_original_signals (); return (sh_chkwrite (display_traps (list))); } else diff --git a/builtins/ulimit.def b/builtins/ulimit.def index 97fcb9ce..03cbe8a0 100644 --- a/builtins/ulimit.def +++ b/builtins/ulimit.def @@ -1,7 +1,7 @@ This file is ulimit.def, from which is created ulimit.c. It implements the builtin "ulimit" in Bash. -Copyright (C) 1987-2009 Free Software Foundation, Inc. +Copyright (C) 1987-2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. diff --git a/builtins/ulimit.def~ b/builtins/ulimit.def~ index 16673063..97fcb9ce 100644 --- a/builtins/ulimit.def~ +++ b/builtins/ulimit.def~ @@ -118,7 +118,7 @@ extern int errno; # undef HAVE_RESOURCE #endif -#if !HAVE_RESOURCE && HAVE_ULIMIT_H +#if !defined (HAVE_RESOURCE) && defined (HAVE_ULIMIT_H) # include <ulimit.h> #endif @@ -1,7 +1,7 @@ /* command.h -- The structures used internally to represent commands, and the extern declarations of the functions used to create them. */ -/* Copyright (C) 1993-2009 Free Software Foundation, Inc. +/* Copyright (C) 1993-2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. @@ -105,6 +105,7 @@ enum command_type { cm_for, cm_case, cm_while, cm_if, cm_simple, cm_select, #define SUBSHELL_PIPE 0x10 /* subshell from a pipeline element */ #define SUBSHELL_PROCSUB 0x20 /* subshell caused by <(command) or >(command) */ #define SUBSHELL_COPROC 0x40 /* subshell from a coproc pipeline */ +#define SUBSHELL_RESETTRAP 0x80 /* subshell needs to reset trap strings on first call to trap */ /* A structure which represents a word. */ typedef struct word_desc { @@ -5,12 +5,12 @@ .\" Case Western Reserve University .\" chet@po.cwru.edu .\" -.\" Last Change: Sat Apr 17 23:24:15 EDT 2010 +.\" Last Change: Sun May 30 17:03:08 EDT 2010 .\" .\" bash_builtins, strip all but Built-Ins section .if \n(zZ=1 .ig zZ .if \n(zY=1 .ig zY -.TH BASH 1 "2010 April 17" "GNU Bash-4.1" +.TH BASH 1 "2010 May 30" "GNU Bash-4.1" .\" .\" There's some problem with having a `@' .\" in a tagged paragraph with the BSD man macros. @@ -565,6 +565,15 @@ under .B "Shell Variables" below. .PP +When the shell is in \fIposix mode\fP, \fBtime\fP +may be followed by a newline. In this case, the shell displays the +total user and system time consumed by the shell and its children. +The +.SM +.B TIMEFORMAT +variable may be used to specify the format of +the time information. +.PP Each command in a pipeline is executed as a separate process (i.e., in a subshell). .SS Lists @@ -1881,6 +1890,11 @@ A sample value is .if t \f(CW".o:~"\fP. .if n ".o:~". .TP +.B FUNCNEST +If set to a numeric value greater than 0, defines a maximum function +nesting level. Function invocations that exceed this nesting level +will cause the current command to abort. +.TP .B GLOBIGNORE A colon-separated list of patterns defining the set of filenames to be ignored by pathname expansion. @@ -3781,6 +3795,11 @@ Variables local to the function may be declared with the builtin command. Ordinarily, variables and their values are shared between the function and its caller. .PP +The \fBFUNCNEST\fP variable, if set to a numeric value greater +than 0, defines a maximum function nesting level. Function +invocations that exceed the limit cause the entire command to +abort. +.PP If the builtin command .B return is executed in a function, the function completes and @@ -4021,7 +4040,7 @@ True if \fIfile1\fP is older than \fIfile2\fP, or if \fIfile2\fP exists and \fIfile1\fP does not. .TP .B \-o \fIoptname\fP -True if shell option +True if the shell option .I optname is enabled. See the list of options under the description of the @@ -4030,6 +4049,11 @@ option to the .B set builtin below. .TP +.B \-v \fIvarname\fP +True if the shell variable +.I varname +is set (has been assigned a value). +.TP .B \-z \fIstring\fP True if the length of \fIstring\fP is zero. .TP @@ -7058,10 +7082,10 @@ is greater than the number of enclosing loops, the last enclosing loop (the ``top-level'' loop) is resumed. The return value is 0 unless \fIn\fP is not greater than or equal to 1. .TP -\fBdeclare\fP [\fB\-aAfFilrtux\fP] [\fB\-p\fP] [\fIname\fP[=\fIvalue\fP] ...] +\fBdeclare\fP [\fB\-aAfFgilrtux\fP] [\fB\-p\fP] [\fIname\fP[=\fIvalue\fP] ...] .PD 0 .TP -\fBtypeset\fP [\fB\-aAfFilrtux\fP] [\fB\-p\fP] [\fIname\fP[=\fIvalue\fP] ...] +\fBtypeset\fP [\fB\-aAfFgilrtux\fP] [\fB\-p\fP] [\fIname\fP[=\fIvalue\fP] ...] .PD Declare variables and/or give them attributes. If no \fIname\fPs are given then display the values of variables. @@ -7090,6 +7114,11 @@ are displayed as well. The .B \-F option implies .BR \-f . +The +.B \-g +option forces variables to be created or modified at the global scope, +even when \fBdeclare\fP is executed in a shell function. +It is ignored in all other cases. The following options can be used to restrict output to variables with the specified attribute or to give variables attributes: @@ -7144,11 +7173,11 @@ turns off the attribute instead, with the exceptions that \fB+a\fP may not be used to destroy an array variable and \fB+r\fP will not remove the readonly attribute. -When used in a function, -makes each +When used in a function, makes each \fIname\fP local, as with the .B local -command. +command, +unless the \fB\-g\P option is supplied, If a variable name is followed by =\fIvalue\fP, the value of the variable is set to \fIvalue\fP. The return value is 0 unless an invalid option is encountered, @@ -8004,7 +8033,8 @@ is specified without .BR \-c , the default quantum is 5000. When \fIcallback\fP is evaluated, it is supplied the index of the next -array element to be assigned as an additional argument. +array element to be assigned and the line to be assigned to that element +as additional arguments. \fIcallback\fP is evaluated after the line is read but before the array element is assigned. .PP @@ -8068,32 +8098,49 @@ directory change fails. \fBprintf\fP [\fB\-v\fP \fIvar\fP] \fIformat\fP [\fIarguments\fP] Write the formatted \fIarguments\fP to the standard output under the control of the \fIformat\fP. +The \fB\-v\fP option causes the output to be assigned to the variable +\fIvar\fP rather than being printed to the standard output. +.sp 1 The \fIformat\fP is a character string which contains three types of objects: plain characters, which are simply copied to standard output, character escape sequences, which are converted and copied to the standard output, and format specifications, each of which causes printing of the next successive \fIargument\fP. -In addition to the standard \fIprintf\fP(1) formats, \fB%b\fP causes +In addition to the standard \fIprintf\fP(1) format specifications, +\fBprintf\fP interprets the following extensions: +.RS +.PD 0 +.TP +.B %b +causes \fBprintf\fP to expand backslash escape sequences in the corresponding \fIargument\fP (except that \fB\ec\fP terminates output, backslashes in \fB\e\(aq\fP, \fB\e"\fP, and \fB\e?\fP are not removed, and octal escapes -beginning with \fB\e0\fP may contain up to four digits), -and \fB%q\fP causes \fBprintf\fP to output the corresponding +beginning with \fB\e0\fP may contain up to four digits). +.TP +.B %q +causes \fBprintf\fP to output the corresponding \fIargument\fP in a format that can be reused as shell input. -.sp 1 +.TP +.B %(\fIdatefmt\fP)T +causes \fBprintf\fP to output the date-time string resulting from using +\fIdatefmt\fP as a format string for \fIstrftime\fP(3). The corresponding +\fIargument\fP is an integer representing the number of seconds since the +epoch. Two special argument values may be used: -1 represents the current +time, and -2 represents the time the shell was invoked. +.PD +.PP Arguments to non-string format specifiers are treated as C constants, except that a leading plus or minus sign is allowed, and if the leading character is a single or double quote, the value is the ASCII value of the following character. -.sp 1 -The \fB\-v\fP option causes the output to be assigned to the variable -\fIvar\fP rather than being printed to the standard output. -.sp 1 +.PP The \fIformat\fP is reused as necessary to consume all of the \fIarguments\fP. If the \fIformat\fP requires more \fIarguments\fP than are supplied, the extra format specifications behave as if a zero value or null string, as -appropriate, had been supplied. The return value is zero on success, -non-zero on failure. +appropriate, had been supplied. +The return value is zero on success, non-zero on failure. +.RE .TP \fBpushd\fP [\fB\-n\fP] [+\fIn\fP] [\-\fIn\fP] .PD 0 diff --git a/doc/bash.1~ b/doc/bash.1~ index 89e4a88d..b05e721b 100644 --- a/doc/bash.1~ +++ b/doc/bash.1~ @@ -5,12 +5,12 @@ .\" Case Western Reserve University .\" chet@po.cwru.edu .\" -.\" Last Change: Sat Apr 17 23:24:15 EDT 2010 +.\" Last Change: Sat May 29 20:59:17 EDT 2010 .\" .\" bash_builtins, strip all but Built-Ins section .if \n(zZ=1 .ig zZ .if \n(zY=1 .ig zY -.TH BASH 1 "2010 April 17" "GNU Bash-4.1" +.TH BASH 1 "2010 May 29" "GNU Bash-4.1" .\" .\" There's some problem with having a `@' .\" in a tagged paragraph with the BSD man macros. @@ -565,6 +565,15 @@ under .B "Shell Variables" below. .PP +When the shell is in \fIposix mode\fP, \fBtime\fP +may be followed by a newline. In this case, the shell displays the +total user and system time consumed by the shell and its children. +The +.SM +.B TIMEFORMAT +variable may be used to specify the format of +the time information. +.PP Each command in a pipeline is executed as a separate process (i.e., in a subshell). .SS Lists @@ -1104,6 +1113,14 @@ the eight-bit character whose value is the octal value \fInnn\fP the eight-bit character whose value is the hexadecimal value \fIHH\fP (one or two hex digits) .TP +.B \eu\fIHHHH\fP +the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value +\fIHHHH\fP (one to four hex digits) +.TP +.B \eU\fIHHHHHHHH\fP +the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value +\fIHHHHHHHH\fP (one to eight hex digits) +.TP .B \ec\fIx\fP a control-\fIx\fP character .PD @@ -1873,6 +1890,11 @@ A sample value is .if t \f(CW".o:~"\fP. .if n ".o:~". .TP +.B FUNCNEST +If set to a numeric value greater than 0, defines a maximum function +nesting level. Function invocations that exceed this nesting level +will cause the current command to abort. +.TP .B GLOBIGNORE A colon-separated list of patterns defining the set of filenames to be ignored by pathname expansion. @@ -3773,6 +3795,11 @@ Variables local to the function may be declared with the builtin command. Ordinarily, variables and their values are shared between the function and its caller. .PP +The \fBFUNCNEST\fP variable, if set to a numeric value greater +than 0, defines a maximum function nesting level. Function +invocations that exceed the limit cause the entire command to +abort. +.PP If the builtin command .B return is executed in a function, the function completes and @@ -4013,7 +4040,7 @@ True if \fIfile1\fP is older than \fIfile2\fP, or if \fIfile2\fP exists and \fIfile1\fP does not. .TP .B \-o \fIoptname\fP -True if shell option +True if the shell option .I optname is enabled. See the list of options under the description of the @@ -4022,6 +4049,11 @@ option to the .B set builtin below. .TP +.B \-v \fIvarname\fP +True if the shell variable +.I varname +is set (has been assigned a value). +.TP .B \-z \fIstring\fP True if the length of \fIstring\fP is zero. .TP @@ -7050,10 +7082,10 @@ is greater than the number of enclosing loops, the last enclosing loop (the ``top-level'' loop) is resumed. The return value is 0 unless \fIn\fP is not greater than or equal to 1. .TP -\fBdeclare\fP [\fB\-aAfFilrtux\fP] [\fB\-p\fP] [\fIname\fP[=\fIvalue\fP] ...] +\fBdeclare\fP [\fB\-aAfFgilrtux\fP] [\fB\-p\fP] [\fIname\fP[=\fIvalue\fP] ...] .PD 0 .TP -\fBtypeset\fP [\fB\-aAfFilrtux\fP] [\fB\-p\fP] [\fIname\fP[=\fIvalue\fP] ...] +\fBtypeset\fP [\fB\-aAfFgilrtux\fP] [\fB\-p\fP] [\fIname\fP[=\fIvalue\fP] ...] .PD Declare variables and/or give them attributes. If no \fIname\fPs are given then display the values of variables. @@ -7082,6 +7114,11 @@ are displayed as well. The .B \-F option implies .BR \-f . +The +.B \-g +option forces variables to be created or modified at the global scope, +even when \fBdeclare\fP is executed in a shell function. +It is ignored in all other cases. The following options can be used to restrict output to variables with the specified attribute or to give variables attributes: @@ -7136,11 +7173,11 @@ turns off the attribute instead, with the exceptions that \fB+a\fP may not be used to destroy an array variable and \fB+r\fP will not remove the readonly attribute. -When used in a function, -makes each +When used in a function, makes each \fIname\fP local, as with the .B local -command. +command, +unless the \fB\-g\P option is supplied, If a variable name is followed by =\fIvalue\fP, the value of the variable is set to \fIvalue\fP. The return value is 0 unless an invalid option is encountered, @@ -7293,6 +7330,14 @@ the eight-bit character whose value is the octal value \fInnn\fP .B \ex\fIHH\fP the eight-bit character whose value is the hexadecimal value \fIHH\fP (one or two hex digits) +.TP +.B \eu\fIHHHH\fP +the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value +\fIHHHH\fP (one to four hex digits) +.TP +.B \eU\fIHHHHHHHH\fP +the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value +\fIHHHHHHHH\fP (one to eight hex digits) .PD .RE .TP @@ -7988,7 +8033,8 @@ is specified without .BR \-c , the default quantum is 5000. When \fIcallback\fP is evaluated, it is supplied the index of the next -array element to be assigned as an additional argument. +array element to be assigned and the line to be assigned to that element +as additional arguments. \fIcallback\fP is evaluated after the line is read but before the array element is assigned. .PP @@ -8052,32 +8098,49 @@ directory change fails. \fBprintf\fP [\fB\-v\fP \fIvar\fP] \fIformat\fP [\fIarguments\fP] Write the formatted \fIarguments\fP to the standard output under the control of the \fIformat\fP. +The \fB\-v\fP option causes the output to be assigned to the variable +\fIvar\fP rather than being printed to the standard output. +.sp 1 The \fIformat\fP is a character string which contains three types of objects: plain characters, which are simply copied to standard output, character escape sequences, which are converted and copied to the standard output, and format specifications, each of which causes printing of the next successive \fIargument\fP. -In addition to the standard \fIprintf\fP(1) formats, \fB%b\fP causes +In addition to the standard \fIprintf\fP(1) format specifications, +\fBprintf\fP interprets the following extensions: +.RS +.PD 0 +.TP +.B %b +causes \fBprintf\fP to expand backslash escape sequences in the corresponding \fIargument\fP (except that \fB\ec\fP terminates output, backslashes in \fB\e\(aq\fP, \fB\e"\fP, and \fB\e?\fP are not removed, and octal escapes -beginning with \fB\e0\fP may contain up to four digits), -and \fB%q\fP causes \fBprintf\fP to output the corresponding +beginning with \fB\e0\fP may contain up to four digits). +.TP +.B %q +causes \fBprintf\fP to output the corresponding \fIargument\fP in a format that can be reused as shell input. -.sp 1 +.TP +.B %(\fIdatefmt\fP)T +causes \fBprintf\fP to output the date-time string resulting from using +\fIdatefmt\fP as a format string for \fIstrftime\fP(3). The corresponding +\fIargument\fP is an integer representing the number of seconds since the +epoch. Two special argument values may be used: -1 represents the current +time, and -2 represents the time the shell was invoked. +.PD +.PP Arguments to non-string format specifiers are treated as C constants, except that a leading plus or minus sign is allowed, and if the leading character is a single or double quote, the value is the ASCII value of the following character. -.sp 1 -The \fB\-v\fP option causes the output to be assigned to the variable -\fIvar\fP rather than being printed to the standard output. -.sp 1 +.PP The \fIformat\fP is reused as necessary to consume all of the \fIarguments\fP. If the \fIformat\fP requires more \fIarguments\fP than are supplied, the extra format specifications behave as if a zero value or null string, as -appropriate, had been supplied. The return value is zero on success, -non-zero on failure. +appropriate, had been supplied. +The return value is zero on success, non-zero on failure. +.RE .TP \fBpushd\fP [\fB\-n\fP] [+\fIn\fP] [\-\fIn\fP] .PD 0 diff --git a/doc/bashref.texi b/doc/bashref.texi index d9f36fa0..be78c6ec 100644 --- a/doc/bashref.texi +++ b/doc/bashref.texi @@ -596,6 +596,7 @@ some other grouping. * Lists:: How to execute commands sequentially. * Compound Commands:: Shell commands for control flow. * Coprocesses:: Two-way communication between commands. +* GNU Parallel:: Running commands in parallel. @end menu @node Simple Commands @@ -654,6 +655,12 @@ The use of @code{time} as a reserved word permits the timing of shell builtins, shell functions, and pipelines. An external @code{time} command cannot time these easily. +When the shell is in @sc{posix} mode (@pxref{Bash POSIX Mode}), @code{time} +may be followed by a newline. In this case, the shell displays the +total user and system time consumed by the shell and its children. +The @env{TIMEFORMAT} variable may be used to specify the format of +the time information. + If the pipeline is not executed asynchronously (@pxref{Lists}), the shell waits for all commands in the pipeline to complete. @@ -1132,6 +1139,79 @@ builtin command may be used to wait for the coprocess to terminate. The return status of a coprocess is the exit status of @var{command}. +@node GNU Parallel +@subsection GNU Parallel + +GNU Parallel, as its name suggests, can be used to build and run commands +in parallel. You may run the same command with different arguments, whether +they are filenames, usernames, hostnames, or lines read from files. + +For a complete description, refer to the GNU Parallel documentation. A few +examples should provide a brief introduction to its use. + +For example, it is easy to prefix each line in a text file with a specified +string: +@example +cat file | parallel -k echo prefix_string +@end example + +The @option{-k} option is required to preserve the lines' order. + +Similarly, you can append a specified string to each line in a text file: +@example +cat file | parallel -k echo @{@} append_string +@end example + +You can use Parallel to move files from the current directory when the +number of files is too large to process with one @code{mv} invocation: +@example +ls | parallel mv @{@} destdir +@end example + +As you can see, the @{@} is replaced with each line read from standard input. +This will run as many @code{mv} commands as there are files in the current +directory. You can emulate a parallel @code{xargs} by adding the @option{-X} +option: +@example +ls | parallel -X mv @{@} destdir +@end example + +GNU Parallel can replace certain common idioms that operate on lines read +from a file (in this case, filenames): +@example + for x in $(cat list); do + do-something1 $x config-$x + do-something2 < $x + done | process-output +@end example + +with a more compact syntax reminiscent of lambdas: +@example +cat list | parallel "do-something1 @{@} config-@{@} ; do-something2 < @{@}" | process-output +@end example + +Parallel provides a built-in mechanism to remove filename extensions, which +lends itself to batch file transformations or renaming: +@example +ls *.gz | parallel -j+0 "zcat @{@} | bzip2 >@{.@}.bz2 && rm @{@}" +@end example + +This will recompress all files in the current directory with names ending +in .gz using bzip2, running one job per CPU (-j+0) in parallel. + +If a command generates output, you may want to preserve the input order in +the output. For instance, the following command +@example +@{ echo foss.org.my ; echo debian.org; echo freenetproject.org; @} | parallel traceroute +@end example + +will display as output the traceroute invocation that finishes first. Using +the @option{-k} option, as we saw above +@example +@{ echo foss.org.my ; echo debian.org; echo freenetproject.org; @} | parallel -k traceroute +@end example +will ensure that the output of @code{traceroute foss.org.my} is displayed first. + @node Shell Functions @section Shell Functions @cindex shell function @@ -1204,6 +1284,11 @@ shell option has been enabled. @xref{Bourne Shell Builtins}, for the description of the @code{trap} builtin. +The @env{FUNCNEST} variable, if set to a numeric value greater +than 0, defines a maximum function nesting level. Function +invocations that exceed the limit cause the entire command to +abort. + If the builtin command @code{return} is executed in a function, the function completes and execution resumes with the next command after the function @@ -3456,6 +3541,11 @@ If the @code{extdebug} shell option is enabled using @code{shopt} (@pxref{The Shopt Builtin}), the source file name and line number where the function is defined are displayed as well. @option{-F} implies @option{-f}. + +The @option{-g} option forces variables to be created or modified at +the global scope, even when \fBdeclare\fP is executed in a shell function. +It is ignored in all other cases. + The following options can be used to restrict output to variables with the specified attributes or to give variables attributes: @@ -3504,8 +3594,9 @@ with the exceptions that @samp{+a} may not be used to destroy an array variable and @samp{+r} will not remove the readonly attribute. When used in a function, @code{declare} makes each @var{name} local, -as with the @code{local} command. If a variable name is followed by -=@var{value}, the value of the variable is set to @var{value}. +as with the @code{local} command, unless the @samp{-g} option is used. +If a variable name is followed by =@var{value}, the value of the variable +is set to @var{value}. The return status is zero unless an invalid option is encountered, an attempt is made to define a function using @samp{-f foo=bar}, @@ -3693,7 +3784,8 @@ Specify the number of lines read between each call to @var{callback}. If @option{-C} is specified without @option{-c}, the default quantum is 5000. When @var{callback} is evaluated, it is supplied the index of the next -array element to be assigned as an additional argument. +array element to be assigned and the line to be assigned to that element +as additional arguments. @var{callback} is evaluated after the line is read but before the array element is assigned. @@ -3711,28 +3803,41 @@ printf [-v @var{var}] @var{format} [@var{arguments}] @end example Write the formatted @var{arguments} to the standard output under the control of the @var{format}. +The @option{-v} option causes the output to be assigned to the variable +@var{var} rather than being printed to the standard output. + The @var{format} is a character string which contains three types of objects: plain characters, which are simply copied to standard output, character escape sequences, which are converted and copied to the standard output, and format specifications, each of which causes printing of the next successive @var{argument}. -In addition to the standard @code{printf(1)} formats, @samp{%b} causes -@code{printf} to expand backslash escape sequences in the corresponding -@var{argument}, +In addition to the standard @code{printf(1)} formats, @code{printf} +interprets the following extensions: + +@table @code +@item %b +causes @code{printf} to expand backslash escape sequences in the +corresponding @var{argument}, (except that @samp{\c} terminates output, backslashes in @samp{\'}, @samp{\"}, and @samp{\?} are not removed, and octal escapes -beginning with @samp{\0} may contain up to four digits), -and @samp{%q} causes @code{printf} to output the +beginning with @samp{\0} may contain up to four digits). +@item %q +causes @code{printf} to output the corresponding @var{argument} in a format that can be reused as shell input. +@item %(@var{datefmt})T +causes @code{printf} to output the date-time string resulting from using +@var{datefmt} as a format string for @code{strftime}(3). The corresponding +@var{argument} is an integer representing the number of seconds since the +epoch. Two special argument values may be used: -1 represents the current +time, and -2 represents the time the shell was invoked. +@end table +@noindent Arguments to non-string format specifiers are treated as C language constants, except that a leading plus or minus sign is allowed, and if the leading character is a single or double quote, the value is the ASCII value of the following character. -The @option{-v} option causes the output to be assigned to the variable -@var{var} rather than being printed to the standard output. - The @var{format} is reused as necessary to consume all of the @var{arguments}. If the @var{format} requires more @var{arguments} than are supplied, the extra format specifications behave as if a zero value or null string, as @@ -4954,6 +5059,11 @@ Assignments to @env{FUNCNAME} have no effect and return an error status. If @env{FUNCNAME} is unset, it loses its special properties, even if it is subsequently reset. +@item FUNCNEST +If set to a numeric value greater than 0, defines a maximum function +nesting level. Function invocations that exceed this nesting level +will cause the current command to abort. + @item GLOBIGNORE A colon-separated list of patterns defining the set of filenames to be ignored by filename expansion. @@ -5855,10 +5965,13 @@ True if @var{file1} is older than @var{file2}, or if @var{file2} exists and @var{file1} does not. @item -o @var{optname} -True if shell option @var{optname} is enabled. +True if the shell option @var{optname} is enabled. The list of options appears in the description of the @option{-o} option to the @code{set} builtin (@pxref{The Set Builtin}). +@item -v @var{varname} +True if the shell variable @var{varname} is set (has been assigned a value). + @item -z @var{string} True if the length of @var{string} is zero. @@ -6490,6 +6603,11 @@ Non-interactive shells exit if a syntax error in an arithmetic expansion results in an invalid expression. @item +Non-interactive shells exit if there is a syntax error in a script read +with the @code{.} or @code{source} builtins, or in a string processed by +the @code{eval} builtin. + +@item Redirection operators do not perform filename expansion on the word in the redirection unless the shell is interactive. @@ -6508,6 +6626,19 @@ causes a fatal syntax error in non-interactive shells. during command lookup. @item +The @code{time} reserved word may be used by itself as a command. When +used in this way, it displays timing statistics for the shell and its +completed children. The @env{TIMEFORMAT} variable controls the format +of the timing information. + +@item +When parsing and expanding a $@{@dots{}@} expansion that appears within +double quotes, single quotes are no longer special and cannot be used to +quote a closing brace or other special character, unless the operator is +one of those defined to perform pattern removal. In this case, they do +not have to appear as matched pairs. + +@item If a @sc{posix} special builtin returns an error status, a non-interactive shell exits. The fatal errors are those listed in the @sc{posix} standard, and include things like passing incorrect options, diff --git a/doc/bashref.texi~ b/doc/bashref.texi~ index 15ede23b..9cb2e6b4 100644 --- a/doc/bashref.texi~ +++ b/doc/bashref.texi~ @@ -522,6 +522,12 @@ the eight-bit character whose value is the octal value @var{nnn} @item \x@var{HH} the eight-bit character whose value is the hexadecimal value @var{HH} (one or two hex digits) +@item \u@var{HHHH} +the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value +@var{HHHH} (one to four hex digits) +@item \U@var{HHHHHHHH} +the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value +@var{HHHHHHHH} (one to eight hex digits) @item \c@var{x} a control-@var{x} character @end table @@ -590,6 +596,7 @@ some other grouping. * Lists:: How to execute commands sequentially. * Compound Commands:: Shell commands for control flow. * Coprocesses:: Two-way communication between commands. +* GNU Parallel:: Running commands in parallel. @end menu @node Simple Commands @@ -648,6 +655,12 @@ The use of @code{time} as a reserved word permits the timing of shell builtins, shell functions, and pipelines. An external @code{time} command cannot time these easily. +When the shell is in @sc{posix} mode (@pxref{Bash POSIX Mode}), @code{time} +may be followed by a newline. In this case, the shell displays the +total user and system time consumed by the shell and its children. +The @env{TIMEFORMAT} variable may be used to specify the format of +the time information. + If the pipeline is not executed asynchronously (@pxref{Lists}), the shell waits for all commands in the pipeline to complete. @@ -1126,6 +1139,79 @@ builtin command may be used to wait for the coprocess to terminate. The return status of a coprocess is the exit status of @var{command}. +@node GNU Parallel +@subsection GNU Parallel + +GNU Parallel, as its name suggests, can be used to build and run commands +in parallel. You may run the same command with different arguments, whether +they are filenames, usernames, hostnames, or lines read from files. + +For a complete description, refer to the GNU Parallel documentation. A few +examples should provide a brief introduction to its use. + +For example, it is easy to prefix each line in a text file with a specified +string: +@example +cat file | parallel -k echo prefix_string +@end example + +The @option{-k} option is required to preserve the lines' order. + +Similarly, you can append a specified string to each line in a text file: +@example +cat file | parallel -k echo @{@} append_string +@end example + +You can use Parallel to move files from the current directory when the +number of files is too large to process with one @code{mv} invocation: +@example +ls | parallel mv @{@} destdir +@end example + +As you can see, the @{@} is replaced with each line read from standard input. +This will run as many @code{mv} commands as there are files in the current +directory. You can emulate a parallel @code{xargs} by adding the @option{-X} +option: +@example +ls | parallel -X mv @{@} destdir +@end example + +GNU Parallel can replace certain common idioms that operate on lines read +from a file (in this case, filenames): +@example + for x in $(cat list); do + do-something1 $x config-$x + do-something2 < $x + done | process-output +@end example + +with a more compact syntax reminiscent of lambdas: +@example +cat list | parallel "do-something1 @{@} config-@{@} ; do-something2 < @{@}" | process-output +@end example + +Parallel provides a built-in mechanism to remove filename extensions, which +lends itself to batch file transformations or renaming: +@example +ls *.gz | parallel -j+0 "zcat @{@} | bzip2 >@{.@}.bz2 && rm @{@}" +@end example + +This will recompress all files in the current directory with names ending +in .gz using bzip2, running one job per CPU (-j+0) in parallel. + +If a command generates output, you may want to preserve the input order in +the output. For instance, the following command +@example +@{ echo foss.org.my ; echo debian.org; echo freenetproject.org; @} | parallel traceroute +@end example + +will display as output the traceroute invocation that finishes first. Using +the @option{-k} option, as we saw above +@example +@{ echo foss.org.my ; echo debian.org; echo freenetproject.org; @} | parallel -k traceroute +@end example +will ensure that the output of @code{traceroute foss.org.my} is displayed first. + @node Shell Functions @section Shell Functions @cindex shell function @@ -1198,6 +1284,11 @@ shell option has been enabled. @xref{Bourne Shell Builtins}, for the description of the @code{trap} builtin. +The @env{FUNCNEST} variable, if set to a numeric value greater +than 0, defines a maximum function nesting level. Function +invocations that exceed the limit cause the entire command to +abort. + If the builtin command @code{return} is executed in a function, the function completes and execution resumes with the next command after the function @@ -3450,6 +3541,11 @@ If the @code{extdebug} shell option is enabled using @code{shopt} (@pxref{The Shopt Builtin}), the source file name and line number where the function is defined are displayed as well. @option{-F} implies @option{-f}. + +The @option{-g} option forces variables to be created or modified at +the global scope, even when \fBdeclare\fP is executed in a shell function. +It is ignored in all other cases. + The following options can be used to restrict output to variables with the specified attributes or to give variables attributes: @@ -3498,8 +3594,9 @@ with the exceptions that @samp{+a} may not be used to destroy an array variable and @samp{+r} will not remove the readonly attribute. When used in a function, @code{declare} makes each @var{name} local, -as with the @code{local} command. If a variable name is followed by -=@var{value}, the value of the variable is set to @var{value}. +as with the @code{local} command, unless the @samp{-g} option is used. +If a variable name is followed by =@var{value}, the value of the variable +is set to @var{value}. The return status is zero unless an invalid option is encountered, an attempt is made to define a function using @samp{-f foo=bar}, @@ -3557,6 +3654,12 @@ the eight-bit character whose value is the octal value @var{nnn} @item \x@var{HH} the eight-bit character whose value is the hexadecimal value @var{HH} (one or two hex digits) +@item \u@var{HHHH} +the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value +@var{HHHH} (one to four hex digits) +@item \U@var{HHHHHHHH} +the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value +@var{HHHHHHHH} (one to eight hex digits) @end table @item enable @@ -3681,7 +3784,8 @@ Specify the number of lines read between each call to @var{callback}. If @option{-C} is specified without @option{-c}, the default quantum is 5000. When @var{callback} is evaluated, it is supplied the index of the next -array element to be assigned as an additional argument. +array element to be assigned and the line to be assigned to that element +as additional arguments. @var{callback} is evaluated after the line is read but before the array element is assigned. @@ -4942,6 +5046,11 @@ Assignments to @env{FUNCNAME} have no effect and return an error status. If @env{FUNCNAME} is unset, it loses its special properties, even if it is subsequently reset. +@item FUNCNEST +If set to a numeric value greater than 0, defines a maximum function +nesting level. Function invocations that exceed this nesting level +will cause the current command to abort. + @item GLOBIGNORE A colon-separated list of patterns defining the set of filenames to be ignored by filename expansion. @@ -5843,10 +5952,13 @@ True if @var{file1} is older than @var{file2}, or if @var{file2} exists and @var{file1} does not. @item -o @var{optname} -True if shell option @var{optname} is enabled. +True if the shell option @var{optname} is enabled. The list of options appears in the description of the @option{-o} option to the @code{set} builtin (@pxref{The Set Builtin}). +@item -v @var{varname} +True if the shell variable @var{varname} is set (has been assigned a value). + @item -z @var{string} True if the length of @var{string} is zero. @@ -6478,6 +6590,11 @@ Non-interactive shells exit if a syntax error in an arithmetic expansion results in an invalid expression. @item +Non-interactive shells exit if there is a syntax error in a script read +with the @code{.} or @code{source} builtins, or in a string processed by +the @code{eval} builtin. + +@item Redirection operators do not perform filename expansion on the word in the redirection unless the shell is interactive. @@ -6496,6 +6613,19 @@ causes a fatal syntax error in non-interactive shells. during command lookup. @item +The @code{time} reserved word may be used by itself as a command. When +used in this way, it displays timing statistics for the shell and its +completed children. The @env{TIMEFORMAT} variable controls the format +of the timing information. + +@item +When parsing and expanding a $@{@dots{}@} expansion that appears within +double quotes, single quotes are no longer special and cannot be used to +quote a closing brace or other special character, unless the operator is +one of those defined to perform pattern removal. In this case, they do +not have to appear as matched pairs. + +@item If a @sc{posix} special builtin returns an error status, a non-interactive shell exits. The fatal errors are those listed in the @sc{posix} standard, and include things like passing incorrect options, diff --git a/doc/version.texi b/doc/version.texi index 972af30c..16b3c8ed 100644 --- a/doc/version.texi +++ b/doc/version.texi @@ -2,9 +2,9 @@ Copyright (C) 1988-2010 Free Software Foundation, Inc. @end ignore -@set LASTCHANGE Sat Apr 17 23:23:55 EDT 2010 +@set LASTCHANGE Sun May 30 17:03:21 EDT 2010 @set EDITION 4.1 @set VERSION 4.1 -@set UPDATED 17 April 2010 -@set UPDATED-MONTH April 2010 +@set UPDATED 30 May 2010 +@set UPDATED-MONTH May 2010 diff --git a/doc/version.texi~ b/doc/version.texi~ index 1628278f..76d40573 100644 --- a/doc/version.texi~ +++ b/doc/version.texi~ @@ -2,9 +2,9 @@ Copyright (C) 1988-2010 Free Software Foundation, Inc. @end ignore -@set LASTCHANGE Fri Jan 15 10:50:20 EST 2010 +@set LASTCHANGE Sat May 29 21:00:00 EDT 2010 @set EDITION 4.1 @set VERSION 4.1 -@set UPDATED 15 January 2010 -@set UPDATED-MONTH January 2010 +@set UPDATED 29 May 2010 +@set UPDATED-MONTH May 2010 diff --git a/execute_cmd.c b/execute_cmd.c index 18579374..9a5c6ed4 100644 --- a/execute_cmd.c +++ b/execute_cmd.c @@ -1,6 +1,6 @@ /* execute_cmd.c -- Execute a COMMAND structure. */ -/* Copyright (C) 1987-2009 Free Software Foundation, Inc. +/* Copyright (C) 1987-2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. @@ -179,7 +179,7 @@ static void execute_subshell_builtin_or_function __P((WORD_LIST *, REDIRECT *, int, int, int, struct fd_bitmap *, int)); -static void execute_disk_command __P((WORD_LIST *, REDIRECT *, char *, +static int execute_disk_command __P((WORD_LIST *, REDIRECT *, char *, int, int, int, struct fd_bitmap *, int)); static char *getinterp __P((char *, int, int *)); @@ -271,7 +271,7 @@ static int showing_function_line; static int line_number_for_err_trap; /* A sort of function nesting level counter */ -static int funcnest = 0; +int funcnest = 0; int funcnest_max = 0; /* XXX - bash-4.2 */ struct fd_bitmap *current_fds_to_close = (struct fd_bitmap *)NULL; @@ -1219,7 +1219,6 @@ time_command (command, asynchronous, pipe_in, pipe_out, fds_to_close) posix_time = (command->flags & CMD_TIME_POSIX); -#if 0 /* XXX - bash-4.2 */ nullcmd = (command == 0) || (command->type == cm_simple && command->value.Simple->words == 0 && command->value.Simple->redirects == 0); if (posixly_correct && nullcmd) { @@ -1233,7 +1232,6 @@ time_command (command, asynchronous, pipe_in, pipe_out, fds_to_close) tbefore = shell_start_time; #endif } -#endif old_flags = command->flags; command->flags &= ~(CMD_TIME_PIPELINE|CMD_TIME_POSIX); @@ -1289,11 +1287,9 @@ time_command (command, asynchronous, pipe_in, pipe_out, fds_to_close) time_format = POSIX_TIMEFORMAT; else if ((time_format = get_string_value ("TIMEFORMAT")) == 0) { -#if 0 /* XXX - bash-4.2 */ if (posixly_correct && nullcmd) time_format = "user\t%2lU\nsys\t%2lS"; else -#endif time_format = BASH_TIMEFORMAT; } if (time_format && *time_format) @@ -3878,7 +3874,7 @@ run_builtin: simple_command->flags &= ~CMD_NO_FORK; #endif - execute_disk_command (words, simple_command->redirects, command_line, + result = execute_disk_command (words, simple_command->redirects, command_line, pipe_in, pipe_out, async, fds_to_close, simple_command->flags); @@ -4040,13 +4036,12 @@ execute_function (var, words, flags, fds_to_close, async, subshell) USE_VAR(fc); -#if 0 /* XXX - bash-4.2 */ if (funcnest_max > 0 && funcnest >= funcnest_max) { internal_error ("%s: maximum function nesting level exceeded (%d)", var->name, funcnest); + funcnest = 0; /* XXX - should we reset it somewhere else? */ jump_to_top_level (DISCARD); } -#endif #if defined (ARRAY_VARS) GET_ARRAY_FROM_VAR ("FUNCNAME", funcname_v, funcname_a); @@ -4505,7 +4500,7 @@ setup_async_signals () # define NOTFOUND_HOOK "command_not_found_handle" #endif -static void +static int execute_disk_command (words, redirects, command_line, pipe_in, pipe_out, async, fds_to_close, cmdflags) WORD_LIST *words; @@ -4516,7 +4511,7 @@ execute_disk_command (words, redirects, command_line, pipe_in, pipe_out, int cmdflags; { char *pathname, *command, **args; - int nofork; + int nofork, result; pid_t pid; SHELL_VAR *hookf; WORD_LIST *wl; @@ -4524,13 +4519,14 @@ execute_disk_command (words, redirects, command_line, pipe_in, pipe_out, nofork = (cmdflags & CMD_NO_FORK); /* Don't fork, just exec, if no pipes */ pathname = words->word->word; + result = EXECUTION_SUCCESS; #if defined (RESTRICTED_SHELL) command = (char *)NULL; if (restricted && mbschr (pathname, '/')) { internal_error (_("%s: restricted: cannot specify `/' in command names"), pathname); - last_command_exit_value = EXECUTION_FAILURE; + result = last_command_exit_value = EXECUTION_FAILURE; /* If we're not going to fork below, we must already be in a child process or a context in which it's safe to call exit(2). */ @@ -4644,6 +4640,7 @@ parent_return: unlink_fifo_list (); #endif FREE (command); + return (result); } } diff --git a/execute_cmd.c~ b/execute_cmd.c~ index a31d98bc..ba711780 100644 --- a/execute_cmd.c~ +++ b/execute_cmd.c~ @@ -179,7 +179,7 @@ static void execute_subshell_builtin_or_function __P((WORD_LIST *, REDIRECT *, int, int, int, struct fd_bitmap *, int)); -static void execute_disk_command __P((WORD_LIST *, REDIRECT *, char *, +static int execute_disk_command __P((WORD_LIST *, REDIRECT *, char *, int, int, int, struct fd_bitmap *, int)); static char *getinterp __P((char *, int, int *)); @@ -271,7 +271,7 @@ static int showing_function_line; static int line_number_for_err_trap; /* A sort of function nesting level counter */ -static int funcnest = 0; +int funcnest = 0; int funcnest_max = 0; /* XXX - bash-4.2 */ struct fd_bitmap *current_fds_to_close = (struct fd_bitmap *)NULL; @@ -1219,7 +1219,6 @@ time_command (command, asynchronous, pipe_in, pipe_out, fds_to_close) posix_time = (command->flags & CMD_TIME_POSIX); -#if 0 /* XXX - bash-4.2 */ nullcmd = (command == 0) || (command->type == cm_simple && command->value.Simple->words == 0 && command->value.Simple->redirects == 0); if (posixly_correct && nullcmd) { @@ -1233,7 +1232,6 @@ time_command (command, asynchronous, pipe_in, pipe_out, fds_to_close) tbefore = shell_start_time; #endif } -#endif old_flags = command->flags; command->flags &= ~(CMD_TIME_PIPELINE|CMD_TIME_POSIX); @@ -1289,11 +1287,9 @@ time_command (command, asynchronous, pipe_in, pipe_out, fds_to_close) time_format = POSIX_TIMEFORMAT; else if ((time_format = get_string_value ("TIMEFORMAT")) == 0) { -#if 0 /* XXX - bash-4.2 */ if (posixly_correct && nullcmd) time_format = "user\t%2lU\nsys\t%2lS"; else -#endif time_format = BASH_TIMEFORMAT; } if (time_format && *time_format) @@ -1390,15 +1386,11 @@ execute_in_subshell (command, asynchronous, pipe_in, pipe_out, fds_to_close) reset_terminating_signals (); /* in sig.c */ /* Cancel traps, in trap.c. */ -#if 0 /* XXX - bash-4.2 */ /* Reset the signal handlers in the child, but don't free the trap strings. Set a flag noting that we have to free the trap strings if we run trap to change a signal disposition. */ reset_signal_handlers (); subshell_environment |= SUBSHELL_RESETTRAP; -#else - restore_original_signals (); -#endif /* Make sure restore_original_signals doesn't undo the work done by make_child to ensure that asynchronous children are immune to SIGINT @@ -3812,16 +3804,11 @@ run_builtin: if (already_forked) { /* reset_terminating_signals (); */ /* XXX */ -#if 0 /* XXX - bash-4.2 */ /* Reset the signal handlers in the child, but don't free the trap strings. Set a flag noting that we have to free the trap strings if we run trap to change a signal disposition. */ reset_signal_handlers (); subshell_environment |= SUBSHELL_RESETTRAP; -#else - /* Cancel traps, in trap.c. */ - restore_original_signals (); -#endif if (async) { @@ -3887,7 +3874,7 @@ run_builtin: simple_command->flags &= ~CMD_NO_FORK; #endif - execute_disk_command (words, simple_command->redirects, command_line, + result = execute_disk_command (words, simple_command->redirects, command_line, pipe_in, pipe_out, async, fds_to_close, simple_command->flags); @@ -4049,13 +4036,12 @@ execute_function (var, words, flags, fds_to_close, async, subshell) USE_VAR(fc); -#if 0 /* XXX - bash-4.2 */ if (funcnest_max > 0 && funcnest >= funcnest_max) { internal_error ("%s: maximum function nesting level exceeded (%d)", var->name, funcnest); + funcnest = 0; /* XXX - should we reset it somewhere else? */ jump_to_top_level (DISCARD); } -#endif #if defined (ARRAY_VARS) GET_ARRAY_FROM_VAR ("FUNCNAME", funcname_v, funcname_a); @@ -4514,7 +4500,7 @@ setup_async_signals () # define NOTFOUND_HOOK "command_not_found_handle" #endif -static void +static int execute_disk_command (words, redirects, command_line, pipe_in, pipe_out, async, fds_to_close, cmdflags) WORD_LIST *words; @@ -4525,7 +4511,7 @@ execute_disk_command (words, redirects, command_line, pipe_in, pipe_out, int cmdflags; { char *pathname, *command, **args; - int nofork; + int nofork, result; pid_t pid; SHELL_VAR *hookf; WORD_LIST *wl; @@ -4533,13 +4519,14 @@ execute_disk_command (words, redirects, command_line, pipe_in, pipe_out, nofork = (cmdflags & CMD_NO_FORK); /* Don't fork, just exec, if no pipes */ pathname = words->word->word; + result = EXECUTION_SUCCESS; #if defined (RESTRICTED_SHELL) command = (char *)NULL; if (restricted && mbschr (pathname, '/')) { internal_error (_("%s: restricted: cannot specify `/' in command names"), pathname); - last_command_exit_value = EXECUTION_FAILURE; + result = last_command_exit_value = EXECUTION_FAILURE; /* If we're not going to fork below, we must already be in a child process or a context in which it's safe to call exit(2). */ @@ -4653,6 +4640,7 @@ parent_return: unlink_fifo_list (); #endif FREE (command); + return (result); } } @@ -1,6 +1,6 @@ /* expr.c -- arithmetic expression evaluation. */ -/* Copyright (C) 1990-2009 Free Software Foundation, Inc. +/* Copyright (C) 1990-2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. @@ -176,6 +176,10 @@ static struct lvalue lastlval = {0, 0, 0, -1}; static int _is_arithop __P((int)); static void readtok __P((void)); /* lexical analyzer */ +static void init_lvalue __P((struct lvalue *)); +static struct lvalue *alloc_lvalue __P((void)); +static void free_lvalue __P((struct lvalue *)); + static intmax_t expr_streval __P((char *, int, struct lvalue *)); static intmax_t strlong __P((char *)); static void evalerror __P((const char *)); @@ -321,7 +325,7 @@ expr_bind_array_element (tok, ind, rhs) size_t llen; char ibuf[INT_STRLEN_BOUND (arrayind_t) + 1], *istr; - istr = fmtulong (ind, 10, ibuf, sizeof (ibuf), 0); + istr = fmtumax (ind, 10, ibuf, sizeof (ibuf), 0); vname = array_variable_name (tok, (char **)NULL, (int *)NULL); llen = strlen (vname) + sizeof (ibuf) + 3; @@ -401,12 +405,14 @@ subexpr (expr) return (0); pushexp (); - curtok = lasttok = 0; expression = savestring (expr); tp = expression; + curtok = lasttok = 0; tokstr = (char *)NULL; tokval = 0; + init_lvalue (&curlval); + lastlval = curlval; readtok (); @@ -955,15 +961,22 @@ exp0 () return (val); } +static void +init_lvalue (lv) + struct lvalue *lv; +{ + lv->tokstr = 0; + lv->tokvar = 0; + lv->tokval = lv->ind = -1; +} + static struct lvalue * alloc_lvalue () { struct lvalue *lv; lv = xmalloc (sizeof (struct lvalue)); - lv->tokstr = 0; - lv->tokvar = 0; - lv->tokval = lv->ind = -1; + init_lvalue (lv); return (lv); } @@ -1,7 +1,7 @@ /* externs.h -- extern function declarations which do not appear in their own header file. */ -/* Copyright (C) 1993-2009 Free Software Foundation, Inc. +/* Copyright (C) 1993-2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. @@ -114,7 +114,7 @@ extern int get_current_prompt_level __P((void)); extern void set_current_prompt_level __P((int)); #if defined (HISTORY) -extern char *history_delimiting_chars __P((void)); +extern char *history_delimiting_chars __P((const char *)); #endif /* Declarations for functions defined in locale.c */ @@ -455,6 +455,9 @@ extern int uconvert __P((char *, long *, long *)); extern unsigned int falarm __P((unsigned int, unsigned int)); extern unsigned int fsleep __P((unsigned int, unsigned int)); +/* declarations for functions defined in lib/sh/unicode.c */ +extern int u32cconv __P((unsigned long, char *)); + /* declarations for functions defined in lib/sh/winsize.c */ extern void get_new_window_size __P((int, int *, int *)); @@ -3,7 +3,7 @@ /* This file works with both POSIX and BSD systems. It implements job control. */ -/* Copyright (C) 1989-2009 Free Software Foundation, Inc. +/* Copyright (C) 1989-2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. @@ -2351,7 +2351,6 @@ wait_for (pid) WAIT s; register PROCESS *child; sigset_t set, oset; - register PROCESS *p; /* In the case that this code is interrupted, and we longjmp () out of it, we are relying on the code in throw_to_top_level () to restore the diff --git a/lib/glob/xmbsrtowcs.c b/lib/glob/xmbsrtowcs.c index 9cf5765e..7abf727b 100644 --- a/lib/glob/xmbsrtowcs.c +++ b/lib/glob/xmbsrtowcs.c @@ -1,6 +1,6 @@ /* xmbsrtowcs.c -- replacement function for mbsrtowcs */ -/* Copyright (C) 2002-2004 Free Software Foundation, Inc. +/* Copyright (C) 2002-2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. diff --git a/lib/glob/xmbsrtowcs.c~ b/lib/glob/xmbsrtowcs.c~ index 3843561f..9cf5765e 100644 --- a/lib/glob/xmbsrtowcs.c~ +++ b/lib/glob/xmbsrtowcs.c~ @@ -219,7 +219,6 @@ xdupmbstowcs2 (destp, src) } #endif /* HAVE_MBSNRTOWCS */ - /* Convert a multibyte string to a wide character string. Memory for the new wide character string is obtained with malloc. @@ -255,6 +254,11 @@ xdupmbstowcs (destp, indicesp, src) return (size_t)-1; } +#if HAVE_MBSNRTOWCS + if (indicesp == NULL) + return (xdupmbstowcs2 (destp, src)); +#endif + memset (&state, '\0', sizeof(mbstate_t)); wsbuf_size = WSBUF_INC; diff --git a/lib/readline/bind.c b/lib/readline/bind.c index 61cc330a..00d559ca 100644 --- a/lib/readline/bind.c +++ b/lib/readline/bind.c @@ -1,6 +1,6 @@ /* bind.c -- key binding and startup file support for the readline library. */ -/* Copyright (C) 1987-2009 Free Software Foundation, Inc. +/* Copyright (C) 1987-2010 Free Software Foundation, Inc. This file is part of the GNU Readline Library (Readline), a library for reading lines of text with interactive input and history editing. @@ -1424,6 +1424,7 @@ static const struct { { "blink-matching-paren", &rl_blink_matching_paren, V_SPECIAL }, { "byte-oriented", &rl_byte_oriented, 0 }, { "completion-ignore-case", &_rl_completion_case_fold, 0 }, + { "completion-map-case", &_rl_completion_case_map, 0 }, { "convert-meta", &_rl_convert_meta_chars_to_ascii, 0 }, { "disable-completion", &rl_inhibit_completion, 0 }, { "echo-control-characters", &_rl_echo_control_chars, 0 }, diff --git a/lib/readline/bind.c~ b/lib/readline/bind.c~ index 6aa9cb67..fea60dce 100644 --- a/lib/readline/bind.c~ +++ b/lib/readline/bind.c~ @@ -1424,6 +1424,7 @@ static const struct { { "blink-matching-paren", &rl_blink_matching_paren, V_SPECIAL }, { "byte-oriented", &rl_byte_oriented, 0 }, { "completion-ignore-case", &_rl_completion_case_fold, 0 }, + { "completion-map-case", &_rl_completion_case_map, 0 }, { "convert-meta", &_rl_convert_meta_chars_to_ascii, 0 }, { "disable-completion", &rl_inhibit_completion, 0 }, { "echo-control-characters", &_rl_echo_control_chars, 0 }, @@ -2283,6 +2284,11 @@ _rl_get_string_variable_value (name) } else if (_rl_stricmp (name, "comment-begin") == 0) return (_rl_comment_begin ? _rl_comment_begin : RL_COMMENT_BEGIN_DEFAULT); + else if (_rl_stricmp (name, "completion-display-width") == 0) + { + sprintf (numbuf, "%d", _rl_completion_columns); + return (numbuf); + } else if (_rl_stricmp (name, "completion-prefix-display-length") == 0) { sprintf (numbuf, "%d", _rl_completion_prefix_display_length); diff --git a/lib/readline/complete.c b/lib/readline/complete.c index 9d6de76c..5b707848 100644 --- a/lib/readline/complete.c +++ b/lib/readline/complete.c @@ -119,6 +119,7 @@ static char **remove_duplicate_matches PARAMS((char **)); static void insert_match PARAMS((char *, int, int, char *)); static int append_to_match PARAMS((char *, int, int, int)); static void insert_all_matches PARAMS((char **, int, char *)); +static int complete_fncmp PARAMS((const char *, int, const char *, int)); static void display_matches PARAMS((char **)); static int compute_lcd_of_matches PARAMS((char **, int, const char *)); static int postprocess_matches PARAMS((char ***, int)); @@ -158,9 +159,13 @@ int _rl_print_completions_horizontally; #if defined (__MSDOS__) && !defined (__DJGPP__) int _rl_completion_case_fold = 1; #else -int _rl_completion_case_fold; +int _rl_completion_case_fold = 0; #endif +/* Non-zero means that `-' and `_' are equivalent when comparing filenames + for completion. */ +int _rl_completion_case_map = 0; + /* If zero, don't match hidden files (filenames beginning with a `.' on Unix) when doing filename completion. */ int _rl_match_hidden_files = 1; @@ -2056,6 +2061,62 @@ rl_username_completion_function (text, state) #endif /* !__WIN32__ && !__OPENNT */ } +/* Return non-zero if CONVFN matches FILENAME up to the length of FILENAME + (FILENAME_LEN). If _rl_completion_case_fold is set, compare without + regard to the alphabetic case of characters. CONVFN is the possibly- + converted directory entry; FILENAME is what the user typed. */ +static int +complete_fncmp (convfn, convlen, filename, filename_len) + const char *convfn; + int convlen; + const char *filename; + int filename_len; +{ + register char *s1, *s2; + int d, len; + + /* Otherwise, if these match up to the length of filename, then + it is a match. */ + if (_rl_completion_case_fold && _rl_completion_case_map) + { + /* Case-insensitive comparison treating _ and - as equivalent */ + if (filename_len == 0) + return 1; + if (convlen < filename_len) + return 0; + s1 = convfn; + s2 = filename; + len = filename_len; + do + { + d = _rl_to_lower (*s1) - _rl_to_lower (*s2); + /* *s1 == [-_] && *s2 == [-_] */ + if ((*s1 == '-' || *s1 == '_') && (*s2 == '-' || *s2 == '_')) + d = 0; + if (d != 0) + return 0; + s1++; s2++; /* already checked convlen >= filename_len */ + } + while (--len != 0); + return 1; + } + else if (_rl_completion_case_fold) + { + if ((_rl_to_lower (convfn[0]) == _rl_to_lower (filename[0])) && + (convlen >= filename_len) && + (_rl_strnicmp (filename, convfn, filename_len) == 0)) + return 1; + } + else + { + if ((convfn[0] == filename[0]) && + (convlen >= filename_len) && + (strncmp (filename, convfn, filename_len) == 0)) + return 1; + } + return 0; +} + /* Okay, now we write the entry_function for filename completion. In the general case. Note that completion in the shell is a little different because of all the pathnames that must be followed when looking up the @@ -2198,22 +2259,8 @@ rl_filename_completion_function (text, state) } else { - /* Otherwise, if these match up to the length of filename, then - it is a match. */ - if (_rl_completion_case_fold) - { - if ((_rl_to_lower (convfn[0]) == _rl_to_lower (filename[0])) && - (convlen >= filename_len) && - (_rl_strnicmp (filename, convfn, filename_len) == 0)) - break; - } - else - { - if ((convfn[0] == filename[0]) && - (convlen >= filename_len) && - (strncmp (filename, convfn, filename_len) == 0)) - break; - } + if (complete_fncmp (convfn, convlen, filename, filename_len)) + break; } } diff --git a/lib/readline/complete.c~ b/lib/readline/complete.c~ index d77089a2..4a1e8ca4 100644 --- a/lib/readline/complete.c~ +++ b/lib/readline/complete.c~ @@ -119,6 +119,7 @@ static char **remove_duplicate_matches PARAMS((char **)); static void insert_match PARAMS((char *, int, int, char *)); static int append_to_match PARAMS((char *, int, int, int)); static void insert_all_matches PARAMS((char **, int, char *)); +static int complete_fncmp PARAMS((const char *, int, const char *, int)); static void display_matches PARAMS((char **)); static int compute_lcd_of_matches PARAMS((char **, int, const char *)); static int postprocess_matches PARAMS((char ***, int)); @@ -158,9 +159,13 @@ int _rl_print_completions_horizontally; #if defined (__MSDOS__) && !defined (__DJGPP__) int _rl_completion_case_fold = 1; #else -int _rl_completion_case_fold; +int _rl_completion_case_fold = 0; #endif +/* Non-zero means that `-' and `_' are equivalent when comparing filenames + for completion. */ +int _rl_completion_case_map = 1; + /* If zero, don't match hidden files (filenames beginning with a `.' on Unix) when doing filename completion. */ int _rl_match_hidden_files = 1; @@ -173,7 +178,7 @@ int _rl_completion_prefix_display_length = 0; /* The readline-private number of screen columns to use when displaying matches. If < 0 or > _rl_screenwidth, it is ignored. */ -int _rl_completion_columns = 0; +int _rl_completion_columns = -1; /* Global variables available to applications using readline. */ @@ -1784,6 +1789,9 @@ rl_complete_internal (what_to_do) int start, end, delimiter, found_quote, i, nontrivial_lcd; char *text, *saved_line_buffer; char quote_char; +#if 1 + int tlen, mlen; +#endif RL_SETSTATE(RL_STATE_COMPLETING); @@ -1811,6 +1819,10 @@ rl_complete_internal (what_to_do) /* nontrivial_lcd is set if the common prefix adds something to the word being completed. */ nontrivial_lcd = matches && strcmp (text, matches[0]) != 0; +#if 1 + if (what_to_do == '!' || what_to_do == '@') + tlen = strlen (text); +#endif free (text); if (matches == 0) @@ -1844,8 +1856,25 @@ rl_complete_internal (what_to_do) case '!': case '@': /* Insert the first match with proper quoting. */ +#if 0 if (*matches[0]) insert_match (matches[0], start, matches[1] ? MULT_MATCH : SINGLE_MATCH, "e_char); +#else + if (what_to_do == TAB) + { + if (*matches[0]) + insert_match (matches[0], start, matches[1] ? MULT_MATCH : SINGLE_MATCH, "e_char); + } + else if (*matches[0] && matches[1] == 0) + /* should we perform the check only if there are multiple matches? */ + insert_match (matches[0], start, matches[1] ? MULT_MATCH : SINGLE_MATCH, "e_char); + else if (*matches[0]) /* what_to_do != TAB && multiple matches */ + { + mlen = *matches[0] ? strlen (matches[0]) : 0; + if (mlen >= tlen) + insert_match (matches[0], start, matches[1] ? MULT_MATCH : SINGLE_MATCH, "e_char); + } +#endif /* If there are more matches, ring the bell to indicate. If we are in vi mode, Posix.2 says to not ring the bell. @@ -2032,6 +2061,62 @@ rl_username_completion_function (text, state) #endif /* !__WIN32__ && !__OPENNT */ } +/* Return non-zero if CONVFN matches FILENAME up to the length of FILENAME + (FILENAME_LEN). If _rl_completion_case_fold is set, compare without + regard to the alphabetic case of characters. CONVFN is the possibly- + converted directory entry; FILENAME is what the user typed. */ +static int +complete_fncmp (convfn, convlen, filename, filename_len) + const char *convfn; + int convlen; + const char *filename; + int filename_len; +{ + register char *s1, *s2; + int d, len; + + /* Otherwise, if these match up to the length of filename, then + it is a match. */ + if (_rl_completion_case_fold && _rl_completion_case_map) + { + /* Case-insensitive comparison treating _ and - as equivalent */ + if (filename_len == 0) + return 1; + if (convlen < filename_len) + return 0; + s1 = convfn; + s2 = filename; + len = filename_len; + do + { + d = _rl_to_lower (*s1) - _rl_to_lower (*s2); + /* *s1 == [-_] && *s2 == [-_] */ + if ((*s1 == '-' || *s1 == '_') && (*s2 == '-' || *s2 == '_')) + d = 0; + if (d != 0) + return 0; + s1++; s2++; /* already checked convlen >= filename_len */ + } + while (--len != 0); + return 1; + } + else if (_rl_completion_case_fold) + { + if ((_rl_to_lower (convfn[0]) == _rl_to_lower (filename[0])) && + (convlen >= filename_len) && + (_rl_strnicmp (filename, convfn, filename_len) == 0)) + return 1; + } + else + { + if ((convfn[0] == filename[0]) && + (convlen >= filename_len) && + (strncmp (filename, convfn, filename_len) == 0)) + return 1; + } + return 0; +} + /* Okay, now we write the entry_function for filename completion. In the general case. Note that completion in the shell is a little different because of all the pathnames that must be followed when looking up the @@ -2174,22 +2259,8 @@ rl_filename_completion_function (text, state) } else { - /* Otherwise, if these match up to the length of filename, then - it is a match. */ - if (_rl_completion_case_fold) - { - if ((_rl_to_lower (convfn[0]) == _rl_to_lower (filename[0])) && - (convlen >= filename_len) && - (_rl_strnicmp (filename, convfn, filename_len) == 0)) - break; - } - else - { - if ((convfn[0] == filename[0]) && - (convlen >= filename_len) && - (strncmp (filename, convfn, filename_len) == 0)) - break; - } + if (complete_fncmp (convfn, convlen, filename, filename_len)) + break; } } diff --git a/lib/readline/doc/readline.3 b/lib/readline/doc/readline.3 index 1f3e0cb7..d729fd1f 100644 --- a/lib/readline/doc/readline.3 +++ b/lib/readline/doc/readline.3 @@ -8,7 +8,7 @@ .\" .\" Last Change: Thu Apr 22 18:59:21 EDT 2010 .\" -.TH READLINE 3 "2009 April 22" "GNU Readline 6.1" +.TH READLINE 3 "2010 April 22" "GNU Readline 6.1" .\" .\" File Name macro. This used to be `.PN', for Path Name, .\" but Sun doesn't seem to like that very much. @@ -373,6 +373,11 @@ The default value is -1. If set to \fBOn\fP, readline performs filename matching and completion in a case\-insensitive fashion. .TP +.B completion\-map\-case (Off) +If set to \fBOn\fP, and \fBcompletion\-ignore\-case\fP is enabled, readline +treats hyphens (\fI\-\fP) and underscores (\fI_\fP) as equivalent when +performing case\-insensitive filename matching and completion. +.TP .B completion\-prefix\-display\-length (0) The length in characters of the common prefix of a list of possible completions that is displayed without modification. When set to a diff --git a/lib/readline/doc/readline.3~ b/lib/readline/doc/readline.3~ index 90ac9ec5..ebc70d68 100644 --- a/lib/readline/doc/readline.3~ +++ b/lib/readline/doc/readline.3~ @@ -373,6 +373,11 @@ The default value is -1. If set to \fBOn\fP, readline performs filename matching and completion in a case\-insensitive fashion. .TP +.B completion\-map\-case (Off) +If set to \fBOn\fP, and \fBcompletion\-ignore\-case\fP is enabled, readline +treats hyphens (\fI\-\fP) and underscores (\fI_\fP) as equivalent when +performing case\-insensitive filename matching and completion. +.TP .B completion\-prefix\-display\-length (0) The length in characters of the common prefix of a list of possible completions that is displayed without modification. When set to a @@ -931,6 +936,12 @@ only attempts filename completion under certain circumstances. .TP .B possible\-completions (M\-?) List the possible completions of the text before point. +When displaying completions, readline sets the number of columns used +for display to the value of \fBcompletion-display-width\fP, the value of +the environment variable +.SM +.BR COLUMNS , +or the screen width, in that order. .TP .B insert\-completions (M\-*) Insert all completions of the text before point diff --git a/lib/readline/doc/rluser.texi b/lib/readline/doc/rluser.texi index 82cbb05f..173d1bbb 100644 --- a/lib/readline/doc/rluser.texi +++ b/lib/readline/doc/rluser.texi @@ -446,6 +446,12 @@ If set to @samp{on}, Readline performs filename matching and completion in a case-insensitive fashion. The default value is @samp{off}. +@item completion-map-case +@vindex completion-map-case +If set to @samp{on}, and @var{completion-ignore-case} is enabled, Readline +treats hyphens (@samp{-}) and underscores (@samp{_}) as equivalent when +performing case-insensitive filename matching and completion. + @item completion-prefix-display-length @vindex completion-prefix-display-length The length in characters of the common prefix of a list of possible diff --git a/lib/readline/doc/version.texi b/lib/readline/doc/version.texi index fb381d71..97eeed9f 100644 --- a/lib/readline/doc/version.texi +++ b/lib/readline/doc/version.texi @@ -4,7 +4,7 @@ Copyright (C) 1988-2010 Free Software Foundation, Inc. @set EDITION 6.1 @set VERSION 6.1 -@set UPDATED April 22 2010 -@set UPDATED-MONTH April 2010 +@set UPDATED May 292010 +@set UPDATED-MONTH May 2010 -@set LASTCHANGE Thu Apr 22 18:59:44 EDT 2010 +@set LASTCHANGE Sat May 29 17:14:10 EDT 2010 diff --git a/lib/readline/doc/version.texi~ b/lib/readline/doc/version.texi~ index 51469e3e..fb381d71 100644 --- a/lib/readline/doc/version.texi~ +++ b/lib/readline/doc/version.texi~ @@ -4,7 +4,7 @@ Copyright (C) 1988-2010 Free Software Foundation, Inc. @set EDITION 6.1 @set VERSION 6.1 -@set UPDATED April 17 2010 +@set UPDATED April 22 2010 @set UPDATED-MONTH April 2010 -@set LASTCHANGE Sat Apr 17 23:45:29 EDT 2010 +@set LASTCHANGE Thu Apr 22 18:59:44 EDT 2010 diff --git a/lib/readline/funmap.c b/lib/readline/funmap.c index cccddb62..86e375f6 100644 --- a/lib/readline/funmap.c +++ b/lib/readline/funmap.c @@ -1,6 +1,6 @@ /* funmap.c -- attach names to functions. */ -/* Copyright (C) 1987-2009 Free Software Foundation, Inc. +/* Copyright (C) 1987-2010 Free Software Foundation, Inc. This file is part of the GNU Readline Library (Readline), a library for reading lines of text with interactive input and history editing. @@ -148,6 +148,8 @@ static const FUNMAP default_funmap[] = { { "vi-append-mode", rl_vi_append_mode }, { "vi-arg-digit", rl_vi_arg_digit }, { "vi-back-to-indent", rl_vi_back_to_indent }, + { "vi-backward-bigword", rl_vi_bWord }, + { "vi-backward-word", rl_vi_bword }, { "vi-bWord", rl_vi_bWord }, { "vi-bword", rl_vi_bword }, { "vi-change-case", rl_vi_change_case }, @@ -160,12 +162,15 @@ static const FUNMAP default_funmap[] = { { "vi-delete-to", rl_vi_delete_to }, { "vi-eWord", rl_vi_eWord }, { "vi-editing-mode", rl_vi_editing_mode }, + { "vi-end-bigword", rl_vi_eWord }, { "vi-end-word", rl_vi_end_word }, { "vi-eof-maybe", rl_vi_eof_maybe }, { "vi-eword", rl_vi_eword }, { "vi-fWord", rl_vi_fWord }, { "vi-fetch-history", rl_vi_fetch_history }, { "vi-first-print", rl_vi_first_print }, + { "vi-forward-bigword", rl_vi_fWord }, + { "vi-forward-word", rl_vi_fword }, { "vi-fword", rl_vi_fword }, { "vi-goto-mark", rl_vi_goto_mark }, { "vi-insert-beg", rl_vi_insert_beg }, diff --git a/lib/readline/funmap.c~ b/lib/readline/funmap.c~ new file mode 100644 index 00000000..b79aaf04 --- /dev/null +++ b/lib/readline/funmap.c~ @@ -0,0 +1,263 @@ +/* funmap.c -- attach names to functions. */ + +/* Copyright (C) 1987-2009 Free Software Foundation, Inc. + + This file is part of the GNU Readline Library (Readline), a library + for reading lines of text with interactive input and history editing. + + Readline 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 3 of the License, or + (at your option) any later version. + + Readline 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 Readline. If not, see <http://www.gnu.org/licenses/>. +*/ + +#define READLINE_LIBRARY + +#if defined (HAVE_CONFIG_H) +# include <config.h> +#endif + +#if !defined (BUFSIZ) +#include <stdio.h> +#endif /* BUFSIZ */ + +#if defined (HAVE_STDLIB_H) +# include <stdlib.h> +#else +# include "ansi_stdlib.h" +#endif /* HAVE_STDLIB_H */ + +#include "rlconf.h" +#include "readline.h" + +#include "xmalloc.h" + +#ifdef __STDC__ +typedef int QSFUNC (const void *, const void *); +#else +typedef int QSFUNC (); +#endif + +extern int _rl_qsort_string_compare PARAMS((char **, char **)); + +FUNMAP **funmap; +static int funmap_size; +static int funmap_entry; + +/* After initializing the function map, this is the index of the first + program specific function. */ +int funmap_program_specific_entry_start; + +static const FUNMAP default_funmap[] = { + { "abort", rl_abort }, + { "accept-line", rl_newline }, + { "arrow-key-prefix", rl_arrow_keys }, + { "backward-byte", rl_backward_byte }, + { "backward-char", rl_backward_char }, + { "backward-delete-char", rl_rubout }, + { "backward-kill-line", rl_backward_kill_line }, + { "backward-kill-word", rl_backward_kill_word }, + { "backward-word", rl_backward_word }, + { "beginning-of-history", rl_beginning_of_history }, + { "beginning-of-line", rl_beg_of_line }, + { "call-last-kbd-macro", rl_call_last_kbd_macro }, + { "capitalize-word", rl_capitalize_word }, + { "character-search", rl_char_search }, + { "character-search-backward", rl_backward_char_search }, + { "clear-screen", rl_clear_screen }, + { "complete", rl_complete }, + { "copy-backward-word", rl_copy_backward_word }, + { "copy-forward-word", rl_copy_forward_word }, + { "copy-region-as-kill", rl_copy_region_to_kill }, + { "delete-char", rl_delete }, + { "delete-char-or-list", rl_delete_or_show_completions }, + { "delete-horizontal-space", rl_delete_horizontal_space }, + { "digit-argument", rl_digit_argument }, + { "do-lowercase-version", rl_do_lowercase_version }, + { "downcase-word", rl_downcase_word }, + { "dump-functions", rl_dump_functions }, + { "dump-macros", rl_dump_macros }, + { "dump-variables", rl_dump_variables }, + { "emacs-editing-mode", rl_emacs_editing_mode }, + { "end-kbd-macro", rl_end_kbd_macro }, + { "end-of-history", rl_end_of_history }, + { "end-of-line", rl_end_of_line }, + { "exchange-point-and-mark", rl_exchange_point_and_mark }, + { "forward-backward-delete-char", rl_rubout_or_delete }, + { "forward-byte", rl_forward_byte }, + { "forward-char", rl_forward_char }, + { "forward-search-history", rl_forward_search_history }, + { "forward-word", rl_forward_word }, + { "history-search-backward", rl_history_search_backward }, + { "history-search-forward", rl_history_search_forward }, + { "insert-comment", rl_insert_comment }, + { "insert-completions", rl_insert_completions }, + { "kill-whole-line", rl_kill_full_line }, + { "kill-line", rl_kill_line }, + { "kill-region", rl_kill_region }, + { "kill-word", rl_kill_word }, + { "menu-complete", rl_menu_complete }, + { "menu-complete-backward", rl_backward_menu_complete }, + { "next-history", rl_get_next_history }, + { "non-incremental-forward-search-history", rl_noninc_forward_search }, + { "non-incremental-reverse-search-history", rl_noninc_reverse_search }, + { "non-incremental-forward-search-history-again", rl_noninc_forward_search_again }, + { "non-incremental-reverse-search-history-again", rl_noninc_reverse_search_again }, + { "old-menu-complete", rl_old_menu_complete }, + { "overwrite-mode", rl_overwrite_mode }, +#ifdef __CYGWIN__ + { "paste-from-clipboard", rl_paste_from_clipboard }, +#endif + { "possible-completions", rl_possible_completions }, + { "previous-history", rl_get_previous_history }, + { "quoted-insert", rl_quoted_insert }, + { "re-read-init-file", rl_re_read_init_file }, + { "redraw-current-line", rl_refresh_line}, + { "reverse-search-history", rl_reverse_search_history }, + { "revert-line", rl_revert_line }, + { "self-insert", rl_insert }, + { "set-mark", rl_set_mark }, + { "skip-csi-sequence", rl_skip_csi_sequence }, + { "start-kbd-macro", rl_start_kbd_macro }, + { "tab-insert", rl_tab_insert }, + { "tilde-expand", rl_tilde_expand }, + { "transpose-chars", rl_transpose_chars }, + { "transpose-words", rl_transpose_words }, + { "tty-status", rl_tty_status }, + { "undo", rl_undo_command }, + { "universal-argument", rl_universal_argument }, + { "unix-filename-rubout", rl_unix_filename_rubout }, + { "unix-line-discard", rl_unix_line_discard }, + { "unix-word-rubout", rl_unix_word_rubout }, + { "upcase-word", rl_upcase_word }, + { "yank", rl_yank }, + { "yank-last-arg", rl_yank_last_arg }, + { "yank-nth-arg", rl_yank_nth_arg }, + { "yank-pop", rl_yank_pop }, + +#if defined (VI_MODE) + { "vi-append-eol", rl_vi_append_eol }, + { "vi-append-mode", rl_vi_append_mode }, + { "vi-arg-digit", rl_vi_arg_digit }, + { "vi-back-to-indent", rl_vi_back_to_indent }, + { "vi-backward-bigword", rl_vi_bWord }, + { "vi-backward-word", rl_vi_bword }, + { "vi-bWord", rl_vi_bWord }, + { "vi-bword", rl_vi_bword }, + { "vi-change-case", rl_vi_change_case }, + { "vi-change-char", rl_vi_change_char }, + { "vi-change-to", rl_vi_change_to }, + { "vi-char-search", rl_vi_char_search }, + { "vi-column", rl_vi_column }, + { "vi-complete", rl_vi_complete }, + { "vi-delete", rl_vi_delete }, + { "vi-delete-to", rl_vi_delete_to }, + { "vi-eWord", rl_vi_eWord }, + { "vi-editing-mode", rl_vi_editing_mode }, + { "vi-end-bigword", rl_vi_eWord }, + { "vi-end-word", rl_vi_end_word }, + { "vi-eof-maybe", rl_vi_eof_maybe }, + { "vi-eword", rl_vi_eword }, + { "vi-fWord", rl_vi_fWord }, + { "vi-fetch-history", rl_vi_fetch_history }, + { "vi-first-print", rl_vi_first_print }, + { "vi-forward-bigword", rl_vi_fWord }, + { "vi-forward-word", rl_vi_fword }, + { "vi-fword", rl_vi_fword }, + { "vi-goto-mark", rl_vi_goto_mark }, + { "vi-insert-beg", rl_vi_insert_beg }, + { "vi-insertion-mode", rl_vi_insertion_mode }, + { "vi-match", rl_vi_match }, + { "vi-movement-mode", rl_vi_movement_mode }, + { "vi-next-word", rl_vi_next_word }, + { "vi-overstrike", rl_vi_overstrike }, + { "vi-overstrike-delete", rl_vi_overstrike_delete }, + { "vi-prev-word", rl_vi_prev_word }, + { "vi-put", rl_vi_put }, + { "vi-redo", rl_vi_redo }, + { "vi-replace", rl_vi_replace }, + { "vi-rubout", rl_vi_rubout }, + { "vi-search", rl_vi_search }, + { "vi-search-again", rl_vi_search_again }, + { "vi-set-mark", rl_vi_set_mark }, + { "vi-subst", rl_vi_subst }, + { "vi-tilde-expand", rl_vi_tilde_expand }, + { "vi-yank-arg", rl_vi_yank_arg }, + { "vi-yank-to", rl_vi_yank_to }, +#endif /* VI_MODE */ + + {(char *)NULL, (rl_command_func_t *)NULL } +}; + +int +rl_add_funmap_entry (name, function) + const char *name; + rl_command_func_t *function; +{ + if (funmap_entry + 2 >= funmap_size) + { + funmap_size += 64; + funmap = (FUNMAP **)xrealloc (funmap, funmap_size * sizeof (FUNMAP *)); + } + + funmap[funmap_entry] = (FUNMAP *)xmalloc (sizeof (FUNMAP)); + funmap[funmap_entry]->name = name; + funmap[funmap_entry]->function = function; + + funmap[++funmap_entry] = (FUNMAP *)NULL; + return funmap_entry; +} + +static int funmap_initialized; + +/* Make the funmap contain all of the default entries. */ +void +rl_initialize_funmap () +{ + register int i; + + if (funmap_initialized) + return; + + for (i = 0; default_funmap[i].name; i++) + rl_add_funmap_entry (default_funmap[i].name, default_funmap[i].function); + + funmap_initialized = 1; + funmap_program_specific_entry_start = i; +} + +/* Produce a NULL terminated array of known function names. The array + is sorted. The array itself is allocated, but not the strings inside. + You should free () the array when you done, but not the pointrs. */ +const char ** +rl_funmap_names () +{ + const char **result; + int result_size, result_index; + + /* Make sure that the function map has been initialized. */ + rl_initialize_funmap (); + + for (result_index = result_size = 0, result = (const char **)NULL; funmap[result_index]; result_index++) + { + if (result_index + 2 > result_size) + { + result_size += 20; + result = (const char **)xrealloc (result, result_size * sizeof (char *)); + } + + result[result_index] = funmap[result_index]->name; + result[result_index + 1] = (char *)NULL; + } + + qsort (result, result_index, sizeof (char *), (QSFUNC *)_rl_qsort_string_compare); + return (result); +} diff --git a/lib/readline/histexpand.c b/lib/readline/histexpand.c index 055abcc0..fd22db3b 100644 --- a/lib/readline/histexpand.c +++ b/lib/readline/histexpand.c @@ -1,6 +1,6 @@ /* histexpand.c -- history expansion. */ -/* Copyright (C) 1989-2009 Free Software Foundation, Inc. +/* Copyright (C) 1989-2010 Free Software Foundation, Inc. This file contains the GNU History Library (History), a set of routines for managing the text of previously typed lines. diff --git a/lib/readline/histexpand.c~ b/lib/readline/histexpand.c~ index 0a6d0b46..055abcc0 100644 --- a/lib/readline/histexpand.c~ +++ b/lib/readline/histexpand.c~ @@ -1472,7 +1472,7 @@ history_tokenize_word (string, ind) } } - /* same mechanism also used for <(...)/>(...) above */ + /* same code also used for $(...)/<(...)/>(...) above */ if (member (string[i], "!@?+*")) { int peek = string[i + 1]; diff --git a/lib/readline/histfile.c b/lib/readline/histfile.c index 80e26042..f9c5bba2 100644 --- a/lib/readline/histfile.c +++ b/lib/readline/histfile.c @@ -1,6 +1,6 @@ /* histfile.c - functions to manipulate the history file. */ -/* Copyright (C) 1989-2009 Free Software Foundation, Inc. +/* Copyright (C) 1989-2010 Free Software Foundation, Inc. This file contains the GNU History Library (History), a set of routines for managing the text of previously typed lines. diff --git a/lib/readline/histfile.c~ b/lib/readline/histfile.c~ index a75fc16b..80e26042 100644 --- a/lib/readline/histfile.c~ +++ b/lib/readline/histfile.c~ @@ -126,8 +126,12 @@ history_filename (filename) if (home == 0) { +#if 0 home = "."; home_len = 1; +#else + return (NULL); +#endif } else home_len = strlen (home); @@ -179,7 +183,7 @@ read_history_range (filename, from, to) buffer = last_ts = (char *)NULL; input = history_filename (filename); - file = open (input, O_RDONLY|O_BINARY, 0666); + file = input ? open (input, O_RDONLY|O_BINARY, 0666) : -1; if ((file < 0) || (fstat (file, &finfo) == -1)) goto error_and_exit; @@ -314,7 +318,7 @@ history_truncate_file (fname, lines) buffer = (char *)NULL; filename = history_filename (fname); - file = open (filename, O_RDONLY|O_BINARY, 0666); + file = filename ? open (filename, O_RDONLY|O_BINARY, 0666) : -1; rv = 0; /* Don't try to truncate non-regular files. */ @@ -436,9 +440,10 @@ history_do_write (filename, nelements, overwrite) mode = overwrite ? O_WRONLY|O_CREAT|O_TRUNC|O_BINARY : O_WRONLY|O_APPEND|O_BINARY; #endif output = history_filename (filename); + file = output ? open (output, mode, 0600) : -1; rv = 0; - if ((file = open (output, mode, 0600)) == -1) + if (file == -1) { FREE (output); return (errno); diff --git a/lib/readline/input.c b/lib/readline/input.c index 544b0590..7c74c995 100644 --- a/lib/readline/input.c +++ b/lib/readline/input.c @@ -1,6 +1,6 @@ /* input.c -- character input functions for readline. */ -/* Copyright (C) 1994-2009 Free Software Foundation, Inc. +/* Copyright (C) 1994-2010 Free Software Foundation, Inc. This file is part of the GNU Readline Library (Readline), a library for reading lines of text with interactive input and history editing. diff --git a/lib/readline/input.c~ b/lib/readline/input.c~ index 6d3181f9..544b0590 100644 --- a/lib/readline/input.c~ +++ b/lib/readline/input.c~ @@ -133,7 +133,6 @@ rl_get_char (key) #endif pop_index = 0; -fprintf(stderr, "rl_get_char: returning 1 (key = %d)\n", *key); return (1); } @@ -428,17 +427,19 @@ rl_read_key () /* If the user has an event function, then call it periodically. */ if (rl_event_hook) { - while (rl_event_hook && rl_get_char (&c) == 0) + while (rl_event_hook) { - (*rl_event_hook) (); - RL_CHECK_SIGNALS (); - if (rl_done) /* XXX - experimental */ - return ('\n'); if (rl_gather_tyi () < 0) /* XXX - EIO */ { rl_done = 1; return ('\n'); } + RL_CHECK_SIGNALS (); + if (rl_get_char (&c) != 0) + break; + if (rl_done) /* XXX - experimental */ + return ('\n'); + (*rl_event_hook) (); } } else diff --git a/lib/readline/rlprivate.h b/lib/readline/rlprivate.h index fba43f6f..8b9b2de0 100644 --- a/lib/readline/rlprivate.h +++ b/lib/readline/rlprivate.h @@ -1,7 +1,7 @@ /* rlprivate.h -- functions and variables global to the readline library, but not intended for use by applications. */ -/* Copyright (C) 1999-2009 Free Software Foundation, Inc. +/* Copyright (C) 1999-2010 Free Software Foundation, Inc. This file is part of the GNU Readline Library (Readline), a library for reading lines of text with interactive input and history editing. @@ -120,7 +120,28 @@ typedef struct __rl_keyseq_context int childval; } _rl_keyseq_cxt; - /* fill in more as needed */ +/* vi-mode commands that use result of motion command to define boundaries */ +#define VIM_DELETE 0x01 +#define VIM_CHANGE 0x02 +#define VIM_YANK 0x04 + +/* various states for vi-mode commands that use motion commands. reflects + RL_READLINE_STATE */ +#define VMSTATE_READ 0x01 +#define VMSTATE_NUMARG 0x02 + +typedef struct __rl_vimotion_context +{ + int op; + int state; + int flags; /* reserved */ + _rl_arg_cxt ncxt; + int numeric_arg; + int start, end; /* rl_point, rl_end */ + int key, motion; /* initial key, motion command */ +} _rl_vimotion_cxt; + +/* fill in more as needed */ /* `Generic' callback data and functions */ typedef struct __rl_callback_generic_arg { @@ -388,6 +409,7 @@ extern int _rl_completion_prefix_display_length; extern int _rl_completion_columns; extern int _rl_print_completions_horizontally; extern int _rl_completion_case_fold; +extern int _rl_completion_case_map; extern int _rl_match_hidden_files; extern int _rl_page_completions; extern int _rl_skip_completed_text; diff --git a/lib/readline/rlprivate.h~ b/lib/readline/rlprivate.h~ index 819f1278..d9044aea 100644 --- a/lib/readline/rlprivate.h~ +++ b/lib/readline/rlprivate.h~ @@ -1,7 +1,7 @@ /* rlprivate.h -- functions and variables global to the readline library, but not intended for use by applications. */ -/* Copyright (C) 1999-2009 Free Software Foundation, Inc. +/* Copyright (C) 1999-2010 Free Software Foundation, Inc. This file is part of the GNU Readline Library (Readline), a library for reading lines of text with interactive input and history editing. @@ -120,7 +120,23 @@ typedef struct __rl_keyseq_context int childval; } _rl_keyseq_cxt; - /* fill in more as needed */ +/* vi-mode commands that use result of motion command to define boundaries */ +#define VIM_DELETE 0x01 +#define VIM_CHANGE 0x02 +#define VIM_YANK 0x04 + +typedef struct __rl_vimotion_context +{ + int op; + int state; + int flags; /* reserved */ + _rl_arg_cxt ncxt; + int numeric_arg; + int start, end; /* rl_point, rl_end */ + int key, motion; /* initial key, motion command */ +} _rl_vimotion_cxt; + +/* fill in more as needed */ /* `Generic' callback data and functions */ typedef struct __rl_callback_generic_arg { @@ -385,8 +401,10 @@ extern int _rl_complete_show_unmodified; extern int _rl_complete_mark_directories; extern int _rl_complete_mark_symlink_dirs; extern int _rl_completion_prefix_display_length; +extern int _rl_completion_columns; extern int _rl_print_completions_horizontally; extern int _rl_completion_case_fold; +extern int _rl_completion_case_map; extern int _rl_match_hidden_files; extern int _rl_page_completions; extern int _rl_skip_completed_text; diff --git a/lib/readline/text.c b/lib/readline/text.c index a1f8e5c4..6f4e2316 100644 --- a/lib/readline/text.c +++ b/lib/readline/text.c @@ -1,6 +1,6 @@ /* text.c -- text handling commands for readline. */ -/* Copyright (C) 1987-2009 Free Software Foundation, Inc. +/* Copyright (C) 1987-2010 Free Software Foundation, Inc. This file is part of the GNU Readline Library (Readline), a library for reading lines of text with interactive input and history editing. diff --git a/lib/readline/text.c~ b/lib/readline/text.c~ index fc391894..a1f8e5c4 100644 --- a/lib/readline/text.c~ +++ b/lib/readline/text.c~ @@ -1495,6 +1495,9 @@ _rl_char_search_internal (count, dir, schar) int prepos; #endif + if (dir == 0) + return -1; + pos = rl_point; inc = (dir < 0) ? -1 : 1; while (count) diff --git a/lib/readline/util.c b/lib/readline/util.c index 6bb64c29..6c68ad8d 100644 --- a/lib/readline/util.c +++ b/lib/readline/util.c @@ -1,6 +1,6 @@ /* util.c -- readline utility functions */ -/* Copyright (C) 1987-2009 Free Software Foundation, Inc. +/* Copyright (C) 1987-2010 Free Software Foundation, Inc. This file is part of the GNU Readline Library (Readline), a library for reading lines of text with interactive input and history editing. @@ -366,41 +366,56 @@ _rl_strpbrk (string1, string2) #if !defined (HAVE_STRCASECMP) /* Compare at most COUNT characters from string1 to string2. Case - doesn't matter. */ + doesn't matter (strncasecmp). */ int _rl_strnicmp (string1, string2, count) char *string1, *string2; int count; { - register char ch1, ch2; + register char *s1, *s2; + int d; - while (count) + if (count <= 0 || (string1 == string2)) + return 0; + + s1 = string1; + s2 = string2; + do { - ch1 = *string1++; - ch2 = *string2++; - if (_rl_to_upper(ch1) == _rl_to_upper(ch2)) - count--; - else + d = _rl_to_lower (*s1) - _rl_to_lower (*s2); /* XXX - cast to unsigned char? */ + if (d != 0) + return d; + if (*s1++ == '\0') break; + s2++; } - return (count); + while (--count != 0) + + return (0); } -/* strcmp (), but caseless. */ +/* strcmp (), but caseless (strcasecmp). */ int _rl_stricmp (string1, string2) char *string1, *string2; { - register char ch1, ch2; + register char *s1, *s2; + int d; - while (*string1 && *string2) + s1 = string1; + s2 = string2; + + if (s1 == s2) + return 0; + + while ((d = _rl_to_lower (*s1) - _rl_to_lower (*s2)) == 0) { - ch1 = *string1++; - ch2 = *string2++; - if (_rl_to_upper(ch1) != _rl_to_upper(ch2)) - return (1); + if (*s1++ == '\0') + return 0; + s2++; } - return (*string1 - *string2); + + return (d); } #endif /* !HAVE_STRCASECMP */ diff --git a/lib/readline/util.c~ b/lib/readline/util.c~ new file mode 100644 index 00000000..83008895 --- /dev/null +++ b/lib/readline/util.c~ @@ -0,0 +1,526 @@ +/* util.c -- readline utility functions */ + +/* Copyright (C) 1987-2009 Free Software Foundation, Inc. + + This file is part of the GNU Readline Library (Readline), a library + for reading lines of text with interactive input and history editing. + + Readline 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 3 of the License, or + (at your option) any later version. + + Readline 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 Readline. If not, see <http://www.gnu.org/licenses/>. +*/ + +#define READLINE_LIBRARY + +#if defined (HAVE_CONFIG_H) +# include <config.h> +#endif + +#include <sys/types.h> +#include <fcntl.h> +#include "posixjmp.h" + +#if defined (HAVE_UNISTD_H) +# include <unistd.h> /* for _POSIX_VERSION */ +#endif /* HAVE_UNISTD_H */ + +#if defined (HAVE_STDLIB_H) +# include <stdlib.h> +#else +# include "ansi_stdlib.h" +#endif /* HAVE_STDLIB_H */ + +#include <stdio.h> +#include <ctype.h> + +/* System-specific feature definitions and include files. */ +#include "rldefs.h" +#include "rlmbutil.h" + +#if defined (TIOCSTAT_IN_SYS_IOCTL) +# include <sys/ioctl.h> +#endif /* TIOCSTAT_IN_SYS_IOCTL */ + +/* Some standard library routines. */ +#include "readline.h" + +#include "rlprivate.h" +#include "xmalloc.h" + +/* **************************************************************** */ +/* */ +/* Utility Functions */ +/* */ +/* **************************************************************** */ + +/* Return 0 if C is not a member of the class of characters that belong + in words, or 1 if it is. */ + +int _rl_allow_pathname_alphabetic_chars = 0; +static const char * const pathname_alphabetic_chars = "/-_=~.#$"; + +int +rl_alphabetic (c) + int c; +{ + if (ALPHABETIC (c)) + return (1); + + return (_rl_allow_pathname_alphabetic_chars && + strchr (pathname_alphabetic_chars, c) != NULL); +} + +#if defined (HANDLE_MULTIBYTE) +int +_rl_walphabetic (wchar_t wc) +{ + int c; + + if (iswalnum (wc)) + return (1); + + c = wc & 0177; + return (_rl_allow_pathname_alphabetic_chars && + strchr (pathname_alphabetic_chars, c) != NULL); +} +#endif + +/* How to abort things. */ +int +_rl_abort_internal () +{ + rl_ding (); + rl_clear_message (); + _rl_reset_argument (); + rl_clear_pending_input (); + + RL_UNSETSTATE (RL_STATE_MACRODEF); + while (rl_executing_macro) + _rl_pop_executing_macro (); + + rl_last_func = (rl_command_func_t *)NULL; + longjmp (_rl_top_level, 1); + return (0); +} + +int +rl_abort (count, key) + int count, key; +{ + return (_rl_abort_internal ()); +} + +int +_rl_null_function (count, key) + int count, key; +{ + return 0; +} + +int +rl_tty_status (count, key) + int count, key; +{ +#if defined (TIOCSTAT) + ioctl (1, TIOCSTAT, (char *)0); + rl_refresh_line (count, key); +#else + rl_ding (); +#endif + return 0; +} + +/* Return a copy of the string between FROM and TO. + FROM is inclusive, TO is not. */ +char * +rl_copy_text (from, to) + int from, to; +{ + register int length; + char *copy; + + /* Fix it if the caller is confused. */ + if (from > to) + SWAP (from, to); + + length = to - from; + copy = (char *)xmalloc (1 + length); + strncpy (copy, rl_line_buffer + from, length); + copy[length] = '\0'; + return (copy); +} + +/* Increase the size of RL_LINE_BUFFER until it has enough space to hold + LEN characters. */ +void +rl_extend_line_buffer (len) + int len; +{ + while (len >= rl_line_buffer_len) + { + rl_line_buffer_len += DEFAULT_BUFFER_SIZE; + rl_line_buffer = (char *)xrealloc (rl_line_buffer, rl_line_buffer_len); + } + + _rl_set_the_line (); +} + + +/* A function for simple tilde expansion. */ +int +rl_tilde_expand (ignore, key) + int ignore, key; +{ + register int start, end; + char *homedir, *temp; + int len; + + end = rl_point; + start = end - 1; + + if (rl_point == rl_end && rl_line_buffer[rl_point] == '~') + { + homedir = tilde_expand ("~"); + _rl_replace_text (homedir, start, end); + xfree (homedir); + return (0); + } + else if (rl_line_buffer[start] != '~') + { + for (; !whitespace (rl_line_buffer[start]) && start >= 0; start--) + ; + start++; + } + + end = start; + do + end++; + while (whitespace (rl_line_buffer[end]) == 0 && end < rl_end); + + if (whitespace (rl_line_buffer[end]) || end >= rl_end) + end--; + + /* If the first character of the current word is a tilde, perform + tilde expansion and insert the result. If not a tilde, do + nothing. */ + if (rl_line_buffer[start] == '~') + { + len = end - start + 1; + temp = (char *)xmalloc (len + 1); + strncpy (temp, rl_line_buffer + start, len); + temp[len] = '\0'; + homedir = tilde_expand (temp); + xfree (temp); + + _rl_replace_text (homedir, start, end); + xfree (homedir); + } + + return (0); +} + +#if defined (USE_VARARGS) +void +#if defined (PREFER_STDARG) +_rl_ttymsg (const char *format, ...) +#else +_rl_ttymsg (va_alist) + va_dcl +#endif +{ + va_list args; +#if defined (PREFER_VARARGS) + char *format; +#endif + +#if defined (PREFER_STDARG) + va_start (args, format); +#else + va_start (args); + format = va_arg (args, char *); +#endif + + fprintf (stderr, "readline: "); + vfprintf (stderr, format, args); + fprintf (stderr, "\n"); + fflush (stderr); + + va_end (args); + + rl_forced_update_display (); +} + +void +#if defined (PREFER_STDARG) +_rl_errmsg (const char *format, ...) +#else +_rl_errmsg (va_alist) + va_dcl +#endif +{ + va_list args; +#if defined (PREFER_VARARGS) + char *format; +#endif + +#if defined (PREFER_STDARG) + va_start (args, format); +#else + va_start (args); + format = va_arg (args, char *); +#endif + + fprintf (stderr, "readline: "); + vfprintf (stderr, format, args); + fprintf (stderr, "\n"); + fflush (stderr); + + va_end (args); +} + +#else /* !USE_VARARGS */ +void +_rl_ttymsg (format, arg1, arg2) + char *format; +{ + fprintf (stderr, "readline: "); + fprintf (stderr, format, arg1, arg2); + fprintf (stderr, "\n"); + + rl_forced_update_display (); +} + +void +_rl_errmsg (format, arg1, arg2) + char *format; +{ + fprintf (stderr, "readline: "); + fprintf (stderr, format, arg1, arg2); + fprintf (stderr, "\n"); +} +#endif /* !USE_VARARGS */ + +/* **************************************************************** */ +/* */ +/* String Utility Functions */ +/* */ +/* **************************************************************** */ + +/* Determine if s2 occurs in s1. If so, return a pointer to the + match in s1. The compare is case insensitive. */ +char * +_rl_strindex (s1, s2) + register const char *s1, *s2; +{ + register int i, l, len; + + for (i = 0, l = strlen (s2), len = strlen (s1); (len - i) >= l; i++) + if (_rl_strnicmp (s1 + i, s2, l) == 0) + return ((char *) (s1 + i)); + return ((char *)NULL); +} + +#ifndef HAVE_STRPBRK +/* Find the first occurrence in STRING1 of any character from STRING2. + Return a pointer to the character in STRING1. */ +char * +_rl_strpbrk (string1, string2) + const char *string1, *string2; +{ + register const char *scan; +#if defined (HANDLE_MULTIBYTE) + mbstate_t ps; + register int i, v; + + memset (&ps, 0, sizeof (mbstate_t)); +#endif + + for (; *string1; string1++) + { + for (scan = string2; *scan; scan++) + { + if (*string1 == *scan) + return ((char *)string1); + } +#if defined (HANDLE_MULTIBYTE) + if (MB_CUR_MAX > 1 && rl_byte_oriented == 0) + { + v = _rl_get_char_len (string1, &ps); + if (v > 1) + string1 += v - 1; /* -1 to account for auto-increment in loop */ + } +#endif + } + return ((char *)NULL); +} +#endif + +#if !defined (HAVE_STRCASECMP) +/* Compare at most COUNT characters from string1 to string2. Case + doesn't matter (strncasecmp). */ +int +_rl_strnicmp (string1, string2, count) + char *string1, *string2; + int count; +{ + register char *s1, *s2; + int d; + + if (count <= 0 || (string1 == string2)) + return 0; + + s1 = string1; + s2 = string2; + do + { + d = _rl_to_lower (*s1) - _rl_to_lower (*s2); /* XXX - cast to unsigned char? */ + if (d != 0) + return d; + if (*s1++ == '\0') + break; + s2++; + } + while (--count != 0) + + return (0); +} + +/* strcmp (), but caseless (strcasecmp). */ +int +_rl_stricmp (string1, string2) + char *string1, *string2; +{ + register char *s1, *s2; + int d; + + s1 = string1; + s2 = string2; + + if (s1 == s2) + return 0; + + while ((d = _rl_to_lower (*s1) - _rl_to_lower (*s2)) == 0) + { + if (*s1++ == '\0') + return 0; + s2++; + } + + return (d); +} +#endif /* !HAVE_STRCASECMP */ + +/* Stupid comparison routine for qsort () ing strings. */ +int +_rl_qsort_string_compare (s1, s2) + char **s1, **s2; +{ +#if defined (HAVE_STRCOLL) + return (strcoll (*s1, *s2)); +#else + int result; + + result = **s1 - **s2; + if (result == 0) + result = strcmp (*s1, *s2); + + return result; +#endif +} + +/* Function equivalents for the macros defined in chardefs.h. */ +#define FUNCTION_FOR_MACRO(f) int (f) (c) int c; { return f (c); } + +FUNCTION_FOR_MACRO (_rl_digit_p) +FUNCTION_FOR_MACRO (_rl_digit_value) +FUNCTION_FOR_MACRO (_rl_lowercase_p) +FUNCTION_FOR_MACRO (_rl_pure_alphabetic) +FUNCTION_FOR_MACRO (_rl_to_lower) +FUNCTION_FOR_MACRO (_rl_to_upper) +FUNCTION_FOR_MACRO (_rl_uppercase_p) + +/* A convenience function, to force memory deallocation to be performed + by readline. DLLs on Windows apparently require this. */ +void +rl_free (mem) + void *mem; +{ + if (mem) + free (mem); +} + +/* Backwards compatibility, now that savestring has been removed from + all `public' readline header files. */ +#undef _rl_savestring +char * +_rl_savestring (s) + const char *s; +{ + return (strcpy ((char *)xmalloc (1 + (int)strlen (s)), (s))); +} + +#if defined (USE_VARARGS) +static FILE *_rl_tracefp; + +void +#if defined (PREFER_STDARG) +_rl_trace (const char *format, ...) +#else +_rl_trace (va_alist) + va_dcl +#endif +{ + va_list args; +#if defined (PREFER_VARARGS) + char *format; +#endif + +#if defined (PREFER_STDARG) + va_start (args, format); +#else + va_start (args); + format = va_arg (args, char *); +#endif + + if (_rl_tracefp == 0) + _rl_tropen (); + vfprintf (_rl_tracefp, format, args); + fprintf (_rl_tracefp, "\n"); + fflush (_rl_tracefp); + + va_end (args); +} + +int +_rl_tropen () +{ + char fnbuf[128]; + + if (_rl_tracefp) + fclose (_rl_tracefp); + sprintf (fnbuf, "/var/tmp/rltrace.%ld", getpid()); + unlink(fnbuf); + _rl_tracefp = fopen (fnbuf, "w+"); + return _rl_tracefp != 0; +} + +int +_rl_trclose () +{ + int r; + + r = fclose (_rl_tracefp); + _rl_tracefp = 0; + return r; +} + +#endif diff --git a/lib/readline/vi_mode.c b/lib/readline/vi_mode.c index fbc04090..faa29ace 100644 --- a/lib/readline/vi_mode.c +++ b/lib/readline/vi_mode.c @@ -1,7 +1,7 @@ /* vi_mode.c -- A vi emulation mode for Bash. Derived from code written by Jeff Sparkes (jsparkes@bnr.ca). */ -/* Copyright (C) 1987-2009 Free Software Foundation, Inc. +/* Copyright (C) 1987-2010 Free Software Foundation, Inc. This file is part of the GNU Readline Library (Readline), a library for reading lines of text with interactive input and history editing. @@ -65,6 +65,8 @@ int _rl_vi_last_command = 'i'; /* default `.' puts you in insert mode */ +_rl_vimotion_cxt *_rl_vimvcxt = 0; + /* Non-zero means enter insertion mode. */ static int _rl_vi_doing_insert; @@ -128,6 +130,12 @@ static int _rl_vi_callback_change_char PARAMS((_rl_callback_generic_arg *)); static int _rl_vi_callback_char_search PARAMS((_rl_callback_generic_arg *)); #endif +static int vi_change_dispatch PARAMS((_rl_vimotion_cxt *)); +static int vi_delete_dispatch PARAMS((_rl_vimotion_cxt *)); +static int vi_yank_dispatch PARAMS((_rl_vimotion_cxt *)); + +static int vidomove_dispatch PARAMS((_rl_vimotion_cxt *)); + void _rl_vi_initialize_line () { @@ -1076,28 +1084,60 @@ rl_digit_loop1 () return (0); } -int -rl_vi_delete_to (count, key) - int count, key; +static void +_rl_mvcxt_init (m, op, key) + _rl_vimotion_cxt *m; + int op, key; { - int c, start_pos; + m->op = op; + m->state = m->flags = 0; + m->ncxt = 0; + m->numeric_arg = -1; + m->start = rl_point; + m->end = rl_end; + m->key = key; + m->motion = -1; +} - if (_rl_uppercase_p (key)) - rl_stuff_char ('$'); - else if (vi_redoing) - rl_stuff_char (_rl_vi_last_motion); +static _rl_vimotion_cxt * +_rl_mvcxt_alloc (op, key) + int op, key; +{ + _rl_vimotion_cxt *m; - start_pos = rl_point; + m = xmalloc (sizeof (_rl_vimotion_cxt)); + _rl_mvcxt_init (m, op, key); + return m; +} - if (rl_vi_domove (key, &c)) - { - rl_ding (); - return -1; - } +static void +_rl_mvcxt_dispose (m) + _rl_vimotion_cxt *m; +{ + free (m); +} + +/* + * 1. Create context, set initial values + * 2. Pass to rl_vi_domove to populate + * 3. Use returned context to finish rest of command + * + * Context: + * op + * state + * flags + * numeric arg + * start, end positions + * initial key (key), motion key (c) + */ +static int +vi_delete_dispatch (m) + _rl_vimotion_cxt *m; +{ /* These are the motion commands that do not require adjusting the mark. */ - if (((strchr (" l|h^0bBFT`", c) == 0) && (rl_point >= start_pos)) && + if (((strchr (" l|h^0bBFT`", m->motion) == 0) && (rl_point >= m->start)) && (rl_mark < rl_end)) rl_mark++; @@ -1106,34 +1146,47 @@ rl_vi_delete_to (count, key) } int -rl_vi_change_to (count, key) +rl_vi_delete_to (count, key) int count, key; { - int c, start_pos; + int c, r; if (_rl_uppercase_p (key)) rl_stuff_char ('$'); else if (vi_redoing) rl_stuff_char (_rl_vi_last_motion); - start_pos = rl_point; + _rl_vimvcxt = _rl_mvcxt_alloc (VIM_DELETE, key); + _rl_vimvcxt->start = rl_point; + /* XXX -- TODO -- pass and use context in rl_vi_domove */ if (rl_vi_domove (key, &c)) { rl_ding (); return -1; } + _rl_vimvcxt->motion = c; + + r = vidomove_dispatch (_rl_vimvcxt); + _rl_mvcxt_dispose (_rl_vimvcxt); + _rl_vimvcxt = 0; + return r; +} +static int +vi_change_dispatch (m) + _rl_vimotion_cxt *m; +{ /* These are the motion commands that do not require adjusting the mark. c[wW] are handled by special-case code in rl_vi_domove(), and already leave the mark at the correct location. */ - if (((strchr (" l|hwW^0bBFT`", c) == 0) && (rl_point >= start_pos)) && + if (((strchr (" l|hwW^0bBFT`", m->motion) == 0) && (rl_point >= m->start)) && (rl_mark < rl_end)) rl_mark++; /* The cursor never moves with c[wW]. */ - if ((_rl_to_upper (c) == 'W') && rl_point < start_pos) - rl_point = start_pos; + if ((_rl_to_upper (m->motion) == 'W') && rl_point < m->start) + rl_point = m->start; if (vi_redoing) { @@ -1151,34 +1204,50 @@ rl_vi_change_to (count, key) rl_begin_undo_group (); /* to make the `u' command work */ rl_kill_text (rl_point, rl_mark); /* `C' does not save the text inserted for undoing or redoing. */ - if (_rl_uppercase_p (key) == 0) + if (_rl_uppercase_p (m->key) == 0) _rl_vi_doing_insert = 1; - rl_vi_start_inserting (key, rl_numeric_arg, rl_arg_sign); + /* XXX -- TODO -- use m->numericarg? */ + rl_vi_start_inserting (m->key, rl_numeric_arg, rl_arg_sign); } return (0); } int -rl_vi_yank_to (count, key) +rl_vi_change_to (count, key) int count, key; { - int c, start_pos; + int c, r; if (_rl_uppercase_p (key)) rl_stuff_char ('$'); + else if (vi_redoing) + rl_stuff_char (_rl_vi_last_motion); - start_pos = rl_point; + _rl_vimvcxt = _rl_mvcxt_alloc (VIM_CHANGE, key); + _rl_vimvcxt->start = rl_point; + /* XXX -- TODO -- pass and use context in rl_vi_domove */ if (rl_vi_domove (key, &c)) { rl_ding (); return -1; } + _rl_vimvcxt->motion = c; + + r = vidomove_dispatch (_rl_vimvcxt); + _rl_mvcxt_dispose (_rl_vimvcxt); + _rl_vimvcxt = 0; + return r; +} +static int +vi_yank_dispatch (m) + _rl_vimotion_cxt *m; +{ /* These are the motion commands that do not require adjusting the mark. */ - if (((strchr (" l|h^0%bBFT`", c) == 0) && (rl_point >= start_pos)) && + if (((strchr (" l|h^0%bBFT`", m->motion) == 0) && (rl_point >= m->start)) && (rl_mark < rl_end)) rl_mark++; @@ -1186,12 +1255,64 @@ rl_vi_yank_to (count, key) rl_kill_text (rl_point, rl_mark); rl_end_undo_group (); rl_do_undo (); - rl_point = start_pos; + rl_point = m->start; return (0); } int +rl_vi_yank_to (count, key) + int count, key; +{ + int c, r; + + if (_rl_uppercase_p (key)) + rl_stuff_char ('$'); + + _rl_vimvcxt = _rl_mvcxt_alloc (VIM_YANK, key); + _rl_vimvcxt->start = rl_point; + + /* XXX -- TODO -- pass and use context in rl_vi_domove */ + if (rl_vi_domove (key, &c)) + { + rl_ding (); + return -1; + } + _rl_vimvcxt->motion = c; + + r = vidomove_dispatch (_rl_vimvcxt); + _rl_mvcxt_dispose (_rl_vimvcxt); + _rl_vimvcxt = 0; + return r; +} + +static int +vidomove_dispatch (m) + _rl_vimotion_cxt *m; +{ + int r; + + switch (m->op) + { + case VIM_DELETE: + r = vi_delete_dispatch (m); + break; + case VIM_CHANGE: + r = vi_change_dispatch (m); + break; + case VIM_YANK: + r = vi_yank_dispatch (m); + break; + default: + fprintf (stderr, "vidomove_dispatch: unknown operator %d", m->op); + r = 1; + break; + } + + return r; +} + +int rl_vi_rubout (count, key) int count, key; { diff --git a/lib/readline/vi_mode.c.save b/lib/readline/vi_mode.c.save new file mode 100644 index 00000000..a5f83cbd --- /dev/null +++ b/lib/readline/vi_mode.c.save @@ -0,0 +1,1849 @@ +/* vi_mode.c -- A vi emulation mode for Bash. + Derived from code written by Jeff Sparkes (jsparkes@bnr.ca). */ + +/* Copyright (C) 1987-2010 Free Software Foundation, Inc. + + This file is part of the GNU Readline Library (Readline), a library + for reading lines of text with interactive input and history editing. + + Readline 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 3 of the License, or + (at your option) any later version. + + Readline 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 Readline. If not, see <http://www.gnu.org/licenses/>. +*/ + +#define READLINE_LIBRARY + +/* **************************************************************** */ +/* */ +/* VI Emulation Mode */ +/* */ +/* **************************************************************** */ +#include "rlconf.h" + +#if defined (VI_MODE) + +#if defined (HAVE_CONFIG_H) +# include <config.h> +#endif + +#include <sys/types.h> + +#if defined (HAVE_STDLIB_H) +# include <stdlib.h> +#else +# include "ansi_stdlib.h" +#endif /* HAVE_STDLIB_H */ + +#if defined (HAVE_UNISTD_H) +# include <unistd.h> +#endif + +#include <stdio.h> + +/* Some standard library routines. */ +#include "rldefs.h" +#include "rlmbutil.h" + +#include "readline.h" +#include "history.h" + +#include "rlprivate.h" +#include "xmalloc.h" + +#ifndef member +#define member(c, s) ((c) ? (char *)strchr ((s), (c)) != (char *)NULL : 0) +#endif + +int _rl_vi_last_command = 'i'; /* default `.' puts you in insert mode */ + +/* Non-zero means enter insertion mode. */ +static int _rl_vi_doing_insert; + +/* Command keys which do movement for xxx_to commands. */ +static const char * const vi_motion = " hl^$0ftFT;,%wbeWBE|`"; + +/* Keymap used for vi replace characters. Created dynamically since + rarely used. */ +static Keymap vi_replace_map; + +/* The number of characters inserted in the last replace operation. */ +static int vi_replace_count; + +/* If non-zero, we have text inserted after a c[motion] command that put + us implicitly into insert mode. Some people want this text to be + attached to the command so that it is `redoable' with `.'. */ +static int vi_continued_command; +static char *vi_insert_buffer; +static int vi_insert_buffer_size; + +static int _rl_vi_last_repeat = 1; +static int _rl_vi_last_arg_sign = 1; +static int _rl_vi_last_motion; +#if defined (HANDLE_MULTIBYTE) +static char _rl_vi_last_search_mbchar[MB_LEN_MAX]; +static int _rl_vi_last_search_mblen; +#else +static int _rl_vi_last_search_char; +#endif +static int _rl_vi_last_replacement; + +static int _rl_vi_last_key_before_insert; + +static int vi_redoing; + +/* Text modification commands. These are the `redoable' commands. */ +static const char * const vi_textmod = "_*\\AaIiCcDdPpYyRrSsXx~"; + +/* Arrays for the saved marks. */ +static int vi_mark_chars['z' - 'a' + 1]; + +static void _rl_vi_stuff_insert PARAMS((int)); +static void _rl_vi_save_insert PARAMS((UNDO_LIST *)); + +static void _rl_vi_backup PARAMS((void)); + +static int _rl_vi_arg_dispatch PARAMS((int)); +static int rl_digit_loop1 PARAMS((void)); + +static int _rl_vi_set_mark PARAMS((void)); +static int _rl_vi_goto_mark PARAMS((void)); + +static void _rl_vi_append_forward PARAMS((int)); + +static int _rl_vi_callback_getchar PARAMS((char *, int)); + +#if defined (READLINE_CALLBACKS) +static int _rl_vi_callback_set_mark PARAMS((_rl_callback_generic_arg *)); +static int _rl_vi_callback_goto_mark PARAMS((_rl_callback_generic_arg *)); +static int _rl_vi_callback_change_char PARAMS((_rl_callback_generic_arg *)); +static int _rl_vi_callback_char_search PARAMS((_rl_callback_generic_arg *)); +#endif + +void +_rl_vi_initialize_line () +{ + register int i, n; + + n = sizeof (vi_mark_chars) / sizeof (vi_mark_chars[0]); + for (i = 0; i < n; i++) + vi_mark_chars[i] = -1; + + RL_UNSETSTATE(RL_STATE_VICMDONCE); +} + +void +_rl_vi_reset_last () +{ + _rl_vi_last_command = 'i'; + _rl_vi_last_repeat = 1; + _rl_vi_last_arg_sign = 1; + _rl_vi_last_motion = 0; +} + +void +_rl_vi_set_last (key, repeat, sign) + int key, repeat, sign; +{ + _rl_vi_last_command = key; + _rl_vi_last_repeat = repeat; + _rl_vi_last_arg_sign = sign; +} + +/* A convenience function that calls _rl_vi_set_last to save the last command + information and enters insertion mode. */ +void +rl_vi_start_inserting (key, repeat, sign) + int key, repeat, sign; +{ + _rl_vi_set_last (key, repeat, sign); + rl_vi_insertion_mode (1, key); +} + +/* Is the command C a VI mode text modification command? */ +int +_rl_vi_textmod_command (c) + int c; +{ + return (member (c, vi_textmod)); +} + +static void +_rl_vi_stuff_insert (count) + int count; +{ + rl_begin_undo_group (); + while (count--) + rl_insert_text (vi_insert_buffer); + rl_end_undo_group (); +} + +/* Bound to `.'. Called from command mode, so we know that we have to + redo a text modification command. The default for _rl_vi_last_command + puts you back into insert mode. */ +int +rl_vi_redo (count, c) + int count, c; +{ + int r; + + if (!rl_explicit_arg) + { + rl_numeric_arg = _rl_vi_last_repeat; + rl_arg_sign = _rl_vi_last_arg_sign; + } + + r = 0; + vi_redoing = 1; + /* If we're redoing an insert with `i', stuff in the inserted text + and do not go into insertion mode. */ + if (_rl_vi_last_command == 'i' && vi_insert_buffer && *vi_insert_buffer) + { + _rl_vi_stuff_insert (count); + /* And back up point over the last character inserted. */ + if (rl_point > 0) + _rl_vi_backup (); + } + /* Ditto for redoing an insert with `I', but move to the beginning of the + line like the `I' command does. */ + else if (_rl_vi_last_command == 'I' && vi_insert_buffer && *vi_insert_buffer) + { + rl_beg_of_line (1, 'I'); + _rl_vi_stuff_insert (count); + if (rl_point > 0) + _rl_vi_backup (); + } + /* Ditto for redoing an insert with `a', but move forward a character first + like the `a' command does. */ + else if (_rl_vi_last_command == 'a' && vi_insert_buffer && *vi_insert_buffer) + { + _rl_vi_append_forward ('a'); + _rl_vi_stuff_insert (count); + if (rl_point > 0) + _rl_vi_backup (); + } + /* Ditto for redoing an insert with `A', but move to the end of the line + like the `A' command does. */ + else if (_rl_vi_last_command == 'A' && vi_insert_buffer && *vi_insert_buffer) + { + rl_end_of_line (1, 'A'); + _rl_vi_stuff_insert (count); + if (rl_point > 0) + _rl_vi_backup (); + } + else + r = _rl_dispatch (_rl_vi_last_command, _rl_keymap); + vi_redoing = 0; + + return (r); +} + +/* A placeholder for further expansion. */ +int +rl_vi_undo (count, key) + int count, key; +{ + return (rl_undo_command (count, key)); +} + +/* Yank the nth arg from the previous line into this line at point. */ +int +rl_vi_yank_arg (count, key) + int count, key; +{ + /* Readline thinks that the first word on a line is the 0th, while vi + thinks the first word on a line is the 1st. Compensate. */ + if (rl_explicit_arg) + rl_yank_nth_arg (count - 1, 0); + else + rl_yank_nth_arg ('$', 0); + + return (0); +} + +/* With an argument, move back that many history lines, else move to the + beginning of history. */ +int +rl_vi_fetch_history (count, c) + int count, c; +{ + int wanted; + + /* Giving an argument of n means we want the nth command in the history + file. The command number is interpreted the same way that the bash + `history' command does it -- that is, giving an argument count of 450 + to this command would get the command listed as number 450 in the + output of `history'. */ + if (rl_explicit_arg) + { + wanted = history_base + where_history () - count; + if (wanted <= 0) + rl_beginning_of_history (0, 0); + else + rl_get_previous_history (wanted, c); + } + else + rl_beginning_of_history (count, 0); + return (0); +} + +/* Search again for the last thing searched for. */ +int +rl_vi_search_again (count, key) + int count, key; +{ + switch (key) + { + case 'n': + rl_noninc_reverse_search_again (count, key); + break; + + case 'N': + rl_noninc_forward_search_again (count, key); + break; + } + return (0); +} + +/* Do a vi style search. */ +int +rl_vi_search (count, key) + int count, key; +{ + switch (key) + { + case '?': + _rl_free_saved_history_line (); + rl_noninc_forward_search (count, key); + break; + + case '/': + _rl_free_saved_history_line (); + rl_noninc_reverse_search (count, key); + break; + + default: + rl_ding (); + break; + } + return (0); +} + +/* Completion, from vi's point of view. */ +int +rl_vi_complete (ignore, key) + int ignore, key; +{ + if ((rl_point < rl_end) && (!whitespace (rl_line_buffer[rl_point]))) + { + if (!whitespace (rl_line_buffer[rl_point + 1])) + rl_vi_end_word (1, 'E'); + rl_point++; + } + + if (key == '*') + rl_complete_internal ('*'); /* Expansion and replacement. */ + else if (key == '=') + rl_complete_internal ('?'); /* List possible completions. */ + else if (key == '\\') + rl_complete_internal (TAB); /* Standard Readline completion. */ + else + rl_complete (0, key); + + if (key == '*' || key == '\\') + rl_vi_start_inserting (key, 1, rl_arg_sign); + + return (0); +} + +/* Tilde expansion for vi mode. */ +int +rl_vi_tilde_expand (ignore, key) + int ignore, key; +{ + rl_tilde_expand (0, key); + rl_vi_start_inserting (key, 1, rl_arg_sign); + return (0); +} + +/* Previous word in vi mode. */ +int +rl_vi_prev_word (count, key) + int count, key; +{ + if (count < 0) + return (rl_vi_next_word (-count, key)); + + if (rl_point == 0) + { + rl_ding (); + return (0); + } + + if (_rl_uppercase_p (key)) + rl_vi_bWord (count, key); + else + rl_vi_bword (count, key); + + return (0); +} + +/* Next word in vi mode. */ +int +rl_vi_next_word (count, key) + int count, key; +{ + if (count < 0) + return (rl_vi_prev_word (-count, key)); + + if (rl_point >= (rl_end - 1)) + { + rl_ding (); + return (0); + } + + if (_rl_uppercase_p (key)) + rl_vi_fWord (count, key); + else + rl_vi_fword (count, key); + return (0); +} + +/* Move to the end of the ?next? word. */ +int +rl_vi_end_word (count, key) + int count, key; +{ + if (count < 0) + { + rl_ding (); + return -1; + } + + if (_rl_uppercase_p (key)) + rl_vi_eWord (count, key); + else + rl_vi_eword (count, key); + return (0); +} + +/* Move forward a word the way that 'W' does. */ +int +rl_vi_fWord (count, ignore) + int count, ignore; +{ + while (count-- && rl_point < (rl_end - 1)) + { + /* Skip until whitespace. */ + while (!whitespace (rl_line_buffer[rl_point]) && rl_point < rl_end) + rl_point++; + + /* Now skip whitespace. */ + while (whitespace (rl_line_buffer[rl_point]) && rl_point < rl_end) + rl_point++; + } + return (0); +} + +int +rl_vi_bWord (count, ignore) + int count, ignore; +{ + while (count-- && rl_point > 0) + { + /* If we are at the start of a word, move back to whitespace so + we will go back to the start of the previous word. */ + if (!whitespace (rl_line_buffer[rl_point]) && + whitespace (rl_line_buffer[rl_point - 1])) + rl_point--; + + while (rl_point > 0 && whitespace (rl_line_buffer[rl_point])) + rl_point--; + + if (rl_point > 0) + { + while (--rl_point >= 0 && !whitespace (rl_line_buffer[rl_point])); + rl_point++; + } + } + return (0); +} + +int +rl_vi_eWord (count, ignore) + int count, ignore; +{ + while (count-- && rl_point < (rl_end - 1)) + { + if (!whitespace (rl_line_buffer[rl_point])) + rl_point++; + + /* Move to the next non-whitespace character (to the start of the + next word). */ + while (rl_point < rl_end && whitespace (rl_line_buffer[rl_point])) + rl_point++; + + if (rl_point && rl_point < rl_end) + { + /* Skip whitespace. */ + while (rl_point < rl_end && whitespace (rl_line_buffer[rl_point])) + rl_point++; + + /* Skip until whitespace. */ + while (rl_point < rl_end && !whitespace (rl_line_buffer[rl_point])) + rl_point++; + + /* Move back to the last character of the word. */ + rl_point--; + } + } + return (0); +} + +int +rl_vi_fword (count, ignore) + int count, ignore; +{ + while (count-- && rl_point < (rl_end - 1)) + { + /* Move to white space (really non-identifer). */ + if (_rl_isident (rl_line_buffer[rl_point])) + { + while (_rl_isident (rl_line_buffer[rl_point]) && rl_point < rl_end) + rl_point++; + } + else /* if (!whitespace (rl_line_buffer[rl_point])) */ + { + while (!_rl_isident (rl_line_buffer[rl_point]) && + !whitespace (rl_line_buffer[rl_point]) && rl_point < rl_end) + rl_point++; + } + + /* Move past whitespace. */ + while (whitespace (rl_line_buffer[rl_point]) && rl_point < rl_end) + rl_point++; + } + return (0); +} + +int +rl_vi_bword (count, ignore) + int count, ignore; +{ + while (count-- && rl_point > 0) + { + int last_is_ident; + + /* If we are at the start of a word, move back to whitespace + so we will go back to the start of the previous word. */ + if (!whitespace (rl_line_buffer[rl_point]) && + whitespace (rl_line_buffer[rl_point - 1])) + rl_point--; + + /* If this character and the previous character are `opposite', move + back so we don't get messed up by the rl_point++ down there in + the while loop. Without this code, words like `l;' screw up the + function. */ + last_is_ident = _rl_isident (rl_line_buffer[rl_point - 1]); + if ((_rl_isident (rl_line_buffer[rl_point]) && !last_is_ident) || + (!_rl_isident (rl_line_buffer[rl_point]) && last_is_ident)) + rl_point--; + + while (rl_point > 0 && whitespace (rl_line_buffer[rl_point])) + rl_point--; + + if (rl_point > 0) + { + if (_rl_isident (rl_line_buffer[rl_point])) + while (--rl_point >= 0 && _rl_isident (rl_line_buffer[rl_point])); + else + while (--rl_point >= 0 && !_rl_isident (rl_line_buffer[rl_point]) && + !whitespace (rl_line_buffer[rl_point])); + rl_point++; + } + } + return (0); +} + +int +rl_vi_eword (count, ignore) + int count, ignore; +{ + while (count-- && rl_point < rl_end - 1) + { + if (!whitespace (rl_line_buffer[rl_point])) + rl_point++; + + while (rl_point < rl_end && whitespace (rl_line_buffer[rl_point])) + rl_point++; + + if (rl_point < rl_end) + { + if (_rl_isident (rl_line_buffer[rl_point])) + while (++rl_point < rl_end && _rl_isident (rl_line_buffer[rl_point])); + else + while (++rl_point < rl_end && !_rl_isident (rl_line_buffer[rl_point]) + && !whitespace (rl_line_buffer[rl_point])); + } + rl_point--; + } + return (0); +} + +int +rl_vi_insert_beg (count, key) + int count, key; +{ + rl_beg_of_line (1, key); + rl_vi_insert_mode (1, key); + return (0); +} + +static void +_rl_vi_append_forward (key) + int key; +{ + int point; + + if (rl_point < rl_end) + { + if (MB_CUR_MAX == 1 || rl_byte_oriented) + rl_point++; + else + { + point = rl_point; + rl_forward_char (1, key); + if (point == rl_point) + rl_point = rl_end; + } + } +} + +int +rl_vi_append_mode (count, key) + int count, key; +{ + _rl_vi_append_forward (key); + rl_vi_start_inserting (key, 1, rl_arg_sign); + return (0); +} + +int +rl_vi_append_eol (count, key) + int count, key; +{ + rl_end_of_line (1, key); + rl_vi_append_mode (1, key); + return (0); +} + +/* What to do in the case of C-d. */ +int +rl_vi_eof_maybe (count, c) + int count, c; +{ + return (rl_newline (1, '\n')); +} + +/* Insertion mode stuff. */ + +/* Switching from one mode to the other really just involves + switching keymaps. */ +int +rl_vi_insertion_mode (count, key) + int count, key; +{ + _rl_keymap = vi_insertion_keymap; + _rl_vi_last_key_before_insert = key; + return (0); +} + +int +rl_vi_insert_mode (count, key) + int count, key; +{ + rl_vi_start_inserting (key, 1, rl_arg_sign); + return (0); +} + +static void +_rl_vi_save_insert (up) + UNDO_LIST *up; +{ + int len, start, end; + + if (up == 0 || up->what != UNDO_INSERT) + { + if (vi_insert_buffer_size >= 1) + vi_insert_buffer[0] = '\0'; + return; + } + + start = up->start; + end = up->end; + len = end - start + 1; + if (len >= vi_insert_buffer_size) + { + vi_insert_buffer_size += (len + 32) - (len % 32); + vi_insert_buffer = (char *)xrealloc (vi_insert_buffer, vi_insert_buffer_size); + } + strncpy (vi_insert_buffer, rl_line_buffer + start, len - 1); + vi_insert_buffer[len-1] = '\0'; +} + +void +_rl_vi_done_inserting () +{ + if (_rl_vi_doing_insert) + { + /* The `C', `s', and `S' commands set this. */ + rl_end_undo_group (); + /* Now, the text between rl_undo_list->next->start and + rl_undo_list->next->end is what was inserted while in insert + mode. It gets copied to VI_INSERT_BUFFER because it depends + on absolute indices into the line which may change (though they + probably will not). */ + _rl_vi_doing_insert = 0; + _rl_vi_save_insert (rl_undo_list->next); + vi_continued_command = 1; + } + else + { + if (rl_undo_list && (_rl_vi_last_key_before_insert == 'i' || + _rl_vi_last_key_before_insert == 'a' || + _rl_vi_last_key_before_insert == 'I' || + _rl_vi_last_key_before_insert == 'A')) + _rl_vi_save_insert (rl_undo_list); + /* XXX - Other keys probably need to be checked. */ + else if (_rl_vi_last_key_before_insert == 'C') + rl_end_undo_group (); + while (_rl_undo_group_level > 0) + rl_end_undo_group (); + vi_continued_command = 0; + } +} + +int +rl_vi_movement_mode (count, key) + int count, key; +{ + if (rl_point > 0) + rl_backward_char (1, key); + + _rl_keymap = vi_movement_keymap; + _rl_vi_done_inserting (); + + /* This is how POSIX.2 says `U' should behave -- everything up until the + first time you go into command mode should not be undone. */ + if (RL_ISSTATE (RL_STATE_VICMDONCE) == 0) + rl_free_undo_list (); + + RL_SETSTATE (RL_STATE_VICMDONCE); + return (0); +} + +int +rl_vi_arg_digit (count, c) + int count, c; +{ + if (c == '0' && rl_numeric_arg == 1 && !rl_explicit_arg) + return (rl_beg_of_line (1, c)); + else + return (rl_digit_argument (count, c)); +} + +/* Change the case of the next COUNT characters. */ +#if defined (HANDLE_MULTIBYTE) +static int +_rl_vi_change_mbchar_case (count) + int count; +{ + wchar_t wc; + char mb[MB_LEN_MAX+1]; + int mlen, p; + size_t m; + mbstate_t ps; + + memset (&ps, 0, sizeof (mbstate_t)); + if (_rl_adjust_point (rl_line_buffer, rl_point, &ps) > 0) + count--; + while (count-- && rl_point < rl_end) + { + m = mbrtowc (&wc, rl_line_buffer + rl_point, rl_end - rl_point, &ps); + if (MB_INVALIDCH (m)) + wc = (wchar_t)rl_line_buffer[rl_point]; + else if (MB_NULLWCH (m)) + wc = L'\0'; + if (iswupper (wc)) + wc = towlower (wc); + else if (iswlower (wc)) + wc = towupper (wc); + else + { + /* Just skip over chars neither upper nor lower case */ + rl_forward_char (1, 0); + continue; + } + + /* Vi is kind of strange here. */ + if (wc) + { + p = rl_point; + mlen = wcrtomb (mb, wc, &ps); + if (mlen >= 0) + mb[mlen] = '\0'; + rl_begin_undo_group (); + rl_vi_delete (1, 0); + if (rl_point < p) /* Did we retreat at EOL? */ + rl_point++; /* XXX - should we advance more than 1 for mbchar? */ + rl_insert_text (mb); + rl_end_undo_group (); + rl_vi_check (); + } + else + rl_forward_char (1, 0); + } + + return 0; +} +#endif + +int +rl_vi_change_case (count, ignore) + int count, ignore; +{ + int c, p; + + /* Don't try this on an empty line. */ + if (rl_point >= rl_end) + return (0); + + c = 0; +#if defined (HANDLE_MULTIBYTE) + if (MB_CUR_MAX > 1 && rl_byte_oriented == 0) + return (_rl_vi_change_mbchar_case (count)); +#endif + + while (count-- && rl_point < rl_end) + { + if (_rl_uppercase_p (rl_line_buffer[rl_point])) + c = _rl_to_lower (rl_line_buffer[rl_point]); + else if (_rl_lowercase_p (rl_line_buffer[rl_point])) + c = _rl_to_upper (rl_line_buffer[rl_point]); + else + { + /* Just skip over characters neither upper nor lower case. */ + rl_forward_char (1, c); + continue; + } + + /* Vi is kind of strange here. */ + if (c) + { + p = rl_point; + rl_begin_undo_group (); + rl_vi_delete (1, c); + if (rl_point < p) /* Did we retreat at EOL? */ + rl_point++; + _rl_insert_char (1, c); + rl_end_undo_group (); + rl_vi_check (); + } + else + rl_forward_char (1, c); + } + return (0); +} + +int +rl_vi_put (count, key) + int count, key; +{ + if (!_rl_uppercase_p (key) && (rl_point + 1 <= rl_end)) + rl_point = _rl_find_next_mbchar (rl_line_buffer, rl_point, 1, MB_FIND_NONZERO); + + while (count--) + rl_yank (1, key); + + rl_backward_char (1, key); + return (0); +} + +static void +_rl_vi_backup () +{ + if (MB_CUR_MAX > 1 && rl_byte_oriented == 0) + rl_point = _rl_find_prev_mbchar (rl_line_buffer, rl_point, MB_FIND_NONZERO); + else + rl_point--; +} + +int +rl_vi_check () +{ + if (rl_point && rl_point == rl_end) + { + if (MB_CUR_MAX > 1 && rl_byte_oriented == 0) + rl_point = _rl_find_prev_mbchar (rl_line_buffer, rl_point, MB_FIND_NONZERO); + else + rl_point--; + } + return (0); +} + +int +rl_vi_column (count, key) + int count, key; +{ + if (count > rl_end) + rl_end_of_line (1, key); + else + rl_point = count - 1; + return (0); +} + +int +rl_vi_domove (key, nextkey) + int key, *nextkey; +{ + int c, save; + int old_end; + + rl_mark = rl_point; + RL_SETSTATE(RL_STATE_MOREINPUT); + c = rl_read_key (); + RL_UNSETSTATE(RL_STATE_MOREINPUT); + + if (c < 0) + { + *nextkey = 0; + return -1; + } + + *nextkey = c; + + if (!member (c, vi_motion)) + { + if (_rl_digit_p (c)) + { + save = rl_numeric_arg; + rl_numeric_arg = _rl_digit_value (c); + rl_explicit_arg = 1; + RL_SETSTATE (RL_STATE_NUMERICARG|RL_STATE_VIMOTION); + rl_digit_loop1 (); + RL_UNSETSTATE (RL_STATE_VIMOTION); + rl_numeric_arg *= save; + RL_SETSTATE(RL_STATE_MOREINPUT); + c = rl_read_key (); /* real command */ + RL_UNSETSTATE(RL_STATE_MOREINPUT); + if (c < 0) + { + *nextkey = 0; + return -1; + } + *nextkey = c; + } + else if (key == c && (key == 'd' || key == 'y' || key == 'c')) + { + rl_mark = rl_end; + rl_beg_of_line (1, c); + _rl_vi_last_motion = c; + return (0); + } + else + return (-1); + } + + _rl_vi_last_motion = c; + + /* Append a blank character temporarily so that the motion routines + work right at the end of the line. */ + old_end = rl_end; + rl_line_buffer[rl_end++] = ' '; + rl_line_buffer[rl_end] = '\0'; + + _rl_dispatch (c, _rl_keymap); + + /* Remove the blank that we added. */ + rl_end = old_end; + rl_line_buffer[rl_end] = '\0'; + if (rl_point > rl_end) + rl_point = rl_end; + + /* No change in position means the command failed. */ + if (rl_mark == rl_point) + return (-1); + + /* rl_vi_f[wW]ord () leaves the cursor on the first character of the next + word. If we are not at the end of the line, and we are on a + non-whitespace character, move back one (presumably to whitespace). */ + if ((_rl_to_upper (c) == 'W') && rl_point < rl_end && rl_point > rl_mark && + !whitespace (rl_line_buffer[rl_point])) + rl_point--; + + /* If cw or cW, back up to the end of a word, so the behaviour of ce + or cE is the actual result. Brute-force, no subtlety. */ + if (key == 'c' && rl_point >= rl_mark && (_rl_to_upper (c) == 'W')) + { + /* Don't move farther back than where we started. */ + while (rl_point > rl_mark && whitespace (rl_line_buffer[rl_point])) + rl_point--; + + /* Posix.2 says that if cw or cW moves the cursor towards the end of + the line, the character under the cursor should be deleted. */ + if (rl_point == rl_mark) + rl_point++; + else + { + /* Move past the end of the word so that the kill doesn't + remove the last letter of the previous word. Only do this + if we are not at the end of the line. */ + if (rl_point >= 0 && rl_point < (rl_end - 1) && !whitespace (rl_line_buffer[rl_point])) + rl_point++; + } + } + + if (rl_mark < rl_point) + SWAP (rl_point, rl_mark); + + return (0); +} + +/* Process C as part of the current numeric argument. Return -1 if the + argument should be aborted, 0 if we should not read any more chars, and + 1 if we should continue to read chars. */ +static int +_rl_vi_arg_dispatch (c) + int c; +{ + int key; + + key = c; + if (c >= 0 && _rl_keymap[c].type == ISFUNC && _rl_keymap[c].function == rl_universal_argument) + { + rl_numeric_arg *= 4; + return 1; + } + + c = UNMETA (c); + + if (_rl_digit_p (c)) + { + if (rl_explicit_arg) + rl_numeric_arg = (rl_numeric_arg * 10) + _rl_digit_value (c); + else + rl_numeric_arg = _rl_digit_value (c); + rl_explicit_arg = 1; + return 1; + } + else + { + rl_clear_message (); + rl_stuff_char (key); + return 0; + } +} + +/* A simplified loop for vi. Don't dispatch key at end. + Don't recognize minus sign? + Should this do rl_save_prompt/rl_restore_prompt? */ +static int +rl_digit_loop1 () +{ + int c, r; + + while (1) + { + if (_rl_arg_overflow ()) + return 1; + + c = _rl_arg_getchar (); + + r = _rl_vi_arg_dispatch (c); + if (r <= 0) + break; + } + + RL_UNSETSTATE(RL_STATE_NUMERICARG); + return (0); +} + +/* + * 1. Create context, set initial values + * 2. Pass to rl_vi_domove to populate + * 3. Use returned context to finish rest of command + * + * Context: + * op + * state + * flags + * numeric arg + * start, end positions + * initial key (key), motion key (c) + */ +int +rl_vi_delete_to (count, key) + int count, key; +{ + int c, start_pos; + + if (_rl_uppercase_p (key)) + rl_stuff_char ('$'); + else if (vi_redoing) + rl_stuff_char (_rl_vi_last_motion); + + start_pos = rl_point; + + if (rl_vi_domove (key, &c)) + { + rl_ding (); + return -1; + } + + /* These are the motion commands that do not require adjusting the + mark. */ + if (((strchr (" l|h^0bBFT`", c) == 0) && (rl_point >= start_pos)) && + (rl_mark < rl_end)) + rl_mark++; + + rl_kill_text (rl_point, rl_mark); + return (0); +} + +int +rl_vi_change_to (count, key) + int count, key; +{ + int c, start_pos; + + if (_rl_uppercase_p (key)) + rl_stuff_char ('$'); + else if (vi_redoing) + rl_stuff_char (_rl_vi_last_motion); + + start_pos = rl_point; + + if (rl_vi_domove (key, &c)) + { + rl_ding (); + return -1; + } + + /* These are the motion commands that do not require adjusting the + mark. c[wW] are handled by special-case code in rl_vi_domove(), + and already leave the mark at the correct location. */ + if (((strchr (" l|hwW^0bBFT`", c) == 0) && (rl_point >= start_pos)) && + (rl_mark < rl_end)) + rl_mark++; + + /* The cursor never moves with c[wW]. */ + if ((_rl_to_upper (c) == 'W') && rl_point < start_pos) + rl_point = start_pos; + + if (vi_redoing) + { + if (vi_insert_buffer && *vi_insert_buffer) + rl_begin_undo_group (); + rl_delete_text (rl_point, rl_mark); + if (vi_insert_buffer && *vi_insert_buffer) + { + rl_insert_text (vi_insert_buffer); + rl_end_undo_group (); + } + } + else + { + rl_begin_undo_group (); /* to make the `u' command work */ + rl_kill_text (rl_point, rl_mark); + /* `C' does not save the text inserted for undoing or redoing. */ + if (_rl_uppercase_p (key) == 0) + _rl_vi_doing_insert = 1; + rl_vi_start_inserting (key, rl_numeric_arg, rl_arg_sign); + } + + return (0); +} + +int +rl_vi_yank_to (count, key) + int count, key; +{ + int c, start_pos; + + if (_rl_uppercase_p (key)) + rl_stuff_char ('$'); + + start_pos = rl_point; + + if (rl_vi_domove (key, &c)) + { + rl_ding (); + return -1; + } + + /* These are the motion commands that do not require adjusting the + mark. */ + if (((strchr (" l|h^0%bBFT`", c) == 0) && (rl_point >= start_pos)) && + (rl_mark < rl_end)) + rl_mark++; + + rl_begin_undo_group (); + rl_kill_text (rl_point, rl_mark); + rl_end_undo_group (); + rl_do_undo (); + rl_point = start_pos; + + return (0); +} + +int +rl_vi_rubout (count, key) + int count, key; +{ + int opoint; + + if (count < 0) + return (rl_vi_delete (-count, key)); + + if (rl_point == 0) + { + rl_ding (); + return -1; + } + + opoint = rl_point; + if (count > 1 && MB_CUR_MAX > 1 && rl_byte_oriented == 0) + rl_backward_char (count, key); + else if (MB_CUR_MAX > 1 && rl_byte_oriented == 0) + rl_point = _rl_find_prev_mbchar (rl_line_buffer, rl_point, MB_FIND_NONZERO); + else + rl_point -= count; + + if (rl_point < 0) + rl_point = 0; + + rl_kill_text (rl_point, opoint); + + return (0); +} + +int +rl_vi_delete (count, key) + int count, key; +{ + int end; + + if (count < 0) + return (rl_vi_rubout (-count, key)); + + if (rl_end == 0) + { + rl_ding (); + return -1; + } + + if (MB_CUR_MAX > 1 && rl_byte_oriented == 0) + end = _rl_find_next_mbchar (rl_line_buffer, rl_point, count, MB_FIND_NONZERO); + else + end = rl_point + count; + + if (end >= rl_end) + end = rl_end; + + rl_kill_text (rl_point, end); + + if (rl_point > 0 && rl_point == rl_end) + rl_backward_char (1, key); + + return (0); +} + +int +rl_vi_back_to_indent (count, key) + int count, key; +{ + rl_beg_of_line (1, key); + while (rl_point < rl_end && whitespace (rl_line_buffer[rl_point])) + rl_point++; + return (0); +} + +int +rl_vi_first_print (count, key) + int count, key; +{ + return (rl_vi_back_to_indent (1, key)); +} + +static int _rl_cs_dir, _rl_cs_orig_dir; + +#if defined (READLINE_CALLBACKS) +static int +_rl_vi_callback_char_search (data) + _rl_callback_generic_arg *data; +{ + int c; +#if defined (HANDLE_MULTIBYTE) + c = _rl_vi_last_search_mblen = _rl_read_mbchar (_rl_vi_last_search_mbchar, MB_LEN_MAX); +#else + RL_SETSTATE(RL_STATE_MOREINPUT); + c = rl_read_key (); + RL_UNSETSTATE(RL_STATE_MOREINPUT); +#endif + + if (c <= 0) + return -1; + +#if !defined (HANDLE_MULTIBYTE) + _rl_vi_last_search_char = c; +#endif + + _rl_callback_func = 0; + _rl_want_redisplay = 1; + +#if defined (HANDLE_MULTIBYTE) + return (_rl_char_search_internal (data->count, _rl_cs_dir, _rl_vi_last_search_mbchar, _rl_vi_last_search_mblen)); +#else + return (_rl_char_search_internal (data->count, _rl_cs_dir, _rl_vi_last_search_char)); +#endif +} +#endif + +int +rl_vi_char_search (count, key) + int count, key; +{ + int c; +#if defined (HANDLE_MULTIBYTE) + static char *target; + static int tlen; +#else + static char target; +#endif + + if (key == ';' || key == ',') + { + if (_rl_cs_orig_dir == 0) + return -1; +#if defined (HANDLE_MULTIBYTE) + if (_rl_vi_last_search_mblen == 0) + return -1; +#else + if (_rl_vi_last_search_char == 0) + return -1; +#endif + _rl_cs_dir = (key == ';') ? _rl_cs_orig_dir : -_rl_cs_orig_dir; + } + else + { + switch (key) + { + case 't': + _rl_cs_orig_dir = _rl_cs_dir = FTO; + break; + + case 'T': + _rl_cs_orig_dir = _rl_cs_dir = BTO; + break; + + case 'f': + _rl_cs_orig_dir = _rl_cs_dir = FFIND; + break; + + case 'F': + _rl_cs_orig_dir = _rl_cs_dir = BFIND; + break; + } + + if (vi_redoing) + { + /* set target and tlen below */ + } +#if defined (READLINE_CALLBACKS) + else if (RL_ISSTATE (RL_STATE_CALLBACK)) + { + _rl_callback_data = _rl_callback_data_alloc (count); + _rl_callback_data->i1 = _rl_cs_dir; + _rl_callback_func = _rl_vi_callback_char_search; + return (0); + } +#endif + else + { +#if defined (HANDLE_MULTIBYTE) + c = _rl_read_mbchar (_rl_vi_last_search_mbchar, MB_LEN_MAX); + if (c <= 0) + return -1; + _rl_vi_last_search_mblen = c; +#else + RL_SETSTATE(RL_STATE_MOREINPUT); + c = rl_read_key (); + RL_UNSETSTATE(RL_STATE_MOREINPUT); + if (c < 0) + return -1; + _rl_vi_last_search_char = c; +#endif + } + } + +#if defined (HANDLE_MULTIBYTE) + target = _rl_vi_last_search_mbchar; + tlen = _rl_vi_last_search_mblen; +#else + target = _rl_vi_last_search_char; +#endif + +#if defined (HANDLE_MULTIBYTE) + return (_rl_char_search_internal (count, _rl_cs_dir, target, tlen)); +#else + return (_rl_char_search_internal (count, _rl_cs_dir, target)); +#endif +} + +/* Match brackets */ +int +rl_vi_match (ignore, key) + int ignore, key; +{ + int count = 1, brack, pos, tmp, pre; + + pos = rl_point; + if ((brack = rl_vi_bracktype (rl_line_buffer[rl_point])) == 0) + { + if (MB_CUR_MAX > 1 && rl_byte_oriented == 0) + { + while ((brack = rl_vi_bracktype (rl_line_buffer[rl_point])) == 0) + { + pre = rl_point; + rl_forward_char (1, key); + if (pre == rl_point) + break; + } + } + else + while ((brack = rl_vi_bracktype (rl_line_buffer[rl_point])) == 0 && + rl_point < rl_end - 1) + rl_forward_char (1, key); + + if (brack <= 0) + { + rl_point = pos; + rl_ding (); + return -1; + } + } + + pos = rl_point; + + if (brack < 0) + { + while (count) + { + tmp = pos; + if (MB_CUR_MAX == 1 || rl_byte_oriented) + pos--; + else + { + pos = _rl_find_prev_mbchar (rl_line_buffer, pos, MB_FIND_ANY); + if (tmp == pos) + pos--; + } + if (pos >= 0) + { + int b = rl_vi_bracktype (rl_line_buffer[pos]); + if (b == -brack) + count--; + else if (b == brack) + count++; + } + else + { + rl_ding (); + return -1; + } + } + } + else + { /* brack > 0 */ + while (count) + { + if (MB_CUR_MAX == 1 || rl_byte_oriented) + pos++; + else + pos = _rl_find_next_mbchar (rl_line_buffer, pos, 1, MB_FIND_ANY); + + if (pos < rl_end) + { + int b = rl_vi_bracktype (rl_line_buffer[pos]); + if (b == -brack) + count--; + else if (b == brack) + count++; + } + else + { + rl_ding (); + return -1; + } + } + } + rl_point = pos; + return (0); +} + +int +rl_vi_bracktype (c) + int c; +{ + switch (c) + { + case '(': return 1; + case ')': return -1; + case '[': return 2; + case ']': return -2; + case '{': return 3; + case '}': return -3; + default: return 0; + } +} + +static int +_rl_vi_change_char (count, c, mb) + int count, c; + char *mb; +{ + int p; + + if (c == '\033' || c == CTRL ('C')) + return -1; + + rl_begin_undo_group (); + while (count-- && rl_point < rl_end) + { + p = rl_point; + rl_vi_delete (1, c); + if (rl_point < p) /* Did we retreat at EOL? */ + rl_point++; +#if defined (HANDLE_MULTIBYTE) + if (MB_CUR_MAX > 1 && rl_byte_oriented == 0) + rl_insert_text (mb); + else +#endif + _rl_insert_char (1, c); + } + + /* The cursor shall be left on the last character changed. */ + rl_backward_char (1, c); + + rl_end_undo_group (); + + return (0); +} + +static int +_rl_vi_callback_getchar (mb, mlen) + char *mb; + int mlen; +{ + int c; + + RL_SETSTATE(RL_STATE_MOREINPUT); + c = rl_read_key (); + RL_UNSETSTATE(RL_STATE_MOREINPUT); + + if (c < 0) + return -1; + +#if defined (HANDLE_MULTIBYTE) + if (MB_CUR_MAX > 1 && rl_byte_oriented == 0) + c = _rl_read_mbstring (c, mb, mlen); +#endif + + return c; +} + +#if defined (READLINE_CALLBACKS) +static int +_rl_vi_callback_change_char (data) + _rl_callback_generic_arg *data; +{ + int c; + char mb[MB_LEN_MAX]; + + _rl_vi_last_replacement = c = _rl_vi_callback_getchar (mb, MB_LEN_MAX); + + if (c < 0) + return -1; + + _rl_callback_func = 0; + _rl_want_redisplay = 1; + + return (_rl_vi_change_char (data->count, c, mb)); +} +#endif + +int +rl_vi_change_char (count, key) + int count, key; +{ + int c; + char mb[MB_LEN_MAX]; + + if (vi_redoing) + { + c = _rl_vi_last_replacement; + mb[0] = c; + mb[1] = '\0'; + } +#if defined (READLINE_CALLBACKS) + else if (RL_ISSTATE (RL_STATE_CALLBACK)) + { + _rl_callback_data = _rl_callback_data_alloc (count); + _rl_callback_func = _rl_vi_callback_change_char; + return (0); + } +#endif + else + _rl_vi_last_replacement = c = _rl_vi_callback_getchar (mb, MB_LEN_MAX); + + if (c < 0) + return -1; + + return (_rl_vi_change_char (count, c, mb)); +} + +int +rl_vi_subst (count, key) + int count, key; +{ + /* If we are redoing, rl_vi_change_to will stuff the last motion char */ + if (vi_redoing == 0) + rl_stuff_char ((key == 'S') ? 'c' : 'l'); /* `S' == `cc', `s' == `cl' */ + + return (rl_vi_change_to (count, 'c')); +} + +int +rl_vi_overstrike (count, key) + int count, key; +{ + if (_rl_vi_doing_insert == 0) + { + _rl_vi_doing_insert = 1; + rl_begin_undo_group (); + } + + if (count > 0) + { + _rl_overwrite_char (count, key); + vi_replace_count += count; + } + + return (0); +} + +int +rl_vi_overstrike_delete (count, key) + int count, key; +{ + int i, s; + + for (i = 0; i < count; i++) + { + if (vi_replace_count == 0) + { + rl_ding (); + break; + } + s = rl_point; + + if (rl_do_undo ()) + vi_replace_count--; + + if (rl_point == s) + rl_backward_char (1, key); + } + + if (vi_replace_count == 0 && _rl_vi_doing_insert) + { + rl_end_undo_group (); + rl_do_undo (); + _rl_vi_doing_insert = 0; + } + return (0); +} + +int +rl_vi_replace (count, key) + int count, key; +{ + int i; + + vi_replace_count = 0; + + if (!vi_replace_map) + { + vi_replace_map = rl_make_bare_keymap (); + + for (i = ' '; i < KEYMAP_SIZE; i++) + vi_replace_map[i].function = rl_vi_overstrike; + + vi_replace_map[RUBOUT].function = rl_vi_overstrike_delete; + vi_replace_map[ESC].function = rl_vi_movement_mode; + vi_replace_map[RETURN].function = rl_newline; + vi_replace_map[NEWLINE].function = rl_newline; + + /* If the normal vi insertion keymap has ^H bound to erase, do the + same here. Probably should remove the assignment to RUBOUT up + there, but I don't think it will make a difference in real life. */ + if (vi_insertion_keymap[CTRL ('H')].type == ISFUNC && + vi_insertion_keymap[CTRL ('H')].function == rl_rubout) + vi_replace_map[CTRL ('H')].function = rl_vi_overstrike_delete; + + } + _rl_keymap = vi_replace_map; + return (0); +} + +#if 0 +/* Try to complete the word we are standing on or the word that ends with + the previous character. A space matches everything. Word delimiters are + space and ;. */ +int +rl_vi_possible_completions() +{ + int save_pos = rl_point; + + if (rl_line_buffer[rl_point] != ' ' && rl_line_buffer[rl_point] != ';') + { + while (rl_point < rl_end && rl_line_buffer[rl_point] != ' ' && + rl_line_buffer[rl_point] != ';') + rl_point++; + } + else if (rl_line_buffer[rl_point - 1] == ';') + { + rl_ding (); + return (0); + } + + rl_possible_completions (); + rl_point = save_pos; + + return (0); +} +#endif + +/* Functions to save and restore marks. */ +static int +_rl_vi_set_mark () +{ + int ch; + + RL_SETSTATE(RL_STATE_MOREINPUT); + ch = rl_read_key (); + RL_UNSETSTATE(RL_STATE_MOREINPUT); + + if (ch < 0 || ch < 'a' || ch > 'z') /* make test against 0 explicit */ + { + rl_ding (); + return -1; + } + ch -= 'a'; + vi_mark_chars[ch] = rl_point; + return 0; +} + +#if defined (READLINE_CALLBACKS) +static int +_rl_vi_callback_set_mark (data) + _rl_callback_generic_arg *data; +{ + _rl_callback_func = 0; + _rl_want_redisplay = 1; + + return (_rl_vi_set_mark ()); +} +#endif + +int +rl_vi_set_mark (count, key) + int count, key; +{ +#if defined (READLINE_CALLBACKS) + if (RL_ISSTATE (RL_STATE_CALLBACK)) + { + _rl_callback_data = 0; + _rl_callback_func = _rl_vi_callback_set_mark; + return (0); + } +#endif + + return (_rl_vi_set_mark ()); +} + +static int +_rl_vi_goto_mark () +{ + int ch; + + RL_SETSTATE(RL_STATE_MOREINPUT); + ch = rl_read_key (); + RL_UNSETSTATE(RL_STATE_MOREINPUT); + + if (ch == '`') + { + rl_point = rl_mark; + return 0; + } + else if (ch < 0 || ch < 'a' || ch > 'z') /* make test against 0 explicit */ + { + rl_ding (); + return -1; + } + + ch -= 'a'; + if (vi_mark_chars[ch] == -1) + { + rl_ding (); + return -1; + } + rl_point = vi_mark_chars[ch]; + return 0; +} + +#if defined (READLINE_CALLBACKS) +static int +_rl_vi_callback_goto_mark (data) + _rl_callback_generic_arg *data; +{ + _rl_callback_func = 0; + _rl_want_redisplay = 1; + + return (_rl_vi_goto_mark ()); +} +#endif + +int +rl_vi_goto_mark (count, key) + int count, key; +{ +#if defined (READLINE_CALLBACKS) + if (RL_ISSTATE (RL_STATE_CALLBACK)) + { + _rl_callback_data = 0; + _rl_callback_func = _rl_vi_callback_goto_mark; + return (0); + } +#endif + + return (_rl_vi_goto_mark ()); +} +#endif /* VI_MODE */ diff --git a/lib/readline/vi_mode.c~ b/lib/readline/vi_mode.c~ index 71599e6c..13bca6db 100644 --- a/lib/readline/vi_mode.c~ +++ b/lib/readline/vi_mode.c~ @@ -1,7 +1,7 @@ /* vi_mode.c -- A vi emulation mode for Bash. Derived from code written by Jeff Sparkes (jsparkes@bnr.ca). */ -/* Copyright (C) 1987-2009 Free Software Foundation, Inc. +/* Copyright (C) 1987-2010 Free Software Foundation, Inc. This file is part of the GNU Readline Library (Readline), a library for reading lines of text with interactive input and history editing. @@ -65,6 +65,8 @@ int _rl_vi_last_command = 'i'; /* default `.' puts you in insert mode */ +_rl_vimotion_cxt *_rl_vimvcxt = 0; + /* Non-zero means enter insertion mode. */ static int _rl_vi_doing_insert; @@ -128,6 +130,12 @@ static int _rl_vi_callback_change_char PARAMS((_rl_callback_generic_arg *)); static int _rl_vi_callback_char_search PARAMS((_rl_callback_generic_arg *)); #endif +static int vi_change_dispatch PARAMS((_rl_vimotion_cxt *)); +static int vi_delete_dispatch PARAMS((_rl_vimotion_cxt *)); +static int vi_yank_dispatch PARAMS((_rl_vimotion_cxt *)); + +static int vidomove_dispatch PARAMS((_rl_vimotion_cxt *)); + void _rl_vi_initialize_line () { @@ -1076,28 +1084,60 @@ rl_digit_loop1 () return (0); } -int -rl_vi_delete_to (count, key) - int count, key; +static void +_rl_mvcxt_init (m, op, key) + _rl_vimotion_cxt *m; + int op, key; { - int c, start_pos; + m->op = op; + m->state = m->flags = 0; + m->ncxt = 0; + m->numeric_arg = -1; + m->start = rl_point; + m->end = rl_end; + m->key = key; + m->motion = -1; +} - if (_rl_uppercase_p (key)) - rl_stuff_char ('$'); - else if (vi_redoing) - rl_stuff_char (_rl_vi_last_motion); +static _rl_vimotion_cxt * +_rl_mvcxt_alloc (op, key) + int op, key; +{ + _rl_vimotion_cxt *m; - start_pos = rl_point; + m = xmalloc (sizeof (_rl_vimotion_cxt)); + _rl_mvcxt_init (m, op, key); + return m; +} - if (rl_vi_domove (key, &c)) - { - rl_ding (); - return -1; - } +static void +_rl_mvcxt_dispose (m) + _rl_vimotion_cxt *m; +{ + free (m); +} + +/* + * 1. Create context, set initial values + * 2. Pass to rl_vi_domove to populate + * 3. Use returned context to finish rest of command + * + * Context: + * op + * state + * flags + * numeric arg + * start, end positions + * initial key (key), motion key (c) + */ +static int +vi_delete_dispatch (m) + _rl_vimotion_cxt *m; +{ /* These are the motion commands that do not require adjusting the mark. */ - if (((strchr (" l|h^0bBFT`", c) == 0) && (rl_point >= start_pos)) && + if (((strchr (" l|h^0bBFT`", m->motion) == 0) && (rl_point >= m->start)) && (rl_mark < rl_end)) rl_mark++; @@ -1106,34 +1146,47 @@ rl_vi_delete_to (count, key) } int -rl_vi_change_to (count, key) +rl_vi_delete_to (count, key) int count, key; { - int c, start_pos; + int c, r; if (_rl_uppercase_p (key)) rl_stuff_char ('$'); else if (vi_redoing) rl_stuff_char (_rl_vi_last_motion); - start_pos = rl_point; + _rl_vimvcxt = _rl_mvcxt_alloc (VIM_DELETE, key); + _rl_vimvcxt->start = rl_point; + /* XXX -- TODO -- pass and use context in rl_vi_domove */ if (rl_vi_domove (key, &c)) { rl_ding (); return -1; } + _rl_vimvcxt->motion = c; + r = vidomove_dispatch (_rl_vimvcxt); + _rl_mvcxt_dispose (_rl_vimvcxt); + _rl_vimvcxt = 0; + return r; +} + +static int +vi_change_dispatch (m) + _rl_vimotion_cxt *m; +{ /* These are the motion commands that do not require adjusting the mark. c[wW] are handled by special-case code in rl_vi_domove(), and already leave the mark at the correct location. */ - if (((strchr (" l|hwW^0bBFT`", c) == 0) && (rl_point >= start_pos)) && + if (((strchr (" l|hwW^0bBFT`", m->motion) == 0) && (rl_point >= m->start)) && (rl_mark < rl_end)) rl_mark++; /* The cursor never moves with c[wW]. */ - if ((_rl_to_upper (c) == 'W') && rl_point < start_pos) - rl_point = start_pos; + if ((_rl_to_upper (m->motion) == 'W') && rl_point < m->start) + rl_point = m->start; if (vi_redoing) { @@ -1151,34 +1204,50 @@ rl_vi_change_to (count, key) rl_begin_undo_group (); /* to make the `u' command work */ rl_kill_text (rl_point, rl_mark); /* `C' does not save the text inserted for undoing or redoing. */ - if (_rl_uppercase_p (key) == 0) + if (_rl_uppercase_p (m->key) == 0) _rl_vi_doing_insert = 1; - rl_vi_start_inserting (key, rl_numeric_arg, rl_arg_sign); + /* XXX -- TODO -- use m->numericarg? */ + rl_vi_start_inserting (m->key, rl_numeric_arg, rl_arg_sign); } return (0); } int -rl_vi_yank_to (count, key) +rl_vi_change_to (count, key) int count, key; { - int c, start_pos; + int c, r; if (_rl_uppercase_p (key)) rl_stuff_char ('$'); + else if (vi_redoing) + rl_stuff_char (_rl_vi_last_motion); - start_pos = rl_point; + _rl_vimvcxt = _rl_mvcxt_alloc (VIM_CHANGE, key); + _rl_vimvcxt->start = rl_point; + /* XXX -- TODO -- pass and use context in rl_vi_domove */ if (rl_vi_domove (key, &c)) { rl_ding (); return -1; } + _rl_vimvcxt->motion = c; + r = vidomove_dispatch (_rl_vimvcxt); + _rl_mvcxt_dispose (_rl_vimvcxt); + _rl_vimvcxt = 0; + return r; +} + +static int +vi_yank_dispatch (m) + _rl_vimotion_cxt *m; +{ /* These are the motion commands that do not require adjusting the mark. */ - if (((strchr (" l|h^0%bBFT`", c) == 0) && (rl_point >= start_pos)) && + if (((strchr (" l|h^0%bBFT`", m->motion) == 0) && (rl_point >= m->start)) && (rl_mark < rl_end)) rl_mark++; @@ -1186,12 +1255,64 @@ rl_vi_yank_to (count, key) rl_kill_text (rl_point, rl_mark); rl_end_undo_group (); rl_do_undo (); - rl_point = start_pos; + rl_point = m->start; return (0); } int +rl_vi_yank_to (count, key) + int count, key; +{ + int c, r; + + if (_rl_uppercase_p (key)) + rl_stuff_char ('$'); + + _rl_vimvcxt = _rl_mvcxt_alloc (VIM_YANK, key); + _rl_vimvcxt->start = rl_point; + + /* XXX -- TODO -- pass and use context in rl_vi_domove */ + if (rl_vi_domove (key, &c)) + { + rl_ding (); + return -1; + } + _rl_vimvcxt->motion = c; + + r = vidomove_dispatch (_rl_vimvcxt); + _rl_mvcxt_dispose (_rl_vimvcxt); + _rl_vimvcxt = 0; + return r; +} + +static int +vidomove_dispatch (m) + _rl_vimotion_cxt *m; +{ + int r; + + switch (m->op) + { + case VIM_DELETE: + r = vi_delete_callback (m); + break; + case VIM_CHANGE: + r = vi_change_callback (m); + break; + case VIM_YANK: + r = vi_yank_callback (m); + break; + default: + fprintf (stderr, "vidomove_dispatch: unknown operator %d", m->op); + r = 1; + break; + } + + return r; +} + +int rl_vi_rubout (count, key) int count, key; { @@ -1316,18 +1437,19 @@ rl_vi_char_search (count, key) static char target; #endif - if (_rl_cs_orig_dir == 0) - return -1; + if (key == ';' || key == ',') + { + if (_rl_cs_orig_dir == 0) + return -1; #if defined (HANDLE_MULTIBYTE) - if (_rl_vi_last_search_mblen == 0) - return -1; + if (_rl_vi_last_search_mblen == 0) + return -1; #else - if (_rl_vi_last_search_char == 0) - return -1; + if (_rl_vi_last_search_char == 0) + return -1; #endif - - if (key == ';' || key == ',') - _rl_cs_dir = (key == ';') ? _rl_cs_orig_dir : -_rl_cs_orig_dir; + _rl_cs_dir = (key == ';') ? _rl_cs_orig_dir : -_rl_cs_orig_dir; + } else { switch (key) diff --git a/lib/sh/dprintf.c b/lib/sh/dprintf.c index 7eca4962..b3b5d644 100644 --- a/lib/sh/dprintf.c +++ b/lib/sh/dprintf.c @@ -1,6 +1,6 @@ /* dprintf -- printf to a file descriptor */ -/* Copyright (C) 2008,2009 Free Software Foundation, Inc. +/* Copyright (C) 2008-2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. diff --git a/lib/sh/dprintf.c~ b/lib/sh/dprintf.c~ index 27d3a4b0..7eca4962 100644 --- a/lib/sh/dprintf.c~ +++ b/lib/sh/dprintf.c~ @@ -1,4 +1,4 @@ -/* fdprintf -- printf to a file descriptor */ +/* dprintf -- printf to a file descriptor */ /* Copyright (C) 2008,2009 Free Software Foundation, Inc. @@ -38,9 +38,9 @@ int #if defined (PREFER_STDARG) -fdprintf(int fd, const char *format, ...) +dprintf(int fd, const char *format, ...) #else -fdprintf(fd, format, va_alist) +dprintf(fd, format, va_alist) int fd; const char *format; va_dcl diff --git a/lib/sh/eaccess.c b/lib/sh/eaccess.c index ff1bb994..4edc612e 100644 --- a/lib/sh/eaccess.c +++ b/lib/sh/eaccess.c @@ -1,6 +1,6 @@ /* eaccess.c - eaccess replacement for the shell, plus other access functions. */ -/* Copyright (C) 2006 Free Software Foundation, Inc. +/* Copyright (C) 2006-2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. diff --git a/lib/sh/eaccess.c~ b/lib/sh/eaccess.c~ index efd9363d..ff1bb994 100644 --- a/lib/sh/eaccess.c~ +++ b/lib/sh/eaccess.c~ @@ -206,7 +206,12 @@ sh_eaccess (path, mode) #if defined (HAVE_FACCESSAT) && defined (AT_EACCESS) return (faccessat (AT_FDCWD, path, mode, AT_EACCESS)); #elif defined (HAVE_EACCESS) /* FreeBSD */ - return (eaccess (path, mode)); /* XXX -- not always correct for X_OK */ + ret = eaccess (path, mode); /* XXX -- not always correct for X_OK */ +# if defined (__FreeBSD__) + if (ret == 0 && current_user.euid == 0 && mode == X_OK) + return (sh_stataccess (path, mode)); +# endif + return ret; #elif defined (EFF_ONLY_OK) /* SVR4(?), SVR4.2 */ return access (path, mode|EFF_ONLY_OK); #else diff --git a/lib/sh/fnxform.c b/lib/sh/fnxform.c index d95274fb..bb91a153 100644 --- a/lib/sh/fnxform.c +++ b/lib/sh/fnxform.c @@ -1,6 +1,6 @@ /* fnxform - use iconv(3) to transform strings to and from "filename" format */ -/* Copyright (C) 2009 Free Software Foundation, Inc. +/* Copyright (C) 2009-2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. diff --git a/lib/sh/fnxform.c~ b/lib/sh/fnxform.c~ new file mode 100644 index 00000000..d95274fb --- /dev/null +++ b/lib/sh/fnxform.c~ @@ -0,0 +1,199 @@ +/* fnxform - use iconv(3) to transform strings to and from "filename" format */ + +/* Copyright (C) 2009 Free Software Foundation, Inc. + + This file is part of GNU Bash, the Bourne Again SHell. + + Bash 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 3 of the License, or + (at your option) any later version. + + Bash 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 Bash. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <config.h> +#if defined (HAVE_UNISTD_H) +# include <unistd.h> +#endif +#include "bashansi.h" +#include <stdio.h> +#include "bashtypes.h" + +#include "stdc.h" +#include "bashintl.h" +#include <xmalloc.h> + +#if defined (HAVE_ICONV) +# include <iconv.h> +#endif + +#if defined (HAVE_LOCALE_CHARSET) +extern const char *locale_charset __P((void)); +#else +extern char *get_locale_var __P((char *)); +#endif + +#if defined (HAVE_ICONV) +static iconv_t conv_fromfs = (iconv_t)-1; +static iconv_t conv_tofs = (iconv_t)-1; + +#define OUTLEN_MAX 4096 + +static char *outbuf = 0; +static size_t outlen = 0; + +static char *curencoding __P((void)); +static void init_tofs __P((void)); +static void init_fromfs __P((void)); + +static char * +curencoding () +{ + char *loc; +#if defined (HAVE_LOCALE_CHARSET) + loc = (char *)locale_charset (); + return loc; +#else + char *dot, *mod; + + loc = get_locale_var ("LC_CTYPE"); + if (loc == 0 || *loc == 0) + return ""; + dot = strchr (loc, '.'); + if (dot == 0) + return loc; + mod = strchr (dot, '@'); + if (mod) + *mod = '\0'; + return dot; +#endif +} + +static void +init_tofs () +{ + char *cur; + + cur = curencoding (); + conv_tofs = iconv_open ("UTF-8-MAC", cur); +} + +static void +init_fromfs () +{ + char *cur; + + cur = curencoding (); + conv_fromfs = iconv_open (cur, "UTF-8-MAC"); +} + +char * +fnx_tofs (string, len) + char *string; + size_t len; +{ +#ifdef MACOSX + ICONV_CONST char *inbuf; + char *tempbuf; + size_t templen; + + if (conv_tofs == (iconv_t)-1) + init_tofs (); + if (conv_tofs == (iconv_t)-1) + return string; + + /* Free and reallocate outbuf if it's *too* big */ + if (outlen >= OUTLEN_MAX && len < OUTLEN_MAX - 8) + { + free (outbuf); + outbuf = 0; + outlen = 0; + } + + inbuf = string; + if (outbuf == 0 || outlen < len + 8) + { + outlen = len + 8; + outbuf = outbuf ? xrealloc (outbuf, outlen + 1) : xmalloc (outlen + 1); + } + tempbuf = outbuf; + templen = outlen; + + iconv (conv_tofs, NULL, NULL, NULL, NULL); + + if (iconv (conv_tofs, &inbuf, &len, &tempbuf, &templen) == (size_t)-1) + return string; + + *tempbuf = '\0'; + return outbuf; +#else + return string; +#endif +} + +char * +fnx_fromfs (string, len) + char *string; + size_t len; +{ +#ifdef MACOSX + ICONV_CONST char *inbuf; + char *tempbuf; + size_t templen; + + if (conv_fromfs == (iconv_t)-1) + init_fromfs (); + if (conv_fromfs == (iconv_t)-1) + return string; + + /* Free and reallocate outbuf if it's *too* big */ + if (outlen >= OUTLEN_MAX && len < OUTLEN_MAX - 8) + { + free (outbuf); + outbuf = 0; + outlen = 0; + } + + inbuf = string; + if (outbuf == 0 || outlen < (len + 8)) + { + outlen = len + 8; + outbuf = outbuf ? xrealloc (outbuf, outlen + 1) : xmalloc (outlen + 1); + } + tempbuf = outbuf; + templen = outlen; + + iconv (conv_fromfs, NULL, NULL, NULL, NULL); + + if (iconv (conv_fromfs, &inbuf, &len, &tempbuf, &templen) == (size_t)-1) + return string; + + *tempbuf = '\0'; + return outbuf; +#else + return string; +#endif +} + +#else +char * +fnx_tofs (string) + char *string; +{ + return string; +} + +char * +fnx_fromfs (string) + char *string; +{ + return string; +} +#endif diff --git a/lib/sh/oslib.c b/lib/sh/oslib.c index d8eba6e6..b3470d16 100644 --- a/lib/sh/oslib.c +++ b/lib/sh/oslib.c @@ -1,6 +1,6 @@ /* oslib.c - functions present only in some unix versions. */ -/* Copyright (C) 1995 Free Software Foundation, Inc. +/* Copyright (C) 1995,2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. diff --git a/lib/sh/oslib.c~ b/lib/sh/oslib.c~ index e554c5cb..d8eba6e6 100644 --- a/lib/sh/oslib.c~ +++ b/lib/sh/oslib.c~ @@ -213,7 +213,8 @@ gethostname (name, namelen) # else /* !HAVE_UNAME */ int gethostname (name, namelen) - int name, namelen; + char *name; + int namelen; { strncpy (name, "unknown", namelen); name[namelen] = '\0'; diff --git a/lib/sh/snprintf.c b/lib/sh/snprintf.c index d681b165..d46b2d9c 100644 --- a/lib/sh/snprintf.c +++ b/lib/sh/snprintf.c @@ -9,7 +9,7 @@ Unix snprintf implementation. derived from inetutils/libinetutils/snprintf.c Version 1.1 - Copyright (C) 2001,2006 Free Software Foundation, Inc. + Copyright (C) 2001,2006,2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. diff --git a/lib/sh/snprintf.c~ b/lib/sh/snprintf.c~ new file mode 100644 index 00000000..d681b165 --- /dev/null +++ b/lib/sh/snprintf.c~ @@ -0,0 +1,2173 @@ +/* snprintf - formatted output to strings, with bounds checking and allocation */ + +/* + build a test version with + gcc -g -DDRIVER -I../.. -I../../include -o test-snprintf snprintf.c fmtu*long.o +*/ + +/* + Unix snprintf implementation. + derived from inetutils/libinetutils/snprintf.c Version 1.1 + + Copyright (C) 2001,2006 Free Software Foundation, Inc. + + This file is part of GNU Bash, the Bourne Again SHell. + + Bash 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 3 of the License, or + (at your option) any later version. + + Bash 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 Bash. If not, see <http://www.gnu.org/licenses/>. + + Revision History: + + 1.1: + * added changes from Miles Bader + * corrected a bug with %f + * added support for %#g + * added more comments :-) + 1.0: + * supporting must ANSI syntaxic_sugars + 0.0: + * support %s %c %d + + THANKS(for the patches and ideas): + Miles Bader + Cyrille Rustom + Jacek Slabocewiz + Mike Parker(mouse) + +*/ + +/* + * Currently doesn't handle (and bash/readline doesn't use): + * * *M$ width, precision specifications + * * %N$ numbered argument conversions + * * inf, nan floating values imperfect (if isinf(), isnan() not in libc) + * * support for `F' is imperfect with ldfallback(), since underlying + * printf may not handle it -- should ideally have another autoconf test + */ + +#define FLOATING_POINT + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +/* GCC 4.2 on Snow Leopard doesn't like the snprintf prototype */ +#if defined(DEBUG) && !defined (MACOSX) +# undef HAVE_SNPRINTF +# undef HAVE_ASPRINTF + +# define HAVE_SNPRINTF 0 +# define HAVE_ASPRINTF 0 +#endif + +#if defined(DRIVER) && !defined(HAVE_CONFIG_H) +#define HAVE_LONG_LONG +#define HAVE_LONG_DOUBLE +#ifdef __linux__ +#define HAVE_PRINTF_A_FORMAT +#endif +#define HAVE_ISINF_IN_LIBC +#define HAVE_ISNAN_IN_LIBC +#define PREFER_STDARG +#define HAVE_STRINGIZE +#define HAVE_LIMITS_H +#define HAVE_STDDEF_H +#define HAVE_LOCALE_H +#define intmax_t long +#endif + +#if !HAVE_SNPRINTF || !HAVE_ASPRINTF + +#include <bashtypes.h> + +#if defined(PREFER_STDARG) +# include <stdarg.h> +#else +# include <varargs.h> +#endif + +#ifdef HAVE_LIMITS_H +# include <limits.h> +#endif +#include <bashansi.h> +#ifdef HAVE_STDDEF_H +# include <stddef.h> +#endif +#include <chartypes.h> + +#ifdef HAVE_STDINT_H +# include <stdint.h> +#endif + +#ifdef FLOATING_POINT +# include <float.h> /* for manifest constants */ +# include <stdio.h> /* for sprintf */ +#endif + +#include <typemax.h> + +#ifdef HAVE_LOCALE_H +# include <locale.h> +#endif + +#include "stdc.h" +#include <shmbutil.h> + +#ifndef DRIVER +# include "shell.h" +#else +# define FL_PREFIX 0x01 /* add 0x, 0X, or 0 prefix as appropriate */ +# define FL_ADDBASE 0x02 /* add base# prefix to converted value */ +# define FL_HEXUPPER 0x04 /* use uppercase when converting to hex */ +# define FL_UNSIGNED 0x08 /* don't add any sign */ +extern char *fmtulong __P((unsigned long int, int, char *, size_t, int)); +extern char *fmtullong __P((unsigned long long int, int, char *, size_t, int)); +#endif + +#ifndef FREE +# define FREE(x) if (x) free (x) +#endif + +/* Bound on length of the string representing an integer value of type T. + Subtract one for the sign bit if T is signed; + 302 / 1000 is log10 (2) rounded up; + add one for integer division truncation; + add one more for a minus sign if t is signed. */ +#define INT_STRLEN_BOUND(t) \ + ((sizeof (t) * CHAR_BIT - TYPE_SIGNED (t)) * 302 / 1000 \ + + 1 + TYPE_SIGNED (t)) + +/* conversion flags */ +#define PF_ALTFORM 0x00001 /* # */ +#define PF_HEXPREFIX 0x00002 /* 0[Xx] */ +#define PF_LADJUST 0x00004 /* - */ +#define PF_ZEROPAD 0x00008 /* 0 */ +#define PF_PLUS 0x00010 /* + */ +#define PF_SPACE 0x00020 /* ' ' */ +#define PF_THOUSANDS 0x00040 /* ' */ + +#define PF_DOT 0x00080 /* `.precision' */ +#define PF_STAR_P 0x00100 /* `*' after precision */ +#define PF_STAR_W 0x00200 /* `*' before or without precision */ + +/* length modifiers */ +#define PF_SIGNEDCHAR 0x00400 /* hh */ +#define PF_SHORTINT 0x00800 /* h */ +#define PF_LONGINT 0x01000 /* l */ +#define PF_LONGLONG 0x02000 /* ll */ +#define PF_LONGDBL 0x04000 /* L */ +#define PF_INTMAX_T 0x08000 /* j */ +#define PF_SIZE_T 0x10000 /* z */ +#define PF_PTRDIFF_T 0x20000 /* t */ + +#define PF_ALLOCBUF 0x40000 /* for asprintf, vasprintf */ + +#define PFM_SN 0x01 /* snprintf, vsnprintf */ +#define PFM_AS 0x02 /* asprintf, vasprintf */ + +#define ASBUFSIZE 128 + +#define x_digs "0123456789abcdef" +#define X_digs "0123456789ABCDEF" + +static char intbuf[INT_STRLEN_BOUND(unsigned long) + 1]; + +static int decpoint; +static int thoussep; +static char *grouping; + +/* + * For the FLOATING POINT FORMAT : + * the challenge was finding a way to + * manipulate the Real numbers without having + * to resort to mathematical function(it + * would require to link with -lm) and not + * going down to the bit pattern(not portable) + * + * so a number, a real is: + + real = integral + fraction + + integral = ... + a(2)*10^2 + a(1)*10^1 + a(0)*10^0 + fraction = b(1)*10^-1 + b(2)*10^-2 + ... + + where: + 0 <= a(i) => 9 + 0 <= b(i) => 9 + + from then it was simple math + */ + +/* + * size of the buffer for the integral part + * and the fraction part + */ +#define MAX_INT 99 + 1 /* 1 for the null */ +#define MAX_FRACT 307 + 1 + +/* + * These functions use static buffers to store the results, + * and so are not reentrant + */ +#define itoa(n) fmtulong(n, 10, intbuf, sizeof(intbuf), 0); +#define dtoa(n, p, f) numtoa(n, 10, p, f) + +#define SWAP_INT(a,b) {int t; t = (a); (a) = (b); (b) = t;} + +#define GETARG(type) (va_arg(args, type)) + +/* Macros that do proper sign extension and handle length modifiers. Used + for the integer conversion specifiers. */ +#define GETSIGNED(p) \ + (((p)->flags & PF_LONGINT) \ + ? GETARG (long) \ + : (((p)->flags & PF_SHORTINT) ? (long)(short)GETARG (int) \ + : (long)GETARG (int))) + +#define GETUNSIGNED(p) \ + (((p)->flags & PF_LONGINT) \ + ? GETARG (unsigned long) \ + : (((p)->flags & PF_SHORTINT) ? (unsigned long)(unsigned short)GETARG (int) \ + : (unsigned long)GETARG (unsigned int))) + + +#ifdef HAVE_LONG_DOUBLE +#define GETLDOUBLE(p) GETARG (long double) +#endif +#define GETDOUBLE(p) GETARG (double) + +#define SET_SIZE_FLAGS(p, type) \ + if (sizeof (type) > sizeof (int)) \ + (p)->flags |= PF_LONGINT; \ + if (sizeof (type) > sizeof (long)) \ + (p)->flags |= PF_LONGLONG; + +/* this struct holds everything we need */ +struct DATA +{ + int length; + char *base; /* needed for [v]asprintf */ + char *holder; + int counter; + const char *pf; + +/* FLAGS */ + int flags; + int justify; + int width, precision; + char pad; +}; + +/* the floating point stuff */ +#ifdef FLOATING_POINT +static double pow_10 __P((int)); +static int log_10 __P((double)); +static double integral __P((double, double *)); +static char *numtoa __P((double, int, int, char **)); +#endif + +static void init_data __P((struct DATA *, char *, size_t, const char *, int)); +static void init_conv_flag __P((struct DATA *)); + +/* for the format */ +#ifdef FLOATING_POINT +static void floating __P((struct DATA *, double)); +static void exponent __P((struct DATA *, double)); +#endif +static void number __P((struct DATA *, unsigned long, int)); +#ifdef HAVE_LONG_LONG +static void lnumber __P((struct DATA *, unsigned long long, int)); +#endif +static void pointer __P((struct DATA *, unsigned long)); +static void strings __P((struct DATA *, char *)); + +#ifdef FLOATING_POINT +# define FALLBACK_FMTSIZE 32 +# define FALLBACK_BASE 4096 +# define LFALLBACK_BASE 5120 +# ifdef HAVE_LONG_DOUBLE +static void ldfallback __P((struct DATA *, const char *, const char *, long double)); +# endif +static void dfallback __P((struct DATA *, const char *, const char *, double)); +#endif + +static char *groupnum __P((char *)); + +#ifndef HAVE_ISINF_IN_LIBC +static int isinf __P((double)); +#endif +#ifndef HAVE_ISNAN_IN_LIBC +static int isnan __P((double)); +#endif + +#ifdef DRIVER +static void memory_error_and_abort (); +static void *xmalloc __P((size_t)); +static void *xrealloc __P((void *, size_t)); +static void xfree __P((void *)); +#else +# include <xmalloc.h> +#endif + +/* those are defines specific to snprintf to hopefully + * make the code clearer :-) + */ +#define RIGHT 1 +#define LEFT 0 +#define NOT_FOUND -1 +#define FOUND 1 +#define MAX_FIELD 15 + +/* round off to the precision */ +#define ROUND(d, p) \ + (d < 0.) ? \ + d - pow_10(-(p)->precision) * 0.5 : \ + d + pow_10(-(p)->precision) * 0.5 + +/* set default precision */ +#define DEF_PREC(p) \ + if ((p)->precision == NOT_FOUND) \ + (p)->precision = 6 + +/* put a char. increment the number of chars written even if we've exceeded + the vsnprintf/snprintf buffer size (for the return value) */ +#define PUT_CHAR(c, p) \ + do \ + { \ + if (((p)->flags & PF_ALLOCBUF) && ((p)->counter >= (p)->length - 1)) \ + { \ + (p)->length += ASBUFSIZE; \ + (p)->base = (char *)xrealloc((p)->base, (p)->length); \ + (p)->holder = (p)->base + (p)->counter; /* in case reallocated */ \ + } \ + if ((p)->counter < (p)->length) \ + *(p)->holder++ = (c); \ + (p)->counter++; \ + } \ + while (0) + +/* Output a string. P->WIDTH has already been adjusted for padding. */ +#define PUT_STRING(string, len, p) \ + do \ + { \ + PAD_RIGHT (p); \ + while ((len)-- > 0) \ + { \ + PUT_CHAR (*(string), (p)); \ + (string)++; \ + } \ + PAD_LEFT (p); \ + } \ + while (0) + +#define PUT_PLUS(d, p, zero) \ + if ((d) > zero && (p)->justify == RIGHT) \ + PUT_CHAR('+', p) + +#define PUT_SPACE(d, p, zero) \ + if (((p)->flags & PF_SPACE) && (d) > zero) \ + PUT_CHAR(' ', p) + +/* pad right */ +#define PAD_RIGHT(p) \ + if ((p)->width > 0 && (p)->justify != LEFT) \ + for (; (p)->width > 0; (p)->width--) \ + PUT_CHAR((p)->pad, p) + +/* pad left */ +#define PAD_LEFT(p) \ + if ((p)->width > 0 && (p)->justify == LEFT) \ + for (; (p)->width > 0; (p)->width--) \ + PUT_CHAR((p)->pad, p) + +/* pad with zeros from decimal precision */ +#define PAD_ZERO(p) \ + if ((p)->precision > 0) \ + for (; (p)->precision > 0; (p)->precision--) \ + PUT_CHAR('0', p) + +/* if width and prec. in the args */ +#define STAR_ARGS(p) \ + do { \ + if ((p)->flags & PF_STAR_W) \ + { \ + (p)->width = GETARG (int); \ + if ((p)->width < 0) \ + { \ + (p)->flags |= PF_LADJUST; \ + (p)->justify = LEFT; \ + (p)->width = -(p)->width; \ + } \ + } \ + if ((p)->flags & PF_STAR_P) \ + { \ + (p)->precision = GETARG (int); \ + if ((p)->precision < 0) \ + { \ + (p)->flags &= ~PF_STAR_P; \ + (p)->precision = NOT_FOUND; \ + } \ + } \ + } while (0) + +#if defined (HAVE_LOCALE_H) && defined (HAVE_LOCALECONV) +# define GETLOCALEDATA(d, t, g) \ + do \ + { \ + struct lconv *lv; \ + if ((d) == 0) { \ + (d) = '.'; (t) = -1; (g) = 0; /* defaults */ \ + lv = localeconv(); \ + if (lv) \ + { \ + if (lv->decimal_point && lv->decimal_point[0]) \ + (d) = lv->decimal_point[0]; \ + if (lv->thousands_sep && lv->thousands_sep[0]) \ + (t) = lv->thousands_sep[0]; \ + (g) = lv->grouping ? lv->grouping : ""; \ + if (*(g) == '\0' || *(g) == CHAR_MAX || (t) == -1) (g) = 0; \ + } \ + } \ + } \ + while (0); +#else +# define GETLOCALEDATA(d, t, g) \ + ( (d) = '.', (t) = ',', g = "\003" ) +#endif + +#ifdef FLOATING_POINT +/* + * Find the nth power of 10 + */ +static double +pow_10(n) + int n; +{ + double P; + + /* handle common cases with fast switch statement. */ + switch (n) + { + case -3: return .001; + case -2: return .01; + case -1: return .1; + case 0: return 1.; + case 1: return 10.; + case 2: return 100.; + case 3: return 1000.; + } + + if (n < 0) + { + P = .0001; + for (n += 4; n < 0; n++) + P /= 10.; + } + else + { + P = 10000.; + for (n -= 4; n > 0; n--) + P *= 10.; + } + + return P; +} + +/* + * Find the integral part of the log in base 10 + * Note: this not a real log10() + I just need and approximation(integerpart) of x in: + 10^x ~= r + * log_10(200) = 2; + * log_10(250) = 2; + * + * NOTE: do not call this with r == 0 -- an infinite loop results. + */ +static int +log_10(r) + double r; +{ + int i = 0; + double result = 1.; + + if (r < 0.) + r = -r; + + if (r < 1.) + { + while (result >= r) + { + result /= 10.; + i++; + } + return (-i); + } + else + { + while (result <= r) + { + result *= 10.; + i++; + } + return (i - 1); + } +} + +/* + * This function return the fraction part of a double + * and set in ip the integral part. + * In many ways it resemble the modf() found on most Un*x + */ +static double +integral(real, ip) + double real; + double *ip; +{ + int j; + double i, s, p; + double real_integral = 0.; + + /* take care of the obvious */ + /* equal to zero ? */ + if (real == 0.) + { + *ip = 0.; + return (0.); + } + + /* negative number ? */ + if (real < 0.) + real = -real; + + /* a fraction ? */ + if ( real < 1.) + { + *ip = 0.; + return real; + } + + /* the real work :-) */ + for (j = log_10(real); j >= 0; j--) + { + p = pow_10(j); + s = (real - real_integral)/p; + i = 0.; + while (i + 1. <= s) + i++; + real_integral += i*p; + } + *ip = real_integral; + return (real - real_integral); +} + +#define PRECISION 1.e-6 +/* + * return an ascii representation of the integral part of the number + * and set fract to be an ascii representation of the fraction part + * the container for the fraction and the integral part or staticly + * declare with fix size + */ +static char * +numtoa(number, base, precision, fract) + double number; + int base, precision; + char **fract; +{ + register int i, j; + double ip, fp; /* integer and fraction part */ + double fraction; + int digits = MAX_INT - 1; + static char integral_part[MAX_INT]; + static char fraction_part[MAX_FRACT]; + double sign; + int ch; + + /* taking care of the obvious case: 0.0 */ + if (number == 0.) + { + integral_part[0] = '0'; + integral_part[1] = '\0'; + /* The fractional part has to take the precision into account */ + for (ch = 0; ch < precision-1; ch++) + fraction_part[ch] = '0'; + fraction_part[ch] = '0'; + fraction_part[ch+1] = '\0'; + if (fract) + *fract = fraction_part; + return integral_part; + } + + /* for negative numbers */ + if ((sign = number) < 0.) + { + number = -number; + digits--; /* sign consume one digit */ + } + + fraction = integral(number, &ip); + number = ip; + + /* do the integral part */ + if (ip == 0.) + { + integral_part[0] = '0'; + i = 1; + } + else + { + for ( i = 0; i < digits && number != 0.; ++i) + { + number /= base; + fp = integral(number, &ip); + ch = (int)((fp + PRECISION)*base); /* force to round */ + integral_part[i] = (ch <= 9) ? ch + '0' : ch + 'a' - 10; + if (! ISXDIGIT((unsigned char)integral_part[i])) + break; /* bail out overflow !! */ + number = ip; + } + } + + /* Oh No !! out of bound, ho well fill it up ! */ + if (number != 0.) + for (i = 0; i < digits; ++i) + integral_part[i] = '9'; + + /* put the sign ? */ + if (sign < 0.) + integral_part[i++] = '-'; + + integral_part[i] = '\0'; + + /* reverse every thing */ + for ( i--, j = 0; j < i; j++, i--) + SWAP_INT(integral_part[i], integral_part[j]); + + /* the fractional part */ + for (i=0, fp=fraction; precision > 0 && i < MAX_FRACT ; i++, precision--) + { + fraction_part[i] = (int)((fp + PRECISION)*10. + '0'); + if (! DIGIT(fraction_part[i])) /* underflow ? */ + break; + fp = (fp*10.0) - (double)(long)((fp + PRECISION)*10.); + } + fraction_part[i] = '\0'; + + if (fract != (char **)0) + *fract = fraction_part; + + return integral_part; +} +#endif + +/* for %d and friends, it puts in holder + * the representation with the right padding + */ +static void +number(p, d, base) + struct DATA *p; + unsigned long d; + int base; +{ + char *tmp, *t; + long sd; + int flags; + + /* An explicit precision turns off the zero-padding flag. */ + if ((p->flags & PF_ZEROPAD) && p->precision >= 0 && (p->flags & PF_DOT)) + p->flags &= ~PF_ZEROPAD; + + sd = d; /* signed for ' ' padding in base 10 */ + flags = 0; + flags = (*p->pf == 'x' || *p->pf == 'X' || *p->pf == 'o' || *p->pf == 'u' || *p->pf == 'U') ? FL_UNSIGNED : 0; + if (*p->pf == 'X') + flags |= FL_HEXUPPER; + + tmp = fmtulong (d, base, intbuf, sizeof(intbuf), flags); + t = 0; + if ((p->flags & PF_THOUSANDS)) + { + GETLOCALEDATA(decpoint, thoussep, grouping); + if (grouping && (t = groupnum (tmp))) + tmp = t; + } + + p->width -= strlen(tmp); + PAD_RIGHT(p); + + if ((p->flags & PF_DOT) && p->precision > 0) + { + p->precision -= strlen(tmp); + PAD_ZERO(p); + } + + switch (base) + { + case 10: + PUT_PLUS(sd, p, 0); + PUT_SPACE(sd, p, 0); + break; + case 8: + if (p->flags & PF_ALTFORM) + PUT_CHAR('0', p); + break; + case 16: + if (p->flags & PF_ALTFORM) + { + PUT_CHAR('0', p); + PUT_CHAR(*p->pf, p); + } + break; + } + + while (*tmp) + { + PUT_CHAR(*tmp, p); + tmp++; + } + + PAD_LEFT(p); + FREE (t); +} + +#ifdef HAVE_LONG_LONG +/* + * identical to number() but works for `long long' + */ +static void +lnumber(p, d, base) + struct DATA *p; + unsigned long long d; + int base; +{ + char *tmp, *t; + long long sd; + int flags; + + /* An explicit precision turns off the zero-padding flag. */ + if ((p->flags & PF_ZEROPAD) && p->precision >= 0 && (p->flags & PF_DOT)) + p->flags &= ~PF_ZEROPAD; + + sd = d; /* signed for ' ' padding in base 10 */ + flags = (*p->pf == 'x' || *p->pf == 'X' || *p->pf == 'o' || *p->pf == 'u' || *p->pf == 'U') ? FL_UNSIGNED : 0; + if (*p->pf == 'X') + flags |= FL_HEXUPPER; + + tmp = fmtullong (d, base, intbuf, sizeof(intbuf), flags); + t = 0; + if ((p->flags & PF_THOUSANDS)) + { + GETLOCALEDATA(decpoint, thoussep, grouping); + if (grouping && (t = groupnum (tmp))) + tmp = t; + } + + p->width -= strlen(tmp); + PAD_RIGHT(p); + + if ((p->flags & PF_DOT) && p->precision > 0) + { + p->precision -= strlen(tmp); + PAD_ZERO(p); + } + + switch (base) + { + case 10: + PUT_PLUS(sd, p, 0); + PUT_SPACE(sd, p, 0); + break; + case 8: + if (p->flags & PF_ALTFORM) + PUT_CHAR('0', p); + break; + case 16: + if (p->flags & PF_ALTFORM) + { + PUT_CHAR('0', p); + PUT_CHAR(*p->pf, p); + } + break; + } + + while (*tmp) + { + PUT_CHAR(*tmp, p); + tmp++; + } + + PAD_LEFT(p); + FREE (t); +} +#endif + +static void +pointer(p, d) + struct DATA *p; + unsigned long d; +{ + char *tmp; + + tmp = fmtulong(d, 16, intbuf, sizeof(intbuf), 0); + p->width -= strlen(tmp); + PAD_RIGHT(p); + + /* prefix '0x' for pointers */ + PUT_CHAR('0', p); + PUT_CHAR('x', p); + + while (*tmp) + { + PUT_CHAR(*tmp, p); + tmp++; + } + + PAD_LEFT(p); +} + +/* %s strings */ +static void +strings(p, tmp) + struct DATA *p; + char *tmp; +{ + size_t len; + + len = strlen(tmp); + if (p->precision != NOT_FOUND) /* the smallest number */ + len = (len < p->precision ? len : p->precision); + p->width -= len; + + PUT_STRING (tmp, len, p); +} + +#if HANDLE_MULTIBYTE +/* %ls wide-character strings */ +static void +wstrings(p, tmp) + struct DATA *p; + wchar_t *tmp; +{ + size_t len; + mbstate_t mbs; + char *os; + const wchar_t *ws; + + memset (&mbs, '\0', sizeof (mbstate_t)); + ws = (const wchar_t *)tmp; + + os = (char *)NULL; + if (p->precision != NOT_FOUND) + { + os = (char *)xmalloc (p->precision + 1); + len = wcsrtombs (os, &ws, p->precision, &mbs); + } + else + { + len = wcsrtombs (NULL, &ws, 0, &mbs); + if (len != (size_t)-1) + { + memset (&mbs, '\0', sizeof (mbstate_t)); + os = (char *)xmalloc (len + 1); + (void)wcsrtombs (os, &ws, len + 1, &mbs); + } + } + if (len == (size_t)-1) + { + /* invalid multibyte sequence; bail now. */ + FREE (os); + return; + } + + p->width -= len; + PUT_STRING (os, len, p); + free (os); +} + +static void +wchars (p, wc) + struct DATA *p; + wint_t wc; +{ + char *lbuf, *l; + mbstate_t mbs; + size_t len; + + lbuf = (char *)malloc (MB_CUR_MAX+1); + if (lbuf == 0) + return; + memset (&mbs, '\0', sizeof (mbstate_t)); + len = wcrtomb (lbuf, wc, &mbs); + if (len == (size_t)-1) + /* conversion failed; bail now. */ + return; + p->width -= len; + l = lbuf; + PUT_STRING (l, len, p); + free (lbuf); +} +#endif /* HANDLE_MULTIBYTE */ + +#ifdef FLOATING_POINT + +#ifndef HAVE_ISINF_IN_LIBC +/* Half-assed versions, since we don't want to link with libm. */ +static int +isinf(d) + double d; +{ +#ifdef DBL_MAX + if (d < DBL_MIN) + return -1; + else if (d > DBL_MAX) + return 1; + else +#endif + return 0; +} +#endif + +#ifndef HAVE_ISNAN_IN_LIBC +static int +isnan(d) + double d; +{ + return 0; +} +#endif + +/* Check for [+-]infinity and NaN. If MODE == 1, we check for Infinity, else + (mode == 2) we check for NaN. This does the necessary printing. Returns + 1 if Inf or Nan, 0 if not. */ +static int +chkinfnan(p, d, mode) + struct DATA *p; + double d; + int mode; /* == 1 for inf, == 2 for nan */ +{ + int i; + char *tmp; + char *big, *small; + + i = (mode == 1) ? isinf(d) : isnan(d); + if (i == 0) + return 0; + big = (mode == 1) ? "INF" : "NAN"; + small = (mode == 1) ? "inf" : "nan"; + + tmp = (*p->pf == 'F' || *p->pf == 'G' || *p->pf == 'E') ? big : small; + + if (i < 0) + PUT_CHAR('-', p); + + while (*tmp) + { + PUT_CHAR (*tmp, p); + tmp++; + } + + return 1; +} + +/* %f %F %g %G floating point representation */ +static void +floating(p, d) + struct DATA *p; + double d; +{ + char *tmp, *tmp2, *t; + int i; + + if (d != 0 && (chkinfnan(p, d, 1) || chkinfnan(p, d, 2))) + return; /* already printed nan or inf */ + + GETLOCALEDATA(decpoint, thoussep, grouping); + DEF_PREC(p); + d = ROUND(d, p); + tmp = dtoa(d, p->precision, &tmp2); + t = 0; + if ((p->flags & PF_THOUSANDS) && grouping && (t = groupnum (tmp))) + tmp = t; + + if ((*p->pf == 'g' || *p->pf == 'G') && (p->flags & PF_ALTFORM) == 0) + { + /* smash the trailing zeros unless altform */ + for (i = strlen(tmp2) - 1; i >= 0 && tmp2[i] == '0'; i--) + tmp2[i] = '\0'; + if (tmp2[0] == '\0') + p->precision = 0; + } + + /* calculate the padding. 1 for the dot */ + p->width = p->width - + ((d > 0. && p->justify == RIGHT) ? 1:0) - + ((p->flags & PF_SPACE) ? 1:0) - + strlen(tmp) - p->precision - + ((p->precision != 0 || (p->flags & PF_ALTFORM)) ? 1 : 0); /* radix char */ + PAD_RIGHT(p); + PUT_PLUS(d, p, 0.); + PUT_SPACE(d, p, 0.); + + while (*tmp) + { + PUT_CHAR(*tmp, p); /* the integral */ + tmp++; + } + FREE (t); + + if (p->precision != 0 || (p->flags & PF_ALTFORM)) + PUT_CHAR(decpoint, p); /* put the '.' */ + + for (; *tmp2; tmp2++) + PUT_CHAR(*tmp2, p); /* the fraction */ + + PAD_LEFT(p); +} + +/* %e %E %g %G exponent representation */ +static void +exponent(p, d) + struct DATA *p; + double d; +{ + char *tmp, *tmp2; + int j, i; + + if (d != 0 && (chkinfnan(p, d, 1) || chkinfnan(p, d, 2))) + return; /* already printed nan or inf */ + + GETLOCALEDATA(decpoint, thoussep, grouping); + DEF_PREC(p); + if (d == 0.) + j = 0; + else + { + j = log_10(d); + d = d / pow_10(j); /* get the Mantissa */ + d = ROUND(d, p); + } + tmp = dtoa(d, p->precision, &tmp2); + + /* 1 for unit, 1 for the '.', 1 for 'e|E', + * 1 for '+|-', 2 for 'exp' */ + /* calculate how much padding need */ + p->width = p->width - + ((d > 0. && p->justify == RIGHT) ? 1:0) - + ((p->flags & PF_SPACE) ? 1:0) - p->precision - 6; + + PAD_RIGHT(p); + PUT_PLUS(d, p, 0.); + PUT_SPACE(d, p, 0.); + + while (*tmp) + { + PUT_CHAR(*tmp, p); + tmp++; + } + + if (p->precision != 0 || (p->flags & PF_ALTFORM)) + PUT_CHAR(decpoint, p); /* the '.' */ + + if ((*p->pf == 'g' || *p->pf == 'G') && (p->flags & PF_ALTFORM) == 0) + /* smash the trailing zeros unless altform */ + for (i = strlen(tmp2) - 1; i >= 0 && tmp2[i] == '0'; i--) + tmp2[i] = '\0'; + + for (; *tmp2; tmp2++) + PUT_CHAR(*tmp2, p); /* the fraction */ + + /* the exponent put the 'e|E' */ + if (*p->pf == 'g' || *p->pf == 'e') + PUT_CHAR('e', p); + else + PUT_CHAR('E', p); + + /* the sign of the exp */ + if (j >= 0) + PUT_CHAR('+', p); + else + { + PUT_CHAR('-', p); + j = -j; + } + + tmp = itoa(j); + /* pad out to at least two spaces. pad with `0' if the exponent is a + single digit. */ + if (j <= 9) + PUT_CHAR('0', p); + + /* the exponent */ + while (*tmp) + { + PUT_CHAR(*tmp, p); + tmp++; + } + + PAD_LEFT(p); +} +#endif + +/* Return a new string with the digits in S grouped according to the locale's + grouping info and thousands separator. If no grouping should be performed, + this returns NULL; the caller needs to check for it. */ +static char * +groupnum (s) + char *s; +{ + char *se, *ret, *re, *g; + int len, slen; + + if (grouping == 0 || *grouping <= 0 || *grouping == CHAR_MAX) + return ((char *)NULL); + + /* find min grouping to size returned string */ + for (len = *grouping, g = grouping; *g; g++) + if (*g > 0 && *g < len) + len = *g; + + slen = strlen (s); + len = slen / len + 1; + ret = (char *)xmalloc (slen + len + 1); + re = ret + slen + len; + *re = '\0'; + + g = grouping; + se = s + slen; + len = *g; + + while (se > s) + { + *--re = *--se; + + /* handle `-' inserted by numtoa() and the fmtu* family here. */ + if (se > s && se[-1] == '-') + continue; + + /* begin new group. */ + if (--len == 0 && se > s) + { + *--re = thoussep; + len = *++g; /* was g++, but that uses first char twice (glibc bug, too) */ + if (*g == '\0') + len = *--g; /* use previous grouping */ + else if (*g == CHAR_MAX) + { + do + *--re = *--se; + while (se > s); + break; + } + } + } + + if (re > ret) +#ifdef HAVE_MEMMOVE + memmove (ret, re, strlen (re) + 1); +#else + strcpy (ret, re); +#endif + + return ret; +} + +/* initialize the conversion specifiers */ +static void +init_conv_flag (p) + struct DATA *p; +{ + p->flags &= PF_ALLOCBUF; /* preserve PF_ALLOCBUF flag */ + p->precision = p->width = NOT_FOUND; + p->justify = NOT_FOUND; + p->pad = ' '; +} + +static void +init_data (p, string, length, format, mode) + struct DATA *p; + char *string; + size_t length; + const char *format; + int mode; +{ + p->length = length - 1; /* leave room for '\0' */ + p->holder = p->base = string; + p->pf = format; + p->counter = 0; + p->flags = (mode == PFM_AS) ? PF_ALLOCBUF : 0; +} + +static int +#if defined (__STDC__) +vsnprintf_internal(struct DATA *data, char *string, size_t length, const char *format, va_list args) +#else +vsnprintf_internal(data, string, length, format, args) + struct DATA *data; + char *string; + size_t length; + const char *format; + va_list args; +#endif +{ + double d; /* temporary holder */ +#ifdef HAVE_LONG_DOUBLE + long double ld; /* for later */ +#endif + unsigned long ul; +#ifdef HAVE_LONG_LONG + unsigned long long ull; +#endif + int state, i, c, n; + char *s; +#if HANDLE_MULTIBYTE + wchar_t *ws; + wint_t wc; +#endif + const char *convstart; + int negprec; + + /* Sanity check, the string length must be >= 0. C99 actually says that + LENGTH can be zero here, in the case of snprintf/vsnprintf (it's never + 0 in the case of asprintf/vasprintf), and the return value is the number + of characters that would have been written. */ + if (length < 0) + return -1; + + if (format == 0) + return 0; + + /* Reset these for each call because the locale might have changed. */ + decpoint = thoussep = 0; + grouping = 0; + + negprec = 0; + for (; c = *(data->pf); data->pf++) + { + if (c != '%') + { + PUT_CHAR (c, data); + continue; + } + + convstart = data->pf; + init_conv_flag (data); /* initialise format flags */ + + state = 1; + for (state = 1; state && *data->pf; ) + { + c = *(++data->pf); + /* fmtend = data->pf */ +#if defined (FLOATING_POINT) && defined (HAVE_LONG_DOUBLE) + if (data->flags & PF_LONGDBL) + { + switch (c) + { + case 'f': case 'F': + case 'e': case 'E': + case 'g': case 'G': +# ifdef HAVE_PRINTF_A_FORMAT + case 'a': case 'A': +# endif + STAR_ARGS (data); + ld = GETLDOUBLE (data); + ldfallback (data, convstart, data->pf, ld); + goto conv_break; + } + } +#endif /* FLOATING_POINT && HAVE_LONG_DOUBLE */ + + switch (c) + { + /* Parse format flags */ + case '\0': /* a NULL here ? ? bail out */ + *data->holder = '\0'; + return data->counter; + break; + case '#': + data->flags |= PF_ALTFORM; + continue; + case '0': + data->flags |= PF_ZEROPAD; + data->pad = '0'; + continue; + case '*': + if (data->flags & PF_DOT) + data->flags |= PF_STAR_P; + else + data->flags |= PF_STAR_W; + continue; + case '-': + if ((data->flags & PF_DOT) == 0) + { + data->flags |= PF_LADJUST; + data->justify = LEFT; + } + else + negprec = 1; + continue; + case ' ': + if ((data->flags & PF_PLUS) == 0) + data->flags |= PF_SPACE; + continue; + case '+': + if ((data->flags & PF_DOT) == 0) + { + data->flags |= PF_PLUS; + data->justify = RIGHT; + } + continue; + case '\'': + data->flags |= PF_THOUSANDS; + continue; + + case '1': case '2': case '3': + case '4': case '5': case '6': + case '7': case '8': case '9': + n = 0; + do + { + n = n * 10 + TODIGIT(c); + c = *(++data->pf); + } + while (DIGIT(c)); + data->pf--; /* went too far */ + if (n < 0) + n = 0; + if (data->flags & PF_DOT) + data->precision = negprec ? NOT_FOUND : n; + else + data->width = n; + continue; + + /* optional precision */ + case '.': + data->flags |= PF_DOT; + data->precision = 0; + continue; + + /* length modifiers */ + case 'h': + data->flags |= (data->flags & PF_SHORTINT) ? PF_SIGNEDCHAR : PF_SHORTINT; + continue; + case 'l': + data->flags |= (data->flags & PF_LONGINT) ? PF_LONGLONG : PF_LONGINT; + continue; + case 'L': + data->flags |= PF_LONGDBL; + continue; + case 'q': + data->flags |= PF_LONGLONG; + continue; + case 'j': + data->flags |= PF_INTMAX_T; + SET_SIZE_FLAGS(data, intmax_t); + continue; + case 'z': + data->flags |= PF_SIZE_T; + SET_SIZE_FLAGS(data, size_t); + continue; + case 't': + data->flags |= PF_PTRDIFF_T; + SET_SIZE_FLAGS(data, ptrdiff_t); + continue; + + /* Conversion specifiers */ +#ifdef FLOATING_POINT + case 'f': /* float, double */ + case 'F': + STAR_ARGS(data); + d = GETDOUBLE(data); + floating(data, d); +conv_break: + state = 0; + break; + case 'g': + case 'G': + STAR_ARGS(data); + DEF_PREC(data); + d = GETDOUBLE(data); + i = (d != 0.) ? log_10(d) : -1; + /* + * for '%g|%G' ANSI: use f if exponent + * is in the range or [-4,p] exclusively + * else use %e|%E + */ + if (-4 < i && i < data->precision) + { + /* reset precision */ + data->precision -= i + 1; + floating(data, d); + } + else + { + /* reduce precision by 1 because of leading digit before + decimal point in e format. */ + data->precision--; + exponent(data, d); + } + state = 0; + break; + case 'e': + case 'E': /* Exponent double */ + STAR_ARGS(data); + d = GETDOUBLE(data); + exponent(data, d); + state = 0; + break; +# ifdef HAVE_PRINTF_A_FORMAT + case 'a': + case 'A': + STAR_ARGS(data); + d = GETDOUBLE(data); + dfallback(data, convstart, data->pf, d); + state = 0; + break; +# endif /* HAVE_PRINTF_A_FORMAT */ +#endif /* FLOATING_POINT */ + case 'U': + data->flags |= PF_LONGINT; + /* FALLTHROUGH */ + case 'u': + STAR_ARGS(data); +#ifdef HAVE_LONG_LONG + if (data->flags & PF_LONGLONG) + { + ull = GETARG (unsigned long long); + lnumber(data, ull, 10); + } + else +#endif + { + ul = GETUNSIGNED(data); + number(data, ul, 10); + } + state = 0; + break; + case 'D': + data->flags |= PF_LONGINT; + /* FALLTHROUGH */ + case 'd': /* decimal */ + case 'i': + STAR_ARGS(data); +#ifdef HAVE_LONG_LONG + if (data->flags & PF_LONGLONG) + { + ull = GETARG (long long); + lnumber(data, ull, 10); + } + else +#endif + { + ul = GETSIGNED(data); + number(data, ul, 10); + } + state = 0; + break; + case 'o': /* octal */ + STAR_ARGS(data); +#ifdef HAVE_LONG_LONG + if (data->flags & PF_LONGLONG) + { + ull = GETARG (unsigned long long); + lnumber(data, ull, 8); + } + else +#endif + { + ul = GETUNSIGNED(data); + number(data, ul, 8); + } + state = 0; + break; + case 'x': + case 'X': /* hexadecimal */ + STAR_ARGS(data); +#ifdef HAVE_LONG_LONG + if (data->flags & PF_LONGLONG) + { + ull = GETARG (unsigned long long); + lnumber(data, ull, 16); + } + else +#endif + { + ul = GETUNSIGNED(data); + number(data, ul, 16); + } + state = 0; + break; + case 'p': + STAR_ARGS(data); + ul = (unsigned long)GETARG (void *); + pointer(data, ul); + state = 0; + break; +#if HANDLE_MULTIBYTE + case 'C': + data->flags |= PF_LONGINT; + /* FALLTHROUGH */ +#endif + case 'c': /* character */ + STAR_ARGS(data); +#if HANDLE_MULTIBYTE + if (data->flags & PF_LONGINT) + { + wc = GETARG (wint_t); + wchars (data, wc); + } + else +#endif + { + ul = GETARG (int); + PUT_CHAR(ul, data); + } + state = 0; + break; +#if HANDLE_MULTIBYTE + case 'S': + data->flags |= PF_LONGINT; + /* FALLTHROUGH */ +#endif + case 's': /* string */ + STAR_ARGS(data); +#if HANDLE_MULTIBYTE + if (data->flags & PF_LONGINT) + { + ws = GETARG (wchar_t *); + wstrings (data, ws); + } + else +#endif + { + s = GETARG (char *); + strings(data, s); + } + state = 0; + break; + case 'n': +#ifdef HAVE_LONG_LONG + if (data->flags & PF_LONGLONG) + *(GETARG (long long *)) = data->counter; + else +#endif + if (data->flags & PF_LONGINT) + *(GETARG (long *)) = data->counter; + else if (data->flags & PF_SHORTINT) + *(GETARG (short *)) = data->counter; + else + *(GETARG (int *)) = data->counter; + state = 0; + break; + case '%': /* nothing just % */ + PUT_CHAR('%', data); + state = 0; + break; + default: + /* is this an error ? maybe bail out */ + state = 0; + break; + } /* end switch */ + } /* end of `%' for loop */ + } /* end of format string for loop */ + + if (data->length >= 0) + *data->holder = '\0'; /* the end ye ! */ + + return data->counter; +} + +#if defined (FLOATING_POINT) && defined (HAVE_LONG_DOUBLE) +/* + * Printing floating point numbers accurately is an art. I'm not good + * at it. Fall back to sprintf for long double formats. + */ +static void +ldfallback (data, fs, fe, ld) + struct DATA *data; + const char *fs, *fe; + long double ld; +{ + register char *x; + char fmtbuf[FALLBACK_FMTSIZE], *obuf; + int fl; + + fl = LFALLBACK_BASE + (data->precision < 6 ? 6 : data->precision) + 2; + obuf = (char *)xmalloc (fl); + fl = fe - fs + 1; + strncpy (fmtbuf, fs, fl); + fmtbuf[fl] = '\0'; + + if ((data->flags & PF_STAR_W) && (data->flags & PF_STAR_P)) + sprintf (obuf, fmtbuf, data->width, data->precision, ld); + else if (data->flags & PF_STAR_W) + sprintf (obuf, fmtbuf, data->width, ld); + else if (data->flags & PF_STAR_P) + sprintf (obuf, fmtbuf, data->precision, ld); + else + sprintf (obuf, fmtbuf, ld); + + for (x = obuf; *x; x++) + PUT_CHAR (*x, data); + xfree (obuf); +} +#endif /* FLOATING_POINT && HAVE_LONG_DOUBLE */ + +#ifdef FLOATING_POINT +/* Used for %a, %A if the libc printf supports them. */ +static void +dfallback (data, fs, fe, d) + struct DATA *data; + const char *fs, *fe; + double d; +{ + register char *x; + char fmtbuf[FALLBACK_FMTSIZE], obuf[FALLBACK_BASE]; + int fl; + + fl = fe - fs + 1; + strncpy (fmtbuf, fs, fl); + fmtbuf[fl] = '\0'; + + if ((data->flags & PF_STAR_W) && (data->flags & PF_STAR_P)) + sprintf (obuf, fmtbuf, data->width, data->precision, d); + else if (data->flags & PF_STAR_W) + sprintf (obuf, fmtbuf, data->width, d); + else if (data->flags & PF_STAR_P) + sprintf (obuf, fmtbuf, data->precision, d); + else + sprintf (obuf, fmtbuf, d); + + for (x = obuf; *x; x++) + PUT_CHAR (*x, data); +} +#endif /* FLOATING_POINT */ + +#if !HAVE_SNPRINTF + +int +#if defined (__STDC__) +vsnprintf(char *string, size_t length, const char *format, va_list args) +#else +vsnprintf(string, length, format, args) + char *string; + size_t length; + const char *format; + va_list args; +#endif +{ + struct DATA data; + + if (string == 0 && length != 0) + return 0; + init_data (&data, string, length, format, PFM_SN); + return (vsnprintf_internal(&data, string, length, format, args)); +} + +int +#if defined(PREFER_STDARG) +snprintf(char *string, size_t length, const char * format, ...) +#else +snprintf(string, length, format, va_alist) + char *string; + size_t length; + const char *format; + va_dcl +#endif +{ + struct DATA data; + int rval; + va_list args; + + SH_VA_START(args, format); + + if (string == 0 && length != 0) + return 0; + init_data (&data, string, length, format, PFM_SN); + rval = vsnprintf_internal (&data, string, length, format, args); + + va_end(args); + + return rval; +} + +#endif /* HAVE_SNPRINTF */ + +#if !HAVE_ASPRINTF + +int +#if defined (__STDC__) +vasprintf(char **stringp, const char *format, va_list args) +#else +vasprintf(stringp, format, args) + char **stringp; + const char *format; + va_list args; +#endif +{ + struct DATA data; + char *string; + int r; + + string = (char *)xmalloc(ASBUFSIZE); + init_data (&data, string, ASBUFSIZE, format, PFM_AS); + r = vsnprintf_internal(&data, string, ASBUFSIZE, format, args); + *stringp = data.base; /* not string in case reallocated */ + return r; +} + +int +#if defined(PREFER_STDARG) +asprintf(char **stringp, const char * format, ...) +#else +asprintf(stringp, format, va_alist) + char **stringp; + const char *format; + va_dcl +#endif +{ + int rval; + va_list args; + + SH_VA_START(args, format); + + rval = vasprintf (stringp, format, args); + + va_end(args); + + return rval; +} + +#endif /* !HAVE_ASPRINTF */ + +#endif /* !HAVE_SNPRINTF || !HAVE_ASPRINTF */ + +#ifdef DRIVER + +static void +memory_error_and_abort () +{ + write (2, "out of virtual memory\n", 22); + abort (); +} + +static void * +xmalloc(bytes) + size_t bytes; +{ + void *ret; + + ret = malloc(bytes); + if (ret == 0) + memory_error_and_abort (); + return ret; +} + +static void * +xrealloc (pointer, bytes) + void *pointer; + size_t bytes; +{ + void *ret; + + ret = pointer ? realloc(pointer, bytes) : malloc(bytes); + if (ret == 0) + memory_error_and_abort (); + return ret; +} + +static void +xfree(x) + void *x; +{ + if (x) + free (x); +} + +/* set of small tests for snprintf() */ +main() +{ + char holder[100]; + char *h; + int i, si, ai; + +#ifdef HAVE_LOCALE_H + setlocale(LC_ALL, ""); +#endif + +#if 1 + si = snprintf((char *)NULL, 0, "abcde\n"); + printf("snprintf returns %d with NULL first argument and size of 0\n", si); + si = snprintf(holder, 0, "abcde\n"); + printf("snprintf returns %d with non-NULL first argument and size of 0\n", si); + si = snprintf((char *)NULL, 16, "abcde\n"); + printf("snprintf returns %d with NULL first argument and non-zero size\n", si); + +/* + printf("Suite of test for snprintf:\n"); + printf("a_format\n"); + printf("printf() format\n"); + printf("snprintf() format\n\n"); +*/ +/* Checking the field widths */ + + printf("/%%ld %%ld/, 336, 336\n"); + snprintf(holder, sizeof holder, "/%ld %ld/\n", 336, 336); + asprintf(&h, "/%ld %ld/\n", 336, 336); + printf("/%ld %ld/\n", 336, 336); + printf("%s", holder); + printf("%s\n", h); + + printf("/%%d/, 336\n"); + snprintf(holder, sizeof holder, "/%d/\n", 336); + asprintf(&h, "/%d/\n", 336); + printf("/%d/\n", 336); + printf("%s", holder); + printf("%s\n", h); + + printf("/%%2d/, 336\n"); + snprintf(holder, sizeof holder, "/%2d/\n", 336); + asprintf(&h, "/%2d/\n", 336); + printf("/%2d/\n", 336); + printf("%s", holder); + printf("%s\n", h); + + printf("/%%10d/, 336\n"); + snprintf(holder, sizeof holder, "/%10d/\n", 336); + asprintf(&h, "/%10d/\n", 336); + printf("/%10d/\n", 336); + printf("%s", holder); + printf("%s\n", h); + + printf("/%%-10d/, 336\n"); + snprintf(holder, sizeof holder, "/%-10d/\n", 336); + asprintf(&h, "/%-10d/\n", 336); + printf("/%-10d/\n", 336); + printf("%s", holder); + printf("%s\n", h); + + +/* floating points */ + + printf("/%%f/, 1234.56\n"); + snprintf(holder, sizeof holder, "/%f/\n", 1234.56); + asprintf(&h, "/%f/\n", 1234.56); + printf("/%f/\n", 1234.56); + printf("%s", holder); + printf("%s\n", h); + + printf("/%%e/, 1234.56\n"); + snprintf(holder, sizeof holder, "/%e/\n", 1234.56); + asprintf(&h, "/%e/\n", 1234.56); + printf("/%e/\n", 1234.56); + printf("%s", holder); + printf("%s\n", h); + + printf("/%%4.2f/, 1234.56\n"); + snprintf(holder, sizeof holder, "/%4.2f/\n", 1234.56); + asprintf(&h, "/%4.2f/\n", 1234.56); + printf("/%4.2f/\n", 1234.56); + printf("%s", holder); + printf("%s\n", h); + + printf("/%%3.1f/, 1234.56\n"); + snprintf(holder, sizeof holder, "/%3.1f/\n", 1234.56); + asprintf(&h, "/%3.1f/\n", 1234.56); + printf("/%3.1f/\n", 1234.56); + printf("%s", holder); + printf("%s\n", h); + + printf("/%%10.3f/, 1234.56\n"); + snprintf(holder, sizeof holder, "/%10.3f/\n", 1234.56); + asprintf(&h, "/%10.3f/\n", 1234.56); + printf("/%10.3f/\n", 1234.56); + printf("%s", holder); + printf("%s\n", h); + + printf("/%%10.3e/, 1234.56\n"); + snprintf(holder, sizeof holder, "/%10.3e/\n", 1234.56); + asprintf(&h, "/%10.3e/\n", 1234.56); + printf("/%10.3e/\n", 1234.56); + printf("%s", holder); + printf("%s\n", h); + + printf("/%%+4.2f/, 1234.56\n"); + snprintf(holder, sizeof holder, "/%+4.2f/\n", 1234.56); + asprintf(&h, "/%+4.2f/\n", 1234.56); + printf("/%+4.2f/\n", 1234.56); + printf("%s", holder); + printf("%s\n", h); + + printf("/%%010.2f/, 1234.56\n"); + snprintf(holder, sizeof holder, "/%010.2f/\n", 1234.56); + asprintf(&h, "/%010.2f/\n", 1234.56); + printf("/%010.2f/\n", 1234.56); + printf("%s", holder); + printf("%s\n", h); + +#define BLURB "Outstanding acting !" +/* strings precisions */ + + printf("/%%2s/, \"%s\"\n", BLURB); + snprintf(holder, sizeof holder, "/%2s/\n", BLURB); + asprintf(&h, "/%2s/\n", BLURB); + printf("/%2s/\n", BLURB); + printf("%s", holder); + printf("%s\n", h); + + printf("/%%22s/ %s\n", BLURB); + snprintf(holder, sizeof holder, "/%22s/\n", BLURB); + asprintf(&h, "/%22s/\n", BLURB); + printf("/%22s/\n", BLURB); + printf("%s", holder); + printf("%s\n", h); + + printf("/%%22.5s/ %s\n", BLURB); + snprintf(holder, sizeof holder, "/%22.5s/\n", BLURB); + asprintf(&h, "/%22.5s/\n", BLURB); + printf("/%22.5s/\n", BLURB); + printf("%s", holder); + printf("%s\n", h); + + printf("/%%-22.5s/ %s\n", BLURB); + snprintf(holder, sizeof holder, "/%-22.5s/\n", BLURB); + asprintf(&h, "/%-22.5s/\n", BLURB); + printf("/%-22.5s/\n", BLURB); + printf("%s", holder); + printf("%s\n", h); + +/* see some flags */ + + printf("%%x %%X %%#x, 31, 31, 31\n"); + snprintf(holder, sizeof holder, "%x %X %#x\n", 31, 31, 31); + asprintf(&h, "%x %X %#x\n", 31, 31, 31); + printf("%x %X %#x\n", 31, 31, 31); + printf("%s", holder); + printf("%s\n", h); + + printf("**%%d**%% d**%% d**, 42, 42, -42\n"); + snprintf(holder, sizeof holder, "**%d**% d**% d**\n", 42, 42, -42); + asprintf(&h, "**%d**% d**% d**\n", 42, 42, -42); + printf("**%d**% d**% d**\n", 42, 42, -42); + printf("%s", holder); + printf("%s\n", h); + +/* other flags */ + + printf("/%%g/, 31.4\n"); + snprintf(holder, sizeof holder, "/%g/\n", 31.4); + asprintf(&h, "/%g/\n", 31.4); + printf("/%g/\n", 31.4); + printf("%s", holder); + printf("%s\n", h); + + printf("/%%.6g/, 31.4\n"); + snprintf(holder, sizeof holder, "/%.6g/\n", 31.4); + asprintf(&h, "/%.6g/\n", 31.4); + printf("/%.6g/\n", 31.4); + printf("%s", holder); + printf("%s\n", h); + + printf("/%%.1G/, 31.4\n"); + snprintf(holder, sizeof holder, "/%.1G/\n", 31.4); + asprintf(&h, "/%.1G/\n", 31.4); + printf("/%.1G/\n", 31.4); + printf("%s", holder); + printf("%s\n", h); + + printf("/%%.1G/, 3100000000.4\n"); + snprintf(holder, sizeof holder, "/%.1G/\n", 3100000000.4); + asprintf(&h, "/%.1G/\n", 3100000000.4); + printf("/%.1G/\n", 3100000000.4); + printf("%s", holder); + printf("%s\n", h); + + printf("abc%%n\n"); + printf("abc%n", &i); printf("%d\n", i); + snprintf(holder, sizeof holder, "abc%n", &i); + printf("%s", holder); printf("%d\n\n", i); + asprintf(&h, "abc%n", &i); + printf("%s", h); printf("%d\n\n", i); + + printf("%%*.*s --> 10.10\n"); + snprintf(holder, sizeof holder, "%*.*s\n", 10, 10, BLURB); + asprintf(&h, "%*.*s\n", 10, 10, BLURB); + printf("%*.*s\n", 10, 10, BLURB); + printf("%s", holder); + printf("%s\n", h); + + printf("%%%%%%%%\n"); + snprintf(holder, sizeof holder, "%%%%\n"); + asprintf(&h, "%%%%\n"); + printf("%%%%\n"); + printf("%s", holder); + printf("%s\n", h); + +#define BIG "Hello this is a too big string for the buffer" +/* printf("A buffer to small of 10, trying to put this:\n");*/ + printf("<%%>, %s\n", BIG); + i = snprintf(holder, 10, "%s\n", BIG); + i = asprintf(&h, "%s", BIG); + printf("<%s>\n", BIG); + printf("<%s>\n", holder); + printf("<%s>\n\n", h); + + printf ("<%%p> vsnprintf\n"); + i = snprintf(holder, 100, "%p", vsnprintf); + i = asprintf(&h, "%p", vsnprintf); + printf("<%p>\n", vsnprintf); + printf("<%s>\n", holder); + printf("<%s>\n\n", h); + + printf ("<%%lu> LONG_MAX+1\n"); + i = snprintf(holder, 100, "%lu", (unsigned long)(LONG_MAX)+1); + i = asprintf(&h, "%lu", (unsigned long)(LONG_MAX)+1); + printf("<%lu>\n", (unsigned long)(LONG_MAX)+1); + printf("<%s>\n", holder); + printf("<%s>\n\n", h); + +#ifdef HAVE_LONG_LONG + printf ("<%%llu> LLONG_MAX+1\n"); + i = snprintf(holder, 100, "%llu", (unsigned long long)(LLONG_MAX)+1); + i = asprintf(&h, "%llu", (unsigned long long)(LLONG_MAX)+1); + printf("<%llu>\n", (unsigned long long)(LLONG_MAX)+1); + printf("<%s>\n", holder); + printf("<%s>\n\n", h); +#endif + +#ifdef HAVE_LONG_DOUBLE + printf ("<%%6.2LE> 42.42\n"); + i = snprintf(holder, 100, "%6.2LE", (long double)42.42); + i = asprintf(&h, "%6.2LE", (long double)42.42); + printf ("<%6.2LE>\n", (long double)42.42); + printf ("<%s>\n", holder); + printf ("<%s>\n\n", h); +#endif + +#ifdef HAVE_PRINTF_A_FORMAT + printf ("<%%6.2A> 42.42\n"); + i = snprintf(holder, 100, "%6.2A", 42.42); + i = asprintf(&h, "%6.2A", 42.42); + printf ("<%6.2A>\n", 42.42); + printf ("<%s>\n", holder); + printf ("<%s>\n\n", h); + + printf ("<%%6.2LA> 42.42\n"); + i = snprintf(holder, 100, "%6.2LA", (long double)42.42); + i = asprintf(&h, "%6.2LA", (long double)42.42); + printf ("<%6.2LA>\n", (long double)42.42); + printf ("<%s>\n", holder); + printf ("<%s>\n\n", h); +#endif + + printf ("<%%.10240f> DBL_MAX\n"); + si = snprintf(holder, 100, "%.10240f", DBL_MAX); + ai = asprintf(&h, "%.10240f", DBL_MAX); + printf ("<%.10240f>\n", DBL_MAX); + printf ("<%d> <%s>\n", si, holder); + printf ("<%d> <%s>\n\n", ai, h); + + printf ("<%%.10240Lf> LDBL_MAX\n"); + si = snprintf(holder, 100, "%.10240Lf", (long double)LDBL_MAX); + ai = asprintf(&h, "%.10240Lf", (long double)LDBL_MAX); + printf ("<%.10240Lf>\n", (long double)LDBL_MAX); + printf ("<%d> <%s>\n", si, holder); + printf ("<%d> <%s>\n\n", ai, h); + + /* huh? */ + printf("/%%g/, 421.2345\n"); + snprintf(holder, sizeof holder, "/%g/\n", 421.2345); + asprintf(&h, "/%g/\n", 421.2345); + printf("/%g/\n", 421.2345); + printf("%s", holder); + printf("%s\n", h); + + printf("/%%g/, 4214.2345\n"); + snprintf(holder, sizeof holder, "/%g/\n", 4214.2345); + asprintf(&h, "/%g/\n", 4214.2345); + printf("/%g/\n", 4214.2345); + printf("%s", holder); + printf("%s\n", h); + + printf("/%%.5g/, 4214.2345\n"); + snprintf(holder, sizeof holder, "/%.5g/\n", 4214.2345); + asprintf(&h, "/%.5g/\n", 4214.2345); + printf("/%.5g/\n", 4214.2345); + printf("%s", holder); + printf("%s\n", h); + + printf("/%%.4g/, 4214.2345\n"); + snprintf(holder, sizeof holder, "/%.4g/\n", 4214.2345); + asprintf(&h, "/%.4g/\n", 4214.2345); + printf("/%.4g/\n", 4214.2345); + printf("%s", holder); + printf("%s\n", h); + + printf("/%%'ld %%'ld/, 12345, 1234567\n"); + snprintf(holder, sizeof holder, "/%'ld %'ld/\n", 12345, 1234567); + asprintf(&h, "/%'ld %'ld/\n", 12345, 1234567); + printf("/%'ld %'ld/\n", 12345, 1234567); + printf("%s", holder); + printf("%s\n", h); + + printf("/%%'ld %%'ld/, 336, 3336\n"); + snprintf(holder, sizeof holder, "/%'ld %'ld/\n", 336, 3336); + asprintf(&h, "/%'ld %'ld/\n", 336, 3336); + printf("/%'ld %'ld/\n", 336, 3336); + printf("%s", holder); + printf("%s\n", h); + + printf("/%%'ld %%'ld/, -42786, -142786\n"); + snprintf(holder, sizeof holder, "/%'ld %'ld/\n", -42786, -142786); + asprintf(&h, "/%'ld %'ld/\n", -42786, -142786); + printf("/%'ld %'ld/\n", -42786, -142786); + printf("%s", holder); + printf("%s\n", h); + + printf("/%%'f %%'f/, 421.2345, 421234.56789\n"); + snprintf(holder, sizeof holder, "/%'f %'f/\n", 421.2345, 421234.56789); + asprintf(&h, "/%'f %'f/\n", 421.2345, 421234.56789); + printf("/%'f %'f/\n", 421.2345, 421234.56789); + printf("%s", holder); + printf("%s\n", h); + + printf("/%%'f %%'f/, -421.2345, -421234.56789\n"); + snprintf(holder, sizeof holder, "/%'f %'f/\n", -421.2345, -421234.56789); + asprintf(&h, "/%'f %'f/\n", -421.2345, -421234.56789); + printf("/%'f %'f/\n", -421.2345, -421234.56789); + printf("%s", holder); + printf("%s\n", h); + + printf("/%%'g %%'g/, 421.2345, 421234.56789\n"); + snprintf(holder, sizeof holder, "/%'g %'g/\n", 421.2345, 421234.56789); + asprintf(&h, "/%'g %'g/\n", 421.2345, 421234.56789); + printf("/%'g %'g/\n", 421.2345, 421234.56789); + printf("%s", holder); + printf("%s\n", h); + + printf("/%%'g %%'g/, -421.2345, -421234.56789\n"); + snprintf(holder, sizeof holder, "/%'g %'g/\n", -421.2345, -421234.56789); + asprintf(&h, "/%'g %'g/\n", -421.2345, -421234.56789); + printf("/%'g %'g/\n", -421.2345, -421234.56789); + printf("%s", holder); + printf("%s\n", h); +#endif + + printf("/%%'g/, 4213455.8392\n"); + snprintf(holder, sizeof holder, "/%'g/\n", 4213455.8392); + asprintf(&h, "/%'g/\n", 4213455.8392); + printf("/%'g/\n", 4213455.8392); + printf("%s", holder); + printf("%s\n", h); + + exit (0); +} +#endif diff --git a/lib/sh/strtrans.c b/lib/sh/strtrans.c index 2a981356..3400eb8f 100644 --- a/lib/sh/strtrans.c +++ b/lib/sh/strtrans.c @@ -1,6 +1,6 @@ /* strtrans.c - Translate and untranslate strings with ANSI-C escape sequences. */ -/* Copyright (C) 2000 Free Software Foundation, Inc. +/* Copyright (C) 2000-2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. diff --git a/lib/sh/strtrans.c~ b/lib/sh/strtrans.c~ index 0d890b4c..2a981356 100644 --- a/lib/sh/strtrans.c~ +++ b/lib/sh/strtrans.c~ @@ -51,6 +51,9 @@ ansicstr (string, len, flags, sawc, rlen) { int c, temp, v; char *ret, *r, *s; +#if defined (HANDLE_MULTIBYTE) + char mbch[25]; /* 25 > MB_LEN_MAX, plus can handle 4-byte UTF-8 and large Unicode characters*/ +#endif if (string == 0 || *string == '\0') return ((char *)NULL); diff --git a/lib/sh/unicode.c b/lib/sh/unicode.c index 2bc0ea34..79a28217 100644 --- a/lib/sh/unicode.c +++ b/lib/sh/unicode.c @@ -1,6 +1,6 @@ /* unicode.c - functions to convert unicode characters */ -/* Copyright (C) 2006 Free Software Foundation, Inc. +/* Copyright (C) 2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. diff --git a/lib/sh/unicode.c~ b/lib/sh/unicode.c~ index f449561a..2bc0ea34 100644 --- a/lib/sh/unicode.c~ +++ b/lib/sh/unicode.c~ @@ -44,6 +44,10 @@ # endif #endif +#if !defined (STREQ) +# define STREQ(a, b) ((a)[0] == (b)[0] && strcmp ((a), (b)) == 0) +#endif /* !STREQ */ + #if defined (HAVE_LOCALE_CHARSET) extern const char *locale_charset __P((void)); #else @@ -1189,31 +1189,19 @@ simple_list1: simple_list1 AND_AND newline_list simple_list1 ; pipeline_command: pipeline - { $$ = $1; } - | BANG pipeline + { $$ = $1; } + | BANG pipeline_command { if ($2) - $2->flags |= CMD_INVERT_RETURN; + $2->flags ^= CMD_INVERT_RETURN; /* toggle */ $$ = $2; } - | timespec pipeline + | timespec pipeline_command { if ($2) $2->flags |= $1; $$ = $2; } - | timespec BANG pipeline - { - if ($3) - $3->flags |= $1|CMD_INVERT_RETURN; - $$ = $3; - } - | BANG timespec pipeline - { - if ($3) - $3->flags |= $2|CMD_INVERT_RETURN; - $$ = $3; - } | timespec list_terminator { ELEMENT x; @@ -1231,7 +1219,24 @@ pipeline_command: pipeline if ($2 == '\n') token_to_read = '\n'; } - + | BANG list_terminator + { + ELEMENT x; + + /* This is just as unclean. Posix says that `!' + by itself should be equivalent to `false'. + We cheat and push a + newline back if the list_terminator was a newline + to avoid the double-newline problem (one to + terminate this, one to terminate the command) */ + x.word = 0; + x.redirect = 0; + $$ = make_simple_command (x, (COMMAND *)NULL); + $$->flags |= CMD_INVERT_RETURN; + /* XXX - let's cheat and push a newline back */ + if ($2 == '\n') + token_to_read = '\n'; + } ; pipeline: pipeline '|' newline_list pipeline @@ -2663,6 +2668,9 @@ time_command_acceptable () case ELSE: case '{': /* } */ case '(': /* ) */ + case BANG: /* ! time pipeline */ + case TIME: /* time time pipeline */ + case TIMEOPT: /* time -p time pipeline */ return 1; default: return 0; @@ -2776,13 +2784,6 @@ special_case_tokens (tokstr) return (TIMEOPT); #endif -#if 0 -#if defined (COMMAND_TIMING) - if (STREQ (token, "time") && ((parser_state & PST_CASEPAT) == 0) && time_command_acceptable ()) - return (TIME); -#endif /* COMMAND_TIMING */ -#endif - #if defined (COND_COMMAND) /* [[ */ if ((parser_state & PST_CONDEXPR) && tokstr[0] == ']' && tokstr[1] == ']' && tokstr[2] == '\0') return (COND_END); @@ -3232,7 +3233,6 @@ parse_matched_pair (qc, open, close, lenp, flags) if MBTEST(ch == '\\') /* backslashes */ tflags |= LEX_PASSNEXT; -#if 0 /* XXX - bash-4.2 */ /* Based on which dolstate is currently in (param, op, or word), decide what the op is. We're really only concerned if it's % or #, so we can turn on a flag that says whether or not we should @@ -3250,17 +3250,14 @@ parse_matched_pair (qc, open, close, lenp, flags) else if MBTEST(dolbrace_state == DOLBRACE_OP && strchr ("#%^,~:-=?+/", ch) == 0) dolbrace_state = DOLBRACE_WORD; } -#endif -#if 0 /* XXX - bash-4.2 */ /* The big hammer. Single quotes aren't special in double quotes. The problem is that Posix used to say the single quotes are semi-special: within a double-quoted ${...} construct "an even number of unescaped double-quotes or single-quotes, if any, shall occur." */ - /* This was changed in Interp 221 */ - if MBTEST(/*posixly_correct && shell_compatibility_level > 41 &&*/ dolbrace_state == DOLBRACE_QUOTE && (flags & P_DQUOTE) && (flags & P_DOLBRACE) && ch == '\'') + /* This was changed in Austin Group Interp 221 */ + if MBTEST(posixly_correct && /*shell_compatibility_level > 41 &&*/ dolbrace_state != DOLBRACE_QUOTE && (flags & P_DQUOTE) && (flags & P_DOLBRACE) && ch == '\'') continue; -#endif /* Could also check open == '`' if we want to parse grouping constructs inside old-style command substitution. */ @@ -3370,13 +3367,12 @@ parse_comsub (qc, open, close, lenp, flags) char *ret, *nestret, *ttrans, *heredelim; int retind, retsize, rflags, hdlen; -#if 0 /* XXX - bash-4.2 -- jwm@horde.net */ - /* Assume $(( introduces arithmetic command and parse accordingly. */ + /* Posix interp 217 says arithmetic expressions have precedence, so + assume $(( introduces arithmetic expansion and parse accordingly. */ peekc = shell_getc (0); shell_ungetc (peekc); if (peekc == '(') return (parse_matched_pair (qc, open, close, lenp, 0)); -#endif /*itrace("parse_comsub: qc = `%c' open = %c close = %c", qc, open, close);*/ count = 1; @@ -5220,7 +5216,7 @@ decode_prompt_string (string) { t = strrchr (t_string, '/'); if (t) - strcpy (t_string, t + 1); + memmove (t_string, t + 1, strlen (t) - 1); } } #undef ROOT_PATH diff --git a/parse.y.save b/parse.y.save index b786fa7d..85787323 100644 --- a/parse.y.save +++ b/parse.y.save @@ -250,6 +250,9 @@ int extended_quote = 1; /* The number of lines read from input while creating the current command. */ int current_command_line_count; +/* The number of lines in a command saved while we run parse_and_execute */ +int saved_command_line_count; + /* The token that currently denotes the end of parse. */ int shell_eof_token; @@ -2305,7 +2308,7 @@ shell_getc (remove_quoted_newline) else { char *hdcs; - hdcs = history_delimiting_chars (); + hdcs = history_delimiting_chars (shell_input_line); if (hdcs && hdcs[0] == ';') maybe_add_history (shell_input_line); } @@ -3062,12 +3065,13 @@ tokword: * reprompting the user, if necessary, after reading a newline, and returning * correct error values if it reads EOF. */ -#define P_FIRSTCLOSE 0x01 -#define P_ALLOWESC 0x02 -#define P_DQUOTE 0x04 -#define P_COMMAND 0x08 /* parsing a command, so look for comments */ -#define P_BACKQUOTE 0x10 /* parsing a backquoted command substitution */ -#define P_ARRAYSUB 0x20 /* parsing a [...] array subscript for assignment */ +#define P_FIRSTCLOSE 0x0001 +#define P_ALLOWESC 0x0002 +#define P_DQUOTE 0x0004 +#define P_COMMAND 0x0008 /* parsing a command, so look for comments */ +#define P_BACKQUOTE 0x0010 /* parsing a backquoted command substitution */ +#define P_ARRAYSUB 0x0020 /* parsing a [...] array subscript for assignment */ +#define P_DOLBRACE 0x0040 /* parsing a ${...} construct */ /* Lexical state while parsing a grouping construct or $(...). */ #define LEX_WASDOL 0x001 @@ -3115,6 +3119,9 @@ parse_matched_pair (qc, open, close, lenp, flags) int nestlen, ttranslen, start_lineno; char *ret, *nestret, *ttrans; int retind, retsize, rflags; + int dolbrace_state; + + dolbrace_state = (flags & P_DOLBRACE) ? DOLBRACE_PARAM : 0; /*itrace("parse_matched_pair[%d]: open = %c close = %c flags = %d", line_number, open, close, flags);*/ count = 1; @@ -3225,12 +3232,33 @@ parse_matched_pair (qc, open, close, lenp, flags) if MBTEST(ch == '\\') /* backslashes */ tflags |= LEX_PASSNEXT; -#if 0 +#if 0 /* XXX - bash-4.2 */ + /* Based on which dolstate is currently in (param, op, or word), + decide what the op is. We're really only concerned if it's % or + #, so we can turn on a flag that says whether or not we should + treat single quotes as special when inside a double-quoted + ${...}. This logic must agree with subst.c:extract_dollar_brace_string + since they share the same defines. */ + if (flags & P_DOLBRACE) + { + if MBTEST(dolbrace_state == DOLBRACE_PARAM && ch == '%' && retind > 0) + dolbrace_state = DOLBRACE_QUOTE; + else if MBTEST(dolbrace_state == DOLBRACE_PARAM && ch == '#' && retind > 1) + dolbrace_state = DOLBRACE_QUOTE; + else if MBTEST(dolbrace_state == DOLBRACE_PARAM && strchr ("#%^,~:-=?+/", ch) != 0) + dolbrace_state = DOLBRACE_OP; + else if MBTEST(dolbrace_state == DOLBRACE_OP && strchr ("#%^,~:-=?+/", ch) == 0) + dolbrace_state = DOLBRACE_WORD; + } +#endif + +#if 0 /* XXX - bash-4.2 */ /* The big hammer. Single quotes aren't special in double quotes. The - problem is that Posix says the single quotes are semi-special: + problem is that Posix used to say the single quotes are semi-special: within a double-quoted ${...} construct "an even number of unescaped double-quotes or single-quotes, if any, shall occur." */ - if MBTEST(open == '{' && (flags & P_DQUOTE) && ch == '\'') /* } */ + /* This was changed in Interp 221 */ + if MBTEST(/*posixly_correct && shell_compatibility_level > 41 &&*/ dolbrace_state == DOLBRACE_QUOTE && (flags & P_DQUOTE) && (flags & P_DOLBRACE) && ch == '\'') continue; #endif @@ -3307,7 +3335,7 @@ parse_dollar_word: if (ch == '(') /* ) */ nestret = parse_comsub (0, '(', ')', &nestlen, (rflags|P_COMMAND) & ~P_DQUOTE); else if (ch == '{') /* } */ - nestret = parse_matched_pair (0, '{', '}', &nestlen, P_FIRSTCLOSE|rflags); + nestret = parse_matched_pair (0, '{', '}', &nestlen, P_FIRSTCLOSE|P_DOLBRACE|rflags); else if (ch == '[') /* ] */ nestret = parse_matched_pair (0, '[', ']', &nestlen, rflags); @@ -3750,7 +3778,7 @@ eof_error: if (ch == '(') /* ) */ nestret = parse_comsub (0, '(', ')', &nestlen, (rflags|P_COMMAND) & ~P_DQUOTE); else if (ch == '{') /* } */ - nestret = parse_matched_pair (0, '{', '}', &nestlen, P_FIRSTCLOSE|rflags); + nestret = parse_matched_pair (0, '{', '}', &nestlen, P_FIRSTCLOSE|P_DOLBRACE|rflags); else if (ch == '[') /* ] */ nestret = parse_matched_pair (0, '[', ']', &nestlen, rflags); @@ -4398,7 +4426,7 @@ read_token_word (character) ((peek_char == '{' || peek_char == '[') && character == '$')) /* ) ] } */ { if (peek_char == '{') /* } */ - ttok = parse_matched_pair (cd, '{', '}', &ttoklen, P_FIRSTCLOSE); + ttok = parse_matched_pair (cd, '{', '}', &ttoklen, P_FIRSTCLOSE|P_DOLBRACE); else if (peek_char == '(') /* ) */ { /* XXX - push and pop the `(' as a delimiter for use by @@ -4803,20 +4831,35 @@ static const int no_semi_successors[] = { /* If we are not within a delimited expression, try to be smart about which separators can be semi-colons and which must be newlines. Returns the string that should be added into the - history entry. */ + history entry. LINE is the line we're about to add; it helps + make some more intelligent decisions in certain cases. */ char * -history_delimiting_chars () +history_delimiting_chars (line) + const char *line; { + static int last_was_heredoc = 0; /* was the last entry the start of a here document? */ register int i; + if ((parser_state & PST_HEREDOC) == 0) + last_was_heredoc = 0; + if (dstack.delimiter_depth != 0) return ("\n"); /* We look for current_command_line_count == 2 because we are looking to add the first line of the body of the here document (the second line - of the command). */ + of the command). We also keep LAST_WAS_HEREDOC as a private sentinel + variable to note when we think we added the first line of a here doc + (the one with a "<<" somewhere in it) */ if (parser_state & PST_HEREDOC) - return (current_command_line_count == 2 ? "\n" : ""); + { + if (last_was_heredoc) + { + last_was_heredoc = 0; + return "\n"; + } + return (current_command_line_count == 2 ? "\n" : ""); + } /* First, handle some special cases. */ /*(*/ @@ -4839,6 +4882,15 @@ history_delimiting_chars () else if (token_before_that == WORD && two_tokens_ago == FUNCTION) return " "; /* function def using `function name' without `()' */ + /* If we're not in a here document, but we think we're about to parse one, + and we would otherwise return a `;', return a newline to delimit the + line with the here-doc delimiter */ + else if ((parser_state & PST_HEREDOC) == 0 && current_command_line_count > 1 && last_read_token == '\n' && strstr (line, "<<")) + { + last_was_heredoc = 1; + return "\n"; + } + else if (token_before_that == WORD && two_tokens_ago == FOR) { /* Tricky. `for i\nin ...' should not have a semicolon, but @@ -250,6 +250,9 @@ int extended_quote = 1; /* The number of lines read from input while creating the current command. */ int current_command_line_count; +/* The number of lines in a command saved while we run parse_and_execute */ +int saved_command_line_count; + /* The token that currently denotes the end of parse. */ int shell_eof_token; @@ -1186,31 +1189,19 @@ simple_list1: simple_list1 AND_AND newline_list simple_list1 ; pipeline_command: pipeline - { $$ = $1; } - | BANG pipeline + { $$ = $1; } + | BANG pipeline_command { if ($2) - $2->flags |= CMD_INVERT_RETURN; + $2->flags ^= CMD_INVERT_RETURN; /* toggle */ $$ = $2; } - | timespec pipeline + | timespec pipeline_command { if ($2) $2->flags |= $1; $$ = $2; } - | timespec BANG pipeline - { - if ($3) - $3->flags |= $1|CMD_INVERT_RETURN; - $$ = $3; - } - | BANG timespec pipeline - { - if ($3) - $3->flags |= $2|CMD_INVERT_RETURN; - $$ = $3; - } | timespec list_terminator { ELEMENT x; @@ -1228,7 +1219,24 @@ pipeline_command: pipeline if ($2 == '\n') token_to_read = '\n'; } - + | BANG list_terminator + { + ELEMENT x; + + /* This is just as unclean. Posix says that `!' + by itself should be equivalent to `false'. + We cheat and push a + newline back if the list_terminator was a newline + to avoid the double-newline problem (one to + terminate this, one to terminate the command) */ + x.word = 0; + x.redirect = 0; + $$ = make_simple_command (x, (COMMAND *)NULL); + $$->flags |= CMD_INVERT_RETURN; + /* XXX - let's cheat and push a newline back */ + if ($2 == '\n') + token_to_read = '\n'; + } ; pipeline: pipeline '|' newline_list pipeline @@ -2660,6 +2668,9 @@ time_command_acceptable () case ELSE: case '{': /* } */ case '(': /* ) */ + case BANG: /* ! time pipeline */ + case TIME: /* time time pipeline */ + case TIMEOPT: /* time -p time pipeline */ return 1; default: return 0; @@ -2773,13 +2784,6 @@ special_case_tokens (tokstr) return (TIMEOPT); #endif -#if 0 -#if defined (COMMAND_TIMING) - if (STREQ (token, "time") && ((parser_state & PST_CASEPAT) == 0) && time_command_acceptable ()) - return (TIME); -#endif /* COMMAND_TIMING */ -#endif - #if defined (COND_COMMAND) /* [[ */ if ((parser_state & PST_CONDEXPR) && tokstr[0] == ']' && tokstr[1] == ']' && tokstr[2] == '\0') return (COND_END); @@ -3229,7 +3233,6 @@ parse_matched_pair (qc, open, close, lenp, flags) if MBTEST(ch == '\\') /* backslashes */ tflags |= LEX_PASSNEXT; -#if 0 /* XXX - bash-4.2 */ /* Based on which dolstate is currently in (param, op, or word), decide what the op is. We're really only concerned if it's % or #, so we can turn on a flag that says whether or not we should @@ -3247,17 +3250,17 @@ parse_matched_pair (qc, open, close, lenp, flags) else if MBTEST(dolbrace_state == DOLBRACE_OP && strchr ("#%^,~:-=?+/", ch) == 0) dolbrace_state = DOLBRACE_WORD; } -#endif -#if 0 /* XXX - bash-4.2 */ /* The big hammer. Single quotes aren't special in double quotes. The problem is that Posix used to say the single quotes are semi-special: within a double-quoted ${...} construct "an even number of unescaped double-quotes or single-quotes, if any, shall occur." */ - /* This was changed in Interp 221 */ - if MBTEST(/*posixly_correct && shell_compatibility_level > 41 &&*/ dolbrace_state == DOLBRACE_QUOTE && (flags & P_DQUOTE) && (flags & P_DOLBRACE) && ch == '\'') + /* This was changed in Austin Group Interp 221 */ + if MBTEST(posixly_correct && /*shell_compatibility_level > 41 &&*/ dolbrace_state != DOLBRACE_QUOTE && (flags & P_DQUOTE) && (flags & P_DOLBRACE) && ch == '\'') +{ +itrace("parse_matched_pair: found single quote in double-quoted dolbrace"); continue; -#endif +} /* Could also check open == '`' if we want to parse grouping constructs inside old-style command substitution. */ @@ -3367,13 +3370,12 @@ parse_comsub (qc, open, close, lenp, flags) char *ret, *nestret, *ttrans, *heredelim; int retind, retsize, rflags, hdlen; -#if 0 /* XXX - bash-4.2 -- jwm@horde.net */ - /* Assume $(( introduces arithmetic command and parse accordingly. */ + /* Posix interp 217 says arithmetic expressions have precedence, so + assume $(( introduces arithmetic expansion and parse accordingly. */ peekc = shell_getc (0); shell_ungetc (peekc); if (peekc == '(') return (parse_matched_pair (qc, open, close, lenp, 0)); -#endif /*itrace("parse_comsub: qc = `%c' open = %c close = %c", qc, open, close);*/ count = 1; @@ -5217,7 +5219,7 @@ decode_prompt_string (string) { t = strrchr (t_string, '/'); if (t) - strcpy (t_string, t + 1); + memmove (t_string, t + 1, strlen (t) - 1); } } #undef ROOT_PATH @@ -1,7 +1,7 @@ /* parser.h -- Everything you wanted to know about the parser, but were afraid to ask. */ -/* Copyright (C) 1995, 2008,2009 Free Software Foundation, Inc. +/* Copyright (C) 1995-2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. @@ -61,4 +61,12 @@ struct dstack { int delimiter_space; }; +/* States we can be in while scanning a ${...} expansion. Shared between + parse.y and subst.c */ +#define DOLBRACE_PARAM 0x01 +#define DOLBRACE_OP 0x02 +#define DOLBRACE_WORD 0x04 + +#define DOLBRACE_QUOTE 0x40 + #endif /* _PARSER_H_ */ diff --git a/patchlevel.h b/patchlevel.h index eab75cf7..52efbd76 100644 --- a/patchlevel.h +++ b/patchlevel.h @@ -1,6 +1,6 @@ /* patchlevel.h -- current bash patch level */ -/* Copyright (C) 2001-2009 Free Software Foundation, Inc. +/* Copyright (C) 2001-2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. diff --git a/patchlevel.h~ b/patchlevel.h~ index c8f400f6..eab75cf7 100644 --- a/patchlevel.h~ +++ b/patchlevel.h~ @@ -25,6 +25,6 @@ regexp `^#define[ ]*PATCHLEVEL', since that's what support/mkversion.sh looks for to find the patch level (for the sccs version string). */ -#define PATCHLEVEL 1 +#define PATCHLEVEL 7 #endif /* _PATCHLEVEL_H_ */ diff --git a/print_cmd.c b/print_cmd.c index 53d5adb1..0d646ec5 100644 --- a/print_cmd.c +++ b/print_cmd.c @@ -1,6 +1,6 @@ /* print_command -- A way to make readable commands from a command tree. */ -/* Copyright (C) 1989-2009 Free Software Foundation, Inc. +/* Copyright (C) 1989-2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. diff --git a/print_cmd.c~ b/print_cmd.c~ index fa031744..53d5adb1 100644 --- a/print_cmd.c~ +++ b/print_cmd.c~ @@ -113,6 +113,12 @@ FILE *xtrace_fp = 0; #define CHECK_XTRACE_FP xtrace_fp = (xtrace_fp ? xtrace_fp : stderr) +#define PRINT_DEFERRED_HEREDOCS(x) \ + do { \ + if (deferred_heredocs) \ + print_deferred_heredocs (x); \ + } while (0) + /* Non-zero means the stuff being printed is inside of a function def. */ static int inside_function_def; static int skip_this_indent; @@ -293,8 +299,7 @@ make_command_string_internal (command) } make_command_string_internal (command->value.Connection->second); - if (deferred_heredocs) - print_deferred_heredocs (""); + PRINT_DEFERRED_HEREDOCS (""); printing_connection--; break; @@ -560,15 +565,15 @@ print_for_command (for_command) FOR_COM *for_command; { print_for_command_head (for_command); - cprintf (";"); newline ("do\n"); + indentation += indentation_amount; make_command_string_internal (for_command->action); - if (deferred_heredocs) - print_deferred_heredocs (""); + PRINT_DEFERRED_HEREDOCS (""); semicolon (); indentation -= indentation_amount; + newline ("done"); } @@ -622,6 +627,7 @@ print_select_command (select_command) newline ("do\n"); indentation += indentation_amount; make_command_string_internal (select_command->action); + PRINT_DEFERRED_HEREDOCS (""); semicolon (); indentation -= indentation_amount; newline ("done"); @@ -705,6 +711,7 @@ print_case_clauses (clauses) indentation += indentation_amount; make_command_string_internal (clauses->action); indentation -= indentation_amount; + PRINT_DEFERRED_HEREDOCS (""); if (clauses->flags & CASEPAT_FALLTHROUGH) newline (";&"); else if (clauses->flags & CASEPAT_TESTNEXT) @@ -738,10 +745,12 @@ print_until_or_while (while_command, which) cprintf ("%s ", which); skip_this_indent++; make_command_string_internal (while_command->test); + PRINT_DEFERRED_HEREDOCS (""); semicolon (); cprintf (" do\n"); /* was newline ("do\n"); */ indentation += indentation_amount; make_command_string_internal (while_command->action); + PRINT_DEFERRED_HEREDOCS (""); indentation -= indentation_amount; semicolon (); newline ("done"); @@ -758,6 +767,7 @@ print_if_command (if_command) cprintf (" then\n"); indentation += indentation_amount; make_command_string_internal (if_command->true_case); + PRINT_DEFERRED_HEREDOCS (""); indentation -= indentation_amount; if (if_command->false_case) @@ -766,6 +776,7 @@ print_if_command (if_command) newline ("else\n"); indentation += indentation_amount; make_command_string_internal (if_command->false_case); + PRINT_DEFERRED_HEREDOCS (""); indentation -= indentation_amount; } semicolon (); @@ -945,7 +956,7 @@ print_deferred_heredocs (cstring) cprintf (" "); print_heredoc_header (hdtail); } - if (cstring[0] != ';' || cstring[1]) + if (cstring[0] && (cstring[0] != ';' || cstring[1])) cprintf ("%s", cstring); if (deferred_heredocs) cprintf ("\n"); @@ -1,6 +1,6 @@ /* shell.c -- GNU's idea of the POSIX shell specification. */ -/* Copyright (C) 1987-2009 Free Software Foundation, Inc. +/* Copyright (C) 1987-2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. @@ -1417,7 +1417,7 @@ open_shell_script (script_name) free (dollar_vars[0]); dollar_vars[0] = exec_argv0 ? savestring (exec_argv0) : savestring (script_name); - if (exec_argv[0]) + if (exec_argv0) { free (exec_argv0); exec_argv0 = (char *)NULL; @@ -1,6 +1,6 @@ /* sig.c - interface for shell signal handlers and signal initialization. */ -/* Copyright (C) 1994-2009 Free Software Foundation, Inc. +/* Copyright (C) 1994-2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. @@ -55,7 +55,7 @@ extern int last_command_exit_value; extern int last_command_exit_signal; extern int return_catch_flag; -extern int loop_level, continuing, breaking; +extern int loop_level, continuing, breaking, funcnest; extern int executing_list; extern int comsub_ignore_return; extern int parse_and_execute_level, shell_initialized; @@ -372,7 +372,7 @@ top_level_cleanup () #endif /* PROCESS_SUBSTITUTION */ run_unwind_protects (); - loop_level = continuing = breaking = 0; + loop_level = continuing = breaking = funcnest = 0; executing_list = comsub_ignore_return = return_catch_flag = 0; } @@ -423,7 +423,7 @@ throw_to_top_level () #endif /* PROCESS_SUBSTITUTION */ run_unwind_protects (); - loop_level = continuing = breaking = 0; + loop_level = continuing = breaking = funcnest = 0; executing_list = comsub_ignore_return = return_catch_flag = 0; if (interactive && print_newline) @@ -541,7 +541,7 @@ termsig_handler (sig) #endif /* PROCESS_SUBSTITUTION */ /* Reset execution context */ - loop_level = continuing = breaking = 0; + loop_level = continuing = breaking = funcnest = 0; executing_list = comsub_ignore_return = return_catch_flag = 0; run_exit_trap (); @@ -55,7 +55,7 @@ extern int last_command_exit_value; extern int last_command_exit_signal; extern int return_catch_flag; -extern int loop_level, continuing, breaking; +extern int loop_level, continuing, breaking, funcnest; extern int executing_list; extern int comsub_ignore_return; extern int parse_and_execute_level, shell_initialized; @@ -372,7 +372,7 @@ top_level_cleanup () #endif /* PROCESS_SUBSTITUTION */ run_unwind_protects (); - loop_level = continuing = breaking = 0; + loop_level = continuing = breaking = funcnest = 0; executing_list = comsub_ignore_return = return_catch_flag = 0; } @@ -423,7 +423,7 @@ throw_to_top_level () #endif /* PROCESS_SUBSTITUTION */ run_unwind_protects (); - loop_level = continuing = breaking = 0; + loop_level = continuing = breaking = funcnest = 0; executing_list = comsub_ignore_return = return_catch_flag = 0; if (interactive && print_newline) @@ -541,7 +541,7 @@ termsig_handler (sig) #endif /* PROCESS_SUBSTITUTION */ /* Reset execution context */ - loop_level = continuing = breaking = 0; + loop_level = continuing = breaking = funcnest = 0; executing_list = comsub_ignore_return = return_catch_flag = 0; run_exit_trap (); @@ -655,12 +655,13 @@ set_signal_handler (sig, handler) act.sa_handler = handler; act.sa_flags = 0; -#if 0 - if (sig == SIGALRM) - act.sa_flags |= SA_INTERRUPT; /* XXX */ - else + + /* XXX - bash-4.2 */ + /* We don't want a child death to interrupt interruptible system calls, even + if we take the time to reap children */ + if (sig == SIGCHLD) act.sa_flags |= SA_RESTART; /* XXX */ -#endif + sigemptyset (&act.sa_mask); sigemptyset (&oact.sa_mask); sigaction (sig, &act, &oact); @@ -4,7 +4,7 @@ /* ``Have a little faith, there's magic in the night. You ain't a beauty, but, hey, you're alright.'' */ -/* Copyright (C) 1987-2009 Free Software Foundation, Inc. +/* Copyright (C) 1987-2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. @@ -1266,9 +1266,8 @@ extract_delimited_string (string, sindex, opener, alt_opener, closer, flags) continue; } -#if 0 - /* Process a nested command substitution, but only if we're parsing a - command substitution. XXX - bash-4.2 */ + /* Process a nested command substitution, but only if we're parsing an + arithmetic substitution. */ if ((flags & SX_COMMAND) && string[i] == '$' && string[i+1] == LPAREN) { si = i + 2; @@ -1276,7 +1275,6 @@ extract_delimited_string (string, sindex, opener, alt_opener, closer, flags) i = si + 1; continue; } -#endif /* Process a nested OPENER. */ if (STREQN (string + i, opener, len_opener)) @@ -1440,7 +1438,7 @@ extract_dollar_brace_string (string, sindex, quoted, flags) continue; } -#if 1 +#if 0 /* Pass the contents of single-quoted and double-quoted strings through verbatim. */ if (c == '\'' || c == '"') @@ -1464,7 +1462,7 @@ extract_dollar_brace_string (string, sindex, quoted, flags) if (c == '\'') { /*itrace("extract_dollar_brace_string: c == single quote flags = %d quoted = %d dolbrace_state = %d", flags, quoted, dolbrace_state);*/ - if (posixly_correct && shell_compatibility_level > 41 && dolbrace_state != DOLBRACE_QUOTE) + if (posixly_correct && /*shell_compatibility_level > 41 &&*/ dolbrace_state != DOLBRACE_QUOTE) ADVANCE_CHAR (string, slen, i); else { @@ -5367,11 +5365,7 @@ command_substitute (string, quoted) pipline, so what we are concerned about is whether or not that pipeline was started in the background. A pipeline started in the background should never get the tty back here. */ -#if 0 - if (interactive && pipeline_pgrp != (pid_t)0 && pipeline_pgrp != last_asynchronous_pid) -#else if (interactive && pipeline_pgrp != (pid_t)0 && (subshell_environment & SUBSHELL_ASYNC) == 0) -#endif give_terminal_to (pipeline_pgrp, 0); #endif /* JOB_CONTROL */ @@ -5747,16 +5741,11 @@ parameter_brace_expand_rhs (name, value, c, quoted, qdollaratp, hasdollarat) else #endif /* ARRAY_VARS */ bind_variable (name, t1, 0); -#if 1 - free (t1); - w->word = temp; -#else /* XXX - bash-4.2 */ /* From Posix group discussion Feb-March 2010. Issue 7 0000221 */ free (temp); w->word = t1; -#endif return w; } @@ -7184,8 +7173,9 @@ parameter_brace_expand (string, indexp, quoted, pflags, quoted_dollar_atp, conta FREE (temp); if (value) { - /* XXX - bash-4.2 */ - /* From Posix discussion on austin-group list. Issue 221 */ + /* From Posix discussion on austin-group list. Issue 221 + requires that backslashes escaping `}' inside + double-quoted ${...} be removed. */ if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) quoted |= Q_DOLBRACE; ret = parameter_brace_expand_rhs (name, value, c, @@ -7231,8 +7221,9 @@ parameter_brace_expand (string, indexp, quoted, pflags, quoted_dollar_atp, conta if (contains_dollar_at) *contains_dollar_at = 0; - /* XXX - bash-4.2 */ - /* From Posix discussion on austin-group list. Issue 221 */ + /* From Posix discussion on austin-group list. Issue 221 requires + that backslashes escaping `}' inside double-quoted ${...} be + removed. */ if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) quoted |= Q_DOLBRACE; ret = parameter_brace_expand_rhs (name, value, c, quoted, @@ -8051,7 +8042,6 @@ add_string: else tflag = 0; - /* XXX - bash-4.2 */ /* From Posix discussion on austin-group list: Backslash escaping a } in ${...} is removed. Issue 0000221 */ if ((quoted & Q_DOLBRACE) && c == RBRACE) @@ -1266,9 +1266,8 @@ extract_delimited_string (string, sindex, opener, alt_opener, closer, flags) continue; } -#if 0 - /* Process a nested command substitution, but only if we're parsing a - command substitution. XXX - bash-4.2 */ + /* Process a nested command substitution, but only if we're parsing an + arithmetic substitution. */ if ((flags & SX_COMMAND) && string[i] == '$' && string[i+1] == LPAREN) { si = i + 2; @@ -1276,7 +1275,6 @@ extract_delimited_string (string, sindex, opener, alt_opener, closer, flags) i = si + 1; continue; } -#endif /* Process a nested OPENER. */ if (STREQN (string + i, opener, len_opener)) @@ -1440,7 +1438,7 @@ extract_dollar_brace_string (string, sindex, quoted, flags) continue; } -#if 1 +#if 0 /* Pass the contents of single-quoted and double-quoted strings through verbatim. */ if (c == '\'' || c == '"') @@ -1464,7 +1462,7 @@ extract_dollar_brace_string (string, sindex, quoted, flags) if (c == '\'') { /*itrace("extract_dollar_brace_string: c == single quote flags = %d quoted = %d dolbrace_state = %d", flags, quoted, dolbrace_state);*/ - if (posixly_correct && shell_compatibility_level > 41 && dolbrace_state != DOLBRACE_QUOTE) + if (posixly_correct && /*shell_compatibility_level > 41 &&*/ dolbrace_state != DOLBRACE_QUOTE) ADVANCE_CHAR (string, slen, i); else { @@ -4677,25 +4675,17 @@ close_new_fifos (list, lsize) if (list == 0) { -itrace("close_new_fifos: list == 0, calling unlink_fifo_list"); unlink_fifo_list (); return; } for (i = 0; i < lsize; i++) if (list[i] == 0 && i < fifo_list_size && fifo_list[i].proc != -1) -{ -itrace("close_new_fifos: closing %d", i); unlink_fifo (i); -} for (i = lsize; i < fifo_list_size; i++) -{ -if (fifo_list[i].proc != -1) - itrace("close_new_fifos: closing %d", i); unlink_fifo (i); } -} int fifos_pending () @@ -4758,7 +4748,6 @@ static void add_fifo_list (fd) int fd; { -itrace("add_fifo_list: adding %d", fd); if (dev_fd_list == 0 || fd >= totfds) { int ofds; @@ -4830,25 +4819,17 @@ close_new_fifos (list, lsize) if (list == 0) { -itrace("close_new_fifos: list == 0, calling unlink_fifo_list"); unlink_fifo_list (); return; } for (i = 0; i < lsize; i++) if (list[i] == 0 && i < totfds && dev_fd_list[i]) -{ -itrace("close_new_fifos: closing %d", i); unlink_fifo (i); -} for (i = lsize; i < totfds; i++) -{ -if (dev_fd_list[i]) - itrace("close_new_fifos: closing %d", i); unlink_fifo (i); } -} #if defined (NOTDEF) print_dev_fd_list () @@ -5384,11 +5365,7 @@ command_substitute (string, quoted) pipline, so what we are concerned about is whether or not that pipeline was started in the background. A pipeline started in the background should never get the tty back here. */ -#if 0 - if (interactive && pipeline_pgrp != (pid_t)0 && pipeline_pgrp != last_asynchronous_pid) -#else if (interactive && pipeline_pgrp != (pid_t)0 && (subshell_environment & SUBSHELL_ASYNC) == 0) -#endif give_terminal_to (pipeline_pgrp, 0); #endif /* JOB_CONTROL */ @@ -5764,16 +5741,11 @@ parameter_brace_expand_rhs (name, value, c, quoted, qdollaratp, hasdollarat) else #endif /* ARRAY_VARS */ bind_variable (name, t1, 0); -#if 1 - free (t1); - w->word = temp; -#else /* XXX - bash-4.2 */ /* From Posix group discussion Feb-March 2010. Issue 7 0000221 */ free (temp); w->word = t1; -#endif return w; } @@ -7201,8 +7173,9 @@ parameter_brace_expand (string, indexp, quoted, pflags, quoted_dollar_atp, conta FREE (temp); if (value) { - /* XXX - bash-4.2 */ - /* From Posix discussion on austin-group list. Issue 221 */ + /* From Posix discussion on austin-group list. Issue 221 + requires that backslashes escaping `}' inside + double-quoted ${...} be removed. */ if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) quoted |= Q_DOLBRACE; ret = parameter_brace_expand_rhs (name, value, c, @@ -7248,8 +7221,9 @@ parameter_brace_expand (string, indexp, quoted, pflags, quoted_dollar_atp, conta if (contains_dollar_at) *contains_dollar_at = 0; - /* XXX - bash-4.2 */ - /* From Posix discussion on austin-group list. Issue 221 */ + /* From Posix discussion on austin-group list. Issue 221 requires + that backslashes escaping `}' inside double-quoted ${...} be + removed. */ if (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)) quoted |= Q_DOLBRACE; ret = parameter_brace_expand_rhs (name, value, c, quoted, @@ -8068,7 +8042,6 @@ add_string: else tflag = 0; - /* XXX - bash-4.2 */ /* From Posix discussion on austin-group list: Backslash escaping a } in ${...} is removed. Issue 0000221 */ if ((quoted & Q_DOLBRACE) && c == RBRACE) @@ -1,6 +1,6 @@ /* subst.h -- Names of externally visible functions in subst.c. */ -/* Copyright (C) 1993-2009 Free Software Foundation, Inc. +/* Copyright (C) 1993-2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. @@ -253,12 +253,14 @@ extern WORD_LIST *expand_words_shellexp __P((WORD_LIST *)); extern WORD_DESC *command_substitute __P((char *, int)); extern char *pat_subst __P((char *, char *, char *, int)); -extern char *copy_fifo_list __P((int *)); extern int fifos_pending __P((void)); extern int num_fifos __P((void)); extern void unlink_fifo_list __P((void)); extern void unlink_fifo __P((int)); +extern char *copy_fifo_list __P((int *)); +extern void unlink_new_fifos __P((char *, int)); + extern WORD_LIST *list_string_with_quotes __P((char *)); #if defined (ARRAY_VARS) @@ -2,7 +2,7 @@ /* Modified to run with the GNU shell Apr 25, 1988 by bfox. */ -/* Copyright (C) 1987-2009 Free Software Foundation, Inc. +/* Copyright (C) 1987-2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. @@ -498,6 +498,7 @@ unary_test (op, arg) { intmax_t r; struct stat stat_buf; + SHELL_VAR *v; switch (op[1]) { @@ -599,6 +600,12 @@ unary_test (op, arg) case 'o': /* True if option `arg' is set. */ return (minus_o_option_value (arg) == 1); + + case 'v': + v = find_variable (arg); + if (v == 0) + return (FALSE); + return (var_isset (v) ? TRUE : FALSE); } /* We can't actually get here, but this shuts up gcc. */ @@ -672,7 +679,7 @@ test_unop (op) case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': case 'h': case 'k': case 'n': case 'o': case 'p': case 'r': case 's': case 't': - case 'u': case 'w': case 'x': case 'z': + case 'u': case 'v': case 'w': case 'x': case 'z': case 'G': case 'L': case 'O': case 'S': case 'N': return (1); } diff --git a/test.c~ b/test.c~ new file mode 100644 index 00000000..190e64d3 --- /dev/null +++ b/test.c~ @@ -0,0 +1,837 @@ +/* test.c - GNU test program (ksb and mjb) */ + +/* Modified to run with the GNU shell Apr 25, 1988 by bfox. */ + +/* Copyright (C) 1987-2009 Free Software Foundation, Inc. + + This file is part of GNU Bash, the Bourne Again SHell. + + Bash 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 3 of the License, or + (at your option) any later version. + + Bash 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 Bash. If not, see <http://www.gnu.org/licenses/>. +*/ + +/* Define PATTERN_MATCHING to get the csh-like =~ and !~ pattern-matching + binary operators. */ +/* #define PATTERN_MATCHING */ + +#if defined (HAVE_CONFIG_H) +# include <config.h> +#endif + +#include <stdio.h> + +#include "bashtypes.h" + +#if !defined (HAVE_LIMITS_H) +# include <sys/param.h> +#endif + +#if defined (HAVE_UNISTD_H) +# include <unistd.h> +#endif + +#include <errno.h> +#if !defined (errno) +extern int errno; +#endif /* !errno */ + +#if !defined (_POSIX_VERSION) && defined (HAVE_SYS_FILE_H) +# include <sys/file.h> +#endif /* !_POSIX_VERSION */ +#include "posixstat.h" +#include "filecntl.h" + +#include "bashintl.h" + +#include "shell.h" +#include "pathexp.h" +#include "test.h" +#include "builtins/common.h" + +#include <glob/strmatch.h> + +#if !defined (STRLEN) +# define STRLEN(s) ((s)[0] ? ((s)[1] ? ((s)[2] ? strlen(s) : 2) : 1) : 0) +#endif + +#if !defined (STREQ) +# define STREQ(a, b) ((a)[0] == (b)[0] && strcmp ((a), (b)) == 0) +#endif /* !STREQ */ +#define STRCOLLEQ(a, b) ((a)[0] == (b)[0] && strcoll ((a), (b)) == 0) + +#if !defined (R_OK) +#define R_OK 4 +#define W_OK 2 +#define X_OK 1 +#define F_OK 0 +#endif /* R_OK */ + +#define EQ 0 +#define NE 1 +#define LT 2 +#define GT 3 +#define LE 4 +#define GE 5 + +#define NT 0 +#define OT 1 +#define EF 2 + +/* The following few defines control the truth and false output of each stage. + TRUE and FALSE are what we use to compute the final output value. + SHELL_BOOLEAN is the form which returns truth or falseness in shell terms. + Default is TRUE = 1, FALSE = 0, SHELL_BOOLEAN = (!value). */ +#define TRUE 1 +#define FALSE 0 +#define SHELL_BOOLEAN(value) (!(value)) + +#define TEST_ERREXIT_STATUS 2 + +static procenv_t test_exit_buf; +static int test_error_return; +#define test_exit(val) \ + do { test_error_return = val; longjmp (test_exit_buf, 1); } while (0) + +extern int sh_stat __P((const char *, struct stat *)); + +static int pos; /* The offset of the current argument in ARGV. */ +static int argc; /* The number of arguments present in ARGV. */ +static char **argv; /* The argument list. */ +static int noeval; + +static void test_syntax_error __P((char *, char *)) __attribute__((__noreturn__)); +static void beyond __P((void)) __attribute__((__noreturn__)); +static void integer_expected_error __P((char *)) __attribute__((__noreturn__)); + +static int unary_operator __P((void)); +static int binary_operator __P((void)); +static int two_arguments __P((void)); +static int three_arguments __P((void)); +static int posixtest __P((void)); + +static int expr __P((void)); +static int term __P((void)); +static int and __P((void)); +static int or __P((void)); + +static int filecomp __P((char *, char *, int)); +static int arithcomp __P((char *, char *, int, int)); +static int patcomp __P((char *, char *, int)); + +static void +test_syntax_error (format, arg) + char *format, *arg; +{ + builtin_error (format, arg); + test_exit (TEST_ERREXIT_STATUS); +} + +/* + * beyond - call when we're beyond the end of the argument list (an + * error condition) + */ +static void +beyond () +{ + test_syntax_error (_("argument expected"), (char *)NULL); +} + +/* Syntax error for when an integer argument was expected, but + something else was found. */ +static void +integer_expected_error (pch) + char *pch; +{ + test_syntax_error (_("%s: integer expression expected"), pch); +} + +/* Increment our position in the argument list. Check that we're not + past the end of the argument list. This check is supressed if the + argument is FALSE. Made a macro for efficiency. */ +#define advance(f) do { ++pos; if (f && pos >= argc) beyond (); } while (0) +#define unary_advance() do { advance (1); ++pos; } while (0) + +/* + * expr: + * or + */ +static int +expr () +{ + if (pos >= argc) + beyond (); + + return (FALSE ^ or ()); /* Same with this. */ +} + +/* + * or: + * and + * and '-o' or + */ +static int +or () +{ + int value, v2; + + value = and (); + if (pos < argc && argv[pos][0] == '-' && argv[pos][1] == 'o' && !argv[pos][2]) + { + advance (0); + v2 = or (); + return (value || v2); + } + + return (value); +} + +/* + * and: + * term + * term '-a' and + */ +static int +and () +{ + int value, v2; + + value = term (); + if (pos < argc && argv[pos][0] == '-' && argv[pos][1] == 'a' && !argv[pos][2]) + { + advance (0); + v2 = and (); + return (value && v2); + } + return (value); +} + +/* + * term - parse a term and return 1 or 0 depending on whether the term + * evaluates to true or false, respectively. + * + * term ::= + * '-'('a'|'b'|'c'|'d'|'e'|'f'|'g'|'h'|'k'|'p'|'r'|'s'|'u'|'w'|'x') filename + * '-'('G'|'L'|'O'|'S'|'N') filename + * '-t' [int] + * '-'('z'|'n') string + * '-o' option + * string + * string ('!='|'='|'==') string + * <int> '-'(eq|ne|le|lt|ge|gt) <int> + * file '-'(nt|ot|ef) file + * '(' <expr> ')' + * int ::= + * positive and negative integers + */ +static int +term () +{ + int value; + + if (pos >= argc) + beyond (); + + /* Deal with leading `not's. */ + if (argv[pos][0] == '!' && argv[pos][1] == '\0') + { + value = 0; + while (pos < argc && argv[pos][0] == '!' && argv[pos][1] == '\0') + { + advance (1); + value = 1 - value; + } + + return (value ? !term() : term()); + } + + /* A paren-bracketed argument. */ + if (argv[pos][0] == '(' && argv[pos][1] == '\0') /* ) */ + { + advance (1); + value = expr (); + if (argv[pos] == 0) /* ( */ + test_syntax_error (_("`)' expected"), (char *)NULL); + else if (argv[pos][0] != ')' || argv[pos][1]) /* ( */ + test_syntax_error (_("`)' expected, found %s"), argv[pos]); + advance (0); + return (value); + } + + /* are there enough arguments left that this could be dyadic? */ + if ((pos + 3 <= argc) && test_binop (argv[pos + 1])) + value = binary_operator (); + + /* Might be a switch type argument */ + else if (argv[pos][0] == '-' && argv[pos][2] == '\0') + { + if (test_unop (argv[pos])) + value = unary_operator (); + else + test_syntax_error (_("%s: unary operator expected"), argv[pos]); + } + else + { + value = argv[pos][0] != '\0'; + advance (0); + } + + return (value); +} + +static int +filecomp (s, t, op) + char *s, *t; + int op; +{ + struct stat st1, st2; + int r1, r2; + + if ((r1 = sh_stat (s, &st1)) < 0) + { + if (op == EF) + return (FALSE); + } + if ((r2 = sh_stat (t, &st2)) < 0) + { + if (op == EF) + return (FALSE); + } + + switch (op) + { + case OT: return (r1 < r2 || (r2 == 0 && st1.st_mtime < st2.st_mtime)); + case NT: return (r1 > r2 || (r1 == 0 && st1.st_mtime > st2.st_mtime)); + case EF: return (same_file (s, t, &st1, &st2)); + } + return (FALSE); +} + +static int +arithcomp (s, t, op, flags) + char *s, *t; + int op, flags; +{ + intmax_t l, r; + int expok; + + if (flags & TEST_ARITHEXP) + { + l = evalexp (s, &expok); + if (expok == 0) + return (FALSE); /* should probably longjmp here */ + r = evalexp (t, &expok); + if (expok == 0) + return (FALSE); /* ditto */ + } + else + { + if (legal_number (s, &l) == 0) + integer_expected_error (s); + if (legal_number (t, &r) == 0) + integer_expected_error (t); + } + + switch (op) + { + case EQ: return (l == r); + case NE: return (l != r); + case LT: return (l < r); + case GT: return (l > r); + case LE: return (l <= r); + case GE: return (l >= r); + } + + return (FALSE); +} + +static int +patcomp (string, pat, op) + char *string, *pat; + int op; +{ + int m; + + m = strmatch (pat, string, FNMATCH_EXTFLAG|FNMATCH_IGNCASE); + return ((op == EQ) ? (m == 0) : (m != 0)); +} + +int +binary_test (op, arg1, arg2, flags) + char *op, *arg1, *arg2; + int flags; +{ + int patmatch; + + patmatch = (flags & TEST_PATMATCH); + + if (op[0] == '=' && (op[1] == '\0' || (op[1] == '=' && op[2] == '\0'))) + return (patmatch ? patcomp (arg1, arg2, EQ) : STREQ (arg1, arg2)); + else if ((op[0] == '>' || op[0] == '<') && op[1] == '\0') + { + if (shell_compatibility_level > 40 && flags & TEST_LOCALE) + return ((op[0] == '>') ? (strcoll (arg1, arg2) > 0) : (strcoll (arg1, arg2) < 0)); + else + return ((op[0] == '>') ? (strcmp (arg1, arg2) > 0) : (strcmp (arg1, arg2) < 0)); + } + else if (op[0] == '!' && op[1] == '=' && op[2] == '\0') + return (patmatch ? patcomp (arg1, arg2, NE) : (STREQ (arg1, arg2) == 0)); + + + else if (op[2] == 't') + { + switch (op[1]) + { + case 'n': return (filecomp (arg1, arg2, NT)); /* -nt */ + case 'o': return (filecomp (arg1, arg2, OT)); /* -ot */ + case 'l': return (arithcomp (arg1, arg2, LT, flags)); /* -lt */ + case 'g': return (arithcomp (arg1, arg2, GT, flags)); /* -gt */ + } + } + else if (op[1] == 'e') + { + switch (op[2]) + { + case 'f': return (filecomp (arg1, arg2, EF)); /* -ef */ + case 'q': return (arithcomp (arg1, arg2, EQ, flags)); /* -eq */ + } + } + else if (op[2] == 'e') + { + switch (op[1]) + { + case 'n': return (arithcomp (arg1, arg2, NE, flags)); /* -ne */ + case 'g': return (arithcomp (arg1, arg2, GE, flags)); /* -ge */ + case 'l': return (arithcomp (arg1, arg2, LE, flags)); /* -le */ + } + } + + return (FALSE); /* should never get here */ +} + + +static int +binary_operator () +{ + int value; + char *w; + + w = argv[pos + 1]; + if ((w[0] == '=' && (w[1] == '\0' || (w[1] == '=' && w[2] == '\0'))) || /* =, == */ + ((w[0] == '>' || w[0] == '<') && w[1] == '\0') || /* <, > */ + (w[0] == '!' && w[1] == '=' && w[2] == '\0')) /* != */ + { + value = binary_test (w, argv[pos], argv[pos + 2], 0); + pos += 3; + return (value); + } + +#if defined (PATTERN_MATCHING) + if ((w[0] == '=' || w[0] == '!') && w[1] == '~' && w[2] == '\0') + { + value = patcomp (argv[pos], argv[pos + 2], w[0] == '=' ? EQ : NE); + pos += 3; + return (value); + } +#endif + + if ((w[0] != '-' || w[3] != '\0') || test_binop (w) == 0) + { + test_syntax_error (_("%s: binary operator expected"), w); + /* NOTREACHED */ + return (FALSE); + } + + value = binary_test (w, argv[pos], argv[pos + 2], 0); + pos += 3; + return value; +} + +static int +unary_operator () +{ + char *op; + intmax_t r; + + op = argv[pos]; + if (test_unop (op) == 0) + return (FALSE); + + /* the only tricky case is `-t', which may or may not take an argument. */ + if (op[1] == 't') + { + advance (0); + if (pos < argc) + { + if (legal_number (argv[pos], &r)) + { + advance (0); + return (unary_test (op, argv[pos - 1])); + } + else + return (FALSE); + } + else + return (unary_test (op, "1")); + } + + /* All of the unary operators take an argument, so we first call + unary_advance (), which checks to make sure that there is an + argument, and then advances pos right past it. This means that + pos - 1 is the location of the argument. */ + unary_advance (); + return (unary_test (op, argv[pos - 1])); +} + +int +unary_test (op, arg) + char *op, *arg; +{ + intmax_t r; + struct stat stat_buf; + SHELL_VAR *v; + + switch (op[1]) + { + case 'a': /* file exists in the file system? */ + case 'e': + return (sh_stat (arg, &stat_buf) == 0); + + case 'r': /* file is readable? */ + return (sh_eaccess (arg, R_OK) == 0); + + case 'w': /* File is writeable? */ + return (sh_eaccess (arg, W_OK) == 0); + + case 'x': /* File is executable? */ + return (sh_eaccess (arg, X_OK) == 0); + + case 'O': /* File is owned by you? */ + return (sh_stat (arg, &stat_buf) == 0 && + (uid_t) current_user.euid == (uid_t) stat_buf.st_uid); + + case 'G': /* File is owned by your group? */ + return (sh_stat (arg, &stat_buf) == 0 && + (gid_t) current_user.egid == (gid_t) stat_buf.st_gid); + + case 'N': + return (sh_stat (arg, &stat_buf) == 0 && + stat_buf.st_atime <= stat_buf.st_mtime); + + case 'f': /* File is a file? */ + if (sh_stat (arg, &stat_buf) < 0) + return (FALSE); + + /* -f is true if the given file exists and is a regular file. */ +#if defined (S_IFMT) + return (S_ISREG (stat_buf.st_mode) || (stat_buf.st_mode & S_IFMT) == 0); +#else + return (S_ISREG (stat_buf.st_mode)); +#endif /* !S_IFMT */ + + case 'd': /* File is a directory? */ + return (sh_stat (arg, &stat_buf) == 0 && (S_ISDIR (stat_buf.st_mode))); + + case 's': /* File has something in it? */ + return (sh_stat (arg, &stat_buf) == 0 && stat_buf.st_size > (off_t) 0); + + case 'S': /* File is a socket? */ +#if !defined (S_ISSOCK) + return (FALSE); +#else + return (sh_stat (arg, &stat_buf) == 0 && S_ISSOCK (stat_buf.st_mode)); +#endif /* S_ISSOCK */ + + case 'c': /* File is character special? */ + return (sh_stat (arg, &stat_buf) == 0 && S_ISCHR (stat_buf.st_mode)); + + case 'b': /* File is block special? */ + return (sh_stat (arg, &stat_buf) == 0 && S_ISBLK (stat_buf.st_mode)); + + case 'p': /* File is a named pipe? */ +#ifndef S_ISFIFO + return (FALSE); +#else + return (sh_stat (arg, &stat_buf) == 0 && S_ISFIFO (stat_buf.st_mode)); +#endif /* S_ISFIFO */ + + case 'L': /* Same as -h */ + case 'h': /* File is a symbolic link? */ +#if !defined (S_ISLNK) || !defined (HAVE_LSTAT) + return (FALSE); +#else + return ((arg[0] != '\0') && + (lstat (arg, &stat_buf) == 0) && S_ISLNK (stat_buf.st_mode)); +#endif /* S_IFLNK && HAVE_LSTAT */ + + case 'u': /* File is setuid? */ + return (sh_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISUID) != 0); + + case 'g': /* File is setgid? */ + return (sh_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISGID) != 0); + + case 'k': /* File has sticky bit set? */ +#if !defined (S_ISVTX) + /* This is not Posix, and is not defined on some Posix systems. */ + return (FALSE); +#else + return (sh_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISVTX) != 0); +#endif + + case 't': /* File fd is a terminal? */ + if (legal_number (arg, &r) == 0) + return (FALSE); + return ((r == (int)r) && isatty ((int)r)); + + case 'n': /* True if arg has some length. */ + return (arg[0] != '\0'); + + case 'z': /* True if arg has no length. */ + return (arg[0] == '\0'); + + case 'o': /* True if option `arg' is set. */ + return (minus_o_option_value (arg) == 1); + + case 'v': + v = find_variable (arg); + if (v == 0) + return (FALSE); + return (var_isset (v) ? TRUE : FALSE); + } + + /* We can't actually get here, but this shuts up gcc. */ + return (FALSE); +} + +/* Return TRUE if OP is one of the test command's binary operators. */ +int +test_binop (op) + char *op; +{ + if (op[0] == '=' && op[1] == '\0') + return (1); /* '=' */ + else if ((op[0] == '<' || op[0] == '>') && op[1] == '\0') /* string <, > */ + return (1); + else if ((op[0] == '=' || op[0] == '!') && op[1] == '=' && op[2] == '\0') + return (1); /* `==' and `!=' */ +#if defined (PATTERN_MATCHING) + else if (op[2] == '\0' && op[1] == '~' && (op[0] == '=' || op[0] == '!')) + return (1); +#endif + else if (op[0] != '-' || op[2] == '\0' || op[3] != '\0') + return (0); + else + { + if (op[2] == 't') + switch (op[1]) + { + case 'n': /* -nt */ + case 'o': /* -ot */ + case 'l': /* -lt */ + case 'g': /* -gt */ + return (1); + default: + return (0); + } + else if (op[1] == 'e') + switch (op[2]) + { + case 'q': /* -eq */ + case 'f': /* -ef */ + return (1); + default: + return (0); + } + else if (op[2] == 'e') + switch (op[1]) + { + case 'n': /* -ne */ + case 'g': /* -ge */ + case 'l': /* -le */ + return (1); + default: + return (0); + } + else + return (0); + } +} + +/* Return non-zero if OP is one of the test command's unary operators. */ +int +test_unop (op) + char *op; +{ + if (op[0] != '-' || op[2] != 0) + return (0); + + switch (op[1]) + { + case 'a': case 'b': case 'c': case 'd': case 'e': + case 'f': case 'g': case 'h': case 'k': case 'n': + case 'o': case 'p': case 'r': case 's': case 't': + case 'u': case 'v': case 'w': case 'x': case 'z': + case 'G': case 'L': case 'O': case 'S': case 'N': + return (1); + } + + return (0); +} + +static int +two_arguments () +{ + if (argv[pos][0] == '!' && argv[pos][1] == '\0') + return (argv[pos + 1][0] == '\0'); + else if (argv[pos][0] == '-' && argv[pos][2] == '\0') + { + if (test_unop (argv[pos])) + return (unary_operator ()); + else + test_syntax_error (_("%s: unary operator expected"), argv[pos]); + } + else + test_syntax_error (_("%s: unary operator expected"), argv[pos]); + + return (0); +} + +#define ANDOR(s) (s[0] == '-' && !s[2] && (s[1] == 'a' || s[1] == 'o')) + +/* This could be augmented to handle `-t' as equivalent to `-t 1', but + POSIX requires that `-t' be given an argument. */ +#define ONE_ARG_TEST(s) ((s)[0] != '\0') + +static int +three_arguments () +{ + int value; + + if (test_binop (argv[pos+1])) + { + value = binary_operator (); + pos = argc; + } + else if (ANDOR (argv[pos+1])) + { + if (argv[pos+1][1] == 'a') + value = ONE_ARG_TEST(argv[pos]) && ONE_ARG_TEST(argv[pos+2]); + else + value = ONE_ARG_TEST(argv[pos]) || ONE_ARG_TEST(argv[pos+2]); + pos = argc; + } + else if (argv[pos][0] == '!' && argv[pos][1] == '\0') + { + advance (1); + value = !two_arguments (); + } + else if (argv[pos][0] == '(' && argv[pos+2][0] == ')') + { + value = ONE_ARG_TEST(argv[pos+1]); + pos = argc; + } + else + test_syntax_error (_("%s: binary operator expected"), argv[pos+1]); + + return (value); +} + +/* This is an implementation of a Posix.2 proposal by David Korn. */ +static int +posixtest () +{ + int value; + + switch (argc - 1) /* one extra passed in */ + { + case 0: + value = FALSE; + pos = argc; + break; + + case 1: + value = ONE_ARG_TEST(argv[1]); + pos = argc; + break; + + case 2: + value = two_arguments (); + pos = argc; + break; + + case 3: + value = three_arguments (); + break; + + case 4: + if (argv[pos][0] == '!' && argv[pos][1] == '\0') + { + advance (1); + value = !three_arguments (); + break; + } + /* FALLTHROUGH */ + default: + value = expr (); + } + + return (value); +} + +/* + * [: + * '[' expr ']' + * test: + * test expr + */ +int +test_command (margc, margv) + int margc; + char **margv; +{ + int value; + int code; + + USE_VAR(margc); + + code = setjmp (test_exit_buf); + + if (code) + return (test_error_return); + + argv = margv; + + if (margv[0] && margv[0][0] == '[' && margv[0][1] == '\0') + { + --margc; + + if (margv[margc] && (margv[margc][0] != ']' || margv[margc][1])) + test_syntax_error (_("missing `]'"), (char *)NULL); + + if (margc < 2) + test_exit (SHELL_BOOLEAN (FALSE)); + } + + argc = margc; + pos = 1; + + if (pos >= argc) + test_exit (SHELL_BOOLEAN (FALSE)); + + noeval = 0; + value = posixtest (); + + if (pos != argc) + test_syntax_error (_("too many arguments"), (char *)NULL); + + test_exit (SHELL_BOOLEAN (value)); +} diff --git a/tests/RUN-ONE-TEST b/tests/RUN-ONE-TEST index 3efcf32d..72ec06a2 100755 --- a/tests/RUN-ONE-TEST +++ b/tests/RUN-ONE-TEST @@ -1,4 +1,4 @@ -BUILD_DIR=/usr/local/build/chet/bash/bash-current +BUILD_DIR=/usr/local/build/bash/bash-current THIS_SH=$BUILD_DIR/bash PATH=$PATH:$BUILD_DIR diff --git a/tests/comsub-posix.right b/tests/comsub-posix.right index a24f25f8..34b57d94 100644 --- a/tests/comsub-posix.right +++ b/tests/comsub-posix.right @@ -55,3 +55,18 @@ here-doc terminated with a parenthesis ./comsub-posix1.sub: command substitution: line 2: syntax error near unexpected token `)' ./comsub-posix1.sub: command substitution: line 2: ` if x; then echo foo )' after +swap32_posix is a function +swap32_posix () +{ + local funcname=swap32_posix; + local arg; + for arg in "$@"; + do + echo $(( + ($arg & 4278190080) >> 24 | + ($arg & 16711680) >> 8 | + ($arg & 65280) << 8 | + ($arg & 255) << 24 + )); + done +} diff --git a/tests/comsub-posix.tests b/tests/comsub-posix.tests index d45cbf21..a2f6b3c2 100644 --- a/tests/comsub-posix.tests +++ b/tests/comsub-posix.tests @@ -198,6 +198,8 @@ eof ${THIS_SH} ./comsub-posix1.sub +${THIS_SH} ./comsub-posix2.sub + # produced a parse error through bash-4.0-beta2 : $(echo foo)" " diff --git a/tests/comsub-posix.tests~ b/tests/comsub-posix.tests~ new file mode 100644 index 00000000..d45cbf21 --- /dev/null +++ b/tests/comsub-posix.tests~ @@ -0,0 +1,207 @@ + +# works right +echo ab$(echo mnop)yz +# works right +echo ab$(echo mnop +)yz +# +# works right +echo $(echo ab + ) +# works right +echo $( +) +echo $() +echo ab$()cd + +echo $(case a in (a) echo sh_352.26ax; esac ) +echo $(case a in (a) echo sh_352.26ay; esac) + +echo $((echo sh_352.25a);(echo sh_352.25b)) + +echo $(echo sh_352.27 ')' ")" \) + # ) comment + ) + +echo $( +echo abc # a comment with ) +) + +echo $( +cat <<eof +here doc with ) +eof +) + +echo $( +echo ')' +) + +unset x +x=$(cat <<"EOF" +bad' syntax +EOF +) +echo "$x" +unset x + +echo $(for f in \); do echo a; done ) +echo $(case a in a) echo sh_352.26a; esac ) +echo $(case a in a) echo sh_352.26a; esac) + +echo $(case a in + (a) echo sh_352.26 + ;; + esac + ) + +echo $(case a in + a) echo sh_352.26 + ;; + esac + ) + + +echo $(case a in + a) echo sh_352.26 + ;; + + + + + + esac + + ) + +echo $(( 4<(2+3) ? 1 : 32)) + +echo $(cat << end +sh_352.28 ) +end +) + +echo $(cat <<- end +sh_352.28 ) + end +) + +k=$(case x in x) echo k;; esac) +echo $k + +x=$( + case $(ls) in + example) echo foobix;; + esac +) + +echo $( echo ab\ +cd) + +echo `echo ab +cd` + +echo `echo ab #xyz +cd` + +echo "$(echo abcde) +" + +recho "$(echo abcde) + " + +echo $(echo abcde)\ +foo + +recho $(echo abcde)\ + foo + +recho "wx$(echo abcde)yz" +recho "$(echo abcde)" + +echo $(cat <<eof +' +eof +) + +echo after 1 + +echo $(cat <<\eof +' +eof +) + +echo after 2 + +echo "$(cat <<\eof +' +eof +)" + +echo after 3 + +echo "$(cat <<\eof +` +eof +)" + +echo after 4 + +echo $( +cat << ')' +hello +) +) + +echo after 5 + +echo $(cat <<'eof' +' +eof +) + +echo after 6 + +echo $( + case x in x) echo x;; esac +) + +echo $( + case x in (x) echo x;; esac +) + +echo $( + echo 'quoted )' +) + +echo $( + echo comment # with ) +) + +echo $( +cat <<\eof + here-doc with ) +eof +) + +echo $( +cat <<\) + here-doc terminated with a parenthesis +) +) + +echo $( +cat <<\eof + ' # or a single back- or doublequote +eof +) + +${THIS_SH} ./comsub-posix1.sub + +# produced a parse error through bash-4.0-beta2 +: $(echo foo)" +" + +# fixed after bash-4.0 released +: $(case a in a) echo ;; # comment +esac) diff --git a/tests/comsub-posix2.sub b/tests/comsub-posix2.sub new file mode 100644 index 00000000..238745f7 --- /dev/null +++ b/tests/comsub-posix2.sub @@ -0,0 +1,16 @@ +# problem with bash-4.x versions before bash-4.2. required posix interp +swap32_posix() +{ + local funcname=swap32_posix + local arg + for arg in "$@"; do + echo $(( + ($arg & 4278190080) >> 24 | + ($arg & 16711680) >> 8 | + ($arg & 65280) << 8 | + ($arg & 255) << 24 + )) + done +} + +type swap32_posix diff --git a/tests/errors.right b/tests/errors.right index ae7bf29c..6d26798f 100644 --- a/tests/errors.right +++ b/tests/errors.right @@ -15,7 +15,7 @@ unset: usage: unset [-f] [-v] [name ...] ./errors.tests: line 52: unset: `/bin/sh': not a valid identifier ./errors.tests: line 55: unset: cannot simultaneously unset a function and a variable ./errors.tests: line 58: declare: -z: invalid option -declare: usage: declare [-aAfFilrtux] [-p] [name[=value] ...] +declare: usage: declare [-aAfFgilrtux] [-p] [name[=value] ...] ./errors.tests: line 60: declare: `-z': not a valid identifier ./errors.tests: line 61: declare: `/bin/sh': not a valid identifier ./errors.tests: line 65: declare: cannot use `-f' to make functions diff --git a/tests/func.right b/tests/func.right index da8b45c7..70bf123b 100644 --- a/tests/func.right +++ b/tests/func.right @@ -153,4 +153,17 @@ expect 2 40 2 40 expect 5 20 5 20 +./func4.sub: line 10: foo: maximum function nesting level exceeded (100) +1 +after: f = 100 +./func4.sub: line 10: foo: maximum function nesting level exceeded (100) +1 +after: f = 100 +7 +after FUNCNEST reset: f = 201 +7 +after FUNCNEST unset: f = 201 +./func4.sub: line 10: foo: maximum function nesting level exceeded (20) +1 +after FUNCNEST assign: f = 38 5 diff --git a/tests/func.tests b/tests/func.tests index 23dff44b..8701c2ad 100644 --- a/tests/func.tests +++ b/tests/func.tests @@ -157,6 +157,9 @@ ${THIS_SH} ./func2.sub # test for some posix-specific function behavior ${THIS_SH} ./func3.sub +# FUNCNEST testing +${THIS_SH} ./func4.sub + unset -f myfunction myfunction() { echo "bad shell function redirection" diff --git a/tests/func.tests~ b/tests/func.tests~ new file mode 100644 index 00000000..23dff44b --- /dev/null +++ b/tests/func.tests~ @@ -0,0 +1,176 @@ +a() +{ + x=$((x - 1)) + return 5 +} + +b() +{ + x=$((x - 1)) + a + echo a returns $? + return 4 +} + +c() +{ + x=$((x - 1)) + b + echo b returns $? + return 3 +} + +d() +{ + x=$((x - 1)) + c + echo c returns $? + return 2 +} + +e() +{ + d + echo d returns $? + echo in e + x=$((x - 1)) + return $x +} + +f() +{ + e + echo e returned $? + echo x is $x + return 0 +} + +x=30 +f + +# make sure unsetting a local variable preserves the `local' attribute +f1() +{ + local zz + zz=abcde + echo $zz + unset zz + zz=defghi + echo $zz +} + +zz=ZZ +echo $zz +f1 +echo $zz + +unset -f f1 +f1() +{ + return 5 +} + +( f1 ) +echo $? + +unset -f f1 +f1() +{ + sleep 5 + return 5 +} + +f1 & +wait +echo $? + +unset -f f1 + +f1() +{ + echo $AVAR + printenv AVAR +} + +AVAR=AVAR +echo $AVAR +f1 +AVAR=foo f1 +echo $AVAR + +unset -f f1 +# make sure subshells can do a `return' if we're executing in a function +f1() +{ + ( return 5 ) + status=$? + echo $status + return $status +} + +f1 +echo $? + +declare -F f1 # should print just the name +declare -f f1 # should print the definition, too + +# no functions should be exported, right? +declare -xF +declare -xf + +# FUNCNAME tests +func2() +{ + echo FUNCNAME = $FUNCNAME +} + +func() +{ + echo before: FUNCNAME = $FUNCNAME + func2 + echo after: FUNCNAME = $FUNCNAME +} + +echo before: try to assign to FUNCNAME +FUNCNAME=7 + +echo outside: FUNCNAME = $FUNCNAME +func +echo outside2: FUNCNAME = $FUNCNAME + +# test exported functions (and cached exportstr) +zf() +{ + echo this is zf +} +export -f zf + +${THIS_SH} -c 'type -t zf' +${THIS_SH} -c 'type zf' + +${THIS_SH} ./func1.sub + +# tests for functions whose bodies are not group commands, with and without +# attached redirections +${THIS_SH} ./func2.sub + +# test for some posix-specific function behavior +${THIS_SH} ./func3.sub + +unset -f myfunction +myfunction() { + echo "bad shell function redirection" +} >> /dev/null + +myfunction +myfunction | cat + +segv() +{ + echo foo | return 5 +} + +segv +echo $? + +exit 0 diff --git a/tests/func4.sub b/tests/func4.sub new file mode 100644 index 00000000..9c9c1b42 --- /dev/null +++ b/tests/func4.sub @@ -0,0 +1,39 @@ +# test FUNCNEST functionality -- bash-4.2 +FUNCNEST=100 + +foo() +{ + (( f++ )) + if (( f > 200 )); then + return 7 + fi + foo +} + +f=0 +foo +echo $? +echo after: f = $f + +f=0 +foo +echo $? +echo after: f = $f + +f=0 +FUNCNEST=0 +foo +echo $? +echo after FUNCNEST reset: f = $f + +f=0 +unset FUNCNEST +foo +echo $? +echo after FUNCNEST unset: f = $f + +FUNCNEST=20 +f=$(( FUNCNEST - 2 )) +foo +echo $? +echo after FUNCNEST assign: f = $f diff --git a/tests/posixexp.right b/tests/posixexp.right new file mode 100644 index 00000000..85d29e7f --- /dev/null +++ b/tests/posixexp.right @@ -0,0 +1,30 @@ +argv[1] = <foo 'bar' baz> +argv[1] = <a b c d> +argv[1] = <a b c d> +argv[1] = <foo ax{{{}b c d{} bar> +argv[2] = <}> +argv[3] = <baz> +argv[1] = <'foo'> +argv[1] = <'foo'> +argv[1] = <$a> +argv[1] = <'foo'> +argv[1] = <foo*bar> +argv[1] = <foo*bar> +argv[1] = <foo*bar'}> +argv[1] = <x'> +<.> <x> <.> <> <.> <x> <.> +<x> <.> <w> <.> <x> <.> <w> <.> +<x> <.> <w> <.> <x> <.> <w> <.> +<x> <.> <w> <.> <x> <.> <w> <.> +<.> <w> <.> <> <.> <w> <.> +<.> <w> <.> <> <.> <w> <.> +<x> <.> <x> <.> <x> <.> <x> <.> +<x> <.> <w> <.> <x> <.> <w> <.> +<x> <.> <w> <.> <x> <.> <w> <.> +<x> <.> <w> <.> <x> <.> <w> <.> +argv[1] = <'bar> +argv[1] = <foo 'bar baz> +argv[1] = <z}> +argv[1] = <''z}> +./posixexp.tests: line 56: unexpected EOF while looking for matching `}' +./posixexp.tests: line 59: syntax error: unexpected end of file diff --git a/tests/posixexp.tests b/tests/posixexp.tests new file mode 100644 index 00000000..bd4a09d7 --- /dev/null +++ b/tests/posixexp.tests @@ -0,0 +1,58 @@ +recho "foo ${IFS+'bar'} baz" +recho "a ${IFS+b c} d" + +recho "a ${IFS+"b c"} d" + +u=x +recho "foo ${IFS+a$u{{{\}b} c ${IFS+d{}} bar" ${IFS-e{}} baz + +a=foo +recho "${IFS+'$a'}" +recho "${IFS+"'$a'"}" + +recho ${IFS+'$a'} +recho ${IFS+"'$a'"} + +unset a u +x='foo*bar' + +recho "${x##"}"}" +recho "${x##'}'}" +recho "${x##'}" + +recho "${x:-'}'}" + +foo="x'a'y" +recho "${foo%*'a'*}" +unset x + +unset u +v=w +printf '<%s> ' ${u+x} . ${v+x} . "${u+x}" . "${v+x}" .; echo +printf '<%s> ' ${u-x} . ${v-x} . "${u-x}" . "${v-x}" .; echo +printf '<%s> ' ${u=x} . ${v=x} . "${u=x}" . "${v=x}" .; echo +printf '<%s> ' ${u?x} . ${v?x} . "${u?x}" . "${v?x}" .; echo +printf '<%s> ' ${u#x} . ${v#x} . "${u#x}" . "${v#x}" .; echo +printf '<%s> ' ${u%x} . ${v%x} . "${u%x}" . "${v%x}" .; echo +printf '<%s> ' ${u:+x} . ${v:+x} . "${u:+x}" . "${v:+x}" .; echo +printf '<%s> ' ${u:-x} . ${v:-x} . "${u:-x}" . "${v:-x}" .; echo +printf '<%s> ' ${u:=x} . ${v:=x} . "${u:=x}" . "${v:=x}" .; echo +printf '<%s> ' ${u:?x} . ${v:?x} . "${u:?x}" . "${v:?x}" .; echo +# these are invalid substitution operators +#printf '<%s> ' ${u:#x} . ${v:#x} . "${u:#x}" . "${v:#x}" .; echo +#printf '<%s> ' ${u:%x} . ${v:%x} . "${u:%x}" . "${v:%x}" .; echo + +unset foo +set -o posix + +recho "${IFS+'bar}" +recho "foo ${IFS+'bar} baz" + +recho ${IFS+'}'z} +recho "${IFS+'}'z}" + +# this will be an error +foo=bar +echo "${foo:-"a}" + + diff --git a/tests/printf.right b/tests/printf.right Binary files differindex a6bb04d6..065d2f79 100644 --- a/tests/printf.right +++ b/tests/printf.right diff --git a/tests/printf.tests b/tests/printf.tests index 3dd52486..9644cc2b 100644 --- a/tests/printf.tests +++ b/tests/printf.tests @@ -308,4 +308,9 @@ shopt -s nullglob extglob echo "x$(printf "%b" @(hugo))x" printf -v var "%b" @(hugo); echo "x${var}x" +# tests variable assignment with -v +${THIS_SH} ./printf1.sub + ${THIS_SH} ./printf2.sub + +${THIS_SH} ./printf3.sub diff --git a/tests/printf.tests~ b/tests/printf.tests~ new file mode 100644 index 00000000..80ab4de1 --- /dev/null +++ b/tests/printf.tests~ @@ -0,0 +1,313 @@ +LC_ALL=C +LC_NUMERIC=C + +# these should output error messages -- the format is required +printf +printf -- + +# these should output nothing +printf "" +printf -- "" + +# in the future this may mean to put the output into VAR, but for +# now it is an error +# 2005-03-15 no longer an error +unset var +printf -v var "%10d" $RANDOM +echo ${#var} + +# this should expand escape sequences in the format string, nothing else +printf "\tone\n" + +# this should not cut off output after the \c +printf "one\ctwo\n" + +# and unrecognized backslash escapes should have the backslash preserverd +printf "4\.2\n" + +printf "no newline " ; printf "now newline\n" + +# %% -> % +printf "%%\n" + +# this was a bug caused by pre-processing the string for backslash escapes +# before doing the `%' format processing -- all versions before bash-2.04 +printf "\045" ; echo +printf "\045d\n" + +# simple character output +printf "%c\n" ABCD + +# test simple string output +printf "%s\n" unquoted + +# test quoted string output +printf "%s %q\n" unquoted quoted +printf "%s%10q\n" unquoted quoted + +printf "%q\n" 'this&that' + +# make sure the format string is reused to use up arguments +printf "%d " 1 2 3 4 5; printf "\n" + +# make sure that extra format characters get null arguments +printf "%s %d %d %d\n" onestring + +printf "%s %d %u %4.2f\n" onestring + +printf -- "--%s %s--\n" 4.2 '' +printf -- "--%s %s--\n" 4.2 + +# test %b escapes + +# 8 is a non-octal digit, so the `81' should be output +printf -- "--%b--\n" '\n\081' + +printf -- "--%b--\n" '\t\0101' +printf -- "--%b--\n" '\t\101' + +# these should all display `A7' +echo -e "\01017" +echo -e "\x417" + +printf "%b\n" '\01017' +printf "%b\n" '\1017' +printf "%b\n" '\x417' + +printf -- "--%b--\n" '\"abcd\"' +printf -- "--%b--\n" "\'abcd\'" + +printf -- "--%b--\n" 'a\\x' + +printf -- "--%b--\n" '\x' + +Z1=$(printf -- "%b\n" '\a\b\e\f\r\v') +Z2=$'\a\b\e\f\r\v' + +if [ "$Z1" != "$Z2" ]; then + echo "whoops: printf %b and $'' differ" >&2 +fi +unset Z1 Z2 + +printf -- "--%b--\n" '' +printf -- "--%b--\n" + +# the stuff following the \c should be ignored, as well as the rest +# of the format string +printf -- "--%b--\n" '4.2\c5.4\n'; printf "\n" + +# unrecognized escape sequences should by displayed unchanged +printf -- "--%b--\n" '4\.2' + +# a bare \ should not be processed as an escape sequence +printf -- "--%b--\n" '\' + +# make sure extra arguments are ignored if the format string doesn't +# actually use them +printf "\n" 4.4 BSD +printf " " 4.4 BSD ; printf "\n" + +# make sure that a fieldwidth and precision of `*' are handled right +printf "%10.8s\n" 4.4BSD +printf "%*.*s\n" 10 8 4.4BSD + +printf "%10.8q\n" 4.4BSD +printf "%*.*q\n" 10 8 4.4BSD + +printf "%6b\n" 4.4BSD +printf "%*b\n" 6 4.4BSD + +# we handle this crap with homemade code in printf.def +printf "%10b\n" 4.4BSD +printf -- "--%-10b--\n" 4.4BSD +printf "%4.2b\n" 4.4BSD +printf "%.3b\n" 4.4BSD +printf -- "--%-8b--\n" 4.4BSD + +# test numeric conversions -- these four lines should echo identically +printf "%d %u %i 0%o 0x%x 0x%X\n" 255 255 255 255 255 255 +printf "%d %u %i %#o %#x %#X\n" 255 255 255 255 255 255 + +printf "%ld %lu %li 0%o 0x%x 0x%X\n" 255 255 255 255 255 255 +printf "%ld %lu %li %#o %#x %#X\n" 255 255 255 255 255 255 + +printf "%10d\n" 42 +printf "%10d\n" -42 + +printf "%*d\n" 10 42 +printf "%*d\n" 10 -42 + +# test some simple floating point formats +printf "%4.2f\n" 4.2 +printf "%#4.2f\n" 4.2 +printf "%#4.1f\n" 4.2 + +printf "%*.*f\n" 4 2 4.2 +printf "%#*.*f\n" 4 2 4.2 +printf "%#*.*f\n" 4 1 4.2 + +printf "%E\n" 4.2 +printf "%e\n" 4.2 +printf "%6.1E\n" 4.2 +printf "%6.1e\n" 4.2 + +printf "%G\n" 4.2 +printf "%g\n" 4.2 +printf "%6.2G\n" 4.2 +printf "%6.2g\n" 4.2 + +# test some of the more esoteric features of POSIX.1 printf +printf "%d\n" "'string'" +printf "%d\n" '"string"' + +printf "%#o\n" "'string'" +printf "%#o\n" '"string"' + +printf "%#x\n" "'string'" +printf "%#X\n" '"string"' + +printf "%6.2f\n" "'string'" +printf "%6.2f\n" '"string"' + +# output from these two lines had better be the same +printf -- "--%6.4s--\n" abcdefghijklmnopqrstuvwxyz +printf -- "--%6.4b--\n" abcdefghijklmnopqrstuvwxyz + +# and these two also +printf -- "--%12.10s--\n" abcdefghijklmnopqrstuvwxyz +printf -- "--%12.10b--\n" abcdefghijklmnopqrstuvwxyz + +# tests for translating \' to ' and \\ to \ +# printf translates \' to ' in the format string... +printf "\'abcd\'\n" + +# but not when the %b format specification is used +printf "%b\n" \\\'abcd\\\' + +# but both translate \\ to \ +printf '\\abcd\\\n' +printf "%b\n" '\\abcd\\' + +# this was reported as a bug in bash-2.03 +# these three lines should all echo `26' +printf "%d\n" 0x1a +printf "%d\n" 032 +printf "%d\n" 26 + +# error messages + +# this should be an overflow, but error messages vary between systems +# printf "%lu\n" 4294967296 + +# ...but we cannot use this because some systems (SunOS4, for example), +# happily ignore overflow conditions in strtol(3) +#printf "%ld\n" 4294967296 + +printf "%10" +printf "ab%Mcd\n" + +# this caused an infinite loop in older versions of printf +printf "%y" 0 + +# these should print a warning and `0', according to POSIX.2 +printf "%d\n" GNU +printf "%o\n" GNU + +# failures in all bash versions through bash-2.05 +printf "%.0s" foo +printf "%.*s" 0 foo + +printf '%.0b-%.0s\n' foo bar +printf '(%*b)(%*s)\n' -4 foo -4 bar + +format='%'`printf '%0100384d' 0`'d\n' +printf $format 0 + +# failures in all bash versions through bash-3.0 - undercounted characters +unset vv +printf " %s %s %s \n%n" ab cd ef vv +echo "$vv" + +# this doesn't work with printf(3) on all systems +#printf "%'s\n" foo + +# test cases from an austin-group list discussion +# prints ^G as an extension +printf '%b\n' '\7' + +# prints ^G +printf '%b\n' '\0007' + +# prints NUL then 7 +printf '\0007\n' + +# prints no more than two hex digits +printf '\x07e\n' + +# additional backslash escapes +printf '\"\?\n' + +# failures with decimal precisions until after bash-3.1 +printf '%0.5d\n' 1 + +printf '%05d\n' 1 +printf '%5d\n' 1 +printf '%0d\n' 1 + +# failures with various floating point formats and 0 after bash-3.2 + +printf "%G\n" 0 +printf "%g\n" 0 +printf "%4.2G\n" 0 +printf "%4.2g\n" 0 + +printf "%G\n" 4 +printf "%g\n" 4 +printf "%4.2G\n" 4 +printf "%4.2g\n" 4 + +printf "%F\n" 0 +printf "%f\n" 0 +printf "%4.2F\n" 0 +printf "%4.2f\n" 0 + +printf "%F\n" 4 +printf "%f\n" 4 +printf "%4.2F\n" 4 +printf "%4.2f\n" 4 + +printf "%E\n" 0 +printf "%e\n" 0 +printf "%4.2E\n" 0 +printf "%4.2e\n" 0 + +printf "%E\n" 4 +printf "%e\n" 4 +printf "%4.2E\n" 4 +printf "%4.2e\n" 4 + +printf "%08X\n" 2604292517 + +# make sure these format specifiers all output '' for empty string arguments +echo q +printf "%q\n" "" +printf "%q\n" + +echo s +printf "%s\n" '' +printf "%s\n" + +echo b +printf "%b\n" '' +printf "%b\n" + +# bug in bash versions up to and including bash-3.2 +v=yyy +printf -v var "%s" '/current/working/directory/*.@(m3|i3|ig|mg)' +shopt -s nullglob extglob +echo "x$(printf "%b" @(hugo))x" +printf -v var "%b" @(hugo); echo "x${var}x" + +${THIS_SH} ./printf2.sub + +${THIS_SH} ./printf3.sub diff --git a/tests/printf1.sub b/tests/printf1.sub index 72d86a21..52612d5c 100644 --- a/tests/printf1.sub +++ b/tests/printf1.sub @@ -67,8 +67,8 @@ printf "%s" "$vv" # test %b escapes # 8 is a non-octal digit, so the `81' should be output -printf -v vv -- "--%b--\n" '\n\081' -printf "%s" "$vv" +#printf -v vv -- "--%b--\n" '\n\081' +#printf "%s" "$vv" printf -v vv -- "--%b--\n" '\t\0101' printf "%s" "$vv" @@ -76,9 +76,6 @@ printf -v vv -- "--%b--\n" '\t\101' printf "%s" "$vv" # these should all display `A7' -echo -e "\1017" -echo -e "\x417" - printf -v vv "%b\n" '\01017' printf "%s" "$vv" printf -v vv "%b\n" '\1017' @@ -326,8 +323,8 @@ printf -v vv '%b\n' '\0007' printf "%s" "$vv" # prints NUL then 7 -printf -v vv '\0007\n' -printf "%s" "$vv" +#printf -v vv '\0007\n' +#printf "%s" "$vv" # prints no more than two hex digits printf -v vv '\x07e\n' diff --git a/tests/printf1.sub~ b/tests/printf1.sub~ new file mode 100644 index 00000000..8b433a2d --- /dev/null +++ b/tests/printf1.sub~ @@ -0,0 +1,338 @@ +LC_ALL=C +LC_NUMERIC=C + +unset vv + +# this should expand escape sequences in the format string, nothing else +printf -v vv "\tone\n" +printf "%s" "$vv" + +# this should not cut off output after the \c +printf -v vv "one\ctwo\n" +printf "%s" "$vv" + +# and unrecognized backslash escapes should have the backslash preserverd +printf -v vv "4\.2\n" +printf "%s" "$vv" + +printf -v vv "no newline " ; printf "%s" "$vv" ; printf -v vv "now newline\n" +printf "%s" "$vv" + +# %% -> % +printf -v vv "%%\n" +printf "%s" "$vv" + +# this was a bug caused by pre-processing the string for backslash escapes +# before doing the `%' format processing -- all versions before bash-2.04 +printf -v vv "\045" +printf "%s" "$vv" +echo +printf -v vv "\045d\n" +printf "%s" "$vv" + +# simple character output +printf -v vv "%c\n" ABCD +printf "%s" "$vv" + +# test simple string output +printf -v vv "%s\n" unquoted +printf "%s" "$vv" + +# test quoted string output +printf -v vv "%s %q\n" unquoted quoted +printf "%s" "$vv" +printf -v vv "%s%10q\n" unquoted quoted +printf "%s" "$vv" + +printf -v vv "%q\n" 'this&that' +printf "%s" "$vv" + +# make sure the format string is reused to use up arguments +printf -v vv "%d " 1 2 3 4 5 +printf "%s" "$vv" +echo + +# make sure that extra format characters get null arguments +printf -v vv "%s %d %d %d\n" onestring +printf "%s" "$vv" + +printf -v vv "%s %d %u %4.2f\n" onestring +printf "%s" "$vv" + +printf -v vv -- "--%s %s--\n" 4.2 '' +printf "%s" "$vv" +printf -v vv -- "--%s %s--\n" 4.2 +printf "%s" "$vv" + +# test %b escapes + +# 8 is a non-octal digit, so the `81' should be output +#printf -v vv -- "--%b--\n" '\n\081' +#printf "%s" "$vv" + +printf -v vv -- "--%b--\n" '\t\0101' +printf "%s" "$vv" +printf -v vv -- "--%b--\n" '\t\101' +printf "%s" "$vv" + +# these should all display `A7' +echo -e "\1017" +echo -e "\x417" + +printf -v vv "%b\n" '\01017' +printf "%s" "$vv" +printf -v vv "%b\n" '\1017' +printf "%s" "$vv" +printf -v vv "%b\n" '\x417' +printf "%s" "$vv" + +printf -v vv -- "--%b--\n" '\"abcd\"' +printf "%s" "$vv" +printf -v vv -- "--%b--\n" "\'abcd\'" +printf "%s" "$vv" + +printf -v vv -- "--%b--\n" 'a\\x' +printf "%s" "$vv" + +printf -v vv -- "--%b--\n" '\x' +printf "%s" "$vv" + +Z1=$(printf -- "%b\n" '\a\b\e\f\r\v') +Z2=$'\a\b\e\f\r\v' + +if [ "$Z1" != "$Z2" ]; then + printf "%s" "whoops: printf -v vv %b and $'' differ" >&2 +fi +unset Z1 Z2 + +printf -v vv -- "--%b--\n" '' +printf "%s" "$vv" +printf -v vv -- "--%b--\n" +printf "%s" "$vv" + +# the stuff following the \c should be ignored, as well as the rest +# of the format string +printf -v vv -- "--%b--\n" '4.2\c5.4\n' +printf "%s" "$vv" +echo + +# unrecognized escape sequences should by displayed unchanged +printf -v vv -- "--%b--\n" '4\.2' +printf "%s" "$vv" + +# a bare \ should not be processed as an escape sequence +printf -v vv -- "--%b--\n" '\' +printf "%s" "$vv" + +# make sure extra arguments are ignored if the format string doesn't +# actually use them +printf -v vv "\n" 4.4 BSD +printf "%s" "$vv" +printf -v vv " " 4.4 BSD +printf "%s" "$vv" +echo + +# make sure that a fieldwidth and precision of `*' are handled right +printf -v vv "%10.8s\n" 4.4BSD +printf "%s" "$vv" +printf -v vv "%*.*s\n" 10 8 4.4BSD +printf "%s" "$vv" + +printf -v vv "%10.8q\n" 4.4BSD +printf "%s" "$vv" +printf -v vv "%*.*q\n" 10 8 4.4BSD +printf "%s" "$vv" + +printf -v vv "%6b\n" 4.4BSD +printf "%s" "$vv" +printf -v vv "%*b\n" 6 4.4BSD +printf "%s" "$vv" + +# we handle this crap with homemade code in printf -v vv.def +printf -v vv "%10b\n" 4.4BSD +printf "%s" "$vv" +printf -v vv -- "--%-10b--\n" 4.4BSD +printf "%s" "$vv" +printf -v vv "%4.2b\n" 4.4BSD +printf "%s" "$vv" +printf -v vv "%.3b\n" 4.4BSD +printf "%s" "$vv" +printf -v vv -- "--%-8b--\n" 4.4BSD +printf "%s" "$vv" + +# test numeric conversions -- these four lines should printf "%s" identically +printf -v vv "%d %u %i 0%o 0x%x 0x%X\n" 255 255 255 255 255 255 +printf "%s" "$vv" +printf -v vv "%d %u %i %#o %#x %#X\n" 255 255 255 255 255 255 +printf "%s" "$vv" + +printf -v vv "%ld %lu %li 0%o 0x%x 0x%X\n" 255 255 255 255 255 255 +printf "%s" "$vv" +printf -v vv "%ld %lu %li %#o %#x %#X\n" 255 255 255 255 255 255 +printf "%s" "$vv" + +printf -v vv "%10d\n" 42 +printf "%s" "$vv" +printf -v vv "%10d\n" -42 +printf "%s" "$vv" + +printf -v vv "%*d\n" 10 42 +printf "%s" "$vv" +printf -v vv "%*d\n" 10 -42 +printf "%s" "$vv" + +# test some simple floating point formats +printf -v vv "%4.2f\n" 4.2 +printf "%s" "$vv" +printf -v vv "%#4.2f\n" 4.2 +printf "%s" "$vv" +printf -v vv "%#4.1f\n" 4.2 +printf "%s" "$vv" + +printf -v vv "%*.*f\n" 4 2 4.2 +printf "%s" "$vv" +printf -v vv "%#*.*f\n" 4 2 4.2 +printf "%s" "$vv" +printf -v vv "%#*.*f\n" 4 1 4.2 +printf "%s" "$vv" + +printf -v vv "%E\n" 4.2 +printf "%s" "$vv" +printf -v vv "%e\n" 4.2 +printf "%s" "$vv" +printf -v vv "%6.1E\n" 4.2 +printf "%s" "$vv" +printf -v vv "%6.1e\n" 4.2 +printf "%s" "$vv" + +printf -v vv "%G\n" 4.2 +printf "%s" "$vv" +printf -v vv "%g\n" 4.2 +printf "%s" "$vv" +printf -v vv "%6.2G\n" 4.2 +printf "%s" "$vv" +printf -v vv "%6.2g\n" 4.2 +printf "%s" "$vv" + +# test some of the more esoteric features of POSIX.1 printf -v vv +printf -v vv "%d\n" "'string'" +printf "%s" "$vv" +printf -v vv "%d\n" '"string"' +printf "%s" "$vv" + +printf -v vv "%#o\n" "'string'" +printf "%s" "$vv" +printf -v vv "%#o\n" '"string"' +printf "%s" "$vv" + +printf -v vv "%#x\n" "'string'" +printf "%s" "$vv" +printf -v vv "%#X\n" '"string"' +printf "%s" "$vv" + +printf -v vv "%6.2f\n" "'string'" +printf "%s" "$vv" +printf -v vv "%6.2f\n" '"string"' +printf "%s" "$vv" + +# output from these two lines had better be the same +printf -v vv -- "--%6.4s--\n" abcdefghijklmnopqrstuvwxyz +printf "%s" "$vv" +printf -v vv -- "--%6.4b--\n" abcdefghijklmnopqrstuvwxyz +printf "%s" "$vv" + +# and these two also +printf -v vv -- "--%12.10s--\n" abcdefghijklmnopqrstuvwxyz +printf "%s" "$vv" +printf -v vv -- "--%12.10b--\n" abcdefghijklmnopqrstuvwxyz +printf "%s" "$vv" + +# tests for translating \' to ' and \\ to \ +# printf -v vv translates \' to ' in the format string... +printf -v vv "\'abcd\'\n" +printf "%s" "$vv" + +# but not when the %b format specification is used +printf -v vv "%b\n" \\\'abcd\\\' +printf "%s" "$vv" + +# but both translate \\ to \ +printf -v vv '\\abcd\\\n' +printf "%s" "$vv" +printf -v vv "%b\n" '\\abcd\\' +printf "%s" "$vv" + +# this was reported as a bug in bash-2.03 +# these three lines should all printf "%s" `26' +printf -v vv "%d\n" 0x1a +printf "%s" "$vv" +printf -v vv "%d\n" 032 +printf "%s" "$vv" +printf -v vv "%d\n" 26 +printf "%s" "$vv" + +# error messages + +# this should be an overflow, but error messages vary between systems +# printf -v vv "%lu\n" 4294967296 + +# ...but we cannot use this because some systems (SunOS4, for example), +# happily ignore overflow conditions in strtol(3) +#printf -v vv "%ld\n" 4294967296 + +printf -v vv "%10" +printf -v vv "ab%Mcd\n" + +# this caused an infinite loop in older versions of printf -v vv +printf -v vv "%y" 0 + +# these should print a warning and `0', according to POSIX.2 +printf -v vv "%d\n" GNU +printf "%s" "$vv" +printf -v vv "%o\n" GNU +printf "%s" "$vv" + +# failures in all bash versions through bash-2.05 +printf -v vv "%.0s" foo +printf "%s" "$vv" +printf -v vv "%.*s" 0 foo +printf "%s" "$vv" + +printf -v vv '%.0b-%.0s\n' foo bar +printf "%s" "$vv" +printf -v vv '(%*b)(%*s)\n' -4 foo -4 bar +printf "%s" "$vv" + +format='%'`printf '%0100384d' 0`'d\n' +printf -v vv $format 0 +printf "%s" "$vv" + +# failures in all bash versions through bash-3.0 - undercounted characters +unset vv +printf -v vv " %s %s %s \n%n" ab cd ef vvv +printf "%s" "$vv" +echo $vvv + +# this doesn't work with printf -v vv(3) on all systems +#printf -v vv "%'s\n" foo + +# test cases from an austin-group list discussion +# prints ^G as an extension +printf -v vv '%b\n' '\7' +printf "%s" "$vv" + +# prints ^G +printf -v vv '%b\n' '\0007' +printf "%s" "$vv" + +# prints NUL then 7 +#printf -v vv '\0007\n' +#printf "%s" "$vv" + +# prints no more than two hex digits +printf -v vv '\x07e\n' +printf "%s" "$vv" + +# additional backslash escapes +printf -v vv '\"\?\n' +printf "%s" "$vv" diff --git a/tests/printf3.sub b/tests/printf3.sub new file mode 100644 index 00000000..0de273af --- /dev/null +++ b/tests/printf3.sub @@ -0,0 +1,53 @@ +LC_ALL=C +LANG=C + +SHELLSTART=$(date +%s) +SECS=1275250155 +export TZ=EST5EDT + +printf "%()T\n" $SECS +printf "%(abde)Z\n" -1 + +printf "%(%e-%b-%Y %T)T\n" $SECS + +printf -v v1 "%(%e-%b-%Y %T)T\n" $(date +%s) +printf -v v2 "%(%e-%b-%Y %T)T\n" -1 + +case $v1 in +$v2) ;; +*) echo "current time and -1 possible mismatch|$v1|$v2|" >&2 ;; +esac +unset v1 v2 + +v1=$(date +%s) +printf -v v2 "%(%s)T" -1 + +case $v1 in +$v2) ;; +*) echo "current time mismatch:$v1|$v2|" >&2 ;; +esac +unset v1 v2 + +printf "%(%x %X)T\n" $(( $SECS - 3600 )) + +printf -v v1 "%(%F %r)T\n" $SHELLSTART +printf -v v2 "%(%F %r)T\n" -2 + +case $v1 in +$v2) ;; +*) echo "shell start time and -2 possible mismatch|$v1|$v2|" >&2 ;; +esac +unset v1 v2 + +printf "current time: %(%F %r)T\n" $SECS + +printf "epoch time: %(%F %r %z)T\n" 0 +printf "random time: %(%F %r %z)T\n" $SECS + +printf "local time: %(%+)T\n" $SECS + +# test fieldwidth, justification, precision +printf "%-40.50(%+)T date-style time\n" $SECS + +# test fieldwidth, justification, precision, embedded parens +printf "%-40.50(%x (foo) %X)T date-style time\n" $SECS diff --git a/tests/run-posixexp b/tests/run-posixexp new file mode 100644 index 00000000..e5a1c3e5 --- /dev/null +++ b/tests/run-posixexp @@ -0,0 +1,2 @@ +${THIS_SH} ./posixexp.tests > /tmp/xx 2>&1 +diff /tmp/xx posixexp.right && rm -f /tmp/xx diff --git a/tests/run-posixexp~ b/tests/run-posixexp~ new file mode 100644 index 00000000..e5087ddd --- /dev/null +++ b/tests/run-posixexp~ @@ -0,0 +1,2 @@ +${THIS_SH} ./posixexp.tests > /tmp/xx +diff /tmp/xx posixexp.right && rm -f /tmp/xx diff --git a/tests/test.right b/tests/test.right index c5d7c904..7fcd9964 100644 --- a/tests/test.right +++ b/tests/test.right @@ -180,6 +180,12 @@ t -o allexport 1 t ! -o allexport 0 +t -v unset +1 +t -v set +0 +t -v set +0 t xx -a yy 0 t xx -o "" @@ -266,7 +272,7 @@ b ( 1 = 2 2 ./test.tests: line 13: test: too many arguments 2 -./test.tests: line 406: [: missing `]' +./test.tests: line 418: [: missing `]' 2 ./test.tests: line 13: test: (: unary operator expected 2 diff --git a/tests/test.tests b/tests/test.tests index 47ad9bb1..47eef354 100644 --- a/tests/test.tests +++ b/tests/test.tests @@ -286,12 +286,24 @@ t -n abcd -a aaa echo 't -n abcd -a -z aaa' t -n abcd -a -z aaa +# test set or unset shell options set +o allexport echo 't -o allexport' t -o allexport echo 't ! -o allexport' t ! -o allexport +#test set or unset shell variables +unset unset +echo 't -v unset' +t -v unset +set= +echo 't -v set' +t -v set +set=set +echo 't -v set' +t -v set + echo 't xx -a yy' t xx -a yy echo 't xx -o ""' diff --git a/tests/test.tests~ b/tests/test.tests~ new file mode 100644 index 00000000..47ad9bb1 --- /dev/null +++ b/tests/test.tests~ @@ -0,0 +1,426 @@ +if (( $UID == 0 )); then + echo "test-tests: the test suite should not be run as root" >&2 +fi + +b() +{ + [ "$@" ] + echo $? +} + +t() +{ + test "$@" + echo $? +} + +echo 't -a noexist' +t -a noexist +echo 't -a run-all' +t -a run-all + +echo 't -b run-all' +t -b run-all +echo 't -b /dev/jb1a' +t -b /dev/jb1a + +echo 't -c run-all' +t -c run-all +echo 't -c /dev/tty' +t -c /dev/tty + +echo 't -d run-all' +t -d run-all +echo 't -d /etc' +t -d /etc +echo 't -d ""' +t -d "" +echo 'b -d ""' +b -d "" + +echo 't -e noexist' +t -e noexist +echo 't -e run-all' +t -e run-all + +echo 't -f noexist' +t -f noexist +echo 't -f /dev/tty' +t -f /dev/tty +echo 't -f run-all' +t -f run-all + +echo 't -g run-all' +t -g run-all + +touch /tmp/test.setgid +chgrp ${GROUPS[0]} /tmp/test.setgid +chmod ug+x /tmp/test.setgid +chmod g+s /tmp/test.setgid +echo 't -g /tmp/test.setgid' +t -g /tmp/test.setgid +rm -f /tmp/test.setgid + +echo 't -k run-all' +t -k run-all + +echo 't -n ""' +t -n "" +echo 't -n "hello"' +t -n "hello" + +echo 't -p run-all' +t -p run-all + +echo 't -r noexist' +t -r noexist + +if (( $UID != 0 )); then + touch /tmp/test.noread + chmod a-r /tmp/test.noread + echo 't -r /tmp/test.noread' + t -r /tmp/test.noread + rm -f /tmp/test.noread +else + echo 't -r /tmp/test.noread' + echo 1 +fi + +echo 't -r run-all' +t -r run-all + +echo 't -s noexist' +t -s noexist +echo 't -s /dev/null' +t -s /dev/null +echo 't -s run-all' +t -s run-all + +echo 't -t 20' +t -t 20 +echo 't -t 0' +t -t 0 < /dev/tty + +echo 't -u noexist' +t -u noexist + +echo 't -u run-all' +t -u run-all + +touch /tmp/test.setuid +chmod u+x /tmp/test.setuid # some systems require this to turn on setuid bit +chmod u+s /tmp/test.setuid +echo 't -u /tmp/test.setuid' +t -u /tmp/test.setuid +rm -f /tmp/test.setuid + +echo 't -w noexist' +t -w noexist + +if (( $UID != 0 )); then + touch /tmp/test.nowrite + chmod a-w /tmp/test.nowrite + echo 't -w /tmp/test.nowrite' + t -w /tmp/test.nowrite + rm -f /tmp/test.nowrite +else + echo 't -w /tmp/test.nowrite' + echo 1 +fi + +echo 't -w /dev/null' +t -w /dev/null + +echo 't -x noexist' +t -x noexist + +touch /tmp/test.exec +chmod u+x /tmp/test.exec +echo 't -x /tmp/test.exec' +t -x /tmp/test.exec +rm -f /tmp/test.exec + +touch /tmp/test.noexec +chmod u-x /tmp/test.noexec +echo 't -x /tmp/test.noexec' +t -x /tmp/test.noexec +rm -f /tmp/test.noexec + +echo 't -z ""' +t -z "" +echo 't -z "foo"' +t -z "foo" + +echo 't "foo"' +t "foo" +echo 't ""' +t "" + +touch /tmp/test.owner +echo 't -O /tmp/test.owner' +t -O /tmp/test.owner +rm -f /tmp/test.owner + +touch /tmp/test.socket +echo 't -S /tmp/test.socket' +t -S /tmp/test.socket # false +rm -f /tmp/test.socket + +touch /tmp/test.newer +echo 't -N /tmp/test.newer' +t -N /tmp/test.newer +rm -f /tmp/test.newer + +echo 't "hello" = "hello"' +t "hello" = "hello" +echo 't "hello" = "goodbye"' +t "hello" = "goodbye" + +echo 't "hello" == "hello"' +t "hello" == "hello" +echo 't "hello" == "goodbye"' +t "hello" == "goodbye" + +echo 't "hello" != "hello"' +t "hello" != "hello" +echo 't "hello" != "goodbye"' +t "hello" != "goodbye" + +echo 't "hello" < "goodbye"' +t "hello" \< "goodbye" +echo 't "hello" > "goodbye"' +t "hello" \> "goodbye" + +echo 't ! "hello" > "goodbye"' +t "! hello" \> "goodbye" + +echo 't 200 -eq 200' +t 200 -eq 200 +echo 't 34 -eq 222' +t 34 -eq 222 +echo 't -32 -eq 32' +t -32 -eq 32 + +echo 't 200 -ne 200' +t 200 -ne 200 +echo 't 34 -ne 222' +t 34 -ne 222 + +echo 't 200 -gt 200' +t 200 -gt 200 +echo 't 340 -gt 222' +t 340 -gt 222 + +echo 't 200 -ge 200' +t 200 -ge 200 +echo 't 34 -ge 222' +t 34 -ge 222 + +echo 't 200 -lt 200' +t 200 -lt 200 +echo 't 34 -lt 222' +t 34 -lt 222 + +echo 't 200 -le 200' +t 200 -le 200 +echo 't 340 -le 222' +t 340 -le 222 + +echo 't 700 -le 1000 -a -n "1" -a "20" = "20"' +t 700 -le 1000 -a -n "1" -a "20" = "20" +echo 't ! \( 700 -le 1000 -a -n "1" -a "20" = "20" \)' +t ! \( 700 -le 1000 -a -n "1" -a "20" = "20" \) + +touch /tmp/abc +sleep 2 +touch /tmp/def + +echo 't /tmp/abc -nt /tmp/def' +t /tmp/abc -nt /tmp/def +echo 't /tmp/abc -ot /tmp/def' +t /tmp/abc -ot /tmp/def +echo 't /tmp/def -nt /tmp/abc' +t /tmp/def -nt /tmp/abc +echo 't /tmp/def -ot /tmp/abc' +t /tmp/def -ot /tmp/abc + +echo 't /tmp/abc -ef /tmp/def' +t /tmp/abc -ef /tmp/def +ln /tmp/abc /tmp/ghi +echo 't /tmp/abc -ef /tmp/ghi' +t /tmp/abc -ef /tmp/ghi + +rm /tmp/abc /tmp/def /tmp/ghi + +echo 't -r /dev/fd/0' +t -r /dev/fd/0 +echo 't -w /dev/fd/1' +t -w /dev/fd/1 +echo 't -w /dev/fd/2' +t -w /dev/fd/2 + +echo 't -r /dev/stdin' +t -r /dev/stdin +echo 't -w /dev/stdout' +t -w /dev/stdout +echo 't -w /dev/stderr' +t -w /dev/stderr + +echo 't' +t +echo 'b' +b + +echo 't 12 -eq 34' +t 12 -eq 34 +echo 't ! 12 -eq 34' +t ! 12 -eq 34 + +echo 't -n abcd -o aaa' +t -n abcd -o aaa +echo 't -n abcd -o -z aaa' +t -n abcd -o -z aaa + +echo 't -n abcd -a aaa' +t -n abcd -a aaa +echo 't -n abcd -a -z aaa' +t -n abcd -a -z aaa + +set +o allexport +echo 't -o allexport' +t -o allexport +echo 't ! -o allexport' +t ! -o allexport + +echo 't xx -a yy' +t xx -a yy +echo 't xx -o ""' +t xx -o "" +echo 't xx -a ""' +t xx -a "" + +echo 't -X -a -X' +t -X -a -X +echo 't -X -o -X' +t -X -o -X +echo 't -X -o ""' +t -X -o "" +echo 't -X -a ""' +t -X -a "" +echo 't "" -a -X' +t "" -a -X +echo 't "" -o -X' +t "" -o -X +echo 't "" -a ""' +t "" -a "" +echo 't "" -o ""' +t "" -o "" +echo 't true -o -X' +t true -o -X +echo 't true -a -X' +t true -a -X + +echo 't ( -E )' +t \( -E \) +echo 't ( "" )' +t \( "" \) + +z=42 + +echo 't ! -z "$z"' +t ! -z "$z" + +echo 't ! -n "$z"' +t ! -n "$z" + +zero= +echo 't "$zero"' +t "$zero" +echo 't ! "$zero"' +t ! "$zero" +echo 'b "$zero"' +b "$zero" +echo 'b ! "$zero"' +b ! "$zero" + +touch /tmp/test.group +chgrp ${GROUPS[0]} /tmp/test.group +echo 't -G /tmp/test.group' +t -G /tmp/test.group +rm /tmp/test.group + +case "${THIS_SH}" in +/*) SHNAME=${THIS_SH} ;; +*) SHNAME=${PWD}/${THIS_SH} ;; +esac + +if ln -s ${SHNAME} /tmp/test.symlink 2>/dev/null; then + chgrp ${GROUPS[0]} /tmp/test.symlink 2>/dev/null + echo 't -h /tmp/test.symlink' + t -h /tmp/test.symlink + # some systems don't let you remove this + rm -f /tmp/test.symlink 2>/dev/null +else + echo 't -h /tmp/test.symlink' + echo 0 +fi + +# arithmetic constant errors +echo "t 4+3 -eq 7" +t 4+3 -eq 7 +echo "b 4-5 -eq 7" +b 4+3 -eq 7 + +echo "t 9 -eq 4+5" +t 9 -eq 4+5 +echo "b 9 -eq 4+5" +b 9 -eq 4+5 + +A=7 +echo "t A -eq 7" +t A -eq 7 +echo "b A -eq 7" +b A -eq 7 + +B=9 +echo "t 9 -eq B" +t 9 -eq B +echo "b 9 -eq B" +b 9 -eq B + +# badly formed expressions +echo 't ( 1 = 2' +t \( 1 = 2 +echo 'b ( 1 = 2' +b \( 1 = 2 + +# more errors +t a b +t a b c +t -A v +# too many arguments -- argument expected is also reasonable +t 4 -eq 4 -a 2 -ne 5 -a 4 -ne +# too many arguments +t 4 -eq 4 -a 3 4 + +[ +echo $? + +t \( \) + +# non-numeric arguments to `test -t' should return failure -- fix in 2.05 +echo 't -t a' +t -t a +echo 't -t addsds' +t -t addsds +echo 't -t 42' +t -t 42 +echo 't -t /dev/tty' +t -t /dev/tty +echo 't -t /dev/tty4' +t -t /dev/tty4 +echo 't -t /dev/tty4444444...' +t -t /dev/tty4444444... + +# fixed in bash-4.0-beta +t -t ' ' @@ -1,7 +1,7 @@ /* trap.c -- Not the trap command, but useful functions for manipulating those objects. The trap command is in builtins/trap.def. */ -/* Copyright (C) 1987-2009 Free Software Foundation, Inc. +/* Copyright (C) 1987-2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. @@ -608,6 +608,15 @@ get_original_signal (sig) } void +get_all_original_signals () +{ + register int i; + + for (i = 1; i < NSIG; i++) + GET_ORIGINAL_SIGNAL (i); +} + +void set_original_signal (sig, handler) int sig; SigHandler *handler; @@ -1,6 +1,6 @@ /* trap.h -- data structures used in the trap mechanism. */ -/* Copyright (C) 1993-2009 Free Software Foundation, Inc. +/* Copyright (C) 1993-2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. @@ -88,6 +88,8 @@ extern void free_trap_strings __P((void)); extern void reset_signal_handlers __P((void)); extern void restore_original_signals __P((void)); +extern void get_all_original_signals __P((void)); + extern char *signal_name __P((int)); extern int decode_signal __P((char *, int)); diff --git a/variables.c b/variables.c index f1e7ca4c..284731e6 100644 --- a/variables.c +++ b/variables.c @@ -1,6 +1,6 @@ /* variables.c -- Functions for hacking shell variables. */ -/* Copyright (C) 1987-2009 Free Software Foundation, Inc. +/* Copyright (C) 1987-2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. @@ -99,6 +99,7 @@ extern char *command_execution_string; extern time_t shell_start_time; extern int assigning_in_environment; extern int executing_builtin; +extern int funcnest_max; #if defined (READLINE) extern int no_line_editing; @@ -1790,6 +1791,20 @@ find_variable_internal (name, force_tempenv) return (var->dynamic_value ? (*(var->dynamic_value)) (var) : var); } +SHELL_VAR * +find_global_variable (name) + const char *name; +{ + SHELL_VAR *var; + + var = var_lookup (name, global_variables); + + if (var == 0) + return ((SHELL_VAR *)NULL); + + return (var->dynamic_value ? (*(var->dynamic_value)) (var) : var); +} + /* Look up the variable entry named NAME. Returns the entry or NULL. */ SHELL_VAR * find_variable (name) @@ -4108,6 +4123,8 @@ static struct name_and_function special_vars[] = { { "COMP_WORDBREAKS", sv_comp_wordbreaks }, #endif + { "FUNCNEST", sv_funcnest }, + { "GLOBIGNORE", sv_globignore }, #if defined (HISTORY) @@ -4279,6 +4296,22 @@ sv_mail (name) } } +void +sv_funcnest (name) + char *name; +{ + SHELL_VAR *v; + intmax_t num; + + v = find_variable (name); + if (v == 0) + funcnest_max = 0; + else if (legal_number (value_cell (v), &num) == 0) + funcnest_max = 0; + else + funcnest_max = num; +} + /* What to do when GLOBIGNORE changes. */ void sv_globignore (name) diff --git a/variables.c~ b/variables.c~ index bccc81d3..baa6c058 100644 --- a/variables.c~ +++ b/variables.c~ @@ -99,6 +99,7 @@ extern char *command_execution_string; extern time_t shell_start_time; extern int assigning_in_environment; extern int executing_builtin; +extern int funcnest_max; #if defined (READLINE) extern int no_line_editing; @@ -1790,6 +1791,20 @@ find_variable_internal (name, force_tempenv) return (var->dynamic_value ? (*(var->dynamic_value)) (var) : var); } +SHELL_VAR * +find_global_variable (name) + const char *name; +{ + SHELL_VAR *var; + + var = var_lookup (name, global_variables); + + if (var == 0) + return ((SHELL_VAR *)NULL); + + return (var->dynamic_value ? (*(var->dynamic_value)) (var) : var); +} + /* Look up the variable entry named NAME. Returns the entry or NULL. */ SHELL_VAR * find_variable (name) @@ -4108,6 +4123,8 @@ static struct name_and_function special_vars[] = { { "COMP_WORDBREAKS", sv_comp_wordbreaks }, #endif + { "FUNCNEST", sv_funcnest }, + { "GLOBIGNORE", sv_globignore }, #if defined (HISTORY) @@ -4279,6 +4296,22 @@ sv_mail (name) } } +void +sv_funcnest (name) + char *name; +{ + SHELL_VAR *v; + intmax_t num; + + v = find_variable (name); + if (v == 0) + funcnest_max = 0; + else if (legal_number (value_cell (v), &num) == 0) + funcnest_max = 0; + else + funcnest_max = num; +} + /* What to do when GLOBIGNORE changes. */ void sv_globignore (name) @@ -4647,12 +4680,12 @@ save_pipestatus_array () ARRAY *a, *a2; v = find_variable ("PIPESTATUS"); - if (v == 0 || array_p (v) == 0) + if (v == 0 || array_p (v) == 0 || array_cell (v) == 0) return ((ARRAY *)NULL); a = array_cell (v); - a2 = a ? array_copy (a) : a; -itrace("save_pipestatus_array: returning 0x%x", a2); + a2 = array_copy (array_cell (v)); + return a2; } @@ -4664,12 +4697,13 @@ restore_pipestatus_array (a) ARRAY *a2; v = find_variable ("PIPESTATUS"); - if (v == 0 || array_p (v) == 0) - return ((ARRAY *)NULL); + /* XXX - should we still assign even if existing value is NULL? */ + if (v == 0 || array_p (v) == 0 || array_cell (v) == 0) + return; a2 = array_cell (v); var_setarray (v, a); -itrace("restore_pipestatus_array: restored 0x%x", a); + array_dispose (a2); } #endif diff --git a/variables.h b/variables.h index 34cf4d2e..637539be 100644 --- a/variables.h +++ b/variables.h @@ -1,6 +1,6 @@ /* variables.h -- data structures for shell variables. */ -/* Copyright (C) 1987-2009 Free Software Foundation, Inc. +/* Copyright (C) 1987-2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. @@ -235,6 +235,7 @@ extern FUNCTION_DEF *find_function_def __P((const char *)); extern SHELL_VAR *find_variable __P((const char *)); extern SHELL_VAR *find_variable_internal __P((const char *, int)); extern SHELL_VAR *find_tempenv_variable __P((const char *)); +extern SHELL_VAR *find_global_variable __P((const char *)); extern SHELL_VAR *copy_variable __P((SHELL_VAR *)); extern SHELL_VAR *make_local_variable __P((const char *)); extern SHELL_VAR *bind_variable __P((const char *, char *, int)); @@ -352,6 +353,7 @@ extern int get_random_number __P((void)); extern void sv_ifs __P((char *)); extern void sv_path __P((char *)); extern void sv_mail __P((char *)); +extern void sv_funcnest __P((char *)); extern void sv_globignore __P((char *)); extern void sv_ignoreeof __P((char *)); extern void sv_strict_posix __P((char *)); diff --git a/variables.h~ b/variables.h~ index ec1bfdcf..513fda3b 100644 --- a/variables.h~ +++ b/variables.h~ @@ -235,6 +235,7 @@ extern FUNCTION_DEF *find_function_def __P((const char *)); extern SHELL_VAR *find_variable __P((const char *)); extern SHELL_VAR *find_variable_internal __P((const char *, int)); extern SHELL_VAR *find_tempenv_variable __P((const char *)); +extern SHELL_VAR *find_global_variable __P((const char *)); extern SHELL_VAR *copy_variable __P((SHELL_VAR *)); extern SHELL_VAR *make_local_variable __P((const char *)); extern SHELL_VAR *bind_variable __P((const char *, char *, int)); @@ -331,6 +332,8 @@ extern SHELL_VAR *make_new_assoc_variable __P((char *)); extern SHELL_VAR *make_local_assoc_variable __P((char *)); extern void set_pipestatus_array __P((int *, int)); +extern ARRAY *save_pipestatus_array __P((void)); +extern void restore_pipestatus_array __P((ARRAY *)); #endif extern void set_pipestatus_from_exit __P((int)); @@ -350,6 +353,7 @@ extern int get_random_number __P((void)); extern void sv_ifs __P((char *)); extern void sv_path __P((char *)); extern void sv_mail __P((char *)); +extern void sv_funcnest __P((char *)); extern void sv_globignore __P((char *)); extern void sv_ignoreeof __P((char *)); extern void sv_strict_posix __P((char *)); |