/// <summary>
 /// Visit a parse tree produced by <see cref="ShellParser.program_shorthand"/>.
 /// <para>
 /// The default implementation returns the result of calling <see cref="AbstractParseTreeVisitor{Result}.VisitChildren(IRuleNode)"/>
 /// on <paramref name="context"/>.
 /// </para>
 /// </summary>
 /// <param name="context">The parse tree.</param>
 /// <return>The visitor result.</return>
 public virtual Result VisitProgram_shorthand([NotNull] ShellParser.Program_shorthandContext context)
 {
     return(VisitChildren(context));
 }
Beispiel #2
0
        /// <summary>
        /// program_shorthand : SYM_DOLLAR ~EOL* ;
        /// </summary>
        override public Shell.Types.IShellReturnable VisitProgram_shorthand(ShellParser.Program_shorthandContext context)
        {
            string execString = tokenSource.GetText(
                new Antlr4.Runtime.Misc.Interval(
                    context.Start.StartIndex, context.Stop.StopIndex
                    )
                )
                                .Substring(1);
            List <string> argsList = execString.Split(' ').ToList();

            if (argsList.First().Length == 0)
            {
                argsList.RemoveAt(0);
            }

            List <string> nargs = new List <string>();

            // Join any arguments if they are valid substitutions
            for (int i = 0; i < argsList.Count; i++)
            {
                bool added = false;
                var  arg   = argsList[i];
                for (int j = i + 1; j < argsList.Count; j++)
                {
                    if (arg.StartsWith("\"") && argsList[j].EndsWith("\""))
                    {
                        nargs.Add(String.Join(" ", argsList.GetRange(i, j)));
                        i     = j + 1;
                        added = true;
                        break;
                    }
                    else if (arg.StartsWith('`') && argsList[j].EndsWith('`'))
                    {
                        nargs.Add(String.Join(" ", argsList.GetRange(i, j)));
                        i     = j + 1;
                        added = true;
                        break;
                    }
                }
                if (!added)
                {
                    nargs.Add(arg);
                }
            }

            argsList = nargs;

            Process process = new Process();

            process.StartInfo.FileName = argsList.First();
            argsList = argsList.Skip(1).Select(
                str => {
                // perform any substitutions
                if (str.StartsWith("\"") && str.EndsWith("\""))
                {
                    return(Utility.GetString(str, this).ToString());
                }
                else if (str.StartsWith('`') && str.EndsWith('`'))
                {
                    return(str[1..^ 1]);
                }