package ecaml

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file process.ml

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
open! Core_kernel
open! Async_kernel
open! Import

module Q = struct
  include Q

  let call_process = "call-process" |> Symbol.intern
  and call_process_region = "call-process-region" |> Symbol.intern
  and closed = "closed" |> Symbol.intern
  and connect = "connect" |> Symbol.intern
  and exit_ = "exit" |> Symbol.intern
  and failed = "failed" |> Symbol.intern
  and listen = "listen" |> Symbol.intern
  and local = "local" |> Symbol.intern
  and make_network_process = "make-network-process" |> Symbol.intern
  and open_ = "open" |> Symbol.intern
  and run = "run" |> Symbol.intern
  and signal = "signal" |> Symbol.intern
  and start_process = "start-process" |> Symbol.intern
  and stop = "stop" |> Symbol.intern
end

include Process0

module Status = struct
  module T = struct
    type t =
      | Closed
      | Connect
      | Exit
      | Failed
      | Listen
      | Open
      | Run
      | Signal
      | Stop
    [@@deriving enumerate, sexp_of]
  end

  include T

  let type_ =
    Value.Type.enum
      [%sexp "process-status"]
      (module T)
      (Symbol.to_value
       << function
         | Closed -> Q.closed
         | Connect -> Q.connect
         | Exit -> Q.exit_
         | Failed -> Q.failed
         | Listen -> Q.listen
         | Open -> Q.open_
         | Run -> Q.run
         | Signal -> Q.signal
         | Stop -> Q.stop)
  ;;

  let t = type_
  let of_value_exn = Value.Type.of_value_exn type_
  let to_value = Value.Type.to_value type_
end

let is_alive = Funcall.("process-live-p" <: t @-> return bool)
let equal = eq
let buffer = Funcall.("process-buffer" <: t @-> return (nil_or Buffer.t))
let process_command = Funcall.("process-command" <: t @-> return value)

let command t =
  let v = process_command t in
  if Value.is_nil v || Value.eq v Value.t
  then None
  else Some (v |> Value.to_list_exn ~f:Value.to_utf8_bytes_exn)
;;

let name = Funcall.("process-name" <: t @-> return string)
let process_id = Funcall.("process-id" <: t @-> return (nil_or int))
let pid t = process_id t |> Option.map ~f:Pid.of_int
let mark = Funcall.("process-mark" <: t @-> return Marker.t)
let query_on_exit = Funcall.("process-query-on-exit-flag" <: t @-> return bool)

let set_query_on_exit =
  Funcall.("set-process-query-on-exit-flag" <: t @-> bool @-> return nil)
;;

let get_property = Funcall.("process-get" <: t @-> Symbol.t @-> return (nil_or value))
let set_property = Funcall.("process-put" <: t @-> Symbol.t @-> value @-> return nil)
let status = Funcall.("process-status" <: t @-> return Status.t)

module Exit_status = struct
  type t =
    | Not_exited
    | Exited of int
    | Fatal_signal of int
  [@@deriving sexp]
end

let process_exit_status = Funcall.("process-exit-status" <: t @-> return int)

let exit_status t : Exit_status.t =
  match status t with
  | Exit -> Exited (process_exit_status t)
  | Signal -> Fatal_signal (process_exit_status t)
  | Closed | Connect | Failed | Listen | Open | Run | Stop -> Not_exited
;;

let find_by_name = Funcall.("get-process" <: string @-> return (nil_or t))
let all_emacs_children = Funcall.("process-list" <: nullary @-> return (list t))

let create prog args ~name ?buffer () =
  Symbol.funcallN
    Q.start_process
    ([ name |> Value.of_utf8_bytes
     ; (match buffer with
        | None -> Value.nil
        | Some b -> b |> Buffer.to_value)
     ; prog |> Value.of_utf8_bytes
     ]
     @ (args |> List.map ~f:Value.of_utf8_bytes))
  |> of_value_exn
;;

let kill = Funcall.("delete-process" <: t @-> return nil)

let create_unix_network_process () ~filter ~name ~socket_path =
  of_value_exn
    (Symbol.funcallN
       Q.make_network_process
       [ Q.K.name |> Symbol.to_value
       ; name |> Value.of_utf8_bytes
       ; Q.K.family |> Symbol.to_value
       ; Q.local |> Symbol.to_value
       ; Q.K.server |> Symbol.to_value
       ; Q.t |> Symbol.to_value
       ; Q.K.service |> Symbol.to_value
       ; socket_path |> Value.of_utf8_bytes
       ; Q.K.filter |> Symbol.to_value
       ; Function.to_value
           (Defun.lambda
              [%here]
              ~docstring:"Network process filter."
              (Returns Value.Type.unit)
              (let%map_open.Defun () = return ()
               and process = required "process" t
               and output = required "output" Text.t in
               filter process output))
       ])
