Commit cf642cb2 authored by Seblu's avatar Seblu

avancee sur les builtins

parent bf73a988
......@@ -17,6 +17,9 @@ bin_PROGRAMS=42sh
src/ast/ast_sepand.c \
src/ast/ast_subshell.c \
src/ast/ast_while.c \
src/builtin/builtin.h \
src/builtin/builtin.c \
src/builtin/builtin_exit.c \
src/common/basename.c \
src/common/common.h \
src/common/constant.h \
......
......@@ -5,101 +5,49 @@
** Login <seblu@epita.fr>
**
** Started on Tue Apr 11 00:22:44 2006 Seblu
** Last update Sun Nov 12 16:47:59 2006 Seblu
** Last update Sun Nov 12 19:56:53 2006 Seblu
*/
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include "builtin.h"
#include "../main/42sh.h"
#include "../ast/ast.h"
#include "../opt/opt.h"
#define BUILTIN_NUMBER 7
#define BUILTIN_BONUS_NUMBER 7
enum { BUILTIN_COUNT = 7 };
static const char *builtin_name[BUILTIN_NUMBER] =
{
"cd",
"echo",
"exit",
"shopt",
"source",
"unset",
"export"
};
static const char *builtin_bonus_name[BUILTIN_BONUS_NUMBER] =
{
".",
"..",
"set",
"varlist",
"funclist",
"exec",
"history"
};
typedef int (*f_builtin)(char *argv[]);
static const t_func_builtin builtin_function[BUILTIN_NUMBER] =
{
builtin_cd,
builtin_echo,
builtin_exit,
builtin_shopt,
builtin_source,
builtin_unset,
builtin_export
};
struct builtin_table
{
const char *name;
f_builtin func;
};
static const t_func_builtin builtin_bonus_function[BUILTIN_BONUS_NUMBER] =
static struct builtin_table builtin_table[BUILTIN_COUNT] =
{
builtin_simpledot,
builtin_doubledot,
builtin_set,
builtin_varlist,
builtin_funclist,
builtin_exec,
builtin_history
{"cd", NULL}, //builtin_cd},
{"echo", NULL}, //builtin_echo},
{"exit", builtin_exit},
{"shopt", NULL}, //builtin_shopt},
{"source", NULL}, //builtin_source},
{"unset", NULL}, //builtin_unset},
{"export", NULL}, //builtin_export}
};
/*!
** test if a command is a builtin
**
** @param name The name of the command
** @return 1 if \a name is a builtin, else 0
*/
int is_a_builtin(const char *name, struct s_42sh *sh)
int is_a_builtin(const char *name)
{
register int i;
for (i = 0; i < BUILTIN_NUMBER; ++i)
if (!strcmp(name, builtin_name[i]))
for (i = 0; i < BUILTIN_COUNT; ++i)
if (!strcmp(name, builtin_table[i].name))
return 1;
if (opt_isset("bonus", sh->opt))
for (i = 0; i < BUILTIN_BONUS_NUMBER; ++i)
if (!strcmp(name, builtin_bonus_name[i]))
return 1;
return 0;
}
/*!
** execute a shell builtin
**
** @param builtin_name The name of the builtin
** @param argv The builtin arguments, terminate by NULL, argv[0] = builtin_name
** @return The return value of the builtin
*/
int exec_builtin(struct s_cmd *cmd, struct s_42sh *sh)
int exec_builtin(s_cmd_node *cmd)
{
register int i;
for (i = 0; i < BUILTIN_NUMBER; ++i)
if (!strcmp(cmd->argv[0], builtin_name[i]))
return builtin_function[i](cmd->argv, sh);
if (opt_isset("bonus", sh->opt))
for (i = 0; i < BUILTIN_BONUS_NUMBER; ++i)
if (!strcmp(cmd->argv[0], builtin_bonus_name[i]))
return builtin_bonus_function[i](cmd->argv, sh);
for (i = 0; i < BUILTIN_COUNT; ++i)
if (!strcmp(cmd->argv[0], builtin_table[i].name))
return builtin_table[i].func(cmd->argv);
return 0;
}
......@@ -5,40 +5,36 @@
** Login <seblu@epita.fr>
**
** Started on Sun Nov 12 16:46:24 2006 Seblu
** Last update Sun Nov 12 16:47:03 2006 Seblu
** Last update Sun Nov 12 19:55:03 2006 Seblu
*/
#ifndef BUILTIN_H_
# define BUILTIN_H_
struct s_42sh;
struct s_cmd;
# include <assert.h>
# include "../ast/ast.h"
# include "../shell/shell.h"
typedef int (*t_func_builtin)(char *argv[], struct s_42sh *sh);
int is_a_builtin(const char *name);
int exec_builtin(s_cmd_node *cmd);
int is_a_builtin(const char *name, struct s_42sh *sh);
int exec_builtin(struct s_cmd *cmd, struct s_42sh *sh);
/*
** Regular builtin
*/
int builtin_cd(char *argv[], struct s_42sh *sh);
int builtin_echo(char *argv[], struct s_42sh *sh);
int builtin_shopt(char *argv[], struct s_42sh *sh);
int builtin_exit(char *argv[], struct s_42sh *sh);
int builtin_source(char *argv[], struct s_42sh *sh);
int builtin_unset(char *argv[], struct s_42sh *sh);
int builtin_export(char *argv[], struct s_42sh *sh);
int builtin_cd(char *argv[]);
int builtin_echo(char *argv[]);
int builtin_shopt(char *argv[]);
int builtin_exit(char *argv[]);
int builtin_source(char *argv[]);
int builtin_unset(char *argv[]);
int builtin_export(char *argv[]);
/*
** Bonus builtin
*/
int builtin_simpledot(char *argv[], struct s_42sh *sh);
int builtin_doubledot(char *argv[], struct s_42sh *sh);
int builtin_set(char *argv[], struct s_42sh *sh);
int builtin_varlist(char *argv[], struct s_42sh *sh);
int builtin_funclist(char *argv[], struct s_42sh *sh);
int builtin_exec(char *argv[], struct s_42sh *sh);
int builtin_history(char *argv[], struct s_42sh *sh);
/* int builtin_simpledot(char *argv[], struct s_42sh *sh); */
/* int builtin_doubledot(char *argv[], struct s_42sh *sh); */
/* int builtin_set(char *argv[], struct s_42sh *sh); */
/* int builtin_varlist(char *argv[], struct s_42sh *sh); */
/* int builtin_funclist(char *argv[], struct s_42sh *sh); */
/* int builtin_exec(char *argv[], struct s_42sh *sh); */
/* int builtin_history(char *argv[], struct s_42sh *sh); */
#endif /* ! BUILTIN_H_ */
/*
** builtin_cd.c for 42sh in /home/seblu/devel/c/42sh/src/execution
** builtin_cd.c for 42sh
**
** Made by Seblu
** Login <seblu@epita.fr>
**
** Started on Tue Apr 11 00:23:47 2006 Seblu
** Last update Sun May 21 19:17:53 2006 Seblu
** Last update Sun Nov 12 19:55:57 2006 Seblu
*/
#include <ctype.h>
......@@ -19,8 +19,6 @@
#include "builtin.h"
#include "../var/var.h"
#include "mem.h"
static int cd_var(struct s_42sh *sh, const char *name);
static int cd_minus(struct s_42sh *sh);
static int secure_chdir(const char *path);
......@@ -30,7 +28,7 @@ static int secure_chdir(const char *path);
** @param argv The tab of args: terminated by NULL, argv[0] = "cd"
** @param sh The 42sh structure
*/
int builtin_cd(char **argv, struct s_42sh *sh)
int builtin_cd(char **argv)
{
assert(sh && argv && argv[0]);
......
......@@ -5,7 +5,7 @@
** Login <seblu@epita.fr>
**
** Started on Sun Nov 12 16:48:50 2006 Seblu
** Last update Sun Nov 12 16:49:42 2006 Seblu
** Last update Sun Nov 12 19:45:36 2006 Seblu
*/
#include <stdlib.h>
......@@ -13,16 +13,8 @@
#include <unistd.h>
#include <stdio.h>
#include "builtin.h"
#include "../main/42sh.h"
#include "../common/macro.h"
/*!
** Terminate program
**
** @param argv vector to argument
** @param shopt option structure of shell
**
** @return
*/
int builtin_exit(char *argv[])
{
long i;
......@@ -30,25 +22,16 @@ int builtin_exit(char *argv[])
assert(argv && argv[0]);
if (!argv[1])
{
/* if (isatty(fileno(stdin))) */
/* printf("exit\n"); */
i = sh->last_status;
sh_destroy(sh);
exit(i);
}
if (argv[2])
{
exit(shell->status);
if (argv[2]) {
fprintf(stderr, "42sh : exit : too many arguments\n");
fflush(stderr);
return 1;
}
i = strtol(argv[1], &endptr, 10);
/* if (isatty(fileno(stdin))) */
/* printf("exit\n"); */
if (isinteractive())
fprintf(stderr, "exit\n");
if (*endptr)
fprintf(stderr, "42sh: exit: %s: numeric argument required\n", argv[1]);
sh_destroy(sh);
exit((!*endptr) ? i : 255);
return 1;
}
......@@ -5,7 +5,7 @@
** Login <seblu@epita.fr>
**
** Started on Tue Mar 21 19:00:38 2006 Seblu
** Last update Wed Aug 30 00:19:38 2006 Seblu
** Last update Sun Nov 12 20:11:40 2006 Seblu
*/
/*
......@@ -42,6 +42,7 @@ s_options *option_init(void)
secmalloc(new, sizeof (s_options));
new->command = NULL;
//FIXME: item is uninitialized! Do it here
return new;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment