155
155
*default_charset= (char*) MYSQL_DEFAULT_CHARSET_NAME;
156
156
static char *histfile;
157
157
static char *histfile_tmp;
158
static GString *glob_buffer, *old_buffer;
159
static GString *processed_prompt;
160
//static GString *current_prompt= g_string_sized_new(16);
158
static GString *glob_buffer;
159
static GString *processed_prompt= g_string_sized_new(16);
161
160
static GString *default_prompt= NULL;
162
161
static char *full_username=0,*part_username=0;
163
162
static int wait_time = 5;
209
208
com_rehash(GString *str, char*), com_tee(GString *str, char*),
210
209
com_notee(GString *str, char*), com_charset(GString *str,char*),
211
210
com_prompt(GString *str, char*), com_delimiter(GString *str, char*),
212
com_warnings(GString *str, char*), com_nowarnings(GString *str, char*);
215
static int com_nopager(GString *str, char*), com_pager(GString *str, char*),
216
com_edit(GString *str,char*), com_shell(GString *str, char *);
211
com_warnings(GString *str, char*), com_nowarnings(GString *str, char*),
212
com_nopager(GString *str, char*), com_pager(GString *str, char*);
219
214
static int read_and_execute(bool interactive);
220
215
static int sql_connect(char *host,char *database,char *user,char *password,
254
249
"Reconnect to the server. Optional arguments are db and host." },
255
250
{ "delimiter", 'd', com_delimiter, 1,
256
251
"Set statement delimiter. NOTE: Takes the rest of the line as new delimiter." },
258
{ "edit", 'e', com_edit, 0, "Edit command with $EDITOR."},
260
252
{ "ego", 'G', com_ego, 0,
261
253
"Send command to mysql server, display result vertically."},
262
254
{ "exit", 'q', com_quit, 0, "Exit mysql. Same as quit."},
263
255
{ "go", 'g', com_go, 0, "Send command to mysql server." },
264
256
{ "help", 'h', com_help, 1, "Display this help." },
266
257
{ "nopager",'n', com_nopager,0, "Disable pager, print to stdout." },
268
258
{ "notee", 't', com_notee, 0, "Don't write into outfile." },
270
259
{ "pager", 'P', com_pager, 1,
271
260
"Set PAGER [to_pager]. Print the query results via PAGER." },
273
261
{ "print", 'p', com_print, 0, "Print current command." },
274
262
{ "prompt", 'R', com_prompt, 1, "Change your mysql prompt."},
275
263
{ "quit", 'q', com_quit, 0, "Quit mysql." },
277
265
{ "source", '.', com_source, 1,
278
266
"Execute an SQL script file. Takes a file name as an argument."},
279
267
{ "status", 's', com_status, 0, "Get status information from the server."},
281
{ "system", '!', com_shell, 1, "Execute a system shell command."},
283
268
{ "tee", 'T', com_tee, 1,
284
269
"Set outfile [to_outfile]. Append everything into given outfile." },
285
270
{ "use", 'u', com_use, 1,
1056
1041
DBUG_PROCESS(argv[0]);
1058
1043
delimiter_str= delimiter;
1059
default_prompt = g_string_new(strdup(getenv("MYSQL_PS1") ?
1060
getenv("MYSQL_PS1") :
1062
current_prompt = my_strdup(default_prompt->str, MYF(MY_WME));
1044
default_prompt = g_string_new(g_strdup(getenv("MYSQL_PS1") ?
1045
getenv("MYSQL_PS1") :
1047
current_prompt = g_strdup(default_prompt->str);
1063
1048
prompt_counter=0;
1065
1050
outfile[0]=0; // no (default) outfile
1151
1136
"Your Drizzle connection id is %lu\nServer version: %s\n",
1152
1137
mysql_thread_id(&mysql), server_version_string(&mysql));
1153
1138
put_info(glob_buffer->str,INFO_INFO);
1139
g_string_truncate(glob_buffer,0);
1155
1141
initialize_readline((char*) my_progname);
1156
1142
if (!status.batch && !quick && !opt_html && !opt_xml)
1158
1144
/* read-history from file, default ~/.mysql_history*/
1159
1145
if (getenv("MYSQL_HISTFILE"))
1160
histfile=my_strdup(getenv("MYSQL_HISTFILE"),MYF(MY_WME));
1146
histfile=g_strdup(getenv("MYSQL_HISTFILE"));
1161
1147
else if (getenv("HOME"))
1163
1149
histfile=(char*) my_malloc((uint) strlen(getenv("HOME"))
1218
1204
put_info(sig ? "Aborted" : "Bye", INFO_RESULT);
1205
assert(glob_buffer != NULL);
1219
1206
g_string_free(glob_buffer,true);
1220
g_string_free(old_buffer,true);
1207
assert(processed_prompt != NULL);
1221
1208
g_string_free(processed_prompt,true);
1209
assert(default_prompt != NULL);
1210
g_string_free(default_prompt,true);
1222
1211
my_free(opt_password,MYF(MY_ALLOW_ZERO_PTR));
1223
1212
my_free(opt_mysql_unix_port,MYF(MY_ALLOW_ZERO_PTR));
1224
1213
my_free(histfile,MYF(MY_ALLOW_ZERO_PTR));
1228
1217
my_free(current_user,MYF(MY_ALLOW_ZERO_PTR));
1229
1218
my_free(full_username,MYF(MY_ALLOW_ZERO_PTR));
1230
1219
my_free(part_username,MYF(MY_ALLOW_ZERO_PTR));
1231
my_free(default_prompt,MYF(MY_ALLOW_ZERO_PTR));
1232
1220
#ifdef HAVE_SMEM
1233
1221
my_free(shared_memory_base_name,MYF(MY_ALLOW_ZERO_PTR));
1253
1241
MYSQL *kill_mysql= NULL;
1255
1243
/* terminate if no query being executed, or we already tried interrupting */
1256
if (!executing_query || interrupted_query)
1244
if (!executing_query || interrupted_query) {
1245
printf("MT: terinate?\n");
1259
1249
kill_mysql= mysql_init(kill_mysql);
1260
1250
if (!mysql_real_connect(kill_mysql,current_host, current_user, opt_password,
1261
1251
"", opt_mysql_port, opt_mysql_unix_port,0))
1253
printf("MT:terminate\n");
1264
1257
/* kill_buffer is always big enough because max length of %lu is 15 */
1265
1258
sprintf(kill_buffer, "KILL /*!50000 QUERY */ %lu", mysql_thread_id(&mysql));
1387
1380
{"one-database", 'o',
1388
1381
"Only update the default database. This is useful for skipping updates to other database in the update log.",
1389
1382
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
1391
1383
{"pager", OPT_PAGER,
1392
1384
"Pager to use to display results. If you don't supply an option the default pager is taken from your ENV variable PAGER. Valid pagers are less, more, cat [> filename], etc. See interactive help (\\h) also. This option does not work in batch mode. Disable with --disable-pager. This option is disabled by default.",
1393
1385
0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
1394
1386
{"no-pager", OPT_NOPAGER,
1395
1387
"Disable pager and print to stdout. See interactive help (\\h) also. WARNING: option deprecated; use --disable-pager instead.",
1396
1388
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
1398
1389
{"password", 'p',
1399
1390
"Password to use when connecting to server. If password is not given it's asked from the tty.",
1400
1391
0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
1805
1797
Check if line is a mysql command line
1806
1798
(We want to allow help, print and clear anywhere at line start
1808
if ((named_cmds || (glob_buffer->len==0))
1800
if ((named_cmds || (glob_buffer->len!=0))
1809
1801
&& !ml_comment && !in_string && (com=find_command(line,0)))
1811
1803
if ((*com->func)(glob_buffer,line) > 0)
1817
1809
add_history(line);
1820
if (add_line(glob_buffer,line,&in_string,&ml_comment))
1812
int ret = add_line(glob_buffer,line,&in_string,&ml_comment);
1813
printf("MT: ret=%d\n",ret);
1823
1817
/* if in batch mode, send last query even if it doesn't end with \g or go */
1819
printf("MT: made it here %d\n", status.exit_status);
1825
1820
if (!interactive && !status.exit_status)
1822
printf("pre remove_cntrl *%s*\n", glob_buffer->str);
1827
1823
remove_cntrl(glob_buffer);
1828
if (glob_buffer->len > 0)
1824
printf("post remove_cntrl *%s*\n", glob_buffer->str);
1825
if (glob_buffer->len != 0)
1830
1827
status.exit_status=1;
1831
1828
if (com_go(glob_buffer,line) <= 0)
1844
1841
DBUG_ENTER("find_command");
1845
1842
DBUG_PRINT("enter",("name: '%s' char: %d", name ? name : "NULL", cmd_char));
1843
printf("MT: find_command name: '%s' char: %d\n", name ? name : "NULL", cmd_char);
1899
1897
bool need_space= 0;
1900
1898
bool ss_comment= 0;
1901
1899
DBUG_ENTER("add_line");
1900
printf("MT: In add_line: buffer:*%s*, line=*%s*, in_string=*%s*\n",
1901
buffer->str, line, in_string);
1903
printf("MT %d\n",__LINE__);
1903
1904
if (!line[0] && (buffer->len==0))
1904
1905
DBUG_RETURN(0);
1906
printf("MT %d\n",__LINE__);
1905
1907
if (status.add_to_history && line[0] && not_in_history(line))
1906
1908
add_history(line);
1907
1909
char *end_of_line=line+(uint) strlen(line);
1911
printf("MT %d\n",__LINE__);
1909
1912
for (pos=out=line ; (inchar= (uchar) *pos) ; pos++)
1914
printf("MT: for pos=*%s* out=*%s* line=*%s*\n",
1911
1916
if (!preserve_comments)
1913
1918
// Skip spaces at the beggining of a statement
2017
2026
Delimiter wants the get rest of the given line as argument to
2018
2027
allow one to change ';' to ';;' and back
2029
printf("MT: before delimiter\n");
2020
2030
g_string_append(buffer,pos);
2021
2031
if (com_delimiter(buffer, pos) > 0)
2022
2032
DBUG_RETURN(1);
2033
printf("MT: after delimiter\n");
2024
2035
g_string_truncate(buffer,0);
2027
2038
else if (!*ml_comment && !*in_string && is_prefix(pos, delimiter))
2040
printf("MT %d\n",__LINE__);
2029
2041
// Found a statement. Continue parsing after the delimiter
2030
2042
pos+= delimiter_length;
2143
2159
*out++= (char) inchar;
2160
printf("MT %d\n",__LINE__);
2162
printf("MT %d\n",__LINE__);
2164
printf("MT %d\n",__LINE__);
2166
printf("MT: out of loop - out *%s* line *%s* buffer->len *%d*\n",
2167
out, line, (int) buffer->len);
2147
2168
if (out != line || (buffer->len > 0))
3585
3592
Sorry, this command is not available in Windows.
3590
com_pager(GString *buffer, char *line __attribute__((unused)))
3596
com_pager(GString *buffer __attribute__((__unused__)),
3597
char *line __attribute__((unused)))
3592
3599
char pager_name[FN_REFLEN], *end, *param;
3639
3646
tee_fprintf(stdout, "PAGER set to stdout\n");
3646
Sorry, you can't send the result to an editor in Win32
3651
com_edit(GString *buffer,char *line __attribute__((unused)))
3653
char filename[FN_REFLEN],buff[160];
3657
if ((fd=create_temp_file(filename,NullS,"sql", O_CREAT | O_WRONLY,
3660
if (buffer->is_empty() && !old_buffer.is_empty())
3661
(void) my_write(fd,(uchar*) old_buffer.ptr(),old_buffer.length(),
3664
(void) my_write(fd,(uchar*) buffer->ptr(),buffer->length(),MYF(MY_WME));
3665
(void) my_close(fd,MYF(0));
3667
if (!(editor = (char *)getenv("EDITOR")) &&
3668
!(editor = (char *)getenv("VISUAL")))
3670
strxmov(buff,editor," ",filename,NullS);
3671
(void) system(buff);
3673
struct stat stat_arg;
3674
if (stat(filename,&stat_arg))
3676
if ((fd = my_open(filename,O_RDONLY, MYF(MY_WME))) < 0)
3678
(void) buffer->alloc((uint) stat_arg.st_size);
3679
if ((tmp=read(fd,(char*) buffer->ptr(),buffer->alloced_length())) >= 0L)
3680
buffer->length((uint) tmp);
3683
(void) my_close(fd,MYF(0));
3684
(void) my_delete(filename,MYF(MY_WME));
3691
3650
/* If arg is given, exit without errors. This happens on command 'quit' */
3713
com_shell(GString *buffer, char *line __attribute__((unused)))
3717
/* Skip space from line begin */
3718
while (my_isspace(charset_info, *line))
3720
if (!(shell_cmd = strchr(line, ' ')))
3722
put_info("Usage: \\! shell-command", INFO_ERROR);
3726
The output of the shell command does not
3727
get directed to the pager or the outfile
3729
if (system(shell_cmd) == -1)
3731
put_info(strerror(errno), INFO_ERROR, errno);
3740
3672
com_print(GString *buffer,char *line __attribute__((unused)))
3772
3704
if (tmp && *tmp)
3774
3706
my_free(current_db, MYF(MY_ALLOW_ZERO_PTR));
3775
current_db= my_strdup(tmp, MYF(MY_WME));
3707
current_db= g_strdup(tmp);
3776
3708
tmp= get_arg(buff, 1);
3779
3711
my_free(current_host,MYF(MY_ALLOW_ZERO_PTR));
3780
current_host=my_strdup(tmp,MYF(MY_WME));
3712
current_host=g_strdup(tmp);
3953
3887
return put_error(&mysql);
3955
3889
my_free(current_db,MYF(MY_ALLOW_ZERO_PTR));
3956
current_db=my_strdup(tmp,MYF(MY_WME));
3890
current_db=g_strdup(tmp);
3957
3891
if (select_db > 1)
3958
3892
build_completion_hash(opt_rehash, 1);
4180
4114
tee_fprintf(stdout, "\nAll updates ignored to this database\n");
4181
4115
vidattr(A_NORMAL);
4184
4117
tee_fprintf(stdout, "Current pager:\t\t%s\n", pager);
4185
4118
tee_fprintf(stdout, "Using outfile:\t\t'%s'\n", opt_outfile ? outfile : "");
4187
4119
tee_fprintf(stdout, "Using delimiter:\t%s\n", delimiter);
4188
4120
tee_fprintf(stdout, "Server version:\t\t%s\n", server_version_string(&mysql));
4189
4121
tee_fprintf(stdout, "Protocol version:\t%d\n", mysql_get_proto_info(&mysql));
4371
4303
char *end= start + (buffer->len);
4372
4304
while (start < end && !my_isgraph(charset_info,end[-1]))
4374
// TODO: Verify we're not tuncating one too many chars here...
4375
g_string_truncate(buffer, (buffer->len - (end-start)));
4306
uint chars_to_truncate = end-start;
4307
if (buffer->len > chars_to_truncate)
4308
g_string_truncate(buffer, chars_to_truncate);
4493
4427
struct tm *t = localtime(&lclock);
4495
4429
/* parse thru the settings for the prompt */
4497
for (c= current_prompt; *c; *c++)
4430
for (char *c= current_prompt; *c; *c++)
4499
4432
if (*c != PROMPT_CHAR)
4501
g_string_append(processed_prompt, c);
4434
g_string_append_c(processed_prompt, c[0]);
4671
4604
(result=mysql_use_result(&mysql)))
4673
4606
MYSQL_ROW cur=mysql_fetch_row(result);
4674
full_username=my_strdup(cur[0],MYF(MY_WME));
4675
part_username=my_strdup(strtok(cur[0],"@"),MYF(MY_WME));
4607
full_username=g_strdup(cur[0]);
4608
part_username=g_strdup(strtok(cur[0],"@"));
4676
4609
(void) mysql_fetch_row(result); // Read eof
4682
4615
char *ptr=strchr(line, ' ');
4683
4616
prompt_counter = 0;
4684
4617
my_free(current_prompt,MYF(MY_ALLOW_ZERO_PTR));
4685
current_prompt=my_strdup(ptr ? ptr+1 : default_prompt->str,MYF(MY_WME));
4618
current_prompt=g_strdup(ptr ? ptr+1 : default_prompt->str);
4687
4620
tee_fprintf(stdout, "Returning to default PROMPT of %s\n",
4688
4621
default_prompt->str);