MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
main.cpp
1 /*
2  Copyright (c) 2003, 2010, 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 as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 #include <ndb_global.h>
19 #include <ndb_opts.h>
20 
21 // copied from mysql.cc to get readline
22 extern "C" {
23 #if defined( __WIN__)
24 #include <conio.h>
25 #elif !defined(__NETWARE__)
26 #include <readline/readline.h>
27 extern "C" int add_history(const char *command); /* From readline directory */
28 extern "C" int read_history(const char *command);
29 extern "C" int write_history(const char *command);
30 #define HAVE_READLINE
31 #endif
32 }
33 
34 #include <NdbMain.h>
35 #include <BaseString.hpp>
36 #include <NdbOut.hpp>
37 #include <mgmapi.h>
38 #include <ndb_version.h>
39 
40 #include "ndb_mgmclient.hpp"
41 
42 const char *load_default_groups[]= { "mysql_cluster","ndb_mgm",0 };
43 
44 
45 static Ndb_mgmclient* com;
46 
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;
52 
53 static struct my_option my_long_options[] =
54 {
55  NDB_STD_OPTS("ndb_mgm"),
56  { "execute", 'e',
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 },
64  { "verbose", 'v',
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}
69 };
70 
71 static void short_usage_sub(void)
72 {
73  ndb_short_usage_sub("[hostname [port]]");
74 }
75 
76 static void usage()
77 {
78  ndb_usage(short_usage_sub, load_default_groups, my_long_options);
79 }
80 
81 static bool
82 read_and_execute(int try_reconnect)
83 {
84  static char *line_read = (char *)NULL;
85 
86  /* If the buffer has already been allocated, return the memory
87  to the free pool. */
88  if (line_read)
89  {
90  free (line_read);
91  line_read = (char *)NULL;
92  }
93 #ifdef HAVE_READLINE
94  /* Get a line from the user. */
95  line_read = readline (prompt);
96  /* If the line has any text in it, save it on the history. */
97  if (line_read && *line_read)
98  add_history (line_read);
99 #else
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) {
105  char *q=linebuffer;
106  while (*q > 31) q++;
107  *q=0;
108  line_read= strdup(linebuffer);
109  }
110 #endif
111  return com->execute(line_read, try_reconnect, 1);
112 }
113 
114 int main(int argc, char** argv){
115  NDB_INIT(argv[0]);
116 
117  ndb_opt_set_usage_funcs(short_usage_sub, usage);
118  load_defaults("my",load_default_groups,&argc,&argv);
119  int ho_error;
120 #ifndef DBUG_OFF
121  opt_debug= "d:t:O,/tmp/ndb_mgm.trace";
122 #endif
123  if ((ho_error=handle_options(&argc, &argv, my_long_options,
124  ndb_std_get_one_option)))
125  exit(ho_error);
126 
127  BaseString connect_str(opt_ndb_connectstring);
128  if(argc == 1) {
129  connect_str.assfmt("%s", argv[0]);
130  } else if (argc >= 2) {
131  connect_str.assfmt("%s:%s", argv[0], argv[1]);
132  }
133 
134  if (!isatty(0) || opt_execute_str)
135  {
136  prompt= 0;
137  }
138 
139  com = new Ndb_mgmclient(connect_str.c_str(), opt_verbose);
140  int ret= 0;
141  BaseString histfile;
142  if (!opt_execute_str)
143  {
144 #ifdef HAVE_READLINE
145  char *histfile_env= getenv("NDB_MGM_HISTFILE");
146  if (histfile_env)
147  histfile.assign(histfile_env,strlen(histfile_env));
148  else if(getenv("HOME"))
149  {
150  histfile.assign(getenv("HOME"),strlen(getenv("HOME")));
151  histfile.append("/.ndb_mgm_history");
152  }
153  if (histfile.length())
154  read_history(histfile.c_str());
155 #endif
156 
157  ndbout << "-- NDB Cluster -- Management Client --" << endl;
158  while(read_and_execute(opt_try_reconnect))
159  ;
160 
161 #ifdef HAVE_READLINE
162  if (histfile.length())
163  {
164  BaseString histfile_tmp;
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));
169  }
170 #endif
171  }
172  else
173  {
174  com->execute(opt_execute_str, opt_try_reconnect, 0, &ret);
175  }
176  delete com;
177 
178  ndb_end(opt_ndb_endinfo ? MY_CHECK_ERROR | MY_GIVE_INFO : 0);
179 
180  // Don't allow negative return code
181  if (ret < 0)
182  ret = 255;
183  return ret;
184 }
185