diff options
author | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2011-02-23 03:17:29 +0000 |
---|---|---|
committer | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2011-02-23 03:17:29 +0000 |
commit | 940ad0838da7d5bc2622606a0b42225683494f11 (patch) | |
tree | c7046e60163ac337f5c146a125130985e08857f0 /nuttx/lib/lib_getopt.c | |
parent | 086efc834d511d771a996f895c73d35ca020b1d6 (diff) | |
download | px4-nuttx-940ad0838da7d5bc2622606a0b42225683494f11.tar.gz px4-nuttx-940ad0838da7d5bc2622606a0b42225683494f11.tar.bz2 px4-nuttx-940ad0838da7d5bc2622606a0b42225683494f11.zip |
Back out last change; correct fix is in NSH
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@3311 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'nuttx/lib/lib_getopt.c')
-rw-r--r-- | nuttx/lib/lib_getopt.c | 65 |
1 files changed, 27 insertions, 38 deletions
diff --git a/nuttx/lib/lib_getopt.c b/nuttx/lib/lib_getopt.c index c75d1d7f5..31ba31f66 100644 --- a/nuttx/lib/lib_getopt.c +++ b/nuttx/lib/lib_getopt.c @@ -1,7 +1,7 @@ /**************************************************************************** * lib/lib_getopt.c * - * Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved. + * Copyright (C) 2007-2009 Gregory Nutt. All rights reserved. * Author: Gregory Nutt <spudmonkey@racsa.co.cr> * * Redistribution and use in source and binary forms, with or without @@ -85,7 +85,7 @@ static bool g_binitialized = false; * If there are no more option characters, getopt() returns -1. Then optind * is the index in argv of the first argv-element that is not an option. * - * The 'optstring' argument is a string containing the legitimate option + * The 'optstring argument is a string containing the legitimate option * characters. If such a character is followed by a colon, this indicates * that the option requires an argument. If an argument is required for an * option so getopt() places a pointer to the following text in the same @@ -104,19 +104,6 @@ static bool g_binitialized = false; * first character in optstring: if it is ':', then ':' is returned; * otherwise '?' is returned. * - * Assumptions: - * - getopt() uses global varialbles and, hence, can only be used on a single - * thread. - * - This version of getopt() implements a state machine with certain, strict - * rules. If these rules are not obeyed, the state machine will get out of - * sync and incorrect results will occur. The rules: - * 1. getopt() must be called repeatedly until a terminating value is - * returned. Terminating values include: {-1, ?, : }. - * 2. After a terminating value is returned, getopt() resets the global - * data and is ready for the next command line. - * 3. If the program chooses to violate rule #1, it may do so if it sets - * optind == -1. - * ****************************************************************************/ int getopt(int argc, FAR char *const argv[], FAR const char *optstring) @@ -175,6 +162,9 @@ int getopt(int argc, FAR char *const argv[], FAR const char *optstring) /* There are no more arguments, we are finished */ g_binitialized = false; + + /* Return -1 with optind == all of the arguments */ + return ERROR; } @@ -186,38 +176,38 @@ int getopt(int argc, FAR char *const argv[], FAR const char *optstring) { /* The argument does not start with '-', we are finished */ + g_optptr = NULL; g_binitialized = false; + + /* Return the -1 with optind set to the non-option argument */ + return ERROR; } /* Skip over the '-' */ g_optptr++; + } - /* Special case handling of "-" */ + /* Special case handling of "-" and "-:" */ - if (!*g_optptr) - { - optopt = '\0'; - g_binitialized = false; - return '?'; - } - } + if (!*g_optptr) + { + optopt = '\0'; /* We'll fix up g_optptr the next time we are called */ + return '?'; + } - /* Handle the case of ":" or '?' in the option list. We need to pick - * these off so that the return values cannot be confused with errors. - */ + /* Handle the case of "-:" */ - if (*g_optptr == ':' || *g_optptr == '?') + if (*g_optptr == ':') { - optopt = *g_optptr; - g_binitialized = false; + optopt = ':'; + g_optptr++; return '?'; } - /* g_optptr now points at the next option and it is not something crazy - * (like NULL or ? or :). Check if the option is in the list of valid - * options. + /* g_optptr now points at the next option and it is not something crazy. + * check if the option is in the list of valid options. */ optchar = strchr(optstring, *g_optptr); @@ -226,7 +216,7 @@ int getopt(int argc, FAR char *const argv[], FAR const char *optstring) /* No this character is not in the list of valid options */ optopt = *g_optptr; - g_binitialized = false; + g_optptr++; return '?'; } @@ -242,9 +232,7 @@ int getopt(int argc, FAR char *const argv[], FAR const char *optstring) return *optchar; } - /* Yes... It has a required argument. Is the required argument after - * the command in this same argument? - */ + /* Yes. Is the required argument after the command in this same argument? */ if (g_optptr[1] != '\0') { @@ -270,11 +258,12 @@ int getopt(int argc, FAR char *const argv[], FAR const char *optstring) /* No argument was supplied */ + optarg = NULL; optopt = *optchar; - g_binitialized = false; + optind++; return noarg_ret; } - g_binitialized = false; + optind = 1; return ERROR; } |