1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
|
/* Copyright (C) 2004 MySQL AB
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#include <stdlib.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#ifdef USE_READLINE
#include <readline/readline.h>
#include <readline/history.h>
#endif
#include <myx_grt_public_interface.h>
#include <myx_grt_lua.h>
#include <myx_grt_java.h>
#include <myx_grt_builtin_module_public_interface.h>
#include <myx_aux_functions.h>
static char *read_command(MYX_GRT *env)
{
char *line= NULL;
//int status;
#ifndef USE_READLINE
char linebuf[1024];
#endif
char *prompt= myx_grt_lua_shell_get_prompt(env);
#ifdef USE_READLINE
line= readline(prompt);
g_free(prompt);
if (!line)
return NULL;
if (*line)
add_history(line);
line= str_g_append(line, "\n");
#else
printf(prompt);
g_free(prompt);
fflush(stdout);
if (fgets(linebuf, sizeof(linebuf), stdin) <= 0)
{
return NULL;
}
line= g_strdup(linebuf);
#endif
return line;
}
static int generate_java_classes(const char *structs_file, const char *output_path_param)
{
MYX_GRT_STRUCTS *gstructs;
MYX_GRT_ERROR error;
char *filename= g_path_get_basename(structs_file);
// Check that the struct file begins with "structs."
if ((!str_beginswith(filename, "structs.")) || (!str_endswith(filename, ".xml")))
{
fprintf(stdout, "ERROR: The structs file has to begin with \"structs.\" and "
"end with \".xml\" but is named %s.\n", filename);
g_free(filename);
return -1;
}
// Load structs
gstructs= myx_grt_struct_load_list(structs_file, &error);
if ((!gstructs) || (error != MYX_GRT_NO_ERROR))
{
fprintf(stdout, "ERROR (%d): Cannot load structs from %s.\n", error, structs_file);
g_free(filename);
return -1;
}
else
{
char *output_path= g_strdup(output_path_param);
char *package_name= g_strdup(filename+8);
char path_sep[]= {MYX_PATH_SEPARATOR, 0};
g_free(filename);
package_name[strlen(package_name)-4]= 0;
if (output_path[strlen(output_path)-1] != MYX_PATH_SEPARATOR)
output_path= str_g_append(output_path, path_sep);
myx_mkdir(output_path, 0755);
error= myx_grt_struct_export_java_classes(gstructs, package_name, output_path);
g_free(package_name);
if (error != MYX_GRT_NO_ERROR)
{
fprintf(stdout, "ERROR (%d): Cannot generate java class files in %s.\n", error, output_path);
return -1;
}
g_free(output_path);
}
return 0;
}
static int generate_php_classes(const char *structs_file, const char *output_path_param)
{
MYX_GRT_STRUCTS *gstructs;
MYX_GRT_ERROR error;
char *filename= g_path_get_basename(structs_file);
// Check that the struct file begins with "structs."
if ((!str_beginswith(filename, "structs.")) || (!str_endswith(filename, ".xml")))
{
fprintf(stdout, "ERROR: The structs file has to begin with \"structs.\" and "
"end with \".xml\" but is named %s.\n", filename);
g_free(filename);
return -1;
}
// Load structs
gstructs= myx_grt_struct_load_list(structs_file, &error);
if ((!gstructs) || (error != MYX_GRT_NO_ERROR))
{
fprintf(stdout, "ERROR (%d): Cannot load structs from %s.\n", error, structs_file);
g_free(filename);
return -1;
}
else
{
char *output_path= g_strdup(output_path_param);
char path_sep[]= {MYX_PATH_SEPARATOR, 0};
g_free(filename);
if (output_path[strlen(output_path)-1] != MYX_PATH_SEPARATOR)
output_path= str_g_append(output_path, path_sep);
myx_mkdir(output_path, 0755);
error= myx_grt_struct_export_php_classes(gstructs, output_path);
if (error != MYX_GRT_NO_ERROR)
{
fprintf(stdout, "ERROR (%d): Cannot generate php class files in %s.\n", error, output_path);
return -1;
}
g_free(output_path);
}
return 0;
}
MYX_GRT *initialize_grt(const char *classpath)
{
MYX_GRT *grt= myx_grt_initialize();
MYX_GRT_MODULE_LOADER *loader;
MYX_GRT_ERROR error;
unsigned int c;
g_message("Scanning for struct definitions in ./xml ...");
c= myx_grt_scan_for_structs(grt, "./xml", &error);
if (error != MYX_GRT_NO_ERROR)
{
g_warning("Error while scanning for struct definitions (%i).", error);
}
else
g_message("Registered %i struct definition files.", c);
g_message("Initializing Builtin modules...");
myx_register_builtin_grt_module_reverse_engineer_mysql(grt);
myx_register_builtin_grt_module_transformation_mysql(grt);
// initialize the loaders
// initialize Java loader
g_message("Initializing Java loader...");
loader= myx_java_init_loader(grt, classpath, &error);
if (!loader)
{
g_warning("Error initializing Java module loader (%i).", error);
}
else
{
if (myx_grt_register_module_loader(grt, loader) < 0)
{
g_warning("Could not register Java module loader.");
}
else
{
// Scan for Java modules
g_message("Scanning for java plugins in ./java/com/mysql/grt/modules ...");
c= myx_grt_scan_for_modules(grt, "./java/com/mysql/grt/modules", &error);
if (error!=MYX_GRT_NO_ERROR)
{
g_warning("Error while scanning for java modules (%i).", error);
}
else
g_message("Initialized %i modules.", c);
}
}
#ifdef ENABLE_PHP_MODULES
// initialize PHP loader
g_message("Initializing PHP loader...");
loader= myx_php_init_loader(grt, &error);
if (!loader)
{
g_warning("Error initializing PHP module loader (%i).", error);
}
else
{
if (myx_grt_register_module_loader(grt, loader) < 0)
{
g_warning("Could not register PHP module loader.");
}
else
{
// Scan for PHP modules
g_message("Scanning for php plugins in ./php/modules ...");
c= myx_grt_scan_for_modules(grt, "./php/modules", &error);
if (error!=MYX_GRT_NO_ERROR)
{
g_warning("Error while scanning for php modules (%i).", error);
}
else
g_message("Initialized %i modules.", c);
}
}
#endif
// initialize Lua loader
g_message("Initializing Lua loader...");
loader= myx_lua_init_loader(grt, &error);
if (!loader)
{
g_warning("Error initializing Lua module loader (%i).", error);
}
else
{
if (myx_grt_register_module_loader(grt, loader) < 0)
{
g_warning("Could not register Lua module loader.");
}
}
myx_grt_init_lua_shell(grt);
return grt;
}
static void print_messages(MYX_GRT *grt)
{
MYX_GRT_MSGS *msgs= myx_grt_get_msgs(grt, 0);
unsigned int i;
if (!msgs) return;
for (i= 0; i < msgs->msgs_num; i++)
{
MYX_GRT_MSG *msg= msgs->msgs+i;
char *type;
switch (msg->msg_type)
{
case 0: type= ""; break;
case 1: type= " WARNING"; break;
case 2: type= " ERROR"; break;
default: type= NULL; break;
}
if (type)
printf("GRT%s: %s\n", type, msg->msg);
else
printf("GRT%i: %s\n", msg->msg_type, msg);
if (msg->msg_detail)
{
unsigned int j;
for (j= 0; j < msg->msg_detail->strings_num; j++)
{
printf("GRT:\t%s\n", msg->msg_detail->strings[j]);
}
}
}
myx_grt_free_msgs(msgs);
}
static void show_help(const char *argv0)
{
fprintf(stdout,
"Usage: %s [-classpath path] [-d path] [-x] [luafile] \n"
" %s -j structsfile outputdir\n\n"
" -classpath .. Sets the java classpath to the given value.\n"
" -d path ..... Modules directory\n"
" -x .......... Exits the shell after running the specified file\n"
" luafile ..... File that is run at startup.\n\n"
" -j .......... Generates java classes from the given structs file.\n"
"\n", argv0, argv0);
}
int main(int argc, char **argv)
{
MYX_GRT_ERROR status;
MYX_GRT *grt;
char *classpath= NULL;
char *linebuf= NULL;
char *load_file= NULL;
char *module_dir= NULL;
int interactive= 1;
int c, i;
// ---------------------------------------------------------------------------------------------------------------
// If the shell is called without arguments, display usage
/*if (argc == 1)
{
show_help(argv[0]);
exit(1);
}*/
// ---------------------------------------------------------------------------------------------------------------
// Handle java class generation
if ((argc>1) && (strcmp(argv[1], "-j")==0))
{
if (argc != 4)
{
show_help(argv[0]);
exit(1);
}
else
{
if (generate_java_classes(argv[2], argv[3])!=0)
exit(1);
else
exit(0);
}
}
// ---------------------------------------------------------------------------------------------------------------
// Handle php class generation
if ((argc>1) && (strcmp(argv[1], "-p")==0))
{
if (argc != 4)
{
show_help(argv[0]);
exit(1);
}
else
{
if (generate_php_classes(argv[2], argv[3])!=0)
exit(1);
else
exit(0);
}
}
// ---------------------------------------------------------------------------------------------------------------
// Handle module_dir and interactive
for (i= 1; i < argc; i++)
{
if (strcmp(argv[i], "-x")==0)
interactive= 0;
else if (strcmp(argv[i], "-d")==0)
{
if (argc <= i+1)
{
show_help(argv[0]);
exit(1);
}
else
module_dir= argv[++i];
}
else if (strcmp(argv[i], "-classpath")==0)
{
if (argc <= i+1)
{
show_help(argv[0]);
exit(1);
}
else
classpath= argv[++i];
}
else
load_file= argv[i];
}
// ---------------------------------------------------------------------------------------------------------------
//Initialize grt
grt= initialize_grt(classpath);
if (!grt)
{
g_message("ERROR: The GRT environment cannot be initialized.");
exit(1);
}
g_assert(lua_gettop(myx_grt_lua_shell_get_lua(grt))==0);
// ---------------------------------------------------------------------------------------------------------------
// Scan for plugins
if (module_dir)
{
g_message("Scanning for plugins in %s...", module_dir);
c= myx_grt_scan_for_modules(grt, module_dir, &status);
g_message("Initialized %i modules", c);
}
g_assert(lua_gettop(myx_grt_lua_shell_get_lua(grt))==0);
// ---------------------------------------------------------------------------------------------------------------
// Run script
if (!interactive)
{
if (load_file)
exit(myx_grt_lua_shell_run_file(grt, load_file));
else
exit(0);
}
else if (load_file)
{
myx_grt_lua_shell_run_file(grt, load_file);
}
// ---------------------------------------------------------------------------------------------------------------
// Main loop
for (;;)
{
linebuf= read_command(grt);
if (!linebuf)
{
break;
}
if(myx_grt_lua_shell_execute(grt, linebuf) == MYX_GRT_SHELL_COMMAND_EXIT)
break;
print_messages(grt);
g_free(linebuf);
}
myx_grt_finalize(grt);
return 0;
}
|