static void main(string[] rawArgs)
        {
            HostConsole.OnStart();
            try
            {
                //work around of nasty Win7x64 problem.
                //http://superuser.com/questions/527728/cannot-resolve-windir-cannot-modify-path-or-path-being-reset-on-boot
                if (Environment.GetEnvironmentVariable("windir") == null)
                {
                    Environment.SetEnvironmentVariable("windir", Environment.GetEnvironmentVariable("SystemRoot"));
                }

                Profiler.Stopwatch.Start();

                string[] args = rawArgs;

                //Debug.Assert(false);

                if (Utils.IsLinux())
                {
                    //because Linux shebang does not properly split arguments we need to take care of this
                    //http://www.daniweb.com/software-development/c/threads/268382
                    List <string> tempArgs = new List <string>();
                    foreach (string arg in rawArgs)
                    {
                        if (arg.StartsWith(CSSUtils.cmdFlagPrefix))
                        {
                            foreach (string subArg in arg.Split(CSSUtils.cmdFlagPrefix.ToCharArray()))
                            {
                                if (subArg.Trim() != "")
                                {
                                    tempArgs.Add(CSSUtils.cmdFlagPrefix + subArg.Trim());
                                }
                            }
                        }
                        else
                        {
                            tempArgs.Add(arg);
                        }
                    }

                    args = tempArgs.ToArray();
                }

                try
                {
                    SetEnvironmentVariable("CSScriptRuntime", System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString());
                    SetEnvironmentVariable("CSScriptRuntimeLocation", System.Reflection.Assembly.GetExecutingAssembly().Location);
                }
                catch { } //SetEnvironmentVariable will always throw an exception on Mono

                CSExecutor exec = new CSExecutor();

                if (AppDomain.CurrentDomain.FriendlyName != "ExecutionDomain") // AppDomain.IsDefaultAppDomain is more appropriate but it is not available in .NET 1.1
                {
                    string configFile = exec.GetCustomAppConfig(args);
                    if (configFile != "")
                    {
                        AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation;
                        setup.ConfigurationFile = configFile;

                        AppDomain appDomain = AppDomain.CreateDomain("ExecutionDomain", null, setup);
#if !net4
                        appDomain.ExecuteAssembly(Assembly.GetExecutingAssembly().Location, null, args);
#else
                        appDomain.ExecuteAssembly(Assembly.GetExecutingAssembly().Location, args);
#endif
                        return;
                    }
                }

                try
                {
                    AppInfo.appName = Path.GetFileName(Assembly.GetExecutingAssembly().Location);
                    exec.Execute(args, new PrintDelegate(Print), null);
                }
                catch (Surrogate86ProcessRequiredException)
                {
#if !net4
                    throw new ApplicationException("Cannot build surrogate host application because this script engine is build against early version of CLR.");
#else
                    try
                    {
                        string thisAssembly = Assembly.GetExecutingAssembly().Location;
                        string runner       = Path.Combine(Path.GetDirectoryName(thisAssembly), "lib\\runasm32.exe");

                        if (!File.Exists(runner))
                        {
                            runner = Path.Combine(Path.GetDirectoryName(thisAssembly), "runasm32.exe");
                        }

                        if (!File.Exists(runner))
                        {
                            runner = Environment.ExpandEnvironmentVariables("CSSCRIPT_32RUNNER");
                        }

                        if (!File.Exists(runner))
                        {
                            Print("This script requires to be executed as x86 process but no runner (e.g. runasm32.exe) can be found.");
                        }
                        else
                        {
                            RunConsoleApp(runner, "\"" + thisAssembly + "\" " + GetCommandLineArgumentsStringFromEnvironment());
                        }
                    }
                    catch { } //This will always throw an exception on Mono
#endif
                }
                catch (SurrogateHostProcessRequiredException e)
                {
#if !net4
                    object dummy = e;
                    throw new ApplicationException("Cannot build surrogate host application because this script engine is build against early version of CLR.");
#else
                    try
                    {
                        string assemblyHost = ScriptLauncherBuilder.GetLauncherName(e.ScriptAssembly);
                        string appArgs      = CSSUtils.cmdFlagPrefix + "css_host_parent:" + Process.GetCurrentProcess().Id + " \"" + CSSUtils.cmdFlagPrefix + "css_host_asm:" + e.ScriptAssembly + "\" " + GenerateCommandLineArgumentsString(e.ScriptArgs);
                        if (e.StartDebugger)
                        {
                            appArgs = CSSUtils.cmdFlagPrefix + "css_host_dbg:true " + appArgs;
                        }

                        RunConsoleApp(assemblyHost, appArgs);
                    }
                    catch (Exception e1)
                    {
                        Console.WriteLine("Cannot execute Surrogate Host Process: " + e1);
                    }
#endif
                }
            }
            finally
            {
                HostConsole.OnExit();
            }
        }
