-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.h
146 lines (117 loc) · 3.09 KB
/
main.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
#ifndef MAIN_H
#define MAIN_H
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <malloc.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#define PROMPT "$ "
#define GET_LINE_BUFFER_SIZE 1024
/* program state */
/**
* struct State - program state
*
* @name: program name
* @prompt: program prompt
* @fd: program file descriptor
* @count: number of commands
* @status: program exit status
* @env: program environment
* @aliases: program aliases
* @commands: program commands
*/
struct State
{
char *name;
char *prompt;
int fd;
size_t count;
int status;
char **env;
char **aliases;
char **commands;
};
struct State *get_state(void);
void free_state(void);
/* input */
char *get_line(int fd);
void display_prompt(void);
bool handle_input(void);
/* parse */
char **parse_operators(const char *string);
char **parse_command(const char *string);
char *parse_comments(const char *string);
char *parse_variables(const char *string);
char *parse_aliases(const char *string);
void cat_token(char **string, char **token);
void push_token(char ***tokens, char **token);
char *get_first_token(char *string);
char *cut_first_token(char *string);
char *parse_delimiter(const char *string, char delim);
/**
* enum Operator - operator types
*
* @AND: &&
* @OR: ||
* @SEMI: ;
* @UNDEF: undefined
*
*/
enum Operator
{
AND,
OR,
SEMI,
UNDEF
};
bool is_operator(const char *str);
enum Operator get_operator_type(const char *str);
void handle_operator_and(char ***commands, int status);
void handle_operator_or(char ***commands, int status);
void handle_operator_semi(char ***commands);
void handle_operator_hash(char ***commands);
void handle_operator(char ***commands, int status);
char *_strtok(char *str, char *delim);
/* env */
extern char **environ;
char *get_env(const char *name);
int set_env(const char *name, const char *value);
int unset_env(const char *name);
bool is_valid_env(const char *name);
int get_env_index(const char *name);
char *assemble_env(const char *name, const char *value);
/* aliases */
char *get_alias(const char *name);
int set_alias(const char *name, const char *value);
int get_alias_index(const char *name);
char *assemble_alias(const char *name, const char *value);
/* commands */
void handle_commands(char **commands);
int handle_command(char **argv);
void builtin_exit(char **argv);
int builtin_cd(char **argv);
int builtin_env(void);
int builtin_alias(char **argv);
int builtin_setenv(char **argv);
int builtin_unsetenv(char **argv);
int execute(char **argv);
/* string array functions */
size_t string_array_length(char **arr);
void string_array_push(char ***arr, const char *str);
void string_array_remove(char ***arr, size_t index);
char **string_array_copy(char **arr);
bool string_array_contains(char **arr, const char *string);
void string_array_free(char ***arr);
/* utility functions */
void string_cat(char **dest, const char *src);
void string_cat_char(char **dest, char src);
char *int_to_string(int num);
void inc_shlvl(void);
char *trim_whitespace(char *str);
#endif