GCOV Execution Analysis for config.c
The left column is the number of times the code was executed
during the unit test suites.
Exec | Code | Line # | |
---|---|---|---|
Source:config.c | 1 | ||
Graph:.libs/config.gcno | 2 | ||
Data:.libs/config.gcda | 3 | ||
Runs:378 | 4 | ||
Programs:378 | 5 | ||
/* -*- linux-c -*- | 6 | ||
* | 7 | ||
* (C) Copyright IBM Corp. 2003-2004 | 8 | ||
* Copyright (c) 2003 by Intel Corp. | 9 | ||
* | 10 | ||
* This program is distributed in the hope that it will be useful, | 11 | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | 12 | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. This | 13 | ||
* file and program are licensed under a BSD style license. See | 14 | ||
* the Copying file included with the OpenHPI distribution for | 15 | ||
* full licensing terms. | 16 | ||
* | 17 | ||
* Authors: | 18 | ||
* Sean Dague <http://dague.net/sean> | 19 | ||
* Louis Zhuang < address removed > | 20 | ||
* David Judkovics < address removed > | 21 | ||
* Thomas Kangieser < address removed > | 22 | ||
* Renier Morales < address removed > | 23 | ||
*/ | 24 | ||
25 | |||
#include <string.h> | 26 | ||
#include <fcntl.h> | 27 | ||
#include <unistd.h> | 28 | ||
#include <oh_config.h> | 29 | ||
#include <oh_plugin.h> | 30 | ||
#include <oh_error.h> | 31 | ||
#include <oh_lock.h> | 32 | ||
#include <config.h> | 33 | ||
34 | |||
/* | 35 | ||
* Global Parameters | 36 | ||
*/ | 37 | ||
static const char *known_globals[] = { | 38 | ||
"OPENHPI_ON_EP", | 39 | ||
"OPENHPI_LOG_ON_SEV", | 40 | ||
//"OPENHPI_DEBUG", | 41 | ||
//"OPENHPI_DEBUG_TRACE", | 42 | ||
//"OPENHPI_DEBUG_LOCK", | 43 | ||
"OPENHPI_THREADED", | 44 | ||
"OPENHPI_PATH", | 45 | ||
"OPENHPI_CONF", | 46 | ||
NULL | 47 | ||
}; | 48 | ||
49 | |||
static struct { | 50 | ||
SaHpiEntityPathT on_ep; | 51 | ||
SaHpiSeverityT log_on_sev; | 52 | ||
//unsigned char dbg; | 53 | ||
//unsigned char dbg_trace; | 54 | ||
//unsigned char dbg_lock; | 55 | ||
unsigned char threaded; | 56 | ||
char path[OH_GLOBAL_STR_MAX_LENGTH]; | 57 | ||
char conf[SAHPI_MAX_TEXT_BUFFER_LENGTH]; | 58 | ||
unsigned char read_env; | 59 | ||
GStaticRecMutex lock; | 60 | ||
} global_params = { | 61 | ||
.on_ep = { .Entry[0] = { .EntityType = SAHPI_ENT_ROOT, .EntityLocation = 0 } }, | 62 | ||
.log_on_sev = SAHPI_MINOR, | 63 | ||
//.dbg = 0, | 64 | ||
//.dbg_trace = 0, | 65 | ||
//.dbg_lock = 0, | 66 | ||
.threaded = 0, | 67 | ||
.path = OH_PLUGIN_PATH, | 68 | ||
.conf = OH_DEFAULT_CONF, | 69 | ||
.read_env = 0, | 70 | ||
.lock = G_STATIC_REC_MUTEX_INIT | 71 | ||
}; | 72 | ||
73 | |||
/* | 74 | ||
* List of plugin names specified in configuration file. | 75 | ||
*/ | 76 | ||
static GSList *plugin_names = NULL; | 77 | ||
78 | |||
/* | 79 | ||
* List of handler configs (parameter tables). This list is | 80 | ||
* populated during config file parse, and used to build the handler_table | 81 | ||
*/ | 82 | ||
static GSList *handler_configs = NULL; | 83 | ||
84 | |||
/******************************************************************************* | 85 | ||
* In order to use the glib lexical parser we need to define token | 86 | ||
* types which we want to switch on | 87 | ||
******************************************************************************/ | 88 | ||
89 | |||
enum { | 90 | ||
HPI_CONF_TOKEN_HANDLER = G_TOKEN_LAST, | 91 | ||
HPI_CONF_TOKEN_PLUGIN | 92 | ||
} hpiConfType; | 93 | ||
94 | |||
struct tokens { | 95 | ||
gchar *name; | 96 | ||
guint token; | 97 | ||
}; | 98 | ||
99 | |||
static struct tokens oh_conf_tokens[] = { | 100 | ||
{ | 101 | ||
.name = "handler", | 102 | ||
.token = HPI_CONF_TOKEN_HANDLER | 103 | ||
}, | 104 | ||
{ | 105 | ||
.name = "plugin", | 106 | ||
.token = HPI_CONF_TOKEN_PLUGIN | 107 | ||
} | 108 | ||
109 | |||
}; | 110 | ||
111 | |||
/******************************************************************************* | 112 | ||
* In order to initialize the lexical scanner, you need the following config. | 113 | ||
* This config was figured out by reading the glib sources, and lots of | 114 | ||
* trial and error (documentation for this isn't very good). | 115 | ||
* | 116 | ||
* G_TOKEN_STRING will be created when anything starts with a-zA-z_/. | 117 | ||
* due to cset_identifier_first and identifier2string values below. | 118 | ||
* Therefor, if you want 0 to be scanned as a string, you need to quote | 119 | ||
* it (e.g. "0") | 120 | ||
* | 121 | ||
*******************************************************************************/ | 122 | ||
123 | |||
static GScannerConfig oh_scanner_config = | 124 | ||
{ | 125 | ||
( | 126 | ||
" \t\n" | 127 | ||
) /* cset_skip_characters */, | 128 | ||
( | 129 | ||
G_CSET_a_2_z | 130 | ||
"_/." | 131 | ||
G_CSET_A_2_Z | 132 | ||
) /* cset_identifier_first */, | 133 | ||
( | 134 | ||
G_CSET_a_2_z | 135 | ||
"_-0123456789/." | 136 | ||
G_CSET_A_2_Z | 137 | ||
) /* cset_identifier_nth */, | 138 | ||
( "#\n" ) /* cpair_comment_single */, | 139 | ||
FALSE /* case_sensitive */, | 140 | ||
TRUE /* skip_comment_multi */, | 141 | ||
TRUE /* skip_comment_single */, | 142 | ||
TRUE /* scan_comment_multi */, | 143 | ||
TRUE /* scan_identifier */, | 144 | ||
TRUE /* scan_identifier_1char */, | 145 | ||
TRUE /* scan_identifier_NULL */, | 146 | ||
TRUE /* scan_symbols */, | 147 | ||
TRUE /* scan_binary */, | 148 | ||
TRUE /* scan_octal */, | 149 | ||
TRUE /* scan_float */, | 150 | ||
TRUE /* scan_hex */, | 151 | ||
TRUE /* scan_hex_dollar */, | 152 | ||
TRUE /* scan_string_sq */, | 153 | ||
TRUE /* scan_string_dq */, | 154 | ||
TRUE /* numbers_2_int */, | 155 | ||
FALSE /* int_2_float */, | 156 | ||
TRUE /* identifier_2_string */, | 157 | ||
TRUE /* char_2_token */, | 158 | ||
TRUE /* symbol_2_token */, | 159 | ||
FALSE /* scope_0_fallback */, | 160 | ||
}; | 161 | ||
162 | |||
static int seen_plugin(char *plugin_name) | 163 | ||
374 | { | 164 | |
374 | int seen = 0; | 165 | |
374 | GSList *node = NULL; | 166 | |
167 | |||
374 | for (node = plugin_names; node; node = node->next) { | 168 | |
0 | char *p_name = (char *)node->data; | 169 | |
0 | if (strcmp(plugin_name, p_name) == 0) { | 170 | |
0 | seen = 1; | 171 | |
0 | break; | 172 | |
} | 173 | ||
} | 174 | ||
175 | |||
374 | return seen; | 176 | |
} | 177 | ||
178 | |||
/** | 179 | ||
* process_plugin_token: | 180 | ||
* @oh_scanner: | 181 | ||
* | 182 | ||
* | 183 | ||
* | 184 | ||
* Return value: | 185 | ||
**/ | 186 | ||
static int process_plugin_token (GScanner *oh_scanner) | 187 | ||
374 | { | 188 | |
374 | guint my_token; | 189 | |
374 | int seen = 0; | 190 | |
191 | |||
374 | data_access_lock(); | 192 | |
193 | |||
374 | my_token = g_scanner_get_next_token(oh_scanner); | 194 | |
374 | if (my_token != HPI_CONF_TOKEN_PLUGIN) { | 195 | |
0 | dbg("Token is not what I was promissed"); | 196 | |
0 | data_access_unlock(); | 197 | |
0 | return -1; | 198 | |
} | 199 | ||
374 | my_token = g_scanner_get_next_token(oh_scanner); | 200 | |
374 | if(my_token != G_TOKEN_STRING) { | 201 | |
0 | dbg("Where the heck is my string!"); | 202 | |
0 | data_access_unlock(); | 203 | |
0 | return -1; | 204 | |
} | 205 | ||
206 | |||
374 | seen = seen_plugin(oh_scanner->value.v_string); | 207 | |
208 | |||
374 | if(!seen) { | 209 | |
374 | plugin_names = g_slist_append( | 210 | |
plugin_names, | 211 | ||
(gpointer) g_strdup(oh_scanner->value.v_string) | 212 | ||
); | 213 | ||
} else { | 214 | ||
0 | dbg("Plugin name %s was found more than once. Ignoring.", | 215 | |
oh_scanner->value.v_string); | 216 | ||
} | 217 | ||
218 | |||
374 | data_access_unlock(); | 219 | |
220 | |||
374 | return 0; | 221 | |
} | 222 | ||
223 | |||
static void process_global_param(const char *name, char *value) | 224 | ||
1496 | { | 225 | |
1496 | if (!strcmp("OPENHPI_ON_EP", name)) { | 226 | |
0 | g_static_rec_mutex_lock(&global_params.lock); | 227 | |
0 | string2entitypath(value, &global_params.on_ep); | 228 | |
0 | g_static_rec_mutex_unlock(&global_params.lock); | 229 | |
1496 | } else if (!strcmp("OPENHPI_LOG_ON_SEV", name)) { | 230 | |
0 | SaHpiTextBufferT buffer; | 231 | |
0 | strncpy(buffer.Data, value, SAHPI_MAX_TEXT_BUFFER_LENGTH); | 232 | |
0 | g_static_rec_mutex_lock(&global_params.lock); | 233 | |
0 | oh_encode_severity(&buffer, &global_params.log_on_sev); | 234 | |
0 | g_static_rec_mutex_unlock(&global_params.lock); | 235 | |
//} else if (!strcmp("OPENHPI_DEBUG", name)) { | 236 | ||
// if (!strcmp("YES", value)) { | 237 | ||
// global_params.dbg = 1; | 238 | ||
// } else { | 239 | ||
// global_params.dbg = 0; | 240 | ||
// } | 241 | ||
//} else if (!strcmp("OPENHPI_DEBUG_TRACE", name)) { | 242 | ||
// if (!strcmp("YES", value)) { | 243 | ||
// global_params.dbg_trace = 1; | 244 | ||
// } else { | 245 | ||
// global_params.dbg_trace = 0; | 246 | ||
// } | 247 | ||
//} else if (!strcmp("OPENHPI_DEBUG_LOCK", name)) { | 248 | ||
// if (!strcmp("YES", value)) { | 249 | ||
// global_params.dbg_lock = 1; | 250 | ||
// } else { | 251 | ||
// global_params.dbg_lock = 0; | 252 | ||
// } | 253 | ||
1496 | } else if (!strcmp("OPENHPI_THREADED", name)) { | 254 | |
0 | g_static_rec_mutex_lock(&global_params.lock); | 255 | |
0 | if (!strcmp("YES", value)) { | 256 | |
0 | global_params.threaded = 1; | 257 | |
} else { | 258 | ||
0 | global_params.threaded = 0; | 259 | |
} | 260 | ||
0 | g_static_rec_mutex_unlock(&global_params.lock); | 261 | |
1496 | } else if (!strcmp("OPENHPI_PATH", name)) { | 262 | |
748 | g_static_rec_mutex_lock(&global_params.lock); | 263 | |
748 | memset(global_params.path, 0, OH_GLOBAL_STR_MAX_LENGTH); | 264 | |
748 | strncpy(global_params.path, value, OH_GLOBAL_STR_MAX_LENGTH-1); | 265 | |
748 | g_static_rec_mutex_unlock(&global_params.lock); | 266 | |
748 | } else if (!strcmp("OPENHPI_CONF", name)) { | 267 | |
748 | g_static_rec_mutex_lock(&global_params.lock); | 268 | |
748 | memset(global_params.conf, 0, SAHPI_MAX_TEXT_BUFFER_LENGTH); | 269 | |
748 | strncpy(global_params.conf, value, SAHPI_MAX_TEXT_BUFFER_LENGTH-1); | 270 | |
748 | g_static_rec_mutex_unlock(&global_params.lock); | 271 | |
} else { | 272 | ||
0 | dbg("ERROR. Invalid global parameter %s in config file", name); | 273 | |
} | 274 | ||
275 | |||
1496 | return; | 276 | |
} | 277 | ||
278 | |||
static void read_globals_from_env(int force) | 279 | ||
5901 | { | 280 | |
5901 | char *tmp_env_str = NULL; | 281 | |
5901 | int i; | 282 | |
283 | |||
5901 | if (!force && global_params.read_env) return; | 284 | |
285 | |||
748 | g_static_rec_mutex_lock(&global_params.lock); | 286 | |
287 | |||
4488 | for (i = 0; known_globals[i]; i++) { | 288 | |
3740 | if ((tmp_env_str = getenv(known_globals[i])) != NULL) { | 289 | |
1496 | process_global_param(known_globals[i], tmp_env_str); | 290 | |
1496 | tmp_env_str = NULL; | 291 | |
} | 292 | ||
} | 293 | ||
294 | |||
748 | global_params.read_env = 1; | 295 | |
748 | g_static_rec_mutex_unlock(&global_params.lock); | 296 | |
} | 297 | ||
298 | |||
/** | 299 | ||
* process_handler_token | 300 | ||
* @oh_scanner | 301 | ||
* | 302 | ||
* handles parsing of handler tokens into a hash table. | 303 | ||
* | 304 | ||
* Return value: 0 on sucess, < 0 on fail | 305 | ||
**/ | 306 | ||
static int process_handler_token (GScanner* oh_scanner) | 307 | ||
374 | { | 308 | |
374 | GHashTable *handler_stanza = NULL; | 309 | |
374 | char *tablekey, *tablevalue; | 310 | |
374 | int found_right_curly = 0; | 311 | |
312 | |||
374 | data_access_lock(); | 313 | |
314 | |||
374 | if (g_scanner_get_next_token(oh_scanner) != HPI_CONF_TOKEN_HANDLER) { | 315 | |
0 | dbg("Processing handler: Unexpected token."); | 316 | |
0 | data_access_unlock(); | 317 | |
0 | return -1; | 318 | |
} | 319 | ||
320 | |||
/* Get the plugin type and store in Hash Table */ | 321 | ||
374 | if (g_scanner_get_next_token(oh_scanner) != G_TOKEN_STRING) { | 322 | |
0 | dbg("Processing handler: Expected string token."); | 323 | |
0 | data_access_unlock(); | 324 | |
0 | return -1; | 325 | |
} else { | 326 | ||
374 | handler_stanza = g_hash_table_new_full(g_str_hash, g_str_equal, | 327 | |
g_free, g_free); | 328 | ||
374 | tablekey = g_strdup("plugin"); | 329 | |
374 | if (!tablekey) { | 330 | |
0 | dbg("Processing handler: Unable to allocate memory"); | 331 | |
0 | goto free_table; | 332 | |
} | 333 | ||
374 | tablevalue = g_strdup(oh_scanner->value.v_string); | 334 | |
374 | if (!tablevalue) { | 335 | |
0 | dbg("Processing handler: Unable to allocate memory"); | 336 | |
0 | goto free_table_and_key; | 337 | |
} | 338 | ||
374 | g_hash_table_insert(handler_stanza, | 339 | |
(gpointer) tablekey, | 340 | ||
(gpointer) tablevalue); | 341 | ||
} | 342 | ||
343 | |||
/* Check for Left Brace token type. If we have it, then continue parsing. */ | 344 | ||
374 | if (g_scanner_get_next_token(oh_scanner) != G_TOKEN_LEFT_CURLY) { | 345 | |
0 | dbg("Processing handler: Expected left curly token."); | 346 | |
0 | goto free_table; | 347 | |
} | 348 | ||
349 | |||
1496 | while(!found_right_curly) { | 350 | |
/* get key token in key\value pair set (e.g. key = value) */ | 351 | ||
1122 | if (g_scanner_get_next_token(oh_scanner) != G_TOKEN_STRING) { | 352 | |
0 | dbg("Processing handler: Expected string token."); | 353 | |
0 | goto free_table; | 354 | |
} else { | 355 | ||
1122 | tablekey = g_strdup(oh_scanner->value.v_string); | 356 | |
1122 | if (!tablekey) { | 357 | |
0 | dbg("Processing handler: Unable to allocate memory"); | 358 | |
0 | goto free_table; | 359 | |
} | 360 | ||
} | 361 | ||
362 | |||
/* Check for the equal sign next. If we have it, continue parsing */ | 363 | ||
1122 | if (g_scanner_get_next_token(oh_scanner) != G_TOKEN_EQUAL_SIGN) { | 364 | |
0 | dbg("Processing handler: Expected equal sign token."); | 365 | |
0 | goto free_table_and_key; | 366 | |
} | 367 | ||
368 | |||
/** | 369 | ||
Now check for the value token in the key\value set. Store the key\value value pair | 370 | ||
in the hash table and continue on. | 371 | ||
*/ | 372 | ||
1122 | if (g_scanner_peek_next_token(oh_scanner) != G_TOKEN_INT && | 373 | |
g_scanner_peek_next_token(oh_scanner) != G_TOKEN_FLOAT && | 374 | ||
g_scanner_peek_next_token(oh_scanner) != G_TOKEN_STRING) { | 375 | ||
0 | dbg("Processing handler: Expected string, integer, or float token."); | 376 | |
0 | goto free_table_and_key; | 377 | |
} else { /* The type of token tells us how to fetch the value from oh_scanner */ | 378 | ||
1122 | gpointer value; | 379 | |
1122 | int current_token = g_scanner_get_next_token(oh_scanner); | 380 | |
381 | |||
1122 | if (current_token == G_TOKEN_INT) { | 382 | |
374 | gulong *value_int = | 383 | |
374 | (gulong *)g_malloc(sizeof(gulong)); | 384 | |
374 | if (value_int != NULL) *value_int = oh_scanner->value.v_int; | 385 | |
374 | value = (gpointer)value_int; | 386 | |
748 | } else if (current_token == G_TOKEN_FLOAT) { | 387 | |
0 | gdouble *value_float = | 388 | |
0 | (gdouble *)g_malloc(sizeof(gdouble)); | 389 | |
0 | if (value_float != NULL) *value_float = oh_scanner->value.v_float; | 390 | |
0 | value = (gpointer)value_float; | 391 | |
} else { | 392 | ||
748 | gchar *value_string = | 393 | |
748 | g_strdup(oh_scanner->value.v_string); | 394 | |
748 | value = (gpointer)value_string; | 395 | |
} | 396 | ||
397 | |||
1122 | if (value == NULL) { | 398 | |
0 | dbg("Processing handler: Unable to allocate memory for value. Token Type: %d", | 399 | |
current_token); | 400 | ||
0 | goto free_table_and_key; | 401 | |
} | 402 | ||
403 | |||
1122 | g_hash_table_insert(handler_stanza, | 404 | |
(gpointer) tablekey, | 405 | ||
value); | 406 | ||
} | 407 | ||
408 | |||
1122 | if (g_scanner_peek_next_token(oh_scanner) == G_TOKEN_RIGHT_CURLY) { | 409 | |
374 | g_scanner_get_next_token(oh_scanner); | 410 | |
374 | found_right_curly = 1; | 411 | |
} | 412 | ||
} /* end of while(!found_right_curly) */ | 413 | ||
414 | |||
/* Attach table describing handler stanza to the global linked list of handlers */ | 415 | ||
374 | if (handler_stanza != NULL) { | 416 | |
374 | handler_configs = g_slist_append( | 417 | |
handler_configs, | 418 | ||
(gpointer) handler_stanza); | 419 | ||
} | 420 | ||
421 | |||
374 | data_access_unlock(); | 422 | |
423 | |||
374 | return 0; | 424 | |
425 | |||
free_table_and_key: | 426 | ||
0 | g_free(tablekey); | 427 | |
free_table: | 428 | ||
/** | 429 | ||
There was an error reading a token so we need to error out, | 430 | ||
but not before cleaning up. Destroy the table. | 431 | ||
*/ | 432 | ||
0 | g_hash_table_destroy(handler_stanza); | 433 | |
434 | |||
0 | data_access_unlock(); | 435 | |
436 | |||
0 | return -1; | 437 | |
} | 438 | ||
439 | |||
static int process_global_token(GScanner *scanner) | 440 | ||
0 | { | 441 | |
0 | char *name = NULL, *value = NULL; | 442 | |
0 | guint current_token; | 443 | |
444 | |||
0 | data_access_lock(); | 445 | |
446 | |||
/* Get the global parameter name */ | 447 | ||
0 | current_token = g_scanner_get_next_token(scanner); | 448 | |
0 | if (current_token != G_TOKEN_STRING) { | 449 | |
0 | dbg("Processing global: Expected string token. Got %d", current_token); | 450 | |
0 | goto quit; | 451 | |
} | 452 | ||
453 | |||
0 | name = g_strdup(scanner->value.v_string); | 454 | |
0 | if (!name) { | 455 | |
0 | dbg("Unable to allocate for global param name."); | 456 | |
0 | goto quit; | 457 | |
} | 458 | ||
459 | |||
0 | current_token = g_scanner_get_next_token(scanner); | 460 | |
0 | if (current_token != G_TOKEN_EQUAL_SIGN) { | 461 | |
0 | dbg("Did not get expected '=' token. Got %d", current_token); | 462 | |
0 | goto free_and_quit; | 463 | |
} | 464 | ||
465 | |||
0 | current_token = g_scanner_get_next_token(scanner); | 466 | |
0 | if (current_token != G_TOKEN_STRING) { | 467 | |
0 | dbg("Did not get expected string value for global parameter. Got %d", current_token); | 468 | |
0 | goto free_and_quit; | 469 | |
} | 470 | ||
471 | |||
0 | value = g_strdup(scanner->value.v_string); | 472 | |
0 | if (!value) { | 473 | |
0 | dbg("Unable to allocate for global param value."); | 474 | |
0 | goto free_and_quit; | 475 | |
} | 476 | ||
477 | |||
0 | process_global_param(name, value); | 478 | |
0 | g_free(name); | 479 | |
0 | g_free(value); | 480 | |
0 | data_access_unlock(); | 481 | |
0 | return 0; | 482 | |
483 | |||
free_and_quit: | 484 | ||
0 | g_free(name); | 485 | |
quit: | 486 | ||
0 | data_access_unlock(); | 487 | |
0 | return -1; | 488 | |
} | 489 | ||
490 | |||
/** | 491 | ||
* scanner_msg_handler: a reference of this function is passed into the GScanner. | 492 | ||
* Used by the GScanner object to output messages that come up during parsing. | 493 | ||
* | 494 | ||
* @scanner: Object used to parse the config file. | 495 | ||
* @message: Message string. | 496 | ||
* @is_error: Bit to say the message is an error. | 497 | ||
* | 498 | ||
* Return value: None (void). | 499 | ||
**/ | 500 | ||
static void scanner_msg_handler (GScanner *scanner, gchar *message, gboolean is_error) | 501 | ||
0 | { | 502 | |
0 | g_return_if_fail (scanner != NULL); | 503 | |
504 | |||
0 | dbg("%s:%d: %s%s\n", | 505 | |
scanner->input_name ? scanner->input_name : "<memory>", | 506 | ||
scanner->line, is_error ? "error: " : "", message ); | 507 | ||
} | 508 | ||
509 | |||
/** | 510 | ||
* oh_load_config: | 511 | ||
* @filename: | 512 | ||
* @config | 513 | ||
* | 514 | ||
* | 515 | ||
* Return value: | 516 | ||
**/ | 517 | ||
int oh_load_config (char *filename, struct oh_parsed_config *config) | 518 | ||
374 | { | 519 | |
374 | int oh_conf_file, i; | 520 | |
374 | GScanner *oh_scanner; | 521 | |
374 | int done = 0; | 522 | |
374 | int num_tokens = sizeof(oh_conf_tokens) / sizeof(oh_conf_tokens[0]); | 523 | |
524 | |||
374 | if (!filename || !config) { | 525 | |
0 | dbg("Error. Invalid parameters"); | 526 | |
0 | return -1; | 527 | |
} | 528 | ||
529 | |||
374 | oh_scanner = g_scanner_new(&oh_scanner_config); | 530 | |
374 | if (!oh_scanner) { | 531 | |
0 | dbg("Couldn't allocate g_scanner for file parsing"); | 532 | |
0 | return -2; | 533 | |
} | 534 | ||
535 | |||
374 | oh_scanner->msg_handler = scanner_msg_handler; | 536 | |
374 | oh_scanner->input_name = filename; | 537 | |
538 | |||
374 | oh_conf_file = open(filename, O_RDONLY); | 539 | |
374 | if (oh_conf_file < 0) { | 540 | |
0 | dbg("Configuration file '%s' could not be opened", filename); | 541 | |
0 | g_scanner_destroy(oh_scanner); | 542 | |
0 | return -4; | 543 | |
} | 544 | ||
545 | |||
374 | g_scanner_input_file(oh_scanner, oh_conf_file); | 546 | |
547 | |||
1122 | for (i = 0; i < num_tokens; i++) { | 548 | |
748 | g_scanner_scope_add_symbol(oh_scanner, 0, oh_conf_tokens[i].name, | 549 | |
(void *)((unsigned long)oh_conf_tokens[i].token)); | 550 | ||
} | 551 | ||
552 | |||
1496 | while (!done) { | 553 | |
1122 | guint my_token; | 554 | |
1122 | my_token = g_scanner_peek_next_token (oh_scanner); | 555 | |
/*dbg("token: %d", my_token);*/ | 556 | ||
1122 | switch (my_token) | 557 | |
{ | 558 | ||
case G_TOKEN_EOF: | 559 | ||
374 | done = 1; | 560 | |
374 | break; | 561 | |
case HPI_CONF_TOKEN_HANDLER: | 562 | ||
374 | process_handler_token(oh_scanner); | 563 | |
374 | break; | 564 | |
case HPI_CONF_TOKEN_PLUGIN: | 565 | ||
374 | process_plugin_token(oh_scanner); | 566 | |
374 | break; | 567 | |
case G_TOKEN_STRING: | 568 | ||
0 | process_global_token(oh_scanner); | 569 | |
0 | break; | 570 | |
default: | 571 | ||
/* need to advance it */ | 572 | ||
0 | my_token = g_scanner_get_next_token(oh_scanner); | 573 | |
0 | g_scanner_unexp_token(oh_scanner, G_TOKEN_SYMBOL, | 574 | |
NULL, "\"handle\" or \"plugin\"", NULL, NULL, 1); | 575 | ||
0 | break; | 576 | |
} | 577 | ||
} | 578 | ||
579 | |||
374 | read_globals_from_env(1); | 580 | |
581 | |||
374 | if (close(oh_conf_file) != 0) { | 582 | |
0 | dbg("Couldn't close file '%s'.", filename); | 583 | |
0 | g_scanner_destroy(oh_scanner); | 584 | |
0 | return -5; | 585 | |
} | 586 | ||
587 | |||
374 | done = oh_scanner->parse_errors; | 588 | |
589 | |||
374 | g_scanner_destroy(oh_scanner); | 590 | |
591 | |||
374 | trace("Done processing conf file.\nNumber of parse errors:%d", done); | 592 | |
593 | |||
374 | config->plugin_names = plugin_names; | 594 | |
374 | config->handler_configs = handler_configs; | 595 | |
596 | |||
374 | return 0; | 597 | |
} | 598 | ||
599 | |||
void oh_clean_config() | 600 | ||
374 | { | 601 | |
/* Free list of plugin names read from configuration file. */ | 602 | ||
374 | g_slist_free(plugin_names); | 603 | |
604 | |||
/* Free list of handler configuration blocks */ | 605 | ||
374 | g_slist_free(handler_configs); | 606 | |
} | 607 | ||
608 | |||
/** | 609 | ||
* oh_get_global_param | 610 | ||
* @param | 611 | ||
* | 612 | ||
* Returns: 0 on Success. | 613 | ||
**/ | 614 | ||
int oh_get_global_param(struct oh_global_param *param) | 615 | ||
5527 | { | 616 | |
5527 | if (!param || !(param->type)) { | 617 | |
0 | dbg("ERROR. Invalid parameters"); | 618 | |
0 | return -1; | 619 | |
} | 620 | ||
621 | |||
5527 | read_globals_from_env(0); | 622 | |
623 | |||
5527 | switch (param->type) { | 624 | |
case OPENHPI_ON_EP: | 625 | ||
2 | g_static_rec_mutex_lock(&global_params.lock); | 626 | |
2 | param->u.on_ep = global_params.on_ep; | 627 | |
2 | g_static_rec_mutex_unlock(&global_params.lock); | 628 | |
2 | break; | 629 | |
case OPENHPI_LOG_ON_SEV: | 630 | ||
4403 | g_static_rec_mutex_lock(&global_params.lock); | 631 | |
4403 | param->u.log_on_sev = global_params.log_on_sev; | 632 | |
4403 | g_static_rec_mutex_unlock(&global_params.lock); | 633 | |
4403 | break; | 634 | |
//case OPENHPI_DEBUG: | 635 | ||
// param->u.dbg = global_params.dbg; | 636 | ||
// break; | 637 | ||
//case OPENHPI_DEBUG_TRACE: | 638 | ||
// param->u.dbg_trace = global_params.dbg_trace; | 639 | ||
// break; | 640 | ||
//case OPENHPI_DEBUG_LOCK: | 641 | ||
// param->u.dbg_lock = global_params.dbg_lock; | 642 | ||
// break; | 643 | ||
case OPENHPI_THREADED: | 644 | ||
374 | param->u.threaded = global_params.threaded; | 645 | |
374 | break; | 646 | |
case OPENHPI_PATH: | 647 | ||
374 | g_static_rec_mutex_lock(&global_params.lock); | 648 | |
374 | strncpy(param->u.path, | 649 | |
global_params.path, | 650 | ||
OH_GLOBAL_STR_MAX_LENGTH); | 651 | ||
374 | g_static_rec_mutex_unlock(&global_params.lock); | 652 | |
374 | break; | 653 | |
case OPENHPI_CONF: | 654 | ||
374 | g_static_rec_mutex_lock(&global_params.lock); | 655 | |
374 | strncpy(param->u.conf, | 656 | |
global_params.conf, | 657 | ||
SAHPI_MAX_TEXT_BUFFER_LENGTH); | 658 | ||
374 | g_static_rec_mutex_unlock(&global_params.lock); | 659 | |
374 | break; | 660 | |
default: | 661 | ||
0 | dbg("ERROR. Invalid global parameter %d!", param->type); | 662 | |
0 | return -2; | 663 | |
} | 664 | ||
665 | |||
5527 | return 0; | 666 | |
} | 667 | ||
668 | |||
/** | 669 | ||
* oh_set_global_param | 670 | ||
* @param | 671 | ||
* | 672 | ||
* Returns: 0 on Success. | 673 | ||
**/ | 674 | ||
int oh_set_global_param(struct oh_global_param *param) | 675 | ||
0 | { | 676 | |
0 | if (!param || !(param->type)) { | 677 | |
0 | dbg("ERROR. Invalid parameters"); | 678 | |
0 | return -1; | 679 | |
} | 680 | ||
681 | |||
0 | read_globals_from_env(0); | 682 | |
683 | |||
0 | switch (param->type) { | 684 | |
case OPENHPI_ON_EP: | 685 | ||
0 | g_static_rec_mutex_lock(&global_params.lock); | 686 | |
0 | global_params.on_ep = param->u.on_ep; | 687 | |
0 | g_static_rec_mutex_unlock(&global_params.lock); | 688 | |
0 | break; | 689 | |
case OPENHPI_LOG_ON_SEV: | 690 | ||
0 | g_static_rec_mutex_lock(&global_params.lock); | 691 | |
0 | global_params.log_on_sev = param->u.log_on_sev; | 692 | |
0 | g_static_rec_mutex_unlock(&global_params.lock); | 693 | |
0 | break; | 694 | |
//case OPENHPI_DEBUG: | 695 | ||
// global_params.dbg = param->u.dbg; | 696 | ||
// break; | 697 | ||
//case OPENHPI_DEBUG_TRACE: | 698 | ||
// global_params.dbg_trace = param->u.dbg_trace; | 699 | ||
// break; | 700 | ||
//case OPENHPI_DEBUG_LOCK: | 701 | ||
// global_params.dbg_lock = param->u.dbg_lock; | 702 | ||
// break; | 703 | ||
case OPENHPI_THREADED: | 704 | ||
0 | global_params.threaded = param->u.threaded; | 705 | |
0 | break; | 706 | |
case OPENHPI_PATH: | 707 | ||
0 | g_static_rec_mutex_lock(&global_params.lock); | 708 | |
0 | memset(global_params.path, 0, OH_GLOBAL_STR_MAX_LENGTH); | 709 | |
0 | strncpy(global_params.path, | 710 | |
param->u.path, | 711 | ||
OH_GLOBAL_STR_MAX_LENGTH-1); | 712 | ||
0 | g_static_rec_mutex_unlock(&global_params.lock); | 713 | |
0 | break; | 714 | |
case OPENHPI_CONF: | 715 | ||
0 | g_static_rec_mutex_lock(&global_params.lock); | 716 | |
0 | memset(global_params.conf, 0, SAHPI_MAX_TEXT_BUFFER_LENGTH); | 717 | |
0 | strncpy(global_params.conf, | 718 | |
param->u.conf, | 719 | ||
SAHPI_MAX_TEXT_BUFFER_LENGTH-1); | 720 | ||
0 | g_static_rec_mutex_unlock(&global_params.lock); | 721 | |
0 | break; | 722 | |
default: | 723 | ||
0 | dbg("ERROR. Invalid global parameter %d!", param->type); | 724 | |
0 | return -2; | 725 | |
} | 726 | ||
727 | |||
0 | return 0; | 728 | |
} | 729 | ||
730 | |||
//unsigned char oh_get_global_bool(oh_global_param_type type) | 731 | ||
//{ | 732 | ||
// if (!type) { | 733 | ||
// dbg("ERROR. Invalid parameters"); | 734 | ||
// return 0; | 735 | ||
// } | 736 | ||
// | 737 | ||
// read_globals_from_env(0); | 738 | ||
// | 739 | ||
// switch (type) { | 740 | ||
// case OPENHPI_DEBUG: | 741 | ||
// return global_params.dbg; | 742 | ||
// case OPENHPI_DEBUG_TRACE: | 743 | ||
// return global_params.dbg_trace; | 744 | ||
// case OPENHPI_DEBUG_LOCK: | 745 | ||
// return global_params.dbg_lock; | 746 | ||
// case OPENHPI_THREADED: | 747 | ||
// return global_params.threaded; | 748 | ||
// default: | 749 | ||
// dbg("ERROR. Invalid global parameter type %d!", type); | 750 | ||
// return 0; | 751 | ||
// } | 752 | ||
//} | 753 |