Re: [PR] nshlib/prompt: add empty prompt string fallbacks [nuttx-apps]

2024-02-25 Thread via GitHub


xiaoxiang781216 merged PR #2300:
URL: https://github.com/apache/nuttx-apps/pull/2300


-- 
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: commits-unsubscr...@nuttx.apache.org

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



Re: [PR] nshlib/prompt: add empty prompt string fallbacks [nuttx-apps]

2024-02-24 Thread via GitHub


yf13 commented on code in PR #2300:
URL: https://github.com/apache/nuttx-apps/pull/2300#discussion_r1501750927


##
nshlib/nsh_prompt.c:
##
@@ -0,0 +1,104 @@
+/
+ * apps/nshlib/nsh_prompt.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 
+
+#include 
+#include 
+#include 
+
+#include "nsh.h"
+
+/
+ * Preprocessor Macros
+ /
+
+#define NSH_PROMPT_SUFFIX_LEN   (sizeof(CONFIG_NSH_PROMPT_SUFFIX) - 1)
+#define NSH_PROMPT_SIZE(CONFIG_NSH_PROMPT_MAX - NSH_PROMPT_SUFFIX_LEN)

Review Comment:
   Okay, that is why I used short names before.



-- 
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: commits-unsubscr...@nuttx.apache.org

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



Re: [PR] nshlib/prompt: add empty prompt string fallbacks [nuttx-apps]

2024-02-24 Thread via GitHub


xiaoxiang781216 commented on code in PR #2300:
URL: https://github.com/apache/nuttx-apps/pull/2300#discussion_r1501745067


##
nshlib/nsh_prompt.c:
##
@@ -0,0 +1,104 @@
+/
+ * apps/nshlib/nsh_prompt.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 
+
+#include 
+#include 
+#include 
+
+#include "nsh.h"
+
+/
+ * Preprocessor Macros
+ /
+
+#define NSH_PROMPT_SUFFIX_LEN   (sizeof(CONFIG_NSH_PROMPT_SUFFIX) - 1)
+#define NSH_PROMPT_SIZE(CONFIG_NSH_PROMPT_MAX - NSH_PROMPT_SUFFIX_LEN)

Review Comment:
   align or remove NSH_PROMPT_SUFFIX_LEN



-- 
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: commits-unsubscr...@nuttx.apache.org

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



Re: [PR] nshlib/prompt: add empty prompt string fallbacks [nuttx-apps]

2024-02-24 Thread via GitHub


xiaoxiang781216 commented on code in PR #2300:
URL: https://github.com/apache/nuttx-apps/pull/2300#discussion_r1501733238


##
nshlib/nsh_prompt.c:
##
@@ -0,0 +1,104 @@
+/
+ * apps/nshlib/nsh_prompt.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 
+
+#include 

Review Comment:
   add #include 



-- 
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: commits-unsubscr...@nuttx.apache.org

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



Re: [PR] nshlib/prompt: add empty prompt string fallbacks [nuttx-apps]

2024-02-24 Thread via GitHub


xiaoxiang781216 commented on code in PR #2300:
URL: https://github.com/apache/nuttx-apps/pull/2300#discussion_r1501731483


