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
|
const std = @import("std");
const types = @import("lib/types.zig");
const CommandContext = types.CommandContext;
const CommandStatus = types.CommandStatus;
const OutputCapture = types.OutputCapture;
pub const Help = struct {
command: ?[]const u8 = null,
pub fn eval(self: Help, ctx: CommandContext) !CommandStatus {
if (self.command) |cmd| {
return showCommandHelp(cmd, ctx);
} else {
return showGeneralHelp(ctx);
}
}
};
fn showGeneralHelp(ctx: CommandContext) !CommandStatus {
const help_text =
\\DOSE - DOS-style Command Shell
\\
\\Available commands:
\\
\\File Operations:
\\ COPY - Copy files
\\ DIR - Display directory contents
\\ TYPE - Display file contents
\\ CD - Change directory
\\ MD - Create directory
\\ RD - Remove directory
\\ REN - Rename files/directories
\\ DEL - Delete files
\\ MOVE - Move files/directories
\\
\\System Commands:
\\ ECHO - Display messages or control echo state
\\ CLS - Clear screen
\\ DATE - Display current date
\\ TIME - Display current time
\\ VER - Display version information
\\ PATH - Display or set PATH environment variable
\\ SORT - Sort input lines alphabetically
\\ EXIT - Exit the shell
\\
\\For help on a specific command, type: command /?
\\Example: DIR /?
\\
;
if (ctx.output_capture) |output| {
try output.write(help_text);
} else {
try std.io.getStdOut().writeAll(help_text);
}
return CommandStatus{ .Code = 0 };
}
fn showCommandHelp(command: []const u8, ctx: CommandContext) !CommandStatus {
const cmd_upper = try std.ascii.allocUpperString(ctx.allocator, command);
defer ctx.allocator.free(cmd_upper);
const help_text = getCommandSpecificHelp(cmd_upper);
if (ctx.output_capture) |output| {
try output.write(help_text);
} else {
try std.io.getStdOut().writeAll(help_text);
}
return CommandStatus{ .Code = 0 };
}
fn getCommandSpecificHelp(command: []const u8) []const u8 {
if (std.mem.eql(u8, command, "DIR")) {
return
\\DIR - Display directory contents
\\
\\Syntax: DIR [path] [/flags]
\\
\\ path Directory path to list (default: current directory)
\\
\\Flags:
\\ /? Display this help message
\\ /W Use wide list format (filenames only, multiple columns)
\\ /B Use bare format (filenames only, one per line)
\\ /S Display files in subdirectories as well
\\
\\Example: DIR C:\ /W
\\
;
} else if (std.mem.eql(u8, command, "COPY")) {
return
\\COPY - Copy files
\\
\\Syntax: COPY source destination [/flags]
\\
\\ source Source file or device
\\ destination Destination file or device
\\
\\Flags:
\\ /? Display this help message
\\ /Y Suppress prompting to confirm overwrite
\\
\\Example: COPY file1.txt file2.txt
\\
;
} else if (std.mem.eql(u8, command, "TYPE")) {
return
\\TYPE - Display file contents
\\
\\Syntax: TYPE filename [/flags]
\\
\\ filename File to display
\\
\\Flags:
\\ /? Display this help message
\\
\\Example: TYPE readme.txt
\\
;
} else if (std.mem.eql(u8, command, "CD") or std.mem.eql(u8, command, "CHDIR")) {
return
\\CD/CHDIR - Change directory
\\
\\Syntax: CD [path] [/flags]
\\
\\ path Directory to change to
\\
\\Flags:
\\ /? Display this help message
\\
\\Example: CD C:\Windows
\\
;
} else if (std.mem.eql(u8, command, "ECHO")) {
return
\\ECHO - Display messages or control echo state
\\
\\Syntax: ECHO [message] [/flags]
\\ ECHO ON|OFF
\\
\\ message Text to display
\\
\\Flags:
\\ /? Display this help message
\\
\\Example: ECHO Hello World
\\
;
} else if (std.mem.eql(u8, command, "CLS")) {
return
\\CLS - Clear screen
\\
\\Syntax: CLS [/flags]
\\
\\Flags:
\\ /? Display this help message
\\
;
} else if (std.mem.eql(u8, command, "EXIT")) {
return
\\EXIT - Exit the shell
\\
\\Syntax: EXIT [/flags]
\\
\\Flags:
\\ /? Display this help message
\\
;
} else if (std.mem.eql(u8, command, "VER")) {
return
\\VER - Display version information
\\
\\Syntax: VER [/flags]
\\
\\Flags:
\\ /? Display this help message
\\
;
} else if (std.mem.eql(u8, command, "DATE")) {
return
\\DATE - Display current date
\\
\\Syntax: DATE [/flags]
\\
\\Flags:
\\ /? Display this help message
\\
;
} else if (std.mem.eql(u8, command, "TIME")) {
return
\\TIME - Display current time
\\
\\Syntax: TIME [/flags]
\\
\\Flags:
\\ /? Display this help message
\\
;
} else {
return
\\Unknown command. Type HELP for a list of commands.
\\
;
}
}
|