From 9066c6796f7b80d411732ac7de95340bbafc68b7 Mon Sep 17 00:00:00 2001 From: Jonah Beckford <9566106-jonahbeckford@users.noreply.gitlab.com> Date: Fri, 8 Nov 2024 22:48:31 -0800 Subject: [PATCH] Add -t output text option. Change default to binary. --- .gitattributes | 6 ++++++ Changes.md | 5 +++++ src/cppo_main.ml | 12 +++++++++++- test/dune | 26 ++++++++++++++++++++++++++ test/text.crlf.cppo.bin | 2 ++ test/text.crlf.ref.bin | 3 +++ test/text.lf.cppo.bin | 2 ++ test/text.lf.ref.bin | 3 +++ test/text_in_text_mode.crlf.ref.bin | 3 +++ test/text_in_text_mode.lf.cppo.bin | 2 ++ test/text_in_text_mode.lf.ref.bin | 3 +++ 11 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 .gitattributes create mode 100644 test/text.crlf.cppo.bin create mode 100644 test/text.crlf.ref.bin create mode 100644 test/text.lf.cppo.bin create mode 100644 test/text.lf.ref.bin create mode 100644 test/text_in_text_mode.crlf.ref.bin create mode 100644 test/text_in_text_mode.lf.cppo.bin create mode 100644 test/text_in_text_mode.lf.ref.bin diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..55d7234 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,6 @@ +*.crlf.cppo.bin text eol=crlf +*.crlf.ref.bin text eol=crlf +*.lf.cppo.bin text eol=lf +*.lf.ref.bin text eol=lf + +*.sh text eol=lf diff --git a/Changes.md b/Changes.md index 9632b7f..a428f08 100644 --- a/Changes.md +++ b/Changes.md @@ -1,3 +1,8 @@ +## Pending + +- [+ui] Added the `-t` output text option so that Windows does + not add CRLF endings. Default is now binary output. + ## v1.8.0 (2024-12-03) - [+ui] A scope, delimited by `#scope ... #endscope`, limits the effect of `#define`, `#def ... #enddef`, and `#undef`. diff --git a/src/cppo_main.ml b/src/cppo_main.ml index 98e9efb..b4dd6de 100644 --- a/src/cppo_main.ml +++ b/src/cppo_main.ml @@ -78,6 +78,7 @@ let main () = let preserve_quotations = ref false in let show_exact_locations = ref false in let show_no_locations = ref false in + let output_text = ref false in let options = [ "-D", Arg.String (fun s -> header := ("#define " ^ s ^ "\n") :: !header), "DEF @@ -133,6 +134,11 @@ let main () = Do not output any line directive other than those found in the input (overrides -s)."; + "-t", Arg.Set output_text, + " + Write output with LF and CRLF normalization on Unix and + Windows, respectively."; + "-version", Arg.Unit (fun () -> print_endline Cppo_version.cppo_version; exit 0), @@ -212,10 +218,14 @@ Options:" Sys.argv.(0) in in match !out_file with None -> + set_binary_mode_out stdout (not !output_text); print_string (Buffer.contents buf); flush stdout | Some file -> - let oc = open_out file in + let oc = + if !output_text then open_out file + else open_out_bin file + in output_string oc (Buffer.contents buf); close_out oc diff --git a/test/dune b/test/dune index b25eb46..fa9f75f 100644 --- a/test/dune +++ b/test/dune @@ -111,6 +111,21 @@ (deps (:< def.cppo)) (action (with-stdout-to %{targets} (run %{bin:cppo} %{<})))) +(rule + (target text.crlf.out.bin) + (deps (:< text.crlf.cppo.bin)) + (action (with-stdout-to %{target} (run %{bin:cppo} %{<})))) + +(rule + (target text.lf.out.bin) + (deps (:< text.lf.cppo.bin)) + (action (with-stdout-to %{target} (run %{bin:cppo} %{<})))) + +(rule + (target text_in_text_mode.lf.out.bin) + (deps (:< text_in_text_mode.lf.cppo.bin)) + (action (with-stdout-to %{target} (run %{bin:cppo} -t %{<})))) + (rule (alias runtest) (package cppo) (action (diff ext.ref ext.out))) @@ -153,6 +168,17 @@ (rule (alias runtest) (package cppo) (action (diff def.ref def.out))) +(rule (alias runtest) (package cppo) + (action (diff text.crlf.ref.bin text.crlf.out.bin))) + +(rule (alias runtest) (package cppo) + (action (diff text.lf.ref.bin text.lf.out.bin))) + +(rule (alias runtest) (package cppo) (enabled_if (= %{os_type} Win32)) + (action (diff text_in_text_mode.crlf.ref.bin text_in_text_mode.lf.out.bin))) +(rule (alias runtest) (package cppo) (enabled_if (<> %{os_type} Win32)) + (action (diff text_in_text_mode.lf.ref.bin text_in_text_mode.lf.out.bin))) + ;; --------------------------------------------------------------------------- ;; Negative tests. diff --git a/test/text.crlf.cppo.bin b/test/text.crlf.cppo.bin new file mode 100644 index 0000000..b2c2a9d --- /dev/null +++ b/test/text.crlf.cppo.bin @@ -0,0 +1,2 @@ +1: CRLF +2: CRLF diff --git a/test/text.crlf.ref.bin b/test/text.crlf.ref.bin new file mode 100644 index 0000000..22cd5bb --- /dev/null +++ b/test/text.crlf.ref.bin @@ -0,0 +1,3 @@ +# 1 "text.crlf.cppo.bin" +1: CRLF +2: CRLF diff --git a/test/text.lf.cppo.bin b/test/text.lf.cppo.bin new file mode 100644 index 0000000..9f39ee8 --- /dev/null +++ b/test/text.lf.cppo.bin @@ -0,0 +1,2 @@ +1: LF +2: LF diff --git a/test/text.lf.ref.bin b/test/text.lf.ref.bin new file mode 100644 index 0000000..d78470f --- /dev/null +++ b/test/text.lf.ref.bin @@ -0,0 +1,3 @@ +# 1 "text.lf.cppo.bin" +1: LF +2: LF diff --git a/test/text_in_text_mode.crlf.ref.bin b/test/text_in_text_mode.crlf.ref.bin new file mode 100644 index 0000000..3da8ee6 --- /dev/null +++ b/test/text_in_text_mode.crlf.ref.bin @@ -0,0 +1,3 @@ +# 1 "text_in_text_mode.lf.cppo.bin" +1: LF +2: LF diff --git a/test/text_in_text_mode.lf.cppo.bin b/test/text_in_text_mode.lf.cppo.bin new file mode 100644 index 0000000..9f39ee8 --- /dev/null +++ b/test/text_in_text_mode.lf.cppo.bin @@ -0,0 +1,2 @@ +1: LF +2: LF diff --git a/test/text_in_text_mode.lf.ref.bin b/test/text_in_text_mode.lf.ref.bin new file mode 100644 index 0000000..3da8ee6 --- /dev/null +++ b/test/text_in_text_mode.lf.ref.bin @@ -0,0 +1,3 @@ +# 1 "text_in_text_mode.lf.cppo.bin" +1: LF +2: LF