2 * Copyright (c) 2000-2004 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 * August 4, 2004 Allan Nathanson <ajn@apple.com>
28 * - added network configuration (prefs) support
30 * September 25, 2002 Allan Nathanson <ajn@apple.com>
31 * - added command line history & editing
33 * July 9, 2001 Allan Nathanson <ajn@apple.com>
34 * - added "-r" option for checking network reachability
35 * - added "-w" option to check/wait for the presence of a
38 * June 1, 2001 Allan Nathanson <ajn@apple.com>
39 * - public API conversion
41 * November 9, 2000 Allan Nathanson <ajn@apple.com>
55 #include <mach/mach.h>
56 #include <mach/mach_error.h>
61 #include "dictionary.h"
67 #include "SCDynamicStoreInternal.h"
70 #define LINE_LENGTH 256
73 __private_extern__ InputRef currentInput
= NULL
;
74 __private_extern__
int nesting
= 0;
75 __private_extern__ CFRunLoopRef notifyRl
= NULL
;
76 __private_extern__ CFRunLoopSourceRef notifyRls
= NULL
;
77 __private_extern__ SCPreferencesRef prefs
= NULL
;
78 __private_extern__ SCDynamicStoreRef store
= NULL
;
79 __private_extern__ CFPropertyListRef value
= NULL
;
80 __private_extern__ CFMutableArrayRef watchedKeys
= NULL
;
81 __private_extern__ CFMutableArrayRef watchedPatterns
= NULL
;
83 static const struct option longopts
[] = {
84 // { "debug", no_argument, NULL, 'd' },
85 // { "verbose", no_argument, NULL, 'v' },
86 // { "SPI", no_argument, NULL, 'p' },
87 // { "check-reachability", required_argument, NULL, 'r' },
88 // { "timeout", required_argument, NULL, 't' },
89 // { "wait-key", required_argument, NULL, 'w' },
90 { "dns", no_argument
, NULL
, 0 },
91 { "get", required_argument
, NULL
, 0 },
92 { "help", no_argument
, NULL
, '?' },
93 { "net", no_argument
, NULL
, 0 },
94 { "proxy", no_argument
, NULL
, 0 },
95 { "set", required_argument
, NULL
, 0 },
101 getLine(char *buf
, int len
, InputRef src
)
109 line
= el_gets(src
->el
, &count
);
113 strncpy(buf
, line
, len
);
115 if (fgets(buf
, len
, src
->fp
) == NULL
)
120 if (buf
[n
-1] == '\n') {
121 /* the entire line fit in the buffer, remove the newline */
123 } else if (!src
->el
) {
124 /* eat the remainder of the line */
127 } while ((n
!= '\n') && (n
!= EOF
));
133 history(src
->h
, &ev
, H_ENTER
, buf
);
142 getString(char **line
)
144 char *s
, *e
, c
, *string
;
145 int i
, isQuoted
= 0, escaped
= 0;
147 if (*line
== NULL
) return NULL
;
148 if (**line
== '\0') return NULL
;
150 /* Skip leading white space */
151 while (isspace(**line
)) *line
+= 1;
153 /* Grab the next string */
156 return NULL
; /* no string available */
157 } else if (*s
== '"') {
158 isQuoted
= 1; /* it's a quoted string */
162 for (e
= s
; (c
= *e
) != '\0'; e
++) {
163 if (isQuoted
&& (c
== '"'))
164 break; /* end of quoted string */
168 break; /* if premature end-of-string */
169 if ((*e
== '"') || isspace(*e
))
170 escaped
++; /* if escaped quote or white space */
172 if (!isQuoted
&& isspace(c
))
173 break; /* end of non-quoted string */
176 string
= malloc(e
- s
- escaped
+ 1);
178 for (i
= 0; s
< e
; s
++) {
180 if (!((s
[0] == '\\') && ((s
[1] == '"') || isspace(s
[1])))) i
++;
185 e
++; /* move past end of quoted string */
194 process_line(InputRef src
)
200 char line
[LINE_LENGTH
];
203 // if end-of-file, exit
204 if (getLine(line
, sizeof(line
), src
) == NULL
)
208 SCPrint(TRUE
, stdout
, CFSTR("%d> %s\n"), nesting
, line
);
211 // break up the input line
212 while ((arg
= getString(&s
)) != NULL
) {
214 argv
= (char **)malloc(2 * sizeof(char *));
216 argv
= (char **)reallocf(argv
, ((argc
+ 2) * sizeof(char *)));
221 return TRUE
; // if no arguments
224 /* process the command */
225 if (*argv
[0] != '#') {
226 argv
[argc
] = NULL
; // just in case...
228 do_command(argc
, argv
);
231 /* free the arguments */
232 for (i
= 0; i
< argc
; i
++) {
237 return !termRequested
;
242 usage(const char *command
)
244 SCPrint(TRUE
, stderr
, CFSTR("usage: %s\n"), command
);
245 SCPrint(TRUE
, stderr
, CFSTR("\tinteractive access to the dynamic store.\n"));
246 SCPrint(TRUE
, stderr
, CFSTR("\n"));
247 SCPrint(TRUE
, stderr
, CFSTR(" or: %s -r nodename\n"), command
);
248 SCPrint(TRUE
, stderr
, CFSTR(" or: %s -r address\n"), command
);
249 SCPrint(TRUE
, stderr
, CFSTR(" or: %s -r local-address remote-address\n"), command
);
250 SCPrint(TRUE
, stderr
, CFSTR("\tcheck reachability of node, address, or address pair.\n"));
251 SCPrint(TRUE
, stderr
, CFSTR("\n"));
252 SCPrint(TRUE
, stderr
, CFSTR(" or: %s -w dynamic-store-key [ -t timeout ]\n"), command
);
253 SCPrint(TRUE
, stderr
, CFSTR("\t-w\twait for presense of dynamic store key\n"));
254 SCPrint(TRUE
, stderr
, CFSTR("\t-t\ttime to wait for key\n"));
255 SCPrint(TRUE
, stderr
, CFSTR("\n"));
256 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --get pref\n"), command
);
257 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --set pref [newval]\n"), command
);
258 SCPrint(TRUE
, stderr
, CFSTR("\tpref\tdisplay (or set) the specified preference. Valid preferences\n"));
259 SCPrint(TRUE
, stderr
, CFSTR("\t\tinclude:\n"));
260 SCPrint(TRUE
, stderr
, CFSTR("\t\t\tComputerName, LocalHostName\n"));
261 SCPrint(TRUE
, stderr
, CFSTR("\tnewval\tNew preference value to be set. If not specified,\n"));
262 SCPrint(TRUE
, stderr
, CFSTR("\t\tthe new value will be read from standard input.\n"));
263 SCPrint(TRUE
, stderr
, CFSTR("\n"));
264 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --dns\n"), command
);
265 SCPrint(TRUE
, stderr
, CFSTR("\tshow DNS configuration.\n"));
266 SCPrint(TRUE
, stderr
, CFSTR("\n"));
267 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --proxy\n"), command
);
268 SCPrint(TRUE
, stderr
, CFSTR("\tshow \"proxy\" configuration.\n"));
270 if (getenv("ENABLE_EXPERIMENTAL_SCUTIL_COMMANDS")) {
271 SCPrint(TRUE
, stderr
, CFSTR("\n"));
272 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --net\n"), command
);
273 SCPrint(TRUE
, stderr
, CFSTR("\tmanage network configuration.\n"));
288 main(int argc
, char * const argv
[])
296 const char *prog
= argv
[0];
297 Boolean proxy
= FALSE
;
298 Boolean reach
= FALSE
;
301 int timeout
= 15; /* default timeout (in seconds) */
303 int xStore
= 0; /* non dynamic store command line options */
305 /* process any arguments */
307 while ((opt
= getopt_long(argc
, argv
, "dvprt:w:", longopts
, &opti
)) != -1)
311 _sc_log
= FALSE
; /* enable framework logging */
315 _sc_log
= FALSE
; /* enable framework logging */
318 enablePrivateAPI
= TRUE
;
325 timeout
= atoi(optarg
);
332 if (strcmp(longopts
[opti
].name
, "dns") == 0) {
335 } else if (strcmp(longopts
[opti
].name
, "get") == 0) {
338 } else if (strcmp(longopts
[opti
].name
, "net") == 0) {
341 } else if (strcmp(longopts
[opti
].name
, "proxy") == 0) {
344 } else if (strcmp(longopts
[opti
].name
, "set") == 0) {
357 // if we are attempting to process more than one type of request
361 /* are we checking the reachability of a host/address */
363 if ((argc
< 1) || (argc
> 2)) {
366 do_checkReachability(argc
, (char **)argv
);
370 /* are we waiting on the presense of a dynamic store key */
372 do_wait(wait
, timeout
);
376 /* are we looking up the DNS configuration */
378 do_showDNSConfiguration(argc
, (char **)argv
);
382 /* are we looking up a preference value */
384 if (findPref(get
) < 0) {
387 do_getPref(get
, argc
, (char **)argv
);
391 /* are we looking up the proxy configuration */
393 do_showProxyConfiguration(argc
, (char **)argv
);
397 /* are we changing a preference value */
399 if (findPref(set
) < 0) {
402 do_setPref(set
, argc
, (char **)argv
);
407 /* if we are going to be managing the network configuration */
408 commands
= (cmdInfo
*)commands_prefs
;
409 nCommands
= nCommands_prefs
;
411 if (!getenv("ENABLE_EXPERIMENTAL_SCUTIL_COMMANDS")) {
415 do_net_init(); /* initialization */
416 do_net_open(0, NULL
); /* open default prefs */
418 /* if we are going to be managing the dynamic store */
419 commands
= (cmdInfo
*)commands_store
;
420 nCommands
= nCommands_store
;
422 do_dictInit(0, NULL
); /* start with an empty dictionary */
423 do_open(0, NULL
); /* open the dynamic store */
426 /* allocate command input stream */
427 src
= (InputRef
)CFAllocatorAllocate(NULL
, sizeof(Input
), 0);
432 if (isatty(fileno(src
->fp
))) {
437 if (tcgetattr(fileno(src
->fp
), &t
) != -1) {
438 if ((t
.c_lflag
& ECHO
) == 0) {
442 src
->el
= el_init(prog
, src
->fp
, stdout
, stderr
);
443 src
->h
= history_init();
445 (void)history(src
->h
, &ev
, H_SETSIZE
, INT_MAX
);
446 el_set(src
->el
, EL_HIST
, history
, src
->h
);
449 el_set(src
->el
, EL_EDITMODE
, 0);
452 el_set(src
->el
, EL_EDITOR
, "emacs");
453 el_set(src
->el
, EL_PROMPT
, prompt
);
455 el_source(src
->el
, NULL
);
457 if ((el_get(src
->el
, EL_EDITMODE
, &editmode
) != -1) && editmode
!= 0) {
458 el_set(src
->el
, EL_SIGNAL
, 1);
467 while (process_line(src
) == TRUE
) {
468 /* debug information, diagnostics */
469 __showMachPortStatus();
472 /* close the socket, free resources */
473 if (src
->h
) history_end(src
->h
);
474 if (src
->el
) el_end(src
->el
);
475 (void)fclose(src
->fp
);
476 CFAllocatorDeallocate(NULL
, src
);
478 exit (EX_OK
); // insure the process exit status is 0
479 return 0; // ...and make main fit the ANSI spec.