📄 env.c
字号:
{
struct s_shell *sp;
sp = shell_vars;
while(1) {
if (sp->name == (char *)0) {
if (sp->next == (struct s_shell *)0)
return(-1);
else {
sp = sp->next;
continue;
}
}
if (strcmp(name,sp->name) == 0) {
env_free(sp->name);
env_free(sp->val);
sp->name = (char *)0;
sp->val = (char *)0;
return(0);
}
if (sp->next == (struct s_shell *)0)
return(-1);
else
sp = sp->next;
}
}
/* ConsoleBaudEnvSet():
* Called by to load/reload the CONSOLEBAUD shell variable based on
* the content of the global variable 'ConsoleBaudRate'.
*/
void
ConsoleBaudEnvSet(void)
{
char buf[16];
sprintf(buf,"%d",ConsoleBaudRate);
setenv("CONSOLEBAUD",buf);
}
/* ChangeConsoleBaudrate():
* Called to attempt to adjust the console baudrate.
*/
int
ChangeConsoleBaudrate(int baud)
{
if (ConsoleBaudSet(baud) < 0) {
printf("Baud=%d failed\n",baud);
return(-1);
}
ConsoleBaudRate = baud;
ConsoleBaudEnvSet();
return(0);
}
/* ShellVarInit();
* Setup the shell_vars pointer appropriately for additional
* shell variable assignments that will be made through shell_alloc().
*/
int
ShellVarInit()
{
char buf[16];
#if !INCLUDE_MALLOC
memset((char *)&envSpace,0,sizeof(envSpace));
#endif
shell_vars = (struct s_shell *)env_alloc(sizeof(struct s_shell));
if (!shell_vars) {
printf("No memory for environment initialization\n");
return(-1);
}
shell_vars->next = (struct s_shell *)0;
shell_vars->name = (char *)0;
setenv("PROMPT",PROMPT);
sprintf(buf,"0x%lx",APPLICATION_RAMSTART);
setenv("APPRAMBASE",buf);
sprintf(buf,"0x%lx",BOOTROM_BASE);
setenv("BOOTROMBASE",buf);
setenv("PLATFORM",PLATFORM_NAME);
setenv("MONITORBUILT",monBuilt());
shell_sprintf("MONCOMPTR","0x%lx",(ulong)&moncomptr);
/* Support the ability to have additional target-specific
* shell variables initialized at startup...
*/
#ifdef TARGET_ENV_SETUP
TARGET_ENV_SETUP();
#endif
shell_sprintf("VERSION_MAJ","%d",MAJOR_VERSION);
shell_sprintf("VERSION_MIN","%d",MINOR_VERSION);
shell_sprintf("VERSION_TGT","%d",TARGET_VERSION);
return(0);
}
/* getenv:
* Return the pointer to the value entry if the shell variable
* name is currently set; otherwise, return a null pointer.
*/
char *
getenv(char *name)
{
// printf("<getenv> %s \n",name);
register struct s_shell *sp;
for(sp = shell_vars;sp != (struct s_shell *)0;sp = sp->next) {
if (sp->name != (char *)0) {
if (strcmp(sp->name,name) == 0)
return(sp->val);
}
}
return((char *)0);
}
/* getenvp:
* Build an environment string consisting of all shell variables and
* their values concatenated into one string. The format is
*
* NAME=VALUE LF NAME=VALUE LF NAME=VALUE LF NULL
*
* with the limit in size being driven only by the space
* available on the heap. Note that this uses malloc, and it
* the responsibility of the caller to free the pointer when done.
*/
char *
getenvp(void)
{
int size;
char *envp, *cp;
register struct s_shell *sp;
size = 0;
/* Get total size of the current environment vars */
for(sp = shell_vars;sp != (struct s_shell *)0;sp = sp->next) {
if (sp->name != (char *)0) {
size += (strlen(sp->name) + strlen(sp->val) + 2);
}
}
if (size == 0)
return((char *)0);
envp = env_alloc(size+1); /* leave room for final NULL */
if (envp == 0)
return((char *)0);
cp = envp;
for(sp = shell_vars;sp != (struct s_shell *)0;sp = sp->next) {
if (sp->name != (char *)0)
cp += sprintf(cp,"%s=%s\n",sp->name,sp->val);
}
*cp = 0; /* Append NULL after final separator */
return(envp);
}
/* clearenv():
* Clear out the entire environment.
*/
void
clearenv(void)
{
struct s_shell *sp;
for(sp = shell_vars;sp != (struct s_shell *)0;sp = sp->next) {
if (sp->name != (char *)0) {
env_free(sp->name);
env_free(sp->val);
sp->name = (char *)0;
sp->val = (char *)0;
}
}
}
/* setenv:
* Interface to shell_dealloc() and shell_alloc().
*/
int
setenv(char *name,char *value)
{
if (!shell_vars)
return(-1);
if ((value == (char *)0) || (*value == 0))
return(shell_dealloc(name));
else
return(shell_alloc(name,value));
}
/* shell_print():
* Print out all of the current shell variables and their values.
*/
int
shell_print(void)
{
int maxlen, len;
char format[8];
register struct s_shell *sp;
/* Before printing the list, pass through the list to determine the
* largest variable name. This is used to create a format string
* that is then passed to printf() when printing the list of
* name/value pairs. It guarantees that regardless of the length
* of the name, the format of the printed out put will be consistent
* for all variables.
*/
maxlen = 0;
sp = shell_vars;
while(1) {
if (sp->name) {
len = strlen(sp->name);
if (len > maxlen)
maxlen = len;
}
if (sp->next != (struct s_shell *)0)
sp = sp->next;
else
break;
}
sprintf(format,"%%%ds = ",maxlen+1);
/* Now that we know the size of the largest variable, we can
* print the list cleanly...
*/
sp = shell_vars;
while(1) {
if (sp->name != (char *)0) {
printf(format, sp->name);
puts(sp->val); /* sp->val may overflow printf, so use puts */
putchar('\n');
}
if (sp->next != (struct s_shell *)0)
sp = sp->next;
else
break;
}
return(0);
}
/* shell_sprintf():
* Simple way to turn a printf-like formatted string into a shell variable.
*/
int
shell_sprintf(char *varname, char *fmt, ...)
{
int tot;
char buf[CMDLINESIZE];
va_list argp;
va_start(argp,fmt);
tot = vsnprintf(buf,CMDLINESIZE-1,fmt,argp);
va_end(argp);
setenv(varname,buf);
return(tot);
}
#if INCLUDE_TFS
/* validEnvToExecVar():
* Return 1 if the variable should be included in the script
* generated by envToExec(); else return 0.
* Specifically... if the variable is generated internally
* then we don't want to include it in the script.
*/
int
validEnvToExecVar(char *varname)
{
int i;
static char *invalid_varnames[] = {
"APPRAMBASE", "BOOTROMBASE", "CMDSTAT",
"CONSOLEBAUD", "MALLOC", "MONCOMPTR",
"MONITORBUILT", "PLATFORM", "PROMPT",
"VERSION_MAJ", "VERSION_MIN", "VERSION_TGT",
0
};
if (varname == 0)
return(0);
if (strncmp(varname,"ARG",3) == 0)
return(0);
#if INCLUDE_BOARDINFO
if (BoardInfoVar(varname))
return(0);
#endif
for(i=0;invalid_varnames[i];i++) {
if (!strcmp(varname,invalid_varnames[i]))
return(0);
}
return(1);
}
/* envToExec():
Create a file of "set" commands that can be run to recreate the
current environment.
*/
int
envToExec(char *filename)
{
int err, vartot;
char *buf, *bp, *cp;
register struct s_shell *sp;
buf = bp = (char *)getAppRamStart();
sp = shell_vars;
vartot = 0;
while(1) {
if (validEnvToExecVar(sp->name)) {
bp += sprintf(bp,"set %s \"",sp->name);
cp = sp->val;
while(*cp) {
if (*cp == '$')
*bp++ = '\\';
*bp++ = *cp++;
}
*bp++ = '\"';
*bp++ = '\n';
*bp = 0;
vartot++;
}
if (sp->next != (struct s_shell *)0)
sp = sp->next;
else
break;
}
if (vartot > 0) {
err = tfsadd(filename,"envsetup","e",buf,strlen(buf));
if (err != TFS_OKAY) {
printf("%s: %s\n",filename,(char *)tfsctrl(TFS_ERRMSG,err,0));
return(-1);
}
}
return(0);
}
#endif
#else
int
setenv(char *name,char *value)
{
return(-1);
}
char *
getenv(char *name)
{
return(0);
}
int
shell_sprintf(char *varname, char *fmt, ...)
{
return(0);
}
void
ConsoleBaudEnvSet(void)
{
}
char *
getenvp(void)
{
return(0);
}
int
ChangeConsoleBaudrate(int baud)
{
return(-1);
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -