From 951d4aeb794707f1d4c571c6ea4b52190655e350 Mon Sep 17 00:00:00 2001 From: Pentium44 Date: Wed, 7 Apr 2021 17:59:55 -0700 Subject: [PATCH] Tweaks --- Makefile | 3 +- README.md | 147 ++++++++++++++++++++++--------------------- examples/index.html | 4 +- examples/net-http.ss | 9 ++- src/network.c | 48 +++++++++----- src/network.h | 2 +- src/util.c | 19 +++++- src/util.h | 3 +- 8 files changed, 139 insertions(+), 96 deletions(-) diff --git a/Makefile b/Makefile index 8f53985..cc50ee1 100644 --- a/Makefile +++ b/Makefile @@ -3,14 +3,13 @@ # Some changes and tweaks from Menchers VERSION = \"0.3.3\" -EXTRA ?= dev VERSION_EXTRA = \"$(EXTRA)\" PREFIX ?= /usr CC ?= gcc CFLAGS += -O2 -pedantic -g -Wall -Wextra --param=ssp-buffer-size=2 -fstack-protector-all -CPPFLAGS += -DVERSION=$(VERSION) -DVERSION_EXTRA=$(VERSION_EXTRA) -D_FORTIFY_SOURCE=2 +CPPFLAGS += -DVERSION=$(VERSION) -D_FORTIFY_SOURCE=2 LDFLAGS += -Wl,-O1,--sort-common,--hash-style=gnu,-z,relro BIN ?= slidescript diff --git a/README.md b/README.md index a1785d4..ae3d2de 100644 --- a/README.md +++ b/README.md @@ -1,108 +1,113 @@ SlideScript -==== -A simple, user friendly scripting language. === +A simple, user friendly scripting language. +===== Using SlideScript ----- +----- * Compile SS using the make command. * Install SS using make install. * Modify test.ss to learn the basics of SS. * Run ./test.ss to execute the script. Documentation ----- +----- Here is a list of functions and features that SlideScript comes with at this moment! -* Commenting! Examples: - * `# Comment is real in this one` + * Commenting! Examples: + * `# 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 and writing to flat files. Examples: + * `write "file.txt" "This will be written to file.txt"` + * `read "file.txt"` -* Delete example: - * `delete "file.txt"` + * Delete example: + * `delete "file.txt"` -* Execute example: - * `exec "ls -al"` + * Execute example: + * `exec "ls -al"` -* Print example: - * `print "Hi everyone!"` + * Print example: + * `print "Hi everyone!"` -* Sleep (Zzz) example (sleeps for 2 seconds): - * `sleep 2` + * Sleep (Zzz) example (sleeps for 2 seconds): + * `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. + * 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 - * `encrypt "Regular string"` -> Converts to encrypted string 'Uhjvqds#xuulqj' - * `decrypt "Uhjvqds#xuulqj"` -> Converts back to 'Regular string' + * 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.txt"` -> outputs filename and md5 hash + * 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 + * 2 Layer piping + * `md5 "file.txt" | write "file.txt.md5" "%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, forkval (0 or 1, 0 don't fork into background / 1 do). + +This will change rapidly as of currently, slidescript is in beavy development! -* 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). Todo list ----- -* Add in-script functions -* New static functions -* Network sockets -* Function piping between each other +------ + * Add in-script functions + * New static functions + * Loops, and if statements Done ----- -* Simple syntax checking and error reporting -* Support for linux system calls -* Read and write from file -* Some simple functions -* Shebang handling -* Variable support +------ + * 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 Changelog ----- -V0.3.3 - * Added first networking functions: netlisten, nettoss, nethttp. - * Embedded web server functionality - * Cleaned up code - * Improved syntax handling on functions +----- + * 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 - * 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.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 "decrypt" decode function - * Added "encrypt" encode function - * Added system "exec" function - * Added basic syntax handling, for a more uniform language + * 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 - * 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.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 - * Added variable handling with a buffer size of 2MB per variable, and - cap of 2048 variables. - * Now operates under the shebang! + * V0.1.1 + * Added variable handling with a buffer size of 2KB per variable, and cap of 2048 variables. + * Now operates under the shebang! -Contributions: +Contributions +----- Robert (OldCoder) Kiraly -> shebang support and string manipulations (C) Copyright 2014-2021 Chris Dorman, some rights reserved (GPLv2) diff --git a/examples/index.html b/examples/index.html index 7555cb8..9b3e050 100644 --- a/examples/index.html +++ b/examples/index.html @@ -1,6 +1,6 @@ -

