Esempio n. 1
0
 /// <summary>
 /// Copy constructor to support cloning
 /// </summary>
 /// <param name="history"></param>
 private HistoryInfo(HistoryInfo history)
 {
     _id = history._id;
     _pipelineId = history._pipelineId;
     _cmdline = history._cmdline;
     _status = history._status;
     _startTime = history._startTime;
     _endTime = history._endTime;
     _cleared = history._cleared;
 }
Esempio n. 2
0
 private long Add(HistoryInfo entry)
 {
     if (entry == null)
     {
         throw PSTraceSource.NewArgumentNullException("entry");
     }
     this._buffer[this.GetIndexForNewEntry()] = entry;
     this._countEntriesAdded += 1L;
     entry.SetId(this._countEntriesAdded);
     this.IncrementCountOfEntriesInBuffer();
     return this._countEntriesAdded;
 }
Esempio n. 3
0
 internal long AddEntry(long pipelineId, string cmdline, PipelineState status, DateTime startTime, DateTime endTime, bool skipIfLocked)
 {
     long num;
     if (!Monitor.TryEnter(this._syncRoot, skipIfLocked ? 0 : -1))
     {
         return -1L;
     }
     try
     {
         this.ReallocateBufferIfNeeded();
         HistoryInfo entry = new HistoryInfo(pipelineId, cmdline, status, startTime, endTime);
         num = this.Add(entry);
     }
     finally
     {
         Monitor.Exit(this._syncRoot);
     }
     return num;
 }
Esempio n. 4
0
 internal HistoryInfo[] GetEntries(long id, long count, SwitchParameter newest)
 {
     using (History._trace.TraceMethod())
     {
         this.ReallocateBufferIfNeeded();
         if (count < -1L)
         {
             throw History._trace.NewArgumentOutOfRangeException(nameof(count), (object)count);
         }
         if (newest.ToString() == null)
         {
             throw History._trace.NewArgumentNullException(nameof(newest));
         }
         if (count == -1L || count > this._countEntriesAdded || count > (long)this._countEntriesInBuffer)
         {
             count = (long)this._countEntriesInBuffer;
         }
         if (count == 0L || this._countEntriesInBuffer == 0)
         {
             return(new HistoryInfo[0]);
         }
         lock (this._syncRoot)
         {
             ArrayList arrayList = new ArrayList();
             if (id > 0L)
             {
                 long num1 = id;
                 if (!newest.IsPresent)
                 {
                     long num2 = num1 - count + 1L;
                     if (num2 < 1L)
                     {
                         num2 = 1L;
                     }
                     for (long id1 = num1; id1 >= num2 && num2 > 1L; --id1)
                     {
                         if (this._buffer[this.GetIndexFromId(id1)] != null && this._buffer[this.GetIndexFromId(id1)].Cleared)
                         {
                             --num2;
                         }
                     }
                     for (long id1 = num2; id1 <= num1; ++id1)
                     {
                         if (this._buffer[this.GetIndexFromId(id1)] != null && !this._buffer[this.GetIndexFromId(id1)].Cleared)
                         {
                             arrayList.Add((object)this._buffer[this.GetIndexFromId(id1)].Clone());
                         }
                     }
                 }
                 else
                 {
                     long num2 = num1 + count - 1L;
                     if (num2 >= this._countEntriesAdded)
                     {
                         num2 = this._countEntriesAdded;
                     }
                     for (long id1 = num1; id1 <= num2 && num2 < this._countEntriesAdded; ++id1)
                     {
                         if (this._buffer[this.GetIndexFromId(id1)] != null && this._buffer[this.GetIndexFromId(id1)].Cleared)
                         {
                             ++num2;
                         }
                     }
                     for (long id1 = num2; id1 >= num1; --id1)
                     {
                         if (this._buffer[this.GetIndexFromId(id1)] != null && !this._buffer[this.GetIndexFromId(id1)].Cleared)
                         {
                             arrayList.Add((object)this._buffer[this.GetIndexFromId(id1)].Clone());
                         }
                     }
                 }
             }
             else
             {
                 long num1 = 0;
                 if (this._capacity != 64)
                 {
                     num1 = this.SmallestIDinBuffer();
                 }
                 if (!newest.IsPresent)
                 {
                     long id1 = 1;
                     if (this._capacity != 64 && this._countEntriesAdded > (long)this._capacity)
                     {
                         id1 = num1;
                     }
                     long num2 = count - 1L;
                     while (num2 >= 0L && id1 <= this._countEntriesAdded)
                     {
                         if (this._buffer[this.GetIndexFromId(id1)].Cleared)
                         {
                             ++id1;
                         }
                         else
                         {
                             arrayList.Add((object)this._buffer[this.GetIndexFromId(id1)].Clone());
                             --num2;
                             ++id1;
                         }
                     }
                 }
                 else
                 {
                     long countEntriesAdded = this._countEntriesAdded;
                     long num2 = count - 1L;
                     while (num2 >= 0L && (this._capacity == 64 || this._countEntriesAdded <= (long)this._capacity || countEntriesAdded >= num1) && countEntriesAdded >= 1L)
                     {
                         if (this._buffer[this.GetIndexFromId(countEntriesAdded)].Cleared)
                         {
                             --countEntriesAdded;
                         }
                         else
                         {
                             arrayList.Add((object)this._buffer[this.GetIndexFromId(countEntriesAdded)].Clone());
                             --num2;
                             --countEntriesAdded;
                         }
                     }
                 }
             }
             HistoryInfo[] historyInfoArray = new HistoryInfo[arrayList.Count];
             arrayList.CopyTo((Array)historyInfoArray);
             return(historyInfoArray);
         }
     }
 }
