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(_configd_verbose
, LOG_DEBUG
, CFSTR("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(_configd_verbose
, LOG_DEBUG
, CFSTR("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
);
176 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("regcomp(%s) failed: %s"), str
, reErrBuf
);
180 *error
= CFRetain(CFSTR("could not convert pattern to regex string"));
181 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("%@"), *error
);
184 if (str
!= str_q
) CFAllocatorDeallocate(NULL
, str
);
191 patternCopy(CFStringRef pattern
)
195 pInfo
= CFDictionaryGetValue(patternData
, pattern
);
196 return pInfo
? CFArrayCreateMutableCopy(NULL
, 0, pInfo
) : NULL
;
202 patternNew(CFStringRef pattern
)
206 CFMutableArrayRef pInfo
;
207 CFMutableDataRef pRegex
;
208 CFArrayRef pSessions
;
210 /* create the pattern info */
211 pInfo
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
213 /* compile the regular expression from the pattern string. */
214 pRegex
= CFDataCreateMutable(NULL
, sizeof(regex_t
));
215 CFDataSetLength(pRegex
, sizeof(regex_t
));
216 if (!patternCompile(pattern
, (regex_t
*)CFDataGetBytePtr(pRegex
), &err
)) {
223 /* add the compiled regular expression to the pattern info */
224 CFArrayAppendValue(pInfo
, pRegex
);
226 /* add the initial (empty) list of sessions watching this pattern */
227 pSessions
= CFArrayCreate(NULL
, NULL
, 0, &kCFTypeArrayCallBacks
);
228 CFArrayAppendValue(pInfo
, pSessions
);
229 CFRelease(pSessions
);
231 /* identify/add all existing keys that match the specified pattern */
232 context
.pInfo
= pInfo
;
233 context
.preg
= (regex_t
*)CFDataGetBytePtr(pRegex
);
234 my_CFDictionaryApplyFunction(storeData
,
235 (CFDictionaryApplierFunction
)identifyKeyForPattern
,
245 patternAddSession(CFStringRef pattern
, CFNumberRef sessionNum
)
249 CFMutableArrayRef pInfo
;
250 CFMutableArrayRef pSessions
;
252 /* find (or create new instance of) this pattern */
253 pInfo
= patternCopy(pattern
);
256 pInfo
= patternNew(pattern
);
262 /* add this session as a pattern watcher */
263 pSessions
= (CFMutableArrayRef
)CFArrayGetValueAtIndex(pInfo
, 1);
264 pSessions
= CFArrayCreateMutableCopy(NULL
, 0, pSessions
);
265 CFArrayAppendValue(pSessions
, sessionNum
);
266 CFArraySetValueAtIndex(pInfo
, 1, pSessions
);
267 CFRelease(pSessions
);
269 /* update pattern watcher info */
270 CFDictionarySetValue(patternData
, pattern
, pInfo
);
272 /* add this session as a watcher of any existing keys */
273 n
= CFArrayGetCount(pInfo
);
274 for (i
= 2; i
< n
; i
++) {
275 CFStringRef matchingKey
;
277 matchingKey
= CFArrayGetValueAtIndex(pInfo
, i
);
278 _addWatcher(sessionNum
, matchingKey
);
288 patternRemoveSession(CFStringRef pattern
, CFNumberRef sessionNum
)
292 CFMutableArrayRef pInfo
;
294 CFMutableArrayRef pSessions
;
296 /* find instance of this pattern */
297 pInfo
= patternCopy(pattern
);
299 /* remove this session as a watcher from all matching keys */
300 n
= CFArrayGetCount(pInfo
);
301 for (i
= 2; i
< n
; i
++) {
302 CFStringRef matchingKey
;
304 matchingKey
= CFArrayGetValueAtIndex(pInfo
, i
);
305 _removeWatcher(sessionNum
, matchingKey
);
308 /* remove session from watchers */
309 pSessions
= (CFMutableArrayRef
)CFArrayGetValueAtIndex(pInfo
, 1);
310 n
= CFArrayGetCount(pSessions
);
312 /* if other sessions are watching this pattern */
314 pSessions
= CFArrayCreateMutableCopy(NULL
, 0, pSessions
);
315 i
= CFArrayGetFirstIndexOfValue(pSessions
, CFRangeMake(0, n
), sessionNum
);
316 CFArrayRemoveValueAtIndex(pSessions
, i
);
317 CFArraySetValueAtIndex(pInfo
, 1, pSessions
);
318 CFRelease(pSessions
);
320 CFDictionarySetValue(patternData
, pattern
, pInfo
);
322 /* if no other sessions are watching this pattern */
324 pRegex
= CFArrayGetValueAtIndex(pInfo
, 0);
325 regfree((regex_t
*)CFDataGetBytePtr(pRegex
));
326 CFDictionaryRemoveValue(patternData
, pattern
);
335 addKeyForPattern(const void *key
, void *val
, void *context
)
337 CFStringRef pattern
= (CFStringRef
)key
;
338 CFArrayRef pInfo
= (CFArrayRef
)val
;
339 CFStringRef storeKey
= (CFStringRef
)context
;
347 /* convert store key to C string */
348 len
= CFStringGetLength(storeKey
) + 1;
349 if (len
> (CFIndex
)sizeof(str_q
))
350 str
= CFAllocatorAllocate(NULL
, len
, 0);
351 if (_SC_cfstring_to_cstring(storeKey
, str
, len
, kCFStringEncodingASCII
) == NULL
) {
352 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("could not convert store key to C string"));
356 /* compare new store key to regular expression pattern */
357 preg
= (regex_t
*)CFDataGetBytePtr(CFArrayGetValueAtIndex(pInfo
, 0));
358 reError
= regexec(preg
, str
, 0, NULL
, 0);
366 CFMutableArrayRef pInfo_new
;
367 CFArrayRef pSessions
;
370 pSessions
= CFArrayGetValueAtIndex(pInfo
, 1);
371 n
= CFArrayGetCount(pSessions
);
372 for (i
= 0; i
< n
; i
++) {
373 CFNumberRef sessionNum
= CFArrayGetValueAtIndex(pSessions
, i
);
375 _addWatcher(sessionNum
, storeKey
);
378 /* add key, update pattern watcher info */
379 pInfo_new
= CFArrayCreateMutableCopy(NULL
, 0, pInfo
);
380 CFArrayAppendValue(pInfo_new
, storeKey
);
381 CFDictionarySetValue(patternData
, pattern
, pInfo_new
);
382 CFRelease(pInfo_new
);
391 (void)regerror(reError
, preg
, reErrBuf
, sizeof(reErrBuf
));
392 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("regexec(): %s"), reErrBuf
);
399 if (str
!= str_q
) CFAllocatorDeallocate(NULL
, str
);
406 patternAddKey(CFStringRef key
)
408 my_CFDictionaryApplyFunction(patternData
,
409 (CFDictionaryApplierFunction
)addKeyForPattern
,
417 removeKeyFromPattern(const void *key
, void *val
, void *context
)
419 CFStringRef pattern
= (CFStringRef
)key
;
420 CFArrayRef pInfo
= (CFArrayRef
)val
;
421 CFStringRef storeKey
= (CFStringRef
)context
;
425 CFMutableArrayRef pInfo_new
;
426 CFArrayRef pSessions
;
428 n
= CFArrayGetCount(pInfo
);
430 /* if no keys match this pattern */
434 i
= CFArrayGetFirstIndexOfValue(pInfo
, CFRangeMake(2, n
-2), storeKey
);
436 /* if this key wasn't matched by this pattern */
440 /* remove key from pattern info */
441 pInfo_new
= CFArrayCreateMutableCopy(NULL
, 0, pInfo
);
442 CFArrayRemoveValueAtIndex(pInfo_new
, i
);
444 /* remove watchers */
445 pSessions
= CFArrayGetValueAtIndex(pInfo_new
, 1);
446 n
= CFArrayGetCount(pSessions
);
447 for (i
= 0; i
< n
; i
++) {
448 CFNumberRef sessionNum
= CFArrayGetValueAtIndex(pSessions
, i
);
450 _removeWatcher(sessionNum
, storeKey
);
453 CFDictionarySetValue(patternData
, pattern
, pInfo_new
);
454 CFRelease(pInfo_new
);
461 patternRemoveKey(CFStringRef key
)
463 my_CFDictionaryApplyFunction(patternData
,
464 (CFDictionaryApplierFunction
)removeKeyFromPattern
,