Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 201 lines (158 sloc) 4.733 kb
0489e93 @brixen Always use #include <xxx.h> rather than <cxxx>.
brixen authored
1 #include <string.h>
2 #include <stdlib.h>
8dc150e Implement ConfigParser, wire in into RUBY_CONFIG.
Evan Phoenix authored
3 #include <string>
97de677 Config files can load other config files
Evan Phoenix authored
4 #include <fstream>
37dc489 Add real Configuration infrastructure
Evan Phoenix authored
5
b09a24c Wire the JIT in via MachineMethod
Evan Phoenix authored
6 #include "config_parser.hpp"
37dc489 Add real Configuration infrastructure
Evan Phoenix authored
7 #include "configuration.hpp"
8dc150e Implement ConfigParser, wire in into RUBY_CONFIG.
Evan Phoenix authored
8
9 namespace rubinius {
10 /* utility: checks whether string contains only digits */
11 static bool is_number(const char *str) {
52123ef Fix parsing negative numbers
Evan Phoenix authored
12 if(*str == '-') str++;
13
8dc150e Implement ConfigParser, wire in into RUBY_CONFIG.
Evan Phoenix authored
14 while(*str) {
15 if(!isdigit(*str)) return false;
16 str++;
17 }
18
19 return true;
20 }
21
52123ef Fix parsing negative numbers
Evan Phoenix authored
22 static bool valid_char(char c) {
23 return isalnum(c) || c == '-';
24 }
25
8dc150e Implement ConfigParser, wire in into RUBY_CONFIG.
Evan Phoenix authored
26
27 /* utility: strips trailing non-alnum chars from string */
28 static char *trim_str(char *str) {
29 int i;
52123ef Fix parsing negative numbers
Evan Phoenix authored
30 while(*str && !valid_char(*str) && *str != '/') str++;
8dc150e Implement ConfigParser, wire in into RUBY_CONFIG.
Evan Phoenix authored
31
52123ef Fix parsing negative numbers
Evan Phoenix authored
32 for(i = strlen(str); i-- && !valid_char(str[i]) && str[i] != '/';) {
8dc150e Implement ConfigParser, wire in into RUBY_CONFIG.
Evan Phoenix authored
33 str[i] = 0;
34 }
35
36 return str;
37 }
38
37dc489 Add real Configuration infrastructure
Evan Phoenix authored
39 ConfigParser::~ConfigParser() {
40 ConfigParser::ConfigMap::iterator i = variables.begin();
41 while(i != variables.end()) {
42 delete i->second;
fa15100 @nazgob changed postfix operators to prefix operators for non-primitive types
nazgob authored
43 ++i;
37dc489 Add real Configuration infrastructure
Evan Phoenix authored
44 }
45 }
46
97de677 Config files can load other config files
Evan Phoenix authored
47 bool ConfigParser::load_file(std::string path) {
48 std::ifstream stream(path.c_str());
49 if(!stream) return false;
50
51 import_stream(stream);
52
53 return true;
54 }
55
56 bool ConfigParser::process_internal(std::string key, std::string val) {
57 if(key == "include") {
58 if(!load_file(val)) {
59 std::cerr << "WARNING: Unable to include '" << val << "'.\n";
60 }
61 } else if(key == "include_maybe") {
62 load_file(val);
63 } else {
64 return false;
65 }
66
67 return true;
68 }
69
8dc150e Implement ConfigParser, wire in into RUBY_CONFIG.
Evan Phoenix authored
70 ConfigParser::Entry* ConfigParser::parse_line(const char* line) {
71 char* var = strdup(line);
8807290 Use strchr() instead of strstr() when searching for single characters.
Tilman Sauerbeck authored
72 char* equals = strchr(var, '=');
8dc150e Implement ConfigParser, wire in into RUBY_CONFIG.
Evan Phoenix authored
73
cd0db01 Rework parse_line
Evan Phoenix authored
74 if(equals) {
6c31ab6 Add cli switch to enable jit
Evan Phoenix authored
75 /* Split the string. */
76 *equals++ = 0;
8dc150e Implement ConfigParser, wire in into RUBY_CONFIG.
Evan Phoenix authored
77 }
78
97de677 Config files can load other config files
Evan Phoenix authored
79 std::string variable = std::string(trim_str(var));
cd0db01 Rework parse_line
Evan Phoenix authored
80
97de677 Config files can load other config files
Evan Phoenix authored
81 std::string value;
cd0db01 Rework parse_line
Evan Phoenix authored
82 // Just the variable name means true, as in enable
83 if(equals) {
97de677 Config files can load other config files
Evan Phoenix authored
84 value = std::string(trim_str(equals));
cd0db01 Rework parse_line
Evan Phoenix authored
85 } else {
97de677 Config files can load other config files
Evan Phoenix authored
86 value = std::string("true");
cd0db01 Rework parse_line
Evan Phoenix authored
87 }
8dc150e Implement ConfigParser, wire in into RUBY_CONFIG.
Evan Phoenix authored
88
97de677 Config files can load other config files
Evan Phoenix authored
89 if(process_internal(variable, value)) {
90 free(var);
91 return NULL;
92 }
93
94 Entry* entry = new ConfigParser::Entry();
95 entry->variable = variable;
96 entry->value = value;
97
8dc150e Implement ConfigParser, wire in into RUBY_CONFIG.
Evan Phoenix authored
98 free(var);
99
100 return entry;
101 }
102
6c31ab6 Add cli switch to enable jit
Evan Phoenix authored
103 void ConfigParser::import_line(const char* line) {
104 ConfigParser::Entry* entry = parse_line(line);
105 if(entry) {
106 variables[entry->variable] = entry;
107 }
108 }
109
8dc150e Implement ConfigParser, wire in into RUBY_CONFIG.
Evan Phoenix authored
110 void ConfigParser::import_stream(std::istream& stream) {
111 while(!stream.eof()) {
112 std::string line;
113 getline(stream, line);
114 ConfigParser::Entry* entry = parse_line(line.c_str());
115 if(entry) {
116 variables[entry->variable] = entry;
117 }
118 }
119 }
120
98cfb93 Support RBX_OPTIONS as a way to set options
Evan Phoenix authored
121 void ConfigParser::import_many(std::string string) {
122 size_t start = 0;
123 size_t end = 0;
124 while((end = string.find(";", start)) != std::string::npos) {
125 std::string sub = string.substr(start, end-start);
126 import_line(sub.c_str());
127 start = end + 1;
128 }
129
130 std::string sub = string.substr(start);
131 import_line(sub.c_str());
132 }
133
8dc150e Implement ConfigParser, wire in into RUBY_CONFIG.
Evan Phoenix authored
134 ConfigParser::Entry* ConfigParser::find(std::string name) {
135 ConfigParser::ConfigMap::iterator i = variables.find(name);
136
137 if(i == variables.end()) return NULL;
138
139 return i->second;
140 }
141
73fb486 Introduce tooling API, convert profiler to it.
Evan Phoenix authored
142 void ConfigParser::set(const char* name, const char* val) {
143 ConfigParser::ConfigMap::iterator i = variables.find(name);
144 if(i == variables.end()) {
145 i->second->value = val;
146 }
147
148 Entry* entry = new ConfigParser::Entry();
149 entry->variable = name;
150 entry->value = val;
151
152 variables[entry->variable] = entry;
153 }
154
bbe4243 @nazgob const ftw!
nazgob authored
155 bool ConfigParser::Entry::is_number() const {
8dc150e Implement ConfigParser, wire in into RUBY_CONFIG.
Evan Phoenix authored
156 return rubinius::is_number(value.c_str());
157 }
158
bbe4243 @nazgob const ftw!
nazgob authored
159 bool ConfigParser::Entry::is_true() const {
6c31ab6 Add cli switch to enable jit
Evan Phoenix authored
160 return value == "true";
161 }
162
bbe4243 @nazgob const ftw!
nazgob authored
163 long ConfigParser::Entry::to_i() const {
d722e15 @brixen Enable configuring some young generation parameters.
brixen authored
164 return strtol(value.c_str(), NULL, 10);
165 }
166
bbe4243 @nazgob const ftw!
nazgob authored
167 bool ConfigParser::Entry::in_section(std::string prefix) const {
8dc150e Implement ConfigParser, wire in into RUBY_CONFIG.
Evan Phoenix authored
168 return variable.find(prefix) == 0;
169 }
170
171 ConfigParser::EntryList* ConfigParser::get_section(std::string prefix) {
172 ConfigParser::EntryList* list = new ConfigParser::EntryList(0);
173
174 ConfigParser::ConfigMap::iterator i = variables.begin();
175 while(i != variables.end()) {
176 if(i->second->in_section(prefix)) {
177 list->push_back(i->second);
178 }
fa15100 @nazgob changed postfix operators to prefix operators for non-primitive types
nazgob authored
179 ++i;
8dc150e Implement ConfigParser, wire in into RUBY_CONFIG.
Evan Phoenix authored
180 }
181
182 return list;
183 }
37dc489 Add real Configuration infrastructure
Evan Phoenix authored
184
185 void ConfigParser::update_configuration(Configuration& config) {
186 for(ConfigParser::ConfigMap::iterator i = variables.begin();
187 i != variables.end();
fa15100 @nazgob changed postfix operators to prefix operators for non-primitive types
nazgob authored
188 ++i) {
f68f4b1 Warn for unrecognized vm options
Evan Phoenix authored
189 if(!config.import(i->first.c_str(), i->second->value.c_str())) {
190 if(i->second->in_section("vm.") ||
d03fb5a Catch unknown gc. options
Evan Phoenix authored
191 i->second->in_section("jit.") ||
192 i->second->in_section("gc.")) {
f68f4b1 Warn for unrecognized vm options
Evan Phoenix authored
193 std::cout << "Warning: Unrecognized VM option '" << i->first << "'\n";
194 }
195 }
37dc489 Add real Configuration infrastructure
Evan Phoenix authored
196 }
b69787a Introduce -Xprofile to be used instead of -P
Evan Phoenix authored
197
198 config.finalize();
37dc489 Add real Configuration infrastructure
Evan Phoenix authored
199 }
8dc150e Implement ConfigParser, wire in into RUBY_CONFIG.
Evan Phoenix authored
200 }
Something went wrong with that request. Please try again.