Esempio n. 5
0
 private void ReallocateBufferIfNeeded()
 {
     int historySize = this.GetHistorySize();
     if (historySize != this._capacity)
     {
         HistoryInfo[] infoArray = new HistoryInfo[historySize];
         int num2 = this._countEntriesInBuffer;
         if (num2 < this._countEntriesAdded)
         {
             num2 = (int) this._countEntriesAdded;
         }
         if (this._countEntriesInBuffer > historySize)
         {
             num2 = historySize;
         }
         for (int i = num2; i > 0; i--)
         {
             long id = (this._countEntriesAdded - i) + 1L;
             infoArray[GetIndexFromId(id, historySize)] = this._buffer[this.GetIndexFromId(id)];
         }
         this._countEntriesInBuffer = num2;
         this._capacity = historySize;
         this._buffer = infoArray;
     }
 }
Esempio n. 6
0
 /// <summary>
 /// Invoke-history is replaced in history by the command it executed.
 /// This replacement happens only if Invoke-History is single element
 /// in the pipeline. If there are more than one element in pipeline 
 /// (ex A | Invoke-History 2 | B) then we cannot do this replacement.
 /// </summary>
 private void ReplaceHistoryString(HistoryInfo entry)
 {
     //Get the current pipeline
     LocalPipeline pipeline = (LocalPipeline)((LocalRunspace)Context.CurrentRunspace).GetCurrentlyRunningPipeline();
     if (pipeline.AddToHistory)
     {
         pipeline.HistoryString = entry.CommandLine;
     }
 }
Esempio n. 7
0
 internal void RemoveFromInvokeHistoryEntryList(HistoryInfo entry)
 {
     this._invokeHistoryIds.Remove(entry.Id);
 }
Esempio n. 8
0
        internal static ArrayList GetSuggestion(HistoryInfo lastHistory, Object lastError, ArrayList errorList)
        {
            ArrayList returnSuggestions = new ArrayList();

            PSModuleInfo invocationModule = new PSModuleInfo(true);
            invocationModule.SessionState.PSVariable.Set("lastHistory", lastHistory);
            invocationModule.SessionState.PSVariable.Set("lastError", lastError);

            int initialErrorCount = 0;

            // Go through all of the suggestions
            foreach (Hashtable suggestion in s_suggestions)
            {
                initialErrorCount = errorList.Count;

                // Make sure the rule is enabled
                if (!LanguagePrimitives.IsTrue(suggestion["Enabled"]))
                    continue;

                SuggestionMatchType matchType = (SuggestionMatchType)LanguagePrimitives.ConvertTo(
                    suggestion["MatchType"],
                    typeof(SuggestionMatchType),
                    CultureInfo.InvariantCulture);

                // If this is a dynamic match, evaluate the ScriptBlock
                if (matchType == SuggestionMatchType.Dynamic)
                {
                    object result = null;

                    ScriptBlock evaluator = suggestion["Rule"] as ScriptBlock;
                    if (evaluator == null)
                    {
                        suggestion["Enabled"] = false;

                        throw new ArgumentException(
                            SuggestionStrings.RuleMustBeScriptBlock, "Rule");
                    }

                    try
                    {
                        result = invocationModule.Invoke(evaluator, null);
                    }
                    catch (Exception e)
                    {
                        // Catch-all OK. This is a third-party call-out.
                        CommandProcessorBase.CheckForSevereException(e);

                        suggestion["Enabled"] = false;
                        continue;
                    }

                    // If it returned results, evaluate its suggestion
                    if (LanguagePrimitives.IsTrue(result))
                    {
                        string suggestionText = GetSuggestionText(suggestion["Suggestion"], (object[])suggestion["SuggestionArgs"], invocationModule);

                        if (!String.IsNullOrEmpty(suggestionText))
                        {
                            string returnString = String.Format(
                                CultureInfo.CurrentCulture,
                                "Suggestion [{0},{1}]: {2}",
                                (int)suggestion["Id"],
                                (string)suggestion["Category"],
                                suggestionText);

                            returnSuggestions.Add(returnString);
                        }
                    }
                }
                else
                {
                    string matchText = String.Empty;

                    // Otherwise, this is a Regex match against the
                    // command or error
                    if (matchType == SuggestionMatchType.Command)
                    {
                        matchText = lastHistory.CommandLine;
                    }
                    else if (matchType == SuggestionMatchType.Error)
                    {
                        if (lastError != null)
                        {
                            Exception lastException = lastError as Exception;
                            if (lastException != null)
                            {
                                matchText = lastException.Message;
                            }
                            else
                            {
                                matchText = lastError.ToString();
                            }
                        }
                    }
                    else
                    {
                        suggestion["Enabled"] = false;

                        throw new ArgumentException(
                            SuggestionStrings.InvalidMatchType,
                            "MatchType");
                    }

                    // If the text matches, evaluate the suggestion
                    if (Regex.IsMatch(matchText, (string)suggestion["Rule"], RegexOptions.IgnoreCase))
                    {
                        string suggestionText = GetSuggestionText(suggestion["Suggestion"], (object[])suggestion["SuggestionArgs"], invocationModule);

                        if (!String.IsNullOrEmpty(suggestionText))
                        {
                            string returnString = String.Format(
                                CultureInfo.CurrentCulture,
                                "Suggestion [{0},{1}]: {2}",
                                (int)suggestion["Id"],
                                (string)suggestion["Category"],
                                suggestionText);

                            returnSuggestions.Add(returnString);
                        }
                    }
                }

                // If the rule generated an error, disable it
                if (errorList.Count != initialErrorCount)
                {
                    suggestion["Enabled"] = false;
                }
            }


            return returnSuggestions;
        }
