Пример #1
0
        public static bool ParseArguments(string[] args, SwitchOptions options)
        {
            if (args.Length == 0)
            {
                PrintHelpTopic("main");
                return false;
            }

            for (var i = 0; i < args.Length; ++i)
            {
                if (args[i] == "-help" || args[i] == "-h")
                {
                    if (i + 1 < args.Length)
                        PrintHelpTopic(args[i + 1]);
                    else
                        PrintHelpTopic("main");
                    return false;
                }
                else if (args[i] == "-i" || args[i] == "-in" || args[i] == "-input" || args[i] == "-infile")
                {
                    if (i + 1 < args.Length)
                    {
                        options.inFile = args[i + 1];
                        ++i;
                    }
                    else
                        return false;
                }
                else if (args[i] == "-o" || args[i] == "-out" || args[i] == "-output" || args[i] == "-outfile")
                {
                    if (i + 1 < args.Length)
                    {
                        options.outFile = args[i + 1];
                        ++i;
                    }
                    else
                    {
                        Console.WriteLine("You must supply an argument to -out");
                        return false;
                    }
                }
                else if (args[i] == "-pre" || args[i] == "-p")
                {
                    if (!SetMode(options, "pre")) return false;
                }
                else if (args[i] == "-m" || args[i] == "-mode")
                {
                    if (i + 1 < args.Length)
                    {
                        if (!SetMode(options, args[i + 1])) return false;
                        ++i;
                    }
                    else
                    {
                        Console.WriteLine("You must supply an argument to -mode");
                        return false;
                    }
                }
                else if (args[i] == "-option" || args[i] == "-op")
                {
                    if (i + 1 < args.Length)
                    {
                        options.options.Add(args[i + 1]);
                        ++i;
                    }
                    else
                    {
                        Console.WriteLine("You must supply an argument to -option");
                        return false;
                    }
                }
                else
                {
                    if (options.inFile == null) options.inFile = args[i];
                    else if (options.outFile == null) options.outFile = args[i];
                    else
                    {
                        Console.WriteLine("I do not understand the switch '" + args[i] + "'");
                        return false;
                    }
                }
            }

            if (options.inFile == null || options.outFile == null)
            {
                Console.WriteLine("You must supply both an input and an output file.");
                Console.WriteLine("For example, 'Passages -in sample.psg -out compiled.txt'");
                return false;
            }

            if (String.IsNullOrEmpty(options.mode)) options.mode = "extract-prose";

            return true;
        }
Пример #2
0
 private static bool SetMode(SwitchOptions options, String mode)
 {
     if (String.IsNullOrEmpty(options.mode))
     {
         if (mode == "extract-prose" || mode == "ep" || mode == "prose")
             options.mode = "extract-prose";
         else if (mode == "pre" || mode == "p")
             options.mode = "pre";
         else
         {
             Console.WriteLine("I don't recognize the mode '" + mode + "'");
             Console.WriteLine("Try -help modes for a list of modes");
             return false;
         }
         return true;
     }
     else
     {
         Console.WriteLine("Mode specified multiple times");
         return false;
     }
 }
Пример #3
0
        static void Main(string[] args)
        {
            var options = new SwitchOptions();
            if (!CommandLine.ParseArguments(args, options))
                return;

            try
            {
                if (options.mode == "pre")
                {
                    System.IO.FileStream destination = null;// System.IO.File.Open(options.outFile, System.IO.FileMode.Create);
                    System.IO.StreamWriter writer = null;// new System.IO.StreamWriter(destination);

                    var scriptEngine = MISP.Environment.CreateStandardEnvironment();
                    Preprocessor.SetupScriptEngine(scriptEngine);

                    scriptEngine.AddCompileTimeConstant("options", options);
                    scriptEngine.AddCompileTimeConstant("@globals", new MISP.ScriptObject());

                    scriptEngine.AddNativeFunction("retarget",
                        (context, cargs) =>
                        {
                            var newTarget = cargs[0].ToString();

                            //Grab existing buffer
                            var bufferBuilder = context.Tag as Preprocessor.PreprocessContext;
                            var buffer = bufferBuilder.builder.ToString();

                            if (writer == null && !String.IsNullOrEmpty(buffer))
                            {
                                destination = System.IO.File.Open(options.outFile, System.IO.FileMode.Create);
                                writer = new System.IO.StreamWriter(destination);
                            }

                            if (writer != null)
                            {
                                if (!String.IsNullOrEmpty(buffer))
                                    writer.Write(buffer);

                                writer.Flush();
                                destination.Flush();
                                destination.Close();
                            }

                            destination = System.IO.File.Open(newTarget, System.IO.FileMode.Create);
                            writer = new System.IO.StreamWriter(destination);

                            return true;
                        });

                    var source = System.IO.File.ReadAllText(options.inFile);
                    var processed = Preprocessor.Preprocess(source, scriptEngine);

                    if (writer == null)
                    {
                        destination = System.IO.File.Open(options.outFile, System.IO.FileMode.Create);
                        writer = new System.IO.StreamWriter(destination);
                    }
                    if (!String.IsNullOrEmpty(processed))
                        writer.Write(processed);

                    writer.Flush();
                    destination.Flush();
                    destination.Close();
                }
                else if (options.mode == "extract-prose")
                {
                    var destination = System.IO.File.Open(options.outFile, System.IO.FileMode.Create);
                    var writer = new System.IO.StreamWriter(destination);

                    var scriptEngine = MISP.Environment.CreateStandardEnvironment();
                    scriptEngine.AddCompileTimeConstant("options", options);
                    scriptEngine.AddCompileTimeConstant("@globals", new MISP.ScriptObject());

                    var source = System.IO.File.ReadAllText(options.inFile);
                    var processed = Preprocessor.Preprocess(source, scriptEngine);

                    foreach (var item in new ExtractProse(Parser.Parse(processed)))
                        writer.WriteLine(item);

                    writer.Flush();
                    destination.Flush();
                    destination.Close();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("An error occured.");
                Console.WriteLine(e.Message);
            }
        }