aboutsummaryrefslogtreecommitdiff
path: root/viv/cli.c
blob: 4cb9b9bff7de47af4f52dbe52e0d0a56f41db00d (plain) (blame)
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
/* Copyright (C) 2021-2021 Fuwn
 * SPDX-License-Identifier: GPL-3.0-only */

#include <viv/cli.h>

#include <stdlib.h>

#include <viv/gemini.h>
#include <viv/log.h>
#include <viv/viv.h>

#define FLAG_IMPLEMENTATION
#include <flag/flag.h>

CLI_handle CLI_cli(int argc, const char *argv[]) {
  CLI_handle cli_handle;

  cli_handle.options = 0x0; /* 0b0000 */

  bool *help = flag_bool(
    "help",
    false,
    "you are here"
  );
  bool *debug = flag_bool(
    "debug",
    false,
    "enable debug information"
  );
  bool *certs = flag_bool(
    "certs",
    false,
    "show certificates"
  );
  bool *trace = flag_bool(
    "trace",
    false,
    "enable trace information"
  );
  size_t *port = flag_size(
    "port",
    GEMINI_port,
    "specify a custom port to use instead of 1965"
  );

  if (!flag_parse(argc, (char **)argv)) {
    CLI_usage(stderr, argv[0]);
    flag_print_error(stderr);
    VIV_exit(EXIT_FAILURE, NULL);
  }

  if (*help) {
    CLI_usage(stdout, argv[0]);
    VIV_exit(EXIT_SUCCESS, NULL);
  }

  cli_handle.argc = flag_rest_argc();
  cli_handle.argv = flag_rest_argv();

  if (cli_handle.argc <= 0) {
    VIV_WARN("%s", "no url provided\n\n")
    CLI_usage(stderr, argv[0]);
    VIV_exit(EXIT_FAILURE, NULL);
  }

  if (*certs) { cli_handle.options = (cli_handle.options | CLI_option_SHOW_CERTS); }
  if (*debug) { cli_handle.options = (cli_handle.options | CLI_option_DEBUG); }
  if (*trace) { cli_handle.options = (cli_handle.options | CLI_option_TRACE); }
  if (*port) {
    cli_handle.options = (cli_handle.options | CLI_option_PORT);
    cli_handle.port = (int)*port;
  }

  return cli_handle;
}

void CLI_usage(FILE *stream, const char *viv_path) {
  fprintf(stream, "usage: %s [option] <hostname> [port]\n", viv_path);
  fprintf(stream, "options:\n");
  flag_print_options(stream);
}