Exemplo n.º 1
0
        // The pipe that is being called.
        /// <summary>
        /// Compiles the filter.
        /// </summary>
        public override void Compile(string cmdLineText, int initialCharPos)
        {
            // Parse the filter's command line:

             LoggingEnabled = true;
             LogWrite("Call.Compile: begin");
             CmdLine.Text = cmdLineText;
             CmdLine.IgnoringExtraneousText = true;
             CmdLine.Parse(initialCharPos, true);

             // Load and compile the contents of the saved-to-disk pipe:

             string pipePath = (string) CmdLine.GetArg(0).Value;
             LogWrite("Call.Compile: Called pipe: \"" + pipePath + "\"");

             if (System.IO.File.Exists(pipePath))
             {
            // The pipe file exists.  Create a new pipe object from the pipe file's contents:

            string tempStr = System.IO.Path.GetFullPath(pipePath);
            string pipeFolder = System.IO.Path.GetDirectoryName(tempStr);
            string pipeName = System.IO.Path.GetFileName(tempStr);
            string pipeScript = System.IO.File.ReadAllText(pipePath);
            CalledPipe = new Pipe(pipeScript, (Engine) Eng, pipeFolder, pipeName,
            ((Filter) Host).CoreLoggingEnabled, false);
            CalledPipe.DebugFile = ((Filter) Host).DebugFile;
            CalledPipe.Errors = ((Filter) Host).Errors;

            // Save the current folder because Pipe.Compile() may change it:

            string savedFolder = System.Environment.CurrentDirectory;

            // Compile the pipe using the CALL filter's
            // "extraneous" command line parameters:

            CalledPipe.Compile(CmdLine.Text, ((CommandLine) CmdLine).CmdLinePtr, Source, PipeLineNo);

            // Restore the prior-saved current folder:

            Directory.SetCurrentDirectory(savedFolder);
             }
             else
             {
            // Pipe file doesn't exist.

            PipeWrenchCompileException ex = new PipeWrenchCompileException("Pipe file not found.");
            ex.Data.Add("CharPos", ((CommandLine) CmdLine).CmdLinePtr);
            ex.Data.Add("CmdLine", CmdLine.Text);
            throw ex;
             }

             LogWrite("Call.Compile: end");
        }
Exemplo n.º 2
0
        // The filter's line number in the pipe.
        /// <summary>
        /// Constructor.
        /// </summary>
        public Filter(string Name, string TypeName, string AssyPath, Pipe Parent, int pipeLineNo)
        {
            this.Parent = Parent;
             this.Name = Name;
             this.Eng = Parent.Eng;
             this.Log = ((Engine) Eng).Log;
             this.coreLoggingEnabled = Parent.LoggingEnabled;
             this.DivTextStack = Parent.DivTextStack;
             this.pipeLineNo = pipeLineNo;

             // Instantiate the plug-in type:

             AssyRef = Assembly.LoadFrom(AssyPath);
             Type type = AssyRef.GetType(TypeName);

             if (type != null)
             {
            object TheObj = Activator.CreateInstance(type, new object[] {this});

            if (TheObj is FilterPlugin)
            {
               // The type is a plugin.

               Plugin = (FilterPlugin) TheObj;
               CmdLine = new CommandLine(Plugin.Template, Parent.Eng.Log, CoreLoggingEnabled);
            }
            else
            {
               // The type isn't a plugin.

               throw new PipeWrenchEngineException("Type isn't a plugin.\nAssembly: " +
               AssyPath + "\nType: " + TypeName); ///
            }
             }
             else
             {
            // Unable to retrieve plugin type.

            throw new PipeWrenchEngineException("Plugin type not found.\nAssembly: " +
            AssyPath + "\nType: " + TypeName); ///
             }
        }
