summaryrefslogtreecommitdiff
path: root/src/main.c
blob: cdcec31a43992a6c86f17bd00fd2199385b42306 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* Copyright (c) 2015-2016 Yoran Heling

  Permission is hereby granted, free of charge, to any person obtaining
  a copy of this software and associated documentation files (the
  "Software"), to deal in the Software without restriction, including
  without limitation the rights to use, copy, modify, merge, publish,
  distribute, sublicense, and/or sell copies of the Software, and to
  permit persons to whom the Software is furnished to do so, subject to
  the following conditions:

  The above copyright notice and this permission notice shall be included
  in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include <fcgy.h>
#include <yopt.h>

static fcgy_app *app; /* TODO: Needs to be a global array of apps */
static ev_signal termsig, intsig;


static const yopt_opt_t cli_options[] = {
#define X(N, n) { FCGY_CONFIG_GLOBAL_##N, 1, "--"n },
    FCGY_CONFIG_GLOBAL
#undef X
#define X(N, n) { FCGY_CONFIG_APP_##N, 1, "--"n },
    FCGY_CONFIG_APP
#undef X
#define X(N, n, a) { FCGY_CONFIG_CLI_##N, a, n },
    FCGY_CONFIG_CLI
#undef X
    {0,0,NULL}
};


static int parse_args(int argc, char **argv) {
    int v;
    char *val;
    yopt_t opts;
    yopt_init(&opts, argc, argv, cli_options);

    while((v = yopt_next(&opts, &val)) >= 0) {
        if(FCGY_CONFIG_IS_APP(v)) {
            if(app_config(app, v, val, opts.errbuf, sizeof(opts.errbuf)) < 0) {
                fprintf(stderr, "fcgy: %s\n", opts.errbuf);
                return -1;
            }
            continue;
        }
        /* TODO: Handle global options */
        switch(v) {
        case FCGY_CONFIG_CLI_VERSION:
            printf("fcgy %s\n", fcgy_version);
            exit(0);
        case FCGY_CONFIG_CLI_HELP:
            /* Very helpful */
            printf("fcgy [options]\n");
            exit(0);
        default:
            assert(0 && "Unhandled option");
        }
    }

    if(v == -2) {
        fprintf(stderr, "fcgy: %s\n", val);
        return -1;
    }

    /* TODO: Some sanity checking (e.g. do we have an app front and backend?) */
    return 0;
}


static void shutdown_sig(EV_P_ ev_signal *w, int revents) {
    ev_signal_stop(EV_DEFAULT_UC_ &termsig);
    ev_signal_stop(EV_DEFAULT_UC_ &intsig);
    app_destroy(app);
}


int main(int argc, char **argv) {
    app = app_create();
    if(parse_args(argc, argv) < 0)
        return 1;

    ev_default_loop(0);

    char ebuff[256];
    if(app_bind(app, ebuff, sizeof(ebuff)) < 0) {
        fprintf(stderr, "fcgy: %s\n", ebuff);
        return 1;
    }
    app_run(app);

    ev_signal_init(&termsig, shutdown_sig, SIGTERM);
    ev_signal_init(&intsig, shutdown_sig, SIGINT);
    ev_signal_start(EV_DEFAULT_UC_ &termsig);
    ev_signal_start(EV_DEFAULT_UC_ &intsig);

    ev_run(EV_DEFAULT_UC_ 0);
    fprintf(stderr, "Clean shutdown.\n");
    return 0;
}