4 * Copyright (c) 1996-1999 Whistle Communications, Inc.
7 * Subject to the following obligations and disclaimer of warranty, use and
8 * redistribution of this software, in source or object code forms, with or
9 * without modifications are expressly permitted by Whistle Communications;
10 * provided, however, that:
11 * 1. Any and all reproductions of the source or object code must include the
12 * copyright notice above and the following disclaimer of warranties; and
13 * 2. No rights are granted, in any manner or form, to use Whistle
14 * Communications, Inc. trademarks, including the mark "WHISTLE
15 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
16 * such appears in the above copyright notice or in the software.
18 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
19 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
20 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
21 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
23 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
24 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
25 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
26 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
27 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
28 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
29 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
30 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
36 * $FreeBSD: src/usr.sbin/ngctl/main.c,v 1.4.2.4 2002/02/01 18:17:43 archie Exp $
37 * $DragonFly: src/usr.sbin/ngctl/main.c,v 1.5 2007/06/04 00:40:31 swildner Exp $
38 * $Whistle: main.c,v 1.12 1999/11/29 19:17:46 archie Exp $
45 #define WHITESPACE " \t\r\n\v\f"
46 #define DUMP_BYTES_PER_LINE 16
48 /* Internal functions */
49 static int ReadFile(FILE *fp
);
50 static int DoParseCommand(char *line
);
51 static int DoCommand(int ac
, const char **av
);
52 static int DoInteractive(void);
53 static const struct ngcmd
*FindCommand(const char *string
);
54 static int MatchCommand(const struct ngcmd
*cmd
, const char *s
);
55 static void Usage(const char *msg
);
56 static int ReadCmd(int ac
, const char **av
);
57 static int HelpCmd(int ac
, const char **av
);
58 static int QuitCmd(int ac
, const char **av
);
60 /* List of commands */
61 static const struct ngcmd
*const cmds
[] = {
81 /* Commands defined in this file */
82 const struct ngcmd read_cmd
= {
85 "Read and execute commands from a file",
89 const struct ngcmd help_cmd
= {
92 "Show command summary or get more help on a specific command",
96 const struct ngcmd quit_cmd
= {
104 /* Our control and data sockets */
111 main(int ac
, char *av
[])
113 char name
[NG_NODESIZ
];
114 int interactive
= isatty(0) && isatty(1);
118 /* Set default node name */
119 snprintf(name
, sizeof(name
), "ngctl%d", getpid());
121 /* Parse command line */
122 while ((ch
= getopt(ac
, av
, "df:n:")) != EOF
) {
125 NgSetDebug(NgSetDebug(-1) + 1);
128 if (strcmp(optarg
, "-") == 0)
130 else if ((fp
= fopen(optarg
, "r")) == NULL
)
131 err(EX_NOINPUT
, "%s", optarg
);
134 snprintf(name
, sizeof(name
), "%s", optarg
);
145 /* Create a new socket node */
146 if (NgMkSockNode(name
, &csock
, &dsock
) < 0)
147 err(EX_OSERR
, "can't create node");
149 /* Do commands as requested */
153 } else if (interactive
) {
154 rtn
= DoInteractive();
156 Usage("no command specified");
158 rtn
= DoCommand(ac
, (const char **)av
);
161 /* Convert command return code into system exit code */
178 * Process commands from a file
186 for (num
= 1; fgets(line
, sizeof(line
), fp
) != NULL
; num
++) {
189 if ((rtn
= DoParseCommand(line
)) != 0) {
190 warnx("line %d: error in file", num
);
203 const int maxfd
= MAX(csock
, dsock
) + 1;
205 (*help_cmd
.func
)(0, NULL
);
210 /* See if any data or control messages are arriving */
212 FD_SET(csock
, &rfds
);
213 FD_SET(dsock
, &rfds
);
214 memset(&tv
, 0, sizeof(tv
));
215 if (select(maxfd
, &rfds
, NULL
, NULL
, &tv
) <= 0) {
217 /* Issue prompt and wait for anything to happen */
218 printf("%s", PROMPT
);
222 FD_SET(csock
, &rfds
);
223 FD_SET(dsock
, &rfds
);
224 if (select(maxfd
, &rfds
, NULL
, NULL
, NULL
) < 0)
225 err(EX_OSERR
, "select");
227 /* If not user input, print a newline first */
228 if (!FD_ISSET(0, &rfds
))
232 /* Display any incoming control message */
233 if (FD_ISSET(csock
, &rfds
))
236 /* Display any incoming data packet */
237 if (FD_ISSET(dsock
, &rfds
)) {
239 char hook
[NG_HOOKSIZ
];
242 /* Read packet from socket */
243 if ((rl
= NgRecvData(dsock
,
244 buf
, sizeof(buf
), hook
)) < 0)
245 err(EX_OSERR
, "reading hook \"%s\"", hook
);
247 errx(EX_OSERR
, "EOF from hook \"%s\"?", hook
);
249 /* Write packet to stdout */
250 printf("Rec'd data packet on hook \"%s\":\n", hook
);
254 /* Get any user input */
255 if (FD_ISSET(0, &rfds
)) {
258 if (fgets(buf
, sizeof(buf
), stdin
) == NULL
) {
262 if (DoParseCommand(buf
) == CMDRTN_QUIT
)
270 * Parse a command line and execute the command
273 DoParseCommand(char *line
)
275 const char *av
[MAX_ARGS
];
279 for (ac
= 0, av
[0] = strtok(line
, WHITESPACE
);
280 ac
< MAX_ARGS
- 1 && av
[ac
];
281 av
[++ac
] = strtok(NULL
, WHITESPACE
));
284 return(DoCommand(ac
, av
));
288 * Execute the command
291 DoCommand(int ac
, const char **av
)
293 const struct ngcmd
*cmd
;
296 if (ac
== 0 || *av
[0] == 0)
298 if ((cmd
= FindCommand(av
[0])) == NULL
)
299 return(CMDRTN_ERROR
);
300 if ((rtn
= (*cmd
->func
)(ac
, av
)) == CMDRTN_USAGE
)
301 warnx("usage: %s", cmd
->cmd
);
308 static const struct ngcmd
*
309 FindCommand(const char *string
)
313 for (k
= 0; cmds
[k
] != NULL
; k
++) {
314 if (MatchCommand(cmds
[k
], string
)) {
316 warnx("\"%s\": ambiguous command", string
);
323 warnx("\"%s\": unknown command", string
);
330 * See if string matches a prefix of "cmd" (or an alias) case insensitively
333 MatchCommand(const struct ngcmd
*cmd
, const char *s
)
337 /* Try to match command, ignoring the usage stuff */
338 if (strlen(s
) <= strcspn(cmd
->cmd
, WHITESPACE
)) {
339 if (strncasecmp(s
, cmd
->cmd
, strlen(s
)) == 0)
343 /* Try to match aliases */
344 for (a
= 0; a
< MAX_CMD_ALIAS
&& cmd
->aliases
[a
] != NULL
; a
++) {
345 if (strlen(cmd
->aliases
[a
]) >= strlen(s
)) {
346 if (strncasecmp(s
, cmd
->aliases
[a
], strlen(s
)) == 0)
359 ReadCmd(int ac
, const char **av
)
367 if ((fp
= fopen(av
[1], "r")) == NULL
) {
369 return(CMDRTN_ERROR
);
373 return(CMDRTN_USAGE
);
386 HelpCmd(int ac
, const char **av
)
388 const struct ngcmd
*cmd
;
394 /* Show all commands */
395 printf("Available commands:\n");
396 for (k
= 0; cmds
[k
] != NULL
; k
++) {
400 snprintf(buf
, sizeof(buf
), "%s", cmd
->cmd
);
401 for (s
= buf
; *s
!= '\0' && !isspace(*s
); s
++);
403 printf(" %-10s %s\n", buf
, cmd
->desc
);
407 /* Show help on a specific command */
408 if ((cmd
= FindCommand(av
[1])) != NULL
) {
409 printf("Usage: %s\n", cmd
->cmd
);
410 if (cmd
->aliases
[0] != NULL
) {
415 printf("%s", cmd
->aliases
[a
++]);
416 if (a
== MAX_CMD_ALIAS
417 || cmd
->aliases
[a
] == NULL
) {
424 printf("Summary: %s\n", cmd
->desc
);
425 if (cmd
->help
!= NULL
) {
430 printf("Description:\n");
431 for (s
= cmd
->help
; *s
!= '\0'; s
+= len
) {
435 sizeof(buf
), "%s", s
);
440 && !isspace(buf
[len
-1]))
443 printf(" %s\n", buf
);
455 QuitCmd(int ac __unused
, const char **av __unused
)
461 * Dump data in hex and ASCII form
464 DumpAscii(const u_char
*buf
, int len
)
469 for (count
= 0; count
< len
; count
+= DUMP_BYTES_PER_LINE
) {
470 snprintf(sbuf
, sizeof(sbuf
), "%04x: ", count
);
471 for (k
= 0; k
< DUMP_BYTES_PER_LINE
; k
++) {
472 if (count
+ k
< len
) {
473 snprintf(sbuf
+ strlen(sbuf
),
474 sizeof(sbuf
) - strlen(sbuf
),
475 "%02x ", buf
[count
+ k
]);
477 snprintf(sbuf
+ strlen(sbuf
),
478 sizeof(sbuf
) - strlen(sbuf
), " ");
481 snprintf(sbuf
+ strlen(sbuf
), sizeof(sbuf
) - strlen(sbuf
), " ");
482 for (k
= 0; k
< DUMP_BYTES_PER_LINE
; k
++) {
483 if (count
+ k
< len
) {
484 ch
= isprint(buf
[count
+ k
]) ?
485 buf
[count
+ k
] : '.';
486 snprintf(sbuf
+ strlen(sbuf
),
487 sizeof(sbuf
) - strlen(sbuf
), "%c", ch
);
489 snprintf(sbuf
+ strlen(sbuf
),
490 sizeof(sbuf
) - strlen(sbuf
), " ");
493 printf("%s\n", sbuf
);
501 Usage(const char *msg
)
505 errx(EX_USAGE
, "usage: ngctl [-d] [-f file] [-n name] [command ...]");