Ejemplo n.º 1
0
 private FileMode GetFileMode(FileRedirectionAst redirectionAst)
 {
     if (redirectionAst.Append)
     {
         return FileMode.Append;
     }
     return FileMode.Create;
 }
Ejemplo n.º 2
0
    public System.Object VisitFileRedirection(System.Management.Automation.Language.FileRedirectionAst fileRedirectionAst)
    {
        IScriptExtent mappedExtent = MapExtent(fileRedirectionAst.Extent);

        ExpressionAst mappedFile = _VisitExpression(fileRedirectionAst.Location);

        return(new FileRedirectionAst(mappedExtent, fileRedirectionAst.FromStream, mappedFile, fileRedirectionAst.Append));
    }
Ejemplo n.º 3
0
 private string GetOutputFileName(FileRedirectionAst redirectionAst)
 {
     object outputPath = _visitor.EvaluateAst(redirectionAst.Location, false);
     if (outputPath != null)
     {
         return outputPath.ToString();
     }
     return null;
 }
Ejemplo n.º 4
0
 private ObjectStream GetStream(FileRedirectionAst redirectionAst)
 {
     switch (redirectionAst.FromStream)
     {
         case RedirectionStream.Output:
             return _runtime.OutputStream;
         case RedirectionStream.Error:
             return _runtime.ErrorStream;
         default:
             throw new NotImplementedException(redirectionAst.FromStream.ToString());
     }
 }
Ejemplo n.º 5
0
        /// <summary>
        /// TODO: Output should be formatted.
        /// </summary>
        public AstVisitAction Visit(FileRedirectionAst redirectionAst)
        {
            string outputPath = GetOutputFileName(redirectionAst);
            if (outputPath == null)
            {
                return AstVisitAction.SkipChildren;
            }

            FileMode fileMode = GetFileMode(redirectionAst);
            ObjectStream stream = GetStream(redirectionAst);
            FileStream file = File.Open(outputPath, fileMode, FileAccess.Write);

            using (var streamWriter = new StreamWriter(file, Encoding.Unicode))
            {
                foreach (object obj in stream.Read())
                {
                    WriteObject(streamWriter, obj);
                }
            }
            return AstVisitAction.SkipChildren;
        }
Ejemplo n.º 6
0
 public object VisitFileRedirection(FileRedirectionAst fileRedirectionAst)
 {
     throw PSTraceSource.NewArgumentException("ast");
 }
Ejemplo n.º 7
0
 public override AstVisitAction VisitFileRedirection(FileRedirectionAst ast)
 {
     return(Check(ast));
 }
Ejemplo n.º 8
0
 public object VisitFileRedirection(FileRedirectionAst fileRedirectionAst)
 {
     fileRedirectionAst.Location.Accept(this);
     return(null);
 }
Ejemplo n.º 9
0
 public override AstVisitAction VisitFileRedirection(FileRedirectionAst redirectionAst)
 {
     throw new NotImplementedException(); //VisitFileRedirection(redirectionAst);
 }
Ejemplo n.º 10
0
        public override AstVisitAction VisitFileRedirection(FileRedirectionAst redirectionAst)
        {
            ThrowError(
                new ScriptBlockToPowerShellNotSupportedException(
                    "CanConvertOneOutputErrorRedir", null, AutomationExceptions.CanConvertOneOutputErrorRedir),
                redirectionAst);

            return AstVisitAction.Continue;
        }
Ejemplo n.º 11
0
 /// <summary/>
 public virtual object VisitFileRedirection(FileRedirectionAst fileRedirectionAst)
 {
     return _decorated.VisitFileRedirection(fileRedirectionAst);
 }
Ejemplo n.º 12
0
 public object VisitFileRedirection(FileRedirectionAst fileRedirectionAst)
 {
     fileRedirectionAst.Location.Accept(this);
     return null;
 }
