~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to storage/ndb/src/mgmsrv/InitConfigFileParser.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2003 MySQL AB
 
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
#include <ndb_global.h>
 
17
 
 
18
#include "InitConfigFileParser.hpp"
 
19
#include "Config.hpp"
 
20
#include "MgmtErrorReporter.hpp"
 
21
#include <NdbOut.hpp>
 
22
#include "ConfigInfo.hpp"
 
23
#include <m_string.h>
 
24
 
 
25
const int MAX_LINE_LENGTH = 1024;  // Max length of line of text in config file
 
26
static void trim(char *);
 
27
 
 
28
static void require(bool v) { if(!v) abort();}
 
29
 
 
30
//****************************************************************************
 
31
//  Ctor / Dtor
 
32
//****************************************************************************
 
33
InitConfigFileParser::InitConfigFileParser(FILE * out)
 
34
{
 
35
  m_info = new ConfigInfo();
 
36
  m_errstream = out ? out : stdout;
 
37
}
 
38
 
 
39
InitConfigFileParser::~InitConfigFileParser() {
 
40
  delete m_info;
 
41
}
 
42
 
 
43
//****************************************************************************
 
44
//  Read Config File
 
45
//****************************************************************************
 
46
InitConfigFileParser::Context::Context(const ConfigInfo * info, FILE * out)
 
47
  :  m_userProperties(true), m_configValues(1000, 20) {
 
48
 
 
49
  m_config = new Properties(true);
 
50
  m_defaults = new Properties(true);
 
51
  m_errstream = out;
 
52
}
 
53
 
 
54
InitConfigFileParser::Context::~Context(){
 
55
  if(m_config != 0)
 
56
    delete m_config;
 
57
 
 
58
  if(m_defaults != 0)
 
59
    delete m_defaults;
 
60
}
 
61
 
 
62
Config *
 
63
InitConfigFileParser::parseConfig(const char * filename) {
 
64
  FILE * file = fopen(filename, "r");
 
65
  if(file == 0){
 
66
    fprintf(m_errstream, "Error opening file: %s\n", filename);
 
67
    return 0;
 
68
  }
 
69
  
 
70
  Config * ret = parseConfig(file);
 
71
  fclose(file);
 
72
  return ret;
 
73
}
 
74
 
 
75
Config *
 
76
InitConfigFileParser::parseConfig(FILE * file) {
 
77
 
 
78
  char line[MAX_LINE_LENGTH];
 
79
 
 
80
  Context ctx(m_info, m_errstream); 
 
81
  ctx.m_lineno = 0;
 
82
  ctx.m_currentSection = 0;
 
83
 
 
84
  /*************
 
85
   * Open file *
 
86
   *************/
 
87
  if (file == NULL) {
 
88
    return 0;
 
89
  }
 
90
 
 
91
  /***********************
 
92
   * While lines to read *
 
93
   ***********************/
 
94
  while (fgets(line, MAX_LINE_LENGTH, file)) {
 
95
    ctx.m_lineno++;
 
96
 
 
97
    trim(line);
 
98
 
 
99
    if (isEmptyLine(line)) // Skip if line is empty or comment
 
100
      continue;   
 
101
 
 
102
    // End with NULL instead of newline
 
103
    if (line[strlen(line)-1] == '\n')
 
104
      line[strlen(line)-1] = '\0';
 
105
    
 
106
    /********************************
 
107
     * 1. Parse new default section *
 
108
     ********************************/
 
109
    if (char* section = parseDefaultSectionHeader(line)) {
 
110
      if(!storeSection(ctx)){
 
111
        free(section);
 
112
        ctx.reportError("Could not store previous default section "
 
113
                        "of configuration file.");
 
114
        return 0;
 
115
      }
 
116
      BaseString::snprintf(ctx.fname, sizeof(ctx.fname), section); free(section);
 
117
      ctx.type             = InitConfigFileParser::DefaultSection;
 
118
      ctx.m_sectionLineno  = ctx.m_lineno;
 
119
      ctx.m_currentSection = new Properties(true);
 
120
      ctx.m_userDefaults   = NULL;
 
121
      require((ctx.m_currentInfo = m_info->getInfo(ctx.fname)) != 0);
 
122
      require((ctx.m_systemDefaults = m_info->getDefaults(ctx.fname)) != 0);
 
123
      continue;
 
124
    }
 
125
    
 
126
    /************************
 
127
     * 2. Parse new section *
 
128
     ************************/
 
129
    if (char* section = parseSectionHeader(line)) {
 
130
      if(!storeSection(ctx)){
 
131
        free(section);
 
132
        ctx.reportError("Could not store previous section "
 
133
                        "of configuration file.");
 
134
        return 0;
 
135
      }
 
136
      BaseString::snprintf(ctx.fname, sizeof(ctx.fname), section);
 
137
      free(section);
 
138
      ctx.type             = InitConfigFileParser::Section;
 
139
      ctx.m_sectionLineno  = ctx.m_lineno;      
 
140
      ctx.m_currentSection = new Properties(true);
 
141
      ctx.m_userDefaults   = getSection(ctx.fname, ctx.m_defaults);
 
142
      require((ctx.m_currentInfo    = m_info->getInfo(ctx.fname)) != 0);
 
143
      require((ctx.m_systemDefaults = m_info->getDefaults(ctx.fname)) != 0);
 
144
      continue;
 
145
    }
 
146
    
 
147
    /****************************
 
148
     * 3. Parse name-value pair *
 
149
     ****************************/
 
150
    if (!parseNameValuePair(ctx, line)) {
 
151
      ctx.reportError("Could not parse name-value pair in config file.");
 
152
      return 0;
 
153
    }
 
154
  }
 
155
  
 
156
  if (ferror(file)){
 
157
    ctx.reportError("Failure in reading");
 
158
    return 0;
 
159
  } 
 
160
 
 
161
  if(!storeSection(ctx)) {
 
162
    ctx.reportError("Could not store section of configuration file.");
 
163
    return 0;
 
164
  }
 
165
 
 
166
  return run_config_rules(ctx);
 
167
}
 
