package b0

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Module Os.ExitSource

Program exit.

There are two ways for a program to exit: either by returning an exit code or by calling B0_std.Os.Cmd.execv. The type here allows to represent these two ways of exiting.

Program exits

Sourcetype code = int

The type for exit codes.

Sourcetype execv

The type for execv calls.

Sourcetype t =
  1. | Code : code -> t
    (*

    exit with code.

    *)
  2. | Execv : execv -> t
    (*

    exit with execv

    *)

The type for specifying program exits.

Sourceval get_code : t -> code

get_code e is the exit code of e. Raises Invalid_argument if e is Execv.

Sourceval exit : ?on_error:t -> t -> 'a

exit ~on_error e exits according to e:

  • If e is Code c, Stdlib.exit c is called and the function never returns.
  • If e is Execv execv, execv is called. This can only return with an Error _. In that case the error is logged and exit is called again with on_error (and the default on_error)

on_error defaults to some_error. Except if an asynchronous exception is raised this function never returns.

Exiting with codes

Note. The constants here match those established by Cmdliner with another one useful in cli tools. But we don't want a Cmdliner dependency on it here.

Sourceval code : code -> t

code c is Code c.

Sourceval ok : t

ok is Code 0.

Sourceval no_such_name : t

no_such_name is Code 122, it indicates a named entity was not found.

Sourceval some_error : t

some_error is Code 123, it indicates an indiscriminate error reported on stdout.

Sourceval cli_error : t

cli_error is Code 124, it indicates a command line parsing error.

Sourceval internal_error : t

internal_error is Code 125, it indicates an unexpected internal error (bug).

Exit with results

Sourceval of_result : (unit, string) result -> t

of_result v exits with ok if v is Ok () and logs the Error and exits with some_error if v is Error _.

Sourceval of_result' : (t, string) result -> t

of_result v exits with e if v is Ok e and logs the error and exits with some_error if v is Error _.

Exit by execv

Sourceval execv : ?env:Env.assignments -> ?cwd:Fpath.t -> ?argv0:string -> Cmd.t -> t

exec ?env ?cwd ?argv0 cmd is an Exec _. That has a call to Os.Cmd.execv with the corresponding arguments.

Sourceval execv_env : execv -> Env.assignments option

execv_env exec is the environment of exec.

Sourceval execv_cwd : execv -> Fpath.t option

execv_env exec is the environment of exec.

Sourceval execv_argv0 : execv -> string option

execv_env exec is the environment of exec.

Sourceval execv_cmd : execv -> Cmd.t

execv_env exec is the command of exec.

Signal exit hooks

Sourceval on_sigint : hook:(unit -> unit) -> (unit -> 'a) -> 'a

on_sigint ~hook f calls f () and returns its value. If SIGINT is signalled during that time hook is called followed by exit 130 – that is the exit code a SIGINT would produce.

on_sigint replaces an existing signal handler for Sys.sigint during time of the function call. It is restored when the function returns.

Note. Since Stdlib.exit is called Stdlib.at_exit functions are called if a SIGINT occurs during the call to f. This is not the case on an unhandled SIGINT.