/* show_param.c */

#include <stdio.h>
#include <stdlib.h>	/* needed for atoi() */
#include "getopt.h"

char *groups[] = { "client", NULL };

struct option long_options[] =
{
	{"host",     required_argument, NULL, 'h'},
	{"user",     required_argument, NULL, 'u'},
	{"password", optional_argument, NULL, 'p'},
	{"port",	 required_argument, NULL, 'P'},
	{"socket",	 required_argument, NULL, 'S'},
	{ 0, 0, 0, 0 }
};

int
main (int argc, char *argv[])
{
char *host_name = NULL;
char *user_name = NULL;
char *password = NULL;
unsigned int port_num = 0;
char *socket_name = NULL;
int	i;
int	c, option_index;

	my_init ();

	printf ("Original connection parameters:\n");
	printf ("host name: %s\n", host_name ? host_name : "(null)");
	printf ("user name: %s\n", user_name ? user_name : "(null)");
	printf ("password: %s\n", password ? password : "(null)");
	printf ("port number: %u\n", port_num);
	printf ("socket name: %s\n", socket_name ? socket_name : "(null)");

	printf ("Original argument vector:\n");
	for (i = 0; i < argc; i++)
		printf ("arg %d: %s\n", i, argv[i]);

	load_defaults ("my", groups, &argc, &argv);

	printf ("Modified argument vector after load_defaults():\n");
	for (i = 0; i < argc; i++)
		printf ("arg %d: %s\n", i, argv[i]);

	while ((c = getopt_long (argc, argv, "h:p::u:P:S:", long_options,
					&option_index)) != EOF)
	{
		switch (c)
		{
		case 'h':
			host_name = optarg;
			break;
		case 'u':
			user_name = optarg;
			break;
		case 'p':
			password = optarg;
			break;
		case 'P':
			port_num = (unsigned int) atoi (optarg);
			break;
		case 'S':
			socket_name = optarg;
			break;
		}
	}

	argc -= optind;	/* advance past the arguments that were processed */
	argv += optind;	/* by getopt_long() */

	printf ("Connection parameters after getopt_long():\n");
	printf ("host name: %s\n", host_name ? host_name : "(null)");
	printf ("user name: %s\n", user_name ? user_name : "(null)");
	printf ("password: %s\n", password ? password : "(null)");
	printf ("port number: %u\n", port_num);
	printf ("socket name: %s\n", socket_name ? socket_name : "(null)");

	printf ("Argument vector after getopt_long():\n");
	for (i = 0; i < argc; i++)
		printf ("arg %d: %s\n", i, argv[i]);

	exit (0);
}
