chiark / gitweb /
test path fixes
[disorder] / server / stats.c
1 /*
2  * This file is part of DisOrder
3  * Copyright (C) 2007 Richard Kettlewell
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA
19  */
20
21 #include <config.h>
22 #include "types.h"
23
24 #include <locale.h>
25 #include <stdio.h>
26 #include <errno.h>
27 #include <pcre.h>
28 #include <getopt.h>
29 #include <syslog.h>
30
31 #include "defs.h"
32 #include "mem.h"
33 #include "log.h"
34 #include "syscalls.h"
35 #include "configuration.h"
36 #include "trackdb.h"
37
38 static const struct option options[] = {
39   { "help", no_argument, 0, 'h' },
40   { "version", no_argument, 0, 'V' },
41   { "config", required_argument, 0, 'c' },
42   { "debug", no_argument, 0, 'd' },
43   { "no-debug", no_argument, 0, 'D' },
44   { "syslog", no_argument, 0, 's' },
45   { "no-syslog", no_argument, 0, 'S' },
46   { 0, 0, 0, 0 }
47 };
48
49 /* display usage message and terminate */
50 static void help(void) {
51   xprintf("Usage:\n"
52           "  disorder-stats [OPTIONS]\n"
53           "Options:\n"
54           "  --help, -h               Display usage message\n"
55           "  --version, -V            Display version number\n"
56           "  --config PATH, -c PATH   Set configuration file\n"
57           "  --[no-]debug, -d         Turn on (off) debugging\n"
58           "  --[no-]syslog            Force logging\n"
59           "\n"
60           "Generate DisOrder database statistics.\n");
61   xfclose(stdout);
62   exit(0);
63 }
64
65 /* display version number and terminate */
66 static void version(void) {
67   xprintf("disorder-stats version %s\n", disorder_version_string);
68   xfclose(stdout);
69   exit(0);
70 }
71
72 int main(int argc, char **argv) {
73   int n, logsyslog = !isatty(2);
74   char **stats;
75
76   set_progname(argv);
77   mem_init();
78   if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale");
79   while((n = getopt_long(argc, argv, "hVc:dDSs", options, 0)) >= 0) {
80     switch(n) {
81     case 'h': help();
82     case 'V': version();
83     case 'c': configfile = optarg; break;
84     case 'd': debugging = 1; break;
85     case 'D': debugging = 0; break;
86     case 'S': logsyslog = 0; break;
87     case 's': logsyslog = 1; break;
88     default: fatal(0, "invalid option");
89     }
90   }
91   if(logsyslog) {
92     openlog(progname, LOG_PID, LOG_DAEMON);
93     log_default = &log_syslog;
94   }
95   if(config_read(0))
96     fatal(0, "cannot read configuration");
97   trackdb_init(0);
98   trackdb_open();
99   stats = trackdb_stats(0);
100   while(*stats)
101     xprintf("%s\n", *stats++);
102   xfclose(stdout);
103   return 0;
104 }
105
106
107 /*
108 Local Variables:
109 c-basic-offset:2
110 comment-column:40
111 fill-column:79
112 indent-tabs-mode:nil
113 End:
114 */