-
Notifications
You must be signed in to change notification settings - Fork 1
/
shell.h
249 lines (156 loc) · 5.99 KB
/
shell.h
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#ifndef SHELL_H
#define SHELL_H
#include <stdio.h> /* for printf*/
#include <unistd.h> /* for fork, execve*/
#include <stdlib.h>
#include <string.h> /* for strtok*/
#include <stddef.h>
#include <errno.h> /* for errno and perror */
#include <sys/types.h> /* for type pid */
#include <sys/wait.h> /* for wait */
#include <sys/stat.h> /* for use of stat function */
#include <signal.h> /* for signal management */
#include <fcntl.h> /* for open files*/
/************* MACROS **************/
#include "macros.h" /* for msg help and prompt */
/************* STRUCTURES **************/
/**
* struct info- struct for the program's data
* @program_name: the name of the executable
* @input_line: pointer to the input read for _getline
* @command_name: pointer to the first command typed by the user
* @exec_counter: number of excecuted comands
* @file_descriptor: file descriptor to the input of commands
* @tokens: pointer to array of tokenized input
* @env: copy of the environ
* @alias_list: array of pointers with aliases.
*/
typedef struct info
{
char *program_name;
char *input_line;
char *command_name;
int exec_counter;
int file_descriptor;
char **tokens;
char **env;
char **alias_list;
} data_of_program;
/**
* struct builtins - struct for the builtins
* @builtin: the name of the builtin
* @function: the associated function to be called for each builtin
*/
typedef struct builtins
{
char *builtin;
int (*function)(data_of_program *data);
} builtins;
/************* MAIN FUNCTIONS *************/
/*======== shell.c ========*/
/* Inicialize the struct with the info of the program */
void inicialize_data(data_of_program *data, int arc, char *argv[], char **env);
/* Makes the infinite loop that shows the prompt*/
void sisifo(char *prompt, data_of_program *data);
/* Print the prompt in a new line */
void handle_ctrl_c(int opr UNUSED);
/*======== _getline.c ========*/
/* Read one line of the standar input*/
int _getline(data_of_program *data);
/* split the each line for the logical operators if it exist */
int check_logic_ops(char *array_commands[], int i, char array_operators[]);
/*======== expansions.c ========*/
/* expand variables */
void expand_variables(data_of_program *data);
/* expand aliases */
void expand_alias(data_of_program *data);
/* append the string to the end of the buffer*/
int buffer_add(char *buffer, char *str_to_add);
/*======== str_tok.c ========*/
/* Separate the string in tokens using a designed delimiter */
void tokenize(data_of_program *data);
/* Creates a pointer to a part of a string */
char *_strtok(char *line, char *delim);
/*======== execute.c ========*/
/* Execute a command with its entire path */
int execute(data_of_program *data);
/*======== builtins_list.c ========*/
/* If match a builtin, executes it */
int builtins_list(data_of_program *data);
/*======== find_in_path.c ========*/
/* Creates an array of the path directories */
char **tokenize_path(data_of_program *data);
/* Search for program in path */
int find_program(data_of_program *data);
/************** HELPERS FOR MEMORY MANAGEMENT **************/
/*======== helpers_free.c ========*/
/* Frees the memory for directories */
void free_array_of_pointers(char **directories);
/* Free the fields needed each loop */
void free_recurrent_data(data_of_program *data);
/* Free all field of the data */
void free_all_data(data_of_program *data);
/************** BUILTINS **************/
/*======== builtins_more.c ========*/
/* Close the shell */
int builtin_exit(data_of_program *data);
/* Change the current directory */
int builtin_cd(data_of_program *data);
/* set the work directory */
int set_work_directory(data_of_program *data, char *new_dir);
/* show help information */
int builtin_help(data_of_program *data);
/* set, unset and show alias */
int builtin_alias(data_of_program *data);
/*======== builtins_env.c ========*/
/* Shows the environment where the shell runs */
int builtin_env(data_of_program *data);
/* create or override a variable of environment */
int builtin_set_env(data_of_program *data);
/* delete a variable of environment */
int builtin_unset_env(data_of_program *data);
/************** HELPERS FOR ENVIRONMENT VARIABLES MANAGEMENT **************/
/*======== env_management.c ========*/
/* Gets the value of an environment variable */
char *env_get_key(char *name, data_of_program *data);
/* Overwrite the value of the environment variable */
int env_set_key(char *key, char *value, data_of_program *data);
/* Remove a key from the environment */
int env_remove_key(char *key, data_of_program *data);
/* prints the current environ */
void print_environ(data_of_program *data);
/************** HELPERS FOR PRINTING **************/
/*======== helpers_print.c ========*/
/* Prints a string in the standar output */
int _print(char *string);
/* Prints a string in the standar error */
int _printe(char *string);
/* Prints a string in the standar error */
int _print_error(int errorcode, data_of_program *data);
/************** HELPERS FOR STRINGS MANAGEMENT **************/
/*======== helpers_string.c ========*/
/* Counts the number of characters of a string */
int str_length(char *string);
/* Duplicates an string */
char *str_duplicate(char *string);
/* Compares two strings */
int str_compare(char *string1, char *string2, int number);
/* Concatenates two strings */
char *str_concat(char *string1, char *string2);
/* Reverse a string */
void str_reverse(char *string);
/*======== helpers_numbers.c ========*/
/* Cast from int to string */
void long_to_string(long number, char *string, int base);
/* convert an string in to a number */
int _atoi(char *s);
/* count the coincidences of character in string */
int count_characters(char *string, char *character);
/*======== alias_management.c ========*/
/* print the list of alias */
int print_alias(data_of_program *data, char *alias);
/* get the alias name */
char *get_alias(data_of_program *data, char *alias);
/* set the alias name */
int set_alias(char *alias_string, data_of_program *data);
#endif /* SHELL_H */