2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 * Modification History
27 * June 1, 2001 Allan Nathanson <ajn@apple.com>
28 * - public API conversion
30 * November 9, 2000 Allan Nathanson <ajn@apple.com>
36 #include <sys/errno.h>
40 #include "dictionary.h"
43 #include "notifications.h"
48 #include "SCDynamicStoreInternal.h"
52 const cmdInfo commands_store
[] = {
53 /* cmd minArgs maxArgs func group ctype */
56 { "help", 0, 0, do_help
, 0, 0,
57 " help : list available commands" },
59 { "f.read", 1, 1, do_readFile
, 0, 0,
60 " f.read file : process commands from file" },
62 { "quit", 0, 0, do_quit
, 0, 0,
65 { "q", 0, 0, do_quit
, 0, -1,
68 { "exit", 0, 0, do_quit
, 0, -1,
71 /* local dictionary manipulation commands */
73 { "d.init", 0, 0, do_dictInit
, 1, 0,
74 " d.init : initialize (empty) dictionary" },
76 { "d.show", 0, 0, do_dictShow
, 1, 0,
77 " d.show : show dictionary contents" },
79 { "d.add", 2, 101, do_dictSetKey
, 1, 0,
80 " d.add key [*#?] val [v2 ...] : add information to dictionary\n"
81 " (*=array, #=number, ?=boolean)" },
83 { "d.remove", 1, 1, do_dictRemoveKey
, 1, 0,
84 " d.remove key : remove key from dictionary" },
86 /* dynamic store manipulation commands */
88 { "open", 0, 1, do_open
, 2, 1,
89 " open [\"temporary\"] : open a session with \"configd\"" },
91 { "close", 0, 0, do_close
, 2, 1,
92 " close : close current \"configd\" session" },
94 { "lock", 0, 0, do_lock
, 3, 1,
95 " lock : locks write access to data store" },
97 { "unlock", 0, 0, do_unlock
, 3, 1,
98 " unlock : unlocks write access to data store" },
100 { "list", 0, 2, do_list
, 4, 0,
101 " list [pattern] : list keys in data store" },
103 { "add", 1, 2, do_add
, 4, 0,
104 " add key [\"temporary\"] : add key in data store w/current dict" },
106 { "get", 1, 1, do_get
, 4, 0,
107 " get key : get dict from data store w/key" },
109 { "set", 1, 1, do_set
, 4, 0,
110 " set key : set key in data store w/current dict" },
112 { "show", 1, 2, do_show
, 4, 0,
113 " show key [\"pattern\"] : show values in data store w/key" },
115 { "remove", 1, 1, do_remove
, 4, 0,
116 " remove key : remove key from data store" },
118 { "notify", 1, 1, do_notify
, 4, 0,
119 " notify key : notify key in data store" },
121 { "touch", 1, 1, do_touch
, 4, 1,
122 " touch key : touch key in data store" },
124 { "n.list", 0, 1, do_notify_list
, 5, 0,
125 " n.list [\"pattern\"] : list notification keys" },
127 { "n.add", 1, 2, do_notify_add
, 5, 0,
128 " n.add key [\"pattern\"] : add notification key" },
130 { "n.remove", 1, 2, do_notify_remove
, 5, 0,
131 " n.remove key [\"pattern\"] : remove notification key" },
133 { "n.changes", 0, 0, do_notify_changes
, 5, 0,
134 " n.changes : list changed keys" },
136 { "n.watch", 0, 1, do_notify_watch
, 5, 0,
137 " n.watch [verbose] : watch for changes" },
139 { "n.wait", 0, 0, do_notify_wait
, 5, 2,
140 " n.wait : wait for changes" },
142 { "n.callback", 0, 1, do_notify_callback
, 5, 2,
143 " n.callback [\"verbose\"] : watch for changes" },
145 { "n.signal", 1, 2, do_notify_signal
, 5, 2,
146 " n.signal sig [pid] : signal changes" },
148 { "n.file", 0, 1, do_notify_file
, 5, 2,
149 " n.file [identifier] : watch for changes via file" },
151 { "n.cancel", 0, 1, do_notify_cancel
, 5, 0,
152 " n.cancel : cancel notification requests" },
154 { "snapshot", 0, 0, do_snapshot
, 99, 2,
155 " snapshot : save snapshot of store and session data" }
158 const int nCommands_store
= (sizeof(commands_store
)/sizeof(cmdInfo
));
162 const cmdInfo commands_net
[] = {
163 /* cmd minArgs maxArgs func group ctype */
166 { "help", 0, 0, do_help
, 0, 0,
167 " help : list available commands" },
169 { "f.read", 1, 1, do_readFile
, 0, 0,
170 " f.read file : process commands from file" },
172 { "quit", 0, 1, do_net_quit
, 0, 0,
173 " quit [!] : quit" },
175 { "q", 0, 1, do_net_quit
, 0, -1,
178 { "exit", 0, 1, do_net_quit
, 0, -1,
181 /* network configuration manipulation commands */
183 { "open", 0, 1, do_net_open
, 2, 1,
184 " open : open the network configuration" },
186 { "commit", 0, 0, do_net_commit
, 2, 0,
187 " commit : commit any changes" },
189 { "apply", 0, 0, do_net_apply
, 2, 0,
190 " apply : apply any changes" },
192 { "close", 0, 1, do_net_close
, 2, 1,
193 " close [!] : close the network configuration" },
195 { "create", 1, 3, do_net_create
, 3, 0,
196 " create interface <interfaceType> [ <interfaceName> | <interface#> ]\n"
197 " create protocol <protocolType>\n"
198 " create service [ <interfaceName> | <interface#> [ <serviceName> ]]\n"
199 " create set [setName]" },
201 { "disable", 1, 2, do_net_disable
, 4, 0,
202 " disable protocol [ <protocolType> ]\n"
203 " disable service [ <serviceName> | <service#> ]" },
205 { "enable", 1, 2, do_net_enable
, 5, 0,
206 " enable protocol [ <protocolType> ]\n"
207 " enable service [ <serviceName> | <service#> ]" },
209 { "remove", 1, 2, do_net_remove
, 6, 0,
210 " remove protocol [ <protocolType> ]\n"
211 " remove service [ <serviceName> | <service#> ]\n"
212 " remove set [ <setName> | <set#> ]" },
214 { "select", 2, 2, do_net_select
, 7, 0,
215 " select interface <interfaceName> | <interface#> | $child | $service\n"
216 " select protocol <protocolType>\n"
217 " select service <serviceName> | <service#>\n"
218 " select set <setName> | <set#>" },
220 { "set", 2, 101, do_net_set
, 8, 0,
221 " set interface context-sensitive-arguments (or ? for help)\n"
222 " set protocol context-sensitive-arguments (or ? for help)\n"
223 " set service [ name <serviceName> ] [ order new-order ]\n"
224 " set set [ name setName ]" },
226 { "show", 1, 2, do_net_show
, 9, 0,
228 " show interface [ <interfaceName> | <interface#> ]\n"
230 " show protocol [ <protocolType> ]\n"
231 " show services [ all ]\n"
232 " show service [ <serviceName> | <service#> ]\n"
234 " show set [ <setName> | <set#> ]" },
236 { "update", 0, 1, do_net_update
, 10, 0,
237 " update : update the network configuration" },
239 { "snapshot", 0, 0, do_net_snapshot
, 99, 2,
244 const int nCommands_net
= (sizeof(commands_net
)/sizeof(cmdInfo
));
248 const cmdInfo commands_prefs
[] = {
249 /* cmd minArgs maxArgs func group ctype */
252 { "help", 0, 0, do_help
, 0, 0,
253 " help : list available commands" },
255 { "f.read", 1, 1, do_readFile
, 0, 0,
256 " f.read file : process commands from file" },
258 { "quit", 0, 1, do_prefs_quit
, 0, 0,
259 " quit [!] : quit" },
261 { "q", 0, 1, do_prefs_quit
, 0, -1,
264 { "exit", 0, 1, do_prefs_quit
, 0, -1,
267 /* local dictionary manipulation commands */
269 { "d.init", 0, 0, do_dictInit
, 1, 0,
270 " d.init : initialize (empty) dictionary" },
272 { "d.show", 0, 0, do_dictShow
, 1, 0,
273 " d.show : show dictionary contents" },
275 { "d.add", 2, 101, do_dictSetKey
, 1, 0,
276 " d.add key [*#?] val [v2 ...] : add information to dictionary\n"
277 " (*=array, #=number, ?=boolean)" },
279 { "d.remove", 1, 1, do_dictRemoveKey
, 1, 0,
280 " d.remove key : remove key from dictionary" },
282 /* data store manipulation commands */
284 { "open", 0, 1, do_prefs_open
, 2, 1,
285 " open [\"prefsID\"] : open a \"preferences\" session" },
287 { "lock", 0, 1, do_prefs_lock
, 3, 1,
288 " lock [wait] : locks write access to preferences" },
290 { "commit", 0, 0, do_prefs_commit
, 2, 0,
291 " commit : commit any changes" },
293 { "apply", 0, 0, do_prefs_apply
, 2, 0,
294 " apply : apply any changes" },
296 { "unlock", 0, 0, do_prefs_unlock
, 3, 1,
297 " unlock : unlocks write access to preferences" },
299 { "close", 0, 1, do_prefs_close
, 2, 1,
300 " close [!] : close current \"preference\" session" },
302 { "synchronize",0, 1, do_prefs_synchronize
, 2, 0,
303 " synchronize : synchronize a \"preferences\" session" },
305 { "list", 0, 1, do_prefs_list
, 4, 0,
306 " list [path] : list preference paths" },
308 { "get", 1, 1, do_prefs_get
, 4, 0,
309 " get path : get dict from preferences w/path" },
311 { "set", 1, 2, do_prefs_set
, 4, 0,
312 " set path : set path in preferences w/current dict\n"
313 " set path link : set path in preferences w/link" },
315 { "remove", 1, 1, do_prefs_remove
, 4, 0,
316 " remove path : remove path from preferences" }
319 const int nCommands_prefs
= (sizeof(commands_prefs
)/sizeof(cmdInfo
));
322 __private_extern__ cmdInfo
*commands
= NULL
;
323 __private_extern__
int nCommands
= 0;
324 __private_extern__ Boolean enablePrivateAPI
= FALSE
;
325 __private_extern__ Boolean termRequested
= FALSE
;
330 do_command(int argc
, char **argv
)
335 for (i
= 0; i
< nCommands
; i
++) {
336 if ((commands
[i
].ctype
> 1) && !enablePrivateAPI
) {
337 continue; /* if "private" API and access has not been enabled */
340 if (strcasecmp(cmd
, commands
[i
].cmd
) == 0) {
343 if (argc
< commands
[i
].minArgs
) {
344 SCPrint(TRUE
, stdout
, CFSTR("%s: too few arguments\n"), cmd
);
346 } else if (argc
> commands
[i
].maxArgs
) {
347 SCPrint(TRUE
, stdout
, CFSTR("%s: too many arguments\n"), cmd
);
350 (*commands
[i
].func
)(argc
, argv
);
355 SCPrint(TRUE
, stdout
, CFSTR("%s: unknown, type \"help\" for command info\n"), cmd
);
362 do_help(int argc
, char **argv
)
364 int g
= -1; /* current group */
367 SCPrint(TRUE
, stdout
, CFSTR("\nAvailable commands:\n"));
368 for (i
= 0; i
< nCommands
; i
++) {
369 if (commands
[i
].ctype
< 0) {
370 continue; /* if "hidden" */
373 if ((commands
[i
].ctype
> 0) && !enablePrivateAPI
) {
374 continue; /* if "private" API and access has not been enabled */
377 /* check if this is a new command group */
378 if (g
!= commands
[i
].group
) {
379 SCPrint(TRUE
, stdout
, CFSTR("\n"));
380 g
= commands
[i
].group
;
383 /* display the command */
384 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), commands
[i
].usage
);
386 SCPrint(TRUE
, stdout
, CFSTR("\n"));
394 do_readFile(int argc
, char **argv
)
398 /* allocate command input stream */
399 src
= (InputRef
)CFAllocatorAllocate(NULL
, sizeof(Input
), 0);
401 src
->fp
= fopen(argv
[0], "r");
403 if (src
->fp
== NULL
) {
404 SCPrint(TRUE
, stdout
, CFSTR("f.read: could not open file (%s).\n"), strerror(errno
));
405 CFAllocatorDeallocate(NULL
, src
);
409 /* open file, increase nesting level */
410 SCPrint(TRUE
, stdout
, CFSTR("f.read: reading file (%s).\n"), argv
[0]);
413 while (process_line(src
) == TRUE
) {
414 /* debug information, diagnostics */
415 __showMachPortStatus();
418 (void)fclose(src
->fp
);
419 CFAllocatorDeallocate(NULL
, src
);
427 do_quit(int argc
, char **argv
)
429 termRequested
= TRUE
;