Esempio n. 9
0
        /// <summary>
        /// Adds an entry to the buffer. If buffer is full, overwrites
        /// oldest entry in the buffer
        /// </summary>
        /// <param name="entry"></param>
        /// <returns>Returns id for the entry. This id should be used to fetch
        /// the entry from the buffer</returns>
        /// <remarks>Id starts from 1 and is incremented by 1 for each new entry</remarks>

        private long Add(HistoryInfo entry)
        {
            if (entry == null)
            {
                throw PSTraceSource.NewArgumentNullException("entry");
            }

            _buffer[GetIndexForNewEntry()] = entry;

            //Increment count of entries added so far
            _countEntriesAdded++;

            //Id of an entry in history is same as its number in history store.
            entry.SetId(_countEntriesAdded);

            //Increment count of entries in buffer by 1
            IncrementCountOfEntriesInBuffer();

            return _countEntriesAdded;
        }
Esempio n. 10
0
        }// end function


        /// <summary>
        /// Get History Entries based on the WildCard Pattern value.
        /// If passed 0, returns all the values, else return on the basis of count.
        /// </summary>
        /// <param name="wildcardpattern"></param>
        /// <param name="count"></param>
        /// <param name="newest"></param>
        /// <returns></returns>
        internal HistoryInfo[] GetEntries(WildcardPattern wildcardpattern, long count, SwitchParameter newest)
        {
            lock (_syncRoot)
            {
                if (count < -1)
                {
                    throw PSTraceSource.NewArgumentOutOfRangeException("count", count);
                }
                if (newest.ToString() == null)
                {
                    throw PSTraceSource.NewArgumentNullException("newest");
                }
                if (count > _countEntriesAdded || count == -1)
                {
                    count = _countEntriesInBuffer;
                }
                List<HistoryInfo> cmdlist = new List<HistoryInfo>();
                long SmallestID = 1;
                //if buffersize is changes,Get the smallest entry that's not cleared in the buffer
                if (_capacity != DefaultHistorySize)
                    SmallestID = SmallestIDinBuffer();
                if (count != 0)
                {
                    if (!newest.IsPresent)
                    {
                        long id = 1;
                        if (_capacity != DefaultHistorySize)
                        {
                            if (_countEntriesAdded > _capacity)
                                id = SmallestID;
                        }
                        for (long i = 0; i <= count - 1;)
                        {
                            if (id > _countEntriesAdded) break;
                            if (_buffer[GetIndexFromId(id)].Cleared == false && wildcardpattern.IsMatch(_buffer[GetIndexFromId(id)].CommandLine.Trim()))
                            {
                                cmdlist.Add(_buffer[GetIndexFromId(id)].Clone()); i++;
                            }
                            id++;
                        }
                    }
                    else
                    {
                        long id = _countEntriesAdded;
                        for (long i = 0; i <= count - 1;)
                        {
                            //if buffersize is changed,we have to loop from max entry to min entry thats not cleared
                            if (_capacity != DefaultHistorySize)
                            {
                                if (_countEntriesAdded > _capacity)
                                {
                                    if (id < SmallestID)
                                        break;
                                }
                            }
                            if (id < 1) break;
                            if (_buffer[GetIndexFromId(id)].Cleared == false && wildcardpattern.IsMatch(_buffer[GetIndexFromId(id)].CommandLine.Trim()))
                            {
                                cmdlist.Add(_buffer[GetIndexFromId(id)].Clone()); i++;
                            }
                            id--;
                        }
                    }
                }
                else
                {
                    for (long i = 1; i <= _countEntriesAdded; i++)
                    {
                        if (_buffer[GetIndexFromId(i)].Cleared == false && wildcardpattern.IsMatch(_buffer[GetIndexFromId(i)].CommandLine.Trim()))
                        {
                            cmdlist.Add(_buffer[GetIndexFromId(i)].Clone());
                        }
                    }
                }
                HistoryInfo[] entries = new HistoryInfo[cmdlist.Count];
                cmdlist.CopyTo(entries);
                return entries;
            }
        }
