summaryrefslogtreecommitdiff
path: root/testsuite/tests/lib-unix/common/test_unix_cmdline.ml
blob: aff235bd3f939f730335e0aa0acbdb26bb1b15b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
(* TEST
 readonly_files = "cmdline_prog.ml";
 hasunix;
 {
   program = "${test_build_directory}/test_unix_cmdline.byte";
   setup-ocamlc.byte-build-env;
   program = "${test_build_directory}/cmdline_prog.exe";
   all_modules = "cmdline_prog.ml";
   ocamlc.byte;
   include unix;
   program = "${test_build_directory}/test_unix_cmdline.byte";
   all_modules = "test_unix_cmdline.ml";
   ocamlc.byte;
   check-ocamlc.byte-output;
   run;
   check-program-output;
 }{
   program = "${test_build_directory}/test_unix_cmdline.opt";
   setup-ocamlopt.byte-build-env;
   program = "${test_build_directory}/cmdline_prog.exe";
   all_modules = "cmdline_prog.ml";
   ocamlc.byte;
   include unix;
   program = "${test_build_directory}/test_unix_cmdline.opt";
   all_modules = "test_unix_cmdline.ml";
   ocamlopt.byte;
   check-ocamlopt.byte-output;
   run;
   check-program-output;
 }
*)

let prog_name = "cmdline_prog.exe"

let run args =
  let out, inp = Unix.pipe () in
  let in_chan = Unix.in_channel_of_descr out in
  set_binary_mode_in in_chan false;
  let pid =
    Unix.create_process ("./" ^ prog_name) (Array.of_list (prog_name :: args))
      Unix.stdin inp Unix.stderr in
  List.iter (fun arg ->
      let s = input_line in_chan in
      Printf.printf "%S -> %S [%s]\n" arg s (if s = arg then "OK" else "FAIL")
    ) args;
  close_in in_chan;
  let _, exit = Unix.waitpid [] pid in
  assert (exit = Unix.WEXITED 0)

let exec args =
  Unix.execv ("./" ^ prog_name) (Array.of_list (prog_name :: args))

let () =
  List.iter run
    [
      [""; ""; "\t \011"];
      ["a"; "b"; "c.txt@!"];
      ["\""];
      [" "; " a "; "  \" \\\" "];
      [" \\ \\ \\\\\\"];
      [" \"hola \""];
      ["a\tb"];
    ];
  Printf.printf "-- execv\n%!";
  exec [
     "";
     "a"; "b"; "c.txt@!";
     "\"";
     " "; " a "; "  \" \\\" ";
     " \\ \\ \\\\\\";
     " \"hola \"";
     "a\tb"
  ]