From 3ed69feb97ab2db11405643b2c01da815eaba5aa Mon Sep 17 00:00:00 2001 From: Jonah Beckford <9566106-jonahbeckford@users.noreply.gitlab.com> Date: Fri, 27 Sep 2024 11:37:19 -0700 Subject: [PATCH] Strip carriage returns from files before processing https://github.com/Octachron/codept/issues/38 --- .gitignore | 1 + core/common.ml | 2 +- core/findlib.ml | 2 +- experimental/client/codept_client.ml | 2 +- lib/win32_compat.ml | 7 +++++++ lib/win32_compat.mli | 2 ++ tests/integrated.ml | 2 +- tests/run.ml | 2 +- 8 files changed, 15 insertions(+), 5 deletions(-) create mode 100644 lib/win32_compat.ml create mode 100644 lib/win32_compat.mli diff --git a/.gitignore b/.gitignore index f63e5d8a..c94899cf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ *~ **/_build/** **/.merlin +**/_opam/** tests/generated/star/m*.mli *.cm[ioax] *.cmxa diff --git a/core/common.ml b/core/common.ml index cac72340..11ed8538 100644 --- a/core/common.ml +++ b/core/common.ml @@ -26,7 +26,7 @@ type task = let compiler_dir = lazy ( let ch = Unix.open_process_in "ocamlc -where" in - let s= input_line ch in + let s= Win32_compat.dos2unix (input_line ch) in close_in ch; s ^ "/" ) diff --git a/core/findlib.ml b/core/findlib.ml index 6f98e59c..602a5050 100644 --- a/core/findlib.ml +++ b/core/findlib.ml @@ -18,7 +18,7 @@ let run cmd = let cin = Unix.open_process_in cmd in let rec read l = try - match input_line cin with + match Win32_compat.dos2unix (input_line cin) with | "" -> read l | s -> read (s::l) with diff --git a/experimental/client/codept_client.ml b/experimental/client/codept_client.ml index e9fe1e9b..f2831280 100644 --- a/experimental/client/codept_client.ml +++ b/experimental/client/codept_client.ml @@ -66,7 +66,7 @@ let client socket = output_value ch query; flush ch; let rec read () = - match input_line inchan with + match Win32_compat.dos2unix (input_line inchan) with | exception End_of_file -> () | s -> print_endline s; read () in read () diff --git a/lib/win32_compat.ml b/lib/win32_compat.ml new file mode 100644 index 00000000..4b6c7233 --- /dev/null +++ b/lib/win32_compat.ml @@ -0,0 +1,7 @@ +let dos2unix s = + let len = String.length s in + let b = Buffer.create len in + for i = 0 to len - 1 do + match s.[i] with '\r' -> () | c -> Buffer.add_char b c + done; + Buffer.contents b diff --git a/lib/win32_compat.mli b/lib/win32_compat.mli new file mode 100644 index 00000000..3f4f7054 --- /dev/null +++ b/lib/win32_compat.mli @@ -0,0 +1,2 @@ +val dos2unix : string -> string +(** [dos2unix s] removes carriage returns (ASCII 0x0D) from [s]. *) diff --git a/tests/integrated.ml b/tests/integrated.ml index 050421e5..cfb02a51 100644 --- a/tests/integrated.ml +++ b/tests/integrated.ml @@ -237,7 +237,7 @@ let extract_attribute x = let f = open_in x in let rec loop l = try - let nl = input_line f in + let nl = Win32_compat.dos2unix (input_line f) in try let att = Scanf.sscanf nl "[%@%@%@%s@]" (fun x -> x) in loop (att :: l) diff --git a/tests/run.ml b/tests/run.ml index b68f06f8..01089646 100644 --- a/tests/run.ml +++ b/tests/run.ml @@ -236,7 +236,7 @@ module Branch(Param:Stage.param) = struct let cmd = "ocamlc -where " in let cin = Unix.open_process_in cmd in try - [Filename.concat (input_line cin) "compiler-libs" ] + [Filename.concat (Win32_compat.dos2unix (input_line cin)) "compiler-libs" ] with End_of_file -> []