Esempio n. 11
0
        /// <summary>
        /// Get count HistoryEntries 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="count"></param>
        /// <param name="newest"></param>        
        /// <returns>history entries</returns>
        internal HistoryInfo[] GetEntries(long id, long count, SwitchParameter newest)
        {
            ReallocateBufferIfNeeded();

            if (count < -1)
            {
                throw PSTraceSource.NewArgumentOutOfRangeException("count", count);
            }

            if (newest.ToString() == null)
            {
                throw PSTraceSource.NewArgumentNullException("newest");
            }


            if (count == -1 || count > _countEntriesAdded || count > _countEntriesInBuffer)
                count = _countEntriesInBuffer;

            if (count == 0 || _countEntriesInBuffer == 0)
            {
                return Utils.EmptyArray<HistoryInfo>();
            }

            lock (_syncRoot)
            {
                //Using list instead of an array to store the entries.With array we are getting null values
                //when the historybuffer size is changed
                List<HistoryInfo> entriesList = new List<HistoryInfo>();
                if (id > 0)
                {
                    long firstId, baseId;
                    baseId = id;

                    //get id,count,newest values
                    if (!newest.IsPresent)
                    {
                        //get older entries

                        //Calculate the first id (i.e lowest id to fetch)
                        firstId = baseId - count + 1;

                        //If first id is less than the lowest id in history store,
                        //assign lowest id as first ID
                        if (firstId < 1)
                        {
                            firstId = 1;
                        }

                        for (long i = baseId; i >= firstId; --i)
                        {
                            if (firstId <= 1) break;
                            // if entry is null , continue the loop with the next entry
                            if (_buffer[GetIndexFromId(i)] == null) continue;
                            if (_buffer[GetIndexFromId(i)].Cleared == true)
                            {
                                // we have to clear count entries before an id, so if an entry is null,decrement 
                                // first id as long as its is greater than the lowest entry in the buffer.
                                firstId--;
                                continue;
                            }
                        }

                        for (long i = firstId; i <= baseId; ++i)
                        {
                            //if an entry is null after being cleared by clear-history cmdlet,
                            //continue with the next entry
                            if (_buffer[GetIndexFromId(i)] == null || _buffer[GetIndexFromId(i)].Cleared == true)
                                continue;
                            entriesList.Add(_buffer[GetIndexFromId(i)].Clone());
                        }
                    }
                    else
                    { //get latest entries
                        // first id becomes the id +count no of entries from the end of the buffer
                        firstId = baseId + count - 1;
                        // if first id is more than the no of entries in the buffer, first id will be the last entry in the buffer
                        if (firstId >= _countEntriesAdded)
                        {
                            firstId = _countEntriesAdded;
                        }

                        for (long i = baseId; i <= firstId; i++)
                        {
                            if (firstId >= _countEntriesAdded) break;
                            // if entry is null , continue the loop with the next entry
                            if (_buffer[GetIndexFromId(i)] == null) continue;
                            if (_buffer[GetIndexFromId(i)].Cleared == true)
                            {
                                // we have to clear count entries before an id, so if an entry is null,increment  first id 
                                firstId++;
                                continue;
                            }
                        }

                        for (long i = firstId; i >= baseId; --i)
                        {
                            //if an entry is null after being cleared by clear-history cmdlet,
                            //continue with the next entry
                            if (_buffer[GetIndexFromId(i)] == null || _buffer[GetIndexFromId(i)].Cleared == true)
                                continue;
                            entriesList.Add(_buffer[GetIndexFromId(i)].Clone());
                        }
                    }
                }
                else
                {
                    //get entries for count,newest                                               

                    long index, SmallestID = 0;
                    //if we change the defaulthistory size and when no of entries exceed the size, then 
                    //we need to get the smallest entry in the buffer when we want to clear the oldest entry
                    //eg if size is 5 and then the entries can be 7,6,1,2,3
                    if (_capacity != DefaultHistorySize)
                        SmallestID = SmallestIDinBuffer();
                    if (!newest.IsPresent)
                    {
                        //get oldest count entries
                        index = 1;
                        if (_capacity != DefaultHistorySize)
                        {
                            if (_countEntriesAdded > _capacity)
                                index = SmallestID;
                        }
                        for (long i = count - 1; i >= 0;)
                        {
                            if (index > _countEntriesAdded) break;
                            if ((index <= 0 || GetIndexFromId(index) >= _buffer.Length) ||
                                (_buffer[GetIndexFromId(index)].Cleared == true))
                            {
                                index++; continue;
                            }
                            else
                            {
                                entriesList.Add(_buffer[GetIndexFromId(index)].Clone());
                                i--; index++;
                            }
                        }
                    }
                    else
                    {
                        index = _countEntriesAdded;//SmallestIDinBuffer

                        for (long i = count - 1; i >= 0;)
                        {
                            // if an entry is cleared continue to the next entry
                            if (_capacity != DefaultHistorySize)
                            {
                                if (_countEntriesAdded > _capacity)
                                {
                                    if (index < SmallestID)
                                        break;
                                }
                            }
                            if (index < 1) break;
                            if ((index <= 0 || GetIndexFromId(index) >= _buffer.Length) ||
                                (_buffer[GetIndexFromId(index)].Cleared == true))
                            { index--; continue; }
                            else
                            {
                                //clone the entry from the history buffer
                                entriesList.Add(_buffer[GetIndexFromId(index)].Clone());
                                i--; index--;
                            }
                        }
                    }
                }
                HistoryInfo[] entries = new HistoryInfo[entriesList.Count];
                entriesList.CopyTo(entries);
                return entries;
            }// end lock
        }// end function
