示例#1
0
bool CPigShip::OnNewChatMessage()
{

    ImodelIGC* pModel = GetIGC()->GetCommandTarget(c_cmdQueued);
    // Return if the chat message was not a command or no command is queued
    if (!pModel)
        return false;

    CommandID idCmd = GetIGC()->GetCommandID(c_cmdQueued);
    const char* pszVerb = (0 <= idCmd && idCmd < c_cidMax) ? c_cdAllCommands[idCmd].szVerb : "";
    debugf("...got a <%s> command to %s!!!\n",pszVerb,pModel->GetName());
    // Accept the queued command if auto accept is enabled
    return m_bAutoAcceptCommands ? SUCCEEDED(AcceptCommand(NULL)) : true;
}
示例#2
0
文件: ipv6cp.c 项目: vstakhov/mpd
static int
Ipv6cpSetCommand(Context ctx, int ac, char *av[], void *arg)
{
	Ipv6cpState const ipv6cp = &ctx->bund->ipv6cp;

	if (ac == 0)
		return (-1);
	switch ((intptr_t)arg) {
	case SET_ACCEPT:
		AcceptCommand(ac, av, &ipv6cp->conf.options, gConfList);
		break;

	case SET_DENY:
		DenyCommand(ac, av, &ipv6cp->conf.options, gConfList);
		break;

	case SET_ENABLE:
		EnableCommand(ac, av, &ipv6cp->conf.options, gConfList);
		break;

	case SET_DISABLE:
		DisableCommand(ac, av, &ipv6cp->conf.options, gConfList);
		break;

	case SET_YES:
		YesCommand(ac, av, &ipv6cp->conf.options, gConfList);
		break;

	case SET_NO:
		NoCommand(ac, av, &ipv6cp->conf.options, gConfList);
		break;

	default:
		assert(0);
	}
	return (0);
}
示例#3
0
int main(){
//------------------------------------------------------------------------------------
    struct termios origConfig;
    tcgetattr(0, &origConfig);      //get original configuration

    struct termios newConfig = origConfig;  // copy original config
    newConfig.c_lflag &= ~(ICANON|ECHO);    // Non-canonical mode; disable echo
    newConfig.c_cc[VMIN] = 1;
    newConfig.c_cc[VTIME] = 0;
    tcsetattr(0, TCSANOW, &newConfig);      //set new config
//------------------------------------------------------------------
    int fd [2];                                 //file descriptor for pipe
    int pid, pid2, status,status2, statusDirctory;       //P ID, status for cd
    char * commands[256];
    char * commands2[256];
    char * CommandLine;
    char c;
    char backspace = '\010', DeletBoutton = '\177', newLine = '\012';         //for reading commandLine char by bhar
    char left = '\103', right = '\104', Up = '\101', Down = '\102';                                          //history count
    char del = '\10';
    char sp = ' ';

    char * HistoryElem;
    char * History[10];
    int historyCount=0;
    int UpCount =0, UpUse =0;
    int len=0;
    int HistoryLocation=0;
    int last;
    int i =0;                           //just fixe a big problem that I cannot figure out why it happends

    while (1) {
        PrintPrompt();

        pipe(fd);
        commands[0] = NULL;
        commands2[0] = NULL;



        CommandLine = (char*) calloc(256, sizeof(char));
        HistoryElem = (char*) calloc(256, sizeof(char));

        len =0;
        historyCount=0;
        UpCount=0;

        if(HistoryLocation > 0){
            last = HistoryLocation-1;
        }

        while(1){
            read(0, &c, 1);                        //read the command Char by Char
            UpUse =0;
            if(c == newLine){
                break;
            }
//---------------------------------------------------
            else if(c == '\033'){}
            else if(c == '\133'){}

            else if (c == left || c == right) {}      //left & rights
//-------------------------------------------------------------------------


            else if(c == Up){                               //Up


                if(UpUse >= 0 && UpUse <= 8){UpUse++;}
                if(UpUse > 8){UpUse =0;
                UpUse++;
                }

                if(UpUse != 0){
                if(i == 0){}
                else if(History[0] != NULL){
                    if(historyCount != 9){
                        if(len != 0){
                            len--;
                            while(len >= 0){
                                CommandLine[len] = '\0';
                                write(1, &del, 1);
                                write(1, &sp, 1);
                                write(1, &del, 1);
                                len--;
                            }
                        }
                        if(last != 0){
                            if(History[last] != NULL){
                            if(UpCount == 1){
                                last--;
                                historyCount++;
                            }

                           write(1, History[last], strlen(History[last]));
                            strcat(CommandLine, History[last]);
                            len = strlen(History[last]);}
                        }
                        else {
                            if (History[last] != NULL){
                            if(History[9] != NULL){
                            if(UpCount ==1){
                                last =9;
                                historyCount++;
                            }
                           write(1, History[last], strlen(History[last]));
                            strcat(CommandLine, History[last]);
                            len = strlen(History[last]);
                        }
                            }
                        else if (History[last] != NULL){
                           write(1, History[last], strlen(History[last]));
                            strcat(CommandLine, History[last]);
                            len = strlen(History[last]);
                        }
                        }

                    }

                }
                UpCount =1;
                }
            }
//--------------------------------------------------------------------------
            else if(c == Down){                           //Down
                if (History[0] != NULL) {
                    if (historyCount != 0) {   // Prevent more than 10 forward history recalls
                        if (len != 0) {   // Clear any characters typed at prompt
                            len--;

                            while (len >= 0) {
                                CommandLine[len] = '\0';
                                write(1, &del, 1);
                                write(1, &sp, 1);
                                write(1, &del, 1);

                                len--;
                            }
                        }
                        if (last != 9) {  // Return forward history recall
                            last++;
                            write(1, History[last], strlen(History[last]));
                            strcat(CommandLine, History[last]);
                            len = strlen(History[last]);
                            historyCount--;
                        }
                        else {
                            if (History[9] != NULL) {
                                // Jump to bottom of full history array after top element is passed
                                write(1, History[0], strlen(History[0]));
                                strcat(CommandLine, History[0]);
                                last = 0;
                                historyCount--;
                            }
                        }
                    } // Do nothing if completed 10 forward history recalls
                    }


            }



// Delete a character--------------------------------------------------------------

            else if ((c == backspace) || (c == DeletBoutton)) {  // Backspace or delete

                    if(len > 0){
                    CommandLine[--len] = '\0';

                    write(1, &del, 1);
                    write(1, &sp, 1);
                    write(1, &del, 1);

                }
                else if (i <= 0){}  //do nothing if it is first letter
            }
//------------------------------------------------------------------------no special case
            else{
                i =1;
                CommandLine[len] = c;             //Make CommandLine ready
                write(1, &CommandLine[len], 1);
                len++;
            }

        }

//------------------------------------------------------------------

        printf("\n");               //equilalent to break key
        strcpy(HistoryElem, CommandLine);

        //if user doesn't enter any command but only enter line, then it simply print the prompt again.

        if(CommandLine[0] == '\0'){}
        else{

            if(HistoryLocation != 9){ History[HistoryLocation] = HistoryElem;
                HistoryLocation++;
            }
            else{ History[HistoryLocation] = HistoryElem;
                HistoryLocation=0;
            }
            AcceptCommand(CommandLine, commands, commands2);   //accept input

        }

//execution ----------------------------------------------------------------------------------------------
            if(commands[0] == NULL){}

            else if(strcmp(commands[0],"exit" )== 0){        //exit command
                break;
            }

            else if(strcmp(commands[0],"cd" )== 0){          //cd command

                statusDirctory = chdir(commands[1]);
                if (statusDirctory == -1) {
                    perror(NULL);
                }
            }

            pid = fork();
            pid2 = fork();
            if(pid2 == 0){
                close(fd[1]);
                dup2(fd[0],0);
                close(fd[0]);
                execvp(commands2[0], commands2);

                perror(NULL);
                exit(0);
            }

            if(pid == 0){
                if(strcmp(commands[0],"cd" )== 0){          //cd command
                exit(0);
            }
            else if(commands2[0] != NULL){
                close(fd[0]);
                dup2(fd[1],1);
                close(fd[1]);


                execvp(commands[0], commands);              //execute the commands
                perror(NULL);                               //error in case of invalid command
                exit(0);
                }

                execvp(commands[0], commands);              //execute the commands
                perror(NULL);                               //error in case of invalid command
                exit(0);
                               //if after an invalid command, there is exit it makes it work
            }
            else{
                close(fd[1]);

            waitpid(pid, &status, WUNTRACED);           //wait for process id

            waitpid(pid2, &status2, WUNTRACED);
            }

        }



    tcsetattr(0, TCSANOW, &origConfig);     // Restore config
    return 0;               //doooooooooooooooooooooooooooooone!!!!!!!!!!!!!!!

}
示例#4
0
文件: ipcp.c 项目: ZRouter/ZRouter
static int
IpcpSetCommand(Context ctx, int ac, char *av[], void *arg)
{
  IpcpState		const ipcp = &ctx->bund->ipcp;
  struct in_addr	*ips;

  if (ac == 0)
    return(-1);
  switch ((intptr_t)arg) {
    case SET_RANGES:
      {
	struct u_range	self_new_allow;
	struct u_range	peer_new_allow;
	int pos = 0, self_new_pool = -1, peer_new_pool = -1;

	/* Parse args */
	if (ac < 2)
	    return (-1);
	if (strcmp(av[pos], "ippool") == 0) {
	    self_new_pool = pos+1;
	    pos+=2;
	} else {
	    if (!ParseRange(av[pos], &self_new_allow, ALLOW_IPV4))
		return(-1);
	    pos++;
	}
	if (pos >= ac)
	    return (-1);
	if (strcmp(av[pos], "ippool") == 0) {
	    if ((pos + 1) >= ac)
		return (-1);
	    peer_new_pool = pos+1;
	    pos+=2;
	} else {
	    if (!ParseRange(av[pos], &peer_new_allow, ALLOW_IPV4))
		return(-1);
	    pos++;
	}
	if (pos != ac)
	    return (-1);

	if (self_new_pool >= 0)
	    strlcpy(ipcp->conf.self_ippool, av[self_new_pool], sizeof(ipcp->conf.self_ippool));
	else
	    ipcp->conf.self_ippool[0] = 0;
	if (peer_new_pool >= 0)
	    strlcpy(ipcp->conf.ippool, av[peer_new_pool], sizeof(ipcp->conf.ippool));
	else
	    ipcp->conf.ippool[0] = 0;
	ipcp->conf.self_allow = self_new_allow;
	ipcp->conf.peer_allow = peer_new_allow;

      }
      break;

    case SET_DNS:
      ips = ipcp->conf.peer_dns;
      goto getPrimSec;
      break;
    case SET_NBNS:
      ips = ipcp->conf.peer_nbns;
getPrimSec:
      if (!inet_aton(av[0], &ips[0]))
	Error("invalid IP address: \'%s\'", av[0]);
      ips[1].s_addr = 0;
      if (ac > 1 && !inet_aton(av[1], &ips[1]))
	Error("invalid IP address: \'%s\'", av[1]);
      break;

    case SET_ACCEPT:
      AcceptCommand(ac, av, &ipcp->conf.options, gConfList);
      break;

    case SET_DENY:
      DenyCommand(ac, av, &ipcp->conf.options, gConfList);
      break;

    case SET_ENABLE:
      EnableCommand(ac, av, &ipcp->conf.options, gConfList);
      break;

    case SET_DISABLE:
      DisableCommand(ac, av, &ipcp->conf.options, gConfList);
      break;

    case SET_YES:
      YesCommand(ac, av, &ipcp->conf.options, gConfList);
      break;

    case SET_NO:
      NoCommand(ac, av, &ipcp->conf.options, gConfList);
      break;

    default:
      assert(0);
  }
  return(0);
}