Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
** shell.c for 42sh
**
** Made by Seblu
** Login <seblu@epita.fr>
**
** Started on Mon Apr 10 23:57:28 2006 Seblu
** Last update Tue Nov 14 15:46:38 2006 seblu
*/
#include <stdio.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;
/*!
** Program entry point
**
** @param argc Argument count
** @param argv Argument vector
**
** @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();
// 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 {
ast = parse(parser);
if (parser->error && isinteractive())
continue;
else if (parser->error)
return ERROR_PARSE;
if (option_isset(shell->option, "ast_print"))
ast_print(ast, NULL);
if (ast)
exec_node(ast);
ast_destruct(ast);
}
while (!parser->lexer->eof);
if (isinteractive())
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();
new->option = option_init();
new->name = "42sh";
new->status = 0;
return new;
}