sbin/atm/atm/atm.c
static int
do_cmd(const struct cmd *descp, int argc, char **argv)
{
	const struct cmd	*cmdp = NULL;

	/*
	 * Make sure we have paramaters to process
	 */
	if (!argc) {
		usage(cmds, "");
		exit(1);
	}

	/*
	 * Figure out what command user wants
	 */
	for (; descp->name; descp++) {
		/*
		 * Use an exact match if there is one
		 */
		if (!strcasecmp(descp->name, argv[0])) {
			cmdp = descp;
			break;
		}
		/*
		 * Look for a match on the first part of keyword
		 */
		if (!strncasecmp(descp->name, argv[0], strlen(argv[0]))) {
			if (cmdp) {
				fprintf(stderr, "%s: Ambiguous parameter \"%s\"\n",
						prog, argv[0]);
				exit(1);
			}
			cmdp = descp;
		}
	}
	if (!cmdp)
		return(1);
	argc--; argv++;

	/*
	 * See if this command has subcommands
	 */
	if (cmdp->func == NULL) {
		strcat(prefix, cmdp->name);
		strcat(prefix, " ");
		return(do_cmd((const struct cmd *)cmdp->help, argc, argv));
	}

	/*
	 * Minimal validation
	 */
	if ((argc < cmdp->minp) || (argc > cmdp->maxp)) {
		fprintf(stderr, "%s: Invalid number of arguments\n",
			prog);
		fprintf(stderr, "\tformat is: %s%s %s\n",
			prefix, cmdp->name, cmdp->help);
		exit(1);
	}

	/*
	 * Process command
	 */
	(*cmdp->func)(argc, argv, cmdp);
	return(0);
}
/*
 * Validate and process user command
 * 
 * Arguments:
 *	descp	pointer to command description array
 *	argc	number of arguments left in command
 *	argv	pointer to argument strings
 *
 * Returns:
 *	none
 *
 */
Validate and process user command

descp   pointer to command description array
argc    number of arguments left in command
argv    pointer to argument strings

none
Validate and process user command descp pointer to command description array argc number of arguments left in command argv pointer to argument strings none
static int
do_cmd(const struct cmd *descp, int argc, char **argv)
{
	const struct cmd	*cmdp = NULL;
	if (!argc) {
		usage(cmds, "");
		exit(1);
	}
	for (; descp->name; descp++) {
		if (!strcasecmp(descp->name, argv[0])) {
			cmdp = descp;
			break;
		}
		if (!strncasecmp(descp->name, argv[0], strlen(argv[0]))) {
			if (cmdp) {
				fprintf(stderr, "%s: Ambiguous parameter \"%s\"\n",
						prog, argv[0]);
				exit(1);
			}
			cmdp = descp;
		}
	}
	if (!cmdp)
		return(1);
	argc--; argv++;
	if (cmdp->func == NULL) {
		strcat(prefix, cmdp->name);
		strcat(prefix, " ");
		return(do_cmd((const struct cmd *)cmdp->help, argc, argv));
	}
	if ((argc < cmdp->minp) || (argc > cmdp->maxp)) {
		fprintf(stderr, "%s: Invalid number of arguments\n",
			prog);
		fprintf(stderr, "\tformat is: %s%s %s\n",
			prefix, cmdp->name, cmdp->help);
		exit(1);
	}
	(*cmdp->func)(argc, argv, cmdp);
	return(0);
}
static int do_cmd ( const struct cmd * descp , int argc , char * * argv ) { const struct cmd * cmdp = NULL ; if ( ! argc ) { usage ( cmds , " " ) ; exit ( 1 ) ; } for ( ; descp -> name ; descp ++ ) { if ( ! strcasecmp ( descp -> name , argv [ 0 ] ) ) { cmdp = descp ; break ; } if ( ! strncasecmp ( descp -> name , argv [ 0 ] , strlen ( argv [ 0 ] ) ) ) { if ( cmdp ) { fprintf ( stderr , " \" \" \n " , prog , argv [ 0 ] ) ; exit ( 1 ) ; } cmdp = descp ; } } if ( ! cmdp ) return ( 1 ) ; argc -- ; argv ++ ; if ( cmdp -> func == NULL ) { strcat ( prefix , cmdp -> name ) ; strcat ( prefix , " " ) ; return ( do_cmd ( ( const struct cmd * ) cmdp -> help , argc , argv ) ) ; } if ( ( argc < cmdp -> minp ) || ( argc > cmdp -> maxp ) ) { fprintf ( stderr , " \n " , prog ) ; fprintf ( stderr , " \t \n " , prefix , cmdp -> name , cmdp -> help ) ; exit ( 1 ) ; } ( * cmdp -> func ) ( argc , argv , cmdp ) ; return ( 0 ) ; }
Validate and process user command
Validate and process user command
/*
	 * Make sure we have paramaters to process
	 */ /*
	 * Figure out what command user wants
	 */ /*
		 * Use an exact match if there is one
		 */ /*
		 * Look for a match on the first part of keyword
		 */ /*
	 * See if this command has subcommands
	 */ /*
	 * Minimal validation
	 */ /*
	 * Process command
	 */