Commit | Line | Data |
---|---|---|
5958d7c0 A |
1 | /* |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * The contents of this file constitute Original Code as defined in and | |
7 | * are subject to the Apple Public Source License Version 1.1 (the | |
8 | * "License"). You may not use this file except in compliance with the | |
9 | * License. Please obtain a copy of the License at | |
10 | * http://www.apple.com/publicsource and read it before using this file. | |
11 | * | |
12 | * This Original Code and all software distributed under the License are | |
13 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
16 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the | |
17 | * License for the specific language governing rights and limitations | |
18 | * under the License. | |
19 | * | |
20 | * @APPLE_LICENSE_HEADER_END@ | |
21 | */ | |
22 | ||
23 | #include "scutil.h" | |
24 | ||
25 | ||
26 | //#include <stdlib.h> | |
27 | //#include <limits.h> | |
28 | ||
29 | ||
30 | void | |
31 | do_dictInit(int argc, char **argv) | |
32 | { | |
33 | CFMutableDictionaryRef dict; | |
34 | ||
35 | if (data != NULL) { | |
36 | SCDHandleRelease(data); | |
37 | } | |
38 | ||
39 | data = SCDHandleInit(); | |
40 | dict = CFDictionaryCreateMutable(NULL | |
41 | ,0 | |
42 | ,&kCFTypeDictionaryKeyCallBacks | |
43 | ,&kCFTypeDictionaryValueCallBacks | |
44 | ); | |
45 | SCDHandleSetData(data, dict); | |
46 | CFRelease(dict); | |
47 | ||
48 | return; | |
49 | } | |
50 | ||
51 | ||
52 | void | |
53 | do_dictShow(int argc, char **argv) | |
54 | { | |
55 | int instance; | |
56 | CFPropertyListRef store; | |
57 | ||
58 | if (data == NULL) { | |
59 | SCDLog(LOG_INFO, CFSTR("d.show: dictionary must be initialized.")); | |
60 | return; | |
61 | } | |
62 | ||
63 | instance = SCDHandleGetInstance(data); | |
64 | store = SCDHandleGetData(data); | |
65 | ||
66 | SCDLog(LOG_NOTICE, CFSTR("dict (instance = %d) = \n\t%@"), instance, store); | |
67 | ||
68 | return; | |
69 | } | |
70 | ||
71 | ||
72 | void | |
73 | do_dictSetKey(int argc, char **argv) | |
74 | { | |
75 | CFPropertyListRef store; | |
76 | CFStringRef key; | |
77 | CFPropertyListRef value = NULL; | |
78 | CFMutableArrayRef array = NULL; | |
79 | boolean_t doArray = FALSE; | |
80 | boolean_t doBoolean = FALSE; | |
81 | boolean_t doNumeric = FALSE; | |
82 | ||
83 | if (data == NULL) { | |
84 | SCDLog(LOG_INFO, CFSTR("d.add: dictionary must be initialized.")); | |
85 | return; | |
86 | } | |
87 | ||
88 | store = SCDHandleGetData(data); | |
89 | if (CFGetTypeID(store) != CFDictionaryGetTypeID()) { | |
90 | SCDLog(LOG_INFO, CFSTR("d.add: data (fetched from configuration server) is not a dictionary")); | |
91 | return; | |
92 | } | |
93 | ||
94 | ||
95 | key = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingMacRoman); | |
96 | argv++; argc--; | |
97 | ||
98 | while (argc > 0) { | |
99 | if (strcmp(argv[0], "*") == 0) { | |
100 | /* if array requested */ | |
101 | doArray = TRUE; | |
102 | } else if (strcmp(argv[0], "-") == 0) { | |
103 | /* if string values requested */ | |
104 | } else if (strcmp(argv[0], "?") == 0) { | |
105 | /* if boolean values requested */ | |
106 | doBoolean = TRUE; | |
107 | } else if (strcmp(argv[0], "#") == 0) { | |
108 | /* if numeric values requested */ | |
109 | doNumeric = TRUE; | |
110 | } else { | |
111 | /* it's not a special flag */ | |
112 | break; | |
113 | } | |
114 | argv++; argc--; | |
115 | } | |
116 | ||
117 | if (argc > 1) { | |
118 | doArray = TRUE; | |
119 | } else if (!doArray && (argc == 0)) { | |
120 | SCDLog(LOG_INFO, CFSTR("d.add: no values")); | |
121 | return; | |
122 | } | |
123 | ||
124 | if (doArray) { | |
125 | array = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks); | |
126 | } | |
127 | ||
128 | while (argc > 0) { | |
129 | if (doBoolean) { | |
130 | if ((strcasecmp(argv[0], "true") == 0) || | |
131 | (strcasecmp(argv[0], "t" ) == 0) || | |
132 | (strcasecmp(argv[0], "yes" ) == 0) || | |
133 | (strcasecmp(argv[0], "y" ) == 0) || | |
134 | (strcmp (argv[0], "1" ) == 0)) { | |
135 | value = CFRetain(kCFBooleanTrue); | |
136 | } else if ((strcasecmp(argv[0], "false") == 0) || | |
137 | (strcasecmp(argv[0], "f" ) == 0) || | |
138 | (strcasecmp(argv[0], "no" ) == 0) || | |
139 | (strcasecmp(argv[0], "n" ) == 0) || | |
140 | (strcmp (argv[0], "0" ) == 0)) { | |
141 | value = CFRetain(kCFBooleanFalse); | |
142 | } else { | |
143 | SCDLog(LOG_INFO, CFSTR("d.add: invalid data")); | |
144 | if (doArray) { | |
145 | CFRelease(array); | |
146 | } | |
147 | return; | |
148 | } | |
149 | } else if (doNumeric) { | |
150 | int intValue; | |
151 | ||
152 | if (sscanf(argv[0], "%d", &intValue) == 1) { | |
153 | value = CFNumberCreate(NULL, kCFNumberIntType, &intValue); | |
154 | } else { | |
155 | SCDLog(LOG_INFO, CFSTR("d.add: invalid data")); | |
156 | if (doArray) { | |
157 | CFRelease(array); | |
158 | } | |
159 | return; | |
160 | } | |
161 | } else { | |
162 | value = (CFPropertyListRef)CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingMacRoman); | |
163 | } | |
164 | ||
165 | if (doArray) { | |
166 | CFArrayAppendValue(array, value); | |
167 | } | |
168 | ||
169 | argv++; argc--; | |
170 | } | |
171 | ||
172 | if (doArray) { | |
173 | value = array; | |
174 | } | |
175 | ||
176 | CFDictionarySetValue((CFMutableDictionaryRef)store, key, value); | |
177 | CFRelease(value); | |
178 | CFRelease(key); | |
179 | ||
180 | return; | |
181 | } | |
182 | ||
183 | ||
184 | void | |
185 | do_dictRemoveKey(int argc, char **argv) | |
186 | { | |
187 | CFPropertyListRef store; | |
188 | CFStringRef key; | |
189 | ||
190 | if (data == NULL) { | |
191 | SCDLog(LOG_INFO, CFSTR("d.remove: dictionary must be initialized.")); | |
192 | return; | |
193 | } | |
194 | ||
195 | store = SCDHandleGetData(data); | |
196 | if (CFGetTypeID(store) == CFDictionaryGetTypeID()) { | |
197 | key = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingMacRoman); | |
198 | CFDictionaryRemoveValue((CFMutableDictionaryRef)store, key); | |
199 | CFRelease(key); | |
200 | } else { | |
201 | SCDLog(LOG_INFO, CFSTR("d.add: data (fetched from configuration server) is not a dictionary")); | |
202 | } | |
203 | ||
204 | return; | |
205 | } |