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
104
105
/*
** builtin.c for 42sh
**
** Made by Seblu
** Login <seblu@epita.fr>
**
** Started on Tue Apr 11 00:22:44 2006 Seblu
** Last update Sun Nov 12 16:47:59 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
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"
};
static const t_func_builtin builtin_function[BUILTIN_NUMBER] =
{
builtin_cd,
builtin_echo,
builtin_exit,
builtin_shopt,
builtin_source,
builtin_unset,
builtin_export
};
static const t_func_builtin builtin_bonus_function[BUILTIN_BONUS_NUMBER] =
{
builtin_simpledot,
builtin_doubledot,
builtin_set,
builtin_varlist,
builtin_funclist,
builtin_exec,
builtin_history
};
/*!
** 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)
{
register int i;
for (i = 0; i < BUILTIN_NUMBER; ++i)
if (!strcmp(name, builtin_name[i]))
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)
{
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);
return 0;
}