0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
InteractiveCommand.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
26 #include "Common/Compat.h"
27 
28 #include <cassert>
29 #include <cstring>
30 #include <iostream>
31 #include <string>
32 
33 #include "InteractiveCommand.h"
34 #include "Usage.h"
35 
36 using namespace Hypertable;
37 using namespace std;
38 
39 
41  size_t cmd_len = strlen(this->command_text());
42  const char *base, *ptr;
43  string key, value;
44 
45  assert(!strncmp(line, command_text(), cmd_len));
46 
47  m_args.clear();
48 
49  ptr = line + cmd_len;
50 
51  while (true) {
52  key = "";
53  value = "";
54 
55  // skip whitespace
56  while (*ptr && isspace(*ptr))
57  ptr++;
58  if (*ptr == 0)
59  break;
60 
61  if (*ptr == '\"') {
62  if (!parse_string_literal(ptr, key, &ptr))
63  exit(EXIT_FAILURE);
64  }
65  else {
66  base = ptr;
67  while (true) {
68  if (*ptr == '=') {
69  key = string(base, ptr-base);
70  base = ++ptr;
71  if (*base == '\"') {
72  if (!parse_string_literal(base, value, &ptr))
73  exit(EXIT_FAILURE);
74  break;
75  }
76  else {
77  for (ptr = base; *ptr && !isspace(*ptr); ptr++)
78  ;
79  value = string(base, ptr-base);
80  break;
81  }
82  }
83  else if (*ptr == 0 || isspace(*ptr)) {
84  key = string(base, ptr-base);
85  break;
86  }
87  ptr++;
88  }
89  }
90  m_args.push_back(pair<string, string>(key, value));
91  }
92 }
93 
94 
95 bool
96 InteractiveCommand::parse_string_literal(const char *str, std::string &text,
97  const char **endptr) {
98  int lastchar = *str;
99  const char *ptr, *base;
100 
101  assert(*str == '"');
102 
103  base = str + 1;
104 
105  for (ptr = base; *ptr; ++ptr) {
106  if (*ptr == '\"' && lastchar != '\\')
107  break;
108  lastchar = *ptr;
109  }
110 
111  if (*ptr == 0) {
112  cerr << "Un-terminated string literal." << endl;
113  return false;
114  }
115 
116  text = string(base, ptr-base);
117 
118  *endptr = ptr + 1;
119  return true;
120 }
Helper class for printing usage banners on the command line.
bool parse_string_literal(const char *str, std::string &text, const char **endptr)
Helper function to parse a string literal.
Po::typed_value< String > * str(String *v=0)
Definition: Properties.h:166
STL namespace.
Base class for interactive shell commands.
Compatibility Macros for C/C++.
Hypertable definitions
void parse_command_line(const char *line)
Parses a command line and stores the arguments in m_args.