summaryrefslogtreecommitdiff
path: root/src/builtins.zig
diff options
context:
space:
mode:
authorLucas Faria Mendes <lucas.oliveira1676@etec.sp.gov.br>2025-12-05 08:36:01 +0000
committerLucas Faria Mendes <lucas.oliveira1676@etec.sp.gov.br>2025-12-05 08:36:01 +0000
commit379cdbd946dfb035e61f96653db618cba4906864 (patch)
tree1245beeb246dafc67c6c407d52f1277f1b7042d2 /src/builtins.zig
parent440715dc1d137de062b5787666689fbd586a5881 (diff)
downloadshell-zig-379cdbd946dfb035e61f96653db618cba4906864.tar.gz
shell-zig-379cdbd946dfb035e61f96653db618cba4906864.zip
codecrafters submit [skip ci]
Diffstat (limited to 'src/builtins.zig')
-rw-r--r--src/builtins.zig34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/builtins.zig b/src/builtins.zig
index 18e1840..a0d844b 100644
--- a/src/builtins.zig
+++ b/src/builtins.zig
@@ -122,6 +122,15 @@ pub fn executeHistory(stdout: anytype, history_list: []const []const u8, args: ?
if (args) |a| {
const trimmed = std.mem.trim(u8, a, " ");
if (trimmed.len > 0) {
+ // Check if it's a -r flag for reading from file
+ if (std.mem.startsWith(u8, trimmed, "-r ")) {
+ const file_path = std.mem.trim(u8, trimmed[3..], " ");
+ if (file_path.len == 0) {
+ try stdout.print("history: -r requires a file path\n", .{});
+ }
+ return;
+ }
+
limit = std.fmt.parseInt(usize, trimmed, 10) catch {
try stdout.print("history: invalid argument\n", .{});
return;
@@ -134,3 +143,28 @@ pub fn executeHistory(stdout: anytype, history_list: []const []const u8, args: ?
try stdout.print(" {d} {s}\n", .{ idx, cmd });
}
}
+
+pub fn executeHistoryRead(allocator: std.mem.Allocator, stdout: anytype, file_path: []const u8, history_list: *std.ArrayList([]const u8)) !void {
+ const file = std.fs.cwd().openFile(file_path, .{}) catch {
+ try stdout.print("history: cannot open {s}: error\n", .{file_path});
+ return;
+ };
+ defer file.close();
+
+ const file_size = try file.getEndPos();
+ const buffer = try allocator.alloc(u8, file_size);
+ defer allocator.free(buffer);
+
+ const bytes_read = try file.readAll(buffer);
+ if (bytes_read == 0) return;
+
+ // Parse lines from file
+ var line_iter = std.mem.splitScalar(u8, buffer[0..bytes_read], '\n');
+ while (line_iter.next()) |line| {
+ const trimmed = std.mem.trim(u8, line, " \r");
+ if (trimmed.len > 0) {
+ const line_copy = try allocator.dupe(u8, trimmed);
+ try history_list.append(allocator, line_copy);
+ }
+ }
+}