]> git.saurik.com Git - apple/configd.git/blame - scutil.tproj/dictionary.c
configd-212.2.tar.gz
[apple/configd.git] / scutil.tproj / dictionary.c
CommitLineData
5958d7c0 1/*
edebe297 2 * Copyright (c) 2000-2004 Apple Computer, 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{
5958d7c0 79 CFMutableArrayRef array = NULL;
0fae82ee
A
80 Boolean doArray = FALSE;
81 Boolean doBoolean = FALSE;
82 Boolean doNumeric = FALSE;
83 CFStringRef key;
84 CFTypeRef val;
5958d7c0 85
0fae82ee
A
86 if (value == NULL) {
87 SCPrint(TRUE, stdout, CFSTR("d.add: dictionary must be initialized.\n"));
5958d7c0
A
88 return;
89 }
90
a5f60add 91 if (!isA_CFDictionary(value)) {
0fae82ee 92 SCPrint(TRUE, stdout, CFSTR("d.add: data (fetched from configuration server) is not a dictionary.\n"));
5958d7c0
A
93 return;
94 }
95
0fae82ee
A
96 val = CFDictionaryCreateMutableCopy(NULL, 0, value);
97 CFRelease(value);
98 value = val;
5958d7c0 99
dbf6a266 100 key = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingUTF8);
5958d7c0
A
101 argv++; argc--;
102
103 while (argc > 0) {
104 if (strcmp(argv[0], "*") == 0) {
105 /* if array requested */
106 doArray = TRUE;
107 } else if (strcmp(argv[0], "-") == 0) {
108 /* if string values requested */
109 } else if (strcmp(argv[0], "?") == 0) {
110 /* if boolean values requested */
111 doBoolean = TRUE;
112 } else if (strcmp(argv[0], "#") == 0) {
113 /* if numeric values requested */
114 doNumeric = TRUE;
115 } else {
116 /* it's not a special flag */
117 break;
118 }
119 argv++; argc--;
120 }
121
122 if (argc > 1) {
123 doArray = TRUE;
124 } else if (!doArray && (argc == 0)) {
0fae82ee 125 SCPrint(TRUE, stdout, CFSTR("d.add: no values.\n"));
5958d7c0
A
126 return;
127 }
128
129 if (doArray) {
130 array = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
131 }
132
133 while (argc > 0) {
134 if (doBoolean) {
135 if ((strcasecmp(argv[0], "true") == 0) ||
136 (strcasecmp(argv[0], "t" ) == 0) ||
137 (strcasecmp(argv[0], "yes" ) == 0) ||
138 (strcasecmp(argv[0], "y" ) == 0) ||
139 (strcmp (argv[0], "1" ) == 0)) {
0fae82ee 140 val = CFRetain(kCFBooleanTrue);
5958d7c0
A
141 } else if ((strcasecmp(argv[0], "false") == 0) ||
142 (strcasecmp(argv[0], "f" ) == 0) ||
143 (strcasecmp(argv[0], "no" ) == 0) ||
144 (strcasecmp(argv[0], "n" ) == 0) ||
145 (strcmp (argv[0], "0" ) == 0)) {
0fae82ee 146 val = CFRetain(kCFBooleanFalse);
5958d7c0 147 } else {
0fae82ee 148 SCPrint(TRUE, stdout, CFSTR("d.add: invalid data.\n"));
5958d7c0
A
149 if (doArray) {
150 CFRelease(array);
151 }
152 return;
153 }
154 } else if (doNumeric) {
155 int intValue;
156
157 if (sscanf(argv[0], "%d", &intValue) == 1) {
0fae82ee 158 val = CFNumberCreate(NULL, kCFNumberIntType, &intValue);
5958d7c0 159 } else {
0fae82ee 160 SCPrint(TRUE, stdout, CFSTR("d.add: invalid data.\n"));
5958d7c0
A
161 if (doArray) {
162 CFRelease(array);
163 }
164 return;
165 }
166 } else {
dbf6a266 167 val = (CFPropertyListRef)CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingUTF8);
5958d7c0
A
168 }
169
170 if (doArray) {
0fae82ee 171 CFArrayAppendValue(array, val);
5958d7c0
A
172 }
173
174 argv++; argc--;
175 }
176
177 if (doArray) {
0fae82ee 178 val = array;
5958d7c0
A
179 }
180
0fae82ee
A
181 CFDictionarySetValue((CFMutableDictionaryRef)value, key, val);
182 CFRelease(val);
5958d7c0
A
183 CFRelease(key);
184
185 return;
186}
187
188
dbf6a266 189__private_extern__
5958d7c0
A
190void
191do_dictRemoveKey(int argc, char **argv)
192{
5958d7c0 193 CFStringRef key;
0fae82ee 194 CFMutableDictionaryRef val;
5958d7c0 195
0fae82ee
A
196 if (value == NULL) {
197 SCPrint(TRUE, stdout, CFSTR("d.remove: dictionary must be initialized.\n"));
5958d7c0
A
198 return;
199 }
200
a5f60add 201 if (!isA_CFDictionary(value)) {
0fae82ee
A
202 SCPrint(TRUE, stdout, CFSTR("d.remove: data (fetched from configuration server) is not a dictionary.\n"));
203 return;
5958d7c0
A
204 }
205
0fae82ee
A
206 val = CFDictionaryCreateMutableCopy(NULL, 0, value);
207 CFRelease(value);
208 value = val;
209
dbf6a266 210 key = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingUTF8);
0fae82ee
A
211 CFDictionaryRemoveValue((CFMutableDictionaryRef)value, key);
212 CFRelease(key);
213
5958d7c0
A
214 return;
215}