* apps/nshlib/nsh_vars.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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include "nsh.h"
#include "nsh_console.h"
#ifdef CONFIG_NSH_VARS
* Private Functions
****************************************************************************/
* Name: nsh_cmpname
****************************************************************************/
static bool nsh_cmpname(FAR const char *pszname, FAR const char *peqname)
{
for (; *pszname == *peqname; pszname++, peqname++)
{
}
if (*pszname == '\0' && *peqname == '=')
{
return true;
}
return false;
}
* Public Functions
****************************************************************************/
* Name: nsh_findvar
*
* Description:
* Search the provided NSH console structure for the variable of the
* specified name.
*
* Input Parameters:
* pstate - The console state containing NSH variable array to be searched.
* name - The variable name to find
*
* Returned Value:
* A pointer to the name=value string in the NSH variable buffer
*
* Assumptions:
* - Not called from an interrupt handler
* - Pre-emption is disabled by caller
*
****************************************************************************/
FAR char *nsh_findvar(FAR struct console_stdio_s *pstate,
FAR const char *name)
{
FAR char *ptr;
FAR char *end;
DEBUGASSERT(pstate != NULL && name != NULL);
end = &pstate->varp[pstate->varsz];
for (ptr = pstate->varp;
ptr < end && !nsh_cmpname(name, ptr);
ptr += (strlen(ptr) + 1));
return (ptr < end) ? ptr : NULL;
}
* Name: nsh_removevar
*
* Description:
* Remove the referenced name=value pair from the NSH variable buffer
*
* Input Parameters:
* pstate - The task pstate with the NSH variable buffer containing the
* name=value pair
* pair - A pointer to the name=value pair in the restroom
*
* Returned Value:
* Zero on success
*
****************************************************************************/
int nsh_removevar(FAR struct console_stdio_s *pstate, FAR char *pair)
{
FAR char *end;
int alloc;
int ret = ERROR;
alloc = pstate->varsz;
end = &pstate->varp[alloc];
if (pair >= pstate->varp && pair < end)
{
int len = strlen(pair) + 1;
FAR char *src = &pair[len];
FAR char *dest = pair;
int count = end - src;
* this is inefficient, but probably not high duty.
*/
while (count-- > 0)
{
*dest++ = *src++;
}
* call realloc at some point but we don't do that here because the
* caller may add more stuff to the NSH variable buffer.
*/
pstate->varsz -= len;
ret = OK;
}
return ret;
}
* Public Functions
****************************************************************************/
* Name: nsh_getvar, nsh_setvar, and nsh_setvar
*
* Description:
* Get, set, or unset an NSH variable.
*
* Input Parameters:
* vtbl - NSH session data
* name - The name of the variable to get or set
* value - The value to use with nsh_setvar()
*
* Returned value:
* nsh_getvar() returns a read-only reference to the variable value on
* success or NULL on failure.
* nset_unsetvar() returns OK on success or an negated errno value on
* failure.
*
****************************************************************************/
* Name: nsh_getvar
****************************************************************************/
FAR char *nsh_getvar(FAR struct nsh_vtbl_s *vtbl, FAR const char *name)
{
FAR struct console_stdio_s *pstate = (FAR struct console_stdio_s *)vtbl;
FAR char *pair;
FAR char *value = NULL;
DEBUGASSERT(pstate != NULL && name != NULL);
if ((pair = nsh_findvar(pstate, name)) == NULL)
{
return NULL;
}
value = strchr(pair, '=');
DEBUGASSERT(value != NULL);
value++;
return value;
}
* Name: nsh_setvar
****************************************************************************/
#ifndef CONFIG_NSH_DISABLE_SET
int nsh_setvar(FAR struct nsh_vtbl_s *vtbl, FAR const char *name,
FAR const char *value)
{
FAR struct console_stdio_s *pstate = (FAR struct console_stdio_s *)vtbl;
FAR char *pair;
FAR char *newvarp;
int newsize;
int varlen;
DEBUGASSERT(pstate != NULL && name != NULL && value != NULL);
if (pstate->varp != NULL &&
(pair = nsh_findvar(pstate, name)) != NULL)
{
* will be added again below.
*/
nsh_removevar(pstate, pair);
}
* for null terminator
*/
varlen = strlen(name) + strlen(value) + 2;
if (pstate->varp != NULL)
{
newsize = pstate->varsz + varlen;
newvarp = (FAR char *)realloc(pstate->varp, newsize);
if (newvarp == NULL)
{
return -ENOMEM;
}
pair = &newvarp[pstate->varsz];
}
else
{
newsize = varlen;
newvarp = (FAR char *)malloc(varlen);
if (!newvarp)
{
return -ENOMEM;
}
pair = newvarp;
}
pstate->varp = newvarp;
pstate->varsz = newsize;
snprintf(pair, varlen, "%s=%s", name, value);
return OK;
}
#endif
* Name: nsh_unsetvar
****************************************************************************/
#if !defined(CONFIG_NSH_DISABLE_UNSET) || !defined(CONFIG_NSH_DISABLE_EXPORT)
int nsh_unsetvar(FAR struct nsh_vtbl_s *vtbl, FAR const char *name)
{
FAR struct console_stdio_s *pstate = (FAR struct console_stdio_s *)vtbl;
FAR char *pair;
FAR char *newvarp;
int newsize;
int ret = OK;
DEBUGASSERT(name != NULL && pstate != NULL);
if (pstate != NULL && (pair = nsh_findvar(pstate, name)) != NULL)
{
nsh_removevar(pstate, pair);
newsize = pstate->varsz;
if (newsize <= 0)
{
if (pstate->varp != NULL)
{
free(pstate->varp);
pstate->varp = NULL;
}
pstate->varsz = 0;
}
else
{
newvarp = (FAR char *)realloc(pstate->varp, newsize);
if (newvarp == NULL)
{
return -ENOMEM;
}
else
{
* to reallocation).
*/
pstate->varp = newvarp;
}
}
}
return ret;
}
#endif
* Name: nsh_foreach_var
*
* Description:
* Visit each name-value pair in the environment.
*
* Input Parameters:
* vtbl - NSH session data
* cb - The callback function to be invoked for each environment
* variable.
*
* Returned Value:
* Zero if the all NSH variables have been traversed. A non-zero value
* means that the callback function requested early termination by
* returning a nonzero value.
*
****************************************************************************/
#ifndef CONFIG_NSH_DISABLE_SET
int nsh_foreach_var(FAR struct nsh_vtbl_s *vtbl, nsh_foreach_var_t cb,
FAR void *arg)
{
FAR struct console_stdio_s *pstate = (FAR struct console_stdio_s *)vtbl;
FAR char *ptr;
FAR char *end;
int ret = OK;
DEBUGASSERT(pstate != NULL && cb != NULL);
end = &pstate->varp[pstate->varsz];
for (ptr = pstate->varp; ptr < end; ptr += (strlen(ptr) + 1))
{
ret = cb(vtbl, arg, ptr);
* returning a non-zero value.
*/
if (ret != 0)
{
break;
}
}
return ret;
}
#endif
#endif