diff --git a/Makefile.am b/Makefile.am index 7e9bd8d0e73803bcc691a26eba7888e51c4819ae..90f5e5eefa28dac9095a2f0f80a3f3666c491864 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 \ diff --git a/src/builtin/builtin.c b/src/builtin/builtin.c index 22f80bee2a160af60b88b89ffcf1c353727a17f0..5e63747e4af728e4b8e2ec272272719668954185 100644 --- a/src/builtin/builtin.c +++ b/src/builtin/builtin.c @@ -5,101 +5,49 @@ ** Login ** ** 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 -#include -#include #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; } diff --git a/src/builtin/builtin.h b/src/builtin/builtin.h index 8c032f463574925329d5019c9793e7124dc23049..4f50b3db500d0c2626fcca98d8a127437f718b48 100644 --- a/src/builtin/builtin.h +++ b/src/builtin/builtin.h @@ -5,40 +5,36 @@ ** Login ** ** 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 +# 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_ */ diff --git a/src/builtin/builtin_cd.c b/src/builtin/builtin_cd.c index 0ade5f674eab8e6767ccd04d012cd459ec23191f..f685ede5843b07903b6c38343cdef83717ff9d3e 100644 --- a/src/builtin/builtin_cd.c +++ b/src/builtin/builtin_cd.c @@ -1,11 +1,11 @@ /* -** builtin_cd.c for 42sh in /home/seblu/devel/c/42sh/src/execution +** builtin_cd.c for 42sh ** ** Made by Seblu ** Login ** ** 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 @@ -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]); diff --git a/src/builtin/builtin_exit.c b/src/builtin/builtin_exit.c index 2de403c97358e6f8e696ba189e4e99ea0488c5e6..e41bf6556c0ec5ba8a99a51095fb68953beb6321 100644 --- a/src/builtin/builtin_exit.c +++ b/src/builtin/builtin_exit.c @@ -5,7 +5,7 @@ ** Login ** ** 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 @@ -13,16 +13,8 @@ #include #include #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; } diff --git a/src/shell/option.c b/src/shell/option.c index 2c6f19cb33dbec0617074645cb1c5b8fcbdd5944..47749a1bcc2715c9e3a79a826f56add51b1dbf3d 100644 --- a/src/shell/option.c +++ b/src/shell/option.c @@ -5,7 +5,7 @@ ** Login ** ** 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; }