diff options
| author | Lucas Faria Mendes <lucas.oliveira1676@etec.sp.gov.br> | 2025-12-05 06:20:38 +0000 |
|---|---|---|
| committer | Lucas Faria Mendes <lucas.oliveira1676@etec.sp.gov.br> | 2025-12-05 06:20:38 +0000 |
| commit | c3c3c2202cf312842230f7316c69a89c28c60ce9 (patch) | |
| tree | f696534bebecb27005fc609738b4b0cfa3fe1e0b /src | |
| parent | 9c60f224204eb2091d64131eba19afcca3bce769 (diff) | |
| download | shell-zig-c3c3c2202cf312842230f7316c69a89c28c60ce9.tar.gz shell-zig-c3c3c2202cf312842230f7316c69a89c28c60ce9.zip | |
codecrafters submit [skip ci]
Diffstat (limited to 'src')
| -rw-r--r-- | src/builtins.zig | 9 | ||||
| -rw-r--r-- | src/parser.zig | 21 |
2 files changed, 27 insertions, 3 deletions
diff --git a/src/builtins.zig b/src/builtins.zig index 409b57d..2a1eed7 100644 --- a/src/builtins.zig +++ b/src/builtins.zig @@ -40,6 +40,15 @@ pub fn executeEcho(stdout: anytype, args: ?[]const u8) !void { } else if (in_quote and a[i] == quote_char) { in_quote = false; last_was_space = false; + } else if (in_quote and quote_char == '"' and a[i] == '\\' and i + 1 < a.len) { + const next = a[i + 1]; + if (next == '"' or next == '\\') { + i += 1; + _ = unquoted.append(std.heap.page_allocator, a[i]) catch {}; + } else { + _ = unquoted.append(std.heap.page_allocator, a[i]) catch {}; + } + last_was_space = false; } else if (!in_quote and a[i] == ' ') { if (!last_was_space) { _ = unquoted.append(std.heap.page_allocator, ' ') catch {}; diff --git a/src/parser.zig b/src/parser.zig index de42f31..7289057 100644 --- a/src/parser.zig +++ b/src/parser.zig @@ -26,13 +26,28 @@ pub fn parseArgs(allocator: std.mem.Allocator, cmd_name: []const u8, args_str: ? defer arg_buf.deinit(allocator); while (i < args.len) { - if (args[i] == '\'' or args[i] == '"') { - const quote = args[i]; + if (args[i] == '\'') { i += 1; - while (i < args.len and args[i] != quote) : (i += 1) { + while (i < args.len and args[i] != '\'') : (i += 1) { try arg_buf.append(allocator, args[i]); } if (i < args.len) i += 1; + } else if (args[i] == '"') { + i += 1; + while (i < args.len and args[i] != '"') : (i += 1) { + if (args[i] == '\\' and i + 1 < args.len) { + const next = args[i + 1]; + if (next == '"' or next == '\\') { + i += 1; + try arg_buf.append(allocator, args[i]); + } else { + try arg_buf.append(allocator, args[i]); + } + } else { + try arg_buf.append(allocator, args[i]); + } + } + if (i < args.len) i += 1; } else if (args[i] == '\\' and i + 1 < args.len) { i += 1; try arg_buf.append(allocator, args[i]); |