@@ -151,7 +151,7 @@ static my_bool ignore_errors=0,wait_flag=0,quick=0,
151151 default_pager_set= 0 , opt_sigint_ignore= 0 ,
152152 auto_vertical_output= 0 ,
153153 show_warnings= 0 , executing_query= 0 , interrupted_query= 0 ,
154- ignore_spaces= 0 ;
154+ ignore_spaces= 0 , opt_binhex= 0 ;
155155static my_bool debug_info_flag, debug_check_flag;
156156static my_bool column_types_flag;
157157static my_bool preserve_comments= 0 ;
@@ -1567,6 +1567,8 @@ static struct my_option my_long_options[] =
15671567 {" bind-address" , 0 , " IP address to bind to." ,
15681568 (uchar**) &opt_bind_addr, (uchar**) &opt_bind_addr, 0 , GET_STR,
15691569 REQUIRED_ARG, 0 , 0 , 0 , 0 , 0 , 0 },
1570+ {" binary-as-hex" , ' b' , " Print binary data as hex" , &opt_binhex, &opt_binhex,
1571+ 0 , GET_BOOL, NO_ARG, 0 , 0 , 0 , 0 , 0 , 0 },
15701572 {" character-sets-dir" , OPT_CHARSETS_DIR,
15711573 " Directory for character set files." , &charsets_dir,
15721574 &charsets_dir, 0 , GET_STR, REQUIRED_ARG, 0 , 0 , 0 , 0 , 0 , 0 },
@@ -3690,6 +3692,39 @@ print_field_types(MYSQL_RES *result)
36903692 tee_puts (" " , PAGER);
36913693}
36923694
3695+ /* Used to determine if we should invoke print_as_hex for this field */
3696+
3697+ static bool
3698+ is_binary_field (MYSQL_FIELD *field)
3699+ {
3700+ if ((field->charsetnr == 63 ) &&
3701+ (field->type == MYSQL_TYPE_BIT ||
3702+ field->type == MYSQL_TYPE_BLOB ||
3703+ field->type == MYSQL_TYPE_LONG_BLOB ||
3704+ field->type == MYSQL_TYPE_MEDIUM_BLOB ||
3705+ field->type == MYSQL_TYPE_TINY_BLOB ||
3706+ field->type == MYSQL_TYPE_VAR_STRING ||
3707+ field->type == MYSQL_TYPE_STRING ||
3708+ field->type == MYSQL_TYPE_VARCHAR ||
3709+ field->type == MYSQL_TYPE_GEOMETRY))
3710+ return 1 ;
3711+ return 0 ;
3712+ }
3713+
3714+
3715+ /* Print binary value as hex literal (0x ...) */
3716+
3717+ static void
3718+ print_as_hex (FILE *output_file, const char *str, ulong len, ulong total_bytes_to_send)
3719+ {
3720+ const char *ptr= str, *end= ptr+len;
3721+ ulong i;
3722+ fprintf (output_file, " 0x" );
3723+ for (; ptr < end; ptr++)
3724+ fprintf (output_file, " %02X" , *((uchar*)ptr));
3725+ for (i= 2 *len+2 ; i < total_bytes_to_send; i++)
3726+ tee_putc ((int )' ' , output_file);
3727+ }
36933728
36943729static void
36953730print_table_data (MYSQL_RES *result)
@@ -3719,7 +3754,9 @@ print_table_data(MYSQL_RES *result)
37193754 length= max<size_t >(length, field->max_length );
37203755 if (length < 4 && !IS_NOT_NULL (field->flags ))
37213756 length=4 ;// Room for "NULL"
3722- field->max_length =length;
3757+ if (opt_binhex && is_binary_field (field))
3758+ length= 2 + length * 2 ;
3759+ field->max_length =(ulong) length;
37233760 separator.fill (separator.length ()+length+2 ,' -' );
37243761 separator.append (' +' );
37253762 }
@@ -3786,9 +3823,11 @@ print_table_data(MYSQL_RES *result)
37863823 many extra padding-characters we should send with the printing function.
37873824 */
37883825 visible_length= charset_info->cset ->numcells (charset_info, buffer, buffer + data_length);
3789- extra_padding= data_length - visible_length;
3826+ extra_padding= (uint) ( data_length - visible_length) ;
37903827
3791- if (field_max_length > MAX_COLUMN_LENGTH)
3828+ if (opt_binhex && is_binary_field (field))
3829+ print_as_hex (PAGER, cur[off], lengths[off], field_max_length);
3830+ else if (field_max_length > MAX_COLUMN_LENGTH)
37923831 tee_print_sized_data (buffer, data_length, MAX_COLUMN_LENGTH+extra_padding, FALSE );
37933832 else
37943833 {
@@ -3914,19 +3953,22 @@ print_table_data_html(MYSQL_RES *result)
39143953 if (interrupted_query)
39153954 break ;
39163955 ulong *lengths=mysql_fetch_lengths (result);
3956+ field= mysql_fetch_fields (result);
39173957 (void ) tee_fputs (" <TR>" , PAGER);
39183958 for (uint i=0 ; i < mysql_num_fields (result); i++)
39193959 {
39203960 (void ) tee_fputs (" <TD>" , PAGER);
3921- xmlencode_print (cur[i], lengths[i]);
3961+ if (opt_binhex && is_binary_field (&field[i]))
3962+ print_as_hex (PAGER, cur[i], lengths[i], lengths[i]);
3963+ else
3964+ xmlencode_print (cur[i], lengths[i]);
39223965 (void ) tee_fputs (" </TD>" , PAGER);
39233966 }
39243967 (void ) tee_fputs (" </TR>" , PAGER);
39253968 }
39263969 (void ) tee_fputs (" </TABLE>" , PAGER);
39273970}
39283971
3929-
39303972static void
39313973print_table_data_xml (MYSQL_RES *result)
39323974{
@@ -3954,7 +3996,10 @@ print_table_data_xml(MYSQL_RES *result)
39543996 if (cur[i])
39553997 {
39563998 tee_fprintf (PAGER, " \" >" );
3957- xmlencode_print (cur[i], lengths[i]);
3999+ if (opt_binhex && is_binary_field (&fields[i]))
4000+ print_as_hex (PAGER, cur[i], lengths[i], lengths[i]);
4001+ else
4002+ xmlencode_print (cur[i], lengths[i]);
39584003 tee_fprintf (PAGER, " </field>\n " );
39594004 }
39604005 else
@@ -3999,7 +4044,10 @@ print_table_data_vertically(MYSQL_RES *result)
39994044 tee_fprintf (PAGER, " %*s: " ,(int ) max_length,field->name );
40004045 if (cur[off])
40014046 {
4002- tee_write (PAGER, cur[off], lengths[off], MY_PRINT_SPS_0 | MY_PRINT_MB);
4047+ if (opt_binhex && is_binary_field (field))
4048+ print_as_hex (PAGER, cur[off], lengths[off], lengths[off]);
4049+ else
4050+ tee_write (PAGER, cur[off], lengths[off], MY_PRINT_SPS_0 | MY_PRINT_MB);
40034051 tee_putc (' \n ' , PAGER);
40044052 }
40054053 else
@@ -4108,11 +4156,21 @@ print_tab_data(MYSQL_RES *result)
41084156 while ((cur = mysql_fetch_row (result)))
41094157 {
41104158 lengths=mysql_fetch_lengths (result);
4111- safe_put_field (cur[0 ],lengths[0 ]);
4159+
4160+ field= mysql_fetch_fields (result);
4161+ if (opt_binhex && is_binary_field (&field[0 ]))
4162+ print_as_hex (PAGER, cur[0 ], lengths[0 ], lengths[0 ]);
4163+ else
4164+ safe_put_field (cur[0 ],lengths[0 ]);
4165+
41124166 for (uint off=1 ; off < mysql_num_fields (result); off++)
41134167 {
41144168 (void ) tee_fputs (" \t " , PAGER);
4115- safe_put_field (cur[off], lengths[off]);
4169+
4170+ if (opt_binhex && field && is_binary_field (&field[off]))
4171+ print_as_hex (PAGER, cur[off], lengths[off], lengths[off]);
4172+ else
4173+ safe_put_field (cur[off], lengths[off]);
41164174 }
41174175 (void ) tee_fputs (" \n " , PAGER);
41184176 }
@@ -4488,10 +4546,9 @@ com_use(String *buffer MY_ATTRIBUTE((unused)), char *line)
44884546 memset (buff, 0 , sizeof (buff));
44894547
44904548 /*
4491- In case number of quotes exceed 2, we try to get
4492- the normalized db name.
4549+ In case of quotes used, try to get the normalized db name.
44934550 */
4494- if (get_quote_count (line) > 2 )
4551+ if (get_quote_count (line) > 0 )
44954552 {
44964553 if (normalize_dbname (line, buff, sizeof (buff)))
44974554 return put_error (&mysql);
@@ -4709,11 +4766,13 @@ char *get_arg(char *line, my_bool get_next_arg)
47094766static int
47104767get_quote_count (const char *line)
47114768{
4712- int quote_count;
4713- const char *ptr = line;
4769+ int quote_count= 0 ;
4770+ const char *quote = line;
47144771
4715- for (quote_count= 0 ; ptr ++ && *ptr; ptr= strpbrk (ptr, " \"\' `" ))
4716- quote_count ++;
4772+ while ((quote= strpbrk (quote, " '`\" " )) != NULL ) {
4773+ quote_count++;
4774+ quote++;
4775+ }
47174776
47184777 return quote_count;
47194778}
0 commit comments