##
nshlib/nsh_prompt.c:
##
@@ -0,0 +1,104 @@
+/
+ * apps/nshlib/nsh_prompt.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 
+
+#include 
+#include 
+
+#ifndef CONFIG_NSH_CLE
+#  include "system/readline.h"
+#endif
+
+#include "nsh.h"
+
+/
+ * Preprocessor Macros
+ /
+
+#define SUFFIX_SIZE sizeof(CONFIG_NSH_PROMPT_SUFFIX)
+#define SOURCE_SIZE (CONFIG_NSH_PROMPT_MAX - SUFFIX_SIZE + 1)
+
+/
+ * Private Variables
+ /
+
+static char g_nshprompt[CONFIG_NSH_PROMPT_MAX] = CONFIG_NSH_PROMPT_STRING;
+
+/
+ * Public Functions
+ /
+
+/
+ * Name: nsh_update_prompt
+ *
+ * Description:
+ *   This function updates g_nshprompt with latest data based on prioritized
+ *   sources, from highest to lowest priority:
+ *
+ *   - non-empty NSH_PROMPT_STRING w/o appending suffix
+ *   - non-empty NSH_PROMPT_ENV variable value with suffix appended
+ *   - non-empty HOSTNAME with g_prompt_suffix appended
+ *   - g_prompt_suffix
+ *
+ * Results:
+ *   - updated g_nsh_prompt value if necessary.
+ *
+ /
+
+void nsh_update_prompt(void)
+{
+  static_assert(CONFIG_NSH_PROMPT_MAX > sizeof(CONFIG_NSH_PROMPT_STRING));
+  static_assert(CONFIG_NSH_PROMPT_MAX > sizeof(CONFIG_NSH_PROMPT_SUFFIX));
+
+  if (CONFIG_NSH_PROMPT_STRING[0] == '\0')

Review Comment:
   let's change the priority, like this:
   ```
   #ifndef CONFIG_DISABLE_ENVIRON
   if (getenv(CONFIG_NSH_PROMPT_ENV))
 {
strlcpy(g_nshprompt, getenv(CONFIG_NSH_PROMPT_ENV), SOURCE_SIZE);
strcat(g_nshprompt, CONFIG_NSH_PROMPT_SUFFIX);
 }
   else
   #endif
   if (CONFIG_NSH_PROMPT_STRING[0])
 {
   strcpy(g_nshprompt, CONFIG_NSH_PROMPT_STRING);
 }
   else
 {
gethostname(g_nshprompt, SOURCE_SIZE);
strcat(g_nshprompt, CONFIG_NSH_PROMPT_SUFFIX);
 }
   ```



##
nshlib/nsh_prompt.c:
##
@@ -0,0 +1,104 @@
+/
+ * apps/nshlib/nsh_prompt.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 
+
+#include 
+#include 
+
+#ifndef CONFIG_NSH_CLE
+#  include "system/readline.h"
+#endif
+
+#include "nsh.h"
+

Re: [PR] nshlib/prompt: add empty prompt string fallbacks [nuttx-apps]

2024-02-24 Thread via GitHub


xiaoxiang781216 commented on code in PR #2300:
URL: https://github.com/apache/nuttx-apps/pull/2300#discussion_r1501638151


##
nshlib/nsh_prompt.c:
##
@@ -0,0 +1,107 @@
+/
+ * apps/nshlib/nsh_prompt.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 
+
+#include 
+#include 
+
+#ifndef CONFIG_NSH_CLE
+#  include "system/readline.h"

Review Comment:
   remove too



##
nshlib/nsh_prompt.c:
##
@@ -0,0 +1,107 @@
+/
+ * apps/nshlib/nsh_prompt.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 
+
+#include 
+#include 
+
+#ifndef CONFIG_NSH_CLE
+#  include "system/readline.h"
+#endif
+
+#include "nsh.h"
+
+/
+ * Preprocessor Macros
+ /
+
+#define MIN(a,b)   (((a) < (b)) ? (a) : (b))
+
+#define SUFFIX_MAX  (CONFIG_NSH_PROMPT_SUFFIX_MAX)
+#define PROMPT_MAX  (CONFIG_NSH_PROMPT_MAX - SUFFIX_MAX)
+
+/
+ * Private Variables
+ /
+
+static char g_nshprompt[CONFIG_NSH_PROMPT_MAX] = CONFIG_NSH_PROMPT_STRING;
+
+/
+ * Public Functions
+ /
+
+/
+ * Name: nsh_update_prompt
+ *
+ * Description:
+ *   This function updates g_nshprompt with latest data based on prioritized
+ *   sources, from highest to lowest priority:
+ *
+ *   - non-empty NSH_PROMPT_STRING w/o appending suffix
+ *   - non-empty NSH_PROMPT_ENV variable value with suffix appended
+ *   - non-empty HOSTNAME with g_prompt_suffix appended
+ *   - g_prompt_suffix
+ *
+ * Results:
+ *   - updated g_nsh_prompt value if necessary.
+ *
+ /
+
+void nsh_update_prompt(void)
+{
+  size_t i; /* position to appending the suffix */
+
+  if (CONFIG_NSH_PROMPT_STRING[0] == '\0')
+{
+#ifndef CONFIG_DISABLE_ENVIRON
+  if (getenv(CONFIG_NSH_PROMPT_ENV))
+{
+  strlcpy(g_nshprompt, getenv(CONFIG_NSH_PROMPT_ENV),
+  sizeof(g_nshprompt));

Review Comment:
   ```suggestion
 NSH_PROMPT_MAX);
   ```



##
nshlib/nsh_prompt.c:
##
@@ -0,0 +1,107 @@
+/
+ * apps/nshlib/nsh_prompt.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file 

Re: [PR] nshlib/prompt: add empty prompt string fallbacks [nuttx-apps]

2024-02-24 Thread via GitHub


xiaoxiang781216 commented on code in PR #2300:
URL: https://github.com/apache/nuttx-apps/pull/2300#discussion_r1501472111


##
nshlib/nsh_prompt.c:
##
@@ -0,0 +1,108 @@
+/
+ * apps/nshlib/nsh_prompt.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 
+
+#include 
+
+#ifndef CONFIG_NSH_CLE
+#  include "system/readline.h"
+#endif
+
+#include "nsh.h"
+
+/
+ * Preprocessor Macros
+ /
+
+#define PROMPT_SIZECONFIG_NSH_PROMPT_MAX
+#define SUFFIX_SIZECONFIG_NSH_PROMPT_SUFFIX_MAX
+
+#define SUF_POS_MAX(PROMPT_SIZE - SUFFIX_SIZE)

Review Comment:
   ```suggestion
   #define SUFFIX_MAX  (CONFIG_NSH_PROMPT_MAX - 
sizeof(CONFIG_NSH_PROMPT_SUFFIX))
   ```
   and remove NSH_PROMPT_SUFFIX_MAX from Kconfig



##
nshlib/nsh_prompt.c:
##
@@ -0,0 +1,108 @@
+/
+ * apps/nshlib/nsh_prompt.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 
+
+#include 
+
+#ifndef CONFIG_NSH_CLE
+#  include "system/readline.h"
+#endif
+
+#include "nsh.h"
+
+/
+ * Preprocessor Macros
+ /
+
+#define PROMPT_SIZECONFIG_NSH_PROMPT_MAX
+#define SUFFIX_SIZECONFIG_NSH_PROMPT_SUFFIX_MAX
+
+#define SUF_POS_MAX(PROMPT_SIZE - SUFFIX_SIZE)

Review Comment:
   ```suggestion
   #define NSH_SUFFIX_MAX  (CONFIG_NSH_PROMPT_MAX - 
sizeof(CONFIG_NSH_PROMPT_SUFFIX))
   ```
   and remove NSH_PROMPT_SUFFIX_MAX from Kconfig



-- 
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: commits-unsubscr...@nuttx.apache.org

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



Re: [PR] nshlib/prompt: add empty prompt string fallbacks [nuttx-apps]

2024-02-24 Thread via GitHub


xiaoxiang781216 commented on code in PR #2300:
URL: https://github.com/apache/nuttx-apps/pull/2300#discussion_r1501472111


##
nshlib/nsh_prompt.c:
##
@@ -0,0 +1,108 @@
+/
+ * apps/nshlib/nsh_prompt.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 
+
+#include 
+
+#ifndef CONFIG_NSH_CLE
+#  include "system/readline.h"
+#endif
+
+#include "nsh.h"
+
+/
+ * Preprocessor Macros
+ /
+
+#define PROMPT_SIZECONFIG_NSH_PROMPT_MAX
+#define SUFFIX_SIZECONFIG_NSH_PROMPT_SUFFIX_MAX
+
+#define SUF_POS_MAX(PROMPT_SIZE - SUFFIX_SIZE)

Review Comment:
   ```suggestion
   #define NSH_SUFFIX_MAX (CONFIG_NSH_PROMPT_MAX - 
sizeof(CONFIG_NSH_PROMPT_SUFFIX))
   ```
   and remove NSH_PROMPT_SUFFIX_MAX from Kconfig



-- 
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: commits-unsubscr...@nuttx.apache.org

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



Re: [PR] nshlib/prompt: add empty prompt string fallbacks [nuttx-apps]

2024-02-24 Thread via GitHub


xiaoxiang781216 commented on code in PR #2300:
URL: https://github.com/apache/nuttx-apps/pull/2300#discussion_r1501470565


##
nshlib/nsh_prompt.c:
##
@@ -0,0 +1,108 @@
+/
+ * apps/nshlib/nsh_prompt.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 
+
+#include 
+
+#ifndef CONFIG_NSH_CLE
+#  include "system/readline.h"
+#endif
+
+#include "nsh.h"
+
+/
+ * Preprocessor Macros
+ /
+
+#define PROMPT_SIZECONFIG_NSH_PROMPT_MAX
+#define SUFFIX_SIZECONFIG_NSH_PROMPT_SUFFIX_MAX
+
+#define SUF_POS_MAX(PROMPT_SIZE - SUFFIX_SIZE)
+
+/
+ * Private Variables
+ /
+
+static char g_nshprompt[PROMPT_SIZE]= CONFIG_NSH_PROMPT_STRING;
+static const char g_prompt_suffix[SUFFIX_SIZE]  = CONFIG_NSH_PROMPT_SUFFIX;
+
+/
+ * Public Functions
+ /
+
+/
+ * Name: nsh_update_prompt
+ *
+ * Description:
+ *   This function updates g_nshprompt with latest data based on prioritized
+ *   sources, from highest to lowest priority:
+ *
+ *   - non-empty NSH_PROMPT_STRING w/o appending suffix
+ *   - non-empty NSH_PROMPT_ENV variable value with suffix appended
+ *   - non-empty HOSTNAME with g_prompt_suffix appended
+ *   - g_prompt_suffix
+ *
+ * Results:
+ *   - updated g_nsh_prompt value if necessary.
+ *
+ /
+
+void nsh_update_prompt(void)
+{
+  size_t j; /* position to appending the suffix */
+
+  if (CONFIG_NSH_PROMPT_STRING[0] == '\0')
+{
+#ifndef CONFIG_DISABLE_ENVIRON
+  if (getenv(CONFIG_NSH_PROMPT_ENV))
+{
+  strlcpy(g_nshprompt, getenv(CONFIG_NSH_PROMPT_ENV),
+  sizeof(g_nshprompt));
+}
+  else
+#endif
+{
+  gethostname(g_nshprompt, sizeof(g_nshprompt));
+}
+
+  j = strlen(g_nshprompt);
+  j = (j <= SUF_POS_MAX) ? j : SUF_POS_MAX;
+  strlcpy(_nshprompt[j], g_prompt_suffix, PROMPT_SIZE - j);
+}
+}
+
+/
+ * Name: nsh_prompt
+ *
+ * Description:
+ *   This function returns latest prompt string.
+ *   It is needed as g_nshprompt is no longer public.
+ *
+ /
+
+char *nsh_prompt(void)

Review Comment:
   ```suggestion
   FAR const char *nsh_prompt(void)
   ```



##
nshlib/nsh_prompt.c:
##
@@ -0,0 +1,108 @@
+/
+ * apps/nshlib/nsh_prompt.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.
+ *
+ 

Re: [PR] nshlib/prompt: add empty prompt string fallbacks [nuttx-apps]

2024-02-24 Thread via GitHub


yf13 commented on code in PR #2300:
URL: https://github.com/apache/nuttx-apps/pull/2300#discussion_r1501468144


##
nshlib/nsh_prompt.c:
##
@@ -0,0 +1,106 @@
+/
+ * apps/nshlib/nsh_prompt.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 
+
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#ifdef CONFIG_NSH_CLE
+#  include "system/cle.h"
+#else
+#  include "system/readline.h"
+#endif
+
+#include "nsh.h"
+#include "nsh_console.h"
+
+/
+ * Private Variables
+ /
+
+static char g_nshprompt[CONFIG_NSH_PROMPT_MAX] = CONFIG_NSH_PROMPT_STRING;
+
+static const char g_prompt_suffix[] = CONFIG_NSH_PROMPT_SUFFIX;

Review Comment:
   Maybe not, we need limit the length to prevent the corner case where suffix 
size is longer than prompt max. See the latest version please.



-- 
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: commits-unsubscr...@nuttx.apache.org

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



Re: [PR] nshlib/prompt: add empty prompt string fallbacks [nuttx-apps]

2024-02-24 Thread via GitHub


xiaoxiang781216 commented on code in PR #2300:
URL: https://github.com/apache/nuttx-apps/pull/2300#discussion_r1501455201


##
nshlib/nsh_init.c:
##
@@ -110,10 +110,14 @@ void nsh_initialize(void)
   FAR struct console_stdio_s *pstate;
 #endif
 
+  /* populate NSH prompt string */
+
+  nsh_update_prompt();
+
 #if defined(CONFIG_NSH_READLINE) && defined(CONFIG_READLINE_TABCOMPLETION)
-  /* Configure the NSH prompt */
+  /* Configure readline prompt */

Review Comment:
   OK



-- 
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: commits-unsubscr...@nuttx.apache.org

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



Re: [PR] nshlib/prompt: add empty prompt string fallbacks [nuttx-apps]

2024-02-24 Thread via GitHub


yf13 commented on code in PR #2300:
URL: https://github.com/apache/nuttx-apps/pull/2300#discussion_r1501438438


##
nshlib/nsh_init.c:
##
@@ -110,10 +110,14 @@ void nsh_initialize(void)
   FAR struct console_stdio_s *pstate;
 #endif
 
+  /* populate NSH prompt string */
+
+  nsh_update_prompt();
+
 #if defined(CONFIG_NSH_READLINE) && defined(CONFIG_READLINE_TABCOMPLETION)
-  /* Configure the NSH prompt */
+  /* Configure readline prompt */

Review Comment:
   It seems no need as readline only holds the address, not a copy.



-- 
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: commits-unsubscr...@nuttx.apache.org

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



Re: [PR] nshlib/prompt: add empty prompt string fallbacks [nuttx-apps]

2024-02-24 Thread via GitHub


xiaoxiang781216 commented on code in PR #2300:
URL: https://github.com/apache/nuttx-apps/pull/2300#discussion_r1501411924


##
nshlib/Kconfig:
##
@@ -66,7 +66,31 @@ config NSH_PROMPT_STRING
string "Prompt String"
default "nsh> "
---help---
-   Provide the shell prompt string, default is "nsh> ".
+   Provide the shell prompt string with size limit NSH_PROMPT_MAX.
+   default is "nsh> ".
+
+config NSH_PROMPT_MAX
+   int "Maximum Size of Prompt String"
+   default 16
+   ---help---
+   The maximum size of shell prompt string.
+
+config NSH_PROMPT_ENV
+   string "Prompt String Environment Variable"
+   default "PS1"
+   depends on !DISABLE_ENVIRON
+   ---help---
+   The environment variable name containing prompt string.
+   Only used when NSH_PROMPT_STRING is empty.
+
+config NSH_PROMPT_SUFFIX
+  string "Suffix used to derive fallback prompt string"

Review Comment:
   tab



##
nshlib/nsh_prompt.c:
##
@@ -0,0 +1,106 @@
+/
+ * apps/nshlib/nsh_prompt.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 
+
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#ifdef CONFIG_NSH_CLE
+#  include "system/cle.h"
+#else
+#  include "system/readline.h"
+#endif
+
+#include "nsh.h"
+#include "nsh_console.h"
+
+/
+ * Private Variables
+ /
+
+static char g_nshprompt[CONFIG_NSH_PROMPT_MAX] = CONFIG_NSH_PROMPT_STRING;
+
+static const char g_prompt_suffix[] = CONFIG_NSH_PROMPT_SUFFIX;

Review Comment:
   let use macro directly?



##
nshlib/nsh_prompt.c:
##
@@ -0,0 +1,106 @@
+/
+ * apps/nshlib/nsh_prompt.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 
+
+#include 
+#include 
+#include 
+#include 
+#include 

Review Comment:
   remove all unneeded header file



##
nshlib/nsh_init.c:
##
@@ -110,10 +110,14 @@ void nsh_initialize(void)
   FAR struct console_stdio_s *pstate;
 #endif
 
+  /* populate NSH prompt string */
+
+  nsh_update_prompt();
+
 #if defined(CONFIG_NSH_READLINE) && defined(CONFIG_READLINE_TABCOMPLETION)
-  /* Configure the NSH prompt */
+  /* Configure readline prompt */

Review Comment:
   should we call readline_prompt in nsh_update_prompt



##
nshlib/nsh_prompt.c:
##
@@ -0,0 +1,106 @@
+/
+ * apps/nshlib/nsh_prompt.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for 

Re: [PR] nshlib/prompt: add empty prompt string fallbacks [nuttx-apps]

2024-02-23 Thread via GitHub


yf13 commented on code in PR #2300:
URL: https://github.com/apache/nuttx-apps/pull/2300#discussion_r1501348259


##
nshlib/nsh_prompt.c:
##
@@ -0,0 +1,122 @@
+/
+ * apps/nshlib/nsh_prompt.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 
+
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#ifdef CONFIG_NSH_CLE
+#  include "system/cle.h"
+#else
+#  include "system/readline.h"
+#endif
+
+#include "nsh.h"
+#include "nsh_console.h"
+
+/
+ * Public Variables
+ /
+
+/* The NSH prompt */
+
+char g_nshprompt[NSH_PROMPT_MAX] = CONFIG_NSH_PROMPT_STRING;
+
+/
+ * Private Variables
+ /
+
+static const char g_prompt_suffix[]   = "> ";
+
+#ifndef CONFIG_DISABLE_ENVIRON
+static const char g_prompt_env[]  = CONFIG_NSH_PROMPT_ENV;
+#endif
+
+#if defined(CONFIG_NSH_READLINE) && defined(CONFIG_READLINE_TABCOMPLETION) && \
+defined(CONFIG_READLINE_HAVE_EXTMATCH)
+static const struct extmatch_vtable_s g_nsh_extmatch =
+{
+  nsh_extmatch_count,  /* count_matches */
+  nsh_extmatch_getname /* getname */
+};
+#endif
+
+/
+ * Public Functions
+ /
+
+/
+ * Name: nsh_refresh_prompt
+ *
+ * Description:
+ *   This function updates g_nshprompt with latest data based on prioritized
+ *   sources, from highest to lowest priority:
+ *
+ *   - non-empty NSH_PROMPT_STRING w/o appending suffix
+ *   - non-empty NSH_PROMPT_ENV variable value with suffix appended
+ *   - non-empty HOSTNAME with g_prompt_suffix appended
+ *   - g_prompt_suffix
+ *
+ * Results:
+ *   - updated g_nsh_prompt value if necessary.
+ *
+ /
+
+void nsh_update_prompt(void)
+{
+  if (strlen(CONFIG_NSH_PROMPT_STRING) == 0)
+{
+#ifndef CONFIG_DISABLE_ENVIRON
+  if (strlen(g_prompt_env) && getenv(g_prompt_env))
+{
+  strlcpy(g_nshprompt, getenv(g_prompt_env), sizeof(g_nshprompt));
+}
+  else
+#endif
+{
+  gethostname(g_nshprompt, sizeof(g_nshprompt));
+}
+
+  uint16_t j = strlen(g_nshprompt) + strlen(g_prompt_suffix) + 1 ;
+  j = (j <= sizeof(g_nshprompt)) ? strlen(g_nshprompt) :
+  sizeof(g_nshprompt) - strlen(g_prompt_suffix) - 1 ;
+  strlcpy(_nshprompt[j], g_prompt_suffix, sizeof(g_nshprompt) - j);

Review Comment:
   I am unsure strlcat() can ensure that the suffix is always there? Current 
way is to truncate the prompt but not the suffix so that to make sure suffix is 
always there to separate the prompt from command inputs clearly.



-- 
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: commits-unsubscr...@nuttx.apache.org

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



Re: [PR] nshlib/prompt: add empty prompt string fallbacks [nuttx-apps]

2024-02-23 Thread via GitHub


yf13 commented on code in PR #2300:
URL: https://github.com/apache/nuttx-apps/pull/2300#discussion_r1501348259


##
nshlib/nsh_prompt.c:
##
@@ -0,0 +1,122 @@
+/
+ * apps/nshlib/nsh_prompt.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 
+
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#ifdef CONFIG_NSH_CLE
+#  include "system/cle.h"
+#else
+#  include "system/readline.h"
+#endif
+
+#include "nsh.h"
+#include "nsh_console.h"
+
+/
+ * Public Variables
+ /
+
+/* The NSH prompt */
+
+char g_nshprompt[NSH_PROMPT_MAX] = CONFIG_NSH_PROMPT_STRING;
+
+/
+ * Private Variables
+ /
+
+static const char g_prompt_suffix[]   = "> ";
+
+#ifndef CONFIG_DISABLE_ENVIRON
+static const char g_prompt_env[]  = CONFIG_NSH_PROMPT_ENV;
+#endif
+
+#if defined(CONFIG_NSH_READLINE) && defined(CONFIG_READLINE_TABCOMPLETION) && \
+defined(CONFIG_READLINE_HAVE_EXTMATCH)
+static const struct extmatch_vtable_s g_nsh_extmatch =
+{
+  nsh_extmatch_count,  /* count_matches */
+  nsh_extmatch_getname /* getname */
+};
+#endif
+
+/
+ * Public Functions
+ /
+
+/
+ * Name: nsh_refresh_prompt
+ *
+ * Description:
+ *   This function updates g_nshprompt with latest data based on prioritized
+ *   sources, from highest to lowest priority:
+ *
+ *   - non-empty NSH_PROMPT_STRING w/o appending suffix
+ *   - non-empty NSH_PROMPT_ENV variable value with suffix appended
+ *   - non-empty HOSTNAME with g_prompt_suffix appended
+ *   - g_prompt_suffix
+ *
+ * Results:
+ *   - updated g_nsh_prompt value if necessary.
+ *
+ /
+
+void nsh_update_prompt(void)
+{
+  if (strlen(CONFIG_NSH_PROMPT_STRING) == 0)
+{
+#ifndef CONFIG_DISABLE_ENVIRON
+  if (strlen(g_prompt_env) && getenv(g_prompt_env))
+{
+  strlcpy(g_nshprompt, getenv(g_prompt_env), sizeof(g_nshprompt));
+}
+  else
+#endif
+{
+  gethostname(g_nshprompt, sizeof(g_nshprompt));
+}
+
+  uint16_t j = strlen(g_nshprompt) + strlen(g_prompt_suffix) + 1 ;
+  j = (j <= sizeof(g_nshprompt)) ? strlen(g_nshprompt) :
+  sizeof(g_nshprompt) - strlen(g_prompt_suffix) - 1 ;
+  strlcpy(_nshprompt[j], g_prompt_suffix, sizeof(g_nshprompt) - j);

Review Comment:
   I am unsure strlcat() can ensure that the suffix is always there? Current 
way is to truncate the prompt to make sure "> " is always there, separating the 
prompt from command inputs clearly.



-- 
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: commits-unsubscr...@nuttx.apache.org

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



Re: [PR] nshlib/prompt: add empty prompt string fallbacks [nuttx-apps]

2024-02-23 Thread via GitHub


yf13 commented on code in PR #2300:
URL: https://github.com/apache/nuttx-apps/pull/2300#discussion_r1501355463


##
nshlib/nsh_prompt.c:
##
@@ -0,0 +1,122 @@
+/
+ * apps/nshlib/nsh_prompt.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 
+
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#ifdef CONFIG_NSH_CLE
+#  include "system/cle.h"
+#else
+#  include "system/readline.h"
+#endif
+
+#include "nsh.h"
+#include "nsh_console.h"
+
+/
+ * Public Variables
+ /
+
+/* The NSH prompt */
+
+char g_nshprompt[NSH_PROMPT_MAX] = CONFIG_NSH_PROMPT_STRING;
+
+/
+ * Private Variables
+ /
+
+static const char g_prompt_suffix[]   = "> ";

Review Comment:
   Added NSH_PROMPT_SUFFIX for it, someone may prefer "$ " to "> "



-- 
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: commits-unsubscr...@nuttx.apache.org

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



Re: [PR] nshlib/prompt: add empty prompt string fallbacks [nuttx-apps]

2024-02-23 Thread via GitHub


yf13 commented on code in PR #2300:
URL: https://github.com/apache/nuttx-apps/pull/2300#discussion_r1501352284


##
nshlib/nsh_prompt.c:
##
@@ -0,0 +1,122 @@
+/
+ * apps/nshlib/nsh_prompt.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 
+
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#ifdef CONFIG_NSH_CLE
+#  include "system/cle.h"
+#else
+#  include "system/readline.h"
+#endif
+
+#include "nsh.h"
+#include "nsh_console.h"
+
+/
+ * Public Variables
+ /
+
+/* The NSH prompt */
+
+char g_nshprompt[NSH_PROMPT_MAX] = CONFIG_NSH_PROMPT_STRING;
+
+/
+ * Private Variables
+ /
+
+static const char g_prompt_suffix[]   = "> ";
+
+#ifndef CONFIG_DISABLE_ENVIRON
+static const char g_prompt_env[]  = CONFIG_NSH_PROMPT_ENV;
+#endif
+
+#if defined(CONFIG_NSH_READLINE) && defined(CONFIG_READLINE_TABCOMPLETION) && \
+defined(CONFIG_READLINE_HAVE_EXTMATCH)
+static const struct extmatch_vtable_s g_nsh_extmatch =
+{
+  nsh_extmatch_count,  /* count_matches */
+  nsh_extmatch_getname /* getname */
+};
+#endif
+
+/
+ * Public Functions
+ /
+
+/
+ * Name: nsh_refresh_prompt
+ *
+ * Description:
+ *   This function updates g_nshprompt with latest data based on prioritized
+ *   sources, from highest to lowest priority:
+ *
+ *   - non-empty NSH_PROMPT_STRING w/o appending suffix
+ *   - non-empty NSH_PROMPT_ENV variable value with suffix appended
+ *   - non-empty HOSTNAME with g_prompt_suffix appended
+ *   - g_prompt_suffix
+ *
+ * Results:
+ *   - updated g_nsh_prompt value if necessary.
+ *
+ /
+
+void nsh_update_prompt(void)
+{
+  if (strlen(CONFIG_NSH_PROMPT_STRING) == 0)
+{
+#ifndef CONFIG_DISABLE_ENVIRON
+  if (strlen(g_prompt_env) && getenv(g_prompt_env))
+{
+  strlcpy(g_nshprompt, getenv(g_prompt_env), sizeof(g_nshprompt));
+}
+  else
+#endif
+{
+  gethostname(g_nshprompt, sizeof(g_nshprompt));
+}
+
+  uint16_t j = strlen(g_nshprompt) + strlen(g_prompt_suffix) + 1 ;
+  j = (j <= sizeof(g_nshprompt)) ? strlen(g_nshprompt) :
+  sizeof(g_nshprompt) - strlen(g_prompt_suffix) - 1 ;
+  strlcpy(_nshprompt[j], g_prompt_suffix, sizeof(g_nshprompt) - j);
+}
+
+#if defined(CONFIG_NSH_READLINE) && defined(CONFIG_READLINE_TABCOMPLETION)
+  /* Configure readline prompt */
+
+  readline_prompt(g_nshprompt);
+
+#  ifdef CONFIG_READLINE_HAVE_EXTMATCH
+  /* Set up for tab completion on NSH commands */
+
+  readline_extmatch(_nsh_extmatch);

Review Comment:
   okay we can do it only once, though it looks like a cheap function.



-- 
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: commits-unsubscr...@nuttx.apache.org

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



Re: [PR] nshlib/prompt: add empty prompt string fallbacks [nuttx-apps]

2024-02-23 Thread via GitHub


yf13 commented on code in PR #2300:
URL: https://github.com/apache/nuttx-apps/pull/2300#discussion_r1501348259


##
nshlib/nsh_prompt.c:
##
@@ -0,0 +1,122 @@
+/
+ * apps/nshlib/nsh_prompt.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 
+
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#ifdef CONFIG_NSH_CLE
+#  include "system/cle.h"
+#else
+#  include "system/readline.h"
+#endif
+
+#include "nsh.h"
+#include "nsh_console.h"
+
+/
+ * Public Variables
+ /
+
+/* The NSH prompt */
+
+char g_nshprompt[NSH_PROMPT_MAX] = CONFIG_NSH_PROMPT_STRING;
+
+/
+ * Private Variables
+ /
+
+static const char g_prompt_suffix[]   = "> ";
+
+#ifndef CONFIG_DISABLE_ENVIRON
+static const char g_prompt_env[]  = CONFIG_NSH_PROMPT_ENV;
+#endif
+
+#if defined(CONFIG_NSH_READLINE) && defined(CONFIG_READLINE_TABCOMPLETION) && \
+defined(CONFIG_READLINE_HAVE_EXTMATCH)
+static const struct extmatch_vtable_s g_nsh_extmatch =
+{
+  nsh_extmatch_count,  /* count_matches */
+  nsh_extmatch_getname /* getname */
+};
+#endif
+
+/
+ * Public Functions
+ /
+
+/
+ * Name: nsh_refresh_prompt
+ *
+ * Description:
+ *   This function updates g_nshprompt with latest data based on prioritized
+ *   sources, from highest to lowest priority:
+ *
+ *   - non-empty NSH_PROMPT_STRING w/o appending suffix
+ *   - non-empty NSH_PROMPT_ENV variable value with suffix appended
+ *   - non-empty HOSTNAME with g_prompt_suffix appended
+ *   - g_prompt_suffix
+ *
+ * Results:
+ *   - updated g_nsh_prompt value if necessary.
+ *
+ /
+
+void nsh_update_prompt(void)
+{
+  if (strlen(CONFIG_NSH_PROMPT_STRING) == 0)
+{
+#ifndef CONFIG_DISABLE_ENVIRON
+  if (strlen(g_prompt_env) && getenv(g_prompt_env))
+{
+  strlcpy(g_nshprompt, getenv(g_prompt_env), sizeof(g_nshprompt));
+}
+  else
+#endif
+{
+  gethostname(g_nshprompt, sizeof(g_nshprompt));
+}
+
+  uint16_t j = strlen(g_nshprompt) + strlen(g_prompt_suffix) + 1 ;
+  j = (j <= sizeof(g_nshprompt)) ? strlen(g_nshprompt) :
+  sizeof(g_nshprompt) - strlen(g_prompt_suffix) - 1 ;
+  strlcpy(_nshprompt[j], g_prompt_suffix, sizeof(g_nshprompt) - j);

Review Comment:
   I am unsure strlcat() can ensure that the suffix is always there? Current 
way is to truncate the prompt to make sure "> " is always there, separating 
newly entered chars from the prompt. 



-- 
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: commits-unsubscr...@nuttx.apache.org

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



Re: [PR] nshlib/prompt: add empty prompt string fallbacks [nuttx-apps]

2024-02-23 Thread via GitHub


yf13 commented on code in PR #2300:
URL: https://github.com/apache/nuttx-apps/pull/2300#discussion_r1501347095


##
nshlib/nsh.h:
##
@@ -748,7 +750,7 @@ extern const char g_loginsuccess[];
 extern const char g_badcredentials[];
 extern const char g_loginfailure[];
 #endif
-extern const char g_nshprompt[];
+extern char g_nshprompt[NSH_PROMPT_MAX];

Review Comment:
   Okay, we need define "char *nshprompt()" so that nsh_session.c can get 
access to the prompt string.



-- 
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: commits-unsubscr...@nuttx.apache.org

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



Re: [PR] nshlib/prompt: add empty prompt string fallbacks [nuttx-apps]

2024-02-23 Thread via GitHub


xiaoxiang781216 commented on code in PR #2300:
URL: https://github.com/apache/nuttx-apps/pull/2300#discussion_r1501337145


##
nshlib/Kconfig:
##
@@ -66,7 +66,21 @@ config NSH_PROMPT_STRING
string "Prompt String"
default "nsh> "
---help---
-   Provide the shell prompt string, default is "nsh> ".
+   Provide the shell prompt string with size limit NSH_PROMPT_MAX.
+   default is "nsh> ".
+
+config NSH_PROMPT_MAX
+   int "Maximum Size of Prompt String"
+   default 16
+   ---help---
+   The maximum size oof shell prompt string.

Review Comment:
   ```suggestion
The maximum size of shell prompt string.
   ```



##
nshlib/Kconfig:
##
@@ -66,7 +66,21 @@ config NSH_PROMPT_STRING
string "Prompt String"
default "nsh> "
---help---
-   Provide the shell prompt string, default is "nsh> ".
+   Provide the shell prompt string with size limit NSH_PROMPT_MAX.
+   default is "nsh> ".
+
+config NSH_PROMPT_MAX
+   int "Maximum Size of Prompt String"
+   default 16
+   ---help---
+   The maximum size oof shell prompt string.
+
+config NSH_PROMPT_ENV
+   string "Prompt String Environment Variable"

Review Comment:
   add:
   ```
   depends on !DISABLE_ENVIRON
   ```



##
nshlib/nsh.h:
##
@@ -748,7 +750,7 @@ extern const char g_loginsuccess[];
 extern const char g_badcredentials[];
 extern const char g_loginfailure[];
 #endif
-extern const char g_nshprompt[];
+extern char g_nshprompt[NSH_PROMPT_MAX];

Review Comment:
   remove, let's change g_nshprompt to static variable



##
nshlib/nsh_prompt.c:
##
@@ -0,0 +1,122 @@
+/
+ * apps/nshlib/nsh_prompt.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 
+
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#ifdef CONFIG_NSH_CLE
+#  include "system/cle.h"
+#else
+#  include "system/readline.h"
+#endif
+
+#include "nsh.h"
+#include "nsh_console.h"
+
+/
+ * Public Variables
+ /
+
+/* The NSH prompt */
+
+char g_nshprompt[NSH_PROMPT_MAX] = CONFIG_NSH_PROMPT_STRING;

Review Comment:
   change to static



##
nshlib/nsh.h:
##
@@ -574,6 +574,8 @@
 #  define CONFIG_NSH_DISABLE_UNSET 1
 #endif
 
+#define NSH_PROMPT_MAXCONFIG_NSH_PROMPT_MAX

Review Comment:
   let's use CONFIG_NSH_PROMPT_MAX directly?



##
nshlib/nsh_prompt.c:
##
@@ -0,0 +1,122 @@
+/
+ * apps/nshlib/nsh_prompt.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 
+
+#include 
+#include