summaryrefslogtreecommitdiff
path: root/other/bootstrap_daemon/src/command_line_arguments.c
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 /other/bootstrap_daemon/src/command_line_arguments.c
parent6b40a581b1ce036e744c51452bbd6be6c690508f (diff)
Put command-line arguments related functions in a separate file
Diffstat (limited to 'other/bootstrap_daemon/src/command_line_arguments.c')
-rw-r--r--other/bootstrap_daemon/src/command_line_arguments.c141
1 files changed, 141 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..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}