168
 
 
169
Config*
 
170
InitConfigFileParser::run_config_rules(Context& ctx)
 
171
{
 
172
  for(size_t i = 0; ConfigInfo::m_ConfigRules[i].m_configRule != 0; i++){
 
173
    ctx.type             = InitConfigFileParser::Undefined;
 
174
    ctx.m_currentSection = 0;
 
175
    ctx.m_userDefaults   = 0;
 
176
    ctx.m_currentInfo    = 0;
 
177
    ctx.m_systemDefaults = 0;
 
178
    
 
179
    Vector<ConfigInfo::ConfigRuleSection> tmp;
 
180
    if(!(* ConfigInfo::m_ConfigRules[i].m_configRule)(tmp, ctx,
 
181
                                                      ConfigInfo::m_ConfigRules[i].m_ruleData))
 
182
      return 0;
 
183
 
 
184
    for(size_t j = 0; j<tmp.size(); j++){
 
185
      BaseString::snprintf(ctx.fname, sizeof(ctx.fname), tmp[j].m_sectionType.c_str());
 
186
      ctx.type             = InitConfigFileParser::Section;
 
187
      ctx.m_currentSection = tmp[j].m_sectionData;
 
188
      ctx.m_userDefaults   = getSection(ctx.fname, ctx.m_defaults);
 
189
      require((ctx.m_currentInfo    = m_info->getInfo(ctx.fname)) != 0);
 
190
      require((ctx.m_systemDefaults = m_info->getDefaults(ctx.fname)) != 0);
 
191
      if(!storeSection(ctx))
 
192
        return 0;
 
193
    }
 
194
  }
 
195
 
 
196
  Uint32 nConnections = 0;
 
197
  Uint32 nComputers = 0;
 
198
  Uint32 nNodes = 0;
 
199
  Uint32 nExtConnections = 0;
 
200
  const char * system = "?";
 
201
  ctx.m_userProperties.get("NoOfConnections", &nConnections);
 
202
  ctx.m_userProperties.get("NoOfComputers", &nComputers);
 
203
  ctx.m_userProperties.get("NoOfNodes", &nNodes);
 
204
  ctx.m_userProperties.get("ExtNoOfConnections", &nExtConnections);
 
205
  ctx.m_userProperties.get("ExtSystem", &system);
 
206
  ctx.m_config->put("NoOfConnections", nConnections);
 
207
  ctx.m_config->put("NoOfComputers", nComputers);
 
208
  ctx.m_config->put("NoOfNodes", nNodes);
 
209
 
 
210
  char tmpLine[MAX_LINE_LENGTH];
 
211
  BaseString::snprintf(tmpLine, MAX_LINE_LENGTH,
 
212
                       "EXTERNAL SYSTEM_%s:NoOfConnections", system);
 
213
  ctx.m_config->put(tmpLine, nExtConnections);
 
214
 
 
215
  Config * ret = new Config();
 
216
  ret->m_configValues = (struct ndb_mgm_configuration*)ctx.m_configValues.getConfigValues();
 
217
  ret->m_oldConfig = ctx.m_config; ctx.m_config = 0;
 
218
  return ret;
 
219
}
 
220
 
 
221
//****************************************************************************
 
222
//  Parse Name-Value Pair
 
223
//****************************************************************************
 
224
 
 
225
bool InitConfigFileParser::parseNameValuePair(Context& ctx, const char* line)
 
