src/main.zig 1 K · 39 lines · raw · history

1 const std = @import("std");
2 const Io = std.Io;
3
4 pub fn main(init: std.process.Init) !void {
5 // This is appropriate for anything that lives as long as the process.
6 const arena: std.mem.Allocator = init.arena.allocator();
7
8 // Accessing command line arguments:
9 const args = try init.minimal.args.toSlice(arena);
10 const filepath = args[0];
11 const filename = std.fs.path.basename(filepath);
12
13 std.log.debug("executable name: {s}", .{filename});
14
15 const io = init.io;
16 var words = try tokenizeIntoArraylist(arena, filename);
17
18 for (args[1..]) |arg| {
19 try words.append(arena, arg);
20 }
21
22 const command = try words.toOwnedSlice(arena);
23
24 return std.process.replace(io, .{
25 .argv = command,
26 });
27 }
28
29 fn tokenizeIntoArraylist(gpa: std.mem.Allocator, buffer: []const u8) !std.ArrayList([]const u8) {
30 var it = std.mem.tokenizeScalar(u8, buffer, ' ');
31
32 var words = std.ArrayList([]const u8).empty;
33
34 while (it.next()) |word| {
35 try words.append(gpa, word);
36 }
37
38 return words;
39 }