@@ -142,7 +142,7 @@ static my_bool ignore_errors=0,wait_flag=0,quick=0,
142142 default_pager_set= 0 , opt_sigint_ignore= 0 ,
143143 auto_vertical_output= 0 ,
144144 show_warnings= 0 , executing_query= 0 , interrupted_query= 0 ,
145- ignore_spaces= 0 ;
145+ ignore_spaces= 0 , opt_binhex= 0 ;
146146static my_bool debug_info_flag, debug_check_flag;
147147static my_bool column_types_flag;
148148static my_bool preserve_comments= 0 ;
@@ -1395,6 +1395,8 @@ static struct my_option my_long_options[] =
13951395 {" batch" , ' B' ,
13961396 " Don't use history file. Disable interactive behavior. (Enables --silent.)" ,
13971397 0 , 0 , 0 , GET_NO_ARG, NO_ARG, 0 , 0 , 0 , 0 , 0 , 0 },
1398+ {" binary-as-hex" , ' b' , " Print binary data as hex" , &opt_binhex, &opt_binhex,
1399+ 0 , GET_BOOL, NO_ARG, 0 , 0 , 0 , 0 , 0 , 0 },
13981400#ifndef MCP_WL3126
13991401 {" bind-address" , 0 , " IP address to bind to." ,
14001402 (uchar**) &opt_bind_addr, (uchar**) &opt_bind_addr, 0 , GET_STR,
@@ -3109,7 +3111,8 @@ com_go(String *buffer,char *line __attribute__((unused)))
31093111 print_table_data_html (result);
31103112 else if (opt_xml)
31113113 print_table_data_xml (result);
3112- else if (vertical || (auto_vertical_output && (terminal_width < get_result_width (result))))
3114+ else if (vertical || (auto_vertical_output &&
3115+ (terminal_width < get_result_width (result))))
31133116 print_table_data_vertically (result);
31143117 else if (opt_silent && verbose <= 2 && !output_tables)
31153118 print_tab_data (result);
@@ -3328,6 +3331,41 @@ print_field_types(MYSQL_RES *result)
33283331}
33293332
33303333
3334+ /* Used to determine if we should invoke print_as_hex for this field */
3335+
3336+ static bool
3337+ is_binary_field (MYSQL_FIELD *field)
3338+ {
3339+ if ((field->charsetnr == 63 ) &&
3340+ (field->type == MYSQL_TYPE_BIT ||
3341+ field->type == MYSQL_TYPE_BLOB ||
3342+ field->type == MYSQL_TYPE_LONG_BLOB ||
3343+ field->type == MYSQL_TYPE_MEDIUM_BLOB ||
3344+ field->type == MYSQL_TYPE_TINY_BLOB ||
3345+ field->type == MYSQL_TYPE_VAR_STRING ||
3346+ field->type == MYSQL_TYPE_STRING ||
3347+ field->type == MYSQL_TYPE_VARCHAR ||
3348+ field->type == MYSQL_TYPE_GEOMETRY))
3349+ return 1 ;
3350+ return 0 ;
3351+ }
3352+
3353+
3354+ /* Print binary value as hex literal (0x ...) */
3355+
3356+ static void
3357+ print_as_hex (FILE *output_file, const char *str, ulong len, ulong total_bytes_to_send)
3358+ {
3359+ const char *ptr= str, *end= ptr+len;
3360+ ulong i;
3361+ fprintf (output_file, " 0x" );
3362+ for (; ptr < end; ptr++)
3363+ fprintf (output_file, " %02X" , *((uchar*)ptr));
3364+ for (i= 2 *len+2 ; i < total_bytes_to_send; i++)
3365+ tee_putc ((int )' ' , output_file);
3366+ }
3367+
3368+
33313369static void
33323370print_table_data (MYSQL_RES *result)
33333371{
@@ -3354,7 +3392,9 @@ print_table_data(MYSQL_RES *result)
33543392 length=max (length,field->max_length );
33553393 if (length < 4 && !IS_NOT_NULL (field->flags ))
33563394 length=4 ; // Room for "NULL"
3357- field->max_length =length;
3395+ if (opt_binhex && is_binary_field (field))
3396+ length= 2 + length * 2 ;
3397+ field->max_length =(ulong) length;
33583398 separator.fill (separator.length ()+length+2 ,' -' );
33593399 separator.append (' +' );
33603400 }
@@ -3421,9 +3461,11 @@ print_table_data(MYSQL_RES *result)
34213461 many extra padding-characters we should send with the printing function.
34223462 */
34233463 visible_length= charset_info->cset ->numcells (charset_info, buffer, buffer + data_length);
3424- extra_padding= data_length - visible_length;
3464+ extra_padding= (uint) ( data_length - visible_length) ;
34253465
3426- if (field_max_length > MAX_COLUMN_LENGTH)
3466+ if (opt_binhex && is_binary_field (field))
3467+ print_as_hex (PAGER, cur[off], lengths[off], field_max_length);
3468+ else if (field_max_length > MAX_COLUMN_LENGTH)
34273469 tee_print_sized_data (buffer, data_length, MAX_COLUMN_LENGTH+extra_padding, FALSE );
34283470 else
34293471 {
@@ -3556,11 +3598,15 @@ print_table_data_html(MYSQL_RES *result)
35563598 if (interrupted_query)
35573599 break ;
35583600 ulong *lengths=mysql_fetch_lengths (result);
3601+ field= mysql_fetch_fields (result);
35593602 (void ) tee_fputs (" <TR>" , PAGER);
35603603 for (uint i=0 ; i < mysql_num_fields (result); i++)
35613604 {
35623605 (void ) tee_fputs (" <TD>" , PAGER);
3563- xmlencode_print (cur[i], lengths[i]);
3606+ if (opt_binhex && is_binary_field (&field[i]))
3607+ print_as_hex (PAGER, cur[i], lengths[i], lengths[i]);
3608+ else
3609+ xmlencode_print (cur[i], lengths[i]);
35643610 (void ) tee_fputs (" </TD>" , PAGER);
35653611 }
35663612 (void ) tee_fputs (" </TR>" , PAGER);
@@ -3596,7 +3642,10 @@ print_table_data_xml(MYSQL_RES *result)
35963642 if (cur[i])
35973643 {
35983644 tee_fprintf (PAGER, " \" >" );
3599- xmlencode_print (cur[i], lengths[i]);
3645+ if (opt_binhex && is_binary_field (&fields[i]))
3646+ print_as_hex (PAGER, cur[i], lengths[i], lengths[i]);
3647+ else
3648+ xmlencode_print (cur[i], lengths[i]);
36003649 tee_fprintf (PAGER, " </field>\n " );
36013650 }
36023651 else
@@ -3643,23 +3692,28 @@ print_table_data_vertically(MYSQL_RES *result)
36433692 {
36443693 unsigned int i;
36453694 const char *p;
3646-
3695+ if (opt_binhex && is_binary_field (field))
3696+ fprintf (PAGER, " 0x" );
36473697 for (i= 0 , p= cur[off]; i < lengths[off]; i+= 1 , p+= 1 )
36483698 {
3649- if (*p == ' \0 ' )
3650- tee_putc (( int ) ' ' , PAGER );
3699+ if (opt_binhex && is_binary_field (field) )
3700+ fprintf (PAGER, " %02X " , *((uchar*)p) );
36513701 else
3652- tee_putc ((int )*p, PAGER);
3702+ {
3703+ if (*p == ' \0 ' )
3704+ tee_putc ((int )' ' , PAGER);
3705+ else
3706+ tee_putc ((int )*p, PAGER);
3707+ }
36533708 }
36543709 tee_putc (' \n ' , PAGER);
36553710 }
3656- else
3711+ else
36573712 tee_fprintf (PAGER, " NULL\n " );
36583713 }
36593714 }
36603715}
36613716
3662-
36633717/* print_warnings should be called right after executing a statement */
36643718
36653719static void print_warnings ()
@@ -3796,11 +3850,19 @@ print_tab_data(MYSQL_RES *result)
37963850 while ((cur = mysql_fetch_row (result)))
37973851 {
37983852 lengths=mysql_fetch_lengths (result);
3799- safe_put_field (cur[0 ],lengths[0 ]);
3853+ field= mysql_fetch_fields (result);
3854+ if (opt_binhex && is_binary_field (&field[0 ]))
3855+ print_as_hex (PAGER, cur[0 ], lengths[0 ], lengths[0 ]);
3856+ else
3857+ safe_put_field (cur[0 ],lengths[0 ]);
3858+
38003859 for (uint off=1 ; off < mysql_num_fields (result); off++)
38013860 {
38023861 (void ) tee_fputs (" \t " , PAGER);
3803- safe_put_field (cur[off], lengths[off]);
3862+ if (opt_binhex && field && is_binary_field (&field[off]))
3863+ print_as_hex (PAGER, cur[off], lengths[off], lengths[off]);
3864+ else
3865+ safe_put_field (cur[off], lengths[off]);
38043866 }
38053867 (void ) tee_fputs (" \n " , PAGER);
38063868 }
@@ -4178,10 +4240,9 @@ com_use(String *buffer __attribute__((unused)), char *line)
41784240 bzero (buff, sizeof (buff));
41794241
41804242 /*
4181- In case number of quotes exceed 2, we try to get
4182- the normalized db name.
4243+ In case of quotes used, try to get the normalized db name.
41834244 */
4184- if (get_quote_count (line) > 2 )
4245+ if (get_quote_count (line) > 0 )
41854246 {
41864247 if (normalize_dbname (line, buff, sizeof (buff)))
41874248 return put_error (&mysql);
@@ -4399,11 +4460,13 @@ char *get_arg(char *line, my_bool get_next_arg)
43994460static int
44004461get_quote_count (const char *line)
44014462{
4402- int quote_count;
4403- const char *ptr = line;
4463+ int quote_count= 0 ;
4464+ const char *quote = line;
44044465
4405- for (quote_count= 0 ; ptr ++ && *ptr; ptr= strpbrk (ptr, " \"\' `" ))
4406- quote_count ++;
4466+ while ((quote= strpbrk (quote, " '`\" " )) != NULL ) {
4467+ quote_count++;
4468+ quote++;
4469+ }
44074470
44084471 return quote_count;
44094472}
0 commit comments