1 /*
2 Copyright (c) 2014, 2021, Oracle and/or its affiliates.
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, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is also distributed with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have included with MySQL.
14
15 This program 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, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25 #include "client_priv.h"
26 #include "my_default.h"
27 #include <sstream>
28 #include "help_options.h"
29 #include "abstract_program.h"
30
31 #include <welcome_copyright_notice.h> /* ORACLE_WELCOME_COPYRIGHT_NOTICE */
32
33 using namespace Mysql::Tools::Base::Options;
34 using Mysql::Tools::Base::Abstract_program;
35 using std::string;
36
37 extern const char *load_default_groups[];
38
Help_options(Abstract_program * program)39 Help_options::Help_options(Abstract_program *program)
40 : m_program(program)
41 {}
42
create_options()43 void Help_options::create_options()
44 {
45 this->create_new_option("help", "Display this help message and exit.")
46 ->set_short_character('?')
47 ->add_callback(new Instance_callback<void, char*, Help_options>(
48 this, &Help_options::help_callback));
49
50 this->create_new_option("version", "Output version information and exit.")
51 ->set_short_character('V')
52 ->add_callback(new Instance_callback<void, char*, Help_options>(
53 this, &Help_options::version_callback));
54 }
55
help_callback(char * argument MY_ATTRIBUTE ((unused)))56 void Help_options::help_callback(char* argument MY_ATTRIBUTE((unused)))
57 {
58 this->print_usage();
59 exit(0);
60 }
61
version_callback(char * argument MY_ATTRIBUTE ((unused)))62 void Help_options::version_callback(char* argument MY_ATTRIBUTE((unused)))
63 {
64 this->print_version_line();
65 exit(0);
66 }
67
68
69 /** A helper function. Prints the program version line. */
print_version_line()70 void Help_options::print_version_line()
71 {
72 printf("%s Ver %s Distrib %s, for %s (%s)\n",
73 this->m_program->get_name().c_str(),
74 this->m_program->get_version().c_str(),
75 MYSQL_SERVER_VERSION, SYSTEM_TYPE, MACHINE_TYPE);
76 }
77
78
print_usage()79 void Mysql::Tools::Base::Options::Help_options::print_usage()
80 {
81
82 this->print_version_line();
83
84 std::ostringstream s;
85 s << m_program->get_first_release_year();
86 string first_year_str(s.str());
87 string copyright;
88
89 if (first_year_str == COPYRIGHT_NOTICE_CURRENT_YEAR)
90 {
91 copyright= ORACLE_WELCOME_COPYRIGHT_NOTICE(COPYRIGHT_NOTICE_CURRENT_YEAR);
92 }
93 else
94 {
95 #define FIRST_YEAR_CONSTANT "$first_year$"
96 string first_year_constant_str= FIRST_YEAR_CONSTANT;
97
98 copyright= ORACLE_WELCOME_COPYRIGHT_NOTICE(FIRST_YEAR_CONSTANT);
99 copyright= copyright.replace(copyright.find(first_year_constant_str),
100 first_year_constant_str.length(), first_year_str);
101 }
102
103 printf("%s\n%s\n",
104 copyright.c_str(),
105 this->m_program->get_description().c_str());
106 /*
107 Turn default for zombies off so that the help on how to
108 turn them off text won't show up.
109 This is safe to do since it's followed by a call to exit().
110 */
111 for (struct my_option *optp= this->m_program->get_options_array();
112 optp->name; optp++)
113 {
114 if (!strcmp(optp->name, "secure-auth"))
115 {
116 optp->def_value= 0;
117 break;
118 }
119 }
120 this->m_program->short_usage();
121 print_defaults("my", load_default_groups);
122 my_print_help(this->m_program->get_options_array());
123 my_print_variables(this->m_program->get_options_array());
124 }
125