Esempio n. 12
0
        /// <summary>
        /// Create a new history entry
        /// </summary>
        /// <param name="pipelineId"></param>
        /// <param name="cmdline"></param>
        /// <param name="status"></param>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <param name="skipIfLocked">If true, the entry will not be added when the history is locked</param>
        /// <returns>id for the new created entry. Use this id to fetch the 
        /// entry. Returns -1 if the entry is not added</returns>
        /// <remarks>This function is thread safe</remarks>
        internal long AddEntry(long pipelineId, string cmdline, PipelineState status, DateTime startTime, DateTime endTime, bool skipIfLocked)
        {
            if (!System.Threading.Monitor.TryEnter(_syncRoot, skipIfLocked ? 0 : System.Threading.Timeout.Infinite))
            {
                return -1;
            }

            try
            {
                ReallocateBufferIfNeeded();

                HistoryInfo entry = new HistoryInfo(pipelineId, cmdline, status, startTime, endTime);
                return Add(entry);
            }
            finally
            {
                System.Threading.Monitor.Exit(_syncRoot);
            }
        }
Esempio n. 13
0
        /// <summary>
        /// Constructs history store
        /// </summary>

        internal History(ExecutionContext context)
        {
            //Create history size variable. Add ValidateRangeAttribute to 
            //validate the range.
            Collection<Attribute> attrs = new Collection<Attribute>();
            attrs.Add(new ValidateRangeAttribute(1, (int)Int16.MaxValue));
            PSVariable historySizeVar = new PSVariable(SpecialVariables.HistorySize, DefaultHistorySize, ScopedItemOptions.None, attrs);
            historySizeVar.Description = SessionStateStrings.MaxHistoryCountDescription;

            context.EngineSessionState.SetVariable(historySizeVar, false, CommandOrigin.Internal);

            _capacity = DefaultHistorySize;
            _buffer = new HistoryInfo[_capacity];
        }
