SCP: Provide a way to gather input from a user into an environment variable

SET ENV -P "Prompt String" EnvVar=Default
This commit is contained in:
Mark Pizzolato 2017-04-30 09:44:37 -07:00
parent 7a46fcf17c
commit 7ae5015610
3 changed files with 91 additions and 49 deletions

View file

@ -261,7 +261,8 @@ The EXPECT command now exists to provide a means of reacting to simulator output
#### New SCP Commands: #### New SCP Commands:
SCREENSHOT filename.bmp Save video window to the specified file SCREENSHOT filename.bmp Save video window to the specified file
SET ENVIRONMENT Name=Value Set Environment variable SET ENV Name=Value Set Environment variable
SET ENV -p "Prompt" Name=Default Gather User input into an Environment Variable
SET ASYNCH Enable Asynchronous I/O SET ASYNCH Enable Asynchronous I/O
SET NOASYNCH Disable Asynchronous I/O SET NOASYNCH Disable Asynchronous I/O
SET VERIFY Enable commang display while processing DO command files SET VERIFY Enable commang display while processing DO command files

Binary file not shown.

47
scp.c
View file

@ -1161,10 +1161,19 @@ static const char simh_help[] =
"3Asynch\n" "3Asynch\n"
"+set asynch enable asynchronous I/O\n" "+set asynch enable asynchronous I/O\n"
"+set noasynch disable asynchronous I/O\n" "+set noasynch disable asynchronous I/O\n"
#define HLP_SET_ENVIRON "*Commands SET Asynch" #define HLP_SET_ENVIRON "*Commands SET Environment"
"3Environment\n" "3Environment\n"
"4Explicitily Changing A Variable\n"
"+set environment name=val set environment variable\n" "+set environment name=val set environment variable\n"
"+set environment name clear environment variable\n" "+set environment name clear environment variable\n"
"4Gathering Input From A User\n"
" Input from a user can be obtained by:\n\n"
"+set environment -p \"Prompt String\" name=default\n\n"
" The -p switch indicates that the user should be prompted\n"
" with the indicated prompt string and the input provided\n"
" will be saved in the environment variable 'name'. If no\n"
" input is provided, the value specified as 'default' will be\n"
" used.\n"
#define HLP_SET_ON "*Commands SET Command_Status_Trap_Dispatching" #define HLP_SET_ON "*Commands SET Command_Status_Trap_Dispatching"
"3Command Status Trap Dispatching\n" "3Command Status Trap Dispatching\n"
"+set on enables error checking after command\n" "+set on enables error checking after command\n"
@ -3996,11 +4005,43 @@ return SCPE_OK;
t_stat sim_set_environment (int32 flag, CONST char *cptr) t_stat sim_set_environment (int32 flag, CONST char *cptr)
{ {
char varname[CBUFSIZE]; char varname[CBUFSIZE], prompt[CBUFSIZE];
if ((!cptr) || (*cptr == 0)) /* now eol? */ if ((!cptr) || (*cptr == 0)) /* now eol? */
return SCPE_2FARG; return SCPE_2FARG;
cptr = get_glyph (cptr, varname, '='); /* get environment variable name */ if (sim_switches & SWMASK ('P')) {
char cbuf[CBUFSIZE];
CONST char *deflt = NULL;
cptr = get_glyph_quoted (cptr, prompt, 0); /* get prompt */
if (prompt[0] == '\0')
return sim_messagef (SCPE_2FARG, "Missing Prompt and Environment Variable Name\n");
if ((prompt[0] == '"') || (prompt[0] == '\'')) {
prompt[strlen (prompt) - 1] = '\0';
memmove (prompt, prompt + 1, strlen (prompt));
}
deflt = get_glyph (cptr, varname, '='); /* get environment variable name */
if (deflt == NULL)
deflt = "";
if (*deflt) {
strlcat (prompt, " [", sizeof (prompt));
strlcat (prompt, deflt, sizeof (prompt));
strlcat (prompt, "] ", sizeof (prompt));
}
else
strlcat (prompt, " ", sizeof (prompt));
if (sim_rem_cmd_active_line == -1) {
cptr = read_line_p (prompt, cbuf, sizeof(cbuf), stdin);
if ((cptr == NULL) || (*cptr == 0))
cptr = deflt;
else
cptr = cbuf;
}
else
cptr = deflt;
}
else
cptr = get_glyph (cptr, varname, '='); /* get environment variable name */
setenv(varname, cptr, 1); setenv(varname, cptr, 1);
return SCPE_OK; return SCPE_OK;
} }