@@ -423,12 +423,12 @@ static char *findspace(const char *s)
423
423
424
424
static int cmd_parseopt (int argc , const char * * argv , const char * prefix )
425
425
{
426
- static int keep_dashdash = 0 , stop_at_non_option = 0 ;
427
- static char const * const parseopt_usage [] = {
426
+ int keep_dashdash = 0 , stop_at_non_option = 0 ;
427
+ char const * const parseopt_usage [] = {
428
428
N_ ("git rev-parse --parseopt [<options>] -- [<args>...]" ),
429
429
NULL
430
430
};
431
- static struct option parseopt_opts [] = {
431
+ struct option parseopt_opts [] = {
432
432
OPT_BOOL (0 , "keep-dashdash" , & keep_dashdash ,
433
433
N_ ("keep the `--` passed as an arg" )),
434
434
OPT_BOOL (0 , "stop-at-non-option" , & stop_at_non_option ,
@@ -438,12 +438,11 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
438
438
N_ ("output in stuck long form" )),
439
439
OPT_END (),
440
440
};
441
- static const char * const flag_chars = "*=?!" ;
442
-
443
441
struct strbuf sb = STRBUF_INIT , parsed = STRBUF_INIT ;
444
- const char * * usage = NULL ;
442
+ struct strvec longnames = STRVEC_INIT ;
443
+ struct strvec usage = STRVEC_INIT ;
445
444
struct option * opts = NULL ;
446
- int onb = 0 , osz = 0 , unb = 0 , usz = 0 ;
445
+ size_t opts_nr = 0 , opts_alloc = 0 ;
447
446
448
447
strbuf_addstr (& parsed , "set --" );
449
448
argc = parse_options (argc , argv , prefix , parseopt_opts , parseopt_usage ,
@@ -453,16 +452,16 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
453
452
454
453
/* get the usage up to the first line with a -- on it */
455
454
for (;;) {
455
+ strbuf_reset (& sb );
456
456
if (strbuf_getline (& sb , stdin ) == EOF )
457
457
die (_ ("premature end of input" ));
458
- ALLOC_GROW (usage , unb + 1 , usz );
459
458
if (!strcmp ("--" , sb .buf )) {
460
- if (unb < 1 )
459
+ if (! usage . nr )
461
460
die (_ ("no usage string given before the `--' separator" ));
462
- usage [unb ] = NULL ;
463
461
break ;
464
462
}
465
- usage [unb ++ ] = strbuf_detach (& sb , NULL );
463
+
464
+ strvec_push (& usage , sb .buf );
466
465
}
467
466
468
467
/* parse: (<short>|<short>,<long>|<long>)[*=?!]*<arghint>? SP+ <help> */
@@ -474,10 +473,10 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
474
473
if (!sb .len )
475
474
continue ;
476
475
477
- ALLOC_GROW (opts , onb + 1 , osz );
478
- memset (opts + onb , 0 , sizeof (opts [ onb ] ));
476
+ ALLOC_GROW (opts , opts_nr + 1 , opts_alloc );
477
+ memset (opts + opts_nr , 0 , sizeof (* opts ));
479
478
480
- o = & opts [onb ++ ];
479
+ o = & opts [opts_nr ++ ];
481
480
help = findspace (sb .buf );
482
481
if (!help || sb .buf == help ) {
483
482
o -> type = OPTION_GROUP ;
@@ -494,20 +493,22 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
494
493
o -> callback = & parseopt_dump ;
495
494
496
495
/* name(s) */
497
- s = strpbrk (sb .buf , flag_chars );
496
+ s = strpbrk (sb .buf , "*=?!" );
498
497
if (!s )
499
498
s = help ;
500
499
501
500
if (s == sb .buf )
502
501
die (_ ("missing opt-spec before option flags" ));
503
502
504
- if (s - sb .buf == 1 ) /* short option only */
503
+ if (s - sb .buf == 1 ) { /* short option only */
505
504
o -> short_name = * sb .buf ;
506
- else if (sb .buf [1 ] != ',' ) /* long option only */
507
- o -> long_name = xmemdupz (sb .buf , s - sb .buf );
508
- else {
505
+ } else if (sb .buf [1 ] != ',' ) { /* long option only */
506
+ o -> long_name = strvec_pushf (& longnames , "%.*s" ,
507
+ (int )(s - sb .buf ), sb .buf );
508
+ } else {
509
509
o -> short_name = * sb .buf ;
510
- o -> long_name = xmemdupz (sb .buf + 2 , s - sb .buf - 2 );
510
+ o -> long_name = strvec_pushf (& longnames , "%.*s" ,
511
+ (int )(s - sb .buf - 2 ), sb .buf + 2 );
511
512
}
512
513
513
514
/* flags */
@@ -537,17 +538,23 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
537
538
strbuf_release (& sb );
538
539
539
540
/* put an OPT_END() */
540
- ALLOC_GROW (opts , onb + 1 , osz );
541
- memset (opts + onb , 0 , sizeof (opts [ onb ] ));
542
- argc = parse_options (argc , argv , prefix , opts , usage ,
541
+ ALLOC_GROW (opts , opts_nr + 1 , opts_alloc );
542
+ memset (opts + opts_nr , 0 , sizeof (* opts ));
543
+ argc = parse_options (argc , argv , prefix , opts , usage . v ,
543
544
(keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0 ) |
544
545
(stop_at_non_option ? PARSE_OPT_STOP_AT_NON_OPTION : 0 ) |
545
546
PARSE_OPT_SHELL_EVAL );
546
547
547
548
strbuf_addstr (& parsed , " --" );
548
549
sq_quote_argv (& parsed , argv );
549
550
puts (parsed .buf );
551
+
550
552
strbuf_release (& parsed );
553
+ strbuf_release (& sb );
554
+ strvec_clear (& longnames );
555
+ strvec_clear (& usage );
556
+ free ((char * ) opts -> help );
557
+ free (opts );
551
558
return 0 ;
552
559
}
553
560
0 commit comments