/**************************************************************************** * Function : GetOptList * Description: This function is similar to the POSIX function getopt. All * options and their corresponding arguments are returned in a * linked list. This function should only be called once per * an option list and it does not modify argv or argc. * Parameters : argc - the number of command line arguments (including the * name of the executable) * argv - pointer to the open binary file to write encoded * output * options - getopt style option list. A NULL terminated * string of single character options. Follow an * option with a colon to indicate that it requires * an argument. * Effects : Creates a link list of command line options and their * arguments. * Returned : option_t type value where the option and arguement fields * contain the next option symbol and its argument (if any). * The argument field will be set to NULL if the option is * specified as having no arguments or no arguments are found. * The option field will be set to PO_NO_OPT if no more * options are found. * * NOTE: The caller is responsible for freeing up the option list when it * is no longer needed. ****************************************************************************/ option_t *GetOptList(const int argc, char *const argv[], char *const options) { int nextArg; option_t *head, *tail; int optIndex; /* start with first argument and nothing found */ nextArg = 1; head = NULL; tail = NULL; /* loop through all of the command line arguments */ while (nextArg < argc) { if ((strlen(argv[nextArg]) > 1) && ('-' == argv[nextArg][0])) { /* possible option */ optIndex = 0; /* attempt to find a matching option */ while ((options[optIndex] != '\0') && (options[optIndex] != argv[nextArg][1])) { do { optIndex++; } while ((options[optIndex] != '\0') && (':' == options[optIndex])); } if (options[optIndex] == argv[nextArg][1]) { /* we found the matching option */ if (NULL == head) { head = MakeOpt(options[optIndex], NULL, OL_NOINDEX); tail = head; } else { tail->next = MakeOpt(options[optIndex], NULL, OL_NOINDEX); tail = tail->next; } if (':' == options[optIndex + 1]) { /* the option found should have a text arguement */ if (strlen(argv[nextArg]) > 2) { /* no space between argument and option */ tail->argument = &(argv[nextArg][2]); tail->argIndex = nextArg; } else if (nextArg < argc) { /* there must be space between the argument option */ nextArg++; tail->argument = argv[nextArg]; tail->argIndex = nextArg; } } } } nextArg++; } return head; }
/**************************************************************************** * Function : GetOptList * Description: This function is similar to the POSIX function getopt. All * options and their corresponding arguments are returned in a * linked list. This function should only be called once per * an option list and it does not modify argv or argc. * Parameters : argc - the number of command line arguments (including the * name of the executable) * argv - pointer to the open binary file to write encoded * output * options - getopt style option list. A NULL terminated * string of single character options. Follow an * option with a colon to indicate that it requires * an argument. * Effects : Creates a link list of command line options and their * arguments. * Returned : option_t type value where the option and arguement fields * contain the next option symbol and its argument (if any). * The argument field will be set to NULL if the option is * specified as having no arguments or no arguments are found. * The option field will be set to PO_NO_OPT if no more * options are found. * * NOTE: The caller is responsible for freeing up the option list when it * is no longer needed. ****************************************************************************/ option_t *GetOptList(const int argc, char *const argv[], char *const options) { int nextArg; option_t *head, *tail; size_t optIndex; size_t argIndex; /* start with first argument and nothing found */ nextArg = 1; head = NULL; tail = NULL; /* loop through all of the command line arguments */ while (nextArg < argc) { argIndex = 1; while ((strlen(argv[nextArg]) > argIndex) && ('-' == argv[nextArg][0])) { /* attempt to find a matching option */ optIndex = MatchOpt(argv[nextArg][argIndex], options); if (options[optIndex] == argv[nextArg][argIndex]) { /* we found the matching option */ if (NULL == head) { head = MakeOpt(options[optIndex], NULL, OL_NOINDEX); tail = head; } else { tail->next = MakeOpt(options[optIndex], NULL, OL_NOINDEX); tail = tail->next; } if (':' == options[optIndex + 1]) { /* the option found should have a text arguement */ argIndex++; if (strlen(argv[nextArg]) > argIndex) { /* no space between argument and option */ tail->argument = &(argv[nextArg][argIndex]); tail->argIndex = nextArg; } else if (nextArg < argc) { /* there must be space between the argument option */ nextArg++; tail->argument = argv[nextArg]; tail->argIndex = nextArg; } break; /* done with argv[nextArg] */ } } argIndex++; } nextArg++; } return head; }