example/ipsec_crypto/odp_ipsec.c
static void parse_args(int argc, char *argv[], appl_args_t *appl_args)
{
	int opt;
	int long_index;
	char *token;
	size_t len;
	int rc = 0;
	int i;

	static const struct option longopts[] = {
		{"count", required_argument, NULL, 'c'},
		{"interface", required_argument, NULL, 'i'},	/* return 'i' */
		{"mode", required_argument, NULL, 'm'},		/* return 'm' */
		{"route", required_argument, NULL, 'r'},	/* return 'r' */
		{"policy", required_argument, NULL, 'p'},	/* return 'p' */
		{"ah", required_argument, NULL, 'a'},		/* return 'a' */
		{"esp", required_argument, NULL, 'e'},		/* return 'e' */
		{"tunnel", required_argument, NULL, 't'},       /* return 't' */
		{"stream", required_argument, NULL, 's'},	/* return 's' */
		{"help", no_argument, NULL, 'h'},		/* return 'h' */
		{NULL, 0, NULL, 0}
	};

	static const char *shortopts = "+c:i:m:r:p:a:e:t:s:h";

	printf("\nParsing command line options\n");

	appl_args->cpu_count = 1; /* use one worker by default */
	appl_args->mode = 0;  /* turn off async crypto API by default */

	while (!rc) {
		opt = getopt_long(argc, argv, shortopts, longopts, &long_index);

		if (-1 == opt)
			break;	/* No more options */

		switch (opt) {
		case 'c':
			appl_args->cpu_count = atoi(optarg);
			break;
			/* parse packet-io interface names */
		case 'i':
			len = strlen(optarg);
			if (0 == len) {
				usage(argv[0]);
				exit(EXIT_FAILURE);
			}
			len += 1;	/* add room for '\0' */

			appl_args->if_str = malloc(len);
			if (appl_args->if_str == NULL) {
				usage(argv[0]);
				exit(EXIT_FAILURE);
			}

			/* count the number of tokens separated by ',' */
			strcpy(appl_args->if_str, optarg);
			for (token = strtok(appl_args->if_str, ","), i = 0;
			     token != NULL;
			     token = strtok(NULL, ","), i++)
				;

			appl_args->if_count = i;

			if (0 == appl_args->if_count) {
				usage(argv[0]);
				exit(EXIT_FAILURE);
			}

			/* allocate storage for the if names */
			appl_args->if_names =
				calloc(appl_args->if_count, sizeof(char *));

			/* store the if names (reset names string) */
			strcpy(appl_args->if_str, optarg);
			for (token = strtok(appl_args->if_str, ","), i = 0;
			     token != NULL; token = strtok(NULL, ","), i++) {
				appl_args->if_names[i] = token;
			}
			break;

		case 'm':
			appl_args->mode = atoi(optarg);
			break;

		case 'r':
			rc = create_fwd_db_entry(optarg, appl_args->if_names,
						 appl_args->if_count);
			break;

		case 'p':
			rc = create_sp_db_entry(optarg, TRUE);
			break;

		case 'a':
			rc = create_sa_db_entry(optarg, FALSE);
			break;

		case 'e':
			rc = create_sa_db_entry(optarg, TRUE);
			break;

		case 't':
			rc = create_tun_db_entry(optarg);
			break;

		case 's':
			rc = create_stream_db_entry(optarg);
			break;

		case 'h':
			usage(argv[0]);
			exit(EXIT_SUCCESS);
			break;

		default:
			break;
		}
	}

	if (rc) {
		printf("ERROR: failed parsing -%c option\n", opt);
		usage(argv[0]);
		exit(EXIT_FAILURE);
	}

	if (0 == appl_args->if_count) {
		usage(argv[0]);
		exit(EXIT_FAILURE);
	}

	optind = 1;		/* reset 'extern optind' from the getopt lib */
}
/**
 * Parse and store the command line arguments
 *
 * @param argc       argument count
 * @param argv[]     argument vector
 * @param appl_args  Store application arguments here
 */
