2 * Copyright (c) 2000-2006 Apple Computer, 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 13, 2005 Allan Nathanson <ajn@apple.com>
28 * - added SCPreferences support
30 * August 4, 2004 Allan Nathanson <ajn@apple.com>
31 * - added network configuration (prefs) support
33 * September 25, 2002 Allan Nathanson <ajn@apple.com>
34 * - added command line history & editing
36 * July 9, 2001 Allan Nathanson <ajn@apple.com>
37 * - added "-r" option for checking network reachability
38 * - added "-w" option to check/wait for the presence of a
41 * June 1, 2001 Allan Nathanson <ajn@apple.com>
42 * - public API conversion
44 * November 9, 2000 Allan Nathanson <ajn@apple.com>
58 #include <mach/mach.h>
59 #include <mach/mach_error.h>
64 #include "dictionary.h"
70 #include "SCDynamicStoreInternal.h"
73 #define LINE_LENGTH 256
76 __private_extern__ AuthorizationRef authorization
= NULL
;
77 __private_extern__ InputRef currentInput
= NULL
;
78 __private_extern__
int nesting
= 0;
79 __private_extern__ CFRunLoopRef notifyRl
= NULL
;
80 __private_extern__ CFRunLoopSourceRef notifyRls
= NULL
;
81 __private_extern__ SCPreferencesRef prefs
= NULL
;
82 __private_extern__ SCDynamicStoreRef store
= NULL
;
83 __private_extern__ CFPropertyListRef value
= NULL
;
84 __private_extern__ CFMutableArrayRef watchedKeys
= NULL
;
85 __private_extern__ CFMutableArrayRef watchedPatterns
= NULL
;
87 static const struct option longopts
[] = {
88 // { "debug", no_argument, NULL, 'd' },
89 // { "verbose", no_argument, NULL, 'v' },
90 // { "SPI", no_argument, NULL, 'p' },
91 // { "check-reachability", required_argument, NULL, 'r' },
92 // { "timeout", required_argument, NULL, 't' },
93 // { "wait-key", required_argument, NULL, 'w' },
94 { "dns", no_argument
, NULL
, 0 },
95 { "get", required_argument
, NULL
, 0 },
96 { "help", no_argument
, NULL
, '?' },
97 { "net", no_argument
, NULL
, 0 },
98 { "prefs", no_argument
, NULL
, 0 },
99 { "proxy", no_argument
, NULL
, 0 },
100 { "set", required_argument
, NULL
, 0 },
106 getLine(char *buf
, int len
, InputRef src
)
114 line
= el_gets(src
->el
, &count
);
118 strncpy(buf
, line
, len
);
120 if (fgets(buf
, len
, src
->fp
) == NULL
)
125 if (buf
[n
-1] == '\n') {
126 /* the entire line fit in the buffer, remove the newline */
128 } else if (!src
->el
) {
129 /* eat the remainder of the line */
132 } while ((n
!= '\n') && (n
!= EOF
));
138 history(src
->h
, &ev
, H_ENTER
, buf
);
147 getString(char **line
)
149 char *s
, *e
, c
, *string
;
150 int i
, isQuoted
= 0, escaped
= 0;
152 if (*line
== NULL
) return NULL
;
153 if (**line
== '\0') return NULL
;
155 /* Skip leading white space */
156 while (isspace(**line
)) *line
+= 1;
158 /* Grab the next string */
161 return NULL
; /* no string available */
162 } else if (*s
== '"') {
163 isQuoted
= 1; /* it's a quoted string */
167 for (e
= s
; (c
= *e
) != '\0'; e
++) {
168 if (isQuoted
&& (c
== '"'))
169 break; /* end of quoted string */
173 break; /* if premature end-of-string */
174 if ((*e
== '"') || isspace(*e
))
175 escaped
++; /* if escaped quote or white space */
177 if (!isQuoted
&& isspace(c
))
178 break; /* end of non-quoted string */
181 string
= malloc(e
- s
- escaped
+ 1);
183 for (i
= 0; s
< e
; s
++) {
185 if (!((s
[0] == '\\') && ((s
[1] == '"') || isspace(s
[1])))) i
++;
190 e
++; /* move past end of quoted string */
199 process_line(InputRef src
)
205 char line
[LINE_LENGTH
];
208 // if end-of-file, exit
209 if (getLine(line
, sizeof(line
), src
) == NULL
)
213 SCPrint(TRUE
, stdout
, CFSTR("%d> %s\n"), nesting
, line
);
216 // break up the input line
217 while ((arg
= getString(&s
)) != NULL
) {
219 argv
= (char **)malloc(2 * sizeof(char *));
221 argv
= (char **)reallocf(argv
, ((argc
+ 2) * sizeof(char *)));
226 return TRUE
; // if no arguments
229 /* process the command */
230 if (*argv
[0] != '#') {
231 argv
[argc
] = NULL
; // just in case...
233 do_command(argc
, argv
);
236 /* free the arguments */
237 for (i
= 0; i
< argc
; i
++) {
242 return !termRequested
;
247 usage(const char *command
)
249 SCPrint(TRUE
, stderr
, CFSTR("usage: %s\n"), command
);
250 SCPrint(TRUE
, stderr
, CFSTR("\tinteractive access to the dynamic store.\n"));
251 SCPrint(TRUE
, stderr
, CFSTR("\n"));
252 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --prefs\n"), command
);
253 SCPrint(TRUE
, stderr
, CFSTR("\tinteractive access to the [raw] stored preferences.\n"));
254 SCPrint(TRUE
, stderr
, CFSTR("\n"));
255 SCPrint(TRUE
, stderr
, CFSTR(" or: %s -r nodename\n"), command
);
256 SCPrint(TRUE
, stderr
, CFSTR(" or: %s -r address\n"), command
);
257 SCPrint(TRUE
, stderr
, CFSTR(" or: %s -r local-address remote-address\n"), command
);
258 SCPrint(TRUE
, stderr
, CFSTR("\tcheck reachability of node, address, or address pair.\n"));
259 SCPrint(TRUE
, stderr
, CFSTR("\n"));
260 SCPrint(TRUE
, stderr
, CFSTR(" or: %s -w dynamic-store-key [ -t timeout ]\n"), command
);
261 SCPrint(TRUE
, stderr
, CFSTR("\t-w\twait for presense of dynamic store key\n"));
262 SCPrint(TRUE
, stderr
, CFSTR("\t-t\ttime to wait for key\n"));
263 SCPrint(TRUE
, stderr
, CFSTR("\n"));
264 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --get pref\n"), command
);
265 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --set pref [newval]\n"), command
);
266 SCPrint(TRUE
, stderr
, CFSTR("\tpref\tdisplay (or set) the specified preference. Valid preferences\n"));
267 SCPrint(TRUE
, stderr
, CFSTR("\t\tinclude:\n"));
268 SCPrint(TRUE
, stderr
, CFSTR("\t\t\tComputerName, LocalHostName, HostName\n"));
269 SCPrint(TRUE
, stderr
, CFSTR("\tnewval\tNew preference value to be set. If not specified,\n"));
270 SCPrint(TRUE
, stderr
, CFSTR("\t\tthe new value will be read from standard input.\n"));
271 SCPrint(TRUE
, stderr
, CFSTR("\n"));
272 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --dns\n"), command
);
273 SCPrint(TRUE
, stderr
, CFSTR("\tshow DNS configuration.\n"));
274 SCPrint(TRUE
, stderr
, CFSTR("\n"));
275 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --proxy\n"), command
);
276 SCPrint(TRUE
, stderr
, CFSTR("\tshow \"proxy\" configuration.\n"));
278 if (getenv("ENABLE_EXPERIMENTAL_SCUTIL_COMMANDS")) {
279 SCPrint(TRUE
, stderr
, CFSTR("\n"));
280 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --net\n"), command
);
281 SCPrint(TRUE
, stderr
, CFSTR("\tmanage network configuration.\n"));
296 main(int argc
, char * const argv
[])
298 Boolean doDNS
= FALSE
;
299 Boolean doNet
= FALSE
;
300 Boolean doPrefs
= FALSE
;
301 Boolean doProxy
= FALSE
;
302 Boolean doReach
= FALSE
;
307 const char *prog
= argv
[0];
310 int timeout
= 15; /* default timeout (in seconds) */
312 int xStore
= 0; /* non dynamic store command line options */
314 /* process any arguments */
316 while ((opt
= getopt_long(argc
, argv
, "dvprt:w:", longopts
, &opti
)) != -1)
320 _sc_log
= FALSE
; /* enable framework logging */
324 _sc_log
= FALSE
; /* enable framework logging */
327 enablePrivateAPI
= TRUE
;
334 timeout
= atoi(optarg
);
341 if (strcmp(longopts
[opti
].name
, "dns") == 0) {
344 } else if (strcmp(longopts
[opti
].name
, "get") == 0) {
347 } else if (strcmp(longopts
[opti
].name
, "net") == 0) {
350 } else if (strcmp(longopts
[opti
].name
, "prefs") == 0) {
353 } else if (strcmp(longopts
[opti
].name
, "proxy") == 0) {
356 } else if (strcmp(longopts
[opti
].name
, "set") == 0) {
369 // if we are attempting to process more than one type of request
373 /* are we checking the reachability of a host/address */
375 if ((argc
< 1) || (argc
> 2)) {
378 do_checkReachability(argc
, (char **)argv
);
382 /* are we waiting on the presense of a dynamic store key */
384 do_wait(wait
, timeout
);
388 /* are we looking up the DNS configuration */
390 do_showDNSConfiguration(argc
, (char **)argv
);
394 /* are we looking up a preference value */
396 if (findPref(get
) < 0) {
399 do_getPref(get
, argc
, (char **)argv
);
403 /* are we looking up the proxy configuration */
405 do_showProxyConfiguration(argc
, (char **)argv
);
409 /* are we changing a preference value */
411 if (findPref(set
) < 0) {
414 do_setPref(set
, argc
, (char **)argv
);
419 /* if we are going to be managing the network configuration */
420 commands
= (cmdInfo
*)commands_net
;
421 nCommands
= nCommands_net
;
423 if (!getenv("ENABLE_EXPERIMENTAL_SCUTIL_COMMANDS")) {
427 do_net_init(); /* initialization */
428 do_net_open(0, NULL
); /* open default prefs */
429 } else if (doPrefs
) {
430 /* if we are going to be managing the network configuration */
431 commands
= (cmdInfo
*)commands_prefs
;
432 nCommands
= nCommands_prefs
;
434 do_dictInit(0, NULL
); /* start with an empty dictionary */
435 do_prefs_init(); /* initialization */
436 do_prefs_open(0, NULL
); /* open default prefs */
438 /* if we are going to be managing the dynamic store */
439 commands
= (cmdInfo
*)commands_store
;
440 nCommands
= nCommands_store
;
442 do_dictInit(0, NULL
); /* start with an empty dictionary */
443 do_open(0, NULL
); /* open the dynamic store */
446 /* allocate command input stream */
447 src
= (InputRef
)CFAllocatorAllocate(NULL
, sizeof(Input
), 0);
452 if (isatty(fileno(src
->fp
))) {
457 if (tcgetattr(fileno(src
->fp
), &t
) != -1) {
458 if ((t
.c_lflag
& ECHO
) == 0) {
462 src
->el
= el_init(prog
, src
->fp
, stdout
, stderr
);
463 src
->h
= history_init();
465 (void)history(src
->h
, &ev
, H_SETSIZE
, INT_MAX
);
466 el_set(src
->el
, EL_HIST
, history
, src
->h
);
469 el_set(src
->el
, EL_EDITMODE
, 0);
472 el_set(src
->el
, EL_EDITOR
, "emacs");
473 el_set(src
->el
, EL_PROMPT
, prompt
);
475 el_source(src
->el
, NULL
);
477 if ((el_get(src
->el
, EL_EDITMODE
, &editmode
) != -1) && editmode
!= 0) {
478 el_set(src
->el
, EL_SIGNAL
, 1);
487 while (process_line(src
) == TRUE
) {
488 /* debug information, diagnostics */
489 __showMachPortStatus();
492 /* close the socket, free resources */
493 if (src
->h
) history_end(src
->h
);
494 if (src
->el
) el_end(src
->el
);
495 (void)fclose(src
->fp
);
496 CFAllocatorDeallocate(NULL
, src
);
498 exit (EX_OK
); // insure the process exit status is 0
499 return 0; // ...and make main fit the ANSI spec.