Ejemplo n.º 1
0
    static UInt32 checkSum = 0;     // in or out

    static bool VerifyArgs(string[] inArgs)
    {
        //   - Make a list of args
        //   - Remove them from the list when they've been processed
        // 1 - Determine COM port
        // 2 - Grab any /r /e /b commands
        // 3 - Get the address value if required
        // 4 - Get the other address value?
        // 5 - File name!
        //

        if (inArgs.Length == 0)
        {
            PrintUsage(false);
            return(false);
        }

        // Thank you, linq <3
        // We'll remove args as they're processed so they're not processed twice
        // and extra args will be left over.

        List <string> remainingArgs = inArgs.ToList();

        // Specified a com port?

                #if DebugArgs
        Console.WriteLine("__TEST__Argsleft: " + remainingArgs.Count);
                #endif

        for (int i = remainingArgs.Count - 1; i >= 0; i--)
        {
            string s = remainingArgs[i];

            Regex COM     = new Regex(@"\b(COM(.*))\b", RegexOptions.IgnoreCase | RegexOptions.Singleline);
            Match COMPORT = COM.Match(s);

            Regex DEV    = new Regex(@"\b(dev(.*))\b", RegexOptions.IgnoreCase | RegexOptions.Singleline);
            Match NIXDEV = DEV.Match(s);

            if (COMPORT.Success)
            {
                argComPort = s.ToUpperInvariant();
                // Console.WriteLine( " - Using port " + argComPort );
                remainingArgs.RemoveAt(i);
                break;
            }
            else if (NIXDEV.Success)
            {
                argComPort = s;
                remainingArgs.RemoveAt(i);
                break;
            }
        }

        if (argComPort == "")
        {
            return(Error("\nERROR! Please specify a COM port!"));
        }

        // going straight into monitor mode?
        if (remainingArgs.Count == 1 && remainingArgs[0].ToLowerInvariant() == "/m")
        {
            return(true);
        }


                #if DebugArgs
        Console.WriteLine("__TEST__Argsleft: " + remainingArgs.Count);
        Console.WriteLine("__TEST__COM: " + argComPort);
                #endif

        // Specified a command arg (or severay for future-proofing)?

        for (int arg = remainingArgs.Count - 1; arg >= 0; arg--)
        {
            string argVal = remainingArgs[arg].ToLowerInvariant();

            if (argVal == "/m")
            {
                                #if DebugArgs
                Console.WriteLine("__TEST__CommsMonitor Enabled: ");
                                #endif

                monitorComms = true;
                remainingArgs.RemoveAt(arg);
                continue;
            }

            // first one is uninitialised... ignore it
            for (int j = 1; j < (int)CommandMode.COUNT; j++)
            {
                CommandMode c = (CommandMode)j;

                if (argVal.ToLowerInvariant() == c.command())
                {
                    // Set the current value and remove
                    // it from the list of available args
                    argCommand     = c;
                    needInputFile  = c.needsInputFile();
                    needOutputFile = c.needsOutputFile();
                    remainingArgs.RemoveAt(arg);

                    // Now we've removed the /command, if there's
                    // an address after it, it will be remaningArgs[ arg ]

                    // end of the array!
                    if (arg >= remainingArgs.Count)
                    {
                        return(Error("Specify an address in the format 0x01234567\n"));
                    }

                    // found the command... do we need to find an address?
                    if (argCommand.needsAddress())
                    {
                                                #if DebugArgs
                        Console.WriteLine("__TEST__Command " + c + " requires an address ");
                                                #endif

                        // reassign it
                        argVal = remainingArgs[arg].ToLowerInvariant();

                        // try and get the address argument
                        try{
                            argAddr = ParseAddress(argCommand, argVal);
                        } catch (System.Exception) {
                            return(Error("EXCEPTION: Specify an address in the format 0x01234567"));
                        }

                        remainingArgs.RemoveAt(arg);
                        satisfiedAddressRequirements = true;
                    }

                    // On top of that... do we need a size? E.g. for dumping bios.
                    if (argCommand.needsSize())
                    {
                                                #if DebugArgs
                        Console.WriteLine("__TEST__Command " + c + " requires a size parameter ");
                                                #endif

                        // reassign it
                        argVal = remainingArgs[arg].ToLowerInvariant();

                        // try to get the size argument
                        try{
                            argSize = ParseAddress(argCommand, argVal);
                        } catch (System.Exception) {
                            return(Error("EXCEPTION: Specify a size in the format 0x01234567"));
                        }

                        remainingArgs.RemoveAt(arg);
                        satisfiedSizeRequirements = true;
                    }

                    break; // outer loop to check new args
                }
            }              // inner loop
        }                  // outer loop

                #if DebugArgs
        Console.WriteLine("__TEST__Argsleft: " + remainingArgs.Count);
        Console.WriteLine("__TEST__COM: " + argComPort);
        Console.WriteLine("__TEST__COMMAND: " + argCommand);
        Console.WriteLine("__TEST__ADDR: " + argAddr.ToString("X8"));
        Console.WriteLine("__TEST__NEEDSADDR: " + argCommand.needsAddress());
        Console.WriteLine("__TEST__NEEDSINPUT: " + argCommand.needsInputFile());
        Console.WriteLine("__TEST__NEEDSOUTPUT: " + argCommand.needsOutputFile());
                #endif

        if (argCommand == CommandMode.NOT_SET)
        {
            return(Error("Please specify a command - e.g. /r, /e /b, etc!\n\n"));
        }

        if (!argCommand.needsAddress())
        {
            satisfiedAddressRequirements = true;
        }

        if (!argCommand.needsSize())
        {
            satisfiedSizeRequirements = true;
        }

        if (!satisfiedAddressRequirements)
        {
            return(Error("Did you specify an address? E.g. 0x23456788\n"));
        }

        if (!satisfiedSizeRequirements)
        {
            return(Error("Did you specify a size? E.g. 0x23456788\n"));
        }


        // We've parsed the arguments, the addresses and the COM port
        // Finish up by finding the file, if necessary

        if (!needInputFile && !needOutputFile)
        {
            satisfiedFileRequirements = true;
        }
        else
        {
            // One of the args specifies a file?
            for (int i = remainingArgs.Count - 1; i >= 0; i--)
            {
                string fName = remainingArgs[i];

                if (needInputFile)
                {
                    argFileName = fName;

                    try {
                        inFile = File.ReadAllBytes(fName);
                    } catch (System.Exception e) {
                        return(Error("Couldn't open input file " + fName + " Exception: " + e));
                    }

                    satisfiedFileRequirements = true;
                    remainingArgs.RemoveAt(i);
                }
            }
        }

        if (!satisfiedFileRequirements)
        {
            return(Error("Specify a filename!"));
        }

                #if DebugArgs
        Console.WriteLine("__TEST__Argsleft: " + remainingArgs.Count);
        Console.WriteLine("__TEST__FILENAME: " + argFileName);
        Console.WriteLine("__TEST__INPUTFILE: " + inFile);
        //Console.WriteLine( "__TEST__OUTPUTFILE: " + argOutFile );
                #endif

        // there shouldn't be any arguments left!
        if (remainingArgs.Count > 0)
        {
            for (int i = remainingArgs.Count - 1; i >= 0; i--)
            {
                Error("Unknown arg! " + remainingArgs[i]);
            }

            return(false);
        }

        // All done
        return(true);
    }     //VerifyArgs
