-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshell.h
314 lines (263 loc) · 7.59 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#ifndef SHELL_H
#define SHELL_H
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <limits.h>
#include <fcntl.h>
#include <errno.h>
#include <time.h>
#include "_getline.h"
/* for read/write buffers */
#define READ_BUF_SIZE 1024
#define WRITE_BUF_SIZE 1024
#define BUF_FLUSH -1
/* for command chaining */
#define CMD_NORM 0
#define CMD_OR 1
#define CMD_AND 2
#define CMD_CHAIN 3
#define CMD_PIPE 4
/* for convert_number() */
#define CONVERT_LOWERCASE 1
#define CONVERT_UNSIGNED 2
/* 1 if using system getline() */
#define USE_GETLINE 0
#define USE_STRTOK 0
#define HIST_FILE ".hsh_history"
#define HIST_MAX 4096
#define HEREDOC_FD -2 /* set if using HEREDOC */
/* Starting since of dynamically reallocating arrays */
#define STARTING_ARR_SIZE 10
/* location of the file to execute its contents at startup */
#define STARTUP_FILE ".hshrc"
extern char **environ;
/**
* struct liststr - singly linked list
* @num: the number field
* @str: a string
* @next: points to the next node
*/
typedef struct liststr
{
int num;
char *str;
struct liststr *next;
} list_t;
/**
*struct passinfo - contains pseudo-arguements to pass into a function,
* allowing uniform prototype for function pointer struct
* @arg: a string generated from getline containing arguements
* @argv: an array of strings generated from arg
* @path: a string path for the current command
* @argc: the argument count
* @line_count: the error count
* @err_num: the error code for exit()s
* @linecount_flag: if on count this line of input
* @fname: the program filename
* @env: linked list local copy of environ
* @environ: custom modified copy of environ from LL env
* @history: the history node
* @alias: the alias node
* @env_changed: on if environ was changed
* @status: the return status of the last exec'd command
* @cmd_buf: address of pointer to cmd_buf, on if chaining
* @cmd_buf_type: CMD_type ||, &&, ;
* @readfd: the fd from which to read line input
* @histcount: the history line number count
* @left_redirect_from_fd: the fd to left redirect from
* @left_append: true if heredoc
* @right_redirect_from_fd: fd right redirecting from (default 1)
* @right_redirect_to_fd: fd right redirecting to
* @right_append: true if right stream appends
* @heredoc: value of HEREDOC delimeter
* @heredoc_txt: accumulated HEREDOC lines
* @heredoc_cmd: the command to pipe HEREDOC line
* @help: help flags
* @pipefd: fd pipe for interprocess communication | pipe
* @startup_fd: fd of startup file or -1
* @dup_stdin: saved duplicate of stdin for restoration after redirect
* @dup_stdout: saved duplicate of stdout for restoration after redirect
*/
typedef struct passinfo
{
char *arg;
char **argv;
char *path;
int argc;
unsigned int line_count;
int err_num;
int linecount_flag;
char *fname;
list_t *env;
list_t *history;
list_t *alias;
char **environ;
int env_changed;
int status;
char **cmd_buf; /* pointer to cmd ; chain buffer, for memory mangement */
int cmd_buf_type; /* CMD_type ||, &&, ; */
int readfd;
int histcount;
int left_redirect_from_fd;
int left_append;
int right_redirect_from_fd;
int right_redirect_to_fd;
int right_append;
char *heredoc;
char *heredoc_txt;
char *heredoc_cmd;
char *help;
int pipefd[2];
int startup_fd;
int dup_stdin;
int dup_stdout;
} info_t;
#define INFO_INIT \
{NULL, NULL, NULL, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, 0, 0, NULL, \
0, 0, 0, -1, 0, 1, -1, 0, NULL, NULL, NULL, NULL, {0}, -1, 0, 0}
/**
*struct builtin - contains a builtin string and related function
*@type: the builtin command flag
*@func: the function
*/
typedef struct builtin
{
char *type;
int (*func)(info_t *);
} builtin_table;
/* hsh.c */
int hsh(info_t *, char **);
int find_builtin(info_t *);
void find_cmd(info_t *);
void fork_cmd(info_t *);
void handle_redirects(info_t *info);
/* path.c */
int is_cmd(info_t *, char *);
char *dup_chars(char *, int, int);
char *find_path(info_t *, char *, char *);
/* help.c */
void help(void);
void help_cd(info_t *);
void help_exit(info_t *);
void help_help(info_t *);
void help_history(info_t *);
/* help_2.c */
void help_alias(info_t *);
void help_echo(info_t *);
void help_pwd(info_t *);
/* err_string_functions.c */
void _eputs(char *);
int _eputchar(char);
int _putfd(char c, int fd);
int _putsfd(char *str, int fd);
/* string_functions.c */
int _strlen(char *);
int _strcmp(char *, char *);
char *starts_with(const char *, const char *);
char *_strcat(char *, char *);
/* string_functions2.c */
char *_strcpy(char *, char *);
char *_strdup(const char *);
void _puts(char *);
int _putchar(char);
/* string_functions3.c */
char *_strncpy(char *, char *, int);
char *_strncat(char *, char *, int);
char *_strchr(char *, char);
char *_strchrlast(char *s, char c);
/* string_functions4.c */
char **strtow(char *, char *);
char **strtow2(char *, char);
/* memory_functions */
char *_memcpy(char *dest, char *src, unsigned int n);
char *_memset(char *, char, unsigned int);
void ffree(char **);
void *_realloc(void *, unsigned int, unsigned int);
int bfree(void **);
/* more_functions.c */
int interactive(info_t *);
int is_delim(char, char *);
int _isalpha(int);
int _atoi(char *);
/* more_functions2.c */
int _erratoi(char *);
int print_d(int, int);
char *convert_number(long int, int, int);
void remove_comments(char *);
/* builtin_emulators.c */
int _myexit(info_t *);
int _mycd(info_t *);
int _myhelp(info_t *);
char *help_flag_check(info_t *, char**);
/* builtin_emulators2.c */
int _myhistory(info_t *);
int _myalias(info_t *);
/* getline.c module */
ssize_t get_input(info_t *);
int _getline(info_t *, char **, size_t *);
void sigintHandler(int);
/* info.c module */
void clear_info(info_t *);
int set_info(info_t *info, char **av);
void free_info(info_t *, int);
void print_info(info_t *info);
/* env.c module */
char *_getenv(info_t *, const char *);
int _myenv(info_t *);
int _mysetenv(info_t *);
int _myunsetenv(info_t *);
int populate_env_list(info_t *);
/* env2.c module */
char **get_environ(info_t *);
int _unsetenv(info_t *, char *);
int _setenv(info_t *, char *, char *);
void print_prompt(info_t *);
/* file_io_functions.c */
char *get_history_file(info_t *info);
int write_history(info_t *info);
int read_history(info_t *info);
int build_history_list(info_t *info, char *buf, int linecount);
int renumber_history(info_t *info);
/* liststr.c module */
list_t *add_node(list_t **, const char *, int);
list_t *add_node_end(list_t **, const char *, int);
size_t print_list_str(const list_t *);
int delete_node_at_index(list_t **, unsigned int);
void free_list(list_t **);
/* liststr2.c module */
size_t list_len(const list_t *);
char **list_to_strings(list_t *);
size_t print_list(const list_t *);
list_t *node_starts_with(list_t *, char *, char);
ssize_t get_node_index(list_t *, list_t *);
/* chain.c */
int is_chain(info_t *, char *, size_t *);
void check_chain(info_t *, char *, size_t *, size_t, size_t);
int replace_alias(info_t *);
int replace_vars(info_t *);
int replace_string(char **, char *);
/* redirect.c */
void parse_left_redirect(info_t *info);
void parse_right_redirect(info_t *info);
int open_redirect(info_t *info, char *file, int left);
size_t parse_heredoc(info_t *info, char **buf, size_t r);
void restore_stdfd(info_t *info);
/* pipe.c */
void open_pipe(info_t *info);
/* error.c */
void print_error(info_t *, char *);
void print_error_noarg(info_t *info, char *estr);
/* startup.c */
int open_file(info_t *info, char *name, int silent);
void read_startup_file(info_t *info);
/* time.c */
char *create_date(void);
char *create_time(int);
char *its_weekday(int);
char *its_month(int);
#endif