Commit 83fa2d13 authored by Sumit Garg's avatar Sumit Garg Committed by Daniel Thompson

kdb: Refactor env variables get/set code

Add two new kdb environment access methods as kdb_setenv() and
kdb_printenv() in order to abstract out environment access code
from kdb command functions.

Also, replace (char *)0 with NULL as an initializer for environment
variables array.
Signed-off-by: default avatarSumit Garg <sumit.garg@linaro.org>
Reviewed-by: default avatarDouglas Anderson <dianders@chromium.org>
Link: https://lore.kernel.org/r/1612771342-16883-1-git-send-email-sumit.garg@linaro.org
[daniel.thompson@linaro.org: Replaced (char *)0/NULL initializers with
an array size]
Signed-off-by: default avatarDaniel Thompson <daniel.thompson@linaro.org>
parent 2bbd9b0f
...@@ -140,42 +140,18 @@ static const int __nkdb_err = ARRAY_SIZE(kdbmsgs); ...@@ -140,42 +140,18 @@ static const int __nkdb_err = ARRAY_SIZE(kdbmsgs);
* KDB_ENVBUFSIZE if required). * KDB_ENVBUFSIZE if required).
*/ */
static char *__env[] = { static char *__env[31] = {
#if defined(CONFIG_SMP) #if defined(CONFIG_SMP)
"PROMPT=[%d]kdb> ", "PROMPT=[%d]kdb> ",
#else #else
"PROMPT=kdb> ", "PROMPT=kdb> ",
#endif #endif
"MOREPROMPT=more> ", "MOREPROMPT=more> ",
"RADIX=16", "RADIX=16",
"MDCOUNT=8", /* lines of md output */ "MDCOUNT=8", /* lines of md output */
KDB_PLATFORM_ENV, KDB_PLATFORM_ENV,
"DTABCOUNT=30", "DTABCOUNT=30",
"NOSECT=1", "NOSECT=1",
(char *)0,
(char *)0,
(char *)0,
(char *)0,
(char *)0,
(char *)0,
(char *)0,
(char *)0,
(char *)0,
(char *)0,
(char *)0,
(char *)0,
(char *)0,
(char *)0,
(char *)0,
(char *)0,
(char *)0,
(char *)0,
(char *)0,
(char *)0,
(char *)0,
(char *)0,
(char *)0,
(char *)0,
}; };
static const int __nenv = ARRAY_SIZE(__env); static const int __nenv = ARRAY_SIZE(__env);
...@@ -317,6 +293,63 @@ int kdbgetintenv(const char *match, int *value) ...@@ -317,6 +293,63 @@ int kdbgetintenv(const char *match, int *value)
return diag; return diag;
} }
/*
* kdb_setenv() - Alter an existing environment variable or create a new one.
* @var: Name of the variable
* @val: Value of the variable
*
* Return: Zero on success, a kdb diagnostic on failure.
*/
static int kdb_setenv(const char *var, const char *val)
{
int i;
char *ep;
size_t varlen, vallen;
varlen = strlen(var);
vallen = strlen(val);
ep = kdballocenv(varlen + vallen + 2);
if (ep == (char *)0)
return KDB_ENVBUFFULL;
sprintf(ep, "%s=%s", var, val);
for (i = 0; i < __nenv; i++) {
if (__env[i]
&& ((strncmp(__env[i], var, varlen) == 0)
&& ((__env[i][varlen] == '\0')
|| (__env[i][varlen] == '=')))) {
__env[i] = ep;
return 0;
}
}
/*
* Wasn't existing variable. Fit into slot.
*/
for (i = 0; i < __nenv-1; i++) {
if (__env[i] == (char *)0) {
__env[i] = ep;
return 0;
}
}
return KDB_ENVFULL;
}
/*
* kdb_printenv() - Display the current environment variables.
*/
static void kdb_printenv(void)
{
int i;
for (i = 0; i < __nenv; i++) {
if (__env[i])
kdb_printf("%s\n", __env[i]);
}
}
/* /*
* kdbgetularg - This function will convert a numeric string into an * kdbgetularg - This function will convert a numeric string into an
* unsigned long value. * unsigned long value.
...@@ -374,10 +407,6 @@ int kdbgetu64arg(const char *arg, u64 *value) ...@@ -374,10 +407,6 @@ int kdbgetu64arg(const char *arg, u64 *value)
*/ */
int kdb_set(int argc, const char **argv) int kdb_set(int argc, const char **argv)
{ {
int i;
char *ep;
size_t varlen, vallen;
/* /*
* we can be invoked two ways: * we can be invoked two ways:
* set var=value argv[1]="var", argv[2]="value" * set var=value argv[1]="var", argv[2]="value"
...@@ -422,37 +451,7 @@ int kdb_set(int argc, const char **argv) ...@@ -422,37 +451,7 @@ int kdb_set(int argc, const char **argv)
* Tokenizer squashed the '=' sign. argv[1] is variable * Tokenizer squashed the '=' sign. argv[1] is variable
* name, argv[2] = value. * name, argv[2] = value.
*/ */
varlen = strlen(argv[1]); return kdb_setenv(argv[1], argv[2]);
vallen = strlen(argv[2]);
ep = kdballocenv(varlen + vallen + 2);
if (ep == (char *)0)
return KDB_ENVBUFFULL;
sprintf(ep, "%s=%s", argv[1], argv[2]);
ep[varlen+vallen+1] = '\0';
for (i = 0; i < __nenv; i++) {
if (__env[i]
&& ((strncmp(__env[i], argv[1], varlen) == 0)
&& ((__env[i][varlen] == '\0')
|| (__env[i][varlen] == '=')))) {
__env[i] = ep;
return 0;
}
}
/*
* Wasn't existing variable. Fit into slot.
*/
for (i = 0; i < __nenv-1; i++) {
if (__env[i] == (char *)0) {
__env[i] = ep;
return 0;
}
}
return KDB_ENVFULL;
} }
static int kdb_check_regs(void) static int kdb_check_regs(void)
...@@ -2055,12 +2054,7 @@ static int kdb_lsmod(int argc, const char **argv) ...@@ -2055,12 +2054,7 @@ static int kdb_lsmod(int argc, const char **argv)
static int kdb_env(int argc, const char **argv) static int kdb_env(int argc, const char **argv)
{ {
int i; kdb_printenv();
for (i = 0; i < __nenv; i++) {
if (__env[i])
kdb_printf("%s\n", __env[i]);
}
if (KDB_DEBUG(MASK)) if (KDB_DEBUG(MASK))
kdb_printf("KDBDEBUG=0x%x\n", kdb_printf("KDBDEBUG=0x%x\n",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment