Skip to content
builtin_alias.c 1.71 KiB
Newer Older
Seblu's avatar
Seblu committed
/*
** builtin_alias.c for 42sh
**
** Made by seblu
** Login   <seblu@epita.fr>
**
** Started on  Thu Nov 16 17:18:24 2006 seblu
** Last update Thu Nov 23 11:23:49 2006 seblu
Seblu's avatar
Seblu committed
*/

#include <assert.h>
#include <string.h>
#include <stdio.h>
#include "builtin.h"
#include "../shell/shell.h"

static int	show_alias(const char *name);
static int	getoption(char *argv[], int *argp, int *p);

int		builtin_alias(char *argv[])
{
  int		argp;
  char		*carg;
  char		*equal;
Seblu's avatar
Seblu committed
  int		all = 0;
  int		ret = 0;
Seblu's avatar
Seblu committed

  assert(argv && argv[0]);
Seblu's avatar
Seblu committed
  if (!getoption(argv, &argp, &all))
Seblu's avatar
Seblu committed
    return 2;
Seblu's avatar
Seblu committed
  if (argv[1] == NULL || all)
    for (size_t i = 0; i < shell->alias->count; ++i)
      printf("alias %s='%s'\n", shell->alias->table[i].name,
	     shell->alias->table[i].value);
Seblu's avatar
Seblu committed
  for (; (carg = argv[argp]); ++argp) {
    if (!(equal = strchr(carg, '=')))
Seblu's avatar
Seblu committed
      ret = show_alias(carg) || ret;
Seblu's avatar
Seblu committed
    else {
      *equal = 0;
      alias_add(shell->alias, carg, equal + 1);
Seblu's avatar
Seblu committed
      *equal = '=';
    }
  }
Seblu's avatar
Seblu committed
  return ret;
Seblu's avatar
Seblu committed
}

static int	getoption(char *argv[], int *argp, int *p)
{
  char		*carg;

  for (*argp = 1; (carg = argv[*argp]); ++*argp)
    if (*carg == '-')
      for (++carg; *carg; ++carg)
	if (*carg == 'p')
	  *p = 1;
	else {
	  fprintf(stderr, "%s: alias: -%c: invalid option.\n",
		  shell->name, *carg);
	  fprintf(stderr, "alias: usage: alias [-p] [name[=value]] ... ]\n");
	  return 0;
	}
Seblu's avatar
Seblu committed
    else
      break;
Seblu's avatar
Seblu committed
  return 1;
}

static int	show_alias(const char *name)
{
  for (size_t i = 0; i < shell->alias->count; ++i)
    if (!strcmp(shell->alias->table[i].name, name)) {
Seblu's avatar
Seblu committed
      printf("alias %s='%s'\n", shell->alias->table[i].name,
Seblu's avatar
Seblu committed
	     shell->alias->table[i].value);
Seblu's avatar
Seblu committed
      return 0;
Seblu's avatar
Seblu committed
  }
  printf("%s: alias: %s: not found.\n", shell->name, name);
Seblu's avatar
Seblu committed
  return 1;
Seblu's avatar
Seblu committed
}