Esempio n. 14
0
 internal HistoryInfo[] GetEntries(
     WildcardPattern wildcardpattern,
     long count,
     SwitchParameter newest)
 {
     using (History._trace.TraceMethod())
     {
         lock (this._syncRoot)
         {
             if (count < -1L)
             {
                 throw History._trace.NewArgumentOutOfRangeException(nameof(count), (object)count);
             }
             if (newest.ToString() == null)
             {
                 throw History._trace.NewArgumentNullException(nameof(newest));
             }
             if (count > this._countEntriesAdded || count == -1L)
             {
                 count = (long)this._countEntriesInBuffer;
             }
             ArrayList arrayList = new ArrayList();
             long      num       = 1;
             if (this._capacity != 64)
             {
                 num = this.SmallestIDinBuffer();
             }
             if (count != 0L)
             {
                 if (!newest.IsPresent)
                 {
                     long id = 1;
                     if (this._capacity != 64 && this._countEntriesAdded > (long)this._capacity)
                     {
                         id = num;
                     }
                     for (long index = 0; index <= count - 1L && id <= this._countEntriesAdded; ++id)
                     {
                         if (!this._buffer[this.GetIndexFromId(id)].Cleared && wildcardpattern.IsMatch(this._buffer[this.GetIndexFromId(id)].CommandLine.Trim()))
                         {
                             arrayList.Add((object)this._buffer[this.GetIndexFromId(id)].Clone());
                             ++index;
                         }
                     }
                 }
                 else
                 {
                     long countEntriesAdded = this._countEntriesAdded;
                     for (long index = 0; index <= count - 1L && (this._capacity == 64 || this._countEntriesAdded <= (long)this._capacity || countEntriesAdded >= num) && countEntriesAdded >= 1L; --countEntriesAdded)
                     {
                         if (!this._buffer[this.GetIndexFromId(countEntriesAdded)].Cleared && wildcardpattern.IsMatch(this._buffer[this.GetIndexFromId(countEntriesAdded)].CommandLine.Trim()))
                         {
                             arrayList.Add((object)this._buffer[this.GetIndexFromId(countEntriesAdded)].Clone());
                             ++index;
                         }
                     }
                 }
             }
             else
             {
                 for (long id = 1; id <= this._countEntriesAdded; ++id)
                 {
                     if (!this._buffer[this.GetIndexFromId(id)].Cleared && wildcardpattern.IsMatch(this._buffer[this.GetIndexFromId(id)].CommandLine.Trim()))
                     {
                         arrayList.Add((object)this._buffer[this.GetIndexFromId(id)].Clone());
                     }
                 }
             }
             HistoryInfo[] historyInfoArray = new HistoryInfo[arrayList.Count];
             arrayList.CopyTo((Array)historyInfoArray);
             return(historyInfoArray);
         }
     }
 }
Esempio n. 15
0
        /// <summary>
        /// Reallocates the buffer if history size changed
        /// </summary>
        private void ReallocateBufferIfNeeded()
        {
            //Get current value of histoysize variable
            int historySize = GetHistorySize();

            if (historySize == _capacity)
                return;

            HistoryInfo[] tempBuffer = new HistoryInfo[historySize];

            //Calculate number of entries to copy in new buffer.                
            int numberOfEntries = _countEntriesInBuffer;

            //when buffer size is changed,we have to consider the totalnumber of entries added 
            if (numberOfEntries < _countEntriesAdded)
                numberOfEntries = (int)_countEntriesAdded;

            if (_countEntriesInBuffer > historySize)
                numberOfEntries = historySize;

            for (int i = numberOfEntries; i > 0; --i)
            {
                long nextId = _countEntriesAdded - i + 1;

                tempBuffer[GetIndexFromId(nextId, historySize)] = _buffer[GetIndexFromId(nextId)];
            }

            _countEntriesInBuffer = numberOfEntries;
            _capacity = historySize;
            _buffer = tempBuffer;
        }
