Fixed bug where variables register without an equal

This commit is contained in:
Chris Dorman 2022-05-17 21:16:33 -07:00
parent 3435d0a48e
commit a0237169f4
7 changed files with 173 additions and 16 deletions

View File

@ -1,8 +1,9 @@
#!/usr/bin/slidescript
print "Compressing..."
compress "test" "docs"
compress "functions.ss"
sleep "1"
print "Decompressing..."
decompress "test.tar.ss"
decompress "functions.ss.lz"

Binary file not shown.

118
docs/examples/functions.ss.ss Executable file
View File

@ -0,0 +1,118 @@
#!/usr/bin/slidescript
#############################################################
#### Welcome to the world of SlideScript! ####
#### This script is here for the learning purposes of SS ####
#### Any line starting with a hashtag will treated as a ####
#### comment! ####
#############################################################
# Slide Script, also refered to as SS, is extremely forgiving in
# syntax handling!
# Example:
# print "content"
# print("content")
###
### These will operate properly, as well as:
# Example:
# write "filename.txt" "data"
# write "filename.txt", "data"
# write("filename.txt" "data")
# write("filename.txt", "data")
#
# IT ALL WORKS!
# SlideScript really syntaxes based on quotes and key function words,
# as well as pipes. It really doesn't mind whats around it otherwise
# And every variable is called by %varname%, defining variables, as normal
# Variables in SS
ss_filename=file.txt
ss_stringdata=Data to encrypt and decrypt
ss_exec_command=uname -a
# Printing function in SS
print "Welcome to SlideScript!"
# Sleep function in SS
print "Some content to print, working with '%ss_filename%' today!"
# Below demonstrates SS encrypting a string, passing the output
# over a pipe, and using the write function and %PIPE% variable
# holding the first functions output, writes to %ss_filename%; file.txt
### %PIPE% is the output from the first line function, enc
### %PIPE% is always applied when a pipe is used!
encode "%ss_stringdata%" | write "%ss_filename%" "%PIPE%"
# You're left with file.txt, lets move on
# Lets read the file we just created and show how SS handles its
# own decryption algorithm
read "%ss_filename%" | decrypt "%PIPE%"
# Will display the original variable string!
# SS MD5 function
# Lets get the md5sum of file.txt with our md5 function
md5 "%ss_filename%" | write "%ss_filename%.md5" "%PIPE%"
# Use a pipe, and push the md5 into a text file of file.txt
# You can also stack pipes for whatever tasks you may need
# Here's the encrypt function in action, can also be used as encode
md5 "%ss_filename%" | encode "%PIPE%" | write "%ss_filename%.md5.enc" "%PIPE%"
# Read md5 file
print "%ss_filename%.md5:"
read "%ss_filename%.md5"
# Read encrypted md5 file and decrypt using decode alias
print "%ss_filename%.md5.enc:"
read "%ss_filename%.md5.enc" | decode "%PIPE%"
# Delete function, SS can delete files and directories with one function
# NOTE: it is extremely powerful and can wreck your system if used in the
# wrong way! Proceed with caution, delete "/" WILL send your files to the
# grave.
###
# Lets delete the files we've been messing with, no system calls needed
delete "%ss_filename%"
delete "%ss_filename%.md5"
delete "%ss_filename%.md5.enc"
# Gone!
print "Playing with some calc..."
# calc function, lets do some basic math
calc "32 * 1024"
# You can pipe calc to do multi layer equations
calc "32 * 1024" | calc "%PIPE% * 2"
# Lets play with some big numbers here!
# SlideScript parses its calc functions using floating points, so you can
# handle decimal as well
calc "1024 * 1024" | calc "%PIPE% * %PIPE%"
# Decimal
print "Here comes the decimal:"
calc "46 / 3.4"
# Execute function, SS can call unix system calls!
# Executes the ss_exec_command variable data, 'ls'
print "Testing exec function on system"
exec "%ss_exec_command%"
# comp, loop, if, and ifn examples
# Check to see if /bin/sh exists
exist=`isfile "/bin/sh"`
# Report findings on /bin/sh
if: %exist%; print "/bin/sh exists!"
ifn: %exist%; printf "/bin/sh does not exist!"
# Loop functions, example
loop: 3; print "Loop print 3 times"
# Compare strings
compvar=`comp: "matching" "match"`
ifn: %compvar%; print "Strings don't match"
# Unset compvar
unset "compvar"
# Set again, and compare integers
compvar=`comp: "245" "245"`
if: %compvar%; print "Values match!"

View File

@ -41,3 +41,4 @@ void parse_args(int argc, char** argv);
char *ss_time();
void gen_random_string(char *s, const int len);
char *remove_extension(char *string);
int check_for_var(char *string);

View File

@ -27,6 +27,9 @@ char *process_line (char *line)
//printf("%s", &line[strlen(line)-2]);
static char pathbuf[MAXPATHLENGTH+1];
// Check to see if we're dealing with a
// variable (for the end if the lexer)
int isVar = check_for_var(line);
tok_srch = strtok_r (line, "=\" ;", &strtok_save);
/* reuse function */
@ -433,7 +436,7 @@ char *process_line (char *line)
return NULL;
}
// Print directory
else if (strncmp("showdir", tok_srch, 7) == 0 || strncmp("ls", tok_srch, 2) == 0)
{
char *dirname;
@ -518,7 +521,7 @@ char *process_line (char *line)
decomp_filename = remove_extension(parse_vars(tok_srch));
if (bsize <= 0) {
x_warn("ss:warn:compress, default buffer not set?");
x_warn("ss:warn:decompress, default buffer not set?");
return NULL;
}
@ -537,6 +540,10 @@ char *process_line (char *line)
x_warn("ss:warn:decompress, failed to decompress tarball: %d", lzret);
return NULL;
}
else
{
sprintf(retbuf, "Decompressed -> %s", decomp_filename);
}
/* Destroyes the wrapper instance */
wrapper_destroy(lzwrapper);
@ -1485,6 +1492,7 @@ char *process_line (char *line)
}
}
// Set variables if needed.
else if (strcmp(tok_srch, "\n") != 0 && strcmp(tok_srch, "") != 0)
{
if (tok_srch[strlen(tok_srch)-1] == '\n')
@ -1492,6 +1500,8 @@ char *process_line (char *line)
return NULL;
}
else
{
if(isVar == 1)
{
int varc = get_var_count();
char *varname_tmp = tok_srch;
@ -1504,6 +1514,19 @@ char *process_line (char *line)
set_var(varc, varname_tmp, bq_check);
printf("ss: var '%s' -> %s\n", varname_tmp, get_var_data(varname_tmp));
}
else
{
if(tok_srch != NULL)
{
printf("ss:warn:unknown function: %s\n", tok_srch);
}
else
{
printf("ss:warn:unknown function\n");
}
return NULL;
}
}
}
return NULL;

View File

@ -40,7 +40,7 @@ int main(int argc, char **argv)
exit(EXIT_FAILURE);
}
/* Run while loop if file is empty. */
/* Run while loop if file isnt empty. */
if (script != NULL)
{
while (1)

View File

@ -134,7 +134,9 @@ void gen_random_string(char *s, const int len) {
s[len] = 0;
}
char *remove_extension(char *string) {
// Removes extension from filename for compression
char *remove_extension(char *string)
{
char *ret_string;
char *lastExt;
if (string == NULL) return (char *)NULL;
@ -145,3 +147,15 @@ char *remove_extension(char *string) {
*lastExt = '\0';
return ret_string;
}
int check_for_var(char *string)
{
if(strchr(string, '=') != NULL)
{
return 1;
}
else
{
return 0;
}
}