]>
git.saurik.com Git - apple/configd.git/blob - scutil.tproj/commands.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
24 * Modification History
26 * June 1, 2001 Allan Nathanson <ajn@apple.com>
27 * - public API conversion
29 * November 9, 2000 Allan Nathanson <ajn@apple.com>
35 #include <sys/errno.h>
39 #include "dictionary.h"
45 #include <SystemConfiguration/SCPrivate.h>
48 const cmdInfo commands
[] = {
49 /* cmd minArgs maxArgs func group ctype */
52 { "help", 0, 0, do_help
, 0, 0,
53 " help : list available commands" },
55 { "f.read", 1, 1, do_readFile
, 0, 0,
56 " f.read file : process commands from file" },
58 /* local dictionary manipulation commands */
60 { "d.init", 0, 0, do_dictInit
, 1, 0,
61 " d.init : initialize (empty) dictionary" },
63 { "d.show", 0, 0, do_dictShow
, 1, 0,
64 " d.show : show dictionary contents" },
66 { "d.add", 2, 101, do_dictSetKey
, 1, 0,
67 " d.add key [*#?] val [v2 ...] : add information to dictionary\n"
68 " (*=array, #=number, ?=boolean)" },
70 { "d.remove", 1, 1, do_dictRemoveKey
, 1, 0,
71 " d.remove key : remove key from dictionary" },
73 /* data store manipulation commands */
75 { "open", 0, 0, do_open
, 2, 0,
76 " open : open a session with \"configd\"" },
78 { "close", 0, 0, do_close
, 2, 0,
79 " close : close current \"configd\" session" },
81 { "lock", 0, 0, do_lock
, 3, 1,
82 " lock : secures write access to data store" },
84 { "unlock", 0, 0, do_unlock
, 3, 1,
85 " unlock : secures write access to data store" },
87 { "list", 0, 2, do_list
, 4, 0,
88 " list [pattern] : list keys in data store" },
90 { "add", 1, 2, do_add
, 4, 0,
91 " add key [\"temporary\"] : add key in data store w/current dict" },
93 { "get", 1, 1, do_get
, 4, 0,
94 " get key : get dict from data store w/key" },
96 { "set", 1, 1, do_set
, 4, 0,
97 " set key : set key in data store w/current dict" },
99 { "show", 1, 1, do_show
, 4, 1,
100 " show key : show dict in data store w/key" },
102 { "remove", 1, 1, do_remove
, 4, 0,
103 " remove key : remove key from data store" },
105 { "notify", 1, 1, do_notify
, 4, 0,
106 " notify key : notify key in data store" },
108 { "touch", 1, 1, do_touch
, 4, 1,
109 " touch key : touch key in data store" },
111 { "n.list", 0, 1, do_notify_list
, 5, 0,
112 " n.list [\"pattern\"] : list notification keys" },
114 { "n.add", 1, 2, do_notify_add
, 5, 0,
115 " n.add key [\"pattern\"] : add notification key" },
117 { "n.remove", 1, 2, do_notify_remove
, 5, 0,
118 " n.remove key [\"pattern\"] : remove notification key" },
120 { "n.changes", 0, 0, do_notify_changes
, 5, 0,
121 " n.changes : list changed keys" },
123 { "n.watch", 0, 1, do_notify_watch
, 5, 0,
124 " n.watch [verbose] : watch for changes" },
126 { "n.wait", 0, 0, do_notify_wait
, 5, 2,
127 " n.wait : wait for changes" },
129 { "n.callback", 0, 1, do_notify_callback
, 5, 2,
130 " n.callback [\"verbose\"] : watch for changes" },
132 { "n.signal", 1, 2, do_notify_signal
, 5, 2,
133 " n.signal sig [pid] : signal changes" },
135 { "n.file", 0, 1, do_notify_file
, 5, 2,
136 " n.file [identifier] : watch for changes via file" },
138 { "n.cancel", 0, 1, do_notify_cancel
, 5, 0,
139 " n.cancel : cancel notification requests" },
141 { "snapshot", 0, 0, do_snapshot
, 9, 2,
142 " snapshot : save snapshot of store and session data" },
145 const int nCommands
= (sizeof(commands
)/sizeof(cmdInfo
));
147 Boolean enablePrivateAPI
= FALSE
;
151 do_command(int argc
, char **argv
)
156 for (i
=0; i
<nCommands
; i
++) {
157 if ((commands
[i
].ctype
> 1) && !enablePrivateAPI
) {
158 continue; /* if "private" API and access has not been enabled */
161 if (strcasecmp(cmd
, commands
[i
].cmd
) == 0) {
164 if (argc
< commands
[i
].minArgs
) {
165 SCPrint(TRUE
, stdout
, CFSTR("%s: too few arguments\n"), cmd
);
167 } else if (argc
> commands
[i
].maxArgs
) {
168 SCPrint(TRUE
, stdout
, CFSTR("%s: too many arguments\n"), cmd
);
171 commands
[i
].func(argc
, argv
);
176 SCPrint(TRUE
, stdout
, CFSTR("%s: unknown, type \"help\" for command info\n"), cmd
);
182 do_help(int argc
, char **argv
)
184 int g
= -1; /* current group */
187 SCPrint(TRUE
, stdout
, CFSTR("\nAvailable commands:\n"));
188 for (i
=0; i
<nCommands
; i
++) {
189 if ((commands
[i
].ctype
> 0) && !enablePrivateAPI
) {
190 continue; /* if "private" API and access has not been enabled */
193 /* check if this is a new command group */
194 if (g
!= commands
[i
].group
) {
195 SCPrint(TRUE
, stdout
, CFSTR("\n"));
196 g
= commands
[i
].group
;
199 /* display the command */
200 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), commands
[i
].usage
);
202 SCPrint(TRUE
, stdout
, CFSTR("\n"));
209 do_readFile(int argc
, char **argv
)
211 CFSocketContext context
;
212 FILE *fp
= fopen(argv
[0], "r");
214 CFRunLoopSourceRef rls
;
217 SCPrint(TRUE
, stdout
, CFSTR("f.read: could not open file (%s).\n"), strerror(errno
));
221 /* open file, increase nesting level */
222 SCPrint(TRUE
, stdout
, CFSTR("f.read: reading file (%s).\n"), argv
[0]);
225 /* create a "socket" reference with the file descriptor associated with stdin */
228 context
.retain
= NULL
;
229 context
.release
= NULL
;
230 context
.copyDescription
= NULL
;
231 in
= CFSocketCreateWithNative(NULL
,
233 kCFSocketReadCallBack
,
237 /* Create and add a run loop source for the file descriptor */
238 rls
= CFSocketCreateRunLoopSource(NULL
, in
, nesting
);
241 * Remove the current input file from the run loop sources. We
242 * will reactivate the current input file source when we are
243 * finished reading data from the new file.
245 CFRunLoopRemoveSource(CFRunLoopGetCurrent(),
246 (CFRunLoopSourceRef
) CFArrayGetValueAtIndex(sources
, 0),
247 kCFRunLoopDefaultMode
);
249 /* keep track of this new source */
250 CFArrayInsertValueAtIndex(sources
, 0, rls
);
252 /* add this source to the run loop */
253 CFRunLoopAddSource(CFRunLoopGetCurrent(), rls
, kCFRunLoopDefaultMode
);