-
Notifications
You must be signed in to change notification settings - Fork 0
/
sh.c
152 lines (132 loc) · 2.72 KB
/
sh.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
#include "crikey.h"
#define UNUSED __attribute__((unused))
/**
* main - main function of a program
* @UNUSED: Macro for unused parameters
* @env: environment
*
* Return: none
*/
int main(int argc UNUSED, char **argv UNUSED, char **env)
{
char *input = NULL;
size_t len = 0;
int is_term, size;
token_t *n_params = NULL;
signal(SIGINT, SIG_IGN);
is_term = isatty(STDIN_FILENO);
while (1)
{
n_params = NULL;
if (is_term)
_print("⚡ ");
if (getline(&input, &len, stdin) == -1)
{
free(input);
if (is_term)
{
_print("\n");
exit(70);
}
exit(0);
}
input = _strtok(input, '#');
dropnl(input);
replaceTabs(input);
size = tokenize(&n_params, input);
helper(size, n_params, env, input);
}
free(input);
return (0);
}
/**
* helper - helper for main
* @size: size
* @n_params: node parameters
* @env: contains the environment variables
* @input: Contains the raw user input
*
* Return: no return
*/
void helper(int size, token_t *n_params, char **env, char *input)
{
token_t *tmp;
char **params;
int i;
static int tally, exitStat;
tally++;
if (size == 0)
return;
params = malloc(sizeof(char *) * (size + 1));
for (i = 0, tmp = n_params; tmp; tmp = tmp->next, ++i)
params[i] = tmp->str;
params[i] = NULL;
if (check_builtins(size, params, env, input, n_params, exitStat))
return;
if (!translateExec(params, env, &exitStat))
{
if (compareStat(params[0], &exitStat, params, n_params, tally) == 0)
return;
if (checkVanilla(params, &exitStat, n_params, tally, env) == 0)
return;
}
free(params);
freenodes(n_params);
}
/**
* compareStat - compares the exitStat if 126 then frees the variables
* @command: the first word the user inputs
* @exitStat: the exit status of the program
* @params: the double pointer where the tokenized inputs are stored
* @n_params: the linked list
* @tally: tally number
*
* Return: returns 0 if success, returns 1 if failed
*/
int compareStat(char *command, int *exitStat, char **params, token_t *n_params,
int tally)
{
struct stat ret;
if ((stat(command, &ret) == 0 && access(command, X_OK) != 0) || *exitStat
== 126)
{
*exitStat = 126;
printComNotFound(tally, command);
free(params);
freenodes(n_params);
return (0);
}
return (1);
}
/**
* dropnl - Removes the newline from the end of a string
* @src: Pointer to the string to manipulate
*/
void dropnl(char *src)
{
int i;
for (i = 0; src[i]; i++)
{
if (src[i] == '\n')
{
*(src + i) = '\0';
return;
}
}
}
/**
* freenodes - frees the nodes to avoid memory leaks
* @head: points to the first node of a linked list
*
* Return: none
*/
void freenodes(token_t *head)
{
token_t *next;
while (head)
{
next = head->next;
free(head);
head = next;
}
}