File: args.c

package info (click to toggle)
avrp 1.0beta3-6
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 204 kB
  • ctags: 194
  • sloc: ansic: 2,463; makefile: 40; perl: 6; sh: 5
file content (199 lines) | stat: -rw-r--r-- 4,761 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/***********************************************************************
 *  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;
	}