Exemplo n.º 3
0
        static int Main(string[] args)
        {
            int exitCode = 0;
             string homeFolder;
            // User's home folder.
             string applDataFolder;
            // The application's folder beneath the home folder.
             string tempStr = string.Empty;
             string debugFile;

             if (args.Length != 0)
             {
            // Executing a pipe.

            try
            {
               // Load configuration parameters:

               if (isLinux)
               {
                  homeFolder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
                  applDataFolder = homeFolder + System.IO.Path.DirectorySeparatorChar + "." + appName.ToLower();
               }
               else
               {
                  homeFolder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData);
                  applDataFolder = homeFolder + System.IO.Path.DirectorySeparatorChar + appName;
               }

               string configFile = applDataFolder + System.IO.Path.DirectorySeparatorChar + appName + "Conf.xml";

               if (File.Exists(configFile))
               {
                  // Configuration file found.

                  XmlDocument xmlDoc = new XmlDocument();
                  xmlDoc.Load(configFile);
                  XmlElement rootElem = xmlDoc["Config"];
                  XmlElement elem = rootElem["DebugFile"];
                  debugFile = elem.InnerText;
               }
               else
               {
                  // Configuration file doesn't exist. Default the debug file:

                  debugFile = applDataFolder + System.IO.Path.DirectorySeparatorChar + "Debug.txt";
               }

               // Create a pipe object:

               string currFolder = System.Environment.CurrentDirectory;
               string script = GetPositionalArg(1, args);
               Pipe thePipe = new Pipe(script, pipeEng, currFolder, string.Empty,
               Engine.CoreLogging(args), true);
               thePipe.DebugFile = debugFile;
               string pipeArgs = GetPositionalArg(2, args);

               // Compile the pipe:

               thePipe.Compile(pipeArgs, 0, string.Empty, 0);

               if (thePipe.Errors.Count == 0)
               {
                  // Create the input text file:

                  string inText = Path.GetTempFileName();
                  StreamWriter inTextWriter = new StreamWriter(inText);

                  // Transfer the text from standard input to the text file object:

                  while ((tempStr = Console.ReadLine()) != null)
                  {
                     inTextWriter.WriteLine(tempStr);
                  }

                  inTextWriter.Close();

                  // Create the output text file:

                  string outText = Path.GetTempFileName();

                  // Execute the pipe:

                  thePipe.Execute(ref inText, ref outText);

                  // Output the text results to standard output:

                  StreamReader outTextReader = new StreamReader(outText);

                  while (!outTextReader.EndOfStream)
                  {
                     Console.WriteLine(outTextReader.ReadLine());
                  }
               }
               else
               {
                  Console.Error.WriteLine(System.Environment.NewLine +
                  thePipe.Errors.ToString());
               }
            }

            catch (PipeWrenchExecException ex)
            {
               // Pipe execution (runtime) exception.

               tempStr = string.Empty;
               string source = (string) ex.Data["Source"];
               tempStr += source + System.Environment.NewLine;
               string lineNoStr = ((int) ex.Data["LineNo"]).ToString();
               string cmdLine = (string) ex.Data["CmdLine"];
               tempStr += "   line " + lineNoStr + ": " + cmdLine +
               System.Environment.NewLine;

               if (ex.Data.Contains("CharPos"))
               {
                  int charPos = (int) ex.Data["CharPos"];
                  tempStr += "^".PadLeft(charPos+10+lineNoStr.Length, ' ');
               }

               tempStr += System.Environment.NewLine + "      " + ex.Message;
               Console.Error.WriteLine(tempStr);
               exitCode = -1;
            }

            catch (Exception ex)
            {
               // Anything not already handled...

               exitCode = -2;
               Console.Error.WriteLine(ex.Message);
               pipeEng.Log.WriteText(ex.ToString(), "Fatal error...", "   ");
            }
             }
             else
             {
            try
            {
               string headerLeftStr = cmdLineAppName + " " + version;
               string headerRightStr;

               if (isLinux)
                  headerRightStr = "Copyright \u00a9 2013 Firefly Software";
               else
                  headerRightStr = "Copyright (C) 2013 Firefly Software";

               string headerStr = headerLeftStr + headerRightStr.PadLeft(consoleCols -
               headerLeftStr.Length - 1) + "\n\n";
               string footerStr = "\n<Press ENTER to Continue>";

               // Display the help screen:

               tempStr = cmdLineAppName.ToUpper() + " is the command-line component of " + appName + ", " +
               "the high-level text scripting tool that allows you to easily transform text, (i.e. lists, " +
               "command output, HTML, config files, log files, source code, CSV data, etc.) from one form " +
               "to another with relative ease.  In a " + appName + " script, there's no need to create conditional " +
               "or looping constructs or even to declare variables.  You simply \"stack\" filters to get " +
               "the results you want.";

               string wrapToWidthPipe = "WrapText " + consoleCols.ToString();
               string bodyStr = pipeEng.RunPipe("wrapToWidthPipe", wrapToWidthPipe, "", tempStr, false, false) + "\n\n";

               tempStr = "Notice: This software is provided WITHOUT WARRANTY OF ANY KIND under " +
               "the Free Software Foundation's GNU General Public License.  For further details, " +
               "see \"http://www.gnu.org/licenses/\". ";

               string noticeStr = pipeEng.RunPipe("wrapToWidthPipe", wrapToWidthPipe, "", tempStr, false, false);

               Console.Error.Write(headerStr + bodyStr + noticeStr + PadLines(headerStr + bodyStr + noticeStr +
               footerStr) + footerStr);
               Console.ReadKey();

               bodyStr = "Syntax: " + cmdLineAppName + " <pipe> [<arguments>]\n\n";
               bodyStr += "Where:\n\n";

               string wrapToWidthIndentedPipe = "WrapText " + (consoleCols-3).ToString() + "|InsStr 1 '   '";
               bodyStr += pipeEng.RunPipe("wrapToWidthIndentedPipe", wrapToWidthIndentedPipe, "",
               "<pipe> is a double-quoted string containing one or more " + appName + " commands separated " +
               "by \"" + Engine.PipeDelim + "\".", false, true) + "\n";

               bodyStr += pipeEng.RunPipe("wrapToWidthIndentedPipe", wrapToWidthIndentedPipe, "",
               "<arguments> is a double-quoted string containing one or more pipe arguments.",
               false, true) + "\n\n";

               bodyStr += pipeEng.RunPipe("wrapToWidthPipe", wrapToWidthPipe, "", appName.ToLower() +
               "'s I/O can be redirected. Following are some examples:", false, true) + "\n\n";

               if (isLinux)
               {
                  bodyStr +=
                  "   LOGNAME=$(echo \"$FILE\" | " + cmdLineAppName + " \"AppendStr '.log'\")\n" +
                  "   echo hello | " + cmdLineAppName + " \"UpperCase " + Engine.PipeDelim + " AppendStr ' THERE'\"\n" +
                  "   cat the.txt | " + cmdLineAppName + " \"SortLines " + Engine.PipeDelim + " DelDuplLines\" >out.txt\n" +
                  "   find . -type f -printf '%P\\n' | " + cmdLineAppName + " \"InsStr 1 '#22' " + Engine.PipeDelim + " \n" +
                  "      AppendStr '#22'\" | xargs md5sum > \"$MD5SUMSFILE\"\n" +
                  "   echo \"$FILELIST\" | " + cmdLineAppName + " \"call '$RENAMEFILESPIPE'\" >\"$TEMPSCRIPT\"\n";
               }
               else
               {
                  bodyStr +=
                  "   echo hello | " + cmdLineAppName + " \"UpperCase " + Engine.PipeDelim + " AppendStr ' THERE'\"\n" +
                  "   type the.txt | " + cmdLineAppName + " \"SortLines " + Engine.PipeDelim + " DelDuplLines\" >out.txt\n" +
                  "   echo \"%FILELIST%\" | " + cmdLineAppName + " \"call '%RENAMEFILESPIPE%'\" >\"%TEMPBATCH%\"\n" +
                  "   type template.txt | " + cmdLineAppName + " \"ReplStr '<KEY>' '%KEY%'\" >page.htm";
               }

               Console.Error.Write(headerStr + bodyStr + PadLines(headerStr + bodyStr + footerStr) + footerStr);
               Console.ReadKey();

               string filterList = GetFilters();

               string displayFiltersPipe = "ParseWords|SortLines|PadLinesRight '~'|InsStr 1 '~'|" +
               "ReplStr '~(\\\\w+)\\\\*' '*$1~' /r|AppendStr ' '|JoinLines|WrapText " +
               consoleCols.ToString() + " /j50|ReplStr '~' ' '";

               tempStr = pipeEng.RunPipe("displayFiltersPipe", displayFiltersPipe, "", filterList, false, true);
               PaginateText(tempStr, headerStr, footerStr);
            }

            catch (PipeWrenchCompileException ex)
            {
               Console.Error.WriteLine(ex.Message);
            }

            catch (PipeWrenchExecException ex)
            {
               // Pipe execution (runtime) exception.

               tempStr = string.Empty;
               string source = (string) ex.Data["Source"];
               tempStr += source + System.Environment.NewLine;
               string lineNoStr = ((int) ex.Data["LineNo"]).ToString();
               string cmdLine = (string) ex.Data["CmdLine"];
               tempStr += "   line " + lineNoStr + ": " + cmdLine + System.Environment.NewLine;

               if (ex.Data.Contains("CharPos"))
               {
                  int charPos = (int) ex.Data["CharPos"];
                  tempStr += "^".PadLeft(charPos+10+lineNoStr.Length, ' ');
               }

               tempStr += System.Environment.NewLine + "      " + ex.Message;
               Console.Error.WriteLine(tempStr);
            }

            catch (Exception ex)
            {
               // Anything not already handled...

               pipeEng.Log.WriteText(ex.ToString(), "Fatal error...", "   ");
               Console.Error.WriteLine(ex.Message);
            }
             }

             return exitCode;
        }
