Пример #1
0
my_bool dynstr_append_os_quoted(DYNAMIC_STRING *str, const char *append, ...)
{
#ifdef __WIN__
  const char *quote_str= "\"";
  const uint  quote_len= 1;
#else
  const char *quote_str= "\'";
  const uint  quote_len= 1;
#endif /* __WIN__ */
  my_bool ret= TRUE;
  va_list dirty_text;

  ret&= dynstr_append_mem(str, quote_str, quote_len); /* Leading quote */
  va_start(dirty_text, append);
  while (append != NullS)
  {
    const char  *cur_pos= append;
    const char *next_pos= cur_pos;

    /* Search for quote in each string and replace with escaped quote */
    while(*(next_pos= strcend(cur_pos, quote_str[0])) != '\0')
    {
      ret&= dynstr_append_mem(str, cur_pos, (uint) (next_pos - cur_pos));
      ret&= dynstr_append_mem(str ,"\\", 1);
      ret&= dynstr_append_mem(str, quote_str, quote_len);
      cur_pos= next_pos + 1;
    }
    ret&= dynstr_append_mem(str, cur_pos, (uint) (next_pos - cur_pos));
    append= va_arg(dirty_text, char *);
  }
  va_end(dirty_text);
  ret&= dynstr_append_mem(str, quote_str, quote_len); /* Trailing quote */

  return ret;
}
Пример #2
0
my_bool init_tmpdir(MY_TMPDIR *tmpdir, const char *pathlist)
{
    char *end, *copy;
    char buff[FN_REFLEN];
    DBUG_ENTER("init_tmpdir");
    DBUG_PRINT("enter", ("pathlist: %s", pathlist ? pathlist : "NULL"));

    mysql_mutex_init(key_TMPDIR_mutex, &tmpdir->mutex, MY_MUTEX_INIT_FAST);
    if (my_init_dynamic_array(&tmpdir->full_list, sizeof(char*), 1, 5))
        goto err;
    if (!pathlist || !pathlist[0])
    {
        /* Get default temporary directory */
        pathlist=getenv("TMPDIR");	/* Use this if possible */
#if defined(_WIN32)
        if (!pathlist)
            pathlist=getenv("TEMP");
        if (!pathlist)
            pathlist=getenv("TMP");
#endif
        if (!pathlist || !pathlist[0])
            pathlist=(char*) P_tmpdir;
    }
    do
    {
        size_t length;
        end=strcend(pathlist, DELIM);
        strmake(buff, pathlist, (uint) (end-pathlist));
        length= cleanup_dirname(buff, buff);
        if (!(copy= my_strndup(key_memory_MY_TMPDIR_full_list,
                               buff, length, MYF(MY_WME))) ||
                insert_dynamic(&tmpdir->full_list, &copy))
            DBUG_RETURN(TRUE);
        pathlist=end+1;
    }
    while (*end);
    freeze_size(&tmpdir->full_list);
    tmpdir->list=(char **)tmpdir->full_list.buffer;
    tmpdir->max=tmpdir->full_list.elements-1;
    tmpdir->cur=0;
    DBUG_RETURN(FALSE);

err:
    delete_dynamic(&tmpdir->full_list);           /* Safe to free */
    mysql_mutex_destroy(&tmpdir->mutex);
    DBUG_RETURN(TRUE);
}
Пример #3
0
int handle_options(int *argc, char ***argv, 
		   const struct my_option *longopts, 
		   my_bool (*get_one_option)(int,
					     const struct my_option *,
					     char *))
{
  uint opt_found, argvpos= 0, length, i;
  my_bool end_of_options= 0, must_be_var, set_maximum_value, special_used,
          option_is_loose;
  char *progname= *(*argv), **pos, **pos_end, *optend, *prev_found;
  const struct my_option *optp;
  int error;

  LINT_INIT(opt_found);
  (*argc)--; /* Skip the program name */
  (*argv)++; /*      --- || ----      */
  init_variables(longopts);

  for (pos= *argv, pos_end=pos+ *argc; pos != pos_end ; pos++)
  {
    char *cur_arg= *pos;
    if (cur_arg[0] == '-' && cur_arg[1] && !end_of_options) /* must be opt */
    {
      char *argument=    0;
      must_be_var=       0;
      set_maximum_value= 0;
      special_used=      0;
      option_is_loose=   0;

      cur_arg++;		/* skip '-' */
      if (*cur_arg == '-' || *cur_arg == 'O') /* check for long option, */
      {                                       /* --set-variable, or -O  */
	if (*cur_arg == 'O')
	{
	  must_be_var= 1;

	  if (!(*++cur_arg))	/* If not -Ovar=# */
	  {
	    /* the argument must be in next argv */
	    if (!*++pos)
	    {
	      if (my_getopt_print_errors)
		fprintf(stderr, "%s: Option '-O' requires an argument\n",
			progname);
	      return EXIT_ARGUMENT_REQUIRED;
	    }
	    cur_arg= *pos;
	    (*argc)--;
	  }
	}
	else if (!getopt_compare_strings(cur_arg, "-set-variable", 13))
	{
	  must_be_var= 1;
	  if (cur_arg[13] == '=')
	  {
	    cur_arg+= 14;
	    if (!*cur_arg)
	    {
	      if (my_getopt_print_errors)
		fprintf(stderr,
			"%s: Option '--set-variable' requires an argument\n",
			progname);
	      return EXIT_ARGUMENT_REQUIRED;
	    }
	  }
	  else if (cur_arg[14]) /* garbage, or another option. break out */
	    must_be_var= 0;
	  else
	  {
	    /* the argument must be in next argv */
	    if (!*++pos)
	    {
	      if (my_getopt_print_errors)
		fprintf(stderr,
			"%s: Option '--set-variable' requires an argument\n",
			progname);
	      return EXIT_ARGUMENT_REQUIRED;
	    }
	    cur_arg= *pos;
	    (*argc)--;
	  }
	}
	else if (!must_be_var)
	{
	  if (!*++cur_arg)	/* skip the double dash */
	  {
	    /* '--' means end of options, look no further */
	    end_of_options= 1;
	    (*argc)--;
	    continue;
	  }
	}
	optend= strcend(cur_arg, '=');
	length= optend - cur_arg;
	if (*optend == '=')
	  optend++;
	else
	  optend=0;

	/*
	  Find first the right option. Return error in case of an ambiguous,
	  or unknown option
	*/
	optp= longopts;
	if (!(opt_found= findopt(cur_arg, length, &optp, &prev_found)))
	{
	  /*
	    Didn't find any matching option. Let's see if someone called
	    option with a special option prefix
	  */
	  if (!must_be_var)
	  {
	    if (optend)
	      must_be_var= 1; /* option is followed by an argument */
	    for (i= 0; special_opt_prefix[i]; i++)
	    {
	      if (!getopt_compare_strings(special_opt_prefix[i], cur_arg,
					  special_opt_prefix_lengths[i]) &&
		  cur_arg[special_opt_prefix_lengths[i]] == '-')
	      {
		/*
		  We were called with a special prefix, we can reuse opt_found
		*/
		special_used= 1;
		cur_arg+= (special_opt_prefix_lengths[i] + 1);
		if (i == OPT_LOOSE)
		  option_is_loose= 1;
		if ((opt_found= findopt(cur_arg, length -
					(special_opt_prefix_lengths[i] + 1),
					&optp, &prev_found)))
		{
		  if (opt_found > 1)
		  {
		    if (my_getopt_print_errors)
		      fprintf(stderr,
			      "%s: ambiguous option '--%s-%s' (--%s-%s)\n",
			      progname, special_opt_prefix[i], cur_arg,
			      special_opt_prefix[i], prev_found);
		    return EXIT_AMBIGUOUS_OPTION;
		  }
		  switch (i) {
		  case OPT_SKIP:
		  case OPT_DISABLE: /* fall through */
		    /*
		      double negation is actually enable again,
		      for example: --skip-option=0 -> option = TRUE
		    */
		    optend= (optend && *optend == '0' && !(*(optend + 1))) ?
		      (char*) "1" : disabled_my_option;
		    break;
		  case OPT_ENABLE:
		    optend= (optend && *optend == '0' && !(*(optend + 1))) ?
 		      disabled_my_option : (char*) "1";
		    break;
		  case OPT_MAXIMUM:
		    set_maximum_value= 1;
		    must_be_var= 1;
		    break;
		  }
		  break; /* break from the inner loop, main loop continues */
		}
	      }
	    }
	  }
	  if (!opt_found)
	  {
	    if (must_be_var)
	    {
	      if (my_getopt_print_errors)
		fprintf(stderr,
			"%s: %s: unknown variable '%s'\n", progname,
			option_is_loose ? "WARNING" : "ERROR", cur_arg);
	      if (!option_is_loose)
		return EXIT_UNKNOWN_VARIABLE;
	    }
	    else
	    {
	      if (my_getopt_print_errors)
		fprintf(stderr,
			"%s: %s: unknown option '--%s'\n", progname,
			option_is_loose ? "WARNING" : "ERROR", cur_arg);
	      if (!option_is_loose)
		return EXIT_UNKNOWN_OPTION;
	    }
	    if (option_is_loose)
	    {
	      (*argc)--;
	      continue;
	    }
	  }
	}
	if (opt_found > 1)
	{
	  if (must_be_var)
	  {
	    if (my_getopt_print_errors)
	      fprintf(stderr, "%s: variable prefix '%s' is not unique\n",
		      progname, cur_arg);
	    return EXIT_VAR_PREFIX_NOT_UNIQUE;
	  }
	  else
	  {
	    if (my_getopt_print_errors)
	      fprintf(stderr, "%s: ambiguous option '--%s' (%s, %s)\n",
		      progname, cur_arg, prev_found, optp->name);
	    return EXIT_AMBIGUOUS_OPTION;
	  }
	}
	if (must_be_var && optp->var_type == GET_NO_ARG)
	{
	  if (my_getopt_print_errors)
	    fprintf(stderr, "%s: option '%s' cannot take an argument\n",
		    progname, optp->name);
	  return EXIT_NO_ARGUMENT_ALLOWED;
	}
	if (optp->arg_type == NO_ARG)
	{
	  if (optend && optp->var_type != GET_BOOL)
	  {
	    if (my_getopt_print_errors)
	      fprintf(stderr, "%s: option '--%s' cannot take an argument\n",
		      progname, optp->name);
	    return EXIT_NO_ARGUMENT_ALLOWED;
	  }
	  if (optp->var_type == GET_BOOL)
	  {
	    /*
	      Set bool to 1 if no argument or if the user has used
	      --enable-'option-name'.
	      *optend was set to '0' if one used --disable-option
	      */
	    *((my_bool*) optp->value)= 	(my_bool) (!optend || *optend == '1');
	    (*argc)--;	    
	    get_one_option(optp->id, optp, argument);
	    continue;
	  }
	  argument= optend;
	}
	else if (optp->arg_type == OPT_ARG && optp->var_type == GET_BOOL)
	{
	  if (optend == disabled_my_option)
	    *((my_bool*) optp->value)= (my_bool) 0;
	  else
	  {
	    if (!optend) /* No argument -> enable option */
	      *((my_bool*) optp->value)= (my_bool) 1;
	    else /* If argument differs from 0, enable option, else disable */
	      *((my_bool*) optp->value)= (my_bool) atoi(optend) != 0;
	  }
	  (*argc)--;	    
	  continue;
	}
	else if (optp->arg_type == REQUIRED_ARG && !optend)
	{
	  /* Check if there are more arguments after this one */
	  if (!*++pos)
	  {
	    if (my_getopt_print_errors)
	      fprintf(stderr, "%s: option '--%s' requires an argument\n",
		      progname, optp->name);
	    return EXIT_ARGUMENT_REQUIRED;
	  }
	  argument= *pos;
	  (*argc)--;
	}
	else
	  argument= optend;
      }
      else  /* must be short option */
      {
	for (optend= cur_arg; *optend; optend++)
	{
	  opt_found= 0;
	  for (optp= longopts; optp->id; optp++)
	  {
	    if (optp->id == (int) (uchar) *optend)
	    {
	      /* Option recognized. Find next what to do with it */
	      opt_found= 1;
	      if (optp->var_type == GET_BOOL && optp->arg_type == NO_ARG)
	      {
		*((my_bool*) optp->value)= (my_bool) 1;
		get_one_option(optp->id, optp, argument);
		continue;
	      }
	      else if (optp->arg_type == REQUIRED_ARG ||
		       optp->arg_type == OPT_ARG)
	      {
		if (*(optend + 1))
		{
		  /* The rest of the option is option argument */
		  argument= optend + 1;
		  /* This is in effect a jump out of the outer loop */
		  optend= (char*) " ";
		}
		else if (optp->arg_type == REQUIRED_ARG)
		{
		  /* Check if there are more arguments after this one */
		  if (!*++pos)
		  {
		    if (my_getopt_print_errors)
		      fprintf(stderr,
			      "%s: option '-%c' requires an argument\n",
			      progname, optp->id);
		    return EXIT_ARGUMENT_REQUIRED;
		  }
		  argument= *pos;
		  (*argc)--;
		  /* the other loop will break, because *optend + 1 == 0 */
		}
	      }
	      if ((error= setval(optp, argument, set_maximum_value)))
	      {
		fprintf(stderr,
			"%s: Error while setting value '%s' to '%s'\n",
			progname, argument, optp->name);
		return error;
	      }
	      get_one_option(optp->id, optp, argument);
	      break;
	    }
	  }
	  if (!opt_found)
	  {
	    if (my_getopt_print_errors)
	      fprintf(stderr,
		      "%s: unknown option '-%c'\n", progname, *optend);
	    return EXIT_UNKNOWN_OPTION;
	  }
	}
	(*argc)--; /* option handled (short), decrease argument count */
	continue;
      }
      if ((error= setval(optp, argument, set_maximum_value)))
      {
	fprintf(stderr,
		"%s: Error while setting value '%s' to '%s'\n",
		progname, argument, optp->name);
	return error;
      }
      get_one_option(optp->id, optp, argument);

      (*argc)--; /* option handled (short�or�long), decrease argument count */
    }
    else /* non-option found */
      (*argv)[argvpos++]= cur_arg;
  }
  /*
    Destroy the first, already handled option, so that programs that look
    for arguments in 'argv', without checking 'argc', know when to stop.
    Items in argv, before the destroyed one, are all non-option -arguments
    to the program, yet to be (possibly) handled.
  */
  (*argv)[argvpos]= 0;
  return 0;
}
Пример #4
0
static uchar* my_hash_get_string(const uchar *record, size_t *length,
                                my_bool first __attribute__ ((unused)))
{
  *length= (size_t) (strcend((const char*) record,',')- (const char*) record);
  return (uchar*) record;
}
Пример #5
0
static void convert_from_float(MYSQL_BIND *r_param, const MYSQL_FIELD *field, double val, int size)
{
  double check_trunc_val= (val > 0) ? floor(val) : -floor(-val);
  char *buf= (char *)r_param->buffer;
  switch (r_param->buffer_type)
  {
    case MYSQL_TYPE_TINY:
      *buf= (r_param->is_unsigned) ? (uint8)val : (int8)val;
      *r_param->error= check_trunc_val != (r_param->is_unsigned ? (double)((uint8)*buf) :
                                          (double)((int8)*buf));
      r_param->buffer_length= 1;
    break;
    case MYSQL_TYPE_SHORT:
    case MYSQL_TYPE_YEAR:
    {
      if (r_param->is_unsigned)
      {
        ushort sval= (ushort)val;
        shortstore(buf, sval);
        *r_param->error= check_trunc_val != (double)sval;
      } else { 
        short sval= (short)val;
        shortstore(buf, sval);
        *r_param->error= check_trunc_val != (double)sval;
      } 
      r_param->buffer_length= 2;
    }
    break; 
    case MYSQL_TYPE_LONG:
    {
      if (r_param->is_unsigned)
      {
        uint32 lval= (uint32)val;
        longstore(buf, lval);
        *r_param->error= (check_trunc_val != (double)lval);
      } else {
        int32 lval= (int32)val;
        longstore(buf, lval);
        *r_param->error= (check_trunc_val != (double)lval);
      }
      r_param->buffer_length= 4;
    }
    break; 
    case MYSQL_TYPE_LONGLONG:
    {
      if (r_param->is_unsigned)
      {
        ulonglong llval= (ulonglong)val;
        longlongstore(buf, llval);
        *r_param->error= (check_trunc_val != (double)llval);
      } else {
        longlong llval= (longlong)val;
        longlongstore(buf, llval);
        *r_param->error= (check_trunc_val != (double)llval);
      }
      r_param->buffer_length= 8;
    }
    break; 
    case MYSQL_TYPE_FLOAT:
    {
      float fval= (float)val;
      memcpy(buf, &fval, sizeof(float));
      *r_param->error= (*(float*)buf != fval);
      r_param->buffer_length= 4;
    }
    break;
    case MYSQL_TYPE_DOUBLE:
    {
      memcpy(buf, &val, sizeof(double));
      r_param->buffer_length= 8;
    }
    break;
    default:
    {
 #define MAX_DOUBLE_STRING_REP_LENGTH 300
     char buff[MAX_DOUBLE_STRING_REP_LENGTH];
     char *end;
     size_t length;

     length= MIN(MAX_DOUBLE_STRING_REP_LENGTH - 1, r_param->buffer_length);

     if (field->decimals >= NOT_FIXED_DEC)
     {
       sprintf(buff, "%-*.*g", (int) length-1, DBL_DIG, val);
       length= strlen(buff);
     }
     else
     {
       sprintf(buff, "%.*f", field->decimals, val);
       length= strlen(buff);
     }

     /* remove possible trailing blanks */
     if ((end= strcend(buff, ' ')))
       *end= 0;

     /* check if ZEROFILL flag is active */
     if (field->flags & ZEROFILL_FLAG)
     {
       /* enough space available ? */
       if (field->length < length || field->length > MAX_DOUBLE_STRING_REP_LENGTH - 1)
         break;
       bmove_upp(buff + field->length, buff + length, length);
       bfill((char*) buff, field->length - length, '0');
     }
     convert_from_string(r_param, buff, strlen(buff));
    }  
    break;
  } 
}