;;

module Call = struct
  module Input = struct
    type t =
      | Dev_null
      | File of string
    [@@deriving sexp_of]

    let to_value = function
      | Dev_null -> Value.nil
      | File file -> file |> Value.of_utf8_bytes
    ;;
  end

  module Region_input = struct
    type t =
      | Region of
          { start : Position.t
          ; end_ : Position.t
          ; delete : bool
          }
      | String of string
    [@@deriving sexp_of]
  end

  module Output = struct
    module Stdout = struct
      type t =
        | Before_point_in of Buffer.t
        | Before_point_in_current_buffer
        | Dev_null
        | Overwrite_file of string
      [@@deriving sexp_of]

      let to_value = function
        | Before_point_in buffer -> buffer |> Buffer.to_value
        | Before_point_in_current_buffer -> Value.t
        | Dev_null -> Value.nil
        | Overwrite_file string ->
          Value.list [ Q.K.file |> Symbol.to_value; string |> Value.of_utf8_bytes ]
      ;;
    end

    module Stderr = struct
      type t =
        | Dev_null
        | Overwrite_file of string
      [@@deriving sexp_of]

      let to_value = function
        | Dev_null -> Value.nil
        | Overwrite_file string -> string |> Value.of_utf8_bytes
      ;;
    end

    type t =
      | Before_point_in of Buffer.t
      | Before_point_in_current_buffer
      | Dev_null
      | Overwrite_file of string
      | Split of
          { stderr : Stderr.t
          ; stdout : Stdout.t
          }
    [@@deriving sexp_of]

    let to_value = function
      | Before_point_in buffer -> buffer |> Buffer.to_value
      | Before_point_in_current_buffer -> Value.t
      | Dev_null -> Value.nil
      | Overwrite_file string ->
        Value.list [ Q.K.file |> Symbol.to_value; string |> Value.of_utf8_bytes ]
      | Split { stderr; stdout } ->
        Value.list [ stdout |> Stdout.to_value; stderr |> Stderr.to_value ]
    ;;
  end

  module Result = struct
    type t =
      | Exit_status of int
      | Signaled of string
    [@@deriving sexp_of]

    let of_value_exn value =
      if Value.is_integer value
      then Exit_status (value |> Value.to_int_exn)
      else if Value.is_string value
      then Signaled (value |> Value.to_utf8_bytes_exn)
      else
        raise_s
          [%message
            "[Process.Call.Result.of_value_exn] got unexpected value" (value : Value.t)]
    ;;
  end
end

let call_region_exn
      ?(input =
        Call.Region_input.Region
          { start = Point.min (); end_ = Point.max (); delete = false })
      ?(output = Call.Output.Dev_null)
      ?(redisplay_on_output = false)
      ?(working_directory = Working_directory.Root)
      prog
      args
  =
  Working_directory.within working_directory ~f:(fun () ->
    let start, end_, delete =
      match input with
      | Region { start; end_; delete } ->
        start |> Position.to_value, end_ |> Position.to_value, delete
      | String s -> s |> Value.of_utf8_bytes, Value.nil, false
    in
    Symbol.funcallN
      Q.call_process_region
      ([ start
       ; end_
       ; prog |> Value.of_utf8_bytes
       ; delete |> Value.of_bool
       ; output |> Call.Output.to_value
       ; redisplay_on_output |> Value.of_bool
       ]
       @ (args |> List.map ~f:Value.of_utf8_bytes))
    |> Call.Result.of_value_exn)
;;

let call_result_exn
      ?(input = Call.Input.Dev_null)
      ?(output = Call.Output.Dev_null)
      ?(redisplay_on_output = false)
      ?(working_directory = Working_directory.Root)
      prog
      args
  =
  Working_directory.within working_directory ~f:(fun () ->
    Symbol.funcallN
      Q.call_process
      ([ prog |> Value.of_utf8_bytes
       ; input |> Call.Input.to_value
       ; output |> Call.Output.to_value
       ; redisplay_on_output |> Value.of_bool
       ]
       @ (args |> List.map ~f:Value.of_utf8_bytes))
    |> Call.Result.of_value_exn)
;;

module Lines_or_sexp = struct
  include Async_unix.Process.Lines_or_sexp

  let of_text text = text |> Text.to_utf8_bytes |> String.strip |> create
end

