Esempio n. 1
0
        private static string RunCommands(string strText, SprContext ctx,
                                          uint uRecursionLevel)
        {
            string        str = strText;
            int           iStart;
            List <string> lParams;

            while (ParseAndRemovePlhWithParams(ref str, ctx, uRecursionLevel,
                                               @"{CMD:", out iStart, out lParams, true))
            {
                if (lParams.Count == 0)
                {
                    continue;
                }
                string strCmd = lParams[0];
                if (string.IsNullOrEmpty(strCmd))
                {
                    continue;
                }

                Process p = null;
                try
                {
                    StringComparison sc = StrUtil.CaseIgnoreCmp;

                    string strOpt = ((lParams.Count >= 2) ? lParams[1] :
                                     string.Empty);
                    Dictionary <string, string> d = SplitParams(strOpt);

                    ProcessStartInfo psi = new ProcessStartInfo();

                    string strApp, strArgs;
                    StrUtil.SplitCommandLine(strCmd, out strApp, out strArgs);
                    strApp = WinUtil.CompileUrl((strApp ?? string.Empty),
                                                ((ctx != null) ? ctx.Entry : null), true, null);
                    if (string.IsNullOrEmpty(strApp))
                    {
                        continue;
                    }
                    psi.FileName = strApp;
                    if (!string.IsNullOrEmpty(strArgs))
                    {
                        psi.Arguments = strArgs;
                    }

                    string strMethod  = GetParam(d, "m", "s");
                    bool   bShellExec = !strMethod.Equals("c", sc);
                    psi.UseShellExecute = bShellExec;

                    string strO    = GetParam(d, "o", (bShellExec ? "0" : "1"));
                    bool   bStdOut = strO.Equals("1", sc);
                    if (bStdOut)
                    {
                        psi.RedirectStandardOutput = true;
                    }

                    string strWS = GetParam(d, "ws", "n");
                    if (strWS.Equals("h", sc))
                    {
                        psi.CreateNoWindow = true;
                        psi.WindowStyle    = ProcessWindowStyle.Hidden;
                    }
                    else if (strWS.Equals("min", sc))
                    {
                        psi.WindowStyle = ProcessWindowStyle.Minimized;
                    }
                    else if (strWS.Equals("max", sc))
                    {
                        psi.WindowStyle = ProcessWindowStyle.Maximized;
                    }
                    else
                    {
                        Debug.Assert(psi.WindowStyle == ProcessWindowStyle.Normal);
                    }

                    string strVerb = GetParam(d, "v", null);
                    if (!string.IsNullOrEmpty(strVerb))
                    {
                        psi.Verb = strVerb;
                    }

                    bool bWait = GetParam(d, "w", "1").Equals("1", sc);

                    p = Process.Start(psi);
                    if (p == null)
                    {
                        Debug.Assert(false); continue;
                    }

                    if (bStdOut)
                    {
                        string strOut = p.StandardOutput.ReadToEnd();
                        strOut = TransformContent(strOut, ctx);
                        str    = str.Insert(iStart, strOut);
                    }

                    if (bWait)
                    {
                        p.WaitForExit();
                    }
                }
                catch (Exception ex)
                {
                    string strMsg = strCmd + MessageService.NewParagraph + ex.Message;
                    MessageService.ShowWarning(strMsg);
                }
                finally
                {
                    try { if (p != null)
                          {
                              p.Dispose();
                          }
                    }
                    catch (Exception) { Debug.Assert(false); }
                }
            }

            return(str);
        }
