Line data Source code
1 : // Snap Websites Server -- to send SIGINT signal to stop a daemon
2 : // Copyright (c) 2011-2024 Made to Order Software Corp. All Rights Reserved
3 : //
4 : // This program is free software; you can redistribute it and/or modify
5 : // it under the terms of the GNU General Public License as published by
6 : // the Free Software Foundation; either version 2 of the License, or
7 : // (at your option) any later version.
8 : //
9 : // This program is distributed in the hope that it will be useful,
10 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : // GNU General Public License for more details.
13 : //
14 : // You should have received a copy of the GNU General Public License
15 : // along with this program; if not, write to the Free Software
16 : // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 :
18 :
19 : // eventdispatcher
20 : //
21 : #include <eventdispatcher/message_definition.h>
22 : #include <eventdispatcher/version.h>
23 :
24 :
25 : // cppprocess
26 : //
27 : #include <cppprocess/process_list.h>
28 :
29 :
30 : // advgetopt
31 : //
32 : #include <advgetopt/advgetopt.h>
33 : #include <advgetopt/options.h>
34 : #include <advgetopt/exception.h>
35 :
36 :
37 : // snaplogger
38 : //
39 : #include <snaplogger/logger.h>
40 : #include <snaplogger/options.h>
41 :
42 :
43 : // snapdev
44 : //
45 : #include <snapdev/pathinfo.h>
46 : #include <snapdev/stringize.h>
47 :
48 :
49 : // C++
50 : //
51 : #include <iostream>
52 :
53 :
54 : // C
55 : //
56 : #include <signal.h>
57 : #include <string.h>
58 :
59 :
60 : // last include
61 : //
62 : #include <snapdev/poison.h>
63 :
64 :
65 :
66 : namespace
67 : {
68 :
69 :
70 :
71 : advgetopt::option const g_options[] =
72 : {
73 : // `--service` is not required because systemd removes the parameter
74 : // altogether when $MAINPID is empty (even with the quotes)
75 : //
76 : advgetopt::define_option(
77 : advgetopt::Name("verbose")
78 : , advgetopt::ShortName('v')
79 : , advgetopt::Flags(advgetopt::any_flags<
80 : advgetopt::GETOPT_FLAG_COMMAND_LINE
81 : , advgetopt::GETOPT_FLAG_GROUP_OPTIONS>())
82 : , advgetopt::Help("show commands and their parameters as the list of commands is being processed.")
83 : ),
84 : advgetopt::define_option(
85 : advgetopt::Name("commands")
86 : , advgetopt::ShortName('c')
87 : , advgetopt::Flags(advgetopt::any_flags<
88 : advgetopt::GETOPT_FLAG_REQUIRED
89 : , advgetopt::GETOPT_FLAG_MULTIPLE
90 : , advgetopt::GETOPT_FLAG_DEFAULT_OPTION
91 : , advgetopt::GETOPT_FLAG_COMMAND_LINE
92 : , advgetopt::GETOPT_FLAG_SHOW_USAGE_ON_ERROR
93 : , advgetopt::GETOPT_FLAG_GROUP_OPTIONS>())
94 : , advgetopt::Help("list of one or more message commands to verify.")
95 : ),
96 : advgetopt::end_options()
97 : };
98 :
99 :
100 : advgetopt::group_description const g_group_descriptions[] =
101 : {
102 : advgetopt::define_group(
103 : advgetopt::GroupNumber(advgetopt::GETOPT_FLAG_GROUP_COMMANDS)
104 : , advgetopt::GroupName("command")
105 : , advgetopt::GroupDescription("Commands:")
106 : ),
107 : advgetopt::define_group(
108 : advgetopt::GroupNumber(advgetopt::GETOPT_FLAG_GROUP_OPTIONS)
109 : , advgetopt::GroupName("option")
110 : , advgetopt::GroupDescription("Options:")
111 : ),
112 : advgetopt::end_groups()
113 : };
114 :
115 :
116 :
117 : advgetopt::options_environment const g_options_environment =
118 : {
119 : .f_project_name = "verify-message-definitions",
120 : .f_group_name = "eventdispatcher",
121 : .f_options = g_options,
122 : .f_options_files_directory = nullptr,
123 : .f_environment_variable_name = "VERIFY_MESSAGE_DEFINITIONS",
124 : .f_environment_variable_intro = nullptr,
125 : .f_section_variables_name = nullptr,
126 : .f_configuration_files = nullptr,
127 : .f_configuration_filename = nullptr,
128 : .f_configuration_directories = nullptr,
129 : .f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_PROCESS_SYSTEM_PARAMETERS,
130 : .f_help_header = "Usage: %p [-<opt>]\n"
131 : "where -<opt> is one or more of:",
132 : .f_help_footer = "%c",
133 : .f_version = EVENTDISPATCHER_VERSION_STRING,
134 : .f_license = "GNU GPL v2 or newer",
135 : .f_copyright = "Copyright (c) 2011-"
136 : SNAPDEV_STRINGIZE(UTC_BUILD_YEAR)
137 : " by Made to Order Software Corporation -- All Rights Reserved",
138 : .f_build_date = UTC_BUILD_DATE,
139 : .f_build_time = UTC_BUILD_TIME,
140 : .f_groups = g_group_descriptions
141 : };
142 :
143 :
144 :
145 : }
146 : // no name namespace
147 :
148 :
149 :
150 :
151 1 : int main(int argc, char *argv[])
152 : {
153 : try
154 : {
155 1 : advgetopt::getopt opts(g_options_environment);
156 1 : snaplogger::add_logger_options(opts);
157 1 : ed::add_message_definition_options(opts);
158 1 : opts.finish_parsing(argc, argv);
159 1 : if(!snaplogger::process_logger_options(opts, "/etc/eventdispatcher/logger", std::cout, false))
160 : {
161 0 : throw advgetopt::getopt_exit("logger options generated an error.", 0);
162 : }
163 1 : ed::process_message_definition_options(opts);
164 1 : snaplogger::logger::get_instance()->set_fatal_error_severity(snaplogger::severity_t::SEVERITY_WARNING);
165 :
166 : // make sure there is at least one command
167 : //
168 1 : if(!opts.is_defined("commands"))
169 : {
170 0 : std::cerr << "verify-message-definitions: error: at least one message name needs to be specified." << std::endl;
171 0 : exit(1);
172 : }
173 1 : std::size_t const size(opts.size("commands"));
174 :
175 1 : bool const verbose(opts.is_defined("verbose"));
176 :
177 15 : for(std::size_t idx(0); idx < size; ++idx)
178 : {
179 42 : std::string name(opts.get_string("commands", idx));
180 :
181 : // because it's much easier to pass full paths from cmake,
182 : // I also apply a basename
183 : //
184 14 : name = snapdev::pathinfo::basename(name, ".conf");
185 :
186 14 : ed::message_definition::pointer_t def(ed::get_message_definition(name));
187 14 : if(verbose)
188 : {
189 0 : std::cout << "--- command: " << def->f_command << " ---\n";
190 : // TODO: display parameters with their flags/type...
191 : }
192 14 : }
193 :
194 1 : return 0;
195 1 : }
196 0 : catch(advgetopt::getopt_exit const & e)
197 : {
198 0 : return e.code();
199 0 : }
200 0 : catch(std::exception const & e)
201 : {
202 : // clean error on exception
203 0 : std::cerr << "verify-message-definitions: exception: " << e.what() << std::endl;
204 0 : }
205 0 : catch(...)
206 : {
207 : // clean error on exception
208 0 : std::cerr << "verify-message-definitions: an unknown exception occurred.\n";
209 0 : }
210 0 : return 1;
211 : }
212 :
213 :
214 : // vim: ts=4 sw=4 et
|