Esempio n. 16
0
        internal static ArrayList GetSuggestion(HistoryInfo lastHistory, object lastError, ArrayList errorList)
        {
            ArrayList list = new ArrayList();
            PSModuleInfo invocationModule = new PSModuleInfo(true);
            invocationModule.SessionState.PSVariable.Set("lastHistory", lastHistory);
            invocationModule.SessionState.PSVariable.Set("lastError", lastError);
            int count = 0;
            foreach (Hashtable hashtable in suggestions)
            {
                count = errorList.Count;
                if (LanguagePrimitives.IsTrue(hashtable["Enabled"]))
                {
                    SuggestionMatchType type = (SuggestionMatchType) LanguagePrimitives.ConvertTo(hashtable["MatchType"], typeof(SuggestionMatchType), CultureInfo.InvariantCulture);
                    if (type == SuggestionMatchType.Dynamic)
                    {
                        object obj2 = null;
                        ScriptBlock sb = hashtable["Rule"] as ScriptBlock;
                        if (sb == null)
                        {
                            hashtable["Enabled"] = false;
                            throw new ArgumentException(SuggestionStrings.RuleMustBeScriptBlock, "Rule");
                        }
                        try
                        {
                            obj2 = invocationModule.Invoke(sb, null);
                        }
                        catch (Exception exception)
                        {
                            CommandProcessorBase.CheckForSevereException(exception);
                            hashtable["Enabled"] = false;
                            continue;
                        }
                        if (LanguagePrimitives.IsTrue(obj2))
                        {
                            string suggestionText = GetSuggestionText(hashtable["Suggestion"], invocationModule);
                            if (!string.IsNullOrEmpty(suggestionText))
                            {
                                string str2 = string.Format(Thread.CurrentThread.CurrentCulture, "Suggestion [{0},{1}]: {2}", new object[] { (int) hashtable["Id"], (string) hashtable["Category"], suggestionText });
                                list.Add(str2);
                            }
                        }
                    }
                    else
                    {
                        string input = string.Empty;
                        switch (type)
                        {
                            case SuggestionMatchType.Command:
                                input = lastHistory.CommandLine;
                                break;

                            case SuggestionMatchType.Error:
                                if (lastError != null)
                                {
                                    Exception exception2 = lastError as Exception;
                                    if (exception2 != null)
                                    {
                                        input = exception2.Message;
                                    }
                                    else
                                    {
                                        input = lastError.ToString();
                                    }
                                }
                                break;

                            default:
                                hashtable["Enabled"] = false;
                                throw new ArgumentException(SuggestionStrings.InvalidMatchType, "MatchType");
                        }
                        if (Regex.IsMatch(input, (string) hashtable["Rule"], RegexOptions.IgnoreCase))
                        {
                            string str4 = GetSuggestionText(hashtable["Suggestion"], invocationModule);
                            if (!string.IsNullOrEmpty(str4))
                            {
                                string str5 = string.Format(Thread.CurrentThread.CurrentCulture, "Suggestion [{0},{1}]: {2}", new object[] { (int) hashtable["Id"], (string) hashtable["Category"], str4 });
                                list.Add(str5);
                            }
                        }
                    }
                    if (errorList.Count != count)
                    {
                        hashtable["Enabled"] = false;
                    }
                }
            }
            return list;
        }
Esempio n. 17
0
 internal HistoryInfo[] GetEntries(long id, long count, SwitchParameter newest)
 {
     this.ReallocateBufferIfNeeded();
     if (count < -1L)
     {
         throw PSTraceSource.NewArgumentOutOfRangeException("count", count);
     }
     if (newest.ToString() == null)
     {
         throw PSTraceSource.NewArgumentNullException("newest");
     }
     if (((count == -1L) || (count > this._countEntriesAdded)) || (count > this._countEntriesInBuffer))
     {
         count = this._countEntriesInBuffer;
     }
     if ((count == 0L) || (this._countEntriesInBuffer == 0))
     {
         return new HistoryInfo[0];
     }
     lock (this._syncRoot)
     {
         ArrayList list = new ArrayList();
         if (id > 0L)
         {
             long num;
             long num2 = id;
             if (!newest.IsPresent)
             {
                 num = (num2 - count) + 1L;
                 if (num < 1L)
                 {
                     num = 1L;
                 }
                 for (long i = num2; i >= num; i -= 1L)
                 {
                     if (num <= 1L)
                     {
                         break;
                     }
                     if ((this._buffer[this.GetIndexFromId(i)] != null) && this._buffer[this.GetIndexFromId(i)].Cleared)
                     {
                         num -= 1L;
                     }
                 }
                 for (long j = num; j <= num2; j += 1L)
                 {
                     if ((this._buffer[this.GetIndexFromId(j)] != null) && !this._buffer[this.GetIndexFromId(j)].Cleared)
                     {
                         list.Add(this._buffer[this.GetIndexFromId(j)].Clone());
                     }
                 }
             }
             else
             {
                 num = (num2 + count) - 1L;
                 if (num >= this._countEntriesAdded)
                 {
                     num = this._countEntriesAdded;
                 }
                 for (long k = num2; k <= num; k += 1L)
                 {
                     if (num >= this._countEntriesAdded)
                     {
                         break;
                     }
                     if ((this._buffer[this.GetIndexFromId(k)] != null) && this._buffer[this.GetIndexFromId(k)].Cleared)
                     {
                         num += 1L;
                     }
                 }
                 for (long m = num; m >= num2; m -= 1L)
                 {
                     if ((this._buffer[this.GetIndexFromId(m)] != null) && !this._buffer[this.GetIndexFromId(m)].Cleared)
                     {
                         list.Add(this._buffer[this.GetIndexFromId(m)].Clone());
                     }
                 }
             }
         }
         else
         {
             long num7;
             long num8 = 0L;
             if (this._capacity != 0x1000)
             {
                 num8 = this.SmallestIDinBuffer();
             }
             if (!newest.IsPresent)
             {
                 num7 = 1L;
                 if ((this._capacity != 0x1000) && (this._countEntriesAdded > this._capacity))
                 {
                     num7 = num8;
                 }
                 long num9 = count - 1L;
                 while (num9 >= 0L)
                 {
                     if (num7 > this._countEntriesAdded)
                     {
                         break;
                     }
                     if (((num7 <= 0L) || (this.GetIndexFromId(num7) >= this._buffer.Length)) || this._buffer[this.GetIndexFromId(num7)].Cleared)
                     {
                         num7 += 1L;
                     }
                     else
                     {
                         list.Add(this._buffer[this.GetIndexFromId(num7)].Clone());
                         num9 -= 1L;
                         num7 += 1L;
                     }
                 }
             }
             else
             {
                 num7 = this._countEntriesAdded;
                 long num10 = count - 1L;
                 while (num10 >= 0L)
                 {
                     if ((((this._capacity != 0x1000) && (this._countEntriesAdded > this._capacity)) && (num7 < num8)) || (num7 < 1L))
                     {
                         break;
                     }
                     if (((num7 <= 0L) || (this.GetIndexFromId(num7) >= this._buffer.Length)) || this._buffer[this.GetIndexFromId(num7)].Cleared)
                     {
                         num7 -= 1L;
                     }
                     else
                     {
                         list.Add(this._buffer[this.GetIndexFromId(num7)].Clone());
                         num10 -= 1L;
                         num7 -= 1L;
                     }
                 }
             }
         }
         HistoryInfo[] array = new HistoryInfo[list.Count];
         list.CopyTo(array);
         return array;
     }
 }