226
{
 
227
  if (ctx.m_currentSection == NULL){
 
228
    ctx.reportError("Value specified outside section");
 
229
    return false;
 
230
  }
 
231
 
 
232
  // *************************************
 
233
  //  Split string at first occurrence of 
 
234
  //  '=' or ':'
 
235
  // *************************************
 
236
 
 
237
  Vector<BaseString> tmp_string_split;
 
238
  if (BaseString(line).split(tmp_string_split,
 
239
                             "=:", 2) != 2)
 
240
  {
 
241
    ctx.reportError("Parse error");
 
242
    return false;
 
243
  }
 
244
 
 
245
  // *************************************
 
246
  //  Remove all after #
 
247
  // *************************************
 
248
 
 
249
  Vector<BaseString> tmp_string_split2;
 
250
  tmp_string_split[1].split(tmp_string_split2,
 
251
                            "#", 2);
 
252
  tmp_string_split[1]=tmp_string_split2[0];
 
253
 
 
254
  // *************************************
 
255
  // Remove leading and trailing chars
 
256
  // *************************************
 
257
  {
 
258
    for (int i = 0; i < 2; i++)
 
259
      tmp_string_split[i].trim("\r\n \t"); 
 
260
  }
 
261
 
 
262
  // *************************************
 
263
  // First in split is fname
 
264
  // *************************************
 
265
 
 
266
  const char *fname= tmp_string_split[0].c_str();
 
267
 
 
268
  if (!ctx.m_currentInfo->contains(fname)) {
 
269
    ctx.reportError("[%s] Unknown parameter: %s", ctx.fname, fname);
 
270
    return false;
 
271
  }
 
272
  ConfigInfo::Status status = m_info->getStatus(ctx.m_currentInfo, fname);
 
273
  if (status == ConfigInfo::CI_NOTIMPLEMENTED) {
 
274
    ctx.reportWarning("[%s] %s not yet implemented", ctx.fname, fname);
 
275
  }
 
276
  if (status == ConfigInfo::CI_DEPRICATED) {
 
277
    const char * desc = m_info->getDescription(ctx.m_currentInfo, fname);
 
278
    if(desc && desc[0]){
 
279
      ctx.reportWarning("[%s] %s is depricated, use %s instead", 
 
280
                        ctx.fname, fname, desc);
 
281
    } else if (desc == 0){
 
282
      ctx.reportWarning("[%s] %s is depricated", ctx.fname, fname);
 
283
    } 
 
284
  }
 
285
 
 
286
  // ***********************
 
287
  //  Store name-value pair
 
288
  // ***********************
 
289
 
 
290
  return storeNameValuePair(ctx, fname, tmp_string_split[1].c_str());
 
291
}
 
292
 
 
293
 
 
294
//****************************************************************************
 
295
//  STORE NAME-VALUE pair in properties section 
 
296
//****************************************************************************
 
297
 
 
298
bool 
 
299
InitConfigFileParser::storeNameValuePair(Context& ctx,
 
300
                                         const char* fname, 
 
301
                                         const char* value) {
 
302
  
 
303
  const char * pname = fname;
 
304
 
 
305
  if (ctx.m_currentSection->contains(pname)) {
 
306
    ctx.reportError("[%s] Parameter %s specified twice", ctx.fname, fname);
 
307
    return false;
 
308
  }
 
309
  
 
310
  // ***********************
 
311
  //  Store name-value pair
 
312
  // ***********************
 
313
 
 
314
  const ConfigInfo::Type type = m_info->getType(ctx.m_currentInfo, fname);
 
315
  switch(type){
 
316
  case ConfigInfo::CI_BOOL: {
 
317
    bool value_bool;
 
318
    if (!convertStringToBool(value, value_bool)) {
 
319
      ctx.reportError("Illegal boolean value for parameter %s", fname);
 
320
      return false;
 
321
    }
 
322
    MGM_REQUIRE(ctx.m_currentSection->put(pname, value_bool));
 
323
    break;
 
324
  }
 
325
  case ConfigInfo::CI_INT:
 
326
  case ConfigInfo::CI_INT64:{
 
327
    Uint64 value_int;
 
328
    if (!convertStringToUint64(value, value_int)) {
 
329
      ctx.reportError("Illegal integer value for parameter %s", fname);
 
330
      return false;
 
331
    }
 
332
    if (!m_info->verify(ctx.m_currentInfo, fname, value_int)) {
 
333
      ctx.reportError("Illegal value %s for parameter %s.\n"
 
334
                      "Legal values are between %Lu and %Lu", value, fname,
 
335
                      m_info->getMin(ctx.m_currentInfo, fname), 
 
336
                      m_info->getMax(ctx.m_currentInfo, fname));
 
337
      return false;
 
338
    }
 
339
    if(type == ConfigInfo::CI_INT){
 
340
      MGM_REQUIRE(ctx.m_currentSection->put(pname, (Uint32)value_int));
 
341
    } else {
 
342
      MGM_REQUIRE(ctx.m_currentSection->put64(pname, value_int));
 
343
    }
 
344
    break;
 
345
  }
 
346
  case ConfigInfo::CI_STRING:
 
347
    MGM_REQUIRE(ctx.m_currentSection->put(pname, value));
 
348
    break;
 
349
  case ConfigInfo::CI_SECTION:
 
350
    abort();
 
351
  }
 
352
  return true;
 
353
}
 
