main.c (158882) | main.c (160002) |
---|---|
1 2/* 3 * main.c 4 * 5 * Copyright (c) 1996-1999 Whistle Communications, Inc. 6 * All rights reserved. 7 * 8 * Subject to the following obligations and disclaimer of warranty, use and --- 20 unchanged lines hidden (view full) --- 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 35 * OF SUCH DAMAGE. 36 * | 1 2/* 3 * main.c 4 * 5 * Copyright (c) 1996-1999 Whistle Communications, Inc. 6 * All rights reserved. 7 * 8 * Subject to the following obligations and disclaimer of warranty, use and --- 20 unchanged lines hidden (view full) --- 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 35 * OF SUCH DAMAGE. 36 * |
37 * $FreeBSD: head/usr.sbin/ngctl/main.c 158882 2006-05-24 14:46:55Z glebius $ | 37 * $FreeBSD: head/usr.sbin/ngctl/main.c 160002 2006-06-28 10:38:38Z glebius $ |
38 * $Whistle: main.c,v 1.12 1999/11/29 19:17:46 archie Exp $ 39 */ 40 41#include <sys/param.h> 42#include <sys/socket.h> 43#include <sys/select.h> 44 45#include <ctype.h> --- 137 unchanged lines hidden (view full) --- 183 break; 184 case CMDRTN_USAGE: 185 rtn = EX_USAGE; 186 break; 187 case CMDRTN_ERROR: 188 rtn = EX_OSERR; 189 break; 190 } | 38 * $Whistle: main.c,v 1.12 1999/11/29 19:17:46 archie Exp $ 39 */ 40 41#include <sys/param.h> 42#include <sys/socket.h> 43#include <sys/select.h> 44 45#include <ctype.h> --- 137 unchanged lines hidden (view full) --- 183 break; 184 case CMDRTN_USAGE: 185 rtn = EX_USAGE; 186 break; 187 case CMDRTN_ERROR: 188 rtn = EX_OSERR; 189 break; 190 } |
191 return(rtn); | 191 return (rtn); |
192} 193 194/* 195 * Process commands from a file 196 */ 197static int 198ReadFile(FILE *fp) 199{ 200 char line[LINE_MAX]; 201 int num, rtn; 202 203 for (num = 1; fgets(line, sizeof(line), fp) != NULL; num++) { 204 if (*line == '#') 205 continue; 206 if ((rtn = DoParseCommand(line)) != 0) { 207 warnx("line %d: error in file", num); | 192} 193 194/* 195 * Process commands from a file 196 */ 197static int 198ReadFile(FILE *fp) 199{ 200 char line[LINE_MAX]; 201 int num, rtn; 202 203 for (num = 1; fgets(line, sizeof(line), fp) != NULL; num++) { 204 if (*line == '#') 205 continue; 206 if ((rtn = DoParseCommand(line)) != 0) { 207 warnx("line %d: error in file", num); |
208 return(rtn); | 208 return (rtn); |
209 } 210 } | 209 } 210 } |
211 return(CMDRTN_OK); | 211 return (CMDRTN_OK); |
212} 213 214/* 215 * Interactive mode 216 */ 217static int 218DoInteractive(void) 219{ --- 55 unchanged lines hidden (view full) --- 275 if (fgets(buf, sizeof(buf), stdin) == NULL) { 276 printf("\n"); 277 break; 278 } 279 if (DoParseCommand(buf) == CMDRTN_QUIT) 280 break; 281 } 282 } | 212} 213 214/* 215 * Interactive mode 216 */ 217static int 218DoInteractive(void) 219{ --- 55 unchanged lines hidden (view full) --- 275 if (fgets(buf, sizeof(buf), stdin) == NULL) { 276 printf("\n"); 277 break; 278 } 279 if (DoParseCommand(buf) == CMDRTN_QUIT) 280 break; 281 } 282 } |
283 return(CMDRTN_QUIT); | 283 return (CMDRTN_QUIT); |
284} 285 286/* 287 * Parse a command line and execute the command 288 */ 289static int 290DoParseCommand(char *line) 291{ 292 char *av[MAX_ARGS]; 293 int ac; 294 295 /* Parse line */ 296 for (ac = 0, av[0] = strtok(line, WHITESPACE); 297 ac < MAX_ARGS - 1 && av[ac]; 298 av[++ac] = strtok(NULL, WHITESPACE)); 299 300 /* Do command */ | 284} 285 286/* 287 * Parse a command line and execute the command 288 */ 289static int 290DoParseCommand(char *line) 291{ 292 char *av[MAX_ARGS]; 293 int ac; 294 295 /* Parse line */ 296 for (ac = 0, av[0] = strtok(line, WHITESPACE); 297 ac < MAX_ARGS - 1 && av[ac]; 298 av[++ac] = strtok(NULL, WHITESPACE)); 299 300 /* Do command */ |
301 return(DoCommand(ac, av)); | 301 return (DoCommand(ac, av)); |
302} 303 304/* 305 * Execute the command 306 */ 307static int 308DoCommand(int ac, char **av) 309{ 310 const struct ngcmd *cmd; 311 int rtn; 312 313 if (ac == 0 || *av[0] == 0) | 302} 303 304/* 305 * Execute the command 306 */ 307static int 308DoCommand(int ac, char **av) 309{ 310 const struct ngcmd *cmd; 311 int rtn; 312 313 if (ac == 0 || *av[0] == 0) |
314 return(CMDRTN_OK); | 314 return (CMDRTN_OK); |
315 if ((cmd = FindCommand(av[0])) == NULL) | 315 if ((cmd = FindCommand(av[0])) == NULL) |
316 return(CMDRTN_ERROR); | 316 return (CMDRTN_ERROR); |
317 if ((rtn = (*cmd->func)(ac, av)) == CMDRTN_USAGE) 318 warnx("usage: %s", cmd->cmd); | 317 if ((rtn = (*cmd->func)(ac, av)) == CMDRTN_USAGE) 318 warnx("usage: %s", cmd->cmd); |
319 return(rtn); | 319 return (rtn); |
320} 321 322/* 323 * Find a command 324 */ 325static const struct ngcmd * 326FindCommand(const char *string) 327{ 328 int k, found = -1; 329 330 for (k = 0; cmds[k] != NULL; k++) { 331 if (MatchCommand(cmds[k], string)) { 332 if (found != -1) { 333 warnx("\"%s\": ambiguous command", string); | 320} 321 322/* 323 * Find a command 324 */ 325static const struct ngcmd * 326FindCommand(const char *string) 327{ 328 int k, found = -1; 329 330 for (k = 0; cmds[k] != NULL; k++) { 331 if (MatchCommand(cmds[k], string)) { 332 if (found != -1) { 333 warnx("\"%s\": ambiguous command", string); |
334 return(NULL); | 334 return (NULL); |
335 } 336 found = k; 337 } 338 } 339 if (found == -1) { 340 warnx("\"%s\": unknown command", string); | 335 } 336 found = k; 337 } 338 } 339 if (found == -1) { 340 warnx("\"%s\": unknown command", string); |
341 return(NULL); | 341 return (NULL); |
342 } | 342 } |
343 return(cmds[found]); | 343 return (cmds[found]); |
344} 345 346/* 347 * See if string matches a prefix of "cmd" (or an alias) case insensitively 348 */ 349static int 350MatchCommand(const struct ngcmd *cmd, const char *s) 351{ --- 26 unchanged lines hidden (view full) --- 378 FILE *fp; 379 int rtn; 380 381 /* Open file */ 382 switch (ac) { 383 case 2: 384 if ((fp = fopen(av[1], "r")) == NULL) { 385 warn("%s", av[1]); | 344} 345 346/* 347 * See if string matches a prefix of "cmd" (or an alias) case insensitively 348 */ 349static int 350MatchCommand(const struct ngcmd *cmd, const char *s) 351{ --- 26 unchanged lines hidden (view full) --- 378 FILE *fp; 379 int rtn; 380 381 /* Open file */ 382 switch (ac) { 383 case 2: 384 if ((fp = fopen(av[1], "r")) == NULL) { 385 warn("%s", av[1]); |
386 return(CMDRTN_ERROR); | 386 return (CMDRTN_ERROR); |
387 } 388 break; 389 default: | 387 } 388 break; 389 default: |
390 return(CMDRTN_USAGE); | 390 return (CMDRTN_USAGE); |
391 } 392 393 /* Process it */ 394 rtn = ReadFile(fp); 395 fclose(fp); | 391 } 392 393 /* Process it */ 394 rtn = ReadFile(fp); 395 fclose(fp); |
396 return(rtn); | 396 return (rtn); |
397} 398 399/* 400 * HelpCmd() 401 */ 402static int 403HelpCmd(int ac, char **av) 404{ --- 9 unchanged lines hidden (view full) --- 414 char *s, buf[100]; 415 416 cmd = cmds[k]; 417 snprintf(buf, sizeof(buf), "%s", cmd->cmd); 418 for (s = buf; *s != '\0' && !isspace(*s); s++); 419 *s = '\0'; 420 printf(" %-10s %s\n", buf, cmd->desc); 421 } | 397} 398 399/* 400 * HelpCmd() 401 */ 402static int 403HelpCmd(int ac, char **av) 404{ --- 9 unchanged lines hidden (view full) --- 414 char *s, buf[100]; 415 416 cmd = cmds[k]; 417 snprintf(buf, sizeof(buf), "%s", cmd->cmd); 418 for (s = buf; *s != '\0' && !isspace(*s); s++); 419 *s = '\0'; 420 printf(" %-10s %s\n", buf, cmd->desc); 421 } |
422 return(CMDRTN_OK); | 422 return (CMDRTN_OK); |
423 default: 424 /* Show help on a specific command */ 425 if ((cmd = FindCommand(av[1])) != NULL) { 426 printf("usage: %s\n", cmd->cmd); 427 if (cmd->aliases[0] != NULL) { 428 int a = 0; 429 430 printf("Aliases: "); --- 26 unchanged lines hidden (view full) --- 457 && !isspace(buf[len-1])) 458 buf[--len] = '\0'; 459 } 460 printf(" %s\n", buf); 461 } 462 } 463 } 464 } | 423 default: 424 /* Show help on a specific command */ 425 if ((cmd = FindCommand(av[1])) != NULL) { 426 printf("usage: %s\n", cmd->cmd); 427 if (cmd->aliases[0] != NULL) { 428 int a = 0; 429 430 printf("Aliases: "); --- 26 unchanged lines hidden (view full) --- 457 && !isspace(buf[len-1])) 458 buf[--len] = '\0'; 459 } 460 printf(" %s\n", buf); 461 } 462 } 463 } 464 } |
465 return(CMDRTN_OK); | 465 return (CMDRTN_OK); |
466} 467 468/* 469 * QuitCmd() 470 */ 471static int 472QuitCmd(int ac __unused, char **av __unused) 473{ | 466} 467 468/* 469 * QuitCmd() 470 */ 471static int 472QuitCmd(int ac __unused, char **av __unused) 473{ |
474 return(CMDRTN_QUIT); | 474 return (CMDRTN_QUIT); |
475} 476 477/* 478 * Dump data in hex and ASCII form 479 */ 480void 481DumpAscii(const u_char *buf, int len) 482{ --- 43 unchanged lines hidden --- | 475} 476 477/* 478 * Dump data in hex and ASCII form 479 */ 480void 481DumpAscii(const u_char *buf, int len) 482{ --- 43 unchanged lines hidden --- |