OCaml: Apply formatting

This commit is contained in:
Juno Takano 2025-04-19 19:09:16 -03:00
commit bb1cd19000
12 changed files with 145 additions and 74 deletions

View file

@ -1,13 +1,16 @@
open Utilities.Aliases
type status = Exit of int | Unevaluated
type command = { name: string; arguments: string list; status: status }
type command = { name : string; arguments : string list; status : status }
let format (command: command): string =
command.name ^
" with arguments: " ^ (String.concat " " command.arguments) ^
" and result " ^ match command.status with
let format (command : command) : string =
command.name ^ " with arguments: "
^ String.concat " " command.arguments
^ " and result "
^
match command.status with
| Exit n -> str_int n
| Unevaluated -> "Not evaluated"
let format_many (commands: command list): string list = List.map format commands
let format_many (commands : command list) : string list =
List.map format commands

View file

@ -1,9 +1,12 @@
let run (command: Command.command): Command.command =
let run (command : Command.command) : Command.command =
match Unix.fork () with
| 0 -> Unix.execvp command.name (Array.of_list command.arguments)
| pid -> let (_, status) = Unix.waitpid [] pid in
| pid -> (
let _, status = Unix.waitpid [] pid in
match status with
| WSTOPPED n | WSIGNALED n | WEXITED n -> { command with status = Exit n }
| WSTOPPED n | WSIGNALED n | WEXITED n ->
{ command with status = Exit n }
)
let run_many (commands: Command.command list): Command.command list =
let run_many (commands : Command.command list) : Command.command list =
List.map run commands

View file

@ -1,15 +1,14 @@
open Utilities.Aliases
type output = { output: string; error: string; status: string; }
type output = { output : string; error : string; status : string }
let handle_exit_status (status: Unix.process_status): string =
let handle_exit_status (status : Unix.process_status) : string =
match status with
| Unix.WEXITED n -> "Exit " ^ str_int n
| Unix.WSIGNALED n -> "Kill " ^ str_int n
| Unix.WSTOPPED n -> "Stopped " ^ str_int n
let read (env: string array) (command: string): output =
let read (env : string array) (command : string) : output =
let stdout, stdin, stderr = Unix.open_process_full command env in
let in_buffer = Buffer.create 4096 in
let err_buffer = Buffer.create 4096 in
@ -20,25 +19,29 @@ let read (env: string array) (command: string): output =
Buffer.add_char in_buffer '\n';
read_in ()
in
try read_in () with End_of_file -> ();
try read_in ()
with End_of_file -> (
();
let rec read_err () =
let err_line = input_line stderr in
Buffer.add_string err_buffer err_line;
Buffer.add_char err_buffer '\n';
read_err ()
in
try read_err () with
End_of_file -> let exit_status =
let rec read_err () =
let err_line = input_line stderr in
Buffer.add_string err_buffer err_line;
Buffer.add_char err_buffer '\n';
read_err ()
in
try read_err ()
with End_of_file ->
let exit_status =
handle_exit_status (Unix.close_process_full (stdout, stdin, stderr))
in
{
output = String.trim (Buffer.contents in_buffer);
error = Buffer.contents err_buffer;
status = exit_status;
output = String.trim (Buffer.contents in_buffer);
error = Buffer.contents err_buffer;
status = exit_status;
}
)
let format (output: output): string =
let format (output : output) : string =
match output with
| { output = o; error = _; status = "Exit 0" } -> o
| { output = ""; error = e; status = s } -> "[" ^ s ^ "]" ^ " " ^ e