/// Entry point static void Main(string[] args) { Execution action = Develop; var addr = new string[] { "localhost" }; var inc = new List <string>(new string[] { "." }); var web = "."; var root = ""; var config = ""; var profile = "dev"; // Parse arguments var i = 0; while (i < args.Length) { switch (args[i]) { case "-p": profile = args[++i]; break; case "-r": root = args[++i]; if (!Directory.Exists(root)) { Console.Error.WriteLine("*** Document root {0} does not exist, exiting.", root); Environment.Exit(0x03); } break; case "-w": web = args[++i]; break; case "-c": config = args[++i]; if ("-" == config) { // No configuration, serve static content } else if (File.Exists(Paths.Compose(config, "web.ini"))) { // Web layout comes from $config/web.ini config = Paths.Resolve(config); } else { // Web layout comes from class $config config = ":" + config; } break; case "-cp": inc.Add(Paths.Resolve(args[++i])); break; case "-i": action = Inspect; break; case "-m": var mode = args[++i]; if ("develop" == mode) { action = Develop; } else { action = (_profile, _server, _port, _web, _root, _config, _inc) => { return(Service(_profile, _server, _port, _web, _root, _config, () => { return Executor.Instance(Paths.DirName(Paths.Binary()), "class", "xp.scriptlet.Server", _inc, new string[] { _web, _config, _profile, _server + ":" + _port, mode }); })); }; } break; case "-?": Execute("class", "xp.scriptlet.Usage", inc.ToArray(), new string[] { "xpws.txt" }); return; default: addr = args[i].Split(':'); break; } i++; } // If no "-c" argument given, try checking ./etc for a web.ini, fall back to // "no configuration" if (String.IsNullOrEmpty(config)) { var etc = Paths.Compose(".", "etc"); config = File.Exists(Paths.Compose(etc, "web.ini")) ? etc : "-"; } // If no document root has been supplied, check for an existing "static" // subdirectory inside the web root; otherwise just use the web root web = Paths.Resolve(web); if (String.IsNullOrEmpty(root)) { var path = Paths.Compose(web, "static"); root = Directory.Exists(path) ? path : web; } else { root = Paths.Resolve(root); } // Run Environment.Exit(action( profile, addr[0], addr.Length > 1 ? addr[1] : "8080", web, root, config, inc.ToArray() )); }
/// <summary> /// /// </summary> /// <param name="base_dir"></param> /// <param name="runner"></param> /// <param name="tool"></param> /// <param name="includes"></param> /// <param name="args"></param> public static Process Instance(string base_dir, string runner, string tool, string[] includes, string[] args) { string home = Environment.GetEnvironmentVariable("HOME"); // Read configuration XpConfigSource configs = new CompositeConfigSource( new EnvironmentConfigSource(), new IniConfigSource(new Ini(Paths.Compose(".", "xp.ini"))), null != home ? new IniConfigSource(new Ini(Paths.Compose(home, ".xp", "xp.ini"))) : null, new IniConfigSource(new Ini(Paths.Compose(Environment.SpecialFolder.LocalApplicationData, "Xp", "xp.ini"))), new IniConfigSource(new Ini(Paths.Compose(base_dir, "xp.ini"))) ); IEnumerable <string> use_xp = configs.GetUse(); string runtime = configs.GetRuntime(); string executor = configs.GetExecutable(runtime) ?? "php"; if (null == use_xp) { throw new EntryPointNotFoundException("Cannot determine use_xp setting from " + configs); } // Pass "USE_XP" and includes inside include_path separated by two path // separators. Prepend "." for the oddity that if the first element does // not exist, PHP scraps all the others(!) // // E.g.: -dinclude_path=".;xp\5.7.0;..\dialog;;..\impl.xar;..\log.xar" // ^ ^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^ // | | include_path // | USE_XP // Dot string argv = String.Format( "-C -q -dinclude_path=\".{1}{0}{1}{1}{2}\" -dmagic_quotes_gpc=0", String.Join(PATH_SEPARATOR, new List <string>(use_xp).ToArray()), PATH_SEPARATOR, String.Join(PATH_SEPARATOR, includes) ); // If input or output encoding are not equal to default, also pass their // names inside an LC_CONSOLE environment variable. Only do this inside // real Windows console windows! // // See http://msdn.microsoft.com/en-us/library/system.text.encoding.headername.aspx // and http://msdn.microsoft.com/en-us/library/system.text.encoding.aspx if (null == Environment.GetEnvironmentVariable("TERM")) { Encoding defaultEncoding = Encoding.Default; if (!defaultEncoding.Equals(Console.InputEncoding) || !defaultEncoding.Equals(Console.OutputEncoding)) { Environment.SetEnvironmentVariable("LC_CONSOLE", Console.InputEncoding.HeaderName + "," + Console.OutputEncoding.HeaderName); } } // Look for PHP configuration foreach (KeyValuePair <string, IEnumerable <string> > kv in configs.GetArgs(runtime)) { foreach (string value in kv.Value) { argv += " -d" + kv.Key + "=\"" + value + "\""; } } // Add extensions IEnumerable <string> extensions = configs.GetExtensions(runtime); if (null != extensions) { foreach (var ext in extensions) { argv += " -dextension=" + ext; } } // Spawn runtime var proc = new Process(); proc.StartInfo.FileName = executor; proc.StartInfo.Arguments = argv + " \"" + new List <string>(Paths.Locate(use_xp, "tools\\" + runner + ".php", true))[0] + "\" " + tool; if (args.Length > 0) { foreach (string arg in args) { proc.StartInfo.Arguments += " \"" + arg.Replace("\"", "\"\"\"") + "\""; } } // Catch Ctrl+C (only works in "real" consoles, not in a cygwin // shell, for example) and kill the spawned process, see also: // http://www.cygwin.com/ml/cygwin/2006-12/msg00151.html // http://www.mail-archive.com/[email protected]/msg74638.html Console.CancelKeyPress += delegate { proc.Kill(); proc.WaitForExit(); }; proc.StartInfo.UseShellExecute = false; return(proc); }
/// <summary> /// Creates the executor process instance /// </summary> public static Process Instance(string base_dir, string runner, string tool, string[] modules, string[] includes, string[] args) { string home = Environment.GetEnvironmentVariable("HOME"); // Read configuration XpConfigSource configs = new CompositeConfigSource( new EnvironmentConfigSource(), new IniConfigSource(new Ini(Paths.Compose(".", "xp.ini"))), null != home ? new IniConfigSource(new Ini(Paths.Compose(home, ".xp", "xp.ini"))) : null, new IniConfigSource(new Ini(Paths.Compose(Environment.SpecialFolder.LocalApplicationData, "Xp", "xp.ini"))), new IniConfigSource(new Ini(Paths.Compose(base_dir, "xp.ini"))) ); IEnumerable <string> use_xp = configs.GetUse(); string runtime = configs.GetRuntime(); string executor = configs.GetExecutable(runtime) ?? "php"; if (null == use_xp) { throw new EntryPointNotFoundException("Cannot determine use_xp setting from " + configs); } // Pass "USE_XP" and includes inside include_path separated by two path // separators. Prepend "." for the oddity that if the first element does // not exist, PHP scraps all the others(!) // // E.g.: -dinclude_path=".;xp\5.7.0;..\dialog;;..\impl.xar;..\log.xar" // ^ ^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^ // | | include_path // | USE_XP // Dot string argv = String.Format( "-C -q -d include_path=\".{0}{1}{0}{2}{0}{0}{3}\" -d magic_quotes_gpc=0", PATH_SEPARATOR, String.Join(PATH_SEPARATOR, use_xp), String.Join(PATH_SEPARATOR, modules), String.Join(PATH_SEPARATOR, includes) ); // Look for PHP configuration foreach (KeyValuePair <string, IEnumerable <string> > kv in configs.GetArgs(runtime)) { foreach (string value in kv.Value) { argv += " -d " + kv.Key + "=\"" + value + "\""; } } // Add extensions IEnumerable <string> extensions = configs.GetExtensions(runtime); if (null != extensions) { foreach (var ext in extensions) { argv += " -d extension=" + ext; } } // Find entry point, which is either a file called [runner]-main.php, which // will receive the arguments in UTF-8, or a file called [runner].php, which // assumes the arguments come in in platform encoding. // // Windows encodes the command line arguments to platform encoding for PHP, // which doesn't define a "wmain()", so we'll need to double-encode our // $argv in a "binary-safe" way in order to transport Unicode into PHP. string entry; bool redirect; Argument argument; if (null != (entry = Paths.Find(use_xp, "tools\\" + runner + ".php"))) { argument = Pass; redirect = false; } else if (null != (entry = Paths.Find(new string[] { base_dir }, runner + "-main.php"))) { argument = Encode; redirect = true; } else { throw new EntryPointNotFoundException("Cannot find tool in " + String.Join(", ", new List <string>(use_xp).ToArray())); } // Spawn runtime var proc = new Process(); proc.StartInfo.RedirectStandardOutput = redirect; proc.StartInfo.RedirectStandardError = redirect; proc.StartInfo.FileName = executor; proc.StartInfo.Arguments = argv + " \"" + entry + "\" " + tool; if (args.Length > 0) { foreach (string arg in args) { proc.StartInfo.Arguments += " " + argument(arg); } } // Catch Ctrl+C (only works in "real" consoles, not in a cygwin // shell, for example) and kill the spawned process, see also: // http://www.cygwin.com/ml/cygwin/2006-12/msg00151.html // http://www.mail-archive.com/[email protected]/msg74638.html Console.CancelKeyPress += (sender, e) => { proc.Kill(); proc.WaitForExit(); }; proc.StartInfo.UseShellExecute = false; return(proc); }
static void Main(string[] args) { var pid = System.Diagnostics.Process.GetCurrentProcess().Id; var addr = new string[] { "localhost" }; var profile = "dev"; var root = ""; var i = 0; var parsing = true; // Parse arguments while (parsing && i < args.Length) { switch (args[i]) { case "-p": profile = args[++i]; break; case "-r": var dir = args[++i]; Environment.SetEnvironmentVariable("DOCUMENT_ROOT", dir); root = " -t " + dir; break; case "-?": Execute("class", "xp.scriptlet.Usage", new string[] { "." }, new string[] { "xpws.txt" }); return; default: addr = args[i].Split(':'); parsing = false; break; } i++; } // Verify we have a web.ini if (!System.IO.File.Exists(Paths.Compose("etc", "web.ini"))) { Console.Error.WriteLine("*** Cannot find the web configuration web.ini in etc/, exiting."); Environment.Exit(0x03); } var server = addr[0]; var port = addr.Length > 1 ? addr[1] : "8080"; if (i > 0) { Array.Copy(args, i, args, 0, args.Length - i); Array.Resize(ref args, args.Length - i); } // Execute var proc = Executor.Instance(Paths.DirName(Paths.Binary()), "web", "", new string[] { "." }, args); proc.StartInfo.Arguments = "-S " + server + ":" + port + root + " " + proc.StartInfo.Arguments; try { Environment.SetEnvironmentVariable("SERVER_PROFILE", profile); proc.Start(); Console.Out.WriteLine("[xpws-{0}#{1}] running @ {2}:{3}. Press <Enter> to exit", profile, pid, server, port); Console.Read(); Console.Out.WriteLine("[xpws-{0}#{1}] shutting down...", profile, pid); proc.Kill(); proc.WaitForExit(); Environment.Exit(proc.ExitCode); } catch (SystemException e) { Console.Error.WriteLine("*** " + proc.StartInfo.FileName + ": " + e.Message); Environment.Exit(0xFF); } finally { proc.Close(); } }