354
 
 
355
//****************************************************************************
 
356
//  Is Empty Line
 
357
//****************************************************************************
 
358
 
 
359
bool InitConfigFileParser::isEmptyLine(const char* line) const {
 
360
  int i;
 
361
  
 
362
  // Check if it is a comment line
 
363
  if (line[0] == '#') return true;               
 
364
 
 
365
  // Check if it is a line with only spaces
 
366
  for (i = 0; i < MAX_LINE_LENGTH && line[i] != '\n' && line[i] != '\0'; i++) {
 
367
    if (line[i] != ' ' && line[i] != '\t') return false;
 
368
  }
 
369
  return true;
 
370
}
 
371
 
 
372
//****************************************************************************
 
373
//  Convert String to Int
 
374
//****************************************************************************
 
375
bool InitConfigFileParser::convertStringToUint64(const char* s, 
 
376
                                                 Uint64& val,
 
377
                                                 Uint32 log10base) {
 
378
  if (s == NULL)
 
379
    return false;
 
380
  if (strlen(s) == 0) 
 
381
    return false;
 
382
 
 
383
  errno = 0;
 
384
  char* p;
 
385
  Int64 v = strtoll(s, &p, log10base);
 
386
  if (errno != 0)
 
387
    return false;
 
388
  
 
389
  long mul = 0;
 
390
  if (p != &s[strlen(s)]){
 
391
    char * tmp = strdup(p);
 
392
    trim(tmp);
 
393
    switch(tmp[0]){
 
394
    case 'k':
 
395
    case 'K':
 
396
      mul = 10;
 
397
      break;
 
398
    case 'M':
 
399
      mul = 20;
 
400
      break;
 
401
    case 'G':
 
402
      mul = 30;
 
403
      break;
 
404
    default:
 
405
      free(tmp);
 
406
      return false;
 
407
    }
 
408
    free(tmp);
 
409
  }
 
410
  
 
411
  val = (v << mul);
 
412
  return true;
 
413
}
 
414
 
 
415
bool InitConfigFileParser::convertStringToBool(const char* s, bool& val) {
 
416
  if (s == NULL) return false;
 
417
  if (strlen(s) == 0) return false;
 
418
 
 
419
  if (!strcmp(s, "Y") || !strcmp(s, "y") || 
 
420
      !strcmp(s, "Yes") || !strcmp(s, "YES") || !strcmp(s, "yes") || 
 
421
      !strcmp(s, "True") || !strcmp(s, "TRUE") || !strcmp(s, "true") ||
 
422
      !strcmp(s, "1")) {
 
423
    val = true;
 
424
    return true;
 
425
  }
 
426
 
 
427
  if (!strcmp(s, "N") || !strcmp(s, "n") || 
 
428
      !strcmp(s, "No") || !strcmp(s, "NO") || !strcmp(s, "no") || 
 
429
      !strcmp(s, "False") || !strcmp(s, "FALSE") || !strcmp(s, "false") ||
 
430
      !strcmp(s, "0")) {
 
431
    val = false;
 
432
    return true;
 
433
  }
 
434
  
 
435
  return false;  // Failure to convert
 
436
}
 
437
 
 
438
//****************************************************************************
 
439
//  Parse Section Header
 
440
//****************************************************************************
 
441
static void
 
442
trim(char * str){
 
443
  int len = strlen(str);
 
444
  for(len--;
 
445
      (str[len] == '\r' || str[len] == '\n' || 
 
446
       str[len] == ' ' || str[len] == '\t') && 
 
447
        len > 0; 
 
448
      len--)
 
449
    str[len] = 0;
 
450
  
 
451
  int pos = 0;
 
452
  while(str[pos] == ' ' || str[pos] == '\t')
 
453
    pos++;
 
454
  
 
455
  if(str[pos] == '\"' && str[len] == '\"') {
 
456
    pos++;
 
457
    str[len] = 0;
 
458
    len--;
 
459
  }
 
460
  
 
461
  memmove(str, &str[pos], len - pos + 2);
 
462
}
 
463
 
 
464
char* 
 
465
InitConfigFileParser::parseSectionHeader(const char* line) const {
 
466
  char * tmp = strdup(line);
 
467
 
 
468
  if(tmp[0] != '['){
 
469
    free(tmp);
 
470
    return NULL;
 
471
  }
 
472
 
 
473
  if(tmp[strlen(tmp)-1] != ']'){
 
474
    free(tmp);
 
475
    return NULL;
 
476
  }
 
477
  tmp[strlen(tmp)-1] = 0;
 
478
 
 
479
  tmp[0] = ' ';
 
480
  trim(tmp);
 
481
 
 
482
  // Get the correct header name if an alias
 
483
  {
 
484
    const char *tmp_alias= m_info->getAlias(tmp);
 
485
    if (tmp_alias) {
 
486
      free(tmp);
 
487
      tmp= strdup(tmp_alias);
 
488
    }
 
489
  }
 
490
 
 
491
  // Lookup token among sections
 
492
  if(!m_info->isSection(tmp)) {
 
493
    free(tmp);
 
494
    return NULL;
 
495
  }
 
496
  if(m_info->getInfo(tmp)) return tmp;
 
497
 
 
498
  free(tmp);
 
499
  return NULL;
 
500
}
 
