Commit a417e7b6 authored by unknown's avatar unknown

Changed get_arg() so that it can be used to fetch more than

just one argument from command line.

Fixed a bug in interactive 'connect' command, which earlier
couldn't take quoted arguments.

How-to-repeat:

mysql> connect `stupid db` localhost

parent d897a495
...@@ -40,7 +40,7 @@ ...@@ -40,7 +40,7 @@
#include <signal.h> #include <signal.h>
#include <violite.h> #include <violite.h>
const char *VER= "13.2"; const char *VER= "13.3";
/* Don't try to make a nice table if the data is too big */ /* Don't try to make a nice table if the data is too big */
#define MAX_COLUMN_LENGTH 1024 #define MAX_COLUMN_LENGTH 1024
...@@ -195,7 +195,7 @@ static void end_pager(); ...@@ -195,7 +195,7 @@ static void end_pager();
static int init_tee(char *); static int init_tee(char *);
static void end_tee(); static void end_tee();
static const char* construct_prompt(); static const char* construct_prompt();
static char *get_arg(char *line); static char *get_arg(char *line, my_bool get_next_arg);
static void init_username(); static void init_username();
static void add_int_to_prompt(int toadd); static void add_int_to_prompt(int toadd);
...@@ -2231,23 +2231,21 @@ com_print(String *buffer,char *line __attribute__((unused))) ...@@ -2231,23 +2231,21 @@ com_print(String *buffer,char *line __attribute__((unused)))
static int static int
com_connect(String *buffer, char *line) com_connect(String *buffer, char *line)
{ {
char *tmp,buff[256]; char *tmp, buff[256];
bool save_rehash= rehash; bool save_rehash= rehash;
int error; int error;
bzero(buff, sizeof(buff));
if (buffer) if (buffer)
{ {
while (my_isspace(system_charset_info,*line)) strmov(buff, line);
line++; tmp= get_arg(buff, 0);
strnmov(buff,line,sizeof(buff)-1); // Don't destroy history if (tmp && *tmp)
if (buff[0] == '\\') // Short command
buff[1]=' ';
tmp=(char *) strtok(buff," \t"); // Skip connect command
if (tmp && (tmp=(char *) strtok(NullS," \t;")))
{ {
my_free(current_db,MYF(MY_ALLOW_ZERO_PTR)); my_free(current_db, MYF(MY_ALLOW_ZERO_PTR));
current_db=my_strdup(tmp,MYF(MY_WME)); current_db= my_strdup(tmp, MYF(MY_WME));
if ((tmp=(char *) strtok(NullS," \t;"))) tmp= get_arg(buff, 1);
if (tmp)
{ {
my_free(current_host,MYF(MY_ALLOW_ZERO_PTR)); my_free(current_host,MYF(MY_ALLOW_ZERO_PTR));
current_host=my_strdup(tmp,MYF(MY_WME)); current_host=my_strdup(tmp,MYF(MY_WME));
...@@ -2333,8 +2331,9 @@ com_use(String *buffer __attribute__((unused)), char *line) ...@@ -2333,8 +2331,9 @@ com_use(String *buffer __attribute__((unused)), char *line)
char *tmp; char *tmp;
char buff[256]; char buff[256];
bzero(buff, sizeof(buff));
strmov(buff, line); strmov(buff, line);
tmp= get_arg(buff); tmp= get_arg(buff, 0);
if (!tmp || !*tmp) if (!tmp || !*tmp)
{ {
put_info("USE must be followed by a database name", INFO_ERROR); put_info("USE must be followed by a database name", INFO_ERROR);
...@@ -2376,9 +2375,20 @@ com_use(String *buffer __attribute__((unused)), char *line) ...@@ -2376,9 +2375,20 @@ com_use(String *buffer __attribute__((unused)), char *line)
} }
/*
Gets argument from a command on the command line. If get_next_arg is
not defined, skips the command and returns the first argument. The
line is modified by adding zero to the end of the argument. If
get_next_arg is defined, then the function searches for end of string
first, after found, returns the next argument and adds zero to the
end. If you ever wish to use this feature, remember to initialize all
items in the array to zero first.
*/
enum quote_type { NO_QUOTE, SQUOTE, DQUOTE, BTICK }; enum quote_type { NO_QUOTE, SQUOTE, DQUOTE, BTICK };
char *get_arg(char *line) char *get_arg(char *line, my_bool get_next_arg)
{ {
char *ptr; char *ptr;
my_bool quoted= 0, valid_arg= 0; my_bool quoted= 0, valid_arg= 0;
...@@ -2386,13 +2396,22 @@ char *get_arg(char *line) ...@@ -2386,13 +2396,22 @@ char *get_arg(char *line)
enum quote_type qtype= NO_QUOTE; enum quote_type qtype= NO_QUOTE;
ptr= line; ptr= line;
/* skip leading white spaces */ if (get_next_arg)
while (my_isspace(system_charset_info, *ptr)) {
ptr++; for (; ptr && *ptr; ptr++);
if (*ptr == '\\') // short command was used if ((ptr + 1) && *(ptr + 1))
ptr+= 2; ptr++;
while (!my_isspace(system_charset_info, *ptr)) // skip command }
ptr++; else
{
/* skip leading white spaces */
while (my_isspace(system_charset_info, *ptr))
ptr++;
if (*ptr == '\\') // short command was used
ptr+= 2;
while (!my_isspace(system_charset_info, *ptr)) // skip command
ptr++;
}
while (my_isspace(system_charset_info, *ptr)) while (my_isspace(system_charset_info, *ptr))
ptr++; ptr++;
if ((*ptr == '\'' && (qtype= SQUOTE)) || if ((*ptr == '\'' && (qtype= SQUOTE)) ||
...@@ -2415,9 +2434,8 @@ char *get_arg(char *line) ...@@ -2415,9 +2434,8 @@ char *get_arg(char *line)
ptr= line; ptr= line;
ptr+= count; ptr+= count;
} }
else if (!quoted && *ptr == ' ') else if ((!quoted && *ptr == ' ') ||
*(ptr + 1) = 0; (*ptr == '\'' && qtype == SQUOTE) ||
else if ((*ptr == '\'' && qtype == SQUOTE) ||
(*ptr == '\"' && qtype == DQUOTE) || (*ptr == '\"' && qtype == DQUOTE) ||
(*ptr == '`' && qtype == BTICK)) (*ptr == '`' && qtype == BTICK))
{ {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment