diff --git a/ShaderMinifier/main.fs b/ShaderMinifier/main.fs index 17ab134..9b3cf5c 100644 --- a/ShaderMinifier/main.fs +++ b/ShaderMinifier/main.fs @@ -33,14 +33,65 @@ let run (options: Options.Options) filenames = printfn "%s" (exn.ToString()) 1 +let splitCommandLine (input: string) : string list = + // " a \\\" b c\"\\d \" c\"\\\d\\\"\"e " -> ["a"; """; "b"; "cd "; "c\d"e"] + let args = ResizeArray() + let sb = System.Text.StringBuilder() + let mutable inQuotes = false + let mutable escapeNext = false + for c in input do + match c with + | _ when escapeNext -> + sb.Append(c) |> ignore + escapeNext <- false + | '\\' -> + escapeNext <- true + | '"' -> + inQuotes <- not inQuotes + | ' ' when not inQuotes -> + if sb.Length > 0 then + args.Add(sb.ToString()) + sb.Clear() |> ignore + | _ -> + sb.Append(c) |> ignore + if sb.Length > 0 then + args.Add(sb.ToString()) + args |> List.ofSeq +let x = splitCommandLine " a \\\" b\"\\c \" z\"\\d\\\"\"e " +in if x <> ["a"; "\""; "bc "; "zd\"e"] then failwithf "unit test failed: %A" x + +let runServer () = + use stdin = new StreamReader(System.Console.OpenStandardInput()) + let mutable exit = false + while not exit do + let line = stdin.ReadLine() + printfn "> %s" line + match line with + | null | "#exit" -> exit <- true + | _ when System.String.IsNullOrWhiteSpace(line) -> () + | _ when line.StartsWith "#" -> () + | _ -> + let argv = splitCommandLine line |> Seq.toArray + printfn "[%s]" (argv |> Array.map (sprintf "\"%s\"") |> String.concat ", ") + let options, files = Options.initFiles argv + if files |> Seq.contains "stdin" then + printfn "Usage of stdin is not supported in server mode." + else + run options files |> ignore + printfn "ok" + 0 + [] let main argv = let err = try - let options, files = Options.initFiles argv - if options.verbose then - printfn "Shader Minifier %s - https://github.com/laurentlb/Shader_Minifier" Options.version - run options files + if argv = [|"--server"|] then + runServer () + else + let options, files = Options.initFiles argv + if options.verbose then + printfn "Shader Minifier %s - https://github.com/laurentlb/Shader_Minifier" Options.version + run options files with | :? Argu.ArguParseException as ex -> printfn "%s" ex.Message