blob: 32130862a474ec2595bb8af9e351398538befbb6 [file] [log] [blame]
Jari Aalto726f6381996-08-26 18:22:31 +00001This file is shift.def, from which is created shift.c.
2It implements the builtin "shift" in Bash.
3
Jari Aalto31859422009-01-12 13:36:28 +00004Copyright (C) 1987-2009 Free Software Foundation, Inc.
Jari Aalto726f6381996-08-26 18:22:31 +00005
6This file is part of GNU Bash, the Bourne Again SHell.
7
Jari Aalto31859422009-01-12 13:36:28 +00008Bash is free software: you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation, either version 3 of the License, or
11(at your option) any later version.
Jari Aalto726f6381996-08-26 18:22:31 +000012
Jari Aalto31859422009-01-12 13:36:28 +000013Bash is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
Jari Aalto726f6381996-08-26 18:22:31 +000017
Jari Aalto31859422009-01-12 13:36:28 +000018You should have received a copy of the GNU General Public License
19along with Bash. If not, see <http://www.gnu.org/licenses/>.
Jari Aalto726f6381996-08-26 18:22:31 +000020
21$PRODUCES shift.c
22
Jari Aaltoccc6cda1996-12-23 17:02:34 +000023#include <config.h>
24
25#if defined (HAVE_UNISTD_H)
Jari Aaltocce855b1998-04-17 19:52:44 +000026# ifdef _MINIX
27# include <sys/types.h>
28# endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +000029# include <unistd.h>
30#endif
31
32#include "../bashansi.h"
Jari Aaltob80f6442004-07-27 13:29:18 +000033#include "../bashintl.h"
Jari Aalto726f6381996-08-26 18:22:31 +000034
35#include "../shell.h"
Jari Aaltoccc6cda1996-12-23 17:02:34 +000036#include "common.h"
Jari Aalto726f6381996-08-26 18:22:31 +000037
38$BUILTIN shift
39$FUNCTION shift_builtin
40$SHORT_DOC shift [n]
Jari Aalto31859422009-01-12 13:36:28 +000041Shift positional parameters.
42
43Rename the positional parameters $N+1,$N+2 ... to $1,$2 ... If N is
Jari Aalto726f6381996-08-26 18:22:31 +000044not given, it is assumed to be 1.
Jari Aalto31859422009-01-12 13:36:28 +000045
46Exit Status:
47Returns success unless N is negative or greater than $#.
Jari Aalto726f6381996-08-26 18:22:31 +000048$END
49
Jari Aaltoccc6cda1996-12-23 17:02:34 +000050int print_shift_error;
51
Jari Aalto726f6381996-08-26 18:22:31 +000052/* 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. */
56int
57shift_builtin (list)
58 WORD_LIST *list;
59{
Jari Aalto7117c2d2002-07-17 14:10:11 +000060 intmax_t times;
Jari Aaltoccc6cda1996-12-23 17:02:34 +000061 register int count;
62 WORD_LIST *temp;
Jari Aalto726f6381996-08-26 18:22:31 +000063
Jari Aalto31859422009-01-12 13:36:28 +000064 if (get_numeric_arg (list, 0, &times) == 0)
65 return (EXECUTION_FAILURE);
Jari Aalto726f6381996-08-26 18:22:31 +000066
Jari Aaltoccc6cda1996-12-23 17:02:34 +000067 if (times == 0)
Jari Aalto726f6381996-08-26 18:22:31 +000068 return (EXECUTION_SUCCESS);
Jari Aaltoccc6cda1996-12-23 17:02:34 +000069 else if (times < 0)
Jari Aalto726f6381996-08-26 18:22:31 +000070 {
Jari Aaltob80f6442004-07-27 13:29:18 +000071 sh_erange (list ? list->word->word : NULL, _("shift count"));
Jari Aalto726f6381996-08-26 18:22:31 +000072 return (EXECUTION_FAILURE);
73 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +000074 else if (times > number_of_args ())
Jari Aalto726f6381996-08-26 18:22:31 +000075 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +000076 if (print_shift_error)
Jari Aaltob80f6442004-07-27 13:29:18 +000077 sh_erange (list ? list->word->word : NULL, _("shift count"));
Jari Aalto726f6381996-08-26 18:22:31 +000078 return (EXECUTION_FAILURE);
79 }
80
81 while (times-- > 0)
82 {
Jari Aalto726f6381996-08-26 18:22:31 +000083 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 Aaltoccc6cda1996-12-23 17:02:34 +000091 temp = rest_of_args;
Jari Aalto726f6381996-08-26 18:22:31 +000092 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 Aalto726f6381996-08-26 18:22:31 +0000100 return (EXECUTION_SUCCESS);
101}