diff --git a/Makefile b/Makefile index c23f7e5..3c05cb0 100755 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ # (C) Copyright 2014-2021 Chris Dorman, some rights reserved (GPLv2) # Some changes and tweaks from Menchers -VERSION = \"0.5.4\" +VERSION = \"0.6.0\" VERSION_EXTRA = \"$(EXTRA)\" PREFIX ?= /usr diff --git a/Makefile.musl b/Makefile.musl index a3a0fea..f0f6e1b 100755 --- a/Makefile.musl +++ b/Makefile.musl @@ -2,7 +2,7 @@ # (C) Copyright 2014-2021 Chris Dorman, some rights reserved (GPLv2) # Some changes and tweaks from Menchers -VERSION = \"0.5.4\" +VERSION = \"0.6.0\" VERSION_EXTRA = \"$(EXTRA)\" PREFIX ?= /usr diff --git a/docs/README.txt b/docs/README.txt index ea33076..313fb5f 100644 --- a/docs/README.txt +++ b/docs/README.txt @@ -13,15 +13,7 @@ Compiling * Modify test.ss to learn the basics of SS. * Run ./test.ss to execute the script. -***NOTE: if compiling on OSX/BSD, please uncomment #define BSD in src/config.h*** - ------ -Directory bins/ ------ - -Since slidescript is so small, I made a collection of current builds of slidescript -in bins/* -These are built on Debian 10 using GCC 9.2.0, and current TCC. +***NOTE: if compiling on OSX/BSD, please uncomment #define BSD in src/config.h ----- Documentation @@ -32,10 +24,12 @@ at this moment! * Commenting! Examples: * # Comment is real in this one -* Comp and loop functions: +* Comp, loop, if, and ifn functions: * comp: "1" "1" -> compares integer 1 and 1 -> returns true * comp: "true" "false" -> compares string true and false -> returns false * loop: 3; print "Hello world!" -> Will loop print "Hello world!" 3 times. + * if: true; print "True!" -> Prints "True!" + * ifn: false; print "False!" -> Prints "False!" * Read, write, and cat(enate) functions. Examples: * write "file.txt" "This will be written to file.txt" @@ -85,6 +79,7 @@ at this moment! * filename=file.txt -> filename * filedata=File '%filename%' is being moved to moo -> %filename% is populated as file.txt * write "%filename%" "%filedata%" -> writes filedata contents to file.txt as expected. + * unset "filename" -> Removes variable data from session memory to be redefinied / reused. * Decoding and encoding strings * encode "Regular string" -> Converts to encrypted string 'Uhjvqds#xuulqj' @@ -152,6 +147,16 @@ Changelog Changes between version bumps in SlideScript. Hoping to have a lightweight top-down scripting language by V1.0.0 release! From there it will be molding and preserving the art. +* V0.6.0 + * Multiple dynamic memory bug fixes, SlideScript is 99% dynamic on memory use + * Included if, and ifn functions for comp, isfile, isdir, etc return values. + * Incorporated unset function to wipe variable data within the same session. + * Additions to help command + * Code cleanup + * Fixed large bug when attempting to use multiple variables within one function. + * Fixed compile warnings on some systems + * Fixed system uncompatibility for Windows systems, should work cross-compiled! + * V0.5.4 * Added memory management framework for dynamic memory handling * tar.c cleanup diff --git a/docs/examples/functions.ss b/docs/examples/functions.ss index 2ac1e8b..74d2eb9 100755 --- a/docs/examples/functions.ss +++ b/docs/examples/functions.ss @@ -93,3 +93,26 @@ calc "46 / 3.4" # 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/deps.h b/src/inc/deps.h index 3c5b00c..24efcd4 100644 --- a/src/inc/deps.h +++ b/src/inc/deps.h @@ -99,7 +99,7 @@ " read \"\" |" \ " write \"\" \"\"\n" \ " cat \"\" \"\" |" \ - " exec \"\"\n" \ + " exec \"\" | unset \"\"\n" \ "\n-[Variables, Pipes, and Backquoting]-\nExample:\n" \ " FORK=`isfile \"index.html\"` -> returns 1 on find\n" \ " write \"port.txt\" \"8888\" -> writes '8888' to port.txt\n" \ @@ -108,7 +108,8 @@ "backquote function execution. Return values are saved as variables.\n" \ "\n-[Specials]-\nExample:\n" \ "comp: \"\" \"\" -> returns (true|false)\n" \ - "loop: ; -> loops function for 'int' times\n" + "loop: ; -> loops function for 'int' times\n" \ + "if(n): ; -> Processes function if true or false\n" // Purpose: UX3 basic definitions // License: MIT/X. (c) OldCoder (Robert Kiraly) 1987-2021. diff --git a/src/inc/vars.h b/src/inc/vars.h index 8c23530..e421ee8 100644 --- a/src/inc/vars.h +++ b/src/inc/vars.h @@ -25,4 +25,5 @@ UX3_EXT QLIST QM_VARIABLES [ONE]; // Dynamic-memory QLIST void set_var(int index, char *varname, char *vardata); int get_var_count(); char *get_var_data(char *varname); +char *clear_var_data(char *varname); char *parse_vars(char *string); diff --git a/src/lexer.c b/src/lexer.c index b9634a0..df03dfc 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -60,8 +60,105 @@ char *process_line(char *line) // Lets add if and loop statements, make this // Somewhat usable as a language in other instances + // If statement, processes string if first arg is true + else if(strncmp("if:", tok_srch, 3) == 0) + { + char *proc_return; + char *dobuf; + + // We have an if statement + tok_srch = strtok(NULL, ";"); + + if(tok_srch == NULL) + { + x_warn("ss:warn:loop statement requires arguments"); + return NULL; + } + + // Check if there's anything after the first quote + if(strcmp(tok_srch, "\n") == 0 || + strcmp(tok_srch, " \n") == 0 || + strcmp(tok_srch, " ") == 0) + { + x_warn("ss:warn:loop syntax error, missing argument?"); + return NULL; + } + + compbuf = qmalloc(QM_SS, (strlen(parse_vars(tok_srch)) + 1)); + *compbuf = '\0'; + strcat(compbuf, parse_vars(tok_srch)); + + tok_srch = strtok(NULL, ";"); + if(tok_srch == NULL) + { + x_warn("ss:warn:loop syntax error, missing last argument?"); + return NULL; + } + + dobuf = qmalloc(QM_SS, (strlen(parse_vars(tok_srch)) + 1)); + *dobuf = '\0'; + strcat(dobuf, parse_vars(tok_srch)); + + if((strncmp("true", compbuf, 4) == 0) || (strncmp("1", compbuf, 1) == 0)) + { + proc_return = process_line(parse_vars(dobuf)); + printf("%s\n", proc_return); + fflush(stdout); + } + + return NULL; + } + + else if(strncmp("ifn:", tok_srch, 4) == 0) + { + char *proc_return; + char *dobuf; + + // We have an if statement + tok_srch = strtok(NULL, ";"); + + if(tok_srch == NULL) + { + x_warn("ss:warn:loop statement requires arguments"); + return NULL; + } + + // Check if there's anything after the first quote + if(strcmp(tok_srch, "\n") == 0 || + strcmp(tok_srch, " \n") == 0 || + strcmp(tok_srch, " ") == 0) + { + x_warn("ss:warn:loop syntax error, missing argument?"); + return NULL; + } + + compbuf = qmalloc(QM_SS, (strlen(parse_vars(tok_srch)) + 1)); + *compbuf = '\0'; + strcat(compbuf, parse_vars(tok_srch)); + + tok_srch = strtok(NULL, ";"); + if(tok_srch == NULL) + { + x_warn("ss:warn:loop syntax error, missing last argument?"); + return NULL; + } + + dobuf = qmalloc(QM_SS, (strlen(parse_vars(tok_srch)) + 1)); + *dobuf = '\0'; + strcat(dobuf, parse_vars(tok_srch)); + + if((strncmp("false", compbuf, 4) == 0) || (strncmp("0", compbuf, 1) == 0)) + { + proc_return = process_line(parse_vars(dobuf)); + printf("%s\n", proc_return); + fflush(stdout); + } + + return NULL; + } + // Loop - else if(strncmp("loop:", tok_srch, 4) == 0) + else if(strncmp("loop:", tok_srch, 5) == 0) { int loop_count, ii; char *proc_return; @@ -97,13 +194,6 @@ char *process_line(char *line) return NULL; } - // Check if there's anything after the first quote - if(strcmp(tok_srch, "\n") == 0 || strcmp(tok_srch, " \n") == 0) - { - x_warn("ss:warn:if syntax error, missing first argument?"); - return NULL; - } - loopbuf = qmalloc(QM_SS, (strlen(parse_vars(tok_srch)) + 1)); *loopbuf = '\0'; strcat(loopbuf, parse_vars(tok_srch)); @@ -121,7 +211,6 @@ char *process_line(char *line) return NULL; } - // IF statement else if(strncmp("comp:", tok_srch, 5) == 0) { @@ -182,6 +271,29 @@ char *process_line(char *line) } } + // Unset variable function + else if(strncmp("unset", tok_srch, 5) == 0) + { + char *varname; + + tok_srch = strtok(NULL, "\""); + if(tok_srch == NULL) + { + x_warn("ss:warn:unset syntax error, missing quote?"); + return NULL; + } + + if(strtok(NULL, "\"") == NULL) + { + x_warn("ss:warn:unset syntax error, missing end quote?"); + return NULL; + } + + varname = clear_var_data(tok_srch); + + return varname; + } + // Change directory else if(strncmp("backdir", tok_srch, 7) == 0) { diff --git a/src/vars.c b/src/vars.c index 527935a..a888f68 100644 --- a/src/vars.c +++ b/src/vars.c @@ -48,7 +48,7 @@ char *get_var_data (char *varname) { char *varp = svars [xx].var_name; if (*varp == '\0') continue; - + if(strcmp (varp, varname) == ZERO) { return svars [xx].var_data; @@ -58,6 +58,31 @@ char *get_var_data (char *varname) return NULL; } +//-------------------------------------------------------------------- +// Clear variable data by name +//-------------------------------------------------------------------- + +char *clear_var_data (char *varname) +{ + int xx; + + for(xx = ZERO; xx < MAXVARS; xx++) + { + char *varp = svars [xx].var_name; + if (*varp == '\0') continue; + + if(strcmp (varp, varname) == ZERO) + { + qfree(svars[xx].var_data); + strcpy(svars[xx].var_name, "\0"); + return "Variable free'd"; + } + + } + + return NULL; +} + //-------------------------------------------------------------------- int get_var_count()