Esempio n. 2
0
        private static string RunCommands(string strText, SprContext ctx,
                                          uint uRecursionLevel)
        {
            string        str = strText;
            int           iStart;
            List <string> lParams;

            while (ParseAndRemovePlhWithParams(ref str, ctx, uRecursionLevel,
                                               @"{CMD:", out iStart, out lParams, false))
            {
                if (lParams.Count == 0)
                {
                    continue;
                }

                string strBaseRaw = null;
                if ((ctx != null) && (ctx.Base != null))
                {
                    if (ctx.BaseIsEncoded)
                    {
                        strBaseRaw = UntransformContent(ctx.Base, ctx);
                    }
                    else
                    {
                        strBaseRaw = ctx.Base;
                    }
                }

                string strCmd = WinUtil.CompileUrl((lParams[0] ?? string.Empty),
                                                   ((ctx != null) ? ctx.Entry : null), true, strBaseRaw, true);
                if (WinUtil.IsCommandLineUrl(strCmd))
                {
                    strCmd = WinUtil.GetCommandLineFromUrl(strCmd);
                }
                if (string.IsNullOrEmpty(strCmd))
                {
                    continue;
                }

                Process p = null;
                try
                {
                    StringComparison sc = StrUtil.CaseIgnoreCmp;

                    string strOpt = ((lParams.Count >= 2) ? lParams[1] :
                                     string.Empty);
                    Dictionary <string, string> d = SplitParams(strOpt);

                    ProcessStartInfo psi = new ProcessStartInfo();

                    string strApp, strArgs;
                    StrUtil.SplitCommandLine(strCmd, out strApp, out strArgs);
                    if (string.IsNullOrEmpty(strApp))
                    {
                        continue;
                    }
                    psi.FileName = strApp;
                    if (!string.IsNullOrEmpty(strArgs))
                    {
                        psi.Arguments = strArgs;
                    }

                    string strMethod  = GetParam(d, "m", "s");
                    bool   bShellExec = !strMethod.Equals("c", sc);
                    psi.UseShellExecute = bShellExec;

                    string strO    = GetParam(d, "o", (bShellExec ? "0" : "1"));
                    bool   bStdOut = strO.Equals("1", sc);
                    if (bStdOut)
                    {
                        psi.RedirectStandardOutput = true;
                    }

                    string strWS = GetParam(d, "ws", "n");
                    if (strWS.Equals("h", sc))
                    {
                        psi.CreateNoWindow = true;
                        psi.WindowStyle    = ProcessWindowStyle.Hidden;
                    }
                    else if (strWS.Equals("min", sc))
                    {
                        psi.WindowStyle = ProcessWindowStyle.Minimized;
                    }
                    else if (strWS.Equals("max", sc))
                    {
                        psi.WindowStyle = ProcessWindowStyle.Maximized;
                    }
                    else
                    {
                        Debug.Assert(psi.WindowStyle == ProcessWindowStyle.Normal);
                    }

                    string strVerb = GetParam(d, "v", null);
                    if (!string.IsNullOrEmpty(strVerb))
                    {
                        psi.Verb = strVerb;
                    }

                    bool bWait = GetParam(d, "w", "1").Equals("1", sc);

                    p = NativeLib.StartProcessEx(psi);
                    if (p == null)
                    {
                        Debug.Assert(false); continue;
                    }

                    if (bStdOut)
                    {
                        string strOut = (p.StandardOutput.ReadToEnd() ?? string.Empty);

                        // Remove trailing new-line characters, like $(...);
                        // https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html
                        // https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html#Command-Substitution
                        strOut = strOut.TrimEnd('\r', '\n');

                        strOut = TransformContent(strOut, ctx);
                        str    = str.Insert(iStart, strOut);
                    }

                    if (bWait)
                    {
                        p.WaitForExit();
                    }
                }
                catch (Exception ex)
                {
                    string strMsg = strCmd + MessageService.NewParagraph + ex.Message;
                    MessageService.ShowWarning(strMsg);
                }
                finally
                {
                    try { if (p != null)
                          {
                              p.Dispose();
                          }
                    }
                    catch (Exception) { Debug.Assert(false); }
                }
            }

            return(str);
        }
        private static void ExecuteShellCmd(EcasAction a, EcasContext ctx)
        {
            string strCmd       = EcasUtil.GetParamString(a.Parameters, 0);
            string strArgs      = EcasUtil.GetParamString(a.Parameters, 1, true, true);
            bool   bWait        = EcasUtil.GetParamBool(a.Parameters, 2);
            uint   uWindowStyle = EcasUtil.GetParamUInt(a.Parameters, 3);
            string strVerb      = EcasUtil.GetParamString(a.Parameters, 4, true);

            if (string.IsNullOrEmpty(strCmd))
            {
                return;
            }

            Process p = null;

            try
            {
                PwEntry pe = null;
                try { pe = Program.MainForm.GetSelectedEntry(false); }
                catch (Exception) { Debug.Assert(false); }

                strCmd = WinUtil.CompileUrl(strCmd, pe, true, null, false);

                ProcessStartInfo psi = new ProcessStartInfo();
                psi.FileName = strCmd;
                if (!string.IsNullOrEmpty(strArgs))
                {
                    psi.Arguments = strArgs;
                }

                bool bShEx = true;
                if (!string.IsNullOrEmpty(strVerb))
                {
                }                                                      // Need ShellExecute
                else if ((uWindowStyle == IdWindowMin) ||
                         (uWindowStyle == IdWindowMax))
                {
                }                                                          // Need ShellExecute
                else
                {
                    string strCmdFlt = strCmd.TrimEnd(new char[] { '\"', '\'',
                                                                   ' ', '\t', '\r', '\n' });
                    if (strCmdFlt.EndsWith(".exe", StrUtil.CaseIgnoreCmp) ||
                        strCmdFlt.EndsWith(".com", StrUtil.CaseIgnoreCmp))
                    {
                        bShEx = false;
                    }
                }
                psi.UseShellExecute = bShEx;

                if (uWindowStyle == IdWindowHidden)
                {
                    psi.CreateNoWindow = true;
                    psi.WindowStyle    = ProcessWindowStyle.Hidden;
                }
                else if (uWindowStyle == IdWindowMin)
                {
                    psi.WindowStyle = ProcessWindowStyle.Minimized;
                }
                else if (uWindowStyle == IdWindowMax)
                {
                    psi.WindowStyle = ProcessWindowStyle.Maximized;
                }

                if (!string.IsNullOrEmpty(strVerb))
                {
                    psi.Verb = strVerb;
                }

                p = NativeLib.StartProcessEx(psi);

                if ((p != null) && bWait)
                {
                    Program.MainForm.UIBlockInteraction(true);
                    MessageService.ExternalIncrementMessageCount();

                    try { p.WaitForExit(); }
                    catch (Exception) { Debug.Assert(false); }

                    MessageService.ExternalDecrementMessageCount();
                    Program.MainForm.UIBlockInteraction(false);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(strCmd + MessageService.NewParagraph + ex.Message);
            }
            finally
            {
                try { if (p != null)
                      {
                          p.Dispose();
                      }
                }
                catch (Exception) { Debug.Assert(false); }
            }
        }