Parse and store the command line arguments
@param argc       argument count
@param argv[]     argument vector
@param appl_args  Store application arguments here
Parse and store the command line arguments @param argc argument count @param argv [] argument vector @param appl_args Store application arguments here
static void parse_args(int argc, char *argv[], appl_args_t *appl_args)
{
	int opt;
	int long_index;
	char *token;
	size_t len;
	int rc = 0;
	int i;
	static const struct option longopts[] = {
		{"count", required_argument, NULL, 'c'},
		{"interface", required_argument, NULL, 'i'},	
		{"mode", required_argument, NULL, 'm'},		
		{"route", required_argument, NULL, 'r'},	
		{"policy", required_argument, NULL, 'p'},	
		{"ah", required_argument, NULL, 'a'},		
		{"esp", required_argument, NULL, 'e'},		
		{"tunnel", required_argument, NULL, 't'},       
		{"stream", required_argument, NULL, 's'},	
		{"help", no_argument, NULL, 'h'},		
		{NULL, 0, NULL, 0}
	};
	static const char *shortopts = "+c:i:m:r:p:a:e:t:s:h";
	printf("\nParsing command line options\n");
	appl_args->cpu_count = 1; 
	appl_args->mode = 0;  
	while (!rc) {
		opt = getopt_long(argc, argv, shortopts, longopts, &long_index);
		if (-1 == opt)
			break;	
		switch (opt) {
		case 'c':
			appl_args->cpu_count = atoi(optarg);
			break;
		case 'i':
			len = strlen(optarg);
			if (0 == len) {
				usage(argv[0]);
				exit(EXIT_FAILURE);
			}
			len += 1;	
			appl_args->if_str = malloc(len);
			if (appl_args->if_str == NULL) {
				usage(argv[0]);
				exit(EXIT_FAILURE);
			}
			strcpy(appl_args->if_str, optarg);
			for (token = strtok(appl_args->if_str, ","), i = 0;
			     token != NULL;
			     token = strtok(NULL, ","), i++)
				;
			appl_args->if_count = i;
			if (0 == appl_args->if_count) {
				usage(argv[0]);
				exit(EXIT_FAILURE);
			}
			appl_args->if_names =
				calloc(appl_args->if_count, sizeof(char *));
			strcpy(appl_args->if_str, optarg);
			for (token = strtok(appl_args->if_str, ","), i = 0;
			     token != NULL; token = strtok(NULL, ","), i++) {
				appl_args->if_names[i] = token;
			}
			break;
		case 'm':
			appl_args->mode = atoi(optarg);
			break;
		case 'r':
			rc = create_fwd_db_entry(optarg, appl_args->if_names,
						 appl_args->if_count);
			break;
		case 'p':
			rc = create_sp_db_entry(optarg, TRUE);
			break;
		case 'a':
			rc = create_sa_db_entry(optarg, FALSE);
			break;
		case 'e':
			rc = create_sa_db_entry(optarg, TRUE);
			break;
		case 't':
			rc = create_tun_db_entry(optarg);
			break;
		case 's':
			rc = create_stream_db_entry(optarg);
			break;
		case 'h':
			usage(argv[0]);
			exit(EXIT_SUCCESS);
			break;
		default:
			break;
		}
	}
	if (rc) {
		printf("ERROR: failed parsing -%c option\n", opt);
		usage(argv[0]);
		exit(EXIT_FAILURE);
	}
	if (0 == appl_args->if_count) {
		usage(argv[0]);
		exit(EXIT_FAILURE);
	}
	optind = 1;		
}
static void parse_args ( int argc , char * argv [ ] , appl_args_t * appl_args ) { int opt ; int long_index ; char * token ; size_t len ; int rc = 0 ; int i ; static const struct option longopts [ ] = { { " " , required_argument , NULL , ' ' } , { " " , required_argument , NULL , ' ' } , { " " , required_argument , NULL , ' ' } , { " " , required_argument , NULL , ' ' } , { " " , required_argument , NULL , ' ' } , { " " , required_argument , NULL , ' ' } , { " " , required_argument , NULL , ' ' } , { " " , required_argument , NULL , ' ' } , { " " , required_argument , NULL , ' ' } , { " " , no_argument , NULL , ' ' } , { NULL , 0 , NULL , 0 } } ; static const char * shortopts = " " ; printf ( " \n \n " ) ; appl_args -> cpu_count = 1 ; appl_args -> mode = 0 ; while ( ! rc ) { opt = getopt_long ( argc , argv , shortopts , longopts , & long_index ) ; if ( -1 == opt ) break ; switch ( opt ) { case ' ' : appl_args -> cpu_count = atoi ( optarg ) ; break ; case ' ' : len = strlen ( optarg ) ; if ( 0 == len ) { usage ( argv [ 0 ] ) ; exit ( EXIT_FAILURE ) ; } len += 1 ; appl_args -> if_str = malloc ( len ) ; if ( appl_args -> if_str == NULL ) { usage ( argv [ 0 ] ) ; exit ( EXIT_FAILURE ) ; } strcpy ( appl_args -> if_str , optarg ) ; for ( token = strtok ( appl_args -> if_str , " " ) , i = 0 ; token != NULL ; token = strtok ( NULL , " " ) , i ++ ) ; appl_args -> if_count = i ; if ( 0 == appl_args -> if_count ) { usage ( argv [ 0 ] ) ; exit ( EXIT_FAILURE ) ; } appl_args -> if_names = calloc ( appl_args -> if_count , sizeof ( char * ) ) ; strcpy ( appl_args -> if_str , optarg ) ; for ( token = strtok ( appl_args -> if_str , " " ) , i = 0 ; token != NULL ; token = strtok ( NULL , " " ) , i ++ ) { appl_args -> if_names [ i ] = token ; } break ; case ' ' : appl_args -> mode = atoi ( optarg ) ; break ; case ' ' : rc = create_fwd_db_entry ( optarg , appl_args -> if_names , appl_args -> if_count ) ; break ; case ' ' : rc = create_sp_db_entry ( optarg , TRUE ) ; break ; case ' ' : rc = create_sa_db_entry ( optarg , FALSE ) ; break ; case ' ' : rc = create_sa_db_entry ( optarg , TRUE ) ; break ; case ' ' : rc = create_tun_db_entry ( optarg ) ; break ; case ' ' : rc = create_stream_db_entry ( optarg ) ; break ; case ' ' : usage ( argv [ 0 ] ) ; exit ( EXIT_SUCCESS ) ; break ; default : break ; } } if ( rc ) { printf ( " \n " , opt ) ; usage ( argv [ 0 ] ) ; exit ( EXIT_FAILURE ) ; } if ( 0 == appl_args -> if_count ) { usage ( argv [ 0 ] ) ; exit ( EXIT_FAILURE ) ; } optind = 1 ; }
Parse and store the command line arguments
 @param argc       argument count
 @param argv[]     argument vector
 @param appl_args  Store application arguments here
Parse and store the command line arguments @param argc argument count @param argv [] argument vector @param appl_args Store application arguments here
/* return 'i' */ /* return 'm' */ /* return 'r' */ /* return 'p' */ /* return 'a' */ /* return 'e' */ /* return 't' */ /* return 's' */ /* return 'h' */ /* use one worker by default */ /* turn off async crypto API by default */ /* No more options */ /* parse packet-io interface names */ /* add room for '\0' */ /* count the number of tokens separated by ',' */ /* allocate storage for the if names */ /* store the if names (reset names string) */ /* reset 'extern optind' from the getopt lib */