Add support for newer CPU models.
[dragonfly.git] / usr.sbin / ngctl / main.c
blob232f01fe40490a04c1bc89adacab12b762486081
1 /*
2 * main.c
4 * Copyright (c) 1996-1999 Whistle Communications, Inc.
5 * All rights reserved.
6 *
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
34 * OF SUCH DAMAGE.
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 $
41 #include "ngctl.h"
43 #define PROMPT "+ "
44 #define MAX_ARGS 512
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[] = {
62 &config_cmd,
63 &connect_cmd,
64 &debug_cmd,
65 &help_cmd,
66 &list_cmd,
67 &mkpeer_cmd,
68 &msg_cmd,
69 &name_cmd,
70 &read_cmd,
71 &rmhook_cmd,
72 &show_cmd,
73 &shutdown_cmd,
74 &status_cmd,
75 &types_cmd,
76 &write_cmd,
77 &quit_cmd,
78 NULL
81 /* Commands defined in this file */
82 const struct ngcmd read_cmd = {
83 ReadCmd,
84 "read <filename>",
85 "Read and execute commands from a file",
86 NULL,
87 { "source", "." }
89 const struct ngcmd help_cmd = {
90 HelpCmd,
91 "help [command]",
92 "Show command summary or get more help on a specific command",
93 NULL,
94 { "?" }
96 const struct ngcmd quit_cmd = {
97 QuitCmd,
98 "quit",
99 "Exit program",
100 NULL,
101 { "exit" }
104 /* Our control and data sockets */
105 int csock, dsock;
108 * main()
111 main(int ac, char *av[])
113 char name[NG_NODESIZ];
114 int interactive = isatty(0) && isatty(1);
115 FILE *fp = NULL;
116 int ch, rtn = 0;
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) {
123 switch (ch) {
124 case 'd':
125 NgSetDebug(NgSetDebug(-1) + 1);
126 break;
127 case 'f':
128 if (strcmp(optarg, "-") == 0)
129 fp = stdin;
130 else if ((fp = fopen(optarg, "r")) == NULL)
131 err(EX_NOINPUT, "%s", optarg);
132 break;
133 case 'n':
134 snprintf(name, sizeof(name), "%s", optarg);
135 break;
136 case '?':
137 default:
138 Usage((char *)NULL);
139 break;
142 ac -= optind;
143 av += optind;
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 */
150 if (ac == 0) {
151 if (fp != NULL) {
152 rtn = ReadFile(fp);
153 } else if (interactive) {
154 rtn = DoInteractive();
155 } else
156 Usage("no command specified");
157 } else {
158 rtn = DoCommand(ac, (const char **)av);
161 /* Convert command return code into system exit code */
162 switch (rtn) {
163 case CMDRTN_OK:
164 case CMDRTN_QUIT:
165 rtn = 0;
166 break;
167 case CMDRTN_USAGE:
168 rtn = EX_USAGE;
169 break;
170 case CMDRTN_ERROR:
171 rtn = EX_OSERR;
172 break;
174 return(rtn);
178 * Process commands from a file
180 static int
181 ReadFile(FILE *fp)
183 char line[LINE_MAX];
184 int num, rtn;
186 for (num = 1; fgets(line, sizeof(line), fp) != NULL; num++) {
187 if (*line == '#')
188 continue;
189 if ((rtn = DoParseCommand(line)) != 0) {
190 warnx("line %d: error in file", num);
191 return(rtn);
194 return(CMDRTN_OK);
198 * Interactive mode
200 static int
201 DoInteractive(void)
203 const int maxfd = MAX(csock, dsock) + 1;
205 (*help_cmd.func)(0, NULL);
206 while (1) {
207 struct timeval tv;
208 fd_set rfds;
210 /* See if any data or control messages are arriving */
211 FD_ZERO(&rfds);
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);
219 fflush(stdout);
220 FD_ZERO(&rfds);
221 FD_SET(0, &rfds);
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))
229 printf("\n");
232 /* Display any incoming control message */
233 if (FD_ISSET(csock, &rfds))
234 MsgRead();
236 /* Display any incoming data packet */
237 if (FD_ISSET(dsock, &rfds)) {
238 u_char buf[8192];
239 char hook[NG_HOOKSIZ];
240 int rl;
242 /* Read packet from socket */
243 if ((rl = NgRecvData(dsock,
244 buf, sizeof(buf), hook)) < 0)
245 err(EX_OSERR, "reading hook \"%s\"", hook);
246 if (rl == 0)
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);
251 DumpAscii(buf, rl);
254 /* Get any user input */
255 if (FD_ISSET(0, &rfds)) {
256 char buf[LINE_MAX];
258 if (fgets(buf, sizeof(buf), stdin) == NULL) {
259 printf("\n");
260 break;
262 if (DoParseCommand(buf) == CMDRTN_QUIT)
263 break;
266 return(CMDRTN_QUIT);
270 * Parse a command line and execute the command
272 static int
273 DoParseCommand(char *line)
275 const char *av[MAX_ARGS];
276 int ac;
278 /* Parse line */
279 for (ac = 0, av[0] = strtok(line, WHITESPACE);
280 ac < MAX_ARGS - 1 && av[ac];
281 av[++ac] = strtok(NULL, WHITESPACE));
283 /* Do command */
284 return(DoCommand(ac, av));
288 * Execute the command
290 static int
291 DoCommand(int ac, const char **av)
293 const struct ngcmd *cmd;
294 int rtn;
296 if (ac == 0 || *av[0] == 0)
297 return(CMDRTN_OK);
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);
302 return(rtn);
306 * Find a command
308 static const struct ngcmd *
309 FindCommand(const char *string)
311 int k, found = -1;
313 for (k = 0; cmds[k] != NULL; k++) {
314 if (MatchCommand(cmds[k], string)) {
315 if (found != -1) {
316 warnx("\"%s\": ambiguous command", string);
317 return(NULL);
319 found = k;
322 if (found == -1) {
323 warnx("\"%s\": unknown command", string);
324 return(NULL);
326 return(cmds[found]);
330 * See if string matches a prefix of "cmd" (or an alias) case insensitively
332 static int
333 MatchCommand(const struct ngcmd *cmd, const char *s)
335 int a;
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)
340 return (1);
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)
347 return (1);
351 /* No match */
352 return (0);
356 * ReadCmd()
358 static int
359 ReadCmd(int ac, const char **av)
361 FILE *fp;
362 int rtn;
364 /* Open file */
365 switch (ac) {
366 case 2:
367 if ((fp = fopen(av[1], "r")) == NULL) {
368 warn("%s", av[1]);
369 return(CMDRTN_ERROR);
371 break;
372 default:
373 return(CMDRTN_USAGE);
376 /* Process it */
377 rtn = ReadFile(fp);
378 fclose(fp);
379 return(rtn);
383 * HelpCmd()
385 static int
386 HelpCmd(int ac, const char **av)
388 const struct ngcmd *cmd;
389 int k;
391 switch (ac) {
392 case 0:
393 case 1:
394 /* Show all commands */
395 printf("Available commands:\n");
396 for (k = 0; cmds[k] != NULL; k++) {
397 char *s, buf[100];
399 cmd = cmds[k];
400 snprintf(buf, sizeof(buf), "%s", cmd->cmd);
401 for (s = buf; *s != '\0' && !isspace(*s); s++);
402 *s = '\0';
403 printf(" %-10s %s\n", buf, cmd->desc);
405 return(CMDRTN_OK);
406 default:
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) {
411 int a = 0;
413 printf("Aliases: ");
414 while (1) {
415 printf("%s", cmd->aliases[a++]);
416 if (a == MAX_CMD_ALIAS
417 || cmd->aliases[a] == NULL) {
418 printf("\n");
419 break;
421 printf(", ");
424 printf("Summary: %s\n", cmd->desc);
425 if (cmd->help != NULL) {
426 const char *s;
427 char buf[65];
428 int tot, len, done;
430 printf("Description:\n");
431 for (s = cmd->help; *s != '\0'; s += len) {
432 while (isspace(*s))
433 s++;
434 tot = snprintf(buf,
435 sizeof(buf), "%s", s);
436 len = strlen(buf);
437 done = len == tot;
438 if (!done) {
439 while (len > 0
440 && !isspace(buf[len-1]))
441 buf[--len] = '\0';
443 printf(" %s\n", buf);
448 return(CMDRTN_OK);
452 * QuitCmd()
454 static int
455 QuitCmd(int ac __unused, const char **av __unused)
457 return(CMDRTN_QUIT);
461 * Dump data in hex and ASCII form
463 void
464 DumpAscii(const u_char *buf, int len)
466 char ch, sbuf[100];
467 int k, count;
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]);
476 } else {
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);
488 } else {
489 snprintf(sbuf + strlen(sbuf),
490 sizeof(sbuf) - strlen(sbuf), " ");
493 printf("%s\n", sbuf);
498 * Usage()
500 static void
501 Usage(const char *msg)
503 if (msg)
504 warnx("%s", msg);
505 errx(EX_USAGE, "usage: ngctl [-d] [-f file] [-n name] [command ...]");