-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_arg.c
152 lines (130 loc) · 3.89 KB
/
parse_arg.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
/*
** parse_arg.c for parse_arg in /home/leroy_v/Perso/parse_arg
**
** Made by vincent leroy
** Login <leroy_v@epitech.eu>
**
** Started on Mon Aug 18 14:24:44 2014 vincent leroy
** Last update Thu Apr 30 15:20:13 2015 vincent leroy
*/
#include <stdlib.h>
#include <string.h>
#include "parse_arg.h"
#include "parse_short_opt.h"
#include "parse_long_opt.h"
static void _remove_option_in_av(int *ac, char ***av, int *idx_in_av_for_remove, int size_of_tab)
{
int i;
int j;
for (i = 0; i < size_of_tab; ++i)
{
for (j = idx_in_av_for_remove[i]; j < *ac; ++j)
(*av)[j] = (*av)[j + 1];
--(*ac);
for (j = i + 1; j < size_of_tab; ++j)
--idx_in_av_for_remove[j];
}
}
static int check_opts_arg(const opts options[], opt_error *error)
{
char array[256] = {};
int err = 0;
int err_idx = -1;
int i;
int j;
for (i = 0; options[i].cb != NULL && err == 0; ++i)
{
if (options[i].short_opt != '\0')
{
if (array[(unsigned char)options[i].short_opt] == 1)
{
err = 1;
err_idx = i;
break;
}
array[(unsigned char)options[i].short_opt] = 1;
}
if (options[i].long_opt != NULL)
for (j = i + 1; options[j].cb != NULL; ++j)
{
if (options[j].long_opt == NULL)
continue;
if (strcmp(options[i].long_opt, options[j].long_opt) == 0)
{
err = 2;
err_idx = j;
break;
}
}
}
if (err != 0)
{
if (error != NULL)
{
error->err_type = INVALID_ARG;
error->is_short_arg = ((err == 1) ? 1 : 0);
error->idx = err_idx;
}
return -1;
}
return 0;
}
int _do_parse(char **argv, int i, int off, const opts options[], opt_error *error, int *idx_in_av_for_remove, int *idx_in_tab, int (*parse_func)(const char*, const char*[], const opts[], opt_error*))
{
int nb_arg_parsed;
int j;
nb_arg_parsed = (*parse_func)(argv[i] + off, (const char**)argv + i + 1, options, error);
if (nb_arg_parsed == -1)
{
free(idx_in_av_for_remove);
return set_parse_error(error, i, error ? error->idx + off : -1, DONT_CHANGE_ERROR_TYPE, -1);
}
for (j = 0; j < nb_arg_parsed; ++j)
{
idx_in_av_for_remove[*idx_in_tab] = i + j;
++(*idx_in_tab);
}
return nb_arg_parsed;
}
int parse_arg(int *ac, char ***av, const opts options[], opt_error *error)
{
int nb_arg_parsed = 0;
int i;
int *idx_in_av_for_remove;
int idx_in_tab = 0;
char **argv = *av;
if (check_opts_arg(options, error) == -1)
return -1;
idx_in_av_for_remove = calloc(*ac, sizeof(int));
clear_parse_error(error);
for (i = 1; i < *ac; i += nb_arg_parsed)
{
if (argv[i][0] == '-' && argv[i][1] != '-' && argv[i][1] != '\0')
{
set_parse_error(error, -1, -1, DONT_CHANGE_ERROR_TYPE, 1);
nb_arg_parsed = _do_parse(argv, i, 1, options, error, idx_in_av_for_remove, &idx_in_tab, &parse_short_opt);
if (nb_arg_parsed == -1)
return -1;
}
else if (argv[i][0] == '-' && argv[i][1] == '-')
{
if (argv[i][2] == '\0')
{
nb_arg_parsed = 2;
continue;
}
set_parse_error(error, -1, -1, DONT_CHANGE_ERROR_TYPE, 0);
nb_arg_parsed = _do_parse(argv, i, 2, options, error, idx_in_av_for_remove, &idx_in_tab, &parse_long_opt);
if (nb_arg_parsed == -1)
return -1;
}
else
{
nb_arg_parsed = 1;
continue;
}
}
_remove_option_in_av(ac, av, idx_in_av_for_remove, idx_in_tab);
free(idx_in_av_for_remove);
return 0;
}