]> git.saurik.com Git - apple/configd.git/blob - scutil.tproj/dictionary.c
configd-42.tar.gz
[apple/configd.git] / scutil.tproj / dictionary.c
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 /*
24 * Modification History
25 *
26 * June 1, 2001 Allan Nathanson <ajn@apple.com>
27 * - public API conversion
28 *
29 * November 9, 2000 Allan Nathanson <ajn@apple.com>
30 * - initial revision
31 */
32
33 #include "scutil.h"
34
35
36 //#include <stdlib.h>
37 //#include <limits.h>
38
39
40 void
41 do_dictInit(int argc, char **argv)
42 {
43 if (value != NULL) {
44 CFRelease(value);
45 }
46
47 value = CFDictionaryCreateMutable(NULL
48 ,0
49 ,&kCFTypeDictionaryKeyCallBacks
50 ,&kCFTypeDictionaryValueCallBacks
51 );
52
53 return;
54 }
55
56
57 void
58 do_dictShow(int argc, char **argv)
59 {
60 if (value == NULL) {
61 SCPrint(TRUE, stdout, CFSTR("d.show: dictionary must be initialized.\n"));
62 return;
63 }
64
65 SCPrint(TRUE, stdout, CFSTR("%@\n"), value);
66
67 return;
68 }
69
70
71 void
72 do_dictSetKey(int argc, char **argv)
73 {
74 CFMutableArrayRef array = NULL;
75 Boolean doArray = FALSE;
76 Boolean doBoolean = FALSE;
77 Boolean doNumeric = FALSE;
78 CFStringRef key;
79 CFTypeRef val;
80
81 if (value == NULL) {
82 SCPrint(TRUE, stdout, CFSTR("d.add: dictionary must be initialized.\n"));
83 return;
84 }
85
86 if (CFGetTypeID(value) != CFDictionaryGetTypeID()) {
87 SCPrint(TRUE, stdout, CFSTR("d.add: data (fetched from configuration server) is not a dictionary.\n"));
88 return;
89 }
90
91 val = CFDictionaryCreateMutableCopy(NULL, 0, value);
92 CFRelease(value);
93 value = val;
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 SCPrint(TRUE, stdout, CFSTR("d.add: no values.\n"));
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 val = 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 val = CFRetain(kCFBooleanFalse);
142 } else {
143 SCPrint(TRUE, stdout, CFSTR("d.add: invalid data.\n"));
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 val = CFNumberCreate(NULL, kCFNumberIntType, &intValue);
154 } else {
155 SCPrint(TRUE, stdout, CFSTR("d.add: invalid data.\n"));
156 if (doArray) {
157 CFRelease(array);
158 }
159 return;
160 }
161 } else {
162 val = (CFPropertyListRef)CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingMacRoman);
163 }
164
165 if (doArray) {
166 CFArrayAppendValue(array, val);
167 }
168
169 argv++; argc--;
170 }
171
172 if (doArray) {
173 val = array;
174 }
175
176 CFDictionarySetValue((CFMutableDictionaryRef)value, key, val);
177 CFRelease(val);
178 CFRelease(key);
179
180 return;
181 }
182
183
184 void
185 do_dictRemoveKey(int argc, char **argv)
186 {
187 CFStringRef key;
188 CFMutableDictionaryRef val;
189
190 if (value == NULL) {
191 SCPrint(TRUE, stdout, CFSTR("d.remove: dictionary must be initialized.\n"));
192 return;
193 }
194
195 if (CFGetTypeID(value) != CFDictionaryGetTypeID()) {
196 SCPrint(TRUE, stdout, CFSTR("d.remove: data (fetched from configuration server) is not a dictionary.\n"));
197 return;
198 }
199
200 val = CFDictionaryCreateMutableCopy(NULL, 0, value);
201 CFRelease(value);
202 value = val;
203
204 key = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingMacRoman);
205 CFDictionaryRemoveValue((CFMutableDictionaryRef)value, key);
206 CFRelease(key);
207
208 return;
209 }