You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by GitBox <gi...@apache.org> on 2020/11/17 18:07:28 UTC

[GitHub] [incubator-nuttx] xiaoxiang781216 opened a new pull request #2326: libc: Enhance getopt function

xiaoxiang781216 opened a new pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326


   ## Summary
   1.Support arguments before options(e.g. ls /dev -l)
   2.Support the long options by getopt_long
   3.Support the reentrant version(e.g. getopt_r)
   4.Move the related declaration to getopt.h
   
   ## Impact
   the getopt user get more functionality automatically
   
   ## Testing
   
   


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a change in pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on a change in pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#discussion_r526103688



##########
File path: include/getopt.h
##########
@@ -0,0 +1,154 @@
+/****************************************************************************
+ * include/getopt.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_GETOPT_H
+#define __INCLUDE_GETOPT_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/compiler.h>
+
+#include <unistd.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Names for the values of the `has_arg' field of `struct option'. */
+
+#define no_argument             0
+#define required_argument       1
+#define optional_argument       2
+
+#define NO_ARG                  no_argument
+#define REQUIRED_ARG            required_argument
+#define OPTIONAL_ARG            optional_argument
+
+/* The GETOPT_DATA_INITIALIZER macro is used to initialize a statically-
+ * allocated variable of type struct getopt_data.
+ */
+
+#define GETOPT_DATA_INITIALIZER {0, 0, 1, 0, 0, 0, 0}
+
+/****************************************************************************
+ * Public Type Definitions
+ ****************************************************************************/
+
+#undef EXTERN
+#if defined(__cplusplus)
+#define EXTERN extern "C"
+extern "C"
+{
+#else
+#define EXTERN extern
+#endif
+
+/* Describe the long-named options requested by the application.
+ * The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector
+ * of `struct option' terminated by an element containing a name which is
+ * zero.
+ *
+ * The field `has_arg' is:
+ * no_argument       (or 0) if the option does not take an argument,
+ * required_argument (or 1) if the option requires an argument,
+ * optional_argument (or 2) if the option takes an optional argument.
+ *
+ * If the field `flag' is not NULL, it points to a variable that is set
+ * to the value given in the field `val' when the option is found, but
+ * left unchanged if the option is not found.
+ *
+ * To have a long-named option do something other than set an `int' to
+ * a compiled-in constant, such as set a value from `optarg', set the
+ * option's `flag' field to zero and its `val' field to a nonzero
+ * value (the equivalent single-letter option character, if there is
+ * one).  For long options that have a zero `flag' field, `getopt'
+ * returns the contents of the `val' field.
+ */
+
+struct option
+{
+  FAR const char *name; /* The name of the long option */
+  int has_arg;          /* One of the above macros */
+  FAR int *flag;        /* Determines if getopt_long() returns a
+                         * value for a long option; if it is
+                         * non-NULL, 0 is returned as a function
+                         * value and the value of val is stored in
+                         * the area pointed to by flag.  Otherwise,
+                         * val is returned.
+                         */
+  int val;              /* Determines the value to return if flag is
+                         * NULL.
+                         */
+};
+
+/* The getopt_data structure is for reentrancy. Its members are similar to
+ * the externally-defined variables.
+ */
+
+typedef struct getopt_data
+{

Review comment:
       Ok, getopt_data doesn't specify in any document. I will change it.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo commented on a change in pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
patacongo commented on a change in pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#discussion_r527857099



##########
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;

Review comment:
       If this gets moved to TLS, I think that the int values should be changed to int16_t.  C requires that :  16 <= width_of(int) <= width_of(long).  So any use of type int must work if width_of(int) == 16.
   
   And this will save a lot of space in TLS.  Above, sizeof(struct getopt_data_s is 48 butes on ARM.  If the the int types were replaced with int16_t, the size would be 16 bytes with no loss of functionality or portability (in fact, the behavior is more portable).  A significant savings if this is reserved on every stack.
   
   It would probably also require some range testing of at least optind, however.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo commented on a change in pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
patacongo commented on a change in pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#discussion_r527866501



##########
File path: include/getopt.h
##########
@@ -0,0 +1,117 @@
+/****************************************************************************
+ * include/getopt.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_GETOPT_H
+#define __INCLUDE_GETOPT_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/compiler.h>
+
+#include <unistd.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Names for the values of the `has_arg' field of `struct option'. */
+
+#define no_argument             0
+#define required_argument       1
+#define optional_argument       2
+

Review comment:
       The coding standard requires that pre-processor varialbe names must be all uppercase.  This must be fixed.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 edited a comment on pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 edited a comment on pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#issuecomment-731379509


   Let me repeat again: the code is written by myself now, no code come from Gregory Pietsch:
   https://users.cs.cf.ac.uk/KurtevaA/MSCProject/code/speak.js-master/src/getopt.h
   If you think there are any similarity, please point out them.


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo commented on a change in pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
patacongo commented on a change in pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#discussion_r527861009



##########
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 */
+

Review comment:
       None of these functions follow the coding conventions at all.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 edited a comment on pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 edited a comment on pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#issuecomment-731379509


   Let me repeat again: the code is written by myself now, no code copy or modify from Gregory Pietsch:
   https://users.cs.cf.ac.uk/KurtevaA/MSCProject/code/speak.js-master/src/getopt.h
   If you think there are any similarity, please point out them.


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo commented on pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
patacongo commented on pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#issuecomment-731201603


   My recommendation is that you do not go in this direction.  It is not standard and totally inappropriate for used in a standards-based OS.
   
   My recommendation is that you consider using TLS to hold getopt() variables.  That is transparent to the user, retains that standard getopt() function, and does not not introduce new, non-portable OS interfaces.  That is against the spirit of Nutt.


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a change in pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on a change in pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#discussion_r526128114



##########
File path: include/getopt.h
##########
@@ -0,0 +1,154 @@
+/****************************************************************************
+ * include/getopt.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_GETOPT_H
+#define __INCLUDE_GETOPT_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/compiler.h>
+
+#include <unistd.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Names for the values of the `has_arg' field of `struct option'. */
+
+#define no_argument             0
+#define required_argument       1
+#define optional_argument       2
+
+#define NO_ARG                  no_argument
+#define REQUIRED_ARG            required_argument
+#define OPTIONAL_ARG            optional_argument
+
+/* The GETOPT_DATA_INITIALIZER macro is used to initialize a statically-
+ * allocated variable of type struct getopt_data.
+ */
+
+#define GETOPT_DATA_INITIALIZER {0, 0, 1, 0, 0, 0, 0}
+
+/****************************************************************************
+ * Public Type Definitions
+ ****************************************************************************/
+
+#undef EXTERN
+#if defined(__cplusplus)
+#define EXTERN extern "C"
+extern "C"
+{
+#else
+#define EXTERN extern
+#endif
+
+/* Describe the long-named options requested by the application.
+ * The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector
+ * of `struct option' terminated by an element containing a name which is
+ * zero.
+ *
+ * The field `has_arg' is:
+ * no_argument       (or 0) if the option does not take an argument,
+ * required_argument (or 1) if the option requires an argument,
+ * optional_argument (or 2) if the option takes an optional argument.
+ *
+ * If the field `flag' is not NULL, it points to a variable that is set
+ * to the value given in the field `val' when the option is found, but
+ * left unchanged if the option is not found.
+ *
+ * To have a long-named option do something other than set an `int' to
+ * a compiled-in constant, such as set a value from `optarg', set the
+ * option's `flag' field to zero and its `val' field to a nonzero
+ * value (the equivalent single-letter option character, if there is
+ * one).  For long options that have a zero `flag' field, `getopt'
+ * returns the contents of the `val' field.
+ */
+
+struct option
+{

Review comment:
       I don't know, but if you grep the code base, many place follow the strategy I use here. For example, all netlink public interface doesn't add _s suffix. so from the consistence and portability perspective, the compromise make sense.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo commented on a change in pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
patacongo commented on a change in pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#discussion_r527858513



##########
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;

Review comment:
       I suppose that these could be similarly int16_t.
   
   Also, there is a violation in the naming convertion:  The coding standard requires that all global varialbes (i.e., variables with scope beyond a function) must begin with the prefix g_  This is important.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 edited a comment on pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 edited a comment on pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#issuecomment-731368344


   @patacongo the code is rewritten, it shouldn't have the copyright issue now.


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a change in pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on a change in pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#discussion_r527347933



##########
File path: include/getopt.h
##########
@@ -0,0 +1,154 @@
+/****************************************************************************
+ * include/getopt.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_GETOPT_H
+#define __INCLUDE_GETOPT_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/compiler.h>
+
+#include <unistd.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Names for the values of the `has_arg' field of `struct option'. */
+
+#define no_argument             0
+#define required_argument       1
+#define optional_argument       2
+
+#define NO_ARG                  no_argument
+#define REQUIRED_ARG            required_argument
+#define OPTIONAL_ARG            optional_argument
+
+/* The GETOPT_DATA_INITIALIZER macro is used to initialize a statically-
+ * allocated variable of type struct getopt_data.
+ */
+
+#define GETOPT_DATA_INITIALIZER {0, 0, 1, 0, 0, 0, 0}
+
+/****************************************************************************
+ * Public Type Definitions
+ ****************************************************************************/
+
+#undef EXTERN
+#if defined(__cplusplus)
+#define EXTERN extern "C"
+extern "C"
+{
+#else
+#define EXTERN extern
+#endif
+
+/* Describe the long-named options requested by the application.
+ * The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector
+ * of `struct option' terminated by an element containing a name which is
+ * zero.
+ *
+ * The field `has_arg' is:
+ * no_argument       (or 0) if the option does not take an argument,
+ * required_argument (or 1) if the option requires an argument,
+ * optional_argument (or 2) if the option takes an optional argument.
+ *
+ * If the field `flag' is not NULL, it points to a variable that is set
+ * to the value given in the field `val' when the option is found, but
+ * left unchanged if the option is not found.
+ *
+ * To have a long-named option do something other than set an `int' to
+ * a compiled-in constant, such as set a value from `optarg', set the
+ * option's `flag' field to zero and its `val' field to a nonzero
+ * value (the equivalent single-letter option character, if there is
+ * one).  For long options that have a zero `flag' field, `getopt'
+ * returns the contents of the `val' field.
+ */
+
+struct option
+{

Review comment:
       @patacongo could you merge this PR if no other coding/naming issue?




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo edited a comment on pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
patacongo edited a comment on pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#issuecomment-731402283


   @xiaoxiang781216 fails:
   
       Error: unistd/lib_getopt.c:300:34: error: 'struct option' declared inside parameter list will not be visible outside of this definition or declaration [-Werror]
         300 |                 FAR const struct option *longopts,
             |                                  ^~~~~~
   
   Maybe libs/libc/unistd/lib_getopt.c  needs to include getopt.h


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo edited a comment on pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
patacongo edited a comment on pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#issuecomment-731393867


   It looks to me that the stripped down version in the current PR does contain only original code.  I think this can be merged as it is.
   
   Please be careful with respecting authorship and licensing.  It is important.  NuttX is an original OS.  It was not built by stealing code from other people.  Third party code is properly marked with the correct author, copyright, and license.  Anything else is not acceptable behavior.
   


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo commented on a change in pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
patacongo commented on a change in pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#discussion_r526064739



##########
File path: include/getopt.h
##########
@@ -0,0 +1,154 @@
+/****************************************************************************
+ * include/getopt.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_GETOPT_H
+#define __INCLUDE_GETOPT_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/compiler.h>
+
+#include <unistd.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Names for the values of the `has_arg' field of `struct option'. */
+
+#define no_argument             0
+#define required_argument       1
+#define optional_argument       2
+
+#define NO_ARG                  no_argument
+#define REQUIRED_ARG            required_argument
+#define OPTIONAL_ARG            optional_argument
+
+/* The GETOPT_DATA_INITIALIZER macro is used to initialize a statically-
+ * allocated variable of type struct getopt_data.
+ */
+
+#define GETOPT_DATA_INITIALIZER {0, 0, 1, 0, 0, 0, 0}
+
+/****************************************************************************
+ * Public Type Definitions
+ ****************************************************************************/
+
+#undef EXTERN
+#if defined(__cplusplus)
+#define EXTERN extern "C"
+extern "C"
+{
+#else
+#define EXTERN extern
+#endif
+
+/* Describe the long-named options requested by the application.
+ * The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector
+ * of `struct option' terminated by an element containing a name which is
+ * zero.
+ *
+ * The field `has_arg' is:
+ * no_argument       (or 0) if the option does not take an argument,
+ * required_argument (or 1) if the option requires an argument,
+ * optional_argument (or 2) if the option takes an optional argument.
+ *
+ * If the field `flag' is not NULL, it points to a variable that is set
+ * to the value given in the field `val' when the option is found, but
+ * left unchanged if the option is not found.
+ *
+ * To have a long-named option do something other than set an `int' to
+ * a compiled-in constant, such as set a value from `optarg', set the
+ * option's `flag' field to zero and its `val' field to a nonzero
+ * value (the equivalent single-letter option character, if there is
+ * one).  For long options that have a zero `flag' field, `getopt'
+ * returns the contents of the `val' field.
+ */
+
+struct option
+{
+  FAR const char *name; /* The name of the long option */
+  int has_arg;          /* One of the above macros */
+  FAR int *flag;        /* Determines if getopt_long() returns a
+                         * value for a long option; if it is
+                         * non-NULL, 0 is returned as a function
+                         * value and the value of val is stored in
+                         * the area pointed to by flag.  Otherwise,
+                         * val is returned.
+                         */
+  int val;              /* Determines the value to return if flag is
+                         * NULL.
+                         */
+};
+
+/* The getopt_data structure is for reentrancy. Its members are similar to
+ * the externally-defined variables.
+ */
+
+typedef struct getopt_data
+{
+  FAR char *optarg;
+  int optind;
+  int opterr;
+  int optopt;
+  int optwhere;
+  int permute_from;
+  int num_nonopts;
+} getopt_data;

Review comment:
       Coding standard requires that type definition names end with _t




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo removed a comment on pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
patacongo removed a comment on pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#issuecomment-731387451


   I have zero confidence in the code and do not believe anything you are saying.  I am going to back out of the picture now.  I will not be merging this PR.
   
   If someone else wants to step in an be responsible for the provenance of this code, then I will not object.  But I will not be involved in this kind of code theft.
   


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a change in pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on a change in pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#discussion_r526103172



##########
File path: include/getopt.h
##########
@@ -0,0 +1,154 @@
+/****************************************************************************
+ * include/getopt.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_GETOPT_H
+#define __INCLUDE_GETOPT_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/compiler.h>
+
+#include <unistd.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Names for the values of the `has_arg' field of `struct option'. */
+
+#define no_argument             0
+#define required_argument       1
+#define optional_argument       2
+
+#define NO_ARG                  no_argument
+#define REQUIRED_ARG            required_argument
+#define OPTIONAL_ARG            optional_argument
+
+/* The GETOPT_DATA_INITIALIZER macro is used to initialize a statically-
+ * allocated variable of type struct getopt_data.
+ */
+
+#define GETOPT_DATA_INITIALIZER {0, 0, 1, 0, 0, 0, 0}
+
+/****************************************************************************
+ * Public Type Definitions
+ ****************************************************************************/
+
+#undef EXTERN
+#if defined(__cplusplus)
+#define EXTERN extern "C"
+extern "C"
+{
+#else
+#define EXTERN extern
+#endif
+
+/* Describe the long-named options requested by the application.
+ * The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector
+ * of `struct option' terminated by an element containing a name which is
+ * zero.
+ *
+ * The field `has_arg' is:
+ * no_argument       (or 0) if the option does not take an argument,
+ * required_argument (or 1) if the option requires an argument,
+ * optional_argument (or 2) if the option takes an optional argument.
+ *
+ * If the field `flag' is not NULL, it points to a variable that is set
+ * to the value given in the field `val' when the option is found, but
+ * left unchanged if the option is not found.
+ *
+ * To have a long-named option do something other than set an `int' to
+ * a compiled-in constant, such as set a value from `optarg', set the
+ * option's `flag' field to zero and its `val' field to a nonzero
+ * value (the equivalent single-letter option character, if there is
+ * one).  For long options that have a zero `flag' field, `getopt'
+ * returns the contents of the `val' field.
+ */
+
+struct option
+{

Review comment:
       But option struct is a defacto standard:
   https://www.freebsd.org/cgi/man.cgi?getopt_long(3)
   https://www.gnu.org/software/libc/manual/html_node/Getopt-Long-Options.html
   renaming it lose the compatablity, so `struct option` should treat like `struct itimerspec`.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo edited a comment on pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
patacongo edited a comment on pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#issuecomment-731393867


   It looks to me that the stripped down version in the current PR does contains only original code.  I think this can be merged as it is.  I will do that after a few more checks complete.
   
   Please be careful with respecting authorship and licensing.  It is important.  NuttX is an original OS.  It was not built by stealing code from other people.  Third party code is properly marked with the correct author, copyright, and license.  Anything else is not acceptable behavior.
   


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo edited a comment on pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
patacongo edited a comment on pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#issuecomment-731319923


   This code appears to have been stolen from other open source implementation.  We cannot plaster an Apache 2.0 license on it and say that we own it!
   
   For example, there are weird definitions like:
   
   #define no_argument             0
   #define required_argument       1
   #define optional_argument       2
   
   The above definitions were certainly NOT written for NuttX since they do not follow the coding standard.
   
   And these exact definitions are found in other GPL sources such as https://github.com/espressif/arduino-esp32/blob/master/tools/sdk/include/newlib/getopt.h
   
   /* include files needed by this include file */
   
   #define no_argument		0
   #define required_argument	1
   #define optional_argument	2
   
   I do not believe that this original original code.  I believed it is stolen and is misrepresented by the putting an illegal Apache license on it.  It must not be merged.  I will mark it as a draft.
   
   


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo commented on a change in pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
patacongo commented on a change in pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#discussion_r526139794



##########
File path: include/getopt.h
##########
@@ -0,0 +1,154 @@
+/****************************************************************************
+ * include/getopt.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_GETOPT_H
+#define __INCLUDE_GETOPT_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/compiler.h>
+
+#include <unistd.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Names for the values of the `has_arg' field of `struct option'. */
+
+#define no_argument             0
+#define required_argument       1
+#define optional_argument       2
+
+#define NO_ARG                  no_argument
+#define REQUIRED_ARG            required_argument
+#define OPTIONAL_ARG            optional_argument
+
+/* The GETOPT_DATA_INITIALIZER macro is used to initialize a statically-
+ * allocated variable of type struct getopt_data.
+ */
+
+#define GETOPT_DATA_INITIALIZER {0, 0, 1, 0, 0, 0, 0}
+
+/****************************************************************************
+ * Public Type Definitions
+ ****************************************************************************/
+
+#undef EXTERN
+#if defined(__cplusplus)
+#define EXTERN extern "C"
+extern "C"
+{
+#else
+#define EXTERN extern
+#endif
+
+/* Describe the long-named options requested by the application.
+ * The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector
+ * of `struct option' terminated by an element containing a name which is
+ * zero.
+ *
+ * The field `has_arg' is:
+ * no_argument       (or 0) if the option does not take an argument,
+ * required_argument (or 1) if the option requires an argument,
+ * optional_argument (or 2) if the option takes an optional argument.
+ *
+ * If the field `flag' is not NULL, it points to a variable that is set
+ * to the value given in the field `val' when the option is found, but
+ * left unchanged if the option is not found.
+ *
+ * To have a long-named option do something other than set an `int' to
+ * a compiled-in constant, such as set a value from `optarg', set the
+ * option's `flag' field to zero and its `val' field to a nonzero
+ * value (the equivalent single-letter option character, if there is
+ * one).  For long options that have a zero `flag' field, `getopt'
+ * returns the contents of the `val' field.
+ */
+
+struct option
+{

Review comment:
       Okay.  But we must make sure that we following the coding./naming standards or inconsistencies in the code base will result.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 edited a comment on pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 edited a comment on pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#issuecomment-731379509


   Let me repeat again: the code is written by myself now, no code come from Gregory Pietsch. If you think there are any similarity, please point out them.


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo commented on pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
patacongo commented on pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#issuecomment-731261177


   An issue with using TLS is that the variables would be duplicated for each thread.  That would be required for true thread safety, but in reality only one instance of the variables are needed per task group (just as there are only one instance of the getopt() variables per process under Linux).  That is because getopt() is normally only used from main() using the argc and argv inputs.
   
   An option would be to put the getopt() variables in struct task_tcb_s or, better, struct group_s.


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo commented on a change in pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
patacongo commented on a change in pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#discussion_r526064407



##########
File path: include/getopt.h
##########
@@ -0,0 +1,154 @@
+/****************************************************************************
+ * include/getopt.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_GETOPT_H
+#define __INCLUDE_GETOPT_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/compiler.h>
+
+#include <unistd.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Names for the values of the `has_arg' field of `struct option'. */
+
+#define no_argument             0
+#define required_argument       1
+#define optional_argument       2
+
+#define NO_ARG                  no_argument
+#define REQUIRED_ARG            required_argument
+#define OPTIONAL_ARG            optional_argument
+
+/* The GETOPT_DATA_INITIALIZER macro is used to initialize a statically-
+ * allocated variable of type struct getopt_data.
+ */
+
+#define GETOPT_DATA_INITIALIZER {0, 0, 1, 0, 0, 0, 0}
+
+/****************************************************************************
+ * Public Type Definitions
+ ****************************************************************************/
+
+#undef EXTERN
+#if defined(__cplusplus)
+#define EXTERN extern "C"
+extern "C"
+{
+#else
+#define EXTERN extern
+#endif
+
+/* Describe the long-named options requested by the application.
+ * The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector
+ * of `struct option' terminated by an element containing a name which is
+ * zero.
+ *
+ * The field `has_arg' is:
+ * no_argument       (or 0) if the option does not take an argument,
+ * required_argument (or 1) if the option requires an argument,
+ * optional_argument (or 2) if the option takes an optional argument.
+ *
+ * If the field `flag' is not NULL, it points to a variable that is set
+ * to the value given in the field `val' when the option is found, but
+ * left unchanged if the option is not found.
+ *
+ * To have a long-named option do something other than set an `int' to
+ * a compiled-in constant, such as set a value from `optarg', set the
+ * option's `flag' field to zero and its `val' field to a nonzero
+ * value (the equivalent single-letter option character, if there is
+ * one).  For long options that have a zero `flag' field, `getopt'
+ * returns the contents of the `val' field.
+ */
+
+struct option
+{
+  FAR const char *name; /* The name of the long option */
+  int has_arg;          /* One of the above macros */
+  FAR int *flag;        /* Determines if getopt_long() returns a
+                         * value for a long option; if it is
+                         * non-NULL, 0 is returned as a function
+                         * value and the value of val is stored in
+                         * the area pointed to by flag.  Otherwise,
+                         * val is returned.
+                         */
+  int val;              /* Determines the value to return if flag is
+                         * NULL.
+                         */
+};
+
+/* The getopt_data structure is for reentrancy. Its members are similar to
+ * the externally-defined variables.
+ */
+
+typedef struct getopt_data
+{

Review comment:
       Coding standard requires that type definition names end with _t




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 commented on pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#issuecomment-731385404


   > This code appears to have been stolen from other open source implementation. We cannot plaster an Apache 2.0 license on it and say that we own it!
   > 
   > For example, there are weird definitions like:
   > 
   > #define no_argument 0
   > #define required_argument 1
   > #define optional_argument 2
   > 
   > The above definitions were certainly NOT written for NuttX since they do not follow the coding standard.
   
   The above names come from https://www.freebsd.org/cgi/man.cgi?getopt_long(3):
   ![image](https://user-images.githubusercontent.com/18066964/99845568-d0f63680-2baf-11eb-9cd2-0c9b0da3cbfe.png)
   they are not invented by Gregory Pietsch


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo commented on pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
patacongo commented on pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#issuecomment-731319923


   This code appears to have been stolen from other open source implementation.  We cannot plaster an Apache 2.0 license on it and say that we own it!
   
   For example, there are weird definitions like:
   
   #define no_argument             0
   #define required_argument       1
   #define optional_argument       2
   
   Which are found in other GPL sourses such as https://github.com/espressif/arduino-esp32/blob/master/tools/sdk/include/newlib/getopt.h
   
   /* include files needed by this include file */
   
   #define no_argument		0
   #define required_argument	1
   #define optional_argument	2
   
   I do not believe that this original original code.  I believed it is stolen and is misrepresented by the putting an illegal Apache license on it.  It must not be merged.  I will mark it as a draft.
   
   


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo edited a comment on pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
patacongo edited a comment on pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#issuecomment-731375176


   > 
   > 
   > @patacongo the code is rewritten, it shouldn't have the copyright issue now.
   
   Modifying copyrighted code does not change the copyright or release you from the obligations of the license.  It does not give you the right to claim that you are the author.  It does not give the ASF the right to claim the coyright on the code.
   
   This must not be merged yet (even though is was incorrectly re-opened).  I believe there are license problems and, at a minumum, we need some guidance from @justinmclean
   
   If Justin thinks this practice is okay, then we can merge.


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo edited a comment on pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
patacongo edited a comment on pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#issuecomment-731393867


   It looks to me that the stripped down version in the current PR does contain only original code.  I think this can be merged as it is.  I will do that after a few more checks complete.
   
   Please be careful with respecting authorship and licensing.  It is important.  NuttX is an original OS.  It was not built by stealing code from other people.  Third party code is properly marked with the correct author, copyright, and license.  Anything else is not acceptable behavior.
   


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo edited a comment on pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
patacongo edited a comment on pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#issuecomment-731375176


   > 
   > 
   > @patacongo the code is rewritten, it shouldn't have the copyright issue now.
   
   Modifying copyrighted code does not change the copyright or release you from the obligations of the license.  It does not give you the right to claim that you are the author.  It does not give the ASF the right to claim the coyright on the code.
   
   This must not be merged yet (even though is was incorrectly re-opened).  I believe there are license problems and, at a minumum, we need some guidance from @justinmclean..  I believe that this is still third party code and that we are currently violating the copyright and licensing.
   
   Editing Gregory Pietsch code with never change that.
   
   If Justin thinks this practice is okay, then we can merge.


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo edited a comment on pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
patacongo edited a comment on pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#issuecomment-731375176


   > 
   > 
   > @patacongo the code is rewritten, it shouldn't have the copyright issue now.
   
   Modifying copyrighted code does not change the copyright or release you from the obligations of the license.  It does not give you the right to claim that you are the author.  It does not give the ASF the right to claim the coyright on the code.
   
   This must not be merged yet (even though is was incorrectly re-opened).  I believe there are license problems and, at a minumum, we need some guidance from @justinmclean..  I believe that this is still third party code and that we are currently violating the copyright and licensing.
   
   Editing Gregory Pietsch code with never change that.  That is just an unethical attempt to hide the truth.  If you bury some one else's work with enough edit's that it is harder to trace to the true owner but does not change the facts.  This code will ALWAYS below to Gregory Pietsch no matter what you do to it.
   
   If Justin thinks this practice is okay, then we can merge.


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo commented on a change in pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
patacongo commented on a change in pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#discussion_r526122605



##########
File path: include/getopt.h
##########
@@ -0,0 +1,154 @@
+/****************************************************************************
+ * include/getopt.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_GETOPT_H
+#define __INCLUDE_GETOPT_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/compiler.h>
+
+#include <unistd.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Names for the values of the `has_arg' field of `struct option'. */
+
+#define no_argument             0
+#define required_argument       1
+#define optional_argument       2
+
+#define NO_ARG                  no_argument
+#define REQUIRED_ARG            required_argument
+#define OPTIONAL_ARG            optional_argument
+
+/* The GETOPT_DATA_INITIALIZER macro is used to initialize a statically-
+ * allocated variable of type struct getopt_data.
+ */
+
+#define GETOPT_DATA_INITIALIZER {0, 0, 1, 0, 0, 0, 0}
+
+/****************************************************************************
+ * Public Type Definitions
+ ****************************************************************************/
+
+#undef EXTERN
+#if defined(__cplusplus)
+#define EXTERN extern "C"
+extern "C"
+{
+#else
+#define EXTERN extern
+#endif
+
+/* Describe the long-named options requested by the application.
+ * The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector
+ * of `struct option' terminated by an element containing a name which is
+ * zero.
+ *
+ * The field `has_arg' is:
+ * no_argument       (or 0) if the option does not take an argument,
+ * required_argument (or 1) if the option requires an argument,
+ * optional_argument (or 2) if the option takes an optional argument.
+ *
+ * If the field `flag' is not NULL, it points to a variable that is set
+ * to the value given in the field `val' when the option is found, but
+ * left unchanged if the option is not found.
+ *
+ * To have a long-named option do something other than set an `int' to
+ * a compiled-in constant, such as set a value from `optarg', set the
+ * option's `flag' field to zero and its `val' field to a nonzero
+ * value (the equivalent single-letter option character, if there is
+ * one).  For long options that have a zero `flag' field, `getopt'
+ * returns the contents of the `val' field.
+ */
+
+struct option
+{

Review comment:
       > 
   > 
   > But option struct is a defacto standard:
   > https://www.freebsd.org/cgi/man.cgi?getopt_long(3)
   > https://www.gnu.org/software/libc/manual/html_node/Getopt-Long-Options.html
   > renaming it lose the compatablity, so `struct option` should treat like `struct itimerspec`.
   
   So which has priority?  Conforming to a non-standard interface or conforming to NuttX naming standards?




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo edited a comment on pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
patacongo edited a comment on pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#issuecomment-731375176


   > 
   > 
   > @patacongo the code is rewritten, it shouldn't have the copyright issue now.
   
   Modifying copyrighted code does not change the copyright or release you from the obligations of the license.  It does not give you the right to claim that you are the author.  It does not give the ASF the right to claim the coyright on the code.
   
   This must not be merged yet (even though is was incorrectly re-opened).  I believe there are license problems and, at a minumum, we need some guidance from @justinmclean..  I believe that this is still third party code and that we are currently violating the copyright and licensing.
   
   Editing Gregory Pietsch code with never change that.  That is just an unethical attempt to hide the truth.  If you bury some one else's work with enought edit's that it is harder to trace to the true owner but does not change the facts.
   
   If Justin thinks this practice is okay, then we can merge.


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo commented on pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
patacongo commented on pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#issuecomment-731383446


   Anyone who merges this PR is potentially an accessory to a crime.
   
   My recommendation is that you wait for the advice of @justinmclean  I will abide by that.


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 edited a comment on pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 edited a comment on pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#issuecomment-731368344


   @patacongo the code is rewritten, it is no copyright issue now.


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo commented on a change in pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
patacongo commented on a change in pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#discussion_r527859737



##########
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)
 {

Review comment:
       This function requires a proper function header per the coding standard.  The function header is often omitted for small simple functions but really is needed for larger, more complex functions.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 commented on pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#issuecomment-731368344


   @patacongo the code is written, it is no copyright issue now.


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo edited a comment on pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
patacongo edited a comment on pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#issuecomment-731261177


   An issue with using TLS is that the variables would be duplicated for each thread.  That would be required for true thread safety, but in reality only one instance of the variables are needed per task group (just as there are only one instance of the getopt() variables per process under Linux).  That is because getopt() is normally only used from main() using the argc and argv inputs.
   
   An option would be to put the getopt() variables in struct task_tcb_s or, better, struct group_s.  But that would require a special system call which would be pretty ugly.


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo commented on pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
patacongo commented on pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#issuecomment-731393867


   It looks to me that the stripped down version in the current PR does contain only original code.  I think this can be merged as it is.
   
   Please be careful with respecting authorship and licensing.  It is important.  NuttX is an orginal OS.  It was not built by stealing code from other people.  That is not an acceptable behavior.
   


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 commented on pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#issuecomment-731287234


   Yes, TLS or task_group_s is better than getopt_r. So I drop the xxx_r from this patch. @patacongo please review whether the rest change is good. We can address the reentrant issue in another patch.


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 commented on pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#issuecomment-731379509


   Let me repeat again: the code is written by myself now. There is no any code come from Gregory Pietsch.


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] acassis commented on pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
acassis commented on pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#issuecomment-731327038


   The file seams to be in public domain, but the author asked to include his name, so @xiaoxiang781216 I think it should include "Copyright (C) 1997 Gregory Pietsch" if this is the case here. As Justin commented if the code come from someone else, we need to include it in the header with Apache license


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo merged pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
patacongo merged pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326


   


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo edited a comment on pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
patacongo edited a comment on pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#issuecomment-731329650


   This code seems to derive from the implementation by Gregory Pietsch.  That is public domain and not GPL:  https://users.cs.cf.ac.uk/KurtevaA/MSCProject/code/speak.js-master/src/getopt.h
   
   COPYRIGHT NOTICE AND DISCLAIMER:
   Copyright (C) 1997 Gregory Pietsch
   This file and the accompanying getopt.c implementation file are hereby 
   placed in the public domain without restrictions.  **_Just give the author 
   credit, don't claim you wrote it or prevent anyone else from using it._**
   Gregory Pietsch's current e-mail address:
   gpietsch@comcast.net
   
   I personally do not know if we can take copyrighted, public doman and claim that the ASF has the copyright.  I think that is a very bad practice.   At the very least is in unethical to claim to wrote and the you own code the you derive from another person's effort
   
   @justinmclean How should we handle this?


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] juniskane commented on a change in pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
juniskane commented on a change in pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#discussion_r525998185



##########
File path: include/unistd.h
##########
@@ -44,6 +44,8 @@
 #include <sys/types.h>
 #include <nuttx/compiler.h>
 
+#include <getopt.h>

Review comment:
       Should not include this here. Non-standard macros and identifiers in getopt.h can break conforming POSIX programs, for example valid declaration:  `bool no_argument = true;`




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 commented on pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#issuecomment-731252590


   > My recommendation is that you do not go in this direction. It is not standard and totally inappropriate for used in a standards-based OS.
   > 
   > My recommendation is that you consider using TLS to hold getopt() variables. That is transparent to the user, retains that standard getopt() function, and does not not introduce new, non-portable OS interfaces. That is against the spirit of Nutt.
   
   Ok, I remove getopt_r from patch, since it's a seldom used feature, the compatiblity isn't a big problem without it. But, many application use getopt_long and TLS can't help here.
   


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo commented on pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
patacongo commented on pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#issuecomment-731329650


   This code seems to derive from the implementation by Gregory Pietsch.  That is public domain and not GPL:  https://users.cs.cf.ac.uk/KurtevaA/MSCProject/code/speak.js-master/src/getopt.h
   
   COPYRIGHT NOTICE AND DISCLAIMER:
   Copyright (C) 1997 Gregory Pietsch
   This file and the accompanying getopt.c implementation file are hereby 
   placed in the public domain without restrictions.  Just give the author 
   credit, don't claim you wrote it or prevent anyone else from using it.
   Gregory Pietsch's current e-mail address:
   gpietsch@comcast.net
   
   I personally do not know if we can take copyrighted, public doman and claim that the ASF has the copyright.  I think that is a very bad practice.   At the very least is in unethical to claim to wrote and the you own code the you derive from another person's effort
   
   @justinmclean How should we handle this?


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a change in pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on a change in pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#discussion_r527931070



##########
File path: include/getopt.h
##########
@@ -0,0 +1,117 @@
+/****************************************************************************
+ * include/getopt.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_GETOPT_H
+#define __INCLUDE_GETOPT_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/compiler.h>
+
+#include <unistd.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Names for the values of the `has_arg' field of `struct option'. */
+
+#define no_argument             0
+#define required_argument       1
+#define optional_argument       2
+

Review comment:
       all of them is part of API definition, we have to keep as lowercase:
   https://www.freebsd.org/cgi/man.cgi?getopt_long(3)
   https://linux.die.net/man/3/getopt_long
   




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a change in pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on a change in pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#discussion_r526016958



##########
File path: include/unistd.h
##########
@@ -44,6 +44,8 @@
 #include <sys/types.h>
 #include <nuttx/compiler.h>
 
+#include <getopt.h>

Review comment:
       Done.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo commented on a change in pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
patacongo commented on a change in pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#discussion_r526064104



##########
File path: include/getopt.h
##########
@@ -0,0 +1,154 @@
+/****************************************************************************
+ * include/getopt.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_GETOPT_H
+#define __INCLUDE_GETOPT_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/compiler.h>
+
+#include <unistd.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Names for the values of the `has_arg' field of `struct option'. */
+
+#define no_argument             0
+#define required_argument       1
+#define optional_argument       2
+
+#define NO_ARG                  no_argument
+#define REQUIRED_ARG            required_argument
+#define OPTIONAL_ARG            optional_argument
+
+/* The GETOPT_DATA_INITIALIZER macro is used to initialize a statically-
+ * allocated variable of type struct getopt_data.
+ */
+
+#define GETOPT_DATA_INITIALIZER {0, 0, 1, 0, 0, 0, 0}
+
+/****************************************************************************
+ * Public Type Definitions
+ ****************************************************************************/
+
+#undef EXTERN
+#if defined(__cplusplus)
+#define EXTERN extern "C"
+extern "C"
+{
+#else
+#define EXTERN extern
+#endif
+
+/* Describe the long-named options requested by the application.
+ * The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector
+ * of `struct option' terminated by an element containing a name which is
+ * zero.
+ *
+ * The field `has_arg' is:
+ * no_argument       (or 0) if the option does not take an argument,
+ * required_argument (or 1) if the option requires an argument,
+ * optional_argument (or 2) if the option takes an optional argument.
+ *
+ * If the field `flag' is not NULL, it points to a variable that is set
+ * to the value given in the field `val' when the option is found, but
+ * left unchanged if the option is not found.
+ *
+ * To have a long-named option do something other than set an `int' to
+ * a compiled-in constant, such as set a value from `optarg', set the
+ * option's `flag' field to zero and its `val' field to a nonzero
+ * value (the equivalent single-letter option character, if there is
+ * one).  For long options that have a zero `flag' field, `getopt'
+ * returns the contents of the `val' field.
+ */
+
+struct option
+{

Review comment:
       Coding standard requires that structure names end with _s




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo commented on a change in pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
patacongo commented on a change in pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#discussion_r527420528



##########
File path: include/getopt.h
##########
@@ -0,0 +1,154 @@
+/****************************************************************************
+ * include/getopt.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_GETOPT_H
+#define __INCLUDE_GETOPT_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/compiler.h>
+
+#include <unistd.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Names for the values of the `has_arg' field of `struct option'. */
+
+#define no_argument             0
+#define required_argument       1
+#define optional_argument       2
+
+#define NO_ARG                  no_argument
+#define REQUIRED_ARG            required_argument
+#define OPTIONAL_ARG            optional_argument
+
+/* The GETOPT_DATA_INITIALIZER macro is used to initialize a statically-
+ * allocated variable of type struct getopt_data.
+ */
+
+#define GETOPT_DATA_INITIALIZER {0, 0, 1, 0, 0, 0, 0}
+
+/****************************************************************************
+ * Public Type Definitions
+ ****************************************************************************/
+
+#undef EXTERN
+#if defined(__cplusplus)
+#define EXTERN extern "C"
+extern "C"
+{
+#else
+#define EXTERN extern
+#endif
+
+/* Describe the long-named options requested by the application.
+ * The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector
+ * of `struct option' terminated by an element containing a name which is
+ * zero.
+ *
+ * The field `has_arg' is:
+ * no_argument       (or 0) if the option does not take an argument,
+ * required_argument (or 1) if the option requires an argument,
+ * optional_argument (or 2) if the option takes an optional argument.
+ *
+ * If the field `flag' is not NULL, it points to a variable that is set
+ * to the value given in the field `val' when the option is found, but
+ * left unchanged if the option is not found.
+ *
+ * To have a long-named option do something other than set an `int' to
+ * a compiled-in constant, such as set a value from `optarg', set the
+ * option's `flag' field to zero and its `val' field to a nonzero
+ * value (the equivalent single-letter option character, if there is
+ * one).  For long options that have a zero `flag' field, `getopt'
+ * returns the contents of the `val' field.
+ */
+
+struct option
+{

Review comment:
       > 
   > 
   > @patacongo could you merge this PR if no other coding/naming issue?
   
   No, I won't do that.  While I do not want to be an obstruction, I will NEVER merge a non-standard application interface into the OS that is not defined at OpenGroup.org.  Others may do that, but I will not.
   
   




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo commented on pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
patacongo commented on pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#issuecomment-731375176


   > 
   > 
   > @patacongo the code is rewritten, it shouldn't have the copyright issue now.
   
   Modifying copyrighted code does not change the copyright or release you from the obligations of the license.
   
   This must not be merged yet (even though is was incorrectly re-opened).  I believe there are license problems and, at a minumum, we need some guidance from @justinmclean
   
   If Justin thinks this practice is okay, then we can merge.


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 commented on pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#issuecomment-731514560


   Yes, The build should pass now.


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo commented on a change in pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
patacongo commented on a change in pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#discussion_r526064407



##########
File path: include/getopt.h
##########
@@ -0,0 +1,154 @@
+/****************************************************************************
+ * include/getopt.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_GETOPT_H
+#define __INCLUDE_GETOPT_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/compiler.h>
+
+#include <unistd.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Names for the values of the `has_arg' field of `struct option'. */
+
+#define no_argument             0
+#define required_argument       1
+#define optional_argument       2
+
+#define NO_ARG                  no_argument
+#define REQUIRED_ARG            required_argument
+#define OPTIONAL_ARG            optional_argument
+
+/* The GETOPT_DATA_INITIALIZER macro is used to initialize a statically-
+ * allocated variable of type struct getopt_data.
+ */
+
+#define GETOPT_DATA_INITIALIZER {0, 0, 1, 0, 0, 0, 0}
+
+/****************************************************************************
+ * Public Type Definitions
+ ****************************************************************************/
+
+#undef EXTERN
+#if defined(__cplusplus)
+#define EXTERN extern "C"
+extern "C"
+{
+#else
+#define EXTERN extern
+#endif
+
+/* Describe the long-named options requested by the application.
+ * The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector
+ * of `struct option' terminated by an element containing a name which is
+ * zero.
+ *
+ * The field `has_arg' is:
+ * no_argument       (or 0) if the option does not take an argument,
+ * required_argument (or 1) if the option requires an argument,
+ * optional_argument (or 2) if the option takes an optional argument.
+ *
+ * If the field `flag' is not NULL, it points to a variable that is set
+ * to the value given in the field `val' when the option is found, but
+ * left unchanged if the option is not found.
+ *
+ * To have a long-named option do something other than set an `int' to
+ * a compiled-in constant, such as set a value from `optarg', set the
+ * option's `flag' field to zero and its `val' field to a nonzero
+ * value (the equivalent single-letter option character, if there is
+ * one).  For long options that have a zero `flag' field, `getopt'
+ * returns the contents of the `val' field.
+ */
+
+struct option
+{
+  FAR const char *name; /* The name of the long option */
+  int has_arg;          /* One of the above macros */
+  FAR int *flag;        /* Determines if getopt_long() returns a
+                         * value for a long option; if it is
+                         * non-NULL, 0 is returned as a function
+                         * value and the value of val is stored in
+                         * the area pointed to by flag.  Otherwise,
+                         * val is returned.
+                         */
+  int val;              /* Determines the value to return if flag is
+                         * NULL.
+                         */
+};
+
+/* The getopt_data structure is for reentrancy. Its members are similar to
+ * the externally-defined variables.
+ */
+
+typedef struct getopt_data
+{

Review comment:
       Coding standard requires that structure names end with _s




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo commented on pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
patacongo commented on pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#issuecomment-731382885


   > 
   > 
   > Let me repeat again: the code is written by myself now, no code copy or modify from Gregory Pietsch:
   > https://users.cs.cf.ac.uk/KurtevaA/MSCProject/code/speak.js-master/src/getopt.h
   > If you think there are any similarity, please point out them.
   
   You cannot change the authorship or copyright on the code by editing it.  This code will ALWAYS belong to Gregory Pietsch no matter what you do to the files.
   


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo commented on pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
patacongo commented on pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#issuecomment-731387451


   I have zero confidence in the code and do not believe anything you are saying.  I am going to back out of the picture now.  I will not be merging this PR.
   
   If someone else wants to step in an be responsible for the provenance of this code, then I will not object.  But I will not be involved in this kind of code theft.
   


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a change in pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on a change in pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#discussion_r526128114



##########
File path: include/getopt.h
##########
@@ -0,0 +1,154 @@
+/****************************************************************************
+ * include/getopt.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_GETOPT_H
+#define __INCLUDE_GETOPT_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/compiler.h>
+
+#include <unistd.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Names for the values of the `has_arg' field of `struct option'. */
+
+#define no_argument             0
+#define required_argument       1
+#define optional_argument       2
+
+#define NO_ARG                  no_argument
+#define REQUIRED_ARG            required_argument
+#define OPTIONAL_ARG            optional_argument
+
+/* The GETOPT_DATA_INITIALIZER macro is used to initialize a statically-
+ * allocated variable of type struct getopt_data.
+ */
+
+#define GETOPT_DATA_INITIALIZER {0, 0, 1, 0, 0, 0, 0}
+
+/****************************************************************************
+ * Public Type Definitions
+ ****************************************************************************/
+
+#undef EXTERN
+#if defined(__cplusplus)
+#define EXTERN extern "C"
+extern "C"
+{
+#else
+#define EXTERN extern
+#endif
+
+/* Describe the long-named options requested by the application.
+ * The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector
+ * of `struct option' terminated by an element containing a name which is
+ * zero.
+ *
+ * The field `has_arg' is:
+ * no_argument       (or 0) if the option does not take an argument,
+ * required_argument (or 1) if the option requires an argument,
+ * optional_argument (or 2) if the option takes an optional argument.
+ *
+ * If the field `flag' is not NULL, it points to a variable that is set
+ * to the value given in the field `val' when the option is found, but
+ * left unchanged if the option is not found.
+ *
+ * To have a long-named option do something other than set an `int' to
+ * a compiled-in constant, such as set a value from `optarg', set the
+ * option's `flag' field to zero and its `val' field to a nonzero
+ * value (the equivalent single-letter option character, if there is
+ * one).  For long options that have a zero `flag' field, `getopt'
+ * returns the contents of the `val' field.
+ */
+
+struct option
+{

Review comment:
       I don't know, but if you grep the code base, many place follow the strategy I use here. For example, all netlink public interface doesn't add _s suffix.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo edited a comment on pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
patacongo edited a comment on pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#issuecomment-731329650


   This code seems to derive from the implementation by Gregory Pietsch.  That is public domain and not GPL:  https://users.cs.cf.ac.uk/KurtevaA/MSCProject/code/speak.js-master/src/getopt.h
   
   COPYRIGHT NOTICE AND DISCLAIMER:
   Copyright (C) 1997 Gregory Pietsch
   This file and the accompanying getopt.c implementation file are hereby 
   placed in the public domain without restrictions.  **_Just give the author 
   credit, don't claim you wrote it or prevent anyone else from using it._**
   Gregory Pietsch's current e-mail address:
   gpietsch@comcast.net
   
   I personally do not know if we can take copyrighted, public doman and claim that the ASF has the copyright.  I think that is a very bad practice... Probably illegal and at the very least is in unethical to claim to wrote and the you own code the you derive from another person's effort
   
   @justinmclean How should we handle this?


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a change in pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on a change in pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#discussion_r526105526



##########
File path: libs/libc/unistd/lib_getopterrp.c
##########
@@ -0,0 +1,57 @@
+/****************************************************************************
+ * libs/libc/unistd/lib_getopterrp.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <unistd.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/

Review comment:
       Done.

##########
File path: include/getopt.h
##########
@@ -0,0 +1,154 @@
+/****************************************************************************
+ * include/getopt.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_GETOPT_H
+#define __INCLUDE_GETOPT_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/compiler.h>
+
+#include <unistd.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Names for the values of the `has_arg' field of `struct option'. */
+
+#define no_argument             0
+#define required_argument       1
+#define optional_argument       2
+
+#define NO_ARG                  no_argument
+#define REQUIRED_ARG            required_argument
+#define OPTIONAL_ARG            optional_argument
+
+/* The GETOPT_DATA_INITIALIZER macro is used to initialize a statically-
+ * allocated variable of type struct getopt_data.
+ */
+
+#define GETOPT_DATA_INITIALIZER {0, 0, 1, 0, 0, 0, 0}
+
+/****************************************************************************
+ * Public Type Definitions
+ ****************************************************************************/
+
+#undef EXTERN
+#if defined(__cplusplus)
+#define EXTERN extern "C"
+extern "C"
+{
+#else
+#define EXTERN extern
+#endif
+
+/* Describe the long-named options requested by the application.
+ * The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector
+ * of `struct option' terminated by an element containing a name which is
+ * zero.
+ *
+ * The field `has_arg' is:
+ * no_argument       (or 0) if the option does not take an argument,
+ * required_argument (or 1) if the option requires an argument,
+ * optional_argument (or 2) if the option takes an optional argument.
+ *
+ * If the field `flag' is not NULL, it points to a variable that is set
+ * to the value given in the field `val' when the option is found, but
+ * left unchanged if the option is not found.
+ *
+ * To have a long-named option do something other than set an `int' to
+ * a compiled-in constant, such as set a value from `optarg', set the
+ * option's `flag' field to zero and its `val' field to a nonzero
+ * value (the equivalent single-letter option character, if there is
+ * one).  For long options that have a zero `flag' field, `getopt'
+ * returns the contents of the `val' field.
+ */
+
+struct option
+{
+  FAR const char *name; /* The name of the long option */
+  int has_arg;          /* One of the above macros */
+  FAR int *flag;        /* Determines if getopt_long() returns a
+                         * value for a long option; if it is
+                         * non-NULL, 0 is returned as a function
+                         * value and the value of val is stored in
+                         * the area pointed to by flag.  Otherwise,
+                         * val is returned.
+                         */
+  int val;              /* Determines the value to return if flag is
+                         * NULL.
+                         */
+};
+
+/* The getopt_data structure is for reentrancy. Its members are similar to
+ * the externally-defined variables.
+ */
+
+typedef struct getopt_data
+{
+  FAR char *optarg;
+  int optind;
+  int opterr;
+  int optopt;
+  int optwhere;
+  int permute_from;
+  int num_nonopts;
+} getopt_data;

Review comment:
       Done.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo removed a comment on pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
patacongo removed a comment on pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#issuecomment-731383446


   Anyone who merges this PR is potentially an accessory to a crime.
   
   My recommendation is that you wait for the advice of @justinmclean  I will abide by that.


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a change in pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on a change in pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#discussion_r527931070



##########
File path: include/getopt.h
##########
@@ -0,0 +1,117 @@
+/****************************************************************************
+ * include/getopt.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_GETOPT_H
+#define __INCLUDE_GETOPT_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/compiler.h>
+
+#include <unistd.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Names for the values of the `has_arg' field of `struct option'. */
+
+#define no_argument             0
+#define required_argument       1
+#define optional_argument       2
+

Review comment:
       all of them are part of API definition:
   https://www.freebsd.org/cgi/man.cgi?getopt_long(3)
   https://linux.die.net/man/3/getopt_long
   we have to keep as lowercase.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo commented on pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
patacongo commented on pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#issuecomment-731402283


   @xiaoxiang781216 fails:
   
       Error: unistd/lib_getopt.c:300:34: error: 'struct option' declared inside parameter list will not be visible outside of this definition or declaration [-Werror]
         300 |                 FAR const struct option *longopts,
             |                                  ^~~~~~


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo commented on a change in pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo commented on pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
patacongo commented on pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#issuecomment-731391670


   The C code comes clearly from Gregory Pietsch implementation https://github.com/eblot/newlib/blob/master/newlib/libc/stdlib/getopt.c down to variable names and functions names.  That code was stolen.  That is undeniable.
   
   I am comparing this against the original PR code not the cut down thing that is in the PR now.  I trust nothing now.  How can I tell what code you have stolen and what you have writing yourself?
   
   It looks like you have removed most of the original PR so that only getopt_long() remains.  Do you claim that that is your original work?


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo commented on a change in pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
patacongo commented on a change in pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#discussion_r526065660



##########
File path: libs/libc/unistd/lib_getopterrp.c
##########
@@ -0,0 +1,57 @@
+/****************************************************************************
+ * libs/libc/unistd/lib_getopterrp.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <unistd.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/

Review comment:
       Empty section block comments can be removed.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a change in pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on a change in pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#discussion_r526103172



##########
File path: include/getopt.h
##########
@@ -0,0 +1,154 @@
+/****************************************************************************
+ * include/getopt.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_GETOPT_H
+#define __INCLUDE_GETOPT_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/compiler.h>
+
+#include <unistd.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Names for the values of the `has_arg' field of `struct option'. */
+
+#define no_argument             0
+#define required_argument       1
+#define optional_argument       2
+
+#define NO_ARG                  no_argument
+#define REQUIRED_ARG            required_argument
+#define OPTIONAL_ARG            optional_argument
+
+/* The GETOPT_DATA_INITIALIZER macro is used to initialize a statically-
+ * allocated variable of type struct getopt_data.
+ */
+
+#define GETOPT_DATA_INITIALIZER {0, 0, 1, 0, 0, 0, 0}
+
+/****************************************************************************
+ * Public Type Definitions
+ ****************************************************************************/
+
+#undef EXTERN
+#if defined(__cplusplus)
+#define EXTERN extern "C"
+extern "C"
+{
+#else
+#define EXTERN extern
+#endif
+
+/* Describe the long-named options requested by the application.
+ * The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector
+ * of `struct option' terminated by an element containing a name which is
+ * zero.
+ *
+ * The field `has_arg' is:
+ * no_argument       (or 0) if the option does not take an argument,
+ * required_argument (or 1) if the option requires an argument,
+ * optional_argument (or 2) if the option takes an optional argument.
+ *
+ * If the field `flag' is not NULL, it points to a variable that is set
+ * to the value given in the field `val' when the option is found, but
+ * left unchanged if the option is not found.
+ *
+ * To have a long-named option do something other than set an `int' to
+ * a compiled-in constant, such as set a value from `optarg', set the
+ * option's `flag' field to zero and its `val' field to a nonzero
+ * value (the equivalent single-letter option character, if there is
+ * one).  For long options that have a zero `flag' field, `getopt'
+ * returns the contents of the `val' field.
+ */
+
+struct option
+{

Review comment:
       But option struct is a defacto standard:
   https://www.freebsd.org/cgi/man.cgi?getopt_long(3)
   https://www.gnu.org/software/libc/manual/html_node/Getopt-Long-Options.html
   renaming it lose the compatablity.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo edited a comment on pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
patacongo edited a comment on pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#issuecomment-731329650


   This code seems to derive from the implementation by Gregory Pietsch.  That is public domain and not GPL:  https://users.cs.cf.ac.uk/KurtevaA/MSCProject/code/speak.js-master/src/getopt.h
   
   COPYRIGHT NOTICE AND DISCLAIMER:
   Copyright (C) 1997 Gregory Pietsch
   This file and the accompanying getopt.c implementation file are hereby 
   placed in the public domain without restrictions.  **_Just give the author 
   credit, don't claim you wrote it or prevent anyone else from using it._**
   Gregory Pietsch's current e-mail address:
   gpietsch@comcast.net
   
   I personally do not know if we can take copyrighted, public doman and claim that the ASF has the copyright.  I think that is a very bad practice... Probably illegal and at the very least is in unethical to claim to wrote and the you own code the you derive from another person's effort
   
   @justinmclean How should we handle this?  It seems to me that this must be treated as third party code if we want to incorporate it (I think we do not).


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo commented on a change in pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
patacongo commented on a change in pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#discussion_r527859737



##########
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)
 {

Review comment:
       This function requires a proper function header per the coding standard.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 edited a comment on pull request #2326: libc: Enhance getopt function

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 edited a comment on pull request #2326:
URL: https://github.com/apache/incubator-nuttx/pull/2326#issuecomment-731379509


   Let me repeat again: the code is written by myself now, no code come from Gregory Pietsch.


----------------------------------------------------------------
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:
users@infra.apache.org