-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShell.cpp
More file actions
514 lines (450 loc) · 10.8 KB
/
Shell.cpp
File metadata and controls
514 lines (450 loc) · 10.8 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
#include "Shell.h"
#include "Context.h"
#include <string>
#include <iomanip>
extern Context cxt;
/**
*@ comment 构造函数
*/
Shell::Shell() {
cxt.GetFileManager()->Initialize();
cxt.c_pwd = cxt.GetrootDirInode();
cxt.c_pwd_path += "/";
strcpy(Shell::CodeTable[0], "ls");
strcpy(Shell::CodeTable[1], "touch");
strcpy(Shell::CodeTable[2], "mkdir");
strcpy(Shell::CodeTable[3], "cd");
strcpy(Shell::CodeTable[4], "rm");
strcpy(Shell::CodeTable[5], "open");
strcpy(Shell::CodeTable[6], "read");
strcpy(Shell::CodeTable[7], "write");
strcpy(Shell::CodeTable[8], "close");
strcpy(Shell::CodeTable[9], "lsof");
strcpy(Shell::CodeTable[10], "seek");
strcpy(Shell::CodeTable[11], "format");
strcpy(Shell::CodeTable[12], "shutdown");
}
/**
*@ comment 析构函数
*/
Shell::~Shell() {
}
/**
*@ comment 运行Shell
*/
void Shell::RunShell() {
char* cli = new char[CNUM*CLENGTH];
while (true)
{
cout <<*(cxt.GetPwdPath())<<"# ";
cin.getline(cli, CNUM*CLENGTH);
if (!this->CmdEntrance(cli))
{
return;
}
}
return;
}
/**
*@ comment 将指令转换为相应的code
*/
int Shell::Cmd2Code(char* cmd) {
for (int i = 0; i < CMD_COUNT; i++)
{
if (!strcmp(cmd, Shell::CodeTable[i]))
{
return i;
}
}
return -1;
}
/**
*@ comment 解析一行命令成一个数组
*/
char* Shell::ParseCmd(char* cli) {
int i = 0;
int CIndex = 0; //CTable的下标
int CCount = 0; //当前处理第CCount个参数
int CFlag = 0; //用来判定是否已经完全读入一个词
char* CTable = new char[CNUM*CLENGTH];
if (cli == NULL)
{
cout << "无效字符串" << endl;
return NULL;
}
for (; cli[i] != '\0'&& CCount < CNUM; i++)
{
if (cli[i] == ' ') //跳过空格
{
if (CFlag == 1)
{
CTable[CLENGTH*CCount + CIndex] = '\0';
CCount++;
CIndex = 0;
CFlag = 0;
}
continue;
}
else
{
CTable[CLENGTH*CCount + CIndex] = cli[i]; //CLENGTH*CCount为第CCount+1个参数的起始地址,CIndex为行内偏移量
CIndex++;
if (CFlag == 0)
{
CFlag = 1;
}
}
if (CIndex >= CLENGTH)
{
cout << "参数长度过长" << endl;
return NULL;
}
}
CTable[CLENGTH*CCount + CIndex] = '\0';
return CTable;
}
/**
*@ comment 处理单个指令
*/
int Shell::CmdEntrance(char* cli) {
char* CTable;
FileManager* fm = cxt.GetFileManager();
CTable = ParseCmd(cli);
switch (Cmd2Code(CTable))
{
case Shell::LS: {
this->ls();
break;
}
case Shell::TOUCH: {
fm->Fcreat(CTable + CLENGTH,Inode::IALLOC);
break;
}
case Shell::MKDIR: {
this->mkdir(CTable + CLENGTH);
break;
}
case Shell::CD: {
this->cd(CTable + CLENGTH);
break;
}
case Shell::RM: {
fm->Fdelete(CTable + CLENGTH);
break;
}
case Shell::OPEN: {
this->open(CTable + CLENGTH);
break;
}
case Shell::READ: {
int fd = 0;
int length = 0;
for (int i = 1; i < CNUM - 1; i++) //粗略处理,最后一个参数不检查,防止越界
{ //这一部分用来获取参数
if (!strcmp(CTable + CLENGTH*i, "-fd"))
{
fd = atoi(CTable + CLENGTH*(i + 1));
continue;
}
if (!strcmp(CTable + CLENGTH*i, "-l"))
{
length = atoi(CTable + CLENGTH*(i + 1));
continue;
}
if (!strcmp(CTable + CLENGTH*i, "-all"))
{
cxt.GetOpenFiles()->GetF(fd)->f_offset = 0;
length = cxt.GetOpenFiles()->GetF(fd)->f_inode->i_size;
continue;
}
}
this->read(fd, length);
break;
}
case Shell::WRITE: {
int fd = 0;
int length = 0;
int FromFile = 0;
char Path[CLENGTH];
FileManager* fm = cxt.GetFileManager();
for (int i = 1; i < CNUM - 1; i++) //粗略处理,最后一个参数不检查,防止越界
{ //这一部分用来获取参数
if (!strcmp(CTable + CLENGTH*i, "-fd"))
{
fd = atoi(CTable + CLENGTH*(i + 1));
continue;
}
if (!strcmp(CTable + CLENGTH*i, "-l"))
{
length = atoi(CTable + CLENGTH*(i + 1));
continue;
}
if (!strcmp(CTable + CLENGTH*i, "-p"))
{
strcpy(Path, CTable + CLENGTH*(i + 1));
FromFile = 1;
continue;
}
}
if (FromFile)
{
int Buffer = 0;
FILE* InFile = fopen(Path, "rb+");
if (!InFile)
{
cout << "文件打开失败" << endl;
break;
}
int count = 0;
while (Buffer != EOF)
{
Buffer = getc(InFile);
char ch = Buffer;
if (Buffer == EOF)
break;
fm->Fwrite(fd, &ch, 1);
count++;
}
cout << "输入的字符数量为:" << count << endl;
fclose(InFile);
}
else
{
char* Buffer = new char[length + 1];
cin.getline(Buffer, length);
fm->Fwrite(fd, Buffer, length);
delete Buffer;
}
break;
}
case Shell::CLOSE: {
int fd = 0;
for (int i = 1; i < CNUM - 1; i++) //粗略处理,最后一个参数不检查,防止越界
{ //这一部分用来获取参数
if (!strcmp(CTable + CLENGTH*i, "-fd"))
{
fd = atoi(CTable + CLENGTH*(i + 1));
continue;
}
}
this->close(fd);
break;
}
case Shell::LSOF: {
this->lsof();
break;
}
case Shell::SEEK: {
int fd = 0;
int position = 0;
for (int i = 1; i < CNUM - 1; i++) //粗略处理,最后一个参数不检查,防止越界
{ //这一部分用来获取参数
if (!strcmp(CTable + CLENGTH*i, "-fd"))
{
fd = atoi(CTable + CLENGTH*(i + 1));
continue;
}
if (!strcmp(CTable + CLENGTH*i, "-offset"))
{
position = atoi(CTable + CLENGTH*(i + 1));
continue;
}
}
this->seek(fd, position);
break;
}
case Shell::FORMAT: {
FileManager* fm = cxt.GetFileManager();
fm->fformat();
break;
}
case Shell::SHUTDOWN: {
return 0;
}
default:
cout << "无效命令" << endl;
break;
}
return 1;
}
/**
*@ comment 列出当前目录的所有文件或者目录名
*/
void Shell::ls() {
Inode* pwd = cxt.GetPwd();
DirectoryEntry* DirEntry = new DirectoryEntry();
int DirNo = pwd->i_size / sizeof(DirectoryEntry); //计算当前目录的表项数量
cxt.GetIOParameter()->m_Base = 0;
cxt.GetIOParameter()->m_Offset = 0;
cxt.GetIOParameter()->m_Count = sizeof(DirectoryEntry);
cxt.c_ReadBuffer = (char*)DirEntry;
for (int i = 0; i < DirNo; i++) //这一部分是一个读入当前目录的每一项,对比当前目录有没有名字相同的表项
{
pwd->ReadI();
cout<< setiosflags(ios::left) <<setw(20)<< DirEntry->m_name;
if (i>0&&i % 5 == 0)
cout << endl;
//cxt.GetIOParameter()->m_Base += sizeof(DirectoryEntry);
cxt.GetIOParameter()->m_Offset = sizeof(DirectoryEntry)*(i+1);
cxt.c_ReadBuffer = (char*)DirEntry;
cxt.GetIOParameter()->m_Count = sizeof(DirectoryEntry);
}
cout << endl;
}
/**
*@ comment 创建目录
*/
void Shell::mkdir(char* fName) {
Inode* pwd = cxt.GetPwd();
FileManager* fm = cxt.GetFileManager();
Inode* DirInode = new Inode();
DirectoryEntry* DirEntry = new DirectoryEntry();
if ((DirInode->i_number = fm->Fcreat(fName, Inode::IALLOC | Inode::IFDIR)) == NULL)
{
return;
}
DirInode->ICopy();
DirEntry->m_ino = DirInode->i_number; //将当前目录 "." 写入目录表
strcpy(DirEntry->m_name, ".");
cxt.GetIOParameter()->m_Base = 0;
cxt.GetIOParameter()->m_Offset = 0;
cxt.GetIOParameter()->m_Count = sizeof(DirectoryEntry);
cxt.c_ReadBuffer = (char*)DirEntry;
DirInode->WriteI();
DirEntry->m_ino = pwd->i_number; //将上级目录 ".." 写入目录表
strcpy(DirEntry->m_name, "..");
cxt.GetIOParameter()->m_Base = 0;
cxt.GetIOParameter()->m_Offset = sizeof(DirectoryEntry);
cxt.GetIOParameter()->m_Count = sizeof(DirectoryEntry);
cxt.c_ReadBuffer = (char*)DirEntry;
DirInode->WriteI();
delete DirInode;
}
/**
*@ comment 改变当前目录,只支持相对路径寻址
*/
void Shell::cd(char* Path) {
DirectoryEntry* DirEntry;
FileManager* fm = cxt.GetFileManager();
Inode* pwd = cxt.GetPwd();
string* PwdPath = cxt.GetPwdPath();
if (Path == NULL)
{
return;
}
if (!strcmp(Path, ".")) //cd到当前目录,什么事都不用做
{
return;
}
if (!strcmp(Path, "..")) //cd到前一目录
{
if ((DirEntry = fm->FindDir("..", pwd)) == NULL)
{
cout << "文件或目录不存在" << endl;
return;
}
pwd->i_number = DirEntry->m_ino; // 改变当前路径pwd的Inode
pwd->ICopy();
int SlashCount = 0;
for (int i = PwdPath->length() - 1; i >= 0; i--) //修改当前路径字符串
{
if (PwdPath->compare(i,1,"/") == 0 )
{
//PwdPath[i] = '\0';
SlashCount++;
}
if (SlashCount == 2)
{
*PwdPath = PwdPath->substr(0, i + 1);
break;
}
}
return;
}
if ((DirEntry = fm->FindDir(Path, pwd)) == NULL)
{
cout << "文件或目录不存在" << endl;
return;
}
int TempIno = pwd->i_number;
pwd->i_number = DirEntry->m_ino; // 改变当前路径pwd的Inode
pwd->ICopy();
if ((pwd->i_mode & Inode::IFDIR) == 0)
{
pwd->i_number = TempIno;
pwd->ICopy();
cout << "不是目录" << endl;
return;
}
PwdPath->append(Path);
PwdPath->append("/");
}
/**
*@ comment 打开文件,修改打开文件列表
*/
void Shell::open(char* fName) {
FileManager* fm = cxt.GetFileManager();
fm->Fopen(fName, File::FREAD | File::FWRITE);
return;
}
/**
*@ comment 读文件
*/
void Shell::read(int fd, int length) {
FileManager* fm = cxt.GetFileManager();
File* OpenFile = cxt.GetOpenFiles()->GetF(fd);
if ((length + OpenFile->f_offset) > OpenFile->f_inode->i_size) //如果读取的长度length加上偏移量大于文件的大小
{
length = OpenFile->f_inode->i_size - OpenFile->f_offset;
}
char* Buffer = new char[2];
for (int i = 0; i < length; i++)
{
fm->Fread(fd, Buffer, 1);
Buffer[1] = '\0';
cout << Buffer;
}
cout << endl;
//fm->Fread(fd, Buffer, length);
//Buffer[length] = '\0';
//cout << Buffer << endl; //输出Buffer内容
delete Buffer;
return;
}
/**
*@ comment 写文件
*/
void Shell::write(int fd, int length) {
return;
}
/**
*@ comment 关闭文件
*/
void Shell::close(int fd) {
FileManager* fm = cxt.GetFileManager();
fm->Fclose(fd);
return;
}
/**
*@ comment 输出当前已经打开的文件列表
*/
void Shell::lsof() {
OpenFiles* FilesTable = cxt.GetOpenFiles();
for (int i = 0; i < OpenFiles::NOFILES; i++)
{
File* pFile;
pFile = FilesTable->ProccessOpenFileTable[i];
if (pFile == NULL)
continue;
cout << "fd:" << setiosflags(ios::left) << setw(5) << i << FilesTable->FileName[i] << endl;
}
}
/**
*@ comment 改变读写指针的offset
*/
void Shell::seek(int fd, int position) {
FileManager* fm = cxt.GetFileManager();
fm->Flseek(fd, position);
return;
}