-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell_loop.c
More file actions
162 lines (151 loc) · 3.26 KB
/
shell_loop.c
File metadata and controls
162 lines (151 loc) · 3.26 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "s_shell.h"
/**
* shellLoop - main shell loop
* @info: the parameter of info struct
* @argv: the argument vector from main()
*
* Return: 0 on success, 1 on error, or error to stdrr
*/
int shellLoop(info_t *info, char **argv)
{
ssize_t bytesRead = 0;
int builtinReturn = 0;
while (bytesRead != -1 && builtinReturn != -2)
{
clearInfo(info);
if (check_interactive_mode(info))
_puts("$ ");
_eputchar(BUFFER_FLUSH);
bytesRead = getInput(info);
if (bytesRead != -1)
{
setInfo(info, argv);
builtinReturn = findBuiltin(info);
if (builtinReturn == -1)
findCommand(info);
}
else if (check_interactive_mode(info))
_putchar('\n');
freeInfo(info, 0);
}
writeHistory(info);
freeInfo(info, 1);
if (!check_interactive_mode(info) && info->status)
exit(info->status);
if (builtinReturn == -2)
{
if (info->errNum == -1)
exit(info->status);
exit(info->errNum);
}
return (builtinReturn);
}
/**
* findBuiltin - finds a builtin command
* @info: the parameter of info struct
*
* Return: -1 if builtin not found,
* 0 if builtin executed successfully,
* 1 if builtin found but not successful,
* -2 if builtin signals exit()
*/
int findBuiltin(info_t *info)
{
int i, builtinReturn = -1;
_builtin_tbl builtinTable[] = {
{"exit", exit_shell},
{"env", displayEnvironment},
{"help", show_help},
{"history", display_history},
{"setenv", _mysetenv},
{"unsetenv", unsetEnvironmentVariable},
{"cd", change_directory},
{"alias", manage_alias},
{NULL, NULL}
};
for (i = 0; builtinTable[i].type; i++)
if (_strcmp(info->arguments[0], builtinTable[i].type) == 0)
{
info->lineCount++;
builtinReturn = builtinTable[i].func(info);
break;
}
return (builtinReturn);
}
/**
* findCommand - finds a command in PATH
* @info: the parameter & return info struct
*
* Return: void
*/
void findCommand(info_t *info)
{
char *commandPath = NULL;
int i, countArgs;
info->path = info->arguments[0];
if (info->lineCountFlag == 1)
{
info->lineCount++;
info->lineCountFlag = 0;
}
for (i = 0, countArgs = 0; info->arg[i]; i++)
if (!is_character_delimiter(info->arg[i], " \t\n"))
countArgs++;
if (!countArgs)
return;
commandPath = findPath(info, getEnvironmentVariable(info, "PATH="),
info->arguments[0]);
if (commandPath)
{
info->path = commandPath;
forkCommand(info);
}
else
{
if ((check_interactive_mode(info) || getEnvironmentVariable(info, "PATH=")
|| info->arguments[0][0] == '/') && isCmd(info, info->arguments[0]))
forkCommand(info);
else if (*(info->arg) != '\n')
{
info->status = 127;
print_custom_err(info, "not found\n");
}
}
}
/**
* forkCommand - forks an exec thread to run cmd
* @info: the parameter & return info struct
*
* Return: void
*/
void forkCommand(info_t *info)
{
pid_t childPid;
childPid = fork();
if (childPid == -1)
{
perror("Error:");
return;
}
if (childPid == 0)
{
if (execve(info->path, info->arguments,
copyEnvironToStringArray(info)) == -1)
{
freeInfo(info, 1);
if (errno == EACCES)
exit(126);
exit(1);
}
}
else
{
wait(&(info->status));
if (WIFEXITED(info->status))
{
info->status = WEXITSTATUS(info->status);
if (info->status == 126)
print_custom_err(info, "Permission denied\n");
}
}
}