501
 
 
502
//****************************************************************************
 
503
//  Parse Default Section Header
 
504
//****************************************************************************
 
505
 
 
506
char* 
 
507
InitConfigFileParser::parseDefaultSectionHeader(const char* line) const {
 
508
  static char token1[MAX_LINE_LENGTH], token2[MAX_LINE_LENGTH];
 
509
 
 
510
  int no = sscanf(line, "[%120[A-Z_a-z] %120[A-Z_a-z]]", token1, token2);
 
511
 
 
512
  // Not correct no of tokens 
 
513
  if (no != 2) return NULL;
 
514
 
 
515
  // Not correct keyword at end
 
516
  if (!strcasecmp(token2, "DEFAULT") == 0) return NULL;
 
517
 
 
518
  const char *token1_alias= m_info->getAlias(token1);
 
519
  if (token1_alias == 0)
 
520
    token1_alias= token1;
 
521
 
 
522
  if(m_info->getInfo(token1_alias)){
 
523
    return strdup(token1_alias);
 
524
  }
 
525
  
 
526
  // Did not find section
 
527
  return NULL;
 
528
}
 
529
 
 
530
const Properties *
 
531
InitConfigFileParser::getSection(const char * name, const Properties * src){
 
532
  const Properties * p;
 
533
  if(src && src->get(name, &p))
 
534
    return p;
 
535
 
 
536
  return 0;
 
537
}
 
538
 
 
539
//****************************************************************************
 
540
//  STORE section
 
541
//****************************************************************************
 
542
bool
 
543
InitConfigFileParser::storeSection(Context& ctx){
 
544
  if(ctx.m_currentSection == NULL)
 
545
    return true;
 
546
  for(int i = strlen(ctx.fname) - 1; i>=0; i--){
 
547
    ctx.fname[i] = toupper(ctx.fname[i]);
 
548
  }
 
549
  BaseString::snprintf(ctx.pname, sizeof(ctx.pname), ctx.fname);
 
550
  char buf[255];
 
551
  if(ctx.type == InitConfigFileParser::Section)
 
552
    BaseString::snprintf(buf, sizeof(buf), "%s", ctx.fname);
 
553
  if(ctx.type == InitConfigFileParser::DefaultSection)
 
554
    BaseString::snprintf(buf, sizeof(buf), "%s DEFAULT", ctx.fname);
 
555
  BaseString::snprintf(ctx.fname, sizeof(ctx.fname), buf);
 
556
  if(ctx.type == InitConfigFileParser::Section){
 
557
    for(int i = 0; i<m_info->m_NoOfRules; i++){
 
558
      const ConfigInfo::SectionRule & rule = m_info->m_SectionRules[i];
 
559
      if(!strcmp(rule.m_section, "*") || !strcmp(rule.m_section, ctx.fname)){
 
560
        if(!(* rule.m_sectionRule)(ctx, rule.m_ruleData)){
 
561
          return false;
 
562
        }
 
563
      }
 
564
    }
 
565
  }
 
566
  if(ctx.type == InitConfigFileParser::DefaultSection &&
 
567
     !ctx.m_defaults->put(ctx.pname, ctx.m_currentSection))
 
568
  {
 
569
    ctx.reportError("Duplicate default section not allowed");
 
570
    return false;
 
571
  }
 
572
  if(ctx.type == InitConfigFileParser::Section)
 
573
    require(ctx.m_config->put(ctx.pname, ctx.m_currentSection));
 
574
  delete ctx.m_currentSection; ctx.m_currentSection = NULL;
 
575
  return true;
 
576
}
 
577
 
 
578
void
 
579
InitConfigFileParser::Context::reportError(const char * fmt, ...){
 
580
  va_list ap;
 
581
  char buf[1000];
 
582
  
 
583
  va_start(ap, fmt);
 
584
  if (fmt != 0)
 
585
    BaseString::vsnprintf(buf, sizeof(buf)-1, fmt, ap);
 
586
  va_end(ap);
 
587
  fprintf(m_errstream, "Error line %d: %s\n",
 
588
          m_lineno, buf);
 
589
 
 
590
  //m_currentSection->print();
 
591
}
 
592
 
 
593
void
 
594
InitConfigFileParser::Context::reportWarning(const char * fmt, ...){
 
595
  va_list ap;
 
596
  char buf[1000];
 
597
  
 
598
  va_start(ap, fmt);
 
599
  if (fmt != 0)
 
600
    BaseString::vsnprintf(buf, sizeof(buf)-1, fmt, ap);
 
601
  va_end(ap);
 
602
  fprintf(m_errstream, "Warning line %d: %s\n",
 
603
          m_lineno, buf);
 
604
}
 
