@@ -78,7 +78,7 @@ static inline bool IsShortOptionChar(int ch)
78
78
return isgraph (ch);
79
79
}
80
80
81
- #if CHIP_CONFIG_NON_POSIX_LONG_OPT
81
+ #if CONFIG_NON_POSIX_GETOPT_LONG
82
82
static inline bool IsNotAnOption (char * const argv[], int element, int character)
83
83
{
84
84
char c = argv[element][character];
@@ -102,7 +102,7 @@ static inline int FirstCharacter(char * const argv[], int element)
102
102
return 0 ;
103
103
return argv[element][1 ] == ' -' ? 2 : 1 ;
104
104
}
105
- #endif // CHIP_CONFIG_NON_POSIX_LONG_OPT
105
+ #endif // CONFIG_NON_POSIX_GETOPT_LONG
106
106
107
107
/* *
108
108
* @brief
@@ -302,6 +302,17 @@ void (*PrintArgError)(const char * msg, ...) = DefaultPrintArgError;
302
302
* PrintOptionHelp() will print the option help for the different OptionSets together under
303
303
* a common section title.
304
304
*
305
+ *
306
+ * ## NON-POSIX IMPLEMENTATIONS OF getopt_long
307
+ *
308
+ * Some platforms have a version of getopt_long that behaves differently from the POSIX
309
+ * version. Set CONFIG_NON_POSIX_LONG_OPT to 1 if the platform's implementation of getopt_long
310
+ * differs from POSIX in the following ways (as is the case for ESP32 and OpenIoT):
311
+ * (a) Sets optopt to '?' when encountering an unknown short option, instead of setting it
312
+ * to the actual value of the character it encountered.
313
+ * (b) Treats an unknown long option like a series of short options.
314
+ * (c) Does not always permute argv to put the nonoptions at the end.
315
+ *
305
316
*/
306
317
bool ParseArgs (const char * progName, int argc, char * const argv[], OptionSet * optSets[],
307
318
NonOptionArgHandlerFunct nonOptArgHandler, bool ignoreUnknown)
@@ -315,13 +326,13 @@ bool ParseArgs(const char * progName, int argc, char * const argv[], OptionSet *
315
326
OptionDef * curOpt;
316
327
bool handlerRes;
317
328
int optIndex = -1 ;
318
- #if CHIP_CONFIG_NON_POSIX_LONG_OPT
319
- // The element and character that is being looked at during the current iteration's call to getopt_long.
329
+ #if CONFIG_NON_POSIX_GETOPT_LONG
330
+ // The element and character being looked at during the current iteration's call to getopt_long.
320
331
int currentElement = 0 ;
321
332
int currentCharacter;
322
333
// The value of optind after the last time getopt_long was called.
323
334
int prevOptind = 1 ;
324
- // All the nonoptions that we have found so far will be in range [firstNonoption, lastNonoptionPlus1).
335
+ // All the nonoptions we have found so far will be in range [firstNonoption, lastNonoptionPlus1).
325
336
int firstNonoption = 1 ;
326
337
int lastNonoptionPlus1 = 1 ;
327
338
// Temporary variables used When finding a new set of nonoptions.
@@ -336,7 +347,7 @@ bool ParseArgs(const char * progName, int argc, char * const argv[], OptionSet *
336
347
char * tempSwap;
337
348
// Permutable version of argv. Needed to move the nonoptions to the end.
338
349
char ** permutableArgv = const_cast <char **>(argv);
339
- #endif // CHIP_CONFIG_NON_POSIX_LONG_OPT
350
+ #endif // CONFIG_NON_POSIX_GETOPT_LONG
340
351
341
352
// The getopt() functions do not support recursion, so exit immediately with an
342
353
// error if called recursively.
@@ -378,7 +389,7 @@ bool ParseArgs(const char * progName, int argc, char * const argv[], OptionSet *
378
389
goto done;
379
390
}
380
391
381
- #if CHIP_CONFIG_NON_POSIX_LONG_OPT
392
+ #if CONFIG_NON_POSIX_GETOPT_LONG
382
393
// This non-POSIX getopt_long will sometimes permute argv, but not in all the cases that the POSIX version would and not always
383
394
// at the same times. This makes it hard to predict when it will permute, which can sometimes break the manual permutation that
384
395
// we're trying to do in here. To avoid this we do an initial pass through the full argv and let getopt_long get all its
@@ -387,7 +398,7 @@ bool ParseArgs(const char * progName, int argc, char * const argv[], OptionSet *
387
398
while (getopt_long (argc, argv, shortOpts, longOpts, &optIndex) != -1 )
388
399
{
389
400
}
390
- #endif // CHIP_CONFIG_NON_POSIX_LONG_OPT
401
+ #endif // CONFIG_NON_POSIX_GETOPT_LONG
391
402
392
403
// Force getopt() to reset its internal state.
393
404
optind = 0 ;
@@ -402,7 +413,7 @@ bool ParseArgs(const char * progName, int argc, char * const argv[], OptionSet *
402
413
optarg = nullptr ;
403
414
optopt = 0 ;
404
415
405
- #if CHIP_CONFIG_NON_POSIX_LONG_OPT
416
+ #if CONFIG_NON_POSIX_GETOPT_LONG
406
417
// Advance to the next element/character that getopt_long will be processing.
407
418
408
419
// If currentElement has been initialized and currentElement is currently a short option (or short option group).
@@ -426,11 +437,11 @@ bool ParseArgs(const char * progName, int argc, char * const argv[], OptionSet *
426
437
// Set currentCharacter to the first character after the hyphens.
427
438
currentCharacter = FirstCharacter (argv, currentElement);
428
439
}
429
- #endif // CHIP_CONFIG_NON_POSIX_LONG_OPT
440
+ #endif // CONFIG_NON_POSIX_GETOPT_LONG
430
441
431
442
id = getopt_long (argc, argv, shortOpts, longOpts, &optIndex);
432
443
433
- #if CHIP_CONFIG_NON_POSIX_LONG_OPT
444
+ #if CONFIG_NON_POSIX_GETOPT_LONG
434
445
// The POSIX implementation does the sorting as it goes, gradually permuting argv to put the nonoptions towards the end.
435
446
// This code attempts to simulate that outcome, the one difference being that the POSIX version will move nonoptions after
436
447
// the next time getopt_long is called, whereas this code will only move nonoptions when it either encounters additional
@@ -486,7 +497,7 @@ bool ParseArgs(const char * progName, int argc, char * const argv[], OptionSet *
486
497
// Store the value of optind so we'll know which elements getopt_long processes the next time it is called.
487
498
prevOptind = optind ;
488
499
489
- #endif // CHIP_CONFIG_NON_POSIX_LONG_OPT
500
+ #endif // CONFIG_NON_POSIX_GETOPT_LONG
490
501
491
502
// Stop if there are no more options.
492
503
if (id == -1 )
@@ -499,7 +510,7 @@ bool ParseArgs(const char * progName, int argc, char * const argv[], OptionSet *
499
510
continue ;
500
511
if (optopt != 0 )
501
512
{
502
- #if CHIP_CONFIG_NON_POSIX_LONG_OPT
513
+ #if CONFIG_NON_POSIX_GETOPT_LONG
503
514
if (IsShortOption (argv, currentElement))
504
515
{
505
516
// On this platform an unknown short option sets optopt to '?' instead of the actual option character, so fetch
@@ -519,7 +530,7 @@ bool ParseArgs(const char * progName, int argc, char * const argv[], OptionSet *
519
530
}
520
531
#else
521
532
PrintArgError (" %s: Unknown option: -%c\n " , progName, optopt );
522
- #endif // CHIP_CONFIG_NON_POSIX_LONG_OPT
533
+ #endif // CONFIG_NON_POSIX_GETOPT_LONG
523
534
}
524
535
else // optopt == 0 only happens for long options, so optind has already advanced.
525
536
PrintArgError (" %s: Unknown option: %s\n " , progName, argv[optind - 1 ]);
@@ -532,11 +543,11 @@ bool ParseArgs(const char * progName, int argc, char * const argv[], OptionSet *
532
543
{
533
544
// NOTE: with the way getopt_long() works, it is impossible to tell whether the option that
534
545
// was missing an argument was a long option or a short option.
535
- #if CHIP_CONFIG_NON_POSIX_LONG_OPT
546
+ #if CONFIG_NON_POSIX_GETOPT_LONG
536
547
PrintArgError (" %s: Missing argument for %s option\n " , progName, argv[currentElement]);
537
548
#else
538
549
PrintArgError (" %s: Missing argument for %s option\n " , progName, argv[optind - 1 ]);
539
- #endif // CHIP_CONFIG_NON_POSIX_LONG_OPT
550
+ #endif // CONFIG_NON_POSIX_GETOPT_LONG
540
551
goto done;
541
552
}
542
553
@@ -576,13 +587,13 @@ bool ParseArgs(const char * progName, int argc, char * const argv[], OptionSet *
576
587
// If supplied, call the non-option argument handler with the remaining arguments (if any).
577
588
if (nonOptArgHandler != nullptr )
578
589
{
579
- #if CHIP_CONFIG_NON_POSIX_LONG_OPT
590
+ #if CONFIG_NON_POSIX_GETOPT_LONG
580
591
// On a POSIX implementation, on the final interation when getopt_long returns -1 indicating it has nothing left to do,
581
592
// optind would be set to the location of the first nonoption (all of which by now would have been moved to the end of
582
593
// argv). On some non-POSIX implementations this is not true -- it simply sets optind to the location of argv's terminal
583
594
// NULL (i.e. optind == argc). So we have to alter optind here to simulate the POSIX behavior.
584
595
optind = firstNonoption;
585
- #endif // CHIP_CONFIG_NON_POSIX_LONG_OPT
596
+ #endif // CONFIG_NON_POSIX_GETOPT_LONG
586
597
587
598
if (!nonOptArgHandler (progName, argc - optind , argv + optind ))
588
599
goto done;
0 commit comments