commit b25886ef13fbe21640650ab566e1b5a3fbd448bb Author: Pentium44 Date: Mon Apr 5 04:22:57 2021 -0700 Call this v0.1.1 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d9974c5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +slidescript +*.o +*.swp + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..40549a3 --- /dev/null +++ b/Makefile @@ -0,0 +1,29 @@ +# SlideScript makefile +# (C) Copyright 2014-2021 Chris Dorman, some rights reserved (CC-BY-SA 3.0) + +VERSION = \"0.1.1\" +EXTRA ?= dev +VERSION_EXTRA = \"$(EXTRA)\" + +PREFIX ?= /usr + +CC ?= cc +CFLAGS += -O2 -ansi -pedantic -g -Wall -Wextra --param=ssp-buffer-size=2 -fstack-protector-all +CPPFLAGS += -DVERSION=$(VERSION) -DVERSION_EXTRA=$(VERSION_EXTRA) -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 + +all: main + +fresh: clean all + +main: $(OBJECTS) + $(CC) $(OBJECTS) -o $(BIN) $(LDFLAGS) $(CFLAGS) + +clean: + rm -f $(OBJECTS) $(BIN) + +install: + install -D $(BIN) $(DESTDIR)/$(PREFIX)/bin/$(BIN) diff --git a/README.md b/README.md new file mode 100644 index 0000000..ca08042 --- /dev/null +++ b/README.md @@ -0,0 +1,47 @@ +SlideScript - v0.1.1-dev +A simple, user friendly scripting language. +==== + +Using SlideScript +---- +* Compile SS using the make command. +* Modify test.ss to learn the basics of SS. +* Run ./slidescript test.ss to execute the script. + +Documentation +---- +Here is a list of functions and features that SlideScript comes with +at this moment! +* Variable setting + variablename=variabledata +* Commenting! Examples: + # this is a comment +* Read and writing to files! Examples: + write "file.txt" "This will be written to file.txt" + read "file.txt" +* Print Example: + print "Hi everyone!" +* Sleep (Zzz) Example (sleeps for 2 seconds): + sleep 2 + +Todo list +---- +* Add in-script functions +* New static functions +* Network sockets + +Done +---- +* Read and write from file +* Some simple functions +* Variable support + +Changelog +---- + +V0.1.1 + * Added variable handling with a buffer size of 2MB per variable, and + cap of 2048 variables. + * Now operates under the shebang! + +(C) Copyright 2014 Chris Dorman, some rights reserved (CC-BY-SA 3.0) diff --git a/file.txt b/file.txt new file mode 100644 index 0000000..9f4b6d8 --- /dev/null +++ b/file.txt @@ -0,0 +1 @@ +This is a test file diff --git a/src/deps.h b/src/deps.h new file mode 100644 index 0000000..1099be9 --- /dev/null +++ b/src/deps.h @@ -0,0 +1,8 @@ +#include +#include +#include +#include + +#define MAX_VAR_NAME_LEN 128 +#define MAX_VAR_DATA_LEN 2048 +#define MAXVARS 2048 diff --git a/src/functions.c b/src/functions.c new file mode 100644 index 0000000..824940b --- /dev/null +++ b/src/functions.c @@ -0,0 +1,101 @@ + +#include "deps.h" +#include "functions.h" +#include "util.h" + +int process_line(char *line) +{ + char *tok_srch = strtok(line, "=\" "); + + /* if line starts with a comment, skip */ + if(strncmp("#",tok_srch,1) == 0) + { + return 0; + } + + /* print function */ + else if(strncmp("print",tok_srch,5) == 0) + { + tok_srch = strtok(NULL, "\""); + printf("%s\n", tok_srch); + } /* print function */ + + /* sleep function */ + else if(strncmp("sleep",tok_srch,5) == 0) + { + tok_srch = strtok(NULL, " "); + + /* if there is a new line, remove it */ + if(tok_srch[strlen(tok_srch)-1] == '\n') + { + tok_srch[strlen(tok_srch)-1] = 0; + } + sleep(atoi(tok_srch)); + } + + /* write */ + else if(strncmp("write",tok_srch,5) == 0) + { + FILE* write_file = NULL; + /* strtok to filename of function */ + tok_srch = strtok(NULL, "\""); + /* open file */ + write_file = fopen(tok_srch, "w"); + /* Check if file exists and can be opened */ + if(write_file == NULL) + { + printf("SlideScript: error: Cannot write to '%s'.\n", tok_srch); + return 1; + } + + /* strtok to the content that will be written to file */ + tok_srch = strtok(NULL, "\""); + tok_srch = strtok(NULL, "\""); + fprintf(write_file, "%s\n", tok_srch); + fclose(write_file); + } /* write function */ + + /* read function */ + else if(strncmp("read",tok_srch,4) == 0) + { + char read_line[1024]; + FILE *read_file = NULL; + + /* strtok to filename of function */ + tok_srch = strtok(NULL, "\""); + + /* open file */ + read_file = fopen(tok_srch, "r"); + + /* Check if file was opened successfully */ + if(read_file == NULL) + { + printf("SlideScript: error: failed to open '%s'.\n", tok_srch); + return 1; + } + + while(fgets(read_line, sizeof(read_line), read_file) != NULL) + { + printf("%s", read_line); + } + + fclose(read_file); + } /* read function */ + + else if(strcmp(tok_srch, "\n") != 0 && strcmp(tok_srch, "") != 0) { + if(tok_srch[strlen(tok_srch)-1] == '\n') + { + return 1; + } + else + { + int varc = get_var_count(); + char *varname_tmp = tok_srch; + tok_srch = strtok(NULL, "="); + set_var(varc, varname_tmp, tok_srch); + printf("ss: var '%s' -> %s", varname_tmp, get_var_data(varname_tmp)); + } + } + + return 0; +} diff --git a/src/functions.h b/src/functions.h new file mode 100644 index 0000000..e16a3cc --- /dev/null +++ b/src/functions.h @@ -0,0 +1 @@ +int process_line(char *line); diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..12eaa07 --- /dev/null +++ b/src/main.c @@ -0,0 +1,46 @@ +/* + SlideScript - Basic scripting for everyday use + (C) Copyright 2014-2021 Chris Dorman - some rights reserved (CC-BY-SA 3.0) + + View read me file supplied with this software +*/ + +#include "deps.h" +#include "util.h" +#include "functions.h" + +int main(int argc, char **argv) +{ + FILE* script; + char script_line[1024]; + + if (argc == 1) + { + script = stdin; + } + else + { + parse_args (argc, argv); + script = fopen (argv [1], "r"); + } + + /* check if config opens successfully */ + if(script == NULL) { + printf("Error: failed to open %s.\n", argv[1]); + exit(EXIT_FAILURE); + } + + /* Run while loop if file is empty. */ + if(script != NULL) + { + + /* parse each line from the script in order. */ + while(fgets(script_line, sizeof(script_line), script) != NULL) + { + process_line(script_line); + } /* end of while */ + } /* file null */ + + if (argc != 1 ) fclose(script); + exit(EXIT_SUCCESS); +} diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..d52cfde --- /dev/null +++ b/src/util.c @@ -0,0 +1,73 @@ +#include "deps.h" +#include "util.h" + +char *null = "SlideScript: error: Variable not found!\n"; + +/*#define tostring(x) #x*/ +void set_var(int index, char *varname, char *vardata) +{ + strcpy(svars[index].var_name, varname); + strcpy(svars[index].var_data, vardata); +} + +char *get_var_data(char *varname) +{ + int xx; + for(xx = 0; xx < MAXVARS; xx++) + { + char *varp = svars[xx].var_name; + if(*varp == '\0') continue; + if(strcmp(varp, varname) == 0) + { + return svars[xx].var_data; + } + } + return null; +} + +int get_var_count() +{ + int yy; + for(yy = 0; yy < MAXVARS; yy++) + { + if(strlen(svars[yy].var_name) > 0) + { + continue; + } + else + { + return yy+1; + } + } + return 0; +} + + + +void parse_args(int argc, char** argv) +{ + /* no arguments given */ + if(argc == 1) + { + printf("Error: no file supplied. use %s -h for help\n", argv[0]); + exit(EXIT_FAILURE); + } + /* -h flag given, show help */ + else if(argc == 2 && strncmp("-h", argv[1], 2) == 0) + { + printf("SlideScript - Simple, everyday use, programming script\n" + "Arguments:\n\n" + "-v\t Display version number\n" + "-h\t Display this help page\n" + "Usage: %s \n", argv[0]); + exit(EXIT_SUCCESS); + } + /* -v flag given, show version */ + else if(argc == 2 && strncmp("-v", argv[1], 2) == 0) + { + printf("SlideScript %s-%s\n", VERSION, VERSION_EXTRA); + exit(EXIT_SUCCESS); + } + +} + diff --git a/src/util.h b/src/util.h new file mode 100644 index 0000000..662e9d3 --- /dev/null +++ b/src/util.h @@ -0,0 +1,13 @@ +struct s_variables +{ + char var_name [MAX_VAR_NAME_LEN]; + char var_data [MAX_VAR_DATA_LEN]; +}; + +typedef struct s_variables VARS; +VARS svars[MAXVARS]; + +void set_var(int index, char *varname, char *vardata); +int get_var_count(); +char *get_var_data(char *varname); +void parse_args(int argc, char** argv); diff --git a/test.ss b/test.ss new file mode 100755 index 0000000..3f083ad --- /dev/null +++ b/test.ss @@ -0,0 +1,117 @@ +#!/usr/bin/slidescript +# This is a comment + +testvar1=set some data into a struct +testvar2=multiple variables work! +futurefeature3=add $testvar2 support :) +testvar1=set some data into a struct +testvar2=multiple variables work! +futurefeature3=add $testvar2 support :) +testvar1=set some data into a struct +testvar2=multiple variables work! +futurefeature3=add $testvar2 support :) +testvar1=set some data into a struct +testvar2=multiple variables work! +futurefeature3=add $testvar2 support :) +testvar1=set some data into a struct +testvar2=multiple variables work! +futurefeature3=add $testvar2 support :) +testvar1=set some data into a struct +testvar2=multiple variables work! +futurefeature3=add $testvar2 support :) +testvar1=set some data into a struct +testvar2=multiple variables work! +futurefeature3=add $testvar2 support :) +testvar1=set some data into a struct +testvar2=multiple variables work! +futurefeature3=add $testvar2 support :) +testvar1=set some data into a struct +testvar2=multiple variables work! +futurefeature3=add $testvar2 support :) +testvar1=set some data into a struct +testvar2=multiple variables work! +futurefeature3=add $testvar2 support :) +testvar1=set some data into a struct +testvar2=multiple variables work! +futurefeature3=add $testvar2 support :) +testvar1=set some data into a struct +testvar2=multiple variables work! +futurefeature3=add $testvar2 support :) +testvar1=set some data into a struct +testvar2=multiple variables work! +futurefeature3=add $testvar2 support :) +testvar1=set some data into a struct +testvar2=multiple variables work! +futurefeature3=add $testvar2 support :) +testvar1=set some data into a struct +testvar2=multiple variables work! +futurefeature3=add $testvar2 support :) +testvar1=set some data into a struct +testvar2=multiple variables work! +futurefeature3=add $testvar2 support :) +testvar1=set some data into a struct +testvar2=multiple variables work! +futurefeature3=add $testvar2 support :) +testvar1=set some data into a struct +testvar2=multiple variables work! +futurefeature3=add $testvar2 support :) +testvar1=set some data into a struct +testvar2=multiple variables work! +futurefeature3=add $testvar2 support :) +testvar1=set some data into a struct +testvar2=multiple variables work! +futurefeature3=add $testvar2 support :) +testvar1=set some data into a struct +testvar2=multiple variables work! +futurefeature3=add $testvar2 support :) +testvar1=set some data into a struct +testvar2=multiple variables work! +futurefeature3=add $testvar2 support :) +testvar1=set some data into a struct +testvar2=multiple variables work! +futurefeature3=add $testvar2 support :) +testvar1=set some data into a struct +testvar2=multiple variables work! +futurefeature3=add $testvar2 support :) +testvar1=set some data into a struct +testvar2=multiple variables work! +futurefeature3=add $testvar2 support :) +testvar1=set some data into a struct +testvar2=multiple variables work! +futurefeature3=add $testvar2 support :) +testvar1=set some data into a struct +testvar2=multiple variables work! +futurefeature3=add $testvar2 support :) +testvar1=set some data into a struct +testvar2=multiple variables work! +futurefeature3=add $testvar2 support :) +testvar1=set some data into a struct +testvar2=multiple variables work! +futurefeature3=add $testvar2 support :) +testvar1=set some data into a struct +testvar2=multiple variables work! +futurefeature3=add $testvar2 support :) +testvar1=set some data into a struct +testvar2=multiple variables work! +futurefeature3=add $testvar2 support :) +testvar1=set some data into a struct +testvar2=multiple variables work! +futurefeature3=add $testvar2 support :) +testvar1=set some data into a struct +testvar2=multiple variables work! +futurefeature3=add $testvar2 support :) +testvar1=set some data into a struct +testvar2=multiple variables work! +futurefeature3=add $testvar2 support :) +testvar1=set some data into a struct +testvar2=multiple variables work! +futurefeature3=add $testvar2 support :) +testvar1=set some data into a struct +testvar2=multiple variables work! +futurefeature3=add $testvar2 support :) + +print "Writing to file.txt in 1 second" +sleep 1 +write "file.txt" "This is a test file" +print "Reading from file.txt:" +read "file.txt"