Use getopt_long

This commit is contained in:
2022-12-15 11:26:40 +01:00
parent 3ce72975b8
commit 3227e615fe
4 changed files with 43 additions and 33 deletions

View File

@ -254,8 +254,7 @@ int client_request_handler(client_ctx_t *cctx, sock *client, unsigned long clien
goto respond; goto respond;
} }
ret = uri_cache_init(&uri); if ((ret = cache_init_uri(&uri)) != 0) {
if (ret != 0) {
res.status = http_get_status(500); res.status = http_get_status(500);
sprintf(err_msg, "Unable to communicate with internal file cache."); sprintf(err_msg, "Unable to communicate with internal file cache.");
goto respond; goto respond;

View File

@ -25,16 +25,17 @@ int cache_continue = 1;
magic_t magic; magic_t magic;
cache_entry *cache; cache_entry *cache;
int magic_init(void) { static int magic_init(void) {
magic = magic_open(MAGIC_MIME); if ((magic = magic_open(MAGIC_MIME)) == NULL) {
if (magic == NULL) {
critical("Unable to open magic cookie"); critical("Unable to open magic cookie");
return -1; return 1;
} }
if (magic_load(magic, CACHE_MAGIC_FILE) != 0) { if (magic_load(magic, CACHE_MAGIC_FILE) != 0) {
critical("Unable to load magic cookie: %s", magic_error(magic)); critical("Unable to load magic cookie: %s", magic_error(magic));
return -2; return 1;
} }
return 0; return 0;
} }
@ -288,7 +289,7 @@ int cache_unload(void) {
return 0; return 0;
} }
int cache_update_entry(int entry_num, const char *filename, const char *webroot) { static int cache_update_entry(int entry_num, const char *filename, const char *webroot) {
void *cache_ro = cache; void *cache_ro = cache;
int shm_id = shmget(CACHE_SHM_KEY, 0, 0); int shm_id = shmget(CACHE_SHM_KEY, 0, 0);
void *shm_rw = shmat(shm_id, NULL, 0); void *shm_rw = shmat(shm_id, NULL, 0);
@ -364,7 +365,7 @@ int cache_filename_comp_invalid(const char *filename) {
return 0; return 0;
} }
int uri_cache_init(http_uri *uri) { int cache_init_uri(http_uri *uri) {
if (uri->filename == NULL) { if (uri->filename == NULL) {
return 0; return 0;
} }

View File

@ -31,8 +31,6 @@ extern cache_entry *cache;
extern int cache_continue; extern int cache_continue;
int magic_init(void);
void cache_process_term(int _); void cache_process_term(int _);
int cache_process(void); int cache_process(void);
@ -41,10 +39,8 @@ int cache_init(void);
int cache_unload(void); int cache_unload(void);
int cache_update_entry(int entry_num, const char *filename, const char *webroot);
int cache_filename_comp_invalid(const char *filename); int cache_filename_comp_invalid(const char *filename);
int uri_cache_init(http_uri *uri); int cache_init_uri(http_uri *uri);
#endif //SESIMOS_CACHE_H #endif //SESIMOS_CACHE_H

View File

@ -19,6 +19,7 @@
#include "lib/utils.h" #include "lib/utils.h"
#include <stdio.h> #include <stdio.h>
#include <getopt.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <signal.h> #include <signal.h>
#include <unistd.h> #include <unistd.h>
@ -145,7 +146,7 @@ void terminate_gracefully(int sig) {
exit(0); exit(0);
} }
int main(int argc, const char *argv[]) { int main(int argc, char *const argv[]) {
const int YES = 1; const int YES = 1;
struct pollfd poll_fds[NUM_SOCKETS]; struct pollfd poll_fds[NUM_SOCKETS];
int ready_sockets_num; int ready_sockets_num;
@ -171,32 +172,45 @@ int main(int argc, const char *argv[]) {
} }
printf("Sesimos web server " SERVER_VERSION "\n"); printf("Sesimos web server " SERVER_VERSION "\n");
static const struct option long_opts[] = {
{"help", no_argument, 0, 'h'},
{"config", required_argument, 0, 'c'},
{ 0, 0, 0, 0 }
};
config_file = NULL; config_file = NULL;
for (int i = 1; i < argc; i++) { int c, opt_idx;
const char *arg = argv[i]; while ((c = getopt_long(argc, argv, "hc:", long_opts, &opt_idx)) != -1) {
if (strcmp(arg, "-h") == 0 || strcmp(arg, "--help") == 0) { switch (c) {
printf("Usage: sesimos [-h] [-c <CONFIG-FILE>]\n" case 'h':
"\n" fprintf(stderr,
"Options:\n" "Usage: sesimos [-h] [-c <CONFIG FILE>]\n"
" -c, --config <CONFIG-FILE> path to the config file. If not provided, default will be used\n" "\n"
" -h, --help print this dialogue\n"); "Options:\n"
return 0; " -c, --config <CONFIG-FILE> path to the config file. If not provided, default will be used\n"
} else if (strcmp(arg, "-c") == 0 || strcmp(arg, "--config") == 0) { " -h, --help print this dialogue\n");
if (i == argc - 1) { return 0;
critical("Unable to parse argument %s, usage: --config <CONFIG-FILE>", arg); case 'c':
config_file = optarg;
break;
case '?':
default:
critical("Unable to parse arguments");
return 1; return 1;
}
config_file = argv[++i];
} else {
critical("Unable to parse argument '%s'", arg);
return 1;
} }
} }
if (optind != argc) {
critical("No positional arguments expected");
return 1;
}
if (config_load(config_file == NULL ? DEFAULT_CONFIG_FILE : config_file) != 0) if (config_load(config_file == NULL ? DEFAULT_CONFIG_FILE : config_file) != 0)
return 1; return 1;
if ((sockets[0] = socket(AF_INET6, SOCK_STREAM, 0)) == - 1 || (sockets[1] = socket(AF_INET6, SOCK_STREAM, 0)) == -1) { if ((sockets[0] = socket(AF_INET6, SOCK_STREAM, 0)) == -1 ||
(sockets[1] = socket(AF_INET6, SOCK_STREAM, 0)) == -1)
{
critical("Unable to create socket"); critical("Unable to create socket");
return 1; return 1;
} }