Esempio n. 1
0
CCommandLineParser::CCommandLineParser(int argc, char *argv[]) 
	: m_unknown_command(""), m_cur_task(NULL), m_argc(argc-1), m_parse_result(Parse_none_found) {
	
	for(int i=1; i<argc; ++i) {
		string arg=argv[i];
		size_t pos=arg.find('=');
		if(pos==string::npos) {
			m_args.push_back(arg);
		} else {
			if(pos==0 || pos==arg.length()-1) {
				unknownCommand(arg);
				i=argc;
			} else {
				m_args.push_back(arg.substr(0, pos));
				m_args.push_back(arg.substr(pos+1));
				++m_argc;
			}
		}
	}
	m_args.push_back("");
}
Esempio n. 2
0
//
// Main game loop
//
void XBoard::mainLoop()
{
  run = true;
  pulchess = NULL;

  while(readCommand() && run)
  {
	// if it is a move... play it
	if( pulchess != NULL && pulchess->IsMove(buff))
    {
	  if( !pulchess->gameCommand(buff) )
      {
	     cout << "Illegal move: " << buff << endl;	
      }
      else
      {
         CheckSendResult();
      }
	  if( !pulchess->IsGameFinished() )
      {
        goCommand();
	  }
	  continue;
    }
	
	// start deciding on the first char
    switch(buff[0])
    {  
	  case 'n':
	
	    // new
	    if( buff == "new" )
	    {
	      pulchess = new Pulchess(HUM_VS_CPU);
	      pulchess->Init();
          pulchess->StartGame();
	    }
	
	    // nopost
		if( buff == "nopost" )
		{
		  CPUPlayer::xboardPost = false;
		}
	    break;

	  // variant - UNSUPPORTED
	  case 'v':
	    unimplCommand(buff);
	    break;
	
	  // post
	  case 'p':
	    if( buff == "post") {
		  CPUPlayer::xboardPost = true;
        }
		break;

	  // quit 
	  case 'q':
	    if( buff == "quit" )
	    {
	     run = false;
	     delete pulchess;
	     return;
	    }
	    break;

      // gioco randomizzato
	  case 'r':
		if( buff == "random" )
		{
			// randomize plays
			// currently pulchess ignores this
		}	  
	    break;

      // force mode
      //
      // with this the engine should only check move correctness.
      // for 2 human players, I think.
	  case 'f':
		if( buff == "force" )
	    {
		  unknownCommand(buff);
		  continue;
	    }
	    unimplCommand(buff);
	    break;

      // go
      case 'g':
		if( buff == "go" )
        {
          goCommand();
		  continue;
        }
	    break;

      // white
      //
      // Set White on move. Set the engine to play Black. Stop clocks.
      case 'w':
		if( buff == "white" && pulchess != NULL)
        {
          pulchess->ResetMode(HUM_VS_CPU);
		  continue;
        }
        break;

      // black
      //
      // Set Black on move. Set the engine to play White. Stop clocks.
      case 'b':
		if( buff == "black"  && pulchess != NULL)
        {
          pulchess->ResetMode(CPU_VS_HUM);
		  continue;
	    }
        break;

	case 'l':
	    // level
	    // -> type 1 or 2 of time control.
		if( buff.substr(0,5) == "level"  && pulchess != NULL) {
          pulchess_debug("Trying to set the clock...");
	      unsigned int _m=40, _s=5, _i=0;
          if( sscanf(buff.c_str(), "level %d %d %d", &_m, &_s, &_i) == 3 ) {
            pulchess->SetTimecontrol(_m,_s*60,_i);
            continue;
          }
        }
		break;

	case '?':
		break;
		
    default:
      unknownCommand(buff);
      break;
	}
  }

} // end mainLoop()
Esempio n. 3
0
Menu * Menu::handleRequest() {

#ifdef SUI_INCLUDE_EXTRA_SAFETYCHECKS

	if (!num_menu_items) {

	#ifdef SUI_SERIALUI_ECHO_WARNINGS
		returnMessage(error_nomenuitems);
	#endif
		// error!
		return this;
	}
#endif

	char * key_entered = mallocReadKey();

	if (!key_entered) {
		// nothing entered
		sui_driver->println(' ');
		return this;
	}

#ifdef SUI_SERIALUI_ECHO_ON
	// echo
	if (sui_driver->echoCommands())
	{
#ifdef SUI_ENABLE_MODES
		if (sui_driver->mode() == SUIMode_User)
#endif
		{
			sui_driver->println(key_entered);
		}
	}

#endif

	MenuItem * itm = itemForKey(key_entered);
	if (itm) {
		// get rid of our malloc'ed key
		MENUFREE(key_entered);
		if (itm->command) {

			SUI_MENU_DEBUG_OUTPUT("Running command");

			(itm->command)();
			return this;
		}
#ifdef SUI_MENU_ENABLE_SUBMENUS
		else if (itm->subMenu) {

			SUI_MENU_DEBUG_OUTPUT("Entering submenu");

			itm->subMenu->enter();
			return itm->subMenu;
		}
#endif
		// get here, we have a problem.
		// error
		return this;
	}

	// not found...

#ifdef SUI_MENU_ENABLE_SUBMENUS
	if (parent_menu && strncmp_P(key_entered, up_key, strlen_P(up_key)) == 0) {
		// get rid of our malloc'ed key

		SUI_MENU_DEBUG_OUTPUT("Going up a level");

		MENUFREE(key_entered);
		return upLevel();
	}
#endif

	if (strncmp_P(key_entered, help_key, strlen_P(help_key)) == 0) {

		SUI_MENU_DEBUG_OUTPUT("Help request");

		// get rid of our malloc'ed key
		MENUFREE(key_entered);
		showHelp();
		return this;
	}

	if ( ( parent_menu == NULL) && strncmp_P(key_entered, exit_key, strlen_P(exit_key)) == 0) {
		// get rid of our malloc'ed key

		SUI_MENU_DEBUG_OUTPUT("Exit request");

		MENUFREE(key_entered);
		return NULL;
	}


#ifdef SUI_ENABLE_MODES
	// check for program mode command...
	if (strncmp_P(key_entered, key_mode_program, strlen_P(key_mode_program)) == 0)
	{
			SUI_MENU_DEBUG_OUTPUT("Entering program mode");
			sui_driver->setMode(SUIMode_Program);
			MENUFREE(key_entered);

			char sepChar[2];
			sepChar[0] = SUI_SERIALUI_PROG_STR_SEP_CHAR;
			sepChar[1] = '\0';

			char outBuf[SUI_SERIALUI_PROGMEM_STRING_ABS_MAXLEN];
			outBuf[0] = '\0';
			strcat(outBuf, sepChar);

			strcat_P(outBuf, prog_mode_info_VERSION);
			strcat(outBuf, sepChar);



#ifdef SUI_MENU_ENABLE_SUBMENUS
				strcat_P(outBuf, up_key);
#else
				strcat(outBuf, "X");
#endif	/* SUI_MENU_ENABLE_SUBMENUS */
			strcat(outBuf, sepChar);

			strcat_P(outBuf, exit_key);
			strcat(outBuf, sepChar);

			strcat_P(outBuf, error_generic);
			strcat(outBuf, sepChar);


			strcat_P(outBuf, prog_mode_info_helpkey);
			strcat(outBuf, sepChar);

			strcat_P(outBuf, help_key_prog_commandprefix);
			strcat(outBuf, sepChar);

			strcat_P(outBuf, help_key_prog_submenuprefix);
			strcat(outBuf, sepChar);


			strcat_P(outBuf, help_sep_prog);
			strcat(outBuf, sepChar);



			strcat_P(outBuf, prog_mode_info_moreprompt_string);
			strcat(outBuf, sepChar);



			strcat_P(outBuf, prog_mode_info_moreprompt_num);
			strcat(outBuf, sepChar);

			strcat(outBuf, SUI_SERIALUI_PROMPT);
			strcat(outBuf, sepChar);

			strcat_P(outBuf, prog_mode_info_EOT);
			strcat(outBuf, sepChar);

#ifdef SUI_ENABLE_STREAMPROMPTING
			strcat_P(outBuf, prog_mode_info_moreprompt_stream);
			strcat(outBuf, sepChar);
#endif


			strcat_P(outBuf, prog_mode_terminate_gui);
			strcat(outBuf, sepChar);

			sui_driver->print(strlen(outBuf) + 1, DEC);
			sui_driver->println(outBuf);

			return this;
	}

	if (strncmp_P(key_entered, key_mode_user, strlen_P(key_mode_user)) == 0) {
		SUI_MENU_DEBUG_OUTPUT("Entering 'user' mode");
		sui_driver->setMode(SUIMode_User);
		MENUFREE(key_entered);
		return this;
	}
#endif	/* SUI_ENABLE_MODES */

	if (strncmp_P(key_entered, key_ping_command, strlen_P(key_ping_command)) == 0) {

			MENUFREE(key_entered);
			pingRespond();
			return this;
		}


	// get rid of our malloc'ed key
	unknownCommand(key_entered);
	MENUFREE(key_entered);
	return this;

}
Esempio n. 4
0
ECLParsingResult CCommandLineParser::parse() {
	if(m_parse_result!=Parse_none_found) return(m_parse_result);
	if(m_args.size()<2) return(Parse_none_found);
	
	m_cur_task=NULL;
	SCLTask* task;
	bool bFound;
	
	for(int i=0; i<m_argc; ++i) {
		const string& arg=m_args[i];
		if(arg.length()>1 && arg[0]=='-') {
			if(arg[1]=='-') { //name
				string arg_name=arg.substr(2);
				if((task=findTask(arg_name))) {
					m_cur_task=task;
					task->bGiven=true;
				} else {
					SCLSwitch* s=NULL;
					SCLParam* p=NULL;
					if(m_cur_task) {
						s=m_cur_task->findSwitch(arg_name);
						p=m_cur_task->findParam(arg_name);
					}
					if(!s) s=m_global.findSwitch(arg_name);
					if(!p) p=m_global.findParam(arg_name);
					
					if(s) {
						s->bGiven=true;
						bFound=true;
					} else if(p) {
						p->values.push(m_args[i+1]);
						++i;
					} else {
						return(unknownCommand(arg));
					}
				}
			} else { //short name
				for(size_t k=1; k<arg.length(); ++k) {
					char arg_name=arg[k];
					if((task=findTask(arg_name))) {
						m_cur_task=task;
						task->bGiven=true;
					} else {
						SCLSwitch* s=NULL;
						SCLParam* p=NULL;
						if(m_cur_task) {
							s=m_cur_task->findSwitch(arg_name);
							p=m_cur_task->findParam(arg_name);
						}
						if(!s) s=m_global.findSwitch(arg_name);
						if(!p) p=m_global.findParam(arg_name);
						
						if(s) {
							s->bGiven=true;
							bFound=true;
						} else if(p && k==arg.length()-1) {
							p->values.push(m_args[i+1]);
							++i;
						} else {
							return(unknownCommand(string("")+arg_name));
						}
					}
				}
			}
		} else {
			return(unknownCommand(arg));
		}
	}
	
	m_cur_task=NULL;
	
	return(m_parse_result=Parse_success);
}
void AMPIC887ConsoleCommandParser::interpretCommandImplementation(const QString &command)
{
    if(command == "quit") {

        emit quit();

    } else if (command == "help") {

        emit help();

    } else if( command == "status") {

        emit status();
    } else if(command.startsWith("init")) {

        emit initializeControllerCommandIssued();
    } else if(command.startsWith("activate")) {

        QStringList commandParts = command.split(" ");
        if(commandParts.count() == 1) {
            emit unknownCommand(QString("Cannot active a controller with no name specified."));
        } else if(commandParts.count() > 2) {
            emit unknownCommand(QString("Cannot active more than one controller at a time."));
        } else {
            emit activeControllerChangeRequested(commandParts.at(1));
        }

    } else if (command.startsWith("STP")) {

        emit stopCommandIssued();

    } else if(command.startsWith("HLT")) {

        emit haltCommandIssued(axesFromCommandString(command));

    } else if(command.startsWith("HPA?")) {

        emit availableParametersCommandIssued();

    } else if(command.startsWith("MST?")) {

        emit motionStatusCommandIssued();

    } else if (command.startsWith("CCL?")) {

        emit commandLevelCommandIssued();

    } else if (command.startsWith("POS?")) {

        emit currentPositionCommandIssued(axesFromCommandString(command));

    } else if (command.startsWith("SCT?")) {

        emit cycleTimeCommandIssued();

    } else if (command.startsWith("IDN?")) {

        emit deviceIdentificationCommandIssued();

    } else if (command.startsWith("NLM?")) {

        emit lowSoftLimitCommandIssued(axesFromCommandString(command));

    } else if (command.startsWith("PLM?")) {

        emit highSoftLimitCommandIssued(axesFromCommandString(command));

    } else if (command.startsWith("LIM?")) {

        emit limitSwitchStatusCommandIssued(axesFromCommandString(command));

    } else if (command.startsWith("TMN?")) {

        emit minPositionCommandIssued(axesFromCommandString(command));

    } else if (command.startsWith("TMX?")) {

        emit maxPositionCommandIssued(axesFromCommandString(command));

    } else if (command.startsWith("ONT?")) {

        emit onTargetCommandIssued(axesFromCommandString(command));

    } else if (command.startsWith("SPI?")) {

        emit pivotPointCommandIssued(axesFromCommandString(command, AMPIC887AxisCollection::LinearAxes));

    } else if (command.startsWith("MOV?")) {

        emit targetPositionCommandIssued(axesFromCommandString(command));

    } else if (command.startsWith("PUN?")) {

        emit positionUnitsCommandIssued(axesFromCommandString(command));

    } else if (command.startsWith("HDR?")) {

        emit availableRecorderOptionsCommandIssued();

    } else if (command.startsWith("DRT?")) {

        emit recordTriggerCommandIssued();

    } else if (command.startsWith("DRR?")) {

        handleDataRecordValuesInput(command);

    } else if (command.startsWith("DRC?")) {

        handleRecordConfigInput(command);

    } else if (command.startsWith("FRF?")) {

        emit referencedStateCommandIssued(axesFromCommandString(command));

    } else if (command.startsWith("RTR?")) {

        emit recordRateCommandIssued();

    } else if (command.startsWith("SVO?")) {

        emit servoModeCommandIssued();

    } else if(command.startsWith("SSL?")) {

        emit softLimitStatusCommandIssued(axesFromCommandString(command));

    } else if (command.startsWith("SST?")) {

        emit stepSizeCommandIssued(axesFromCommandString(command));

    } else if (command.startsWith("VLS?")) {

        emit systemVelocityCommandIssued();

    } else if(command.startsWith("MOV")) {

        bool parseSuccess = false;
        AMPIC887AxisMap<double> parsedArguments = axisMapFromCommandString(command, &parseSuccess);
        if(parseSuccess) {
            emit moveCommandIssued(parsedArguments);
        }

    } else if(command.startsWith("FRF")) {

        emit referenceMoveCommandIssued(axesFromCommandString(command));

    } else if(command.startsWith("MVR")) {

        bool parseSuccess = false;
        AMPIC887AxisMap<double> parsedArguments = axisMapFromCommandString(command, &parseSuccess);
        if(parseSuccess) {
            emit moveRelativeCommandIssued(parsedArguments);
        }

    } else if(command.startsWith("CCL")) {

        handleSetCommandLevelInput(command);

    } else if (command.startsWith("SCT")) {

        bool parseSuccess = false;
        double parsedArgument = doubleValueFromCommandString(command, &parseSuccess);
        if(parseSuccess) {
            emit setCycleTimeCommandIssued(parsedArgument);
        }

    } else if (command.startsWith("PLM")) {

        bool parseSuccess = false;
        AMPIC887AxisMap<double> parsedArguments = axisMapFromCommandString(command, &parseSuccess);
        if(parseSuccess) {
            emit setHighSoftLimitsCommandIssued(parsedArguments);
        }

    } else if (command.startsWith("NLM")) {

        bool parseSuccess = false;
        AMPIC887AxisMap<double> parsedArguments = axisMapFromCommandString(command, &parseSuccess);
        if(parseSuccess) {
            emit setLowSoftLimitsCommandIssued(parsedArguments);
        }

    } else if (command.startsWith("SPI")) {

        bool parseSuccess = false;
        AMPIC887AxisMap<double> parsedArguments = axisMapFromCommandString(command, &parseSuccess);
        if(parseSuccess) {
            emit setPivotPointsCommandIssued(parsedArguments);
        }

    } else if (command.startsWith("DRT")) {

        handleSetRecordTriggerInput(command);

    } else if (command.startsWith("DRC")) {

        handleSetRecordConfigInput(command);

    } else if(command.startsWith("RTR")) {

        bool parseSuccess = false;
        double value = doubleValueFromCommandString(command, &parseSuccess);
        if(parseSuccess) {
            emit setRecordRateCommandIssued(value);
        }

    } else if (command.startsWith("SVO")) {

        bool parseSuccess = false;
        bool value = boolValueFromCommandString(command, &parseSuccess);
        if(parseSuccess) {
            emit setServoModeCommandIssued(value);
        }

    } else if (command.startsWith("SSL")) {

        handleSetSoftLimitStatusInput(command);

    } else if (command.startsWith("SST")) {

        bool parseSuccess = false;
        AMPIC887AxisMap<double> parsedArguments = axisMapFromCommandString(command, &parseSuccess);
        if(parseSuccess) {
            emit setStepSizesCommandIssued(parsedArguments);
        }

    } else if (command.startsWith("VLS")) {

        bool parseSuccess = false;
        double parsedArgument = doubleValueFromCommandString(command, &parseSuccess);
        if(parseSuccess) {
            emit setSystemVelocityCommandIssued(parsedArgument);
        }

    } else {
        emit unknownCommand("Command not recognized. Type 'help' to see a list of commands");
    }

    if(command != "quit") {
        emit parseComplete();
    }
}