Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2b3d25117fe33634d08b412f13f09f337a517be1
[simgrid.git] / tools / tesh2 / w32 / src / getopt.c
1 #include <stdio.h>\r
2 #include <string.h>\r
3 \r
4 #include <getopt.h>\r
5 \r
6 char* \r
7 optarg = NULL;\r
8 \r
9 int \r
10 optind = 0;\r
11 \r
12 int \r
13 optopt = '?';\r
14 \r
15 int \r
16 opterr = 1;\r
17 \r
18 static char*\r
19 nextchar;\r
20 \r
21 static \r
22 int first_nonopt;\r
23 \r
24 static int \r
25 last_nonopt;\r
26 \r
27 \r
28 static enum\r
29 {\r
30         REQUIRE_ORDER, \r
31         PERMUTE, \r
32         RETURN_IN_ORDER\r
33 }ordering;\r
34 \r
35 \r
36 static const char *\r
37 __getopt_initialize (const char *optstring);\r
38 \r
39 static int\r
40 __getopt_internal (int argc, char *const *argv, const char* optstring, const struct option *longopts, int* longind, int long_only);\r
41 \r
42 static void\r
43 __exchange (char **argv);\r
44 \r
45 int \r
46 getopt (int argc, char * const argv[], const char *optstring)\r
47 {\r
48         return __getopt_internal(argc, argv, optstring,(const struct option *) 0,(int *) 0,0);\r
49 }\r
50 \r
51 static const char *\r
52 __getopt_initialize (const char *optstring)\r
53 {\r
54         first_nonopt = last_nonopt = optind = 1;\r
55         nextchar = NULL;\r
56         \r
57         if (optstring[0] == '-')\r
58         {\r
59                 ordering = RETURN_IN_ORDER;\r
60                 ++optstring;\r
61         }\r
62         \r
63         /* if the optstring begining with the character +, the getopt() function\r
64          * stop when an argument of the command line is not an option.\r
65          */\r
66         else if (optstring[0] == '+')\r
67         {\r
68                 ordering = REQUIRE_ORDER;\r
69                 ++optstring;\r
70         }\r
71         else\r
72         {\r
73                 ordering = PERMUTE;\r
74         }\r
75         \r
76         return optstring;\r
77 }\r
78 \r
79 int\r
80 __getopt_internal (int argc, char *const *argv, const char* optstring, const struct option *longopts, int* longind, int long_only)\r
81 {\r
82         optarg = NULL;\r
83         \r
84         if (optind == 0)\r
85                 optstring = __getopt_initialize (optstring);\r
86         \r
87         if (nextchar == NULL || *nextchar == '\0')\r
88         {\r
89                 if (ordering == PERMUTE)\r
90                 {\r
91                         if (first_nonopt != last_nonopt && last_nonopt != optind)\r
92                                 __exchange ((char **) argv);\r
93                         else if (last_nonopt != optind)\r
94                                 first_nonopt = optind;\r
95         \r
96                         \r
97         \r
98                         while (optind < argc && (argv[optind][0] != '-' || argv[optind][1] == '\0'))\r
99                                 optind++;\r
100                         \r
101                         last_nonopt = optind;\r
102                 }\r
103 \r
104         \r
105                 if (optind != argc && !strcmp (argv[optind], "--"))\r
106                 {\r
107                         optind++;\r
108         \r
109                         if (first_nonopt != last_nonopt && last_nonopt != optind)\r
110                                 __exchange ((char **) argv);\r
111                         else if (first_nonopt == last_nonopt)\r
112                                 first_nonopt = optind;\r
113                         \r
114                         last_nonopt = argc;\r
115         \r
116                         optind = argc;\r
117                 }\r
118         \r
119                 if (optind == argc)\r
120                 {\r
121                         if (first_nonopt != last_nonopt)\r
122                                 optind = first_nonopt;\r
123                         \r
124                         return EOF;\r
125                 }\r
126         \r
127                 if ((argv[optind][0] != '-' || argv[optind][1] == '\0'))\r
128                 {\r
129                         if (ordering == REQUIRE_ORDER)\r
130                                 return EOF;\r
131                         optarg = argv[optind++];\r
132                                 return 1;\r
133                 }\r
134         \r
135                 nextchar = (argv[optind] + 1 + (longopts != NULL && argv[optind][1] == '-'));\r
136         }\r
137         \r
138         if (longopts != NULL && (argv[optind][1] == '-' || (long_only && (argv[optind][2] || !strchr (optstring, argv[optind][1])))))\r
139         {\r
140                 char *nameend;\r
141                 const struct option *p;\r
142                 const struct option *pfound = NULL;\r
143                 int exact = 0;\r
144                 int ambig = 0;\r
145                 int indfound = 0;\r
146                 int option_index;\r
147         \r
148                 for (nameend = nextchar; *nameend !='\0' && *nameend != '='; nameend++)\r
149                         \r
150                 for (p = longopts, option_index = 0; p->name; p++, option_index++)\r
151                 {\r
152                         if(!strncmp (p->name, nextchar, nameend - nextchar))\r
153                         {\r
154 \r
155                                 if ((nameend - nextchar) == strlen (p->name))\r
156                                 {\r
157                                         pfound = p;\r
158                                         indfound = option_index;\r
159                                         exact = 1;\r
160                                         break;\r
161                                 }\r
162                                 else if (pfound == NULL)\r
163                                 {\r
164                                         exact = 0;\r
165                                         break;\r
166                                 }\r
167                                 else\r
168                                         ambig = 1;\r
169                                 \r
170                         }\r
171                 }\r
172         \r
173                 if (ambig && !exact)\r
174                 {\r
175                         if (opterr)\r
176                                 fprintf (stderr, "ERROR   : %s: option `%s' is ambiguous\n",argv[0], argv[optind]);\r
177                         \r
178                         nextchar += strlen (nextchar);\r
179                         optind++;\r
180                         return '?';\r
181                 }\r
182         \r
183                 if (pfound != NULL)\r
184                 {\r
185                         option_index = indfound;\r
186                         optind++;\r
187                         \r
188                         if (*nameend)\r
189                         {\r
190                                 if (pfound->has_arg)\r
191                                         optarg = nameend + 1;\r
192                                 else\r
193                                 {\r
194                                         if (opterr)\r
195                                         {\r
196                                                 if (argv[optind - 1][1] == '-')\r
197                                                         /* --option */\r
198                                                         fprintf (stderr,"error   : %s: option `--%s' doesn't allow an argument\n",argv[0], pfound->name);\r
199                                                 else\r
200                                                         /* +option or -option */\r
201                                                         fprintf (stderr,"error   : %s: option `%c%s' doesn't allow an argument\n",argv[0], argv[optind - 1][0], pfound->name);\r
202                                         }\r
203                                         \r
204                                         nextchar += strlen (nextchar);\r
205                                         return '?';\r
206                                 }\r
207                         }\r
208                         else if (pfound->has_arg == 1)\r
209                         {\r
210                                 if (optind < argc)\r
211                                         optarg = argv[optind++];\r
212                                 else\r
213                                 {\r
214                                         if (opterr)\r
215                                                 fprintf (stderr, "error   : %s: option `%s' requires an argument\n",argv[0], argv[optind - 1]);\r
216                                         \r
217                                         nextchar += strlen (nextchar);\r
218                                         return optstring[0] == ':' ? ':' : '?';\r
219                                 }\r
220                         }\r
221                         \r
222                         nextchar += strlen (nextchar);\r
223                         \r
224                         if (longind != NULL)\r
225                                 *longind = option_index;\r
226                         \r
227                         if (pfound->flag)\r
228                         {\r
229                                 *(pfound->flag) = pfound->val;\r
230                                 return 0;\r
231                         }\r
232                         \r
233                         return pfound->val;\r
234                 }\r
235         \r
236                 if (!long_only || argv[optind][1] == '-'|| strchr (optstring, *nextchar) == NULL)\r
237                 {\r
238                         if (opterr)\r
239                         {\r
240                                 if (argv[optind][1] == '-')\r
241                                         /* --option */\r
242                                         fprintf (stderr, "error   : %s: unrecognized option `--%s'\n",argv[0], nextchar);\r
243                                 else\r
244                                         /* +option or -option */\r
245                                         fprintf (stderr, "error   : %s: unrecognized option `%c%s'\n",argv[0], argv[optind][0], nextchar);\r
246                         }\r
247                         \r
248                         nextchar = (char *) "";\r
249                         optind++;\r
250                         return '?';\r
251                 }\r
252         }\r
253         \r
254         {\r
255                 char c = *nextchar++;\r
256                 char *temp = strchr (optstring, c);\r
257         \r
258                 /* Increment `optind' when we start to process its last character.  */\r
259                 if (*nextchar == '\0')\r
260                         ++optind;\r
261         \r
262                 if (temp == NULL || c == ':')\r
263                 {\r
264                         if (opterr)\r
265                                         fprintf (stderr, "error   : %s: invalid option -- %c\n", argv[0], c);\r
266                         \r
267                         optopt = c;\r
268                         return '?';\r
269                 }\r
270                 \r
271                 if (temp[1] == ':')\r
272                 {\r
273                         if (temp[2] == ':')\r
274                         {\r
275                                 /* it's an option that accepts an argument optionally.  */\r
276                                 if (*nextchar != '\0')\r
277                                 {\r
278                                         optarg = nextchar;\r
279                                         optind++;\r
280                                 }\r
281                                 else\r
282                                         optarg = NULL;\r
283                                 \r
284                                 nextchar = NULL;\r
285                         }\r
286                         else\r
287                         {\r
288                                 /* it's an option that requires an argument.  */\r
289                                 if (*nextchar != '\0')\r
290                                 {\r
291                                         optarg = nextchar;\r
292                                         optind++;\r
293                                 }\r
294                                 else if (optind == argc)\r
295                                 {\r
296                                         if (opterr)\r
297                                         {\r
298                                                 /* 1003.2 specifies the format of this message.  */\r
299                                                 fprintf (stderr, "ERROR   : %s: option requires an argument -- %c\n",argv[0], c);\r
300                                         }\r
301                                         optopt = c;\r
302                                         \r
303                                         if (optstring[0] == ':')\r
304                                                 c = ':';\r
305                                         else\r
306                                                 c = '?';\r
307                                 }\r
308                                 else\r
309                                         optarg = argv[optind++];\r
310                                 \r
311                                 nextchar = NULL;\r
312                         }\r
313                 }\r
314         return c;\r
315         \r
316         }\r
317 }\r
318 \r
319 \r
320 static void\r
321 __exchange (char **argv)\r
322 {\r
323         int bottom = first_nonopt;\r
324         int middle = last_nonopt;\r
325         int top = optind;\r
326         char *tem;\r
327         \r
328         while (top > middle && middle > bottom)\r
329         {\r
330                 if (top - middle > middle - bottom)\r
331                 {\r
332                         int len = middle - bottom;\r
333                         register int i;\r
334         \r
335                         for (i = 0; i < len; i++)\r
336                         {\r
337                                 tem = argv[bottom + i];\r
338                                 argv[bottom + i] = argv[top - (middle - bottom) + i];\r
339                                 argv[top - (middle - bottom) + i] = tem;\r
340                         }\r
341                         \r
342                         top -= len;\r
343                 }\r
344                 else\r
345                 {\r
346                         int len = top - middle;\r
347                         register int i;\r
348                 \r
349                         for (i = 0; i < len; i++)\r
350                         {\r
351                                 tem = argv[bottom + i];\r
352                                 argv[bottom + i] = argv[middle + i];\r
353                                 argv[middle + i] = tem;\r
354                         }\r
355 \r
356                         bottom += len;\r
357                 }\r
358         }\r
359         \r
360         \r
361         first_nonopt += (optind - last_nonopt);\r
362         last_nonopt = optind;\r
363 }\r
364 \r
365 int\r
366 getopt_long (int argc, char *const *argv, const char *options, const struct option *long_options, int *opt_index)\r
367 {\r
368         return __getopt_internal (argc, argv, options, long_options, opt_index, 0);\r
369 }\r
370 \r
371 \r
372 int\r
373 getopt_long_only(int argc, char *const *argv, const char *options, const struct option *long_options,int *opt_index)\r
374 {\r
375         return __getopt_internal (argc, argv, options, long_options, opt_index, 1);\r
376 }\r
377 \r
378 \r