2 * Copyright (c) 2003 Apple Computer, 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
= CFStringGetLength(storeKey
) + 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
);
123 __private_extern__ Boolean
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
);
195 patternCopy(CFStringRef pattern
)
199 pInfo
= CFDictionaryGetValue(patternData
, pattern
);
200 return pInfo
? CFArrayCreateMutableCopy(NULL
, 0, pInfo
) : NULL
;
206 patternNew(CFStringRef pattern
)
210 CFMutableArrayRef pInfo
;
211 CFMutableDataRef pRegex
;
212 CFArrayRef pSessions
;
214 /* create the pattern info */
215 pInfo
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
217 /* compile the regular expression from the pattern string. */
218 pRegex
= CFDataCreateMutable(NULL
, sizeof(regex_t
));
219 CFDataSetLength(pRegex
, sizeof(regex_t
));
220 if (!patternCompile(pattern
, (regex_t
*)CFDataGetBytePtr(pRegex
), &err
)) {
227 /* add the compiled regular expression to the pattern info */
228 CFArrayAppendValue(pInfo
, pRegex
);
230 /* add the initial (empty) list of sessions watching this pattern */
231 pSessions
= CFArrayCreate(NULL
, NULL
, 0, &kCFTypeArrayCallBacks
);
232 CFArrayAppendValue(pInfo
, pSessions
);
233 CFRelease(pSessions
);
235 /* identify/add all existing keys that match the specified pattern */
236 context
.pInfo
= pInfo
;
237 context
.preg
= (regex_t
*)CFDataGetBytePtr(pRegex
);
238 my_CFDictionaryApplyFunction(storeData
,
239 (CFDictionaryApplierFunction
)identifyKeyForPattern
,
249 patternAddSession(CFStringRef pattern
, CFNumberRef sessionNum
)
253 CFMutableArrayRef pInfo
;
254 CFMutableArrayRef pSessions
;
256 /* find (or create new instance of) this pattern */
257 pInfo
= patternCopy(pattern
);
260 pInfo
= patternNew(pattern
);
266 /* add this session as a pattern watcher */
267 pSessions
= (CFMutableArrayRef
)CFArrayGetValueAtIndex(pInfo
, 1);
268 pSessions
= CFArrayCreateMutableCopy(NULL
, 0, pSessions
);
269 CFArrayAppendValue(pSessions
, sessionNum
);
270 CFArraySetValueAtIndex(pInfo
, 1, pSessions
);
271 CFRelease(pSessions
);
273 /* update pattern watcher info */
274 CFDictionarySetValue(patternData
, pattern
, pInfo
);
276 /* add this session as a watcher of any existing keys */
277 n
= CFArrayGetCount(pInfo
);
278 for (i
= 2; i
< n
; i
++) {
279 CFStringRef matchingKey
;
281 matchingKey
= CFArrayGetValueAtIndex(pInfo
, i
);
282 _addWatcher(sessionNum
, matchingKey
);
292 patternRemoveSession(CFStringRef pattern
, CFNumberRef sessionNum
)
296 CFMutableArrayRef pInfo
;
298 CFMutableArrayRef pSessions
;
300 /* find instance of this pattern */
301 pInfo
= patternCopy(pattern
);
303 /* remove this session as a watcher from all matching keys */
304 n
= CFArrayGetCount(pInfo
);
305 for (i
= 2; i
< n
; i
++) {
306 CFStringRef matchingKey
;
308 matchingKey
= CFArrayGetValueAtIndex(pInfo
, i
);
309 _removeWatcher(sessionNum
, matchingKey
);
312 /* remove session from watchers */
313 pSessions
= (CFMutableArrayRef
)CFArrayGetValueAtIndex(pInfo
, 1);
314 n
= CFArrayGetCount(pSessions
);
316 /* if other sessions are watching this pattern */
318 pSessions
= CFArrayCreateMutableCopy(NULL
, 0, pSessions
);
319 i
= CFArrayGetFirstIndexOfValue(pSessions
, CFRangeMake(0, n
), sessionNum
);
320 CFArrayRemoveValueAtIndex(pSessions
, i
);
321 CFArraySetValueAtIndex(pInfo
, 1, pSessions
);
322 CFRelease(pSessions
);
324 CFDictionarySetValue(patternData
, pattern
, pInfo
);
326 /* if no other sessions are watching this pattern */
328 pRegex
= CFArrayGetValueAtIndex(pInfo
, 0);
329 regfree((regex_t
*)CFDataGetBytePtr(pRegex
));
330 CFDictionaryRemoveValue(patternData
, pattern
);
339 addKeyForPattern(const void *key
, void *val
, void *context
)
341 CFStringRef pattern
= (CFStringRef
)key
;
342 CFArrayRef pInfo
= (CFArrayRef
)val
;
343 CFStringRef storeKey
= (CFStringRef
)context
;
351 /* convert store key to C string */
352 len
= CFStringGetLength(storeKey
) + 1;
353 if (len
> (CFIndex
)sizeof(str_q
))
354 str
= CFAllocatorAllocate(NULL
, len
, 0);
355 if (_SC_cfstring_to_cstring(storeKey
, str
, len
, kCFStringEncodingASCII
) == NULL
) {
356 SCLog(TRUE
, LOG_DEBUG
, CFSTR("addKeyForPattern(): could not convert store key to C string"));
360 /* compare new store key to regular expression pattern */
361 preg
= (regex_t
*)CFDataGetBytePtr(CFArrayGetValueAtIndex(pInfo
, 0));
362 reError
= regexec(preg
, str
, 0, NULL
, 0);
370 CFMutableArrayRef pInfo_new
;
371 CFArrayRef pSessions
;
374 pSessions
= CFArrayGetValueAtIndex(pInfo
, 1);
375 n
= CFArrayGetCount(pSessions
);
376 for (i
= 0; i
< n
; i
++) {
377 CFNumberRef sessionNum
= CFArrayGetValueAtIndex(pSessions
, i
);
379 _addWatcher(sessionNum
, storeKey
);
382 /* add key, update pattern watcher info */
383 pInfo_new
= CFArrayCreateMutableCopy(NULL
, 0, pInfo
);
384 CFArrayAppendValue(pInfo_new
, storeKey
);
385 CFDictionarySetValue(patternData
, pattern
, pInfo_new
);
386 CFRelease(pInfo_new
);
395 (void)regerror(reError
, preg
, reErrBuf
, sizeof(reErrBuf
));
396 SCLog(TRUE
, LOG_DEBUG
, CFSTR("addKeyForPattern regexec(): %s"), reErrBuf
);
403 if (str
!= str_q
) CFAllocatorDeallocate(NULL
, str
);
410 patternAddKey(CFStringRef key
)
412 my_CFDictionaryApplyFunction(patternData
,
413 (CFDictionaryApplierFunction
)addKeyForPattern
,
421 removeKeyFromPattern(const void *key
, void *val
, void *context
)
423 CFStringRef pattern
= (CFStringRef
)key
;
424 CFArrayRef pInfo
= (CFArrayRef
)val
;
425 CFStringRef storeKey
= (CFStringRef
)context
;
429 CFMutableArrayRef pInfo_new
;
430 CFArrayRef pSessions
;
432 n
= CFArrayGetCount(pInfo
);
434 /* if no keys match this pattern */
438 i
= CFArrayGetFirstIndexOfValue(pInfo
, CFRangeMake(2, n
-2), storeKey
);
439 if (i
== kCFNotFound
) {
440 /* if this key wasn't matched by this pattern */
444 /* remove key from pattern info */
445 pInfo_new
= CFArrayCreateMutableCopy(NULL
, 0, pInfo
);
446 CFArrayRemoveValueAtIndex(pInfo_new
, i
);
448 /* remove watchers */
449 pSessions
= CFArrayGetValueAtIndex(pInfo_new
, 1);
450 n
= CFArrayGetCount(pSessions
);
451 for (i
= 0; i
< n
; i
++) {
452 CFNumberRef sessionNum
= CFArrayGetValueAtIndex(pSessions
, i
);
454 _removeWatcher(sessionNum
, storeKey
);
457 CFDictionarySetValue(patternData
, pattern
, pInfo_new
);
458 CFRelease(pInfo_new
);
465 patternRemoveKey(CFStringRef key
)
467 my_CFDictionaryApplyFunction(patternData
,
468 (CFDictionaryApplierFunction
)removeKeyFromPattern
,