diff --git a/docs/examples/tar.ss b/docs/examples/compress.ss similarity index 60% rename from docs/examples/tar.ss rename to docs/examples/compress.ss index 7bff79c..c4aeee5 100755 --- a/docs/examples/tar.ss +++ b/docs/examples/compress.ss @@ -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" diff --git a/docs/examples/functions.ss.lz b/docs/examples/functions.ss.lz new file mode 100644 index 0000000..024be65 Binary files /dev/null and b/docs/examples/functions.ss.lz differ diff --git a/docs/examples/functions.ss.ss b/docs/examples/functions.ss.ss new file mode 100755 index 0000000..74d2eb9 --- /dev/null +++ b/docs/examples/functions.ss.ss @@ -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!" diff --git a/src/inc/util.h b/src/inc/util.h index 54209dd..51aff9e 100644 --- a/src/inc/util.h +++ b/src/inc/util.h @@ -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); diff --git a/src/lexer.c b/src/lexer.c index 3545a1a..f88a6d1 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -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') @@ -1493,16 +1501,31 @@ char *process_line (char *line) } else { - int varc = get_var_count(); - char *varname_tmp = tok_srch; - char *bq_check; - tok_srch = strtok_next ("="); + if(isVar == 1) + { + int varc = get_var_count(); + char *varname_tmp = tok_srch; + char *bq_check; + tok_srch = strtok_next ("="); - // Check for back quotes, return string with backquotes processed - bq_check = strip_nl(parse_vars(parse_bq(tok_srch))); - // Don't check if variable is blank, if so let it fly! - set_var(varc, varname_tmp, bq_check); - printf("ss: var '%s' -> %s\n", varname_tmp, get_var_data(varname_tmp)); + // Check for back quotes, return string with backquotes processed + bq_check = strip_nl(parse_vars(parse_bq(tok_srch))); + // Don't check if variable is blank, if so let it fly! + 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; + } } } diff --git a/src/main.c b/src/main.c index c209cec..3ab9d2f 100644 --- a/src/main.c +++ b/src/main.c @@ -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) @@ -55,7 +55,7 @@ int main(int argc, char **argv) fflush (stdout); } -/* parse each line from the script in order. */ + /* parse each line from the script in order. */ if (fgets (script_line, sizeof(script_line), script) == NULL) break; diff --git a/src/util.c b/src/util.c index 05a0042..19c3724 100644 --- a/src/util.c +++ b/src/util.c @@ -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; + } +}