Parser.cpp 7.85 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* Copyright (C) 2003 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */


18 19
#include <ndb_global.h>

20 21 22 23 24
#include "Parser.hpp"
#include <NdbOut.hpp>
#include <Properties.hpp>
#include <Base64.hpp>

unknown's avatar
unknown committed
25
#undef DEBUG
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
#define DEBUG(x) ndbout << x << endl;

static void trim(char * str);

class ParseInputStream : public InputStream {
public:
  ParseInputStream(InputStream & in, bool trim = true, char eofComment = '#');
  
  char* gets(char * buf, int bufLen); 
  void push_back(const char *);
private:
  InputStream & in;
  char * buffer;
};

ParseInputStream::ParseInputStream(InputStream & _in, 
				   bool /* unused */, 
				   char /* unused */)
  : in(_in){
  buffer = 0;
}

char*
ParseInputStream::gets(char * buf, int bufLen){
  if(buffer != 0){
    strncpy(buf, buffer, bufLen);
    free(buffer);
    buffer = 0;
    return buf;
  }
  char *t = in.gets(buf, bufLen);
  return t;
}

void
ParseInputStream::push_back(const char * str){
  if(buffer != 0)
    abort();
  buffer = strdup(str);
}

ParserImpl::ParserImpl(const DummyRow * rows, InputStream & in,
		       bool b_cmd, bool b_empty, bool b_iarg) 
  : m_rows(rows), input(* new ParseInputStream(in))
{
  m_breakOnCmd = b_cmd;
  m_breakOnEmpty = b_empty;
  m_breakOnInvalidArg = b_iarg;
}

ParserImpl::~ParserImpl(){
  delete & input;
}

static
bool
Empty(const char * str){
  if(str == 0)
    return true;
  const int len = strlen(str);
  if(len == 0)
    return false;
  for(int i = 0; i<len; i++)
    if(str[i] != ' ' && str[i] != '\t' && str[i] != '\n')
      return false;
  return true;
}

static
bool
Eof(const char * str) { return str == 0;}

static
void
trim(char * str){
  if(str == NULL)
    return;
  int len = strlen(str);
  for(len--; str[len] == '\n' || str[len] == ' ' || str[len] == '\t'; len--)
    str[len] = 0;
  
  int pos = 0;
  while(str[pos] == ' ' || str[pos] == '\t')
    pos++;
  
  if(str[pos] == '\"' && str[len] == '\"') {
    pos++;
    str[len] = 0;
    len--;
  }
  
  memmove(str, &str[pos], len - pos + 2);
}

static
bool
split(char * buf, char ** name, char ** value){
  
  * value = strchr(buf, ':');
  if(* value == 0)
    * value = strchr(buf, '=');


  if(* value == 0){
    return false;
  }
  (* value)[0] = 0;
  * value = (* value + 1);
  * name = buf;
  
  trim(* name);
  trim(* value);

  return true;
}

bool
ParserImpl::run(Context * ctx, const class Properties ** pDst,
unknown's avatar
unknown committed
144 145 146 147
		volatile bool * stop) const
{
  DBUG_ENTER("ParserImpl::run");

148 149 150 151 152 153 154 155 156 157 158
  * pDst = 0;
  bool ownStop = false;
  if(stop == 0)
    stop = &ownStop;
  
  ctx->m_aliasUsed.clear();
  
  const unsigned sz = sizeof(ctx->m_tokenBuffer);
  ctx->m_currentToken = input.gets(ctx->m_tokenBuffer, sz);
  if(Eof(ctx->m_currentToken)){
    ctx->m_status = Parser<Dummy>::Eof;
unknown's avatar
unknown committed
159
    DBUG_RETURN(false);
160 161 162 163
  }
  
  if(ctx->m_currentToken[0] == 0){
    ctx->m_status = Parser<Dummy>::NoLine;
unknown's avatar
unknown committed
164
    DBUG_RETURN(false);
165 166 167 168
  }
  
  if(Empty(ctx->m_currentToken)){
    ctx->m_status = Parser<Dummy>::EmptyLine;
unknown's avatar
unknown committed
169
    DBUG_RETURN(false);
170 171 172 173 174 175
  }

  trim(ctx->m_currentToken);
  ctx->m_currentCmd = matchCommand(ctx, ctx->m_currentToken, m_rows);
  if(ctx->m_currentCmd == 0){
    ctx->m_status = Parser<Dummy>::UnknownCommand;
unknown's avatar
unknown committed
176
    DBUG_RETURN(false);
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
  }
  
  Properties * p = new Properties();
  
  bool invalidArgument = false;
  ctx->m_currentToken = input.gets(ctx->m_tokenBuffer, sz);
  
  while((! * stop) && 
	!Eof(ctx->m_currentToken) && 
	!Empty(ctx->m_currentToken)){
    if(ctx->m_currentToken[0] != 0){
      trim(ctx->m_currentToken);
      if(!parseArg(ctx, ctx->m_currentToken, ctx->m_currentCmd + 1, p)){
	delete p;
	invalidArgument = true;
	break;
      }
    }
    ctx->m_currentToken = input.gets(ctx->m_tokenBuffer, sz);
  }
  
  if(invalidArgument){
    char buf[sz];
    char * tmp;
    if(!m_breakOnInvalidArg){
      do {
	tmp = input.gets(buf, sz);
      } while((! * stop) && !Eof(tmp) && !Empty(tmp));
    }
unknown's avatar
unknown committed
206
    DBUG_RETURN(false);
207 208 209 210 211
  }
  
  if(* stop){
    delete p;
    ctx->m_status = Parser<Dummy>::ExternalStop;
unknown's avatar
unknown committed
212
    DBUG_RETURN(false);
213 214 215 216 217
  }
  
  if(!checkMandatory(ctx, p)){
    ctx->m_status = Parser<Dummy>::MissingMandatoryArgument;
    delete p;
unknown's avatar
unknown committed
218
    DBUG_RETURN(false);
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
  }

  /**
   * Add alias to properties
   */
  for(unsigned i = 0; i<ctx->m_aliasUsed.size(); i++){
    const ParserRow<Dummy> * alias = ctx->m_aliasUsed[i];
    Properties tmp;
    tmp.put("name", alias->name);
    tmp.put("realName", alias->realName);
    p->put("$ALIAS", i, &tmp);
  }    
  p->put("$ALIAS", ctx->m_aliasUsed.size());
  
  ctx->m_status = Parser<Dummy>::Ok;
  * pDst = p;
unknown's avatar
unknown committed
235
  DBUG_RETURN(true);
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
}