605
 
 
606
#include <my_sys.h>
 
607
#include <my_getopt.h>
 
608
 
 
609
static int order = 1;
 
610
static 
 
611
my_bool 
 
612
parse_mycnf_opt(int, const struct my_option * opt, char * value)
 
613
{
 
614
  long *app_type= (long*) &opt->app_type;
 
615
  if(opt->comment)
 
616
    (*app_type)++;
 
617
  else
 
618
    *app_type = order++;
 
619
  return 0;
 
620
}
 
621
 
 
622
bool
 
623
InitConfigFileParser::store_in_properties(Vector<struct my_option>& options, 
 
624
                                          InitConfigFileParser::Context& ctx,
 
625
                                          const char * name)
 
626
{
 
627
  for(unsigned i = 0; i<options.size(); i++)
 
628
  {
 
629
    if(options[i].comment && 
 
630
       options[i].app_type && 
 
631
       strcmp(options[i].comment, name) == 0)
 
632
    {
 
633
      Uint64 value_int;
 
634
      switch(options[i].var_type){
 
635
      case GET_INT:
 
636
        value_int = *(Uint32*)options[i].value;
 
637
        break;
 
638
      case GET_LL:
 
639
        value_int = *(Uint64*)options[i].value;
 
640
        break;
 
641
      case GET_STR:
 
642
        ctx.m_currentSection->put(options[i].name, *(char**)options[i].value);
 
643
        continue;
 
644
      default:
 
645
        abort();
 
646
      }
 
647
 
 
648
      const char * fname = options[i].name;
 
649
      if (!m_info->verify(ctx.m_currentInfo, fname, value_int)) {
 
650
        ctx.reportError("Illegal value %lld for parameter %s.\n"
 
651
                        "Legal values are between %Lu and %Lu", 
 
652
                        value_int, fname,
 
653
                        m_info->getMin(ctx.m_currentInfo, fname), 
 
654
                        m_info->getMax(ctx.m_currentInfo, fname));
 
655
        return false;
 
656
      }
 
657
 
 
658
      ConfigInfo::Status status = m_info->getStatus(ctx.m_currentInfo, fname);
 
659
      if (status == ConfigInfo::CI_DEPRICATED) {
 
660
        const char * desc = m_info->getDescription(ctx.m_currentInfo, fname);
 
661
        if(desc && desc[0]){
 
662
          ctx.reportWarning("[%s] %s is depricated, use %s instead", 
 
663
                            ctx.fname, fname, desc);
 
664
        } else if (desc == 0){
 
665
          ctx.reportWarning("[%s] %s is depricated", ctx.fname, fname);
 
666
        } 
 
667
      }
 
668
      
 
669
      if (options[i].var_type == GET_INT)
 
670
        ctx.m_currentSection->put(options[i].name, (Uint32)value_int);
 
671
      else
 
672
        ctx.m_currentSection->put64(options[i].name, value_int);        
 
673
    }
 
674
  }
 
675
  return true;
 
676
}
 
677
 
 
678
bool
 
679
InitConfigFileParser::handle_mycnf_defaults(Vector<struct my_option>& options,
 
680
                                            InitConfigFileParser::Context& ctx, 
 
681
                                            const char * name)
 
682
{
 
683
  strcpy(ctx.fname, name);
 
684
  ctx.type = InitConfigFileParser::DefaultSection;
 
685
  ctx.m_currentSection = new Properties(true);
 
686
  ctx.m_userDefaults   = NULL;
 
687
  require((ctx.m_currentInfo = m_info->getInfo(ctx.fname)) != 0);
 
688
  require((ctx.m_systemDefaults = m_info->getDefaults(ctx.fname)) != 0);
 
689
  if(store_in_properties(options, ctx, name))
 
690
    return storeSection(ctx);
 
691
  return false;
 
692
}
 
693
 
 
694
static
 
695
int
 
696
load_defaults(Vector<struct my_option>& options, const char* groups[])
 