Exemplo n.º 4
0
        /// <summary>
        /// Executes the pipe.
        /// </summary>
        private void ExecutePipe(string pipeText)
        {
            try
             {
            ErrorsTextBox.Text = string.Empty;
            string pipeFolder = string.Empty;
            string pipeName = "<new pipe>";

            if (PipePath != string.Empty)
            {
               string tempStr = System.IO.Path.GetFullPath(PipePath);
               pipeFolder = System.IO.Path.GetDirectoryName(tempStr);
               pipeName = System.IO.Path.GetFileName(tempStr);
            }

            Pipe ThePipe = new Pipe(pipeText, PipeEng, pipeFolder, pipeName, CoreLogging, false);
            ThePipe.DebugFile = DebugFile;
            ThePipe.Compile(ArgsTextBox.Text, 0, string.Empty, 0);

            if (ThePipe.Errors.Count == 0)
            {
               if ((!File.Exists(OutputFileTextBox.Text)) ||
               MessageBox.Show("Overwrite output file?", "Warning!", MessageBoxButtons.YesNoCancel,
               MessageBoxIcon.Warning) == DialogResult.Yes)
               {
                  if ((DebugFile != null) && (File.Exists(DebugFile)))
                  {
                     File.Delete(DebugFile);
                  }

                  string inTempFile = System.IO.Path.GetTempFileName();
                  string outTempFile = System.IO.Path.GetTempFileName();

                  try
                  {
                     if (InputFileTextBox.Text == string.Empty)
                     {
                        File.WriteAllText(inTempFile, InputTextBox.Text);
                     }
                     else
                     {
                        File.Copy(InputFileTextBox.Text, inTempFile, true);
                     }

                     ThePipe.Execute(ref inTempFile, ref outTempFile);

                     if (OutputFileTextBox.Text == string.Empty)
                     {
                        // Unsubscribe from the TextChanged event handler so that
                        // the updates made to the ouput textbox don't change
                        // the caret's current location:

                        OutputTextBox.TextChanged -= IOTextBox_OnChanged;

                        // Write changes to the output text box:

                        OutputTextBox.Text = File.ReadAllText(outTempFile);

                        // Re-subscribe to the TextChanged event handler:

                        OutputTextBox.TextChanged += IOTextBox_OnChanged;
                     }
                     else
                     {
                        File.Copy(outTempFile, OutputFileTextBox.Text, true);
                     }
                  }

                  finally
                  {
                     File.Delete(inTempFile);
                     File.Delete(outTempFile);
                  }

                  TextTabControl.SelectedTab = OutputTabPage;
                  PipeTextBox.Focus();
               }
            }
            else
            {
               // Errors.

               ErrorsTextBox.Text = ThePipe.Errors.ToString();
               TextTabControl.SelectedTab = ErrorsTabPage;
            }
             }

             catch (PipeWrenchExecException ex2)
             {
            // Pipe execution (runtime) exception.

            string tempStr = string.Empty;
            string source = (string) ex2.Data["Source"];
            tempStr += source + System.Environment.NewLine;
            string lineNoStr = ((int) ex2.Data["LineNo"]).ToString();
            string cmdLine = (string) ex2.Data["CmdLine"];
            tempStr += "   line " + lineNoStr + ": " + cmdLine + System.Environment.NewLine;

            if (ex2.Data.Contains("CharPos"))
            {
               int charPos = (int) ex2.Data["CharPos"];
               tempStr += "^".PadLeft(charPos+10+lineNoStr.Length, ' ');
            }

            tempStr += System.Environment.NewLine + "      " + ex2.Message;
            ErrorsTextBox.Text = tempStr;
            TextTabControl.SelectedTab = ErrorsTabPage;
             }

             catch (Exception ex3)
             {
            // Anything not already handled...

            ErrorsTextBox.Text = ex3.Message;
            TextTabControl.SelectedTab = ErrorsTabPage;
            PipeEng.Log.WriteText(ex3.ToString(), "Fatal error...", "   ");
             }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Implements a pipe wrapper for use internally or by front-end.
        /// </summary>
        public string RunPipe(string name, string script, string args, string inText, bool loggingEnabled, bool replacingDelims)
        {
            Pipe thePipe = new Pipe(script, this, string.Empty, string.Empty, loggingEnabled, replacingDelims);

             thePipe.Compile(args, 0, string.Empty, 0);

             if (thePipe.Errors.Count == 0)
             {
            string inTempFile = System.IO.Path.GetTempFileName();
            File.WriteAllText(inTempFile, inText);
            string outTempFile = System.IO.Path.GetTempFileName();

            thePipe.Execute(ref inTempFile, ref outTempFile);

            string results = File.ReadAllText(outTempFile);

            results = results.Substring(0, results.Length - System.Environment.NewLine.Length);

            File.Delete(inTempFile);
            File.Delete(outTempFile);

            return results;
             }
             else
             {
            if ((name == null) || (name == string.Empty)) name = "<unknown>";
            throw new PipeWrenchCompileException(System.Environment.NewLine +
            "Internal error. Pipe passed to RunPipe, (" + name + "), " +
            "failed compile: " + System.Environment.NewLine + System.Environment.NewLine +
            thePipe.Errors.ToString());
             }
        }