const ParserImpl::DummyRow* 
ParserImpl::matchCommand(Context* ctx, const char* buf, const DummyRow rows[]){
  const char * name = buf;
  const DummyRow * tmp = &rows[0];
  while(tmp->name != 0 && name != 0){
    if(strcmp(tmp->name, name) == 0){
      if(tmp->type == DummyRow::Cmd)
	return tmp;
      if(tmp->type == DummyRow::CmdAlias){
	if(ctx != 0)
	  ctx->m_aliasUsed.push_back(tmp);
	name = tmp->realName;
	tmp = &rows[0];
	continue;
      }
    }
    tmp++;
  }
  return 0;
}

const ParserImpl::DummyRow* 
ParserImpl::matchArg(Context* ctx, const char * buf, const DummyRow rows[]){
  const char * name = buf;
  const DummyRow * tmp = &rows[0];
  while(tmp->name != 0){
    const DummyRow::Type t = tmp->type;
    if(t != DummyRow::Arg && t != DummyRow::ArgAlias && t !=DummyRow::CmdAlias)
      break;
    if(t !=DummyRow::CmdAlias && strcmp(tmp->name, name) == 0){
      if(tmp->type == DummyRow::Arg){
	return tmp;
      }
      if(tmp->type == DummyRow::ArgAlias){
	if(ctx != 0)
	  ctx->m_aliasUsed.push_back(tmp);
	name = tmp->realName;
	tmp = &rows[0];
	continue;
      }
    }
    tmp++;
  }
  return 0;
}

bool
ParserImpl::parseArg(Context * ctx,
		     char * buf, 
		     const DummyRow * rows,
		     Properties * p){
  char * name;
  char * value;
  if(!split(buf, &name, &value)){
    ctx->m_status = Parser<Dummy>::InvalidArgumentFormat;
    return false;
  }
  const DummyRow * arg = matchArg(ctx, name, rows);
  if(arg == 0){
    ctx->m_status = Parser<Dummy>::UnknownArgument;
    return false;
  }
  
  switch(arg->argType){
  case DummyRow::String:
    if(p->put(arg->name, value))
      return true;
    break;
  case DummyRow::Int:{
    Uint32 i;
    int c = sscanf(value, "%u", &i);
    if(c != 1){
      ctx->m_status = Parser<Dummy>::TypeMismatch;
      return false;
    }
    if(p->put(arg->name, i))
      return true;
    break;
  }

  case DummyRow::Properties: {
    Properties *sp = new Properties();
    BaseString v(value);
    UtilBuffer b;
    base64_decode(v, b);
    sp->unpack((const Uint32 *)b.get_data(), b.length());
    break;
  }
  default:
    ctx->m_status = Parser<Dummy>::UnknownArgumentType;
    return false;
  }
  if(p->getPropertiesErrno() == E_PROPERTIES_ELEMENT_ALREADY_EXISTS){
    ctx->m_status = Parser<Dummy>::ArgumentGivenTwice;
    return false;
  }

  abort();
}

bool
ParserImpl::checkMandatory(Context* ctx, const Properties* props){
  const DummyRow * tmp = &ctx->m_currentCmd[1];
  while(tmp->name != 0 && tmp->type == DummyRow::Arg){
    if(tmp->argRequired == ParserRow<Dummy>::Mandatory &&
       !props->contains(tmp->name)){
      ctx->m_status = Parser<Dummy>::MissingMandatoryArgument;
      ctx->m_currentArg = tmp;
      return false;
    }
    tmp++;
  }
  return true;
}

unknown's avatar
unknown committed
353
template class Vector<const ParserRow<ParserImpl::Dummy>*>;