mysql client 入口

mysql client的入口函数在和sql同级的client 这个目录下,其入口函数是如下:
int main(int argc, char *argv[]) {

  default_prompt = my_strdup(
      PSI_NOT_INSTRUMENTED,
      getenv("MYSQL_PS1") ? getenv("MYSQL_PS1") : "mysql> ", MYF(MY_WME));
}
从这里可以知道,平时我们看到的提示符mysql> 是因为我们没有设置环境变量MYSQL_PS1。我们可以通过这个环境变量来设置自己的
命令行提示符。
  if (sql_connect(current_host, current_db, current_user, opt_password,
                  opt_silent)) {
    quick = 1;  // Avoid history
    status.exit_status = 1;
    mysql_end(-1);
  }
这里就开始连接server,连接到server后我们就看到下面熟悉的log
  put_info("Welcome to the MySQL monitor.  Commands end with ; or \\g.",
           INFO_INFO);
再往下就是定义三个信号
  signal(SIGINT, handle_ctrlc_signal);  // Catch SIGINT to clean up
  signal(SIGQUIT, mysql_end);           // Catch SIGQUIT to clean up
  signal(SIGHUP, handle_quit_signal);   // Catch SIGHUP to clean up

这里有注册三个信号,其中SIGQUIT信号会退出mysql,而sigint其实啥作用都没有
void handle_ctrlc_signal(int) {
  sigint_received = 1;

  /* Skip rest if --sigint-ignore is used. */
  if (opt_sigint_ignore) return;

  if (executing_query) kill_query("^C");
  /* else, do nothing, just terminate the current line (like /c command). */
  return;
}
可以看到如果没有定义opt_sigint_ignore和executing_query,那么用户按ctrl+c其实一点作用都没有
最后在main函数开始时候有通过  charset_index = get_command_index('C');来得到C这个命令的index,
inline int get_command_index(char cmd_char) {
  /*
    All client-specific commands are in the first part of commands array
    and have a function to implement it.
  */
#从这里可以知道mysql中支持的clint命令都是在command这个函数中
  for (uint i = 0; *commands[i].func != NULL; i++)
    if (commands[i].cmd_char == cmd_char) return i;
  return -1;
}


猜你喜欢

转载自blog.csdn.net/tiantao2012/article/details/81262160