Beispiel #1
0
void processCmd(char cmd, FILE *fp, int doCmd) {
	long pos = 0;
	char c;

	switch(cmd) {
		case '+':
			if (doCmd)
				++*p;
			break;
		case '-':
			if (doCmd)
				--*p;
			break;
		case '>':
			if (doCmd)
				++p;
			break;
		case '<':
			if (doCmd)
				--p;
			break;
		case '.':
			if (doCmd)
				putchar(*p);
			break;
		case ',':
			if (doCmd)
				*p = getchar();
			break;
		case '[':
			pos = ftell(fp);
			if (*p) {
				doCmd = YES;
				while (*p) {
					fseek(fp, pos, SEEK_SET);
					c = fgetc(fp);
					while( c!= ']' && c != EOF) {
						processCmd(c, fp, YES);
						c = fgetc(fp);
					}
				}
			} else {
				c = fgetc(fp);
				while (c != ']' && c!= EOF) {
					processCmd(c, fp, NO);
					c = fgetc(fp);
				}
			}
	}
}
Beispiel #2
0
void ELM327Emu::loop()
{
    int incoming;

    while (serialInterface->available()) {
        incoming = serialInterface->read();

        if (incoming != -1) { //and there is no reason it should be -1
            if (incoming == 13 || ibWritePtr > 126) { // on CR or full buffer, process the line
                incomingBuffer[ibWritePtr] = 0; //null terminate the string
                ibWritePtr = 0; //reset the write pointer

                if (Logger::isDebug()) {
                    Logger::debug(this, incomingBuffer);
                }

                processCmd();

            } else { // add more characters
                if (incoming != 10 && incoming != ' ') { // don't add a LF character or spaces. Strip them right out
                    incomingBuffer[ibWritePtr++] = (char) tolower(incoming);   //force lowercase to make processing easier
                }
            }
        } else {
            return;
        }
    }
}
Beispiel #3
0
static void process( int fd )
{
   std::string curCmd ;

   while( 1 )
   {
      char inBuf[256];
      int numRead = read( fd, inBuf, sizeof( inBuf ) - 1 );
      if( 0 < numRead )
      {
         inBuf[numRead] = '\0' ;
         for( int i = 0 ; i < numRead ; i++ )
         {
            char const c = inBuf[i];
            if( ( '\r' == c ) || ( '\n' == c ) )
            {
               if( 0 < curCmd.size() )
               {
                  processCmd( curCmd, fd );
                  curCmd = "" ;
               }
            }
            else
               curCmd += c ;
         } // parse for complete lines
      }
      else
         break; // eof
   }
}
Beispiel #4
0
void CommandControl::parseCmd(byte *rxData, byte rxLen){
  //copy new rx data to rx buffer, and increase the rx len in buffer
  if((rxLenInBuffer + rxLen) <= RX_BUFFER_LEN){
    memcpy(rxBuffer+rxLenInBuffer,rxData,rxLen);
    rxLenInBuffer += rxLen;
  }
  else{//copy new rx data to the beginning of rx buffer, as it is overflowing
    memcpy(rxBuffer,rxData,rxLen);
    rxLenInBuffer = rxLen;
  }
  
  //find the start flag of the command to be processed
  int startPos = 0;
  while(1){
    //Serial.println("here1");
    if((startPos = findCmd()) >= 0){
      //Serial.println(startPos);
      processCmd((byte)startPos);
    }
    else{
      break;
    }
  }
  
}
//-----------------------------
// Name : update()
//-----------------------------
void PoetryGen::update()
{
    int cmd_ind = 0;
    while(( cmd_ind = getCmdInd( prompt() )))
    {
        processCmd( cmd_ind );
    }
}
int32 main(int32 argc, char *argv[])
{

  if (argc <= 1 || argc > 2)
  {
    printCmdHelp();
    exit(0);
  }

  processCmd(argv[1]);
}
Beispiel #7
0
readStartup()
{
FILE *fp;
char line[1024];

    if( !(fp=fopen(".sre_init", "r")) )
        return(0);

    while(fgets(line, 1000, fp) != NULL)
        processCmd(line);
    fclose(fp);
}
Beispiel #8
0
int main(){
    while(scanf("%d\n",&maxCount)==1){
		init();
		scanf("%d\n",&maxRemove);	
		for(i=0;i<maxRemove;i++){
                       int x;
                       scanf("%d",&x);
                       reList[x] = true;
                   }
		while(scanf("%s",cmd)==1 
                      &&processCmd(cmd[0]));
		printf("\n");
	}
	return 0;
}
Beispiel #9
0
void Configuration::check() {
    if (!request) return;
    // have a request pending
    switch (buffer[1]&0xC0) {
        case CFG_CMD_READ:
            processRead(buffer, length);
            break;
        case CFG_CMD_WRITE:
            processWrite(buffer, length);
            break;
        case CFG_CMD_OPERATION:
            processCmd(buffer, length);
            break;
    }
}
//process serial interface
void Interface::processSerial(void)
{
  switch (serialState){
  case COMMAND_MODE:
    {
      processCmd();
      break;
    }//when in command mode,receive and reslove it 
  case DATA_MODE:
    {
      processData();
      break;
    }//when in data mode, receive and process it
  default:
    break;
  }
}
Beispiel #11
0
void loop() {
	int read = 1;

	crtDir = (char *)malloc(256*sizeof(char));
	getcwd(crtDir, 256);
	//printf("cd: %s\n", crtDir);

	while (read) {
		char *line = readLine();
		if (strcmp(line, "exit") == 0) {
			read = 0;
			break;
		}
		processCmd(line);
	}
	free(crtDir);
}
Beispiel #12
0
/* Read the .srerc file if it exists */
readRC()
{
char *s;
char line[1024];
FILE *fp;

    if( (s=getenv("HOME")) == NULL )
	return(0);
    sprintf(line, "%s/.srerc", s);
    if( !(fp=fopen(line, "r")) )
	return(0);
    while(fgets(line, 1000, fp) != NULL)
	if( processCmd(line) == 0)
	    {
	    printf("Bad .srerc init file; correct and restart\n");
	    exit(0);
	    }
    fclose(fp);
    return(1);
}
Beispiel #13
0
int main(int argc, char **argv) {
	if (argc != 2) {
		fprintf(stderr, "Usage: %s <program name>\n", argv[0]);
		exit(1);
	}

	FILE *fp = fopen(argv[1], "r");

	if (!fp) {
		fprintf(stderr, "file '%s' not found\n", argv[1]);
		exit(1);
	}

	char c = fgetc(fp);
	while (c != EOF) {
		processCmd(c, fp, YES);
		c = fgetc(fp);
	}

	fclose(fp);

	return 0;
}
Beispiel #14
0
int main(int argc, char *argv[]) 
{
  char cmd_buff[1024];
  char *pinput;
  char *fname = argv[1];
  char *mode = "r";
  FILE *f= fopen(fname, mode);
  cmd_line *c, *backup;
  char* extra_buff;
  char* test = malloc(sizeof(char)*514);
  char buf[1024];

  if(argv[2] != NULL){ /*if trying to run batch with 2 filenames*/                                                
    err();
    exit(0);
  }

  if(f == NULL){ /*if read is invalid*/
    err();
    exit(0);
  }
  dw = getcwd(buf, 1024); /*for global var accessible to function calls*/
  while (1) {
    /*INTERACTIVE MODE*/
    if(argv[1] == NULL){ 
      myPrint("myshell> ");
      pinput = fgets(cmd_buff, 100, stdin);
      c = parseCmdLine(cmd_buff);
      backup = c;
      while(backup != NULL){
        processCmd(backup->c);
        backup = backup->next;
      }
    /*BATCH MODE*/ 
    }  else if(argv[1] != NULL){
      memset(cmd_buff,'\0',514);
      pinput = fgets(cmd_buff, 514, f);
      
      if (!pinput) { /*invalid fgets*/
        exit(0);
      }

      if((extra_buff = strstr(cmd_buff,"\n"))!=NULL) {/*check if whole command has been taken in*/
        if (check_space(cmd_buff)==0) /*if all blank spaces, don't print*/
          continue;
        else {
      myPrint(cmd_buff); /*print command*/
      collapsed_str(cmd_buff,test); 
      /*CATCH ERRORS RIGHT AWAY*/
      if(exception1(test)==1){
        err();
      }
      else if(exception2(test)==1) {
        err();
      }
      else if(exception3(test)==1) {
        continue;
      }
      /*PARSE*/
      else {
        c = parseCmdLine(cmd_buff);
        backup = c;
        while(backup != NULL){
          processCmd(backup->c);
          
          backup = backup->next;
        }
        free_cmdline(c);
      }
    }
      }
      /*COMMAND TOO LONG*/
      if(extra_buff==NULL) {
        myPrint(cmd_buff);

        while(fgets(cmd_buff,1024,f)!=NULL) {/*keep getting more chars while you can*/
          if((strlen(cmd_buff)<1023) || cmd_buff[1022]=='\n') { /*if new one is shorter than max, break*/
            myPrint(cmd_buff);
            memset(cmd_buff, '\0', 1024);
            break;
          }
          else 
            myPrint(cmd_buff); /*else keep getting*/
          memset(cmd_buff, '\0', 1024);
        }
    err();
      }
      
    }
  }
  free(test);   
fclose(f);
return 0;
}
int main(int argc,char** argv){

  int yes = 1;
  int i,j,rv;
  char *commands ;
  int length = 0 ;
  char array[100];
  char *cmdargs;
  char *res ;
  cmdargs = array;
  head = NULL ;
  headRef = &head; 
  int ln1;
  int obytes ;
  int retSel; 
  char ipremote[INET_ADDRSTRLEN];

  /* Bufferrs for read and write */

  char buf[350] ;
  char opbuff[350];
 
  
  

  if(argc != 3){

   printf("Invalid call ..exiting............\n");
   exit(0);
  }
  
  if(strcasecmp(argv[1],"s") != 0 &&  strcasecmp(argv[1],"c") != 0 ){

    printf("Invalid arguments ..exiting............\n");
    exit(0);

  }


  int iport ;
  iport = atoi(argv[2]);

  if(iport <= 1024 || iport > 45355){
 
     printf("Invalid port ..exiting............\n");
    exit(0);   

  }

 /* Setting the client or server flag */

  if(strcasecmp(argv[1],"S")== 0 ){
      serverclientflag = 1;
  }else{

    serverclientflag = 2 ;
  }

  portnum = argv[2]; // setting the portnum to global variable
  
  //Method to initialise the system

  initialize();
 
  
  for(;;){
 

  read_fds = master ;

  if(select(fdmax + 1,&read_fds,NULL,NULL,NULL) == -1){
    perror("select");
    exit(4);
  }

        //run through the existing connections looking for data
  
  for(i=0 ; i<= fdmax ; i++){

         if(FD_ISSET(i,&read_fds)){ // we got one conncetion

            if(i == 0){
             //read the command from the terminal
             //printf("[PA1]$");
             if((res=fgets(cmd,sizeof cmd,stdin) )!= NULL){
               length = strlen(cmd);
               cmd[length-1] = '\0';   
                //fflush(stdin);
                commands = cmd ;
                 returnfd = processCommand(commands,serverclientflag,portnum); //get the file descriptor for server connection 
                 fflush(stdout);

                 if(returnfd == -1){
                  
                   printf("Invalid arguments to command.");
                   fflush(stdout); 
                   printf("\n[PA1]$:");
                   fflush(stdout); 
                 }
               }
                 
             }else{
              
              if(i == listener){
                //handle new connection accept the connection and add it to the read set
                addrlen = sizeof remoteaddr ;
                newfd = accept(listener,(struct sockaddr *)&remoteaddr,&addrlen);
                
                if(newfd == -1){
                 perror("accept");
                 } else{

                    /* Getting the ip address of the remote connection */
                    /*And adding the  connection to list*/

                         struct sockaddr_in *s = (struct sockaddr_in *)&remoteaddr;
                         struct hostent *hnet; 
                         inet_ntop(AF_INET, &s->sin_addr, ipremote, sizeof ipremote);
                         int port = s->sin_port;
                         char tempport[33]; 
                         sprintf(tempport,"%d",port);
                         //hnet = gethostbyaddr(&ipremote,sizeof ipremote,AF_INET);
                         if(serverclientflag == 1){

                           getConnectDetails(newfd,tempport,&head,ipremote);
                         }else{

                           getConnectDetails(newfd,tempport,&headConnectdNode,ipremote);
                         }
                         
                         //adding the connection details in the memory  
                         //addClientstoList(&headConnectdNode,ipremote,portnum,hnet->h_name,iconnid,i); 
                         //portnum;
                    printf("----------A new connection requested from :%s",inet_ntop(remoteaddr.ss_family,get_in_addr((struct sockaddr*)&remoteaddr),remoteIP,INET6_ADDRSTRLEN));
                    printf ("\n---------------------Connected to :%s-----------\n",inet_ntop(remoteaddr.ss_family,get_in_addr((struct sockaddr*)&remoteaddr),remoteIP,INET6_ADDRSTRLEN));
                    
                    FD_SET(newfd,&master);
                    
                    //End Send the updated list of clients
                    if(newfd > fdmax){
                      fdmax = newfd ;
                   }
                    // process register command
                  }

                }else{

                     /*handle data from the client
                     clear the data from the buffer
                     */

                     if((nbytes = recv(i,buf,sizeof buf,0)) <= 0 ){
                       fflush(stdout);
                       if(nbytes == 0 ){
                         //connection closed
                          if(serverclientflag == 1){                       
                          //Reset the client list and push data to other clients
                           updateClientList(i,serverclientflag) ;   
                           buildclist(head);
                           sendupdate(i,listener);  
                          }else{
                         
                          updateClientList(i,serverclientflag);  
                          iconnid = iconnid - 1 ; 
                          }                              
                          printf("\n[PA1]$:");
                          fflush(stdout);
                        }else{
                         perror("recv");
                         printf("\n wrog connection");
                        } 
                      //close(i);
                      FD_CLR(i,&master);
                      printf("\n[PA1]$:"); 
                      }else{

                            //printf("\n Bytes received is %d",nbytes); 
                            
                             //we have data from the client
                            //buf[nbytes] ='\0';
                            //printf("\nBytes %d",nbytes) ;
                            //Get the data about the server
                            if(serverclientflag == 1){
                            
                                //Adding the server details first
                                //getClientDetails(i,buf,&head);
                                char ips[INET6_ADDRSTRLEN] ;
                                char *rip = ips ;
                                updateConnctdPort(i,buf,&head,rip);
                                //addClientstoList(&head,ipaddress,portnum,"dokken.cse.buffalo.edu",1);
                                ln1 =buildclist(head);
                                strcpy(opbuff,sbcheckdata);
                                obytes = send(i,opbuff,sizeof opbuff,0);
                                if(obytes < 0){
                                perror("senddata");
                            }
                               //obytes = send(i,"HEllo",6,0);
                              fflush(stdout);
                              //printf("\n Bytes transferred %d",obytes);
                           }else{

                            processCmd(buf,i,nbytes);
                            memset(buf,0,sizeof buf);
                            
                           }
                         
                           if(serverclientflag == 1 ){

                          //Send the updated data to all the clients
                          for(j=0;j<=fdmax;j++){
                             if(FD_ISSET(j,&master)){
                              //need not send the data to listener && this machine
                              if(j!=listener && j!=i){

                                if(send(j,opbuff,sizeof opbuff,0)== -1){
                                //perror("Sending the list data");
                              }
                              }
                            }
                          }
                        }
                        
                        //Send the updated data to all the clients
                       fflush(stdout);
                       printf("\n[PA1]$:");
                       fflush(stdout);
                       
                 }
               }

             }

           }
        } //end of the readset polling loop
    } //end of the running loop 
  
    return 0 ;
  }     
