casaroli commented on code in PR #3952:
URL: https://github.com/apache/nuttx-apps/pull/3952#discussion_r4441025906


##########
system/nxinit/builtin.c:
##########
@@ -359,68 +399,203 @@ static int cmd_unlink(FAR struct action_manager_s *am,
   return ret;
 }
 
-/****************************************************************************
- * Public Functions
- ****************************************************************************/
+#ifndef CONFIG_DISABLE_MOUNTPOINT
+/* Take the flags (g_mount_flags) out of the comma-separated list in
+ * options and return them.  The file system options stay in options.
+ */
 
-/****************************************************************************
- * Name: init_builtin_run
- *
- * Description:
- *   Execute the builtin command of NxInit, builtin App, or NSH builtin.
- *
- * Input Parameters:
- *   am   - Instance of Action Manager.
- *   argc - Argument count.
- *   argv - A pointer to an array of string arguments. The end of the array
- *          is indicated with a NULL entry.
+static unsigned long mount_parse_options(FAR char *options)
+{
+  FAR char *src = options;
+  FAR char *dst = options;
+  unsigned long flags = 0;
+  FAR char *end;
+  size_t len;
+  size_t j;
+
+  while (*src != '\0')
+    {
+      end = strchrnul(src, ',');
+      len = end - src;
+
+      for (j = 0; j < nitems(g_mount_flags); j++)
+        {
+          if (!strncmp(g_mount_flags[j].name, src, len) &&
+              g_mount_flags[j].name[len] == '\0')
+            {
+              flags |= g_mount_flags[j].flag;
+              break;
+            }
+        }
+
+      if (j == nitems(g_mount_flags) && len > 0)
+        {
+          if (dst != options)
+            {
+              *dst++ = ',';
+            }
+
+          memmove(dst, src, len);
+          dst += len;
+        }
+
+      src = *end == ',' ? end + 1 : end;
+    }
+
+  *dst = '\0';
+  return flags;
+}
+
+/* mount <type> <device> <dir> [<flag>]* [<options>]
+ * mount -t <type> [-o <options>] [<device>] <dir>
  *
- * Returned Value:
- *   Returns 0 (for NxInit builtin) or a PID (for builtin App or NSH builtin)
- *   on success; Returns the negative value of errno on failure.
- ****************************************************************************/
+ * <flag> is one of g_mount_flags.  In the first form, a last argument that
+ * is not a flag is the option string of the file system (the data of
+ * mount()).  The second form is the one of the NSH command: <options> is a
+ * comma-separated list of flags and file system options.
+ */
 
-int init_builtin_run(FAR struct action_manager_s *am,
+static int cmd_mount(FAR struct action_manager_s *am,
                      int argc, FAR char **argv)
 {
-  char cmd[CONFIG_SYSTEM_NXINIT_RC_LINE_MAX];
-  posix_spawnattr_t attr;
-  FAR char *args[4];
-  sigset_t blocked;
-  sigset_t builtin;
-  sigset_t mask;
-  pid_t pid;
-  size_t i;
+  FAR const char *source = NULL;
+  FAR const char *target;
+  FAR const char *type = NULL;
+  FAR const char *data = NULL;
+  FAR char *options = NULL;
+  unsigned long flags = 0;
+  size_t j;
   int ret;
+  int i;
 
-  for (i = 0; i < nitems(g_builtin); i++)
+  UNUSED(am);
+
+  if (argc > 1 && argv[1][0] == '-')
     {
-      if (!strcmp(g_builtin[i].cmd, argv[0]))
+      for (i = 1; i < argc - 1 && argv[i][0] == '-'; i += 2)
         {
-          if (argc < g_builtin[i].minargs || argc > g_builtin[i].maxargs)
+          if (!strcmp(argv[i], "-t"))
             {
-              init_err("Executing command '%s': invalid argument", argv[0]);
-              init_dump_args(argc, argv);
-              return -EINVAL;
+              type = argv[i + 1];
             }
+          else if (!strcmp(argv[i], "-o"))
+            {
+              data = argv[i + 1];
+            }
+          else
+            {
+              break;
+            }
+        }
 
-          init_info("Executing command '%s'", argv[0]);
+      if (type == NULL || i >= argc || argv[i][0] == '-' || argc - i > 2)
+        {
+          init_err("mount: bad arguments");
+          return -EINVAL;
+        }
 
-          /* Init blocks all signals.  Run the builtin command with the
-           * original mask, as tasks and threads it creates (e.g. the NTP
-           * client and the network monitor started by netinit) inherit
-           * the mask of this task.  Keep SIGCHLD blocked so that a child
-           * exiting meanwhile still wakes up ppoll() in init.
-           */
+      if (argc - i == 2)
+        {
+          source = argv[i++];
+        }
 
-          builtin = am->sigmask;
-          sigaddset(&builtin, SIGCHLD);
-          sigprocmask(SIG_SETMASK, &builtin, &blocked);
-          ret = g_builtin[i].func(am, argc, argv);
-          sigprocmask(SIG_SETMASK, &blocked, NULL);
-          return ret;
+      target = argv[i];
+
+      /* The command can run again: parse a copy of the options */
+
+      if (data != NULL)
+        {
+          options = strdup(data);
+          if (options == NULL)
+            {
+              return -ENOMEM;
+            }
+
+          flags = mount_parse_options(options);
+          data = *options != '\0' ? options : NULL;
+        }
+    }
+  else if (argc >= 4)
+    {
+      type   = argv[1];
+      source = argv[2];
+      target = argv[3];
+
+      for (i = 4; i < argc; i++)
+        {
+          for (j = 0; j < nitems(g_mount_flags); j++)
+            {
+              if (!strcmp(argv[i], g_mount_flags[j].name))
+                {
+                  flags |= g_mount_flags[j].flag;
+                  break;
+                }
+            }
+
+          if (j < nitems(g_mount_flags))
+            {
+              continue;
+            }
+
+          if (i != argc - 1)
+            {
+              init_err("mount '%s': unknown flag '%s'", target, argv[i]);
+              return -EINVAL;
+            }
+
+          data = argv[i];
         }
     }
+  else
+    {
+      return builtin_spawn(argc, argv);

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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to