]> git.saurik.com Git - apple/configd.git/blob - scutil.tproj/commands.c
configd-84.tar.gz
[apple/configd.git] / scutil.tproj / commands.c
1 /*
2 * Copyright (c) 2000-2003 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 <stdio.h>
37 #include <string.h>
38 #include <sys/errno.h>
39
40 #include "scutil.h"
41 #include "commands.h"
42 #include "dictionary.h"
43 #include "session.h"
44 #include "cache.h"
45 #include "notify.h"
46 #include "tests.h"
47
48 #include "SCDynamicStoreInternal.h"
49
50
51 const cmdInfo commands[] = {
52 /* cmd minArgs maxArgs func group ctype */
53 /* usage */
54
55 { "help", 0, 0, do_help, 0, 0,
56 " help : list available commands" },
57
58 { "f.read", 1, 1, do_readFile, 0, 0,
59 " f.read file : process commands from file" },
60
61 /* local dictionary manipulation commands */
62
63 { "d.init", 0, 0, do_dictInit, 1, 0,
64 " d.init : initialize (empty) dictionary" },
65
66 { "d.show", 0, 0, do_dictShow, 1, 0,
67 " d.show : show dictionary contents" },
68
69 { "d.add", 2, 101, do_dictSetKey, 1, 0,
70 " d.add key [*#?] val [v2 ...] : add information to dictionary\n"
71 " (*=array, #=number, ?=boolean)" },
72
73 { "d.remove", 1, 1, do_dictRemoveKey, 1, 0,
74 " d.remove key : remove key from dictionary" },
75
76 /* data store manipulation commands */
77
78 { "open", 0, 0, do_open, 2, 0,
79 " open : open a session with \"configd\"" },
80
81 { "close", 0, 0, do_close, 2, 0,
82 " close : close current \"configd\" session" },
83
84 { "lock", 0, 0, do_lock, 3, 1,
85 " lock : secures write access to data store" },
86
87 { "unlock", 0, 0, do_unlock, 3, 1,
88 " unlock : secures write access to data store" },
89
90 { "list", 0, 2, do_list, 4, 0,
91 " list [pattern] : list keys in data store" },
92
93 { "add", 1, 2, do_add, 4, 0,
94 " add key [\"temporary\"] : add key in data store w/current dict" },
95
96 { "get", 1, 1, do_get, 4, 0,
97 " get key : get dict from data store w/key" },
98
99 { "set", 1, 1, do_set, 4, 0,
100 " set key : set key in data store w/current dict" },
101
102 { "show", 1, 2, do_show, 4, 0,
103 " show key [\"pattern\"] : show values in data store w/key" },
104
105 { "remove", 1, 1, do_remove, 4, 0,
106 " remove key : remove key from data store" },
107
108 { "notify", 1, 1, do_notify, 4, 0,
109 " notify key : notify key in data store" },
110
111 { "touch", 1, 1, do_touch, 4, 1,
112 " touch key : touch key in data store" },
113
114 { "n.list", 0, 1, do_notify_list, 5, 0,
115 " n.list [\"pattern\"] : list notification keys" },
116
117 { "n.add", 1, 2, do_notify_add, 5, 0,
118 " n.add key [\"pattern\"] : add notification key" },
119
120 { "n.remove", 1, 2, do_notify_remove, 5, 0,
121 " n.remove key [\"pattern\"] : remove notification key" },
122
123 { "n.changes", 0, 0, do_notify_changes, 5, 0,
124 " n.changes : list changed keys" },
125
126 { "n.watch", 0, 1, do_notify_watch, 5, 0,
127 " n.watch [verbose] : watch for changes" },
128
129 { "n.wait", 0, 0, do_notify_wait, 5, 2,
130 " n.wait : wait for changes" },
131
132 { "n.callback", 0, 1, do_notify_callback, 5, 2,
133 " n.callback [\"verbose\"] : watch for changes" },
134
135 { "n.signal", 1, 2, do_notify_signal, 5, 2,
136 " n.signal sig [pid] : signal changes" },
137
138 { "n.file", 0, 1, do_notify_file, 5, 2,
139 " n.file [identifier] : watch for changes via file" },
140
141 { "n.cancel", 0, 1, do_notify_cancel, 5, 0,
142 " n.cancel : cancel notification requests" },
143
144 { "snapshot", 0, 0, do_snapshot, 9, 2,
145 " snapshot : save snapshot of store and session data" },
146 };
147
148 const int nCommands = (sizeof(commands)/sizeof(cmdInfo));
149
150 Boolean enablePrivateAPI = FALSE;
151
152
153 void
154 do_command(int argc, char **argv)
155 {
156 int i;
157 char *cmd = argv[0];
158
159 for (i = 0; i < nCommands; i++) {
160 if ((commands[i].ctype > 1) && !enablePrivateAPI) {
161 continue; /* if "private" API and access has not been enabled */
162 }
163
164 if (strcasecmp(cmd, commands[i].cmd) == 0) {
165 --argc;
166 argv++;
167 if (argc < commands[i].minArgs) {
168 SCPrint(TRUE, stdout, CFSTR("%s: too few arguments\n"), cmd);
169 return;
170 } else if (argc > commands[i].maxArgs) {
171 SCPrint(TRUE, stdout, CFSTR("%s: too many arguments\n"), cmd);
172 return;
173 }
174 commands[i].func(argc, argv);
175 return;
176 }
177 }
178
179 SCPrint(TRUE, stdout, CFSTR("%s: unknown, type \"help\" for command info\n"), cmd);
180 return;
181 }
182
183
184 void
185 do_help(int argc, char **argv)
186 {
187 int g = -1; /* current group */
188 int i;
189
190 SCPrint(TRUE, stdout, CFSTR("\nAvailable commands:\n"));
191 for (i = 0; i < nCommands; i++) {
192 if ((commands[i].ctype > 0) && !enablePrivateAPI) {
193 continue; /* if "private" API and access has not been enabled */
194 }
195
196 /* check if this is a new command group */
197 if (g != commands[i].group) {
198 SCPrint(TRUE, stdout, CFSTR("\n"));
199 g = commands[i].group;
200 }
201
202 /* display the command */
203 SCPrint(TRUE, stdout, CFSTR("%s\n"), commands[i].usage);
204 }
205 SCPrint(TRUE, stdout, CFSTR("\n"));
206
207 return;
208 }
209
210
211 void
212 do_readFile(int argc, char **argv)
213 {
214 InputRef src;
215
216 /* allocate command input stream */
217 src = (InputRef)CFAllocatorAllocate(NULL, sizeof(Input), 0);
218 src->el = NULL;
219 src->fp = fopen(argv[0], "r");
220
221 if (src->fp == NULL) {
222 SCPrint(TRUE, stdout, CFSTR("f.read: could not open file (%s).\n"), strerror(errno));
223 CFAllocatorDeallocate(NULL, src);
224 return;
225 }
226
227 /* open file, increase nesting level */
228 SCPrint(TRUE, stdout, CFSTR("f.read: reading file (%s).\n"), argv[0]);
229 nesting++;
230
231 while (process_line(src) == TRUE) {
232 /* debug information, diagnostics */
233 __showMachPortStatus();
234 }
235
236 (void)fclose(src->fp);
237 CFAllocatorDeallocate(NULL, src);
238
239 return;
240 }