-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysh.h
261 lines (232 loc) · 4.16 KB
/
mysh.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
/*
* Dakota Kallas & Jack Parker
*
* CS441/541: Project 3
*
*/
#ifndef MYSHELL_H
#define MYSHELL_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <fcntl.h>
#include <errno.h>
/* For fork, exec, sleep */
#include <sys/types.h>
#include <unistd.h>
/* For waitpid */
#include <sys/wait.h>
/******************************
* Defines
******************************/
#define TRUE 1
#define FALSE 0
#define MAX_COMMAND_LINE 1024
#define PROMPT ("mysh$ ")
/******************************
* Structures
******************************/
/*
* A job struct. Feel free to change as needed.
*/
struct job_t
{
char *full_command;
int argc;
char **argv;
int is_background;
char *binary;
int pid;
};
typedef struct job_t job_t;
struct job_node
{
int pid;
bool running;
char *full_command;
struct job_node *next;
int job_num;
};
typedef struct job_node job_node;
/******************************
* Global Variables
******************************/
/*
* Interactive or batch mode
*/
int is_batch;
FILE **input_files;
/*
* Counts
*/
int total_jobs_display_ctr = 0;
int total_jobs = 0;
int total_jobs_bg = 0;
int total_history = 0;
int num_files;
/*
* Job Lists
*/
job_node *bg_jobs_head = NULL;
job_node *bg_jobs_tail = NULL;
job_node *all_jobs_head = NULL;
job_node *all_jobs_tail = NULL;
/*
* File I/O
*/
bool file_input = false;
int input_file;
bool file_output = false;
int output_file;
/*
* Debugging mode
*/
int is_debug = FALSE;
/******************************
* Function declarations
******************************/
/*
* Parse command line arguments passed to myshell upon startup.
*
* Parameters:
* argc : Number of command line arguments
* argv : Array of pointers to strings
*
* Returns:
* 0 on success
* Negative value on error
*/
int parse_args_main(int argc, char **argv);
/*
* Main routine for batch mode
*
* Parameters:
* None
*
* Returns:
* 0 on success
* Negative value on error
*/
int batch_mode(void);
/*
* Main routine for interactive mode
*
* Parameters:
* None
*
* Returns:
* 0 on success
* Negative value on error
*/
int interactive_mode(void);
/*
* Launch a job
*
* Parameters:
* loc_job : job to execute
*
* Returns:
* 0 on success
* Negative value on error
*/
int launch_job(job_t *loc_job);
/*
* Built-in 'exit' command
*
* Parameters:
* None
*
* Returns:
* 0 on success
* Negative value on error
*/
int builtin_exit(void);
/*
* Built-in 'jobs' command
*
* Parameters:
* None
*
* Returns:
* 0 on success
* Negative value on error
*/
int builtin_jobs(void);
/*
* Built-in 'history' command
*
* Parameters:
* None
*
* Returns:
* 0 on success
* Negative value on error
*/
int builtin_history(void);
/*
* Built-in 'wait' command
*
* Parameters:
* None
*
* Returns:
* 0 on success
* Negative value on error
*/
int builtin_wait(void);
/*
* Built-in 'fg' command
*
* Parameters:
* None (use default behavior)
*
* Returns:
* 0 on success
* Negative value on error
*/
int builtin_fg(void);
/*
* Built-in 'fg' command
*
* Parameters:
* Job id
*
* Returns:
* 0 on success
* Negative value on error
*/
int builtin_fg_num(int job_num);
/**
* @brief Prints out the final counts of jobs before exiting
*
*/
void display_counts(void);
/**
* @brief Given an array of strings, concatenate all strings together with the joint separating them into one long string
*
* @param size The size of the array
* @param array The array of strings
* @param joint What character should separate each string
*
* @return char* That represents all strings joined together
*/
char *concatenate(size_t size, char *array[size], const char *joint);
/**
* @brief Parse a given input and designate jobs for it
*
* @param input User given input used to be entered into the shell
*/
void parse_job(char *input);
/**
* @brief Add a given job to the larger list of jobs
*
* @param loc_job Job to be added to respective lists
*/
void add_job_to_lists(job_t *loc_job);
/**
* @brief Remove all finished jobs from the list of current jobs
*
*/
void removeFinishedJobs();
#endif /* MYSHELL_H */