-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.c
More file actions
102 lines (81 loc) · 1.67 KB
/
parser.c
File metadata and controls
102 lines (81 loc) · 1.67 KB
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
#include "main.h"
/**
* run_args - executes builtin or command
* @cmd_info: shell data structure
*
* Return: void
*/
int run_args(CommandInfo *cmd_info)
{
int ret;
int (*builtin)(CommandInfo *cmd_info);
if (!cmd_info->args[0])
_pute("Args[0] missing\n");
builtin = get_builtin(cmd_info->args[0]);
if (builtin)
ret = (builtin(cmd_info));
else
{
parser(cmd_info);
ret = (execute(cmd_info));
}
free(cmd_info->command);
cmd_info->command = NULL;
return (ret);
}
/**
* parser - handles input stream
* @cmd_info: shell data structure
* Return: count
*/
int parser(CommandInfo *cmd_info)
{
char *command = cmd_info->args[0];
if (command[0] == '.' && command[1] == '/')
is_dot(cmd_info);
else if (command[0] == '/')
{
cmd_info->command = _strdup(command);
if (!cmd_info->command)
_pute("Memory allocation error\n");
}
else
is_rel_path(cmd_info);
return (0);
}
/**
* get_builtin - checks if the command is a builtin
* @command: pointer to vector argv
* Return: function pointer to builtin
*/
int (*get_builtin(char *command))(CommandInfo *)
{
builtin_t funcs[] = {
{ "exit", builtin_exit },
{ "env", print_env },
{ "setenv", set_env },
{ "unsetenv", unset_env },
{ "cd", cd_builtin },
{ "help", builtin_help },
{ NULL, NULL },
};
int i;
for (i = 0; funcs[i].name; i++)
{
if (_strcmp(funcs[i].name, command) == 0)
return (funcs[i].f);
}
return (NULL);
}
/**
* is_directory - checks to see if path is a directory
* @path: path to parse
* Return: success or fail
*/
int is_directory(const char *path)
{
struct stat path_stat;
if (stat(path, &path_stat) == 0)
return ((S_ISDIR(path_stat.st_mode)) ? 1 : 0);
return (0);
}