-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
111 lines (111 loc) · 2.6 KB
/
main.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
#include "shell.h"
/**
* main - initialize the variables of the program
* @argc: number of values received from the command line
* @argv: values received from the command line
* @env: number of values received from the command line
* Return: zero on succes.
*/
int main(int argc, char *argv[], char *env[])
{
data_of_program data_struct = {NULL}, *data = &data_struct;
char *prompt = "";
inicialize_data(data, argc, argv, env);
signal(SIGINT, handle_ctrl_c);
if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO) && argc == 1)
{/* We are in the terminal, interactive mode */
errno = 2;/*???????*/
prompt = PROMPT_MSG;
}
errno = 0;
sisifo(prompt, data);
return (0);
}
/**
* handle_ctrl_c - print the prompt in a new line
* when the signal SIGINT (ctrl + c) is send to the program
* @UNUSED: option of the prototype
*/
void handle_ctrl_c(int opr UNUSED)
{
_print("\n");
_print(PROMPT_MSG);
}
/**
* inicialize_data - inicialize the struct with the info of the program
* @data: pointer to the structure of data
* @argv: array of arguments pased to the program execution
* @env: environ pased to the program execution
* @argc: number of values received from the command line
*/
void inicialize_data(data_of_program *data, int argc, char *argv[], char **env)
{
int i = 0;
data->program_name = argv[0];
data->input_line = NULL;
data->command_name = NULL;
data->exec_counter = 0;
/* define the file descriptor to be readed*/
if (argc == 1)
data->file_descriptor = STDIN_FILENO;
else
{
data->file_descriptor = open(argv[1], O_RDONLY);
if (data->file_descriptor == -1)
{
_printe(data->program_name);
_printe(": 0: Can't open ");
_printe(argv[1]);
_printe("\n");
exit(127);
}
}
data->tokens = NULL;
data->env = malloc(sizeof(char *) * 50);
if (env)
{
for (; env[i]; i++)
{
data->env[i] = str_duplicate(env[i]);
}
}
data->env[i] = NULL;
env = data->env;
data->alias_list = malloc(sizeof(char *) * 20);
for (i = 0; i < 20; i++)
{
data->alias_list[i] = NULL;
}
}
/**
* sisifo - its a infinite loop that shows the prompt
* @prompt: prompt to be printed
* @data: its a infinite loop that shows the prompt
*/
void sisifo(char *prompt, data_of_program *data)
{
int error_code = 0, string_len = 0;
while (++(data->exec_counter))
{
_print(prompt);
error_code = string_len = _getline(data);
if (error_code == EOF)
{
free_all_data(data);
exit(errno); /* if EOF is the fisrt Char of string, exit*/
}
if (string_len >= 1)
{
expand_alias(data);
expand_variables(data);
tokenize(data);
if (data->tokens[0])
{ /* if a text is given to prompt, execute */
error_code = execute(data);
if (error_code != 0)
_print_error(error_code, data);
}
free_recurrent_data(data);
}
}
}