Ejemplo n.º 13
0
 private RedirectionAst RedirectionRule(RedirectionToken redirectionToken, RedirectionAst[] redirections, ref IScriptExtent extent)
 {
     RedirectionAst fileRedirectionAst;
     string allStream;
     FileRedirectionToken fileRedirectionToken = redirectionToken as FileRedirectionToken;
     if (fileRedirectionToken != null || redirectionToken as InputRedirectionToken != null)
     {
         ExpressionAst singleCommandArgument = this.GetSingleCommandArgument(Parser.CommandArgumentContext.FileName);
         if (singleCommandArgument == null)
         {
             this.ReportError(Parser.After(redirectionToken), ParserStrings.MissingFileSpecification, new object[0]);
             singleCommandArgument = new ErrorExpressionAst(redirectionToken.Extent, null);
         }
         if (fileRedirectionToken != null)
         {
             fileRedirectionAst = new FileRedirectionAst(Parser.ExtentOf(fileRedirectionToken, singleCommandArgument), fileRedirectionToken.FromStream, singleCommandArgument, fileRedirectionToken.Append);
         }
         else
         {
             object[] text = new object[1];
             text[0] = redirectionToken.Text;
             this.ReportError(redirectionToken.Extent, ParserStrings.RedirectionNotSupported, text);
             extent = Parser.ExtentOf(redirectionToken, singleCommandArgument);
             return null;
         }
     }
     else
     {
         MergingRedirectionToken mergingRedirectionToken = (MergingRedirectionToken)redirectionToken;
         RedirectionStream fromStream = mergingRedirectionToken.FromStream;
         RedirectionStream toStream = mergingRedirectionToken.ToStream;
         if (toStream == RedirectionStream.Output)
         {
             if (fromStream == toStream)
             {
                 object[] objArray = new object[1];
                 objArray[0] = mergingRedirectionToken.Text;
                 this.ReportError(redirectionToken.Extent, ParserStrings.RedirectionNotSupported, objArray);
             }
         }
         else
         {
             object[] text1 = new object[1];
             text1[0] = mergingRedirectionToken.Text;
             this.ReportError(redirectionToken.Extent, ParserStrings.RedirectionNotSupported, text1);
             toStream = RedirectionStream.Output;
         }
         fileRedirectionAst = new MergingRedirectionAst(mergingRedirectionToken.Extent, mergingRedirectionToken.FromStream, toStream);
     }
     if (redirections[(int)fileRedirectionAst.FromStream] != null)
     {
         RedirectionStream redirectionStream = fileRedirectionAst.FromStream;
         if (redirectionStream == RedirectionStream.All)
         {
             allStream = ParserStrings.AllStream;
         }
         else if (redirectionStream == RedirectionStream.Output)
         {
             allStream = ParserStrings.OutputStream;
         }
         else if (redirectionStream == RedirectionStream.Error)
         {
             allStream = ParserStrings.ErrorStream;
         }
         else if (redirectionStream == RedirectionStream.Warning)
         {
             allStream = ParserStrings.WarningStream;
         }
         else if (redirectionStream == RedirectionStream.Verbose)
         {
             allStream = ParserStrings.VerboseStream;
         }
         else if (redirectionStream == RedirectionStream.Debug)
         {
             allStream = ParserStrings.DebugStream;
         }
         else if (redirectionStream == RedirectionStream.Host)
         {
             allStream = ParserStrings.HostStream;
         }
         else
         {
             throw PSTraceSource.NewArgumentOutOfRangeException("result.FromStream", (object)fileRedirectionAst.FromStream);
         }
         object[] objArray1 = new object[1];
         objArray1[0] = allStream;
         this.ReportError(fileRedirectionAst.Extent, ParserStrings.StreamAlreadyRedirected, objArray1);
     }
     else
     {
         redirections[(int)fileRedirectionAst.FromStream] = fileRedirectionAst;
     }
     extent = fileRedirectionAst.Extent;
     return fileRedirectionAst;
 }
