2 * Copyright (c) 2000-2005, 2009-2011, 2013, 2016, 2017 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 * June 1, 2001 Allan Nathanson <ajn@apple.com>
28 * - public API conversion
30 * November 9, 2000 Allan Nathanson <ajn@apple.com>
35 #include "dictionary.h"
40 do_dictInit(int argc
, char **argv
)
48 value
= CFDictionaryCreateMutable(NULL
50 ,&kCFTypeDictionaryKeyCallBacks
51 ,&kCFTypeDictionaryValueCallBacks
60 do_dictShow(int argc
, char **argv
)
65 SCPrint(TRUE
, stdout
, CFSTR("d.show: dictionary must be initialized.\n"));
69 SCPrint(TRUE
, stdout
, CFSTR("%@\n"), value
);
77 do_dictSetKey(int argc
, char **argv
)
79 CFMutableArrayRef array
= NULL
;
80 Boolean doArray
= FALSE
;
81 Boolean doBoolean
= FALSE
;
82 Boolean doData
= FALSE
;
83 Boolean doNumeric
= FALSE
;
85 CFMutableDictionaryRef newValue
;
89 SCPrint(TRUE
, stdout
, CFSTR("d.add: dictionary must be initialized.\n"));
93 if (!isA_CFDictionary(value
)) {
94 SCPrint(TRUE
, stdout
, CFSTR("d.add: data (fetched from configuration server) is not a dictionary.\n"));
98 key
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
102 if (strcmp(argv
[0], "*") == 0) {
103 /* if array requested */
105 } else if (strcmp(argv
[0], "-") == 0) {
106 /* if string values follow */
109 } else if (strcmp(argv
[0], "?") == 0) {
110 /* if boolean values requested */
112 } else if (strcmp(argv
[0], "%") == 0) {
113 /* if [hex] data values requested */
115 } else if (strcmp(argv
[0], "#") == 0) {
116 /* if numeric values requested */
119 /* it's not a special flag */
127 } else if (!doArray
&& (argc
== 0)) {
128 SCPrint(TRUE
, stdout
, CFSTR("d.add: no values.\n"));
134 array
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
139 if ((strcasecmp(argv
[0], "true") == 0) ||
140 (strcasecmp(argv
[0], "t" ) == 0) ||
141 (strcasecmp(argv
[0], "yes" ) == 0) ||
142 (strcasecmp(argv
[0], "y" ) == 0) ||
143 (strcmp (argv
[0], "1" ) == 0)) {
144 val
= CFRetain(kCFBooleanTrue
);
145 } else if ((strcasecmp(argv
[0], "false") == 0) ||
146 (strcasecmp(argv
[0], "f" ) == 0) ||
147 (strcasecmp(argv
[0], "no" ) == 0) ||
148 (strcasecmp(argv
[0], "n" ) == 0) ||
149 (strcmp (argv
[0], "0" ) == 0)) {
150 val
= CFRetain(kCFBooleanFalse
);
152 SCPrint(TRUE
, stdout
, CFSTR("d.add: invalid data.\n"));
153 if (doArray
) CFRelease(array
);
159 CFMutableDataRef data
;
164 n
= (int)strlen(argv
[0]);
166 SCPrint(TRUE
, stdout
, CFSTR("d.add: not enough bytes.\n"));
167 if (doArray
) CFRelease(array
);
172 data
= CFDataCreateMutable(NULL
, (n
/ 2));
173 CFDataSetLength(data
, (n
/ 2));
175 bytes
= (uint8_t *)CFDataGetBytePtr(data
);
176 for (i
= 0, j
= 0; i
< n
; i
+= 2, j
++) {
182 str
[1] = argv
[0][i
+ 1];
184 byte
= strtoul(str
, &end
, 16);
185 if ((*end
!= '\0') || (errno
!= 0)) {
195 SCPrint(TRUE
, stdout
, CFSTR("d.add: invalid data.\n"));
196 if (doArray
) CFRelease(array
);
202 } else if (doNumeric
) {
205 if (sscanf(argv
[0], "%d", &intValue
) == 1) {
206 val
= CFNumberCreate(NULL
, kCFNumberIntType
, &intValue
);
208 SCPrint(TRUE
, stdout
, CFSTR("d.add: invalid data.\n"));
209 if (doArray
) CFRelease(array
);
214 val
= (CFPropertyListRef
)CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
218 CFArrayAppendValue(array
, val
);
226 newValue
= CFDictionaryCreateMutableCopy(NULL
, 0, value
);
228 CFDictionarySetValue(newValue
, key
, array
);
230 } else if (val
!= NULL
) {
231 CFDictionarySetValue(newValue
, key
, val
);
244 do_dictRemoveKey(int argc
, char **argv
)
248 CFMutableDictionaryRef val
;
251 SCPrint(TRUE
, stdout
, CFSTR("d.remove: dictionary must be initialized.\n"));
255 if (!isA_CFDictionary(value
)) {
256 SCPrint(TRUE
, stdout
, CFSTR("d.remove: data (fetched from configuration server) is not a dictionary.\n"));
260 val
= CFDictionaryCreateMutableCopy(NULL
, 0, value
);
264 key
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
265 CFDictionaryRemoveValue((CFMutableDictionaryRef
)value
, key
);