int main(int argc, char *argv[]) { char** hosts; char* hostname; char* user; char* name; int i, c = 0; uv_interface_address_t* addresses; uv_err_t err; srand(time(NULL)); atexit(forza__kill); signal(SIGTERM, forza__on_sigterm); loop = uv_default_loop(); #ifdef FORZA_VERSION_HASH printf("forza "FORZA_VERSION_HASH"\n"); #else printf("forza\n"); #endif opt = saneopt_init(argc - 1, argv + 1); saneopt_alias(opt, "host", "h"); hosts = saneopt_get_all(opt, "host"); hostname = saneopt_get(opt, "hostname"); user = saneopt_get(opt, "app-user"); name = saneopt_get(opt, "app-name"); arguments = saneopt_arguments(opt); if (hostname == NULL) { hostname = malloc(256 * sizeof(*hostname)); err = uv_interface_addresses(&addresses, &c); if (err.code != UV_OK) { fprintf(stderr, "uv_interface_addresses: %s\n", uv_err_name(uv_last_error(loop))); return 1; } for (i = 0; i < c; i++) { /* For now, only grab the first non-internal, non 0.0.0.0 interface. * TODO: Make this smarter. */ if (addresses[i].is_internal) continue; uv_ip4_name(&addresses[i].address.address4, hostname, 255 * sizeof(*hostname)); if (strcmp(hostname, "0.0.0.0") != 0) break; } uv_free_interface_addresses(addresses, c); } forza_connect(hosts, hostname, user, name, on_connect); uv_run(loop, UV_RUN_DEFAULT); free(hosts); return 0; }
void test_stop_after_arguments() { printf("test_stop_after_arguments()\n"); char** argv = malloc(5 * sizeof(char*)); argv[0] = "--option"; argv[1] = "value"; argv[2] = "--"; argv[3] = "--next-option"; argv[4] = "value"; saneopt_t* opt = saneopt_init(5, argv); assert(strcmp(saneopt_get(opt, "option"), "value") == 0); assert(saneopt_get(opt, "next-option") == NULL); free(argv); free(opt); }
void test_no_arg() { printf("test_no_arg()\n"); char** argv; saneopt_t* opt = saneopt_init(0, argv); assert(saneopt_get(opt, "no-option") == NULL); free(opt); }
void test_value() { printf("test_value()\n"); char** argv = malloc(6 * sizeof(char*)); argv[0] = "--option"; argv[1] = "value"; argv[2] = "--next-option"; argv[3] = "--third-option"; argv[4] = "-k"; argv[5] = "short-value"; saneopt_t* opt = saneopt_init(6, argv); assert(strcmp(saneopt_get(opt, "option"), "value") == 0); assert(strcmp(saneopt_get(opt, "next-option"), "") == 0); assert(strcmp(saneopt_get(opt, "third-option"), "") == 0); assert(strcmp(saneopt_get(opt, "k"), "short-value") == 0); free(argv); free(opt); }
void test_no_value() { printf("test_no_value()\n"); char** argv = malloc(1 * sizeof(char*)); argv[0] = "--option"; saneopt_t* opt = saneopt_init(1, argv); assert(strcmp(saneopt_get(opt, "option"), "") == 0); free(argv); free(opt); }
void test_short_alias() { printf("test_short_alias()\n"); char** argv = malloc(2 * sizeof(char*)); argv[0] = "-o"; argv[1] = "value"; saneopt_t* opt = saneopt_init(2, argv); assert(saneopt_alias(opt, "option", "o") == 0); assert(strcmp(saneopt_get(opt, "option"), "value") == 0); free(argv); free(opt); }
void test_long_alias() { printf("test_long_alias()\n"); char** argv = malloc(3 * sizeof(char*)); argv[0] = "--some-opt"; argv[1] = "--alias"; argv[2] = "value"; saneopt_t* opt = saneopt_init(3, argv); assert(saneopt_alias(opt, "option", "alias") == 0); assert(strcmp(saneopt_get(opt, "option"), "value") == 0); free(argv); free(opt); }
void test_arguments() { printf("test_arguments()\n"); char** argv = malloc(6 * sizeof(char*)); char** args; argv[0] = "--option"; argv[1] = "value"; argv[2] = "arg0"; argv[3] = "arg1"; argv[4] = "--"; argv[5] = "arg2"; saneopt_t* opt = saneopt_init(6, argv); args = saneopt_arguments(opt); assert(strcmp(saneopt_get(opt, "option"), "value") == 0); assert(strcmp(args[0], "arg0") == 0); assert(strcmp(args[1], "arg1") == 0); assert(strcmp(args[2], "arg2") == 0); assert(args[3] == NULL); free(argv); free(opt); }
int main(int argc, char *argv[]) { char* host; char* hostname; char* app; char* port_str; int port; int i, c, v = 0; uv_interface_address_t* addresses; uv_err_t err; srand(time(NULL)); atexit(forza__kill); signal(SIGTERM, forza__on_sigterm); loop = uv_default_loop(); #ifdef FORZA_VERSION_HASH printf("forza %s\n", FORZA_VERSION_HASH); // saneopt doesn't allow options without input for (v = 0; v < argc; v++) { if (strcmp(argv[v], "-v") == 0 || strcmp(argv[v], "--version") == 0) { return 0; } } #else printf("forza\n"); #endif opt = saneopt_init(argc - 1, argv + 1); saneopt_alias(opt, "host", "h"); saneopt_alias(opt, "port", "p"); host = saneopt_get(opt, "host"); port_str = saneopt_get(opt, "port"); hostname = saneopt_get(opt, "hostname"); app = saneopt_get(opt, "app"); arguments = saneopt_arguments(opt); if (host == NULL || port_str == NULL) { fprintf(stderr, "Host and port required\n"); return 2; } sscanf(port_str, "%d", &port); if (port <= 0 || port > 65535) { fprintf(stderr, "Port has to be <= 0 and > 65535\n"); return 3; } if (hostname == NULL) { hostname = malloc(256 * sizeof(*hostname)); err = uv_interface_addresses(&addresses, &c); if (err.code != UV_OK) { fprintf(stderr, "uv_interface_addresses: %s\n", uv_err_name(uv_last_error(loop))); return 1; } for (i = 0; i < c; i++) { /* For now, only grab the first non-internal, non 0.0.0.0 interface. * TODO: Make this smarter. */ if (addresses[i].is_internal) continue; uv_ip4_name(&addresses[i].address.address4, hostname, 255 * sizeof(*hostname)); if (strcmp(hostname, "0.0.0.0") != 0) break; } uv_free_interface_addresses(addresses, c); } forza_connect(host, port, hostname, app, on_connect); uv_run(loop, UV_RUN_DEFAULT); free(hostname); free(opt); return 0; }
int main(int argc, char *argv[]) { int r; saneopt_t* opt; char** args; char* min_uptime; signal(SIGHUP, SIG_IGN); signal(SIGINT, handle_signal); signal(SIGTERM, handle_signal); signal(SIGUSR1, restart_child); loop = uv_default_loop(); if (argc == 1) { printf("Usage: aeternum [action] [options] -- program\n"); return 1; } opt = saneopt_init(argc - 1, argv + 1); args = saneopt_arguments(opt); if (strcmp(args[0], "start") == 0) { argv[1] = "run"; opts.infile = NULL; opts.outfile = NULL; opts.errfile = NULL; opts.pidname = NULL; opts.min_uptime = 0; opts.target = argv[0]; opts.json = 0; opts.child_args = argv; spawn_child(1); printf("%d\n", child_req.pid); return 0; } opts.infile = saneopt_get(opt, "i"); opts.outfile = saneopt_get(opt, "o"); opts.errfile = saneopt_get(opt, "e"); opts.pidname = saneopt_get(opt, "p"); opts.json = (saneopt_get(opt, "j") != NULL) ? 1 : 0; opts.target = args[1]; opts.child_args = &args[1]; min_uptime = saneopt_get(opt, "min-uptime"); if (min_uptime != NULL) { sscanf(min_uptime, "%d", &opts.min_uptime); gettimeofday(&start_tv, NULL); } else { opts.min_uptime = 0; } configure_stdio(); spawn_child(0); r = uv_run(loop, UV_RUN_DEFAULT); return r; }