Commit de22dd53 authored by Seblu's avatar Seblu

work on execution

work on variables
work on functions
parent cf642cb2
......@@ -21,8 +21,10 @@ bin_PROGRAMS=42sh
src/builtin/builtin.c \
src/builtin/builtin_exit.c \
src/common/basename.c \
src/common/common.h \
src/common/constant.h \
src/common/env.c \
src/common/function.h \
src/common/getcwd2.c \
src/common/isdigitstr.c \
src/common/macro.h \
src/common/strmerge.c \
......@@ -39,21 +41,24 @@ bin_PROGRAMS=42sh
src/exec/exec_sep.c \
src/exec/exec_sepand.c \
src/exec/exec_while.c \
src/parser/alias.h \
src/parser/alias.c \
src/parser/getline.h \
src/parser/alias.h \
src/parser/getline.c \
src/parser/parser.h \
src/parser/getline.h \
src/parser/parser.c \
src/parser/parser.h \
src/parser/lexer.c \
src/shell/func.c \
src/shell/func.h \
src/shell/getoptions.c \
src/shell/option.h \
src/shell/option.c \
src/shell/option.h \
src/shell/shell.c \
src/shell/shell.h \
src/shell/shell_entry.c \
src/shell/shell_init.c \
src/shell/shell_destroy.c \
src/shell/prompt.c
src/shell/prompt.c \
src/shell/var.c \
src/shell/var.h
CLEANFILES= *~ \#*\#
......
42sh
This program was written by Sebastien Luttringer aka Seblu
......@@ -98,16 +98,6 @@ AC_ARG_WITH([noerror],
]
)
AC_ARG_WITH([dynamic],
[AS_HELP_STRING([--with-dynamic], [Compiling with dynamic libraries])],
[dnl action-if-given
dynamic=true
],
[dnl action-if-not-given
dynamic=false
]
)
AC_ARG_WITH([debug],
[AS_HELP_STRING([--with-debug], [use -g and don't use -DNDEBUG -O3])],
[dnl action-if-given
......@@ -125,7 +115,7 @@ AC_ARG_WITH([efence],
CFLAGS="$CFLAGS -include stdlib.h -include efence.h"
],
[dnl action-if-not-given
test "$dynamic" = "true" || LDFLAGS="$LDFLAGS -static"
true
]
)
......@@ -141,4 +131,4 @@ AC_CONFIG_FILES([
Makefile
])
AC_OUTPUT
\ No newline at end of file
AC_OUTPUT
......@@ -5,11 +5,11 @@
** Login <seblu@epita.fr>
**
** Started on Thu Aug 3 05:29:46 2006 Seblu
** Last update Sat Aug 19 01:57:49 2006 Seblu
** Last update Tue Nov 14 15:37:34 2006 seblu
*/
#include <string.h>
#include "common.h"
#include "function.h"
char *basename(const char *path)
{
......
/*
** env.c for 42sh
**
** Made by seblu
** Login <seblu@epita.fr>
**
** Started on Tue Nov 14 14:38:42 2006 seblu
** Last update Tue Nov 14 15:37:46 2006 seblu
*/
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <errno.h>
#include "function.h"
int setenv2(char *name,
char *value,
int overwrite)
{
extern char **environ;
char *buf;
char **env = environ;
size_t len;
assert(name && value && env);
len = strlen(name);
if (!overwrite)
for (; *env; ++env)
if (!strncmp(name, *env, len) && (*env)[len] == '=')
return 1;
buf = strmerge(3, name, "=", value);
return !putenv(buf) ? 0 : 2;
}
int unsetenv2(const char *name)
{
extern char **environ;
size_t len;
char **ep = environ;
char **dp = ep;
if (name == NULL || *name == '\0' || strchr(name, '=') != NULL)
{
errno = EINVAL;
return -1;
}
len = strlen(name);
for (; *ep; ++ep)
{
if (!strncmp(*ep, name, len) && (*ep)[len] == '=')
{
dp = ep;
do
dp[0] = dp[1];
while (++dp, *dp);
}
}
return 0;
}
/*
** common.h for 42sh
** function.h for 42sh
**
** Made by Seblu
** Login <seblu@epita.fr>
**
** Started on Sun Jul 30 03:59:48 2006 Seblu
** Last update Wed Oct 11 16:56:07 2006 seblu
** Last update Tue Nov 14 15:36:14 2006 seblu
*/
#ifndef COMMON_H_
# define COMMON_H_
#ifndef FUNCTION_H_
# define FUNCTION_H_
/*!
** Merge strings
......@@ -74,4 +74,34 @@ int isdigitstr(const char *str);
*/
char **strvectoradd(char **vector, char *str);
/*!
** Return the current working directory in a malloced string.
**
** @return the current working directory
*/
char *getcwd2(void);
/*!
** Add a name and value to the current environment
**
** @param name variable name
** @param value variable value
** @param overwrite boolean which tell if overwrite a existing value
**
** @return boolean success
*/
int setenv2(char *name,
char *value,
int overwrite);
/*!
** Remove from environment a variable.
** @warning Don't use unsetenv !
**
** @param name env variable to remove
**
** @return boolean success
*/
int unsetenv2(const char *name);
#endif
/*
** getcwd2.c for 42sh
**
** Made by seblu
** Login <seblu@epita.fr>
**
** Started on Tue Nov 14 14:20:08 2006 seblu
** Last update Tue Nov 14 14:23:39 2006 seblu
*/
#include <unistd.h>
#include <limits.h>
#include "macro.h"
char *getcwd2(void)
{
char *get;
const size_t BUF_SIZE = PATH_MAX * sizeof (char);
secmalloc(get, BUF_SIZE);
return getcwd(get, BUF_SIZE);
}
This diff is collapsed.
......@@ -5,7 +5,7 @@
** Login <seblu@epita.fr>
**
** Started on Wed Aug 23 00:39:17 2006 Seblu
** Last update Mon Aug 28 23:08:31 2006 Seblu
** Last update Tue Nov 14 15:34:32 2006 seblu
*/
#include <string.h>
......@@ -14,20 +14,20 @@
static size_t step = 5;
s_aliases *alias_init(void)
s_alias *alias_init(void)
{
s_aliases *new;
s_alias *new;
secmalloc(new, sizeof (s_aliases));
secmalloc(new, sizeof (s_alias));
new->size = step;
new->pos = 0;
new->db = NULL;
return new;
}
void alias_add(s_aliases *aliases, const char *name, const char *value)
void alias_add(s_alias *aliases, const char *name, const char *value)
{
s_alias *new;
s_alias_item *new;
int freeplace = 0;
size_t index;
......@@ -47,19 +47,19 @@ void alias_add(s_aliases *aliases, const char *name, const char *value)
return;
}
//create a new one
secmalloc(new, sizeof (s_alias));
secmalloc(new, sizeof (s_alias_item));
new->name = strdup(name);
new->value = strdup(value);
//add a new alias if not find
if (aliases->pos >= aliases->size) {
aliases->size += step;
secrealloc(aliases->db, aliases->db, aliases->size * sizeof (s_alias));
secrealloc(aliases->db, aliases->db, aliases->size * sizeof (s_alias_item));
}
aliases->db[aliases->pos] = new;
++aliases->pos;
}
int alias_remove(s_aliases *aliases, const char *name)
int alias_remove(s_alias *aliases, const char *name)
{
//find alias index
for (register int i = 0; aliases && aliases->db[i]; ++i)
......
......@@ -5,7 +5,7 @@
** Login <seblu@epita.fr>
**
** Started on Wed Aug 23 00:32:09 2006 Seblu
** Last update Mon Aug 28 23:07:38 2006 Seblu
** Last update Tue Nov 14 15:32:55 2006 seblu
*/
#ifndef ALIAS_H_
......@@ -13,25 +13,25 @@
# include <stddef.h>
typedef struct alias
typedef struct alias_item
{
char *name;
char *value;
} s_alias;
} s_alias_item;
typedef struct aliases
typedef struct alias
{
size_t size;
size_t pos;
s_alias **db;
} s_aliases;
s_alias_item **db;
} s_alias;
/*!
** Create a new aliases database
**
** @return new alias database
*/
s_aliases *alias_init(void);
s_alias *alias_init(void);
/*!
** Add an alias into an alias database
......@@ -40,7 +40,7 @@ s_aliases *alias_init(void);
** @param name new alias name
** @param value new alias value
*/
void alias_add(s_aliases *aliases, const char *name, const char *value);
void alias_add(s_alias *aliases, const char *name, const char *value);
/*!
** Remove an alias into an alias database
......@@ -50,6 +50,6 @@ void alias_add(s_aliases *aliases, const char *name, const char *value);
**
** @return true, if alias was found in database, else false
*/
int alias_remove(s_aliases *aliases, const char *name);
int alias_remove(s_alias *aliases, const char *name);
#endif
......@@ -5,7 +5,7 @@
** Login <seblu@epita.fr>
**
** Started on Sun Jul 30 04:36:53 2006 Seblu
** Last update Sun Nov 12 03:11:19 2006 seblu
** Last update Tue Nov 14 15:38:16 2006 seblu
*/
#include <stdio.h>
......@@ -15,7 +15,7 @@
#include "parser.h"
#include "../shell/shell.h"
#include "getline.h"
#include "../common/common.h"
#include "../common/function.h"
#include "../common/macro.h"
/*
......
......@@ -5,7 +5,7 @@
** Login <seblu@epita.fr>
**
** Started on Wed Aug 2 00:56:07 2006 Seblu
** Last update Sun Nov 12 02:58:03 2006 seblu
** Last update Tue Nov 14 15:38:06 2006 seblu
*/
......@@ -17,7 +17,7 @@
#include <assert.h>
#include "parser.h"
#include "../common/macro.h"
#include "../common/common.h"
#include "../common/function.h"
#include "../shell/shell.h"
#include "getline.h"
......
/*
** func.c for 42sh
**
** Made by seblu
** Login <seblu@epita.fr>
**
** Started on Tue Nov 14 14:52:39 2006 seblu
** Last update Tue Nov 14 15:59:57 2006 seblu
*/
/*
** ============
** DECLARATIONS
** ============
*/
#include <string.h>
#include "func.h"
#include "../common/macro.h"
/*
** ===========
** DEFINITIONS
** ===========
*/
s_func *func_init(void)
{
s_func *new;
secmalloc(new, sizeof (s_func));
new->count = 0;
new->size = FUNC_DEFAULT_SIZE;
secmalloc(new->table, new->size * sizeof (s_func_item));
return new;
}
/*
** func.h for 42sh
**
** Made by seblu
** Login <seblu@epita.fr>
**
** Started on Tue Nov 14 14:52:22 2006 seblu
** Last update Tue Nov 14 15:59:32 2006 seblu
*/
#ifndef FUNC_H_
# define FUNC_H_
# include <assert.h>
# include <stdlib.h>
# include "../ast/ast.h"
enum { FUNC_DEFAULT_SIZE = 5 };
typedef struct func_item
{
char *name;
s_ast_node *body;
} s_func_item;
typedef struct func
{
s_func_item *table;
size_t count;
size_t size;
} s_func;
/*!
** Init a function structure.
**
** @return a malloced func structure
*/
s_func *func_init(void);
/* int func_add(char *, struct s_ast *, struct s_func **); */
/* int func_del(const char *, struct s_func **); */
/* int func_exec(const char *, struct s_42sh *); */
/* void func_clean(struct s_func **); */
/* struct s_ast *func_get(const char *, struct s_func **); */
#endif
......@@ -5,14 +5,14 @@
** Login <seblu@epita.fr>
**
** Started on Sun Jul 30 03:28:26 2006 Seblu
** Last update Mon Sep 25 02:52:45 2006 Seblu
** Last update Tue Nov 14 15:38:32 2006 seblu
*/
#include <stdio.h>
#include <string.h>
#include "option.h"
#include "shell.h"
#include "../common/common.h"
#include "../common/function.h"
/*!
** Parse the command line
......@@ -21,7 +21,7 @@
** @param argv program argv
** @param opt shell opt structure to set with options
*/
void getoptions(s_options *opt, int argc, char **argv)
void getoptions(s_option *opt, int argc, char **argv)
{
#if DEBUG_OPTION == 1
printf("* Option Parser\n");
......@@ -41,7 +41,7 @@ void getoptions(s_options *opt, int argc, char **argv)
break;
}
else if (!strcmp(copt, "--ast-print"))
option_set(shell->options, "ast_print");
option_set(shell->option, "ast_print");
else {
printf("Error: Invalid option %s\n", copt);
exit(1);
......
......@@ -5,7 +5,7 @@
** Login <seblu@epita.fr>
**
** Started on Tue Mar 21 19:00:38 2006 Seblu
** Last update Sun Nov 12 20:11:40 2006 Seblu
** Last update Tue Nov 14 20:30:23 2006 seblu
*/
/*
......@@ -19,16 +19,16 @@
#include "../shell/shell.h"
#include "../common/macro.h"
static const char *opts_table[NBR_OPTION] =
{
"xpg_echo",
"dotglob",
"extglob",
"nocaseglob",
"nullglob",
"expand_aliases",
"ast_print",
};
static const char *opts_table[OPTION_COUNT] = {
"xpg_echo",
"dotglob",
"extglob",
"nocaseglob",
"nullglob",
"expand_aliases",
"ast_print",
"readline",
};
/*
** ===========
......@@ -36,30 +36,38 @@ static const char *opts_table[NBR_OPTION] =
** ===========
*/
s_options *option_init(void)
s_option *option_init(void)
{
s_options *new;
s_option *new;
secmalloc(new, sizeof (s_options));
secmalloc(new, sizeof (s_option));
new->command = NULL;
//FIXME: item is uninitialized! Do it here
return new;
}
int option_set(s_options *shopt, const char *name)
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)
{
register int i;
for (i = 0; opts_table[i]; ++i)
if (!strcmp(name, opts_table[i]))
{
if (!strcmp(name, opts_table[i])) {
shopt->item[i] = 1;
return 1;
}
return 0;
}
int option_unset(s_options *shopt, const char *name)
int option_unset(s_option *shopt, const char *name)
{
register int i;
......@@ -72,7 +80,7 @@ int option_unset(s_options *shopt, const char *name)
return 0;
}
int option_isset(const s_options *shopt, const char *name)
int option_isset(const s_option *shopt, const char *name)
{
register int i;
......
......@@ -5,21 +5,21 @@
** Login <seblu@epita.fr>
**
** Started on Tue Mar 21 18:50:03 2006 Seblu
** Last update Wed Aug 30 00:19:19 2006 Seblu
** Last update Tue Nov 14 15:29:08 2006 seblu
*/
#ifndef OPTION_H_
# define OPTION_H_
#define NBR_OPTION 9
#define DEBUG_OPTION 0
typedef struct options
enum { OPTION_COUNT = 9 };
typedef struct option
{
signed char item[NBR_OPTION];
signed char item[OPTION_COUNT];
char *command;
} s_options;
} s_option;
/*
** ==============
......@@ -32,7 +32,14 @@ typedef struct options
**
** @return A new options structure
*/
s_options *option_init(void);
s_option *option_init(void);
/*!
** Set default shell options
**
** @param opt option struct
*/
void option_set_default(s_option *shopt);
/*!
** Set a shell option
......@@ -42,7 +49,7 @@ s_options *option_init(void);
**
** @return 0 on failure, else 1
*/
int option_set(s_options *shopt, const char *name);
int option_set(s_option *shopt, const char *name);
/*!
** Unset a shell option
......@@ -52,7 +59,7 @@ int option_set(s_options *shopt, const char *name);
**
** @return 0 on failure, else 1
*/
int option_unset(s_options *shopt, const char *name);
int option_unset(s_option *shopt, const char *name);
/*!
** Tell if an option is set. if option nane is not set return -1
......@@ -62,7 +69,7 @@ int option_unset(s_options *shopt, const char *name);
**
** @return 0 if unset, 1 if set and -1 if not exist
*/
int option_isset(const s_options *shopt, const char *name);
int option_isset(const s_option *shopt, const char *name);
/*!
** Return a list of know opt
......@@ -84,6 +91,6 @@ const char **opt_get();
** @param argc program argc
** @param argv program argv
*/
void getoptions(s_options *opt, int argc, char **argv);
void getoptions(s_option *opt, int argc, char **argv);
#endif
/*
** main.c for 42sh
** shell.c for 42sh
**
** Made by Seblu
** Login <seblu@epita.fr>
**
** Started on Mon Apr 10 23:57:28 2006 Seblu
** Last update Sun Nov 12 16:38:29 2006 Seblu
** Last update Tue Nov 14 15:46:38 2006 seblu
*/
#include <stdio.h>
#include <libgen.h>
#include "shell.h"
#include "option.h"
#include "../ast/ast.h"
#include "../parser/parser.h"
#include "../exec/exec.h"
#include "../common/macro.h"
#include "../common/function.h"
/*
** ============
** DECLARATIONS
** ============
*/
/*
** Global shell structure
*/
s_shell *shell = NULL;
s_shell *shell = NULL;
/*!
** Program entry point
......@@ -30,26 +36,47 @@ s_shell *shell = NULL;
**
** @return return program value
*/
int main(int argc, char *argv[]);
/*!
** Initialize a shell structure
**
** @param path path of the current shell
**
** @return an initialized shell structure
*/
s_shell *shell_init(void);
/*
** ===========
** DEFINITIONS
** ===========
*/
int main(int argc, char *argv[])
{
s_ast_node *ast;
s_parser *parser;
// shell initialization
shell = shell_init(argv[0]);
// parse argv
getoptions(shell->options, argc, argv);
shell = shell_init();
// Set shell name
shell->name = basename(argv[0]);
// Set default option
option_set_default(shell->option);
// parse argv for user options
getoptions(shell->option, argc, argv);
//FIXME: set PWD et OLDPWD variable
// shell parser init
parser = parser_init(STDIN_FILENO);
// parse and execute stdin stream
do
{
do {
ast = parse(parser);
if (parser->error && isinteractive())
continue;
else if (parser->error)
return ERROR_PARSE;
if (option_isset(shell->options, "ast_print"))
if (option_isset(shell->option, "ast_print"))
ast_print(ast, NULL);
if (ast)
exec_node(ast);
......@@ -60,3 +87,17 @@ int main(int argc, char *argv[])
fprintf(stderr, "exit\n");
return shell->status;
}
s_shell *shell_init(void)
{
s_shell *new;
secmalloc(new, sizeof (s_shell));
new->var = var_init();
new->func = func_init();
new->alias = alias_init();