]> git.ipfire.org Git - thirdparty/bash.git/blame - builtins/shift.def
Imported from ../bash-2.04.tar.gz.
[thirdparty/bash.git] / builtins / shift.def
CommitLineData
726f6388
JA
1This file is shift.def, from which is created shift.c.
2It implements the builtin "shift" in Bash.
3
4Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
5
6This file is part of GNU Bash, the Bourne Again SHell.
7
8Bash is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
bb70624e 10Software Foundation; either version 2, or (at your option) any later
726f6388
JA
11version.
12
13Bash is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
17
18You should have received a copy of the GNU General Public License along
19with Bash; see the file COPYING. If not, write to the Free Software
bb70624e 20Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
726f6388
JA
21
22$PRODUCES shift.c
23
ccc6cda3
JA
24#include <config.h>
25
26#if defined (HAVE_UNISTD_H)
cce855bc
JA
27# ifdef _MINIX
28# include <sys/types.h>
29# endif
ccc6cda3
JA
30# include <unistd.h>
31#endif
32
33#include "../bashansi.h"
726f6388
JA
34
35#include "../shell.h"
ccc6cda3 36#include "common.h"
726f6388
JA
37
38$BUILTIN shift
39$FUNCTION shift_builtin
40$SHORT_DOC shift [n]
41The positional parameters from $N+1 ... are renamed to $1 ... If N is
42not given, it is assumed to be 1.
43$END
44
ccc6cda3
JA
45int print_shift_error;
46
726f6388
JA
47/* Shift the arguments ``left''. Shift DOLLAR_VARS down then take one
48 off of REST_OF_ARGS and place it into DOLLAR_VARS[9]. If LIST has
49 anything in it, it is a number which says where to start the
50 shifting. Return > 0 if `times' > $#, otherwise 0. */
51int
52shift_builtin (list)
53 WORD_LIST *list;
54{
ccc6cda3
JA
55 int times;
56 register int count;
57 WORD_LIST *temp;
726f6388 58
d166f048 59 times = get_numeric_arg (list, 0);
726f6388 60
ccc6cda3 61 if (times == 0)
726f6388 62 return (EXECUTION_SUCCESS);
ccc6cda3 63 else if (times < 0)
726f6388
JA
64 {
65 builtin_error ("shift count must be >= 0");
66 return (EXECUTION_FAILURE);
67 }
ccc6cda3 68 else if (times > number_of_args ())
726f6388 69 {
ccc6cda3
JA
70 if (print_shift_error)
71 builtin_error ("shift count must be <= $#");
726f6388
JA
72 return (EXECUTION_FAILURE);
73 }
74
75 while (times-- > 0)
76 {
726f6388
JA
77 if (dollar_vars[1])
78 free (dollar_vars[1]);
79
80 for (count = 1; count < 9; count++)
81 dollar_vars[count] = dollar_vars[count + 1];
82
83 if (rest_of_args)
84 {
ccc6cda3 85 temp = rest_of_args;
726f6388
JA
86 dollar_vars[9] = savestring (temp->word->word);
87 rest_of_args = rest_of_args->next;
88 temp->next = (WORD_LIST *)NULL;
89 dispose_words (temp);
90 }
91 else
92 dollar_vars[9] = (char *)NULL;
93 }
726f6388
JA
94 return (EXECUTION_SUCCESS);
95}