-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path537shell.cpp
More file actions
1066 lines (909 loc) · 35.7 KB
/
537shell.cpp
File metadata and controls
1066 lines (909 loc) · 35.7 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
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <filesystem>
#include <fstream>
#include <ctime>
#include <algorithm>
#include <map>
#include <set>
#include <regex>
#include <locale>
#include <codecvt>
#include <iomanip>
#ifdef _WIN32
#include <direct.h>
#include <io.h>
#else
#include <unistd.h>
#include <sys/stat.h>
#endif
using namespace std;
namespace fs = std::filesystem;
// 全局变量
string current_path;
vector<string> command_history;
map<string, string> environment_vars;
bool chinese_mode = false;
// 辅助函数声明
vector<string> split_command(const string& cmd);
void print_prompt();
void execute_command(const vector<string>& args);
void init_locale();
string to_utf8(const string& str);
//函数声明宏
#define COMM(name) void name(const vector<string>& args)
COMM(cmd_help); COMM(cmd_ls);
COMM(cmd_cd); COMM(cmd_pwd);
COMM(cmd_mkdir); COMM(cmd_rmdir);
COMM(cmd_rm); COMM(cmd_cp);
COMM(cmd_mv); COMM(cmd_cat);
COMM(cmd_echo); COMM(cmd_find);
COMM(cmd_grep); COMM(cmd_ps);
COMM(cmd_date); COMM(cmd_clear);
COMM(cmd_history); COMM(cmd_touch);
COMM(cmd_tree); COMM(cmd_wc);
COMM(cmd_sort); COMM(cmd_uniq);
COMM(cmd_head); COMM(cmd_tail);
COMM(cmd_cut); COMM(cmd_sed);
COMM(cmd_tr); COMM(cmd_which);
COMM(cmd_env); COMM(cmd_set);
COMM(cmd_export); COMM(cmd_diff);
COMM(cmd_du); COMM(cmd_df);
COMM(cmd_lang);
// 工具函数实现
void init_locale() {
try {
locale::global(locale(""));
cout.imbue(locale(""));
cin.imbue(locale(""));
} catch (const exception& e) {
// 如果设置locale失败,继续使用默认
}
}
string get_current_time() {
time_t now = time(0);
char buffer[100];
struct tm* timeinfo = localtime(&now);
if (chinese_mode) {
strftime(buffer, sizeof(buffer), "%Y年%m月%d日 %H:%M:%S", timeinfo);
} else {
strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", timeinfo);
}
return string(buffer);
}
void print_tree(const string& path, const string& prefix = "", bool is_last = true) {
try {
fs::path dir_path(path);
if (!fs::exists(dir_path) || !fs::is_directory(dir_path)) {
return;
}
vector<fs::directory_entry> entries;
for (const auto& entry : fs::directory_iterator(dir_path)) {
entries.push_back(entry);
}
sort(entries.begin(), entries.end(), [](const fs::directory_entry& a, const fs::directory_entry& b) {
return a.path().filename().string() < b.path().filename().string();
});
for (size_t i = 0; i < entries.size(); ++i) {
bool last = (i == entries.size() - 1);
cout << prefix << (last ? "└── " : "├── ") << entries[i].path().filename().string() << endl;
if (entries[i].is_directory()) {
string new_prefix = prefix + (last ? " " : "│ ");
print_tree(entries[i].path().string(), new_prefix, last);
}
}
} catch (const exception& e) {
cout << (chinese_mode ? "错误: " : "Error: ") << e.what() << endl;
}
}
vector<string> split_command(const string& cmd) {
vector<string> tokens;
istringstream iss(cmd);
string token;
while (iss >> token) {
tokens.push_back(token);
}
return tokens;
}
void print_prompt() {
string prompt_symbol = "$ ";
cout << "537Shell:" << fs::current_path().filename().string() << prompt_symbol;
}
// 命令函数指针类型
using CommandFunction = void (*)(const vector<string>&);
// 命令映射表
unordered_map<string, CommandFunction> command_map;
// 批量添加命令别名
void add_command_aliases(CommandFunction func, const vector<string>& aliases) {
for (const string& alias : aliases) {
command_map[alias] = func;
}
}
// 初始化命令映射表
void init_command_map() {
add_command_aliases(cmd_help, {"help", "?", "帮助"});
add_command_aliases(cmd_ls, {"ls", "dir", "列表"});
add_command_aliases(cmd_cd, {"cd", "进入"});
add_command_aliases(cmd_pwd, {"pwd", "路径"});
add_command_aliases(cmd_mkdir, {"mkdir", "创建目录"});
add_command_aliases(cmd_rmdir, {"rmdir", "删除目录"});
add_command_aliases(cmd_rm, {"rm", "del", "删除"});
add_command_aliases(cmd_cp, {"cp", "copy", "复制"});
add_command_aliases(cmd_mv, {"mv", "move", "移动"});
add_command_aliases(cmd_cat, {"cat", "type", "查看"});
add_command_aliases(cmd_echo, {"echo", "输出"});
add_command_aliases(cmd_find, {"find", "查找"});
add_command_aliases(cmd_grep, {"grep", "搜索"});
add_command_aliases(cmd_ps, {"ps", "进程"});
add_command_aliases(cmd_date, {"date", "时间"});
add_command_aliases(cmd_clear, {"clear", "cls", "清屏"});
add_command_aliases(cmd_history, {"history", "历史"});
add_command_aliases(cmd_touch, {"touch", "创建"});
add_command_aliases(cmd_tree, {"tree", "树形"});
add_command_aliases(cmd_wc, {"wc", "统计"});
add_command_aliases(cmd_sort, {"sort", "排序"});
add_command_aliases(cmd_uniq, {"uniq", "去重"});
add_command_aliases(cmd_head, {"head", "头部"});
add_command_aliases(cmd_tail, {"tail", "尾部"});
add_command_aliases(cmd_cut, {"cut", "切割"});
add_command_aliases(cmd_sed, {"sed", "替换"});
add_command_aliases(cmd_tr, {"tr", "转换"});
add_command_aliases(cmd_which, {"which", "位置"});
add_command_aliases(cmd_env, {"env", "环境"});
add_command_aliases(cmd_set, {"set", "设置"});
add_command_aliases(cmd_export, {"export", "导出"});
add_command_aliases(cmd_diff, {"diff", "比较"});
add_command_aliases(cmd_du, {"du", "磁盘使用"});
add_command_aliases(cmd_df, {"df", "磁盘空间"});
add_command_aliases(cmd_lang, {"lang", "语言"});
}
// 命令实现
void cmd_help([[maybe_unused]] const vector<string>& args) {
if (chinese_mode) {
cout << "537Shell - 可用命令:" << endl;
cout << " 帮助, help - 显示此帮助信息" << endl;
cout << " 列表, ls - 列出目录内容" << endl;
cout << " 进入, cd <路径> - 切换目录" << endl;
cout << " 路径, pwd - 显示当前目录" << endl;
cout << " 创建目录, mkdir - 创建目录" << endl;
cout << " 删除目录, rmdir - 删除目录" << endl;
cout << " 删除, rm - 删除文件" << endl;
cout << " 复制, cp - 复制文件" << endl;
cout << " 移动, mv - 移动/重命名文件" << endl;
cout << " 查看, cat - 显示文件内容" << endl;
cout << " 输出, echo - 显示文本" << endl;
cout << " 查找, find - 查找文件/目录" << endl;
cout << " 搜索, grep - 在文件中搜索文本" << endl;
cout << " 进程, ps - 列出运行进程" << endl;
cout << " 时间, date - 显示当前日期/时间" << endl;
cout << " 清屏, clear - 清除屏幕" << endl;
cout << " 历史, history - 显示命令历史" << endl;
cout << " 创建, touch - 创建空文件" << endl;
cout << " 树形, tree - 显示目录树" << endl;
cout << " 统计, wc - 统计文件行数/字数" << endl;
cout << " 排序, sort - 排序文件内容" << endl;
cout << " 去重, uniq - 去除重复行" << endl;
cout << " 头部, head - 显示文件前几行" << endl;
cout << " 尾部, tail - 显示文件后几行" << endl;
cout << " 切割, cut - 提取文件列" << endl;
cout << " 替换, sed - 文本替换" << endl;
cout << " 转换, tr - 字符转换" << endl;
cout << " 比较, diff - 比较文件差异" << endl;
cout << " 磁盘使用, du - 显示目录大小" << endl;
cout << " 磁盘空间, df - 显示磁盘空间" << endl;
cout << " 环境, env - 显示环境变量" << endl;
cout << " 语言, lang - 切换语言模式" << endl;
cout << " 退出, exit - 退出程序" << endl;
} else {
cout << "537Shell - Available Commands:" << endl;
cout << " help, ? - Show this help message" << endl;
cout << " ls, dir - List directory contents" << endl;
cout << " cd <path> - Change directory" << endl;
cout << " pwd - Print working directory" << endl;
cout << " mkdir <dir> - Create directory" << endl;
cout << " rmdir <dir> - Remove directory" << endl;
cout << " rm <file> - Remove file" << endl;
cout << " cp <src> <dst> - Copy file" << endl;
cout << " mv <src> <dst> - Move/rename file" << endl;
cout << " cat <file> - Display file contents" << endl;
cout << " echo <text> - Display text" << endl;
cout << " find <name> - Find files/directories" << endl;
cout << " grep <pattern> - Search text in files" << endl;
cout << " ps - List running processes" << endl;
cout << " date - Show current date/time" << endl;
cout << " clear, cls - Clear screen" << endl;
cout << " history - Show command history" << endl;
cout << " touch <file> - Create empty file" << endl;
cout << " tree - Show directory tree" << endl;
cout << " wc <file> - Word, line, character count" << endl;
cout << " sort <file> - Sort lines in file" << endl;
cout << " uniq <file> - Remove duplicate lines" << endl;
cout << " head <file> - Show first lines of file" << endl;
cout << " tail <file> - Show last lines of file" << endl;
cout << " cut <options> <file> - Extract columns from file" << endl;
cout << " sed <pattern> <file> - Stream editor for text" << endl;
cout << " tr <set1> <set2> - Translate characters" << endl;
cout << " diff <file1> <file2> - Compare files" << endl;
cout << " du [path] - Show directory size" << endl;
cout << " df - Show disk space" << endl;
cout << " env - Show environment variables" << endl;
cout << " lang <en|zh> - Switch language mode" << endl;
cout << " exit, quit - Exit shell" << endl;
}
}
void cmd_ls(const vector<string>& args) {
string path = ".";
bool show_all = false;
bool long_format = false;
bool show_size = false;
for (size_t i = 1; i < args.size(); ++i) {
if (args[i] == "-a") {
show_all = true;
} else if (args[i] == "-l") {
long_format = true;
} else if (args[i] == "-h") {
show_size = true;
} else if (args[i][0] != '-') {
path = args[i];
}
}
try {
vector<fs::directory_entry> entries;
for (const auto& entry : fs::directory_iterator(path)) {
entries.push_back(entry);
}
sort(entries.begin(), entries.end(), [](const fs::directory_entry& a, const fs::directory_entry& b) {
return a.path().filename().string() < b.path().filename().string();
});
for (const auto& entry : entries) {
string filename = entry.path().filename().string();
if (!show_all && filename[0] == '.') continue;
if (long_format) {
cout << (entry.is_directory() ? "d" : "-");
cout << "rwxrwxrwx ";
cout << "1 user user ";
if (entry.is_regular_file()) {
auto size = entry.file_size();
if (show_size) {
if (size >= 1024 * 1024 * 1024) {
cout << fixed << setprecision(1) << (double)size / (1024 * 1024 * 1024) << "G ";
} else if (size >= 1024 * 1024) {
cout << fixed << setprecision(1) << (double)size / (1024 * 1024) << "M ";
} else if (size >= 1024) {
cout << fixed << setprecision(1) << (double)size / 1024 << "K ";
} else {
cout << size << " ";
}
} else {
cout << size << " ";
}
} else {
cout << "0 ";
}
auto ftime = fs::last_write_time(entry);
cout << (chinese_mode ? "1月01日 12:00 " : "Jan 01 12:00 ");
}
cout << filename;
if (entry.is_directory()) cout << "/";
cout << endl;
}
} catch (const exception& e) {
cout << (chinese_mode ? "ls: " : "ls: ") << e.what() << endl;
}
}
void cmd_cd(const vector<string>& args) {
string new_path;
if (args.size() < 2) {
new_path = fs::current_path().root_path().string();
} else {
new_path = args[1];
}
try {
fs::current_path(new_path);
current_path = fs::current_path().string();
} catch (const exception& e) {
cout << (chinese_mode ? "cd: 没有此文件或目录: " : "cd: no such file or directory: ") << new_path << endl;
}
}
void cmd_pwd([[maybe_unused]] const vector<string>& args) {
cout << fs::current_path().string() << endl;
}
void cmd_mkdir(const vector<string>& args) {
if (args.size() < 2) {
cout << (chinese_mode ? "mkdir: 缺少操作数" : "mkdir: missing operand") << endl;
return;
}
for (size_t i = 1; i < args.size(); ++i) {
try {
fs::create_directories(args[i]);
cout << (chinese_mode ? "已创建目录: " : "Directory created: ") << args[i] << endl;
} catch (const exception& e) {
cout << "mkdir: " << e.what() << endl;
}
}
}
void cmd_rmdir(const vector<string>& args) {
if (args.size() < 2) {
cout << (chinese_mode ? "rmdir: 缺少操作数" : "rmdir: missing operand") << endl;
return;
}
for (size_t i = 1; i < args.size(); ++i) {
try {
fs::remove_all(args[i]);
cout << (chinese_mode ? "已删除目录: " : "Directory removed: ") << args[i] << endl;
} catch (const exception& e) {
cout << "rmdir: " << e.what() << endl;
}
}
}
void cmd_rm(const vector<string>& args) {
if (args.size() < 2) {
cout << (chinese_mode ? "rm: 缺少操作数" : "rm: missing operand") << endl;
return;
}
for (size_t i = 1; i < args.size(); ++i) {
try {
fs::remove(args[i]);
cout << (chinese_mode ? "已删除文件: " : "File removed: ") << args[i] << endl;
} catch (const exception& e) {
cout << "rm: " << e.what() << endl;
}
}
}
void cmd_cp(const vector<string>& args) {
if (args.size() < 3) {
cout << (chinese_mode ? "cp: 缺少操作数" : "cp: missing operand") << endl;
cout << (chinese_mode ? "用法: cp <源文件> <目标文件>" : "usage: cp <source> <destination>") << endl;
return;
}
try {
fs::copy_file(args[1], args[2], fs::copy_options::overwrite_existing);
cout << (chinese_mode ? "已复制文件: " : "File copied: ") << args[1] << " -> " << args[2] << endl;
} catch (const exception& e) {
cout << "cp: " << e.what() << endl;
}
}
void cmd_mv(const vector<string>& args) {
if (args.size() < 3) {
cout << (chinese_mode ? "mv: 缺少操作数" : "mv: missing operand") << endl;
cout << (chinese_mode ? "用法: mv <源文件> <目标文件>" : "usage: mv <source> <destination>") << endl;
return;
}
try {
fs::rename(args[1], args[2]);
cout << (chinese_mode ? "已移动文件: " : "File moved: ") << args[1] << " -> " << args[2] << endl;
} catch (const exception& e) {
cout << "mv: " << e.what() << endl;
}
}
void cmd_cat(const vector<string>& args) {
if (args.size() < 2) {
cout << (chinese_mode ? "cat: 缺少操作数" : "cat: missing operand") << endl;
return;
}
for (size_t i = 1; i < args.size(); ++i) {
ifstream file(args[i]);
if (file.is_open()) {
string line;
while (getline(file, line)) {
cout << line << endl;
}
file.close();
} else {
cout << (chinese_mode ? "cat: " : "cat: ") << args[i] << (chinese_mode ? ": 没有此文件或目录" : ": No such file or directory") << endl;
}
}
}
void cmd_echo(const vector<string>& args) {
for (size_t i = 1; i < args.size(); ++i) {
cout << args[i];
if (i < args.size() - 1) cout << " ";
}
cout << endl;
}
void cmd_find(const vector<string>& args) {
string search_path = ".";
string pattern = "*";
if (args.size() > 1) {
pattern = args[1];
}
try {
for (const auto& entry : fs::recursive_directory_iterator(search_path)) {
string filename = entry.path().filename().string();
if (pattern == "*" || filename.find(pattern) != string::npos) {
cout << entry.path().string() << endl;
}
}
} catch (const exception& e) {
cout << "find: " << e.what() << endl;
}
}
void cmd_grep(const vector<string>& args) {
if (args.size() < 3) {
cout << (chinese_mode ? "grep: 用法: grep <模式> <文件>" : "grep: usage: grep <pattern> <file>") << endl;
return;
}
string pattern = args[1];
string filename = args[2];
ifstream file(filename);
if (!file.is_open()) {
cout << (chinese_mode ? "grep: " : "grep: ") << filename << (chinese_mode ? ": 没有此文件或目录" : ": No such file or directory") << endl;
return;
}
string line;
int line_number = 1;
while (getline(file, line)) {
if (line.find(pattern) != string::npos) {
cout << line_number << ": " << line << endl;
}
line_number++;
}
file.close();
}
void cmd_ps([[maybe_unused]] const vector<string>& args) {
if (chinese_mode) {
cout << "PID PPID 命令" << endl;
cout << "1 0 537Shell" << endl;
} else {
cout << "PID PPID CMD" << endl;
cout << "1 0 537Shell" << endl;
}
}
void cmd_date([[maybe_unused]] const vector<string>& args) {
cout << get_current_time() << endl;
}
void cmd_clear([[maybe_unused]] const vector<string>& args) {
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
void cmd_history([[maybe_unused]] const vector<string>& args) {
for (size_t i = 0; i < command_history.size(); ++i) {
cout << i + 1 << " " << command_history[i] << endl;
}
}
void cmd_touch(const vector<string>& args) {
if (args.size() < 2) {
cout << (chinese_mode ? "touch: 缺少操作数" : "touch: missing operand") << endl;
return;
}
for (size_t i = 1; i < args.size(); ++i) {
ofstream file(args[i]);
if (file.is_open()) {
file.close();
cout << (chinese_mode ? "已创建文件: " : "File created: ") << args[i] << endl;
} else {
cout << (chinese_mode ? "touch: 无法创建 " : "touch: cannot create ") << args[i] << endl;
}
}
}
void cmd_tree(const vector<string>& args) {
string path = ".";
if (args.size() > 1) {
path = args[1];
}
cout << path << endl;
print_tree(path);
}
void cmd_wc(const vector<string>& args) {
if (args.size() < 2) {
cout << (chinese_mode ? "wc: 缺少操作数" : "wc: missing operand") << endl;
return;
}
for (size_t i = 1; i < args.size(); ++i) {
ifstream file(args[i]);
if (!file.is_open()) {
cout << (chinese_mode ? "wc: " : "wc: ") << args[i] << (chinese_mode ? ": 没有此文件或目录" : ": No such file or directory") << endl;
continue;
}
int lines = 0, words = 0, chars = 0;
string line;
while (getline(file, line)) {
lines++;
chars += line.length() + 1; // +1 for newline
istringstream iss(line);
string word;
while (iss >> word) {
words++;
}
}
cout << lines << " " << words << " " << chars << " " << args[i] << endl;
file.close();
}
}
void cmd_sort(const vector<string>& args) {
if (args.size() < 2) {
cout << (chinese_mode ? "sort: 缺少操作数" : "sort: missing operand") << endl;
return;
}
ifstream file(args[1]);
if (!file.is_open()) {
cout << (chinese_mode ? "sort: " : "sort: ") << args[1] << (chinese_mode ? ": 没有此文件或目录" : ": No such file or directory") << endl;
return;
}
vector<string> lines;
string line;
while (getline(file, line)) {
lines.push_back(line);
}
file.close();
sort(lines.begin(), lines.end());
for (const auto& sorted_line : lines) {
cout << sorted_line << endl;
}
}
void cmd_uniq(const vector<string>& args) {
if (args.size() < 2) {
cout << (chinese_mode ? "uniq: 缺少操作数" : "uniq: missing operand") << endl;
return;
}
ifstream file(args[1]);
if (!file.is_open()) {
cout << (chinese_mode ? "uniq: " : "uniq: ") << args[1] << (chinese_mode ? ": 没有此文件或目录" : ": No such file or directory") << endl;
return;
}
string line, prev_line;
bool first = true;
while (getline(file, line)) {
if (first || line != prev_line) {
cout << line << endl;
prev_line = line;
first = false;
}
}
file.close();
}
void cmd_head(const vector<string>& args) {
if (args.size() < 2) {
cout << (chinese_mode ? "head: 缺少操作数" : "head: missing operand") << endl;
return;
}
int num_lines = 10;
string filename;
if (args.size() >= 4 && args[1] == "-n") {
num_lines = stoi(args[2]);
filename = args[3];
} else {
filename = args[1];
}
ifstream file(filename);
if (!file.is_open()) {
cout << (chinese_mode ? "head: " : "head: ") << filename << (chinese_mode ? ": 没有此文件或目录" : ": No such file or directory") << endl;
return;
}
string line;
int count = 0;
while (getline(file, line) && count < num_lines) {
cout << line << endl;
count++;
}
file.close();
}
void cmd_tail(const vector<string>& args) {
if (args.size() < 2) {
cout << (chinese_mode ? "tail: 缺少操作数" : "tail: missing operand") << endl;
return;
}
int num_lines = 10;
string filename;
if (args.size() >= 4 && args[1] == "-n") {
num_lines = stoi(args[2]);
filename = args[3];
} else {
filename = args[1];
}
ifstream file(filename);
if (!file.is_open()) {
cout << (chinese_mode ? "tail: " : "tail: ") << filename << (chinese_mode ? ": 没有此文件或目录" : ": No such file or directory") << endl;
return;
}
vector<string> lines;
string line;
while (getline(file, line)) {
lines.push_back(line);
}
file.close();
int start = max(0, (int)lines.size() - num_lines);
for (int i = start; i < (int)lines.size(); ++i) {
cout << lines[i] << endl;
}
}
void cmd_cut(const vector<string>& args) {
if (args.size() < 4) {
cout << (chinese_mode ? "cut: 用法: cut -f <字段> <文件>" : "cut: usage: cut -f <field> <file>") << endl;
return;
}
if (args[1] != "-f") {
cout << (chinese_mode ? "cut: 不支持的选项" : "cut: unsupported option") << endl;
return;
}
int field = stoi(args[2]) - 1; // 转为0索引
string filename = args[3];
char delimiter = '\t';
ifstream file(filename);
if (!file.is_open()) {
cout << (chinese_mode ? "cut: " : "cut: ") << filename << (chinese_mode ? ": 没有此文件或目录" : ": No such file or directory") << endl;
return;
}
string line;
while (getline(file, line)) {
istringstream iss(line);
string token;
int current_field = 0;
while (getline(iss, token, delimiter)) {
if (current_field == field) {
cout << token << endl;
break;
}
current_field++;
}
}
file.close();
}
void cmd_sed(const vector<string>& args) {
if (args.size() < 3) {
cout << (chinese_mode ? "sed: 用法: sed 's/old/new/' <文件>" : "sed: usage: sed 's/old/new/' <file>") << endl;
return;
}
string pattern = args[1];
string filename = args[2];
// 简单的替换模式解析 s/old/new/
if (pattern.length() < 5 || pattern[0] != 's' || pattern[1] != '/') {
cout << (chinese_mode ? "sed: 无效的模式" : "sed: invalid pattern") << endl;
return;
}
size_t first_slash = pattern.find('/', 2);
size_t second_slash = pattern.find('/', first_slash + 1);
if (first_slash == string::npos || second_slash == string::npos) {
cout << (chinese_mode ? "sed: 无效的模式" : "sed: invalid pattern") << endl;
return;
}
string old_str = pattern.substr(2, first_slash - 2);
string new_str = pattern.substr(first_slash + 1, second_slash - first_slash - 1);
ifstream file(filename);
if (!file.is_open()) {
cout << (chinese_mode ? "sed: " : "sed: ") << filename << (chinese_mode ? ": 没有此文件或目录" : ": No such file or directory") << endl;
return;
}
string line;
while (getline(file, line)) {
size_t pos = line.find(old_str);
if (pos != string::npos) {
line.replace(pos, old_str.length(), new_str);
}
cout << line << endl;
}
file.close();
}
void cmd_tr(const vector<string>& args) {
if (args.size() < 3) {
cout << (chinese_mode ? "tr: 用法: tr <集合1> <集合2>" : "tr: usage: tr <set1> <set2>") << endl;
return;
}
string set1 = args[1];
string set2 = args[2];
string line;
while (getline(cin, line)) {
for (char& c : line) {
size_t pos = set1.find(c);
if (pos != string::npos && pos < set2.length()) {
c = set2[pos];
}
}
cout << line << endl;
}
}
void cmd_which(const vector<string>& args) {
if (args.size() < 2) {
cout << (chinese_mode ? "which: 缺少操作数" : "which: missing operand") << endl;
return;
}
string command = args[1];
// 简单实现:检查是否是内置命令
vector<string> builtin_commands = {
"help", "ls", "cd", "pwd", "mkdir", "rmdir", "rm", "cp", "mv",
"cat", "echo", "find", "grep", "ps", "date", "clear", "history",
"touch", "tree", "wc", "sort", "uniq", "head", "tail", "cut",
"sed", "tr", "which", "env", "set", "export", "diff", "du", "df",
"lang"
};
if (find(builtin_commands.begin(), builtin_commands.end(), command) != builtin_commands.end()) {
cout << (chinese_mode ? "内置命令: " : "builtin: ") << command << endl;
} else {
cout << (chinese_mode ? "未找到命令: " : "command not found: ") << command << endl;
}
}
void cmd_env([[maybe_unused]] const vector<string>& args) {
for (const auto& pair : environment_vars) {
cout << pair.first << "=" << pair.second << endl;
}
}
void cmd_set(const vector<string>& args) {
if (args.size() < 2) {
cmd_env({});
return;
}
string var_assignment = args[1];
size_t eq_pos = var_assignment.find('=');
if (eq_pos != string::npos) {
string var_name = var_assignment.substr(0, eq_pos);
string var_value = var_assignment.substr(eq_pos + 1);
environment_vars[var_name] = var_value;
cout << (chinese_mode ? "已设置变量: " : "Variable set: ") << var_name << "=" << var_value << endl;
} else {
cout << (chinese_mode ? "set: 无效的格式" : "set: invalid format") << endl;
}
}
void cmd_export(const vector<string>& args) {
if (args.size() < 2) {
cout << (chinese_mode ? "export: 缺少操作数" : "export: missing operand") << endl;
return;
}
string var_assignment = args[1];
size_t eq_pos = var_assignment.find('=');
if (eq_pos != string::npos) {
string var_name = var_assignment.substr(0, eq_pos);
string var_value = var_assignment.substr(eq_pos + 1);
environment_vars[var_name] = var_value;
cout << (chinese_mode ? "已导出变量: " : "Variable exported: ") << var_name << "=" << var_value << endl;
} else {
cout << (chinese_mode ? "export: 无效的格式" : "export: invalid format") << endl;
}
}
void cmd_diff(const vector<string>& args) {
if (args.size() < 3) {
cout << (chinese_mode ? "diff: 用法: diff <文件1> <文件2>" : "diff: usage: diff <file1> <file2>") << endl;
return;
}
ifstream file1(args[1]);
ifstream file2(args[2]);
if (!file1.is_open()) {
cout << (chinese_mode ? "diff: " : "diff: ") << args[1] << (chinese_mode ? ": 没有此文件或目录" : ": No such file or directory") << endl;
return;
}
if (!file2.is_open()) {
cout << (chinese_mode ? "diff: " : "diff: ") << args[2] << (chinese_mode ? ": 没有此文件或目录" : ": No such file or directory") << endl;
return;
}
vector<string> lines1, lines2;
string line;
while (getline(file1, line)) {
lines1.push_back(line);
}
while (getline(file2, line)) {
lines2.push_back(line);
}
file1.close();
file2.close();
size_t max_lines = max(lines1.size(), lines2.size());
bool found_diff = false;
for (size_t i = 0; i < max_lines; ++i) {
string line1 = (i < lines1.size()) ? lines1[i] : "";
string line2 = (i < lines2.size()) ? lines2[i] : "";
if (line1 != line2) {
if (!found_diff) {
cout << (chinese_mode ? "文件差异:" : "File differences:") << endl;
found_diff = true;
}
cout << (chinese_mode ? "行 " : "Line ") << (i + 1) << ":" << endl;
cout << "< " << line1 << endl;
cout << "> " << line2 << endl;
cout << "---" << endl;
}
}
if (!found_diff) {
cout << (chinese_mode ? "文件相同" : "Files are identical") << endl;
}
}
void cmd_du(const vector<string>& args) {
string path = ".";
if (args.size() > 1) {
path = args[1];
}
try {
uintmax_t total_size = 0;
map<string, uintmax_t> dir_sizes;
for (const auto& entry : fs::recursive_directory_iterator(path)) {
if (entry.is_regular_file()) {
uintmax_t size = entry.file_size();
total_size += size;
string dir = entry.path().parent_path().string();
dir_sizes[dir] += size;
}
}
for (const auto& pair : dir_sizes) {
double size_kb = pair.second / 1024.0;
cout << fixed << setprecision(1) << size_kb << "K\t" << pair.first << endl;
}
double total_kb = total_size / 1024.0;
cout << fixed << setprecision(1) << total_kb << "K\t" << (chinese_mode ? "总计" : "total") << endl;
} catch (const exception& e) {
cout << "du: " << e.what() << endl;
}
}
void cmd_df([[maybe_unused]] const vector<string>& args) {
try {
fs::space_info space = fs::space(".");
double total_gb = space.capacity / (1024.0 * 1024.0 * 1024.0);
double available_gb = space.available / (1024.0 * 1024.0 * 1024.0);
double used_gb = (space.capacity - space.available) / (1024.0 * 1024.0 * 1024.0);
double used_percent = (used_gb / total_gb) * 100.0;
if (chinese_mode) {
cout << "文件系统\t总计\t已用\t可用\t使用%\t挂载点" << endl;
cout << "当前磁盘\t" << fixed << setprecision(1) << total_gb << "G\t"
<< used_gb << "G\t" << available_gb << "G\t"
<< setprecision(0) << used_percent << "%\t/" << endl;
} else {
cout << "Filesystem\tSize\tUsed\tAvail\tUse%\tMounted on" << endl;
cout << "current\t" << fixed << setprecision(1) << total_gb << "G\t"
<< used_gb << "G\t" << available_gb << "G\t"
<< setprecision(0) << used_percent << "%\t/" << endl;
}
} catch (const exception& e) {
cout << "df: " << e.what() << endl;
}
}
void cmd_lang(const vector<string>& args) {
if (args.size() < 2) {
cout << (chinese_mode ? "当前语言模式: 中文" : "Current language mode: English") << endl;
cout << (chinese_mode ? "用法: lang <en|zh|english|chinese|英文|中文>" : "Usage: lang <en|zh|english|chinese>") << endl;