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