patacongo commented on a change in pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#discussion_r527861295



##########
File path: libs/libc/unistd/lib_getopt.c
##########
@@ -39,257 +39,565 @@
 
 #include <nuttx/config.h>
 
-#include <stdbool.h>
-#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
+#include <getopt.h>
 
 /****************************************************************************
  * Pre-processor Definitions
  ****************************************************************************/
 
+#define PERMUTE                 0
+#define RETURN_IN_ORDER         1
+#define REQUIRE_ORDER           2
+
 /****************************************************************************
- * Public Data
+ * Private Type Definitions
  ****************************************************************************/
 
-FAR char *optarg; /* Optional argument following option */
-int optind = 1;   /* Index into argv */
-int optopt = '?'; /* unrecognized option character */
+typedef struct getopt_data_s
+{
+  FAR char *optarg;
+  int optind;
+  int opterr;
+  int optopt;
+  int optwhere;
+  int permute_from;
+  int num_nonopts;
+} getopt_data_t;
 
 /****************************************************************************
  * Private Data
  ****************************************************************************/
 
-static FAR char         *g_optptr       = NULL;
-static FAR char * const *g_argv         = NULL;
-static int               g_argc         = 0;
-static bool              g_binitialized = false;
+static int optwhere = 1;
+static int permute_from = 0;
+static int num_nonopts = 0;
+
+static int prev_argc;
+static FAR char *const *prev_argv;
 
 /****************************************************************************
- * Public Functions
+ * Public Data
  ****************************************************************************/
 
+FAR char *optarg; /* Optional argument following option */
+int opterr = 1;   /* Print error message */
+int optind = 1;   /* Index into argv */
+int optopt = '?'; /* unrecognized option character */
+
 /****************************************************************************
- * Name: getopt
- *
- * Description:
- *   getopt() parses command-line arguments.  Its arguments argc and argv
- *   are the argument count and array as passed to the main() function on
- *   program invocation.  An element of argv that starts with '-' is an
- *   option element. The characters of this element (aside from the initial
- *   '-') are option characters. If getopt() is called repeatedly, it
- *   returns successively each of the option characters from each of the
- *   option elements.
- *
- *   If getopt() finds another option character, it returns that character,
- *   updating the external variable optind and a static variable nextchar so
- *   that the next call to getopt() can resume the scan with the following
- *   option character or argv-element.
- *
- *   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
- *   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
- *   argv-element, or the text of the following argv-element, in optarg.
- *
- *   NOTES:
- *   1. opterr is not supported and this implementation of getopt() never
- *      printfs error messages.
- *   2. getopt is NOT threadsafe!
- *   3. This version of getopt() does not reset global variables until
- *      -1 is returned.  As a result, your command line parsing loops
- *      must call getopt() repeatedly and continue to parse if other
- *      errors are returned ('?' or ':') until getopt() finally returns -1.
- *     (You can also set optind to -1 to force a reset).
- *
- * Returned Value:
- *   If an option was successfully found, then getopt() returns the option
- *   character. If all command-line options have been parsed, then getopt()
- *   returns -1.  If getopt() encounters an option character that was not
- *   in optstring, then '?' is returned. If getopt() encounters an option
- *   with a missing argument, then the return value depends on the first
- *   character in optstring: if it is ':', then ':' is returned; otherwise
- *   '?' is returned.
- *
+ * Private Functions
  ****************************************************************************/
 
-int getopt(int argc, FAR char * const argv[], FAR const char *optstring)
+/* reverse_argv_elements:  reverses num elements starting at argv */
+
+static void reverse_argv_elements(FAR char **argv, int num)
 {
-  /* Were new argc or argv passed in?  This detects misuse of getopt() by
-   * applications that break out of the getopt() loop before getop() returns
-   * -1.
-   */
+  int i;
+  FAR char *tmp;
 
-  if (argc != g_argc || argv != g_argv)
+  for (i = 0; i < (num >> 1); i++)
     {
-      /* Yes, clear the internal state */
+      tmp = argv[i];
+      argv[i] = argv[num - i - 1];
+      argv[num - i - 1] = tmp;
+    }
+}
+
+/* permute: swap two blocks of argv-elements given their lengths */
+
+static void permute(FAR char *const argv[], int len1, int len2)
+{
+  reverse_argv_elements((FAR char **)argv, len1);
+  reverse_argv_elements((FAR char **)argv, len1 + len2);
+  reverse_argv_elements((FAR char **)argv, len2);
+}
 
-      g_binitialized = false;
-      g_argc         = argc;
-      g_argv         = argv;
+/* is_option: is this argv-element an option or the end of the option list? */
+
+static int is_option(FAR char *argv_element, int only)
+{
+  return ((argv_element == 0) ||
+         (argv_element[0] == '-') || (only && argv_element[0] == '+'));
+}
+
+/* read_globals: read the values from the globals into a getopt_data_s
+ * structure
+ */
+
+static void read_globals(int argc, FAR char *const argv[],
+                         FAR struct getopt_data_s *data)
+{
+  bool reinit = false;
+
+  if (prev_argc != argc || prev_argv != argv)
+    {
+      prev_argc = argc;
+      prev_argv = argv;
+      reinit = true;
     }
 
-  /* Verify input parameters. */
+  data->optarg = optarg;
+  data->optind = reinit ? 0 : optind;
+  data->opterr = opterr;
+  data->optopt = optopt;
+  data->optwhere = optwhere;
+  data->permute_from = permute_from;
+  data->num_nonopts = num_nonopts;
+}
+
+/* write_globals: write the values into the globals from a getopt_data_s
+ * structure
+ */
 
-  if (argv != NULL && optstring != NULL && argc > 1)
+static void write_globals(int r, FAR struct getopt_data_s *data)
+{
+  if (r == EOF)
     {
-      FAR char *optchar;
-      int noarg_ret = '?';
+      prev_argc = 0;
+      prev_argv = NULL;
+    }
 
-      /* The initial value of optind is 1.  If getopt() is called again in
-       * the program, optind must be reset to some value <= 1.
-       */
+  optarg = data->optarg;
+  optind = data->optind;
+  opterr = data->opterr;
+  optopt = data->optopt;
+  optwhere = data->optwhere;
+  permute_from = data->permute_from;
+  num_nonopts = data->num_nonopts;
+}
 
-      if (optind < 1 || !g_binitialized)
-        {
-          optarg         = NULL;
-          optind         = 1;     /* Skip over the program name */
-          optopt         = '?';
-          g_optptr       = NULL;  /* Start at the beginning of the first 
argument */
-          g_binitialized = true;  /* Now we are initialized */
-        }
+/* getopt_internal:  the function that does all the dirty work
+ * NOTE: to reduce the code and RAM footprint this function uses
+ * fputs()/fputc() to do output to stderr instead of fprintf().
+ */

Review comment:
       Same comment




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to