int try_command(int argc, char* argv[], GString* output) { if (argc <= 1) { return HERBST_NEED_MORE_ARGS; } (void)SHIFT(argc, argv); call_command(argc, argv, output); return 0; }
int main(int argc, char **argv) { int c, lind, i, err = 0; struct option opts[] = { {"help", 0, 0, 'h'}, {"tcp", 0, 0, 't'}, {"nosdp", 0, 0, 's'}, {"version", 0, 0, 'V'}, {0, 0, 0, 0} }; if (affix_init(argc, argv, LOG_USER)) { fprintf(stderr, "Affix initialization failed\n"); return 1; } atexit(do_exit); for (;;) { c = getopt_long(argc, __argv, "+htvsVi:", opts, &lind); if (c == -1) break; switch (c) { case 'h': usage(); return 0; break; case 'v': verboseflag = 1; break; case 'i': strncpy(btdev, optarg, IFNAMSIZ); break; case 's': sdpmode = 0; break; case 'V': printf("Affix version: %s\n", affix_version); return 0; break; case ':': printf("Missing parameters for option: %c\n", optopt); return 1; break; case '?': printf("Unknown option: %c\n", optopt); return 1; break; } } if (__argv[optind] && sscanf(__argv[optind], "bt%d", &i) > 0) { /* interface name */ sprintf(btdev, "bt%d", i); optind++; } if (__argv[optind] == NULL) { usage(); //err = cmd_prompt(NULL); goto exit; } err = call_command(cmds, __argv[optind++]); exit: return err; }
void parse_command() { int i = 0; int j = 0; int k = 0; uint8_t buf[USART0_RECEIVE_BUFFER_LEN]; uint8_t temp_buf[20]; uint8_t command[20]; uint8_t data[20]; // Transfer to input to local buffer // TODO: This could cause problems if the ISR is loading new // data while the buffer is being transferred. FIX THIS strcpy((char *)buf,(char *)usart0_receive_buffer); //#define TERMINATOR '!' //#define DELIMITER ',' //#define SEPARATOR ' ' // Show each command and data for(i = 0; i < strlen((const char *)buf); i++) { if(buf[i] != DELIMITER)// printf("i = %d\n",i); { temp_buf[j++] = buf[i]; } else // Process command unit { temp_buf[j] = '\0'; // Turn it into a string j = 0; // Extract the command name for(j = 0; j < 20; j++) { // Validate that command is all alphabetic or numeric if(temp_buf[j] != SEPARATOR) { if(isalnum(temp_buf[j])) { command[k++] = temp_buf[j]; } else { printf("ERROR in parse_command(): not a command - not alpha: %c\n",temp_buf[j]); return; } } else { command[k] = '\0'; k = 0; j++; for(k = 0; k < 20; k++) { if(temp_buf[j] != '\0')//DELIMITER) { // Validate that the data is all digits if(isdigit(temp_buf[j])) { data[k] = temp_buf[j++]; } else { printf_P(PSTR("ERROR in parse_command(): not a number - not digit: %c\n"),temp_buf[j]); return; } } else { data[k] = '\0'; //printf((const char *)data); //printf(" 3 \n"); k = 0; j = 0; /*printf((const char *)command); printf(" - \n"); printf((const char *)data); printf(" - \n");*/ //Call the command - bail out if it isn't valid if(!call_command((const char *)command,(const char *)data))return; break; } } break; } } } } /**/ /* //j = 0; for(j = 0; j < 20; j++) { // Extract the command name if(temp_buf[j] != SEPARATOR) { command[j] = temp_buf[j]; } else { command[j] = '\0'; printf((const char *)command); printf(" 3 \n"); // Extract the number for(k = 0; k < 20; k++) { if(temp_buf[j] != '\0') { data[k] = temp_buf[j++]; } else { data[k] = '\0'; printf((const char *)data); printf(" 4 \n"); //j = 20; //k = 20; break; } } } }*/ /*for(j = 0; j < 20; j++) { if(temp_buf[j] != SEPARATOR) { command[j] = temp_buf[j]; } else { printf((char *)command); for(k = 0; k < 20; k++) { if(temp_buf[j] != DELIMITER) { data[k] = temp_buf[j++]; } else { k = 0; printf((char *)data); } } } }*/ // Repeat until the terminator is found /* printf("before: %s\n",(const char*)usart0_receive_buffer); // Use strtok to replace the terminator with '\0' strtok((char *)usart0_receive_buffer,delim); printf("after: %s\n",(const char*)usart0_receive_buffer); */ /* void (*p_func)(uint16_t); for (i=0; pgm_read_word(&COMMANDS[i].PTEXT); i++) { // Show the command name printf_P(PSTR("%s\n"),(char*)pgm_read_word(&COMMANDS[i].PTEXT)); // Get the function and call it p_func = (PGM_VOID_P)pgm_read_word(&COMMANDS[i].PFUNC); p_func(i); } */ /* printf("Received: %s\n",usart0_receive_buffer); char cbuf[10]; // Replace the terminator with '\0' // Is it a valid command? int i = 0; while(COMMANDS[i]) { strcpy_P(cbuf,COMMANDS[i]); printf_P(PSTR("The receive buffer contained: %s, the COMMANDS[%d] was %s\n"),(const char *)usart0_receive_buffer, i, cbuf); //if( strcmp_P("COMMAND2!" , COMMANDS[i] ) == 0)//(const char *)usart0_receive_buffer cbuf,"COMMAND2!"))//!strcmp(cbuf,(const char *)usart0_receive_buffer)) if( strcmp_P((const char *)usart0_receive_buffer, COMMANDS[i]) == 0)//(const char *)usart0_receive_buffer cbuf,"COMMAND2!"))//!strcmp(cbuf,(const char *)usart0_receive_buffer)) { printf_P(PSTR("Was a command\n"));//"Command = %s\n"),COMMANDS[i++]); // clear it //memset(usart0_receive_buffer, '\0',USART0_RECEIVE_BUFFER_LEN); i++; } else { printf_P(COMMANDS[i]); printf_P(PSTR("Not a command\n")); i++; //printf("Not %s\n",COMMANDS[i++]); //delay(1500); } } delay(2000); // Report string length printf("Length: %d\n",strlen((const char *)usart0_receive_buffer)); // Load into local string // clear it usart0_receive_buffer_clear(); // refill it with'\0' memset(usart0_receive_buffer, '\0',USART0_RECEIVE_BUFFER_LEN); */ }