Help updating process.Child from 0.15 to 0.16

I can’t seem to convert it to 0.16

I’m completely lost

I’ve passed about 90% of my scripts, but now I’m stuck

///-----------------------
/// callpgm
///-----------------------
const std = @import("std");

var threaded: std.Io.Threaded = .init_single_threaded;
const io = threaded.io();

var TTY: std.Io.File = undefined;
fn Perror(err: ErrCallpgm) void {
    var out = std.Io.File.stdout().writerStreaming(io, &.{});
    out.interface.print("\r\nCallpgm err: {}\n", .{err}) catch unreachable;
    var stdin = std.Io.File.stdin();
    var buf: [16]u8 = [_]u8{0} ** 16;
    var c: usize = 0;
    while (c == 0) {
        c = stdin.readPositionalAll(io, &buf, 0) catch unreachable;
    }
}

/// Errors that may occur when using String
pub const ErrCallpgm = error{
    Module_Interface_Invalid,
    Module_error_internal_Occurs,
    Module_Error,
};
var allocChild = std.heap.page_allocator;

var CallModule: std.process.Child = undefined;

pub fn getProcess() *std.process.Child {
    return &CallModule;
}

pub fn callPgmPid(pgm: []const u8, module: []const u8, pid: ?[]const u8, wait: bool) !void {
    if (!std.mem.eql(u8, pgm, "SH") and !std.mem.eql(u8, pgm, "APPTERM")) {
        Perror(ErrCallpgm.Module_Interface_Invalid);
        return ErrCallpgm.Module_Error;
    }

    // Retrieval of the working library.
    var buf: [std.fs.max_path_bytes]u8 = undefined;
    const cwd = std.posix.getcwd(&buf) catch unreachable;
    //The set of modules is located in the manager's library, for example: (APPTERM).
    CallModule.cwd = cwd;

    var arg2: [2][]const u8 = undefined;
    var arg3: [3][]const u8 = undefined;
    var prog: []const u8 = undefined;

    var cmd: []const u8 = undefined;

    var parm: []const u8 = undefined;

    if (pid) |value| {
        if (std.mem.eql(u8, pgm, "SH")) {
            cmd = std.fmt.allocPrint(allocChild, "./{s}  {s}", .{ module, value }) catch unreachable;
            arg3 = .{ "/bin/sh", "-c", cmd };
            CallModule = std.process.Child.init(arg3[0..], allocChild);
        }
        if (std.mem.eql(u8, pgm, "APPTERM")) {
            prog = std.fmt.allocPrint(allocChild, "./{s}", .{pgm}) catch unreachable;
            cmd = std.fmt.allocPrint(allocChild, "./{s}", .{module}) catch unreachable;
            parm = std.fmt.allocPrint(allocChild, "{s}", .{value}) catch unreachable;
            arg3 = .{ prog, cmd, parm };
            CallModule = std.process.Child.init(arg3[0..], allocChild);
        }
    } else {
        if (std.mem.eql(u8, pgm, "SH")) {
            cmd = std.fmt.allocPrint(allocChild, "./{s}", .{module}) catch unreachable;
            arg3 = .{ "/bin/sh", "-c", cmd };
            CallModule = std.process.Child.init(arg3[0..], allocChild);
        }
        if (std.mem.eql(u8, pgm, "APPTERM")) {
            prog = std.fmt.allocPrint(allocChild, "./{s}", .{pgm}) catch unreachable;
            cmd = std.fmt.allocPrint(allocChild, "./{s}", .{module}) catch unreachable;
            arg2 = .{ prog, cmd };
            CallModule = std.process.Child.init(arg2[0..], allocChild);
        }
    }

    if (wait) {
        // CallModule = std.ChildProcess.init(args[0..], allocChild);
        // Execution and suspension of the caller.
        const childTerm = std.process.Child.spawnAndWait(&CallModule) catch unreachable;
        switch (childTerm) {
            .Exited => |code| {
                if (code != 0) {
                    // permet l'affichage de l'erreur renvoyé par zmmap
                    Perror(ErrCallpgm.Module_error_internal_Occurs);
                    return ErrCallpgm.Module_Error;
                }
            },
            else => unreachable,
        }
    } else {
        std.process.Child.spawn(&CallModule) catch unreachable;
    }
}

