xiaoxiang781216 commented on code in PR #3168:
URL: https://github.com/apache/nuttx-apps/pull/3168#discussion_r2306442660
##########
nshlib/nsh_builtin.c:
##########
@@ -49,6 +49,83 @@
* Public Functions
****************************************************************************/
+#ifdef CONFIG_NSH_BUILTIN_AS_COMMAND
+
+#define MAX_ARGC 256
+
+/****************************************************************************
+ * Name: nsh_builtin
+ *
+ * Description:
+ * Try to execute a built-in command (registered at compile time).
+ * When CONFIG_NSH_BUILTIN_AS_COMMAND is defined, the command will not be
+ * executed by creating a new task, but instead by directly calling the
+ * corresponding builtin main() function.
+ *
+ * Input Parameters:
+ * vtbl - NSH vtable pointer, provides input/output interfaces
+ * (unused here)
+ * cmd - The command string to be executed
+ * argv - Argument array, argv[0] is the command itself
+ *
+ * Returned Value:
+ * -1 (ERROR) If the command does not exist, too many arguments, or
+ * builtin lookup fails
+ * Other The return value from builtin->main(argc, argv)
+ *
+ ****************************************************************************/
+
+int nsh_builtin(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
+ FAR char **argv,
+ FAR const struct nsh_param_s *param)
+{
+ const struct builtin_s *builtin;
+ int argc;
+ int index;
+
+ UNUSED(vtbl);
+
+ /* Check if the command is available in the builtin list */
+
+ index = builtin_isavail(cmd);
+ if (index < 0)
+ {
+ return -1;
+ }
+
+ /* Get the builtin structure by index */
+
+ builtin = builtin_for_index(index);
+ if (builtin == NULL)
+ {
+ return -1;
+ }
+
+ /* Count the number of arguments, ensure it does not exceed MAX_ARGC */
+
+ argc = 0;
+ if (argv != NULL)
+ {
+ while (argv[argc])
+ {
+ if (++argc > MAX_ARGC)
+ {
+ return -1;
+ }
+ }
+ }
+ else
+ {
+ return -1;
+ }
+
+ /* Directly call the builtin main() function to execute the command */
+
+ return (builtin->main)(argc, (FAR char **)argv);
Review Comment:
it's better to move to nsh_command
##########
nshlib/Kconfig:
##########
@@ -5,6 +5,12 @@
menu "NSH Library"
+config NSH_BUILTIN_AS_COMMAND
Review Comment:
move after
https://github.com/apache/nuttx-apps/pull/3168/files#diff-db678cbb3e1ee7ce9282fc5e697f533c7f50c8aee4a4fd84871eb66fff54e19eL235
and should be exclusived from NSH_BUILTIN_APPS.
--
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]