-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsh_loop.c
More file actions
131 lines (121 loc) · 2.57 KB
/
sh_loop.c
File metadata and controls
131 lines (121 loc) · 2.57 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
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
#include "shell.h"
/**
* sh_loop - loops through to emulate a UNIX Commandline Interpreter
*
* Return: nothing
*/
pid_t sh_loop(void)
{
char *line = NULL, **sep_cmds = NULL;
pid_t status = 0;
int i = 0;
char *separator = ";";
while (1)
{
line = NULL;
errno = 0;
if (SHELL.interactive)
printf(SHELL_PROMPT);
line = sh_readline();
if (line == NULL)
{
fflush(STDIN_FILENO);
errno = 0;
continue;
}
sep_cmds = tokenize(line, separator);
for (i = 0; sep_cmds[i]; i++)
{/* Enabling command separtor in shell ';' */
status = sh_run(sep_cmds[i]);
if (status == SHELL.FORK_EXEC_FAILURE ||
errno == SHELL.END_SHELL)
break;
}
fflush(STDIN_FILENO);
free_str_safe(&line);
free(sep_cmds);
if (status == SHELL.FORK_EXEC_FAILURE)
{/* child process could not execute the command */
exit(EXIT_FAILURE);
}
if (errno == SHELL.END_SHELL)
{
exit(SHELL.LAST_EXIT_STATUS);
}
}
return (status);
}
/**
* sh_run - prepare and execute a line
* @line: line for execution
*
* Description: A new child is not called if command doesn't exist
* Return: status of execution
*/
pid_t sh_run(char *line)
{
char **args = NULL, **found = NULL;
bool changed = false;
pid_t status = 0;
if (line == NULL)
{
return (0);
}
args = tokenize(line, NULL);
if (args == NULL)
{
return (0);
}
found = add_path(args, &changed);
if (args && args[0] != NULL && found != NULL)
{
status = sh_execute(args);
status = (status < 0) ? SHELL.FORK_EXEC_FAILURE : status;
}
if (args && args[0] != NULL && found == NULL)
{
errno = 0;
status = run_builtin(args[0], (const char **)args);
}
if (errno == SHELL.ERR_NO_BUILTIN ||
status == SHELL.ERR_NO_BUILTIN)
{
fprintf(stderr, "%s: 1: %s: not found\n",
ERR_PROMPT, args[0]);
SHELL.LAST_EXIT_STATUS = SHELL.CMD_NOT_FOUND;
}
if (changed == true)
free(args[0]);
args[0] = "dummy";
free(args);
return (status);
}
/**
* run_builtin - executes a SHELL_BUILTIN
* @builtin: name of builtin to execute
* @args: a vector of all arguments from cmd as well as builtin
*
* Return: the return status of the builtin
*/
int run_builtin(const char *builtin, const char **args)
{
int i = 0;
builtin_t funcs[] = {
{"exit", _exit_shell}, {"env", _env_builtin},
{"setenv", _setenv_btn}, {"unsetenv", _unsetenv_btn},
{"cd", _cd_btn},
{NULL, NULL}
};
if (builtin == NULL)
{
return (SHELL.ERR_NO_BUILTIN);
}
for (i = 0; funcs[i].opcode != NULL; i++)
{
if (strcmp(builtin, funcs[i].opcode) == 0)
{/* run and return exit status */
return (funcs[i].f(args));
}
}
return (SHELL.ERR_NO_BUILTIN);
}