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
);
141 getString(char **line
)
143 char *s
, *e
, c
, *string
;
144 int i
, isQuoted
= 0, escaped
= 0;
146 if (*line
== NULL
) return NULL
;
147 if (**line
== '\0') return NULL
;
149 /* Skip leading white space */
150 while (isspace(**line
)) *line
+= 1;
152 /* Grab the next string */
155 return NULL
; /* no string available */
156 } else if (*s
== '"') {
157 isQuoted
= 1; /* it's a quoted string */
161 for (e
= s
; (c
= *e
) != '\0'; e
++) {
162 if (isQuoted
&& (c
== '"'))
163 break; /* end of quoted string */
167 break; /* if premature end-of-string */
168 if ((*e
== '"') || isspace(*e
))
169 escaped
++; /* if escaped quote or white space */
171 if (!isQuoted
&& isspace(c
))
172 break; /* end of non-quoted string */
175 string
= malloc(e
- s
- escaped
+ 1);
177 for (i
= 0; s
< e
; s
++) {
179 if (!((s
[0] == '\\') && ((s
[1] == '"') || isspace(s
[1])))) i
++;
184 e
++; /* move past end of quoted string */
193 process_line(InputRef src
)
199 char line
[LINE_LENGTH
];
202 // if end-of-file, exit
203 if (getLine(line
, sizeof(line
), src
) == NULL
)
207 SCPrint(TRUE
, stdout
, CFSTR("%d> %s\n"), nesting
, line
);
210 // break up the input line
211 while ((arg
= getString(&s
)) != NULL
) {
213 argv
= (char **)malloc(2 * sizeof(char *));
215 argv
= (char **)reallocf(argv
, ((argc
+ 2) * sizeof(char *)));
220 return TRUE
; // if no arguments
223 /* process the command */
224 if (*argv
[0] != '#') {
225 argv
[argc
] = NULL
; // just in case...
227 do_command(argc
, argv
);
230 /* free the arguments */
231 for (i
= 0; i
< argc
; i
++) {
236 return !termRequested
;
241 usage(const char *command
)
243 SCPrint(TRUE
, stderr
, CFSTR("usage: %s\n"), command
);
244 SCPrint(TRUE
, stderr
, CFSTR("\tinteractive access to the dynamic store.\n"));
245 SCPrint(TRUE
, stderr
, CFSTR("\n"));
246 SCPrint(TRUE
, stderr
, CFSTR(" or: %s -r nodename\n"), command
);
247 SCPrint(TRUE
, stderr
, CFSTR(" or: %s -r address\n"), command
);
248 SCPrint(TRUE
, stderr
, CFSTR(" or: %s -r local-address remote-address\n"), command
);
249 SCPrint(TRUE
, stderr
, CFSTR("\tcheck reachability of node, address, or address pair.\n"));
250 SCPrint(TRUE
, stderr
, CFSTR("\n"));
251 SCPrint(TRUE
, stderr
, CFSTR(" or: %s -w dynamic-store-key [ -t timeout ]\n"), command
);
252 SCPrint(TRUE
, stderr
, CFSTR("\t-w\twait for presense of dynamic store key\n"));
253 SCPrint(TRUE
, stderr
, CFSTR("\t-t\ttime to wait for key\n"));
254 SCPrint(TRUE
, stderr
, CFSTR("\n"));
255 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --get pref\n"), command
);
256 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --set pref [newval]\n"), command
);
257 SCPrint(TRUE
, stderr
, CFSTR("\tpref\tdisplay (or set) the specified preference. Valid preferences\n"));
258 SCPrint(TRUE
, stderr
, CFSTR("\t\tinclude:\n"));
259 SCPrint(TRUE
, stderr
, CFSTR("\t\t\tComputerName, LocalHostName\n"));
260 SCPrint(TRUE
, stderr
, CFSTR("\tnewval\tNew preference value to be set. If not specified,\n"));
261 SCPrint(TRUE
, stderr
, CFSTR("\t\tthe new value will be read from standard input.\n"));
262 SCPrint(TRUE
, stderr
, CFSTR("\n"));
263 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --dns\n"), command
);
264 SCPrint(TRUE
, stderr
, CFSTR("\tshow DNS configuration.\n"));
265 SCPrint(TRUE
, stderr
, CFSTR("\n"));
266 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --proxy\n"), command
);
267 SCPrint(TRUE
, stderr
, CFSTR("\tshow \"proxy\" configuration.\n"));
269 if (getenv("ENABLE_EXPERIMENTAL_SCUTIL_COMMANDS")) {
270 SCPrint(TRUE
, stderr
, CFSTR("\n"));
271 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --net\n"), command
);
272 SCPrint(TRUE
, stderr
, CFSTR("\tmanage network configuration.\n"));
287 main(int argc
, char * const argv
[])
295 const char *prog
= argv
[0];
296 Boolean proxy
= FALSE
;
297 Boolean reach
= FALSE
;
300 int timeout
= 15; /* default timeout (in seconds) */
302 int xStore
= 0; /* non dynamic store command line options */
304 /* process any arguments */
306 while ((opt
= getopt_long(argc
, argv
, "dvprt:w:", longopts
, &opti
)) != -1)
310 _sc_log
= FALSE
; /* enable framework logging */
314 _sc_log
= FALSE
; /* enable framework logging */
317 enablePrivateAPI
= TRUE
;
324 timeout
= atoi(optarg
);
331 if (strcmp(longopts
[opti
].name
, "dns") == 0) {
334 } else if (strcmp(longopts
[opti
].name
, "get") == 0) {
337 } else if (strcmp(longopts
[opti
].name
, "net") == 0) {
340 } else if (strcmp(longopts
[opti
].name
, "proxy") == 0) {
343 } else if (strcmp(longopts
[opti
].name
, "set") == 0) {
356 // if we are attempting to process more than one type of request
360 /* are we checking the reachability of a host/address */
362 if ((argc
< 1) || (argc
> 2)) {
365 do_checkReachability(argc
, (char **)argv
);
369 /* are we waiting on the presense of a dynamic store key */
371 do_wait(wait
, timeout
);
375 /* are we looking up the DNS configuration */
377 do_showDNSConfiguration(argc
, (char **)argv
);
381 /* are we looking up a preference value */
383 if (findPref(get
) < 0) {
386 do_getPref(get
, argc
, (char **)argv
);
390 /* are we looking up the proxy configuration */
392 do_showProxyConfiguration(argc
, (char **)argv
);
396 /* are we changing a preference value */
398 if (findPref(set
) < 0) {
401 do_setPref(set
, argc
, (char **)argv
);
406 /* if we are going to be managing the network configuration */
407 commands
= (cmdInfo
*)commands_prefs
;
408 nCommands
= nCommands_prefs
;
410 if (!getenv("ENABLE_EXPERIMENTAL_SCUTIL_COMMANDS")) {
414 do_net_init(); /* initialization */
415 do_net_open(0, NULL
); /* open default prefs */
417 /* if we are going to be managing the dynamic store */
418 commands
= (cmdInfo
*)commands_store
;
419 nCommands
= nCommands_store
;
421 do_dictInit(0, NULL
); /* start with an empty dictionary */
422 do_open(0, NULL
); /* open the dynamic store */
425 /* allocate command input stream */
426 src
= (InputRef
)CFAllocatorAllocate(NULL
, sizeof(Input
), 0);
431 if (isatty(fileno(src
->fp
))) {
436 if (tcgetattr(fileno(src
->fp
), &t
) != -1) {
437 if ((t
.c_lflag
& ECHO
) == 0) {
441 src
->el
= el_init(prog
, src
->fp
, stdout
, stderr
);
442 src
->h
= history_init();
444 (void)history(src
->h
, &ev
, H_SETSIZE
, INT_MAX
);
445 el_set(src
->el
, EL_HIST
, history
, src
->h
);
448 el_set(src
->el
, EL_EDITMODE
, 0);
451 el_set(src
->el
, EL_EDITOR
, "emacs");
452 el_set(src
->el
, EL_PROMPT
, prompt
);
454 el_source(src
->el
, NULL
);
456 if ((el_get(src
->el
, EL_EDITMODE
, &editmode
) != -1) && editmode
!= 0) {
457 el_set(src
->el
, EL_SIGNAL
, 1);
466 while (process_line(src
) == TRUE
) {
467 /* debug information, diagnostics */
468 __showMachPortStatus();
471 /* close the socket, free resources */
472 if (src
->h
) history_end(src
->h
);
473 if (src
->el
) el_end(src
->el
);
474 (void)fclose(src
->fp
);
475 CFAllocatorDeallocate(NULL
, src
);
477 exit (EX_OK
); // insure the process exit status is 0
478 return 0; // ...and make main fit the ANSI spec.