Initial yacc commit

This commit is contained in:
(≧◡≦) 2022-07-02 13:57:59 +02:00
parent 64767aa30d
commit f03e656ecb
8 changed files with 128 additions and 96 deletions

2
.clangd Normal file
View file

@ -0,0 +1,2 @@
CompileFlags:
Add: [-Wall, -I/usr/local/include]

1
sakisafecli/.depend Normal file
View file

@ -0,0 +1 @@
sakisafecli.full: /usr/lib/libc.a

View file

@ -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";
}

View file

@ -1,9 +1,21 @@
#include <libconfig.h>
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);

0
sakisafecli/config.y Normal file
View file

18
sakisafecli/parse.l Normal file
View file

@ -0,0 +1,18 @@
%{
#include <stdio.h>
#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;
%%

48
sakisafecli/parse.y Normal file
View file

@ -0,0 +1,48 @@
%{
#include <stdio.h>
#include "string.h"
#include <stdlib.h>
#include <unistd.h>
#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
%%

View file

@ -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;
}