]> git.saurik.com Git - apple/configd.git/blob - scutil.tproj/dictionary.c
configd-84.6.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 * 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 * 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
34 #include "scutil.h"
35 #include "dictionary.h"
36
37
38 //#include <stdlib.h>
39 //#include <limits.h>
40
41
42 void
43 do_dictInit(int argc, char **argv)
44 {
45 if (value != NULL) {
46 CFRelease(value);
47 }
48
49 value = CFDictionaryCreateMutable(NULL
50 ,0
51 ,&kCFTypeDictionaryKeyCallBacks
52 ,&kCFTypeDictionaryValueCallBacks
53 );
54
55 return;
56 }
57
58
59 void
60 do_dictShow(int argc, char **argv)
61 {
62 if (value == NULL) {
63 SCPrint(TRUE, stdout, CFSTR("d.show: dictionary must be initialized.\n"));
64 return;
65 }
66
67 SCPrint(TRUE, stdout, CFSTR("%@\n"), value);
68
69 return;
70 }
71
72
73 void
74 do_dictSetKey(int argc, char **argv)
75 {
76 CFMutableArrayRef array = NULL;
77 Boolean doArray = FALSE;
78 Boolean doBoolean = FALSE;
79 Boolean doNumeric = FALSE;
80 CFStringRef key;
81 CFTypeRef val;
82
83 if (value == NULL) {
84 SCPrint(TRUE, stdout, CFSTR("d.add: dictionary must be initialized.\n"));
85 return;
86 }
87
88 if (!isA_CFDictionary(value)) {
89 SCPrint(TRUE, stdout, CFSTR("d.add: data (fetched from configuration server) is not a dictionary.\n"));
90 return;
91 }
92
93 val = CFDictionaryCreateMutableCopy(NULL, 0, value);
94 CFRelease(value);
95 value = val;
96
97 key = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingMacRoman);
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)) {
122 SCPrint(TRUE, stdout, CFSTR("d.add: no values.\n"));
123 return;
124 }
125
126 if (doArray) {
127 array = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
128 }
129
130 while (argc > 0) {
131 if (doBoolean) {
132 if ((strcasecmp(argv[0], "true") == 0) ||
133 (strcasecmp(argv[0], "t" ) == 0) ||
134 (strcasecmp(argv[0], "yes" ) == 0) ||
135 (strcasecmp(argv[0], "y" ) == 0) ||
136 (strcmp (argv[0], "1" ) == 0)) {
137 val = CFRetain(kCFBooleanTrue);
138 } else if ((strcasecmp(argv[0], "false") == 0) ||
139 (strcasecmp(argv[0], "f" ) == 0) ||
140 (strcasecmp(argv[0], "no" ) == 0) ||
141 (strcasecmp(argv[0], "n" ) == 0) ||
142 (strcmp (argv[0], "0" ) == 0)) {
143 val = CFRetain(kCFBooleanFalse);
144 } else {
145 SCPrint(TRUE, stdout, CFSTR("d.add: invalid data.\n"));
146 if (doArray) {
147 CFRelease(array);
148 }
149 return;
150 }
151 } else if (doNumeric) {
152 int intValue;
153
154 if (sscanf(argv[0], "%d", &intValue) == 1) {
155 val = CFNumberCreate(NULL, kCFNumberIntType, &intValue);
156 } else {
157 SCPrint(TRUE, stdout, CFSTR("d.add: invalid data.\n"));
158 if (doArray) {
159 CFRelease(array);
160 }
161 return;
162 }
163 } else {
164 val = (CFPropertyListRef)CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingMacRoman);
165 }
166
167 if (doArray) {
168 CFArrayAppendValue(array, val);
169 }
170
171 argv++; argc--;
172 }
173
174 if (doArray) {
175 val = array;
176 }
177
178 CFDictionarySetValue((CFMutableDictionaryRef)value, key, val);
179 CFRelease(val);
180 CFRelease(key);
181
182 return;
183 }
184
185
186 void
187 do_dictRemoveKey(int argc, char **argv)
188 {
189 CFStringRef key;
190 CFMutableDictionaryRef val;
191
192 if (value == NULL) {
193 SCPrint(TRUE, stdout, CFSTR("d.remove: dictionary must be initialized.\n"));
194 return;
195 }
196
197 if (!isA_CFDictionary(value)) {
198 SCPrint(TRUE, stdout, CFSTR("d.remove: data (fetched from configuration server) is not a dictionary.\n"));
199 return;
200 }
201
202 val = CFDictionaryCreateMutableCopy(NULL, 0, value);
203 CFRelease(value);
204 value = val;
205
206 key = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingMacRoman);
207 CFDictionaryRemoveValue((CFMutableDictionaryRef)value, key);
208 CFRelease(key);
209
210 return;
211 }