This file is alias.def, from which is created alias.c
It implements the builtins "alias" and "unalias" in Bash.

Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.

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
Software Foundation; either version 1, or (at your option) any later
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
Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

$BUILTIN alias
$FUNCTION alias_builtin
$DEPENDS_ON ALIAS
$PRODUCES alias.c
$SHORT_DOC alias [ name[=value] ... ]
`alias' with no arguments prints the list of aliases in the form
NAME=VALUE on standard output.  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.  Alias returns true
unless a NAME is given for which no alias has been defined.
$END

#include "../config.h"

#if defined (ALIAS)
#  include <stdio.h>
#  include "../shell.h"
#  include "../alias.h"
#  include "common.h"

extern int interactive;
static void print_alias ();

/* Hack the alias command in a Korn shell way. */
alias_builtin (list)
     WORD_LIST *list;
{
  int any_failed = 0;

  if (!list)
    {
      register int i;
      ASSOC **alias_list;

      if (!aliases)
	return (EXECUTION_FAILURE);

      alias_list = all_aliases ();

      if (!alias_list)
	return (EXECUTION_FAILURE);

      for (i = 0; alias_list[i]; i++)
	print_alias (alias_list[i]);

      free (alias_list);	/* XXX - Do not free the strings. */
    }
  else
    {
      while (list)
	{
	  register char *value, *name = list->word->word;
	  register int offset;

	  for (offset = 0; name[offset] && name[offset] != '='; offset++)
	    ;

	  if (offset && name[offset] == '=')
	    {
	      name[offset] = '\0';
	      value = name + offset + 1;

	      add_alias (name, value);
	    }
	  else
	    {
	      ASSOC *t = find_alias (name);
	      if (t)
		print_alias (t);
	      else
		{
		  if (interactive)
		    builtin_error ("`%s' not found", name);
		  any_failed++;
		}
	    }
	  list = list->next;
	}
    }
  if (any_failed)
    return (EXECUTION_FAILURE);
  else
    return (EXECUTION_SUCCESS);
}
#endif /* ALIAS */

$BUILTIN unalias
$FUNCTION unalias_builtin
$DEPENDS_ON ALIAS
$SHORT_DOC unalias [-a] [name ...]
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. */
unalias_builtin (list)
     register WORD_LIST *list;
{
  register ASSOC *alias;
  int any_failed = 0;

  while (list && *list->word->word == '-')
    {
      register char *word = list->word->word;

      if (ISOPTION (word, 'a'))
	{
	  delete_all_aliases ();
	  list = list->next;
	}
      else if (ISOPTION (word, '-'))
	{
	  list = list->next;
	  break;
	}
      else
	{
	  bad_option (word);
	  return (EXECUTION_FAILURE);
	}
    }

  while (list)
    {
      alias = find_alias (list->word->word);

      if (alias)
	remove_alias (alias->name);
      else
	{
	  if (interactive)
	    builtin_error ("`%s' not an alias", list->word->word);

	  any_failed++;
	}

      list = list->next;
    }

  if (any_failed)
    return (EXECUTION_FAILURE);
  else
    return (EXECUTION_SUCCESS);
}

/* Output ALIAS in such a way as to allow it to be read back in. */
static void
print_alias (alias)
     ASSOC *alias;
{
  char *value = single_quote (alias->value);

  printf ("alias %s=%s\n", alias->name, value);
  free (value);

  fflush (stdout);
}
#endif /* ALIAS */
