1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
|
/* $Id: VBoxAutostartCfg.cpp $ */
/** @file
* VBoxAutostart - VirtualBox Autostart service, configuration parser.
*/
/*
* Copyright (C) 2012-2020 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#include <iprt/ctype.h>
#include <iprt/err.h>
#include <iprt/mem.h>
#include <iprt/message.h>
#include <iprt/process.h>
#include <iprt/stream.h>
#include <iprt/string.h>
#include "VBoxAutostart.h"
/*********************************************************************************************************************************
* Constants And Macros, Structures and Typedefs *
*********************************************************************************************************************************/
/**
* Token type.
*/
typedef enum CFGTOKENTYPE
{
/** Invalid token type. */
CFGTOKENTYPE_INVALID = 0,
/** Identifier. */
CFGTOKENTYPE_ID,
/** Comma. */
CFGTOKENTYPE_COMMA,
/** Equal sign. */
CFGTOKENTYPE_EQUAL,
/** Open curly brackets. */
CFGTOKENTYPE_CURLY_OPEN,
/** Closing curly brackets. */
CFGTOKENTYPE_CURLY_CLOSING,
/** End of file. */
CFGTOKENTYPE_EOF,
/** 32bit hack. */
CFGTOKENTYPE_32BIT_HACK = 0x7fffffff
} CFGTOKENTYPE;
/** Pointer to a token type. */
typedef CFGTOKENTYPE *PCFGTOKENTYPE;
/** Pointer to a const token type. */
typedef const CFGTOKENTYPE *PCCFGTOKENTYPE;
/**
* A token.
*/
typedef struct CFGTOKEN
{
/** Type of the token. */
CFGTOKENTYPE enmType;
/** Line number of the token. */
unsigned iLine;
/** Starting character of the token in the stream. */
size_t cchStart;
/** Type dependen token data. */
union
{
/** Data for the ID type. */
struct
{
/** Size of the id in characters, excluding the \0 terminator. */
size_t cchToken;
/** Token data, variable size (given by cchToken member). */
char achToken[1];
} Id;
} u;
} CFGTOKEN;
/** Pointer to a token. */
typedef CFGTOKEN *PCFGTOKEN;
/** Pointer to a const token. */
typedef const CFGTOKEN *PCCFGTOKEN;
/**
* Tokenizer instance data for the config data.
*/
typedef struct CFGTOKENIZER
{
/** Config file handle. */
PRTSTREAM hStrmConfig;
/** String buffer for the current line we are operating in. */
char *pszLine;
/** Size of the string buffer. */
size_t cbLine;
/** Current position in the line. */
char *pszLineCurr;
/** Current line in the config file. */
unsigned iLine;
/** Current character of the line. */
size_t cchCurr;
/** Flag whether the end of the config stream is reached. */
bool fEof;
/** Pointer to the next token in the stream (used to peek). */
PCFGTOKEN pTokenNext;
} CFGTOKENIZER, *PCFGTOKENIZER;
/*********************************************************************************************************************************
* Internal Functions *
*********************************************************************************************************************************/
/**
* Free a config token.
*
* @returns nothing.
* @param pCfgTokenizer The config tokenizer.
* @param pToken The token to free.
*/
static void autostartConfigTokenFree(PCFGTOKENIZER pCfgTokenizer, PCFGTOKEN pToken)
{
NOREF(pCfgTokenizer);
RTMemFree(pToken);
}
/**
* Reads the next line from the config stream.
*
* @returns VBox status code.
* @param pCfgTokenizer The config tokenizer.
*/
static int autostartConfigTokenizerReadNextLine(PCFGTOKENIZER pCfgTokenizer)
{
int rc = VINF_SUCCESS;
if (pCfgTokenizer->fEof)
return VERR_EOF;
do
{
rc = RTStrmGetLine(pCfgTokenizer->hStrmConfig, pCfgTokenizer->pszLine,
pCfgTokenizer->cbLine);
if (rc == VERR_BUFFER_OVERFLOW)
{
char *pszTmp;
pCfgTokenizer->cbLine += 128;
pszTmp = (char *)RTMemRealloc(pCfgTokenizer->pszLine, pCfgTokenizer->cbLine);
if (pszTmp)
pCfgTokenizer->pszLine = pszTmp;
else
rc = VERR_NO_MEMORY;
}
} while (rc == VERR_BUFFER_OVERFLOW);
if ( RT_SUCCESS(rc)
|| rc == VERR_EOF)
{
pCfgTokenizer->iLine++;
pCfgTokenizer->cchCurr = 1;
pCfgTokenizer->pszLineCurr = pCfgTokenizer->pszLine;
if (rc == VERR_EOF)
pCfgTokenizer->fEof = true;
}
return rc;
}
/**
* Get the next token from the config stream and create a token structure.
*
* @returns VBox status code.
* @param pCfgTokenizer The config tokenizer data.
* @param pCfgTokenUse Allocated token structure to use or NULL to allocate
* a new one. It will bee freed if an error is encountered.
* @param ppCfgToken Where to store the pointer to the next token on success.
*/
static int autostartConfigTokenizerCreateToken(PCFGTOKENIZER pCfgTokenizer,
PCFGTOKEN pCfgTokenUse, PCFGTOKEN *ppCfgToken)
{
const char *pszToken = NULL;
size_t cchToken = 1;
size_t cchAdvance = 0;
CFGTOKENTYPE enmType = CFGTOKENTYPE_INVALID;
int rc = VINF_SUCCESS;
for (;;)
{
pszToken = pCfgTokenizer->pszLineCurr;
/* Skip all spaces. */
while (RT_C_IS_BLANK(*pszToken))
{
pszToken++;
cchAdvance++;
}
/* Check if we have to read a new line. */
if ( *pszToken == '\0'
|| *pszToken == '#')
{
rc = autostartConfigTokenizerReadNextLine(pCfgTokenizer);
if (rc == VERR_EOF)
{
enmType = CFGTOKENTYPE_EOF;
rc = VINF_SUCCESS;
break;
}
else if (RT_FAILURE(rc))
break;
/* start from the beginning. */
cchAdvance = 0;
}
else if (*pszToken == '=')
{
enmType = CFGTOKENTYPE_EQUAL;
break;
}
else if (*pszToken == ',')
{
enmType = CFGTOKENTYPE_COMMA;
break;
}
else if (*pszToken == '{')
{
enmType = CFGTOKENTYPE_CURLY_OPEN;
break;
}
else if (*pszToken == '}')
{
enmType = CFGTOKENTYPE_CURLY_CLOSING;
break;
}
else
{
const char *pszTmp = pszToken;
cchToken = 0;
enmType = CFGTOKENTYPE_ID;
/* Get the complete token. */
while ( RT_C_IS_ALNUM(*pszTmp)
|| *pszTmp == '_'
|| *pszTmp == '.')
{
pszTmp++;
cchToken++;
}
break;
}
}
Assert(RT_FAILURE(rc) || enmType != CFGTOKENTYPE_INVALID);
if (RT_SUCCESS(rc))
{
/* Free the given token if it is an ID or the current one is an ID token. */
if ( pCfgTokenUse
&& ( pCfgTokenUse->enmType == CFGTOKENTYPE_ID
|| enmType == CFGTOKENTYPE_ID))
{
autostartConfigTokenFree(pCfgTokenizer, pCfgTokenUse);
pCfgTokenUse = NULL;
}
if (!pCfgTokenUse)
{
size_t cbToken = sizeof(CFGTOKEN);
if (enmType == CFGTOKENTYPE_ID)
cbToken += (cchToken + 1) * sizeof(char);
pCfgTokenUse = (PCFGTOKEN)RTMemAllocZ(cbToken);
if (!pCfgTokenUse)
rc = VERR_NO_MEMORY;
}
if (RT_SUCCESS(rc))
{
/* Copy token data. */
pCfgTokenUse->enmType = enmType;
pCfgTokenUse->cchStart = pCfgTokenizer->cchCurr;
pCfgTokenUse->iLine = pCfgTokenizer->iLine;
if (enmType == CFGTOKENTYPE_ID)
{
pCfgTokenUse->u.Id.cchToken = cchToken;
memcpy(pCfgTokenUse->u.Id.achToken, pszToken, cchToken);
}
}
else if (pCfgTokenUse)
autostartConfigTokenFree(pCfgTokenizer, pCfgTokenUse);
if (RT_SUCCESS(rc))
{
/* Set new position in config stream. */
pCfgTokenizer->pszLineCurr += cchToken + cchAdvance;
pCfgTokenizer->cchCurr += cchToken + cchAdvance;
*ppCfgToken = pCfgTokenUse;
}
}
return rc;
}
/**
* Destroys the given config tokenizer.
*
* @returns nothing.
* @param pCfgTokenizer The config tokenizer to destroy.
*/
static void autostartConfigTokenizerDestroy(PCFGTOKENIZER pCfgTokenizer)
{
if (pCfgTokenizer->pszLine)
RTMemFree(pCfgTokenizer->pszLine);
if (pCfgTokenizer->hStrmConfig)
RTStrmClose(pCfgTokenizer->hStrmConfig);
if (pCfgTokenizer->pTokenNext)
RTMemFree(pCfgTokenizer->pTokenNext);
RTMemFree(pCfgTokenizer);
}
/**
* Creates the config tokenizer from the given filename.
*
* @returns VBox status code.
* @param pszFilename Config filename.
* @param ppCfgTokenizer Where to store the pointer to the config tokenizer on
* success.
*/
static int autostartConfigTokenizerCreate(const char *pszFilename, PCFGTOKENIZER *ppCfgTokenizer)
{
int rc = VINF_SUCCESS;
PCFGTOKENIZER pCfgTokenizer = (PCFGTOKENIZER)RTMemAllocZ(sizeof(CFGTOKENIZER));
if (pCfgTokenizer)
{
pCfgTokenizer->iLine = 0;
pCfgTokenizer->cbLine = 128;
pCfgTokenizer->pszLine = (char *)RTMemAllocZ(pCfgTokenizer->cbLine);
if (pCfgTokenizer->pszLine)
{
rc = RTStrmOpen(pszFilename, "r", &pCfgTokenizer->hStrmConfig);
if (RT_SUCCESS(rc))
{
rc = autostartConfigTokenizerReadNextLine(pCfgTokenizer);
if (RT_SUCCESS(rc))
rc = autostartConfigTokenizerCreateToken(pCfgTokenizer, NULL,
&pCfgTokenizer->pTokenNext);
}
}
else
rc = VERR_NO_MEMORY;
}
else
rc = VERR_NO_MEMORY;
if (RT_SUCCESS(rc))
*ppCfgTokenizer = pCfgTokenizer;
else if ( RT_FAILURE(rc)
&& pCfgTokenizer)
autostartConfigTokenizerDestroy(pCfgTokenizer);
return rc;
}
/**
* Return the next token from the config stream.
*
* @returns VBox status code.
* @param pCfgTokenizer The config tokenizer.
* @param ppCfgToken Where to store the next token.
*/
static int autostartConfigTokenizerGetNextToken(PCFGTOKENIZER pCfgTokenizer,
PCFGTOKEN *ppCfgToken)
{
*ppCfgToken = pCfgTokenizer->pTokenNext;
return autostartConfigTokenizerCreateToken(pCfgTokenizer, NULL, &pCfgTokenizer->pTokenNext);
}
/**
* Returns a stringified version of the token type.
*
* @returns Stringified version of the token type.
* @param enmType Token type.
*/
static const char *autostartConfigTokenTypeToStr(CFGTOKENTYPE enmType)
{
switch (enmType)
{
case CFGTOKENTYPE_COMMA:
return ",";
case CFGTOKENTYPE_EQUAL:
return "=";
case CFGTOKENTYPE_CURLY_OPEN:
return "{";
case CFGTOKENTYPE_CURLY_CLOSING:
return "}";
case CFGTOKENTYPE_EOF:
return "<EOF>";
case CFGTOKENTYPE_ID:
return "<Identifier>";
default:
AssertFailed();
return "<Invalid>";
}
/* not reached */
}
/**
* Returns a stringified version of the token.
*
* @returns Stringified version of the token type.
* @param pToken Token.
*/
static const char *autostartConfigTokenToString(PCFGTOKEN pToken)
{
if (pToken->enmType == CFGTOKENTYPE_ID)
return pToken->u.Id.achToken;
else
return autostartConfigTokenTypeToStr(pToken->enmType);
}
/**
* Returns the length of the token in characters (without zero terminator).
*
* @returns Token length.
* @param pToken Token.
*/
static size_t autostartConfigTokenGetLength(PCFGTOKEN pToken)
{
switch (pToken->enmType)
{
case CFGTOKENTYPE_COMMA:
case CFGTOKENTYPE_EQUAL:
case CFGTOKENTYPE_CURLY_OPEN:
case CFGTOKENTYPE_CURLY_CLOSING:
return 1;
case CFGTOKENTYPE_EOF:
return 0;
case CFGTOKENTYPE_ID:
return strlen(pToken->u.Id.achToken);
default:
AssertFailed();
return 0;
}
/* not reached */
}
/**
* Log unexpected token error.
*
* @returns nothing.
* @param pToken The token which caused the error.
* @param pszExpected String of the token which was expected.
*/
static void autostartConfigTokenizerMsgUnexpectedToken(PCFGTOKEN pToken, const char *pszExpected)
{
autostartSvcLogError("Unexpected token '%s' at %d:%d.%d, expected '%s'",
autostartConfigTokenToString(pToken),
pToken->iLine, pToken->cchStart,
pToken->cchStart + autostartConfigTokenGetLength(pToken) - 1, pszExpected);
}
/**
* Verfies a token and consumes it.
*
* @returns VBox status code.
* @param pCfgTokenizer The config tokenizer.
* @param pszTokenCheck The token to check for.
*/
static int autostartConfigTokenizerCheckAndConsume(PCFGTOKENIZER pCfgTokenizer, CFGTOKENTYPE enmType)
{
int rc = VINF_SUCCESS;
PCFGTOKEN pCfgToken = NULL;
rc = autostartConfigTokenizerGetNextToken(pCfgTokenizer, &pCfgToken);
if (RT_SUCCESS(rc))
{
if (pCfgToken->enmType != enmType)
{
autostartConfigTokenizerMsgUnexpectedToken(pCfgToken, autostartConfigTokenTypeToStr(enmType));
rc = VERR_INVALID_PARAMETER;
}
autostartConfigTokenFree(pCfgTokenizer, pCfgToken);
}
return rc;
}
/**
* Consumes the next token in the stream.
*
* @returns VBox status code.
* @param pCfgTokenizer Tokenizer instance data.
*/
static int autostartConfigTokenizerConsume(PCFGTOKENIZER pCfgTokenizer)
{
int rc = VINF_SUCCESS;
PCFGTOKEN pCfgToken = NULL;
rc = autostartConfigTokenizerGetNextToken(pCfgTokenizer, &pCfgToken);
if (RT_SUCCESS(rc))
autostartConfigTokenFree(pCfgTokenizer, pCfgToken);
return rc;
}
/**
* Returns the start of the next token without consuming it.
*
* @returns The next token without consuming it.
* @param pCfgTokenizer Tokenizer instance data.
*/
DECLINLINE(PCFGTOKEN) autostartConfigTokenizerPeek(PCFGTOKENIZER pCfgTokenizer)
{
return pCfgTokenizer->pTokenNext;
}
/**
* Check whether the next token is equal to the given one.
*
* @returns true if the next token in the stream is equal to the given one
* false otherwise.
* @param pszToken The token to check for.
*/
DECLINLINE(bool) autostartConfigTokenizerPeekIsEqual(PCFGTOKENIZER pCfgTokenizer, CFGTOKENTYPE enmType)
{
PCFGTOKEN pToken = autostartConfigTokenizerPeek(pCfgTokenizer);
return pToken->enmType == enmType;
}
/**
* Parse a key value node and returns the AST.
*
* @returns VBox status code.
* @param pCfgTokenizer The tokenizer for the config stream.
* @param pszKey The key for the pair.
* @param ppCfgAst Where to store the resulting AST on success.
*/
static int autostartConfigParseValue(PCFGTOKENIZER pCfgTokenizer, const char *pszKey,
PCFGAST *ppCfgAst)
{
int rc = VINF_SUCCESS;
PCFGTOKEN pToken = NULL;
rc = autostartConfigTokenizerGetNextToken(pCfgTokenizer, &pToken);
if ( RT_SUCCESS(rc)
&& pToken->enmType == CFGTOKENTYPE_ID)
{
PCFGAST pCfgAst = NULL;
pCfgAst = (PCFGAST)RTMemAllocZ(RT_UOFFSETOF_DYN(CFGAST, u.KeyValue.aszValue[pToken->u.Id.cchToken + 1]));
if (!pCfgAst)
return VERR_NO_MEMORY;
pCfgAst->enmType = CFGASTNODETYPE_KEYVALUE;
pCfgAst->pszKey = RTStrDup(pszKey);
if (!pCfgAst->pszKey)
{
RTMemFree(pCfgAst);
return VERR_NO_MEMORY;
}
memcpy(pCfgAst->u.KeyValue.aszValue, pToken->u.Id.achToken, pToken->u.Id.cchToken);
pCfgAst->u.KeyValue.cchValue = pToken->u.Id.cchToken;
*ppCfgAst = pCfgAst;
}
else
{
autostartConfigTokenizerMsgUnexpectedToken(pToken, "non reserved token");
rc = VERR_INVALID_PARAMETER;
}
return rc;
}
/**
* Parses a compound node constructing the AST and returning it on success.
*
* @returns VBox status code.
* @param pCfgTokenizer The tokenizer for the config stream.
* @param pszScopeId The scope ID of the compound node.
* @param ppCfgAst Where to store the resulting AST on success.
*/
static int autostartConfigParseCompoundNode(PCFGTOKENIZER pCfgTokenizer, const char *pszScopeId,
PCFGAST *ppCfgAst)
{
unsigned cAstNodesMax = 10;
PCFGAST pCfgAst = (PCFGAST)RTMemAllocZ(RT_UOFFSETOF_DYN(CFGAST, u.Compound.apAstNodes[cAstNodesMax]));
if (!pCfgAst)
return VERR_NO_MEMORY;
pCfgAst->enmType = CFGASTNODETYPE_COMPOUND;
pCfgAst->u.Compound.cAstNodes = 0;
pCfgAst->pszKey = RTStrDup(pszScopeId);
if (!pCfgAst->pszKey)
{
RTMemFree(pCfgAst);
return VERR_NO_MEMORY;
}
int rc = VINF_SUCCESS;
do
{
PCFGTOKEN pToken = NULL;
PCFGAST pAstNode = NULL;
if ( autostartConfigTokenizerPeekIsEqual(pCfgTokenizer, CFGTOKENTYPE_CURLY_CLOSING)
|| autostartConfigTokenizerPeekIsEqual(pCfgTokenizer, CFGTOKENTYPE_EOF))
break;
rc = autostartConfigTokenizerGetNextToken(pCfgTokenizer, &pToken);
if ( RT_SUCCESS(rc)
&& pToken->enmType == CFGTOKENTYPE_ID)
{
/* Next must be a = token in all cases at this place. */
rc = autostartConfigTokenizerCheckAndConsume(pCfgTokenizer, CFGTOKENTYPE_EQUAL);
if (RT_SUCCESS(rc))
{
/* Check whether this is a compound node. */
if (autostartConfigTokenizerPeekIsEqual(pCfgTokenizer, CFGTOKENTYPE_CURLY_OPEN))
{
rc = autostartConfigTokenizerConsume(pCfgTokenizer);
if (RT_SUCCESS(rc))
rc = autostartConfigParseCompoundNode(pCfgTokenizer, pToken->u.Id.achToken,
&pAstNode);
if (RT_SUCCESS(rc))
rc = autostartConfigTokenizerCheckAndConsume(pCfgTokenizer, CFGTOKENTYPE_CURLY_CLOSING);
}
else
rc = autostartConfigParseValue(pCfgTokenizer, pToken->u.Id.achToken,
&pAstNode);
}
}
else if (RT_SUCCESS(rc))
{
autostartConfigTokenizerMsgUnexpectedToken(pToken, "non reserved token");
rc = VERR_INVALID_PARAMETER;
}
/* Add to the current compound node. */
if (RT_SUCCESS(rc))
{
if (pCfgAst->u.Compound.cAstNodes >= cAstNodesMax)
{
cAstNodesMax += 10;
PCFGAST pCfgAstNew = (PCFGAST)RTMemRealloc(pCfgAst, RT_UOFFSETOF_DYN(CFGAST, u.Compound.apAstNodes[cAstNodesMax]));
if (!pCfgAstNew)
rc = VERR_NO_MEMORY;
else
pCfgAst = pCfgAstNew;
}
if (RT_SUCCESS(rc))
{
pCfgAst->u.Compound.apAstNodes[pCfgAst->u.Compound.cAstNodes] = pAstNode;
pCfgAst->u.Compound.cAstNodes++;
}
}
autostartConfigTokenFree(pCfgTokenizer, pToken);
} while (RT_SUCCESS(rc));
if (RT_SUCCESS(rc))
*ppCfgAst = pCfgAst;
else
autostartConfigAstDestroy(pCfgAst);
return rc;
}
DECLHIDDEN(int) autostartParseConfig(const char *pszFilename, PCFGAST *ppCfgAst)
{
PCFGTOKENIZER pCfgTokenizer = NULL;
int rc = VINF_SUCCESS;
PCFGAST pCfgAst = NULL;
AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
AssertPtrReturn(ppCfgAst, VERR_INVALID_POINTER);
rc = autostartConfigTokenizerCreate(pszFilename, &pCfgTokenizer);
if (RT_SUCCESS(rc))
{
rc = autostartConfigParseCompoundNode(pCfgTokenizer, "", &pCfgAst);
if (RT_SUCCESS(rc))
rc = autostartConfigTokenizerCheckAndConsume(pCfgTokenizer, CFGTOKENTYPE_EOF);
}
if (pCfgTokenizer)
autostartConfigTokenizerDestroy(pCfgTokenizer);
if (RT_SUCCESS(rc))
*ppCfgAst = pCfgAst;
return rc;
}
DECLHIDDEN(void) autostartConfigAstDestroy(PCFGAST pCfgAst)
{
AssertPtrReturnVoid(pCfgAst);
switch (pCfgAst->enmType)
{
case CFGASTNODETYPE_KEYVALUE:
{
RTMemFree(pCfgAst);
break;
}
case CFGASTNODETYPE_COMPOUND:
{
for (unsigned i = 0; i < pCfgAst->u.Compound.cAstNodes; i++)
autostartConfigAstDestroy(pCfgAst->u.Compound.apAstNodes[i]);
RTMemFree(pCfgAst);
break;
}
case CFGASTNODETYPE_LIST:
default:
AssertMsgFailed(("Invalid AST node type %d\n", pCfgAst->enmType));
}
}
DECLHIDDEN(PCFGAST) autostartConfigAstGetByName(PCFGAST pCfgAst, const char *pszName)
{
if (!pCfgAst)
return NULL;
AssertReturn(pCfgAst->enmType == CFGASTNODETYPE_COMPOUND, NULL);
for (unsigned i = 0; i < pCfgAst->u.Compound.cAstNodes; i++)
{
PCFGAST pNode = pCfgAst->u.Compound.apAstNodes[i];
if (!RTStrCmp(pNode->pszKey, pszName))
return pNode;
}
return NULL;
}
|