Programming Language

Nocter

A self-contained systems language built around simplicity, encapsulation, and foolproof design.

/development/std/process/capture_launch_darwin.nct

capture_launch_darwin.nct

//! Darwin child setup for captured standard output and standard error.
//!
//! This source is the only bridge between owning capture pipes and standard descriptor
//! installation. It reports every rejected setup before raw child exit and delegates executable
//! replacement to the launch-report authority.

see ./capture_session_darwin.nct
see ./launch_report_darwin.nct
see ./pipe_darwin.nct

use /internal/os/darwin

#target: "arm64-darwin"
noalloc func reject_capture_setup(
    report: &+OwnedPipe,
    captures: &+CaptureSession,
    errno: i32,
): never {
    close_all_capture_ends(captures)
    write_launch_failure_and_exit(report, LAUNCH_STAGE_SETUP, errno)
}

#target: "arm64-darwin"
noalloc func reject_malformed_capture_setup(
    report: &+OwnedPipe,
    captures: &+CaptureSession,
): never {
    // An impossible successful dup2 result must not become clean launch EOF. An unknown stage is
    // deliberately decoded as a malformed launch report by the parent.
    close_all_capture_ends(captures)
    write_launch_failure_and_exit(report, 0, 1)
}

#target: "arm64-darwin"
noalloc func exec_captured_child(
    path_address: usize,
    argv_address: usize,
    environment_address: usize,
    report: OwnedPipe,
    captures: CaptureSession,
): never {
    var launch_report = move report
    var capture = move captures
    close_pipe_read(&+launch_report)
    close_capture_reads(&+capture)

    match install_descriptor(
        pipe_write_fd(&capture.stdout_pipe),
        darwin.STANDARD_OUTPUT_FD,
    ) {
        DescriptorInstallAttempt.ready {}
        DescriptorInstallAttempt.failed(errno) {
            reject_capture_setup(&+launch_report, &+capture, errno)
        }
        DescriptorInstallAttempt.invalid {
            reject_malformed_capture_setup(&+launch_report, &+capture)
        }
    }
    match install_descriptor(
        pipe_write_fd(&capture.stderr_pipe),
        darwin.STANDARD_ERROR_FD,
    ) {
        DescriptorInstallAttempt.ready {}
        DescriptorInstallAttempt.failed(errno) {
            reject_capture_setup(&+launch_report, &+capture, errno)
        }
        DescriptorInstallAttempt.invalid {
            reject_malformed_capture_setup(&+launch_report, &+capture)
        }
    }
    close_capture_writes(&+capture)
    exec_child_after_setup(
        path_address,
        argv_address,
        environment_address,
        &+launch_report,
    )
}