Ejemplo n.º 14
0
        private RedirectionAst RedirectionRule(RedirectionToken redirectionToken, RedirectionAst[] redirections, ref IScriptExtent extent)
        {
            //G  redirections:
            //G      redirection
            //G      redirections   redirection
            //G  redirection:
            //G      merging-redirection-operator
            //G      file-redirection-operator   redirected-file-name
            //G  redirected-file-name:
            //G      command-argument
            //G      primary-expression

            RedirectionAst result;

            var fileRedirectionToken = redirectionToken as FileRedirectionToken;
            if (fileRedirectionToken != null || (redirectionToken is InputRedirectionToken))
            {
                // get location
                var filename = GetSingleCommandArgument(CommandArgumentContext.FileName);
                if (filename == null)
                {
                    // ErrorRecovery: Just pretend we have a filename and continue parsing.

                    ReportError(After(redirectionToken), () => ParserStrings.MissingFileSpecification);
                    filename = new ErrorExpressionAst(redirectionToken.Extent);
                }

                if (fileRedirectionToken == null)
                {
                    // Must be an input redirection
                    ReportError(redirectionToken.Extent, () => ParserStrings.RedirectionNotSupported, redirectionToken.Text);
                    extent = ExtentOf(redirectionToken, filename);
                    return null;
                }

                result = new FileRedirectionAst(ExtentOf(fileRedirectionToken, filename), fileRedirectionToken.FromStream,
                                                filename, fileRedirectionToken.Append);
            }
            else
            {
                var mergingRedirectionToken = (MergingRedirectionToken)redirectionToken;

                RedirectionStream fromStream = mergingRedirectionToken.FromStream;
                RedirectionStream toStream = mergingRedirectionToken.ToStream;
                if (toStream != RedirectionStream.Output)
                {
                    // Have we seen something like 1>&2 or 2>&3
                    // ErrorRecovery: This is just a semantic error, so no special recovery.

                    ReportError(redirectionToken.Extent, () => ParserStrings.RedirectionNotSupported, mergingRedirectionToken.Text);
                    toStream = RedirectionStream.Output;
                }
                else if (fromStream == toStream)
                {
                    // Make sure 1>&1, 2>&2, etc. is an error.
                    // ErrorRecovery: This is just a semantic error, so no special recovery.

                    ReportError(redirectionToken.Extent, () => ParserStrings.RedirectionNotSupported, mergingRedirectionToken.Text);
                }

                result = new MergingRedirectionAst(mergingRedirectionToken.Extent, mergingRedirectionToken.FromStream, toStream);
            }

            if (redirections[(int)result.FromStream] == null)
            {
                redirections[(int)result.FromStream] = result;
            }
            else
            {
                string errorStream;
                switch (result.FromStream)
                {
                    case RedirectionStream.All: errorStream = ParserStrings.AllStream; break;
                    case RedirectionStream.Output: errorStream = ParserStrings.OutputStream; break;
                    case RedirectionStream.Error: errorStream = ParserStrings.ErrorStream; break;
                    case RedirectionStream.Warning: errorStream = ParserStrings.WarningStream; break;
                    case RedirectionStream.Verbose: errorStream = ParserStrings.VerboseStream; break;
                    case RedirectionStream.Debug: errorStream = ParserStrings.DebugStream; break;
                    case RedirectionStream.Information: errorStream = ParserStrings.InformationStream; break;
                    default:
                        throw PSTraceSource.NewArgumentOutOfRangeException("result.FromStream", result.FromStream);
                }
                ReportError(result.Extent, () => ParserStrings.StreamAlreadyRedirected, errorStream);
            }

            extent = result.Extent;
            return result;
        }
Ejemplo n.º 15
0
 /// <summary/>
 public virtual object VisitFileRedirection(FileRedirectionAst fileRedirectionAst) { return null; }
Ejemplo n.º 16
0
 public object VisitFileRedirection(FileRedirectionAst fileRedirectionAst)
 {
     return(false);
 }
Ejemplo n.º 17
0
 public override AstVisitAction VisitFileRedirection(FileRedirectionAst ast) { return CheckParent(ast); }
Ejemplo n.º 18
0
 public object VisitFileRedirection(FileRedirectionAst fileRedirectionAst)
 {
     Expression expression;
     StringConstantExpressionAst location = fileRedirectionAst.Location as StringConstantExpressionAst;
     if (location != null)
     {
         expression = this.Compile(location);
     }
     else
     {
         expression = Expression.Dynamic(PSToStringBinder.Get(), typeof(string), this.CompileExpressionOperand(fileRedirectionAst.Location), _executionContextParameter);
     }
     return Expression.New(CachedReflectionInfo.FileRedirection_ctor, new Expression[] { Expression.Constant(fileRedirectionAst.FromStream), ExpressionCache.Constant(fileRedirectionAst.Append), expression });
 }
