-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell_core.c
More file actions
258 lines (222 loc) · 6.12 KB
/
shell_core.c
File metadata and controls
258 lines (222 loc) · 6.12 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
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
#include "shell_core.h"
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#define MULTI_MAX 8 //同時に実行できるコマンドの最大値
#define WORD_MAX 32
#define PATH_DEPTH_MAX 8
#define QUICK_MAX 8
#define WORD_MAX 32
static const char multi_split[] = ":";
static const char word_split[] = " \r\n";
file_t current; //現在のパス
file_t root; //最上位パス
file_t lst_quick[QUICK_MAX]; //クイックリスト
file_t path_root(){
return root;
}
file_t path_current(){
return current;
}
void path_move(file_t path){
current=path;
}
void path_init(){
//メモリ空間を初期化しroot,currentの場所を決める。
gc_init();
current=root = directory_create("root");
}
int path_insert(file_t brother, file_t it) {
if (brother == NULL)return -1;
if (it == NULL)return -2;
//brother
it->next = brother->next;
brother->next = it;
}
Access_t path_access_get(file_t path) {
if (path != NULL) {
return path->fags&AccessMask;
} else {
return AccessNone;
}
}
void path_access_set(file_t path, Access_t acc) {
if (path != NULL) {
path->fags = (path->fags&~AccessMask) | (acc & AccessMask);
}
}
FileType_t path_filetype_get(file_t path) {
if (path != NULL) {
return path->fags&FileTypeMask;
} else {
return FileTypeNone;
}
}
void path_filetype_set(file_t path, FileType_t type) {
if (path != NULL) {
path->fags = (path->fags&~FileTypeMask) | (type & FileTypeMask);
}
}
file_t path_get(const char* name) {
const char split[] = "/"; //Directory用区切り文字
file_t it;
bool fag = true;
char buf[WORD_MAX], *p, *word;
if (name == NULL)return NULL;
strcpy(buf, name); //const外しを行う
p = buf;
if (*p == '/') {
it = root;
p++;
if (*p == 0)return root;
} else {
it = current;
}
it = it->child;
word = strtok(p, split);
while (word != NULL) {
while (!strcmp(".", word)) {
word = strtok(NULL, split);
if (word == NULL)return it;
}
while (!strcmp("..", word)) {
//初回は下がりすぎているので2つ戻す。
if (fag) {
it = it->parent;
fag = false;
}
it = it->parent;
word = strtok(NULL, split);
if (word == NULL)return it;
}
if (it->name == NULL) {
} else if (!strcmp(word, it->name)) {
word = strtok(NULL, split);
if (word == NULL) {
return it;
} else {
it = it->child;
if (it==NULL)return NULL;
}
} else {
it = it->next;
if (it == NULL)return NULL;
}
}
return it;
}
file_t path_get_registor(const char* name) {
int idx;
file_t it;
if (name == NULL)return NULL;
for (idx = 0; idx < QUICK_MAX; idx++) {
if (lst_quick[idx] != NULL) {
for (it = lst_quick[idx]->child; it != NULL; it = it->next) {
if (!strcmp(name, it->name)) {
return it;
}
}
}
}
return NULL;
}
file_t path_get_all(const char* name) {
file_t it;
int idx;
it = path_get(name);
if (it != NULL)return it;
it = path_get_registor(name);
if (it != NULL)return it;
return NULL;
}
file_t directory_create(const char *name) {
file_t path = gc_path_create(name);
if (name == NULL)return NULL;
path->fags = AccessNone | FileTypeDirectory;
path->child = NULL; //まだ子要素は存在しない。
return path;
}
int directory_insert(file_t directory, file_t file) {
if (directory == NULL)return -1; //error
if (file == NULL)return -2;
if ((directory->fags & FileTypeMask) != FileTypeDirectory)return -3;
//メモリー配置を行う。
file->parent = directory;
file->next = directory->child;
directory->child = file;
return 0;
}
int directory_registor(file_t dir) {
if (dir == NULL)return -1;
int idx;
for (idx = 0; idx < QUICK_MAX; idx++) {
if (lst_quick[idx] == NULL) {
lst_quick[idx] = dir;
return 0;
}
}
return -1;
}
void directory_registor_init() {
int idx;
for (idx = 0; idx < QUICK_MAX; idx++) {
lst_quick[idx] = NULL;
}
}
file_t execute_create(const char* name, int(*func)(int, char**)) {
file_t path = gc_path_create(name);
if (path == NULL)return NULL;
path->fags = AccessExcute | FileTypeExcute;
path->func = func;
return path;
}
file_t integer_create(const char* name, int* ptr, Access_t acc) {
file_t path = gc_path_create(name);
if (path == NULL)return NULL;
if (ptr == NULL)return NULL;
path->fags = acc | FileTypeInteger;
path->ptr_int = ptr;
return path;
}
file_t float_create(const char* name,float* ptr,Access_t acc){
file_t path = gc_path_create(name);
if (path == NULL)return NULL;
if (ptr == NULL)return NULL;
path->fags = acc | FileTypeFloat;
path->ptr_float = ptr;
return path;
}
int shell_system(char *command) {
int mcnt = 0, midx = 0, argc, ans;
char *argv[WORD_MAX], *mul[MULTI_MAX];
char *temp;
mul[mcnt] = temp = strtok(command, multi_split);
while (temp != NULL && argc < MULTI_MAX) {
mul[++mcnt] = temp = strtok(NULL, multi_split);
}
for (; midx < mcnt; midx++) {
argv[argc = 0] = temp = strtok(mul[midx], word_split);
while (temp != NULL && argc < WORD_MAX) {
argv[++argc] = temp = strtok(NULL, word_split);
}
ans = shell_system_s(argc, argv);
}
return ans;
}
int shell_system_s(int argc, char* argv[]) {
file_t it;
it = path_get_all(argv[0]);
if (it == NULL)return -1;
switch (it->fags & FileTypeExcute) {
case FileTypeExcute:
if (it->fags & AccessExcute) {
return it->func(argc, argv);
} else {
puts("Don't Run!!\n");
return -3;
}
default:
puts("Error:Don't excute!!");
return -4;
}
}