| 1 | // **************************************************************************** |
| 2 | // Project: libguytools |
| 3 | // **************************************************************************** |
| 4 | // Programmer: Guy Voncken |
| 5 | // Police Grand-Ducale |
| 6 | // Service de Police Judiciaire |
| 7 | // Section Nouvelles Technologies |
| 8 | // **************************************************************************** |
| 9 | // Module: Configuration utility |
| 10 | // **************************************************************************** |
| 11 | |
| 12 | // Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, |
| 13 | // 2018, 2019 |
| 14 | // Guy Voncken |
| 15 | // |
| 16 | // This file is part of libguytools. |
| 17 | // |
| 18 | // libguytools is free software: you can redistribute it and/or modify |
| 19 | // it under the terms of the GNU General Public License as published by |
| 20 | // the Free Software Foundation, either version 2 of the License, or |
| 21 | // (at your option) any later version. |
| 22 | // |
| 23 | // libguytools is distributed in the hope that it will be useful, |
| 24 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 25 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 26 | // GNU General Public License for more details. |
| 27 | // |
| 28 | // You should have received a copy of the GNU General Public License |
| 29 | // along with libguytools. If not, see <http://www.gnu.org/licenses/>. |
| 30 | |
| 31 | /* ------------------------------ */ |
| 32 | /* Includes */ |
| 33 | /* ------------------------------ */ |
| 34 | |
| 35 | #include <stdio.h> |
| 36 | #include <stdlib.h> |
| 37 | #include <stdarg.h> |
| 38 | #include <limits.h> |
| 39 | #include <string.h> |
| 40 | #include <time.h> |
| 41 | #include <locale.h> |
| 42 | |
| 43 | #include <sys/types.h> |
| 44 | #include <sys/stat.h> |
| 45 | #include <unistd.h> |
| 46 | |
| 47 | #include "tooltypes.h" |
| 48 | #include "toolglobalid.h" |
| 49 | #include "toolconstants.h" |
| 50 | #include "toolerror.h" |
| 51 | #include "toolcfg.h" |
| 52 | |
| 53 | /* ------------------------------ */ |
| 54 | /* Constants */ |
| 55 | /* ------------------------------ */ |
| 56 | |
| 57 | const t_char CHAR_LF = 0x0A; |
| 58 | const t_char CHAR_CR = 0x0D; |
| 59 | const t_char CHAR_EOF = 0x1A; |
| 60 | |
| 61 | const t_char CHAR_APPEND_LINE = '\\'; |
| 62 | const t_char STRING_DELIMITER = '\''; |
| 63 | |
| 64 | const t_int MAX_CFG_NESTING_LEVEL = 6; /* maximum number of nested INCLUDE directives */ |
| 65 | |
| 66 | /* reserved keywords */ |
| 67 | |
| 68 | static t_pcchar KEYWORD_TABLESTART = "TABLE"; |
| 69 | static t_pcchar KEYWORD_TABLEEND = "ENDTABLE"; |
| 70 | static t_pcchar KEYWORD_SECTIONSTART = "SECTION"; |
| 71 | static t_pcchar KEYWORD_SECTIONEND = "ENDSECTION"; |
| 72 | static t_pcchar KEYWORD_INCLUDE = "INCLUDE"; |
| 73 | static t_pcchar KEYWORD_INCLUDE_OPT = "INCLUDE_OPTIONAL"; |
| 74 | static t_pcchar KEYWORD_DEFINE = "DEFINE"; |
| 75 | static t_pcchar KEYWORD_UNDEFINE = "UNDEFINE"; |
| 76 | static t_pcchar KEYWORD_REMARK = "REM"; |
| 77 | static t_char KEYCHAR_REMARK = '#'; |
| 78 | |
| 79 | /* ------------ */ |
| 80 | /* Memory IDs */ |
| 81 | /* ------------ */ |
| 82 | |
| 83 | static const t_int MemIdCfgBuffer = MEMID_BASE_TOOL_CFG + 0; |
| 84 | static const t_int MemIdCfgErrorHeader = MEMID_BASE_TOOL_CFG + 1; |
| 85 | static const t_int MemIdCfgContextStack = MEMID_BASE_TOOL_CFG + 2; |
| 86 | static const t_int MemIdCfgTemplateHeader = MEMID_BASE_TOOL_CFG + 3; |
| 87 | static const t_int MemIdCfgHelpBuff = MEMID_BASE_TOOL_CFG + 4; |
| 88 | static const t_int MemIdCfgHelpString = MEMID_BASE_TOOL_CFG + 5; |
| 89 | static const t_int MemIdCfgHelpInt = MEMID_BASE_TOOL_CFG + 6; |
| 90 | static const t_int MemIdCfgHelpTable = MEMID_BASE_TOOL_CFG + 7; |
| 91 | static const t_int MemIdCfgTableType = MEMID_BASE_TOOL_CFG + 8; |
| 92 | static const t_int MemIdCfgTableName = MEMID_BASE_TOOL_CFG + 9; |
| 93 | static const t_int MemIdCfgLogConfiguration = MEMID_BASE_TOOL_CFG + 10; |
| 94 | |
| 95 | /* ------------------------------ */ |
| 96 | /* Type & structure definitions */ |
| 97 | /* ------------------------------ */ |
| 98 | |
| 99 | typedef enum |
| 100 | { /*! \brief Command line parameter priority */ |
| 101 | PRIORITY_HIGH, /*!< Command line parameters have high priority */ |
| 102 | PRIORITY_LOW /*!< Command line parameters have low priority */ |
| 103 | } t_Priority; |
| 104 | |
| 105 | typedef enum /*! \brief States for CFG parser state machine */ |
| 106 | { |
| 107 | CFGSTATE_INCLUDENEWFILE, |
| 108 | CFGSTATE_INCLUDENEWFILE_OPT, |
| 109 | CFGSTATE_GETNEWLINE, |
| 110 | CFGSTATE_CHECKINCLUDE, |
| 111 | CFGSTATE_CHECKSECTION, |
| 112 | CFGSTATE_LINEREAD, |
| 113 | CFGSTATE_COMPLETED, |
| 114 | CFGSTATE_CHECKEOF, |
| 115 | CFGSTATE_SEARCHSECTIONEND, |
| 116 | CFGSTATE_CHECKSECTIONEND |
| 117 | } t_ToolCfgState; |
| 118 | |
| 119 | |
| 120 | #define MAX_SECTIONNAME_LEN 63 |
| 121 | #define MAX_FILENAME_LEN 4095 |
| 122 | #define MAX_SECTIONNAMES 16 |
| 123 | #define MAX_GLOBALSECTIONNAMES 16 |
| 124 | |
| 125 | typedef char t_ToolCfgSectionName[MAX_SECTIONNAME_LEN+1]; |
| 126 | |
| 127 | typedef struct |
| 128 | { |
| 129 | t_int BufferLen; |
| 130 | t_int ActLineLen; |
| 131 | t_int ActLineNr; |
| 132 | t_pchar pActLine; |
| 133 | t_pchar pBuffer; |
| 134 | t_char FileName [MAX_FILENAME_LEN +1]; |
| 135 | t_int SectionNesting; |
| 136 | t_ToolCfgSectionName SectionNameArr[MAX_SECTIONNAMES]; // These section names are only valid during the the scanning of one include file |
| 137 | t_ToolCfgState State; |
| 138 | } t_ToolCfgContext, *t_pToolCfgContext; |
| 139 | |
| 140 | |
| 141 | typedef struct |
| 142 | { |
| 143 | int argc; |
| 144 | char **argv; |
| 145 | |
| 146 | t_Priority Priority; |
| 147 | t_pToolCfgUserLogFn pUserLogFn; |
| 148 | t_pToolCfgContext pCfgContextStack; |
| 149 | t_pToolCfgContext pActCfgContext; |
| 150 | t_int IncludeNestingLevel; |
| 151 | t_int MaxIncludeNestingLevel; |
| 152 | t_char TempFileName[MAX_FILENAME_LEN+1]; |
| 153 | t_ToolCfgSectionName GlobalSectionNameArr[MAX_GLOBALSECTIONNAMES]; // These section names remain defined during the whole cfg sanning time |
| 154 | } t_ToolCfgLocal; |
| 155 | |
| 156 | |
| 157 | #define max(a,b) (((a) > (b)) ? (a) : (b)) |
| 158 | #define min(a,b) (((a) < (b)) ? (a) : (b)) |
| 159 | |
| 160 | /* ------------------------------ */ |
| 161 | /* Variables */ |
| 162 | /* ------------------------------ */ |
| 163 | |
| 164 | static t_ToolCfgLocal ToolCfgLocal; |
| 165 | |
| 166 | |
| 167 | /* ------------------------------ */ |
| 168 | /* Functions */ |
| 169 | /* ------------------------------ */ |
| 170 | |
| 171 | /* -------------------------------------- */ |
| 172 | /* Memory access with / without ToolMem */ |
| 173 | /* -------------------------------------- */ |
| 174 | |
| 175 | #ifdef TOCFG_COMPILE_FOR_USE_WITHOUT_TOOLBOX |
| 176 | #define MEM_OPT_NONE 0 |
| 177 | #define MEM_REGISTER_MEMID(MemId) NO_ERROR |
| 178 | #endif |
| 179 | |
| 180 | static APIRET ToolCfgMemAlloc (t_pvoid *ppMem, t_uint Size, t_uint Options, t_int MemId) |
| 181 | { |
| 182 | APIRET rc; |
| 183 | |
| 184 | #ifdef TOCFG_COMPILE_FOR_USE_WITHOUT_TOOLBOX |
| 185 | rc = Options; // To prevent from compiler warning "unused..." |
| 186 | rc = MemId; // To prevent from compiler warning "unused..." |
| 187 | *ppMem = malloc (Size); |
| 188 | if (*ppMem == nullptr) |
| 189 | rc = TOOLCFG_ERROR_MALLOC_FAILED; |
| 190 | else rc = NO_ERROR; |
| 191 | #else |
| 192 | rc = ToolMemAlloc (ppMem, Size, Options, MemId); |
| 193 | #endif |
| 194 | |
| 195 | return rc; |
| 196 | } |
| 197 | |
| 198 | static APIRET ToolCfgMemFree (t_pvoid pMem, t_int MemId) |
| 199 | { |
| 200 | APIRET rc; |
| 201 | |
| 202 | #ifdef TOCFG_COMPILE_FOR_USE_WITHOUT_TOOLBOX |
| 203 | rc = MemId; // To prevent from compiler warning "unused..." |
| 204 | free (pMem); |
| 205 | rc = NO_ERROR; |
| 206 | #else |
| 207 | rc = ToolMemFree (pMem, MemId); |
| 208 | #endif |
| 209 | |
| 210 | return rc; |
| 211 | } |
| 212 | |
| 213 | /* -------------------- */ |
| 214 | /* Error management */ |
| 215 | /* -------------------- */ |
| 216 | |
| 217 | /* Error return values in this module are simply raised to the calling function. */ |
| 218 | /* This way, the module CFG does not depend on any log management. */ |
| 219 | |
| 220 | #define CFG_CHK_APP(Fn) \ |
| 221 | { \ |
| 222 | APIRET ec; \ |
| 223 | \ |
| 224 | if ((ec=Fn) != NO_ERROR) \ |
| 225 | { \ |
| 226 | if (ec != TOOLCFG_ERROR_CONFIG_ERROR) \ |
| 227 | (void) ToolCfgLogEntry(__FFL__, "Error %d.", ec); \ |
| 228 | return ec; \ |
| 229 | } \ |
| 230 | } |
| 231 | |
| 232 | #define CFG_CHK_FPRINTF(Fn) \ |
| 233 | { \ |
| 234 | if ((Fn) < 1) \ |
| 235 | return TOOLCFG_ERROR_FPRINTF_FAILED; \ |
| 236 | } |
| 237 | |
| 238 | static APIRET ToolCfgLogEntry (t_pcchar pFileName, t_pcchar pFunctionName, t_int LineNr, t_pcchar pFormat, ...) |
| 239 | { |
| 240 | // APIRET rc=1; |
| 241 | va_list VaList; |
| 242 | |
| 243 | va_start(VaList, pFormat); /* VaList points to the argument following pFormat */ |
| 244 | if (ToolCfgLocal.pUserLogFn) |
| 245 | (*ToolCfgLocal.pUserLogFn)(pFileName, pFunctionName, LineNr, pFormat, VaList); |
| 246 | // if (rc) |
| 247 | // (void) vfprintf (stderr, pFormat, VaList); |
| 248 | va_end(VaList); |
| 249 | |
| 250 | return NO_ERROR; |
| 251 | } |
| 252 | |
| 253 | static APIRET ToolCfgStdErrorHeader (t_pcchar pActCursor) |
| 254 | { |
| 255 | t_pToolCfgContext pCfgContext; |
| 256 | t_pchar pEndOfLine; |
| 257 | t_pchar pBuff; |
| 258 | t_int Cursor; |
| 259 | t_int LineLen; |
| 260 | |
| 261 | pCfgContext = ToolCfgLocal.pActCfgContext; |
| 262 | if (pCfgContext->FileName[0] == '\0') |
| 263 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "Configuration error in command line. Parameter %d", pCfgContext->ActLineNr)) |
| 264 | else CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "Configuration error in file %s line %d" , &pCfgContext->FileName[0], pCfgContext->ActLineNr)) |
| 265 | if (pCfgContext->pActLine) |
| 266 | { |
| 267 | pEndOfLine = pCfgContext->pActLine + pCfgContext->ActLineLen; |
| 268 | *pEndOfLine = '\0'; |
| 269 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "[%s]", pCfgContext->pActLine)) |
| 270 | } |
| 271 | if (pActCursor && pCfgContext->pActLine) |
| 272 | { |
| 273 | LineLen = (t_int) strlen (pCfgContext->pActLine); |
| 274 | Cursor = pActCursor - pCfgContext->pActLine; |
| 275 | Cursor = max (Cursor, 0 ); |
| 276 | Cursor = min (Cursor, LineLen-1); |
| 277 | |
| 278 | CFG_CHK_APP (ToolCfgMemAlloc ((void **)&pBuff, (t_uint)(LineLen+3), MEM_OPT_NONE, MemIdCfgErrorHeader)) /* +3 for [ ] and '\0' */ |
| 279 | (void) sprintf (pBuff, "[%*s^%*s]", Cursor, "", LineLen-Cursor-1, ""); |
| 280 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, pBuff)) |
| 281 | CFG_CHK_APP (ToolCfgMemFree (pBuff, MemIdCfgErrorHeader)) |
| 282 | } |
| 283 | |
| 284 | return NO_ERROR; |
| 285 | } |
| 286 | |
| 287 | /* ------------------------------------- */ |
| 288 | /* Utility functions */ |
| 289 | /* ------------------------------------- */ |
| 290 | |
| 291 | static t_int ToolCfgStrCmpNoCase (const char *pStr1, const char *pStr2) |
| 292 | { |
| 293 | if ((pStr1 == nullptr) || (pStr2 == nullptr)) |
| 294 | return 1; |
| 295 | |
| 296 | #ifdef _WIN32 |
| 297 | return (t_int) strcmpi (pStr1, pStr2); |
| 298 | #else |
| 299 | return (t_int) strcasecmp (pStr1, pStr2); |
| 300 | #endif |
| 301 | } |
| 302 | |
| 303 | static t_int ToolCfgStrNCmpNoCase (const char *pStr1, const char *pStr2, t_int Len) |
| 304 | { |
| 305 | if ((pStr1 == nullptr) || (pStr2 == nullptr)) |
| 306 | return 1; |
| 307 | |
| 308 | #ifdef _WIN32 |
| 309 | return strnicmp (pStr1, pStr2, Len); |
| 310 | #else |
| 311 | return strncasecmp (pStr1, pStr2, (size_t)Len); |
| 312 | #endif |
| 313 | } |
| 314 | |
| 315 | static t_pchar ToolCfgStrMaxCpy (t_pchar pDest, t_pcchar pSrc, t_int DestLen) |
| 316 | { |
| 317 | if (DestLen == 0) |
| 318 | return pDest; |
| 319 | pDest[DestLen-1] = '\0'; |
| 320 | return strncpy (pDest, pSrc, DestLen-1); |
| 321 | } |
| 322 | |
| 323 | static APIRET ToolCfgCopyName (t_pcchar pSrc, t_pchar pDst, t_int DstLen) |
| 324 | { |
| 325 | t_int SrcLen; |
| 326 | |
| 327 | if (pSrc == nullptr) |
| 328 | pDst[0] = '\0'; |
| 329 | else |
| 330 | { |
| 331 | SrcLen = (t_int)strlen(pSrc); |
| 332 | if (SrcLen > DstLen) |
| 333 | { |
| 334 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "SrcLen: %d DstLen: %d", SrcLen, DstLen)) |
| 335 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "SrcString: %50.50s", pSrc)) |
| 336 | |
| 337 | return TOOLCFG_ERROR_NAME_TOO_LONG; |
| 338 | } |
| 339 | else |
| 340 | (void) strcpy (pDst, pSrc); |
| 341 | } |
| 342 | |
| 343 | return NO_ERROR; |
| 344 | } |
| 345 | |
| 346 | static APIRET ToolCfgFindFirstWord (t_pcchar pActLine, const char **ppFirstWord, t_int LineLen, t_pint pRemLineLen) |
| 347 | { |
| 348 | for ( *ppFirstWord = pActLine; |
| 349 | ((**ppFirstWord == ' ') || (**ppFirstWord == '\t')) && (LineLen > 0); |
| 350 | ( *ppFirstWord)++, LineLen--) |
| 351 | { |
| 352 | } |
| 353 | if ((LineLen <= 0) || (**ppFirstWord == CHAR_LF) || (**ppFirstWord == CHAR_CR)) |
| 354 | *ppFirstWord = nullptr; |
| 355 | |
| 356 | *pRemLineLen = LineLen; |
| 357 | |
| 358 | return NO_ERROR; |
| 359 | } |
| 360 | |
| 361 | static APIRET ToolCfgCompareKeyWord (t_pcchar pTableKeyWord, t_pchar pCheckKeyWord, t_int LineLen, t_pint pEq, t_pchar *ppNextChar) |
| 362 | { |
| 363 | t_pchar pNextChar; |
| 364 | t_int TableKeyLen; |
| 365 | t_int CheckKeyLen; |
| 366 | t_pchar pCh; |
| 367 | |
| 368 | pNextChar = nullptr; |
| 369 | *pEq = FALSE; |
| 370 | |
| 371 | TableKeyLen = (t_int) strlen (pTableKeyWord); |
| 372 | if (pCheckKeyWord && (LineLen >= TableKeyLen)) |
| 373 | { |
| 374 | CheckKeyLen = 0; |
| 375 | for (pCh = pCheckKeyWord; |
| 376 | (*pCh != ' ') && (*pCh != CHAR_CR) && |
| 377 | (*pCh != '=') && (*pCh != CHAR_LF) && (*pCh != '\t') && (*pCh != '\0'); |
| 378 | pCh++) |
| 379 | CheckKeyLen++; |
| 380 | |
| 381 | if (CheckKeyLen == TableKeyLen) |
| 382 | { |
| 383 | if (ToolCfgStrNCmpNoCase (pTableKeyWord, pCheckKeyWord, CheckKeyLen) == 0) |
| 384 | { |
| 385 | pNextChar = &pCheckKeyWord[CheckKeyLen]; |
| 386 | *pEq = TRUE; |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | if (ppNextChar) |
| 392 | *ppNextChar = pNextChar; |
| 393 | |
| 394 | return NO_ERROR; |
| 395 | } |
| 396 | |
| 397 | static APIRET ToolCfgGetParamLen (t_pcchar pParam, t_pint pParamLen) |
| 398 | { |
| 399 | t_int LoopEnd; |
| 400 | |
| 401 | /* count param len */ |
| 402 | *pParamLen = 0; |
| 403 | |
| 404 | LoopEnd = FALSE; |
| 405 | if (*pParam == STRING_DELIMITER) |
| 406 | { |
| 407 | pParam++; |
| 408 | (*pParamLen)++; |
| 409 | while (!LoopEnd) |
| 410 | { |
| 411 | if (*pParam == '\0') |
| 412 | LoopEnd = TRUE; |
| 413 | else if (*pParam == STRING_DELIMITER) |
| 414 | { |
| 415 | if (*(pParam+1) == STRING_DELIMITER) |
| 416 | { |
| 417 | pParam++; |
| 418 | (*pParamLen)++; |
| 419 | } |
| 420 | else |
| 421 | { |
| 422 | LoopEnd = TRUE; |
| 423 | } |
| 424 | } |
| 425 | if (!LoopEnd) |
| 426 | { |
| 427 | pParam++; |
| 428 | (*pParamLen)++; |
| 429 | } |
| 430 | } |
| 431 | } |
| 432 | else |
| 433 | { |
| 434 | while ((*pParam != ' ') && (*pParam != '\t') && (*pParam != '\0') && (*pParam != CHAR_CR) && (*pParam != CHAR_LF)) |
| 435 | { |
| 436 | pParam++; |
| 437 | (*pParamLen)++; |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | if (*pParam == STRING_DELIMITER) /* is there a string end delimiter? */ |
| 442 | { |
| 443 | pParam++; |
| 444 | (*pParamLen)++; |
| 445 | } |
| 446 | |
| 447 | /* was there a param? */ |
| 448 | // if ((*pParam == '\0') && (*pParamLen == 0)) |
| 449 | if (*pParamLen == 0) |
| 450 | { |
| 451 | CFG_CHK_APP (ToolCfgStdErrorHeader (pParam-1)) |
| 452 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "Parameter expected.")) |
| 453 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 454 | } |
| 455 | |
| 456 | return NO_ERROR; |
| 457 | } |
| 458 | |
| 459 | static APIRET ToolCfgCheckIfOnlyOneParam (t_pchar pParam, t_pint pParamLen) |
| 460 | { |
| 461 | CFG_CHK_APP (ToolCfgGetParamLen (pParam, pParamLen)) |
| 462 | pParam += *pParamLen; |
| 463 | |
| 464 | /* skip trailing spaces */ |
| 465 | while (((*pParam == ' ') || (*pParam == '\t')) && (*pParam != '\0')) |
| 466 | pParam++; |
| 467 | |
| 468 | /* is there a second param? */ |
| 469 | if (*pParam != '\0') |
| 470 | { |
| 471 | CFG_CHK_APP (ToolCfgStdErrorHeader (pParam)) |
| 472 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "No further parameter expected.")) |
| 473 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 474 | } |
| 475 | |
| 476 | return NO_ERROR; |
| 477 | } |
| 478 | |
| 479 | static APIRET ToolCfgCheckIfNoParam (t_pchar pCh) |
| 480 | { |
| 481 | while (((*pCh == ' ') || (*pCh == '\t')) && (*pCh != '\0')) |
| 482 | pCh++; |
| 483 | |
| 484 | if (*pCh != '\0') |
| 485 | { |
| 486 | CFG_CHK_APP (ToolCfgStdErrorHeader (pCh)) |
| 487 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "No further parameter expected.")) |
| 488 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 489 | } |
| 490 | |
| 491 | return NO_ERROR; |
| 492 | } |
| 493 | |
| 494 | /* ----------------------------------------------------------------------------------- */ |
| 495 | /* Help text builder. It is used to help the user enter the correct parameter values */ |
| 496 | /* ----------------------------------------------------------------------------------- */ |
| 497 | |
| 498 | static APIRET ToolCfgChkStrCpy (t_pchar pDstStr, t_pcchar pSrcStr, t_pint pCumulLen) |
| 499 | { |
| 500 | if (pDstStr) |
| 501 | (void) strcpy (pDstStr + *pCumulLen, pSrcStr); |
| 502 | (*pCumulLen) += (t_int)strlen (pSrcStr); |
| 503 | |
| 504 | return NO_ERROR; |
| 505 | } |
| 506 | |
| 507 | static APIRET ToolCfgBuildHelpHMS(t_pToolCfgDataDesc pCfgDataDesc, t_pchar pHelpBuff, t_pint pHelpBuffLen) |
| 508 | { |
| 509 | t_pchar pTmp; |
| 510 | t_int MinVal; |
| 511 | t_int MaxVal; |
| 512 | |
| 513 | MinVal = (t_int)pCfgDataDesc->MinValue; |
| 514 | MaxVal = (t_int)pCfgDataDesc->MaxValue; |
| 515 | CFG_CHK_APP (ToolCfgMemAlloc ((void **)&pTmp, 256, MEM_OPT_NONE, MemIdCfgHelpInt)) |
| 516 | (void) sprintf (pTmp, "[%02d:%02d:%02d..%02d:%02d:%02d]", MinVal/3600, (MinVal/60)%60, MinVal%60, |
| 517 | MaxVal/3600, (MaxVal/60)%60, MaxVal%60); |
| 518 | CFG_CHK_APP (ToolCfgChkStrCpy (pHelpBuff, pTmp, pHelpBuffLen)) |
| 519 | CFG_CHK_APP (ToolCfgMemFree (pTmp, MemIdCfgHelpInt)) |
| 520 | |
| 521 | return NO_ERROR; |
| 522 | } |
| 523 | |
| 524 | static APIRET ToolCfgBuildHelpPresence (t_pToolCfgDataDesc /*pCfgDataDesc*/, t_pchar pHelpBuff, t_pint pHelpBuffLen) |
| 525 | { |
| 526 | t_pcchar pTmp; |
| 527 | |
| 528 | pTmp = "Optional"; |
| 529 | CFG_CHK_APP (ToolCfgChkStrCpy (pHelpBuff, pTmp, pHelpBuffLen)) |
| 530 | |
| 531 | return NO_ERROR; |
| 532 | } |
| 533 | |
| 534 | static APIRET ToolCfgBuildHelpInteger (t_pToolCfgDataDesc pCfgDataDesc, t_pchar pHelpBuff, t_pint pHelpBuffLen) |
| 535 | { |
| 536 | t_pchar pTmp; |
| 537 | |
| 538 | CFG_CHK_APP (ToolCfgMemAlloc ((void **)&pTmp, 256, MEM_OPT_NONE, MemIdCfgHelpInt)) |
| 539 | (void) sprintf (pTmp, "[%d..%d] | [0x%X..0x%X]", |
| 540 | (t_int)pCfgDataDesc->MinValue, (t_int)pCfgDataDesc->MaxValue, |
| 541 | (t_int)pCfgDataDesc->MinValue, (t_int)pCfgDataDesc->MaxValue); |
| 542 | CFG_CHK_APP (ToolCfgChkStrCpy (pHelpBuff, pTmp, pHelpBuffLen)) |
| 543 | CFG_CHK_APP (ToolCfgMemFree (pTmp, MemIdCfgHelpInt)) |
| 544 | |
| 545 | return NO_ERROR; |
| 546 | } |
| 547 | |
| 548 | static APIRET ToolCfgBuildHelpDouble (t_pToolCfgDataDesc pCfgDataDesc, t_pchar pHelpBuff, t_pint pHelpBuffLen) |
| 549 | { |
| 550 | t_pchar pTmp; |
| 551 | |
| 552 | CFG_CHK_APP (ToolCfgMemAlloc ((void **)&pTmp, 256, MEM_OPT_NONE, MemIdCfgHelpInt)) |
| 553 | (void) sprintf (pTmp, "[%G..%G]", pCfgDataDesc->MinValue, pCfgDataDesc->MaxValue); |
| 554 | CFG_CHK_APP (ToolCfgChkStrCpy (pHelpBuff, pTmp, pHelpBuffLen)) |
| 555 | CFG_CHK_APP (ToolCfgMemFree (pTmp, MemIdCfgHelpInt)) |
| 556 | |
| 557 | return NO_ERROR; |
| 558 | } |
| 559 | |
| 560 | static APIRET ToolCfgBuildHelpString (t_pToolCfgDataDesc pCfgDataDesc, t_pchar pHelpBuff, t_pint pHelpBuffLen) |
| 561 | { |
| 562 | t_pchar pTmp; |
| 563 | |
| 564 | CFG_CHK_APP (ToolCfgMemAlloc ((void **)&pTmp, 256, MEM_OPT_NONE, MemIdCfgHelpString)) |
| 565 | (void) sprintf (pTmp, "'<MaxStringLength=%d>'", (t_int)pCfgDataDesc->DestLen); |
| 566 | CFG_CHK_APP (ToolCfgChkStrCpy (pHelpBuff, pTmp, pHelpBuffLen)) |
| 567 | CFG_CHK_APP (ToolCfgMemFree (pTmp, MemIdCfgHelpString)) |
| 568 | |
| 569 | return NO_ERROR; |
| 570 | } |
| 571 | |
| 572 | static APIRET ToolCfgBuildHelpSet (t_pToolCfgDataDesc pCfgDataDesc, t_pchar pHelpBuff, t_pint pHelpBuffLen) |
| 573 | { |
| 574 | t_pToolCfgSet pSetArray; |
| 575 | t_int i; |
| 576 | |
| 577 | pSetArray = pCfgDataDesc->pSetArray; |
| 578 | CFG_CHK_APP (ToolCfgChkStrCpy (pHelpBuff, "[", pHelpBuffLen)) |
| 579 | |
| 580 | for (i=0; ; i++) |
| 581 | { |
| 582 | if (pSetArray[i].pSetString == nullptr) /* end of array? */ |
| 583 | break; |
| 584 | CFG_CHK_APP (ToolCfgChkStrCpy (pHelpBuff, pSetArray[i].pSetString, pHelpBuffLen)) |
| 585 | if (pSetArray[i+1].pSetString) |
| 586 | CFG_CHK_APP (ToolCfgChkStrCpy (pHelpBuff, ", ", pHelpBuffLen)) |
| 587 | } |
| 588 | CFG_CHK_APP (ToolCfgChkStrCpy (pHelpBuff, "]", pHelpBuffLen)) |
| 589 | |
| 590 | return NO_ERROR; |
| 591 | } |
| 592 | |
| 593 | static APIRET ToolCfgBuildHelpRange (t_pToolCfgDataDesc pCfgDataDesc, t_pchar pHelpBuff, t_pint pHelpBuffLen) |
| 594 | { |
| 595 | switch (pCfgDataDesc->CfgType) |
| 596 | { |
| 597 | case CFGTYPE_PRESENCE: CFG_CHK_APP (ToolCfgBuildHelpPresence (pCfgDataDesc, pHelpBuff, pHelpBuffLen)) break; |
| 598 | case CFGTYPE_INTEGER: CFG_CHK_APP (ToolCfgBuildHelpInteger (pCfgDataDesc, pHelpBuff, pHelpBuffLen)) break; |
| 599 | case CFGTYPE_DOUBLE: CFG_CHK_APP (ToolCfgBuildHelpDouble (pCfgDataDesc, pHelpBuff, pHelpBuffLen)) break; |
| 600 | case CFGTYPE_STRING: CFG_CHK_APP (ToolCfgBuildHelpString (pCfgDataDesc, pHelpBuff, pHelpBuffLen)) break; |
| 601 | case CFGTYPE_SET: CFG_CHK_APP (ToolCfgBuildHelpSet (pCfgDataDesc, pHelpBuff, pHelpBuffLen)) break; |
| 602 | case CFGTYPE_HMS: CFG_CHK_APP (ToolCfgBuildHelpHMS (pCfgDataDesc, pHelpBuff, pHelpBuffLen)) break; |
| 603 | case CFGTYPE_NULL: break; |
| 604 | default: |
| 605 | return TOOLCFG_ERROR_INVALID_CFGTYPE; |
| 606 | } |
| 607 | |
| 608 | return NO_ERROR; |
| 609 | } |
| 610 | |
| 611 | static APIRET ToolCfgBuildHelp (t_pToolCfgDataDesc pCfgDataDesc, t_pchar pHelpBuff, t_pint pHelpBuffLen) |
| 612 | { |
| 613 | *pHelpBuffLen = 0; |
| 614 | CFG_CHK_APP (ToolCfgChkStrCpy (pHelpBuff, pCfgDataDesc->pName, pHelpBuffLen)) |
| 615 | CFG_CHK_APP (ToolCfgChkStrCpy (pHelpBuff, " = " , pHelpBuffLen)) |
| 616 | CFG_CHK_APP (ToolCfgBuildHelpRange (pCfgDataDesc, pHelpBuff, pHelpBuffLen)) |
| 617 | (*pHelpBuffLen)++; |
| 618 | |
| 619 | return NO_ERROR; |
| 620 | } |
| 621 | |
| 622 | static APIRET ToolCfgBuildTableHelp (t_pToolCfgTableDesc pCfgTableDesc, |
| 623 | t_pchar pHelpBuff1, t_pchar pHelpBuff2, t_pint pHelpBuffLen) |
| 624 | { |
| 625 | t_pToolCfgDataDesc pCfgDataDesc; |
| 626 | t_pchar pTmp; |
| 627 | t_int i; |
| 628 | t_int NameLen; |
| 629 | t_int RangeLen; |
| 630 | t_int Spaces; |
| 631 | t_int HelpBuffLenDummy; |
| 632 | t_int PrevLen; |
| 633 | |
| 634 | *pHelpBuffLen = 0; |
| 635 | HelpBuffLenDummy = 0; |
| 636 | CFG_CHK_APP (ToolCfgMemAlloc ((void **)&pTmp, 4096, MEM_OPT_NONE, MemIdCfgHelpTable)) |
| 637 | |
| 638 | for (i=0;;i++) |
| 639 | { |
| 640 | pCfgDataDesc = &pCfgTableDesc->pDataDescArray[i]; |
| 641 | if (pCfgDataDesc->pName == nullptr) |
| 642 | break; |
| 643 | NameLen = (t_int)strlen (pCfgDataDesc->pName); |
| 644 | CFG_CHK_APP (ToolCfgChkStrCpy (pHelpBuff1, pCfgDataDesc->pName, pHelpBuffLen)) |
| 645 | PrevLen = HelpBuffLenDummy; |
| 646 | CFG_CHK_APP (ToolCfgBuildHelpRange (pCfgDataDesc, pHelpBuff2, &HelpBuffLenDummy)) |
| 647 | RangeLen = HelpBuffLenDummy - PrevLen; |
| 648 | if (RangeLen != NameLen) |
| 649 | { |
| 650 | Spaces = abs (RangeLen - NameLen); |
| 651 | memset (pTmp, ' ', Spaces); |
| 652 | pTmp[Spaces] = '\0'; |
| 653 | if (RangeLen > NameLen) |
| 654 | CFG_CHK_APP (ToolCfgChkStrCpy (pHelpBuff1, pTmp, pHelpBuffLen )) |
| 655 | else CFG_CHK_APP (ToolCfgChkStrCpy (pHelpBuff2, pTmp, &HelpBuffLenDummy)) |
| 656 | } |
| 657 | CFG_CHK_APP (ToolCfgChkStrCpy (pHelpBuff1, " ", pHelpBuffLen )) |
| 658 | CFG_CHK_APP (ToolCfgChkStrCpy (pHelpBuff2, " ", &HelpBuffLenDummy)) |
| 659 | } |
| 660 | |
| 661 | CFG_CHK_APP (ToolCfgMemFree (pTmp, MemIdCfgHelpTable)) |
| 662 | (*pHelpBuffLen)++; |
| 663 | |
| 664 | return NO_ERROR; |
| 665 | } |
| 666 | |
| 667 | static APIRET ToolCfgQueryFileInfo (t_pchar pFileName, t_pint pYear, t_pint pMonth , t_pint pDay, |
| 668 | t_pint pHour, t_pint pMinute, t_pint pSize) |
| 669 | { |
| 670 | // OS/2 code |
| 671 | // --------- |
| 672 | // FILEFINDBUF3 FindBuff; |
| 673 | // HDIR hDir; |
| 674 | // ULONG Count; |
| 675 | // |
| 676 | // hDir = HDIR_SYSTEM; |
| 677 | // Count = 1; // Search for only 1 entry |
| 678 | // CFG_CHK_APP(DosFindFirst(pFileName, &hDir, FILE_NORMAL, &FindBuff, sizeof(FILEFINDBUF3), &Count, FIL_STANDARD)) |
| 679 | // CFG_CHK_APP(DosFindClose(hDir)) |
| 680 | // |
| 681 | // if (pYear ) *pYear = FindBuff.fdateLastWrite.year + OS2DOS_YEAROFFSET; |
| 682 | // if (pMonth ) *pMonth = FindBuff.fdateLastWrite.month; |
| 683 | // if (pDay ) *pDay = FindBuff.fdateLastWrite.day; |
| 684 | // if (pHour ) *pHour = FindBuff.ftimeLastWrite.hours; |
| 685 | // if (pMinute) *pMinute = FindBuff.ftimeLastWrite.minutes; |
| 686 | // if (pSize ) *pSize = (t_int)FindBuff.cbFile; |
| 687 | |
| 688 | // Linux code |
| 689 | // ---------- |
| 690 | struct stat FileInfo; |
| 691 | struct tm* LastModified = nullptr; |
| 692 | |
| 693 | memset (&FileInfo, 0, sizeof(FileInfo)); |
| 694 | |
| 695 | // Get file statistics |
| 696 | if (((stat (pFileName, &FileInfo)) == -1) || (!(FileInfo.st_mode & S_IFREG))) |
| 697 | { |
| 698 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "Could not stat configuration file %s: file not found", pFileName)) |
| 699 | return TOOLCFG_ERROR_FILE_NOT_FOUND; |
| 700 | } |
| 701 | |
| 702 | LastModified = localtime (&(FileInfo.st_mtime)); |
| 703 | if (pYear ) *pYear = LastModified->tm_year + 1900; |
| 704 | if (pMonth ) *pMonth = LastModified->tm_mon + 1; |
| 705 | if (pDay ) *pDay = LastModified->tm_mday; |
| 706 | if (pHour ) *pHour = LastModified->tm_hour; |
| 707 | if (pMinute) *pMinute = LastModified->tm_min; |
| 708 | if (pSize ) *pSize = FileInfo.st_size; |
| 709 | |
| 710 | return NO_ERROR; |
| 711 | } |
| 712 | |
| 713 | static APIRET ToolCfgReadCmdLine (t_pToolCfgContext pCfgContext) |
| 714 | { |
| 715 | t_pchar pTmp; |
| 716 | t_int i; |
| 717 | t_uint Alloc; |
| 718 | t_int SpaceSplit; |
| 719 | |
| 720 | /* Find size */ |
| 721 | pCfgContext->BufferLen = 0; |
| 722 | for (i=1; i<ToolCfgLocal.argc; i++) |
| 723 | { |
| 724 | pCfgContext->BufferLen += (t_int) strlen (ToolCfgLocal.argv[i]) + 1; /* +1 for CR */ |
| 725 | } |
| 726 | pCfgContext->BufferLen++; /* ++ for ending '\0' */ |
| 727 | |
| 728 | /* Alloc memory */ |
| 729 | Alloc = pCfgContext->BufferLen; |
| 730 | if (Alloc == sizeof(t_pvoid)) |
| 731 | Alloc++; |
| 732 | CFG_CHK_APP (ToolCfgMemAlloc((void **)&pCfgContext->pBuffer, Alloc, MEM_OPT_NONE, MemIdCfgBuffer)) |
| 733 | |
| 734 | // Read command line into buffer, seperate arguments by a spaces |
| 735 | pTmp = pCfgContext->pBuffer; |
| 736 | for (i=1; i<ToolCfgLocal.argc; i++) |
| 737 | { |
| 738 | (void) strcpy (pTmp, ToolCfgLocal.argv[i]); |
| 739 | pTmp += strlen (ToolCfgLocal.argv[i]); |
| 740 | *pTmp++ = ' '; |
| 741 | } |
| 742 | *pTmp = '\0'; |
| 743 | |
| 744 | // Split arguments into lines. Attention: Do |
| 745 | // not split string containing spaces! |
| 746 | SpaceSplit=TRUE; |
| 747 | for ( pTmp = pCfgContext->pBuffer; |
| 748 | *pTmp != '\0'; |
| 749 | pTmp++) |
| 750 | { |
| 751 | if (*pTmp == STRING_DELIMITER) |
| 752 | SpaceSplit = !SpaceSplit; |
| 753 | if (SpaceSplit && (*pTmp == ' ')) |
| 754 | *pTmp = CHAR_CR; |
| 755 | } |
| 756 | |
| 757 | // pTmp = pCfgContext->pBuffer; |
| 758 | // for (i=1; i<ToolCfgLocal.argc; i++) |
| 759 | // { |
| 760 | // (void) strcpy (pTmp, ToolCfgLocal.argv[i]); |
| 761 | // pTmp += strlen (ToolCfgLocal.argv[i]); |
| 762 | // *pTmp++ = CHAR_CR; |
| 763 | // } |
| 764 | // *pTmp = '\0'; |
| 765 | |
| 766 | /* Set initial context */ |
| 767 | pCfgContext->ActLineLen = 0; |
| 768 | pCfgContext->ActLineNr = 1; |
| 769 | pCfgContext->pActLine = pCfgContext->pBuffer; |
| 770 | |
| 771 | return NO_ERROR; |
| 772 | } |
| 773 | |
| 774 | static APIRET ToolCfgReadCfgFile (t_pToolCfgContext pCfgContext) |
| 775 | { |
| 776 | FILE *pCfgFile; |
| 777 | t_int SizeRead; |
| 778 | t_int rc; |
| 779 | t_int Year, Month , Day; |
| 780 | t_int Hour, Minute; |
| 781 | t_int Size; |
| 782 | |
| 783 | /* Expand filename (only possible when using the toolbox) */ |
| 784 | #ifndef TOCFG_COMPILE_FOR_USE_WITHOUT_TOOLBOX |
| 785 | (void) strcpy (&ToolCfgLocal.TempFileName[0], &pCfgContext->FileName[0]); |
| 786 | CFG_CHK_APP (ToolEnvExpand (&ToolCfgLocal.TempFileName[0], &pCfgContext->FileName[0], MAX_FILENAME_LEN)) |
| 787 | #endif |
| 788 | |
| 789 | /* Find size */ |
| 790 | pCfgFile = fopen (&pCfgContext->FileName[0], "rb"); |
| 791 | if (pCfgFile == nullptr) |
| 792 | { |
| 793 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "Could not open configuration file %s", &pCfgContext->FileName[0])) |
| 794 | return TOOLCFG_ERROR_OPEN_FAILED; |
| 795 | } |
| 796 | rc = setvbuf (pCfgFile, nullptr, _IOFBF, 32768); |
| 797 | if (rc) |
| 798 | return TOOLCFG_ERROR_SETBUF_FAILED; |
| 799 | |
| 800 | rc = fseek (pCfgFile, 0, SEEK_END); |
| 801 | if (rc) |
| 802 | return TOOLCFG_ERROR_SEEKEND_FAILED; |
| 803 | pCfgContext->BufferLen = (t_int) ftell (pCfgFile) + 1; /* +1 for ending '\0' */ |
| 804 | |
| 805 | /* Alloc memory */ |
| 806 | CFG_CHK_APP (ToolCfgMemAlloc((void **)&pCfgContext->pBuffer, (t_uint)pCfgContext->BufferLen, MEM_OPT_NONE, MemIdCfgBuffer)) |
| 807 | |
| 808 | /* Read command line into buffer */ |
| 809 | rc = fseek (pCfgFile, 0, SEEK_SET); |
| 810 | if (rc) |
| 811 | return TOOLCFG_ERROR_SEEKSET_FAILED; |
| 812 | SizeRead = (t_int) fread (pCfgContext->pBuffer, 1, (size_t)pCfgContext->BufferLen, pCfgFile); |
| 813 | if (SizeRead > pCfgContext->BufferLen) |
| 814 | return TOOLCFG_ERROR_READ_FAILED; |
| 815 | rc = fclose (pCfgFile); |
| 816 | if (rc) |
| 817 | return TOOLCFG_ERROR_CLOSE_FAILED; |
| 818 | pCfgContext->pBuffer[SizeRead] = '\0'; |
| 819 | |
| 820 | CFG_CHK_APP (ToolCfgQueryFileInfo (&pCfgContext->FileName[0], &Year, &Month, &Day, &Hour, &Minute, &Size)) |
| 821 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "Scanning configuration file %s (%d bytes %02d.%02d.%d %02d:%02d)", |
| 822 | &pCfgContext->FileName[0], Size, Day, Month, Year, Hour, Minute)) |
| 823 | /* Set initial context */ |
| 824 | pCfgContext->ActLineLen = 0; |
| 825 | pCfgContext->ActLineNr = 1; |
| 826 | pCfgContext->pActLine = pCfgContext->pBuffer; |
| 827 | |
| 828 | return NO_ERROR; |
| 829 | } |
| 830 | |
| 831 | static APIRET ToolCfgGetLineLen (t_pToolCfgContext pCfgContext) |
| 832 | { |
| 833 | t_pchar pEol; |
| 834 | t_pchar pLastBackslash; |
| 835 | t_int CopyLen; |
| 836 | t_int KeepRunning; |
| 837 | |
| 838 | pLastBackslash = nullptr; |
| 839 | KeepRunning = TRUE; |
| 840 | |
| 841 | pEol = pCfgContext->pActLine; |
| 842 | |
| 843 | while (KeepRunning) |
| 844 | { |
| 845 | switch (*pEol) |
| 846 | { |
| 847 | case CHAR_APPEND_LINE: |
| 848 | pLastBackslash = pEol++; |
| 849 | break; |
| 850 | case ' ' : |
| 851 | case '\t' : pEol++; break; |
| 852 | case CHAR_CR: |
| 853 | case CHAR_LF: if (pLastBackslash) |
| 854 | { |
| 855 | if (((*pEol == CHAR_CR) && (pEol[1] == CHAR_LF)) || |
| 856 | ((*pEol == CHAR_LF) && (pEol[1] == CHAR_CR))) |
| 857 | pEol++; |
| 858 | pEol++; |
| 859 | |
| 860 | CopyLen = pCfgContext->pBuffer + pCfgContext->BufferLen - pEol; |
| 861 | memmove (pLastBackslash, pEol, CopyLen); |
| 862 | pEol = pLastBackslash; |
| 863 | pLastBackslash = nullptr; |
| 864 | pCfgContext->ActLineNr++; |
| 865 | } |
| 866 | else |
| 867 | KeepRunning = FALSE; |
| 868 | break; |
| 869 | case '\0' : KeepRunning = FALSE; |
| 870 | break; |
| 871 | default : pLastBackslash = nullptr; |
| 872 | pEol++; |
| 873 | } |
| 874 | } |
| 875 | pCfgContext->ActLineLen = pEol - pCfgContext->pActLine; |
| 876 | |
| 877 | return NO_ERROR; |
| 878 | } |
| 879 | |
| 880 | static APIRET ToolCfgSearchLine (t_pToolCfgContext pCfgContext) |
| 881 | { |
| 882 | t_pchar pTmp; |
| 883 | |
| 884 | pTmp = pCfgContext->pActLine + pCfgContext->ActLineLen; |
| 885 | if (pTmp[0] != '\0') |
| 886 | { |
| 887 | if (pTmp[1] != '\0') |
| 888 | { |
| 889 | if (((pTmp[0] == CHAR_LF) && (pTmp[1] == CHAR_CR)) || |
| 890 | ((pTmp[1] == CHAR_LF) && (pTmp[0] == CHAR_CR))) |
| 891 | pTmp += 2; |
| 892 | else pTmp += 1; |
| 893 | } |
| 894 | else pTmp += 1; |
| 895 | } |
| 896 | pCfgContext->ActLineNr++; |
| 897 | pCfgContext->pActLine = pTmp; |
| 898 | CFG_CHK_APP (ToolCfgGetLineLen (pCfgContext)) |
| 899 | |
| 900 | return NO_ERROR; |
| 901 | } |
| 902 | |
| 903 | /* CfgNoFurtherParamsExpected reports an error if further */ |
| 904 | /* parameters are found between pStart and the end of line. */ |
| 905 | static APIRET ToolCfgNoFurtherParamsExpected (t_pcchar pStart, t_pcchar pCorrectSyntaxText) |
| 906 | { |
| 907 | while ((*pStart == ' ') || (*pStart == '\t')) |
| 908 | pStart++; |
| 909 | if ((*pStart != CHAR_CR) && |
| 910 | (*pStart != CHAR_LF) && |
| 911 | (*pStart != '\0')) |
| 912 | { |
| 913 | CFG_CHK_APP (ToolCfgStdErrorHeader (pStart)) |
| 914 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "No further parameter expected.")) |
| 915 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, pCorrectSyntaxText)) |
| 916 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 917 | } |
| 918 | |
| 919 | return NO_ERROR; |
| 920 | } |
| 921 | |
| 922 | //static t_pchar ToolCfgFindEndOfString (t_pchar pString) |
| 923 | //{ |
| 924 | // t_pchar pTmp; |
| 925 | // |
| 926 | // for ( pTmp = pString; |
| 927 | // (*pTmp != CHAR_CR) && (*pTmp != ' ') && |
| 928 | // (*pTmp != CHAR_LF) && (*pTmp != '\t') && (*pTmp != '\0'); |
| 929 | // pTmp++) |
| 930 | // { |
| 931 | // } |
| 932 | // return pTmp-1; |
| 933 | //} |
| 934 | |
| 935 | static t_int ToolCfgScanningCmdLine (void) |
| 936 | { |
| 937 | if (ToolCfgLocal.pActCfgContext->FileName[0] == '\0') |
| 938 | return TRUE; |
| 939 | else return FALSE; |
| 940 | } |
| 941 | |
| 942 | static APIRET ToolCfgDropContext (t_pToolCfgContext pCfgContext) |
| 943 | { |
| 944 | CFG_CHK_APP (ToolCfgMemFree (pCfgContext->pBuffer, MemIdCfgBuffer)) |
| 945 | |
| 946 | return NO_ERROR; |
| 947 | } |
| 948 | |
| 949 | /* CfgGetNextLine is the core function of the configuration scanner. From the calling */ |
| 950 | /* function's point of view, it simply reads the next line (or returns ppLine = nullptr if */ |
| 951 | /* no more data can be found). Internally it manages the INCLUDE and SECTION statements */ |
| 952 | /* as well as trailing backslashes (for joining lines). */ |
| 953 | static APIRET ToolCfgGetNextLine0 (t_pchar *ppLine, t_pint pLineLen) |
| 954 | { |
| 955 | t_pToolCfgContext pContext; |
| 956 | t_pchar pEol, pTmp, pStart, pKeyWord, pFirstWord; |
| 957 | t_int ParamLen; |
| 958 | t_int RemLineLen; |
| 959 | char TmpChar; |
| 960 | t_int KeyLen; |
| 961 | t_int KeyLenSectionStart; |
| 962 | t_int KeyLenSectionEnd; |
| 963 | t_int SectionNameFound; |
| 964 | t_int i; |
| 965 | t_int SectionSubNesting; |
| 966 | bool ChkInclude; |
| 967 | bool ChkIncludeOpt; |
| 968 | APIRET rc; |
| 969 | char Space[] = " "; |
| 970 | |
| 971 | pTmp = Space; |
| 972 | pContext = ToolCfgLocal.pActCfgContext; |
| 973 | while ((pContext->State != CFGSTATE_LINEREAD) && |
| 974 | (pContext->State != CFGSTATE_COMPLETED)) |
| 975 | { |
| 976 | switch (pContext->State) |
| 977 | { |
| 978 | case CFGSTATE_INCLUDENEWFILE: |
| 979 | case CFGSTATE_INCLUDENEWFILE_OPT: |
| 980 | if (strlen (pContext->FileName) == 0) |
| 981 | rc = ToolCfgReadCmdLine (pContext); /* The cmd line has to be read into pBuffer */ |
| 982 | else rc = ToolCfgReadCfgFile (pContext); /* The cfg file has to be read into pBuffer */ |
| 983 | if ((rc == TOOLCFG_ERROR_OPEN_FAILED) && (pContext->State == CFGSTATE_INCLUDENEWFILE_OPT)) |
| 984 | { |
| 985 | ToolCfgLocal.IncludeNestingLevel--; |
| 986 | ToolCfgLocal.pActCfgContext = &ToolCfgLocal.pCfgContextStack[ToolCfgLocal.IncludeNestingLevel]; |
| 987 | pContext = ToolCfgLocal.pActCfgContext; |
| 988 | pContext->State = CFGSTATE_GETNEWLINE; |
| 989 | } |
| 990 | else |
| 991 | { |
| 992 | CFG_CHK_APP (rc) |
| 993 | pTmp = pContext->pActLine; |
| 994 | CFG_CHK_APP (ToolCfgGetLineLen (pContext)) |
| 995 | pContext->State = CFGSTATE_CHECKEOF; |
| 996 | } |
| 997 | break; |
| 998 | |
| 999 | case CFGSTATE_GETNEWLINE: |
| 1000 | CFG_CHK_APP (ToolCfgSearchLine (pContext)) |
| 1001 | pContext->State = CFGSTATE_CHECKEOF; |
| 1002 | break; |
| 1003 | |
| 1004 | case CFGSTATE_CHECKEOF: |
| 1005 | for ( pTmp = pContext->pActLine; |
| 1006 | (*pTmp == ' ') || (*pTmp == '\t'); |
| 1007 | pTmp++) |
| 1008 | { |
| 1009 | } |
| 1010 | if ((*pTmp == '\0') || (*pTmp == CHAR_EOF)) |
| 1011 | { |
| 1012 | if (pContext->SectionNesting != 0) |
| 1013 | { |
| 1014 | pContext->pActLine = nullptr; |
| 1015 | CFG_CHK_APP (ToolCfgStdErrorHeader (nullptr)) |
| 1016 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "Missing statement %s before end of file.", KEYWORD_SECTIONEND)) |
| 1017 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 1018 | } |
| 1019 | CFG_CHK_APP (ToolCfgDropContext (pContext)) |
| 1020 | ToolCfgLocal.IncludeNestingLevel--; |
| 1021 | if (ToolCfgLocal.IncludeNestingLevel == -1) /* did we leave the last nesting level */ |
| 1022 | { |
| 1023 | pTmp = nullptr; |
| 1024 | pContext->pActLine = nullptr; |
| 1025 | pContext->ActLineLen = 0; |
| 1026 | pContext->State = CFGSTATE_COMPLETED; |
| 1027 | } |
| 1028 | else |
| 1029 | { |
| 1030 | ToolCfgLocal.pActCfgContext = &ToolCfgLocal.pCfgContextStack[ToolCfgLocal.IncludeNestingLevel]; |
| 1031 | pContext = ToolCfgLocal.pActCfgContext; |
| 1032 | pContext->State = CFGSTATE_GETNEWLINE; |
| 1033 | } |
| 1034 | } |
| 1035 | else |
| 1036 | pContext->State = CFGSTATE_CHECKINCLUDE; |
| 1037 | break; |
| 1038 | |
| 1039 | case CFGSTATE_CHECKINCLUDE: |
| 1040 | for ( pKeyWord = pTmp; |
| 1041 | (*pKeyWord == ' ') || (*pKeyWord == '\t'); |
| 1042 | pKeyWord++) |
| 1043 | { |
| 1044 | } |
| 1045 | if (pKeyWord == nullptr) // Should not occur (has been checked in CFGSTATE_CHECKEOF) |
| 1046 | return TOOLCFG_ERROR_UNEXPECTED_EOL; |
| 1047 | ChkInclude = ToolCfgStrNCmpNoCase (pKeyWord, KEYWORD_INCLUDE , (t_int) strlen (KEYWORD_INCLUDE )) == 0; |
| 1048 | ChkIncludeOpt = ToolCfgStrNCmpNoCase (pKeyWord, KEYWORD_INCLUDE_OPT, (t_int) strlen (KEYWORD_INCLUDE_OPT)) == 0; |
| 1049 | if (ChkInclude || ChkIncludeOpt) |
| 1050 | { |
| 1051 | if (++ToolCfgLocal.IncludeNestingLevel >= ToolCfgLocal.MaxIncludeNestingLevel) |
| 1052 | return TOOLCFG_ERROR_INCLUDE_NESTING_OVERFLOW; |
| 1053 | pEol = pContext->pActLine + pContext->ActLineLen; |
| 1054 | TmpChar = *pEol; |
| 1055 | *pEol = '\0'; /* set to 0 to have sscanf stop at the end of line */ |
| 1056 | ToolCfgLocal.pActCfgContext = &ToolCfgLocal.pCfgContextStack[ToolCfgLocal.IncludeNestingLevel]; |
| 1057 | pContext = ToolCfgLocal.pActCfgContext; |
| 1058 | if (ChkIncludeOpt) |
| 1059 | { |
| 1060 | pContext->State = CFGSTATE_INCLUDENEWFILE_OPT; |
| 1061 | pFirstWord = pKeyWord + strlen (KEYWORD_INCLUDE_OPT); |
| 1062 | } |
| 1063 | else |
| 1064 | { |
| 1065 | pContext->State = CFGSTATE_INCLUDENEWFILE; |
| 1066 | pFirstWord = pKeyWord + strlen (KEYWORD_INCLUDE); |
| 1067 | } |
| 1068 | |
| 1069 | // Initialize for parameter scanning |
| 1070 | ParamLen = 0; |
| 1071 | RemLineLen = strlen (pFirstWord); |
| 1072 | |
| 1073 | // Get the filename |
| 1074 | pStart = pFirstWord; |
| 1075 | CFG_CHK_APP (ToolCfgFindFirstWord (pFirstWord, (const char **) &pFirstWord, RemLineLen, &RemLineLen)) |
| 1076 | if (pFirstWord == nullptr) |
| 1077 | { |
| 1078 | ToolCfgLocal.IncludeNestingLevel--; |
| 1079 | ToolCfgLocal.pActCfgContext = &ToolCfgLocal.pCfgContextStack[ToolCfgLocal.IncludeNestingLevel]; |
| 1080 | CFG_CHK_APP (ToolCfgStdErrorHeader (pStart)) |
| 1081 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "Filename missing")) |
| 1082 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "The correct syntax is: %s <FileName> [SectionName1] [SectionName2] ... [SectionName16]", KEYWORD_INCLUDE)) |
| 1083 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 1084 | } |
| 1085 | CFG_CHK_APP (ToolCfgGetParamLen (pFirstWord, &ParamLen)) |
| 1086 | ToolCfgStrMaxCpy (&pContext->FileName[0], pFirstWord, min(MAX_FILENAME_LEN, ParamLen+1)); |
| 1087 | RemLineLen -= ParamLen; |
| 1088 | |
| 1089 | // Get the section names |
| 1090 | for (i=0; ;i++) |
| 1091 | { |
| 1092 | CFG_CHK_APP (ToolCfgFindFirstWord (pFirstWord + ParamLen, (const char **) &pFirstWord, RemLineLen, &RemLineLen)) |
| 1093 | if (pFirstWord == nullptr) |
| 1094 | break; |
| 1095 | if (i >= MAX_SECTIONNAMES) |
| 1096 | { |
| 1097 | ToolCfgLocal.IncludeNestingLevel--; |
| 1098 | ToolCfgLocal.pActCfgContext = &ToolCfgLocal.pCfgContextStack[ToolCfgLocal.IncludeNestingLevel]; |
| 1099 | CFG_CHK_APP (ToolCfgStdErrorHeader (pFirstWord + ParamLen)) |
| 1100 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "Too many parameters for keyword %s or %s", KEYWORD_INCLUDE, KEYWORD_INCLUDE_OPT)) |
| 1101 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "The correct syntax is: %s <FileName> [SectionName1] [SectionName2] ... [SectionName16]", KEYWORD_INCLUDE)) |
| 1102 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 1103 | } |
| 1104 | CFG_CHK_APP (ToolCfgGetParamLen (pFirstWord, &ParamLen)) |
| 1105 | ToolCfgStrMaxCpy (&(pContext->SectionNameArr[i][0]), pFirstWord, min(MAX_SECTIONNAME_LEN, ParamLen)+1); |
| 1106 | RemLineLen -= ParamLen; |
| 1107 | } |
| 1108 | *pEol = TmpChar; |
| 1109 | |
| 1110 | while (i<MAX_SECTIONNAMES) |
| 1111 | (pContext->SectionNameArr[i++])[0] = '\0'; |
| 1112 | pContext->SectionNesting = 0; |
| 1113 | } |
| 1114 | else |
| 1115 | pContext->State = CFGSTATE_CHECKSECTION; |
| 1116 | break; |
| 1117 | |
| 1118 | case CFGSTATE_CHECKSECTION: |
| 1119 | for ( pKeyWord = pTmp; /* lint ?????????? */ |
| 1120 | (*pKeyWord == ' ') || (*pKeyWord == '\t'); |
| 1121 | pKeyWord++) |
| 1122 | { |
| 1123 | } |
| 1124 | if (pKeyWord == nullptr) // Should not occur (has been checked in CFGSTATE_CHECKEOF) |
| 1125 | return TOOLCFG_ERROR_UNEXPECTED_EOL; |
| 1126 | if (ToolCfgStrNCmpNoCase (pKeyWord, KEYWORD_SECTIONSTART, (t_int) strlen (KEYWORD_SECTIONSTART)) == 0) |
| 1127 | { |
| 1128 | pFirstWord = pKeyWord + strlen (KEYWORD_SECTIONSTART); |
| 1129 | RemLineLen = strlen (pFirstWord); |
| 1130 | |
| 1131 | CFG_CHK_APP (ToolCfgFindFirstWord (pFirstWord, (const char **) &pFirstWord, RemLineLen, &RemLineLen)) |
| 1132 | if (pFirstWord == nullptr) |
| 1133 | { |
| 1134 | CFG_CHK_APP (ToolCfgStdErrorHeader (pFirstWord)) |
| 1135 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "Section name expected.")) |
| 1136 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "The correct syntax is: SECTION [SectionName1] [SectionName2] ...")) |
| 1137 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 1138 | } |
| 1139 | SectionNameFound = FALSE; |
| 1140 | while (pFirstWord != nullptr) |
| 1141 | { |
| 1142 | CFG_CHK_APP (ToolCfgGetParamLen (pFirstWord, &ParamLen)) |
| 1143 | TmpChar = pFirstWord[ParamLen]; |
| 1144 | pFirstWord[ParamLen] = '\0'; |
| 1145 | for (i=0; (i<MAX_SECTIONNAMES) && !SectionNameFound; i++) |
| 1146 | { |
| 1147 | SectionNameFound = (ToolCfgStrCmpNoCase (&(pContext->SectionNameArr[i][0]), pFirstWord) == 0); // pFirstWord may have less chars than SectionNameArr[i], |
| 1148 | } // but in that case, ToolCfgStrCmpNoCase will no return 0 (eq) |
| 1149 | if (!SectionNameFound) |
| 1150 | { |
| 1151 | for (i=0; (i<MAX_GLOBALSECTIONNAMES) && !SectionNameFound; i++) |
| 1152 | SectionNameFound = (ToolCfgStrCmpNoCase (&(ToolCfgLocal.GlobalSectionNameArr[i][0]), pFirstWord) == 0); |
| 1153 | } |
| 1154 | pFirstWord[ParamLen] = TmpChar; |
| 1155 | if (SectionNameFound) |
| 1156 | break; |
| 1157 | RemLineLen -= ParamLen; |
| 1158 | CFG_CHK_APP (ToolCfgFindFirstWord (pFirstWord + ParamLen, (const char **) &pFirstWord, RemLineLen, &RemLineLen)) |
| 1159 | } |
| 1160 | if (SectionNameFound) |
| 1161 | { |
| 1162 | pContext->SectionNesting++; |
| 1163 | pContext->State = CFGSTATE_GETNEWLINE; |
| 1164 | } |
| 1165 | else |
| 1166 | { |
| 1167 | pContext->State = CFGSTATE_SEARCHSECTIONEND; |
| 1168 | } |
| 1169 | } |
| 1170 | else |
| 1171 | { |
| 1172 | pContext->State = CFGSTATE_CHECKSECTIONEND; |
| 1173 | } |
| 1174 | break; |
| 1175 | |
| 1176 | case CFGSTATE_CHECKSECTIONEND: |
| 1177 | KeyLen = (t_int) strlen (KEYWORD_SECTIONEND); |
| 1178 | for ( pKeyWord = pTmp; /* lint: ???? */ |
| 1179 | (*pKeyWord == ' ') || (*pKeyWord == '\t'); |
| 1180 | pKeyWord++) |
| 1181 | { |
| 1182 | } |
| 1183 | if (ToolCfgStrNCmpNoCase (pKeyWord, KEYWORD_SECTIONEND, KeyLen) == 0) |
| 1184 | { |
| 1185 | CFG_CHK_APP (ToolCfgNoFurtherParamsExpected (pKeyWord + KeyLen, "ENDSECTION requires no parameters.")) |
| 1186 | if (pContext->SectionNesting <= 0) |
| 1187 | { |
| 1188 | CFG_CHK_APP (ToolCfgStdErrorHeader (pKeyWord)) |
| 1189 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "No corresponding SECTION statement encountered.")) |
| 1190 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 1191 | } |
| 1192 | else |
| 1193 | { |
| 1194 | pContext->SectionNesting--; |
| 1195 | pContext->State = CFGSTATE_GETNEWLINE; |
| 1196 | } |
| 1197 | } |
| 1198 | else |
| 1199 | pContext->State = CFGSTATE_LINEREAD; |
| 1200 | break; |
| 1201 | |
| 1202 | case CFGSTATE_SEARCHSECTIONEND: |
| 1203 | KeyLenSectionStart = (t_int) strlen (KEYWORD_SECTIONSTART); |
| 1204 | KeyLenSectionEnd = (t_int) strlen (KEYWORD_SECTIONEND ); |
| 1205 | for (SectionSubNesting = 0;;) |
| 1206 | { |
| 1207 | CFG_CHK_APP (ToolCfgSearchLine (pContext)) |
| 1208 | if (*pContext->pActLine == '\0') |
| 1209 | { |
| 1210 | pContext->pActLine = nullptr; |
| 1211 | CFG_CHK_APP (ToolCfgStdErrorHeader (nullptr)) |
| 1212 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "Missing statement ENDSECTION before end of file.")) |
| 1213 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 1214 | } |
| 1215 | pTmp = pContext->pActLine; |
| 1216 | while ((*pTmp != '\0') && ((*pTmp == ' ') || (*pTmp == '\t'))) |
| 1217 | pTmp++; |
| 1218 | |
| 1219 | if (ToolCfgStrNCmpNoCase (pTmp, KEYWORD_SECTIONEND, KeyLenSectionEnd) == 0) |
| 1220 | { |
| 1221 | CFG_CHK_APP (ToolCfgNoFurtherParamsExpected (pTmp + KeyLenSectionEnd, "ENDSECTION requires no parameters.")) |
| 1222 | if (SectionSubNesting == 0) |
| 1223 | { |
| 1224 | pContext->State = CFGSTATE_GETNEWLINE; |
| 1225 | break; |
| 1226 | } |
| 1227 | SectionSubNesting--; |
| 1228 | } |
| 1229 | else if (ToolCfgStrNCmpNoCase (pTmp, KEYWORD_SECTIONSTART, KeyLenSectionStart) == 0) |
| 1230 | { |
| 1231 | SectionSubNesting++; |
| 1232 | } |
| 1233 | } |
| 1234 | break; |
| 1235 | |
| 1236 | default: |
| 1237 | return TOOLCFG_ERROR_INVALID_STATE; |
| 1238 | } |
| 1239 | } |
| 1240 | pContext->State = CFGSTATE_GETNEWLINE; /* This will be our task on the next call of this function */ |
| 1241 | *ppLine = pContext->pActLine; |
| 1242 | *pLineLen = pContext->ActLineLen; |
| 1243 | |
| 1244 | return NO_ERROR; |
| 1245 | } |
| 1246 | |
| 1247 | static APIRET ToolCfgGetNextLine (t_pchar *ppLine, t_pint pLineLen) |
| 1248 | { |
| 1249 | t_pchar pFirstWord; |
| 1250 | APIRET rc; |
| 1251 | int Eq, RemLineLen; |
| 1252 | |
| 1253 | for (;;) // Stay in loop until non-remark |
| 1254 | { |
| 1255 | rc = ToolCfgGetNextLine0 (ppLine, pLineLen); |
| 1256 | if ((rc != NO_ERROR) || (*ppLine == nullptr) || (*pLineLen == 0)) |
| 1257 | return rc; |
| 1258 | |
| 1259 | if (**ppLine != KEYCHAR_REMARK) |
| 1260 | { |
| 1261 | CFG_CHK_APP (ToolCfgFindFirstWord (*ppLine, (const char **)&pFirstWord, *pLineLen, &RemLineLen)) |
| 1262 | CFG_CHK_APP (ToolCfgCompareKeyWord (KEYWORD_REMARK, pFirstWord, RemLineLen, &Eq, nullptr)) |
| 1263 | if (!Eq) |
| 1264 | return rc; |
| 1265 | } |
| 1266 | } |
| 1267 | } |
| 1268 | |
| 1269 | |
| 1270 | /* ----------------------------------- */ |
| 1271 | /* Conversion functions */ |
| 1272 | /* ----------------------------------- */ |
| 1273 | |
| 1274 | static APIRET ToolCfgConvertpParamToHMS (t_pchar pParam, t_int /*ParamLen*/, t_pint pValue) |
| 1275 | { |
| 1276 | t_int rc; |
| 1277 | t_int Hour, Min, Sec; |
| 1278 | |
| 1279 | if (((pParam[0] >= '0') && (pParam[0] <= '9'))) |
| 1280 | { |
| 1281 | rc = sscanf (pParam, "%d:%d:%d", &Hour, &Min, &Sec); |
| 1282 | if (rc!=3) |
| 1283 | { |
| 1284 | Sec = 0; |
| 1285 | rc = sscanf (pParam, "%d:%d", &Hour, &Min); // Check reduced HH:MM format |
| 1286 | if (rc!=2) |
| 1287 | { |
| 1288 | CFG_CHK_APP (ToolCfgStdErrorHeader (pParam)) |
| 1289 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "Invalid hh:mm:ss value.")) |
| 1290 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 1291 | } |
| 1292 | } |
| 1293 | *pValue = Hour*3600 + Min*60 + Sec; // convert to seconds |
| 1294 | } |
| 1295 | else |
| 1296 | { |
| 1297 | CFG_CHK_APP (ToolCfgStdErrorHeader (pParam)) |
| 1298 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "Invalid entry, hh:mm:ss expected.")) |
| 1299 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 1300 | } |
| 1301 | return NO_ERROR; |
| 1302 | } |
| 1303 | |
| 1304 | static APIRET ToolCfgConvertpParamToInteger (t_pchar pParam, t_int ParamLen, t_pint pValue, t_pint pDecConv) |
| 1305 | { |
| 1306 | t_int rc; |
| 1307 | t_int i; |
| 1308 | t_int MinLen; |
| 1309 | |
| 1310 | if ( ((pParam[0] == '0') && ((pParam[1] == 'x') || (pParam[1] == 'X'))) || |
| 1311 | (pParam[0] == '$')) |
| 1312 | { |
| 1313 | if (pParam[0] == '$') |
| 1314 | MinLen = 1; |
| 1315 | else MinLen = 2; |
| 1316 | |
| 1317 | for (i=MinLen; i<ParamLen; i++) |
| 1318 | { |
| 1319 | if(!((pParam[i] >= '0') && (pParam[i] <= '9')) && |
| 1320 | !((pParam[i] >= 'a') && (pParam[i] <= 'f')) && |
| 1321 | !((pParam[i] >= 'A') && (pParam[i] <= 'F'))) |
| 1322 | { |
| 1323 | CFG_CHK_APP (ToolCfgStdErrorHeader (pParam+i)) |
| 1324 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "Invalid hexadecimal value.")) |
| 1325 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 1326 | } |
| 1327 | } |
| 1328 | |
| 1329 | if (ParamLen > MinLen) |
| 1330 | rc = sscanf (&pParam[MinLen], "%x", pValue); |
| 1331 | else rc = 0; |
| 1332 | if (rc!=1) |
| 1333 | { |
| 1334 | CFG_CHK_APP (ToolCfgStdErrorHeader (pParam)) |
| 1335 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "Invalid hexadecimal value.")) |
| 1336 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 1337 | } |
| 1338 | *pDecConv=FALSE; |
| 1339 | } |
| 1340 | else if (((pParam[0] >= '0') && (pParam[0] <= '9')) || (pParam[0] == '-')) |
| 1341 | { |
| 1342 | for (i=1; i<ParamLen; i++) |
| 1343 | { |
| 1344 | if ((pParam[i] < '0') || (pParam[i] > '9')) |
| 1345 | { |
| 1346 | CFG_CHK_APP (ToolCfgStdErrorHeader (pParam+i)) |
| 1347 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "Invalid decimal value.")) |
| 1348 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 1349 | } |
| 1350 | } |
| 1351 | |
| 1352 | rc = sscanf (pParam, "%d", pValue); |
| 1353 | if (rc!=1) |
| 1354 | { |
| 1355 | CFG_CHK_APP (ToolCfgStdErrorHeader (pParam)) |
| 1356 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "Invalid decimal value.")) |
| 1357 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 1358 | } |
| 1359 | *pDecConv=TRUE; |
| 1360 | } |
| 1361 | else |
| 1362 | { |
| 1363 | CFG_CHK_APP (ToolCfgStdErrorHeader (pParam)) |
| 1364 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "Invalid entry, decimal or hexadecimal number expected.")) |
| 1365 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 1366 | } |
| 1367 | |
| 1368 | return NO_ERROR; |
| 1369 | } |
| 1370 | |
| 1371 | static APIRET ToolCfgConvertpParamToDouble (t_pchar pParam, t_int ParamLen, t_pdouble pValue) |
| 1372 | { |
| 1373 | t_int rc; |
| 1374 | long double LongDoubleVal; |
| 1375 | t_pchar pOldLocale; |
| 1376 | t_pchar pSavedLocale; |
| 1377 | t_int i; |
| 1378 | t_int DecimalPoint; |
| 1379 | t_int Exponent; |
| 1380 | |
| 1381 | // Check all the chars in the param to detect entry errors |
| 1382 | // Convert ',' to '.' |
| 1383 | DecimalPoint = FALSE; |
| 1384 | Exponent = FALSE; |
| 1385 | for (i=0; i<ParamLen; i++) |
| 1386 | { |
| 1387 | if (pParam[i] == ',') |
| 1388 | pParam[i] = '.'; |
| 1389 | if (pParam[i] == '.') |
| 1390 | { |
| 1391 | if (i==0) |
| 1392 | { |
| 1393 | CFG_CHK_APP (ToolCfgStdErrorHeader (pParam+i)) |
| 1394 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "Invalid double value, replace '.XXX' by '0.XXX'")) |
| 1395 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 1396 | } |
| 1397 | |
| 1398 | if (DecimalPoint) |
| 1399 | { |
| 1400 | CFG_CHK_APP (ToolCfgStdErrorHeader (pParam+i)) |
| 1401 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "Invalid double value, two decimal points have been detected")) |
| 1402 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 1403 | } |
| 1404 | else |
| 1405 | { |
| 1406 | DecimalPoint = TRUE; |
| 1407 | } |
| 1408 | } |
| 1409 | else if ((pParam[i] == 'E') || (pParam[i] == 'e')) |
| 1410 | { |
| 1411 | if (Exponent) |
| 1412 | { |
| 1413 | CFG_CHK_APP (ToolCfgStdErrorHeader (pParam+i)) |
| 1414 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "Invalid double value, two 'E'xponents have been detected")) |
| 1415 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 1416 | } |
| 1417 | else |
| 1418 | { |
| 1419 | Exponent = TRUE; |
| 1420 | } |
| 1421 | } |
| 1422 | else if (pParam[i] == '-') |
| 1423 | { |
| 1424 | if (i!=0) |
| 1425 | { |
| 1426 | if ((pParam[i-1] != 'E') && |
| 1427 | (pParam[i-1] != 'e')) |
| 1428 | { |
| 1429 | CFG_CHK_APP (ToolCfgStdErrorHeader (pParam+i)) |
| 1430 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "Invalid double value, minus sign must be placed at the beginning")) |
| 1431 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 1432 | } |
| 1433 | } |
| 1434 | } |
| 1435 | else if ((pParam[i] < '0') || (pParam[i] > '9')) |
| 1436 | { |
| 1437 | CFG_CHK_APP (ToolCfgStdErrorHeader (pParam+i)) |
| 1438 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "Invalid character encountered")) |
| 1439 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 1440 | } |
| 1441 | } |
| 1442 | |
| 1443 | // switch the 'locale' to accept '.' as decimal point, |
| 1444 | // scan the double value and switch back. |
| 1445 | pOldLocale = setlocale (LC_NUMERIC, nullptr); |
| 1446 | pSavedLocale = strdup (pOldLocale); |
| 1447 | (void) setlocale (LC_NUMERIC, "C"); |
| 1448 | rc = sscanf (pParam, "%LG", &LongDoubleVal); |
| 1449 | (void) setlocale (LC_NUMERIC, pSavedLocale); |
| 1450 | free (pSavedLocale); |
| 1451 | |
| 1452 | if (rc!=1) |
| 1453 | { |
| 1454 | CFG_CHK_APP (ToolCfgStdErrorHeader (pParam)) |
| 1455 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "Invalid double value.")) |
| 1456 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 1457 | } |
| 1458 | *pValue = (t_double) LongDoubleVal; |
| 1459 | |
| 1460 | return NO_ERROR; |
| 1461 | } |
| 1462 | |
| 1463 | static APIRET ToolCfgReadParameterHMS (t_pchar pParam, t_int ParamLen, t_pToolCfgDataDesc pCfgDataDesc, long BaseAddr, t_int Assign) |
| 1464 | { |
| 1465 | t_pcchar pFormatStr; |
| 1466 | t_int Value; |
| 1467 | t_int MinVal, MaxVal; |
| 1468 | |
| 1469 | CFG_CHK_APP (ToolCfgConvertpParamToHMS (pParam, ParamLen, &Value)) |
| 1470 | |
| 1471 | if ((Value < (t_int)pCfgDataDesc->MinValue) || (Value > (t_int)pCfgDataDesc->MaxValue)) |
| 1472 | { |
| 1473 | CFG_CHK_APP (ToolCfgStdErrorHeader (pParam)) |
| 1474 | MinVal = (t_int)pCfgDataDesc->MinValue; |
| 1475 | MaxVal = (t_int)pCfgDataDesc->MaxValue; |
| 1476 | |
| 1477 | pFormatStr = "Value out of range: %02d:%02d:%02d <= %s <= %02d:%02d:%02d"; |
| 1478 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, pFormatStr, MinVal/3600, (MinVal/60)%60, MinVal%60, pCfgDataDesc->pName, |
| 1479 | MaxVal/3600, (MaxVal/60)%60, MaxVal%60)) |
| 1480 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 1481 | } |
| 1482 | if (Assign) |
| 1483 | *((t_pint)((t_pchar)pCfgDataDesc->DestAddr + BaseAddr)) = Value; |
| 1484 | |
| 1485 | return NO_ERROR; |
| 1486 | } |
| 1487 | |
| 1488 | static APIRET ToolCfgReadParameterInteger (t_pchar pParam, t_int ParamLen, t_pToolCfgDataDesc pCfgDataDesc, long BaseAddr, t_int Assign) |
| 1489 | { |
| 1490 | t_pcchar pFormatStr; |
| 1491 | t_int Value; |
| 1492 | t_int DecConv; /* To check whether a decimal or hexadecimal conversion had be performed */ |
| 1493 | |
| 1494 | CFG_CHK_APP (ToolCfgConvertpParamToInteger (pParam, ParamLen, &Value, &DecConv)) |
| 1495 | |
| 1496 | if ((Value < (t_int)pCfgDataDesc->MinValue) || (Value > (t_int)pCfgDataDesc->MaxValue)) |
| 1497 | { |
| 1498 | CFG_CHK_APP (ToolCfgStdErrorHeader (pParam)) |
| 1499 | if (DecConv) |
| 1500 | pFormatStr = "Value out of range: %d <= %s <= %d"; |
| 1501 | else pFormatStr = "Value out of range: 0x%X <= %s <= 0x%X"; |
| 1502 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, pFormatStr, (t_int)pCfgDataDesc->MinValue, pCfgDataDesc->pName, (t_int)pCfgDataDesc->MaxValue)) |
| 1503 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 1504 | } |
| 1505 | if (Assign) |
| 1506 | *((t_pint)((t_pchar)pCfgDataDesc->DestAddr + BaseAddr)) = Value; |
| 1507 | |
| 1508 | return NO_ERROR; |
| 1509 | } |
| 1510 | |
| 1511 | static APIRET ToolCfgReadParameterDouble (t_pchar pParam, t_int ParamLen, t_pToolCfgDataDesc pCfgDataDesc, long BaseAddr, t_int Assign) |
| 1512 | { |
| 1513 | t_pcchar pFormatStr; |
| 1514 | t_double Value; |
| 1515 | |
| 1516 | CFG_CHK_APP (ToolCfgConvertpParamToDouble (pParam, ParamLen, &Value)) |
| 1517 | |
| 1518 | if ((Value < pCfgDataDesc->MinValue) || (Value > pCfgDataDesc->MaxValue)) |
| 1519 | { |
| 1520 | CFG_CHK_APP (ToolCfgStdErrorHeader (pParam)) |
| 1521 | pFormatStr = "Value out of range: %G <= %s <= %G"; |
| 1522 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, pFormatStr, pCfgDataDesc->MinValue, pCfgDataDesc->pName, pCfgDataDesc->MaxValue)) |
| 1523 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 1524 | } |
| 1525 | if (Assign) |
| 1526 | *((t_pdouble)((t_pchar)pCfgDataDesc->DestAddr + BaseAddr)) = Value; |
| 1527 | |
| 1528 | return NO_ERROR; |
| 1529 | } |
| 1530 | |
| 1531 | static APIRET ToolCfgConvertpParamToString (t_pchar pParam, t_int ParamLen, t_pchar *ppString, t_pint pStringLen) |
| 1532 | { |
| 1533 | if (pParam[0] != STRING_DELIMITER) |
| 1534 | { |
| 1535 | CFG_CHK_APP (ToolCfgStdErrorHeader (pParam)) |
| 1536 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "String expected. Use %c %c as string delimiters.", STRING_DELIMITER, STRING_DELIMITER)) |
| 1537 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 1538 | } |
| 1539 | if (pParam[ParamLen-1] != STRING_DELIMITER) |
| 1540 | { |
| 1541 | CFG_CHK_APP (ToolCfgStdErrorHeader (pParam+ParamLen-1)) |
| 1542 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "String exceeds end of line, use %c %c as string delimiters, or %c to append next line.", STRING_DELIMITER, STRING_DELIMITER, CHAR_APPEND_LINE)) |
| 1543 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 1544 | } |
| 1545 | *ppString = pParam+1; |
| 1546 | *pStringLen = ParamLen-2; |
| 1547 | |
| 1548 | return NO_ERROR; |
| 1549 | } |
| 1550 | |
| 1551 | static APIRET ToolCfgReadParameterString (t_pchar pParam, t_int ParamLen, t_pToolCfgDataDesc pCfgDataDesc, long BaseAddr, t_int Assign) |
| 1552 | { |
| 1553 | t_pchar pString; |
| 1554 | t_int StringLen; |
| 1555 | t_pchar pDest; |
| 1556 | t_int Src, Dst; |
| 1557 | |
| 1558 | CFG_CHK_APP (ToolCfgConvertpParamToString (pParam, ParamLen, &pString, &StringLen)) |
| 1559 | if (StringLen > pCfgDataDesc->DestLen) |
| 1560 | { |
| 1561 | CFG_CHK_APP (ToolCfgStdErrorHeader (pParam)) |
| 1562 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "String too long. The maximum length of the string is %d", pCfgDataDesc->DestLen)) |
| 1563 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 1564 | } |
| 1565 | if (Assign) |
| 1566 | { |
| 1567 | /* Copy the string, replace duplicate '' by single' */ |
| 1568 | pDest = (t_pchar)pCfgDataDesc->DestAddr + BaseAddr; |
| 1569 | for (Src=0, Dst=0; Src<StringLen; Src++, Dst++) |
| 1570 | { |
| 1571 | pDest[Dst] = pString[Src]; |
| 1572 | if ((pString[Src] == STRING_DELIMITER) && (Src<(StringLen-1))) |
| 1573 | { |
| 1574 | if (pString[Src+1] == STRING_DELIMITER) |
| 1575 | Src++; |
| 1576 | } |
| 1577 | } |
| 1578 | pDest[Dst] = '\0'; |
| 1579 | |
| 1580 | // (void) memcpy ((t_pchar)pCfgDataDesc->DestAddr + BaseAddr, pString, (size_t) StringLen); |
| 1581 | // ((t_pchar)pCfgDataDesc->DestAddr + BaseAddr)[StringLen] = '\0'; |
| 1582 | } |
| 1583 | |
| 1584 | return NO_ERROR; |
| 1585 | } |
| 1586 | |
| 1587 | static APIRET ToolCfgConvertpParamToSet (t_pchar pParam, t_int ParamLen, t_pToolCfgDataDesc pCfgDataDesc, t_pint pSetValue) |
| 1588 | { |
| 1589 | t_pchar pTmp; |
| 1590 | t_int i; |
| 1591 | t_int Len; |
| 1592 | t_pToolCfgSet pSetArray; |
| 1593 | |
| 1594 | pSetArray = pCfgDataDesc->pSetArray; |
| 1595 | for (i=0; ; i++) |
| 1596 | { |
| 1597 | if (pSetArray[i].pSetString == nullptr) |
| 1598 | break; /* end of array */ |
| 1599 | if (ParamLen == (t_int)strlen (pSetArray[i].pSetString)) |
| 1600 | if (ToolCfgStrNCmpNoCase (pSetArray[i].pSetString, pParam, ParamLen) == 0) |
| 1601 | break; /* set entry found */ |
| 1602 | } |
| 1603 | if (pSetArray[i].pSetString == nullptr) |
| 1604 | { |
| 1605 | CFG_CHK_APP (ToolCfgStdErrorHeader (pParam)) |
| 1606 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "Invalid set value.")) |
| 1607 | CFG_CHK_APP (ToolCfgBuildHelp (pCfgDataDesc, nullptr, &Len)) |
| 1608 | CFG_CHK_APP (ToolCfgMemAlloc ((void **)&pTmp, (t_uint)Len, MEM_OPT_NONE, MemIdCfgHelpBuff)) |
| 1609 | CFG_CHK_APP (ToolCfgBuildHelp (pCfgDataDesc, pTmp, &Len)) |
| 1610 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, pTmp)) |
| 1611 | CFG_CHK_APP (ToolCfgMemFree (pTmp, MemIdCfgHelpBuff)) |
| 1612 | |
| 1613 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 1614 | } |
| 1615 | *pSetValue = pSetArray[i].SetValue; |
| 1616 | |
| 1617 | return NO_ERROR; |
| 1618 | } |
| 1619 | |
| 1620 | static APIRET ToolCfgReadParameterSet (t_pchar pParam, t_int ParamLen, t_pToolCfgDataDesc pCfgDataDesc, long BaseAddr, t_int Assign) |
| 1621 | { |
| 1622 | t_int SetValue; |
| 1623 | |
| 1624 | CFG_CHK_APP (ToolCfgConvertpParamToSet (pParam, ParamLen, pCfgDataDesc, &SetValue)) |
| 1625 | if (Assign) |
| 1626 | *((t_pint)((t_pchar)pCfgDataDesc->DestAddr + BaseAddr)) = SetValue; |
| 1627 | |
| 1628 | return NO_ERROR; |
| 1629 | } |
| 1630 | |
| 1631 | static APIRET ToolCfgReadParameter0 (t_pchar pFirstParamChar, t_int ParamLen, t_pToolCfgDataDesc pCfgDataDesc, long BaseAddr, t_int Assign) |
| 1632 | { |
| 1633 | if (BaseAddr == (long)INT_MIN) |
| 1634 | Assign = FALSE; |
| 1635 | switch (pCfgDataDesc->CfgType) |
| 1636 | { |
| 1637 | case CFGTYPE_INTEGER: CFG_CHK_APP (ToolCfgReadParameterInteger (pFirstParamChar, ParamLen, pCfgDataDesc, BaseAddr, Assign)) break; |
| 1638 | case CFGTYPE_DOUBLE : CFG_CHK_APP (ToolCfgReadParameterDouble (pFirstParamChar, ParamLen, pCfgDataDesc, BaseAddr, Assign)) break; |
| 1639 | case CFGTYPE_STRING : CFG_CHK_APP (ToolCfgReadParameterString (pFirstParamChar, ParamLen, pCfgDataDesc, BaseAddr, Assign)) break; |
| 1640 | case CFGTYPE_SET : CFG_CHK_APP (ToolCfgReadParameterSet (pFirstParamChar, ParamLen, pCfgDataDesc, BaseAddr, Assign)) break; |
| 1641 | case CFGTYPE_HMS : CFG_CHK_APP (ToolCfgReadParameterHMS (pFirstParamChar, ParamLen, pCfgDataDesc, BaseAddr, Assign)) break; |
| 1642 | case CFGTYPE_NULL : break; |
| 1643 | default: |
| 1644 | return TOOLCFG_ERROR_INVALID_CFGTYPE; |
| 1645 | } |
| 1646 | |
| 1647 | return NO_ERROR; |
| 1648 | } |
| 1649 | |
| 1650 | /* ---------------------------------------------- */ |
| 1651 | /* Table scanning functions */ |
| 1652 | /* ---------------------------------------------- */ |
| 1653 | |
| 1654 | APIRET ToolCfgAddGlobalSectionName (t_pcchar pSectionName) |
| 1655 | { |
| 1656 | t_int i; |
| 1657 | |
| 1658 | if (strlen (pSectionName) > MAX_SECTIONNAME_LEN) |
| 1659 | return TOOLCFG_ERROR_SECTIONNAME_TOO_LONG; |
| 1660 | |
| 1661 | for (i=0; i<MAX_GLOBALSECTIONNAMES; i++) |
| 1662 | { |
| 1663 | if (ToolCfgLocal.GlobalSectionNameArr[i][0] == '\0') |
| 1664 | { |
| 1665 | strcpy (&(ToolCfgLocal.GlobalSectionNameArr[i][0]), pSectionName); |
| 1666 | break; |
| 1667 | } |
| 1668 | } |
| 1669 | if (i>=MAX_GLOBALSECTIONNAMES) |
| 1670 | return TOOLCFG_ERROR_TOO_MANY_SECTIONNAMES; |
| 1671 | |
| 1672 | return NO_ERROR; |
| 1673 | } |
| 1674 | |
| 1675 | APIRET ToolCfgDelGlobalSectionName (t_pcchar pSectionName) |
| 1676 | { |
| 1677 | t_int i; |
| 1678 | |
| 1679 | if (strlen (pSectionName) > MAX_SECTIONNAME_LEN) |
| 1680 | return TOOLCFG_ERROR_SECTIONNAME_TOO_LONG; |
| 1681 | |
| 1682 | for (i=0; i<MAX_GLOBALSECTIONNAMES; i++) |
| 1683 | { |
| 1684 | if (ToolCfgStrCmpNoCase (&ToolCfgLocal.GlobalSectionNameArr[i][0], pSectionName) == 0) |
| 1685 | { |
| 1686 | ToolCfgLocal.GlobalSectionNameArr[i][0] = '\0'; |
| 1687 | break; |
| 1688 | } |
| 1689 | } |
| 1690 | if (i>=MAX_GLOBALSECTIONNAMES) |
| 1691 | return TOOLCFG_ERROR_SECTIONNAME_NOTFOUND; |
| 1692 | |
| 1693 | return NO_ERROR; |
| 1694 | } |
| 1695 | |
| 1696 | static APIRET ToolCfgScanTableAnalyseLine (t_pToolCfgTableDesc pCfgTableDesc, t_pchar pFirstWord, t_int RemLineLen, |
| 1697 | long BaseAddr, t_pint pLineOk) |
| 1698 | { |
| 1699 | t_int i; |
| 1700 | t_int ParamLen; |
| 1701 | t_pToolCfgDataDesc pCfgDataDesc; |
| 1702 | |
| 1703 | *pLineOk = FALSE; |
| 1704 | /* printf ("\r\nTable entry: %*.*s", RemLineLen, RemLineLen, pFirstWord); */ |
| 1705 | if (RemLineLen == 0) |
| 1706 | return NO_ERROR; |
| 1707 | ParamLen = 0; |
| 1708 | for (i=0; ;i++) |
| 1709 | { |
| 1710 | pCfgDataDesc = &pCfgTableDesc->pDataDescArray[i]; |
| 1711 | if (pCfgDataDesc->pName == nullptr) |
| 1712 | break; /* Array ends here */ |
| 1713 | |
| 1714 | CFG_CHK_APP (ToolCfgFindFirstWord (pFirstWord + ParamLen, (const char **) &pFirstWord, RemLineLen, &RemLineLen)) |
| 1715 | CFG_CHK_APP (ToolCfgGetParamLen (pFirstWord, &ParamLen)) |
| 1716 | CFG_CHK_APP (ToolCfgReadParameter0 (pFirstWord, ParamLen, pCfgDataDesc, BaseAddr, TRUE)) |
| 1717 | } |
| 1718 | *pLineOk = TRUE; |
| 1719 | |
| 1720 | return NO_ERROR; |
| 1721 | } |
| 1722 | |
| 1723 | static APIRET ToolCfgScanTable (t_pchar pActLine, t_int LineLen, t_pToolCfgTableDesc pCfgTableDescArray) |
| 1724 | { |
| 1725 | t_pchar pFirstWord; |
| 1726 | t_int RemLineLen; |
| 1727 | t_int i, rcs, Eq; |
| 1728 | t_int LineOk; |
| 1729 | t_int Found; |
| 1730 | t_pchar pTableType; |
| 1731 | t_pchar pTableName; |
| 1732 | long BaseAddr; |
| 1733 | t_pcchar pErrorText; |
| 1734 | t_char TmpChar; |
| 1735 | t_pToolCfgTableDesc pCfgTableDesc = nullptr; |
| 1736 | |
| 1737 | TmpChar = pActLine[LineLen]; /* set end of line to '\0' to ease processing */ |
| 1738 | pActLine[LineLen] = '\0'; |
| 1739 | CFG_CHK_APP (ToolCfgMemAlloc ((void **)&pTableType, 64, MEM_OPT_NONE, MemIdCfgTableType)) |
| 1740 | CFG_CHK_APP (ToolCfgMemAlloc ((void **)&pTableName, 64, MEM_OPT_NONE, MemIdCfgTableName)) |
| 1741 | CFG_CHK_APP (ToolCfgFindFirstWord (pActLine, (const char **) &pFirstWord, LineLen, &RemLineLen)) |
| 1742 | rcs = sscanf (pActLine, "%*s %64s %64s", pTableType, pTableName); |
| 1743 | if (rcs != 2) |
| 1744 | { |
| 1745 | CFG_CHK_APP (ToolCfgStdErrorHeader (pFirstWord)) |
| 1746 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "Invalid parameters for keyword %s", KEYWORD_TABLESTART)) |
| 1747 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "The correct syntax is: %s <TableType> <TableName>", KEYWORD_TABLESTART)) |
| 1748 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 1749 | } |
| 1750 | Found = FALSE; |
| 1751 | if (pCfgTableDescArray != nullptr) |
| 1752 | { |
| 1753 | for (i=0; ;i++) |
| 1754 | { |
| 1755 | pCfgTableDesc = &pCfgTableDescArray[i]; |
| 1756 | if (pCfgTableDesc->pTableType == nullptr) /* End of array reached? */ |
| 1757 | break; |
| 1758 | if (ToolCfgStrCmpNoCase(pCfgTableDesc->pTableType, pTableType) == 0) /* Table type found? */ |
| 1759 | { |
| 1760 | Found = TRUE; |
| 1761 | break; |
| 1762 | } |
| 1763 | } |
| 1764 | } |
| 1765 | if (!Found) |
| 1766 | { |
| 1767 | CFG_CHK_APP (ToolCfgStdErrorHeader (pFirstWord)) |
| 1768 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "Invalid table type: %s", pTableType)) |
| 1769 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 1770 | } |
| 1771 | CFG_CHK_APP ((*pCfgTableDesc->pStartFn) (pTableName, &BaseAddr, &pErrorText)) |
| 1772 | if (pErrorText) |
| 1773 | { |
| 1774 | CFG_CHK_APP (ToolCfgStdErrorHeader (pActLine)) |
| 1775 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, pErrorText)) |
| 1776 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 1777 | } |
| 1778 | |
| 1779 | pActLine[LineLen] = TmpChar; |
| 1780 | for (LineLen=0;;) |
| 1781 | { |
| 1782 | CFG_CHK_APP (ToolCfgGetNextLine (&pActLine, &LineLen)) |
| 1783 | if (pActLine == nullptr) |
| 1784 | { |
| 1785 | CFG_CHK_APP (ToolCfgStdErrorHeader (nullptr)) |
| 1786 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "Missing statement %s before end of file.", KEYWORD_TABLEEND)) |
| 1787 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 1788 | } |
| 1789 | TmpChar = pActLine[LineLen]; // set end of line to '\0' to ease processing |
| 1790 | pActLine[LineLen] = '\0'; |
| 1791 | CFG_CHK_APP (ToolCfgFindFirstWord (pActLine, (const char **) &pFirstWord, LineLen, &RemLineLen)) |
| 1792 | CFG_CHK_APP (ToolCfgCompareKeyWord (KEYWORD_TABLEEND, pFirstWord, RemLineLen, &Eq, nullptr)) |
| 1793 | if (Eq) |
| 1794 | { |
| 1795 | if (pCfgTableDesc->pEndFn) |
| 1796 | { |
| 1797 | CFG_CHK_APP ((*pCfgTableDesc->pEndFn) (&pErrorText)) |
| 1798 | if (pErrorText) |
| 1799 | { |
| 1800 | CFG_CHK_APP (ToolCfgStdErrorHeader (pActLine)) |
| 1801 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, pErrorText)) |
| 1802 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 1803 | } |
| 1804 | } |
| 1805 | pActLine[LineLen] = TmpChar; |
| 1806 | break; |
| 1807 | } |
| 1808 | CFG_CHK_APP (ToolCfgScanTableAnalyseLine (pCfgTableDesc, pFirstWord, RemLineLen, BaseAddr, &LineOk)) |
| 1809 | if (LineOk) |
| 1810 | { |
| 1811 | CFG_CHK_APP ((*pCfgTableDesc->pSaveAndNextFn) (&BaseAddr, &pErrorText)) |
| 1812 | if (pErrorText) |
| 1813 | { |
| 1814 | CFG_CHK_APP (ToolCfgStdErrorHeader (pActLine)) |
| 1815 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, pErrorText)) |
| 1816 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 1817 | } |
| 1818 | } |
| 1819 | pActLine[LineLen] = TmpChar; |
| 1820 | } |
| 1821 | CFG_CHK_APP (ToolCfgMemFree (pTableType, MemIdCfgTableType)) |
| 1822 | CFG_CHK_APP (ToolCfgMemFree (pTableName, MemIdCfgTableName)) |
| 1823 | |
| 1824 | return NO_ERROR; |
| 1825 | } |
| 1826 | |
| 1827 | /* ---------------------------------------------- */ |
| 1828 | /* Parameter scanning functions */ |
| 1829 | /* ---------------------------------------------- */ |
| 1830 | |
| 1831 | static APIRET ToolCfgCheckAssignment (t_pchar pKeyWord, t_pToolCfgParamDesc pCfgParamDesc, t_pint pAssign) |
| 1832 | { |
| 1833 | t_ToolCfgAssignment AssignSource; |
| 1834 | t_ToolCfgAssignment AssignCount; |
| 1835 | |
| 1836 | AssignSource = (t_ToolCfgAssignment)(pCfgParamDesc->Assign & CFGASN_SOURCE); |
| 1837 | AssignCount = (t_ToolCfgAssignment)(pCfgParamDesc->Assign & CFGASN_COUNT ); |
| 1838 | |
| 1839 | if ((AssignSource == CFGASN_CMD) && !ToolCfgScanningCmdLine()) |
| 1840 | { |
| 1841 | CFG_CHK_APP (ToolCfgStdErrorHeader (pKeyWord)) |
| 1842 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "Assignment for this parameter is not allowed in configuration file (only on command line).")) |
| 1843 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 1844 | } |
| 1845 | if ((AssignSource == CFGASN_CFG) && ToolCfgScanningCmdLine()) |
| 1846 | { |
| 1847 | CFG_CHK_APP (ToolCfgStdErrorHeader (pKeyWord)) |
| 1848 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "Assignment for this parameter is not allowed on command line (only in configuration file).")) |
| 1849 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 1850 | } |
| 1851 | |
| 1852 | if ((AssignCount == CFGASN_ONCE) && ((pCfgParamDesc->CfgAssignments + pCfgParamDesc->CmdAssignments) >= 1)) |
| 1853 | { |
| 1854 | CFG_CHK_APP (ToolCfgStdErrorHeader (pKeyWord)) |
| 1855 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "Assignment for this parameter is only allowed once")) |
| 1856 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 1857 | } |
| 1858 | |
| 1859 | if (AssignCount == CFGASN_TWICE) |
| 1860 | { |
| 1861 | if (((pCfgParamDesc->CfgAssignments>0) && !ToolCfgScanningCmdLine()) || |
| 1862 | ((pCfgParamDesc->CmdAssignments>0) && ToolCfgScanningCmdLine())) |
| 1863 | { |
| 1864 | CFG_CHK_APP (ToolCfgStdErrorHeader (pKeyWord)) |
| 1865 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "Assignment for this parameter is only allowed once in configuration file.")) |
| 1866 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "The assignment in configuration file may be replaced by a command line")) |
| 1867 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "assignment")) |
| 1868 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 1869 | } |
| 1870 | } |
| 1871 | |
| 1872 | if (ToolCfgScanningCmdLine()) |
| 1873 | *pAssign = TRUE; |
| 1874 | else *pAssign = pCfgParamDesc->CmdAssignments ? FALSE : TRUE; |
| 1875 | |
| 1876 | return NO_ERROR; |
| 1877 | } |
| 1878 | |
| 1879 | static APIRET ToolCfgCheckEqualSign (t_pcchar pNextChar, t_pcchar *ppFirstParamChar) |
| 1880 | { |
| 1881 | while (((*pNextChar == ' ') || (*pNextChar == '\t')) && (*pNextChar != '\0')) |
| 1882 | pNextChar++; |
| 1883 | if ((*pNextChar == '\0') || (*pNextChar != '=')) |
| 1884 | { |
| 1885 | CFG_CHK_APP (ToolCfgStdErrorHeader (pNextChar-1)) |
| 1886 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "Equal sign '=' expected.")) |
| 1887 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 1888 | } |
| 1889 | |
| 1890 | for ( *ppFirstParamChar = pNextChar+1; |
| 1891 | ((**ppFirstParamChar == ' ') || (**ppFirstParamChar == '\t')) && (**ppFirstParamChar != '\0'); |
| 1892 | ( *ppFirstParamChar)++) |
| 1893 | { |
| 1894 | } |
| 1895 | if (**ppFirstParamChar == '\0') |
| 1896 | { |
| 1897 | CFG_CHK_APP (ToolCfgStdErrorHeader ((*ppFirstParamChar)-1)) |
| 1898 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "Parameter expected.")) |
| 1899 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 1900 | } |
| 1901 | |
| 1902 | return NO_ERROR; |
| 1903 | } |
| 1904 | |
| 1905 | static APIRET ToolCfgReadParameter (t_pchar pKeyWord, t_pchar pNextChar, t_pToolCfgParamDesc pCfgParamDesc) |
| 1906 | { |
| 1907 | t_pToolCfgDataDesc pCfgDataDesc; |
| 1908 | t_pchar pFirstParamChar; |
| 1909 | t_pcchar pErrorText; |
| 1910 | t_int Assign=0; |
| 1911 | t_int ParamLen; |
| 1912 | |
| 1913 | if (pCfgParamDesc) |
| 1914 | CFG_CHK_APP (ToolCfgCheckAssignment (pKeyWord, pCfgParamDesc, &Assign)) |
| 1915 | pCfgDataDesc = &pCfgParamDesc->DataDesc; |
| 1916 | if (pCfgDataDesc->CfgType == CFGTYPE_PRESENCE) |
| 1917 | { |
| 1918 | CFG_CHK_APP (ToolCfgCheckIfNoParam (pNextChar)) |
| 1919 | *((t_pint)(pCfgParamDesc->DataDesc.DestAddr)) = TRUE; |
| 1920 | } |
| 1921 | else |
| 1922 | { |
| 1923 | CFG_CHK_APP (ToolCfgCheckEqualSign (pNextChar, (const char **) &pFirstParamChar)) |
| 1924 | CFG_CHK_APP (ToolCfgCheckIfOnlyOneParam (pFirstParamChar, &ParamLen)) |
| 1925 | CFG_CHK_APP (ToolCfgReadParameter0 (pFirstParamChar, ParamLen, pCfgDataDesc, 0, Assign)) |
| 1926 | } |
| 1927 | if (Assign) |
| 1928 | { |
| 1929 | if (ToolCfgScanningCmdLine()) |
| 1930 | pCfgParamDesc->CmdAssignments++; |
| 1931 | else pCfgParamDesc->CfgAssignments++; |
| 1932 | } |
| 1933 | |
| 1934 | if (pCfgParamDesc->pCallOnInitFn) |
| 1935 | { |
| 1936 | CFG_CHK_APP ((*pCfgParamDesc->pCallOnInitFn)(pCfgParamDesc, &pErrorText)) |
| 1937 | if (pErrorText) |
| 1938 | { |
| 1939 | CFG_CHK_APP (ToolCfgStdErrorHeader (pKeyWord)) |
| 1940 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, pErrorText)) |
| 1941 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 1942 | } |
| 1943 | } |
| 1944 | |
| 1945 | return NO_ERROR; |
| 1946 | } |
| 1947 | |
| 1948 | static APIRET ToolCfgAnalyseEntry (t_pchar pActLine, t_int LineLen, |
| 1949 | t_pToolCfgParamDesc pParamDescArray, |
| 1950 | t_pToolCfgTableDesc pTableDescArray) |
| 1951 | { |
| 1952 | t_pchar pKeyWord; |
| 1953 | t_pchar pNextChar; |
| 1954 | t_pToolCfgDataDesc pCfgDataDesc; |
| 1955 | t_int i; |
| 1956 | t_int RemLineLen; |
| 1957 | t_int Eq = 0; |
| 1958 | t_int EqDef = 0; |
| 1959 | t_int EqUnDef = 0; |
| 1960 | t_char TmpChar; |
| 1961 | t_pchar pSectionName; |
| 1962 | t_int SectionNameLen; |
| 1963 | APIRET rc; |
| 1964 | |
| 1965 | if (LineLen <=0) /* it's an empty line, unnecessary to continue */ |
| 1966 | return NO_ERROR; |
| 1967 | |
| 1968 | TmpChar = pActLine[LineLen]; /* set end of line to '\0' to ease processing */ |
| 1969 | pActLine[LineLen] = '\0'; |
| 1970 | |
| 1971 | CFG_CHK_APP (ToolCfgFindFirstWord (pActLine, (const char **) &pKeyWord, LineLen, &RemLineLen)) |
| 1972 | if (pKeyWord == nullptr) // Were there just spaces or tabs in this line ? |
| 1973 | { |
| 1974 | pActLine[LineLen] = TmpChar; |
| 1975 | return NO_ERROR; |
| 1976 | } |
| 1977 | |
| 1978 | CFG_CHK_APP (ToolCfgCompareKeyWord (KEYWORD_TABLESTART, pKeyWord, RemLineLen, &Eq, nullptr)) |
| 1979 | if (Eq) |
| 1980 | { |
| 1981 | pActLine[LineLen] = TmpChar; |
| 1982 | CFG_CHK_APP (ToolCfgScanTable(pActLine, LineLen, pTableDescArray)) |
| 1983 | return NO_ERROR; |
| 1984 | } |
| 1985 | |
| 1986 | CFG_CHK_APP (ToolCfgCompareKeyWord (KEYWORD_DEFINE , pKeyWord, RemLineLen, &EqDef , &pSectionName)) |
| 1987 | if (!EqDef) |
| 1988 | CFG_CHK_APP (ToolCfgCompareKeyWord (KEYWORD_UNDEFINE, pKeyWord, RemLineLen, &EqUnDef, &pSectionName)) |
| 1989 | if (EqDef || EqUnDef) |
| 1990 | { |
| 1991 | while (((*pSectionName == ' ') || (*pSectionName == '\t')) && (*pSectionName != '\0')) |
| 1992 | pSectionName++; |
| 1993 | |
| 1994 | if (*pSectionName == '=') |
| 1995 | { |
| 1996 | pSectionName++; |
| 1997 | while (((*pSectionName == ' ') || (*pSectionName == '\t')) && (*pSectionName != '\0')) |
| 1998 | pSectionName++; |
| 1999 | } |
| 2000 | |
| 2001 | CFG_CHK_APP (ToolCfgCheckIfOnlyOneParam (pSectionName, &SectionNameLen)) |
| 2002 | pActLine[LineLen] = TmpChar; |
| 2003 | TmpChar = pSectionName[SectionNameLen]; |
| 2004 | pSectionName[SectionNameLen] = '\0'; |
| 2005 | if (EqDef) |
| 2006 | rc = ToolCfgAddGlobalSectionName (pSectionName); |
| 2007 | else rc = ToolCfgDelGlobalSectionName (pSectionName); |
| 2008 | switch (rc) |
| 2009 | { |
| 2010 | case TOOLCFG_ERROR_SECTIONNAME_TOO_LONG : |
| 2011 | CFG_CHK_APP (ToolCfgStdErrorHeader (pSectionName)) |
| 2012 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "Section name too long (Max. %d characters).", MAX_SECTIONNAME_LEN)) |
| 2013 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 2014 | |
| 2015 | case TOOLCFG_ERROR_TOO_MANY_SECTIONNAMES: |
| 2016 | CFG_CHK_APP (ToolCfgStdErrorHeader (pSectionName)) |
| 2017 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "Too many global section names defined (Max. is %d).", MAX_GLOBALSECTIONNAMES)) |
| 2018 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 2019 | |
| 2020 | case TOOLCFG_ERROR_SECTIONNAME_NOTFOUND: |
| 2021 | CFG_CHK_APP (ToolCfgStdErrorHeader (pSectionName)) |
| 2022 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "The section name %s is not defined.", pSectionName)) |
| 2023 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 2024 | |
| 2025 | default: CFG_CHK_APP (rc) |
| 2026 | } |
| 2027 | pSectionName[SectionNameLen] = TmpChar; |
| 2028 | |
| 2029 | return NO_ERROR; |
| 2030 | } |
| 2031 | |
| 2032 | Eq = FALSE; |
| 2033 | if (pParamDescArray != nullptr) |
| 2034 | { |
| 2035 | for (i=0; ; i++) |
| 2036 | { |
| 2037 | pCfgDataDesc = &pParamDescArray[i].DataDesc; |
| 2038 | if (pCfgDataDesc->pName == nullptr) |
| 2039 | break;/* end of array */ |
| 2040 | CFG_CHK_APP (ToolCfgCompareKeyWord (pCfgDataDesc->pName, pKeyWord, RemLineLen, &Eq, &pNextChar)) |
| 2041 | if (Eq) |
| 2042 | { |
| 2043 | CFG_CHK_APP (ToolCfgReadParameter (pKeyWord, pNextChar, &pParamDescArray[i])) |
| 2044 | break; |
| 2045 | } |
| 2046 | } |
| 2047 | } |
| 2048 | |
| 2049 | if (!Eq) /* keyword not found in table */ |
| 2050 | { |
| 2051 | CFG_CHK_APP (ToolCfgStdErrorHeader (pKeyWord)) |
| 2052 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "Invalid keyword.")) |
| 2053 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 2054 | } |
| 2055 | |
| 2056 | pActLine[LineLen] = TmpChar; |
| 2057 | |
| 2058 | return NO_ERROR; |
| 2059 | } |
| 2060 | |
| 2061 | /* CfgFreeContextBuffers: Free all context buffers that are still in use */ |
| 2062 | static APIRET ToolCfgFreeContextBuffers (void) |
| 2063 | { |
| 2064 | t_pToolCfgContext pCfgContext; |
| 2065 | t_int i; |
| 2066 | |
| 2067 | for (i=ToolCfgLocal.IncludeNestingLevel; i>0; i--) |
| 2068 | { |
| 2069 | pCfgContext = &ToolCfgLocal.pCfgContextStack[i]; |
| 2070 | if (pCfgContext->pBuffer) |
| 2071 | CFG_CHK_APP (ToolCfgMemFree (pCfgContext->pBuffer, MemIdCfgBuffer)) |
| 2072 | } |
| 2073 | |
| 2074 | return NO_ERROR; |
| 2075 | } |
| 2076 | |
| 2077 | static APIRET ToolCfgScanParams (t_pToolCfgParamDesc pParamDescArray, t_pToolCfgTableDesc pTableDescArray) |
| 2078 | { |
| 2079 | t_pchar pActLine; |
| 2080 | t_int LineLen; |
| 2081 | APIRET rc; |
| 2082 | |
| 2083 | CFG_CHK_APP (ToolCfgGetNextLine (&pActLine, &LineLen)) |
| 2084 | rc = NO_ERROR; |
| 2085 | while (pActLine && !rc) |
| 2086 | { |
| 2087 | rc = ToolCfgAnalyseEntry(pActLine, LineLen, pParamDescArray, pTableDescArray); |
| 2088 | if (!rc) |
| 2089 | CFG_CHK_APP (ToolCfgGetNextLine (&pActLine, &LineLen)) |
| 2090 | } |
| 2091 | CFG_CHK_APP (ToolCfgFreeContextBuffers()) |
| 2092 | CFG_CHK_APP (rc) |
| 2093 | |
| 2094 | return NO_ERROR; |
| 2095 | } |
| 2096 | |
| 2097 | static APIRET ToolCfgScanSource (t_pcchar pFileName, |
| 2098 | t_pcchar pSectionName, |
| 2099 | t_pToolCfgParamDesc pParamDescArray, |
| 2100 | t_pToolCfgTableDesc pTableDescArray) |
| 2101 | { |
| 2102 | APIRET rc; |
| 2103 | t_int ContextStackSize; |
| 2104 | t_int i; |
| 2105 | |
| 2106 | ContextStackSize = ToolCfgLocal.MaxIncludeNestingLevel * (t_int) sizeof (t_ToolCfgContext); |
| 2107 | CFG_CHK_APP (ToolCfgMemAlloc ((void **)&ToolCfgLocal.pCfgContextStack, (t_uint)ContextStackSize, MEM_OPT_NONE, MemIdCfgContextStack)) |
| 2108 | ToolCfgLocal.pActCfgContext = &ToolCfgLocal.pCfgContextStack[0]; |
| 2109 | ToolCfgLocal.IncludeNestingLevel = 0; |
| 2110 | |
| 2111 | /* initialise first context stack entry */ |
| 2112 | ToolCfgLocal.pActCfgContext->BufferLen = 0; |
| 2113 | ToolCfgLocal.pActCfgContext->ActLineNr = 0; |
| 2114 | ToolCfgLocal.pActCfgContext->pActLine = nullptr; |
| 2115 | ToolCfgLocal.pActCfgContext->pBuffer = nullptr; |
| 2116 | ToolCfgLocal.pActCfgContext->SectionNesting = 0; |
| 2117 | ToolCfgLocal.pActCfgContext->State = CFGSTATE_INCLUDENEWFILE; |
| 2118 | CFG_CHK_APP (ToolCfgCopyName (pFileName , & ToolCfgLocal.pActCfgContext->FileName [0] , MAX_FILENAME_LEN )) |
| 2119 | CFG_CHK_APP (ToolCfgCopyName (pSectionName, &(ToolCfgLocal.pActCfgContext->SectionNameArr[0][0]), MAX_SECTIONNAME_LEN)) |
| 2120 | for (i=1; i<MAX_SECTIONNAMES; i++) |
| 2121 | ToolCfgLocal.pActCfgContext->SectionNameArr[i][0] = '\0'; |
| 2122 | |
| 2123 | rc = ToolCfgScanParams (pParamDescArray, pTableDescArray); |
| 2124 | CFG_CHK_APP (ToolCfgMemFree (ToolCfgLocal.pCfgContextStack, MemIdCfgContextStack)) |
| 2125 | CFG_CHK_APP (rc) |
| 2126 | |
| 2127 | return NO_ERROR; |
| 2128 | } |
| 2129 | |
| 2130 | /* CfgCheckInitialisation: Check whether all parameters have been initialized. */ |
| 2131 | static APIRET ToolCfgCheckInitialisation(t_pToolCfgParamDesc pParamDescArray) |
| 2132 | { |
| 2133 | t_int i, Err; |
| 2134 | t_pToolCfgParamDesc pCfgParamDesc; |
| 2135 | t_pToolCfgDataDesc pCfgDataDesc; |
| 2136 | |
| 2137 | Err = FALSE; |
| 2138 | for (i=0; ;i++) |
| 2139 | { |
| 2140 | pCfgParamDesc = &pParamDescArray[i]; |
| 2141 | pCfgDataDesc = &pCfgParamDesc->DataDesc; |
| 2142 | if (pCfgDataDesc->pName == nullptr) |
| 2143 | break; /* end of array reached */ |
| 2144 | if (( pCfgParamDesc->CfgAssignments == 0 ) && |
| 2145 | ( pCfgParamDesc->CmdAssignments == 0 ) && |
| 2146 | ( pCfgParamDesc->DataDesc.CfgType != CFGTYPE_NULL ) && |
| 2147 | ( pCfgParamDesc->DataDesc.CfgType != CFGTYPE_PRESENCE) && // Parameters of type 'presence' are always optional |
| 2148 | ((pCfgParamDesc->Assign & CFGASN_OPTIONAL) == 0 )) |
| 2149 | { |
| 2150 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "Configuration error: Parameter %s has not been initialized.", pCfgDataDesc->pName)) |
| 2151 | Err = TRUE; |
| 2152 | } |
| 2153 | } |
| 2154 | if (Err) |
| 2155 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 2156 | |
| 2157 | return NO_ERROR; |
| 2158 | } |
| 2159 | |
| 2160 | static APIRET ToolCfgLogGetSetName (t_pToolCfgSet pSetArray, t_int SetValue, t_pcchar *ppSetString) |
| 2161 | { |
| 2162 | t_int i; |
| 2163 | |
| 2164 | *ppSetString = nullptr; |
| 2165 | for (i=0; ; i++) |
| 2166 | { |
| 2167 | if (pSetArray[i].pSetString == nullptr) |
| 2168 | break; /* end of array */ |
| 2169 | if (pSetArray[i].SetValue == SetValue) |
| 2170 | { |
| 2171 | *ppSetString = pSetArray[i].pSetString; |
| 2172 | break; |
| 2173 | } |
| 2174 | } |
| 2175 | |
| 2176 | return NO_ERROR; |
| 2177 | } |
| 2178 | |
| 2179 | static APIRET ToolCfgPrintContents (t_pchar pBuff, t_pToolCfgParamDesc pCfgParamDesc) |
| 2180 | { |
| 2181 | t_pToolCfgDataDesc pCfgDataDesc; |
| 2182 | t_pchar pSetString; |
| 2183 | t_int Hms; |
| 2184 | t_int SetValue; |
| 2185 | |
| 2186 | if ((pCfgParamDesc->CfgAssignments == 0) && |
| 2187 | (pCfgParamDesc->CmdAssignments == 0)) |
| 2188 | { |
| 2189 | sprintf (pBuff, "not initialized"); |
| 2190 | } |
| 2191 | else |
| 2192 | { |
| 2193 | pCfgDataDesc = &pCfgParamDesc->DataDesc; |
| 2194 | switch (pCfgDataDesc->CfgType) |
| 2195 | { |
| 2196 | case CFGTYPE_PRESENCE:sprintf (pBuff, "%s" , *(int *)pCfgDataDesc->DestAddr ? "present" : "not present"); break; |
| 2197 | case CFGTYPE_INTEGER: sprintf (pBuff, "%d (0x%X)", *(int *)pCfgDataDesc->DestAddr, *(int *)pCfgDataDesc->DestAddr); break; |
| 2198 | case CFGTYPE_DOUBLE: sprintf (pBuff, "%G" , *(double*)pCfgDataDesc->DestAddr); break; |
| 2199 | case CFGTYPE_STRING: sprintf (pBuff, "%s" , (char *)pCfgDataDesc->DestAddr); break; |
| 2200 | |
| 2201 | case CFGTYPE_SET: SetValue = *(int *)pCfgDataDesc->DestAddr; |
| 2202 | CFG_CHK_APP (ToolCfgLogGetSetName (pCfgDataDesc->pSetArray, SetValue, (const char **) &pSetString)) |
| 2203 | if (pSetString == nullptr) |
| 2204 | sprintf (pBuff, "Set str unknown for value %d", SetValue); |
| 2205 | else sprintf (pBuff, "%s", pSetString); |
| 2206 | break; |
| 2207 | case CFGTYPE_HMS: Hms = (*(int *)pCfgDataDesc->DestAddr); |
| 2208 | sprintf (pBuff, "%02d:%02d:%02d", Hms/3600, (Hms/60)%60, Hms%60); |
| 2209 | break; |
| 2210 | case CFGTYPE_NULL: break; |
| 2211 | default: |
| 2212 | return TOOLCFG_ERROR_INVALID_CFGTYPE; |
| 2213 | } |
| 2214 | } |
| 2215 | return NO_ERROR; |
| 2216 | } |
| 2217 | |
| 2218 | APIRET ToolCfgPrintParamContents (t_pToolCfgParamDesc pParamDescArray, t_pchar pName, t_pchar pBuff) |
| 2219 | { |
| 2220 | t_pToolCfgParamDesc pCfgParamDesc; |
| 2221 | t_pToolCfgDataDesc pCfgDataDesc; |
| 2222 | t_int i; |
| 2223 | |
| 2224 | if (pBuff) |
| 2225 | pBuff[0] = '\0'; |
| 2226 | for (i=0; ;i++) |
| 2227 | { |
| 2228 | pCfgParamDesc = &pParamDescArray[i]; |
| 2229 | pCfgDataDesc = &pCfgParamDesc->DataDesc; |
| 2230 | if (pCfgDataDesc->pName == nullptr) |
| 2231 | return TOOLCFG_ERROR_UNKNOWN_PARAMETER; |
| 2232 | if (ToolCfgStrCmpNoCase (pCfgDataDesc->pName, pName) == 0) |
| 2233 | { |
| 2234 | if (pBuff) |
| 2235 | CFG_CHK_APP (ToolCfgPrintContents (pBuff, pCfgParamDesc)) |
| 2236 | break; |
| 2237 | } |
| 2238 | } |
| 2239 | |
| 2240 | return NO_ERROR; |
| 2241 | } |
| 2242 | |
| 2243 | /* ToolCfgLogConfiguration: Write all the configuration parameters with their values to the log file */ |
| 2244 | APIRET ToolCfgLogConfiguration (t_pToolCfgParamDesc pParamDescArray) |
| 2245 | { |
| 2246 | t_pToolCfgParamDesc pCfgParamDesc; |
| 2247 | t_pToolCfgDataDesc pCfgDataDesc; |
| 2248 | t_pchar pBuff; |
| 2249 | t_int i, Err; |
| 2250 | t_int wr; |
| 2251 | |
| 2252 | CFG_CHK_APP (ToolCfgMemAlloc ((void **)&pBuff, 4096, MEM_OPT_NONE, MemIdCfgLogConfiguration)) |
| 2253 | Err = FALSE; |
| 2254 | for (i=0; ;i++) |
| 2255 | { |
| 2256 | pCfgParamDesc = &pParamDescArray[i]; |
| 2257 | pCfgDataDesc = &pCfgParamDesc->DataDesc; |
| 2258 | if (pCfgDataDesc->pName == nullptr) |
| 2259 | break; /* end of array reached */ |
| 2260 | if (( pCfgParamDesc->CfgAssignments == 0 ) && |
| 2261 | ( pCfgParamDesc->CmdAssignments == 0 ) && |
| 2262 | ( pCfgParamDesc->DataDesc.CfgType != CFGTYPE_NULL ) && |
| 2263 | ( pCfgParamDesc->DataDesc.CfgType != CFGTYPE_PRESENCE) && // Parameters of type 'presence' are always optional |
| 2264 | ((pCfgParamDesc->Assign & CFGASN_OPTIONAL) == 0 )) |
| 2265 | { |
| 2266 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "Parameter %s has not been initialized.", pCfgDataDesc->pName)) |
| 2267 | Err = TRUE; |
| 2268 | } |
| 2269 | else |
| 2270 | { |
| 2271 | wr = sprintf (pBuff, "%s = ", pCfgDataDesc->pName); |
| 2272 | CFG_CHK_APP (ToolCfgPrintContents (&pBuff[wr], pCfgParamDesc)) |
| 2273 | CFG_CHK_APP (ToolCfgLogEntry(__FFL__, "%s", pBuff)) |
| 2274 | } |
| 2275 | } |
| 2276 | CFG_CHK_APP (ToolCfgMemFree (pBuff, MemIdCfgLogConfiguration)) |
| 2277 | if (Err) |
| 2278 | return TOOLCFG_ERROR_CONFIG_ERROR; |
| 2279 | |
| 2280 | return NO_ERROR; |
| 2281 | } |
| 2282 | |
| 2283 | /* ToolCfgResetAssignCounters: Reset all assignment counters for all keywords */ |
| 2284 | static APIRET ToolCfgResetAssignCounters (t_pToolCfgParamDesc pParamDescArray) |
| 2285 | { |
| 2286 | t_int i; |
| 2287 | |
| 2288 | for (i=0; ; i++) |
| 2289 | { |
| 2290 | if (pParamDescArray[i].DataDesc.pName == nullptr) |
| 2291 | break; /* end of array reached */ |
| 2292 | pParamDescArray[i].CmdAssignments = 0; |
| 2293 | pParamDescArray[i].CfgAssignments = 0; |
| 2294 | } |
| 2295 | |
| 2296 | return NO_ERROR; |
| 2297 | } |
| 2298 | |
| 2299 | /* CfgGetCmdLineOption: Read an option from the command line. This function is */ |
| 2300 | /* usually called to get the configuration or log file name. */ |
| 2301 | APIRET ToolCfgGetCmdLineOption (t_pcchar pParamName, t_pcchar *ppValue) |
| 2302 | { |
| 2303 | t_ToolCfgContext CfgContext; |
| 2304 | t_int i, Eq; |
| 2305 | t_int LineLen = 0; |
| 2306 | t_pchar pNextChar = nullptr; |
| 2307 | |
| 2308 | *ppValue = nullptr; |
| 2309 | Eq = 0; |
| 2310 | for (i=1; i<ToolCfgLocal.argc; i++) |
| 2311 | { |
| 2312 | LineLen = (t_int) strlen (ToolCfgLocal.argv[i]); |
| 2313 | CFG_CHK_APP (ToolCfgCompareKeyWord (pParamName, ToolCfgLocal.argv[i], LineLen, &Eq, &pNextChar)) |
| 2314 | if (Eq) |
| 2315 | break; |
| 2316 | } |
| 2317 | if (!Eq) |
| 2318 | return TOOLCFG_ERROR_CMDLINE_OPTION_NOT_FOUND; |
| 2319 | CfgContext.FileName[0] = '\0'; // build pseudo context, to have CfgStdErrorHeader |
| 2320 | CfgContext.pActLine = ToolCfgLocal.argv[i]; // (called by ToolCfgCheckEqualSign) work correctly |
| 2321 | CfgContext.ActLineLen = LineLen; |
| 2322 | CfgContext.ActLineNr = i; |
| 2323 | ToolCfgLocal.pActCfgContext= &CfgContext; /*lint !e789 */ /* Info 789: Assigning address of auto (CfgContext) to static */ |
| 2324 | CFG_CHK_APP (ToolCfgCheckEqualSign (pNextChar, ppValue)) |
| 2325 | |
| 2326 | return NO_ERROR; |
| 2327 | } |
| 2328 | |
| 2329 | /* CfgScanConfiguration: API function to scan the configuration. Two subcalls */ |
| 2330 | /* have to be done: Scan the command line and scan the configuration file. */ |
| 2331 | APIRET ToolCfgScanConfiguration (t_pcchar pFileName, t_pcchar pSectionName, |
| 2332 | t_ToolCfgParamDesc *pParamDescArray, |
| 2333 | t_ToolCfgTableDesc *pTableDescArray) |
| 2334 | { |
| 2335 | CFG_CHK_APP (ToolCfgResetAssignCounters (pParamDescArray)) |
| 2336 | ToolCfgLocal.Priority = PRIORITY_HIGH; /* we will now scan the command line params which have high priority */ |
| 2337 | CFG_CHK_APP (ToolCfgScanSource (nullptr, pSectionName, pParamDescArray, pTableDescArray)) |
| 2338 | if (pFileName != nullptr) |
| 2339 | { |
| 2340 | ToolCfgLocal.Priority = PRIORITY_LOW; /* cfg file params with low priority */ |
| 2341 | CFG_CHK_APP (ToolCfgScanSource (pFileName, pSectionName, pParamDescArray, pTableDescArray)) |
| 2342 | } |
| 2343 | CFG_CHK_APP (ToolCfgCheckInitialisation (pParamDescArray)) |
| 2344 | |
| 2345 | return NO_ERROR; |
| 2346 | } |
| 2347 | |
| 2348 | /* ----------------------------------- */ |
| 2349 | /* Build configuration file template */ |
| 2350 | /* ----------------------------------- */ |
| 2351 | |
| 2352 | static APIRET ToolCfgEnterRemark (FILE *pFile, t_pcchar pRemark, t_int Indent, t_int LineFlag) |
| 2353 | { |
| 2354 | t_int rcp; |
| 2355 | t_int LineLen; |
| 2356 | t_int i; |
| 2357 | |
| 2358 | rcp = fprintf (pFile, "\r\n%*s%c %s", Indent, "", KEYCHAR_REMARK, pRemark); |
| 2359 | if ((rcp > 0) && (LineFlag)) |
| 2360 | { |
| 2361 | LineLen = (t_int) strlen (pRemark); |
| 2362 | rcp = fprintf (pFile, "\r\n%*s%c ", Indent, "", KEYCHAR_REMARK); |
| 2363 | CFG_CHK_FPRINTF(rcp) |
| 2364 | for (i=0; i<LineLen; i++) |
| 2365 | { |
| 2366 | rcp = fprintf (pFile, "-"); |
| 2367 | CFG_CHK_FPRINTF(rcp) |
| 2368 | } |
| 2369 | } |
| 2370 | |
| 2371 | return NO_ERROR; |
| 2372 | } |
| 2373 | |
| 2374 | static APIRET ToolCfgEnterEmptyLine (FILE *pFile) |
| 2375 | { |
| 2376 | CFG_CHK_FPRINTF(fprintf (pFile, "\r\n")) |
| 2377 | |
| 2378 | return NO_ERROR; |
| 2379 | } |
| 2380 | |
| 2381 | static APIRET ToolCfgBuildTemplate0 (FILE *pFile, t_int CfgParams, t_pint pEntries, |
| 2382 | t_pToolCfgParamDesc pParamDescArray, |
| 2383 | t_pToolCfgTableDesc pTableDescArray) |
| 2384 | { |
| 2385 | t_pToolCfgDataDesc pCfgDataDesc; |
| 2386 | t_pToolCfgTableDesc pCfgTableDesc; |
| 2387 | t_pchar pTmp1, pTmp2; |
| 2388 | t_int i, Len; |
| 2389 | t_ToolCfgAssignment Source; |
| 2390 | |
| 2391 | if (pParamDescArray) |
| 2392 | { |
| 2393 | *pEntries = 0; |
| 2394 | for (i=0; ; i++) |
| 2395 | { |
| 2396 | pCfgDataDesc = &pParamDescArray[i].DataDesc; |
| 2397 | if (pCfgDataDesc->pName == nullptr) |
| 2398 | break; /* end of array reached */ |
| 2399 | |
| 2400 | Source = (t_ToolCfgAssignment) (pParamDescArray[i].Assign & CFGASN_SOURCE); |
| 2401 | |
| 2402 | if (((Source & CFGASN_CMD) && !(Source & CFGASN_CFG) && (CfgParams == FALSE)) || |
| 2403 | ((Source & CFGASN_CFG) && (CfgParams == TRUE ))) |
| 2404 | { |
| 2405 | CFG_CHK_APP (ToolCfgBuildHelp (pCfgDataDesc, nullptr, &Len)) |
| 2406 | CFG_CHK_APP (ToolCfgMemAlloc ((void **)&pTmp1, (t_uint)Len, MEM_OPT_NONE, MemIdCfgHelpBuff)) |
| 2407 | CFG_CHK_APP (ToolCfgBuildHelp (pCfgDataDesc, pTmp1, &Len)) |
| 2408 | CFG_CHK_FPRINTF (fprintf (pFile, "\r\n %s", pTmp1)) |
| 2409 | CFG_CHK_APP (ToolCfgMemFree (pTmp1, MemIdCfgHelpBuff)) |
| 2410 | (*pEntries)++; |
| 2411 | } |
| 2412 | } |
| 2413 | } |
| 2414 | if (CfgParams && pTableDescArray) |
| 2415 | { |
| 2416 | for (i=0; ; i++) |
| 2417 | { |
| 2418 | pCfgTableDesc = &pTableDescArray[i]; |
| 2419 | if (pCfgTableDesc->pTableType == nullptr) |
| 2420 | break; /* end of array reached */ |
| 2421 | CFG_CHK_APP (ToolCfgBuildTableHelp (pCfgTableDesc, nullptr, nullptr, &Len)) |
| 2422 | CFG_CHK_APP (ToolCfgMemAlloc ((void **)&pTmp1, (t_uint)Len, MEM_OPT_NONE, MemIdCfgHelpBuff)) |
| 2423 | CFG_CHK_APP (ToolCfgMemAlloc ((void **)&pTmp2, (t_uint)Len, MEM_OPT_NONE, MemIdCfgHelpBuff)) |
| 2424 | CFG_CHK_APP (ToolCfgBuildTableHelp (pCfgTableDesc, pTmp1, pTmp2, &Len)) |
| 2425 | CFG_CHK_FPRINTF (fprintf (pFile, "\r\n\r\n %s %s <TableName>", KEYWORD_TABLESTART, pCfgTableDesc->pTableType)) |
| 2426 | CFG_CHK_APP (ToolCfgEnterRemark (pFile, pTmp1, 6, TRUE )) |
| 2427 | CFG_CHK_APP (ToolCfgEnterRemark (pFile, pTmp2, 6, FALSE)) |
| 2428 | CFG_CHK_FPRINTF (fprintf (pFile, "\r\n %s", KEYWORD_TABLEEND)) |
| 2429 | CFG_CHK_APP (ToolCfgMemFree (pTmp1, MemIdCfgHelpBuff)) |
| 2430 | CFG_CHK_APP (ToolCfgMemFree (pTmp2, MemIdCfgHelpBuff)) |
| 2431 | (*pEntries)++; |
| 2432 | } |
| 2433 | } |
| 2434 | return NO_ERROR; |
| 2435 | } |
| 2436 | |
| 2437 | APIRET ToolCfgBuildTemplate (t_pcchar pFileName, t_pcchar pComment, |
| 2438 | t_pcchar pSectionName, |
| 2439 | t_ToolCfgParamDesc *pParamDescArray, |
| 2440 | t_ToolCfgTableDesc *pTableDescArray) |
| 2441 | { |
| 2442 | FILE *pFile; |
| 2443 | t_int Entries, rc; |
| 2444 | t_pchar pTmp; |
| 2445 | const t_int TmpLen = 256; |
| 2446 | struct tm *NowTM; |
| 2447 | time_t NowT; |
| 2448 | |
| 2449 | pFile = fopen(pFileName, "wb"); |
| 2450 | if (pFile == nullptr) |
| 2451 | return TOOLCFG_ERROR_TEMPLATE_OPEN_FAILED; |
| 2452 | setbuf (pFile, nullptr); |
| 2453 | |
| 2454 | CFG_CHK_APP (ToolCfgMemAlloc ((void **)&pTmp, TmpLen, MEM_OPT_NONE, MemIdCfgTemplateHeader)) |
| 2455 | (void) time (&NowT); |
| 2456 | NowTM = localtime (&NowT); |
| 2457 | rc = sprintf (pTmp, "Configuration file template, created on "); |
| 2458 | (void) strftime (pTmp+rc, (size_t)(TmpLen-rc), "%d/%m/%Y %H:%M:%S", NowTM); |
| 2459 | CFG_CHK_APP (ToolCfgEnterRemark (pFile, pTmp, 0, TRUE)) |
| 2460 | CFG_CHK_APP (ToolCfgMemFree (pTmp, MemIdCfgTemplateHeader)) |
| 2461 | |
| 2462 | if(pComment) |
| 2463 | CFG_CHK_APP (ToolCfgEnterRemark (pFile, pComment, 0, TRUE)) |
| 2464 | |
| 2465 | if (pSectionName) |
| 2466 | CFG_CHK_FPRINTF (fprintf (pFile, "\r\n\r\n%s %s\r\n", KEYWORD_SECTIONSTART, pSectionName)) |
| 2467 | CFG_CHK_APP (ToolCfgBuildTemplate0 (pFile, TRUE, &Entries, pParamDescArray, pTableDescArray)) |
| 2468 | if (pSectionName) |
| 2469 | CFG_CHK_FPRINTF (fprintf (pFile, "\r\n\r\n%s", KEYWORD_SECTIONEND)) |
| 2470 | |
| 2471 | CFG_CHK_APP (ToolCfgEnterEmptyLine (pFile)) |
| 2472 | CFG_CHK_APP (ToolCfgEnterRemark (pFile, "Options, that can only be entered on command line", 0, TRUE)) |
| 2473 | CFG_CHK_APP (ToolCfgBuildTemplate0 (pFile, FALSE, &Entries, pParamDescArray, pTableDescArray)) |
| 2474 | if (Entries == 0) |
| 2475 | CFG_CHK_APP (ToolCfgEnterRemark (pFile, "There are no such options", 0, FALSE)) |
| 2476 | CFG_CHK_APP (ToolCfgEnterEmptyLine (pFile)) |
| 2477 | CFG_CHK_APP (ToolCfgEnterEmptyLine (pFile)) |
| 2478 | |
| 2479 | rc = fclose(pFile); |
| 2480 | if (rc) |
| 2481 | return TOOLCFG_ERROR_CLOSE_FAILED; |
| 2482 | |
| 2483 | return NO_ERROR; |
| 2484 | } |
| 2485 | |
| 2486 | /* ToolCfgSetErrLogFn: The application can specify the desired logging */ |
| 2487 | /* function here (for example vprintf, or an own function). */ |
| 2488 | APIRET ToolCfgSetLogFn (t_pToolCfgUserLogFn pUserLogFn) |
| 2489 | { |
| 2490 | ToolCfgLocal.pUserLogFn = pUserLogFn; |
| 2491 | |
| 2492 | return NO_ERROR; |
| 2493 | } |
| 2494 | |
| 2495 | APIRET ToolCfgGetSetString (t_pToolCfgSet pSetArray, int SetValue, const char **ppSetString) |
| 2496 | { |
| 2497 | t_pToolCfgSet pSet; |
| 2498 | |
| 2499 | *ppSetString = nullptr; |
| 2500 | for (pSet = &pSetArray[0]; pSet->pSetString != nullptr; pSet++) |
| 2501 | { |
| 2502 | if (pSet->SetValue == SetValue) |
| 2503 | { |
| 2504 | *ppSetString = pSet->pSetString; |
| 2505 | break; |
| 2506 | } |
| 2507 | } |
| 2508 | |
| 2509 | return NO_ERROR; |
| 2510 | } |
| 2511 | |
| 2512 | /* ------------------------------ */ |
| 2513 | /* Module initialisation */ |
| 2514 | /* ------------------------------ */ |
| 2515 | static int IsInit = 0; |
| 2516 | |
| 2517 | APIRET ToolCfgInit (int argc, char *argv[]) |
| 2518 | { |
| 2519 | if(IsInit == 1) |
| 2520 | return NO_ERROR; |
| 2521 | IsInit = 1; |
| 2522 | |
| 2523 | CFG_CHK_APP (TOOL_ERROR_REGISTER_CODE (ERROR_BASE_TOOL_CFG )) |
| 2524 | CFG_CHK_APP (TOOL_ERROR_REGISTER_CODE (TOOLCFG_ERROR_OPEN_FAILED )) |
| 2525 | CFG_CHK_APP (TOOL_ERROR_REGISTER_CODE (TOOLCFG_ERROR_READ_FAILED )) |
| 2526 | CFG_CHK_APP (TOOL_ERROR_REGISTER_CODE (TOOLCFG_ERROR_CLOSE_FAILED )) |
| 2527 | CFG_CHK_APP (TOOL_ERROR_REGISTER_CODE (TOOLCFG_ERROR_SEEKEND_FAILED )) |
| 2528 | CFG_CHK_APP (TOOL_ERROR_REGISTER_CODE (TOOLCFG_ERROR_SEEKSET_FAILED )) |
| 2529 | CFG_CHK_APP (TOOL_ERROR_REGISTER_CODE (TOOLCFG_ERROR_INVALID_STATE )) |
| 2530 | CFG_CHK_APP (TOOL_ERROR_REGISTER_CODE (TOOLCFG_ERROR_NAME_TOO_LONG )) |
| 2531 | CFG_CHK_APP (TOOL_ERROR_REGISTER_CODE (TOOLCFG_ERROR_INCLUDE_NESTING_OVERFLOW )) |
| 2532 | CFG_CHK_APP (TOOL_ERROR_REGISTER_CODE (TOOLCFG_ERROR_CONFIG_ERROR )) |
| 2533 | CFG_CHK_APP (TOOL_ERROR_REGISTER_CODE (TOOLCFG_ERROR_INVALID_ASSIGNMENT_OPTION)) |
| 2534 | CFG_CHK_APP (TOOL_ERROR_REGISTER_CODE (TOOLCFG_ERROR_INVALID_CFGTYPE )) |
| 2535 | CFG_CHK_APP (TOOL_ERROR_REGISTER_CODE (TOOLCFG_ERROR_TEMPLATE_OPEN_FAILED )) |
| 2536 | CFG_CHK_APP (TOOL_ERROR_REGISTER_CODE (TOOLCFG_ERROR_FPRINTF_FAILED )) |
| 2537 | CFG_CHK_APP (TOOL_ERROR_REGISTER_CODE (TOOLCFG_ERROR_CMDLINE_OPTION_NOT_FOUND )) |
| 2538 | CFG_CHK_APP (TOOL_ERROR_REGISTER_CODE (TOOLCFG_ERROR_MALLOC_FAILED )) |
| 2539 | CFG_CHK_APP (TOOL_ERROR_REGISTER_CODE (TOOLCFG_ERROR_SECTIONNAME_TOO_LONG )) |
| 2540 | CFG_CHK_APP (TOOL_ERROR_REGISTER_CODE (TOOLCFG_ERROR_TOO_MANY_SECTIONNAMES )) |
| 2541 | CFG_CHK_APP (TOOL_ERROR_REGISTER_CODE (TOOLCFG_ERROR_SECTIONNAME_NOTFOUND )) |
| 2542 | CFG_CHK_APP (TOOL_ERROR_REGISTER_CODE (TOOLCFG_ERROR_UNKNOWN_PARAMETER )) |
| 2543 | CFG_CHK_APP (TOOL_ERROR_REGISTER_CODE (TOOLCFG_ERROR_UNEXPECTED_EOL )) |
| 2544 | |
| 2545 | CFG_CHK_APP (MEM_REGISTER_MEMID (MemIdCfgBuffer )) |
| 2546 | CFG_CHK_APP (MEM_REGISTER_MEMID (MemIdCfgErrorHeader )) |
| 2547 | CFG_CHK_APP (MEM_REGISTER_MEMID (MemIdCfgContextStack )) |
| 2548 | CFG_CHK_APP (MEM_REGISTER_MEMID (MemIdCfgTemplateHeader )) |
| 2549 | CFG_CHK_APP (MEM_REGISTER_MEMID (MemIdCfgHelpBuff )) |
| 2550 | CFG_CHK_APP (MEM_REGISTER_MEMID (MemIdCfgHelpString )) |
| 2551 | CFG_CHK_APP (MEM_REGISTER_MEMID (MemIdCfgHelpInt )) |
| 2552 | CFG_CHK_APP (MEM_REGISTER_MEMID (MemIdCfgHelpTable )) |
| 2553 | CFG_CHK_APP (MEM_REGISTER_MEMID (MemIdCfgTableType )) |
| 2554 | CFG_CHK_APP (MEM_REGISTER_MEMID (MemIdCfgTableName )) |
| 2555 | CFG_CHK_APP (MEM_REGISTER_MEMID (MemIdCfgLogConfiguration)) |
| 2556 | |
| 2557 | ToolCfgLocal.argc = argc; |
| 2558 | ToolCfgLocal.argv = argv; |
| 2559 | ToolCfgLocal.pUserLogFn = nullptr; |
| 2560 | ToolCfgLocal.MaxIncludeNestingLevel = MAX_CFG_NESTING_LEVEL; |
| 2561 | memset (&ToolCfgLocal.GlobalSectionNameArr[0], 0, sizeof (ToolCfgLocal.GlobalSectionNameArr)); |
| 2562 | |
| 2563 | return NO_ERROR; |
| 2564 | } |
| 2565 | |
| 2566 | APIRET ToolCfgUseAdjustedCommandLine (int argc, char *argv[]) |
| 2567 | { |
| 2568 | ToolCfgLocal.argc = argc; |
| 2569 | ToolCfgLocal.argv = argv; |
| 2570 | return NO_ERROR; |
| 2571 | } |
| 2572 | |
| 2573 | APIRET ToolCfgDeInit (void) |
| 2574 | { |
| 2575 | if(IsInit == 0) |
| 2576 | return NO_ERROR; |
| 2577 | CFG_CHK_APP (ToolCfgFreeContextBuffers ()) |
| 2578 | IsInit = 0; |
| 2579 | return NO_ERROR; |
| 2580 | } |
| 2581 |