Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
sanitize the use of logs in unit testing of parmap to fix supernovae
[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 \rchar *\r optarg = NULL;
6 \r\rint \r optind = 0;
7 \r\rint \r optopt = '?';
8 \r\rint \r opterr = 1;
9 \r\rstatic char *\r nextchar;
10 \r\rstatic \r int first_nonopt;
11 \r\rstatic int \r last_nonopt;
12 \r\r\rstatic enum \r { \rREQUIRE_ORDER, \rPERMUTE, \rRETURN_IN_ORDER \r
13 } ordering;
14 \r\r\rstatic const char *\r __getopt_initialize(const char *optstring);
15 \r\rstatic int \r
16 __getopt_internal(int argc, char *const *argv, const char *optstring,
17                   const struct option *longopts, int *longind,
18                   int long_only);
19 \r\rstatic void \r __exchange(char **argv);
20 \r\rint \r getopt(int argc, char *const argv[], const char *optstring) \r
21 {
22   \rreturn __getopt_internal(argc, argv, optstring,
23                             (const struct option *) 0, (int *) 0, 0);
24 \r\r\rstatic const char *\r __getopt_initialize(const char *optstring) \r
25 {
26   \r
27       /* Start processing options with ARGV-element 1 (since ARGV-element 0\r
28          is the program name); the sequence of previously skipped\r
29          non-option ARGV-elements is empty.  */ \r
30       \rfirst_nonopt = last_nonopt = optind = 1;
31   \rnextchar = NULL;
32   \r\r
33       /* Determine how to handle the ordering of options and nonoptions.  */ \r
34       \rif (optstring[0] == '-')
35     \r {
36     \rordering = RETURN_IN_ORDER;
37     \r++optstring;
38     \r}
39   \r
40       /* si la chaîne d'options commence par un + alors la fonction getopt() s'arrête\r
41        * dès qu'un argument de la ligne de commande n'est pas une option\r
42        */ \r
43       else if (optstring[0] == '+')
44     \r {
45     \rordering = REQUIRE_ORDER;
46     \r++optstring;
47     \r}
48   \r
49   else
50     \r {
51     \rordering = PERMUTE;
52     \r}
53   \r\rreturn optstring;
54 \r}
55
56 \r\rint \r
57 __getopt_internal(int argc, char *const *argv, const char *optstring,
58                   const struct option *longopts, int *longind,
59                   int long_only) \r
60 {
61   \roptarg = NULL;
62   \r\rif (optind == 0)
63     \roptstring = __getopt_initialize(optstring);
64   \r\rif (nextchar == NULL || *nextchar == '\0')
65     \r {
66     \r
67         /* Advance to the next ARGV-element.  */ \r
68         \rif (ordering == PERMUTE)
69       \r {
70       \r
71           /* If we have just processed some options following some non-options,\r
72              __exchange them so that the options come first.  */ \r
73           \rif (first_nonopt != last_nonopt && last_nonopt != optind)
74         \r__exchange((char **) argv);
75       \r
76       else if (last_nonopt != optind)
77         \rfirst_nonopt = optind;
78       \r\r
79           /* Skip any additional non-options\r
80              and extend the range of non-options previously skipped.  */ \r
81           \rwhile (optind < argc
82                   && (argv[optind][0] != '-' || argv[optind][1] == '\0'))
83         \roptind++;
84       \r\rlast_nonopt = optind;
85       \r}
86     \r\r
87         /* The special ARGV-element `--' means premature end of options.\r
88            Skip it like a null option,\r
89            then __exchange with previous non-options as if it were an option,\r
90            then skip everything else like a non-option.  */ \r
91         \rif (optind != argc && !strcmp(argv[optind], "--"))
92       \r {
93       \roptind++;
94       \r\rif (first_nonopt != last_nonopt && last_nonopt != optind)
95         \r__exchange((char **) argv);
96       \r
97       else if (first_nonopt == last_nonopt)
98         \rfirst_nonopt = optind;
99       \r\rlast_nonopt = argc;
100       \r\roptind = argc;
101       \r}
102     \r\r
103         /* If we have done all the ARGV-elements, stop the scan\r
104            and back over any non-options that we skipped and permuted.  */ \r
105         \rif (optind == argc)
106       \r {
107       \r
108           /* Set the next-arg-index to point at the non-options\r
109              that we previously skipped, so the caller will digest them.  */ \r
110           if (first_nonopt != last_nonopt)
111         \roptind = first_nonopt;
112       \r\rreturn EOF;
113       \r}
114     \r\r
115         /* If we have come to a non-option and did not permute it,\r
116            either stop the scan or describe it to the caller and pass it by.  */ \r
117         \rif ((argv[optind][0] != '-' || argv[optind][1] == '\0'))
118       \r {
119       \rif (ordering == REQUIRE_ORDER)
120         \rreturn EOF;
121       \roptarg = argv[optind++];
122       \rreturn 1;
123       \r}
124     \r\r
125         /* We have found another option-ARGV-element.\r
126            Skip the initial punctuation.  */ \r
127         \rnextchar =
128         (argv[optind] + 1 + (longopts != NULL && argv[optind][1] == '-'));
129     \r}
130   \r\r
131       /* Decode the current option-ARGV-element.  */ \r
132       \r
133       /* Check whether the ARGV-element is a long option.\r
134          \r
135          If long_only and the ARGV-element has the form "-f", where f is\r
136          a valid short option, don't consider it an abbreviated form of\r
137          a long option that starts with f.  Otherwise there would be no\r
138          way to give the -f short option.\r
139          \r
140          On the other hand, if there's a long option "fubar" and\r
141          the ARGV-element is "-fu", do consider that an abbreviation of\r
142          the long option, just like "--fu", and not "-f" with arg "u".\r
143          \r
144          This distinction seems to be the most useful approach.  */ \r
145       \rif (longopts != NULL
146            && (argv[optind][1] == '-'
147                || (long_only
148                    && (argv[optind][2]
149                        || !strchr(optstring, argv[optind][1])))))
150     \r {
151     \rchar *nameend;
152     \rconst struct option *p;
153     \rconst struct option *pfound = NULL;
154     \rint exact = 0;
155     \rint ambig = 0;
156     \rint indfound = 0;
157     \rint option_index;
158     \r\rfor (nameend = nextchar; *nameend != '\0' && *nameend != '=';
159            nameend++)
160       \r
161           /* Do nothing.  */ ;
162     \r\r
163         /* Test all long options for either exact match\r
164            or abbreviated matches.  */ \r
165         for (p = longopts, option_index = 0; p->name; p++, option_index++)
166       \r {
167       \rif (!strncmp(p->name, nextchar, nameend - nextchar))
168         \r {
169         \r\rif (nameend - nextchar == strlen(p->name))
170           \r {
171           \r
172               /* Exact match found.  */ \r
173               pfound = p;
174           \rindfound = option_index;
175           \rexact = 1;
176           \rbreak;
177           \r}
178         \r
179         else if (pfound == NULL)
180           \r {
181           \r
182               /* First nonexact match found.  */ \r
183               exact = 0;
184           \r
185               /* begin change\r
186                  pfound = p;\r
187                  indfound = option_index;\r
188                  end change */ \r
189               break;
190           \r}
191         \r
192         else
193           \r {
194           \r\r
195               /* Second or later nonexact match found.  */ \r
196               ambig = 1;
197           \r}
198         \r}
199       \r}
200     \r\rif (ambig && !exact)
201       \r {
202       \rif (opterr)
203         \rfprintf(stderr, "error   : %s: option `%s' is ambiguous\n",
204                  argv[0], argv[optind]);
205       \r\rnextchar += strlen(nextchar);
206       \roptind++;
207       \rreturn '?';
208       \r}
209     \r\rif (pfound != NULL)
210       \r {
211       \roption_index = indfound;
212       \roptind++;
213       \r\rif (*nameend)
214         \r {
215         \r
216             /* Don't test has_arg with >, because some C compilers don't\r
217                allow it to be used on enums.  */ \r
218             if (pfound->has_arg)
219           \roptarg = nameend + 1;
220         \r
221         else
222           \r {
223           \rif (opterr)
224             \r {
225             \rif (argv[optind - 1][1] == '-')
226               \r
227                   /* --option */ \r
228                   fprintf(stderr,
229                           "error   : %s: option `--%s' doesn't allow an argument\n",
230                           argv[0], pfound->name);
231             \r
232             else
233               \r
234                   /* +option or -option */ \r
235                   fprintf(stderr,
236                           "error   : %s: option `%c%s' doesn't allow an argument\n",
237                           argv[0], argv[optind - 1][0], pfound->name);
238             \r}
239           \r\rnextchar += strlen(nextchar);
240           \rreturn '?';
241           \r}
242         \r}
243       \r
244       else if (pfound->has_arg == 1)
245         \r {
246         \rif (optind < argc)
247           \roptarg = argv[optind++];
248         \r
249         else
250           \r {
251           \rif (opterr)
252             \rfprintf(stderr,
253                      "error   : %s: option `%s' requires an argument\n",
254                      argv[0], argv[optind - 1]);
255           \r\rnextchar += strlen(nextchar);
256           \rreturn optstring[0] == ':' ? ':' : '?';
257           \r}
258         \r}
259       \r\rnextchar += strlen(nextchar);
260       \r\rif (longind != NULL)
261         \r*longind = option_index;
262       \r\rif (pfound->flag)
263         \r {
264         \r*(pfound->flag) = pfound->val;
265         \rreturn 0;
266         \r}
267       \r\rreturn pfound->val;
268       \r}
269     \r\r
270         /* Can't find it as a long option.  If this is not getopt_long_only,\r
271            or the option starts with '--' or is not a valid short\r
272            option, then it's an error.\r
273            Otherwise interpret it as a short option.  */ \r
274         if (!long_only || argv[optind][1] == '-'
275             || strchr(optstring, *nextchar) == NULL)
276       \r {
277       \rif (opterr)
278         \r {
279         \rif (argv[optind][1] == '-')
280           \r
281               /* --option */ \r
282               fprintf(stderr, "error   : %s: unrecognized option `--%s'\n",
283                       argv[0], nextchar);
284         \r
285         else
286           \r
287               /* +option or -option */ \r
288               fprintf(stderr, "error   : %s: unrecognized option `%c%s'\n",
289                       argv[0], argv[optind][0], nextchar);
290         \r}
291       \r\rnextchar = (char *) "";
292       \roptind++;
293       \rreturn '?';
294       \r}
295     \r}
296   \r\r
297       /* Look at and handle the next short option-character.  */ \r
298       \r {
299     \rchar c = *nextchar++;
300     \rchar *temp = strchr(optstring, c);
301     \r\r
302         /* Increment `optind' when we start to process its last character.  */ \r
303         if (*nextchar == '\0')
304       \r++optind;
305     \r\rif (temp == NULL || c == ':')
306       \r {
307       \rif (opterr)
308         \rfprintf(stderr, "error   : %s: invalid option -- %c\n", argv[0],
309                  c);
310       \r\roptopt = c;
311       \rreturn '?';
312       \r}
313     \r\rif (temp[1] == ':')
314       \r {
315       \rif (temp[2] == ':')
316         \r {
317         \r
318             /* This is an option that accepts an argument optionally.  */ \r
319             if (*nextchar != '\0')
320           \r {
321           \roptarg = nextchar;
322           \roptind++;
323           \r}
324         \r
325         else
326           \roptarg = NULL;
327         \r\rnextchar = NULL;
328         \r}
329       \r
330       else
331         \r {
332         \r
333             /* This is an option that requires an argument.  */ \r
334             if (*nextchar != '\0')
335           \r {
336           \roptarg = nextchar;
337           \r
338               /* If we end this ARGV-element by taking the rest as an arg,\r
339                  we must advance to the next element now.  */ \r
340               optind++;
341           \r}
342         \r
343         else if (optind == argc)
344           \r {
345           \rif (opterr)
346             \r {
347             \r
348                 /* 1003.2 specifies the format of this message.  */ \r
349                 fprintf(stderr,
350                         "error   : %s: option requires an argument -- %c\n",
351                         argv[0], c);
352             \r}
353           \roptopt = c;
354           \r\rif (optstring[0] == ':')
355             \rc = ':';
356           \r
357           else
358             \rc = '?';
359           \r}
360         \r
361         else
362           \r
363               /* We already incremented `optind' once;\r
364                  increment it again when taking next ARGV-elt as argument.  */ \r
365               optarg = argv[optind++];
366         \r\rnextchar = NULL;
367         \r}
368       \r}
369     \rreturn c;
370   \r\r}
371 \r}
372
373 \r\r\rstatic void \r __exchange(char **argv) \r
374 {
375   \rint bottom = first_nonopt;
376   \rint middle = last_nonopt;
377   \rint top = optind;
378   \rchar *tem;
379   \r\r
380       /* Exchange the shorter segment with the far end of the longer segment.\r
381          That puts the shorter segment into the right place.\r
382          It leaves the longer segment in the right place overall,\r
383          but it consists of two parts that need to be swapped next.  */ \r
384       \rwhile (top > middle && middle > bottom)
385     \r {
386     \rif (top - middle > middle - bottom)
387       \r {
388       \r
389           /* Bottom segment is the short one.  */ \r
390       int len = middle - bottom;
391       \rregister int i;
392       \r\r
393           /* Swap it with the top part of the top segment.  */ \r
394           for (i = 0; i < len; i++)
395         \r {
396         \rtem = argv[bottom + i];
397         \rargv[bottom + i] = argv[top - (middle - bottom) + i];
398         \rargv[top - (middle - bottom) + i] = tem;
399         \r}
400       \r
401           /* Exclude the moved bottom segment from further swapping.  */ \r
402           \rtop -= len;
403       \r}
404     \r
405     else
406       \r {
407       \r
408           /* Top segment is the short one.  */ \r
409       int len = top - middle;
410       \rregister int i;
411       \r\r
412           /* Swap it with the bottom part of the bottom segment.  */ \r
413           for (i = 0; i < len; i++)
414         \r {
415         \rtem = argv[bottom + i];
416         \rargv[bottom + i] = argv[middle + i];
417         \rargv[middle + i] = tem;
418         \r}
419       \r
420           /* Exclude the moved top segment from further swapping.  */ \r
421           bottom += len;
422       \r}
423     \r}
424   \r\r
425       /* Update records for the slots the non-options now occupy.  */ \r
426       \rfirst_nonopt += (optind - last_nonopt);
427   \rlast_nonopt = optind;
428 \r}
429
430 \r\rint \r
431 getopt_long(int argc, char *const *argv, const char *options,
432             const struct option *long_options, int *opt_index) \r
433 {
434   \rreturn __getopt_internal(argc, argv, options, long_options, opt_index,
435                             0);
436 \r}
437
438 \r\r\rint \r
439 getopt_long_only(int argc, char *const *argv, const char *options,
440                  const struct option *long_options, int *opt_index) \r
441 {
442   \rreturn __getopt_internal(argc, argv, options, long_options, opt_index,
443                             1);
444 \r}
445
446 \r\r\r