Ejemplo n.º 2
0
    //static UInt32 checkSum = 0; // in or out


    // Ensure the input is vaguely sane.
    //
    // - Make a list of args
    // - Remove them from the list when they've been processed
    // - Determine COM port
    // - Grab any /r /e /b commands
    // - Get the address value if required
    // - Get the other address value?
    // - File name!
    //


    static bool VerifyArgs(string[] inArgs)
    {
        if (inArgs.Length == 0)
        {
            PrintUsage(false);
            return(false);
        }

        // Thank you, linq <3
        // We'll remove args as they're processed so they're not processed twice
        // and extra args will be left over.

        List <string> remainingArgs = inArgs.ToList();

        // Specified a com port?

                #if DebugArgs
        Console.WriteLine("__DEBUG__Argsleft: " + remainingArgs.Count);
                #endif

        for (int i = remainingArgs.Count - 1; i >= 0; i--)
        {
            string s = remainingArgs[i];

            if (ValidPort(s))
            {
                argComPort = s;
                remainingArgs.RemoveAt(i);
                break;
            }
        }

        // Likewise get the "/FAST" parameter out of the way

        for (int i = remainingArgs.Count - 1; i >= 0; i--)
        {
            string s = remainingArgs[i].ToUpperInvariant();

            if (s == "/FAST")
            {
                fastMode = true;
                remainingArgs.RemoveAt(i);
            }
        }



        if (argComPort == "")
        {
            try{
                if (System.IO.File.Exists("comport.txt"))
                {
                    argComPort = File.ReadAllText("comport.txt");
                }
            } catch (System.Exception e) {
                Console.Write("Error checking for cached com port...\n" + e);
            }

            if (argComPort == "")
            {
                if (POSIXEnvironment)
                {
                    return(Error("\nERROR! Please specify a device - e.g. /dev/ttyUSB0\n"));
                }
                else
                {
                    return(Error("\nERROR! Please specify a COM port - e.g. COM8\n"));
                }
            }
            else
            {
                usingCachedComPort = true;
            }
        }
        else
        {
            try{
                File.WriteAllText("comport.txt", argComPort);
            } catch (System.Exception e) {
                Console.Write("Error writing cached com port!\n" + e);
            }
        }


        // is /m the last remaining arg?
        if (remainingArgs.Count == 1 && remainingArgs[0].ToLowerInvariant() == "/m")
        {
            monitorComms = true;
            return(true);
        }


                #if DebugArgs
        Console.WriteLine("__DEBUG__Argsleft: " + remainingArgs.Count);
        Console.WriteLine("__DEBUG__COM: " + argComPort);
        Console.WriteLine("__DEBUG__FAST: " + fastMode);
                #endif

        // Specified a command arg (or severay for future-proofing)?

        for (int arg = remainingArgs.Count - 1; arg >= 0; arg--)
        {
            string argVal = remainingArgs[arg].ToLowerInvariant();

            if (argVal == "/m")
            {
                                #if DebugArgs
                Console.WriteLine("__DEBUG__CommsMonitor Enabled: ");
                                #endif

                monitorComms = true;
                remainingArgs.RemoveAt(arg);
                continue;
            }

            // first one is uninitialised... ignore it
            for (int j = 1; j < (int)CommandMode.COUNT; j++)
            {
                CommandMode c = (CommandMode)j;

                if (argVal.ToLowerInvariant() == c.command())
                {
                    // Set the current value and remove
                    // it from the list of available args
                    argCommand     = c;
                    needInputFile  = c.needsInputFile();
                    needOutputFile = c.needsOutputFile();
                    remainingArgs.RemoveAt(arg);

                    // On top of that... do we need a size? E.g. for dumping bios.
                    if (argCommand.needsRegister())
                    {
                                                #if DebugArgs
                        Console.WriteLine("__DEBUG__Command " + c + " requires a register ");
                                                #endif

                        // reassign it
                        argVal = remainingArgs[arg].ToLowerInvariant();

                        // TODO: verify it's a register, I guess.
                        argRegister = argVal;

                        remainingArgs.RemoveAt(arg);
                        satisfiedRegisterRequirements = true;
                    }

                    // Now we've removed the /command, if there's
                    // an address after it, it will be remaningArgs[ arg ]

                    // found the command... do we need to find an address?
                    if (argCommand.needsAddress())
                    {
                        // end of the array!
                        if (arg >= remainingArgs.Count)
                        {
                            return(Error("Specify an address in the format 0x01234567\n"));
                        }

                                                #if DebugArgs
                        Console.WriteLine("__DEBUG__Command " + c + " requires an address ");
                                                #endif

                        // reassign it
                        argVal = remainingArgs[arg].ToLowerInvariant();

                        // try and get the address argument
                        try{
                            argAddr = ParseAddress(argCommand, argVal);
                        } catch (System.Exception) {
                            return(Error("EXCEPTION: Specify an address in the format 0x01234567"));
                        }

                        remainingArgs.RemoveAt(arg);
                        satisfiedAddressRequirements = true;
                    }

                    // On top of that... do we need a size? E.g. for dumping bios.
                    if (argCommand.needsSize())
                    {
                                                #if DebugArgs
                        Console.WriteLine("__DEBUG__Command " + c + " requires a size parameter ");
                                                #endif

                        // reassign it
                        argVal = remainingArgs[arg].ToLowerInvariant();

                        // try to get the size argument
                        try{
                            argSize = ParseAddress(argCommand, argVal);
                        } catch (System.Exception) {
                            return(Error("EXCEPTION: Specify a size in the format 0x01234567"));
                        }

                        remainingArgs.RemoveAt(arg);
                        satisfiedSizeRequirements = true;
                    }



                    break; // outer loop to check new args
                }
            }              // inner loop
        }                  // outer loop

                #if DebugArgs
        Console.WriteLine("__DEBUG__Argsleft: " + remainingArgs.Count);
        Console.WriteLine("__DEBUG__COM: " + argComPort);
        Console.WriteLine("__DEBUG__COMMAND: " + argCommand);
        Console.WriteLine("__DEBUG__ADDR: " + argAddr.ToString("X8"));
        Console.WriteLine("__DEBUG__NEEDSADDR: " + argCommand.needsAddress());
        Console.WriteLine("__DEBUG__NEEDSINPUT: " + argCommand.needsInputFile());
        Console.WriteLine("__DEBUG__NEEDSOUTPUT: " + argCommand.needsOutputFile());
                #endif

        if (argCommand == CommandMode.NOT_SET)
        {
            return(Error("Please specify a command - e.g. /r, /e /b, etc!\n\n"));
        }

        if (!argCommand.needsRegister())
        {
            satisfiedRegisterRequirements = true;
        }

        if (!argCommand.needsAddress())
        {
            satisfiedAddressRequirements = true;
        }

        if (!argCommand.needsSize())
        {
            satisfiedSizeRequirements = true;
        }

        if (!satisfiedAddressRequirements)
        {
            return(Error("Did you specify an address or hex value? E.g. 0x23456788\n"));
        }

        if (!satisfiedSizeRequirements)
        {
            return(Error("Did you specify a size? E.g. 0x23456788\n"));
        }

        if (!satisfiedRegisterRequirements)
        {
            return(Error("Did you specify a register? E.g. a0"));
        }


        // We've parsed the arguments, the addresses and the COM port
        // Finish up by finding the file, if necessary

        if (!needInputFile && !needOutputFile)
        {
            satisfiedFileRequirements = true;
        }
        else
        {
            // One of the args specifies a file?
            for (int i = remainingArgs.Count - 1; i >= 0; i--)
            {
                string fName = remainingArgs[i];

                if (needInputFile)
                {
                    argFileName = fName;

                    try {
                        inFile = File.ReadAllBytes(fName);
                    } catch (System.Exception e) {
                        return(Error("Couldn't open input file " + fName + " Exception: " + e));
                    }

                    satisfiedFileRequirements = true;
                    remainingArgs.RemoveAt(i);
                }
            }
        }

        if (!satisfiedFileRequirements)
        {
            return(Error("Specify a filename!"));
        }

                #if DebugArgs
        Console.WriteLine("__DEBUG__Argsleft: " + remainingArgs.Count);
        Console.WriteLine("__DEBUG__FILENAME: " + argFileName);
        Console.WriteLine("__DEBUG__INPUTFILE: " + inFile);
        //Console.WriteLine( "__DEBUG__OUTPUTFILE: " + argOutFile );
                #endif

        // there shouldn't be any arguments left!
        if (remainingArgs.Count > 0)
        {
            for (int i = remainingArgs.Count - 1; i >= 0; i--)
            {
                Error("Unknown arg! " + remainingArgs[i]);
            }

            return(false);
        }

        // All done
        return(true);
    }     //VerifyArgs