Source

ocaml-pkgbuild / bashexpand.ml

Diff from to

File bashexpand.ml

     exception ExpandError       of int
     exception UnbalancedBracket of int
 
-    let get_param name plist =
-      printf "*DBG* get_param: name=%s\n" name ;
-      try List.assoc name plist
-      with Not_found -> Bashparams.param_of_string ""
+    let param_lookup expander name plist =
+      let param = try List.assoc name plist
+        with Not_found -> Bashparams.param_of_string ""
+      in expander param
+
+    (** Lookup the value of a parameter in string context.
+        This is like $FOO in bash. *)
+    let param_str_ctx = param_lookup Bashparams.string_of_param
+
+    (** Lookup the value of a parameter in array context.
+        This is like ${FOO[@]} in bash. *)
+    (* Commented out because we don't use this, yet. *)
+    (* let param_arr_ctx = param_lookup Bashparams.string_of_array *)
 
     let set_param name param plist =
       (name, param) :: List.remove_assoc name plist
 
+    (** Search and destroy parameters ($VARs) in a string. **)
     let parameter_expand str params =
 
       (* Recursive helper function for parameter_expand. *)
             let expand_param_subst name idx =
               (* XXX: What about badly formed input? *)
               match expand_until_curly (idx + 1) with (newidx, subst) ->
-                let pval = Bashparams.string_of_param (get_param name params) in
+                let pval = param_str_ctx name params in
                 let expstr =
                   match str.[idx] with
                   | '-' -> if "" = pval then subst else pval
             in
 
             match str.[idx] with
-            | '}' -> (idx+1, Bashparams.string_of_param (get_param name params))
+            | '}' -> (idx+1, param_str_ctx name params)
             | ':' -> expand_param_subst name (idx+1)
             |  _  -> raise Not_found
           in (* end of expand_inbraces *)
               begin
                 try
                   match expand_name (idx+1) with (newidx, name) ->
-                    (newidx, Bashparams.string_of_param (get_param name params))
+                    (newidx, param_str_ctx name params)
                 with Invalid_argument(_) -> raise (ExpandError idx)
               end
           | '{' ->
               end
           | _ ->
               match extract_name idx with (newidx, name) ->
-                (newidx, Bashparams.string_of_param (get_param name params))
+                (newidx, param_str_ctx name params)
         in (* end of expand_name *)
 
         try