697
{
 
698
  int argc = 1;
 
699
  const char * argv[] = { "ndb_mgmd", 0, 0, 0, 0 };
 
700
  BaseString file;
 
701
  BaseString extra_file;
 
702
  BaseString group_suffix;
 
703
 
 
704
  const char *save_file = my_defaults_file;
 
705
  char *save_extra_file = my_defaults_extra_file;
 
706
  const char *save_group_suffix = my_defaults_group_suffix;
 
707
 
 
708
  if (my_defaults_file)
 
709
  {
 
710
    file.assfmt("--defaults-file=%s", my_defaults_file);
 
711
    argv[argc++] = file.c_str();
 
712
  }
 
713
 
 
714
  if (my_defaults_extra_file)
 
715
  {
 
716
    extra_file.assfmt("--defaults-extra-file=%s", my_defaults_extra_file);
 
717
    argv[argc++] = extra_file.c_str();
 
718
  }
 
719
 
 
720
  if (my_defaults_group_suffix)
 
721
  {
 
722
    group_suffix.assfmt("--defaults-group-suffix=%s",
 
723
                        my_defaults_group_suffix);
 
724
    argv[argc++] = group_suffix.c_str();
 
725
  }
 
726
 
 
727
  char ** tmp = (char**)argv;
 
728
  int ret = load_defaults("my", groups, &argc, &tmp);
 
729
  
 
730
  my_defaults_file = save_file;
 
731
  my_defaults_extra_file = save_extra_file;
 
732
  my_defaults_group_suffix = save_group_suffix;
 
733
  
 
734
  if (ret == 0)
 
735
  {
 
736
    return handle_options(&argc, &tmp, options.getBase(), parse_mycnf_opt);
 
737
  }
 
738
  
 
739
  return ret;
 
740
}
 
741
 
 
742
bool
 
743
InitConfigFileParser::load_mycnf_groups(Vector<struct my_option> & options,
 
744
                                        InitConfigFileParser::Context& ctx,
 
745
                                        const char * name,
 
746
                                        const char *groups[])
 
747
{
 
748
  unsigned i;
 
749
  Vector<struct my_option> copy;
 
750
  for(i = 0; i<options.size(); i++)
 
751
  {
 
752
    if(options[i].comment && strcmp(options[i].comment, name) == 0)
 
753
    {
 
754
      options[i].app_type = 0;
 
755
      copy.push_back(options[i]);
 
756
    }
 
757
  }
 
758
 
 
759
  struct my_option end;
 
760
  bzero(&end, sizeof(end));
 
761
  copy.push_back(end);
 
762
 
 
763
  if (load_defaults(copy, groups))
 
764
    return false;
 
765
  
 
766
  return store_in_properties(copy, ctx, name);
 
767
}
 
768
 
 
769
Config *
 
770
InitConfigFileParser::parse_mycnf() 
 
