-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseashell.c
More file actions
152 lines (131 loc) · 3.85 KB
/
seashell.c
File metadata and controls
152 lines (131 loc) · 3.85 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include "./seashell.h"
#include "./cmds/cmds.h"
#include "./prompt/prompt.h"
#include "./runner/runner.h"
#include "./signals/signals.h"
#include "./terminal/terminal.h"
#include <standardloop/logger.h>
#include <standardloop/util.h>
#define CHILD_PID 0
int seaShellInteractive();
void seaShellNoInteractive();
static inline bool isInteractive();
static inline bool isInteractive()
{
return isatty(STDIN_FILENO) == 1;
}
extern void ClearBuffer(char *buffer, int size)
{
if (buffer == NULL || size <= 0)
{
return;
}
for (int reset_index = 0; reset_index < size - 1; reset_index++)
{
buffer[reset_index] = NULL_CHAR;
}
}
static char **stringArrToExecArgs(StringArr *arr)
{
if (arr == NULL || arr->num_strings == 0 || arr->strings == NULL)
{
return NULL;
}
char **return_value = malloc(sizeof(char *) * (arr->num_strings + 1)); // +1 for NULL at end
if (return_value == NULL)
{
return NULL;
}
int i;
for (i = 0; i < arr->num_strings; i++)
{
return_value[i] = arr->strings[i];
}
return_value[i] = NULL;
return return_value;
}
int seaShellInteractive()
{
Log(INFO, "Running Interactive");
InitTerminal();
// populate global pwd variable
if (getcwd(GLOBAL_pwd, sizeof(GLOBAL_pwd)) == NULL)
{
Log(ERROR, "couldn't populate GLOBAL_pwd");
return 1;
}
while (GLOBAL_seashell_running)
{
DisplayPrompt(GLOBAL_last_status);
fflush(stdout);
char command_buffer[COMMAND_BUFFER_SIZE] = {0};
int err = GetSeashellLine(command_buffer);
// TODO
if (err != 0)
;
if (GLOBAL_seashell_running)
{
printf("\n");
printf("\r");
if (command_buffer[0] != NULL_CHAR)
{
StringArr *buffer_seperated_by_spaces = EveryoneExplodeNowHandleQuotes(command_buffer, SPACE_CHAR, DOUBLE_QUOTES_CHAR); // looks of work needed here
PrintStringArr(buffer_seperated_by_spaces);
// see if command matches built in
SeashellFunction *built_in = FunctionStringToFunction(buffer_seperated_by_spaces->strings[0]);
if (built_in != NULL)
{
GLOBAL_last_status = built_in(buffer_seperated_by_spaces);
}
else if (buffer_seperated_by_spaces->num_strings >= 1 && buffer_seperated_by_spaces->strings[0] != NULL && buffer_seperated_by_spaces->strings[0][0] != NULL_CHAR)
{
// execvp requires char** instead of our custom StringArr for our custom <standardloop/util.h>
char **exec_args = stringArrToExecArgs(buffer_seperated_by_spaces);
// TODO: need to check if a command exists before trying to run it.
GLOBAL_last_status = RunCommand(exec_args);
free(exec_args);
}
ClearBuffer(command_buffer, COMMAND_BUFFER_SIZE);
FreeStringArr(buffer_seperated_by_spaces);
}
else
{
GLOBAL_last_status = 0;
}
}
}
RestoreTerminal();
return GLOBAL_last_status;
}
void seaShellNoInteractive()
{
Log(INFO, "Running Non Interactive");
}
int main(int argc, char **argv)
{
InitLogger(TRACE, STANDARD_FMT, false, true, true, true);
Log(TRACE, "歡迎光臨");
for (int i = 0; i < argc; i++)
{
Log(INFO, "argv[%d]: %s", i, argv[i]);
}
if (SignalsInit() != 0)
{
return EXIT_FAILURE;
}
if (isInteractive())
{
seaShellInteractive();
return GLOBAL_last_status;
}
else
{
// TODO
seaShellNoInteractive();
}
return EXIT_SUCCESS;
}