예제 #1
0
// https://github.com/mmalecki/saneopt/issues/3
void test_arguments_issue_3() {
  printf("test_arguments_issue_3()\n");

  char** argv = malloc(8 * sizeof(char*));
  char** args;

  argv[0] = "-o";
  argv[1] = "out.log";
  argv[2] = "--min-uptime";
  argv[3] = "2000";
  argv[4] = "run";
  argv[5] = "--";
  argv[6] = "node";
  argv[7] = "s.js";

  saneopt_t* opt = saneopt_init(8, argv);
  args = saneopt_arguments(opt);
  assert(strcmp(args[0], "run") == 0);
  assert(strcmp(args[1], "node") == 0);
  assert(strcmp(args[2], "s.js") == 0);
  assert(args[3] == NULL);

  free(argv);
  free(opt);
}
예제 #2
0
void test_all() {
  printf("test_all()\n");

  char** argv = malloc(6 * sizeof(char*));
  char** args;

  argv[0] = "--option";
  argv[1] = "first";
  argv[2] = "--option";
  argv[3] = "second";
  argv[4] = "-o";
  argv[5] = "third";

  saneopt_t* opt = saneopt_init(6, argv);
  assert(saneopt_alias(opt, "option", "o") == 0);
  args = saneopt_get_all(opt, "option");
  assert(strcmp(args[0], "first") == 0);
  assert(strcmp(args[1], "second") == 0);
  assert(strcmp(args[2], "third") == 0);
  assert(args[3] == NULL);

  free(argv);
  free(opt);
  free(args);
}
예제 #3
0
void test_arguments_many_markers() {
  printf("test_arguments_many_markers()\n");

  char** argv = malloc(9 * sizeof(char*));
  char** args;

  argv[0] = "-o";
  argv[1] = "stdout.log";
  argv[2] = "--";
  argv[3] = "estragon";
  argv[4] = "-h";
  argv[5] = "127.0.0.1:1337";
  argv[6] = "--";
  argv[7] = "node";
  argv[8] = "script.js";

  saneopt_t* opt = saneopt_init(9, argv);
  args = saneopt_arguments(opt);
  assert(strcmp(args[0], "estragon") == 0);
  assert(strcmp(args[1], "-h") == 0);
  assert(strcmp(args[2], "127.0.0.1:1337") == 0);
  assert(strcmp(args[3], "--") == 0);
  assert(strcmp(args[4], "node") == 0);
  assert(strcmp(args[5], "script.js") == 0);
  assert(args[6] == NULL);

  free(argv);
  free(opt);
}
예제 #4
0
파일: forza.c 프로젝트: revington/forza
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;
}
예제 #5
0
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);
}
예제 #6
0
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);
}
예제 #7
0
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);
}
예제 #8
0
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);
}
예제 #9
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);
}
예제 #10
0
void test_arguments_first() {
  printf("test_arguments_first()\n");

  char** argv = malloc(2 * sizeof(char*));
  char** args;

  argv[0] = "value";
  argv[1] = "next-value";

  saneopt_t* opt = saneopt_init(2, argv);
  args = saneopt_arguments(opt);
  assert(strcmp(args[0], "value") == 0);
  assert(strcmp(args[1], "next-value") == 0);
  assert(args[2] == NULL);

  free(argv);
  free(opt);
}
예제 #11
0
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);
}
예제 #12
0
// https://github.com/mmalecki/saneopt/issues/2
void test_arguments_issue_2() {
  printf("test_arguments_issue_2()\n");

  char** argv = malloc(7 * sizeof(char*));
  char** args;

  argv[0] = "-h";
  argv[1] = "127.0.0.1:1337";
  argv[2] = "-h";
  argv[3] = "127.0.0.1:1338";
  argv[4] = "--";
  argv[5] = "node";
  argv[6] = "test/fixtures/listen.js";

  saneopt_t* opt = saneopt_init(7, argv);
  args = saneopt_arguments(opt);
  assert(strcmp(args[0], "node") == 0);
  assert(strcmp(args[1], "test/fixtures/listen.js") == 0);
  assert(args[2] == NULL);

  free(argv);
  free(opt);
}
예제 #13
0
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);
}
예제 #14
0
파일: forza.c 프로젝트: cloudninja-io/forza
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;
}
예제 #15
0
파일: aeternum.c 프로젝트: 4nodejs/aeternum
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;
}