i-bash/builtins/alias.def

230 lines
4.8 KiB
Modula-2
Raw Normal View History

1996-08-26 18:22:31 +00:00
This file is alias.def, from which is created alias.c
It implements the builtins "alias" and "unalias" in Bash.
2004-07-27 13:29:18 +00:00
Copyright (C) 1987-2004 Free Software Foundation, Inc.
1996-08-26 18:22:31 +00:00
This file is part of GNU Bash, the Bourne Again SHell.
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
2000-03-17 21:46:59 +00:00
Software Foundation; either version 2, or (at your option) any later
1996-08-26 18:22:31 +00:00
version.
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.
You should have received a copy of the GNU General Public License along
with Bash; see the file COPYING. If not, write to the Free Software
2000-03-17 21:46:59 +00:00
Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
1996-08-26 18:22:31 +00:00
$BUILTIN alias
$FUNCTION alias_builtin
$DEPENDS_ON ALIAS
$PRODUCES alias.c
1996-12-23 17:02:34 +00:00
$SHORT_DOC alias [-p] [name[=value] ... ]
`alias' with no arguments or with the -p option prints the list
of aliases in the form alias NAME=VALUE on standard output.
Otherwise, an alias is defined for each NAME whose VALUE is given.
A trailing space in VALUE causes the next word to be checked for
alias substitution when the alias is expanded. Alias returns
true unless a NAME is given for which no alias has been defined.
1996-08-26 18:22:31 +00:00
$END
1996-12-23 17:02:34 +00:00
#include <config.h>
1996-08-26 18:22:31 +00:00
#if defined (ALIAS)
1996-12-23 17:02:34 +00:00
#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
1997-06-05 14:59:13 +00:00
# include "../bashansi.h"
2004-07-27 13:29:18 +00:00
# include "../bashintl.h"
1997-06-05 14:59:13 +00:00
1996-08-26 18:22:31 +00:00
# include <stdio.h>
# include "../shell.h"
# include "../alias.h"
# include "common.h"
1996-12-23 17:02:34 +00:00
# include "bashgetopt.h"
1996-08-26 18:22:31 +00:00
2004-07-27 13:29:18 +00:00
/* Flags for print_alias */
#define AL_REUSABLE 0x01
static void print_alias __P((alias_t *, int));
extern int posixly_correct;
1996-08-26 18:22:31 +00:00
/* Hack the alias command in a Korn shell way. */
1996-12-23 17:02:34 +00:00
int
1996-08-26 18:22:31 +00:00
alias_builtin (list)
WORD_LIST *list;
{
2004-07-27 13:29:18 +00:00
int any_failed, offset, pflag, dflags;
1996-12-23 17:02:34 +00:00
alias_t **alias_list, *t;
char *name, *value;
1996-08-26 18:22:31 +00:00
2004-07-27 13:29:18 +00:00
dflags = posixly_correct ? 0 : AL_REUSABLE;
1996-12-23 17:02:34 +00:00
pflag = 0;
reset_internal_getopt ();
while ((offset = internal_getopt (list, "p")) != -1)
1996-08-26 18:22:31 +00:00
{
1996-12-23 17:02:34 +00:00
switch (offset)
{
case 'p':
pflag = 1;
2004-07-27 13:29:18 +00:00
dflags |= AL_REUSABLE;
1996-12-23 17:02:34 +00:00
break;
default:
builtin_usage ();
return (EX_USAGE);
}
}
1996-08-26 18:22:31 +00:00
1996-12-23 17:02:34 +00:00
list = loptend;
if (list == 0 || pflag)
{
if (aliases == 0)
1998-04-17 19:52:44 +00:00
return (EXECUTION_SUCCESS);
1996-08-26 18:22:31 +00:00
alias_list = all_aliases ();
1996-12-23 17:02:34 +00:00
if (alias_list == 0)
1998-04-17 19:52:44 +00:00
return (EXECUTION_SUCCESS);
1996-08-26 18:22:31 +00:00
1996-12-23 17:02:34 +00:00
for (offset = 0; alias_list[offset]; offset++)
2004-07-27 13:29:18 +00:00
print_alias (alias_list[offset], dflags);
1996-08-26 18:22:31 +00:00
free (alias_list); /* XXX - Do not free the strings. */
1996-12-23 17:02:34 +00:00
if (list == 0)
return (EXECUTION_SUCCESS);
1996-08-26 18:22:31 +00:00
}
1996-12-23 17:02:34 +00:00
any_failed = 0;
while (list)
1996-08-26 18:22:31 +00:00
{
1996-12-23 17:02:34 +00:00
name = list->word->word;
1996-08-26 18:22:31 +00:00
1996-12-23 17:02:34 +00:00
for (offset = 0; name[offset] && name[offset] != '='; offset++)
;
1996-08-26 18:22:31 +00:00
1996-12-23 17:02:34 +00:00
if (offset && name[offset] == '=')
{
name[offset] = '\0';
value = name + offset + 1;
1996-08-26 18:22:31 +00:00
2004-07-27 13:29:18 +00:00
if (legal_alias_name (name, 0) == 0)
{
builtin_error (_("`%s': invalid alias name"), name);
any_failed++;
}
else
add_alias (name, value);
1996-12-23 17:02:34 +00:00
}
else
{
t = find_alias (name);
if (t)
2004-07-27 13:29:18 +00:00
print_alias (t, dflags);
1996-08-26 18:22:31 +00:00
else
{
2002-07-17 14:10:11 +00:00
sh_notfound (name);
1996-12-23 17:02:34 +00:00
any_failed++;
1996-08-26 18:22:31 +00:00
}
}
1996-12-23 17:02:34 +00:00
list = list->next;
1996-08-26 18:22:31 +00:00
}
1996-12-23 17:02:34 +00:00
return (any_failed ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
1996-08-26 18:22:31 +00:00
}
#endif /* ALIAS */
$BUILTIN unalias
$FUNCTION unalias_builtin
$DEPENDS_ON ALIAS
2004-07-27 13:29:18 +00:00
$SHORT_DOC unalias [-a] name [name ...]
1996-08-26 18:22:31 +00:00
Remove NAMEs from the list of defined aliases. If the -a option is given,
then remove all alias definitions.
$END
#if defined (ALIAS)
/* Remove aliases named in LIST from the aliases database. */
1996-12-23 17:02:34 +00:00
int
1996-08-26 18:22:31 +00:00
unalias_builtin (list)
register WORD_LIST *list;
{
1996-12-23 17:02:34 +00:00
register alias_t *alias;
int opt, aflag;
1996-08-26 18:22:31 +00:00
1996-12-23 17:02:34 +00:00
aflag = 0;
reset_internal_getopt ();
while ((opt = internal_getopt (list, "a")) != -1)
1996-08-26 18:22:31 +00:00
{
1996-12-23 17:02:34 +00:00
switch (opt)
1996-08-26 18:22:31 +00:00
{
1996-12-23 17:02:34 +00:00
case 'a':
aflag = 1;
1996-08-26 18:22:31 +00:00
break;
1996-12-23 17:02:34 +00:00
default:
builtin_usage ();
return (EX_USAGE);
1996-08-26 18:22:31 +00:00
}
}
1996-12-23 17:02:34 +00:00
list = loptend;
if (aflag)
{
delete_all_aliases ();
return (EXECUTION_SUCCESS);
}
2004-07-27 13:29:18 +00:00
if (list == 0)
{
builtin_usage ();
return (EX_USAGE);
}
1996-12-23 17:02:34 +00:00
aflag = 0;
1996-08-26 18:22:31 +00:00
while (list)
{
alias = find_alias (list->word->word);
if (alias)
remove_alias (alias->name);
else
{
2002-07-17 14:10:11 +00:00
sh_notfound (list->word->word);
1996-12-23 17:02:34 +00:00
aflag++;
1996-08-26 18:22:31 +00:00
}
list = list->next;
}
1996-12-23 17:02:34 +00:00
return (aflag ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
1996-08-26 18:22:31 +00:00
}
/* Output ALIAS in such a way as to allow it to be read back in. */
static void
2004-07-27 13:29:18 +00:00
print_alias (alias, flags)
1996-12-23 17:02:34 +00:00
alias_t *alias;
2004-07-27 13:29:18 +00:00
int flags;
1996-08-26 18:22:31 +00:00
{
1996-12-23 17:02:34 +00:00
char *value;
1996-08-26 18:22:31 +00:00
2001-04-06 19:14:31 +00:00
value = sh_single_quote (alias->value);
2004-07-27 13:29:18 +00:00
if (flags & AL_REUSABLE)
printf ("alias ");
printf ("%s=%s\n", alias->name, value);
1996-08-26 18:22:31 +00:00
free (value);
fflush (stdout);
}
#endif /* ALIAS */