void
Z47Controller::processData(BYTE val)
{
    debugss(ssH47, ALL, "0x%02x\n", val);

    switch (curState)
    {
        case st_None_c:
            debugss(ssH47, ALL, "st_None_c\n");
            processCmd(val);
            break;

        case st_Boot_c:
            debugss(ssH47, ERROR, "st_Boot_c\n");
            break;

        case st_ReadCntrlStat_c:
            debugss(ssH47, ERROR, "Invalid state st_ReadCntrlStat_c\n");
            // processReadControlStatus();
            break;

        case st_ReadAuxStat_c:
            debugss(ssH47, INFO, "st_ReadAuxStat_c - 0x%02x\n", val);
            processReadAuxStatus(val);
            break;

        case st_LoadSectorCount_c:
            debugss(ssH47, INFO, "st_LoadSectorCount_c\n");
            processLoadSectorCount(val);
            break;

        case st_ReadLastAddr_c:
            debugss(ssH47, ERROR, "st_ReadLastAddr_c\n");
            break;

        case st_ReadSectors_c:
            debugss(ssH47, ERROR, "st_ReadSectors_c\n");
            break;

        case st_WriteSectors_c:
            debugss(ssH47, ERROR, "st_WriteSectors_c\n");
            break;

        case st_ReadSectorsBuffered_c:
            debugss(ssH47, INFO, "st_ReadSectorsBuffered_c\n");
            processReadSectorsBufffered(val);
            break;

        case st_WriteSectorsBuffered_c:
            debugss(ssH47, INFO, "st_WriteSectorsBuffered_c\n");
            processWriteSectorsBufffered(val);
            break;

        case st_WriteSectorsAndDelete_c:
            debugss(ssH47, ERROR, "st_WriteSectorsAndDelete_c\n");
            break;

        case st_WriteSectorsBufferedAndDelete_c:
            debugss(ssH47, ERROR, "st_WriteSectorsBufferedAndDelete_c\n");
            break;

        case st_Copy_c:
            debugss(ssH47, ERROR, "st_Copy_c\n");
            break;

        case st_FormatIBM_SD_c:
            debugss(ssH47, ERROR, "st_FormatIBM_SD_c\n");
            break;

        case st_Format_SD_c:
            debugss(ssH47, INFO, "st_Format_SD_c\n");
            processFormatSingleDensity(val);
            break;

        case st_FormatIBM_DD_c:
            debugss(ssH47, INFO, "st_FormatIBM_DD_c\n");
            processFormatIBMDoubleDensity(val);

            break;

        case st_Format_DD_c:
            debugss(ssH47, ERROR, "st_Format_DD_c\n");
            processFormatDoubleDensity(val);
            break;

        default:
            debugss(ssH47, ERROR, "Unknown state\n");

            break;
    }
}
Beispiel #17
0
void WEXPORT WComboBox::select( int index )
{
        sendMsg( CB_SETCURSEL, index, 0 );
		processCmd( 0, CBN_SELCHANGE);     //should we have this??
}
Beispiel #18
0
int main(int argc, CHAR *argv[])
{
	SOCKET clientSock = INVALID_SOCKET;
	struct sockaddr_in remoteSAddr;		/* Address structure of socket to connect */

	WSADATA WSStartData;				/* Socket library data structure   */
	CLIENT_ARGS cargs;
	

	// check application args
	if (!processArgs(argc, argv, &cargs)) {
		_tprintf(_T("usage: cloudFSC [-s <serverIp>] { -d(ownload) <file> | -u(pload) <file> | -l(ist) }+ \n"));
		return 1;
	}

	// sets the current directory to the local file folder
	SetCurrentDirectory(CLIENT_FOLDER);

	/*	Initialize the WS library. Ver 2.0 */
	if (WSAStartup(MAKEWORD(2, 0), &WSStartData) != 0) {
		_tprintf(_T("Cannot support sockets lib version 2.0"));
		return 1;
	}
 
	/* Connect to the server */
	/* Follow the standard client socket/connect sequence */
	clientSock = socket(AF_INET, SOCK_STREAM, 0);

	if (clientSock == INVALID_SOCKET) {
		_tprintf(_T("Failed client socket() call"));
		WSACleanup();
		return 1;
	}

	memset(&remoteSAddr, 0, sizeof(remoteSAddr));
	remoteSAddr.sin_family = AF_INET;
	if (cargs.serverIP != NULL)
		remoteSAddr.sin_addr.s_addr = inet_addr(argv[1]);
	else
		remoteSAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
	if (remoteSAddr.sin_addr.s_addr == INADDR_NONE) {
		_tprintf(_T("Invalid server IP address!\n"));
	}
	else {
		remoteSAddr.sin_port = htons(SERVER_PORT);

		DWORD conVal = connect(clientSock, (struct sockaddr *)&remoteSAddr,
			sizeof(remoteSAddr));
		if (conVal == SOCKET_ERROR) {
			_tprintf(_T("Failed client connect() call)"));
		}
		else {
			_tprintf(_T("Connection done!\n"));

			LONGLONG start = GetTickCount64();
			BOOL error = FALSE;
			for (int i = 0; i < cargs.nCmds; ++i) {
				int res = processCmd(clientSock, &cargs.cmds[i]);
				if (res != STATUS_OK) {
					error = TRUE;
					showInfoError(res, &cargs.cmds[i]);
				}
					 
			}
			TCHAR *extra = error ? _T("(with errors)") : _T("");
			_tprintf(_T("done %s in %I64ums!\n"), extra, GetTickCount64() - start);
			// Cleanup
			shutdown(clientSock, SD_BOTH); /* Disallow sends and receives */
		}
	}
	closesocket(clientSock);
	WSACleanup();

	return 0;
}