From d4e38860710f8dc286669e4f57de96118abc2cea Mon Sep 17 00:00:00 2001 From: Pentium44 Date: Thu, 8 Apr 2021 14:52:56 -0700 Subject: [PATCH] First part of adding in backquoting for embedded functions within variables and other functions --- Makefile | 2 +- src/deps.h | 3 +- src/functions.c | 10 ++++-- src/inset.c | 93 +++++++++++++++++++++++++++++++++++++++++++++++++ src/inset.h | 24 +++++++++++++ src/math.c | 8 ++++- src/math.h | 8 ++++- 7 files changed, 142 insertions(+), 6 deletions(-) create mode 100644 src/inset.c create mode 100644 src/inset.h diff --git a/Makefile b/Makefile index 1e63702..0518bee 100644 --- a/Makefile +++ b/Makefile @@ -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 src/math.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 src/inset.o all: main diff --git a/src/deps.h b/src/deps.h index fcb4bfd..887033b 100644 --- a/src/deps.h +++ b/src/deps.h @@ -28,7 +28,7 @@ #define MAX_VAR_NAME_LEN 512 #define MAX_VAR_NAME_BUFSIZE (MAX_VAR_NAME_LEN + 1) -#define MAX_STRING_LEN 4096 +#define MAX_STRING_LEN 8128 #define MAX_STRING_BUFSIZE (MAX_STRING_LEN + 1) #define MAX_VAR_DATA_LEN 65536 #define MAX_DATA_BUFSIZE (MAX_VAR_DATA_LEN + 1) @@ -49,6 +49,7 @@ // END #define TOKEN '%' #define TOKEN_STR "%" +#define TOKEN_BQ '`' #define NULLBYTE '\0' #define ENCOFFSET 3 #define ENCSTEPODD 2 diff --git a/src/functions.c b/src/functions.c index 2458f85..8c7ccf7 100644 --- a/src/functions.c +++ b/src/functions.c @@ -13,11 +13,14 @@ #include "math.h" #include "md5.h" #include "network.h" +#include "inset.h" char *process_line(char *line) { char *tok_srch; + //printf("%s", &line[strlen(line)-2]); + static char concatbuf[MAX_CONCAT_BUF+1]; static char filebuf[MAX_READFILE_LEN+1]; @@ -96,7 +99,7 @@ char *process_line(char *line) 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) + 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) @@ -495,9 +498,12 @@ char *process_line(char *line) { int varc = get_var_count(); char *varname_tmp = tok_srch; + char *bq_check; tok_srch = strtok(NULL, "="); + // Check for back quotes, return string with backquotes processed + bq_check = strip_nl(parse_bq(tok_srch)); // Don't check if variable is blank, if so let it fly! - set_var(varc, varname_tmp, parse_vars(strip_nl(tok_srch))); + set_var(varc, varname_tmp, bq_check); /* printf("ss: var '%s' -> %s", varname_tmp, get_var_data(varname_tmp)); */ } } diff --git a/src/inset.c b/src/inset.c new file mode 100644 index 0000000..77aba32 --- /dev/null +++ b/src/inset.c @@ -0,0 +1,93 @@ +/* + 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 "inset.h" +#include "functions.h" +#include "vars.h" +#include "util.h" + +void s_inset(int index, char *function) +{ + strcpy(s_bqf[index].function, function); +} + +int get_bq_count() +{ + int yy; + for(yy = 0; yy < MAXVARS; yy++) + { + if(strlen(s_bqf[yy].function) > 0) + { + continue; + } + else + { + return yy; + } + } + return 0; +} + +char *parse_bq(char *string) +{ + int str_char, buf_len, var_len; + static char finished [MAX_STRING_BUFSIZE]; + char varbuffer [MAX_BQ_BUFSIZE]; + + /* String pointers */ + char *input_pointer; + char *output_pointer; + char *variable_pointer; + + *finished = NULLBYTE; + + for (input_pointer = string, output_pointer = finished; + (str_char = *input_pointer) != NULLBYTE; + input_pointer++) + { + if (str_char != TOKEN_BQ) + { *output_pointer++ = str_char; *output_pointer = NULLBYTE; continue; } + + variable_pointer = varbuffer; + do { *variable_pointer++ = (str_char = *++input_pointer); } + while ((str_char != NULLBYTE) && (str_char != TOKEN_BQ)); + + str_char = *input_pointer; + if ((str_char != NULLBYTE) && (str_char != TOKEN_BQ)) + syn_error("ss:error:backquote buffer out of sync"); + str_char = variable_pointer [-1]; + if ((str_char != NULLBYTE) && (str_char != TOKEN_BQ)) + syn_error("ss:error:backquote out of sync"); + + variable_pointer[-1] = NULLBYTE; + + if(*varbuffer == NULLBYTE) + syn_error("ss:error:backquote syntax error!"); + + // Add newline so other function processing doesn't miss end quotes + strcat (varbuffer, "\n"); + + variable_pointer = process_line(varbuffer); + if(variable_pointer == NULL) + syn_error("ss:error:backquoted function must return string!"); + + var_len = strlen(variable_pointer); + buf_len = strlen(finished) + var_len; + + if(buf_len > MAX_STRING_LEN) + syn_error("ss:error:string buffer overflow!"); + + strcat (finished, variable_pointer); + output_pointer += var_len; + if(*input_pointer == NULLBYTE) break; + } + + *output_pointer = NULLBYTE; + return finished; +} + diff --git a/src/inset.h b/src/inset.h new file mode 100644 index 0000000..1b8c5fa --- /dev/null +++ b/src/inset.h @@ -0,0 +1,24 @@ +/* + 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 +*/ + +// Inset backquote buffers +#define MAX_BACKQUOTE_LEN 6144 +#define MAX_BQ_BUFSIZE (MAX_BACKQUOTE_LEN + 1) +#define MAX_BQ_FUNCTIONS 16 + +struct s_bq +{ + char function [MAX_BQ_BUFSIZE]; +}; + +typedef struct s_bq BQ; +BQ s_bqf [MAX_BQ_FUNCTIONS]; + + +void s_set_bq(int index, char *function); +int get_bq_count(); +char *parse_bq(char *string); diff --git a/src/math.c b/src/math.c index 11ebbd1..c0ab665 100644 --- a/src/math.c +++ b/src/math.c @@ -1,4 +1,10 @@ -// expr.c, from suckless sbase. +/* + 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 "math.h" #include "util.h" diff --git a/src/math.h b/src/math.h index d912d6f..feb5b27 100644 --- a/src/math.h +++ b/src/math.h @@ -1,4 +1,10 @@ -// For SS +/* + 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 +*/ + struct s_expr { char arg[MAX_EXPR_LEN+1];