let call_exn
      ?input
      ?working_directory
      ?(strip_whitespace = true)
      ?(verbose_exn = true)
      prog
      args
  =
  Current_buffer.set_temporarily_to_temp_buffer Sync (fun () ->
    match
      call_result_exn
        prog
        args
        ?input
        ?working_directory
        ~output:Before_point_in_current_buffer
    with
    | Exit_status 0 ->
      let buffer_contents = Current_buffer.contents () |> Text.to_utf8_bytes in
      if strip_whitespace then String.strip buffer_contents else buffer_contents
    | result ->
      let output = Current_buffer.contents () |> Lines_or_sexp.of_text in
      (match verbose_exn with
       | true ->
         raise_s
           [%message
             "[Process.call_exn] failed"
               (prog : string)
               (args : string list)
               (result : Call.Result.t)
               (output : Lines_or_sexp.t)]
       | false -> raise_s [%sexp (output : Lines_or_sexp.t)]))
;;

let call_expect_no_output_exn
      ?input
      ?working_directory
      ?(strip_whitespace = false)
      ?verbose_exn
      prog
      args
  =
  let result =
    call_exn ?input ?working_directory ?verbose_exn ~strip_whitespace prog args
  in
  if String.is_empty result
  then ()
  else
    raise_s
      [%message
        "[Process.call_expect_no_output_exn] produced unexpected output"
          (prog : string)
          (args : string list)
          (result : string)
          ~output:(Current_buffer.contents () |> Lines_or_sexp.of_text : Lines_or_sexp.t)]
;;

let bash = "/bin/bash"

let shell_command_result ?input ?output ?redisplay_on_output ?working_directory command =
  call_result_exn
    bash
    [ "-c"; command ]
    ?input
    ?output
    ?redisplay_on_output
    ?working_directory
;;

let shell_command_exn ?input ?working_directory ?verbose_exn command =
  call_exn bash [ "-c"; command ] ?input ?working_directory ?verbose_exn
;;

let shell_command_expect_no_output_exn ?input ?working_directory ?verbose_exn command =
  call_expect_no_output_exn bash [ "-c"; command ] ?input ?working_directory ?verbose_exn
;;

let process_sentinel = Funcall.("process-sentinel" <: t @-> return (nil_or Function.t))

let set_process_sentinel =
  Funcall.("set-process-sentinel" <: t @-> Function.t @-> return nil)
;;

let extend_sentinel
      (type a)
      here
      t
      (returns : (unit, a) Defun.Returns.t)
      ~(sentinel : event:string -> a)
  =
  let previous_sentinel = process_sentinel t in
  set_process_sentinel
    t
    (Defun.lambda
       here
       returns
       (let%map_open.Defun () = return ()
        and process = required "process" value
        and event = required "event" value in
        let run_previous_sentinel () =
          match previous_sentinel with
          | None -> ()
          | Some previous_sentinel ->
            Value.funcall2_i (previous_sentinel |> Function.to_value) process event
        in
        match returns with
        | Returns _ ->
          run_previous_sentinel ();
          Background.Private.mark_running_in_background [%here] ~f:(fun () ->
            (sentinel ~event:(event |> Value.to_utf8_bytes_exn) : a))
        | Returns_deferred _ ->
          let%bind.Deferred () =
            Value.Private.run_outside_async
              [%here]
              ~allowed_in_background:true
              run_previous_sentinel
          in
          Background.Private.mark_running_in_background [%here] ~f:(fun () ->
            sentinel ~event:(event |> Value.to_utf8_bytes_exn))))
;;

module Exited = struct
  type t =
    | Exited of int
    | Fatal_signal of int
  [@@deriving sexp_of]

  let successfully = function
    | Exited 0 -> true
    | Exited _ | Fatal_signal _ -> false
  ;;
end

let exited =
  let property = "exited" |> Symbol.intern in
  let type_ =
    Caml_embed.create_type
      (Type_equal.Id.create ~name:"exited" [%sexp_of: Exited.t Deferred.t])
  in
  fun t ->
    let check_status () : Exited.t option =
      match exit_status t with
      | Not_exited -> None
      | Exited i -> Some (Exited i)
      | Fatal_signal i -> Some (Fatal_signal i)
    in
    match get_property t property with
    | Some v -> v |> Value.Type.of_value_exn type_
    | None ->
      (match check_status () with
       | Some x -> return x
       | None ->
         let ivar : Exited.t Ivar.t = Ivar.create () in
         extend_sentinel [%here] t (Returns Value.Type.unit) ~sentinel:(fun ~event:_ ->
           Option.iter (check_status ()) ~f:(Ivar.fill_if_empty ivar));
         let exited = Ivar.read ivar in
         set_property t property (exited |> Value.Type.to_value type_);
         exited)
;;
OCaml

Innovation. Community. Security.