From f03e656ecb1034cabec9c71ac159cdb84ac8172a Mon Sep 17 00:00:00 2001 From: qorg11 Date: Sat, 2 Jul 2022 13:57:59 +0200 Subject: [PATCH] Initial yacc commit --- .clangd | 2 ++ sakisafecli/.depend | 1 + sakisafecli/config.c | 56 +++++++++-------------------- sakisafecli/config.h | 24 +++++++++---- sakisafecli/config.y | 0 sakisafecli/parse.l | 18 ++++++++++ sakisafecli/parse.y | 48 +++++++++++++++++++++++++ sakisafecli/sakisafecli.c | 75 +++++++++++++-------------------------- 8 files changed, 128 insertions(+), 96 deletions(-) create mode 100644 .clangd create mode 100644 sakisafecli/.depend create mode 100644 sakisafecli/config.y create mode 100644 sakisafecli/parse.l create mode 100644 sakisafecli/parse.y diff --git a/.clangd b/.clangd new file mode 100644 index 0000000..5d0741b --- /dev/null +++ b/.clangd @@ -0,0 +1,2 @@ +CompileFlags: + Add: [-Wall, -I/usr/local/include] diff --git a/sakisafecli/.depend b/sakisafecli/.depend new file mode 100644 index 0000000..a690d43 --- /dev/null +++ b/sakisafecli/.depend @@ -0,0 +1 @@ +sakisafecli.full: /usr/lib/libc.a diff --git a/sakisafecli/config.c b/sakisafecli/config.c index 4e1cf10..ffd0a03 100644 --- a/sakisafecli/config.c +++ b/sakisafecli/config.c @@ -9,19 +9,19 @@ print_config() { puts("Current configuration:"); printf("Server: %s\n",server); - if(socks_proxy_flag) - printf("Socks proxy url: %s",socks_proxy_url); - if(http_proxy_flag) - printf("HTTP proxy url: %s",http_proxy_url); - if(silent_flag) + if(rc.socks_proxy_flag) + printf("Socks proxy url: %s",rc.socks_proxy_url); + if(rc.http_proxy_flag) + printf("HTTP proxy url: %s",rc.http_proxy_url); + if(rc.silent_flag) puts("Silent: true"); else puts("Silent: false"); - if(ipv6_flag) + if(rc.ipv6_flag) printf("Force IPv6: true\n"); else printf("Force IPv6: false\n"); - if(ipv4_flag) + if(rc.ipv4_flag) printf("Force IPv4: true\n"); else printf("Force IPv4: false\n"); @@ -30,38 +30,14 @@ print_config() } void -parse_config_file(FILE *config) +init_config(struct config *rc) { - - config_t runtime_config; - - config_init(&runtime_config); - config_read(&runtime_config, config); - config_setting_t *cur; - cur = config_lookup(&runtime_config, "server"); - if(config != NULL) { - if(cur != NULL) - server = (char *)config_setting_get_string(cur); - cur = config_lookup(&runtime_config, "socks_proxy"); - if(cur != NULL) - socks_proxy_url = (char *)config_setting_get_string(cur); - cur = config_lookup(&runtime_config, "http_proxy"); - if(cur != NULL) - http_proxy_url = (char *)config_setting_get_string(cur); - cur = config_lookup(&runtime_config, "use_socks_proxy"); - if(cur != NULL) - socks_proxy_flag = config_setting_get_bool(cur); - cur = config_lookup(&runtime_config, "use_http_proxy"); - if(cur != NULL) - http_proxy_flag = config_setting_get_bool(cur); - cur = config_lookup(&runtime_config, "silent"); - if(cur != NULL) - silent_flag = config_setting_get_bool(cur); - cur = config_lookup(&runtime_config, "force_ipv6"); - if(cur != NULL) - ipv6_flag = config_setting_get_bool(cur); - cur = config_lookup(&runtime_config, "force_ipv4"); - if(cur != NULL) - ipv4_flag = config_setting_get_bool(cur); - } + rc->http_proxy_flag = false; + rc->socks_proxy_flag = false; + rc->http_proxy_url = NULL; + rc->socks_proxy_url = NULL; + rc->ipv4_flag = false; + rc->ipv6_flag = false; + rc->silent_flag = false; + rc->server = "https://lainsafe.delegao.moe"; } diff --git a/sakisafecli/config.h b/sakisafecli/config.h index ede4bc2..79c568f 100644 --- a/sakisafecli/config.h +++ b/sakisafecli/config.h @@ -1,9 +1,21 @@ -#include +struct config +{ + bool ipv6_flag; + bool ipv4_flag; + bool http_proxy_flag; + bool socks_proxy_flag; + bool silent_flag; + bool paste_flag; + char *http_proxy_url; + char *socks_proxy_url; + char *server; +}; -/* Parse the config file */ -void -parse_config_file(FILE *config); +extern struct config rc; -/* Print the current settings */ +/* Init the config */ void -print_config(); +init_config(struct config *rc); + + + diff --git a/sakisafecli/config.y b/sakisafecli/config.y new file mode 100644 index 0000000..e69de29 diff --git a/sakisafecli/parse.l b/sakisafecli/parse.l new file mode 100644 index 0000000..c1c089f --- /dev/null +++ b/sakisafecli/parse.l @@ -0,0 +1,18 @@ +%{ +#include +#define YYSTYPE char* +#include "y.tab.h" +extern YYSTYPE yylval; +%} + +%% +server return SERVERTOK; +http_proxy return HPTOK; +socks_proxy return SPTOK; +use_socks_proxy return USPTOK; +use_http_proxy return UHPTOK; +force_ipv4 return IP4TOK; +force_ipv6 return IP6TOK; +\n /* ignore EOL */; +[a-zA-Z][a-zA-Z0-9]* yylval=strdup(yytext); return WORD; +%% diff --git a/sakisafecli/parse.y b/sakisafecli/parse.y new file mode 100644 index 0000000..867469e --- /dev/null +++ b/sakisafecli/parse.y @@ -0,0 +1,48 @@ +%{ +#include +#include "string.h" +#include +#include +#include "config.h" + + struct config rc; + int yyparse(void); + void yyerror(const char *str) { + fprintf(stderr,"error: %s\n",str); + _exit(-1); + } + %} + + +%token SERVERTOK HPTOK SPTOK USPTOK UHPTOK IP4TOK IP6TOK + + +%% +%% + +conf_statements: +| +conf_statements conf_statement SEMICOLON +; + +conf_statement: +statements +| +SERVERTOK quotedname +{ + rc.server = $2; +} + +quotedname: +QUOTE WORD QUOTE +{ + $$=$2; +} + +statements: +| statements statement +; + +statement: WORD | quotedname + +%% diff --git a/sakisafecli/sakisafecli.c b/sakisafecli/sakisafecli.c index 7e13da9..de5ef72 100644 --- a/sakisafecli/sakisafecli.c +++ b/sakisafecli/sakisafecli.c @@ -15,12 +15,7 @@ /* Config variables */ -bool ipv6_flag = false, ipv4_flag = false, http_proxy_flag = false, - socks_proxy_flag = false, silent_flag = false, paste_flag = false; - -char *http_proxy_url, *socks_proxy_url; - -config_t runtime_config; +struct config rc; char *server = "https://lainsafe.delegao.moe"; const char *path = ".cache/sakisafelinks"; @@ -35,7 +30,6 @@ main(int argc, char **argv) #endif char *form_key = "file"; - char *buffer = (char *)calloc(1024, sizeof(char)); if(buffer == NULL) { @@ -43,27 +37,9 @@ main(int argc, char **argv) return -1; } char config_location[512]; - char *sakisafeclirc_env = getenv("SAKISAFECLIRC"); + + init_config(&rc); - if(sakisafeclirc_env == NULL) { - snprintf(config_location, 512, "%s/.sakisafeclirc", getenv("HOME")); - FILE *fp = fopen(config_location, "r"); - if(fp != NULL) { - parse_config_file(fp); - fclose(fp); - } - } else { -#if defined(__OpenBSD__) || defined(__FreeBSD__) - strlcpy(config_location, sakisafeclirc_env, 512); -#else /* Linux sucks! */ - strncpy(config_location, sakisafeclirc_env, 512); -#endif - FILE *fp = fopen(config_location, "r"); - if(fp != NULL) { - parse_config_file(fp); - fclose(fp); - } - } /* libcurl initialization */ CURL *easy_handle = curl_easy_init(); @@ -105,7 +81,7 @@ main(int argc, char **argv) -1) { switch(c) { case 's': - server = optarg; + rc.server = optarg; break; case 'h': print_help(); @@ -114,26 +90,26 @@ main(int argc, char **argv) return 0; break; case 'p': - socks_proxy_url = optarg; - socks_proxy_flag = true; + rc.socks_proxy_url = optarg; + rc.socks_proxy_flag = true; break; case 'P': - http_proxy_url = optarg; - http_proxy_flag = true; + rc.http_proxy_url = optarg; + rc.http_proxy_flag = true; break; case 'S': - silent_flag = true; + rc.silent_flag = true; break; case '4': - ipv4_flag = true; + rc.ipv4_flag = true; break; case '6': - ipv6_flag = true; + rc.ipv6_flag = true; break; case 'x': /* We don't want the progress bar in this case */ - silent_flag = true; - paste_flag = true; + rc.silent_flag = true; + rc.paste_flag = true; break; case '?': print_usage(); @@ -149,7 +125,7 @@ main(int argc, char **argv) } } - if(access(argv[optind], F_OK) && !paste_flag) { + if(access(argv[optind], F_OK) && !rc.paste_flag) { fprintf(stderr, "Error opening file\n"); return -1; } @@ -161,23 +137,23 @@ main(int argc, char **argv) /* Proxy options */ - if(socks_proxy_flag && http_proxy_flag) { + if(rc.socks_proxy_flag && rc.http_proxy_flag) { fprintf(stderr, "Socks_Proxy and HTTP_PROXY can't be used at once\n"); return -1; - } else if(socks_proxy_flag) { - curl_easy_setopt(easy_handle, CURLOPT_PROXY, socks_proxy_url); + } else if(rc.socks_proxy_flag) { + curl_easy_setopt(easy_handle, CURLOPT_PROXY, rc.socks_proxy_url); curl_easy_setopt( easy_handle, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5_HOSTNAME); - } else if(http_proxy_flag) { - curl_easy_setopt(easy_handle, CURLOPT_PROXY, http_proxy_url); + } else if(rc.http_proxy_flag) { + curl_easy_setopt(easy_handle, CURLOPT_PROXY, rc.http_proxy_url); curl_easy_setopt(easy_handle, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); } /* Which address to use */ - if(ipv6_flag) + if(rc.ipv6_flag) curl_easy_setopt(easy_handle, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6); - else if(ipv4_flag) + else if(rc.ipv4_flag) curl_easy_setopt(easy_handle, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); else curl_easy_setopt( @@ -194,21 +170,21 @@ main(int argc, char **argv) file_data = curl_mime_addpart(mime); char *filename = argv[optind]; - if(paste_flag) + if(rc.paste_flag) filename = "/dev/stdin"; curl_mime_filedata(file_data, filename); curl_mime_name(file_data, form_key); - if(paste_flag) + if(rc.paste_flag) curl_mime_filename(file_data, "-"); - curl_easy_setopt(easy_handle, CURLOPT_NOPROGRESS, silent_flag); + curl_easy_setopt(easy_handle, CURLOPT_NOPROGRESS, rc.silent_flag); curl_easy_setopt(easy_handle, CURLOPT_PROGRESSFUNCTION, progress); curl_easy_setopt(easy_handle, CURLOPT_MIMEPOST, mime); curl_easy_perform(easy_handle); - if(!silent_flag) + if(!rc.silent_flag) putchar('\n'); puts(buffer); @@ -217,6 +193,5 @@ main(int argc, char **argv) curl_easy_cleanup(easy_handle); free(buffer); - config_destroy(&runtime_config); return 0; }