-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_input.c
155 lines (135 loc) · 3.34 KB
/
parse_input.c
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "shell.h"
/**
* parse_input - Parse user input and execute commands.
*
* @lsh: Pointer to the shell structure containing relevant information.
*
* Return: Exit code of the last executed command or -1 on failure.
*/
int parse_input(shell_t *lsh)
{
size_t i;
if (*lsh->user_input == '\n' || *lsh->user_input == '#')
return (0);
/* remove comments if any */
lsh->user_input = remove_comments(lsh->user_input);
lsh->command_array = _strtok(lsh->user_input, "\n");
if (lsh->command_array == NULL)
{
return (-1);
}
for (i = 0; lsh->command_array[i] != NULL; i++)
{
lsh->token = lsh->command_array[i];
lsh->tokens = _strtok(lsh->token, ";\n");
if (lsh->tokens == NULL)
{
fprintf(stderr, "Couldn't allocate memory\n");
safefree(lsh->command_array);
return (-1);
}
lsh->exit_code = parser(lsh);
free_string(&lsh->tokens);
}
free_string(&lsh->command_array);
return (lsh->exit_code);
}
/**
* parser - Parse and executes tokens.
*
* @lsh: Pointer to the shell structure containing relevant information.
*
* Return: Exit code of the last executed command or -1 on failure.
*/
int parser(shell_t *lsh)
{
size_t i;
for (i = 0; lsh->tokens[i] != NULL; i++)
{
lsh->exit_code = execute_tokens(lsh, i);
safefree(lsh->tokens[i]);
}
free_string(&lsh->tokens);
return (lsh->exit_code);
}
/**
* execute_tokens - Executes individual tokens.
*
* @lsh: Pointer to the shell structure containing relevant information.
*
* @index: Index of the token in the token array.
*
* Return: Exit code of the executed command or -1 on failure.
*/
int execute_tokens(shell_t *lsh, size_t index)
{
lsh->tokenized_commands = _strtok(lsh->tokens[index], NULL);
if (lsh->tokenized_commands == NULL)
{
return (0);
}
if (lsh->tokenized_commands[0] != NULL && lsh->tokenized_commands != NULL)
{
aux_parser(lsh, index);
}
else
{
free_string(&lsh->tokenized_commands);
}
if (lsh->tokenized_commands != NULL)
{
safefree(lsh->tokenized_commands);
}
safefree(lsh->tokens[index]);
return (lsh->exit_code);
}
/**
* cmd_not_found - Handle command not found error.
*
* @lsh: Pointer to the shell structure containing relevant information.
*
* Return: Exit code for command not found (127).
*/
int cmd_not_found(shell_t *lsh)
{
dprintf(STDERR_FILENO, "%s: %lu: %s: not found\n", lsh->prog_name,
lsh->cmd_count, lsh->tokenized_commands[0]);
return (127); /* command not found */
}
/**
* aux_parser - Auxiliary function to parse and execute commands.
*
* @lsh: Pointer to the shell structure containing relevant information.
*
* @index: Index of the tokenized command to be processed.
*/
void aux_parser(shell_t *lsh, size_t index __attribute__((unused)))
{
lsh->exit_code = execute_builtin(lsh);
if (lsh->exit_code != 18)
{
free_string(&lsh->tokenized_commands);
return;
}
if (lsh->path_list != NULL && !_strchr(lsh->tokenized_commands[0], '/'))
{
lsh->exit_code = execute_with_path(lsh);
if (lsh->exit_code == -1)
{
lsh->exit_code = cmd_not_found(lsh);
}
}
else
{
if (access(lsh->tokenized_commands[0], X_OK) == 0 &&
_strchr(lsh->tokenized_commands[0], '/'))
{
lsh->exit_code = execute_command(lsh->tokenized_commands[0], lsh);
}
else
{
lsh->exit_code = cmd_not_found(lsh);
}
}
free_string(&lsh->tokenized_commands);
}