Example #1
0
        /// <summary>
        /// Handle Falcom commands. Determine if command is valid,
        /// what is the type of it, and execute (if valid).
        /// </summary>
        /// <param name="input">Falcon command (including cmd char)</param>
        /// <param name="output">Execution result</param>
        /// <returns>Is command valid, Command message, Command Type, On or Off flag</returns>
        public static bool Parse(string cmd, ref string message, ref Command.Type type, ref Argument argumentObj)
        {
            // extract command name and arguments
            string[] cmdArr  = cmd.Split(CMD_SPLITTER);
            string   cmdName = cmdArr[CMD_NAME_INDX];


            bool noArgument = (cmdArr.Length <= 1) ? true : false;

            SshCommand     sshCmd     = new SshCommand();
            PingCommand    pingCmd    = new PingCommand();
            ClearCommand   clearCmd   = new ClearCommand();
            HelpCommand    helpCmd    = new HelpCommand();
            InvalidCommand invalidCmd = new InvalidCommand();

            // return values to caller according to cmd name
            switch (cmdName)
            {
            case "ssh":

                if (noArgument)
                {
                    message = sshCmd.GetNoArgumentMsg();
                    return(false);
                }

                if (cmdArr.Length < 4)
                {
                    message = sshCmd.GetInvalidArgumentMsg();
                    return(false);
                }

                string sshArgs = cmdArr[CMD_ARG_INDX] + " " +
                                 cmdArr[CMD_ARG_INDX + 1] + " " +
                                 cmdArr[CMD_ARG_INDX + 2];

                sshCmd.InitArgument(sshArgs);
                type = sshCmd.GetCommandType();

                if (!sshCmd.IsValidArgument())
                {
                    message = sshCmd.GetInvalidArgumentMsg();
                    return(false);
                }
                argumentObj = sshCmd.GetArgumentObject();
                return(true);

            case "ping":

                if (noArgument)
                {
                    message = pingCmd.GetNoArgumentMsg();
                    return(false);
                }

                pingCmd.InitArgument(cmdArr[CMD_ARG_INDX]);
                type = pingCmd.GetCommandType();

                argumentObj = pingCmd.GetArgumentObject();
                message     = pingCmd.GetSuccessMsg();
                return(true);

            case "clear":
                type    = clearCmd.GetCommandType();
                message = clearCmd.GetSuccessMsg();
                return(true);

            case "help":
                if (noArgument)
                {
                    message = helpCmd.GetNoArgumentMsg();
                    return(false);
                }
                helpCmd.InitArgument(cmdArr[CMD_ARG_INDX]);
                if (!helpCmd.IsValidArgument())
                {
                    message = helpCmd.GetInvalidArgumentMsg();
                    return(false);
                }

                switch (cmdArr[CMD_ARG_INDX])
                {
                case "ssh":
                    message = sshCmd.GetHelpMsg();
                    break;

                case "ping":
                    message = pingCmd.GetHelpMsg();
                    break;

                case "clear":
                    message = clearCmd.GetHelpMsg();
                    break;

                case "help":
                    message = helpCmd.GetHelpMsg();
                    break;
                }
                break;

            default:
            {
                invalidCmd.InitArgument(cmd);
                type    = invalidCmd.GetCommandType();
                message = invalidCmd.GetInvalidArgumentMsg();
                break;
            }
            }
            return(false);
        }