-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
1846 lines (1618 loc) · 61.6 KB
/
main.cpp
File metadata and controls
1846 lines (1618 loc) · 61.6 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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Tác giả: moonshineTP
// Bài tập lớn môn OS
//-------------Các macros của chương trình------------------------
// ------------Các thư viện cần thiết cho chương trình------------------------
// Thư viện chuẩn
#include <iostream>
#include <fstream>
#include <string>
#include <unordered_map>
#include <map>
#include <unordered_set>
#include <vector>
#include <windows.h>
#include <fcntl.h>
#include <io.h>
#include <psapi.h>
// ------------Các cấu trúc dữ liệu của chương trình------------------------
/*
* Cấu trúc dữ liệu này được sử dụng để lưu trữ thông tin về lệnh và cờ
* trong phần mềm MoonShell.
*
* - Command: Biểu trưng về lệnh được nhập vào từ bàn phím,
* - CommandDescription: Mô tả lệnh, bao gồm tên lệnh, mô tả và các cờ
* - Flag: Biểu trưng về cờ được nhập vào từ lệnh từ bàn phím.
* - FlagDescription: Mô tả cờ, bao gồm tên cờ và mô tả.
*
* Các cấu trúc dữ liệu này sẽ được sử dụng để quản lý và thao tác với các đối
* tượng trong phần mềm,
*/
// ---Cấu trúc dữ liệu lưu trữ lệnh và cờ
struct Command {
std::wstring name; // Tên lệnh
std::unordered_map<std::wstring, std::vector<std::wstring>> map; // Cặp cờ - dãy tham số
Command(const std::wstring& _name,
const std::unordered_map<std::wstring, std::vector<std::wstring>> _map)
: name(_name), map(_map) {}
};
struct CommandDescription {
std::wstring name; // Tên lệnh
std::wstring description; // Mô tả lệnh
std::unordered_set<std::wstring> possible_flags; // Tập hợp các cờ khả dụng cho lệnh
CommandDescription(const std::wstring& _name,
const std::wstring& _description,
const std::unordered_set<std::wstring>& _flags)
: name(_name), description(_description), possible_flags(_flags) {}
CommandDescription() = default;
};
struct Flag {
std::wstring name; // Tên cờ
Flag (const std::wstring& _name)
: name(_name) {}
};
struct FlagDescription {
std::wstring name; // Tên cờ
std::wstring description; // Mô tả cờ
FlagDescription(const std::wstring& _name, const std::wstring& _description)
: name(_name), description(_description) {}
FlagDescription() = default;
};
struct CommandDictionary {
std::map<std::wstring, CommandDescription> commandMap;
std::map<std::wstring, FlagDescription> flagMap;
CommandDictionary() = default;
};
// ------------Các biến toàn cục của chương trình------------------------
CommandDictionary commandDict = CommandDictionary(); // Lưu trữ các lệnh và cờ
std::unordered_map<DWORD, HANDLE> currentProcesses; // Lưu trữ các tiến trình theo PID
std::wstring rootPath; // Đường dẫn gốc
std::wstring currentPath; // Đường dẫn hiện tại
std::unordered_map<std::wstring, std::wstring> variables; // Biến của tiến trình
// ------------Các hàm tiện ích của chương trình------------------------
// Handler for Ctrl+C and other console events
BOOL WINAPI ConsoleHandler(DWORD signal) {
if (signal == CTRL_C_EVENT || signal == CTRL_BREAK_EVENT || signal == CTRL_CLOSE_EVENT) {
std::wcout << L"\nĐang kết thúc tất cả tiến trình do Ctrl+C...\n";
for (auto& [pid, hProcess] : currentProcesses) {
if (hProcess && TerminateProcess(hProcess, 0)) {
std::wcout << L"Đã kết thúc tiến trình PID: " << pid << L"\n";
CloseHandle(hProcess);
}
}
currentProcesses.clear();
std::wcout << L"Đã kết thúc tất cả tiến trình. Thoát chương trình.\n";
ExitProcess(0);
}
return FALSE;
}
void initializeRootDirectory() {
// Lấy đường dẫn thư mục hiện tại khi chạy chương trình
wchar_t buffer[MAX_PATH];
GetCurrentDirectoryW(MAX_PATH, buffer);
std::wstring repoPath = buffer;
// Thiết lập rootDirectory là thư mục "root" trong repo này
rootPath = repoPath + L"\\root";
currentPath = rootPath;
SetCurrentDirectoryW(rootPath.c_str());
}
void initializeCommandDictionary() {
// Khởi tạo các lệnh đặc biệt
commandDict.commandMap[L"NULL"] = {
L"NULL", L"Lệnh trống", {}
};
commandDict.commandMap[L"INVALID"] = {
L"INVALID", L"Lệnh không hợp lệ", {}
};
commandDict.commandMap[L"WRONG_VAR"] = {
L"WRONG_VAR", L"Biến không tồn tại", {}
};
// Lệnh không có cờ
commandDict.commandMap[L"intro"] = {
L"intro", L"Giới thiệu phần mềm MoonShell", {}
};
commandDict.commandMap[L"help"] = {
L"help", L"Hiển thị danh sách các lệnh và cờ", {}
};
commandDict.commandMap[L"exit"] = {
L"exit", L"Thoát khỏi phần mềm", {}
};
commandDict.commandMap[L"example"] = {
L"example", L"Hiển thị ví dụ sử dụng phần mềm", {}
};
commandDict.commandMap[L"clrscr"] = {
L"clrscr", L"Làm sạch console", {}
};
commandDict.commandMap[L"viewvar"] = {
L"viewvar", L"Hiển thị tất cả các biến shell lưu giữ", {}
};
commandDict.commandMap[L"viewpath"] = {
L"viewpath", L"Hiển thị các biến trong PATH", {}
};
commandDict.commandMap[L"up"] = {
L"up", L"Quay lại thư mục cha", {}
};
commandDict.commandMap[L"return"] = {
L"return", L"Quay về thư mục gốc", {}
};
commandDict.commandMap[L"viewdir"] = {
L"viewdir", L"Hiển thị danh sách file và thư mục trong thư mục hiện tại", {}
};
commandDict.commandMap[L"sysinfo"] = {
L"sysinfo", L"Hiển thị thông tin hệ thống", {}
};
commandDict.commandMap[L"timeinfo"] = {
L"timeinfo", L"Hiển thị thời gian hệ thống", {}
};
commandDict.commandMap[L"userinfo"] = {
L"userinfo", L"Hiển thị thông tin người dùng", {}
};
commandDict.commandMap[L"ipinfo"] = {
L"ipinfo", L"Hiển thị thông tin IP/network adapters", {}
};
// Lệnh có cờ
commandDict.commandMap[L"echo"] = {
L"echo", L"In ra nội dung lệnh", {L"-argS"}
};
commandDict.commandMap[L"setfgcol"] = {
L"setfgcol", L"Thay đổi màu sắc của chữ", {L"-color"}
};
commandDict.commandMap[L"setbgcol"] = {
L"setbgcol", L"Thay đổi màu sắc của console", {L"-color"}
};
commandDict.commandMap[L"setvar"] = {
L"setvar", L"Thiết lập biến shell", {L"-key", L"-value"}
};
commandDict.commandMap[L"getvar"] = {
L"getvar", L"Lấy giá trị của biến trong shell", {L"-key"}
};
commandDict.commandMap[L"rmvar"] = {
L"rmvar", L"Xóa biến trong shell", {L"-key"}
};
commandDict.commandMap[L"addpath"] = {
L"addpath", L"Thêm biến trong PATH", {L"-path"}
};
commandDict.commandMap[L"rmpath"] = {
L"rmpath", L"Xóa biến trong PATH", {L"-path"}
};
commandDict.commandMap[L"down"] = {
L"down", L"Chuyển đến thư mục con", {L"-dir"}
};
commandDict.commandMap[L"adddir"] = {
L"adddir", L"Tạo thư mục", {L"-dir"}
};
commandDict.commandMap[L"addfile"] = {
L"addfile", L"Tạo file", {L"-csv", L"-txt", L"-json", L"-bat"}
};
commandDict.commandMap[L"viewfile"] = {
L"viewfile", L"Xem nội dung file", {L"-csv", L"-txt", L"-json", L"-bat"}
};
commandDict.commandMap[L"writefile"] = {
L"writefile", L"Viết đè lên file", {L"-csv", L"-txt", L"-json", L"-bat"}
};
commandDict.commandMap[L"rename"] = {
L"rename", L"Đổi tên thư mục hoặc file", {L"-targetname", L"-newname"}
};
commandDict.commandMap[L"delete"] = {
L"delete", L"Xóa thư mục hoặc file", {L"-targetname"}
};
commandDict.commandMap[L"viewproc"] = {
L"viewproc", L"Hiển thị danh sách các tiến trình được quản lý bới shell", {}
};
commandDict.commandMap[L"killproc"] = {
L"killproc", L"Kết thúc tiến trình đang chạy", {L"-pidS"}
};
commandDict.commandMap[L"suspendproc"] = {
L"suspendproc", L"Tạm dừng tiến trình đang chạy", {L"-pidS"}
};
commandDict.commandMap[L"resumeproc"] = {
L"resumeproc", L"Tiếp tục tiến trình đã tạm dừng", {L"-pidS"}
};
commandDict.commandMap[L"build"] = {
L"build", L"Xây dựng file thực thi từ file chương trình", {L"-c", L"-cpp", L"-exe", L"-java"}
};
commandDict.commandMap[L"run"] = {
L"run", L"Chạy file (được hỗ trợ trong PATHEXT) cùng với dãy tham số", {L"-argS", L"-bg"}
};
commandDict.commandMap[L"runbatch"] = {
L"runbatch", L"Chạy file .bat cùng với dãy tham số", {L"-argS", L"-bg"}
};
commandDict.commandMap[L"runclass"] = {
L"runclass", L"Chạy file .class cùng với dãy tham số", {L"-argS", L"-optionS", L"-bg"}
};
commandDict.commandMap[L"runjar"] = {
L"runjar", L"Chạy file .jar cùng với dãy tham số", {L"-argS", L"-optionS", L"-bg"}
};
commandDict.commandMap[L"ping"] = {
L"ping", L"Ping một host", {L"-host"}
};
commandDict.commandMap[L"traceroute"] = {
L"traceroute", L"Traceroute tới một host", {L"-host"}
};
commandDict.commandMap[L"nslookup"] = {
L"nslookup", L"Tra cứu DNS cho một host", {L"-host"}
};
commandDict.commandMap[L"portcheck"] = {
L"portcheck", L"Kiểm tra cổng mạng", {L"-port"}
};
// Khởi tạo các cờ
commandDict.flagMap[L"-dir"] = {
L"-dir", L"Tên thư mục đích khi chuyển đến thư mục con hoặc tạo thư mục"
};
commandDict.flagMap[L"-targetname"] = {
L"-targetname", L"Tên thư mục/file được nhắm đến trong lệnh"
};
commandDict.flagMap[L"-newname"] = {
L"-newname", L"Tên mới của thư mục hoặc file"
};
commandDict.flagMap[L"-c"] = {
L"-c", L"File C"
};
commandDict.flagMap[L"-cpp"] = {
L"-cpp", L"File C++"
};
commandDict.flagMap[L"-java"] = {
L"-java", L"File Java"
};
commandDict.flagMap[L"-exe"] = {
L"-exe", L"File exe"
};
commandDict.flagMap[L"-bg"] = {
L"-bg", L"Chạy file trong nền không (background), phải đi kèm với tham số <yes>"
};
commandDict.flagMap[L"-argS"] = {
L"-argS", L"Tham số truyền vào khi chạy file"
};
commandDict.flagMap[L"-optionS"] = {
L"-optionS", L"Các tùy chọn bổ sung khi chạy file .class hoặc .jar"
};
commandDict.flagMap[L"-csv"] = {
L"-csv", L"File CSV"
};
commandDict.flagMap[L"-txt"] = {
L"-txt", L"File TXT"
};
commandDict.flagMap[L"-json"] = {
L"-json", L"File JSON"
};
commandDict.flagMap[L"-bat"] = {
L"-bat", L"File BAT"
};
commandDict.flagMap[L"-pidS"] = {
L"-pidS", L"Mã tiến trình (PID) của tiến trình đang chạy"
};
commandDict.flagMap[L"-color"] = {
L"-color",
L"Màu sắc của console. Các giá trị hợp lệ:\n"
L" * black\n"
L" * red\n"
L" * green\n"
L" * blue\n"
L" * yellow\n"
L" * magenta\n"
L" * cyan\n"
L" * white"
};
commandDict.flagMap[L"-key"] = {
L"-key", L"Tên biến trong shell"
};
commandDict.flagMap[L"-value"] = {
L"-value", L"Giá trị của biến trong shell"
};
commandDict.flagMap[L"-path"] = {
L"-path", L"Đường dẫn của biến trong PATH"
};
commandDict.flagMap[L"-host"] = {
L"-host", L"Địa chỉ mạng"
};
commandDict.flagMap[L"-port"] = {
L"-port", L"Cổng kết nối"
};
}
// Hàm lấy lệnh thô từ người dùng
std::wstring getRawCommandLine() {
std::wcout << currentPath << L">"; // Hiển thị đường dẫn hiện tại
std::wstring commandLine;
std::getline(std::wcin, commandLine);
return commandLine;
}
Command decomposeCommandLine(const std::wstring& commandLine) {
// Tách lệnh, cờ và tham số từ chuỗi lệnh theo quy tắc đặc biệt
std::wstring commandName;
std::unordered_map<std::wstring, std::vector<std::wstring>> commandMap;
// Collect tokens
std::vector<std::wstring> tokens;
size_t start = 0, end;
while ((end = commandLine.find(' ', start)) != std::wstring::npos) {
tokens.push_back(commandLine.substr(start, end - start));
start = end + 1;
}
if (start < commandLine.size())
tokens.push_back(commandLine.substr(start));
// If empty command, return nullptr
if (tokens.empty()) return Command(
L"NULL",
std::unordered_map<std::wstring, std::vector<std::wstring>>{}
);
// The first word must be the command
commandName = tokens[0];
if (commandDict.commandMap.find(commandName) == commandDict.commandMap.end()) {
return Command(
L"INVALID",
std::unordered_map<std::wstring, std::vector<std::wstring>>{}
);
}
size_t idx = 1;
while (idx < tokens.size()) {
std::wstring flag = tokens[idx];
if (commandDict.flagMap.find(flag) == commandDict.flagMap.end()
|| commandDict.commandMap[commandName].possible_flags.find(flag)
== commandDict.commandMap[commandName].possible_flags.end())
{
return Command(
L"INVALID",
std::unordered_map<std::wstring, std::vector<std::wstring>>{}
);
}
commandMap[flag] = {};
idx++;
// Kiểm tra cờ có phải là dạng số nhiều hay khôn
bool is_plural = flag[flag.size() - 1] == 'S';
// Đọc dãy tham số ứng với cờ
int cnt = 0;
while (idx < tokens.size()) {
std::wstring param = tokens[idx];
// Nếu là biến,
if (param[0] == L'%' && param.size() > 1) {
// Nếu là biến, thay thế bằng giá trị của biến
std::wstring varKey = param.substr(1);
if (variables.find(varKey) != variables.end()) {
param = variables[varKey];
} else {
// Biến không tồn tại, trả về lệnh không hợp lệ
return Command(
L"WRONG_VAR",
std::unordered_map<std::wstring, std::vector<std::wstring>>{}
);
}
}
// Nếu gặp cờ mới, dừng lại
if (commandDict.flagMap.find(param) != commandDict.flagMap.end()) {
break;
}
// Cập nhật tham số vào cờ
commandMap[flag].push_back(param);
cnt++;
idx++;
// Nếu là cờ số ít, chỉ cho phép 1 tham số
if (!is_plural && cnt > 1) {
return Command(
L"INVALID",
std::unordered_map<std::wstring, std::vector<std::wstring>>{}
);
}
}
// Mọi cờ phải có ít nhất 1 tham số
if (cnt == 0) {
return Command(
L"INVALID",
std::unordered_map<std::wstring, std::vector<std::wstring>>{}
);
}
}
// Trả về lệnh đã phân tích
return Command(commandName, commandMap);
}
bool checkCommand(Command& command) {
// Kiểm tra lệnh trống
if (command.name == L"NULL") {
std::wcout << L"Lệnh trống.\n";
return false;
}
// Kiểm tra lệnh không hợp lệ
if (command.name == L"INVALID") {
std::wcout << L"Lệnh sai cú pháp!\n";
return false;
}
if (command.name == L"WRONG_VAR") {
std::wcout << L"Biến/các biến được chèn không tồn tại trong shell. "
L"Gõ lệnh viewvar hoặc getvar để kiểm tra.\n";
return false;
}
return true;
}
// ---Các hàm thực thi các lệnh
void printCommandList() {
for (const auto& [_, command] : commandDict.commandMap) {
std::wcout << L" > " << command.name << L": " << command.description << L".\n";
}
}
void printFlagList() {
for (const auto& [_,flag] : commandDict.flagMap) {
std::wcout << L" > " << flag.name << L": " << flag.description << L".\n";
}
}
void handleProcessResult(BOOL success, PROCESS_INFORMATION& pi, bool runInBackground) {
if (!success) {
std::wcout << L"Chạy file thất bại. Hãy thử lại.\n";
return;
}
if (runInBackground) {
currentProcesses[pi.dwProcessId] = pi.hProcess;
std::wcout << L"Chạy file thành công (background).\n";
CloseHandle(pi.hThread);
} else {
WaitForSingleObject(pi.hProcess, INFINITE);
std::wcout << L"Chạy file thành công.\n";
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
}
}
// ------------Các hàm lệnh của chương trình------------------------
// Hàm hiển thị thông tin chương trình
void cmd_intro() {
std::wcout << L"--------------------------------------------------------\n";
std::wcout << L"Phần mềm MoonShell - Phương tiện giao tiếp hệ thống\n";
std::wcout << L"Phiên bản 1.0 \n";
std::wcout << L"Chào mừng bạn =^.^= \n";
std::wcout << L" \n";
std::wcout << L" _..._ \n";
std::wcout << L" .::' ``. \n";
std::wcout << L" ::: :: \n";
std::wcout << L" ::: :: \n";
std::wcout << L" `::. ;' \n";
std::wcout << L" `':...-'' \n";
std::wcout << L" \n";
std::wcout << L"--------------------------------------------------------\n";
std::wcout << L"Gõ lệnh 'intro' để xem giới thiệu phần mềm. \n";
std::wcout << L"Gõ lệnh 'help' để xem danh sách các lệnh và cờ. \n";
std::wcout << L"Gõ lệnh 'example' để xem ví dụ sử dụng phần mềm. \n";
std::wcout << L"Gõ lệnh 'exit' để thoát khỏi phần mềm. \n";
std::wcout << L"--------------------------------------------------------\n";
}
void cmd_intro(Command& command) {
if (!command.map.empty()) {
std::wcout << L"Lệnh intro không có cờ tham số.\n";
return;
}
std::wcout << L"--------------------------------------------------------\n";
std::wcout << L"Phần mềm MoonShell - Phương tiện giao tiếp hệ thống\n";
std::wcout << L"Phiên bản 1.0 \n";
std::wcout << L"Chào mừng bạn =^.^= \n";
std::wcout << L" \n";
std::wcout << L" _..._ \n";
std::wcout << L" .::' ``. \n";
std::wcout << L" ::: :: \n";
std::wcout << L" ::: :: \n";
std::wcout << L" `::. ;' \n";
std::wcout << L" `':...-'' \n";
std::wcout << L" \n";
std::wcout << L"--------------------------------------------------------\n";
std::wcout << L"Gõ lệnh 'intro' để xem giới thiệu phần mềm. \n";
std::wcout << L"Gõ lệnh 'help' để xem danh sách các lệnh và cờ. \n";
std::wcout << L"Gõ lệnh 'example' để xem ví dụ sử dụng phần mềm. \n";
std::wcout << L"Gõ lệnh 'exit' để thoát khỏi phần mềm. \n";
std::wcout << L"--------------------------------------------------------\n";
}
void cmd_help(Command& command) {
if (!command.map.empty()) {
std::wcout << L"Lệnh help không có cờ tham số.\n";
return;
}
std::wcout << L"Một lệnh hợp lệ có dạng:\n";
std::wcout << L" <command> <flag_1> <params_1> <flag_2> <params_2> ... <flag_n> <params_n>\n";
std::wcout << L"Trong đó:\n";
std::wcout << L" - command: lệnh của chương trình.\n";
std::wcout << L" - flag_i: cờ của chương trình. Các cờ luôn có đầu là -;\n";
std::wcout << L" Nếu đuôi là \'S\' thì đó là cờ số nhiều, còn lại là cờ số ít.\n";
std::wcout << L" - params_i: dãy tham số ứng với cờ.\n";
std::wcout << L" + Nếu là cờ số ít, dãy chỉ được có đúng 1 tham số.\n";
std::wcout << L" + Nếu là cờ số nhiều, dãy phải có ít nhất 1 tham số.\n";
std::wcout << L" + Nếu params_i là biến (bắt đầu bởi %), tham số được thay thế bằng giá trị của biến.\n";
std::wcout << L"\n";
std::wcout << L"Danh sách các lệnh khả dụng:\n";
printCommandList();
std::wcout << L"\n";
std::wcout << L"Danh sách các cờ khả dụng:\n";
printFlagList();
std::wcout << L"\n";
}
void cmd_exit(Command& command){
if (!command.map.empty()) {
std::wcout << L"Lệnh exit không có cờ tham số.\n";
return;
}
std::wcout << L"Bạn chắc chăn muốn thoát khỏi phần mềm MoonShell (gõ Y để thoát): ";
std::wstring choice;
std::getline(std::wcin, choice);
if (choice != L"Y") {
std::wcout << L"Đã hủy thoát phần mềm.\n";
return;
}
std::wcout << L"Chào tạm biệt! =^.^=";
exit(0);
}
void cmd_example(Command& command) {
if (!command.map.empty()) {
std::wcout << L"Lệnh example không có cờ tham số.\n";
return;
}
std::wcout << L"Ví dụ sử dụng phần mềm MoonShell:\n";
std::wcout << L"1. Chuyển đến thư mục con: down -dir example_matmul\n";
std::wcout << L"2. Xây dựng file .exe từ file .cpp: build -cpp matrix_generator.cpp -exe matrix_generator.exe\n";
std::wcout << L"3. Chạy file: run -argS matrix_generator.exe 128 256 512\n";
std::wcout << L"4. Chạy file .class: runclass -argS matrix_multiplication matrix1.csv matrix2.csv matrix3.csv\n";
std::wcout << L"5. Đổi tên thư mục hoặc file: rename -targetname matrix_generator.cpp -newname matgen.cpp\n";
std::wcout << L"6. Xóa thư mục hoặc file: delete -targetname matgen.cpp\n";
}
void cmd_clrscr(Command& command) {
if (!command.map.empty()) {
std::wcout << L"Lệnh clrscr không có cờ tham số.\n";
return;
}
// Các biến
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD count, cellCount;
COORD homeCoords = { 0, 0 };
// Kiểm tra handle hợp lệ
if (hConsole == INVALID_HANDLE_VALUE) return;
// Lấy thông tin về console
if (!GetConsoleScreenBufferInfo(hConsole, &csbi)) return;
cellCount = csbi.dwSize.X * csbi.dwSize.Y;
// Xóa
if (!FillConsoleOutputCharacterW(
hConsole, L' ', cellCount, homeCoords, &count)) return;
// Đặt thuộc tính mặc định
if (!FillConsoleOutputAttribute(
hConsole, csbi.wAttributes, cellCount, homeCoords, &count)) return;
// Đặt con trỏ về vị trí đầu tiên
SetConsoleCursorPosition(hConsole, homeCoords);
}
void cmd_setfgcol(Command& command) {
// Kiểm tra cờ tham số tồn tại không
if (command.map.size() != 1 ||
command.map.find(L"-color") == command.map.end())
{
std::wcout << L"Lệnh color thiếu cờ tham số!\n";
return;
}
// Lấy màu từ cờ
std::wstring color = command.map[L"-color"][0];
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
// Lấy thuộc tính hiện tại
CONSOLE_SCREEN_BUFFER_INFO csbi;
if (!GetConsoleScreenBufferInfo(hConsole, &csbi)) {
std::wcout << L"Không thể lấy thông tin console.\n";
return;
}
WORD attr = csbi.wAttributes;
// Xóa phần foreground cũ
attr &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
// Thêm màu mới
if (color == L"black") {
// Không thêm gì, giữ nguyên attr đã xóa fg
} else if (color == L"red") {
attr |= FOREGROUND_RED | FOREGROUND_INTENSITY;
} else if (color == L"green") {
attr |= FOREGROUND_GREEN | FOREGROUND_INTENSITY;
} else if (color == L"blue") {
attr |= FOREGROUND_BLUE | FOREGROUND_INTENSITY;
} else if (color == L"yellow") {
attr |= FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY;
} else if (color == L"magenta") {
attr |= FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
} else if (color == L"cyan") {
attr |= FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
} else if (color == L"white") {
attr |= FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
} else {
std::wcout << L"Màu không hợp lệ!\n";
return;
}
if (!SetConsoleTextAttribute(hConsole, attr)) {
std::wcout << L"Không thể đổi màu chữ.\n";
} else {
std::wcout << L"Đã đổi màu chữ thành công.\n";
}
}
void cmd_setbgcol(Command& command) {
// Kiểm tra cờ tham số tồn tại không
if (command.map.size() != 1 ||
command.map.find(L"-color") == command.map.end())
{
std::wcout << L"Lệnh sai cờ!\n";
return;
}
// Lấy màu từ cờ
std::wstring color = command.map[L"-color"][0];
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
// Lấy thuộc tính hiện tại
CONSOLE_SCREEN_BUFFER_INFO csbi;
if (!GetConsoleScreenBufferInfo(hConsole, &csbi)) {
std::wcout << L"Không thể lấy thông tin console.\n";
return;
}
WORD attr = csbi.wAttributes;
// Xóa phần background cũ
attr &= ~(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY);
// Thêm màu mới
if (color == L"black") {
// Không thêm gì, giữ nguyên attr đã xóa bg
} else if (color == L"red") {
attr |= BACKGROUND_RED | BACKGROUND_INTENSITY;
} else if (color == L"green") {
attr |= BACKGROUND_GREEN | BACKGROUND_INTENSITY;
} else if (color == L"blue") {
attr |= BACKGROUND_BLUE | BACKGROUND_INTENSITY;
} else if (color == L"yellow") {
attr |= BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_INTENSITY;
} else if (color == L"magenta") {
attr |= BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_INTENSITY;
} else if (color == L"cyan") {
attr |= BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY;
} else if (color == L"white") {
attr |= BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY;
} else {
std::wcout << L"Màu không hợp lệ!\n";
return;
}
if (!SetConsoleTextAttribute(hConsole, attr)) {
std::wcout << L"Không thể đổi màu nền.\n";
} else {
std::wcout << L"Đã đổi màu nền thành công.\n";
}
}
void cmd_echo(Command& command) {
// Kiểm tra cờ tham số tồn tại không
if (command.map.size() != 1 ||
command.map.find(L"-argS") == command.map.end())
{
std::wcout << L"Lệnh echo thiếu cờ tham số!\n";
return;
}
// In ra nội dung lệnh
std::vector<std::wstring> params = command.map[L"-argS"];
for (const auto& param : params) {
std::wcout << std::wstring(param.begin(), param.end()) << L" ";
}
std::wcout << '\n';
}
void cmd_viewvar(Command& command) {
if (!command.map.empty()) {
std::wcout << L"Lệnh viewvar không có cờ tham số.\n";
return;
}
if (variables.empty()) {
std::wcout << L"Không có biến nào trong tiến trình.\n";
return;
}
std::wcout << L"Các biến trong tiến trình:\n";
for (const auto& [key, value] : variables) {
std::wcout << L" " << key << L": " << value << L"\n";
}
}
void cmd_setvar(Command& command) {
// Kiểm tra cờ tham số tồn tại không
if (command.map.size() != 2 ||
command.map.find(L"-key") == command.map.end() ||
command.map.find(L"-value") == command.map.end())
{
std::wcout << L"Lệnh setvar thiếu cờ tham số.\n";
return;
}
// Lấy tên biến và giá trị từ cờ
std::wstring varKey = command.map[L"-key"][0];
std::wstring varValue = command.map[L"-value"][0];
// Thiết lập biến trong tiến trình
variables[varKey] = varValue;
std::wcout << L"Thiết lập thành công.\n";
}
void cmd_getvar(Command& command) {
// Kiểm tra cờ tham số tồn tại không
if (command.map.size() != 1 ||
command.map.find(L"-key") == command.map.end())
{
std::wcout << L"Lệnh getvar thiếu cờ tham số.\n";
return;
}
// Lấy tên biến từ cờ
std::wstring varKey = command.map[L"-key"][0];
// Kiểm tra biến có tồn tại không
auto varpair = variables.find(varKey);
if (varpair != variables.end()) {
std::wcout << L"Giá trị của biến " << varKey << L": " << varpair->second << L"\n";
} else {
std::wcout << L"Biến " << varKey << L" không tồn tại trong tiến trình.\n";
}
}
void cmd_rmvar(Command& command) {
// Kiểm tra cờ tham số tồn tại không
if (command.map.size() != 1 ||
command.map.find(L"-key") == command.map.end())
{
std::wcout << L"Lệnh hiếu cờ -key.\n";
return;
}
// Lấy tên biến từ cờ
std::wstring varKey = command.map[L"-key"][0];
// Xóa biến trong tiến trình
auto varpair = variables.find(varKey);
if (varpair != variables.end()) {
variables.erase(varpair);
std::wcout << L"Đã xóa biến " << varKey << L" khỏi tiến trình.\n";
} else {
std::wcout << L"Biến " << varKey << L" không tồn tại trong tiến trình.\n";
}
}
void cmd_viewpath(Command& command) {
if (!command.map.empty()) {
std::wcout << L"Lệnh viewpath không có cờ tham số.\n";
return;
}
// Lấy độ dài thực sự của PATH
DWORD len = GetEnvironmentVariableW(L"PATH", nullptr, 0);
if (len == 0) {
std::wcout << L"Không thể lấy biến PATH.\n";
return;
}
std::vector<wchar_t> pathBuffer(len);
GetEnvironmentVariableW(L"PATH", pathBuffer.data(), len);
// In ra các đường dẫn trong PATH
std::wcout << L"Các đường dẫn trong PATH:\n";
std::wstring path(pathBuffer.data());
size_t start = 0, end, cnt = 1;
while ((end = path.find(L';', start)) != std::wstring::npos) {
std::wstring dir = path.substr(start, end - start);
// Bỏ qua các đường dẫn rỗng
if (!dir.empty()) {
std::wcout << L" [" << cnt << L"] " << dir << L"\n";
cnt++;
}
start = end + 1;
}
// In phần cuối nếu có
if (start < path.size()) {
std::wstring dir = path.substr(start);
if (!dir.empty()) {
std::wcout << L" [" << cnt << L"] " << dir << L"\n";
}
}
}
void cmd_addpath(Command& command) {
// Kiểm tra cờ tham số tồn tại không
if (command.map.size() != 1 ||
command.map.find(L"-path") == command.map.end())
{
std::wcout << L"Lệnh addpath thiếu cờ -path.\n";
return;
}
// Lấy đường dẫn từ cờ
std::wstring newPath = command.map[L"-path"][0];
// Lấy PATH hiện tại
DWORD len = GetEnvironmentVariableW(L"PATH", nullptr, 0);
std::vector<wchar_t> pathBuffer(len);
if (len > 0) {
GetEnvironmentVariableW(L"PATH", pathBuffer.data(), len);
}
std::wstring currentPathEnv = (len > 0) ? std::wstring(pathBuffer.data()) : L"";
// Kiểm tra nếu newPath đã tồn tại trong PATH
if (currentPathEnv.find(newPath) != std::wstring::npos) {
std::wcout << L"Đường dẫn đã tồn tại trong PATH.\n";
return;
}
// Thêm dấu ; nếu PATH hiện tại không rỗng và không kết thúc bằng ;
if (!currentPathEnv.empty() && currentPathEnv.back() != L';') {
currentPathEnv += L";";
}
// Nối đường dẫn mới vào cuối PATH
currentPathEnv += newPath;
// Cập nhật PATH
if (SetEnvironmentVariableW(L"PATH", currentPathEnv.c_str())) {
std::wcout << L"Đã thêm đường dẫn vào PATH thành công.\n";
} else {
std::wcout << L"Không thể thêm đường dẫn vào PATH.\n";
}
}
void cmd_rmpath(Command& command) {
// Kiểm tra cờ tham số tồn tại không
if (command.map.size() != 1 ||
command.map.find(L"-path") == command.map.end())
{
std::wcout << L"Thiếu cờ tham số -path.\n";
return;
}
// Lấy đường dẫn từ cờ
std::wstring path = command.map[L"-path"][0];
// Lấy độ dài thực sự của PATH
DWORD len = GetEnvironmentVariableW(L"PATH", nullptr, 0);
if (len == 0) {
std::wcout << L"Không thể lấy biến PATH.\n";
return;
}
std::vector<wchar_t> pathBuffer(len);
GetEnvironmentVariableW(L"PATH", pathBuffer.data(), len);
// Chuyển đổi PATH thành chuỗi để thao tác
std::wstring pathStr(pathBuffer.data());
// Tìm và xóa đường dẫn
size_t pos = pathStr.find(path);
if (pos != std::wstring::npos) {
pathStr.erase(pos, path.length());
if (pathStr[pos] == L';') {
pathStr.erase(pos, 1); // Xóa dấu chấm phẩy nếu có
}
// Cập nhật PATH mới
if (SetEnvironmentVariableW(L"PATH", pathStr.c_str())) {
std::wcout << L"Xóa đường dẫn khỏi PATH thành công.\n";
} else {
std::wcout << L"Không thể cập nhật PATH.\n";
}
} else {
std::wcout << L"Đường dẫn không tồn tại trong PATH.\n";
}
}
void cmd_viewfile(Command& command) {
// Mở và hiển thị nội dung file
for (const auto& [flag, params] : command.map) {
if (params.empty()) continue;
std::wstring filename = params[0];
std::wifstream file(filename.c_str());
if (!file) {
std::wcout << L"Không thể mở file: " << filename << L".\n";
continue;
}
std::wcout << L"Mở file " << filename << L" thành công. " << L"Nội dung file:\n";
std::wstring line;
// In lên console với màu mới
while (std::getline(file, line)) {
std::wcout << line << L"\n";
}
file.close();
}
}
void cmd_addfile(Command& command) {
for (const auto& [flag, params] : command.map) {
if (params.empty()) continue;
std::wstring filename = params[0];
std::wofstream file(filename.c_str());
if (!file) {
std::wcout << L"Không thể tạo file: " << filename << L".\n";
continue;
}
file.close();
std::wcout << L"Đã tạo file: " << filename << L".\n";
}
}
void cmd_writefile(Command& command) {
// Ghi đè nội dung file
for (const auto& [flag, params] : command.map) {
if (params.empty()) continue;
std::wstring filename = params[0];
std::wofstream file(filename.c_str(), std::ios::trunc);