From 8608bf2392eafc40aa5cbdc16e010b674410ff57 Mon Sep 17 00:00:00 2001 From: Pentium44 Date: Thu, 8 Apr 2021 02:19:12 -0700 Subject: [PATCH] Call this v0.4.0 --- Makefile | 4 +- README.md | 76 ++++++++++++-------- examples/net-listen.ss | 2 +- examples/{net-connect.ss => net-toss.ss} | 4 +- src/deps.h | 9 +++ src/functions.c | 29 ++++++++ src/main.c | 42 +++++------ src/math.c | 90 ++++++++++++++++++++++++ src/math.h | 11 +++ src/pipe.c | 44 +++++++++--- src/pipe.h | 13 +++- src/util.c | 6 ++ src/util.h | 3 + 13 files changed, 270 insertions(+), 63 deletions(-) rename examples/{net-connect.ss => net-toss.ss} (84%) create mode 100644 src/math.c create mode 100644 src/math.h diff --git a/Makefile b/Makefile index cc50ee1..1e63702 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.3.3\" +VERSION = \"0.4.0\" VERSION_EXTRA = \"$(EXTRA)\" PREFIX ?= /usr @@ -13,7 +13,7 @@ CPPFLAGS += -DVERSION=$(VERSION) -D_FORTIFY_SOURCE=2 LDFLAGS += -Wl,-O1,--sort-common,--hash-style=gnu,-z,relro BIN ?= slidescript -OBJECTS = src/main.o src/functions.o src/util.o src/vars.o src/enc.o src/md5.o src/pipe.o src/network.o +OBJECTS = src/main.o src/functions.o src/util.o src/vars.o src/enc.o src/md5.o src/pipe.o src/network.o src/math.o all: main diff --git a/README.md b/README.md index 1d4f304..0ca1388 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ Using SlideScript * 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*** Documentation @@ -17,93 +18,110 @@ Documentation Here is a list of functions and features that SlideScript comes with at this moment! - * Commenting! Examples: +* Commenting! Examples: * `# Comment is real in this one` - * Read and writing to flat files. Examples: +* Read and writing to flat files. Examples: * `write "file.txt" "This will be written to file.txt"` * `read "file.txt"` - * Delete example: +* Basic math expressions + * `calc "45 / 5"` or `calc "255.3 * 442.77"` + * Of course addition and subtraction as well! + +* Delete example: * `delete "file.txt"` - * Execute example: +* Execute example: * `exec "ls -al"` - * Print example: +* Print example: * `print "Hi everyone!"` - * Sleep (Zzz) example (sleeps for 2 seconds): +* Sleep (Zzz) example (sleeps for 2 seconds): * `sleep 2` - * Variable setting and passing +* 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. - * Decoding and encoding strings +* Decoding and encoding strings * `encrypt "Regular string"` -> Converts to encrypted string 'Uhjvqds#xuulqj' * `decrypt "Uhjvqds#xuulqj"` -> Converts back to 'Regular string' - * MD5 file checking +* MD5 file checking * `md5 "file.txt"` -> outputs filename and md5 hash - * 2 Layer piping - * `md5 "file.txt" | write "file.txt.md5" "%PIPE%"` -> writes output of md5 to file.txt.md5 +* Layered piping + * `md5 "file.txt" | encrypt "%PIPE" | write "file.txt.md5.enc" "%PIPE%"` -> writes output of md5 to file.txt.md5 - * Networking functions +* 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, forkval (0 or 1, 0 don't fork into background / 1 do). + + This will change rapidly as of currently, slidescript is in beavy development! Todo list ----- - * Add in-script functions - * New static functions - * Loops, and if statements + +* Add in-script functions +* New static functions +* Loops, and if statements + Done ----- - * Simple syntax checking and error reporting - * 2 layer function piping - * Support for linux system calls - * Network listen socket, toss function - * Builtin HTTP server for disposeable use, can be ran in the foreground or forked into the background - * Read and write from file - * Some simple functions - * Shebang handling - * Variable support + +* Simple syntax checking and error reporting +* Up to 32 layer function piping +* Support for linux system calls +* Network listen socket, toss function +* Builtin HTTP server for disposeable use, can be ran in the foreground or forked into the background +* Read and write from file +* Some simple functions +* Shebang handling +* Variable support + Changelog ----- - * V0.3.3 +* V0.4.0 + * Added calc function for floating math equations + * Cleaned up some more syntax handling, as well as bugs + * Added structured and savable multi-layer piping up to 32 functions deep + +* V0.3.3 * Added first networking functions: netlisten, nettoss, nethttp. * Embedded web server functionality * Cleaned up code * Improved syntax handling on functions - * V0.3.0 + +* V0.3.0 * Added simple 2 layer function piping for more useful tasks * Fixed a couple core dump bugs * Now reads files into memory, and then forces text files through SS, allows for in file variables * Added "md5" functionality * Multi-formal working syntax - * V0.2.1 + +* V0.2.1 * Added "decrypt" decode function * Added "encrypt" encode function * Added system "exec" function * Added basic syntax handling, for a more uniform language - * V0.2.0 +* V0.2.0 * Added "delete" function * Added embedded variable handling to SS functions (variables can be used like everywhere!) * Added linux system calls via exec * Some cleaning up. - * V0.1.1 +* V0.1.1 * Added variable handling with a buffer size of 2KB per variable, and cap of 2048 variables. * Now operates under the shebang! diff --git a/examples/net-listen.ss b/examples/net-listen.ss index e19aade..e9adbbd 100755 --- a/examples/net-listen.ss +++ b/examples/net-listen.ss @@ -1,6 +1,6 @@ #!/usr/bin/slidescript -port=2001 +port=2020 search=hi respond=bye diff --git a/examples/net-connect.ss b/examples/net-toss.ss similarity index 84% rename from examples/net-connect.ss rename to examples/net-toss.ss index 5559ac7..59b9ba5 100755 --- a/examples/net-connect.ss +++ b/examples/net-toss.ss @@ -1,13 +1,13 @@ #!/usr/bin/slidescript address=127.0.0.1 -port=2001 +port=2020 data1=This data is gonna be pushed to the server side host! data2=Here's the second set of data getting tossed to the server data3=Of course, do a little more considering the transfer is instant! nettoss "%address%" "%port%" "%data1%" nettoss "%address%" "%port%" "%data2%" -nettoss "%address%" "%port%" "%data3%" +encode "%data3%" | nettoss "%address%" "%port%" "%PIPE%" nettoss "%address%" "%port%" "hi" diff --git a/src/deps.h b/src/deps.h index c333fba..93bb045 100644 --- a/src/deps.h +++ b/src/deps.h @@ -9,9 +9,13 @@ #include #include #include +#include #include #include #include +#include +#include +#include #include "config.h" #include "sbyteswap.h" #include @@ -26,7 +30,9 @@ #define MAX_STRING_LEN 4096 #define MAX_STRING_BUFSIZE (MAX_STRING_LEN + 1) #define MAX_VAR_DATA_LEN 65536 +#define MAX_DATA_BUFSIZE (MAX_VAR_DATA_LEN + 1) #define MAXVARS 1488 +#define MAX_PIPE_CMDS 32 #define MAX_CONCAT_BUF 131072 #define MAX_READFILE_LEN 2097152 // for networking functions @@ -36,6 +42,9 @@ #define MAX_NETSRCH_BUF (MAX_NETSRCH_LEN + 1) #define MAX_NETRESP_LEN 65536 #define MAX_NETRESP_BUF (MAX_NETRESP_BUF + 1) +// For Expr +#define MAX_EXPR_ARGS 128 +#define MAX_EXPR_LEN 512 // END #define TOKEN '%' #define TOKEN_STR "%" diff --git a/src/functions.c b/src/functions.c index 9f40406..5fa8024 100644 --- a/src/functions.c +++ b/src/functions.c @@ -10,6 +10,7 @@ #include "util.h" #include "vars.h" #include "enc.h" +#include "math.h" #include "md5.h" #include "network.h" @@ -37,6 +38,15 @@ char *process_line(char *line) if(strncmp("exit", tok_srch, 4) == 0) syn_error("ss:exit called"); + /* time function */ + /* print function */ + else if(strncmp("time",tok_srch,4) == 0) + { + char *parsed; + parsed = ss_time(); + return strip_nl(parsed); + } + /* print function */ else if(strncmp("print",tok_srch,5) == 0) { @@ -78,6 +88,25 @@ char *process_line(char *line) return NULL; } + // Calc function, some math involved! // + else if(strncmp("calc",tok_srch,5) == 0) + { + char *expr_return; + tok_srch = strtok(NULL, "\""); + if(tok_srch == NULL) + syn_error("ss:error:calc syntax error, missing quotes around equation?"); + + if(strcmp(tok_srch, "\n") == 0 || strcmp(tok_srch, " \n") == 0) + syn_error("ss:error:calc syntax error, missing quotes around equation?"); + + if(strtok(NULL, "\"") == NULL) + syn_error("ss:error:calc missing end quote"); + + expr_return = ss_expr(tok_srch); + + return expr_return; + } + /* Networking, listen */ else if(strncmp("netlisten",tok_srch,9) == 0) { diff --git a/src/main.c b/src/main.c index 288e856..1b2946f 100644 --- a/src/main.c +++ b/src/main.c @@ -41,34 +41,36 @@ int main(int argc, char **argv) /* parse each line from the script in order. */ while(fgets(script_line, sizeof(script_line), script) != NULL) { - char *return_dat, *pipechk, *return_pipe_dat; - pipechk = ss_piping(script_line); + char *return_dat; + int pipechk; + ss_piping(script_line); - return_dat = process_line(script_line); + pipechk = get_cmd_count(); + + return_dat = process_line(spipe[0].command); // Blank line, getting up outta here. if(return_dat == NULL) continue; - if(pipechk != NULL) + int varc, ii; + varc = get_var_count(); + if(pipechk > 1) { - int varc; - varc = get_var_count(); - set_var(varc, "PIPE", return_dat); - // Process functions based on previous pipe return - return_pipe_dat = process_line(pipechk); - // set PIPE var back to NULL after use, keep the struct clean - set_var(varc, "\0", "\0"); - // Check to see if there's anything to even display - if(return_pipe_dat == NULL) continue; + for(ii = 1; ii < pipechk; ii++) + { + set_var(varc, "PIPE", return_dat); + // Process functions based on previous pipe return + return_dat = process_line(spipe[ii].command); + // set PIPE var back to NULL after use, keep the struct clean + set_var(varc, "\0", "\0"); + // Check to see if there's anything to even display + if(return_dat == NULL) break; + } + } - // Print the pipe line - printf("%s\n", return_pipe_dat); - } - else - { - // If return is not null, and no pipe; provide the return + // Print the pipe line + if(return_dat != NULL) printf("%s\n", return_dat); - } } /* end of while */ } /* file null */ diff --git a/src/math.c b/src/math.c new file mode 100644 index 0000000..a34597d --- /dev/null +++ b/src/math.c @@ -0,0 +1,90 @@ +// expr.c, from suckless sbase. +#include "deps.h" +#include "math.h" +#include "util.h" + +static char expr_out[(MAX_EXPR_LEN*2)+1]; + +int get_expr_count() +{ + int yy; + for(yy = 0; yy < MAX_EXPR_ARGS; yy++) + { + if(strlen(sexpr[yy].arg) > 0) + { + continue; + } + else + { + return yy; + } + } + return 0; +} + +void set_expr_arg(int index, char *arg) +{ + strcpy(sexpr[index].arg, arg); +} + +// Main expr function +char *ss_expr(char *string) +{ + char *expr_tok, *f_end, *s_end, *calc_operator; + int expr_argc, n; + float first, second, finalf; + + expr_tok = strtok(string, " "); + if(expr_tok == NULL) + syn_error("ss:error:calc missing equation"); + + // Clear arguments from previous calculations. + for(int ff = 0; ff < MAX_EXPR_ARGS; ff++) bzero(sexpr[ff].arg, MAX_EXPR_LEN); + + // Set initial argument + set_expr_arg(0, expr_tok); + + // Write new arguments + n = 1; + while((expr_tok = strtok(NULL, " ")) != NULL) + { + if(n > 3) syn_error("ss:error:calc recieved too many arguments");; + int argc = get_expr_count(); + set_expr_arg(argc, expr_tok); + n++; + } + + // Check routine to make sure we're doing simple math xD + expr_argc = get_expr_count(); + + if(expr_argc > 3) + syn_error("ss:error:calc only supports dual values (3 * 14)"); + + first = strtof(sexpr[0].arg, &f_end); + calc_operator = sexpr[1].arg; + second = strtof(sexpr[2].arg, &s_end); + + switch (calc_operator[0]) { + case '+': + finalf = first + second; + break; + case '-': + finalf = first - second; + break; + case '*': + finalf = first * second; + break; + case '/': + finalf = first / second; + break; + // operator doesn't match any case constant + default: + finalf = 0; + syn_error("ss:error:calc operator not supported"); + } + + if(gcvt(finalf, 12, expr_out)==NULL) + syn_error("ss:error:calc illegal instruction"); + + return expr_out; +} diff --git a/src/math.h b/src/math.h new file mode 100644 index 0000000..3a482d1 --- /dev/null +++ b/src/math.h @@ -0,0 +1,11 @@ +// For SS +struct s_expr +{ + char arg[MAX_EXPR_LEN+1]; +}; + +typedef struct s_expr EXPR; +EXPR sexpr [MAX_EXPR_ARGS]; + +void set_expr_arg(int index, char *arg); +char *ss_expr(char *string); diff --git a/src/pipe.c b/src/pipe.c index d2f3d59..2709301 100644 --- a/src/pipe.c +++ b/src/pipe.c @@ -7,10 +7,33 @@ #include "deps.h" +#include "vars.h" #include "pipe.h" #include "util.h" -char *ss_piping(char *string) +void set_cmd(int index, char *cmd) +{ + strcpy(spipe[index].command, cmd); +} + +int get_cmd_count() +{ + int yy; + for(yy = 0; yy < MAX_PIPE_CMDS; yy++) + { + if(strlen(spipe[yy].command) > 0) + { + continue; + } + else + { + return yy; + } + } + return 0; +} + +void ss_piping(char *string) { char *pipe_tok; @@ -22,15 +45,20 @@ char *ss_piping(char *string) sprintf(stringmem, "%s", string); pipe_tok = strtok(stringmem, "|"); - if(pipe_tok != NULL && strcmp(pipe_tok, string) !=0) + if(pipe_tok == NULL) { - char *second_func; - pipe_tok = strtok(NULL, "|"); - second_func = pipe_tok + 1; - return second_func; + set_cmd(0, string); + } + else + { + set_cmd(0, pipe_tok); + } + + while((pipe_tok = strtok(NULL, "|")) != NULL) + { + int cmdc = get_cmd_count(); + set_cmd(cmdc, pipe_tok); } free(stringmem); - - return NULL; } diff --git a/src/pipe.h b/src/pipe.h index 5ee8976..84531db 100644 --- a/src/pipe.h +++ b/src/pipe.h @@ -6,4 +6,15 @@ */ -char *ss_piping(char *string); + +struct s_cmds +{ + char command [MAX_STRING_BUFSIZE]; +}; + +typedef struct s_cmds CMDS; +MY_GLOBAL CMDS spipe [MAX_PIPE_CMDS]; + +void set_cmd(int index, char *cmd); +int get_cmd_count(); +void ss_piping(char *string); diff --git a/src/util.c b/src/util.c index c7cdb2a..33312f9 100644 --- a/src/util.c +++ b/src/util.c @@ -78,3 +78,9 @@ void parse_args(int argc, char** argv) } +char *ss_time() +{ + time(¤t_time); + + return ctime(¤t_time); +} diff --git a/src/util.h b/src/util.h index 33c49b2..5a62994 100644 --- a/src/util.h +++ b/src/util.h @@ -5,9 +5,12 @@ View README file supplied with this software for more details */ +time_t current_time; + void syn_error(char *message); char *strip_nl(char *string); int file_exists(char *path); int is_dir(char *path); char *ss_concat(char *str1, char *str2); void parse_args(int argc, char** argv); +char *ss_time();