summaryrefslogtreecommitdiff
path: root/other/bootstrap_daemon/src/command_line_arguments.c
diff options
context:
space:
mode:
Diffstat (limited to 'other/bootstrap_daemon/src/command_line_arguments.c')
-rw-r--r--other/bootstrap_daemon/src/command_line_arguments.c145
1 files changed, 145 insertions, 0 deletions
diff --git a/other/bootstrap_daemon/src/command_line_arguments.c b/other/bootstrap_daemon/src/command_line_arguments.c
new file mode 100644
index 00000000..187fa786
--- /dev/null
+++ b/other/bootstrap_daemon/src/command_line_arguments.c
@@ -0,0 +1,145 @@
1/* command_line_arguments.c
2 *
3 * Tox DHT bootstrap daemon.
4 * Command line argument handling.
5 *
6 * Copyright (C) 2015-2016 Tox project All Rights Reserved.
7 *
8 * This file is part of Tox.
9 *
10 * Tox is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * Tox is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
22 *
23 */
24
25#include "command_line_arguments.h"
26
27#include "global.h"
28
29#include <getopt.h>
30
31#include <stdlib.h>
32#include <string.h>
33
34
35/**
36 * Prints --help message
37 */
38void print_help()
39{
40 // 2 space ident
41 // make sure all lines fit into 80 columns
42 // make sure options are listed in alphabetical order
43 write_log(LOG_LEVEL_INFO,
44 "Usage: tox-bootstrapd [OPTION]... --config=FILE_PATH\n"
45 "\n"
46 "Options:\n"
47 " --config=FILE_PATH Specify path to the config file.\n"
48 " This is a required option.\n"
49 " Set FILE_PATH to a path to an empty file in order to\n"
50 " use default settings.\n"
51 " --foreground Run the daemon in foreground. The daemon won't fork\n"
52 " (detach from the terminal) and won't use the PID file.\n"
53 " --help Print this help message.\n"
54 " --log-backend=BACKEND Specify which logging backend to use.\n"
55 " Valid BACKEND values (case sensetive):\n"
56 " syslog Writes log messages to syslog.\n"
57 " Default option when no --log-backend is\n"
58 " specified.\n"
59 " stdout Writes log messages to stdout/stderr.\n"
60 " --version Print version information.\n");
61}
62
63void handle_command_line_arguments(int argc, char *argv[], char **cfg_file_path, LOG_BACKEND *log_backend, bool *run_in_foreground)
64{
65 if (argc < 2) {
66 write_log(LOG_LEVEL_ERROR, "Error: No arguments provided.\n\n");
67 print_help();
68 exit(1);
69 }
70
71 opterr = 0;
72
73 static struct option long_options[] = {
74 {"config", required_argument, 0, 'c'}, // required option
75 {"foreground", no_argument, 0, 'f'},
76 {"help", no_argument, 0, 'h'},
77 {"log-backend", required_argument, 0, 'l'}, // optional, defaults to syslog
78 {"version", no_argument, 0, 'v'},
79 {0, 0, 0, 0 }
80 };
81
82 bool cfg_file_path_set = false;
83 bool log_backend_set = false;
84
85 *run_in_foreground = false;
86
87 int opt;
88
89 while ((opt = getopt_long(argc, argv, ":", long_options, NULL)) != -1) {
90
91 switch (opt) {
92
93 case 'c':
94 *cfg_file_path = optarg;
95 cfg_file_path_set = true;
96 break;
97
98 case 'f':
99 *run_in_foreground = true;
100 break;
101
102 case 'h':
103 print_help();
104 exit(0);
105
106 case 'l':
107 if (strcmp(optarg, "syslog") == 0) {
108 *log_backend = LOG_BACKEND_SYSLOG;
109 log_backend_set = true;
110 } else if (strcmp(optarg, "stdout") == 0) {
111 *log_backend = LOG_BACKEND_STDOUT;
112 log_backend_set = true;
113 } else {
114 write_log(LOG_LEVEL_ERROR, "Error: Invalid BACKEND value for --log-backend option passed: %s\n\n", optarg);
115 print_help();
116 exit(1);
117 }
118 break;
119
120 case 'v':
121 write_log(LOG_LEVEL_INFO, "Version: %lu\n", DAEMON_VERSION_NUMBER);
122 exit(0);
123
124 case '?':
125 write_log(LOG_LEVEL_ERROR, "Error: Unrecognized option %s\n\n", argv[optind-1]);
126 print_help();
127 exit(1);
128
129 case ':':
130 write_log(LOG_LEVEL_ERROR, "Error: No argument provided for option %s\n\n", argv[optind-1]);
131 print_help();
132 exit(1);
133 }
134 }
135
136 if (!log_backend_set) {
137 *log_backend = LOG_BACKEND_SYSLOG;
138 }
139
140 if (!cfg_file_path_set) {
141 write_log(LOG_LEVEL_ERROR, "Error: The required --config option wasn't specified\n\n");
142 print_help();
143 exit(1);
144 }
145}