2 * Copyright (c) 2000-2003 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 * September 25, 2002 Allan Nathanson <ajn@apple.com>
28 * - added command line history & editing
30 * July 9, 2001 Allan Nathanson <ajn@apple.com>
31 * - added "-r" option for checking network reachability
32 * - added "-w" option to check/wait for the presence of a
35 * June 1, 2001 Allan Nathanson <ajn@apple.com>
36 * - public API conversion
38 * November 9, 2000 Allan Nathanson <ajn@apple.com>
52 #include <mach/mach.h>
53 #include <mach/mach_error.h>
58 #include "dictionary.h"
62 #include "SCDynamicStoreInternal.h"
65 #define LINE_LENGTH 256
69 CFRunLoopRef notifyRl
= NULL
;
70 CFRunLoopSourceRef notifyRls
= NULL
;
71 SCDynamicStoreRef store
= NULL
;
72 CFPropertyListRef value
= NULL
;
73 CFMutableArrayRef watchedKeys
= NULL
;
74 CFMutableArrayRef watchedPatterns
= NULL
;
76 static struct option longopts
[] = {
77 // { "debug", no_argument, NULL, 'd' },
78 // { "verbose", no_argument, NULL, 'v' },
79 // { "SPI", no_argument, NULL, 'p' },
80 // { "check-reachability", required_argument, NULL, 'r' },
81 // { "timeout", required_argument, NULL, 't' },
82 // { "wait-key", required_argument, NULL, 'w' },
83 { "get", required_argument
, NULL
, 0 },
84 { "help", no_argument
, NULL
, '?' },
85 { "set", required_argument
, NULL
, 0 },
91 getLine(char *buf
, int len
, InputRef src
)
99 line
= el_gets(src
->el
, &count
);
103 strncpy(buf
, line
, len
);
105 if (fgets(buf
, len
, src
->fp
) == NULL
)
110 if (buf
[n
-1] == '\n') {
111 /* the entire line fit in the buffer, remove the newline */
113 } else if (!src
->el
) {
114 /* eat the remainder of the line */
117 } while ((n
!= '\n') && (n
!= EOF
));
123 history(src
->h
, &ev
, H_ENTER
, buf
);
131 getString(char **line
)
133 char *s
, *e
, c
, *string
;
134 int i
, isQuoted
= 0, escaped
= 0;
136 if (*line
== NULL
) return NULL
;
137 if (**line
== '\0') return NULL
;
139 /* Skip leading white space */
140 while (isspace(**line
)) *line
+= 1;
142 /* Grab the next string */
145 return NULL
; /* no string available */
146 } else if (*s
== '"') {
147 isQuoted
= 1; /* it's a quoted string */
151 for (e
= s
; (c
= *e
) != '\0'; e
++) {
152 if (isQuoted
&& (c
== '"'))
153 break; /* end of quoted string */
157 break; /* if premature end-of-string */
158 if ((*e
== '"') || isspace(*e
))
159 escaped
++; /* if escaped quote or white space */
161 if (!isQuoted
&& isspace(c
))
162 break; /* end of non-quoted string */
165 string
= malloc(e
- s
- escaped
+ 1);
167 for (i
= 0; s
< e
; s
++) {
169 if (!((s
[0] == '\\') && ((s
[1] == '"') || isspace(s
[1])))) i
++;
174 e
++; /* move past end of quoted string */
182 process_line(InputRef src
)
184 char line
[LINE_LENGTH
], *s
, *arg
, **argv
= NULL
;
187 /* if end-of-file, exit */
188 if (getLine(line
, sizeof(line
), src
) == NULL
)
192 SCPrint(TRUE
, stdout
, CFSTR("%d> %s\n"), nesting
, line
);
195 /* if requested, exit */
196 if (strcasecmp(line
, "exit") == 0) return FALSE
;
197 if (strcasecmp(line
, "quit") == 0) return FALSE
;
198 if (strcasecmp(line
, "q" ) == 0) return FALSE
;
200 /* break up the input line */
203 while ((arg
= getString(&s
)) != NULL
) {
205 argv
= (char **)malloc(2 * sizeof(char *));
207 argv
= (char **)realloc(argv
, ((argc
+ 2) * sizeof(char *)));
211 /* process the command */
213 argv
[argc
] = NULL
; /* just in case... */
216 do_command(argc
, argv
);
218 for (i
= 0; i
< argc
; i
++)
228 usage(const char *command
)
230 SCPrint(TRUE
, stderr
, CFSTR("usage: %s\n"), command
);
231 SCPrint(TRUE
, stderr
, CFSTR("\tinteractive access to the dynamic store.\n"));
232 SCPrint(TRUE
, stderr
, CFSTR("\n"));
233 SCPrint(TRUE
, stderr
, CFSTR(" or: %s -r nodename\n"), command
);
234 SCPrint(TRUE
, stderr
, CFSTR(" or: %s -r address\n"), command
);
235 SCPrint(TRUE
, stderr
, CFSTR(" or: %s -r local-address remote-address\n"), command
);
236 SCPrint(TRUE
, stderr
, CFSTR("\tcheck reachability of node, address, or address pair.\n"));
237 SCPrint(TRUE
, stderr
, CFSTR("\n"));
238 SCPrint(TRUE
, stderr
, CFSTR(" or: %s -w dynamic-store-key [ -t timeout ]\n"), command
);
239 SCPrint(TRUE
, stderr
, CFSTR("\t-w\twait for presense of dynamic store key\n"));
240 SCPrint(TRUE
, stderr
, CFSTR("\t-t\ttime to wait for key\n"));
241 SCPrint(TRUE
, stderr
, CFSTR("\n"));
242 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --get pref\n"), command
);
243 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --set pref [newval]\n"), command
);
244 SCPrint(TRUE
, stderr
, CFSTR("\tpref\tdisplay (or set) the specified preference. Valid preferences\n"));
245 SCPrint(TRUE
, stderr
, CFSTR("\t\tinclude:\n"));
246 SCPrint(TRUE
, stderr
, CFSTR("\t\t\tComputerName, LocalHostName\n"));
247 SCPrint(TRUE
, stderr
, CFSTR("\tnewval\tNew preference value to be set. If not specified,\n"));
248 SCPrint(TRUE
, stderr
, CFSTR("\t\tthe new value will be read from standard input.\n"));
261 main(int argc
, char * const argv
[])
267 const char *prog
= argv
[0];
268 Boolean reach
= FALSE
;
271 int timeout
= 15; /* default timeout (in seconds) */
273 int xStore
= 0; /* non dynamic store command line options */
275 /* process any arguments */
277 while ((opt
= getopt_long(argc
, argv
, "dvprt:w:", longopts
, &opti
)) != -1)
281 _sc_log
= FALSE
; /* enable framework logging */
285 _sc_log
= FALSE
; /* enable framework logging */
288 enablePrivateAPI
= TRUE
;
295 timeout
= atoi(optarg
);
302 if (strcmp(longopts
[opti
].name
, "get") == 0) {
305 } else if (strcmp(longopts
[opti
].name
, "set") == 0) {
318 // if we are attempting to process more than one type of request
322 /* are we checking the reachability of a host/address */
324 if ((argc
< 1) || (argc
> 2)) {
327 do_checkReachability(argc
, (char **)argv
);
331 /* are we waiting on the presense of a dynamic store key */
333 do_wait(wait
, timeout
);
337 /* are we looking up a preference value */
339 if (findPref(get
) < 0) {
342 do_getPref(get
, argc
, (char **)argv
);
346 /* are we changing a preference value */
348 if (findPref(set
) < 0) {
351 do_setPref(set
, argc
, (char **)argv
);
355 /* start with an empty dictionary */
356 do_dictInit(0, NULL
);
358 /* allocate command input stream */
359 src
= (InputRef
)CFAllocatorAllocate(NULL
, sizeof(Input
), 0);
364 if (isatty(fileno(src
->fp
))) {
369 if (tcgetattr(fileno(src
->fp
), &t
) != -1) {
370 if ((t
.c_lflag
& ECHO
) == 0) {
374 src
->el
= el_init(prog
, src
->fp
, stdout
, stderr
);
375 src
->h
= history_init();
377 (void)history(src
->h
, &ev
, H_SETSIZE
, INT_MAX
);
378 el_set(src
->el
, EL_HIST
, history
, src
->h
);
381 el_set(src
->el
, EL_EDITMODE
, 0);
384 el_set(src
->el
, EL_EDITOR
, "emacs");
385 el_set(src
->el
, EL_PROMPT
, prompt
);
387 el_source(src
->el
, NULL
);
389 if ((el_get(src
->el
, EL_EDITMODE
, &editmode
) != -1) && editmode
!= 0) {
390 el_set(src
->el
, EL_SIGNAL
, 1);
399 while (process_line(src
) == TRUE
) {
400 /* debug information, diagnostics */
401 __showMachPortStatus();
404 /* close the socket, free resources */
405 if (src
->h
) history_end(src
->h
);
406 if (src
->el
) el_end(src
->el
);
407 (void)fclose(src
->fp
);
408 CFAllocatorDeallocate(NULL
, src
);
410 exit (EX_OK
); // insure the process exit status is 0
411 return 0; // ...and make main fit the ANSI spec.