-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptlist.c
290 lines (255 loc) · 10.6 KB
/
optlist.c
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/***************************************************************************
* Command Line Option Parser
*
* File : optlist.c
* Purpose : Provide getopt style command line option parsing
* Author : Michael Dipperstein
* Date : August 1, 2007
*
****************************************************************************
*
* OptList: A command line option parsing library
* Copyright (C) 2007, 2014 by
* Michael Dipperstein (mdipper@alumni.engr.ucsb.edu)
*
* This file is part of the OptList library.
*
* OptList is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* OptList 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 Lesser
* General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
***************************************************************************/
/***************************************************************************
* INCLUDED FILES
***************************************************************************/
#include "optlist.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/***************************************************************************
* TYPE DEFINITIONS
***************************************************************************/
/***************************************************************************
* CONSTANTS
***************************************************************************/
/***************************************************************************
* GLOBAL VARIABLES
***************************************************************************/
/***************************************************************************
* PROTOTYPES
***************************************************************************/
static option_t *MakeOpt(
const char option, char *const argument, const int index);
static size_t MatchOpt(
const char argument, char *const options);
/***************************************************************************
* FUNCTIONS
***************************************************************************/
/****************************************************************************
* Function : GetOptList
* Description: This function is similar to the POSIX function getopt. All
* options and their corresponding arguments are returned in a
* linked list. This function should only be called once per
* an option list and it does not modify argv or argc.
* Parameters : argc - the number of command line arguments (including the
* name of the executable)
* argv - pointer to the open binary file to write encoded
* output
* options - getopt style option list. A NULL terminated
* string of single character options. Follow an
* option with a colon to indicate that it requires
* an argument.
* Effects : Creates a link list of command line options and their
* arguments.
* Returned : option_t type value where the option and arguement fields
* contain the next option symbol and its argument (if any).
* The argument field will be set to NULL if the option is
* specified as having no arguments or no arguments are found.
* The option field will be set to PO_NO_OPT if no more
* options are found.
*
* NOTE: The caller is responsible for freeing up the option list when it
* is no longer needed.
****************************************************************************/
option_t *GetOptList(const int argc, char *const argv[], char *const options)
{
int nextArg;
option_t *head, *tail;
size_t optIndex;
size_t argIndex;
/* start with first argument and nothing found */
nextArg = 1;
head = NULL;
tail = NULL;
/* loop through all of the command line arguments */
while (nextArg < argc)
{
argIndex = 1;
while ((strlen(argv[nextArg]) > argIndex) && ('-' == argv[nextArg][0]))
{
/* attempt to find a matching option */
optIndex = MatchOpt(argv[nextArg][argIndex], options);
if (options[optIndex] == argv[nextArg][argIndex])
{
/* we found the matching option */
if (NULL == head)
{
head = MakeOpt(options[optIndex], NULL, OL_NOINDEX);
tail = head;
}
else
{
tail->next = MakeOpt(options[optIndex], NULL, OL_NOINDEX);
tail = tail->next;
}
if (':' == options[optIndex + 1])
{
/* the option found should have a text arguement */
argIndex++;
if (strlen(argv[nextArg]) > argIndex)
{
/* no space between argument and option */
tail->argument = &(argv[nextArg][argIndex]);
tail->argIndex = nextArg;
}
else if (nextArg < argc)
{
/* there must be space between the argument option */
nextArg++;
tail->argument = argv[nextArg];
tail->argIndex = nextArg;
}
break; /* done with argv[nextArg] */
}
}
argIndex++;
}
nextArg++;
}
return head;
}
/****************************************************************************
* Function : MakeOpt
* Description: This function uses malloc to allocate space for an option_t
* type structure and initailizes the structure with the
* values passed as a parameter.
* Parameters : option - this option character
* argument - pointer string containg the argument for option.
* Use NULL for no argument
* index - argv[index] contains argument use OL_NOINDEX for
* no argument
* Effects : A new option_t type variable is created on the heap.
* Returned : Pointer to newly created and initialized option_t type
* structure. NULL if space for structure can't be allocated.
****************************************************************************/
static option_t *MakeOpt(
const char option, char *const argument, const int index)
{
option_t *opt;
opt = malloc(sizeof(option_t));
if (opt != NULL)
{
opt->option = option;
opt->argument = argument;
opt->argIndex = index;
opt->next = NULL;
}
else
{
perror("Failed to Allocate option_t");
}
return opt;
}
/****************************************************************************
* Function : FreeOptList
* Description: This function will free all the elements in an option_t
* type linked list starting from the node passed as a
* parameter.
* Parameters : list - head of linked list to be freed
* Effects : All elements of the linked list pointed to by list will
* be freed and list will be set to NULL.
* Returned : None
****************************************************************************/
void FreeOptList(option_t *list)
{
option_t *head, *next;
head = list;
list = NULL;
while (head != NULL)
{
next = head->next;
free(head);
head = next;
}
return;
}
/****************************************************************************
* Function : MatchOpt
* Description: This function searches for an arguement in an option list.
* It will return the index to the option matching the
* arguement or the index to the NULL if none is found.
* Parameters : arguement - character arguement to be matched to an
* option in the option list
* options - getopt style option list. A NULL terminated
* string of single character options. Follow an
* option with a colon to indicate that it requires
* an argument.
* Effects : None
* Returned : Index of argument in option list. Index of end of string
* if arguement does not appear in the option list.
****************************************************************************/
static size_t MatchOpt(
const char argument, char *const options)
{
size_t optIndex = 0;
/* attempt to find a matching option */
while ((options[optIndex] != '\0') &&
(options[optIndex] != argument))
{
do
{
optIndex++;
}
while ((options[optIndex] != '\0') &&
(':' == options[optIndex]));
}
return optIndex;
}
/****************************************************************************
* Function : FindFileName
* Description: This is function accepts a pointer to the name of a file
* along with path information and returns a pointer to the
* first character that is not part of the path.
* Parameters : fullPath - pointer to an array of characters containing
* a file name and possible path modifiers.
* Effects : None
* Returned : Returns a pointer to the first character after any path
* information.
****************************************************************************/
char *FindFileName(const char *const fullPath)
{
int i;
const char *start; /* start of file name */
const char *tmp;
const char delim[3] = {'\\', '/', ':'}; /* path deliminators */
start = fullPath;
/* find the first character after all file path delimiters */
for (i = 0; i < 3; i++)
{
tmp = strrchr(start, delim[i]);
if (tmp != NULL)
{
start = tmp + 1;
}
}
return (char *)start;
}