]> git.saurik.com Git - apple/configd.git/blob - configd.tproj/pattern.c
configd-84.6.tar.gz
[apple/configd.git] / configd.tproj / pattern.c
1 /*
2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 /*
25 * Modification History
26 *
27 * April 23, 2003 Allan Nathanson <ajn@apple.com>
28 * - initial revision
29 */
30
31
32 #include "configd.h"
33 #include "pattern.h"
34
35
36 /*
37 * Notes:
38 *
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
45 */
46
47
48 typedef struct {
49 CFMutableArrayRef pInfo;
50 regex_t *preg;
51 } addContext, *addContextRef;
52
53
54 static __inline__ void
55 my_CFDictionaryApplyFunction(CFDictionaryRef theDict,
56 CFDictionaryApplierFunction applier,
57 void *context)
58 {
59 CFAllocatorRef myAllocator;
60 CFDictionaryRef myDict;
61
62 myAllocator = CFGetAllocator(theDict);
63 myDict = CFDictionaryCreateCopy(myAllocator, theDict);
64 CFDictionaryApplyFunction(myDict, applier, context);
65 CFRelease(myDict);
66 return;
67 }
68
69
70 static void
71 identifyKeyForPattern(const void *key, void *val, void *context)
72 {
73 CFStringRef storeKey = (CFStringRef)key;
74 CFDictionaryRef storeValue = (CFDictionaryRef)val;
75 CFMutableArrayRef pInfo = ((addContextRef)context)->pInfo;
76 regex_t * preg = ((addContextRef)context)->preg;
77
78 CFIndex len;
79 int reError;
80 char str_q[256];
81 char * str = str_q;
82
83 if (!CFDictionaryContainsKey(storeValue, kSCDData)) {
84 /* if no data (yet) */
85 return;
86 }
87
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"));
94 goto done;
95 }
96
97 /* compare store key to new notification keys regular expression pattern */
98 reError = regexec(preg, str, 0, NULL, 0);
99 switch (reError) {
100 case 0 :
101 /* we've got a match */
102 CFArrayAppendValue(pInfo, storeKey);
103 break;
104 case REG_NOMATCH :
105 /* no match */
106 break;
107 default : {
108 char reErrBuf[256];
109
110 (void)regerror(reError, preg, reErrBuf, sizeof(reErrBuf));
111 SCLog(_configd_verbose, LOG_DEBUG, CFSTR("regexec(): %s"), reErrBuf);
112 break;
113 }
114 }
115
116 done :
117
118 if (str != str_q) CFAllocatorDeallocate(NULL, str);
119 return;
120 }
121
122
123 __private_extern__ Boolean
124 patternCompile(CFStringRef pattern, regex_t *preg, CFStringRef *error)
125 {
126 Boolean append = FALSE;
127 Boolean insert = FALSE;
128 CFIndex len = 0;
129 Boolean ok;
130 char str_q[256];
131 char * str = str_q;
132
133 if (!CFStringHasPrefix(pattern, CFSTR("^"))) {
134 insert = TRUE;
135 }
136
137 if (!CFStringHasSuffix(pattern, CFSTR("$")) ||
138 CFStringHasSuffix(pattern, CFSTR("\\$"))) {
139 append = TRUE;
140 }
141
142 /* if regex pattern is not bounded at both ends */
143 if (insert || append) {
144 pattern = CFStringCreateWithFormat(NULL,
145 NULL,
146 CFSTR("%s%@%s"),
147 insert ? "^" : "",
148 pattern,
149 append ? "$" : "");
150 }
151
152 (void)CFStringGetBytes(pattern,
153 CFRangeMake(0, CFStringGetLength(pattern)),
154 kCFStringEncodingASCII,
155 0,
156 FALSE,
157 NULL,
158 0,
159 &len);
160 if (++len > (CFIndex)sizeof(str_q)) {
161 str = CFAllocatorAllocate(NULL, len, 0);
162 }
163 ok = (_SC_cfstring_to_cstring(pattern, str, len, kCFStringEncodingASCII) != NULL);
164 if (insert || append) {
165 CFRelease(pattern);
166 }
167 if (ok) {
168 int reError;
169
170 reError = regcomp(preg, str, REG_EXTENDED);
171 if (reError != 0) {
172 char reErrBuf[256];
173
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);
177 ok = FALSE;
178 }
179 } else {
180 *error = CFRetain(CFSTR("could not convert pattern to regex string"));
181 SCLog(_configd_verbose, LOG_DEBUG, CFSTR("%@"), *error);
182 }
183
184 if (str != str_q) CFAllocatorDeallocate(NULL, str);
185 return ok;
186 }
187
188
189 __private_extern__
190 CFMutableArrayRef
191 patternCopy(CFStringRef pattern)
192 {
193 CFArrayRef pInfo;
194
195 pInfo = CFDictionaryGetValue(patternData, pattern);
196 return pInfo ? CFArrayCreateMutableCopy(NULL, 0, pInfo) : NULL;
197 }
198
199
200 __private_extern__
201 CFMutableArrayRef
202 patternNew(CFStringRef pattern)
203 {
204 addContext context;
205 CFStringRef err;
206 CFMutableArrayRef pInfo;
207 CFMutableDataRef pRegex;
208 CFArrayRef pSessions;
209
210 /* create the pattern info */
211 pInfo = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
212
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)) {
217 CFRelease(err);
218 CFRelease(pRegex);
219 CFRelease(pInfo);
220 return NULL;
221 }
222
223 /* add the compiled regular expression to the pattern info */
224 CFArrayAppendValue(pInfo, pRegex);
225
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);
230
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,
236 &context);
237
238 CFRelease(pRegex);
239 return pInfo;
240 }
241
242
243 __private_extern__
244 Boolean
245 patternAddSession(CFStringRef pattern, CFNumberRef sessionNum)
246 {
247 CFIndex i;
248 CFIndex n;
249 CFMutableArrayRef pInfo;
250 CFMutableArrayRef pSessions;
251
252 /* find (or create new instance of) this pattern */
253 pInfo = patternCopy(pattern);
254 if (!pInfo) {
255 /* if new pattern */
256 pInfo = patternNew(pattern);
257 if (!pInfo) {
258 return FALSE;
259 }
260 }
261
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);
268
269 /* update pattern watcher info */
270 CFDictionarySetValue(patternData, pattern, pInfo);
271
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;
276
277 matchingKey = CFArrayGetValueAtIndex(pInfo, i);
278 _addWatcher(sessionNum, matchingKey);
279 }
280
281 CFRelease(pInfo);
282 return TRUE;
283 }
284
285
286 __private_extern__
287 void
288 patternRemoveSession(CFStringRef pattern, CFNumberRef sessionNum)
289 {
290 CFIndex i;
291 CFIndex n;
292 CFMutableArrayRef pInfo;
293 CFDataRef pRegex;
294 CFMutableArrayRef pSessions;
295
296 /* find instance of this pattern */
297 pInfo = patternCopy(pattern);
298
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;
303
304 matchingKey = CFArrayGetValueAtIndex(pInfo, i);
305 _removeWatcher(sessionNum, matchingKey);
306 }
307
308 /* remove session from watchers */
309 pSessions = (CFMutableArrayRef)CFArrayGetValueAtIndex(pInfo, 1);
310 n = CFArrayGetCount(pSessions);
311 if (n > 1) {
312 /* if other sessions are watching this pattern */
313
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);
319
320 CFDictionarySetValue(patternData, pattern, pInfo);
321 } else {
322 /* if no other sessions are watching this pattern */
323
324 pRegex = CFArrayGetValueAtIndex(pInfo, 0);
325 regfree((regex_t *)CFDataGetBytePtr(pRegex));
326 CFDictionaryRemoveValue(patternData, pattern);
327 }
328
329 CFRelease(pInfo);
330 return;
331 }
332
333
334 static void
335 addKeyForPattern(const void *key, void *val, void *context)
336 {
337 CFStringRef pattern = (CFStringRef)key;
338 CFArrayRef pInfo = (CFArrayRef)val;
339 CFStringRef storeKey = (CFStringRef)context;
340
341 CFIndex len;
342 regex_t *preg;
343 int reError;
344 char str_q[256];
345 char * str = str_q;
346
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"));
353 goto done;
354 }
355
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);
359 switch (reError) {
360 case 0 : {
361 /*
362 * we've got a match
363 */
364 CFIndex i;
365 CFIndex n;
366 CFMutableArrayRef pInfo_new;
367 CFArrayRef pSessions;
368
369 /* add watchers */
370 pSessions = CFArrayGetValueAtIndex(pInfo, 1);
371 n = CFArrayGetCount(pSessions);
372 for (i = 0; i < n; i++) {
373 CFNumberRef sessionNum = CFArrayGetValueAtIndex(pSessions, i);
374
375 _addWatcher(sessionNum, storeKey);
376 }
377
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);
383 break;
384 }
385 case REG_NOMATCH :
386 /* no match */
387 break;
388 default : {
389 char reErrBuf[256];
390
391 (void)regerror(reError, preg, reErrBuf, sizeof(reErrBuf));
392 SCLog(_configd_verbose, LOG_DEBUG, CFSTR("regexec(): %s"), reErrBuf);
393 break;
394 }
395 }
396
397 done :
398
399 if (str != str_q) CFAllocatorDeallocate(NULL, str);
400 return;
401 }
402
403
404 __private_extern__
405 void
406 patternAddKey(CFStringRef key)
407 {
408 my_CFDictionaryApplyFunction(patternData,
409 (CFDictionaryApplierFunction)addKeyForPattern,
410 (void *)key);
411
412 return;
413 }
414
415
416 static void
417 removeKeyFromPattern(const void *key, void *val, void *context)
418 {
419 CFStringRef pattern = (CFStringRef)key;
420 CFArrayRef pInfo = (CFArrayRef)val;
421 CFStringRef storeKey = (CFStringRef)context;
422
423 CFIndex i;
424 CFIndex n;
425 CFMutableArrayRef pInfo_new;
426 CFArrayRef pSessions;
427
428 n = CFArrayGetCount(pInfo);
429 if (n <= 2) {
430 /* if no keys match this pattern */
431 return;
432 }
433
434 i = CFArrayGetFirstIndexOfValue(pInfo, CFRangeMake(2, n-2), storeKey);
435 if (i == -1) {
436 /* if this key wasn't matched by this pattern */
437 return;
438 }
439
440 /* remove key from pattern info */
441 pInfo_new = CFArrayCreateMutableCopy(NULL, 0, pInfo);
442 CFArrayRemoveValueAtIndex(pInfo_new, i);
443
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);
449
450 _removeWatcher(sessionNum, storeKey);
451 }
452
453 CFDictionarySetValue(patternData, pattern, pInfo_new);
454 CFRelease(pInfo_new);
455 return;
456 }
457
458
459 __private_extern__
460 void
461 patternRemoveKey(CFStringRef key)
462 {
463 my_CFDictionaryApplyFunction(patternData,
464 (CFDictionaryApplierFunction)removeKeyFromPattern,
465 (void *)key);
466
467 return;
468 }