MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
typelib.c
1 /* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software
14  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
15 
16 /* Functions to handle typelib */
17 
18 #include "mysys_priv.h"
19 #include <m_string.h>
20 #include <m_ctype.h>
21 
22 
23 #define is_field_separator(X) ((X) == ',' || (X) == '=')
24 
25 int find_type_or_exit(const char *x, TYPELIB *typelib, const char *option)
26 {
27  int res;
28  const char **ptr;
29 
30  if ((res= find_type((char *) x, typelib, FIND_TYPE_BASIC)) <= 0)
31  {
32  ptr= typelib->type_names;
33  if (!*x)
34  fprintf(stderr, "No option given to %s\n", option);
35  else
36  fprintf(stderr, "Unknown option to %s: %s\n", option, x);
37  fprintf(stderr, "Alternatives are: '%s'", *ptr);
38  while (*++ptr)
39  fprintf(stderr, ",'%s'", *ptr);
40  fprintf(stderr, "\n");
41  exit(1);
42  }
43  return res;
44 }
45 
46 
68 int find_type(const char *x, const TYPELIB *typelib, uint flags)
69 {
70  int find,pos;
71  int UNINIT_VAR(findpos); /* guarded by find */
72  const char *i;
73  const char *j;
74  DBUG_ENTER("find_type");
75  DBUG_PRINT("enter",("x: '%s' lib: 0x%lx", x, (long) typelib));
76 
77  DBUG_ASSERT(!(flags & ~(FIND_TYPE_NO_PREFIX | FIND_TYPE_ALLOW_NUMBER |
78  FIND_TYPE_NO_OVERWRITE | FIND_TYPE_COMMA_TERM)));
79  if (!typelib->count)
80  {
81  DBUG_PRINT("exit",("no count"));
82  DBUG_RETURN(0);
83  }
84  find=0;
85  for (pos=0 ; (j=typelib->type_names[pos]) ; pos++)
86  {
87  for (i=x ;
88  *i && (!(flags & FIND_TYPE_COMMA_TERM) || !is_field_separator(*i)) &&
89  my_toupper(&my_charset_latin1,*i) ==
90  my_toupper(&my_charset_latin1,*j) ; i++, j++) ;
91  if (! *j)
92  {
93  while (*i == ' ')
94  i++; /* skip_end_space */
95  if (! *i || ((flags & FIND_TYPE_COMMA_TERM) && is_field_separator(*i)))
96  DBUG_RETURN(pos+1);
97  }
98  if ((!*i &&
99  (!(flags & FIND_TYPE_COMMA_TERM) || !is_field_separator(*i))) &&
100  (!*j || !(flags & FIND_TYPE_NO_PREFIX)))
101  {
102  find++;
103  findpos=pos;
104  }
105  }
106  if (find == 0 && (flags & FIND_TYPE_ALLOW_NUMBER) && x[0] == '#' &&
107  strend(x)[-1] == '#' &&
108  (findpos=atoi(x+1)-1) >= 0 && (uint) findpos < typelib->count)
109  find=1;
110  else if (find == 0 || ! x[0])
111  {
112  DBUG_PRINT("exit",("Couldn't find type"));
113  DBUG_RETURN(0);
114  }
115  else if (find != 1 || (flags & FIND_TYPE_NO_PREFIX))
116  {
117  DBUG_PRINT("exit",("Too many possybilities"));
118  DBUG_RETURN(-1);
119  }
120  DBUG_RETURN(findpos+1);
121 } /* find_type */
122 
123 
131 void make_type(register char * to, register uint nr,
132  register TYPELIB *typelib)
133 {
134  DBUG_ENTER("make_type");
135  if (!nr)
136  to[0]=0;
137  else
138  (void) strmov(to,get_type(typelib,nr-1));
139  DBUG_VOID_RETURN;
140 } /* make_type */
141 
142 
150 const char *get_type(TYPELIB *typelib, uint nr)
151 {
152  if (nr < (uint) typelib->count && typelib->type_names)
153  return(typelib->type_names[nr]);
154  return "?";
155 }
156 
157 
171 my_ulonglong find_typeset(char *x, TYPELIB *lib, int *err)
172 {
173  my_ulonglong result;
174  int find;
175  char *i;
176  DBUG_ENTER("find_set");
177  DBUG_PRINT("enter",("x: '%s' lib: 0x%lx", x, (long) lib));
178 
179  if (!lib->count)
180  {
181  DBUG_PRINT("exit",("no count"));
182  DBUG_RETURN(0);
183  }
184  result= 0;
185  *err= 0;
186  while (*x)
187  {
188  (*err)++;
189  i= x;
190  while (*x && !is_field_separator(*x))
191  x++;
192  if (x[0] && x[1]) /* skip separator if found */
193  x++;
194  if ((find= find_type(i, lib, FIND_TYPE_COMMA_TERM) - 1) < 0)
195  DBUG_RETURN(0);
196  result|= (ULL(1) << find);
197  }
198  *err= 0;
199  DBUG_RETURN(result);
200 } /* find_set */
201 
202 
215 TYPELIB *copy_typelib(MEM_ROOT *root, TYPELIB *from)
216 {
217  TYPELIB *to;
218  uint i;
219 
220  if (!from)
221  return NULL;
222 
223  if (!(to= (TYPELIB*) alloc_root(root, sizeof(TYPELIB))))
224  return NULL;
225 
226  if (!(to->type_names= (const char **)
227  alloc_root(root, (sizeof(char *) + sizeof(int)) * (from->count + 1))))
228  return NULL;
229  to->type_lengths= (unsigned int *)(to->type_names + from->count + 1);
230  to->count= from->count;
231  if (from->name)
232  {
233  if (!(to->name= strdup_root(root, from->name)))
234  return NULL;
235  }
236  else
237  to->name= NULL;
238 
239  for (i= 0; i < from->count; i++)
240  {
241  if (!(to->type_names[i]= strmake_root(root, from->type_names[i],
242  from->type_lengths[i])))
243  return NULL;
244  to->type_lengths[i]= from->type_lengths[i];
245  }
246  to->type_names[to->count]= NULL;
247  to->type_lengths[to->count]= 0;
248 
249  return to;
250 }
251 
252 
253 static const char *on_off_default_names[]= { "off","on","default", 0};
254 static TYPELIB on_off_default_typelib= {array_elements(on_off_default_names)-1,
255  "", on_off_default_names, 0};
256 
275 static uint parse_name(const TYPELIB *lib, const char **strpos, const char *end)
276 {
277  const char *pos= *strpos;
278  uint find= find_type(pos, lib, FIND_TYPE_COMMA_TERM);
279  for (; pos != end && *pos != '=' && *pos !=',' ; pos++);
280  *strpos= pos;
281  return find;
282 }
283 
319 my_ulonglong find_set_from_flags(const TYPELIB *lib, uint default_name,
320  my_ulonglong cur_set, my_ulonglong default_set,
321  const char *str, uint length,
322  char **err_pos, uint *err_len)
323 {
324  const char *end= str + length;
325  my_ulonglong flags_to_set= 0, flags_to_clear= 0, res;
326  my_bool set_defaults= 0;
327 
328  *err_pos= 0; /* No error yet */
329  if (str != end)
330  {
331  const char *start= str;
332  for (;;)
333  {
334  const char *pos= start;
335  uint flag_no, value;
336 
337  if (!(flag_no= parse_name(lib, &pos, end)))
338  goto err;
339 
340  if (flag_no == default_name)
341  {
342  /* Using 'default' twice isn't allowed. */
343  if (set_defaults)
344  goto err;
345  set_defaults= TRUE;
346  }
347  else
348  {
349  my_ulonglong bit= (1ULL << (flag_no - 1));
350  /* parse the '=on|off|default' */
351  if ((flags_to_clear | flags_to_set) & bit ||
352  pos >= end || *pos++ != '=' ||
353  !(value= parse_name(&on_off_default_typelib, &pos, end)))
354  goto err;
355 
356  if (value == 1) /* this is '=off' */
357  flags_to_clear|= bit;
358  else if (value == 2) /* this is '=on' */
359  flags_to_set|= bit;
360  else /* this is '=default' */
361  {
362  if (default_set & bit)
363  flags_to_set|= bit;
364  else
365  flags_to_clear|= bit;
366  }
367  }
368  if (pos >= end)
369  break;
370 
371  if (*pos++ != ',')
372  goto err;
373 
374  start=pos;
375  continue;
376  err:
377  *err_pos= (char*)start;
378  *err_len= end - start;
379  break;
380  }
381  }
382  res= set_defaults? default_set : cur_set;
383  res|= flags_to_set;
384  res&= ~flags_to_clear;
385  return res;
386 }
387