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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
open Base
open Stdio
module Sys = Caml.Sys
module Unix = struct
include Unix
let rec try_until_no_eintr f =
try f () with Unix.Unix_error (Unix.EINTR, _, _) -> try_until_no_eintr f
let with_restart_on_eintr ?(restart = false) f =
if restart then try_until_no_eintr f else f ()
let dup = dup ~cloexec:true
let pipe = pipe ~cloexec:true
let read ?restart fd buf pos len =
with_restart_on_eintr ?restart (fun () -> read fd buf pos len)
let write ?restart fd buf pos len =
with_restart_on_eintr ?restart (fun () -> write fd buf pos len)
let waitpid ?(restart = true) wait_flags pid =
with_restart_on_eintr ~restart (fun () -> waitpid wait_flags pid)
end
module Thread = struct
include Thread
let run f =
let (_ : t) = create f () in
()
end
module Mutex = struct
include Mutex
let with_lock m f =
Mutex.lock m;
Exn.protect ~f ~finally:(fun () -> Mutex.unlock m)
end
module Background_process = struct
type 'a t = {
mutable result : 'a option;
condition : Condition.t;
mutex : Mutex.t;
}
let create () =
{ condition = Condition.create (); mutex = Mutex.create (); result = None }
type packed = T : _ t -> packed
let pack t = T t
end
type 'a background_process = 'a Background_process.t
module State : sig
val pid : int
val new_background_process : unit -> 'a Background_process.t
val all_background_processes : unit -> Background_process.packed list
end = struct
let pid = Unix.getpid ()
let background_process_mutex = Mutex.create ()
let background_processes : Background_process.packed list ref = ref []
let new_background_process () =
Mutex.with_lock background_process_mutex (fun () ->
let process = Background_process.create () in
background_processes :=
Background_process.pack process :: !background_processes;
process)
let all_background_processes () =
Mutex.with_lock background_process_mutex (fun () -> !background_processes)
end
let debug = ref false
type env = (string * string) list
type context = {
stdin_reader : Unix.file_descr;
stdout_writer : Unix.file_descr;
stderr_writer : Unix.file_descr;
cwd : string option;
env : env option;
}
type cmd =
| Process of string * string list
| Of_list of string list
| Pipe of cmd * cmd
| And of cmd * cmd
| Or of cmd * cmd
| Sequence of cmd * cmd
| Write_out_to of string * cmd
| Append_out_to of string * cmd
| Write_err_to of string * cmd
| Append_err_to of string * cmd
| Read_in_from of string * cmd
| Out_to_err of cmd
| Err_to_out of cmd
| Filter_mapi of (string -> int -> string option)
[@@deriving sexp]
type everything = { stdout : string; stderr : string; status : int }
type _ what_to_collect =
| Collect_status : int what_to_collect
| Collect_stdout : string what_to_collect
| Collect_stderr : string what_to_collect
| Collect_stdout_stderr : (string * string) what_to_collect
| Collect_stdout_status : (string * int) what_to_collect
| Collect_stderr_status : (string * int) what_to_collect
| Collect_everything : everything what_to_collect
let resolve_in_path prog =
if String.split ~on:'/' prog |> List.length <> 1 then Some prog
else
let paths = Sys.getenv "PATH" |> String.split ~on:':' in
List.map paths ~f:(fun d -> Caml.Filename.concat d prog)
|> List.find ~f:Caml.Sys.file_exists
let resolve_in_path_exn prog =
match resolve_in_path prog with
| None -> failwith (Printf.sprintf "no program in path %s" prog)
| Some prog -> prog
let fd_iter_lines ~f fd =
let buf = Bytes.create 1 in
let line = ref [] in
try
while true do
match Unix.read fd buf 0 1 with
| 0 -> raise End_of_file
| 1 -> (
match Bytes.get buf 0 with
| '\n' ->
f (String.of_char_list (List.rev !line));
line := []
| c -> line := c :: !line)
| _ -> assert false
done
with End_of_file ->
if List.length !line <> 0 then f (String.of_char_list (List.rev !line))
let filter_mapi ~f ctx =
let count = ref 0 in
fd_iter_lines ctx.stdin_reader ~f:(fun line ->
(match f line !count with
| Some out ->
let buf = Bytes.of_string out in
let (_ : int) =
Unix.write ctx.stdout_writer buf 0 (Bytes.length buf)
in
let (_ : int) =
Unix.write ctx.stdout_writer (Bytes.of_string "\n") 0 1
in
()
| None -> ());
Int.incr count);
Unix.close ctx.stdout_writer
let exec prog args ctx =
let argv = prog :: args in
let prog = resolve_in_path_exn prog in
let cwd : Spawn.Working_dir.t =
match ctx.cwd with
| None -> Inherit
| Some cwd ->
prerr_endline cwd;
Path cwd
in
let env : Spawn.Env.t option =
Option.map ctx.env ~f:(fun env ->
List.map env ~f:(fun (key, value) -> Printf.sprintf "%s=%s" key value)
|> Spawn.Env.of_list)
in
(if !debug then
let tm = Unix.localtime (Unix.time ()) in
eprintf "%d-%d-%d %d:%d:%d - %s %s\n" tm.tm_year tm.tm_mon tm.tm_mday
tm.tm_hour tm.tm_min tm.tm_sec prog
("(" ^ String.concat ~sep:" " args ^ ")"));
let pid =
Spawn.spawn ~cwd ?env ~stdin:ctx.stdin_reader ~stdout:ctx.stdout_writer
~stderr:ctx.stderr_writer ~prog ~argv ()
in
let status =
match snd (Unix.waitpid [] pid) with
| WEXITED s -> s
| WSIGNALED s | WSTOPPED s -> 128 + s
in
Unix.close ctx.stdin_reader;
Unix.close ctx.stdout_writer;
Unix.close ctx.stderr_writer;
status
let success_status = 0
let exec_list list ctx =
let stdout_channel = Unix.out_channel_of_descr ctx.stdout_writer in
Out_channel.output_lines stdout_channel list;
Unix.close ctx.stdin_reader;
Out_channel.close stdout_channel;
Unix.close ctx.stderr_writer;
success_status
let rec eval cmd ctx =
match cmd with
| Process (name, args) -> exec name args ctx
| Of_list list -> exec_list list ctx
| Pipe (a, b) ->
let pipe_reader, pipe_writer = Spawn.safe_pipe () in
Thread.run (fun () ->
let _status =
eval a
{
ctx with
stdout_writer = pipe_writer;
stderr_writer = Unix.dup ctx.stderr_writer;
}
in
());
eval b { ctx with stdin_reader = pipe_reader }
| And (a, b) ->
let a_status =
eval a
{
ctx with
stdout_writer = Unix.dup ctx.stdout_writer;
stderr_writer = Unix.dup ctx.stderr_writer;
stdin_reader = Unix.dup ctx.stdin_reader;
}
in
if a_status = success_status then eval b ctx
else (
Unix.close ctx.stdout_writer;
Unix.close ctx.stderr_writer;
Unix.close ctx.stdin_reader;
a_status)
| Or (a, b) ->
let a_status =
eval a
{
ctx with
stdout_writer = Unix.dup ctx.stdout_writer;
stderr_writer = Unix.dup ctx.stderr_writer;
stdin_reader = Unix.dup ctx.stdin_reader;
}
in
if a_status = success_status then (
Unix.close ctx.stdout_writer;
Unix.close ctx.stderr_writer;
Unix.close ctx.stdin_reader;
a_status)
else eval b ctx
| Sequence (a, b) ->
let (_status : int) =
eval a
{
ctx with
stdout_writer = Unix.dup ctx.stdout_writer;
stderr_writer = Unix.dup ctx.stderr_writer;
stdin_reader = Unix.dup ctx.stdin_reader;
}
in
eval b ctx
| Write_out_to (str, cmd) ->
Unix.close ctx.stdout_writer;
let stdout_writer =
Unix.openfile str [ O_WRONLY; O_TRUNC; O_CREAT ] 0o644
in
eval cmd { ctx with stdout_writer }
| Append_out_to (str, cmd) ->
Unix.close ctx.stdout_writer;
let stdout_writer =
Unix.openfile str [ O_WRONLY; O_APPEND; O_CREAT ] 0o644
in
eval cmd { ctx with stdout_writer }
| Write_err_to (str, cmd) ->
let stderr_writer =
Unix.openfile str [ O_WRONLY; O_TRUNC; O_CREAT ] 0o644
in
eval cmd { ctx with stderr_writer }
| Append_err_to (str, cmd) ->
let stderr_writer =
Unix.openfile str [ O_WRONLY; O_APPEND; O_CREAT ] 0o644
in
eval cmd { ctx with stderr_writer }
| Read_in_from (str, cmd) ->
let stdin_reader = Unix.openfile str [ O_RDONLY ] 0 in
eval cmd { ctx with stdin_reader }
| Out_to_err cmd ->
Unix.close ctx.stdout_writer;
let stdout_writer = Unix.dup ctx.stderr_writer in
eval cmd { ctx with stdout_writer }
| Err_to_out cmd ->
Unix.close ctx.stderr_writer;
let stderr_writer = Unix.dup ctx.stdout_writer in
eval cmd { ctx with stderr_writer }
| Filter_mapi f ->
filter_mapi ~f ctx;
0
let process name args = Process (name, args)
let ( |. ) a b = Pipe (a, b)
let and_ a b = And (a, b)
let or_ a b = Or (a, b)
let sequence a b = Sequence (a, b)
let write_stdout_to str cmd = Write_out_to (str, cmd)
let append_stdout_to str cmd = Append_out_to (str, cmd)
let write_stderr_to str cmd = Write_err_to (str, cmd)
let append_stderr_to str cmd = Append_err_to (str, cmd)
let read_stdin_from str cmd = Read_in_from (str, cmd)
module Infix = struct
let ( &&. ) = and_
let ( ||. ) = or_
let ( ->. ) = sequence
let ( > ) cmd str = write_stdout_to str cmd
let ( >> ) cmd str = append_stdout_to str cmd
let ( >! ) cmd str = write_stderr_to str cmd
let ( >>! ) cmd str = append_stderr_to str cmd
let ( < ) cmd str = read_stdin_from str cmd
end
let stdout_to_stderr cmd = Out_to_err cmd
let stderr_to_stdout cmd = Err_to_out cmd
let status = Collect_status
let stdout = Collect_stdout
let stderr = Collect_stderr
let stdout_and_stderr = Collect_stdout_stderr
let stdout_and_status = Collect_stdout_status
let stderr_and_status = Collect_stderr_status
let everything = Collect_everything
let collect_into_string fd =
let out = In_channel.input_all (Unix.in_channel_of_descr fd) in
String.chop_suffix_if_exists out ~suffix:"\n"
let collect (type a) ?cwd ?env (what_to_collect : a what_to_collect) cmd : a =
let eval ?(stdout_writer = Unix.dup Unix.stdout)
?(stderr_writer = Unix.dup Unix.stderr) () =
eval cmd
{
stdin_reader = Unix.dup Unix.stdin;
stdout_writer;
stderr_writer;
cwd;
env;
}
in
match what_to_collect with
| Collect_status -> eval ()
| Collect_stdout ->
let stdout_reader, stdout_writer = Unix.pipe () in
let (_ : int) = eval ~stdout_writer () in
let stdout = collect_into_string stdout_reader in
stdout
| Collect_stderr ->
let stderr_reader, stderr_writer = Unix.pipe () in
let (_ : int) = eval ~stderr_writer () in
let stderr = collect_into_string stderr_reader in
stderr
| Collect_stdout_status ->
let stdout_reader, stdout_writer = Unix.pipe () in
let status = eval ~stdout_writer () in
let stdout = collect_into_string stdout_reader in
(stdout, status)
| Collect_stderr_status ->
let stderr_reader, stderr_writer = Unix.pipe () in
let status = eval ~stderr_writer () in
let stderr = collect_into_string stderr_reader in
(stderr, status)
| Collect_stdout_stderr ->
let stdout_reader, stdout_writer = Unix.pipe () in
let stderr_reader, stderr_writer = Unix.pipe () in
let (_ : int) = eval ~stdout_writer ~stderr_writer () in
let stdout = collect_into_string stdout_reader in
let stderr = collect_into_string stderr_reader in
(stdout, stderr)
| Collect_everything ->
let stdout_reader, stdout_writer = Unix.pipe () in
let stderr_reader, stderr_writer = Unix.pipe () in
let status = eval ~stdout_writer ~stderr_writer () in
let stdout = collect_into_string stdout_reader in
let stderr = collect_into_string stderr_reader in
{ status; stdout; stderr }
let lines = String.split_lines
let filter_mapi_lines ~f = Filter_mapi f
let filter_map_lines ~f = filter_mapi_lines ~f:(fun a _ -> f a)
let filteri_lines ~f =
filter_mapi_lines ~f:(fun a i -> if f a i then Some a else None)
let filter_lines ~f = filteri_lines ~f:(fun a _ -> f a)
let mapi_lines ~f = filter_mapi_lines ~f:(fun a i -> Some (f a i))
let map_lines ~f = mapi_lines ~f:(fun a _ -> f a)
let run' ?cwd ?env cmd =
Caml.flush_all ();
eval cmd
{
stdin_reader = Unix.dup Unix.stdin;
stdout_writer = Unix.dup Unix.stdout;
stderr_writer = Unix.dup Unix.stderr;
cwd;
env;
}
let run_in_background ?cwd ?env cmd =
let process = State.new_background_process () in
Thread.run (fun () ->
let (_status : int) = run' ?cwd ?env cmd in
Mutex.with_lock process.mutex (fun () ->
process.result <- Some ();
Condition.broadcast process.condition));
process
let collect_in_background ?cwd ?env what_to_collect cmd =
let process = State.new_background_process () in
Thread.run (fun () ->
let result = collect ?cwd ?env what_to_collect cmd in
Mutex.with_lock process.mutex (fun () ->
process.result <- Some result;
Condition.broadcast process.condition));
process
let wait (process : 'a Background_process.t) =
Mutex.with_lock process.mutex (fun () ->
match process.result with
| Some a -> a
| None ->
Condition.wait process.condition process.mutex;
Option.value_exn process.result)
let wait_all () =
State.all_background_processes ()
|> List.iter ~f:(function Background_process.T process ->
let _ = wait process in
())
let run ?cwd ?env cmd =
let (_status : int) = run' ?cwd ?env cmd in
()
let ls s = process "ls" [ s ]
let find ?(include_starting_dir = false) ?(ignore_hidden = false)
?(kind : [ `Directories | `Files ] option) ?name ?depth directory =
let args = [ directory ] in
let args =
match kind with
| Some `Files -> args @ [ "-type"; "f" ]
| Some `Directories -> args @ [ "-type"; "d" ]
| None -> args
in
let args =
args
@ ([
Option.map depth ~f:(fun depth -> [ "-maxdepth"; Int.to_string depth ]);
Option.map name ~f:(fun name -> [ "-name"; name ]);
(if ignore_hidden then Some [ "-not"; "-path"; "*/\\.*" ] else None);
(if include_starting_dir then None
else Some [ "-not"; "-path"; directory ]);
]
|> List.filter_opt |> List.concat)
in
process "find" args
let sh s = process "sh" [ "-c"; s ]
let rg ?in_ regex = process "rg" (List.filter_opt [ Some regex; in_ ])
let rg_v ?in_ regex =
process "rg" ([ "-v" ] @ List.filter_opt [ Some regex; in_ ])
let grep ?in_ regex = process "grep" (List.filter_opt [ Some regex; in_ ])
let cat file = process "cat" [ file ]
let less = process "less" []
let mkdir dir = process "mkdir" [ dir ]
let mkdir_p dir = process "mkdir" [ "-p"; dir ]
let sort = process "sort" []
let uniq = process "uniq" []
let shuf = process "shuf" []
let head ?file n =
process "head" (List.filter_opt [ Some "-n"; Some (Int.to_string n); file ])
let tail ?file n =
process "tail" (List.filter_opt [ Some "-n"; Some (Int.to_string n); file ])
let tail_f file = process "tail" [ "-f"; file ]
let echo s = process "echo" [ s ]
let cut' ?complement ?(d = ' ') fs =
process
(match resolve_in_path "gcut" with Some _ -> "gcut" | None -> "cut")
([
"-f";
List.map fs ~f:Int.to_string |> String.concat ~sep:",";
"-d";
Char.to_string d;
]
@ List.filter_opt [ Option.map complement ~f:(fun () -> "--complement") ])
let cut ?d f = cut' ?d [ f ]
let cp src dst = process "cp" [ src; dst ]
let cp_r src dst = process "cp" [ "-r"; src; dst ]
let mv src dst = process "mv" [ src; dst ]
let pwd = process "pwd" []
let sed ?(g = true) pattern replace =
process "sed" [ ("s/" ^ pattern ^ "/" ^ replace ^ if g then "/g" else "/") ]
let tr from_ to_ = process "tr" [ from_; to_ ]
let tr_d chars = process "tr" [ "-d"; chars ]
let of_list l = Of_list l
let devnull = "/dev/null"
let fzf ?cwd ?env cmd =
let stdout, status =
cmd |. process "fzf" [] |> collect ?cwd ?env stdout_and_status
in
if status = 0 then Some stdout else None
let terminate_child_processes () =
process "pgrep" [ "-P"; Int.to_string State.pid ]
|> collect stdout |> lines |> List.map ~f:Int.of_string
|> List.iter ~f:(fun pid ->
try Unix.kill Sys.sigterm pid
with Unix.Unix_error (Unix.ESRCH, _, _) ->
())
let () = Caml.at_exit terminate_child_processes
let%test_module _ =
(module struct
open Infix
let print cmd = cmd |> collect stdout |> lines |> List.iter ~f:print_endline
let%expect_test _ =
echo "hi\n" |> print;
[%expect "hi"]
let%expect_test _ =
echo {|
hi
you
there
hi
|} |. sort |. uniq |. rg "h" |. sed "^" "<> "
|> print;
[%expect {|
<> hi
<> there |}]
let%expect_test _ =
let text = {|
0 hi,2 a
1 you,3 b
|} in
echo text |. cut 1 |> print;
[%expect {|
0
1 |}];
echo text |. cut ~d:',' 2 |> print;
[%expect {|
2 a
3 b |}];
echo text |. cut' [ 1; 3 ] |> print;
[%expect {|
0 a
1 b |}];
echo text |. cut' ~complement:() [ 1; 3 ] |> print;
[%expect {|
hi,2
you,3 |}];
echo text
|. map_lines ~f:(fun line ->
String.split line ~on:' '
|> List.filter ~f:(fun x -> String.length x = 1)
|> List.rev |> String.concat ~sep:"--")
|> print;
[%expect {|
a--0
b--1 |}]
let%expect_test _ =
process "false" [] &&. echo "test" |> print;
[%expect ""]
let%expect_test _ =
process "true" [] &&. echo "test" |> print;
[%expect "test"]
let%expect_test _ =
process "false" [] ||. echo "test" |> print;
[%expect "test"]
let%expect_test _ =
process "true" [] ||. echo "test" |> print;
[%expect ""]
let%expect_test _ =
process "false" [] ||. process "true" [] &&. echo "test" |> print;
[%expect "test"]
let%expect_test _ =
process "true" [] &&. process "false" [] ||. echo "test" |> print;
[%expect "test"]
let%expect_test _ =
process "false" [] ||. process "false" [] &&. echo "test" |> print;
[%expect ""]
let%expect_test _ =
process "true" [] &&. process "false" [] ||. echo "test" |> print;
[%expect "test"]
let%expect_test _ =
echo "test" |. process "true" [] ->. cat "-" |> print;
[%expect "test"]
let%expect_test _ =
echo "test" |. process "false" [] ->. cat "-" |> print;
[%expect "test"]
let%expect_test "find" =
find "." ~ignore_hidden:true ~kind:`Files ~name:"*.ml"
|. rg_v {|\.pp\.|} |. sort |> print;
[%expect {|
./example.ml
./feather.ml |}]
let%expect_test "waitpid should retry on EINTR" =
process "kill"
(List.map ~f:Int.to_string [ Caml.Sys.sigurg; Unix.getpid () ])
|> print;
[%expect ""]
let%expect_test "redirection/collection" =
let print cmd =
let stdout, stderr = cmd |> collect stdout_and_stderr in
printf "== Stdout ==\n%s\n== Stderr ==\n%s\n" stdout stderr
in
echo "test" |> print;
[%expect {|
== Stdout ==
test
== Stderr == |}];
echo "test1" |> stdout_to_stderr &&. echo "test2" |> print;
[%expect
{|
== Stdout ==
test2
== Stderr ==
test1 |}];
echo "test1" |> stdout_to_stderr &&. echo "test2" |> stderr_to_stdout
|> print;
[%expect
{|
== Stdout ==
test1
test2
== Stderr ==
|}]
let%expect_test "of_list" =
of_list [ "one"; "two"; "three" ] |. sort |> print;
[%expect {|
one
three
two |}]
end)