31908b3128
Currently NSH prompt is defined at build time, thus improper for AMP cases where the same NSH binary is used on different nodes as the same NSH prompt shows on all nodes. This patch attempts to support runtime prompt string population from ordered sources: - the environment variable defined by NSH_PROMPT_ENV plus suffix - the NSH_PROMPT_STRING - the HOSTNAME plus suffix The suffix is defined by NSH_PROMPT_SUFFIX so that to clearly separate the command inputs. Changes in `nshlib/` - Kconfig: add configs NSH_PROMPT_MAX/ENV/SUFFIX etc - nsh.h: adjust g_nshprompt defs, add nsh_update_prompt - nsh_parse.c relocate g_nshpromt to nsh_prompt.c - nsh_init.c revise to use nsh_update_prompt once - nsh_session.c revise to use methods in nsh_prompt.c - Makefile add nsh_prompt.c - CMakeLists.txt add nsh_prompt.c New additions in `nshlib/` - nsh_prompt.c prompt related data structures and methods. Signed-off-by: Yanfeng Liu <yfliu2008@qq.com>
106 lines
3.6 KiB
C
106 lines
3.6 KiB
C
/****************************************************************************
|
|
* 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 <nuttx/config.h>
|
|
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include <assert.h>
|
|
|
|
#include "nsh.h"
|
|
|
|
/****************************************************************************
|
|
* Preprocessor Macros
|
|
****************************************************************************/
|
|
|
|
#define NSH_PROMPT_SIZE (CONFIG_NSH_PROMPT_MAX + 1 - \
|
|
sizeof(CONFIG_NSH_PROMPT_SUFFIX))
|
|
|
|
/****************************************************************************
|
|
* Private Variables
|
|
****************************************************************************/
|
|
|
|
static char g_nshprompt[CONFIG_NSH_PROMPT_MAX] = CONFIG_NSH_PROMPT_STRING;
|
|
|
|
/****************************************************************************
|
|
* Public Functions
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
|
* Name: nsh_update_prompt
|
|
*
|
|
* Description:
|
|
* This updates g_nshprompt from multiple sources, in the following order:
|
|
*
|
|
* - non-empty NSH_PROMPT_ENV variable and suffix
|
|
* - non-empty NSH_PROMPT_STRING
|
|
* - non-empty HOSTNAME and suffix
|
|
*
|
|
* Note that suffix has higher priority when used to help clearly separate
|
|
* prompts from command line inputs.
|
|
*
|
|
* Results:
|
|
* - updated g_nsh_prompt value.
|
|
*
|
|
****************************************************************************/
|
|
|
|
void nsh_update_prompt(void)
|
|
{
|
|
static_assert(CONFIG_NSH_PROMPT_MAX > sizeof(CONFIG_NSH_PROMPT_STRING),
|
|
"NSH_PROMPT_STRING too long!");
|
|
static_assert(CONFIG_NSH_PROMPT_MAX > sizeof(CONFIG_NSH_PROMPT_SUFFIX),
|
|
"NSH_PROMPT_SUFFIX too long!");
|
|
#ifndef CONFIG_DISABLE_ENVIRON
|
|
if (getenv(CONFIG_NSH_PROMPT_ENV))
|
|
{
|
|
strlcpy(g_nshprompt, getenv(CONFIG_NSH_PROMPT_ENV), NSH_PROMPT_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, NSH_PROMPT_SIZE);
|
|
strcat(g_nshprompt, CONFIG_NSH_PROMPT_SUFFIX);
|
|
}
|
|
}
|
|
|
|
/****************************************************************************
|
|
* Name: nsh_prompt
|
|
*
|
|
* Description:
|
|
* This function returns latest prompt string.
|
|
* It is needed as g_nshprompt is no longer public.
|
|
*
|
|
****************************************************************************/
|
|
|
|
FAR const char *nsh_prompt(void)
|
|
{
|
|
return g_nshprompt;
|
|
}
|