Shells in OCaml
3
fork

Configure Feed

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

Unset built-in

Unsetting of variables and functions from the environment.

+95
+35
src/lib/built_ins.ml
··· 43 43 | Set of set 44 44 | Wait of int 45 45 | Dot of string (* a.k.a source *) 46 + | Unset of [ `Variables of string list | `Functions of string list ] 46 47 47 48 (* Change Directory *) 48 49 module Cd = struct ··· 131 132 Cmd.v info term 132 133 end 133 134 135 + module Unset = struct 136 + open Cmdliner 137 + 138 + let names = 139 + let doc = "Names of variables or functions to remove." in 140 + Arg.(value & pos_all string [] & info [] ~docv:"NAMES" ~doc) 141 + 142 + let kind = 143 + let func = 144 + let doc = "Names refer to functions to be removed." in 145 + Arg.(info [ "f" ] ~docv:"FUNCTION" ~doc) 146 + in 147 + let vari = 148 + let doc = "Names refer to variables to be removed." in 149 + Arg.(info [ "v" ] ~docv:"VARIABLE" ~doc) 150 + in 151 + Arg.(value & vflag `Variable [ (`Function, func); (`Variable, vari) ]) 152 + 153 + let t = 154 + let make_unset kind n = 155 + let names = 156 + match kind with `Variable -> `Variables n | `Function -> `Functions n 157 + in 158 + Unset names 159 + in 160 + let term = Term.(const make_unset $ kind $ names) in 161 + let info = 162 + let doc = "Wait for a particular PID (default is 0)" in 163 + Cmd.info "wait" ~doc 164 + in 165 + Cmd.v info term 166 + end 167 + 134 168 module Make_dot (T : sig 135 169 val name : string 136 170 end) = ··· 177 211 | "wait" :: _ as cmd -> exec_cmd cmd Wait.t 178 212 | "source" :: _ as cmd -> exec_cmd cmd Source.t 179 213 | "." :: _ as cmd -> exec_cmd cmd Dot.t 214 + | "unset" :: _ as cmd -> exec_cmd cmd Unset.t 180 215 | _ -> None
+1
src/lib/built_ins.mli
··· 20 20 | Set of set 21 21 | Wait of int 22 22 | Dot of string 23 + | Unset of [ `Variables of string list | `Functions of string list ] 23 24 24 25 val of_args : string list -> (t, string) result option 25 26 (** Parses a command-line to the built-ins, errors are returned if parsing. *)
+16
src/lib/eval.ml
··· 744 744 let program = Ast.of_file (ctx.fs / f) in 745 745 let ctx, _ = run (Exit.zero ctx) program in 746 746 ctx) 747 + | Unset names -> ( 748 + match names with 749 + | `Variables names -> 750 + let state = 751 + List.fold_left 752 + (fun t param -> S.remove ~param t |> snd) 753 + ctx.state names 754 + in 755 + Exit.zero { ctx with state } 756 + | `Functions names -> 757 + let functions = 758 + List.fold_left 759 + (fun t param -> List.remove_assoc param t) 760 + ctx.functions names 761 + in 762 + Exit.zero { ctx with functions }) 747 763 748 764 and exec initial_ctx ((command, sep) : Ast.complete_command) = 749 765 let rec loop : Eio.Switch.t -> ctx -> Ast.clist -> ctx Exit.t =
+5
src/lib/posix/state.ml
··· 22 22 let variables' = Variables.add param (export, v) t.variables in 23 23 { t with variables = variables' } 24 24 25 + let remove ~param t = 26 + match Variables.find_opt param t.variables with 27 + | None -> (false, t) 28 + | Some _ -> (true, { t with variables = Variables.remove param t.variables }) 29 + 25 30 let exports t = 26 31 Variables.to_list t.variables 27 32 |> List.filter_map (function p, (true, v) -> Some (p, v) | _ -> None)
+4
src/lib/types.ml
··· 27 27 (** Update the state with a new parameter mapping and whether or not it should 28 28 exported to the environment (default false). *) 29 29 30 + val remove : param:string -> t -> bool * t 31 + (** [remove ~param t] removes [param] from [t] if it exists. [bool] is [true] 32 + if a removal took place. *) 33 + 30 34 val exports : t -> (string * Ast.word_cst) list 31 35 (** All of the variables that must be exported to the environment *) 32 36
+34
test/built_ins.t
··· 59 59 hello world 60 60 $ msh run.sh 61 61 hello world 62 + 63 + 4. Unset 64 + 65 + $ cat > test.sh <<EOF 66 + > FOO=bar 67 + > echo \$FOO 68 + > unset FOO 69 + > echo \$FOO 70 + > EOF 71 + 72 + $ sh test.sh 73 + bar 74 + 75 + $ msh test.sh 76 + bar 77 + 78 + 79 + $ cat > test.sh <<EOF 80 + > shout () { 81 + > echo \$1 | tr a-z A-Z 82 + > } 83 + > shout "hey" 84 + > unset -f shout 85 + > shout "ho" 86 + > EOF 87 + 88 + $ sh test.sh 89 + HEY 90 + test.sh: line 6: shout: command not found 91 + [127] 92 + $ msh test.sh 93 + HEY 94 + msh: command not found: shout 95 + [127]