Shells in OCaml
3
fork

Configure Feed

Select the types of activity you want to include in your feed.

Tidy resolve_program a bit

+68 -44
+20
src/lib/eunix.ml
··· 142 142 ~finally:(fun () -> Unix.tcsetattr Unix.stdin TCSADRAIN saved_tio) 143 143 fn 144 144 145 + let resolve_program ?(update = true) ?path hash name = 146 + let v = 147 + if not (String.contains name '/') then begin 148 + path 149 + |> Option.value ~default:"/bin:/usr/bin" 150 + |> String.split_on_char ':' 151 + |> List.find_map (fun dir -> 152 + let p = Filename.concat dir name in 153 + if Sys.file_exists p then Some p else None) 154 + end 155 + else if Sys.file_exists name then Some name 156 + else None 157 + in 158 + match (update, v) with 159 + | true, Some loc -> 160 + let hash = Hash.add ~utility:name ~loc hash in 161 + (hash, Some loc) 162 + | false, Some loc -> (hash, Some loc) 163 + | _, None -> (hash, None) 164 + 145 165 module Signals = struct 146 166 type t = 147 167 | Interrupt
+47 -43
src/lib/eval.ml
··· 159 159 let file_creation_mode ctx = 0o666 - ctx.umask 160 160 let cwd_of_ctx ctx = S.cwd ctx.state |> Fpath.to_string |> ( / ) ctx.fs 161 161 162 - let resolve_program ?(update = true) ctx name = 163 - let v = 164 - if not (String.contains name '/') then begin 165 - S.lookup ctx.state ~param:"PATH" 166 - |> Option.value ~default:"/bin:/usr/bin" 167 - |> String.split_on_char ':' 168 - |> List.find_map (fun dir -> 169 - let p = Filename.concat dir name in 170 - if Sys.file_exists p then Some p else None) 171 - end 172 - else if Sys.file_exists name then Some name 173 - else None 174 - in 175 - match (update, v) with 176 - | true, Some loc -> 177 - let hash = Hash.add ~utility:name ~loc ctx.hash in 178 - ({ ctx with hash }, Some loc) 179 - | false, Some loc -> (ctx, Some loc) 180 - | _, None -> (ctx, None) 181 - 182 162 let get_env ?(extra = []) ctx = 183 163 let extra = 184 164 extra @ List.map (fun (k, v) -> (k, v)) @@ S.exports ctx.state ··· 237 217 let mode = if async then Types.Async else Types.Switched sw in 238 218 let fds = ctx.rdrs @ Option.value ~default:[] fds in 239 219 let ctx, process = 240 - match (executable, resolve_program ctx executable) with 241 - | _, (ctx, None) | "", (ctx, _) -> 220 + let hash, prog = 221 + Eunix.resolve_program 222 + ?path:(S.lookup ctx.state ~param:"PATH") 223 + ctx.hash executable 224 + in 225 + let ctx = { ctx with hash } in 226 + match (executable, prog) with 227 + | _, None | "", _ -> 242 228 Eio.Flow.copy_string 243 229 (Fmt.str "msh: command not found: %s\n" executable) 244 230 stdout; 245 231 (ctx, Error (127, `Not_found)) 246 - | _, (ctx, Some full_path) -> 232 + | _, Some full_path -> 247 233 Debug.Log.debug (fun f -> 248 234 f "executing %a\n%a" 249 235 Fmt.(list ~sep:(Fmt.any " ") (quote string)) ··· 397 383 @@ fun () -> 398 384 if args <> [] then 399 385 let name = List.hd args in 400 - let prog = 401 - match 402 - resolve_program ~update:false ctx name 403 - with 404 - | _, None -> Fmt.failwith "%s not found" name 405 - | _, Some p -> p 386 + let ctx, prog = 387 + let hash, prog = 388 + Eunix.resolve_program ~update:false 389 + ?path:(S.lookup ctx.state ~param:"PATH") 390 + ctx.hash name 391 + in 392 + let ctx = { ctx with hash } in 393 + match prog with 394 + | None -> Fmt.failwith "%s not found" name 395 + | Some p -> (ctx, p) 406 396 in 407 397 Unix.execve prog (Array.of_list args) 408 398 (Array.of_list ··· 473 463 in 474 464 loop (Exit.value ctx) job rest 475 465 | _ -> ( 476 - let exec_and_args = 466 + let ctx, exec_and_args = 477 467 if is_command then begin 478 468 match command_args with 479 469 | [] -> assert false 480 470 | x :: xs -> ( 481 - match 482 - resolve_program ~update:false 483 - ctx x 484 - with 485 - | _, None -> 486 - Exit.nonzero ("", []) 1 487 - | _, Some prog -> 471 + let hash, prog = 472 + Eunix.resolve_program 473 + ~update:false 474 + ?path: 475 + (S.lookup ctx.state 476 + ~param:"PATH") 477 + ctx.hash x 478 + in 479 + let ctx = { ctx with hash } in 480 + match prog with 481 + | None -> 482 + (ctx, Exit.nonzero ("", []) 1) 483 + | Some prog -> 488 484 if print_command then 489 - Exit.zero ("echo", [ prog ]) 490 - else Exit.zero (x, xs)) 485 + ( ctx, 486 + Exit.zero 487 + ("echo", [ prog ]) ) 488 + else (ctx, Exit.zero (x, xs))) 491 489 end 492 - else Exit.zero (executable, args) 490 + else (ctx, Exit.zero (executable, args)) 493 491 in 494 492 match exec_and_args with 495 493 | Exit.Nonzero _ as v -> ··· 1482 1480 | _, WEXITED 0 -> Exit.zero ctx 1483 1481 | _, (WEXITED n | WSIGNALED n | WSTOPPED n) -> Exit.nonzero ctx n) 1484 1482 | Dot file -> ( 1485 - match resolve_program ctx file with 1486 - | ctx, None -> Exit.nonzero ctx 127 1487 - | ctx, Some fname -> 1483 + let hash, prog = 1484 + Eunix.resolve_program 1485 + ?path:(S.lookup ctx.state ~param:"PATH") 1486 + ctx.hash file 1487 + in 1488 + let ctx = { ctx with hash } in 1489 + match prog with 1490 + | None -> Exit.nonzero ctx 127 1491 + | Some fname -> 1488 1492 Debug.Log.debug (fun f -> f "sourcing..."); 1489 1493 let program = Ast.of_file (ctx.fs / fname) in 1490 1494 let ctx, _ =
+1 -1
src/lib/posix/exec.ml
··· 204 204 with_fds m @@ fun m -> 205 205 (* TODO: investigate -- the plan from Eio seems to also invert the list of redirections. 206 206 This is problematic for redirections, so we have copied the entire action here. *) 207 - let plan = Eio_unix__.Inherit_fds.plan m |> List.rev in 207 + let plan = Eio_unix__.Inherit_fds.plan m in 208 208 Eio_unix.Private.Fork_action. 209 209 { run = (fun k -> k (Obj.repr (action_dups, plan, blocking))) } 210 210