| 1 |
/* |
| 2 |
* Copyright (c) 2002-2005, Network Appliance, Inc. All rights reserved. |
| 3 |
* |
| 4 |
* This Software is licensed under one of the following licenses: |
| 5 |
* |
| 6 |
* 1) under the terms of the "Common Public License 1.0" a copy of which is |
| 7 |
* in the file LICENSE.txt in the root directory. The license is also |
| 8 |
* available from the Open Source Initiative, see |
| 9 |
* http://www.opensource.org/licenses/cpl.php. |
| 10 |
* |
| 11 |
* 2) under the terms of the "The BSD License" a copy of which is in the file |
| 12 |
* LICENSE2.txt in the root directory. The license is also available from |
| 13 |
* the Open Source Initiative, see |
| 14 |
* http://www.opensource.org/licenses/bsd-license.php. |
| 15 |
* |
| 16 |
* 3) under the terms of the "GNU General Public License (GPL) Version 2" a |
| 17 |
* copy of which is in the file LICENSE3.txt in the root directory. The |
| 18 |
* license is also available from the Open Source Initiative, see |
| 19 |
* http://www.opensource.org/licenses/gpl-license.php. |
| 20 |
* |
| 21 |
* Licensee has the right to choose one of the above licenses. |
| 22 |
* |
| 23 |
* Redistributions of source code must retain the above copyright |
| 24 |
* notice and one of the license notices. |
| 25 |
* |
| 26 |
* Redistributions in binary form must reproduce both the above copyright |
| 27 |
* notice, one of the license notices in the documentation |
| 28 |
* and/or other materials provided with the distribution. |
| 29 |
*/ |
| 30 |
|
| 31 |
/********************************************************************** |
| 32 |
* |
| 33 |
* MODULE: dat_sr_parser.c |
| 34 |
* |
| 35 |
* PURPOSE: static registry parser |
| 36 |
* |
| 37 |
* $Id: udat_sr_parser.c,v 1.6 2005/03/24 05:58:36 jlentini Exp $ |
| 38 |
**********************************************************************/ |
| 39 |
|
| 40 |
#include <dat2/udat.h> |
| 41 |
#include "udat_sr_parser.h" |
| 42 |
#include "dat_sr.h" |
| 43 |
|
| 44 |
/********************************************************************* |
| 45 |
* * |
| 46 |
* Constants * |
| 47 |
* * |
| 48 |
*********************************************************************/ |
| 49 |
|
| 50 |
#define DAT_SR_CONF_ENV "DAT_OVERRIDE" |
| 51 |
#if defined(_WIN32) || defined(_WIN64) |
| 52 |
#define DAT_SR_CONF_DEFAULT "C:\\DAT\\dat.conf" |
| 53 |
#else |
| 54 |
#define DAT_SR_CONF_DEFAULT "/etc/dat.conf" |
| 55 |
#endif |
| 56 |
|
| 57 |
#define DAT_SR_TOKEN_THREADSAFE "threadsafe" |
| 58 |
#define DAT_SR_TOKEN_NONTHREADSAFE "nonthreadsafe" |
| 59 |
#define DAT_SR_TOKEN_DEFAULT "default" |
| 60 |
#define DAT_SR_TOKEN_NONDEFAULT "nondefault" |
| 61 |
|
| 62 |
#define DAT_SR_CHAR_NEWLINE '\n' |
| 63 |
#define DAT_SR_CHAR_COMMENT '#' |
| 64 |
#define DAT_SR_CHAR_QUOTE '"' |
| 65 |
#define DAT_SR_CHAR_BACKSLASH '\\' |
| 66 |
|
| 67 |
/********************************************************************* |
| 68 |
* * |
| 69 |
* Enumerations * |
| 70 |
* * |
| 71 |
*********************************************************************/ |
| 72 |
|
| 73 |
typedef enum { |
| 74 |
DAT_SR_TOKEN_STRING, /* text field (both quoted or unquoted) */ |
| 75 |
DAT_SR_TOKEN_EOR, /* end of record (newline) */ |
| 76 |
DAT_SR_TOKEN_EOF /* end of file */ |
| 77 |
} DAT_SR_TOKEN_TYPE; |
| 78 |
|
| 79 |
typedef enum { |
| 80 |
DAT_SR_API_UDAT, |
| 81 |
DAT_SR_API_KDAT |
| 82 |
} DAT_SR_API_TYPE; |
| 83 |
|
| 84 |
/********************************************************************* |
| 85 |
* * |
| 86 |
* Structures * |
| 87 |
* * |
| 88 |
*********************************************************************/ |
| 89 |
|
| 90 |
typedef struct { |
| 91 |
DAT_SR_TOKEN_TYPE type; |
| 92 |
char *value; /* valid if type is DAT_SR_TOKEN_STRING */ |
| 93 |
DAT_OS_SIZE value_len; |
| 94 |
} DAT_SR_TOKEN; |
| 95 |
|
| 96 |
typedef struct DAT_SR_STACK_NODE { |
| 97 |
DAT_SR_TOKEN token; |
| 98 |
struct DAT_SR_STACK_NODE *next; |
| 99 |
} DAT_SR_STACK_NODE; |
| 100 |
|
| 101 |
typedef struct { |
| 102 |
DAT_UINT32 major; |
| 103 |
DAT_UINT32 minor; |
| 104 |
} DAT_SR_VERSION; |
| 105 |
|
| 106 |
typedef struct { |
| 107 |
char *id; |
| 108 |
DAT_SR_VERSION version; |
| 109 |
} DAT_SR_PROVIDER_VERSION; |
| 110 |
|
| 111 |
typedef struct { |
| 112 |
DAT_SR_API_TYPE type; |
| 113 |
DAT_SR_VERSION version; |
| 114 |
} DAT_SR_API_VERSION; |
| 115 |
|
| 116 |
typedef struct { |
| 117 |
char *ia_name; |
| 118 |
DAT_SR_API_VERSION api_version; |
| 119 |
DAT_BOOLEAN is_thread_safe; |
| 120 |
DAT_BOOLEAN is_default; |
| 121 |
char *lib_path; |
| 122 |
DAT_SR_PROVIDER_VERSION provider_version; |
| 123 |
char *ia_params; |
| 124 |
char *platform_params; |
| 125 |
} DAT_SR_CONF_ENTRY; |
| 126 |
|
| 127 |
/********************************************************************* |
| 128 |
* * |
| 129 |
* Internal Function Declarations * |
| 130 |
* * |
| 131 |
*********************************************************************/ |
| 132 |
|
| 133 |
static DAT_RETURN dat_sr_load_entry(DAT_SR_CONF_ENTRY * entry); |
| 134 |
|
| 135 |
static DAT_BOOLEAN dat_sr_is_valid_entry(DAT_SR_CONF_ENTRY * entry); |
| 136 |
|
| 137 |
static char *dat_sr_type_to_str(DAT_SR_TOKEN_TYPE type); |
| 138 |
|
| 139 |
static DAT_RETURN dat_sr_parse_eof(DAT_OS_FILE * file); |
| 140 |
|
| 141 |
static DAT_RETURN dat_sr_parse_entry(DAT_OS_FILE * file); |
| 142 |
|
| 143 |
static DAT_RETURN |
| 144 |
dat_sr_parse_ia_name(DAT_OS_FILE * file, DAT_SR_CONF_ENTRY * entry); |
| 145 |
|
| 146 |
static DAT_RETURN |
| 147 |
dat_sr_parse_api(DAT_OS_FILE * file, DAT_SR_CONF_ENTRY * entry); |
| 148 |
|
| 149 |
static DAT_RETURN |
| 150 |
dat_sr_parse_thread_safety(DAT_OS_FILE * file, DAT_SR_CONF_ENTRY * entry); |
| 151 |
|
| 152 |
static DAT_RETURN |
| 153 |
dat_sr_parse_default(DAT_OS_FILE * file, DAT_SR_CONF_ENTRY * entry); |
| 154 |
|
| 155 |
static DAT_RETURN |
| 156 |
dat_sr_parse_lib_path(DAT_OS_FILE * file, DAT_SR_CONF_ENTRY * entry); |
| 157 |
|
| 158 |
static DAT_RETURN |
| 159 |
dat_sr_parse_provider_version(DAT_OS_FILE * file, DAT_SR_CONF_ENTRY * entry); |
| 160 |
|
| 161 |
static DAT_RETURN |
| 162 |
dat_sr_parse_ia_params(DAT_OS_FILE * file, DAT_SR_CONF_ENTRY * entry); |
| 163 |
|
| 164 |
static DAT_RETURN |
| 165 |
dat_sr_parse_platform_params(DAT_OS_FILE * file, DAT_SR_CONF_ENTRY * entry); |
| 166 |
|
| 167 |
static DAT_RETURN |
| 168 |
dat_sr_parse_eoe(DAT_OS_FILE * file, DAT_SR_CONF_ENTRY * entry); |
| 169 |
|
| 170 |
static DAT_RETURN |
| 171 |
dat_sr_convert_api(char *str, DAT_SR_API_VERSION * api_version); |
| 172 |
|
| 173 |
static DAT_RETURN |
| 174 |
dat_sr_convert_thread_safety(char *str, DAT_BOOLEAN * is_thread_safe); |
| 175 |
|
| 176 |
static DAT_RETURN dat_sr_convert_default(char *str, DAT_BOOLEAN * is_default); |
| 177 |
|
| 178 |
static DAT_RETURN |
| 179 |
dat_sr_convert_provider_version(char *str, |
| 180 |
DAT_SR_PROVIDER_VERSION * provider_version); |
| 181 |
|
| 182 |
static DAT_RETURN dat_sr_get_token(DAT_OS_FILE * file, DAT_SR_TOKEN * token); |
| 183 |
|
| 184 |
static DAT_RETURN dat_sr_put_token(DAT_OS_FILE * file, DAT_SR_TOKEN * token); |
| 185 |
|
| 186 |
static DAT_RETURN dat_sr_read_token(DAT_OS_FILE * file, DAT_SR_TOKEN * token); |
| 187 |
|
| 188 |
static DAT_RETURN |
| 189 |
dat_sr_read_str(DAT_OS_FILE * file, |
| 190 |
DAT_SR_TOKEN * token, DAT_OS_SIZE token_len); |
| 191 |
|
| 192 |
static DAT_RETURN |
| 193 |
dat_sr_read_quoted_str(DAT_OS_FILE * file, |
| 194 |
DAT_SR_TOKEN * token, |
| 195 |
DAT_OS_SIZE token_len, DAT_COUNT num_escape_seq); |
| 196 |
|
| 197 |
static void dat_sr_read_comment(DAT_OS_FILE * file); |
| 198 |
|
| 199 |
/********************************************************************* |
| 200 |
* * |
| 201 |
* Global Variables * |
| 202 |
* * |
| 203 |
*********************************************************************/ |
| 204 |
|
| 205 |
static DAT_SR_STACK_NODE *g_token_stack = NULL; |
| 206 |
|
| 207 |
/********************************************************************* |
| 208 |
* * |
| 209 |
* External Function Definitions * |
| 210 |
* * |
| 211 |
*********************************************************************/ |
| 212 |
|
| 213 |
/*********************************************************************** |
| 214 |
* Function: dat_sr_load |
| 215 |
***********************************************************************/ |
| 216 |
|
| 217 |
DAT_RETURN dat_sr_load(void) |
| 218 |
{ |
| 219 |
char *sr_path; |
| 220 |
DAT_OS_FILE *sr_file; |
| 221 |
|
| 222 |
sr_path = dat_os_getenv(DAT_SR_CONF_ENV); |
| 223 |
|
| 224 |
/* environment override */ |
| 225 |
if ((sr_path != NULL) && ((sr_file = dat_os_fopen(sr_path)) == NULL)) { |
| 226 |
dat_os_dbg_print(DAT_OS_DBG_TYPE_ERROR, |
| 227 |
"DAT Registry: DAT_OVERRIDE, " |
| 228 |
"bad filename - %s, aborting\n", sr_path); |
| 229 |
goto bail; |
| 230 |
} |
| 231 |
|
| 232 |
if (sr_path == NULL) { |
| 233 |
|
| 234 |
#ifdef DAT_CONF |
| 235 |
sr_path = DAT_CONF; |
| 236 |
#else |
| 237 |
sr_path = DAT_SR_CONF_DEFAULT; |
| 238 |
#endif |
| 239 |
sr_file = dat_os_fopen(sr_path); |
| 240 |
if (sr_file == NULL) { |
| 241 |
#ifdef DAT_CONF |
| 242 |
dat_os_dbg_print(DAT_OS_DBG_TYPE_SR, |
| 243 |
"DAT Registry: sysconfdir, " |
| 244 |
"bad filename - %s, retry default at %s\n", |
| 245 |
sr_path, DAT_SR_CONF_DEFAULT); |
| 246 |
/* try default after sysconfdir fails */ |
| 247 |
sr_path = DAT_SR_CONF_DEFAULT; |
| 248 |
sr_file = dat_os_fopen(sr_path); |
| 249 |
if (sr_file == NULL) { |
| 250 |
#endif |
| 251 |
dat_os_dbg_print(DAT_OS_DBG_TYPE_ERROR, |
| 252 |
"DAT Registry: default, " |
| 253 |
"bad filename - %s, aborting\n", |
| 254 |
sr_path); |
| 255 |
goto bail; |
| 256 |
#ifdef DAT_CONF |
| 257 |
} |
| 258 |
#endif |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
dat_os_dbg_print(DAT_OS_DBG_TYPE_GENERIC, |
| 263 |
"DAT Registry: using config file %s\n", sr_path); |
| 264 |
|
| 265 |
for (;;) { |
| 266 |
if (DAT_SUCCESS == dat_sr_parse_eof(sr_file)) { |
| 267 |
break; |
| 268 |
} else if (DAT_SUCCESS == dat_sr_parse_entry(sr_file)) { |
| 269 |
continue; |
| 270 |
} else { |
| 271 |
dat_os_dbg_print(DAT_OS_DBG_TYPE_ERROR, |
| 272 |
"DAT Registry: ERROR parsing - %s\n", |
| 273 |
sr_path); |
| 274 |
goto cleanup; |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
if (0 != dat_os_fclose(sr_file)) { |
| 279 |
dat_os_dbg_print(DAT_OS_DBG_TYPE_ERROR, |
| 280 |
"DAT Registry: ERROR closing - %s\n", sr_path); |
| 281 |
goto bail; |
| 282 |
} |
| 283 |
|
| 284 |
return DAT_SUCCESS; |
| 285 |
|
| 286 |
cleanup: |
| 287 |
dat_os_fclose(sr_file); |
| 288 |
bail: |
| 289 |
return DAT_INTERNAL_ERROR; |
| 290 |
|
| 291 |
} |
| 292 |
|
| 293 |
/********************************************************************* |
| 294 |
* * |
| 295 |
* Internal Function Definitions * |
| 296 |
* * |
| 297 |
*********************************************************************/ |
| 298 |
|
| 299 |
/*********************************************************************** |
| 300 |
* Function: dat_sr_is_valid_entry |
| 301 |
***********************************************************************/ |
| 302 |
|
| 303 |
DAT_BOOLEAN dat_sr_is_valid_entry(DAT_SR_CONF_ENTRY * entry) |
| 304 |
{ |
| 305 |
if ((DAT_SR_API_UDAT == entry->api_version.type) && (entry->is_default)) { |
| 306 |
return DAT_TRUE; |
| 307 |
} else { |
| 308 |
return DAT_FALSE; |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
/*********************************************************************** |
| 313 |
* Function: dat_sr_load_entry |
| 314 |
***********************************************************************/ |
| 315 |
|
| 316 |
DAT_RETURN dat_sr_load_entry(DAT_SR_CONF_ENTRY * conf_entry) |
| 317 |
{ |
| 318 |
DAT_SR_ENTRY entry; |
| 319 |
|
| 320 |
if (DAT_NAME_MAX_LENGTH < (strlen(conf_entry->ia_name) + 1)) { |
| 321 |
dat_os_dbg_print(DAT_OS_DBG_TYPE_SR, |
| 322 |
"DAT Registry: ia name %s is longer than " |
| 323 |
"DAT_NAME_MAX_LENGTH (%i)\n", |
| 324 |
conf_entry->ia_name, DAT_NAME_MAX_LENGTH); |
| 325 |
|
| 326 |
return DAT_INSUFFICIENT_RESOURCES; |
| 327 |
} |
| 328 |
|
| 329 |
dat_os_strncpy(entry.info.ia_name, conf_entry->ia_name, |
| 330 |
DAT_NAME_MAX_LENGTH); |
| 331 |
entry.info.dapl_version_major = conf_entry->api_version.version.major; |
| 332 |
entry.info.dapl_version_minor = conf_entry->api_version.version.minor; |
| 333 |
entry.info.is_thread_safe = conf_entry->is_thread_safe; |
| 334 |
entry.lib_path = conf_entry->lib_path; |
| 335 |
entry.ia_params = conf_entry->ia_params; |
| 336 |
entry.lib_handle = NULL; |
| 337 |
entry.ref_count = 0; |
| 338 |
|
| 339 |
dat_os_dbg_print(DAT_OS_DBG_TYPE_SR, |
| 340 |
"DAT Registry: loading provider for %s\n", |
| 341 |
conf_entry->ia_name); |
| 342 |
|
| 343 |
return dat_sr_insert(&entry.info, &entry); |
| 344 |
} |
| 345 |
|
| 346 |
/*********************************************************************** |
| 347 |
* Function: dat_sr_type_to_str |
| 348 |
***********************************************************************/ |
| 349 |
|
| 350 |
char *dat_sr_type_to_str(DAT_SR_TOKEN_TYPE type) |
| 351 |
{ |
| 352 |
static char *str_array[] = { "string", "eor", "eof" }; |
| 353 |
|
| 354 |
if ((type < 0) || (2 < type)) { |
| 355 |
return "error: invalid token type"; |
| 356 |
} |
| 357 |
|
| 358 |
return str_array[type]; |
| 359 |
} |
| 360 |
|
| 361 |
/*********************************************************************** |
| 362 |
* Function: dat_sr_parse_eof |
| 363 |
***********************************************************************/ |
| 364 |
|
| 365 |
DAT_RETURN dat_sr_parse_eof(DAT_OS_FILE * file) |
| 366 |
{ |
| 367 |
DAT_SR_TOKEN token; |
| 368 |
|
| 369 |
if (DAT_SUCCESS != dat_sr_get_token(file, &token)) { |
| 370 |
return DAT_INTERNAL_ERROR; |
| 371 |
} |
| 372 |
|
| 373 |
if (DAT_SR_TOKEN_EOF == token.type) { |
| 374 |
return DAT_SUCCESS; |
| 375 |
} else { |
| 376 |
dat_sr_put_token(file, &token); |
| 377 |
return DAT_INTERNAL_ERROR; |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
/*********************************************************************** |
| 382 |
* Function: dat_sr_parse_ia_name |
| 383 |
***********************************************************************/ |
| 384 |
|
| 385 |
DAT_RETURN dat_sr_parse_entry(DAT_OS_FILE * file) |
| 386 |
{ |
| 387 |
DAT_SR_CONF_ENTRY entry; |
| 388 |
DAT_RETURN status; |
| 389 |
|
| 390 |
dat_os_memset(&entry, 0, sizeof(DAT_SR_CONF_ENTRY)); |
| 391 |
|
| 392 |
if ((DAT_SUCCESS == dat_sr_parse_ia_name(file, &entry)) && |
| 393 |
(DAT_SUCCESS == dat_sr_parse_api(file, &entry)) && |
| 394 |
(DAT_SUCCESS == dat_sr_parse_thread_safety(file, &entry)) && |
| 395 |
(DAT_SUCCESS == dat_sr_parse_default(file, &entry)) && |
| 396 |
(DAT_SUCCESS == dat_sr_parse_lib_path(file, &entry)) && |
| 397 |
(DAT_SUCCESS == dat_sr_parse_provider_version(file, &entry)) && |
| 398 |
(DAT_SUCCESS == dat_sr_parse_ia_params(file, &entry)) && |
| 399 |
(DAT_SUCCESS == dat_sr_parse_platform_params(file, &entry)) && |
| 400 |
(DAT_SUCCESS == dat_sr_parse_eoe(file, &entry))) { |
| 401 |
dat_os_dbg_print(DAT_OS_DBG_TYPE_SR, |
| 402 |
"\n" |
| 403 |
"DAT Registry: entry \n" |
| 404 |
" ia_name %s\n" |
| 405 |
" api_version\n" |
| 406 |
" type 0x%X\n" |
| 407 |
" major.minor %d.%d\n" |
| 408 |
" is_thread_safe %d\n" |
| 409 |
" is_default %d\n" |
| 410 |
" lib_path %s\n" |
| 411 |
" provider_version\n" |
| 412 |
" id %s\n" |
| 413 |
" major.minor %d.%d\n" |
| 414 |
" ia_params %s\n" |
| 415 |
"\n", |
| 416 |
entry.ia_name, |
| 417 |
entry.api_version.type, |
| 418 |
entry.api_version.version.major, |
| 419 |
entry.api_version.version.minor, |
| 420 |
entry.is_thread_safe, |
| 421 |
entry.is_default, |
| 422 |
entry.lib_path, |
| 423 |
entry.provider_version.id, |
| 424 |
entry.provider_version.version.major, |
| 425 |
entry.provider_version.version.minor, |
| 426 |
entry.ia_params); |
| 427 |
|
| 428 |
if (DAT_TRUE == dat_sr_is_valid_entry(&entry)) { |
| 429 |
/* |
| 430 |
* The static registry configuration file may have multiple |
| 431 |
* entries with the same IA name. The first entry will be |
| 432 |
* installed in the static registry causing subsequent attempts |
| 433 |
* to register the same IA name to fail. Therefore the return code |
| 434 |
* from dat_sr_load_entry() is ignored. |
| 435 |
*/ |
| 436 |
(void)dat_sr_load_entry(&entry); |
| 437 |
} |
| 438 |
|
| 439 |
status = DAT_SUCCESS; |
| 440 |
} else { /* resync */ |
| 441 |
|
| 442 |
DAT_SR_TOKEN token; |
| 443 |
|
| 444 |
/* |
| 445 |
* The static registry format is specified in the DAT specification. |
| 446 |
* While the registry file's contents may change between revisions of |
| 447 |
* the specification, there is no way to determine the specification |
| 448 |
* version to which the configuration file conforms. If an entry is |
| 449 |
* found that does not match the expected format, the entry is discarded |
| 450 |
* and the parsing of the file continues. There is no way to determine if |
| 451 |
* the entry was an error or an entry confirming to an alternate version |
| 452 |
* of specification. |
| 453 |
*/ |
| 454 |
|
| 455 |
for (;;) { |
| 456 |
if (DAT_SUCCESS != dat_sr_get_token(file, &token)) { |
| 457 |
status = DAT_INTERNAL_ERROR; |
| 458 |
break; |
| 459 |
} |
| 460 |
|
| 461 |
if (DAT_SR_TOKEN_STRING != token.type) { |
| 462 |
status = DAT_SUCCESS; |
| 463 |
break; |
| 464 |
} else { |
| 465 |
dat_os_free(token.value, |
| 466 |
(sizeof(char) * |
| 467 |
dat_os_strlen(token.value)) + 1); |
| 468 |
continue; |
| 469 |
} |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
/* free resources */ |
| 474 |
if (NULL != entry.ia_name) { |
| 475 |
dat_os_free(entry.ia_name, |
| 476 |
sizeof(char) * (dat_os_strlen(entry.ia_name) + 1)); |
| 477 |
} |
| 478 |
if (NULL != entry.lib_path) { |
| 479 |
dat_os_free(entry.lib_path, |
| 480 |
sizeof(char) * (dat_os_strlen(entry.lib_path) + 1)); |
| 481 |
} |
| 482 |
|
| 483 |
if (NULL != entry.provider_version.id) { |
| 484 |
dat_os_free(entry.provider_version.id, |
| 485 |
sizeof(char) * |
| 486 |
(dat_os_strlen(entry.provider_version.id) + 1)); |
| 487 |
} |
| 488 |
|
| 489 |
if (NULL != entry.ia_params) { |
| 490 |
dat_os_free(entry.ia_params, |
| 491 |
sizeof(char) * (dat_os_strlen(entry.ia_params) + |
| 492 |
1)); |
| 493 |
} |
| 494 |
|
| 495 |
if (NULL != entry.platform_params) { |
| 496 |
dat_os_free(entry.platform_params, |
| 497 |
sizeof(char) * |
| 498 |
(dat_os_strlen(entry.platform_params) + 1)); |
| 499 |
} |
| 500 |
|
| 501 |
return status; |
| 502 |
} |
| 503 |
|
| 504 |
/*********************************************************************** |
| 505 |
* Function: dat_sr_parse_ia_name |
| 506 |
***********************************************************************/ |
| 507 |
|
| 508 |
DAT_RETURN dat_sr_parse_ia_name(DAT_OS_FILE * file, DAT_SR_CONF_ENTRY * entry) |
| 509 |
{ |
| 510 |
DAT_SR_TOKEN token; |
| 511 |
|
| 512 |
if (DAT_SUCCESS != dat_sr_get_token(file, &token)) |
| 513 |
goto bail; |
| 514 |
|
| 515 |
if (DAT_SR_TOKEN_STRING != token.type) { |
| 516 |
dat_sr_put_token(file, &token); |
| 517 |
goto bail; |
| 518 |
} |
| 519 |
entry->ia_name = token.value; |
| 520 |
return DAT_SUCCESS; |
| 521 |
|
| 522 |
bail: |
| 523 |
return DAT_INTERNAL_ERROR; |
| 524 |
} |
| 525 |
|
| 526 |
/*********************************************************************** |
| 527 |
* Function: dat_sr_parse_ia_name |
| 528 |
***********************************************************************/ |
| 529 |
|
| 530 |
DAT_RETURN dat_sr_parse_api(DAT_OS_FILE * file, DAT_SR_CONF_ENTRY * entry) |
| 531 |
{ |
| 532 |
DAT_SR_TOKEN token; |
| 533 |
|
| 534 |
if (DAT_SUCCESS != dat_sr_get_token(file, &token)) |
| 535 |
goto bail; |
| 536 |
|
| 537 |
if (DAT_SR_TOKEN_STRING != token.type) |
| 538 |
goto cleanup; |
| 539 |
|
| 540 |
if (DAT_SUCCESS != dat_sr_convert_api(token.value, &entry->api_version)) |
| 541 |
goto cleanup; |
| 542 |
|
| 543 |
dat_os_free(token.value, |
| 544 |
(sizeof(char) * dat_os_strlen(token.value)) + 1); |
| 545 |
return DAT_SUCCESS; |
| 546 |
|
| 547 |
cleanup: |
| 548 |
dat_sr_put_token(file, &token); |
| 549 |
bail: |
| 550 |
dat_os_dbg_print(DAT_OS_DBG_TYPE_ERROR, |
| 551 |
" ERR: corrupt dat.conf entry field:" |
| 552 |
" api_ver, file offset=%ld\n", ftell(file)); |
| 553 |
return DAT_INTERNAL_ERROR; |
| 554 |
} |
| 555 |
|
| 556 |
/*********************************************************************** |
| 557 |
* Function: dat_sr_parse_thread_safety |
| 558 |
***********************************************************************/ |
| 559 |
|
| 560 |
static DAT_RETURN |
| 561 |
dat_sr_parse_thread_safety(DAT_OS_FILE * file, DAT_SR_CONF_ENTRY * entry) |
| 562 |
{ |
| 563 |
DAT_SR_TOKEN token; |
| 564 |
|
| 565 |
if (DAT_SUCCESS != dat_sr_get_token(file, &token)) |
| 566 |
goto bail; |
| 567 |
|
| 568 |
if (DAT_SR_TOKEN_STRING != token.type) |
| 569 |
goto cleanup; |
| 570 |
|
| 571 |
if (DAT_SUCCESS != |
| 572 |
dat_sr_convert_thread_safety(token.value, &entry->is_thread_safe)) |
| 573 |
goto cleanup; |
| 574 |
|
| 575 |
dat_os_free(token.value, |
| 576 |
(sizeof(char) * dat_os_strlen(token.value)) + 1); |
| 577 |
return DAT_SUCCESS; |
| 578 |
|
| 579 |
cleanup: |
| 580 |
dat_sr_put_token(file, &token); |
| 581 |
bail: |
| 582 |
dat_os_dbg_print(DAT_OS_DBG_TYPE_ERROR, |
| 583 |
" ERR: corrupt dat.conf entry field:" |
| 584 |
" thread_safety, file offset=%ld\n", ftell(file)); |
| 585 |
return DAT_INTERNAL_ERROR; |
| 586 |
} |
| 587 |
|
| 588 |
/*********************************************************************** |
| 589 |
* Function: dat_sr_parse_default |
| 590 |
***********************************************************************/ |
| 591 |
|
| 592 |
DAT_RETURN dat_sr_parse_default(DAT_OS_FILE * file, DAT_SR_CONF_ENTRY * entry) |
| 593 |
{ |
| 594 |
DAT_SR_TOKEN token; |
| 595 |
|
| 596 |
if (DAT_SUCCESS != dat_sr_get_token(file, &token)) |
| 597 |
goto bail; |
| 598 |
|
| 599 |
if (DAT_SR_TOKEN_STRING != token.type) |
| 600 |
goto cleanup; |
| 601 |
|
| 602 |
if (DAT_SUCCESS != |
| 603 |
dat_sr_convert_default(token.value, &entry->is_default)) |
| 604 |
goto cleanup; |
| 605 |
|
| 606 |
dat_os_free(token.value, |
| 607 |
(sizeof(char) * dat_os_strlen(token.value)) + 1); |
| 608 |
return DAT_SUCCESS; |
| 609 |
|
| 610 |
cleanup: |
| 611 |
dat_sr_put_token(file, &token); |
| 612 |
bail: |
| 613 |
dat_os_dbg_print(DAT_OS_DBG_TYPE_ERROR, |
| 614 |
" ERR: corrupt dat.conf entry field:" |
| 615 |
" default section, file offset=%ld\n", ftell(file)); |
| 616 |
return DAT_INTERNAL_ERROR; |
| 617 |
} |
| 618 |
|
| 619 |
/*********************************************************************** |
| 620 |
* Function: dat_sr_parse_lib_path |
| 621 |
***********************************************************************/ |
| 622 |
|
| 623 |
DAT_RETURN dat_sr_parse_lib_path(DAT_OS_FILE * file, DAT_SR_CONF_ENTRY * entry) |
| 624 |
{ |
| 625 |
DAT_SR_TOKEN token; |
| 626 |
|
| 627 |
if (DAT_SUCCESS != dat_sr_get_token(file, &token)) |
| 628 |
goto bail; |
| 629 |
|
| 630 |
if (DAT_SR_TOKEN_STRING != token.type) { |
| 631 |
dat_sr_put_token(file, &token); |
| 632 |
goto bail; |
| 633 |
} |
| 634 |
entry->lib_path = token.value; |
| 635 |
return DAT_SUCCESS; |
| 636 |
|
| 637 |
bail: |
| 638 |
dat_os_dbg_print(DAT_OS_DBG_TYPE_ERROR, |
| 639 |
" ERR: corrupt dat.conf entry field:" |
| 640 |
" lib_path, file offset=%ld\n", ftell(file)); |
| 641 |
return DAT_INTERNAL_ERROR; |
| 642 |
} |
| 643 |
|
| 644 |
/*********************************************************************** |
| 645 |
* Function: dat_sr_parse_provider_version |
| 646 |
***********************************************************************/ |
| 647 |
|
| 648 |
DAT_RETURN |
| 649 |
dat_sr_parse_provider_version(DAT_OS_FILE * file, DAT_SR_CONF_ENTRY * entry) |
| 650 |
{ |
| 651 |
DAT_SR_TOKEN token; |
| 652 |
|
| 653 |
if (DAT_SUCCESS != dat_sr_get_token(file, &token)) |
| 654 |
goto bail; |
| 655 |
|
| 656 |
if (DAT_SR_TOKEN_STRING != token.type) |
| 657 |
goto cleanup; |
| 658 |
|
| 659 |
if (DAT_SUCCESS != |
| 660 |
dat_sr_convert_provider_version(token.value, |
| 661 |
&entry->provider_version)) |
| 662 |
goto cleanup; |
| 663 |
|
| 664 |
dat_os_free(token.value, |
| 665 |
(sizeof(char) * dat_os_strlen(token.value)) + 1); |
| 666 |
return DAT_SUCCESS; |
| 667 |
|
| 668 |
cleanup: |
| 669 |
dat_sr_put_token(file, &token); |
| 670 |
bail: |
| 671 |
dat_os_dbg_print(DAT_OS_DBG_TYPE_ERROR, |
| 672 |
" ERR: corrupt dat.conf entry field:" |
| 673 |
" provider_ver, file offset=%ld\n", ftell(file)); |
| 674 |
return DAT_INTERNAL_ERROR; |
| 675 |
} |
| 676 |
|
| 677 |
/*********************************************************************** |
| 678 |
* Function: dat_sr_parse_ia_params |
| 679 |
***********************************************************************/ |
| 680 |
|
| 681 |
DAT_RETURN dat_sr_parse_ia_params(DAT_OS_FILE * file, DAT_SR_CONF_ENTRY * entry) |
| 682 |
{ |
| 683 |
DAT_SR_TOKEN token; |
| 684 |
|
| 685 |
if (DAT_SUCCESS != dat_sr_get_token(file, &token)) |
| 686 |
goto bail; |
| 687 |
|
| 688 |
if (DAT_SR_TOKEN_STRING != token.type) { |
| 689 |
dat_sr_put_token(file, &token); |
| 690 |
goto bail; |
| 691 |
} |
| 692 |
|
| 693 |
entry->ia_params = token.value; |
| 694 |
return DAT_SUCCESS; |
| 695 |
|
| 696 |
bail: |
| 697 |
dat_os_dbg_print(DAT_OS_DBG_TYPE_ERROR, |
| 698 |
" ERR: corrupt dat.conf entry field:" |
| 699 |
" ia_params, file offset=%ld\n", ftell(file)); |
| 700 |
return DAT_INTERNAL_ERROR; |
| 701 |
} |
| 702 |
|
| 703 |
/*********************************************************************** |
| 704 |
* Function: dat_sr_parse_platform_params |
| 705 |
***********************************************************************/ |
| 706 |
|
| 707 |
DAT_RETURN |
| 708 |
dat_sr_parse_platform_params(DAT_OS_FILE * file, DAT_SR_CONF_ENTRY * entry) |
| 709 |
{ |
| 710 |
DAT_SR_TOKEN token; |
| 711 |
|
| 712 |
if (DAT_SUCCESS != dat_sr_get_token(file, &token)) |
| 713 |
goto bail; |
| 714 |
|
| 715 |
if (DAT_SR_TOKEN_STRING != token.type) { |
| 716 |
dat_sr_put_token(file, &token); |
| 717 |
goto bail; |
| 718 |
} |
| 719 |
|
| 720 |
entry->platform_params = token.value; |
| 721 |
return DAT_SUCCESS; |
| 722 |
|
| 723 |
bail: |
| 724 |
dat_os_dbg_print(DAT_OS_DBG_TYPE_ERROR, |
| 725 |
" ERR: corrupt dat.conf entry field:" |
| 726 |
" platform_params, file offset=%ld\n", ftell(file)); |
| 727 |
return DAT_INTERNAL_ERROR; |
| 728 |
} |
| 729 |
|
| 730 |
/*********************************************************************** |
| 731 |
* Function: dat_sr_parse_eoe |
| 732 |
***********************************************************************/ |
| 733 |
|
| 734 |
DAT_RETURN dat_sr_parse_eoe(DAT_OS_FILE * file, DAT_SR_CONF_ENTRY * entry) |
| 735 |
{ |
| 736 |
DAT_SR_TOKEN token; |
| 737 |
|
| 738 |
if (DAT_SUCCESS != dat_sr_get_token(file, &token)) |
| 739 |
goto bail; |
| 740 |
|
| 741 |
if ((DAT_SR_TOKEN_EOF != token.type) && |
| 742 |
(DAT_SR_TOKEN_EOR != token.type)) { |
| 743 |
dat_sr_put_token(file, &token); |
| 744 |
goto bail; |
| 745 |
} |
| 746 |
|
| 747 |
return DAT_SUCCESS; |
| 748 |
|
| 749 |
bail: |
| 750 |
dat_os_dbg_print(DAT_OS_DBG_TYPE_ERROR, |
| 751 |
" ERR: corrupt dat.conf entry field:" |
| 752 |
" EOR, EOF, file offset=%ld\n", ftell(file)); |
| 753 |
return DAT_INTERNAL_ERROR; |
| 754 |
} |
| 755 |
|
| 756 |
/*********************************************************************** |
| 757 |
* Function: dat_sr_convert_api |
| 758 |
***********************************************************************/ |
| 759 |
|
| 760 |
DAT_RETURN dat_sr_convert_api(char *str, DAT_SR_API_VERSION * api_version) |
| 761 |
{ |
| 762 |
int i; |
| 763 |
int minor_i; |
| 764 |
|
| 765 |
if (dat_os_strlen(str) <= 0) |
| 766 |
return DAT_INTERNAL_ERROR; |
| 767 |
|
| 768 |
if ('u' == str[0]) { |
| 769 |
api_version->type = DAT_SR_API_UDAT; |
| 770 |
} else if ('k' == str[0]) { |
| 771 |
api_version->type = DAT_SR_API_KDAT; |
| 772 |
} else { |
| 773 |
return DAT_INTERNAL_ERROR; |
| 774 |
} |
| 775 |
|
| 776 |
for (i = 1 /* move past initial [u|k] */ ; '\0' != str[i]; i++) { |
| 777 |
if ('.' == str[i]) { |
| 778 |
break; |
| 779 |
} else if (DAT_TRUE != dat_os_isdigit(str[i])) { |
| 780 |
return DAT_INTERNAL_ERROR; |
| 781 |
} |
| 782 |
} |
| 783 |
|
| 784 |
api_version->version.major = |
| 785 |
(DAT_UINT32) dat_os_strtol(str + 1, NULL, 10); |
| 786 |
|
| 787 |
/* move past '.' */ |
| 788 |
minor_i = ++i; |
| 789 |
|
| 790 |
for (; '\0' != str[i]; i++) { |
| 791 |
if (DAT_TRUE != dat_os_isdigit(str[i])) { |
| 792 |
return DAT_INTERNAL_ERROR; |
| 793 |
} |
| 794 |
} |
| 795 |
|
| 796 |
api_version->version.minor = |
| 797 |
(DAT_UINT32) dat_os_strtol(str + minor_i, NULL, 10); |
| 798 |
|
| 799 |
if ('\0' != str[i]) { |
| 800 |
return DAT_INTERNAL_ERROR; |
| 801 |
} |
| 802 |
|
| 803 |
return DAT_SUCCESS; |
| 804 |
} |
| 805 |
|
| 806 |
/*********************************************************************** |
| 807 |
* Function: dat_sr_convert_thread_safety |
| 808 |
***********************************************************************/ |
| 809 |
|
| 810 |
static DAT_RETURN |
| 811 |
dat_sr_convert_thread_safety(char *str, DAT_BOOLEAN * is_thread_safe) |
| 812 |
{ |
| 813 |
if (!dat_os_strncmp(str, |
| 814 |
DAT_SR_TOKEN_THREADSAFE, |
| 815 |
dat_os_strlen(DAT_SR_TOKEN_THREADSAFE))) { |
| 816 |
*is_thread_safe = DAT_TRUE; |
| 817 |
return DAT_SUCCESS; |
| 818 |
} else if (!dat_os_strncmp(str, |
| 819 |
DAT_SR_TOKEN_NONTHREADSAFE, |
| 820 |
dat_os_strlen(DAT_SR_TOKEN_NONTHREADSAFE))) { |
| 821 |
*is_thread_safe = DAT_FALSE; |
| 822 |
return DAT_SUCCESS; |
| 823 |
} else { |
| 824 |
return DAT_INTERNAL_ERROR; |
| 825 |
} |
| 826 |
} |
| 827 |
|
| 828 |
/*********************************************************************** |
| 829 |
* Function: dat_sr_convert_default |
| 830 |
***********************************************************************/ |
| 831 |
|
| 832 |
static DAT_RETURN dat_sr_convert_default(char *str, DAT_BOOLEAN * is_default) |
| 833 |
{ |
| 834 |
if (!dat_os_strncmp(str, |
| 835 |
DAT_SR_TOKEN_DEFAULT, |
| 836 |
dat_os_strlen(DAT_SR_TOKEN_DEFAULT))) { |
| 837 |
*is_default = DAT_TRUE; |
| 838 |
return DAT_SUCCESS; |
| 839 |
} else if (!dat_os_strncmp(str, |
| 840 |
DAT_SR_TOKEN_NONDEFAULT, |
| 841 |
dat_os_strlen(DAT_SR_TOKEN_NONDEFAULT))) { |
| 842 |
*is_default = DAT_FALSE; |
| 843 |
return DAT_SUCCESS; |
| 844 |
} else { |
| 845 |
return DAT_INTERNAL_ERROR; |
| 846 |
} |
| 847 |
} |
| 848 |
|
| 849 |
/*********************************************************************** |
| 850 |
* Function: dat_sr_convert_provider_version |
| 851 |
***********************************************************************/ |
| 852 |
|
| 853 |
DAT_RETURN |
| 854 |
dat_sr_convert_provider_version(char *str, |
| 855 |
DAT_SR_PROVIDER_VERSION * provider_version) |
| 856 |
{ |
| 857 |
DAT_RETURN status; |
| 858 |
int i; |
| 859 |
int decimal_i; |
| 860 |
|
| 861 |
if ((dat_os_strlen(str) <= 0) || (NULL != provider_version->id)) |
| 862 |
return DAT_INTERNAL_ERROR; |
| 863 |
|
| 864 |
status = DAT_SUCCESS; |
| 865 |
|
| 866 |
for (i = 0; '\0' != str[i]; i++) { |
| 867 |
if ('.' == str[i]) { |
| 868 |
break; |
| 869 |
} |
| 870 |
} |
| 871 |
|
| 872 |
/* if no id value was found */ |
| 873 |
if (0 == i) { |
| 874 |
status = DAT_INTERNAL_ERROR; |
| 875 |
goto exit; |
| 876 |
} |
| 877 |
|
| 878 |
if (NULL == |
| 879 |
(provider_version->id = dat_os_alloc(sizeof(char) * (i + 1)))) { |
| 880 |
status = |
| 881 |
DAT_ERROR(DAT_INSUFFICIENT_RESOURCES, DAT_RESOURCE_MEMORY); |
| 882 |
goto exit; |
| 883 |
} |
| 884 |
|
| 885 |
dat_os_strncpy(provider_version->id, str, i); |
| 886 |
provider_version->id[i] = '\0'; |
| 887 |
|
| 888 |
/* move past '.' */ |
| 889 |
decimal_i = ++i; |
| 890 |
|
| 891 |
for (; '\0' != str[i]; i++) { |
| 892 |
if ('.' == str[i]) { |
| 893 |
break; |
| 894 |
} else if (DAT_TRUE != dat_os_isdigit(str[i])) { |
| 895 |
status = DAT_INTERNAL_ERROR; |
| 896 |
goto exit; |
| 897 |
} |
| 898 |
} |
| 899 |
|
| 900 |
/* if no version value was found */ |
| 901 |
if (decimal_i == i) { |
| 902 |
status = DAT_INTERNAL_ERROR; |
| 903 |
goto exit; |
| 904 |
} |
| 905 |
|
| 906 |
provider_version->version.major = (DAT_UINT32) |
| 907 |
dat_os_strtol(str + decimal_i, NULL, 10); |
| 908 |
|
| 909 |
/* move past '.' */ |
| 910 |
decimal_i = ++i; |
| 911 |
|
| 912 |
for (; '\0' != str[i]; i++) { |
| 913 |
if (DAT_TRUE != dat_os_isdigit(str[i])) { |
| 914 |
status = DAT_INTERNAL_ERROR; |
| 915 |
goto exit; |
| 916 |
} |
| 917 |
} |
| 918 |
|
| 919 |
/* if no version value was found */ |
| 920 |
if (decimal_i == i) { |
| 921 |
status = DAT_INTERNAL_ERROR; |
| 922 |
goto exit; |
| 923 |
} |
| 924 |
|
| 925 |
provider_version->version.minor = (DAT_UINT32) |
| 926 |
dat_os_strtol(str + decimal_i, NULL, 10); |
| 927 |
|
| 928 |
if ('\0' != str[i]) { |
| 929 |
status = DAT_INTERNAL_ERROR; |
| 930 |
goto exit; |
| 931 |
} |
| 932 |
|
| 933 |
exit: |
| 934 |
if (DAT_SUCCESS != status) { |
| 935 |
if (NULL != provider_version->id) { |
| 936 |
dat_os_free(provider_version->id, |
| 937 |
sizeof(char) * |
| 938 |
(dat_os_strlen(provider_version->id) + 1)); |
| 939 |
provider_version->id = NULL; |
| 940 |
} |
| 941 |
} |
| 942 |
|
| 943 |
return status; |
| 944 |
} |
| 945 |
|
| 946 |
/*********************************************************************** |
| 947 |
* Function: dat_sr_get_token |
| 948 |
***********************************************************************/ |
| 949 |
|
| 950 |
DAT_RETURN dat_sr_get_token(DAT_OS_FILE * file, DAT_SR_TOKEN * token) |
| 951 |
{ |
| 952 |
if (NULL == g_token_stack) { |
| 953 |
return dat_sr_read_token(file, token); |
| 954 |
} else { |
| 955 |
DAT_SR_STACK_NODE *top; |
| 956 |
|
| 957 |
top = g_token_stack; |
| 958 |
|
| 959 |
*token = top->token; |
| 960 |
g_token_stack = top->next; |
| 961 |
|
| 962 |
dat_os_free(top, sizeof(DAT_SR_STACK_NODE)); |
| 963 |
|
| 964 |
return DAT_SUCCESS; |
| 965 |
} |
| 966 |
} |
| 967 |
|
| 968 |
/*********************************************************************** |
| 969 |
* Function: dat_sr_put_token |
| 970 |
***********************************************************************/ |
| 971 |
|
| 972 |
DAT_RETURN dat_sr_put_token(DAT_OS_FILE * file, DAT_SR_TOKEN * token) |
| 973 |
{ |
| 974 |
DAT_SR_STACK_NODE *top; |
| 975 |
|
| 976 |
if (NULL == (top = dat_os_alloc(sizeof(DAT_SR_STACK_NODE)))) { |
| 977 |
return DAT_INSUFFICIENT_RESOURCES | DAT_RESOURCE_MEMORY; |
| 978 |
} |
| 979 |
|
| 980 |
top->token = *token; |
| 981 |
top->next = g_token_stack; |
| 982 |
g_token_stack = top; |
| 983 |
|
| 984 |
return DAT_SUCCESS; |
| 985 |
} |
| 986 |
|
| 987 |
/*********************************************************************** |
| 988 |
* Function: dat_sr_read_token |
| 989 |
***********************************************************************/ |
| 990 |
|
| 991 |
DAT_RETURN dat_sr_read_token(DAT_OS_FILE * file, DAT_SR_TOKEN * token) |
| 992 |
{ |
| 993 |
DAT_OS_FILE_POS pos; |
| 994 |
DAT_OS_SIZE token_len; |
| 995 |
DAT_COUNT num_escape_seq; |
| 996 |
DAT_BOOLEAN is_quoted_str; |
| 997 |
DAT_BOOLEAN is_prev_char_backslash; |
| 998 |
|
| 999 |
/* |
| 1000 |
* The DAT standard does not specify a maximum size for quoted strings. |
| 1001 |
* Therefore the tokenizer must be able to read in a token of arbitrary |
| 1002 |
* size. Instead of allocating a fixed length buffer, the tokenizer first |
| 1003 |
* scans the input a single character at a time looking for the begining |
| 1004 |
* and end of the token. Once the these positions are found, the entire |
| 1005 |
* token is read into memory. By using this algorithm,the implementation |
| 1006 |
* does not place an arbitrary maximum on the token size. |
| 1007 |
*/ |
| 1008 |
|
| 1009 |
token_len = 0; |
| 1010 |
num_escape_seq = 0; |
| 1011 |
is_quoted_str = DAT_FALSE; |
| 1012 |
is_prev_char_backslash = DAT_FALSE; |
| 1013 |
|
| 1014 |
for (;;) { |
| 1015 |
DAT_OS_FILE_POS cur_pos; |
| 1016 |
int c; |
| 1017 |
|
| 1018 |
/* if looking for start of the token */ |
| 1019 |
if (0 == token_len) { |
| 1020 |
if (DAT_SUCCESS != dat_os_fgetpos(file, &cur_pos)) { |
| 1021 |
return DAT_INTERNAL_ERROR; |
| 1022 |
} |
| 1023 |
} |
| 1024 |
|
| 1025 |
c = dat_os_fgetc(file); |
| 1026 |
|
| 1027 |
/* if looking for start of the token */ |
| 1028 |
if (0 == token_len) { |
| 1029 |
if (EOF == c) { |
| 1030 |
token->type = DAT_SR_TOKEN_EOF; |
| 1031 |
token->value = NULL; |
| 1032 |
token->value_len = 0; |
| 1033 |
goto success; |
| 1034 |
} else if (DAT_SR_CHAR_NEWLINE == c) { |
| 1035 |
token->type = DAT_SR_TOKEN_EOR; |
| 1036 |
token->value = NULL; |
| 1037 |
token->value_len = 0; |
| 1038 |
goto success; |
| 1039 |
} else if (dat_os_isblank(c)) { |
| 1040 |
continue; |
| 1041 |
} else if (DAT_SR_CHAR_COMMENT == c) { |
| 1042 |
dat_sr_read_comment(file); |
| 1043 |
continue; |
| 1044 |
} else { |
| 1045 |
if (DAT_SR_CHAR_QUOTE == c) { |
| 1046 |
is_quoted_str = DAT_TRUE; |
| 1047 |
} |
| 1048 |
|
| 1049 |
pos = cur_pos; |
| 1050 |
token_len++; |
| 1051 |
} |
| 1052 |
} else { /* looking for the end of the token */ |
| 1053 |
|
| 1054 |
if (EOF == c) { |
| 1055 |
break; |
| 1056 |
} else if (DAT_SR_CHAR_NEWLINE == c) { |
| 1057 |
/* put back the newline */ |
| 1058 |
dat_os_fputc(file, c); |
| 1059 |
break; |
| 1060 |
} else if (!is_quoted_str && dat_os_isblank(c)) { |
| 1061 |
break; |
| 1062 |
} else { |
| 1063 |
token_len++; |
| 1064 |
|
| 1065 |
if ((DAT_SR_CHAR_QUOTE == c) |
| 1066 |
&& !is_prev_char_backslash) { |
| 1067 |
break; |
| 1068 |
} else if ((DAT_SR_CHAR_BACKSLASH == c) |
| 1069 |
&& !is_prev_char_backslash) { |
| 1070 |
is_prev_char_backslash = DAT_TRUE; |
| 1071 |
num_escape_seq++; |
| 1072 |
} else { |
| 1073 |
is_prev_char_backslash = DAT_FALSE; |
| 1074 |
} |
| 1075 |
} |
| 1076 |
} |
| 1077 |
} |
| 1078 |
|
| 1079 |
/* the token was a string */ |
| 1080 |
if (DAT_SUCCESS != dat_os_fsetpos(file, &pos)) { |
| 1081 |
return DAT_INTERNAL_ERROR; |
| 1082 |
} |
| 1083 |
|
| 1084 |
if (is_quoted_str) { |
| 1085 |
if (DAT_SUCCESS != dat_sr_read_quoted_str(file, |
| 1086 |
token, |
| 1087 |
token_len, |
| 1088 |
num_escape_seq)) { |
| 1089 |
return DAT_INTERNAL_ERROR; |
| 1090 |
} |
| 1091 |
} else { |
| 1092 |
if (DAT_SUCCESS != dat_sr_read_str(file, token, token_len)) { |
| 1093 |
return DAT_INTERNAL_ERROR; |
| 1094 |
} |
| 1095 |
} |
| 1096 |
|
| 1097 |
success: |
| 1098 |
dat_os_dbg_print(DAT_OS_DBG_TYPE_SR, |
| 1099 |
"\n" |
| 1100 |
"DAT Registry: token\n" |
| 1101 |
" type %s\n" |
| 1102 |
" value <%s>\n" |
| 1103 |
"\n", |
| 1104 |
dat_sr_type_to_str(token->type), |
| 1105 |
((DAT_SR_TOKEN_STRING == |
| 1106 |
token->type) ? token->value : "")); |
| 1107 |
|
| 1108 |
return DAT_SUCCESS; |
| 1109 |
} |
| 1110 |
|
| 1111 |
/*********************************************************************** |
| 1112 |
* Function: dat_sr_read_str |
| 1113 |
***********************************************************************/ |
| 1114 |
|
| 1115 |
DAT_RETURN |
| 1116 |
dat_sr_read_str(DAT_OS_FILE * file, DAT_SR_TOKEN * token, DAT_OS_SIZE token_len) |
| 1117 |
{ |
| 1118 |
token->type = DAT_SR_TOKEN_STRING; |
| 1119 |
token->value_len = sizeof(char) * (token_len + 1); /* +1 for null termination */ |
| 1120 |
if (NULL == (token->value = dat_os_alloc(token->value_len))) { |
| 1121 |
return DAT_INSUFFICIENT_RESOURCES | DAT_RESOURCE_MEMORY; |
| 1122 |
} |
| 1123 |
|
| 1124 |
if (token_len != dat_os_fread(file, token->value, token_len)) { |
| 1125 |
dat_os_free(token->value, token->value_len); |
| 1126 |
token->value = NULL; |
| 1127 |
|
| 1128 |
return DAT_INTERNAL_ERROR; |
| 1129 |
} |
| 1130 |
|
| 1131 |
token->value[token->value_len - 1] = '\0'; |
| 1132 |
|
| 1133 |
return DAT_SUCCESS; |
| 1134 |
} |
| 1135 |
|
| 1136 |
/*********************************************************************** |
| 1137 |
* Function: dat_sr_read_quoted_str |
| 1138 |
***********************************************************************/ |
| 1139 |
|
| 1140 |
DAT_RETURN |
| 1141 |
dat_sr_read_quoted_str(DAT_OS_FILE * file, |
| 1142 |
DAT_SR_TOKEN * token, |
| 1143 |
DAT_OS_SIZE token_len, DAT_COUNT num_escape_seq) |
| 1144 |
{ |
| 1145 |
DAT_OS_SIZE str_len; |
| 1146 |
DAT_OS_SIZE i; |
| 1147 |
DAT_OS_SIZE j; |
| 1148 |
int c; |
| 1149 |
DAT_RETURN status; |
| 1150 |
DAT_BOOLEAN is_prev_char_backslash; |
| 1151 |
|
| 1152 |
str_len = token_len - 2; /* minus 2 " characters */ |
| 1153 |
is_prev_char_backslash = DAT_FALSE; |
| 1154 |
status = DAT_SUCCESS; |
| 1155 |
|
| 1156 |
token->type = DAT_SR_TOKEN_STRING; |
| 1157 |
/* +1 for null termination */ |
| 1158 |
token->value_len = sizeof(char) * (str_len - num_escape_seq + 1); |
| 1159 |
|
| 1160 |
if (NULL == (token->value = dat_os_alloc(token->value_len))) { |
| 1161 |
status = DAT_INSUFFICIENT_RESOURCES | DAT_RESOURCE_MEMORY; |
| 1162 |
goto exit; |
| 1163 |
} |
| 1164 |
|
| 1165 |
/* throw away " */ |
| 1166 |
if (DAT_SR_CHAR_QUOTE != dat_os_fgetc(file)) { |
| 1167 |
status = DAT_INTERNAL_ERROR; |
| 1168 |
goto exit; |
| 1169 |
} |
| 1170 |
|
| 1171 |
for (i = 0, j = 0; i < str_len; i++) { |
| 1172 |
c = dat_os_fgetc(file); |
| 1173 |
|
| 1174 |
if (EOF == c) { |
| 1175 |
status = DAT_INTERNAL_ERROR; |
| 1176 |
goto exit; |
| 1177 |
} else if ((DAT_SR_CHAR_BACKSLASH == c) |
| 1178 |
&& !is_prev_char_backslash) { |
| 1179 |
is_prev_char_backslash = DAT_TRUE; |
| 1180 |
} else { |
| 1181 |
token->value[j] = (char)c; |
| 1182 |
j++; |
| 1183 |
|
| 1184 |
is_prev_char_backslash = DAT_FALSE; |
| 1185 |
} |
| 1186 |
} |
| 1187 |
|
| 1188 |
/* throw away " */ |
| 1189 |
if (DAT_SR_CHAR_QUOTE != dat_os_fgetc(file)) { |
| 1190 |
status = DAT_INTERNAL_ERROR; |
| 1191 |
goto exit; |
| 1192 |
} |
| 1193 |
|
| 1194 |
token->value[token->value_len - 1] = '\0'; |
| 1195 |
|
| 1196 |
exit: |
| 1197 |
if (DAT_SUCCESS != status) { |
| 1198 |
if (NULL != token->value) { |
| 1199 |
dat_os_free(token->value, token->value_len); |
| 1200 |
token->value = NULL; |
| 1201 |
} |
| 1202 |
} |
| 1203 |
|
| 1204 |
return status; |
| 1205 |
} |
| 1206 |
|
| 1207 |
/*********************************************************************** |
| 1208 |
* Function: dat_sr_read_comment |
| 1209 |
***********************************************************************/ |
| 1210 |
|
| 1211 |
void dat_sr_read_comment(DAT_OS_FILE * file) |
| 1212 |
{ |
| 1213 |
int c; |
| 1214 |
|
| 1215 |
/* read up to an EOR or EOF to move past the comment */ |
| 1216 |
do { |
| 1217 |
c = dat_os_fgetc(file); |
| 1218 |
} while ((DAT_SR_CHAR_NEWLINE != c) && (EOF != c)); |
| 1219 |
|
| 1220 |
/* put back the newline */ |
| 1221 |
dat_os_ungetc(file, c); |
| 1222 |
} |