commit ad8f6bc9c823b6cb34963e51538441bf0d3f8bdc Author: zapashcanon Date: Sat Sep 14 02:24:23 2019 +0200 first commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..26a197e --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +_build/ +*.install +*.merlin +*.opam diff --git a/CHANGES.md b/CHANGES.md new file mode 100644 index 0000000..f61efcf --- /dev/null +++ b/CHANGES.md @@ -0,0 +1,3 @@ +## 0.1.0 - 2019-09-14 + +First release diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..020701c --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,8 @@ +The ISC License (ISC) +===================== + +Copyright © 2019, Léo Andrès + +Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..9f64564 --- /dev/null +++ b/README.md @@ -0,0 +1,31 @@ +# Exit + +Exit is an OCaml library to get exit status as declared in `stdlib.h`. It gives access to the values of `EXIT_SUCCESS` and `EXIT_FAILURE` macros and provides some functions built around them. + +## Install +```sh +opam install exit +``` +## Usage + +See [examples]. + +## Build +```sh +dune build @all +``` +## Test +```sh +dune runtest +``` +## Changes + +See [CHANGES]. + +## License + +See [LICENSE]. + +[CHANGES]: ./CHANGES.md +[examples]: ./examples/ +[LICENSE]: ./LICENSE.md diff --git a/dune-project b/dune-project new file mode 100644 index 0000000..eb84c38 --- /dev/null +++ b/dune-project @@ -0,0 +1,19 @@ +(lang dune 1.11) +(name exit) +(license ISC) +(authors "Léo Andrès ") +(maintainers "Léo Andrès ") +(source (uri git://git.zapashcanon.fr/zapashcanon/exit.git)) +(bug_reports https://git.zapashcanon.fr/zapashcanon/exit/issues) +(homepage https://git.zapashcanon.fr/zapashcanon/exit/issues) + +(generate_opam_files true) +(explicit_js_mode) + +(package + (name exit) + (synopsis "An OCaml library to get exit status as declared in `stdlib.h`") + (description "Exit is an OCaml library to get exit status as declared in `stdlib.h`. + It gives access to the values of `EXIT_SUCCESS` and `EXIT_FAILURE` macros and provides some functions built around them.") + (depends + (dune (> 1.11.0)))) diff --git a/examples/dune b/examples/dune new file mode 100644 index 0000000..003d7a2 --- /dev/null +++ b/examples/dune @@ -0,0 +1,3 @@ +(executable + (name print_code) + (libraries exit)) diff --git a/examples/print_code.ml b/examples/print_code.ml new file mode 100644 index 0000000..caaf221 --- /dev/null +++ b/examples/print_code.ml @@ -0,0 +1,11 @@ +let _ = + + Format.printf "succes code is %d@." Exit.success_code; + Format.printf "failure code is %d@." Exit.failure_code; + + if Exit.success_code <> 0 || Exit.failure_code <> 1 then begin + Format.printf "not POSIX !@."; + Exit.failure () + end; + + Exit.success () diff --git a/src/dune b/src/dune new file mode 100644 index 0000000..89b8c4e --- /dev/null +++ b/src/dune @@ -0,0 +1,4 @@ +(library + (public_name exit) + (wrapped false) + (c_names exit_stubs)) diff --git a/src/exit.ml b/src/exit.ml new file mode 100644 index 0000000..f522bf6 --- /dev/null +++ b/src/exit.ml @@ -0,0 +1,14 @@ +external success_code : unit -> int = "get_success_code" +external failure_code : unit -> int = "get_failure_code" + +let success_code = success_code () +let failure_code = failure_code () + +let success () = + exit success_code + +let failure () = + exit failure_code + +let is_posix = + success_code = 0 && failure_code = 1 diff --git a/src/exit_stubs.c b/src/exit_stubs.c new file mode 100644 index 0000000..80c9084 --- /dev/null +++ b/src/exit_stubs.c @@ -0,0 +1,14 @@ +#include + +#include +#include + +CAMLprim value get_success_code(value unit) { + CAMLparam1(unit); + return Val_int(EXIT_SUCCESS); +} + +CAMLprim value get_failure_code(value unit) { + CAMLparam1(unit); + return Val_int(EXIT_FAILURE); +} diff --git a/test/dune b/test/dune new file mode 100644 index 0000000..5046d24 --- /dev/null +++ b/test/dune @@ -0,0 +1,3 @@ +(test + (name test) + (libraries exit)) diff --git a/test/test.ml b/test/test.ml new file mode 100644 index 0000000..17c4d9e --- /dev/null +++ b/test/test.ml @@ -0,0 +1,5 @@ +assert (Exit.success_code >= 0); +assert (Exit.failure_code >= 0); +assert (Exit.success_code <= 255); +assert (Exit.failure_code <= 255); +assert (Exit.success_code <> Exit.failure_code);