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