i-bash/builtins/shift.def

107 lines
2.6 KiB
Modula-2
Raw Normal View History

1996-08-26 18:22:31 +00:00
This file is shift.def, from which is created shift.c.
It implements the builtin "shift" in Bash.
2009-01-12 13:36:28 +00:00
Copyright (C) 1987-2009 Free Software Foundation, Inc.
1996-08-26 18:22:31 +00:00
This file is part of GNU Bash, the Bourne Again SHell.
2009-01-12 13:36:28 +00:00
Bash is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
1996-08-26 18:22:31 +00:00
2009-01-12 13:36:28 +00:00
Bash is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
1996-08-26 18:22:31 +00:00
2009-01-12 13:36:28 +00:00
You should have received a copy of the GNU General Public License
along with Bash. If not, see <http://www.gnu.org/licenses/>.
1996-08-26 18:22:31 +00:00
$PRODUCES shift.c
1996-12-23 17:02:34 +00:00
#include <config.h>
#if defined (HAVE_UNISTD_H)
1998-04-17 19:52:44 +00:00
# ifdef _MINIX
# include <sys/types.h>
# endif
1996-12-23 17:02:34 +00:00
# include <unistd.h>
#endif
#include "../bashansi.h"
2004-07-27 13:29:18 +00:00
#include "../bashintl.h"
1996-08-26 18:22:31 +00:00
#include "../shell.h"
1996-12-23 17:02:34 +00:00
#include "common.h"
1996-08-26 18:22:31 +00:00
$BUILTIN shift
$FUNCTION shift_builtin
$SHORT_DOC shift [n]
2009-01-12 13:36:28 +00:00
Shift positional parameters.
Rename the positional parameters $N+1,$N+2 ... to $1,$2 ... If N is
1996-08-26 18:22:31 +00:00
not given, it is assumed to be 1.
2009-01-12 13:36:28 +00:00
Exit Status:
Returns success unless N is negative or greater than $#.
1996-08-26 18:22:31 +00:00
$END
1996-12-23 17:02:34 +00:00
int print_shift_error;
1996-08-26 18:22:31 +00:00
/* Shift the arguments ``left''. Shift DOLLAR_VARS down then take one
off of REST_OF_ARGS and place it into DOLLAR_VARS[9]. If LIST has
anything in it, it is a number which says where to start the
shifting. Return > 0 if `times' > $#, otherwise 0. */
int
shift_builtin (list)
WORD_LIST *list;
{
2002-07-17 14:10:11 +00:00
intmax_t times;
1996-12-23 17:02:34 +00:00
register int count;
WORD_LIST *temp;
1996-08-26 18:22:31 +00:00
CHECK_HELPOPT (list);
2009-01-12 13:36:28 +00:00
if (get_numeric_arg (list, 0, &times) == 0)
return (EXECUTION_FAILURE);
1996-08-26 18:22:31 +00:00
1996-12-23 17:02:34 +00:00
if (times == 0)
1996-08-26 18:22:31 +00:00
return (EXECUTION_SUCCESS);
1996-12-23 17:02:34 +00:00
else if (times < 0)
1996-08-26 18:22:31 +00:00
{
2004-07-27 13:29:18 +00:00
sh_erange (list ? list->word->word : NULL, _("shift count"));
1996-08-26 18:22:31 +00:00
return (EXECUTION_FAILURE);
}
1996-12-23 17:02:34 +00:00
else if (times > number_of_args ())
1996-08-26 18:22:31 +00:00
{
1996-12-23 17:02:34 +00:00
if (print_shift_error)
2004-07-27 13:29:18 +00:00
sh_erange (list ? list->word->word : NULL, _("shift count"));
1996-08-26 18:22:31 +00:00
return (EXECUTION_FAILURE);
}
while (times-- > 0)
{
if (dollar_vars[1])
free (dollar_vars[1]);
for (count = 1; count < 9; count++)
dollar_vars[count] = dollar_vars[count + 1];
if (rest_of_args)
{
1996-12-23 17:02:34 +00:00
temp = rest_of_args;
1996-08-26 18:22:31 +00:00
dollar_vars[9] = savestring (temp->word->word);
rest_of_args = rest_of_args->next;
temp->next = (WORD_LIST *)NULL;
dispose_words (temp);
}
else
dollar_vars[9] = (char *)NULL;
}
invalidate_cached_quoted_dollar_at ();
1996-08-26 18:22:31 +00:00
return (EXECUTION_SUCCESS);
}