-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.c
189 lines (157 loc) · 4.22 KB
/
shell.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "main.h"
/**
* runShell - add descr
* @shellInfo: Pointer to the commandInfo structure.
* @argv: Command-line arguments.
* Return: Exit status.
*/
int runShell(commandInfo *shellInfo, char **argv)
{
ssize_t inputResult = 0;
int builtinReturn = 0;
char *prompt = "$ ";
while (inputResult != -1 && builtinReturn != -2)
{
clearCommandInfo(shellInfo); /*We can also use commandinfoinit*/
if ((isatty(STDIN_FILENO) && shellInfo->input_file_descriptor <= 2))
print_str("%s", prompt);
print_error_char(FLUSH_BUFFER);
inputResult = getInput(shellInfo);
if (inputResult != -1)
{
setInfo(shellInfo, argv);
builtinReturn = findBuiltinCmd(shellInfo);
if (builtinReturn == -1)
executeExternalCmd(shellInfo);
}
else if ((isatty(STDIN_FILENO) && shellInfo->input_file_descriptor <= 2))
_putchar('\n');
freeCommandInfo(shellInfo, 0);
}
freeCommandInfo(shellInfo, 1);
if (!((isatty(STDIN_FILENO) && shellInfo->input_file_descriptor <= 2))
&& shellInfo->commandExecStatus)
exit(shellInfo->commandExecStatus);
if (builtinReturn == -2)
{
if (shellInfo->error_number == -1)
exit(shellInfo->commandExecStatus);
exit(shellInfo->error_number);
}
return (builtinReturn);
}
/**
* main - Entry point for the shell program
* @argc: Argument count
* @argv: Argument vector
*
* Return: 0 on success, 1 on error
*/
int main(int argc, char **argv)
{
commandInfo shellInfo[] = { COMMAND_INFO_INIT };
int fileDescriptor = STDERR_FILENO + 3;
if (argc == 2)
{
fileDescriptor = open(argv[1], O_RDONLY);
if (fileDescriptor == -1)
{
if (errno == EACCES)
exit(126);
if (errno == ENOENT)
{
_fprintf(STDERR_FILENO, "%s: 0: Can't open %s\n", argv[0], argv[1]);
print_error_char(FLUSH_BUFFER);
exit(127);
}
return (EXIT_FAILURE);
}
shellInfo->input_file_descriptor = fileDescriptor;
}
populateEnvironmentList(shellInfo);
runShell(shellInfo, argv);
return (EXIT_SUCCESS);
}
/*==================================================*/
/*SHELLINFO COMMANDS*/
/**
* clearCommandInfo - initializes struct
* @shellInfo: add descr
*/
void clearCommandInfo(commandInfo *shellInfo)
{
shellInfo->current_argument = NULL;
shellInfo->command_arguments = NULL;
shellInfo->executable_path = NULL;
shellInfo->argument_count = 0;
}
/**
* setInfo - initializes commandInfo struct
* @shellInfo: struct address
* @argv: argument vector
*/
void setInfo(commandInfo *shellInfo, char **argv)
{
int i = 0;
shellInfo->file_name = argv[0];
if (shellInfo->current_argument)
{
shellInfo->command_arguments = strtow(shellInfo->current_argument, " \t");
if (!shellInfo->command_arguments)
{
shellInfo->command_arguments = malloc(sizeof(char *) * 2);
if (shellInfo->command_arguments)
{
shellInfo->command_arguments[0] = _strdup(shellInfo->current_argument);
shellInfo->command_arguments[1] = NULL;
}
}
for (i = 0; shellInfo->command_arguments
&& shellInfo->command_arguments[i]; i++)
;
shellInfo->argument_count = i;
replaceAlias(shellInfo);
replaceVars(shellInfo);
}
}
/*====================================================*/
/*GETLINE COMMANDS*/
/**
* getInput - add descr
* @shellInfo: add descr
*
* Return: add descr
*/
ssize_t getInput(commandInfo *shellInfo)
{
static char *buf; /* the ';' command chain buffer */
static size_t i, j, len;
ssize_t r = 0;
char **buf_p = &(shellInfo->current_argument), *p;
_putchar(FLUSH_BUFFER);
r = inputBuf(shellInfo, &buf, &len);
if (r == -1) /* EOF */
return (-1);
if (len) /* we have commands left in the chain buffer */
{
j = i; /* init new iterator to current buf position */
p = buf + i; /* get pointer for return */
chkChain(shellInfo, buf, &j, i, len);
while (j < len) /* iterate to semicolon or end */
{
if (isChn(shellInfo, buf, &j))
break;
j++;
}
i = j + 1; /* increment past nulled ';'' */
if (i >= len) /* reached end of buffer? */
{
i = len = 0; /* reset position and length */
shellInfo->command_buffer_type = 0;
}
*buf_p = p; /* pass back pointer to current command position */
return (_strlen(p)); /* return length of current command */
}
*buf_p = buf; /* else not a chain, pass back buffer from getLine() */
return (r); /* return length of buffer from getLine() */
}