summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaxim Biro <nurupo.contributions@gmail.com>2016-01-01 00:36:57 -0500
committerMaxim Biro <nurupo.contributions@gmail.com>2016-01-01 00:36:57 -0500
commit7d86caf51fd2f127a7f12c90293981650f6aae3c (patch)
tree3437b7b85b7245e80a08d664734f824576f81cc6
parent6b40a581b1ce036e744c51452bbd6be6c690508f (diff)
Put command-line arguments related functions in a separate file
-rw-r--r--other/bootstrap_daemon/Makefile.inc4
-rw-r--r--other/bootstrap_daemon/src/command_line_arguments.c141
-rw-r--r--other/bootstrap_daemon/src/command_line_arguments.h34
-rw-r--r--other/bootstrap_daemon/src/tox-bootstrapd.c114
4 files changed, 179 insertions, 114 deletions
diff --git a/other/bootstrap_daemon/Makefile.inc b/other/bootstrap_daemon/Makefile.inc
index 5dce910e..b278e3f0 100644
--- a/other/bootstrap_daemon/Makefile.inc
+++ b/other/bootstrap_daemon/Makefile.inc
@@ -10,7 +10,9 @@ tox_bootstrapd_SOURCES = \
10 ../other/bootstrap_daemon/src/config.c \ 10 ../other/bootstrap_daemon/src/config.c \
11 ../other/bootstrap_daemon/src/config_defaults.h \ 11 ../other/bootstrap_daemon/src/config_defaults.h \
12 ../other/bootstrap_node_packets.h \ 12 ../other/bootstrap_node_packets.h \
13 ../other/bootstrap_node_packets.c 13 ../other/bootstrap_node_packets.c \
14 ../other/bootstrap_daemon/src/command_line_arguments.h \
15 ../other/bootstrap_daemon/src/command_line_arguments.c
14 16
15tox_bootstrapd_CFLAGS = \ 17tox_bootstrapd_CFLAGS = \
16 -I$(top_srcdir)/other/bootstrap_daemon \ 18 -I$(top_srcdir)/other/bootstrap_daemon \
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..9b3b2e8c
--- /dev/null
+++ b/other/bootstrap_daemon/src/command_line_arguments.c
@@ -0,0 +1,141 @@
1/* command_line_arguments.c
2 *
3 * Tox DHT bootstrap daemon.
4 *
5 * Copyright (C) 2015 Tox project All Rights Reserved.
6 *
7 * This file is part of Tox.
8 *
9 * Tox is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * Tox is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23
24#include "command_line_arguments.h"
25
26#include "global.h"
27
28#include <getopt.h>
29
30#include <stdlib.h>
31#include <string.h>
32
33// Prints --help message
34
35void print_help()
36{
37 // 2 space ident
38 // make sure all lines fit into 80 columns
39 // make sure options are listed in alphabetical order
40 write_log(LOG_LEVEL_INFO,
41 "Usage: tox-bootstrapd [OPTION]... --config=FILE_PATH\n"
42 "\n"
43 "Options:\n"
44 " --config=FILE_PATH Specify path to the config file.\n"
45 " This is a required option.\n"
46 " Set FILE_PATH to a path to an empty file in order to\n"
47 " use default settings.\n"
48 " --foreground Run the daemon in foreground. The daemon won't fork\n"
49 " (detach from the terminal) and won't use the PID file.\n"
50 " --help Print this help message.\n"
51 " --log-backend=BACKEND Specify which logging backend to use.\n"
52 " Valid BACKEND values (case sensetive):\n"
53 " syslog Writes log messages to syslog.\n"
54 " Default option when no --log-backend is\n"
55 " specified.\n"
56 " stdout Writes log messages to stdout/stderr.\n"
57 " --version Print version information.\n");
58}
59
60void handle_command_line_arguments(int argc, char *argv[], char **cfg_file_path, LOG_BACKEND *log_backend, bool *run_in_foreground)
61{
62 if (argc < 2) {
63 write_log(LOG_LEVEL_ERROR, "Error: No arguments provided.\n\n");
64 print_help();
65 exit(1);
66 }
67
68 opterr = 0;
69
70 static struct option long_options[] = {
71 {"help", no_argument, 0, 'h'},
72 {"config", required_argument, 0, 'c'}, // required option
73 {"foreground", no_argument, 0, 'f'},
74 {"log-backend", required_argument, 0, 'l'}, // optional, defaults to syslog
75 {"version", no_argument, 0, 'v'},
76 {0, 0, 0, 0 }
77 };
78
79 bool cfg_file_path_set = false;
80 bool log_backend_set = false;
81
82 *run_in_foreground = false;
83
84 int opt;
85
86 while ((opt = getopt_long(argc, argv, ":", long_options, NULL)) != -1) {
87
88 switch (opt) {
89 case 'h':
90 print_help();
91 exit(0);
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 'l':
103 if (strcmp(optarg, "syslog") == 0) {
104 *log_backend = LOG_BACKEND_SYSLOG;
105 log_backend_set = true;
106 } else if (strcmp(optarg, "stdout") == 0) {
107 *log_backend = LOG_BACKEND_STDOUT;
108 log_backend_set = true;
109 } else {
110 write_log(LOG_LEVEL_ERROR, "Error: Invalid BACKEND value for --log-backend option passed: %s\n\n", optarg);
111 print_help();
112 exit(1);
113 }
114 break;
115
116 case 'v':
117 write_log(LOG_LEVEL_INFO, "Version: %lu\n", DAEMON_VERSION_NUMBER);
118 exit(0);
119
120 case '?':
121 write_log(LOG_LEVEL_ERROR, "Error: Unrecognized option %s\n\n", argv[optind-1]);
122 print_help();
123 exit(1);
124
125 case ':':
126 write_log(LOG_LEVEL_ERROR, "Error: No argument provided for option %s\n\n", argv[optind-1]);
127 print_help();
128 exit(1);
129 }
130 }
131
132 if (!log_backend_set) {
133 *log_backend = LOG_BACKEND_SYSLOG;
134 }
135
136 if (!cfg_file_path_set) {
137 write_log(LOG_LEVEL_ERROR, "Error: The required --config option wasn't specified\n\n");
138 print_help();
139 exit(1);
140 }
141}
diff --git a/other/bootstrap_daemon/src/command_line_arguments.h b/other/bootstrap_daemon/src/command_line_arguments.h
new file mode 100644
index 00000000..1581278f
--- /dev/null
+++ b/other/bootstrap_daemon/src/command_line_arguments.h
@@ -0,0 +1,34 @@
1/* command_line_arguments.h
2 *
3 * Tox DHT bootstrap daemon.
4 *
5 * Copyright (C) 2015 Tox project All Rights Reserved.
6 *
7 * This file is part of Tox.
8 *
9 * Tox is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * Tox is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23
24#ifndef COMMAND_LINE_ARGUMENTS_H
25#define COMMAND_LINE_ARGUMENTS_H
26
27#include "log.h"
28
29// Handles command line arguments, setting cfg_file_path and log_backend.
30// Terminates the application if incorrect arguments are specified.
31
32void handle_command_line_arguments(int argc, char *argv[], char **cfg_file_path, LOG_BACKEND *log_backend, bool *run_in_foreground);
33
34#endif // COMMAND_LINE_ARGUMENTS_H
diff --git a/other/bootstrap_daemon/src/tox-bootstrapd.c b/other/bootstrap_daemon/src/tox-bootstrapd.c
index d4191e80..8ffef009 100644
--- a/other/bootstrap_daemon/src/tox-bootstrapd.c
+++ b/other/bootstrap_daemon/src/tox-bootstrapd.c
@@ -49,6 +49,7 @@
49#include "../bootstrap_node_packets.h" 49#include "../bootstrap_node_packets.h"
50#include "../../testing/misc_tools.c" 50#include "../../testing/misc_tools.c"
51 51
52#include "command_line_arguments.h"
52#include "config.h" 53#include "config.h"
53#include "global.h" 54#include "global.h"
54#include "log.h" 55#include "log.h"
@@ -122,119 +123,6 @@ void print_public_key(const uint8_t *public_key)
122 return; 123 return;
123} 124}
124 125
125// Prints --help message
126
127bool print_help()
128{
129 // 2 space ident
130 // make sure all lines fit into 80 columns
131 // make sure options are listed in alphabetical order
132 write_log(LOG_LEVEL_INFO,
133 "Usage: tox-bootstrapd [OPTION]... --config=FILE_PATH\n"
134 "\n"
135 "Options:\n"
136 " --config=FILE_PATH Specify path to the config file.\n"
137 " This is a required option.\n"
138 " Set FILE_PATH to a path to an empty file in order to\n"
139 " use default settings.\n"
140 " --foreground Run the daemon in foreground. The daemon won't fork\n"
141 " (detach from the terminal) and won't use the PID file.\n"
142 " --help Print this help message.\n"
143 " --log-backend=BACKEND Specify which logging backend to use.\n"
144 " Valid BACKEND values (case sensetive):\n"
145 " syslog Writes log messages to syslog.\n"
146 " Default option when no --log-backend is\n"
147 " specified.\n"
148 " stdout Writes log messages to stdout/stderr.\n"
149 " --version Print version information.\n");
150}
151
152// Handels command line arguments, setting cfg_file_path and log_backend.
153// Terminates the application if incorrect arguments are specified.
154
155void handle_command_line_arguments(int argc, char *argv[], char **cfg_file_path, LOG_BACKEND *log_backend, bool *run_in_foreground)
156{
157 if (argc < 2) {
158 write_log(LOG_LEVEL_ERROR, "Error: No arguments provided.\n\n");
159 print_help();
160 exit(1);
161 }
162
163 opterr = 0;
164
165 static struct option long_options[] = {
166 {"help", no_argument, 0, 'h'},
167 {"config", required_argument, 0, 'c'}, // required option
168 {"foreground", no_argument, 0, 'f'},
169 {"log-backend", required_argument, 0, 'l'}, // optional, defaults to syslog
170 {"version", no_argument, 0, 'v'},
171 {0, 0, 0, 0 }
172 };
173
174 bool cfg_file_path_set = false;
175 bool log_backend_set = false;
176
177 *run_in_foreground = false;
178
179 int opt;
180
181 while ((opt = getopt_long(argc, argv, ":", long_options, NULL)) != -1) {
182
183 switch (opt) {
184 case 'h':
185 print_help();
186 exit(0);
187
188 case 'c':
189 *cfg_file_path = optarg;
190 cfg_file_path_set = true;
191 break;
192
193 case 'f':
194 *run_in_foreground = true;
195 break;
196
197 case 'l':
198 if (strcmp(optarg, "syslog") == 0) {
199 *log_backend = LOG_BACKEND_SYSLOG;
200 log_backend_set = true;
201 } else if (strcmp(optarg, "stdout") == 0) {
202 *log_backend = LOG_BACKEND_STDOUT;
203 log_backend_set = true;
204 } else {
205 write_log(LOG_LEVEL_ERROR, "Error: Invalid BACKEND value for --log-backend option passed: %s\n\n", optarg);
206 print_help();
207 exit(1);
208 }
209 break;
210
211 case 'v':
212 write_log(LOG_LEVEL_INFO, "Version: %lu\n", DAEMON_VERSION_NUMBER);
213 exit(0);
214
215 case '?':
216 write_log(LOG_LEVEL_ERROR, "Error: Unrecognized option %s\n\n", argv[optind-1]);
217 print_help();
218 exit(1);
219
220 case ':':
221 write_log(LOG_LEVEL_ERROR, "Error: No argument provided for option %s\n\n", argv[optind-1]);
222 print_help();
223 exit(1);
224 }
225 }
226
227 if (!log_backend_set) {
228 *log_backend = LOG_BACKEND_SYSLOG;
229 }
230
231 if (!cfg_file_path_set) {
232 write_log(LOG_LEVEL_ERROR, "Error: The required --config option wasn't specified\n\n");
233 print_help();
234 exit(1);
235 }
236}
237
238// Demonizes the process, appending PID to the PID file and closing file descriptors based on log backend 126// Demonizes the process, appending PID to the PID file and closing file descriptors based on log backend
239// Terminates the application if the daemonization fails. 127// Terminates the application if the daemonization fails.
240 128