`

and toml auto-format = true

What are you stuck on?

The code is badly formatted and uses non-standard conventions, so it’s kind of hard to read.

 CallModule = std.process.Child.init(arg3[0..], allocChild);

thank you

test

CallModule = std.process.spawn(io, ???);

What exactly are you seeing? Saying I’m stuck doesn’t help. Neither does showing the line where you’re stuck. What are you trying to do? Is zig throwing an error?

Please take some time to write your issue down rather than just dumping code without any context.

Spawn docs

Spawn options

Probably something like:

Call Module = std.process.spawn(io, .{ .argv = arg3[0..] });
1 Like

I use deepl.com to write. I don’t speak English, but I use it, and I’ve often noticed that the meaning of my sentences isn’t getting across. In fact, it sometimes translates word for w

I see that .cwd is an option. How do you set the path, please?

callModule = std.process.spawn(io, .{
    .argv = arg3[0..],
    // To inherit
    .cwd = .{ .inherit = {} },
    // To an Io.Dir
    .cwd = .{ .dir = some_dir },
    // To a path
    .cwd = .{ .path = "/home/user/.local" },
});
1 Like

Hello, I’m having some issues: error.OutOfMemory

However, I’m following the instructions in this document:

https://codeberg.org/ziglang/zig/pulls/30644

My function:

/// Fonction de remplacement pour std.process.Child.spawnAndWait
pub fn spawnAndWait(args: []const []const u8) !std.process.Child.Term {
    WriteAll(" spwan start\n");
    var vChild = std.process.spawn(io, .{
        .argv = args,
        .stdin = .pipe,
        .stdout = .pipe,
        .stderr = .pipe,
    }) catch |err| {
        std.debug.print("Processus stoppé par le signal: {any}\n", .{err});
        return ErrCallpgm.Module_Error;
    };
    WriteAll(" spwan ok");

    const childTerm = vChild.wait(io) catch unreachable;
    return childTerm;
}

/bin/sh -c ./Pecho 1778810662622

            cmd = std.fmt.allocPrint(allocChild, "./{s}  {s}", .{ module, value }) catch unreachable;
            arg3 = .{ "/bin/sh", "-c", cmd };
            Print("\r\n arg3 {s}  {s} {s} \n", .{ arg3[0], arg3[1], arg3[2] });

            // Nouveau code (0.16) avec le wrapper :
            const childTerm = spawnAndWait(&arg3) catch unreachable;

            switch (childTerm) {
                .exited => |code| {
                    if (code != 0) {
                        Perror(ErrCallpgm.Module_error_internal_Occurs);
                        return ErrCallpgm.Module_Error;
                    }
                },
                .signal => |signal| {
                    std.debug.print("Processus tué par le signal: {d}\n", .{signal});
                    return ErrCallpgm.Module_Error;
                },
                .stopped => |signal| {
                    std.debug.print("Processus stoppé par le signal: {d}\n", .{signal});
                    return ErrCallpgm.Module_Error;
                },
                // .unknown => |signal| {
                //     std.debug.print("Processus repris par le signal: {d}\n", .{signal});
                //     // Gérer si nécessaire
                // },
                else => unreachable,
            }

I can’t get past the “spawn ok”

It throws an OutOfMemory error and std.process.spawn

Can someone please help me?

Processus stoppé par le signal: error.OutOfMemory
thread 37242 panic: attempt to unwrap error: Module_Error
error return context:
/home/soleil/.zig/lib/std/mem/Allocator.zig:300:82: 0x10429a3 in allocBytesWithAlignment__anon_8785 (std.zig)
    const byte_ptr = self.rawAlloc(byte_count, alignment, return_address) orelse return error.OutOfMemory;
                                                                                 ^
/home/soleil/.zig/lib/std/mem/Allocator.zig:286:5: 0x10975ee in allocWithSizeAndAlignment__anon_11507 (std.zig)
    return self.allocBytesWithAlignment(alignment, byte_count, return_address);
    ^
/home/soleil/.zig/lib/std/mem/Allocator.zig:274:55: 0x1166470 in allocAdvancedWithRetAddr (std.zig)
    const ptr: [*]align(a.toByteUnits()) T = @ptrCast(try self.allocWithSizeAndAlignment(@sizeOf(T), a, n, return_address));
                                                      ^
/home/soleil/.zig/lib/std/mem/Allocator.zig:222:21: 0x116656a in allocWithOptionsRetAddr__anon_25119 (std.zig)
        const ptr = try self.allocAdvancedWithRetAddr(Elem, optional_alignment, n + 1, return_address);
                    ^
/home/soleil/.zig/lib/std/mem/Allocator.zig:252:5: 0x116626c in allocSentinel__anon_24324 (std.zig)
    return self.allocWithOptionsRetAddr(Elem, n, null, sentinel, @returnAddress());
    ^
/home/soleil/.zig/lib/std/Io/Threaded.zig:14926:22: 0x115398b in spawnPosix (std.zig)
    const argv_buf = try arena.allocSentinel(?[*:0]const u8, options.argv.len, null);
                     ^
/home/soleil/.zig/lib/std/Io/Threaded.zig:15098:21: 0x1151cb6 in processSpawnPosix (std.zig)
    const spawned = try spawnPosix(t, options);
                    ^
/home/soleil/.zig/lib/std/process.zig:443:5: 0x11f81fd in spawn (std.zig)
    return io.vtable.processSpawn(io.userdata, options);
    ^
/home/soleil/Zmmap/libtui/calling/callpgm.zig:59:9: 0x11d568e in spawnAndWait (callpgm.zig)
        return ErrCallpgm.Module_Error;
        ^

stack trace:
/home/soleil/Zmmap/libtui/calling/callpgm.zig:100:57: 0x11d61db in callPgmPid (callpgm.zig)
            const childTerm = spawnAndWait(&arg3) catch unreachable;

my OS linux arch

I can’t find a solution, so I’m reverting to version 0.15.2