From 3f25d819089d5a34f1cf8ac8e0138d7942cd1e2c Mon Sep 17 00:00:00 2001 From: Pentium44 Date: Thu, 8 Apr 2021 20:55:59 -0700 Subject: [PATCH] Call this v0.4.4 --- Makefile | 2 +- README.txt | 63 ++++++++++++------- examples/file.txt | 1 + examples/index.html | 6 -- examples/net-http.ss | 20 ------ examples/webserver.ss | 25 ++++++++ src/functions.c | 141 +++++++++++++++++++++++++++++++++++++++--- src/vars.c | 14 ++++- 8 files changed, 214 insertions(+), 58 deletions(-) create mode 100644 examples/file.txt delete mode 100644 examples/index.html delete mode 100755 examples/net-http.ss create mode 100755 examples/webserver.ss diff --git a/Makefile b/Makefile index 929d6e4..e1df3ce 100644 --- 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.4.0\" +VERSION = \"0.4.4\" VERSION_EXTRA = \"$(EXTRA)\" PREFIX ?= /usr diff --git a/README.txt b/README.txt index 275fd6f..a960d6c 100644 --- a/README.txt +++ b/README.txt @@ -22,49 +22,61 @@ Here is a list of functions and features that SlideScript comes with at this moment! * Commenting! Examples: - * `# Comment is real in this one` + * # Comment is real in this one -* Read and writing to flat files. Examples: - * `write "file.txt" "This will be written to file.txt"` - * `read "file.txt"` +* Read, write, and cat(enate) functions. Examples: + * write "file.txt" "This will be written to file.txt" + * read "file.txt" + * cat "file.txt" "Data to write to end of file.txt" * Basic math expressions - * `calc "45 / 5"` or `calc "255.3 * 442.77"` - * Of course addition and subtraction as well! + * calc "45 / 5"` or `calc "255.3 * 442.77" + * Of course addition and subtraction as well + +* Time example: + * time + +* Isdir function, example: + * isdir "examples/" -> returns 0 for not, 1 for is * Delete example: - * `delete "file.txt"` + * delete "file.txt" * Execute example: - * `exec "ls -al"` + * exec "ls -al" * Print example: - * `print "Hi everyone!"` + * print "Hi everyone!" * Sleep (Zzz) example (sleeps for 2 seconds): - * `sleep 2` + * sleep 2 * Variable setting and passing - * `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. + * 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. * Decoding and encoding strings - * `encrypt "Regular string"` -> Converts to encrypted string 'Uhjvqds#xuulqj' - * `decrypt "Uhjvqds#xuulqj"` -> Converts back to 'Regular string' + * encode "Regular string" -> Converts to encrypted string 'Uhjvqds#xuulqj' + * decode "Uhjvqds#xuulqj" -> Converts back to 'Regular string' * MD5 file checking - * `md5 "file.txt"` -> outputs filename and md5 hash + * md5 "file.txt" -> outputs filename and md5 hash * Layered piping - * `md5 "file.txt" | encrypt "%PIPE" | write "file.txt.md5.enc" "%PIPE%"` -> writes output of md5 to file.txt.md5 + * md5 "file.txt" | encrypt "%PIPE" | write "file.txt.md5.enc" "%PIPE%" -> writes output of md5 to file.txt.md5 * Networking functions - * `netlisten "" "" ""` -> listens on and replies on found from outside - * `nettoss "
" "" ""` -> binds to outside server at
: and pushes thus, disconnecting - * `nethttp "" ""` -> throws up a web server on in the current working directory, + * netlisten "" "" "" -> listens on and replies on found from outside + * nettoss "
" "" "" -> binds to outside server at
: and pushes thus, disconnecting + * nethttp "" "" -> throws up a web server on in the current working directory, forkval (0 or 1, 0 don't fork into background / 1 do). +* Backquoted function parsing within variables + * curtime=`time` -> Will set curtime value to return of time at the time of parse. + print "%curtime%" -> Prints the variable value of curtime, same operation as function time, but good for timing scripts + + This will change rapidly as of currently, slidescript is in beavy development! To get the feel of the scripting, the best bet is to take a look at some of the slidescript examples in 'examples/' @@ -92,7 +104,8 @@ Done List of finished features, in a rough summary. -* Simple syntax checking and error reporting +* Simple syntax checking and error reporting, its a lazy language, syntax can sway. +* Most syntax errors will produce warnings instead of terminating process * Up to 32 layer function piping * Support for linux system calls * Network listen socket, toss function @@ -109,6 +122,14 @@ 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.4.4 + * Softened the kill program errors, added warn functions behind the scenes + * Added backquote function processing in variables + * Fixed bug in read not displaying multi-line files + * Fixed bug in write not finding first quote syntax + * Added function isdir, and cat(enate) function. NOT cat as in linux env. + * Code clean-up + * V0.4.0 * Added calc function for floating math equations * Cleaned up some more syntax handling, as well as bugs diff --git a/examples/file.txt b/examples/file.txt new file mode 100644 index 0000000..9b24f01 --- /dev/null +++ b/examples/file.txt @@ -0,0 +1 @@ +hi1 diff --git a/examples/index.html b/examples/index.html deleted file mode 100644 index 9b3e050..0000000 --- a/examples/index.html +++ /dev/null @@ -1,6 +0,0 @@ - - -

SlideScript

-

This is an index page hosted by the built in SS HTTP server

- - diff --git a/examples/net-http.ss b/examples/net-http.ss deleted file mode 100755 index a1ebfad..0000000 --- a/examples/net-http.ss +++ /dev/null @@ -1,20 +0,0 @@ -#!/usr/bin/slidescript -# SlideScript example, start a disposable http server -# -# Function nethttp "" "" -# fork values: -# * 0 = do not fork to background -# * 1 = fork to background - -# Daemonize port -port1=8081 -# Forground port -port2=8080 - -# Daemonize, run in background -#nethttp "%port1%" "1" - -# Run in foreground, Ctrl-C to exit ;) -nethttp "%port1%" "0" - -print "Server on %port1% still running in the background" diff --git a/examples/webserver.ss b/examples/webserver.ss new file mode 100755 index 0000000..2bbfc23 --- /dev/null +++ b/examples/webserver.ss @@ -0,0 +1,25 @@ +#!/usr/bin/slidescript +# Generate an index.html example for the built-in HTTP daemon example +# This will be used as a disposible web server! + +# First, lets populate some data for the web server itself, this script +# will generate the index.html for the web server before launch. + +# Variables of the contents for index.html +header=SlideScript ~ Web server +head=

SlideScript - disposible web server index page

+content=

There's not much to see here yet, more to come!

+footer=
powered by ss:http
+ +# Webserver port +port=8080 + +# Filename in a variable +filename=index.html + +# Writing the variable data to the file +write "%filename%" "%header%%head%%content%%footer%" + +# Starting web server in the foreground, change 0 to 1 to fork to background +print "Ctrl-C to kill me!" +nethttp "%port%" "0" diff --git a/src/functions.c b/src/functions.c index c89dd4a..c3b8d06 100644 --- a/src/functions.c +++ b/src/functions.c @@ -50,6 +50,34 @@ char *process_line(char *line) return strip_nl(parsed); } + /* print function */ + else if(strncmp("isdir",tok_srch,5) == 0) + { + int parsed; + tok_srch = strtok(NULL, "\""); + if(tok_srch == NULL) + { + syn_warn("ss:warn:isdir syntax error, missing quote?"); + return NULL; + } + + if(strtok(NULL, "\"")==NULL) + { + syn_warn("ss:warn:isdir syntax error, missing end quote?"); + return NULL; + } + + parsed = is_dir(parse_vars(tok_srch)); + if(parsed == 1) + { + return "1"; + } + else + { + return "0"; + } + } + /* print function */ else if(strncmp("print",tok_srch,5) == 0) { @@ -500,6 +528,7 @@ char *process_line(char *line) /* strtok to filename of function */ tok_srch = strtok(NULL, "\""); + if(tok_srch == NULL) { syn_warn("ss:warn:write syntax error, missing quote?"); return NULL; @@ -518,7 +547,7 @@ char *process_line(char *line) /* Check if file exists and can be opened */ if(write_file == NULL) { - syn_warn("ss:warn:write syntax error, missing quote?"); + syn_warn("ss:warn:write, failed to open file"); return NULL; } @@ -546,7 +575,7 @@ char *process_line(char *line) if(strtok(NULL, "\"") == NULL) { - syn_warn("ss:warn:write syntax error, missing quote?"); + syn_warn("ss:warn:write syntax error, missing end quote?"); return NULL; } @@ -613,25 +642,123 @@ char *process_line(char *line) if(read_size <= MAX_READFILE_LEN) { + // Copy string to end of file, rewrite file sprintf(filebuf, "%s", dynfile); + free(dynfile); } else { - fclose(read_file); free(dynfile); + fclose(read_file); syn_warn("ss:warn:write, file too large (2MB max)"); return NULL; } - fclose(read_file); - // For now print to test // - free(dynfile); - return strip_nl(filebuf); } /* read function */ + /* Cat function, writes to end of file specified */ + else if(strncmp("cat",tok_srch,3) == 0) + { + unsigned long read_size; + char read_line[1024]; + char filename[2048+1], *dynfile; + FILE *read_file = NULL; + + /* strtok to filename of function */ + tok_srch = strtok(NULL, "\""); + if(tok_srch == NULL) + { + syn_warn("ss:warn:cat syntax error, missing quote?"); + return NULL; + } + + if(strcmp(tok_srch, "\n") == 0 || strcmp(tok_srch, " \n") == 0) + { + syn_warn("ss:warn:cat syntax error, missing filename?"); + return NULL; + } + + if(strlen(parse_vars(tok_srch)) < 2048) + { + strcpy(filename, parse_vars(tok_srch)); + } + else + { + syn_warn("ss:warn:cat, filename too long"); + return NULL; + } + + // WERE LOADING FILES INTO MEMORY NOW! + /* open file */ + read_file = fopen(filename, "r"); + + /* strtok to the content that will be written to file */ + if(read_file == NULL) + { + syn_warn("ss:warn:cat, failed to open file"); + return NULL; + } + + tok_srch = strtok(NULL, "\""); + if(tok_srch == NULL) + { + syn_warn("ss:warn:cat syntax error, missing quote?"); + return NULL; + } + + tok_srch = strtok(NULL, "\""); + if(tok_srch == NULL) + { + syn_warn("ss:warn:cat syntax error, missing quote?"); + return NULL; + } + + if(strcmp(tok_srch, "\n") == 0 || strcmp(tok_srch, " \n") == 0) + { + syn_warn("ss:warn:cat syntax error, missing write data?"); + return NULL; + } + + if(strtok(NULL, "\"") == NULL) + { + syn_warn("ss:warn:cat syntax error, missing end quote?"); + return NULL; + } + + fseek(read_file, 0L, SEEK_END); + read_size = ftell(read_file); + rewind(read_file); + + dynfile = (char *)malloc(read_size + 1); + + while(fgets(read_line, sizeof(read_line), read_file) != NULL) + { + strcat(dynfile, read_line); + } + + fclose(read_file); + + if((read_size + strlen(parse_vars(tok_srch))) <= MAX_READFILE_LEN) + { + read_file = fopen(filename, "w"); + sprintf(filebuf, "%s%s", dynfile, parse_vars(tok_srch)); + fprintf(read_file, "%s", filebuf); + free(dynfile); + fclose(read_file); + } + else + { + free(dynfile); + syn_warn("ss:warn:cat, file too large (2MB max)"); + return NULL; + } + + return NULL; + } /* cat function */ + else if(strncmp("delete", tok_srch, 6) == 0) { char *del_filename; tok_srch = strtok(NULL, "\""); diff --git a/src/vars.c b/src/vars.c index b227494..63e7456 100644 --- a/src/vars.c +++ b/src/vars.c @@ -82,17 +82,25 @@ char *parse_vars(char *string) variable_pointer[-1] = NULLBYTE; if(*varbuffer == NULLBYTE) - syn_error("ss:error:variable syntax error!"); + { + syn_warn("ss:warning:variable syntax error!"); + return NULL; + } variable_pointer = get_var_data(varbuffer); if(variable_pointer == NULL) - syn_error("ss:error:variable syntax error!"); + { + syn_error("ss:error:variable data not found, abort!"); + } var_len = strlen(variable_pointer); buf_len = strlen(finished) + var_len; if(buf_len > MAX_STRING_LEN) - syn_error("ss:error:string buffer overflow!"); + { + syn_warn("ss:error:variable data too long, abort!"); + } + strcat (finished, variable_pointer); output_pointer += var_len;