mariadb/strings/my_vsnprintf.c
Michael Widenius 5eccc400b1 Implementation of Multi-source replication (MDEV:253)
Documentation of the feature can be found at: http://kb.askmonty.org/en/multi-source-replication/
This code is based on code from Taobao, developed by Plinux

Other things:
- Added new commands: START ALL SLAVES, STOP ALL SLAVES and SHOW FULL SLAVE STATUS
- Almost all usage of 'active_mi' is deleted.
- Added parameter to reset_logs() so that one can specify if new logs should be created.
- Check wildcard match as early as possible for SHOW STATUS. This makes SHOW STATUS like 'xxx' a lot faster and use less mutex
- Made max_relay_log_size depending on master connection.
- Added sys_vars.default_master_connection_basic to fix a failure in sys_vars.all_vars, modified sql_slave_skip_counter_basic to allow session-level settings
- Added commands to mysqladmin: start-all-slaves & stop-all-slaves
- Removed logging of "next log '%s' is currently active | not active"
- Fixed bug in my_vsnprintf() when using positional parameters with length
- Added fn_ext2(), which returns pointer to last '.' in file name
- max_relay_log_size now acts as a normal slave specific variable
- Don't store replication position if innobase_overwrite_relay_log_info is not set
- max_relay_log_size copies it's values from max_binlog_size at startup



BUILD/SETUP.sh:
  Added -Wno-invalid-offsetof to get rid of warning of offsetof() on C++ class (safe in the contex we use it)
client/mysqladmin.cc:
  Added commands start-all-slaves & stop-all-slaves
client/mysqltest.cc:
  Added support for error names starting with 'W'
  Added connection_name support to --sync_with_master
cmake/maintainer.cmake:
  Added -Wno-invalid-offsetof to get rid of warning of offsetof() on C++ class (safe in the contex we use it)
include/my_sys.h:
  Added fn_ext2(), which returns pointer to last '.' in file name
include/mysql/plugin.h:
  Added SHOW_SIMPLE_FUNC
include/mysql/plugin_audit.h.pp:
  Updated signature file
include/mysql/plugin_auth.h.pp:
  Updated signature file
include/mysql/plugin_ftparser.h.pp:
  Updated signature file
mysql-test/extra/rpl_tests/rpl_max_relay_size.test:
  Updated test
mysql-test/include/setup_fake_relay_log.inc:
  There is no orphan relay log files anymore
mysql-test/mysql-test-run.pl:
  Added multi_source to test suite
