Directory: | src/ |
---|---|
File: | src/worker_main.c |
Date: | 2024-04-25 03:45:42 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 34 | 43 | 79.1% |
Branches: | 10 | 14 | 71.4% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * Copyright (c) 2022 Egor Tensin <egor@tensin.name> | ||
3 | * This file is part of the "cimple" project. | ||
4 | * For details, see https://github.com/egor-tensin/cimple. | ||
5 | * Distributed under the MIT License. | ||
6 | */ | ||
7 | |||
8 | #include "cmd_line.h" | ||
9 | #include "const.h" | ||
10 | #include "log.h" | ||
11 | #include "worker.h" | ||
12 | |||
13 | #include <getopt.h> | ||
14 | #include <unistd.h> | ||
15 | |||
16 | 60 | static struct settings default_settings(void) | |
17 | { | ||
18 | 60 | struct settings settings = { | |
19 | .host = default_host, | ||
20 | .port = default_port, | ||
21 | }; | ||
22 | 60 | return settings; | |
23 | } | ||
24 | |||
25 | 4 | const char *get_usage_string(void) | |
26 | { | ||
27 | 4 | return "[-h|--help] [-V|--version] [-v|--verbose] [-H|--host HOST] [-p|--port PORT]"; | |
28 | } | ||
29 | |||
30 | 60 | static int parse_settings(struct settings *settings, int argc, char *argv[]) | |
31 | { | ||
32 | int opt, longind; | ||
33 | |||
34 | 60 | *settings = default_settings(); | |
35 | |||
36 | /* clang-format off */ | ||
37 | static struct option long_options[] = { | ||
38 | {"help", no_argument, 0, 'h'}, | ||
39 | {"version", no_argument, 0, 'V'}, | ||
40 | {"verbose", no_argument, 0, 'v'}, | ||
41 | {"host", required_argument, 0, 'H'}, | ||
42 | {"port", required_argument, 0, 'p'}, | ||
43 | {0, 0, 0, 0}, | ||
44 | }; | ||
45 | /* clang-format on */ | ||
46 | |||
47 |
2/2✓ Branch 1 taken 114 times.
✓ Branch 2 taken 54 times.
|
168 | while ((opt = getopt_long(argc, argv, "hVvH:p:", long_options, &longind)) != -1) { |
48 |
5/6✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 54 times.
✓ Branch 4 taken 54 times.
✓ Branch 5 taken 2 times.
|
114 | switch (opt) { |
49 | 2 | case 'h': | |
50 | 2 | exit_with_usage(0); | |
51 | ✗ | break; | |
52 | 2 | case 'V': | |
53 | 2 | exit_with_version(); | |
54 | ✗ | break; | |
55 | ✗ | case 'v': | |
56 | ✗ | g_log_lvl = LOG_LVL_DEBUG; | |
57 | ✗ | break; | |
58 | 54 | case 'H': | |
59 | 54 | settings->host = optarg; | |
60 | 54 | break; | |
61 | 54 | case 'p': | |
62 | 54 | settings->port = optarg; | |
63 | 54 | break; | |
64 | 2 | default: | |
65 | 2 | exit_with_usage(1); | |
66 | ✗ | break; | |
67 | } | ||
68 | } | ||
69 | |||
70 | 54 | return 0; | |
71 | } | ||
72 | |||
73 | 60 | int main(int argc, char *argv[]) | |
74 | { | ||
75 | struct settings settings; | ||
76 | 60 | struct worker *worker = NULL; | |
77 | 60 | int ret = 0; | |
78 | |||
79 | 60 | ret = parse_settings(&settings, argc, argv); | |
80 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
|
54 | if (ret < 0) |
81 | ✗ | return ret; | |
82 | |||
83 | 54 | ret = worker_create(&worker, &settings); | |
84 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
|
54 | if (ret < 0) |
85 | ✗ | return ret; | |
86 | |||
87 | 54 | ret = worker_main(worker); | |
88 |
1/2✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
|
54 | if (ret < 0) |
89 | ✗ | goto destroy_worker; | |
90 | |||
91 | 54 | destroy_worker: | |
92 | 54 | worker_destroy(worker); | |
93 | |||
94 | 54 | return ret; | |
95 | } | ||
96 |