X-Git-Url: http://git.annexia.org/?p=whenjobs.git;a=blobdiff_plain;f=lib%2Fpa_when.ml;h=0d164698897405d5e81200a88a3aa3a141787d29;hp=d646822e609812b922ab98c8f9ff7094303e9774;hb=38e734a745fef6c2ccb6fc9f657f106a69fc86e6;hpb=61cad7bbaf63389b520b695eefdd735bc11a8aa6 diff --git a/lib/pa_when.ml b/lib/pa_when.ml index d646822..0d16469 100644 --- a/lib/pa_when.ml +++ b/lib/pa_when.ml @@ -29,12 +29,40 @@ open Camlp4.PreCast open Syntax open Ast -open Whenutils - let unique = let i = ref 0 in fun () -> incr i; !i let unique_job_name () = sprintf "job$%d" (unique ()) +let rec find s sub = + let len = String.length s in + let sublen = String.length sub in + let rec loop i = + if i <= len-sublen then ( + let rec loop2 j = + if j < sublen then ( + if s.[i+j] = sub.[j] then loop2 (j+1) + else -1 + ) else + i (* found *) + in + let r = loop2 0 in + if r = -1 then loop (i+1) else r + ) else + -1 (* not found *) + in + loop 0 + +let rec replace_str s s1 s2 = + let len = String.length s in + let sublen = String.length s1 in + let i = find s s1 in + if i = -1 then s + else ( + let s' = String.sub s 0 i in + let s'' = String.sub s (i+sublen) (len-i-sublen) in + s' ^ s2 ^ replace_str s'' s1 s2 + ) + (* Convert a _loc to an AST. *) let expr_of_loc _loc loc = let file_name, @@ -89,8 +117,8 @@ let () = let sh_quotation_expander _loc _ sh = let loc = expr_of_loc _loc _loc in - (* XXX Expand %- or $- expressions in code. *) - (* XXX Escape >> in code. *) + (* Convert ">\>" to ">>" in code. *) + let sh = replace_str sh ">\\>" ">>" in <:expr< { Whenutils.sh_loc = $loc$; sh_script = $str:sh$ } >>