771
{
 
772
  int i;
 
773
  Config * res = 0;
 
774
  Vector<struct my_option> options;
 
775
  for(i = 0; i<ConfigInfo::m_NoOfParams; i++)
 
776
  {
 
777
    {
 
778
      struct my_option opt;
 
779
      bzero(&opt, sizeof(opt));
 
780
      const ConfigInfo::ParamInfo& param = ConfigInfo::m_ParamInfo[i];
 
781
      switch(param._type){
 
782
      case ConfigInfo::CI_BOOL:
 
783
        opt.value = (uchar **)malloc(sizeof(int));
 
784
        opt.var_type = GET_INT;
 
785
        break;
 
786
      case ConfigInfo::CI_INT: 
 
787
        opt.value = (uchar**)malloc(sizeof(int));
 
788
        opt.var_type = GET_INT;
 
789
        break;
 
790
      case ConfigInfo::CI_INT64:
 
791
        opt.value = (uchar**)malloc(sizeof(Int64));
 
792
        opt.var_type = GET_LL;
 
793
        break;
 
794
      case ConfigInfo::CI_STRING: 
 
795
        opt.value = (uchar**)malloc(sizeof(char *));
 
796
        opt.var_type = GET_STR;
 
797
        break;
 
798
      default:
 
799
        continue;
 
800
      }
 
801
      opt.name = param._fname;
 
802
      opt.id = 256;
 
803
      opt.app_type = 0;
 
804
      opt.arg_type = REQUIRED_ARG;
 
805
      opt.comment = param._section;
 
806
      options.push_back(opt);
 
807
    }
 
808
  }
 
809
  
 
810
  struct my_option *ndbd, *ndb_mgmd, *mysqld, *api;
 
811
 
 
812
  /**
 
813
   * Add ndbd, ndb_mgmd, api/mysqld
 
814
   */
 
815
  Uint32 idx = options.size();
 
816
  {
 
817
    struct my_option opt;
 
818
    bzero(&opt, sizeof(opt));
 
819
    opt.name = "ndbd";
 
820
    opt.id = 256;
 
821
    opt.value = (uchar**)malloc(sizeof(char*));
 
822
    opt.var_type = GET_STR;
 
823
    opt.arg_type = REQUIRED_ARG;
 
824
    options.push_back(opt);
 
825
 
 
826
    opt.name = "ndb_mgmd";
 
827
    opt.id = 256;
 
828
    opt.value = (uchar**)malloc(sizeof(char*));
 
829
    opt.var_type = GET_STR;
 
830
    opt.arg_type = REQUIRED_ARG;
 
831
    options.push_back(opt);
 
832
 
 
833
    opt.name = "mysqld";
 
834
    opt.id = 256;
 
835
    opt.value = (uchar**)malloc(sizeof(char*));
 
836
    opt.var_type = GET_STR;
 
837
    opt.arg_type = REQUIRED_ARG;
 
838
    options.push_back(opt);
 
839
 
 
840
    opt.name = "ndbapi";
 
841
    opt.id = 256;
 
842
    opt.value = (uchar**)malloc(sizeof(char*));
 
843
    opt.var_type = GET_STR;
 
844
    opt.arg_type = REQUIRED_ARG;
 
845
    options.push_back(opt);
 
846
 
 
847
    bzero(&opt, sizeof(opt));
 
848
    options.push_back(opt);
 
849
 
 
850
    ndbd = &options[idx];
 
851
    ndb_mgmd = &options[idx+1];
 
852
    mysqld = &options[idx+2];
 
853
    api = &options[idx+3];
 
854
  }
 
855
  
 
856
  Context ctx(m_info, m_errstream); 
 
857
  const char *groups[]= { "cluster_config", 0 };
 
858
  if (load_defaults(options, groups))
 
859
    goto end;
 
860
 
 
861
  ctx.m_lineno = 0;
 
862
  if(!handle_mycnf_defaults(options, ctx, "DB"))
 
863
    goto end;
 
864
  if(!handle_mycnf_defaults(options, ctx, "API"))
 
865
    goto end;
 
866
  if(!handle_mycnf_defaults(options, ctx, "MGM"))
 
867
    goto end;
 
868
  if(!handle_mycnf_defaults(options, ctx, "TCP"))
 
869
    goto end;
 
870
  if(!handle_mycnf_defaults(options, ctx, "SHM"))
 
871
    goto end;
 
872
  if(!handle_mycnf_defaults(options, ctx, "SCI"))
 
873
    goto end;
 
874
 
 
875
  {
 
876
    struct sect { struct my_option* src; const char * name; } sections[] = 
 
877
      {
 
878
        { ndb_mgmd, "MGM" }
 
879
        ,{ ndbd, "DB" }
 
880
        ,{ mysqld, "API" }
 
881
        ,{ api, "API" }
 
882
        ,{ 0, 0 }, { 0, 0 }
 
883
      };
 
884
    
 
885
    for(i = 0; sections[i].src; i++)
 
886
    {
 
887
      for(int j = i + 1; sections[j].src; j++)
 
888
      {
 
889
        if (sections[j].src->app_type < sections[i].src->app_type)
 
890
        {
 
891
          sect swap = sections[i];
 
892
          sections[i] = sections[j];
 
893
          sections[j] = swap;
 
894
        }
 
895
      }
 
896
    }
 
897
    
 
898
    ctx.type = InitConfigFileParser::Section;
 
899
    ctx.m_sectionLineno  = ctx.m_lineno;      
 
900
    for(i = 0; sections[i].src; i++)
 
901
    {
 
902
      if (sections[i].src->app_type)
 
903
      {
 
904
        strcpy(ctx.fname, sections[i].name);
 
905
        BaseString str(*(char**)sections[i].src->value);
 
906
        Vector<BaseString> list;
 
907
        str.split(list, ",");
 
908
        
 
909
        const char * defaults_groups[] = { 0,  0, 0 };
 
910
        for(unsigned j = 0; j<list.size(); j++)
 
911
        {
 
912
          BaseString group_idx;
 
913
          BaseString group_host;
 
914
          group_idx.assfmt("%s.%s.%d", groups[0], 
 
915
                           sections[i].src->name, j + 1);
 
916
          group_host.assfmt("%s.%s.%s", groups[0], 
 
917
                            sections[i].src->name, list[j].c_str());
 
918
          defaults_groups[0] = group_idx.c_str();
 
919
          if(list[j].length())
 
920
            defaults_groups[1] = group_host.c_str();
 
921
          else
 
922
            defaults_groups[1] = 0;
 
923
          
 
924
          ctx.m_currentSection = new Properties(true);
 
925
          ctx.m_userDefaults = getSection(ctx.fname, ctx.m_defaults);
 
926
          require((ctx.m_currentInfo = m_info->getInfo(ctx.fname)) != 0);
 
927
          require((ctx.m_systemDefaults = m_info->getDefaults(ctx.fname))!= 0);
 
928
          ctx.m_currentSection->put("HostName", list[j].c_str());
 
929
          if(!load_mycnf_groups(options, ctx, sections[i].name, 
 
930
                                defaults_groups))
 
931
            goto end;
 
932
          
 
933
          if(!storeSection(ctx))
 
934
            goto end;
 
935
        }
 
936
      }
 
937
    }
 
938
  }
 
939
 
 
940
  res = run_config_rules(ctx);
 
941
 
 
942
end:
 
943
  for(i = 0; options[i].name; i++)
 
944
    free(options[i].value);
 
945
 
 
946
  return res;
 
947
}
 
948
 
 
949
template class Vector<struct my_option>;
 
950
 
 
951
/*
 
952
  See include/my_getopt.h for the declaration of struct my_option
 
953
*/