diff --git a/src/main.zig b/src/main.zig index e874f27..7689b5a 100644 --- a/src/main.zig +++ b/src/main.zig @@ -11,8 +11,8 @@ pub fn main() anyerror!void { while (true) { var input = prompt.readline("topiku> ") orelse break; - defer std.c.free(&input); - + defer prompt.free(input); + try stdout.print(">>> {s}\n", .{input}); } } diff --git a/src/prompt.zig b/src/prompt.zig index e97e8a2..63385f9 100644 --- a/src/prompt.zig +++ b/src/prompt.zig @@ -5,8 +5,11 @@ const ceditline = @cImport({ const std = @import("std"); const io = std.io; -pub fn readline(prompt: [*c]const u8) ?[]u8 { - var line = ceditline.readline(@as([*c]const u8, prompt)); +const __sighandler = ?fn (c_int) callconv(.C) void; +extern fn signal(sig: c_int, handler: __sighandler) __sighandler; + +pub fn readline(prompt: []const u8) ?[]u8 { + var line = ceditline.readline(@as([*c]const u8, prompt.ptr)); if (line == 0) { std.c.free(line); return null; @@ -14,8 +17,18 @@ pub fn readline(prompt: [*c]const u8) ?[]u8 { return std.mem.sliceTo(line, 0); } +pub fn free(slice: []u8) void { + std.c.free(slice.ptr); +} + pub fn init() void { ceditline.rl_initialize(); + _ = signal(std.os.SIG.INT, exit); +} + +fn exit(_ : c_int) callconv(.C) void { + deinit(); + std.os.exit(0); } pub fn deinit() void {