18 #include <ndb_global.h>
25 #elif !defined(__NETWARE__)
26 #include <readline/readline.h>
27 extern "C" int add_history(
const char *command);
28 extern "C" int read_history(
const char *command);
29 extern "C" int write_history(
const char *command);
35 #include <BaseString.hpp>
38 #include <ndb_version.h>
40 #include "ndb_mgmclient.hpp"
42 const char *load_default_groups[]= {
"mysql_cluster",
"ndb_mgm",0 };
47 static const char default_prompt[]=
"ndb_mgm> ";
48 static unsigned opt_try_reconnect;
49 static const char *prompt= default_prompt;
50 static char *opt_execute_str= 0;
51 static unsigned opt_verbose = 1;
53 static struct my_option my_long_options[] =
55 NDB_STD_OPTS(
"ndb_mgm"),
57 "execute command and exit",
58 (uchar**) &opt_execute_str, (uchar**) &opt_execute_str, 0,
59 GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
60 {
"try-reconnect",
't',
61 "Specify number of tries for connecting to ndb_mgmd (0 = infinite)",
62 (uchar**) &opt_try_reconnect, (uchar**) &opt_try_reconnect, 0,
63 GET_UINT, REQUIRED_ARG, 3, 0, 0, 0, 0, 0 },
65 "Control the amount of printout",
66 (uchar**) &opt_verbose, (uchar**) &opt_verbose, 0,
67 GET_UINT, REQUIRED_ARG, 1, 0, 0, 0, 0, 0},
68 { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
71 static void short_usage_sub(
void)
73 ndb_short_usage_sub(
"[hostname [port]]");
78 ndb_usage(short_usage_sub, load_default_groups, my_long_options);
82 read_and_execute(
int try_reconnect)
84 static char *line_read = (
char *)NULL;
91 line_read = (
char *)NULL;
95 line_read = readline (prompt);
97 if (line_read && *line_read)
98 add_history (line_read);
100 static char linebuffer[254];
101 fputs(prompt, stdout);
102 linebuffer[
sizeof(linebuffer)-1]=0;
103 line_read = fgets(linebuffer,
sizeof(linebuffer)-1, stdin);
104 if (line_read == linebuffer) {
108 line_read= strdup(linebuffer);
111 return com->execute(line_read, try_reconnect, 1);
114 int main(
int argc,
char** argv){
117 ndb_opt_set_usage_funcs(short_usage_sub, usage);
118 load_defaults(
"my",load_default_groups,&argc,&argv);
121 opt_debug=
"d:t:O,/tmp/ndb_mgm.trace";
123 if ((ho_error=handle_options(&argc, &argv, my_long_options,
124 ndb_std_get_one_option)))
127 BaseString connect_str(opt_ndb_connectstring);
129 connect_str.assfmt(
"%s", argv[0]);
130 }
else if (argc >= 2) {
131 connect_str.assfmt(
"%s:%s", argv[0], argv[1]);
134 if (!isatty(0) || opt_execute_str)
142 if (!opt_execute_str)
145 char *histfile_env= getenv(
"NDB_MGM_HISTFILE");
147 histfile.
assign(histfile_env,strlen(histfile_env));
148 else if(getenv(
"HOME"))
150 histfile.
assign(getenv(
"HOME"),strlen(getenv(
"HOME")));
151 histfile.
append(
"/.ndb_mgm_history");
154 read_history(histfile.
c_str());
157 ndbout <<
"-- NDB Cluster -- Management Client --" << endl;
158 while(read_and_execute(opt_try_reconnect))
165 histfile_tmp.
assign(histfile);
166 histfile_tmp.
append(
".TMP");
167 if(!write_history(histfile_tmp.
c_str()))
168 my_rename(histfile_tmp.
c_str(), histfile.
c_str(), MYF(MY_WME));
174 com->execute(opt_execute_str, opt_try_reconnect, 0, &ret);
178 ndb_end(opt_ndb_endinfo ? MY_CHECK_ERROR | MY_GIVE_INFO : 0);