-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.h
133 lines (113 loc) · 3.19 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
#ifndef SIMPLE_SHELL_H
#define SIMPLE_SHELL_H
/*
* Desc: header file containing all libraries, object-like macros,
* structures and prototypes used by `0x16. C - Simple Shell` project.
*/
/* Libraries */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <signal.h>
/* Structures */
/**
* struct list_s - singly linked list
* @str: string - (malloc'ed string)
* @next: points to the next node
*
* Description: singly linked list node structure
*/
typedef struct list_s
{
char *str;
struct list_s *next;
} list_t;
/**
* struct built_in_functions - type struct builtin.
* @cmd_name: the command's name.
* @function: the function that executes the corresponding command.
*
* Description: maps a command name to a function.
*/
typedef struct built_in_functions
{
char *cmd_name;
void (*function)(char **parse);
} builtin;
/* Global variables */
char *shell_name;
extern char **environ;
int should_run;
int cmd_counter;
int logical_counter;
int exit_status;
/* Object_like macros */
#define BUFFER_SIZE 1024
#define INVALID_CMD -1
#define EXTERNAL_CMD 1
#define PATH_CMD 2
#define INTERNAL_CMD 3
/* Prototypes */
/* Print a string */
int _putchar(char c);
void _print(char *str);
void _perror(char *cmd);
void _snprint(char *ptr, int size, char *s1, char *s2);
/* String functions*/
int _strlen(char *s);
char *_strcpy(char *dest, char *src);
char *_strdup(char *line);
int _strcmp(char *s1, char *s2);
int _strncmp(const char *s1, const char *s2, int n);
char *_strcat(char *dest, char *src);
char *_strstr(char *line, char *str);
int _ptrlen(char **ptr);
/* Assistance functions */
void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size);
int _atoi(char *s);
void _ctrl_c_signal_handler(int signal __attribute__((unused)));
char *int_to_str(int num);
/* Handle errors*/
void _exit_error(char *status);
char *_env_error(char *ptr);
void _cd_error(char *arg);
/* Handle comments */
char *_handle_comments(char *line);
/* Handel environment*/
size_t _list_len(const list_t *h);
list_t *_get_node_at_index(list_t *head, unsigned int index);
list_t *_add_node_end(list_t **head, char *str);
char *_getenv(char *name);
/* Advanced functions */
ssize_t _getline_(char **line, size_t *size, FILE *stream);
void _update_env(char *path);
void _cd_(char **parse);
/* Handle the PATH */
char *_check_path(char *cmd);
int _cmd_type(char *cmd);
/* Built_in functions */
void _exit_(char **parse);
void (*_get_function(char *cmd))(char **);
void _env_(char **parse);
void _setenv_(char **parse);
void _unsetenv_(char **parse);
/* Handle variables replacement */
void _handle_dollar_qst_mark_var_rep(char *parse, char *after, int pos);
void _handle_env_var_replacement(char *parse, int pos, char *after);
void _handle_var_replacement(char **parse);
/* Interactive functions */
char *_read_line(void);
char **_split_line(char *line, char *delimiter);
void _execute_ext_path_cmds(char **parse, int cmd_type);
void _execute(char **parse);
int _handle_logical_operators(char *line);
/* No-interactive functions */
char *_read_stream(void);
/* Main functions */
int interactive_mode(void);
int no_interactive_mode(void);
#endif