Test page

-

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

+

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 index 91ea006..1c1aa6f 100755 --- a/examples/net-http.ss +++ b/examples/net-http.ss @@ -6,4 +6,11 @@ # * 0 = do not fork to background # * 1 = fork to background -nethttp "8080" "0" +port1=8081 +port2=8080 + +# Daemonize +nethttp "%port1%" "1" +nethttp "%port1%" "0" + +print "Server on %port1% still running in the background" diff --git a/src/network.c b/src/network.c index 6435f8e..63cf80f 100644 --- a/src/network.c +++ b/src/network.c @@ -31,6 +31,7 @@ struct { {"css", "text/css" }, {"c", "text/plain" }, {"h", "text/plain" }, + {"md", "text/plain" }, {"txt", "text/plain" }, {"ss", "text/plain" }, {"sh", "text/plain" }, @@ -161,7 +162,7 @@ void snet_toss(char *address, int port, char *string) } // Process connection of nethttp -void snet_http_process(int fd) +void snet_http_process(int fd, int forkval) { int j, file_fd, buflen, len; long i, filesize; @@ -173,7 +174,8 @@ void snet_http_process(int fd) if(filesize == 0 || filesize == -1) { sprintf(buffer,"HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"); if(write(fd,buffer,strlen(buffer)) < 0) syn_error("ss:net:http socket write error"); - sprintf(buffer,"

Error reading requested file.

"); + sprintf(buffer,"ss:http:missed header

301: Missed header call

" \ + "

Could not locate file



ss:http:%s
", VERSION); if(write(fd,buffer,strlen(buffer)) < 0) syn_error("ss:net:http socket write error"); exit(1); } @@ -191,9 +193,11 @@ void snet_http_process(int fd) } if(strncmp(buffer,"GET ",4) && strncmp(buffer,"get ",4)) { - sprintf(buffer,"HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"); + sprintf(buffer,"HTTP/1.0 501 Not Implemented\r\nContent-Type: text/html\r\n\r\n"); if(write(fd,buffer,strlen(buffer)) < 0) syn_error("ss:net:http socket write error"); - sprintf(buffer,"

Simple HTTP get support only.

"); + sprintf(buffer,"ss:http: server not implemented" \ + "

501: Not Implemented

ss:http only supports simple GET requests via HTTP/1.0

" \ + "

ss:http:%s
", VERSION); if(write(fd,buffer,strlen(buffer)) < 0) syn_error("ss:net:http socket write error"); exit(1); } @@ -212,7 +216,8 @@ void snet_http_process(int fd) if(buffer[j] == '.' && buffer[j+1] == '.') { sprintf(buffer,"HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"); if(write(fd,buffer,strlen(buffer)) < 0) syn_error("ss:net:http socket write error"); - sprintf(buffer,"

Parent directories (..) not supported

"); + sprintf(buffer,"ss:http: parent directory not available

FORBIDDEN: parent directory

" \ + "

You do not have access to view ../. Blocked.



ss:http:%s
", VERSION); if(write(fd,buffer,strlen(buffer)) < 0) syn_error("ss:net:http socket write error"); exit(1); } @@ -225,7 +230,8 @@ void snet_http_process(int fd) } else { sprintf(buffer,"HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"); if(write(fd,buffer,strlen(buffer)) < 0) syn_error("ss:net:http socket write error"); - sprintf(buffer,"

Index not found

"); + sprintf(buffer,"ss:http: file not found

File not found

" \ + "

Could not locate index.html