Esempio n. 18
0
 internal bool PresentInInvokeHistoryEntryList(HistoryInfo entry)
 {
     return this._invokeHistoryIds.Contains(entry.Id);
 }
Esempio n. 19
0
 internal HistoryInfo[] GetEntries(WildcardPattern wildcardpattern, long count, SwitchParameter newest)
 {
     lock (this._syncRoot)
     {
         if (count < -1L)
         {
             throw PSTraceSource.NewArgumentOutOfRangeException("count", count);
         }
         if (newest.ToString() == null)
         {
             throw PSTraceSource.NewArgumentNullException("newest");
         }
         if ((count > this._countEntriesAdded) || (count == -1L))
         {
             count = this._countEntriesInBuffer;
         }
         ArrayList list = new ArrayList();
         long num = 1L;
         if (this._capacity != 0x1000)
         {
             num = this.SmallestIDinBuffer();
         }
         if (count != 0L)
         {
             if (!newest.IsPresent)
             {
                 long id = 1L;
                 if ((this._capacity != 0x1000) && (this._countEntriesAdded > this._capacity))
                 {
                     id = num;
                 }
                 long num3 = 0L;
                 while (num3 <= (count - 1L))
                 {
                     if (id > this._countEntriesAdded)
                     {
                         break;
                     }
                     if (!this._buffer[this.GetIndexFromId(id)].Cleared && wildcardpattern.IsMatch(this._buffer[this.GetIndexFromId(id)].CommandLine.Trim()))
                     {
                         list.Add(this._buffer[this.GetIndexFromId(id)].Clone());
                         num3 += 1L;
                     }
                     id += 1L;
                 }
             }
             else
             {
                 long num4 = this._countEntriesAdded;
                 long num5 = 0L;
                 while (num5 <= (count - 1L))
                 {
                     if ((((this._capacity != 0x1000) && (this._countEntriesAdded > this._capacity)) && (num4 < num)) || (num4 < 1L))
                     {
                         break;
                     }
                     if (!this._buffer[this.GetIndexFromId(num4)].Cleared && wildcardpattern.IsMatch(this._buffer[this.GetIndexFromId(num4)].CommandLine.Trim()))
                     {
                         list.Add(this._buffer[this.GetIndexFromId(num4)].Clone());
                         num5 += 1L;
                     }
                     num4 -= 1L;
                 }
             }
         }
         else
         {
             for (long i = 1L; i <= this._countEntriesAdded; i += 1L)
             {
                 if (!this._buffer[this.GetIndexFromId(i)].Cleared && wildcardpattern.IsMatch(this._buffer[this.GetIndexFromId(i)].CommandLine.Trim()))
                 {
                     list.Add(this._buffer[this.GetIndexFromId(i)].Clone());
                 }
             }
         }
         HistoryInfo[] array = new HistoryInfo[list.Count];
         list.CopyTo(array);
         return array;
     }
 }
Esempio n. 20
0
 internal void AddToInvokeHistoryEntryList(HistoryInfo entry)
 {
     this._invokeHistoryIds.Add(entry.Id);
 }
Esempio n. 21
0
 private void ReplaceHistoryString(HistoryInfo entry)
 {
     LocalPipeline currentlyRunningPipeline = (LocalPipeline) ((LocalRunspace) base.Context.CurrentRunspace).GetCurrentlyRunningPipeline();
     if (currentlyRunningPipeline.AddToHistory)
     {
         currentlyRunningPipeline.HistoryString = entry.CommandLine;
     }
 }