-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell0.2.c
101 lines (94 loc) · 2.26 KB
/
shell0.2.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
#include "main.h"
/**
* _error - fun that print error messesge
* @shell: pointer to shell name
* @arg: pointer to command name
* @numOforders: orders numbers
*/
void _error(char *shell, char *arg, int numOforders)
{
dprintf(STDOUT_FILENO, "%s: %d: ", shell, numOforders);
perror(arg);
}
/**
* print_prompt - fun that print prompt
* @ac: num of arguments
*/
void print_prompt(int ac)
{
if (isatty(STDIN_FILENO) && ac != 2)/* if we in interactive mode */
write(1, promt, 2); /* print the prompt */
}
/**
* file_script - fun that handle file script
* @av: array of strings of the arguments
* @head: pointer to the list of alias
* @head_asign: pointer to the list of replacement var
*/
void file_script(char **av, list_t *head, list_t *head_asign)
{
size_t len = 1024;
FILE *fd;
char **arg = NULL;
char *line = NULL;
int numOforders = 0;
fd = fopen(av[1], "r");
if (fd == NULL)
_error(av[0], av[1], numOforders);
else
{
line = malloc(sizeof(char) * len);
while (fgets(line, len, fd) != NULL)
{
numOforders++;
arg = execute_line(line, &head, &head_asign);
if (arg != NULL)
_error(av[0], arg[0], numOforders);
}
fclose(fd);
}
}
/**
* main - fun that act like sh shell
* @ac: num of arguments
* @av: array of strings of the arguments
* Return: 0
*/
int main(int ac, __attribute__((unused))char **av)
{
int n_read, numOforders = 0;
char *line = NULL;
char **arg = NULL;
size_t len = 0;
list_t *head = NULL, *head_asign = NULL;
add_list(&head_asign, "?", "0");/* set the defult exit status to 0 */
if (ac == 2)/* start get the line from the terminal */
{
file_script(av, head, head_asign);
}
else if (ac == 1)
{
print_prompt(ac);/* print the prompt */
while ((n_read = getline(&line, &len, stdin)) != -1)
{
/* line = */check_begin_space(&line);
if (n_read == 1 || line[0] == '\n')
{
write(1, promt, 2);
continue;
}
numOforders++;/* num of written commands */
arg = execute_line(line, &head, &head_asign);
if (arg != NULL)
_error(av[0], arg[0], numOforders);
print_prompt(ac);/* print the prompt */
}
}
if (n_read == -1 && isatty(STDIN_FILENO) && ac != 2)
printf("\n");/* if terminate without type anything */
free(arg);
free(line);
free_list(head);
free_list(head_asign);
return (0);
}