From 9b29cf8de92152bc6a46d1cf1504d8e19d275f00 Mon Sep 17 00:00:00 2001 From: Pentium44 Date: Tue, 6 Apr 2021 22:27:35 -0700 Subject: [PATCH] Call this v0.3.0 --- Makefile | 4 +- README.md | 7 +++ src/deps.h | 2 + src/functions.c | 47 ++++++++++++++++++--- src/main.c | 33 +++++++++++++-- src/pipe.c | 36 ++++++++++++++++ src/pipe.h | 9 ++++ src/util.c | 7 +++ src/util.h | 1 + test.ss | 110 +++++++++++++++++++++++++++--------------------- 10 files changed, 196 insertions(+), 60 deletions(-) create mode 100644 src/pipe.c create mode 100644 src/pipe.h diff --git a/Makefile b/Makefile index 713ed3c..2e6bc17 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.2.1\" +VERSION = \"0.3.0\" EXTRA ?= dev VERSION_EXTRA = \"$(EXTRA)\" @@ -14,7 +14,7 @@ CPPFLAGS += -DVERSION=$(VERSION) -DVERSION_EXTRA=$(VERSION_EXTRA) -D_FORTIFY 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 +OBJECTS = src/main.o src/functions.o src/util.o src/vars.o src/enc.o src/md5.o src/pipe.o all: main diff --git a/README.md b/README.md index e14cfe9..cb07ae5 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,13 @@ Done Changelog ---- +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 * Added "dec" decode function * Added "enc" encode function diff --git a/src/deps.h b/src/deps.h index 6bda6a9..1606921 100644 --- a/src/deps.h +++ b/src/deps.h @@ -17,6 +17,8 @@ #define MAX_STRING_BUFSIZE (MAX_STRING_LEN + 1) #define MAX_VAR_DATA_LEN 2048 #define MAXVARS 2048 +#define MAX_CONCAT_BUF 2048 +#define MAX_READFILE_LEN 2097152 #define TOKEN '%' #define TOKEN_STR "%" #define NULLBYTE '\0' diff --git a/src/functions.c b/src/functions.c index 1f6b16f..3205588 100644 --- a/src/functions.c +++ b/src/functions.c @@ -16,6 +16,9 @@ char *process_line(char *line) { char *tok_srch; + static char concatbuf[MAX_CONCAT_BUF+1]; + static char filebuf[MAX_READFILE_LEN+1]; + tok_srch = strtok(line, "=\" |"); /* reuse function */ @@ -105,7 +108,8 @@ char *process_line(char *line) /* builtin md5 function */ else if(strncmp("md5",tok_srch,3) == 0) { - char *file_line, *file_md5_val; + char *file_line, *catfile, *file_md5_val; + tok_srch = strtok(NULL, "\""); if(strcmp(tok_srch, "\n") == 0 || strcmp(tok_srch, " \n") == 0 || tok_srch == NULL) @@ -113,8 +117,18 @@ char *process_line(char *line) file_line = parse_vars(tok_srch); file_md5_val = md5_file(file_line); - printf("ss:md5: %s, %s\n", file_md5_val, file_line); - return NULL; + catfile = ss_concat(file_md5_val, file_line); + if(strlen(catfile) < MAX_CONCAT_BUF) + { + strcpy(concatbuf, catfile); + free(catfile); + return concatbuf; + } + else + { + free(catfile); + return NULL; + } } /* system execute function */ @@ -179,8 +193,9 @@ char *process_line(char *line) /* read function */ else if(strncmp("read",tok_srch,4) == 0) { + unsigned long read_size; char read_line[1024]; - char *filename; + char *filename, *dynfile; FILE *read_file = NULL; /* strtok to filename of function */ @@ -193,9 +208,16 @@ char *process_line(char *line) that may be, and BAM, variable fill! */ filename = parse_vars(tok_srch); + // WERE LOADING FILES INTO MEMORY NOW! /* open file */ read_file = fopen(filename, "r"); + fseek(read_file, 0L, SEEK_END); + read_size = ftell(read_file); + rewind(read_file); + + dynfile = (char *)malloc(read_size + 1); + /* Check if file was opened successfully */ if(read_file == NULL) { @@ -204,11 +226,24 @@ char *process_line(char *line) while(fgets(read_line, sizeof(read_line), read_file) != NULL) { - printf("%s", read_line); + sprintf(dynfile, "%s", read_line); + } + + if(read_size <= MAX_READFILE_LEN) + { + sprintf(filebuf, "%s", dynfile); + } + else + { + syn_error("ss:error:read, file too large, 2MB max"); } fclose(read_file); - return NULL; + + // For now print to test // + free(dynfile); + + return strip_nl(filebuf); } /* read function */ else if(strncmp("delete", tok_srch, 6) == 0) { diff --git a/src/main.c b/src/main.c index 1670250..e680970 100644 --- a/src/main.c +++ b/src/main.c @@ -8,6 +8,8 @@ #include "deps.h" #include "util.h" #include "functions.h" +#include "pipe.h" +#include "vars.h" int main(int argc, char **argv) { @@ -37,11 +39,34 @@ 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; - return_dat = process_line(script_line); + char *return_dat, *pipechk, *return_pipe_dat; + pipechk = ss_piping(script_line); + + return_dat = process_line(script_line); + + // Blank line, getting up outta here. if(return_dat == NULL) continue; - // If return is not null, provide the return - printf("%s\n", return_dat); + + if(pipechk != NULL) + { + 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; + + // Print the pipe line + printf("%s\n", return_pipe_dat); + } + else + { + // If return is not null, and no pipe; provide the return + printf("%s\n", return_dat); + } } /* end of while */ } /* file null */ diff --git a/src/pipe.c b/src/pipe.c new file mode 100644 index 0000000..d2f3d59 --- /dev/null +++ b/src/pipe.c @@ -0,0 +1,36 @@ +/* + SlideScript - minimalistic top-down scripting language. + (C) Copyright 2014-2021 Chris Dorman - some rights reserved (GPLv2) + + View README file supplied with this software for more details +*/ + + +#include "deps.h" +#include "pipe.h" +#include "util.h" + +char *ss_piping(char *string) +{ + char *pipe_tok; + + char *stringmem; + + stringmem = (char *) malloc(strlen(string)+1); + if(stringmem == NULL) syn_error("ss:error:memory mapping error..."); + + sprintf(stringmem, "%s", string); + + pipe_tok = strtok(stringmem, "|"); + if(pipe_tok != NULL && strcmp(pipe_tok, string) !=0) + { + char *second_func; + pipe_tok = strtok(NULL, "|"); + second_func = pipe_tok + 1; + return second_func; + } + + free(stringmem); + + return NULL; +} diff --git a/src/pipe.h b/src/pipe.h new file mode 100644 index 0000000..5ee8976 --- /dev/null +++ b/src/pipe.h @@ -0,0 +1,9 @@ +/* + SlideScript - minimalistic top-down scripting language. + (C) Copyright 2014-2021 Chris Dorman - some rights reserved (GPLv2) + + View README file supplied with this software for more details +*/ + + +char *ss_piping(char *string); diff --git a/src/util.c b/src/util.c index 4a1ba9c..c6cfe2e 100644 --- a/src/util.c +++ b/src/util.c @@ -31,6 +31,13 @@ char *strip_nl (char *string) } } +char *ss_concat(char *str1, char *str2) +{ + char *dymem = (char *) malloc((strlen(str1)+strlen(str2)) + 3); + sprintf(dymem, "%s, %s", str1, str2); + return dymem; +} + void parse_args(int argc, char** argv) { /* -h flag given, show help */ diff --git a/src/util.h b/src/util.h index 43cc177..39f9855 100644 --- a/src/util.h +++ b/src/util.h @@ -7,4 +7,5 @@ void syn_error(char *message); char *strip_nl(char *string); +char *ss_concat(char *str1, char *str2); void parse_args(int argc, char** argv); diff --git a/test.ss b/test.ss index d5ae456..3802496 100755 --- a/test.ss +++ b/test.ss @@ -1,62 +1,76 @@ #!/usr/bin/slidescript -# This is a comment +############################################################# +#### 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! #### +############################################################# -### These are variables in SS -# Sets 'ss_filename' to 'file.txt' +# Slide Script, also refered to as SS, is extremely forgiving in +# syntax handling! +# Example: +# print "content" +# print "content"; +# 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 -# Sets 'ss_exec_command' to 'ls -al' -ss_exec_command=ls -al -# Sets 'ss_decoded' to 'Encrypt This Data!' -ss_decoded=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/][-_.,?! -ss_encoded2=defejihknknopqrqvutwzwz{|}DCHGFILILMNOPOTSRUXUXYZ[\[4`\0d//B$ -# Sets 'ss_encoded' to 'Ghfs~su#Yilv#Gduf$' -ss_encoded=Ghfs~su#Yilv#Gduf$ -# Sets 'ss_file_content' to 'This will be written to file.txt!' -ss_file_content=Encrypted data: %ss_encoded% +ss_stringdata=Data to encrypt and decrypt +ss_exec_command=echo SS executing echo from system shell -### The main program ### -# Prints 'Writing 'This will be written to file.txt!' -# to 'file.txt' -print "Sleeping before writing '%ss_file_content%' to '%ss_filename%'" +# 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! +enc "%ss_stringdata%" | write "%ss_filename%" "%PIPE%" +# You're left with file.txt, lets move on -# Pauses the script parse for x seconds, in this case 1. -sleep 1 +# Lets read the file we just created and show how SS handles its +# own decryption algorithm +read "%ss_filename%" | dec "%PIPE%" +# Will display the original variable string! -# Writes the content of ss_file_content to ss_filename -write "%ss_filename%" "%ss_file_content%" +# 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 -print "Getting md5 of %ss_filename%" +# Read md5 file +read "%ss_filename%.md5" -md5 "%ss_filename%" +write("test.txt", "testing data"); -print "Data written to %ss_filename%:" -# Reads data and prints to screen from ss_filename - -read "%ss_filename%" - -print "Deleting %ss_filename% after 1 second sleep..." -# Sleep again for the giggles :P -sleep 1 - -# Deletes file written previously from ss_filename +# 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" +# Gone! -print "Deleted %ss_filename%, executing '%ss_exec_command%' in 1 second" -# Sleep some more +print("Testing execution functions"); sleep 1 -# Executes the ss_exec_command variable data, 'ls -al' +# Execute function, SS can call unix system calls! +# Executes the ss_exec_command variable data, 'ls' exec "%ss_exec_command%" - -print "Encoding '%ss_decoded%' in 1 second" -# More snooze -sleep 1 - -# Encodes 'ss_encode' data -enc "%ss_decoded%" - -print "Decoding '%ss_encoded%'" -# Decodes 'ss_decode' data -dec "%ss_encoded%" - -dec "%ss_encoded2%"