Skip to content
option.c 1.55 KiB
Newer Older
Seblu's avatar
Seblu committed
/*
** opt_shopt.c for 42sh
**
** Made by Seblu
** Login   <seblu@epita.fr>
**
** Started on  Tue Mar 21 19:00:38 2006 Seblu
Seblu's avatar
Seblu committed
** Last update Tue Nov 14 20:30:23 2006 seblu
Seblu's avatar
Seblu committed
*/

/*
** ============
** DECLARATIONS
** ============
Seblu's avatar
Seblu committed
*/

#include <string.h>
Seblu's avatar
Seblu committed
#include "option.h"
Seblu's avatar
Seblu committed
#include "../shell/shell.h"
#include "../common/macro.h"
Seblu's avatar
Seblu committed
static const char *opts_table[OPTION_COUNT] = {
  "xpg_echo",
  "dotglob",
  "extglob",
  "nocaseglob",
  "nullglob",
  "expand_aliases",
  "ast_print",
  "readline",
};
Seblu's avatar
Seblu committed
/*
** ===========
** DEFINITIONS
** ===========
Seblu's avatar
Seblu committed
*/
Seblu's avatar
Seblu committed

Seblu's avatar
Seblu committed
s_option	*option_init(void)
Seblu's avatar
Seblu committed
{
Seblu's avatar
Seblu committed
  s_option	*new;
Seblu's avatar
Seblu committed

Seblu's avatar
Seblu committed
  secmalloc(new, sizeof (s_option));
Seblu's avatar
Seblu committed
  new->command = NULL;
Seblu's avatar
Seblu committed
  //FIXME: item is uninitialized! Do it here
Seblu's avatar
Seblu committed
  return new;
}

Seblu's avatar
Seblu committed
void		option_set_default(s_option *shopt)
{
  //unset all
  for (int i = 0; i < OPTION_COUNT; ++i)
    shopt->item[i] = 0;
  //set those which are on by default
  option_set(shopt, "readline");
}

int		option_set(s_option *shopt, const char *name)
Seblu's avatar
Seblu committed
{
  register int	i;

  for (i = 0; opts_table[i]; ++i)
Seblu's avatar
Seblu committed
    if (!strcmp(name, opts_table[i])) {
Seblu's avatar
Seblu committed
      shopt->item[i] = 1;
      return 1;
    }
  return 0;
}

Seblu's avatar
Seblu committed
int		option_unset(s_option *shopt, const char *name)
Seblu's avatar
Seblu committed
{
  register int	i;

  for (i = 0; opts_table[i]; ++i)
    if (!strcmp(name, opts_table[i]))
    {
      shopt->item[i] = 0;
      return 1;
    }
  return 0;
}

Seblu's avatar
Seblu committed
int		option_isset(const s_option *shopt, const char *name)
Seblu's avatar
Seblu committed
{
  register int	i;

  for (i = 0; opts_table[i]; ++i)
    if (!strcmp(name, opts_table[i]))
      return shopt->item[i];
  return -1;
}

const char	**opt_get()
{
  return opts_table;
}