static int parseArg(Options * options, int argc, char **argv, int *pos, Option * option, int shift) { int ret; char *arg = argv[*pos] + shift; option->alreadyParsed = 1; if (option->type == OTflag) { *((unsigned char *) (option->res)) = 1; return 0; } else { if (argc <= *pos) { usageOptions(options, "given no argument to option -%c.", option->marker); return 1; } else { ret = convertArg(options, arg, option); if (!ret) { (*pos)++; return 0; } else { return ret; } } } }
static void setOptionsDefaults(Options * options) { Option *option; int i; for (i = 0; i < options->optionNb; i++) { option = &(options->options[i]); if (!(option->alreadyParsed) && (option->defaultv)) { convertArg(options, option->defaultv, option); } } }
/* Gets an input line from the shell and splits it by spaces. * If the first token is an known function and the arguments match, the function is called. * Otherwise, the function prints an "invalid argument" message and returns. */ void parse(char* buff) { int index, cant; char flag = 0; char* args[6]; cant = splitArgs(args, buff); if (cant == -1) { printf(ANSI_COLOR_RED "No injection allowed\n" ANSI_COLOR_RESET); return; } // The first input is not a parameter cant -= 1; for (index = 0; !flag && index < COM_SIZE ; index++) { if (!strcmp(args[0], commands[index].name)) { flag = 1; if (cant != commands[index].argsCant) { printf("Se esperaban %i argumentos y se recibieron %i, revisa el comando \n", commands[index].argsCant, cant); } else { convertArg(args, commands[index].args, commands[index].argsCant); switch (commands[index].argsCant) { case 0: commands[index].function(); break; case 1: commands[index].function(args[1]); break; case 2: commands[index].function(args[1], args[2]); break; case 3: commands[index].function(args[1], args[2], args[3]); break; case 5: commands[index].function(args[1], args[2], args[3], args[4], args[5]); break; } } } } if (!flag) { printf(ANSI_COLOR_MAGENTA "Comando invalido: %s. Intente el comando 'help'\n", args[0]); } }