Imported from ../bash-3.0.tar.gz.
[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-2003 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 2, 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, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
21
22 $PRODUCES shift.c
23
24 #include <config.h>
25
26 #if defined (HAVE_UNISTD_H)
27 #  ifdef _MINIX
28 #    include <sys/types.h>
29 #  endif
30 #  include <unistd.h>
31 #endif
32
33 #include "../bashansi.h"
34 #include "../bashintl.h"
35
36 #include "../shell.h"
37 #include "common.h"
38
39 $BUILTIN shift
40 $FUNCTION shift_builtin
41 $SHORT_DOC shift [n]
42 The positional parameters from $N+1 ... are renamed to $1 ...  If N is
43 not given, it is assumed to be 1.
44 $END
45
46 int print_shift_error;
47
48 /* Shift the arguments ``left''.  Shift DOLLAR_VARS down then take one
49    off of REST_OF_ARGS and place it into DOLLAR_VARS[9].  If LIST has
50    anything in it, it is a number which says where to start the
51    shifting.  Return > 0 if `times' > $#, otherwise 0. */
52 int
53 shift_builtin (list)
54      WORD_LIST *list;
55 {
56   intmax_t times;
57   register int count;
58   WORD_LIST *temp;
59
60   times = get_numeric_arg (list, 0);
61
62   if (times == 0)
63     return (EXECUTION_SUCCESS);
64   else if (times < 0)
65     {
66       sh_erange (list ? list->word->word : NULL, _("shift count"));
67       return (EXECUTION_FAILURE);
68     }
69   else if (times > number_of_args ())
70     {
71       if (print_shift_error)
72         sh_erange (list ? list->word->word : NULL, _("shift count"));
73       return (EXECUTION_FAILURE);
74     }
75
76   while (times-- > 0)
77     {
78       if (dollar_vars[1])
79         free (dollar_vars[1]);
80
81       for (count = 1; count < 9; count++)
82         dollar_vars[count] = dollar_vars[count + 1];
83
84       if (rest_of_args)
85         {
86           temp = rest_of_args;
87           dollar_vars[9] = savestring (temp->word->word);
88           rest_of_args = rest_of_args->next;
89           temp->next = (WORD_LIST *)NULL;
90           dispose_words (temp);
91         }
92       else
93         dollar_vars[9] = (char *)NULL;
94     }
95   return (EXECUTION_SUCCESS);
96 }