📄 mysql.cc
字号:
break; case 'v': if (argument == disabled_my_option) verbose= 0; else verbose++; break; case 'B': status.batch= 1; status.add_to_history= 0; set_if_bigger(opt_silent,1); // more silent break; case 'W':#ifdef __WIN__ opt_protocol = MYSQL_PROTOCOL_PIPE;#endif break;#include <sslopt-case.h> case 'V': usage(1); exit(0); case 'I': case '?': usage(0); exit(0); } return 0;}static int get_options(int argc, char **argv){ char *tmp, *pagpoint; int ho_error; MYSQL_PARAMETERS *mysql_params= mysql_get_parameters(); tmp= (char *) getenv("MYSQL_HOST"); if (tmp) current_host= my_strdup(tmp, MYF(MY_WME)); pagpoint= getenv("PAGER"); if (!((char*) (pagpoint))) { strmov(pager, "stdout"); opt_nopager= 1; } else strmov(pager, pagpoint); strmov(default_pager, pager); opt_max_allowed_packet= *mysql_params->p_max_allowed_packet; opt_net_buffer_length= *mysql_params->p_net_buffer_length; if ((ho_error=handle_options(&argc, &argv, my_long_options, get_one_option))) exit(ho_error); *mysql_params->p_max_allowed_packet= opt_max_allowed_packet; *mysql_params->p_net_buffer_length= opt_net_buffer_length; if (status.batch) /* disable pager and outfile in this case */ { strmov(default_pager, "stdout"); strmov(pager, "stdout"); opt_nopager= 1; default_pager_set= 0; opt_outfile= 0; opt_reconnect= 0; connect_flag= 0; /* Not in interactive mode */ } if (strcmp(default_charset, charset_info->csname) && !(charset_info= get_charset_by_csname(default_charset, MY_CS_PRIMARY, MYF(MY_WME)))) exit(1); if (argc > 1) { usage(0); exit(1); } if (argc == 1) { skip_updates= 0; my_free(current_db, MYF(MY_ALLOW_ZERO_PTR)); current_db= my_strdup(*argv, MYF(MY_WME)); } if (tty_password) opt_password= get_tty_password(NullS); return(0);}static int read_and_execute(bool interactive){#if defined(OS2) || defined(__NETWARE__) char linebuffer[254]; String buffer;#endif#if defined(__WIN__) String tmpbuf; String buffer;#endif char *line; char in_string=0; ulong line_number=0; bool ml_comment= 0; COMMANDS *com; status.exit_status=1; for (;;) { if (!interactive) { line=batch_readline(status.line_buff); line_number++; if (!glob_buffer.length()) status.query_start_line=line_number; } else { char *prompt= (char*) (ml_comment ? " /*> " : glob_buffer.is_empty() ? construct_prompt() : !in_string ? " -> " : in_string == '\'' ? " '> " : (in_string == '`' ? " `> " : " \"> ")); if (opt_outfile && glob_buffer.is_empty()) fflush(OUTFILE);#if defined( __WIN__) || defined(OS2) || defined(__NETWARE__) tee_fputs(prompt, stdout);#if defined(__NETWARE__) line=fgets(linebuffer, sizeof(linebuffer)-1, stdin); /* Remove the '\n' */ if (line) { char *p = strrchr(line, '\n'); if (p != NULL) *p = '\0'; }#elif defined(__WIN__) if (!tmpbuf.is_alloced()) tmpbuf.alloc(65535); tmpbuf.length(0); buffer.length(0); unsigned long clen; do { line= my_cgets((char*)tmpbuf.ptr(), tmpbuf.alloced_length()-1, &clen); buffer.append(line, clen); /* if we got buffer fully filled than there is a chance that something else is still in console input buffer */ } while (tmpbuf.alloced_length() <= clen); line= buffer.c_ptr();#else /* OS2 */ buffer.length(0); /* _cgets() expects the buffer size - 3 as the first byte */ linebuffer[0]= (char) sizeof(linebuffer) - 3; do { line= _cgets(linebuffer); buffer.append(line, (unsigned char)linebuffer[1]); /* If _cgets() gets an input line that is linebuffer[0] bytes long, the next call to _cgets() will return immediately with linebuffer[1] == 0, and it does the same thing for input that is linebuffer[0]-1 bytes long. So it appears that even though _cgets() replaces the newline (which is two bytes on Window) with a nil, it still needs the space in the linebuffer for it. This is, naturally, undocumented. */ } while ((unsigned char)linebuffer[0] <= (unsigned char)linebuffer[1] + 1); line= buffer.c_ptr();#endif /* __NETWARE__ */#else if (opt_outfile) fputs(prompt, OUTFILE); line= readline(prompt);#endif /* defined( __WIN__) || defined(OS2) || defined(__NETWARE__) */ /* When Ctrl+d or Ctrl+z is pressed, the line may be NULL on some OS which may cause coredump. */ if (opt_outfile && line) fprintf(OUTFILE, "%s\n", line); } if (!line) // End of file { status.exit_status=0; break; } if (!in_string && (line[0] == '#' || (line[0] == '-' && line[1] == '-') || line[0] == 0)) continue; // Skip comment lines /* Check if line is a mysql command line (We want to allow help, print and clear anywhere at line start */ if ((named_cmds || glob_buffer.is_empty()) && !in_string && (com=find_command(line,0))) { if ((*com->func)(&glob_buffer,line) > 0) break; if (glob_buffer.is_empty()) // If buffer was emptied in_string=0;#ifdef HAVE_READLINE if (interactive && status.add_to_history && not_in_history(line)) add_history(line);#endif continue; } if (add_line(glob_buffer,line,&in_string,&ml_comment)) break; } /* if in batch mode, send last query even if it doesn't end with \g or go */ if (!interactive && !status.exit_status) { remove_cntrl(glob_buffer); if (!glob_buffer.is_empty()) { status.exit_status=1; if (com_go(&glob_buffer,line) <= 0) status.exit_status=0; } }#if defined( __WIN__) || defined(OS2) || defined(__NETWARE__) buffer.free();#endif#if defined( __WIN__) tmpbuf.free();#endif return status.exit_status;}static COMMANDS *find_command(char *name,char cmd_char){ uint len; char *end; DBUG_ENTER("find_command"); DBUG_PRINT("enter",("name: '%s' char: %d", name ? name : "NULL", cmd_char)); if (!name) { len=0; end=0; } else { while (my_isspace(charset_info,*name)) name++; /* If there is an \\g in the row or if the row has a delimiter but this is not a delimiter command, let add_line() take care of parsing the row and calling find_command() */ if (strstr(name, "\\g") || (strstr(name, delimiter) && !(strlen(name) >= 9 && !my_strnncoll(charset_info, (uchar*) name, 9, (const uchar*) "delimiter", 9)))) DBUG_RETURN((COMMANDS *) 0); if ((end=strcont(name," \t"))) { len=(uint) (end - name); while (my_isspace(charset_info,*end)) end++; if (!*end) end=0; // no arguments to function } else len=(uint) strlen(name); } for (uint i= 0; commands[i].name; i++) { if (commands[i].func && ((name && !my_strnncoll(charset_info,(uchar*)name,len, (uchar*)commands[i].name,len) && !commands[i].name[len] && (!end || (end && commands[i].takes_params))) || !name && commands[i].cmd_char == cmd_char)) { DBUG_PRINT("exit",("found command: %s", commands[i].name)); DBUG_RETURN(&commands[i]); } } DBUG_RETURN((COMMANDS *) 0);}static bool add_line(String &buffer,char *line,char *in_string, bool *ml_comment){ uchar inchar; char buff[80], *pos, *out; COMMANDS *com; bool need_space= 0; DBUG_ENTER("add_line"); if (!line[0] && buffer.is_empty()) DBUG_RETURN(0);#ifdef HAVE_READLINE if (status.add_to_history && line[0] && not_in_history(line)) add_history(line);#endif#ifdef USE_MB char *end_of_line=line+(uint) strlen(line);#endif for (pos=out=line ; (inchar= (uchar) *pos) ; pos++) { if (my_isspace(charset_info,inchar) && out == line && buffer.is_empty()) continue;#ifdef USE_MB int length; if (use_mb(charset_info) && (length= my_ismbchar(charset_info, pos, end_of_line))) { if (!*ml_comment) { while (length--) *out++ = *pos++; pos--; } else pos+= length - 1; continue; }#endif if (!*ml_comment && inchar == '\\') { // Found possbile one character command like \c if (!(inchar = (uchar) *++pos)) break; // readline adds one '\' if (*in_string || inchar == 'N') // \N is short for NULL { // Don't allow commands in string *out++='\\'; *out++= (char) inchar; continue; } if ((com=find_command(NullS,(char) inchar))) { const String tmp(line,(uint) (out-line), charset_info); buffer.append(tmp); if ((*com->func)(&buffer,pos-1) > 0) DBUG_RETURN(1); // Quit if (com->takes_params) { for (pos++ ; *pos && (*pos != *delimiter || !is_prefix(pos + 1, delimiter + 1)) ; pos++) ; // Remove parameters if (!*pos) pos--; else pos+= delimiter_length - 1; // Point at last delim char } out=line; } else { sprintf(buff,"Unknown command '\\%c'.",inchar); if (put_info(buff,INFO_ERROR) > 0) DBUG_RETURN(1); *out++='\\'; *out++=(char) inchar; continue; } } else if (!*ml_comment && !*in_string && (*pos == *delimiter && is_prefix(pos + 1, delimiter + 1) || buffer.length() == 0 && (out - line) >= 9 && !my_strcasecmp(charset_info, line, "delimiter"))) { uint old_delimiter_length= delimiter_length; if (out != line) buffer.append(line, (uint) (out - line)); // Add this line if ((com= find_command(buffer.c_ptr(), 0))) { if (com->func == com_delimiter) { /* Delimiter wants the get rest of the given line as argument to allow one to change ';' to ';;' and back */ char *end= strend(pos); buffer.append(pos, (uint) (end - pos)); /* Ensure pos will point at \0 after the pos+= below */ pos= end - old_delimiter_length + 1; } if ((*com->func)(&buffer, buffer.c_ptr()) > 0) DBUG_RETURN(1); // Quit } else { if (com_go(&buffer, 0) > 0) // < 0 is not fatal DBUG_RETURN(1); } buffer.length(0); out= line; pos+= old_delimiter_length - 1; } else if (!*ml_comment && (!*in_string && (inchar == '#' || inchar == '-' && pos[1] == '-' && my_isspace(charset_info,pos[2])))) break; // comment to end of line else if (!*in_string && inchar == '/' && *(pos+1) == '*' && *(pos+2) != '!') { pos++; *ml_comment= 1; if (out != line) { buffer.append(line,(uint) (out-line)); out=line; } } else if (*ml_comment && inchar == '*' && *(pos + 1) == '/') { pos++; *ml_comment= 0; need_space= 1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -