]> git.saurik.com Git - apple/configd.git/blame - scutil.tproj/dictionary.c
configd-395.11.tar.gz
[apple/configd.git] / scutil.tproj / dictionary.c
CommitLineData
5958d7c0 1/*
6bb65964 2 * Copyright (c) 2000-2004, 2009, 2010 Apple Inc. All rights reserved.
5958d7c0
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
009ee3c6 5 *
009ee3c6
A
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
5958d7c0
A
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
009ee3c6
A
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 *
5958d7c0
A
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
0fae82ee
A
24/*
25 * Modification History
26 *
27 * June 1, 2001 Allan Nathanson <ajn@apple.com>
28 * - public API conversion
29 *
30 * November 9, 2000 Allan Nathanson <ajn@apple.com>
31 * - initial revision
32 */
33
5958d7c0 34#include "scutil.h"
009ee3c6 35#include "dictionary.h"
5958d7c0
A
36
37
38//#include <stdlib.h>
39//#include <limits.h>
40
41
dbf6a266 42__private_extern__
5958d7c0
A
43void
44do_dictInit(int argc, char **argv)
45{
0fae82ee
A
46 if (value != NULL) {
47 CFRelease(value);
5958d7c0
A
48 }
49
0fae82ee 50 value = CFDictionaryCreateMutable(NULL
5958d7c0
A
51 ,0
52 ,&kCFTypeDictionaryKeyCallBacks
53 ,&kCFTypeDictionaryValueCallBacks
54 );
5958d7c0
A
55
56 return;
57}
58
59
dbf6a266 60__private_extern__
5958d7c0
A
61void
62do_dictShow(int argc, char **argv)
63{
0fae82ee
A
64 if (value == NULL) {
65 SCPrint(TRUE, stdout, CFSTR("d.show: dictionary must be initialized.\n"));
5958d7c0
A
66 return;
67 }
68
0fae82ee 69 SCPrint(TRUE, stdout, CFSTR("%@\n"), value);
5958d7c0
A
70
71 return;
72}
73
74
dbf6a266 75__private_extern__
5958d7c0
A
76void
77do_dictSetKey(int argc, char **argv)
78{
a40a14f8
A
79 CFMutableArrayRef array = NULL;
80 Boolean doArray = FALSE;
81 Boolean doBoolean = FALSE;
82 Boolean doNumeric = FALSE;
0fae82ee 83 CFStringRef key;
a40a14f8
A
84 CFMutableDictionaryRef newValue;
85 CFTypeRef val = NULL;
5958d7c0 86
0fae82ee
A
87 if (value == NULL) {
88 SCPrint(TRUE, stdout, CFSTR("d.add: dictionary must be initialized.\n"));
5958d7c0
A
89 return;
90 }
91
a5f60add 92 if (!isA_CFDictionary(value)) {
0fae82ee 93 SCPrint(TRUE, stdout, CFSTR("d.add: data (fetched from configuration server) is not a dictionary.\n"));
5958d7c0
A
94 return;
95 }
96
dbf6a266 97 key = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingUTF8);
5958d7c0
A
98 argv++; argc--;
99
100 while (argc > 0) {
101 if (strcmp(argv[0], "*") == 0) {
102 /* if array requested */
103 doArray = TRUE;
104 } else if (strcmp(argv[0], "-") == 0) {
105 /* if string values requested */
106 } else if (strcmp(argv[0], "?") == 0) {
107 /* if boolean values requested */
108 doBoolean = TRUE;
109 } else if (strcmp(argv[0], "#") == 0) {
110 /* if numeric values requested */
111 doNumeric = TRUE;
112 } else {
113 /* it's not a special flag */
114 break;
115 }
116 argv++; argc--;
117 }
118
119 if (argc > 1) {
120 doArray = TRUE;
121 } else if (!doArray && (argc == 0)) {
0fae82ee 122 SCPrint(TRUE, stdout, CFSTR("d.add: no values.\n"));
a40a14f8 123 CFRelease(key);
5958d7c0
A
124 return;
125 }
126
127 if (doArray) {
128 array = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
129 }
130
131 while (argc > 0) {
132 if (doBoolean) {
133 if ((strcasecmp(argv[0], "true") == 0) ||
134 (strcasecmp(argv[0], "t" ) == 0) ||
135 (strcasecmp(argv[0], "yes" ) == 0) ||
136 (strcasecmp(argv[0], "y" ) == 0) ||
137 (strcmp (argv[0], "1" ) == 0)) {
0fae82ee 138 val = CFRetain(kCFBooleanTrue);
5958d7c0
A
139 } else if ((strcasecmp(argv[0], "false") == 0) ||
140 (strcasecmp(argv[0], "f" ) == 0) ||
141 (strcasecmp(argv[0], "no" ) == 0) ||
142 (strcasecmp(argv[0], "n" ) == 0) ||
143 (strcmp (argv[0], "0" ) == 0)) {
0fae82ee 144 val = CFRetain(kCFBooleanFalse);
5958d7c0 145 } else {
0fae82ee 146 SCPrint(TRUE, stdout, CFSTR("d.add: invalid data.\n"));
a40a14f8
A
147 if (doArray) CFRelease(array);
148 CFRelease(key);
5958d7c0
A
149 return;
150 }
151 } else if (doNumeric) {
152 int intValue;
153
154 if (sscanf(argv[0], "%d", &intValue) == 1) {
0fae82ee 155 val = CFNumberCreate(NULL, kCFNumberIntType, &intValue);
5958d7c0 156 } else {
0fae82ee 157 SCPrint(TRUE, stdout, CFSTR("d.add: invalid data.\n"));
a40a14f8
A
158 if (doArray) CFRelease(array);
159 CFRelease(key);
5958d7c0
A
160 return;
161 }
162 } else {
dbf6a266 163 val = (CFPropertyListRef)CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingUTF8);
5958d7c0
A
164 }
165
166 if (doArray) {
0fae82ee 167 CFArrayAppendValue(array, val);
a40a14f8
A
168 CFRelease(val);
169 argv++; argc--;
170 } else {
171 break;
5958d7c0 172 }
5958d7c0
A
173 }
174
a40a14f8 175 newValue = CFDictionaryCreateMutableCopy(NULL, 0, value);
5958d7c0 176 if (doArray) {
a40a14f8
A
177 CFDictionarySetValue(newValue, key, array);
178 CFRelease(array);
6bb65964 179 } else if (val != NULL) {
a40a14f8
A
180 CFDictionarySetValue(newValue, key, val);
181 CFRelease(val);
5958d7c0 182 }
5958d7c0 183 CFRelease(key);
a40a14f8
A
184 CFRelease(value);
185 value = newValue;
5958d7c0
A
186
187 return;
188}
189
190
dbf6a266 191__private_extern__
5958d7c0
A
192void
193do_dictRemoveKey(int argc, char **argv)
194{
5958d7c0 195 CFStringRef key;
0fae82ee 196 CFMutableDictionaryRef val;
5958d7c0 197
0fae82ee
A
198 if (value == NULL) {
199 SCPrint(TRUE, stdout, CFSTR("d.remove: dictionary must be initialized.\n"));
5958d7c0
A
200 return;
201 }
202
a5f60add 203 if (!isA_CFDictionary(value)) {
0fae82ee
A
204 SCPrint(TRUE, stdout, CFSTR("d.remove: data (fetched from configuration server) is not a dictionary.\n"));
205 return;
5958d7c0
A
206 }
207
0fae82ee
A
208 val = CFDictionaryCreateMutableCopy(NULL, 0, value);
209 CFRelease(value);
210 value = val;
211
dbf6a266 212 key = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingUTF8);
0fae82ee
A
213 CFDictionaryRemoveValue((CFMutableDictionaryRef)value, key);
214 CFRelease(key);
215
5958d7c0
A
216 return;
217}