5 * Copyright (c) 1996-1999 Whistle Communications, Inc.
8 * Subject to the following obligations and disclaimer of warranty, use and
9 * redistribution of this software, in source or object code forms, with or
10 * without modifications are expressly permitted by Whistle Communications;
11 * provided, however, that:
12 * 1. Any and all reproductions of the source or object code must include the
13 * copyright notice above and the following disclaimer of warranties; and
14 * 2. No rights are granted, in any manner or form, to use Whistle
15 * Communications, Inc. trademarks, including the mark "WHISTLE
16 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
17 * such appears in the above copyright notice or in the software.
19 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
20 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
21 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
22 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
25 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
26 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
27 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
28 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
29 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
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, 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, char **av);
57 static int HelpCmd(int ac, char **av);
58 static int QuitCmd(int ac, char **av);
60 /* List of commands */
61 static const struct ngcmd *const cmds[] = {
82 /* Commands defined in this file */
83 const struct ngcmd read_cmd = {
86 "Read and execute commands from a file",
90 const struct ngcmd help_cmd = {
93 "Show command summary or get more help on a specific command",
97 const struct ngcmd quit_cmd = {
105 /* Our control and data sockets */
112 main(int ac, char *av[])
114 char name[NG_NODESIZ];
115 int interactive = isatty(0) && isatty(1);
119 /* Set default node name */
120 snprintf(name, sizeof(name), "ngctl%d", getpid());
122 /* Parse command line */
123 while ((ch = getopt(ac, av, "df:n:")) != EOF) {
126 NgSetDebug(NgSetDebug(-1) + 1);
129 if (strcmp(optarg, "-") == 0)
131 else if ((fp = fopen(optarg, "r")) == NULL)
132 err(EX_NOINPUT, "%s", optarg);
135 snprintf(name, sizeof(name), "%s", optarg);
146 /* Create a new socket node */
147 if (NgMkSockNode(name, &csock, &dsock) < 0)
148 err(EX_OSERR, "can't create node");
150 /* Do commands as requested */
154 } else if (interactive) {
155 rtn = DoInteractive();
157 Usage("no command specified");
159 rtn = DoCommand(ac, av);
162 /* Convert command return code into system exit code */
179 * Process commands from a file
187 for (num = 1; fgets(line, sizeof(line), fp) != NULL; num++) {
190 if ((rtn = DoParseCommand(line)) != 0) {
191 warnx("line %d: error in file", num);
204 const int maxfd = MAX(csock, dsock) + 1;
206 (*help_cmd.func)(0, NULL);
211 /* See if any data or control messages are arriving */
213 FD_SET(csock, &rfds);
214 FD_SET(dsock, &rfds);
215 memset(&tv, 0, sizeof(tv));
216 if (select(maxfd, &rfds, NULL, NULL, &tv) <= 0) {
218 /* Issue prompt and wait for anything to happen */
219 printf("%s", PROMPT);
223 FD_SET(csock, &rfds);
224 FD_SET(dsock, &rfds);
225 if (select(maxfd, &rfds, NULL, NULL, NULL) < 0)
226 err(EX_OSERR, "select");
228 /* If not user input, print a newline first */
229 if (!FD_ISSET(0, &rfds))
233 /* Display any incoming control message */
234 if (FD_ISSET(csock, &rfds))
237 /* Display any incoming data packet */
238 if (FD_ISSET(dsock, &rfds)) {
240 char hook[NG_HOOKSIZ];
243 /* Read packet from socket */
244 if ((rl = NgAllocRecvData(dsock, &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);
255 /* Get any user input */
256 if (FD_ISSET(0, &rfds)) {
259 if (fgets(buf, sizeof(buf), stdin) == NULL) {
263 if (DoParseCommand(buf) == CMDRTN_QUIT)
271 * Parse a command line and execute the command
274 DoParseCommand(char *line)
280 for (ac = 0, av[0] = strtok(line, WHITESPACE);
281 ac < MAX_ARGS - 1 && av[ac];
282 av[++ac] = strtok(NULL, WHITESPACE));
285 return(DoCommand(ac, av));
289 * Execute the command
292 DoCommand(int ac, char **av)
294 const struct ngcmd *cmd;
297 if (ac == 0 || *av[0] == 0)
299 if ((cmd = FindCommand(av[0])) == NULL)
300 return(CMDRTN_ERROR);
301 if ((rtn = (*cmd->func)(ac, av)) == CMDRTN_USAGE)
302 warnx("usage: %s", cmd->cmd);
309 static const struct ngcmd *
310 FindCommand(const char *string)
314 for (k = 0; cmds[k] != NULL; k++) {
315 if (MatchCommand(cmds[k], string)) {
317 warnx("\"%s\": ambiguous command", string);
324 warnx("\"%s\": unknown command", string);
331 * See if string matches a prefix of "cmd" (or an alias) case insensitively
334 MatchCommand(const struct ngcmd *cmd, const char *s)
338 /* Try to match command, ignoring the usage stuff */
339 if (strlen(s) <= strcspn(cmd->cmd, WHITESPACE)) {
340 if (strncasecmp(s, cmd->cmd, strlen(s)) == 0)
344 /* Try to match aliases */
345 for (a = 0; a < MAX_CMD_ALIAS && cmd->aliases[a] != NULL; a++) {
346 if (strlen(cmd->aliases[a]) >= strlen(s)) {
347 if (strncasecmp(s, cmd->aliases[a], strlen(s)) == 0)
360 ReadCmd(int ac, char **av)
368 if ((fp = fopen(av[1], "r")) == NULL) {
370 return(CMDRTN_ERROR);
374 return(CMDRTN_USAGE);
387 HelpCmd(int ac, char **av)
389 const struct ngcmd *cmd;
395 /* Show all commands */
396 printf("Available commands:\n");
397 for (k = 0; cmds[k] != NULL; k++) {
401 snprintf(buf, sizeof(buf), "%s", cmd->cmd);
402 for (s = buf; *s != '\0' && !isspace(*s); s++);
404 printf(" %-10s %s\n", buf, cmd->desc);
408 /* Show help on a specific command */
409 if ((cmd = FindCommand(av[1])) != NULL) {
410 printf("usage: %s\n", cmd->cmd);
411 if (cmd->aliases[0] != NULL) {
416 printf("%s", cmd->aliases[a++]);
417 if (a == MAX_CMD_ALIAS
418 || cmd->aliases[a] == NULL) {
425 printf("Summary: %s\n", cmd->desc);
426 if (cmd->help != NULL) {
431 printf("Description:\n");
432 for (s = cmd->help; *s != '\0'; s += len) {
436 sizeof(buf), "%s", s);
441 && !isspace(buf[len-1]))
444 printf(" %s\n", buf);
456 QuitCmd(int ac __unused, char **av __unused)
462 * Dump data in hex and ASCII form
465 DumpAscii(const u_char *buf, int len)
470 for (count = 0; count < len; count += DUMP_BYTES_PER_LINE) {
471 snprintf(sbuf, sizeof(sbuf), "%04x: ", count);
472 for (k = 0; k < DUMP_BYTES_PER_LINE; k++) {
473 if (count + k < len) {
474 snprintf(sbuf + strlen(sbuf),
475 sizeof(sbuf) - strlen(sbuf),
476 "%02x ", buf[count + k]);
478 snprintf(sbuf + strlen(sbuf),
479 sizeof(sbuf) - strlen(sbuf), " ");
482 snprintf(sbuf + strlen(sbuf), sizeof(sbuf) - strlen(sbuf), " ");
483 for (k = 0; k < DUMP_BYTES_PER_LINE; k++) {
484 if (count + k < len) {
485 ch = isprint(buf[count + k]) ?
486 buf[count + k] : '.';
487 snprintf(sbuf + strlen(sbuf),
488 sizeof(sbuf) - strlen(sbuf), "%c", ch);
490 snprintf(sbuf + strlen(sbuf),
491 sizeof(sbuf) - strlen(sbuf), " ");
494 printf("%s\n", sbuf);
502 Usage(const char *msg)
507 "usage: ngctl [-d] [-f file] [-n name] [command ...]\n");