Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1 | This file is shift.def, from which is created shift.c. |
| 2 | It implements the builtin "shift" in Bash. |
| 3 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 4 | Copyright (C) 1987-2009 Free Software Foundation, Inc. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5 | |
| 6 | This file is part of GNU Bash, the Bourne Again SHell. |
| 7 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 8 | Bash is free software: you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation, either version 3 of the License, or |
| 11 | (at your option) any later version. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 12 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 13 | Bash is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 17 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 18 | You should have received a copy of the GNU General Public License |
| 19 | along with Bash. If not, see <http://www.gnu.org/licenses/>. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 20 | |
| 21 | $PRODUCES shift.c |
| 22 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 23 | #include <config.h> |
| 24 | |
| 25 | #if defined (HAVE_UNISTD_H) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 26 | # ifdef _MINIX |
| 27 | # include <sys/types.h> |
| 28 | # endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 29 | # include <unistd.h> |
| 30 | #endif |
| 31 | |
| 32 | #include "../bashansi.h" |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 33 | #include "../bashintl.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 34 | |
| 35 | #include "../shell.h" |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 36 | #include "common.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 37 | |
| 38 | $BUILTIN shift |
| 39 | $FUNCTION shift_builtin |
| 40 | $SHORT_DOC shift [n] |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 41 | Shift positional parameters. |
| 42 | |
| 43 | Rename the positional parameters $N+1,$N+2 ... to $1,$2 ... If N is |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 44 | not given, it is assumed to be 1. |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 45 | |
| 46 | Exit Status: |
| 47 | Returns success unless N is negative or greater than $#. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 48 | $END |
| 49 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 50 | int print_shift_error; |
| 51 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 52 | /* Shift the arguments ``left''. Shift DOLLAR_VARS down then take one |
| 53 | off of REST_OF_ARGS and place it into DOLLAR_VARS[9]. If LIST has |
| 54 | anything in it, it is a number which says where to start the |
| 55 | shifting. Return > 0 if `times' > $#, otherwise 0. */ |
| 56 | int |
| 57 | shift_builtin (list) |
| 58 | WORD_LIST *list; |
| 59 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 60 | intmax_t times; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 61 | register int count; |
| 62 | WORD_LIST *temp; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 63 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 64 | if (get_numeric_arg (list, 0, ×) == 0) |
| 65 | return (EXECUTION_FAILURE); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 66 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 67 | if (times == 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 68 | return (EXECUTION_SUCCESS); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 69 | else if (times < 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 70 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 71 | sh_erange (list ? list->word->word : NULL, _("shift count")); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 72 | return (EXECUTION_FAILURE); |
| 73 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 74 | else if (times > number_of_args ()) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 75 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 76 | if (print_shift_error) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 77 | sh_erange (list ? list->word->word : NULL, _("shift count")); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 78 | return (EXECUTION_FAILURE); |
| 79 | } |
| 80 | |
| 81 | while (times-- > 0) |
| 82 | { |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 83 | if (dollar_vars[1]) |
| 84 | free (dollar_vars[1]); |
| 85 | |
| 86 | for (count = 1; count < 9; count++) |
| 87 | dollar_vars[count] = dollar_vars[count + 1]; |
| 88 | |
| 89 | if (rest_of_args) |
| 90 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 91 | temp = rest_of_args; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 92 | dollar_vars[9] = savestring (temp->word->word); |
| 93 | rest_of_args = rest_of_args->next; |
| 94 | temp->next = (WORD_LIST *)NULL; |
| 95 | dispose_words (temp); |
| 96 | } |
| 97 | else |
| 98 | dollar_vars[9] = (char *)NULL; |
| 99 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 100 | return (EXECUTION_SUCCESS); |
| 101 | } |