2 * Copyright (c) 2003, 2004, 2006, 2007 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 * Modification History
27 * April 23, 2003 Allan Nathanson <ajn@apple.com>
39 * - pattern matching information is maintained in a dictionary (patternData).
40 * - the dictionary "key" is the regular expression being matched
41 * - the dictionary "value" is a CFArray with the following contents
42 * [0] = CFData consisting of the pre-compiled regular expression
43 * [1] = CFArray[CFNumber] consisting of the sessions watching this pattern
44 * [2-n] = dynamic store keys which match this pattern
49 CFMutableArrayRef pInfo
;
51 } addContext
, *addContextRef
;
54 static __inline__
void
55 my_CFDictionaryApplyFunction(CFDictionaryRef theDict
,
56 CFDictionaryApplierFunction applier
,
59 CFAllocatorRef myAllocator
;
60 CFDictionaryRef myDict
;
62 myAllocator
= CFGetAllocator(theDict
);
63 myDict
= CFDictionaryCreateCopy(myAllocator
, theDict
);
64 CFDictionaryApplyFunction(myDict
, applier
, context
);
71 identifyKeyForPattern(const void *key
, void *val
, void *context
)
73 CFStringRef storeKey
= (CFStringRef
)key
;
74 CFDictionaryRef storeValue
= (CFDictionaryRef
)val
;
75 CFMutableArrayRef pInfo
= ((addContextRef
)context
)->pInfo
;
76 regex_t
* preg
= ((addContextRef
)context
)->preg
;
83 if (!CFDictionaryContainsKey(storeValue
, kSCDData
)) {
84 /* if no data (yet) */
88 /* convert store key to C string */
89 len
= CFStringGetMaximumSizeForEncoding(CFStringGetLength(storeKey
), kCFStringEncodingASCII
) + 1;
90 if (len
> (CFIndex
)sizeof(str_q
))
91 str
= CFAllocatorAllocate(NULL
, len
, 0);
92 if (_SC_cfstring_to_cstring(storeKey
, str
, len
, kCFStringEncodingASCII
) == NULL
) {
93 SCLog(TRUE
, LOG_DEBUG
, CFSTR("identifyKeyForPattern(): could not convert store key to C string"));
97 /* compare store key to new notification keys regular expression pattern */
98 reError
= regexec(preg
, str
, 0, NULL
, 0);
101 /* we've got a match */
102 CFArrayAppendValue(pInfo
, storeKey
);
110 (void)regerror(reError
, preg
, reErrBuf
, sizeof(reErrBuf
));
111 SCLog(TRUE
, LOG_DEBUG
, CFSTR("identifyKeyForPattern regexec(): %s"), reErrBuf
);
118 if (str
!= str_q
) CFAllocatorDeallocate(NULL
, str
);
124 patternCompile(CFStringRef pattern
, regex_t
*preg
, CFStringRef
*error
)
126 Boolean append
= FALSE
;
127 Boolean insert
= FALSE
;
133 if (!CFStringHasPrefix(pattern
, CFSTR("^"))) {
137 if (!CFStringHasSuffix(pattern
, CFSTR("$")) ||
138 CFStringHasSuffix(pattern
, CFSTR("\\$"))) {
142 /* if regex pattern is not bounded at both ends */
143 if (insert
|| append
) {
144 pattern
= CFStringCreateWithFormat(NULL
,
152 (void)CFStringGetBytes(pattern
,
153 CFRangeMake(0, CFStringGetLength(pattern
)),
154 kCFStringEncodingASCII
,
160 if (++len
> (CFIndex
)sizeof(str_q
)) {
161 str
= CFAllocatorAllocate(NULL
, len
, 0);
163 ok
= (_SC_cfstring_to_cstring(pattern
, str
, len
, kCFStringEncodingASCII
) != NULL
);
164 if (insert
|| append
) {
170 reError
= regcomp(preg
, str
, REG_EXTENDED
);
174 (void)regerror(reError
, preg
, reErrBuf
, sizeof(reErrBuf
));
175 *error
= CFStringCreateWithCString(NULL
, reErrBuf
, kCFStringEncodingASCII
);
177 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("patternCompile regcomp(%s) failed: %s"), str
, reErrBuf
);
182 *error
= CFRetain(CFSTR("could not convert pattern to regex string"));
184 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("%@"), *error
);
188 if (str
!= str_q
) CFAllocatorDeallocate(NULL
, str
);
193 static CFMutableArrayRef
194 patternCopy(CFStringRef pattern
)
198 pInfo
= CFDictionaryGetValue(patternData
, pattern
);
199 return pInfo
? CFArrayCreateMutableCopy(NULL
, 0, pInfo
) : NULL
;
203 static CFMutableArrayRef
204 patternNew(CFStringRef pattern
)
207 CFStringRef err
= NULL
;
208 CFMutableArrayRef pInfo
;
209 CFMutableDataRef pRegex
;
210 CFArrayRef pSessions
;
212 /* create the pattern info */
213 pInfo
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
215 /* compile the regular expression from the pattern string. */
216 pRegex
= CFDataCreateMutable(NULL
, sizeof(regex_t
));
217 CFDataSetLength(pRegex
, sizeof(regex_t
));
218 if (!patternCompile(pattern
, (regex_t
*)CFDataGetBytePtr(pRegex
), &err
)) {
225 /* add the compiled regular expression to the pattern info */
226 CFArrayAppendValue(pInfo
, pRegex
);
228 /* add the initial (empty) list of sessions watching this pattern */
229 pSessions
= CFArrayCreate(NULL
, NULL
, 0, &kCFTypeArrayCallBacks
);
230 CFArrayAppendValue(pInfo
, pSessions
);
231 CFRelease(pSessions
);
233 /* identify/add all existing keys that match the specified pattern */
234 context
.pInfo
= pInfo
;
235 context
.preg
= (regex_t
*)CFDataGetBytePtr(pRegex
);
236 my_CFDictionaryApplyFunction(storeData
,
237 (CFDictionaryApplierFunction
)identifyKeyForPattern
,
247 patternCopyMatches(CFStringRef pattern
)
249 Boolean isNew
= FALSE
;
251 CFMutableArrayRef pInfo
;
253 /* find (or create new instance of) this pattern */
254 pInfo
= patternCopy(pattern
);
257 pInfo
= patternNew(pattern
);
268 pRegex
= CFArrayGetValueAtIndex(pInfo
, 0);
269 regfree((regex_t
*)CFDataGetBytePtr(pRegex
));
272 CFArrayReplaceValues(pInfo
, CFRangeMake(0, 2), NULL
, 0);
273 keys
= CFArrayCreateCopy(NULL
, pInfo
);
282 patternAddSession(CFStringRef pattern
, CFNumberRef sessionNum
)
286 CFMutableArrayRef pInfo
;
287 CFMutableArrayRef pSessions
;
289 /* find (or create new instance of) this pattern */
290 pInfo
= patternCopy(pattern
);
293 pInfo
= patternNew(pattern
);
299 /* add this session as a pattern watcher */
300 pSessions
= (CFMutableArrayRef
)CFArrayGetValueAtIndex(pInfo
, 1);
301 pSessions
= CFArrayCreateMutableCopy(NULL
, 0, pSessions
);
302 CFArrayAppendValue(pSessions
, sessionNum
);
303 CFArraySetValueAtIndex(pInfo
, 1, pSessions
);
304 CFRelease(pSessions
);
306 /* update pattern watcher info */
307 CFDictionarySetValue(patternData
, pattern
, pInfo
);
309 /* add this session as a watcher of any existing keys */
310 n
= CFArrayGetCount(pInfo
);
311 for (i
= 2; i
< n
; i
++) {
312 CFStringRef matchingKey
;
314 matchingKey
= CFArrayGetValueAtIndex(pInfo
, i
);
315 _addWatcher(sessionNum
, matchingKey
);
325 patternRemoveSession(CFStringRef pattern
, CFNumberRef sessionNum
)
329 CFMutableArrayRef pInfo
;
331 CFMutableArrayRef pSessions
;
333 /* find instance of this pattern */
334 pInfo
= patternCopy(pattern
);
336 /* remove this session as a watcher from all matching keys */
337 n
= CFArrayGetCount(pInfo
);
338 for (i
= 2; i
< n
; i
++) {
339 CFStringRef matchingKey
;
341 matchingKey
= CFArrayGetValueAtIndex(pInfo
, i
);
342 _removeWatcher(sessionNum
, matchingKey
);
345 /* remove session from watchers */
346 pSessions
= (CFMutableArrayRef
)CFArrayGetValueAtIndex(pInfo
, 1);
347 n
= CFArrayGetCount(pSessions
);
349 /* if other sessions are watching this pattern */
351 pSessions
= CFArrayCreateMutableCopy(NULL
, 0, pSessions
);
352 i
= CFArrayGetFirstIndexOfValue(pSessions
, CFRangeMake(0, n
), sessionNum
);
353 CFArrayRemoveValueAtIndex(pSessions
, i
);
354 CFArraySetValueAtIndex(pInfo
, 1, pSessions
);
355 CFRelease(pSessions
);
357 CFDictionarySetValue(patternData
, pattern
, pInfo
);
359 /* if no other sessions are watching this pattern */
361 pRegex
= CFArrayGetValueAtIndex(pInfo
, 0);
362 regfree((regex_t
*)CFDataGetBytePtr(pRegex
));
363 CFDictionaryRemoveValue(patternData
, pattern
);
372 addKeyForPattern(const void *key
, void *val
, void *context
)
374 CFStringRef pattern
= (CFStringRef
)key
;
375 CFArrayRef pInfo
= (CFArrayRef
)val
;
376 CFStringRef storeKey
= (CFStringRef
)context
;
384 /* convert store key to C string */
385 len
= CFStringGetMaximumSizeForEncoding(CFStringGetLength(storeKey
), kCFStringEncodingASCII
) + 1;
386 if (len
> (CFIndex
)sizeof(str_q
))
387 str
= CFAllocatorAllocate(NULL
, len
, 0);
388 if (_SC_cfstring_to_cstring(storeKey
, str
, len
, kCFStringEncodingASCII
) == NULL
) {
389 SCLog(TRUE
, LOG_DEBUG
, CFSTR("addKeyForPattern(): could not convert store key to C string"));
393 /* compare new store key to regular expression pattern */
394 preg
= (regex_t
*)CFDataGetBytePtr(CFArrayGetValueAtIndex(pInfo
, 0));
395 reError
= regexec(preg
, str
, 0, NULL
, 0);
403 CFMutableArrayRef pInfo_new
;
404 CFArrayRef pSessions
;
407 pSessions
= CFArrayGetValueAtIndex(pInfo
, 1);
408 n
= CFArrayGetCount(pSessions
);
409 for (i
= 0; i
< n
; i
++) {
410 CFNumberRef sessionNum
= CFArrayGetValueAtIndex(pSessions
, i
);
412 _addWatcher(sessionNum
, storeKey
);
415 /* add key, update pattern watcher info */
416 pInfo_new
= CFArrayCreateMutableCopy(NULL
, 0, pInfo
);
417 CFArrayAppendValue(pInfo_new
, storeKey
);
418 CFDictionarySetValue(patternData
, pattern
, pInfo_new
);
419 CFRelease(pInfo_new
);
428 (void)regerror(reError
, preg
, reErrBuf
, sizeof(reErrBuf
));
429 SCLog(TRUE
, LOG_DEBUG
, CFSTR("addKeyForPattern regexec(): %s"), reErrBuf
);
436 if (str
!= str_q
) CFAllocatorDeallocate(NULL
, str
);
443 patternAddKey(CFStringRef key
)
445 my_CFDictionaryApplyFunction(patternData
,
446 (CFDictionaryApplierFunction
)addKeyForPattern
,
454 removeKeyFromPattern(const void *key
, void *val
, void *context
)
456 CFStringRef pattern
= (CFStringRef
)key
;
457 CFArrayRef pInfo
= (CFArrayRef
)val
;
458 CFStringRef storeKey
= (CFStringRef
)context
;
462 CFMutableArrayRef pInfo_new
;
463 CFArrayRef pSessions
;
465 n
= CFArrayGetCount(pInfo
);
467 /* if no keys match this pattern */
471 i
= CFArrayGetFirstIndexOfValue(pInfo
, CFRangeMake(2, n
-2), storeKey
);
472 if (i
== kCFNotFound
) {
473 /* if this key wasn't matched by this pattern */
477 /* remove key from pattern info */
478 pInfo_new
= CFArrayCreateMutableCopy(NULL
, 0, pInfo
);
479 CFArrayRemoveValueAtIndex(pInfo_new
, i
);
481 /* remove watchers */
482 pSessions
= CFArrayGetValueAtIndex(pInfo_new
, 1);
483 n
= CFArrayGetCount(pSessions
);
484 for (i
= 0; i
< n
; i
++) {
485 CFNumberRef sessionNum
= CFArrayGetValueAtIndex(pSessions
, i
);
487 _removeWatcher(sessionNum
, storeKey
);
490 CFDictionarySetValue(patternData
, pattern
, pInfo_new
);
491 CFRelease(pInfo_new
);
498 patternRemoveKey(CFStringRef key
)
500 my_CFDictionaryApplyFunction(patternData
,
501 (CFDictionaryApplierFunction
)removeKeyFromPattern
,