diff options
Diffstat (limited to 'src/syntax.zig')
-rw-r--r-- | src/syntax.zig | 72 |
1 files changed, 66 insertions, 6 deletions
diff --git a/src/syntax.zig b/src/syntax.zig index 6d5ae2e..dde6dba 100644 --- a/src/syntax.zig +++ b/src/syntax.zig @@ -190,11 +190,43 @@ pub const Command = union(enum) { .Redirect => |*redirect| { redirect.command.deinit(allocator); allocator.destroy(redirect.command); + for (redirect.redirects.items) |redir| { + switch (redir.target) { + .Path => |path| allocator.free(path), + else => {}, + } + } redirect.redirects.deinit(); }, .External => |*external| { external.args.deinit(); }, + .Builtin => |builtin| { + switch (builtin) { + .Dir => |dir| allocator.free(dir.path), + .Chdir => |chdir| allocator.free(chdir.path), + .EchoText => |echo| allocator.free(echo.message), + .Type => |type_cmd| { + switch (type_cmd.file) { + .Path => |path| allocator.free(path), + else => {}, + } + }, + .Copy => |copy| { + switch (copy.from) { + .Path => |path| allocator.free(path), + else => {}, + } + switch (copy.to) { + .Path => |path| allocator.free(path), + else => {}, + } + }, + .Remove => |remove| allocator.free(remove.path), + .Mkdir => |mkdir| allocator.free(mkdir.path), + else => {}, + } + }, else => {}, } } @@ -417,7 +449,7 @@ const Parser = struct { self.advance(); // consume redirect token const target_str = try self.expectWord(); - const target = parseFilespec(target_str); + const target = try parseFilespec(self.allocator, target_str); try redirects.append(Redirect{ .redirect_type = redirect_type, @@ -488,7 +520,10 @@ const Parser = struct { } else if (std.mem.eql(u8, cmd_upper, "VERIFY")) { return Command{ .Builtin = BuiltinCommand.Verify }; } else if (std.mem.eql(u8, cmd_upper, "DIR")) { - const path = if (args.items.len == 0) "." else args.items[0]; + const path = if (args.items.len == 0) + try self.allocator.dupe(u8, ".") + else + try self.allocator.dupe(u8, args.items[0]); return Command{ .Builtin = BuiltinCommand{ .Dir = .{ .path = path } } }; } else if (std.mem.eql(u8, cmd_upper, "VER")) { return Command{ .Builtin = BuiltinCommand.Ver }; @@ -500,10 +535,35 @@ const Parser = struct { if (args.items.len == 0) { return error.ExpectedWord; // Will be caught and show "Bad command or file name" } - const file_spec = parseFilespec(args.items[0]); + const file_spec = try parseFilespec(self.allocator, args.items[0]); return Command{ .Builtin = BuiltinCommand{ .Type = .{ .file = file_spec } } }; } else if (std.mem.eql(u8, cmd_upper, "SORT")) { return Command{ .Builtin = BuiltinCommand.Sort }; + } else if (std.mem.eql(u8, cmd_upper, "CD") or std.mem.eql(u8, cmd_upper, "CHDIR")) { + const path = if (args.items.len == 0) + try self.allocator.dupe(u8, "") + else + try self.allocator.dupe(u8, args.items[0]); + return Command{ .Builtin = BuiltinCommand{ .Chdir = .{ .path = path } } }; + } else if (std.mem.eql(u8, cmd_upper, "COPY")) { + if (args.items.len < 2) { + return error.ExpectedWord; // Will show "Bad command or file name" + } + const from_spec = try parseFilespec(self.allocator, args.items[0]); + const to_spec = try parseFilespec(self.allocator, args.items[1]); + return Command{ .Builtin = BuiltinCommand{ .Copy = .{ .from = from_spec, .to = to_spec } } }; + } else if (std.mem.eql(u8, cmd_upper, "DEL") or std.mem.eql(u8, cmd_upper, "ERASE")) { + if (args.items.len == 0) { + return error.ExpectedWord; // Will show "Bad command or file name" + } + const path = try self.allocator.dupe(u8, args.items[0]); + return Command{ .Builtin = BuiltinCommand{ .Remove = .{ .path = path } } }; + } else if (std.mem.eql(u8, cmd_upper, "MD") or std.mem.eql(u8, cmd_upper, "MKDIR")) { + if (args.items.len == 0) { + return error.ExpectedWord; // Will show "Bad command or file name" + } + const path = try self.allocator.dupe(u8, args.items[0]); + return Command{ .Builtin = BuiltinCommand{ .Mkdir = .{ .path = path } } }; } else { // External command return Command{ .External = .{ .program = command_name, .args = args } }; @@ -511,9 +571,9 @@ const Parser = struct { } }; -fn parseFilespec(path_str: []const u8) FileSpec { +fn parseFilespec(allocator: Allocator, path_str: []const u8) !FileSpec { var upper_buf: [256]u8 = undefined; - if (path_str.len >= upper_buf.len) return FileSpec{ .Path = path_str }; + if (path_str.len >= upper_buf.len) return FileSpec{ .Path = try allocator.dupe(u8, path_str) }; const upper_str = std.ascii.upperString(upper_buf[0..path_str.len], path_str); if (std.mem.eql(u8, upper_str, "CON")) return FileSpec.Con; @@ -521,7 +581,7 @@ fn parseFilespec(path_str: []const u8) FileSpec { if (std.mem.eql(u8, upper_str, "LPT2")) return FileSpec.Lpt2; if (std.mem.eql(u8, upper_str, "LPT3")) return FileSpec.Lpt3; if (std.mem.eql(u8, upper_str, "PRN")) return FileSpec.Prn; - return FileSpec{ .Path = path_str }; + return FileSpec{ .Path = try allocator.dupe(u8, path_str) }; } pub fn parse(input: []const u8, allocator: Allocator) !Command { |