Beispiel #2
0
        static void main(string[] rawArgs)
        {
            Utils.SetMonoRootDirEnvvar();

            if (rawArgs.Contains("-preload"))
            {
                rawArgs = SchedulePreloadCompiler(rawArgs);
            }

            for (int i = 0; i < rawArgs.Length; i++)
            {
                rawArgs[i] = Environment.ExpandEnvironmentVariables(rawArgs[i]);
            }

            HostConsole.OnStart();
            try
            {
                //work around of nasty Win7x64 problem.
                //http://superuser.com/questions/527728/cannot-resolve-windir-cannot-modify-path-or-path-being-reset-on-boot
                if (Environment.GetEnvironmentVariable("windir") == null)
                {
                    Environment.SetEnvironmentVariable("windir", Environment.GetEnvironmentVariable("SystemRoot"));
                }

                Environment.SetEnvironmentVariable("pid", Process.GetCurrentProcess().Id.ToString());

                Profiler.Stopwatch.Start();

                string[] args = rawArgs;

                // if (args.Contains("-check"))
                // Debug.Assert(false);

                if (Utils.IsLinux)
                {
                    //because Linux shebang does not properly split arguments we need to take care of this
                    //http://www.daniweb.com/software-development/c/threads/268382
                    List <string> tempArgs = new List <string>();
                    foreach (string arg in rawArgs)
                    {
                        if (arg == CSSUtils.Args.DefaultPrefix)
                        {
                            foreach (string subArg in arg.Split(CSSUtils.Args.DefaultPrefix[0]))
                            {
                                if (subArg.Trim() != "")
                                {
                                    tempArgs.Add(CSSUtils.Args.DefaultPrefix + subArg.Trim());
                                }
                            }
                        }
                        else
                        {
                            tempArgs.Add(arg);
                        }
                    }

                    args = tempArgs.ToArray();
                }

                try
                {
                    Utils.SetEnvironmentVariable("CSScriptRuntime", System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString());
                    Utils.SetEnvironmentVariable("CSScriptRuntimeLocation", System.Reflection.Assembly.GetExecutingAssembly().Location);
                    Utils.SetEnvironmentVariable("cscs_exe_dir", Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location));

                    if (Environment.GetEnvironmentVariable("CSSCRIPT_DIR") == null && Utils.IsLinux)
                    {
                        // GetExecutingAssembly().Location may be empty even for the entry assembly
                        var cscs_exe_dir = Environment.GetEnvironmentVariable("cscs_exe_dir");
                        if (cscs_exe_dir != null && cscs_exe_dir.StartsWith("/usr/local/"))
                        {
                            Utils.SetEnvironmentVariable("CSSCRIPT_DIR", cscs_exe_dir);
                        }
                    }
                }
                catch { } //SetEnvironmentVariable may throw an exception on Mono

                CSExecutor.print = new PrintDelegate(Print);

                CSExecutor exec = new CSExecutor();

                try
                {
                    if (AppDomain.CurrentDomain.FriendlyName != "ExecutionDomain") // AppDomain.IsDefaultAppDomain is more appropriate but it is not available in .NET 1.1
                    {
                        string configFile = exec.GetCustomAppConfig(args);
                        if (configFile != "")
                        {
                            AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation;
                            setup.ConfigurationFile = configFile;

                            AppDomain appDomain = AppDomain.CreateDomain("ExecutionDomain", null, setup);
#if !net4
                            appDomain.ExecuteAssembly(Assembly.GetExecutingAssembly().Location, null, args);
#else
                            appDomain.ExecuteAssembly(Assembly.GetExecutingAssembly().Location, args);
#endif
                            return;
                        }
                    }

#if net4
                    //CSSUtils.DbgInjectionCode = cscscript.Resources.dbg;
                    CSSUtils.DbgInjectionCode = embedded_strings.dbg_source;
#endif
                    AppInfo.appName = Path.GetFileName(Assembly.GetExecutingAssembly().Location);
                    exec.Execute(args, Print, null);
                }
                catch (Surrogate86ProcessRequiredException)
                {
#if !net4
                    throw new ApplicationException("Cannot build surrogate host application because this script engine is build against early version of CLR.");
#else
                    try
                    {
                        string thisAssembly = Assembly.GetExecutingAssembly().Location;
                        string runner       = Path.Combine(Path.GetDirectoryName(thisAssembly), "lib\\runasm32.exe");

                        if (!File.Exists(runner))
                        {
                            runner = Path.Combine(Path.GetDirectoryName(thisAssembly), "runasm32.exe");
                        }

                        if (!File.Exists(runner))
                        {
                            runner = Environment.ExpandEnvironmentVariables("%CSSCRIPT_32RUNNER%");
                        }

                        if (!File.Exists(runner))
                        {
                            Print("This script requires to be executed as x86 process but no runner (e.g. runasm32.exe) can be found.");
                        }
                        else
                        {
                            RunConsoleApp(runner, "\"" + thisAssembly + "\" " + GetCommandLineArgumentsStringFromEnvironment());
                        }
                    }
                    catch { } //This will always throw an exception on Mono
#endif
                }
                catch (CLIException e)
                {
                    if (!(e is CLIExitRequest))
                    {
                        Console.WriteLine(e.Message);
                        Environment.ExitCode = e.ExitCode;
                    }
                }
                catch (SurrogateHostProcessRequiredException e)
                {
#if !net4
                    object dummy = e;
                    throw new ApplicationException("Cannot build surrogate host application because this script engine is build against early version of CLR.");
#else
                    try
                    {
                        string assemblyHost = ScriptLauncherBuilder.GetLauncherName(e.ScriptAssembly);
                        string appArgs      = CSSUtils.Args.DefaultPrefix + "css_host_parent:" + Process.GetCurrentProcess().Id + " \"" + CSSUtils.Args.DefaultPrefix + "css_host_asm:" + e.ScriptAssembly + "\" " + GenerateCommandLineArgumentsString(e.ScriptArgs);
                        if (e.StartDebugger)
                        {
                            appArgs = CSSUtils.Args.DefaultPrefix + "css_host_dbg:true " + appArgs;
                        }

                        RunConsoleApp(assemblyHost, appArgs);
                    }
                    catch (Exception e1)
                    {
                        Console.WriteLine("Cannot execute Surrogate Host Process: " + e1);
                    }
#endif
                }

                if (exec.WaitForInputBeforeExit != null)
                {
                    Console.WriteLine(exec.WaitForInputBeforeExit);
                    Console.ReadKey();
                }
            }
            finally
            {
                HostConsole.OnExit();
            }
        }