Mercurial > sci
view scid/main.c @ 80:71cd8447e3a4
misc: update copyright years
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 01 Feb 2023 13:14:46 +0100 |
parents | 85c59fbf1407 |
children |
line wrap: on
line source
/* * scid.c -- main scid(8) program file * * Copyright (c) 2021-2023 David Demelier <markand@malikania.fr> * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <unistd.h> #include "db.h" #include "http.h" #include "log.h" #include "scid.h" #include "util.h" static void usage(void) { fprintf(stderr, "usage: scid [-d database] [-s sock] [-t themedir] [command]\n"); exit(1); } static void help(void) { fprintf(stderr, "usage: scid\n"); fprintf(stderr, " scid api-get\n"); fprintf(stderr, " scid api-reset\n"); exit(1); } static void cmd_api_get(int argc, char **argv) { char key[SCID_API_KEY_MAX] = {0}; if (db_key_get(key, sizeof (key)) == 1) puts(key); } static void cmd_api_reset(int argc, char **argv) { char key[SCID_API_KEY_MAX] = {0}; if (db_key_init(key, sizeof (key)) == 0) printf("new key: %s\n", key); } static const struct { const char *name; void (*exec)(int, char **); } commands[] = { { "api-get", cmd_api_get }, { "api-reset", cmd_api_reset }, { NULL, NULL } }; int main(int argc, char **argv) { int ch; void (*run)(void) = &(http_cgi_run); while ((ch = util_getopt(argc, argv, "d:ft:")) != -1) { switch (ch) { case 'd': util_strlcpy(scid.dbpath, optarg, sizeof (scid.dbpath)); break; case 'f': run = &(http_fcgi_run); break; case 't': util_strlcpy(scid.themedir, optarg, sizeof (scid.themedir)); break; default: usage(); break; } } argc -= optind; argv += optind; /* Always logs first. */ scid_init(SCID_INIT_LOG); srand(time(NULL)); /* Check if there is a command provided, otherwise run as HTTP. */ if (argc == 0) { scid_init(SCID_INIT_DATABASE | SCID_INIT_THEME); log_info("scid: " VERSION); run(); } else { if (strcmp(argv[0], "help") == 0) help(); for (size_t i = 0; commands[i].name; ++i) { if (strcmp(commands[i].name, argv[0]) == 0) { scid_init(SCID_INIT_DATABASE); commands[i].exec(argc, argv); goto over; } } usage(); } over: scid_finish(); }