2 * Copyright (c) 2003, 2004, 2006-2008 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
;
134 if (CFStringGetLength(pattern
) == 0) {
135 SCLog(TRUE
, LOG_ERR
, CFSTR("patternCompile(): empty string"));
138 if (!CFStringHasPrefix(pattern
, CFSTR("^"))) {
142 if (!CFStringHasSuffix(pattern
, CFSTR("$")) ||
143 CFStringHasSuffix(pattern
, CFSTR("\\$"))) {
147 /* if regex pattern is not bounded at both ends */
148 if (insert
|| append
) {
149 pattern
= CFStringCreateWithFormat(NULL
,
157 len_c
= CFStringGetBytes(pattern
,
158 CFRangeMake(0, CFStringGetLength(pattern
)),
159 kCFStringEncodingASCII
,
166 SCLog(TRUE
, LOG_ERR
, CFSTR("patternCompile(): could not get buffer length for \"%@\""), pattern
);
167 len
= sizeof(str_q
) - 1;
169 if (++len
> (CFIndex
)sizeof(str_q
)) {
170 str
= CFAllocatorAllocate(NULL
, len
, 0);
172 ok
= (_SC_cfstring_to_cstring(pattern
, str
, len
, kCFStringEncodingASCII
) != NULL
);
173 if (insert
|| append
) {
179 reError
= regcomp(preg
, str
, REG_EXTENDED
);
183 (void)regerror(reError
, preg
, reErrBuf
, sizeof(reErrBuf
));
184 *error
= CFStringCreateWithCString(NULL
, reErrBuf
, kCFStringEncodingASCII
);
186 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("patternCompile regcomp(%s) failed: %s"), str
, reErrBuf
);
191 *error
= CFRetain(CFSTR("could not convert pattern to regex string"));
193 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("%@"), *error
);
197 if (str
!= str_q
) CFAllocatorDeallocate(NULL
, str
);
202 static CFMutableArrayRef
203 patternCopy(CFStringRef pattern
)
207 pInfo
= CFDictionaryGetValue(patternData
, pattern
);
208 return pInfo
? CFArrayCreateMutableCopy(NULL
, 0, pInfo
) : NULL
;
212 static CFMutableArrayRef
213 patternNew(CFStringRef pattern
)
216 CFStringRef err
= NULL
;
217 CFMutableArrayRef pInfo
;
218 CFMutableDataRef pRegex
;
219 CFArrayRef pSessions
;
221 /* create the pattern info */
222 pInfo
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
224 /* compile the regular expression from the pattern string. */
225 pRegex
= CFDataCreateMutable(NULL
, sizeof(regex_t
));
226 CFDataSetLength(pRegex
, sizeof(regex_t
));
227 if (!patternCompile(pattern
, (regex_t
*)CFDataGetBytePtr(pRegex
), &err
)) {
234 /* add the compiled regular expression to the pattern info */
235 CFArrayAppendValue(pInfo
, pRegex
);
237 /* add the initial (empty) list of sessions watching this pattern */
238 pSessions
= CFArrayCreate(NULL
, NULL
, 0, &kCFTypeArrayCallBacks
);
239 CFArrayAppendValue(pInfo
, pSessions
);
240 CFRelease(pSessions
);
242 /* identify/add all existing keys that match the specified pattern */
243 context
.pInfo
= pInfo
;
244 context
.preg
= (regex_t
*)CFDataGetBytePtr(pRegex
);
245 my_CFDictionaryApplyFunction(storeData
,
246 (CFDictionaryApplierFunction
)identifyKeyForPattern
,
256 patternCopyMatches(CFStringRef pattern
)
258 Boolean isNew
= FALSE
;
260 CFMutableArrayRef pInfo
;
262 /* find (or create new instance of) this pattern */
263 pInfo
= patternCopy(pattern
);
266 pInfo
= patternNew(pattern
);
277 pRegex
= CFArrayGetValueAtIndex(pInfo
, 0);
278 regfree((regex_t
*)CFDataGetBytePtr(pRegex
));
281 CFArrayReplaceValues(pInfo
, CFRangeMake(0, 2), NULL
, 0);
282 keys
= CFArrayCreateCopy(NULL
, pInfo
);
291 patternAddSession(CFStringRef pattern
, CFNumberRef sessionNum
)
295 CFMutableArrayRef pInfo
;
296 CFMutableArrayRef pSessions
;
298 /* find (or create new instance of) this pattern */
299 pInfo
= patternCopy(pattern
);
302 pInfo
= patternNew(pattern
);
308 /* add this session as a pattern watcher */
309 pSessions
= (CFMutableArrayRef
)CFArrayGetValueAtIndex(pInfo
, 1);
310 pSessions
= CFArrayCreateMutableCopy(NULL
, 0, pSessions
);
311 CFArrayAppendValue(pSessions
, sessionNum
);
312 CFArraySetValueAtIndex(pInfo
, 1, pSessions
);
313 CFRelease(pSessions
);
315 /* update pattern watcher info */
316 CFDictionarySetValue(patternData
, pattern
, pInfo
);
318 /* add this session as a watcher of any existing keys */
319 n
= CFArrayGetCount(pInfo
);
320 for (i
= 2; i
< n
; i
++) {
321 CFStringRef matchingKey
;
323 matchingKey
= CFArrayGetValueAtIndex(pInfo
, i
);
324 _addWatcher(sessionNum
, matchingKey
);
334 patternRemoveSession(CFStringRef pattern
, CFNumberRef sessionNum
)
338 CFMutableArrayRef pInfo
;
340 CFMutableArrayRef pSessions
;
342 /* find instance of this pattern */
343 pInfo
= patternCopy(pattern
);
345 /* remove this session as a watcher from all matching keys */
346 n
= CFArrayGetCount(pInfo
);
347 for (i
= 2; i
< n
; i
++) {
348 CFStringRef matchingKey
;
350 matchingKey
= CFArrayGetValueAtIndex(pInfo
, i
);
351 _removeWatcher(sessionNum
, matchingKey
);
354 /* remove session from watchers */
355 pSessions
= (CFMutableArrayRef
)CFArrayGetValueAtIndex(pInfo
, 1);
356 n
= CFArrayGetCount(pSessions
);
358 /* if other sessions are watching this pattern */
360 pSessions
= CFArrayCreateMutableCopy(NULL
, 0, pSessions
);
361 i
= CFArrayGetFirstIndexOfValue(pSessions
, CFRangeMake(0, n
), sessionNum
);
362 CFArrayRemoveValueAtIndex(pSessions
, i
);
363 CFArraySetValueAtIndex(pInfo
, 1, pSessions
);
364 CFRelease(pSessions
);
366 CFDictionarySetValue(patternData
, pattern
, pInfo
);
368 /* if no other sessions are watching this pattern */
370 pRegex
= CFArrayGetValueAtIndex(pInfo
, 0);
371 regfree((regex_t
*)CFDataGetBytePtr(pRegex
));
372 CFDictionaryRemoveValue(patternData
, pattern
);
381 addKeyForPattern(const void *key
, void *val
, void *context
)
383 CFStringRef pattern
= (CFStringRef
)key
;
384 CFArrayRef pInfo
= (CFArrayRef
)val
;
385 CFStringRef storeKey
= (CFStringRef
)context
;
393 /* convert store key to C string */
394 len
= CFStringGetMaximumSizeForEncoding(CFStringGetLength(storeKey
), kCFStringEncodingASCII
) + 1;
395 if (len
> (CFIndex
)sizeof(str_q
))
396 str
= CFAllocatorAllocate(NULL
, len
, 0);
397 if (_SC_cfstring_to_cstring(storeKey
, str
, len
, kCFStringEncodingASCII
) == NULL
) {
398 SCLog(TRUE
, LOG_DEBUG
, CFSTR("addKeyForPattern(): could not convert store key to C string"));
402 /* compare new store key to regular expression pattern */
403 preg
= (regex_t
*)CFDataGetBytePtr(CFArrayGetValueAtIndex(pInfo
, 0));
404 reError
= regexec(preg
, str
, 0, NULL
, 0);
412 CFMutableArrayRef pInfo_new
;
413 CFArrayRef pSessions
;
416 pSessions
= CFArrayGetValueAtIndex(pInfo
, 1);
417 n
= CFArrayGetCount(pSessions
);
418 for (i
= 0; i
< n
; i
++) {
419 CFNumberRef sessionNum
= CFArrayGetValueAtIndex(pSessions
, i
);
421 _addWatcher(sessionNum
, storeKey
);
424 /* add key, update pattern watcher info */
425 pInfo_new
= CFArrayCreateMutableCopy(NULL
, 0, pInfo
);
426 CFArrayAppendValue(pInfo_new
, storeKey
);
427 CFDictionarySetValue(patternData
, pattern
, pInfo_new
);
428 CFRelease(pInfo_new
);
437 (void)regerror(reError
, preg
, reErrBuf
, sizeof(reErrBuf
));
438 SCLog(TRUE
, LOG_DEBUG
, CFSTR("addKeyForPattern regexec(): %s"), reErrBuf
);
445 if (str
!= str_q
) CFAllocatorDeallocate(NULL
, str
);
452 patternAddKey(CFStringRef key
)
454 void *context
= (void *)key
;
456 my_CFDictionaryApplyFunction(patternData
,
457 (CFDictionaryApplierFunction
)addKeyForPattern
,
465 removeKeyFromPattern(const void *key
, void *val
, void *context
)
467 CFStringRef pattern
= (CFStringRef
)key
;
468 CFArrayRef pInfo
= (CFArrayRef
)val
;
469 CFStringRef storeKey
= (CFStringRef
)context
;
473 CFMutableArrayRef pInfo_new
;
474 CFArrayRef pSessions
;
476 n
= CFArrayGetCount(pInfo
);
478 /* if no keys match this pattern */
482 i
= CFArrayGetFirstIndexOfValue(pInfo
, CFRangeMake(2, n
-2), storeKey
);
483 if (i
== kCFNotFound
) {
484 /* if this key wasn't matched by this pattern */
488 /* remove key from pattern info */
489 pInfo_new
= CFArrayCreateMutableCopy(NULL
, 0, pInfo
);
490 CFArrayRemoveValueAtIndex(pInfo_new
, i
);
492 /* remove watchers */
493 pSessions
= CFArrayGetValueAtIndex(pInfo_new
, 1);
494 n
= CFArrayGetCount(pSessions
);
495 for (i
= 0; i
< n
; i
++) {
496 CFNumberRef sessionNum
= CFArrayGetValueAtIndex(pSessions
, i
);
498 _removeWatcher(sessionNum
, storeKey
);
501 CFDictionarySetValue(patternData
, pattern
, pInfo_new
);
502 CFRelease(pInfo_new
);
509 patternRemoveKey(CFStringRef key
)
511 void *context
= (void *)key
;
513 my_CFDictionaryApplyFunction(patternData
,
514 (CFDictionaryApplierFunction
)removeKeyFromPattern
,