Ejemplo n.º 19
0
        /// <summary>
        /// Visit file direction
        /// </summary>
        /// <param name="fileRedirectionAst"></param>
        /// <returns></returns>
        public object VisitFileRedirection(FileRedirectionAst fileRedirectionAst)
        {
            if (fileRedirectionAst == null) return null;

            fileRedirectionAst.Location.Visit(this.Decorator);
            return null;
        }
Ejemplo n.º 20
0
 /// <summary/>
 public virtual AstVisitAction VisitFileRedirection(FileRedirectionAst redirectionAst) => DefaultVisit(redirectionAst);
Ejemplo n.º 21
0
 public override AstVisitAction VisitFileRedirection(FileRedirectionAst redirectionAst)
 {
     return new FileRedirectionVisitor(this, _pipelineCommandRuntime).Visit(redirectionAst);
 }
 public object VisitFileRedirection(FileRedirectionAst fileRedirectionAst) { throw new UnexpectedElementException(); }
Ejemplo n.º 23
0
 public override AstVisitAction VisitFileRedirection(FileRedirectionAst fileRedirectionAst)
 {
     this.ReportError(fileRedirectionAst, () => ParserStrings.RedirectionNotSupportedInDataSection, new object[0]);
     return(AstVisitAction.Continue);
 }
Ejemplo n.º 24
0
 public override AstVisitAction VisitFileRedirection(FileRedirectionAst ast)
 {
     return this.Check(ast);
 }
Ejemplo n.º 25
0
 public virtual AstVisitAction VisitFileRedirection(FileRedirectionAst redirectionAst)
 {
     return AstVisitAction.Continue;
 }
Ejemplo n.º 26
0
 public object VisitFileRedirection(FileRedirectionAst fileRedirectionAst) { return AutomationNull.Value; }
Ejemplo n.º 27
0
 /// <summary/>
 public virtual object VisitFileRedirection(FileRedirectionAst fileRedirectionAst)
 {
     return(null);
 }
Ejemplo n.º 28
0
 public object VisitFileRedirection(FileRedirectionAst fileRedirectionAst) { throw PSTraceSource.NewArgumentException("ast"); }
Ejemplo n.º 29
0
 public object VisitFileRedirection(FileRedirectionAst fileRedirectionAst)
 {
     return false;
 }
Ejemplo n.º 30
0
        public object VisitFileRedirection(FileRedirectionAst fileRedirectionAst)
        {
            Expression fileNameExpr;
            var strConst = fileRedirectionAst.Location as StringConstantExpressionAst;
            if (strConst != null)
            {
                // When the filename is a constant, we still must generate a new FileRedirection
                // at runtime because we can't keep a cached object in the closure because the object
                // is disposed after executing the command.
                fileNameExpr = Compile(strConst);
            }
            else
            {
                // The filename is not constant, so we must generate code to evaluate the filename at runtime.
                fileNameExpr = DynamicExpression.Dynamic(PSToStringBinder.Get(), typeof(string),
                                                         CompileExpressionOperand(fileRedirectionAst.Location), _executionContextParameter);
            }

            return Expression.New(CachedReflectionInfo.FileRedirection_ctor,
                                  Expression.Constant(fileRedirectionAst.FromStream),
                                  ExpressionCache.Constant(fileRedirectionAst.Append),
                                  fileNameExpr);
        }
Ejemplo n.º 31
0
 /// <summary/>
 public virtual AstVisitAction VisitFileRedirection(FileRedirectionAst redirectionAst)
 {
     return(AstVisitAction.Continue);
 }
Ejemplo n.º 32
0
 public override AstVisitAction VisitFileRedirection(FileRedirectionAst fileRedirectionAst)
 {
     this.ReportError(fileRedirectionAst, () => ParserStrings.RedirectionNotSupportedInDataSection, new object[0]);
     return AstVisitAction.Continue;
 }
Ejemplo n.º 33
0
 public object VisitFileRedirection(FileRedirectionAst fileRedirectionAst)
 {
     return(AutomationNull.Value);
 }
 public object VisitFileRedirection(FileRedirectionAst fileRedirectionAst)
 {
     throw new NotImplementedException();
 }