comparison scid/main.c @ 18:600204c31bf0

misc: refactor
author David Demelier <markand@malikania.fr>
date Tue, 12 Jul 2022 20:20:51 +0200
parents
children 7e10cace67a3
comparison
equal deleted inserted replaced
17:40fe70256fb0 18:600204c31bf0
1 /*
2 * scid.c -- main scid(8) program file
3 *
4 * Copyright (c) 2021 David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <limits.h>
20 #include <stdio.h>
21 #include <stdnoreturn.h>
22 #include <string.h>
23 #include <unistd.h>
24
25 #include "db.h"
26 #include "log.h"
27 #include "http.h"
28
29 static char dbpath[PATH_MAX] = VARDIR "/db/sci/sci.db";
30
31 noreturn static void
32 usage(void)
33 {
34 fprintf(stderr, "usage: %s [-d database] [-s sock]\n", getprogname());
35 exit(1);
36 }
37
38 static void
39 init(void)
40 {
41 log_open("scid");
42 log_info("opening database %s", dbpath);
43
44 if (db_open(dbpath) < 0)
45 log_die("abort: unable to open database");
46 }
47
48 static void
49 finish(void)
50 {
51 db_finish();
52 log_finish();
53 }
54
55 int
56 main(int argc, char **argv)
57 {
58 int ch;
59 void (*run)(void) = &(http_cgi_run);
60
61 while ((ch = getopt(argc, argv, "d:f")) != -1) {
62 switch (ch) {
63 case 'd':
64 strlcpy(dbpath, optarg, sizeof (dbpath));
65 break;
66 case 'f':
67 run = &(http_fcgi_run);
68 break;
69 default:
70 usage();
71 break;
72 }
73 }
74
75 argc -= optind;
76 argv += optind;
77
78 init();
79
80 for (;;)
81 run();
82
83 finish();
84 }