Repository navigation
Expand file tree
/
Copy pathQEQuery.c
More file actions
1449 lines (1248 loc) · 42.2 KB
/
Copy pathQEQuery.c
File metadata and controls
1449 lines (1248 loc) · 42.2 KB
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
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2006 Apple Computer, Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this
* file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
#include "QEQuery.h"
#include "kext_tools_util.h"
/*******************************************************************************
* The internal query engine object definition.
*******************************************************************************/
struct __QEQuery {
/* queryRoot: the outermost query element, always an OR group
*/
CFMutableDictionaryRef queryRoot;
/* queryStack, queryStackTop: a lifo stack of query elements representing
* open groups (the root is always considered open for appending).
*/
CFMutableArrayRef queryStack;
CFMutableDictionaryRef queryStackTop;
/* The number of explicit OR groups that are open.
*/
uint32_t nestingLevel;
/* Is an AND or OR operator pending?
*/
Boolean logicOpActive;
/* End evaluation as soon as truth value known? (on by default)
*/
Boolean shortCircuitEval;
/* The last error to happen with this query.
*/
QEQueryError lastError;
/* Client-defined synonyms to their underlying operator equivalents.
*/
CFMutableDictionaryRef synonyms;
/* Lookup operators by default token to client-defined token (which
* may be the default).
*/
CFMutableDictionaryRef operators;
/* parse, eval callbacks keyed by client-registered keyword.
*/
CFMutableDictionaryRef parseCallbacks;
CFMutableDictionaryRef evaluationCallbacks;
/* Client-defined data passed to all callbacks.
*/
void * userData;
};
/*******************************************************************************
* Internal query element keys for the basic operators and such.
********************************************************************************
* IMPORTANT NOTE ABOUT GROUPS
* "Or" groups are always explicit, except for the query root. "And" groups are
* always implicit, and are created as soon as an "and" operator is seen. The
* query engine promotes the element preceding the "and" into the new "and"
* group. When an "or" group is closed, several checks are made to close implicit
* "and" groups as well as any trivial groups of one element.
* See QEQueryEndGroup() and particularly _QEQueryPopGroupAndCoalesce() for
* the gory details.
*******************************************************************************/
#define kQEQueryKeyPredicate CFSTR("_QEQueryPredicate")
#define kQEQueryKeyArguments CFSTR("_QEQueryArguments")
#define kQEQueryKeyNegated CFSTR("_QEQueryNegated")
#define kQEQueryPredicateAnd CFSTR("_QEQueryAndGroup")
#define kQEQueryPredicateOr CFSTR("_QEQueryOrGroup")
#define kMsgStringConversionError "string conversion error"
/*******************************************************************************
* Internal function prototypes.
*******************************************************************************/
Boolean _QEQueryStartGroup(
QEQueryRef query,
Boolean andFlag,
Boolean negated);
CFMutableDictionaryRef _QEQueryCreateGroup(Boolean andFlag);
CF_RETURNS_RETAINED CFMutableDictionaryRef _QEQueryYankElement(QEQueryRef query);
Boolean _QEQueryStackTopIsAndGroup(QEQueryRef query);
Boolean _QEQueryStackTopIsOrGroup(QEQueryRef query);
Boolean _QEQueryStackTopHasCount(QEQueryRef query, CFIndex count);
Boolean _QEQueryPushGroup(QEQueryRef query, Boolean negated, Boolean andFlag);
Boolean _QEQueryPopGroup(QEQueryRef query);
Boolean _QEQueryPopGroupAndCoalesce(
QEQueryRef query,
Boolean onlyIfCanCoalesce);
CFStringRef _QEQueryOperatorForToken(
QEQueryRef query,
CFStringRef token);
CF_RETURNS_RETAINED CFStringRef _QEPredicateForString(
QEQueryRef query,
char * string);
QEQueryParseCallback _QEQueryParseCallbackForPredicate(
QEQueryRef query,
CFStringRef predicate);
QEQueryEvaluationCallback _QEQueryEvaluationCallbackForPredicate(
QEQueryRef query,
CFStringRef predicate);
Boolean _QEQueryElementIsNegated(CFDictionaryRef element);
void _QEQueryElementNegate(CFMutableDictionaryRef element);
#pragma mark Creation/Setup/Destruction
/*******************************************************************************
*
*******************************************************************************/
QEQueryRef
QEQueryCreate(void * userData)
{
struct __QEQuery * result = NULL; // returned
Boolean ok = false;
result = (QEQueryRef)malloc(sizeof(struct __QEQuery));
if (!result) {
goto finish;
}
bzero(result, sizeof(struct __QEQuery));
result->userData = userData;
result->queryRoot = _QEQueryCreateGroup(false /* andGroup */);
if (!result->queryRoot) {
goto finish;
}
result->queryStack = CFArrayCreateMutable(kCFAllocatorDefault,
0, &kCFTypeArrayCallBacks);
if (!result->queryStack) {
goto finish;
}
result->parseCallbacks = CFDictionaryCreateMutable(kCFAllocatorDefault,
0 /* no limit */, &kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
if (!result->parseCallbacks) {
goto finish;
}
result->operators = CFDictionaryCreateMutable(kCFAllocatorDefault,
0 /* no limit */, &kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
if (!result->operators) {
goto finish;
}
result->synonyms = CFDictionaryCreateMutable(kCFAllocatorDefault,
0 /* no limit */, &kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
if (!result->synonyms) {
goto finish;
}
result->evaluationCallbacks = CFDictionaryCreateMutable(kCFAllocatorDefault,
0 /* no limit */, &kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
if (!result->evaluationCallbacks) {
goto finish;
}
CFArrayAppendValue(result->queryStack, result->queryRoot);
result->queryStackTop = result->queryRoot;
result->nestingLevel = 0;
result->logicOpActive = false;
result->shortCircuitEval = true;
QEQuerySetOperators(result, NULL, NULL, NULL, NULL, NULL);
ok = true;
finish:
if (!ok && result) {
QEQueryFree(result);
result = NULL;
}
return result;
}
/*******************************************************************************
*
*******************************************************************************/
void
QEQueryFree(QEQueryRef query)
{
if (query->queryRoot) CFRelease(query->queryRoot);
if (query->queryStack) CFRelease(query->queryStack);
if (query->operators) CFRelease(query->operators);
if (query->parseCallbacks) CFRelease(query->parseCallbacks);
if (query->evaluationCallbacks) CFRelease(query->evaluationCallbacks);
if (query->synonyms) CFRelease(query->synonyms);
free(query);
return;
}
/*******************************************************************************
*
*******************************************************************************/
void
QEQueryEmptyParseDictionaries(QEQueryRef query)
{
CFDictionaryRemoveAllValues(query->parseCallbacks);
CFDictionaryRemoveAllValues(query->evaluationCallbacks);
CFDictionaryRemoveAllValues(query->synonyms);
return;
}
/*******************************************************************************
* QEQuerySetOperators() -- all operators must have a definition, so if NULL is
* passed for any, the basic token defined in the header file is assigned.
*******************************************************************************/
void
QEQuerySetOperators(QEQueryRef query,
CFStringRef andPredicate,
CFStringRef orPredicate,
CFStringRef notPredicate,
CFStringRef groupStartPredicate,
CFStringRef groupEndPredicate)
{
CFDictionarySetValue(query->operators, CFSTR(kQEQueryTokenAnd),
andPredicate ? andPredicate : CFSTR(kQEQueryTokenAnd));
CFDictionarySetValue(query->operators, CFSTR(kQEQueryTokenOr),
orPredicate ? orPredicate : CFSTR(kQEQueryTokenOr));
CFDictionarySetValue(query->operators, CFSTR(kQEQueryTokenNot),
notPredicate ? notPredicate : CFSTR(kQEQueryTokenNot));
CFDictionarySetValue(query->operators, CFSTR(kQEQueryTokenGroupStart),
groupStartPredicate ? groupStartPredicate : CFSTR(kQEQueryTokenGroupStart));
CFDictionarySetValue(query->operators, CFSTR(kQEQueryTokenGroupEnd),
groupEndPredicate ? groupEndPredicate : CFSTR(kQEQueryTokenGroupEnd));
return;
}
/*******************************************************************************
*
*******************************************************************************/
void
QEQuerySetParseCallbackForPredicate(
QEQueryRef query,
CFStringRef predicate,
QEQueryParseCallback parseCallback)
{
CFDataRef pCallback = NULL;
if (parseCallback) {
pCallback = CFDataCreate(kCFAllocatorDefault,
(void *)&parseCallback, sizeof(QEQueryParseCallback));
if (!pCallback) {
goto finish;
}
CFDictionarySetValue(query->parseCallbacks, predicate, pCallback);
} else {
CFDictionaryRemoveValue(query->parseCallbacks, predicate);
}
finish:
if (pCallback) CFRelease(pCallback);
return;
}
/*******************************************************************************
*
*******************************************************************************/
void
QEQuerySetEvaluationCallbackForPredicate(
QEQueryRef query,
CFStringRef predicate,
QEQueryEvaluationCallback evaluationCallback)
{
CFDataRef eCallback = NULL;
if (evaluationCallback) {
eCallback = CFDataCreate(kCFAllocatorDefault,
(void *)&evaluationCallback, sizeof(QEQueryEvaluationCallback));
if (!eCallback) {
goto finish;
}
CFDictionarySetValue(query->evaluationCallbacks, predicate, eCallback);
} else {
CFDictionaryRemoveValue(query->evaluationCallbacks, predicate);
}
finish:
if (eCallback) CFRelease(eCallback);
return;
}
/*******************************************************************************
*
*******************************************************************************/
void
QEQuerySetSynonymForPredicate(
QEQueryRef query,
CFStringRef synonym,
CFStringRef predicate)
{
if (predicate) {
CFDictionarySetValue(query->synonyms, synonym, predicate);
} else {
CFDictionaryRemoveValue(query->synonyms, synonym);
}
return;
}
/*******************************************************************************
*
*******************************************************************************/
Boolean
QEQueryIsComplete(QEQueryRef query)
{
if (!query->nestingLevel &&
!query->logicOpActive &&
query->lastError == kQEQueryErrorNone) {
return true;
}
return false;
}
/*******************************************************************************
*
*******************************************************************************/
QEQueryError
QEQueryLastError(QEQueryRef query)
{
return query->lastError;
}
#pragma mark Evaluation
/*******************************************************************************
*
*******************************************************************************/
void
QEQuerySetShortCircuits(
QEQueryRef query,
Boolean flag)
{
query->shortCircuitEval = flag;
}
/*******************************************************************************
*
*******************************************************************************/
Boolean
QEQueryGetShortCircuits(QEQueryRef query)
{
return query->shortCircuitEval;
}
/*******************************************************************************
*
*******************************************************************************/
Boolean
_QEQueryElementEvaluate(
QEQueryRef query,
CFDictionaryRef element,
void * object)
{
Boolean result = false;
CFStringRef predicate = NULL;
CFArrayRef elements = NULL;
CFIndex count, i;
predicate = CFDictionaryGetValue(element, kQEQueryKeyPredicate);
if (CFEqual(predicate, kQEQueryPredicateAnd)) {
elements = QEQueryElementGetArguments(element);
/* Empty groups can't normally be created, except for the
* root query, but empty groups are trivially true; basically,
* an empty group is true.
*/
count = CFArrayGetCount(elements); if (!count) {
result = true;
}
/* If any element in an AND group is false, stop evaluating the
* group and return false, else return true.
*/
for (i = 0; i < count; i++) {
CFDictionaryRef thisElement = CFArrayGetValueAtIndex(elements, i);
if (!_QEQueryElementEvaluate(query, thisElement, object)) {
if (query->shortCircuitEval) {
goto finish;
}
}
}
result = true;
} else if (CFEqual(predicate, kQEQueryPredicateOr)) {
elements = QEQueryElementGetArguments(element);
/* Empty groups can't normally be created, except for the
* root query, but empty groups are trivially true; basically,
* an empty group is true.
*/
count = CFArrayGetCount(elements);
if (!count) {
result = true;
}
/* If any element in an OR group is true, stop evaluating the
* group and return true, else return false.
*/
for (i = 0; i < count; i++) {
CFDictionaryRef thisElement = CFArrayGetValueAtIndex(elements, i);
if (_QEQueryElementEvaluate(query, thisElement, object)) {
result = true;
if (query->shortCircuitEval) {
goto finish;
}
}
}
} else {
/* Look for a client callback.
*/
QEQueryEvaluationCallback evalCallback =
_QEQueryEvaluationCallbackForPredicate(query, predicate);
if (evalCallback) {
result = evalCallback(element, object, query->userData,
&query->lastError);
} else {
query->lastError = kQEQueryErrorNoEvaluationCallback;
}
}
finish:
/* Flip the result if the element is negated.
*/
if (_QEQueryElementIsNegated(element)) {
result = !result;
}
/* Set the result to false upon any error.
*/
if (query->lastError != kQEQueryErrorNone) {
result = false;
}
return result;
}
/*******************************************************************************
*
*******************************************************************************/
Boolean
QEQueryEvaluate(
QEQueryRef query,
void * object)
{
Boolean result = false;
if (!QEQueryIsComplete(query) || query->lastError != kQEQueryErrorNone) {
goto finish;
}
result = _QEQueryElementEvaluate(query, query->queryRoot, object);
finish:
return result;
}
#pragma mark Command-Line Argument Processing
/*******************************************************************************
*
*******************************************************************************/
Boolean
QEQueryAppendElementFromArgs(
QEQueryRef query,
int argc,
char * const argv[],
uint32_t * num_used)
{
Boolean result = false;
uint32_t index = 0;
Boolean negated = false;
CFMutableDictionaryRef element = NULL; // must release
CFStringRef predicate = NULL; // must release
if (!argv[index]) {
goto finish;
}
predicate = _QEPredicateForString(query, argv[index]);
if (CFEqual(predicate,
CFDictionaryGetValue(query->operators, CFSTR(kQEQueryTokenNot)))) {
index++;
negated = true;
if (!argv[index]) {
query->lastError = kQEQueryErrorSyntax;
goto finish;
}
}
if (predicate) CFRelease(predicate);
predicate = _QEPredicateForString(query, argv[index]);
/* This is not an 'else'! Not applies to most of the following.
*/
if (CFEqual(predicate,
CFDictionaryGetValue(query->operators, CFSTR(kQEQueryTokenAnd)))) {
if (negated) {
query->lastError = kQEQueryErrorSyntax;
goto finish;
}
if (!QEQueryAppendAndOperator(query)) {
goto finish;
}
index++;
} else if (CFEqual(predicate,
CFDictionaryGetValue(query->operators, CFSTR(kQEQueryTokenOr)))) {
if (negated) {
query->lastError = kQEQueryErrorSyntax;
goto finish;
}
if (!QEQueryAppendOrOperator(query)) {
goto finish;
}
index++;
} else if (CFEqual(predicate,
CFDictionaryGetValue(query->operators, CFSTR(kQEQueryTokenGroupStart)))) {
if (!_QEQueryStartGroup(query, false /* 'and' group */, negated)) {
// called function sets query->lastError
goto finish;
}
index++;
} else if (CFEqual(predicate,
CFDictionaryGetValue(query->operators, CFSTR(kQEQueryTokenGroupEnd)))) {
if (!QEQueryEndGroup(query)) {
// called function sets query->lastError
goto finish;
}
index++;
} else {
QEQueryParseCallback parseCallback = NULL;
uint32_t callbackNumUsed = 0;
element = QEQueryCreateElement(query, predicate, NULL, negated);
if (!element) {
// called function sets query->lastError
goto finish;
}
parseCallback = _QEQueryParseCallbackForPredicate(query, predicate);
if (!parseCallback) {
query->lastError = kQEQueryErrorNoParseCallback;
goto finish;
}
/* Base args off index in case we consumed a NOT.
*/
if (!parseCallback(element, argc - index, &argv[index],
&callbackNumUsed, query->userData, &query->lastError)) {
// user callback sets query->lastError
goto finish;
}
index += callbackNumUsed;
if (!QEQueryAppendElement(query, element)) {
// called function sets query->lastError
goto finish;
}
}
result = true;
finish:
if (num_used) {
if (result) {
*num_used += index;
} else if (negated) {
*num_used += 1;
}
}
if (element) CFRelease(element);
if (predicate) CFRelease(predicate);
return result;
}
#pragma mark Hand-Building Queries
/*******************************************************************************
*
*******************************************************************************/
CFMutableDictionaryRef
QEQueryCreateElement(
QEQueryRef query,
CFStringRef predicate,
CFArrayRef arguments,
Boolean negated)
{
CFMutableDictionaryRef result = NULL;
CFStringRef synonym = NULL; // do not release (or retain!)
if (!predicate) {
goto finish;
}
result = CFDictionaryCreateMutable(kCFAllocatorDefault,
0 /* no limit */, &kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
if (!result) {
query->lastError = kQEQueryErrorNoMemory;
goto finish;
}
synonym = CFDictionaryGetValue(query->synonyms, predicate);
if (synonym) {
predicate = synonym;
}
QEQueryElementSetPredicate(result, predicate);
if (arguments) {
QEQueryElementSetArgumentsArray(result, arguments);
}
if (negated) {
CFDictionarySetValue(result, kQEQueryKeyNegated,
kCFBooleanTrue);
}
finish:
return result;
}
/*******************************************************************************
*
*******************************************************************************/
Boolean
QEQueryAppendElement(
QEQueryRef query,
CFMutableDictionaryRef element)
{
Boolean result = false;
CFMutableArrayRef elements = NULL;
/* If there is no logic operator active, and the current group has
* at least one item, treat this as being ANDed to the current group.
*/
if (!query->logicOpActive && !_QEQueryStackTopHasCount(query, 0)) {
if (!QEQueryAppendAndOperator(query)) {
// called function sets query->lastError
goto finish;
}
}
elements = (CFMutableArrayRef)CFDictionaryGetValue(
query->queryStackTop, kQEQueryKeyArguments);
CFArrayAppendValue(elements, (const void *)element);
query->logicOpActive = false;
result = true;
finish:
return result;
}
/*******************************************************************************
*
*******************************************************************************/
Boolean
QEQueryAppendAndOperator(QEQueryRef query)
{
Boolean result = false;
if (query->logicOpActive) {
query->lastError = kQEQueryErrorSyntax;
goto finish;
}
query->logicOpActive = true;
if (_QEQueryStackTopIsOrGroup(query)) {
if (!_QEQueryStartGroup(query, true /* 'and' group */,
false /* negated */)) {
// called function sets query->lastError
goto finish;
}
}
result = true;
finish:
return result;
}
/*******************************************************************************
*
*******************************************************************************/
Boolean
QEQueryAppendOrOperator(QEQueryRef query)
{
Boolean result = false;
if (query->logicOpActive) {
query->lastError = kQEQueryErrorSyntax;
goto finish;
}
query->logicOpActive = true;
if (_QEQueryStackTopIsAndGroup(query)) {
if (!_QEQueryPopGroup(query)) {
goto finish;
}
}
result = true;
finish:
return result;
}
/*******************************************************************************
*
*******************************************************************************/
Boolean
QEQueryStartGroup(
QEQueryRef query,
Boolean negated)
{
return _QEQueryStartGroup(query, false /* not an 'and' group */, negated);
}
/*******************************************************************************
*
*******************************************************************************/
Boolean
_QEQueryStartGroup(
QEQueryRef query,
Boolean andFlag,
Boolean negated)
{
Boolean result = false;
if (!query->logicOpActive && _QEQueryStackTopIsOrGroup(query)) {
if (!_QEQueryPushGroup(query, false /* negated */,
true /* 'and' group */)) {
// called function sets query->lastError
goto finish;
}
}
if (!_QEQueryPushGroup(query, negated, andFlag)) {
// called function sets query->lastError
goto finish;
}
result = true;
finish:
return result;
}
/*******************************************************************************
*
*******************************************************************************/
Boolean
QEQueryEndGroup(QEQueryRef query)
{
Boolean result = false;
/* A complete query has no open groups.
*/
if (QEQueryIsComplete(query)) {
query->lastError = kQEQueryErrorGroupNesting;
goto finish;
}
/* Don't allow empty parentheses within an explicit group.
* Why? Well, find(1) doesn't.
*/
if (_QEQueryStackTopHasCount(query, 0)) {
query->lastError = kQEQueryErrorEmptyGroup;
goto finish;
}
/* If we are in an implicit group binding AND operators, end it
* now so that the explicit group binding OR operators can be ended.
*/
if (_QEQueryStackTopIsAndGroup(query)) {
if (!_QEQueryPopGroup(query)) {
// called function sets query->lastError
goto finish;
}
}
/* Two nested AND groups is an internal logic error.
*/
if (_QEQueryStackTopIsAndGroup(query)) {
query->lastError = kQEQueryErrorGroupNesting;
goto finish;
}
/* Now, if the topmost group is not an OR group, something else
* is wrong with out internal logic.
*/
if (!_QEQueryStackTopIsOrGroup(query)) {
query->lastError = kQEQueryErrorGroupNesting;
goto finish;
}
/* At last we can do something! Pop that OR group off the stack to close
* it, and if it only contains one element, coalesce it into its parent.
*/
if (!_QEQueryPopGroupAndCoalesce(query, false /* pop only if can coalesce */)) {
query->lastError = kQEQueryErrorGroupNesting;
goto finish;
}
/* Decrement the stack's OR group nesting level.
*/
query->nestingLevel--;
/* Now, if we are left with an AND group at the top of the stack, see if
* we can coalesce that too.
*/
if (_QEQueryStackTopIsAndGroup(query)) {
if (!_QEQueryPopGroupAndCoalesce(query, true /* pop only if can coalesce */)) {
query->lastError = kQEQueryErrorGroupNesting;
goto finish;
}
}
result = true;
finish:
return result;
}
#pragma mark Query Elements
/*******************************************************************************
*
*******************************************************************************/
CFStringRef
QEQueryElementGetPredicate(CFDictionaryRef element)
{
return CFDictionaryGetValue(element, kQEQueryKeyPredicate);
}
/*******************************************************************************
*
*******************************************************************************/
CFMutableArrayRef
QEQueryElementGetArguments(CFDictionaryRef element)
{
CFMutableDictionaryRef dict = (CFMutableDictionaryRef)element;
CFMutableArrayRef args = (CFMutableArrayRef)CFDictionaryGetValue(
element, kQEQueryKeyArguments);
if (!args) {
args = CFArrayCreateMutable(kCFAllocatorDefault,
0, &kCFTypeArrayCallBacks);
if (!args) {
goto finish;
}
QEQueryElementSetArgumentsArray(dict, args);
CFRelease(args);
}
finish:
return args;
}
/*******************************************************************************
*
*******************************************************************************/
CFTypeRef
QEQueryElementGetArgumentAtIndex(
CFDictionaryRef element,
CFIndex i)
{
CFArrayRef arguments = QEQueryElementGetArguments(element);
return CFArrayGetValueAtIndex(arguments, i);
}
/*******************************************************************************
*
*******************************************************************************/
void
QEQueryElementSetPredicate(
CFMutableDictionaryRef element,
CFStringRef predicate)
{
CFDictionarySetValue(element, kQEQueryKeyPredicate, predicate);
return;
}
/*******************************************************************************
*
*******************************************************************************/
void
QEQueryElementSetArgumentsArray(
CFMutableDictionaryRef element,
CFArrayRef arguments)
{
CFDictionarySetValue(element, kQEQueryKeyArguments, arguments);
return;
}
/*******************************************************************************
*
*******************************************************************************/
void
QEQueryElementAppendArgument(
CFMutableDictionaryRef element,
CFTypeRef argument)
{
CFMutableArrayRef arguments = (CFMutableArrayRef)QEQueryElementGetArguments(element);
CFArrayAppendValue(arguments, argument);
return;
}
/*******************************************************************************
*
*******************************************************************************/
Boolean
QEQueryElementSetArguments(
CFMutableDictionaryRef element,
uint32_t numArgs,
...)
{
Boolean result = false;
CFMutableArrayRef arguments = NULL;
va_list ap;
uint32_t i;
if (!numArgs) {
goto finish;
}
arguments = QEQueryElementGetArguments(element);
if (!arguments) {
arguments = CFArrayCreateMutable(kCFAllocatorDefault,
0, &kCFTypeArrayCallBacks);
if (!arguments) {
goto finish;
}
CFDictionarySetValue(element, kQEQueryKeyArguments, arguments);
CFRelease(arguments);
}
va_start(ap, numArgs);
for (i = 0; i < numArgs; i++) {
char * arg = va_arg(ap, char *);
if (arg) {
CFStringRef string = CFStringCreateWithCString(kCFAllocatorDefault,
arg, kCFStringEncodingUTF8);
if (!string) {
goto finish;
}
CFArrayAppendValue(arguments, string);
CFRelease(string);
}
}
result = true;
finish:
va_end(ap);