1 /*
2    Copyright (c) 2008, 2016, Oracle and/or its affiliates. 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, 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 #define OPTEXPORT
26 #include <ndb_opts.h>
27 
28 #include <ndb_version.h>
29 #ifdef HAVE_MY_DEFAULT_H
30 #include <my_default.h>
31 #endif
32 
default_ndb_opt_short(void)33 static void default_ndb_opt_short(void)
34 {
35   ndb_short_usage_sub(NULL);
36 }
37 
default_ndb_opt_usage(void)38 static void default_ndb_opt_usage(void)
39 {
40   struct my_option my_long_options[] =
41     {
42       NDB_STD_OPTS("ndbapi_program")
43     };
44   const char *load_default_groups[]= { "mysql_cluster", 0 };
45 
46   ndb_usage(default_ndb_opt_short, load_default_groups, my_long_options);
47 }
48 
49 static void (*g_ndb_opt_short_usage)(void)= default_ndb_opt_short;
50 static void (*g_ndb_opt_usage)(void)= default_ndb_opt_usage;
51 
52 extern "C"
ndb_opt_set_usage_funcs(void (* short_usage)(void),void (* usage)(void))53 void ndb_opt_set_usage_funcs(void (*short_usage)(void),
54                              void (*usage)(void))
55 {
56   /* Check that the program name has been set already */
57   assert(my_progname);
58 
59   if(short_usage)
60     g_ndb_opt_short_usage= short_usage;
61   if(usage)
62     g_ndb_opt_usage= usage;
63 }
64 
65 static inline
ndb_progname(void)66 const char* ndb_progname(void)
67 {
68   if (my_progname)
69     return my_progname;
70   return "<unknown program>";
71 }
72 
73 extern "C"
ndb_short_usage_sub(const char * extra)74 void ndb_short_usage_sub(const char* extra)
75 {
76   printf("Usage: %s [OPTIONS]%s%s\n", ndb_progname(),
77          (extra)?" ":"",
78          (extra)?extra:"");
79 }
80 
81 extern "C"
ndb_usage(void (* usagefunc)(void),const char * load_default_groups[],struct my_option * my_long_options)82 void ndb_usage(void (*usagefunc)(void), const char *load_default_groups[],
83                struct my_option *my_long_options)
84 {
85   (*usagefunc)();
86 
87   ndb_std_print_version();
88   print_defaults(MYSQL_CONFIG_NAME,load_default_groups);
89   puts("");
90   my_print_help(my_long_options);
91   my_print_variables(my_long_options);
92 }
93 
94 extern "C"
95 my_bool
ndb_std_get_one_option(int optid,const struct my_option * opt MY_ATTRIBUTE ((unused)),char * argument MY_ATTRIBUTE ((unused)))96 ndb_std_get_one_option(int optid,
97                        const struct my_option *opt MY_ATTRIBUTE((unused)),
98                        char *argument MY_ATTRIBUTE((unused)))
99 {
100   switch (optid) {
101 #ifndef DBUG_OFF
102   case '#':
103     if (!opt_debug)
104       opt_debug= "d:t";
105     DBUG_SET_INITIAL(argument ? argument : opt_debug);
106     opt_ndb_endinfo= 1;
107     break;
108 #endif
109   case 'V':
110     ndb_std_print_version();
111     exit(0);
112   case '?':
113     (*g_ndb_opt_usage)();
114     exit(0);
115   }
116   return 0;
117 }
118 
119 extern "C"
ndb_std_print_version()120 void ndb_std_print_version()
121 {
122 #ifndef DBUG_OFF
123   const char *suffix= "-debug";
124 #else
125   const char *suffix= "";
126 #endif
127   printf("MySQL distrib %s%s, for %s (%s)\n",
128          NDB_VERSION_STRING,suffix,SYSTEM_TYPE,MACHINE_TYPE);
129 }
130 
131 extern "C"
ndb_is_load_default_arg_separator(const char * arg)132 my_bool ndb_is_load_default_arg_separator(const char* arg)
133 {
134 #ifndef MYSQL_VERSION_ID
135 #error "Need MYSQL_VERSION_ID defined"
136 #endif
137 
138 #if MYSQL_VERSION_ID >= 50510
139   /*
140     load_default() in 5.5+ returns an extra arg which has to
141     be skipped when processing the argv array
142    */
143   if (my_getopt_is_args_separator(arg))
144     return TRUE;
145 #elif MYSQL_VERSION_ID >= 50501
146   if (arg == args_separator)
147     return TRUE;
148 #else
149   (void)arg;
150 #endif
151   return FALSE;
152 }
153 
154 extern "C"
155 int
ndb_load_defaults(const char * conf_file,const char ** groups,int * argc,char *** argv)156 ndb_load_defaults(const char* conf_file, const char** groups,
157                   int *argc, char*** argv)
158 {
159   return my_load_defaults(conf_file ? conf_file : MYSQL_CONFIG_NAME,
160                           groups, argc, argv, NULL);
161 }
162 
163 extern "C"
164 void
ndb_free_defaults(char ** argv)165 ndb_free_defaults(char** argv)
166 {
167   free_defaults(argv);
168 }
169 
170