Scripting: added strrep command

This commit is contained in:
d0k3 2017-12-31 02:09:43 +01:00
parent 58046d0a20
commit 918482ef91
2 changed files with 34 additions and 0 deletions

View File

@ -85,6 +85,23 @@ set USERINPUT "Hello World"
input "Enter something please?" USERINPUT
echo "You entered:\n$[USERINPUT]"
# 'strsplit' COMMAND
# The 'strsplit' command extracts a substring from a string
# -b / --before extracts the substring *before* the split char
# -f / --first matches the first, not last occurence of the split char
strsplit TESTSPLIT1 "one/two/three" "/"
strsplit -b TESTSPLIT2 "one/two/three" "/"
strsplit -f TESTSPLIT3 "one/two/three" "/"
strsplit -b -f TESTSPLIT4 "one/two/three" "/"
echo "one/two/three\n$[TESTSPLIT1]\n$[TESTSPLIT2]\n$[TESTSPLIT3]\n$[TESTSPLIT4]"
# strrep COMMAND
# The 'strrep' command replaces one char with another inside a string
# notice how we use TESTREP both as input and output
set TESTREP "Hello_World"
strrep TESTREP $[TESTREP] "_ "
echo $[TESTREP]
# 'filesel' COMMAND
# The 'filesel' command allows the user to choose a file inside a directory
# The path is stored inside a variable, and the selection can be limited via wildcards

View File

@ -70,6 +70,7 @@ typedef enum {
CMD_ID_FILESEL,
CMD_ID_SET,
CMD_ID_STRSPLIT,
CMD_ID_STRREP,
CMD_ID_CHK,
CMD_ID_ALLOW,
CMD_ID_CP,
@ -123,6 +124,7 @@ Gm9ScriptCmd cmd_list[] = {
{ CMD_ID_FILESEL , "filesel" , 3, 0 },
{ CMD_ID_SET , "set" , 2, 0 },
{ CMD_ID_STRSPLIT, "strsplit", 3, _FLG('b') | _FLG('f')},
{ CMD_ID_STRREP , "strrep" , 3, 0 },
{ CMD_ID_CHK , "chk" , 2, _FLG('u') },
{ CMD_ID_ALLOW , "allow" , 1, _FLG('a') },
{ CMD_ID_CP , "cp" , 2, _FLG('h') | _FLG('w') | _FLG('k') | _FLG('s') | _FLG('n')},
@ -835,6 +837,21 @@ bool run_cmd(cmd_id id, u32 flags, char** argv, char* err_str) {
}
} else if (err_str) snprintf(err_str, _ERR_STR_LEN, "argv[2] is not a char");
}
else if (id == CMD_ID_STRREP) {
char str[_ARG_MAX_LEN];
strncpy(str, argv[1], _ARG_MAX_LEN);
if (strnlen(argv[2], _ARG_MAX_LEN) != 2) {
if (err_str) snprintf(err_str, _ERR_STR_LEN, "argv[2] must be 2 chars");
ret = false;
} else {
for (u32 i = 0; str[i] && (i < _ARG_MAX_LEN); i++) {
if (str[i] == argv[2][0]) str[i] = argv[2][1];
}
ret = set_var(argv[0], str);
if (err_str) snprintf(err_str, _ERR_STR_LEN, "var fail");
}
}
else if (id == CMD_ID_CHK) {
if (flags & _FLG('u')) {
ret = (strncasecmp(argv[0], argv[1], _VAR_CNT_LEN) != 0);