Commit b8d1083f authored by Seblu's avatar Seblu

general improvment

parent cd0809ca
bin_PROGRAMS=42sh
bin_PROGRAMS= 42sh
42sh_SOURCES= src/ast/ast.h \
src/ast/ast_and.c \
......@@ -22,8 +22,12 @@ bin_PROGRAMS=42sh
src/builtin/builtin_alias.c \
src/builtin/builtin_cd.c \
src/builtin/builtin_echo.c \
src/builtin/builtin_exec.c \
src/builtin/builtin_exit.c \
src/builtin/builtin_export.c \
src/builtin/builtin_unalias.c \
src/builtin/builtin_unset.c \
src/builtin/builtin_set.c \
src/builtin/builtin_shopt.c \
src/common/basename.c \
src/common/constant.h \
......@@ -39,14 +43,18 @@ bin_PROGRAMS=42sh
src/exec/exec.h \
src/exec/exec_and.c \
src/exec/exec_bang.c \
src/exec/exec_case.c \
src/exec/exec_cmd.c \
src/exec/exec_for.c \
src/exec/exec_funcdec.c \
src/exec/exec_node.c \
src/exec/exec_or.c \
src/exec/exec_if.c \
src/exec/exec_pipe.c \
src/exec/exec_red.c \
src/exec/exec_sep.c \
src/exec/exec_sepand.c \
src/exec/exec_subshell.c \
src/exec/exec_while.c \
src/parser/getline.c \
src/parser/getline.h \
......
......@@ -53,7 +53,6 @@ AC_FUNC_CLOSEDIR_VOID
AC_FUNC_REALLOC
AC_FUNC_FORK
AC_FUNC_STAT
AC_FUNC_ALLOCA
AC_HEADER_DIRENT
AC_HEADER_SYS_WAIT
AC_CHECK_HEADERS([malloc.h])
......@@ -69,13 +68,13 @@ AC_CHECK_FUNCS([strstr])
AC_CHECK_FUNCS([atexit])
AC_CHECK_FUNCS([putenv])
AC_CHECK_FUNCS([strtoul])
AC_CHECK_FUNCS([strerror])
AC_TYPE_PID_T
AC_TYPE_SIZE_T
# Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_HEADER_STDBOOL
AC_STRUCT_TM
......@@ -120,7 +119,6 @@ AC_ARG_WITH([efence],
]
)
AC_SUBST([CFLAGS])
AC_SUBST([LDFLAGS])
......
......@@ -5,7 +5,7 @@
** Login <seblu@epita.fr>
**
** Started on Sun Jul 30 04:40:03 2006 Seblu
** Last update Thu Nov 16 17:01:11 2006 seblu
** Last update Fri Nov 17 14:17:02 2006 seblu
*/
#ifndef AST_H_
......@@ -104,9 +104,9 @@ typedef struct cmd_node
/*
** Binary ast node
** Generic node, it's a contener !
** T_PIPE, T_SEP* , T_AND, T_OR : binary operator
** T_BANG : unary operator
** Generic node, it's a contener for:
** T_PIPE, T_SEP, T_SEPAND , T_AND, T_OR : binary operator
** T_BANG, T_SUBSHELL : unary operator
*/
typedef struct bin_node
{
......
......@@ -5,14 +5,14 @@
** Login <seblu@epita.fr>
**
** Started on Tue Apr 11 00:22:44 2006 Seblu
** Last update Thu Nov 16 18:59:29 2006 seblu
** Last update Fri Nov 17 13:12:22 2006 seblu
*/
#include <string.h>
#include <assert.h>
#include "builtin.h"
enum { BUILTIN_COUNT = 10 };
enum { BUILTIN_COUNT = 12 };
struct builtin_table
{
......@@ -29,9 +29,11 @@ static struct builtin_table builtin_table[BUILTIN_COUNT] =
{"alias", builtin_alias},
{"unalias", builtin_unalias},
{"source", NULL}, //builtin_source},
{"set", NULL}, //builtin_set},
{"unset", NULL}, //builtin_unset},
{"export", NULL}, //builtin_export}
{".", NULL}, //builtin_source},
{"set", builtin_set},
{"unset", builtin_unset},
{"export", builtin_export},
{"exec", builtin_exec}
};
int is_a_builtin(const char *name)
......
......@@ -5,7 +5,7 @@
** Login <seblu@epita.fr>
**
** Started on Sun Nov 12 16:46:24 2006 Seblu
** Last update Thu Nov 16 17:49:13 2006 seblu
** Last update Fri Nov 17 13:01:45 2006 seblu
*/
#ifndef BUILTIN_H_
......@@ -67,12 +67,31 @@ int builtin_shopt(char *argv[]);
*/
int builtin_exit(char *argv[]);
/*!
** Set or show aliases.
**
** @param argv argument vector
**
** @return 0 on succes, or 1 if no alias found
*/
int builtin_alias(char *argv[]);
/*!
** Unset one or more aliases
**
** @param argv argument vector
**
** @return 0 on success, or 1 if no alias found
*/
int builtin_unalias(char *argv[]);
int builtin_source(char *argv[]);
int builtin_set(char *argv[]);
int builtin_unset(char *argv[]);
int builtin_exec(char *argv[]);
int builtin_export(char *argv[]);
int builtin_alias(char *argv[]);
int builtin_unalias(char *argv[]);
/*
......
......@@ -5,7 +5,7 @@
** Login <seblu@epita.fr>
**
** Started on Thu Nov 16 17:18:24 2006 seblu
** Last update Thu Nov 16 19:27:01 2006 seblu
** Last update Fri Nov 17 12:43:05 2006 seblu
*/
#include <assert.h>
......@@ -14,7 +14,6 @@
#include "builtin.h"
#include "../shell/shell.h"
static int show_all_alias(void);
static int show_alias(const char *name);
static int getoption(char *argv[], int *argp, int *p);
......@@ -23,23 +22,26 @@ int builtin_alias(char *argv[])
int argp;
char *carg;
char *equal;
int show = 0;
int all = 0;
int ret = 0;
assert(argv && argv[0]);
if (!getoption(argv, &argp, &show))
if (!getoption(argv, &argp, &all))
return 2;
if (argv[1] == NULL || show)
return show_all_alias();
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);
for (; (carg = argv[argp]); ++argp) {
if (!(equal = strchr(carg, '=')))
show_alias(carg);
ret = show_alias(carg) || ret;
else {
*equal = 0;
alias_add(shell->alias, strdup(carg), strdup(equal + 1));
*equal = '=';
}
}
return 0;
return ret;
}
static int getoption(char *argv[], int *argp, int *p)
......@@ -57,6 +59,8 @@ static int getoption(char *argv[], int *argp, int *p)
fprintf(stderr, "alias: usage: alias [-p] [name[=value]] ... ]\n");
return 0;
}
else
break;
return 1;
}
......@@ -64,18 +68,10 @@ 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)) {
printf("alias %s=%s\n", shell->alias->table[i].name,
printf("alias %s='%s'\n", shell->alias->table[i].name,
shell->alias->table[i].value);
return 1;
return 0;
}
printf("%s: alias: %s: not found.\n", shell->name, name);
return 0;
}
static int show_all_alias(void)
{
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);
return 0;
return 1;
}
/*
** builtin_doubledot.c for 42sh in /home/seblu/devel/c/towork/42sh
**
** Made by Seblu
** Login <seblu@epita.fr>
**
** Started on Sat Apr 8 18:02:31 2006 Seblu
** Last update Sun Apr 9 12:38:38 2006 SIGOURE Benoit
*/
#include <stddef.h>
#include "builtin.h"
#include "mem.h"
/*!
** Builtin double dot, allow you to cdup
**
** @param argv argv vector
** @param sh sh data
**
** @return builtin return success
*/
int builtin_doubledot(char *argv[], struct s_42sh *sh)
{
char *argv2[3];
argv2[0] = "cd";
argv2[1] = "..";
argv2[2] = NULL;
return builtin_cd(argv2, sh);
argv = argv;
}
......@@ -5,34 +5,24 @@
** Login <luttri_s@epita.fr>
**
** Started on Thu May 11 10:00:36 2006 Seblu
** Last update Sun May 21 18:42:29 2006 Seblu
** Last update Fri Nov 17 13:01:59 2006 seblu
*/
#include <assert.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include "builtin.h"
#include "../main/42sh.h"
#include "../shell/shell.h"
/*!
** Replace current process by which is given in arg
**
** @param argv arg vector
** @param sh shell struct
**
** @return success status
*/
int builtin_exec(char *argv[], struct s_42sh *sh)
int builtin_exec(char *argv[])
{
assert(sh && argv);
if (!argv[1])
return 0;
if (execvp(argv[1], argv + 1) == -1)
{
perror("42sh: exec");
return 127;
}
return 0;
argv = argv;
sh = sh;
assert(argv);
if (!argv[1])
return 0;
if (execvp(argv[1], argv + 1) == -1) {
fprintf(stderr, "%s: exec: %s.\n", shell->name, strerror(errno));
return 127;
}
return 0;
}
/*
** builtin_export.c for 42sh in /goinfre/seblu/42sh/src/builtin
** builtin_export.c for 42sh
**
** Made by Seblu
** Login <luttri_s@epita.fr>
** Login <seblu@epita.fr>
**
** Started on Thu May 11 09:32:30 2006 Seblu
** Last update Thu May 11 09:41:01 2006 Seblu
** Last update Fri Nov 17 12:57:12 2006 seblu
*/
#include <stdio.h>
#include "builtin.h"
#include <assert.h>
#include "../shell/shell.h"
int builtin_export(char *argv[], struct s_42sh *sh)
int builtin_export(char *argv[])
{
printf("export: not yet implemeted\n");
sh = sh;
argv = argv;
return 1;
assert(argv);
fprintf(stderr, "%s: export: not yet implemeted.\n", shell->name);
argv = argv;
return 1;
}
/*
** builtin_funclist.c for 42sh in /goinfre/seblu/42sh/src/builtin
**
** Made by Seblu
** Login <luttri_s@epita.fr>
**
** Started on Thu May 11 10:00:36 2006 Seblu
** Last update Sun May 21 18:40:33 2006 Seblu
*/
#include <assert.h>
#include <stdio.h>
#include "builtin.h"
#include "../func/function.h"
#include "../main/42sh.h"
int builtin_funclist(char *argv[], struct s_42sh *sh)
{
assert(sh);
/* FIXME
func_print(sh->funcs);
*/
printf("Not yet implemented !\n");
return 0;
argv = argv;
sh = sh;
}
/*
** builtin_history.c for in /home/joel/42sh
**
** Made by joel
** Login <joel@epita.fr>
**
** Started on Wed May 17 12:28:30 2006 joel
** Last update Sun May 21 18:44:31 2006 Seblu
*/
#include <assert.h>
#include <stdio.h>
#include "../main/42sh.h"
#include "../history/history.h"
#include "builtin.h"
/*!
** Builtin history.
** @param argv The tab of args: terminated by NULL, argv[0] = "cd"
** @param sh The 42sh structure
*/
int builtin_history(char **argv, struct s_42sh *sh)
{
struct s_my_dlist *list;
char *elem;
int count = 1;
assert(sh && argv && argv[0]);
if (!sh->history->list)
return 0;
for (list = sh->history->list; list->next != NULL; list = list->next)
;
for (; list; list = list->prev, ++count)
fprintf(stdout, " %i %s\n", count, elem = list->elem);
return 0;
argv = argv;
}
/*
** builtin_set.c for 42sh in /goinfre/seblu/42sh/src/builtin
** builtin_set.c for 42sh
**
** Made by Seblu
** Login <luttri_s@epita.fr>
** Login <seblu@epita.fr>
**
** Started on Thu May 11 09:32:30 2006 Seblu
** Last update Thu May 11 09:38:54 2006 Seblu
** Last update Fri Nov 17 12:57:32 2006 seblu
*/
#include <stdio.h>
#include "builtin.h"
#include <assert.h>
#include "../shell/shell.h"
int builtin_set(char *argv[], struct s_42sh *sh)
int builtin_set(char *argv[])
{
printf("set: not yet implemeted\n");
sh = sh;
argv = argv;
return 1;
assert(argv);
fprintf(stderr, "%s: set: not yet implemeted.\n", shell->name);
argv = argv;
return 1;
}
/*
** builtin_simpledot.c for 42sh in /home/seblu/devel/c/42sh
**
** Made by Seblu
** Login <seblu@epita.fr>
**
** Started on Tue May 16 19:59:03 2006 Seblu
** Last update Tue May 16 20:10:11 2006 Seblu
*/
#include <stddef.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include "builtin.h"
#include "mem.h"
/*!
** Builtin simple dot, equivalent to source
**
** @param argv argv vector
** @param sh sh data
**
** @return builtin return success
*/
int builtin_simpledot(char *argv[], struct s_42sh *sh)
{
assert(argv && sh);
free(argv[0]);
argv[0] = strdup("source");
return builtin_source(argv, sh);
}
/*
** builtin_source.c for 42sh in /goinfre/42sh/src
** builtin_source.c for 42sh
**
** Made by Seblu
** Login <luttri_s@epita.fr>
** Login <seblu@epita.fr>
**
** Started on Sun Apr 9 04:40:11 2006 Seblu
** Last update Sun May 21 18:35:59 2006 Seblu
** Last update Fri Nov 17 13:03:34 2006 seblu
*/
#include <stdio.h>
......@@ -14,11 +14,9 @@
#include "../main/42sh.h"
#include "../execution/execution.h"
#include "mem.h"
static int builtin_source_error(int err);
int builtin_source(char *argv[], struct s_42sh *sh)
int builtin_source(char *argv[])
{
FILE *fd;
int yyparse();
......
......@@ -5,16 +5,58 @@
** Login <seblu@epita.fr>
**
** Started on Thu Nov 16 17:18:24 2006 seblu
** Last update Thu Nov 16 17:49:49 2006 seblu
** Last update Fri Nov 17 12:41:53 2006 seblu
*/
#include <assert.h>
#include "builtin.h"
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include "builtin.h"
#include "../shell/shell.h"
static int getoption(char *argv[], int *argp, int *p);
int builtin_unalias(char *argv[])
{
int argp;
char *carg;
int all = 0;
int ret = 0;
assert(argv && argv[0]);
assert(0);
argv = argv;
return 0;
if (!getoption(argv, &argp, &all))
return 2;
if (argv[1] == NULL || all)
for (size_t i = 0; i < shell->alias->count; ++i)
alias_del(shell->alias, shell->alias->table[i].name);
for (; (carg = argv[argp]); ++argp) {
if (!alias_del(shell->alias, argv[argp])) {
printf("%s: unalias: %s: not found.\n", shell->name, argv[argp]);
ret = 1;
}
}
return ret;
}
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 == 'a')
*p = 1;
else {
fprintf(stderr, "%s: unalias: -%c: invalid option.\n",
shell->name, *carg);
fprintf(stderr, "unalias: usage: unalias [-a] name [name ...]\n");
return 0;
}
else
break;
return 1;
}
/*
** builtin_unset.c for 42sh in /goinfre/seblu/42sh/src/builtin
** builtin_unset.c for 42sh
**
** Made by Seblu
** Login <luttri_s@epita.fr>
** Login <seblu@epita.fr>
**
** Started on Thu May 11 09:36:06 2006 Seblu
** Last update Thu May 11 10:19:36 2006 Seblu
** Last update Fri Nov 17 13:09:29 2006 seblu
*/
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "builtin.h"
#include "../var/var.h"
#include "../func/function.h"
#include "../main/42sh.h"
#include "../shell/var.h"
#include "../shell/func.h"
#include "../shell/shell.h"
int builtin_unset(char *argv[], struct s_42sh *sh)
int builtin_unset(char *argv[])
{
int f = 0;
int v = 1;
int i;
int f = 0;
int v = 1;
assert(argv && sh);
for (i = 0; argv[i]; ++i)
if (!strcmp(argv[i], "-v"))
v = 1;
else if (!strcmp(argv[i], "-v"))
f = 1;
for (i = 0; argv[i]; ++i)
if (argv[i][0] == '-')
continue;
else
{
if (v)
var_unset(sh->vars, argv[i]);
if (f)
func_del(argv[i], &sh->funcs);
}
return 0;
assert(argv);
for (int i = 0; argv[i]; ++i)
if (!strcmp(argv[i], "-v"))
v = 1;
else if (!strcmp(argv[i], "-f"))
f = 1;
for (int i = 0; argv[i]; ++i)
if (argv[i][0] == '-')
continue;
else {
if (v) var_del(shell->var, argv[i]);
if (f) func_del(shell->func, argv[i]);
}
return 0;
}
/*
** builtin_varlist.c for 42sh in /goinfre/seblu/42sh/src/builtin
**
** Made by Seblu
** Login <luttri_s@epita.fr>
**
** Started on Thu May 11 10:00:36 2006 Seblu
** Last update Sun May 21 18:40:43 2006 Seblu
*/
#include <assert.h>
#include "builtin.h"
#include "../var/var.h"
#include "../main/42sh.h"
int builtin_varlist(char *argv[], struct s_42sh *sh)
{
assert(sh);
var_print(sh->vars);
return 0;
argv = argv;
sh = sh;
}
......@@ -5,7 +5,7 @@
** Login <Seblu@epita.fr>
**
** Started on Fri Aug 25 03:39:03 2006 Seblu
** Last update Fri Oct 13 14:25:22 2006 seblu
** Last update Fri Nov 17 13:16:09 2006 seblu
*/
#ifndef CONSTANT_H_
......@@ -13,10 +13,11 @@
typedef enum error
{
ERROR_PARSE = 2,
ERROR_FORK = 128,
ERROR_REDIR = 1,
ERROR_MEM = 42,
ERROR_RED = 1, //redirection error
ERROR_PARSE = 2, //parser or lex error
ERROR_MEM = 42, //no memory left
ERROR_FORK = 128, //error in fork
ERROR_PIPE = 128, // error in pipe
} e_error;
typedef enum prompt {
......
......@@ -5,7 +5,7 @@
** Login <seblu@epita.fr>
**
** Started on Sun Mar 30 16:02:07 2006 Seblu
** Last update Thu Nov 16 16:06:41 2006 seblu
** Last update Fri Nov 17 14:27:55 2006 seblu
*/
#ifndef EXEC_H_
......@@ -17,17 +17,67 @@
# define DEBUG_EXEC 0
/*!
** Exec an ast node. Call an exec function according node type
**
** @param node node to execute.
*/
void exec_node(s_ast_node *node);
/*!
** Execute a command.
**
** @param cmd command to execute
*/
void exec_cmd(s_cmd_node *cmd);
/*!
** Execute a command in a pipeline. This call preserve for too
** many system call fork Take care, this must be called after a fork
**
** @param cmd command to execute
*/
void exec_piped_cmd(s_cmd_node *cmd);
void exec_case(s_case_node *node);
void exec_for(s_for_node *node);
void exec_and(s_bin_node *node);
void exec_or(s_bin_node *node);
void exec_bang(s_bin_node *node);
void exec_if(s_if_node *node);
void exec_red(s_red_node *red);
void exec_pipe(s_bin_node *node);
void exec_red(s_red_node *node);
void exec_while(s_while_node *node);
/*!
** Execute a task list
**
** @param node task list
*/
void exec_sep(s_bin_node *node);