ss:http:%s
", VERSION); if(write(fd,buffer,strlen(buffer)) < 0) syn_error("ss:net:http socket write error"); exit(1); } @@ -245,14 +251,26 @@ void snet_http_process(int fd) { fstr = "application/octet-stream"; } - - printf("ss:net:http request %s, %s\n", &buffer[5], fstr); - + + if(forkval == 0) + printf("ss:net:http request %s, %s\n", &buffer[5], fstr); + + if(is_dir(&buffer[5]) == 1) + { + sprintf(buffer,"HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"); + if(write(fd,buffer,strlen(buffer)) < 0) syn_error("ss:net:http socket write error"); + sprintf(buffer,"ss:http: directory

Directory

" \ + "

No directory listings at this time...



ss:http:%s
", VERSION); + if(write(fd,buffer,strlen(buffer)) < 0) syn_error("ss:net:http socket write error"); + exit(1); + } + // See if the web server can open requested file from browser if(( file_fd = open(&buffer[5],O_RDONLY)) == -1) { - sprintf(buffer,"HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"); + sprintf(buffer,"HTTP/1.0 404 Not Found\r\nContent-Type: text/html\r\n\r\n"); if(write(fd,buffer,strlen(buffer)) < 0) syn_error("ss:net:http socket write error"); - sprintf(buffer,"

Error, file cound not be retrieved

"); + sprintf(buffer,"ss:http:file not found

404: File not found

" \ + "

Could not locate file



ss:http:%s
", VERSION); if(write(fd,buffer,strlen(buffer)) < 0) syn_error("ss:net:http socket write error"); exit(1); } @@ -285,7 +303,7 @@ void snet_http(int port, int forkval) if(forkval > 0) { if(fork() != 0) - syn_error("ss:net:http failed to fork to background"); + syn_error("ss:net:http forked"); signal(SIGCLD, SIG_IGN); signal(SIGHUP, SIG_IGN); @@ -295,8 +313,8 @@ void snet_http(int port, int forkval) } setpgrp(); - - printf("Starting httpd...\n"); + if(forkval < 1) + printf("Starting httpd...\n"); // Check if socket succeeded to open if((listenfd = socket(AF_INET, SOCK_STREAM,0)) <0) { @@ -336,7 +354,7 @@ void snet_http(int port, int forkval) else { if(pid == 0) { close(listenfd); - snet_http_process(socketfd); + snet_http_process(socketfd, forkval); } else { close(socketfd); } diff --git a/src/network.h b/src/network.h index 4f94c4c..dddb9df 100644 --- a/src/network.h +++ b/src/network.h @@ -13,5 +13,5 @@ void snet_process_connection(int sockfd, char *srch, char *resp); void snet_listen(int port, char *srch, char *resp); void snet_toss(char *address, int port, char *string); void snet_process(int sockfd); -void snet_http_process(int fd); +void snet_http_process(int fd, int forkval); void snet_http(int port, int fork); diff --git a/src/util.c b/src/util.c index 76e046c..c7cdb2a 100644 --- a/src/util.c +++ b/src/util.c @@ -32,9 +32,22 @@ char *strip_nl (char *string) } // Check if a file exists, return code -int file_exists(char *fname) { +int file_exists(char *path) { struct stat buffer; - return (stat (fname, &buffer) == 0); + return (stat (path, &buffer) == 0); +} + +int is_dir(char *path) +{ + struct stat buffer; + + stat(path, &buffer); + + // Check for file existence + if (S_ISDIR(buffer.st_mode)) + return 1; + + return 0; } char *ss_concat(char *str1, char *str2) @@ -59,7 +72,7 @@ void parse_args(int argc, char** argv) /* -v flag given, show version */ else if(argc == 2 && strncmp("-v", argv[1], 2) == 0) { - printf("SlideScript %s-%s, Chris Dorman (cddo@riseup.net), 2021\n", VERSION, VERSION_EXTRA); + printf("SlideScript %s, Chris Dorman (cddo@riseup.net), 2021\n", VERSION); exit(EXIT_SUCCESS); } diff --git a/src/util.h b/src/util.h index 1c24620..33c49b2 100644 --- a/src/util.h +++ b/src/util.h @@ -7,6 +7,7 @@ void syn_error(char *message); char *strip_nl(char *string); -int file_exists(char *fname); +int file_exists(char *path); +int is_dir(char *path); char *ss_concat(char *str1, char *str2); void parse_args(int argc, char** argv);