/***********************************************************************
 *  args - Argument reading system
 *  Copyright (C) 1997-1998 Jon Anders Haugum
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; see the file COPYING.  If not, write to
 *  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 *  Boston, MA 02111-1307, USA.
 *
 *
 *  Author of args can be reached at:
 *     email: jonah@colargol.tihlde.hist.no
 *     www: http://www.colargol.tihlde.hist.no/~jonah/el/avrp.html
 *     Postal address: Jon Anders Haugum
 *                     Øvre Møllenbergsgt 52
 *                     7014 Trondheim
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "misc.h"
#include "args.h"


struct args *alloc_args(int arg_count)
	{
	struct args *args;

	args = malloc(sizeof(struct args));
	if(args)
		{
		args->arg = malloc(sizeof(struct arg) * arg_count);
		if(args->arg)
			{
			args->count = arg_count;
			args->first_data = NULL;
			return(args);
			}
		free(args);
		}
	printf("Error: Unable to allocate memory\n");
	return(NULL);
	}



int read_args(struct args *args, int argc, char *argv[])
	{
	int i, j, k, ok = True, i_old;
	struct data_list **last_data, *data;

	args->first_data = NULL;
	last_data = &args->first_data;
	for(i = 1; (i < argc) && ok; i++)
		{
		if(argv[i][0] == '-')
			{
			if(argv[i][1] == '-')
				{
				j = 0;
				while((args->arg[j].longarg ? strcmp(&argv[i][2], args->arg[j].longarg) : True) && (j != args->count))
					j++;
				if(j == args->count)
					{
					printf("Error: Unknown option: %s\n", argv[i]);
					ok = False;
					}
				else
					{
					switch(args->arg[j].type)
						{
						case ARGTYPE_STRING:
							if((i + 1) == argc)
								{
								printf("Error: No argument supplied with option: %s\n", argv[i]);
								ok = False;
								}
							else
								args->arg[j].data = argv[++i];
							break;
						case ARGTYPE_BOOLEAN:
							args->arg[j].data = (char *)True;
							break;
						case ARGTYPE_STRING_MULTI:
							last_data = (struct data_list **)&args->arg[j].data;
							break;
						}
					}
				}
			else
				{
				last_data = &args->first_data;
				// ToDo: Sjekk for enkel -
				for(k = 1, i_old = i; (argv[i][k] != '\0') && ok && (i == i_old); k++)
					{
					j = 0;
					while((argv[i][k] != args->arg[j].letter) && (j != args->count))
						j++;
					if(j == args->count)
						{
						printf("Error: Unknown option: -%c\n", argv[i][k]);
						ok = False;
						}
					else
						{
						switch(args->arg[j].type)
							{
							case ARGTYPE_STRING:
								if(argv[i][k + 1] != '\0')
									{
									printf("Error: Option -%c must be followed by it's argument\n", argv[i][k]);
									ok = False;
									}
								else
									{
									if((i + 1) == argc)
										{
										printf("Error: No argument supplied with option: -%c\n", argv[i][k]);
										ok = False;
										}
									else
										args->arg[j].data = argv[++i];
									}
								break;
							case ARGTYPE_BOOLEAN:
								args->arg[j].data = (char *)True;
								break;
							case ARGTYPE_STRING_MULTI:
								last_data = (struct data_list **)&args->arg[j].data;
								break;
							}
						}
					}
				}
			}
		else
			{
			while(*last_data) last_data = &((*last_data)->next);
			data = malloc(sizeof(struct data_list));
			if(data)
				{
				data->next = NULL;
				data->data = argv[i];
				*last_data = data;
				last_data = &data->next;
				}
			else
				{
				printf("Error: Unable to allocate memory\n");
				ok = False;
				}
			}
		}
	return(ok);
	}


void free_args(struct args *args)
	{
	int i;
	struct data_list *data, *temp;

	for(data = args->first_data; data;)
		{
		temp = data;
		data = data->next;
		free(temp);
		}
	for(i = 0; i != args->count; i++)
		if(args->arg[i].type == ARGTYPE_STRING_MULTI)
			for(data = args->arg[i].data; data;)
				{
				temp = data;
				data = data->next;
				free(temp);
				}
	free(args);
	}


void define_arg(struct args *args, int index, int type, char letter, char *longarg, void *def_value)
	{
	args->arg[index].type = type;
	args->arg[index].letter = letter;
	args->arg[index].longarg = longarg;
	args->arg[index].data = def_value;
	}
