From 1f91a4eb2cd24e6b1bbc4a89c55241d262d85d3f Mon Sep 17 00:00:00 2001
From: qorg11 <teru-sama@riseup.net>
Date: Sat, 2 Jul 2022 15:54:12 +0200
Subject: [PATCH] Updated the thing so the config file uses yacc

---
 sakisafecli/Makefile      |  8 ++++++-
 sakisafecli/config.c      |  8 ++++++-
 sakisafecli/config.h      |  9 ++++---
 sakisafecli/parse.l       | 16 ++++++-------
 sakisafecli/parse.y       | 50 ++++++++++++++++++++++++++++++++++-----
 sakisafecli/sakisafecli.c |  7 ++----
 6 files changed, 74 insertions(+), 24 deletions(-)

diff --git a/sakisafecli/Makefile b/sakisafecli/Makefile
index 6a2de40..7862ab4 100644
--- a/sakisafecli/Makefile
+++ b/sakisafecli/Makefile
@@ -1,8 +1,14 @@
 PROG   = sakisafecli
-SRCS  += funcs.c sakisafecli.c config.c
+SRCS   = parse.y
+SRCS  += funcs.c sakisafecli.c config.c lex.yy.c
 MAN    = sakisafecli.1 sakisafeclirc.5
 LDADD  = -lssl -lz -lpthread -lnghttp2 -lcurl -lconfig -lcrypto -L/usr/local/lib
+YFLAGS = -d
 PREFIX = /usr/local
 BINDIR = $(PREFIX)/bin
 MANDIR = $(PREFIX)/man/man
+
+lex.yy.c:
+	lex parse.l
+
 .include <bsd.prog.mk>
diff --git a/sakisafecli/config.c b/sakisafecli/config.c
index ffd0a03..bb0539b 100644
--- a/sakisafecli/config.c
+++ b/sakisafecli/config.c
@@ -8,7 +8,7 @@ void
 print_config()
 {
 	puts("Current configuration:");
-	printf("Server: %s\n",server);
+	printf("Server: %s\n",rc.server);
 	if(rc.socks_proxy_flag)
 		printf("Socks proxy url: %s",rc.socks_proxy_url);
 	if(rc.http_proxy_flag)
@@ -41,3 +41,9 @@ init_config(struct config *rc)
 	rc->silent_flag = false;
 	rc->server = "https://lainsafe.delegao.moe";
 }
+void
+load_config()
+{
+	yyin = fopen("/usr/home/qorg/.sakisafeclirc","r");
+	yyparse();
+}
diff --git a/sakisafecli/config.h b/sakisafecli/config.h
index 79c568f..b9048e8 100644
--- a/sakisafecli/config.h
+++ b/sakisafecli/config.h
@@ -1,3 +1,6 @@
+#include <stdbool.h>
+#include <stdio.h>
+
 struct config
 {
 	bool ipv6_flag;
@@ -12,10 +15,10 @@ struct config
 };
 
 extern struct config rc;
-
+extern FILE *yyin;
 /* Init the config */
 void
 init_config(struct config *rc);
 
-
-
+void
+load_config();
diff --git a/sakisafecli/parse.l b/sakisafecli/parse.l
index c1c089f..051c64e 100644
--- a/sakisafecli/parse.l
+++ b/sakisafecli/parse.l
@@ -1,18 +1,18 @@
 %{
 #include <stdio.h>
 #define YYSTYPE char*
-#include "y.tab.h"
+#include "parse.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 */;
+ipv4                    return IP4TOK;
+ipv6                    return IP6TOK;
 [a-zA-Z][a-zA-Z0-9]*    yylval=strdup(yytext); return WORD;
+[a-zA-Z0-9\/.-]+        yylval=strdup(yytext); return URL;
+\n                      /* ignore EOL */;
+[ \t]+
+\"                     return QUOTE;
+;                      return SEMICOLON;
 %%
diff --git a/sakisafecli/parse.y b/sakisafecli/parse.y
index 867469e..3ad578b 100644
--- a/sakisafecli/parse.y
+++ b/sakisafecli/parse.y
@@ -4,20 +4,25 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include "config.h"
-
+#define YYSTYPE char*
     struct config rc;
     int yyparse(void);
     void yyerror(const char *str) {
-	   fprintf(stderr,"error: %s\n",str);
+	   fprintf(stderr,"Error reading config file: %s\n",str);
 	   _exit(-1);
     }
-	   %}
+
+    int yywrap()
+    {
+	   return 1;
+    }
+
+%}
 
 
-%token SERVERTOK HPTOK SPTOK USPTOK UHPTOK IP4TOK IP6TOK
+%token SERVERTOK HPTOK SPTOK USPTOK UHPTOK IP4TOK IP6TOK QUOTE URL SEMICOLON WORD
 
 
-%%
 %%
 
 conf_statements:
@@ -28,12 +33,45 @@ conf_statements conf_statement SEMICOLON
 conf_statement:
 statements
 |
+IP4TOK quotedword
+{
+    if(!strcmp($2,"true"))
+	   rc.ipv4_flag = true;
+    else
+	   rc.ipv4_flag = false;
+}
+|
+IP6TOK quotedword
+{
+    if(!strcmp($2,"true"))
+	   rc.ipv6_flag = true;
+    else
+	   rc.ipv6_flag = false;
+}
+|
 SERVERTOK quotedname
 {
     rc.server = $2;
 }
+|
+HPTOK quotedname
+{
+    rc.http_proxy_url = $2;
+}
+|
+SPTOK quotedname
+{
+    rc.socks_proxy_url = $2;
+}
+;
 
 quotedname:
+QUOTE URL QUOTE
+{
+    $$=$2;
+}
+
+quotedword:
 QUOTE WORD QUOTE
 {
     $$=$2;
@@ -43,6 +81,6 @@ statements:
 | statements statement
 ;
 
-statement: WORD | quotedname
+statement: URL | quotedname | quotedword
 
 %%
diff --git a/sakisafecli/sakisafecli.c b/sakisafecli/sakisafecli.c
index de5ef72..19e796b 100644
--- a/sakisafecli/sakisafecli.c
+++ b/sakisafecli/sakisafecli.c
@@ -15,9 +15,6 @@
 
 /* Config variables */
 
-struct config rc;
-
-char *server = "https://lainsafe.delegao.moe";
 const char *path = ".cache/sakisafelinks";
 int
 main(int argc, char **argv)
@@ -39,7 +36,7 @@ main(int argc, char **argv)
 	char config_location[512];
 	
 	init_config(&rc);
-
+	load_config();
 	/* libcurl initialization */
 
 	CURL *easy_handle = curl_easy_init();
@@ -133,7 +130,7 @@ main(int argc, char **argv)
 	/* curl options */
 	curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, write_data);
 	curl_easy_setopt(easy_handle, CURLOPT_WRITEDATA, buffer);
-	curl_easy_setopt(easy_handle, CURLOPT_URL, server);
+	curl_easy_setopt(easy_handle, CURLOPT_URL, rc.server);
 
 	/* Proxy options */