-
Notifications
You must be signed in to change notification settings - Fork 1
/
Interprete_line.c
121 lines (118 loc) · 2.33 KB
/
Interprete_line.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
#include "main.h"
/**
* main - main function that executes the prompt
*
* @argv: main argument containing the name of the executable
* @argc: arguments counter
* Return: EXIT_SUCCESS
*/
int main(__attribute((unused))int argc, char **argv)
{
command_t *tokens_input = NULL, *command = NULL;
char *input = NULL;
int status = 0, count_error = 1, not_file = 0;
while (1)
{
if (isatty(STDIN_FILENO))
write(STDERR_FILENO, PID, _strlen(PID));
input = Read_Line(not_file);
if (!input)
break;
tokens_input = Input_Tokenize(input);
if (!tokens_input)
{
free(input);
continue;
}
if (!_strcmp(tokens_input->args, "env"))
{
Print_Environ(tokens_input, input);
continue;
}
command = Concatenate_Command(tokens_input);
if (!command)
{
fprintf(stderr, "%s: %d: %s: not found\n",
argv[0], count_error++, tokens_input->args);
Free_List(tokens_input);
free(input);
not_file = 1;
continue;
}
else
not_file = 0;
status = Run_Command(command, input, argv, count_error);
if (status == 0)
count_error++;
free(input);
}
exit(EXIT_SUCCESS);
}
/**
* Read_Line - Read the input line and keep it in a buffer
*
* @not_file: Error indicator
* Return: buffer
*/
char *Read_Line(int not_file)
{
char *input = NULL;
size_t size = 0;
if (not_file == 0)
{
if (getline(&input, &size, stdin) == -1)/*Condition EOF*/
{
free(input);
exit(0);
}
else if (!_strcmp(input, "exit\n"))
{
free(input);
exit(0);
}
}
else if (not_file == 1)
{
if (getline(&input, &size, stdin) == -1)/*Condition EOF*/
{
free(input);
exit(127);
}
else if (!_strcmp(input, "exit"))
{
free(input);
exit(2);
}
}
return (input);
}
/**
* Input_Tokenize - divide the input into multiple nodes
* @input: input line
*
* Return: A pointer to the first node on the list
*/
command_t *Input_Tokenize(char *input)
{
command_t *head = NULL;
char *token = NULL;
token = strtok(input, DELIMS);/*se tokenize el input*/
while (token)
{
Add_Node_End(&head, token);/*new node is added the end of the list*/
token = strtok(NULL, DELIMS);
}
return (head);
}
/**
* Print_Environ - condition that prints in Environment
*
* @tokens_input: lista de arguemntos
* @input: input
*/
void Print_Environ(command_t *tokens_input, char *input)
{
_printenv();
Free_List(tokens_input);
free(input);
}