mysql-test/r/flush.result:
  Added test for new syntax of flush relay logs (can't repeat relay logs or slave)
mysql-test/r/mysqld--help.result:
  Updated result
mysql-test/r/mysqltest.result:
  Updated result
mysql-test/r/parser.result:
  Updated result
mysql-test/r/signal_code.result:
  Updated result after introducing new commands
mysql-test/r/sp-code.result:
  Updated result after introducing new commands
mysql-test/suite/multi_source:
  Tests for multi-source
mysql-test/suite/multi_source/info_logs-master.opt:
  Test with strange file names
mysql-test/suite/multi_source/info_logs.result:
  Test of logs
mysql-test/suite/multi_source/info_logs.test:
  Test of logs
mysql-test/suite/multi_source/my.cnf:
  Setup of multi-master tests
  Added log-warnings to get more information to the log files
mysql-test/suite/multi_source/relaylog_events.result:
  Test relay log handling
mysql-test/suite/multi_source/relaylog_events.test:
  Test relay log handling
mysql-test/suite/multi_source/reset_slave.result:
  Test RESET SLAVE
mysql-test/suite/multi_source/reset_slave.test:
  Test RESET SLAVE
mysql-test/suite/multi_source/simple.result:
  Simple basic test of multi-source functionality
mysql-test/suite/multi_source/simple.test:
  Simple basic test of multi-source functionality
mysql-test/suite/multi_source/skip_counter.result:
  Testing skip_counter and max_relay_log_size
mysql-test/suite/multi_source/skip_counter.test:
  Testing skip_counter and max_relay_log_size
mysql-test/suite/multi_source/syntax.result:
  Test of multi-source syntax
mysql-test/suite/multi_source/syntax.test:
  Test of multi-source syntax
mysql-test/suite/rpl/r/rpl_deadlock_innodb.result:
  New warnings
mysql-test/suite/rpl/r/rpl_filter_dbs_dynamic.result:
  Improved error texts
mysql-test/suite/rpl/r/rpl_filter_tables_dynamic.result:
  Improved error texts
mysql-test/suite/rpl/r/rpl_filter_wild_tables_dynamic.result:
  Improved error texts
mysql-test/suite/rpl/r/rpl_flush_logs.result:
  Update results
mysql-test/suite/rpl/r/rpl_heartbeat.result:
  Warning was removed
mysql-test/suite/rpl/r/rpl_heartbeat_basic.result:
  Warning was removed
mysql-test/suite/rpl/r/rpl_rotate_logs.result:
  Updated results because of new error messages
mysql-test/suite/rpl/r/rpl_row_max_relay_size.result:
  Updated results
mysql-test/suite/rpl/r/rpl_row_show_relaylog_events.result:
  Updated results after improved RESET SLAVE (we now use less relay log files)
mysql-test/suite/rpl/r/rpl_skip_replication.result:
  New error message
mysql-test/suite/rpl/r/rpl_start_slave_deadlock_sys_vars.result:
  Test removed as the old DBUG_SYNC entries doesn't exist anymore
mysql-test/suite/rpl/r/rpl_stm_max_relay_size.result:
  Updated results
mysql-test/suite/rpl/r/rpl_stm_mix_show_relaylog_events.result:
  Updated results after improved RESET SLAVE (we now use less relay log files)
mysql-test/suite/rpl/t/rpl_flush_logs.test:
  Updated tests as relay log files is created a bit differently than before
mysql-test/suite/rpl/t/rpl_start_slave_deadlock_sys_vars.test:
  Test removed as the old DBUG_SYNC entries doesn't exist anymore
mysql-test/suite/sys_vars/r/default_master_connection_basic.result:
  New test to test usage of default_master_connection
mysql-test/suite/sys_vars/r/max_relay_log_size_basic.result:
  Updated results
mysql-test/suite/sys_vars/r/sql_slave_skip_counter_basic.result:
  Updated usage of sql_slave_skip_counter
mysql-test/suite/sys_vars/t/default_master_connection_basic.test:
  New test to test usage of default_master_connection
mysql-test/suite/sys_vars/t/max_relay_log_size_basic.test:
  Updated results
mysql-test/suite/sys_vars/t/sql_slave_skip_counter_basic.test:
  Updated test for multi-source
mysql-test/t/flush.test:
  Added test for new syntax of flush relay logs (can't repeat relay logs or slave)
mysql-test/t/parser.test:
  Updated test as master_pos_wait() now takes more arguments than before
mysys/mf_fn_ext.c:
  Added fn_ext2(), which returns pointer to last '.' in file name
plugin/semisync/semisync_master_plugin.cc:
  Use SHOW_SIMPLE_FUNC to optimize SHOW STATUS
sql/event_scheduler.cc:
  No reason to initialize slave_thread (it's guaranteed to be zero here)
sql/item_create.cc:
  Added connection_name argument to master_pos_wait()
  Simplified code
sql/item_func.cc:
  Added connection_name argument to master_pos_wait()
sql/item_func.h:
  Added connection_name argument to master_pos_wait()
sql/lex.h:
  Added SLAVES keyword
sql/log.cc:
  Added tag "Master 'connection_name'" to slave errors that has a connection name.
  Added parameter to reset_logs() so that one can specify if new logs should be created.
  Removed some wrong casts
  Changed some constants to defines
sql/log.h:
  Added parameter to reset_logs()
  Updated comment to reflect new code
sql/log_event.cc:
  Updated DBUG_PRINT
sql/mysqld.cc:
  Added variable mysqld_server_initialized so that other functions can test if server is fully initialized.
  Free all slave data in one place (fewer ifdef's)
  Removed not needed call to close_active_mi()
  Initialize slaves() later in startup to ensure that everthing is really initialized when slaves start.
  Made status variable slave_running multi-source safe
  max_relay_log_size copies it's values from max_binlog_size at startup
  SHOW_FUNC -> SHOW_SIMPLE_FUNC
sql/mysqld.h:
  Added mysqld_server_initialized
  Removed max_relay_log_size
sql/rpl_mi.cc:
  Store connection name and cmp_connection_name (only used for show full slave status) in Master_info
  Added code for Master_info_index, which handles storage of multi-master information
  Don't write the empty "" connection_name to multi-master.info file. This is handled by the original code.
  Create Master_info_index::index_file_names once at init
  More DBUG_PRINT
  Give error if Master_info_index::check_duplicate_master_info fails
  Added start|stop all slaves
sql/rpl_mi.h:
  Added connection_name and Master_info_index
  Updated prototypes
sql/rpl_rli.cc:
  Added connection_name to relay log files.
  If we do a full reset, don't create any new relay log files.
  Updated comment to reflect new code
  Made max_relay_log_size depending on master connection.
sql/rpl_rli.h:
  Fixed type of slave_skip_counter as we now access it directly in sys_vars.cc, so it must be ulong
  Made executed_entries and max_relay_log_size depending on master connection.
sql/set_var.cc:
  Fixed that one can get variable name also when one uses DEFAULT
sql/set_var.h:
  Made option global so that one can check and change min & max values (sorry Sergei)
sql/share/errmsg-utf8.txt:
  Added new error messages needed for multi-source
  Added multi-source name to error ER_MASTER_INFO and WARN_NO_MASTER_INFO
  Improved error message if connection exists
sql/slave.cc:
  Moved things a bit around to make it easier to handle error conditions.
  Create a global master_info_index and add the "" connection to it
  Ensure that new Master_info doesn't fail.
  Don't call terminate_slave_threads(active_mi..) on end_slave() as this is now done automaticly when deleting master_info_index.
  Delete not needed function close_active_mi(). One can achive same thing by calling end_slave().
  Added support for SHOW FULL SLAVE STATUS (show status for all master connections with connection_name as first column)
  Removed logging of "next log '%s' is currently active | not active"
  Added Slave_received_heartbeats and Slave_heartbeat_period
sql/slave.h:
  Added new defines and prototypes
sql/sql_base.cc:
  More DBUG_PRINT
sql/sql_class.cc:
  Reset thd->connection_name and thd-->default_master_connection
sql/sql_class.h:
  Added thd->connection_name and thd-->default_master_connection
  Made slave_skip_count and max_relay_log_size depending on master connection. These variables are in THD to make changing them thread safe.
sql/sql_const.h:
  Added MAX_CONNECTION_NAME
sql/sql_insert.cc:
  thd->slave_thread -> thd->rli_slave
  Removed usage of active_mi
sql/sql_lex.cc:
  Reset 'lex->verbose' (to simplify some sql_yacc.yy code)
sql/sql_lex.h:
  Added connection_name, relay_log_connection_name, SQLCOM_SLAVE_ALL_START and SQLCOM_SLAVE_ALL_STOP
sql/sql_load.cc:
  thd->slave_thread -> thd->rli_slave
  Removed usage of active_mi
sql/sql_parse.cc:
  Added support for connection_name to all SLAVE commands.
  - Instead of using active_mi, we now get the current Master_info from master_info_index.
  - Create new replication threads with CHANGE MASTER
  - Added support for show_all_master_info()-
sql/sql_prepare.cc:
  Added SQLCOM_SLAVE_ALL_START and SQLCOM_SLAVE_ALL_STOP
sql/sql_reload.cc:
  Made reset/full slave use master_info_index->get_master_info() instead of active_mi.
  If one uses 'RESET SLAVE "connection_name" all' the connection is removed from master_info_index.
  Fixed issues with FLUSH RELAY LOGS
sql/sql_repl.cc:
  sql_slave_skip_counter is moved to thd->variables to make it thread safe and fix some bugs with it
  Add connection name to relay log files.
  Added connection name to errors.
  Added some logging for multi-master if log_warnings > 1
  stop_slave():
  - Don't check if thd is set. It's guaranteed to always be set.
  change_master():
  - Check for duplicate connection names in change_master()
  - Check for wrong arguments first in file (to simplify error handling)
  - Register new connections in master_info_index
  
  
  
  
  
  
  ******
  Added multi-source support to show relaylog events
  ******
  check_duplicate_master_info() now generates an error
  Added parameter to reset_logs()
  ******
  Updated calls to create_signed_file_name()
sql/sql_show.cc:
  Check wildcard match as early as possible for SHOW STATUS. This makes SHOW STATUS like 'xxx' a lot faster and use less mutex
sql/sql_yacc.yy:
  Added optional connection_name to a all relevant master/slave commands
  Added multi-source support to show relaylog events
  Added new commands START ALL SLAVES, STOP ALL SLAVES and SHOW FULL SLAVE STATUS
sql/strfunc.cc:
  my_global.h shoud always be included first.
sql/sys_vars.cc:
  Added variable default_master_connection
  Made variable sql_slave_skip_counter multi-source safe
  Made max_relay_log_size depending on master connection.
  Made old code more reusable
sql/sys_vars.h:
  Added Sys_var_session_lexstring (needed for default_master_connection)
  Added Sys_var_multi_source_uint (needed for sql_slave_skip_counter).
  Changed Sys_var_multi_source_uint to ulong to be able to handle max_relay_log_size
  Made old code more reusable
storage/example/ha_example.cc:
  Use SHOW_SIMPLE_FUNC to optimize SHOW STATUS
storage/sphinx/ha_sphinx.cc:
  Use SHOW_SIMPLE_FUNC to optimize SHOW STATUS
storage/xtradb/handler/ha_innodb.cc:
  Don't store replication position if innobase_overwrite_relay_log_info is not set
strings/my_vsnprintf.c:
  Fixed bug when using positional parameters with length
2012-10-04 01:37:58 +03:00

845 lines
21 KiB
C

/* Copyright (c) 2000, 2011, Oracle and/or its affiliates.
Copyright (c) 2009-2011, Monty Program 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; version 2 of the License.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#include "strings_def.h"
#include <m_ctype.h>
#include <stdarg.h>
#include <my_sys.h>
#include <my_base.h>
#include <my_handler_errors.h>
#define MAX_ARGS 32 /* max positional args count*/
#define MAX_PRINT_INFO 32 /* max print position count */
#define MAX_WIDTH 65535
#define LENGTH_ARG 1
#define WIDTH_ARG 2
#define PREZERO_ARG 4
#define ESCAPED_ARG 8
typedef struct pos_arg_info ARGS_INFO;
typedef struct print_info PRINT_INFO;
struct pos_arg_info
{
char arg_type; /* argument type */
uint have_longlong; /* used from integer values */
char *str_arg; /* string value of the arg */
longlong longlong_arg; /* integer value of the arg */
double double_arg; /* double value of the arg */
};
struct print_info
{
char arg_type; /* argument type */
size_t arg_idx; /* index of the positional arg */
size_t length; /* print width or arg index */
size_t width; /* print width or arg index */
uint flags;
const char *begin; /**/
const char *end; /**/
};
/**
Calculates print length or index of positional argument
@param fmt processed string
@param length print length or index of positional argument
@param pre_zero returns flags with PREZERO_ARG set if necessary
@retval
string position right after length digits
*/
static const char *get_length(const char *fmt, size_t *length, uint *pre_zero)
{
for (; my_isdigit(&my_charset_latin1, *fmt); fmt++)
{
*length= *length * 10 + (uint)(*fmt - '0');
if (!*length)
*pre_zero|= PREZERO_ARG; /* first digit was 0 */
}
return fmt;
}
/*
Get argument for '*' parameter
@param fmt processed string
@param args_arr Arguments to printf
@param arg_count Number of arguments to printf
@param length returns length of argument
@param flag returns flags with PREZERO_ARG set if necessary
@return new fmt
*/
static const char *get_length_arg(const char *fmt, ARGS_INFO *args_arr,
uint *arg_count, size_t *length, uint *flags)
{
fmt= get_length(fmt+1, length, flags);
*arg_count= max(*arg_count, (uint) *length);
(*length)--;
DBUG_ASSERT(*fmt == '$' && *length < MAX_ARGS);
args_arr[*length].arg_type= 'd';
args_arr[*length].have_longlong= 0;
return fmt+1;
}
/**
Calculates print width or index of positional argument
@param fmt processed string
@param have_longlong TRUE if longlong is required
@retval
string position right after modifier symbol
*/
static const char *check_longlong(const char *fmt, uint *have_longlong)
{
*have_longlong= 0;
if (*fmt == 'l')
{
fmt++;
if (*fmt != 'l')
*have_longlong= (sizeof(long) == sizeof(longlong));
else
{
fmt++;
*have_longlong= 1;
}
}
else if (*fmt == 'z')
{
fmt++;
*have_longlong= (sizeof(size_t) == sizeof(longlong));
}
else if (*fmt == 'p')
*have_longlong= (sizeof(void *) == sizeof(longlong));
return fmt;
}
/**
Returns escaped string
@param cs string charset
@param to buffer where escaped string will be placed
@param end end of buffer
@param par string to escape
@param par_len string length
@param quote_char character for quoting
@retval
position in buffer which points on the end of escaped string
*/
static char *backtick_string(CHARSET_INFO *cs, char *to, char *end,
char *par, size_t par_len, char quote_char)
{
uint char_len;
char *start= to;
char *par_end= par + par_len;
size_t buff_length= (size_t) (end - to);
if (buff_length <= par_len)
goto err;
*start++= quote_char;
for ( ; par < par_end; par+= char_len)
{
uchar c= *(uchar *) par;
if (!(char_len= my_mbcharlen(cs, c)))
char_len= 1;
if (char_len == 1 && c == (uchar) quote_char )
{
if (start + 1 >= end)
goto err;
*start++= quote_char;
}
if (start + char_len >= end)
goto err;
start= strnmov(start, par, char_len);
}
if (start + 1 >= end)
goto err;
*start++= quote_char;
return start;
err:
*to='\0';
return to;
}
/**
Prints string argument
*/
static char *process_str_arg(CHARSET_INFO *cs, char *to, char *end,
size_t width, char *par, uint print_type)
{
int well_formed_error;
size_t plen, left_len= (size_t) (end - to) + 1;
if (!par)
par = (char*) "(null)";
plen= strnlen(par, width);
if (left_len <= plen)
plen = left_len - 1;
plen= cs->cset->well_formed_len(cs, par, par + plen,
width, &well_formed_error);
if (print_type & ESCAPED_ARG)
to= backtick_string(cs, to, end, par, plen, '`');
else
to= strnmov(to,par,plen);
return to;
}
/**
Prints binary argument
*/
static char *process_bin_arg(char *to, char *end, size_t width, char *par)
{
DBUG_ASSERT(to <= end);
if (to + width + 1 > end)
width= end - to - 1; /* sign doesn't matter */
memmove(to, par, width);
to+= width;
return to;
}
/**
Prints double or float argument
*/
static char *process_dbl_arg(char *to, char *end, size_t width,
double par, char arg_type)
{
if (width == MAX_WIDTH)
width= FLT_DIG; /* width not set, use default */
else if (width >= NOT_FIXED_DEC)
width= NOT_FIXED_DEC - 1; /* max.precision for my_fcvt() */
width= min(width, (size_t)(end-to) - 1);
if (arg_type == 'f')
to+= my_fcvt(par, (int)width , to, NULL);
else
to+= my_gcvt(par, MY_GCVT_ARG_DOUBLE, (int) width , to, NULL);
return to;
}
/**
Prints integer argument
*/
static char *process_int_arg(char *to, char *end, size_t length,
longlong par, char arg_type, uint print_type)
{
size_t res_length, to_length;
char *store_start= to, *store_end;
char buff[32];
if ((to_length= (size_t) (end-to)) < 16 || length)
store_start= buff;
if (arg_type == 'd' || arg_type == 'i')
store_end= longlong10_to_str(par, store_start, -10);
else if (arg_type == 'u')
store_end= longlong10_to_str(par, store_start, 10);
else if (arg_type == 'p')
{
store_start[0]= '0';
store_start[1]= 'x';
store_end= ll2str(par, store_start + 2, 16, 0);
}
else if (arg_type == 'o')
{
store_end= ll2str(par, store_start, 8, 0);
}
else
{
DBUG_ASSERT(arg_type == 'X' || arg_type =='x');
store_end= ll2str(par, store_start, 16, (arg_type == 'X'));
}
if ((res_length= (size_t) (store_end - store_start)) > to_length)
return to; /* num doesn't fit in output */
/* If %#d syntax was used, we have to pre-zero/pre-space the string */
if (store_start == buff)
{
length= min(length, to_length);
if (res_length < length)
{
size_t diff= (length- res_length);
bfill(to, diff, (print_type & PREZERO_ARG) ? '0' : ' ');
if (arg_type == 'p' && print_type & PREZERO_ARG)
{
if (diff > 1)
to[1]= 'x';
else
store_start[0]= 'x';
store_start[1]= '0';
}
to+= diff;
}
bmove(to, store_start, res_length);
}
to+= res_length;
return to;
}
/**
Procesed positional arguments.
@param cs string charset
@param to buffer where processed string will be place
@param end end of buffer
@param par format string
@param arg_index arg index of the first occurrence of positional arg
@param ap list of parameters
@retval
end of buffer where processed string is placed
*/
static char *process_args(CHARSET_INFO *cs, char *to, char *end,
const char* fmt, size_t arg_index, va_list ap)
{
ARGS_INFO args_arr[MAX_ARGS];
PRINT_INFO print_arr[MAX_PRINT_INFO];
uint idx= 0, arg_count= arg_index;
start:
/* Here we are at the beginning of positional argument, right after $ */
arg_index--;
print_arr[idx].flags= 0;
if (*fmt == '`')
{
print_arr[idx].flags|= ESCAPED_ARG;
fmt++;
}
if (*fmt == '-')
fmt++;
print_arr[idx].length= print_arr[idx].width= 0;
/* Get print length */
if (*fmt == '*')
{
fmt= get_length_arg(fmt, args_arr, &arg_count, &print_arr[idx].length,
&print_arr[idx].flags);
print_arr[idx].flags|= LENGTH_ARG;
}
else
fmt= get_length(fmt, &print_arr[idx].length, &print_arr[idx].flags);
if (*fmt == '.')
{
uint unused_flags= 0;
fmt++;
/* Get print width */
if (*fmt == '*')
{
fmt= get_length_arg(fmt, args_arr, &arg_count, &print_arr[idx].width,
&unused_flags);
print_arr[idx].flags|= WIDTH_ARG;
}
else
fmt= get_length(fmt, &print_arr[idx].width, &unused_flags);
}
else
print_arr[idx].width= MAX_WIDTH;
fmt= check_longlong(fmt, &args_arr[arg_index].have_longlong);
args_arr[arg_index].arg_type= print_arr[idx].arg_type= *fmt;
print_arr[idx].arg_idx= arg_index;
print_arr[idx].begin= ++fmt;
while (*fmt && *fmt != '%')
fmt++;
if (!*fmt) /* End of format string */
{
uint i;
print_arr[idx].end= fmt;
/* Obtain parameters from the list */
for (i= 0 ; i < arg_count; i++)
{
switch (args_arr[i].arg_type) {
case 's':
case 'b':
args_arr[i].str_arg= va_arg(ap, char *);
break;
case 'f':
case 'g':
args_arr[i].double_arg= va_arg(ap, double);
break;
case 'd':
case 'i':
case 'u':
case 'x':
case 'X':
case 'o':
case 'p':
if (args_arr[i].have_longlong)
args_arr[i].longlong_arg= va_arg(ap,longlong);
else if (args_arr[i].arg_type == 'd' || args_arr[i].arg_type == 'i')
args_arr[i].longlong_arg= va_arg(ap, int);
else
args_arr[i].longlong_arg= va_arg(ap, uint);
break;
case 'M':
case 'c':
args_arr[i].longlong_arg= va_arg(ap, int);
break;
default:
DBUG_ASSERT(0);
}
}
/* Print result string */
for (i= 0; i <= idx; i++)
{
size_t width= 0, length= 0;
switch (print_arr[i].arg_type) {
case 's':
{
char *par= args_arr[print_arr[i].arg_idx].str_arg;
width= (print_arr[i].flags & WIDTH_ARG)
? (size_t)args_arr[print_arr[i].width].longlong_arg
: print_arr[i].width;
to= process_str_arg(cs, to, end, width, par, print_arr[i].flags);
break;
}
case 'b':
{
char *par = args_arr[print_arr[i].arg_idx].str_arg;
width= (print_arr[i].flags & WIDTH_ARG)
? (size_t)args_arr[print_arr[i].width].longlong_arg
: print_arr[i].width;
to= process_bin_arg(to, end, width, par);
break;
}
case 'c':
{
if (to == end)
break;
*to++= (char) args_arr[print_arr[i].arg_idx].longlong_arg;
break;
}
case 'f':
case 'g':
{
double d= args_arr[print_arr[i].arg_idx].double_arg;
width= (print_arr[i].flags & WIDTH_ARG) ?
(uint)args_arr[print_arr[i].width].longlong_arg : print_arr[i].width;
to= process_dbl_arg(to, end, width, d, print_arr[i].arg_type);
break;
}
case 'd':
case 'i':
case 'u':
case 'x':
case 'X':
case 'o':
case 'p':
{
/* Integer parameter */
longlong larg;
length= (print_arr[i].flags & LENGTH_ARG)
? (size_t)args_arr[print_arr[i].length].longlong_arg
: print_arr[i].length;
larg = args_arr[print_arr[i].arg_idx].longlong_arg;
to= process_int_arg(to, end, length, larg, print_arr[i].arg_type,
print_arr[i].flags);
break;
}
case 'M':
{
longlong larg;
char *org_to= to;
char errmsg_buff[MYSYS_STRERROR_SIZE];
length= (print_arr[i].flags & WIDTH_ARG)
? (size_t)args_arr[print_arr[i].width].longlong_arg
: print_arr[i].width;
larg = args_arr[print_arr[i].arg_idx].longlong_arg;
to= process_int_arg(to, end, 0, larg, 'd', print_arr[i].flags);
width-= (to - org_to);
if (width <= 4)
break;
*to++= ' ';
*to++= '"';
my_strerror(errmsg_buff, sizeof(errmsg_buff), (int) larg);
to= process_str_arg(cs, to, end, width-3, errmsg_buff,
print_arr[i].flags);
*to++= '"';
break;
}
default:
break;
}
if (to == end)
break;
/* Copy data after the % format expression until next % */
length= min(end - to , print_arr[i].end - print_arr[i].begin);
if (to + length < end)
length++;
to= strnmov(to, print_arr[i].begin, length);
}
DBUG_ASSERT(to <= end);
*to='\0'; /* End of errmessage */
return to;
}
else
{
uint unused_flags= 0;
/* Process next positional argument*/
DBUG_ASSERT(*fmt == '%');
print_arr[idx].end= fmt - 1;
idx++;
fmt++;
arg_index= 0;
fmt= get_length(fmt, &arg_index, &unused_flags);
DBUG_ASSERT(*fmt == '$');
fmt++;
arg_count= max(arg_count, arg_index);
goto start;
}
return 0;
}
/**
Produces output string according to a format string
See the detailed documentation around my_snprintf_service_st
@param cs string charset
@param to buffer where processed string will be place
@param n size of buffer
@param par format string
@param ap list of parameters
@retval
length of result string
*/
size_t my_vsnprintf_ex(CHARSET_INFO *cs, char *to, size_t n,
const char* fmt, va_list ap)
{
char *start=to, *end=to+n-1;
size_t length, width;
uint print_type, have_longlong;
for (; *fmt ; fmt++)
{
if (*fmt != '%')
{
if (to == end) /* End of buffer */
break;
*to++= *fmt; /* Copy ordinary char */
continue;
}
fmt++; /* skip '%' */
length= width= 0;
print_type= 0;
/* Read max fill size (only used with %d and %u) */
if (my_isdigit(&my_charset_latin1, *fmt))
{
fmt= get_length(fmt, &length, &print_type);
if (*fmt == '$')
{
to= process_args(cs, to, end, (fmt+1), length, ap);
return (size_t) (to - start);
}
}
else
{
if (*fmt == '`')
{
print_type|= ESCAPED_ARG;
fmt++;
}
if (*fmt == '-')
fmt++;
if (*fmt == '*')
{
fmt++;
length= va_arg(ap, int);
}
else
fmt= get_length(fmt, &length, &print_type);
}
if (*fmt == '.')
{
uint unused_flags= 0;
fmt++;
if (*fmt == '*')
{
fmt++;
width= va_arg(ap, int);
}
else
fmt= get_length(fmt, &width, &unused_flags);
}
else
width= MAX_WIDTH;
fmt= check_longlong(fmt, &have_longlong);
if (*fmt == 's') /* String parameter */
{
reg2 char *par= va_arg(ap, char *);
to= process_str_arg(cs, to, end, width, par, print_type);
continue;
}
else if (*fmt == 'b') /* Buffer parameter */
{
char *par = va_arg(ap, char *);
to= process_bin_arg(to, end, width, par);
continue;
}
else if (*fmt == 'f' || *fmt == 'g')
{
double d= va_arg(ap, double);
to= process_dbl_arg(to, end, width, d, *fmt);
continue;
}
else if (*fmt == 'd' || *fmt == 'i' || *fmt == 'u' || *fmt == 'x' ||
*fmt == 'X' || *fmt == 'p' || *fmt == 'o')
{
/* Integer parameter */
longlong larg;
if (have_longlong)
larg = va_arg(ap,longlong);
else if (*fmt == 'd' || *fmt == 'i')
larg = va_arg(ap, int);
else
larg= va_arg(ap, uint);
to= process_int_arg(to, end, length, larg, *fmt, print_type);
continue;
}
else if (*fmt == 'c') /* Character parameter */
{
register int larg;
if (to == end)
break;
larg = va_arg(ap, int);
*to++= (char) larg;
continue;
}
else if (*fmt == 'M')
{
const char *org_to= to;
int larg= va_arg(ap, int);
to= process_int_arg(to, end, 0, larg, 'd', print_type);
width-= (to - org_to);
if ((end - to) >= 4 && (int) width >= 4)
{
char errmsg_buff[MYSYS_STRERROR_SIZE];
*to++= ' ';
*to++= '"';
my_strerror(errmsg_buff, sizeof(errmsg_buff), larg);
to= process_str_arg(cs, to, end, width-3, errmsg_buff, print_type);
*to++= '"';
}
continue;
}
/* We come here on '%%', unknown code or too long parameter */
if (to == end)
break;
*to++='%'; /* % used as % or unknown code */
}
DBUG_ASSERT(to <= end);
*to='\0'; /* End of errmessage */
return (size_t) (to - start);
}
/*
Limited snprintf() implementations
exported to plugins as a service, see the detailed documentation
around my_snprintf_service_st
*/
size_t my_vsnprintf(char *to, size_t n, const char* fmt, va_list ap)
{
return my_vsnprintf_ex(&my_charset_latin1, to, n, fmt, ap);
}
size_t my_snprintf(char* to, size_t n, const char* fmt, ...)
{
size_t result;
va_list args;
va_start(args,fmt);
result= my_vsnprintf(to, n, fmt, args);
va_end(args);
return result;
}
/**
Writes output to the stream according to a format string.
@param stream file to write to
@param format string format
@param args list of parameters
@retval
number of the characters written.
*/
int my_vfprintf(FILE *stream, const char* format, va_list args)
{
char cvtbuf[1024];
int alloc= 0;
char *p= cvtbuf;
size_t cur_len= sizeof(cvtbuf);
int ret;
/*
We do not know how much buffer we need.
So start with a reasonably-sized stack-allocated buffer, and increase
it exponentially until it is big enough.
*/
for (;;)
{
size_t new_len;
size_t actual= my_vsnprintf(p, cur_len, format, args);
if (actual < cur_len - 1)
break;
/*
Not enough space (or just enough with nothing to spare - but we cannot
distinguish this case from the return value). Allocate a bigger buffer
and try again.
*/
if (alloc)
(*my_str_free)(p);
else
alloc= 1;
new_len= cur_len*2;
if (new_len < cur_len)
return 0; /* Overflow */
cur_len= new_len;
p= (*my_str_malloc)(cur_len);
if (!p)
return 0;
}
ret= fprintf(stream, "%s", p);
if (alloc)
(*my_str_free)(p);
return ret;
}
int my_fprintf(FILE *stream, const char* format, ...)
{
int result;
va_list args;
va_start(args, format);
result= my_vfprintf(stream, format, args);
va_end(args);
return result;
}
/*
Return system error text for given error number
@param buf Buffer (of size MYSYS_STRERROR_SIZE)
@param len Length of buffer
@param nr Error number
*/
void my_strerror(char *buf, size_t len, int nr)
{
char *msg= NULL;
buf[0]= '\0'; /* failsafe */
if (nr <= 0)
{
strmake(buf, (nr == 0 ?
"Internal error/check (Not system error)" :
"Internal error < 0 (Not system error)"),
len-1);
return;
}
/*
These (handler-) error messages are shared by perror, as required
by the principle of least surprise.
*/
if ((nr >= HA_ERR_FIRST) && (nr <= HA_ERR_LAST))
{
msg= (char *) handler_error_messages[nr - HA_ERR_FIRST];
strmake(buf, msg, len - 1);
}
else
{
/*
On Windows, do things the Windows way. On a system that supports both
the GNU and the XSI variant, use whichever was configured (GNU); if
this choice is not advertised, use the default (POSIX/XSI). Testing
for __GNUC__ is not sufficient to determine whether this choice exists.
*/
#if defined(__WIN__)
strerror_s(buf, len, nr);
#elif ((defined _POSIX_C_SOURCE && (_POSIX_C_SOURCE >= 200112L)) || \
(defined _XOPEN_SOURCE && (_XOPEN_SOURCE >= 600))) && \
! defined _GNU_SOURCE
strerror_r(nr, buf, len); /* I can build with or without GNU */
#elif defined _GNU_SOURCE
char *r= strerror_r(nr, buf, len);
if (r != buf) /* Want to help, GNU? */
strmake(buf, r, len - 1); /* Then don't. */
#else
strerror_r(nr, buf, len);
#endif
}
/*
strerror() return values are implementation-dependent, so let's
be pragmatic.
*/
if (!buf[0])
strmake(buf, "unknown error", len - 1);
}