This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
popt.c
1787 lines (1573 loc) · 47.1 KB
/
popt.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** \ingroup popt
* \file popt/popt.c
*/
/* (C) 1998-2002 Red Hat, Inc. -- Licensing details are in the COPYING
file accompanying popt source distributions, available from
ftp://ftp.rpm.org/pub/rpm/dist */
#undef MYDEBUG
#include "system.h"
#if defined(__LCLINT__)
/*@-declundef -exportheader @*/
extern long long int strtoll(const char *nptr, /*@null@*/ char **endptr,
int base)
/*@modifies *endptr@*/;
/*@=declundef =exportheader @*/
#endif
#ifdef HAVE_FLOAT_H
#include <float.h>
#endif
#include <math.h>
#include "poptint.h"
#ifdef MYDEBUG
/*@unchecked@*/
int _popt_debug = 0;
#endif
/*@unchecked@*/
unsigned int _poptArgMask = POPT_ARG_MASK;
/*@unchecked@*/
unsigned int _poptGroupMask = POPT_GROUP_MASK;
#if !defined(HAVE_STRERROR) && !defined(__LCLINT__)
static char * strerror(int errno)
{
extern int sys_nerr;
extern char * sys_errlist[];
if ((0 <= errno) && (errno < sys_nerr))
return sys_errlist[errno];
else
return POPT_("unknown errno");
}
#endif
#ifdef MYDEBUG
/*@unused@*/
static void prtcon(const char *msg, poptContext con)
{
if (msg) fprintf(stderr, "%s", msg);
fprintf(stderr, "\tcon %p os %p nextCharArg \"%s\" nextArg \"%s\" argv[%d] \"%s\"\n",
con, con->os,
(con->os->nextCharArg ? con->os->nextCharArg : ""),
(con->os->nextArg ? con->os->nextArg : ""),
con->os->next,
(con->os->argv && con->os->argv[con->os->next]
? con->os->argv[con->os->next] : ""));
}
#endif
void poptSetExecPath(poptContext con, const char * path, int allowAbsolute)
{
con->execPath = _free(con->execPath);
con->execPath = xstrdup(path);
con->execAbsolute = allowAbsolute;
return;
}
static void invokeCallbacksPRE(poptContext con, const struct poptOption * opt)
/*@globals internalState@*/
/*@modifies internalState@*/
{
if (opt != NULL)
for (; opt->longName || opt->shortName || opt->arg; opt++) {
poptArg arg = { .ptr = opt->arg };
if (arg.ptr)
switch (poptArgType(opt)) {
case POPT_ARG_INCLUDE_TABLE: /* Recurse on included sub-tables. */
poptSubstituteHelpI18N(arg.opt); /* XXX side effects */
invokeCallbacksPRE(con, arg.opt);
/*@switchbreak@*/ break;
case POPT_ARG_CALLBACK: /* Perform callback. */
if (!CBF_ISSET(opt, PRE))
/*@switchbreak@*/ break;
/*@-noeffectuncon @*/ /* XXX no known way to annotate (*vector) calls. */
arg.cb(con, POPT_CALLBACK_REASON_PRE, NULL, NULL, opt->descrip);
/*@=noeffectuncon @*/
/*@switchbreak@*/ break;
}
}
}
static void invokeCallbacksPOST(poptContext con, const struct poptOption * opt)
/*@globals internalState@*/
/*@modifies internalState@*/
{
if (opt != NULL)
for (; opt->longName || opt->shortName || opt->arg; opt++) {
poptArg arg = { .ptr = opt->arg };
if (arg.ptr)
switch (poptArgType(opt)) {
case POPT_ARG_INCLUDE_TABLE: /* Recurse on included sub-tables. */
poptSubstituteHelpI18N(arg.opt); /* XXX side effects */
invokeCallbacksPOST(con, arg.opt);
/*@switchbreak@*/ break;
case POPT_ARG_CALLBACK: /* Perform callback. */
if (!CBF_ISSET(opt, POST))
/*@switchbreak@*/ break;
/*@-noeffectuncon @*/ /* XXX no known way to annotate (*vector) calls. */
arg.cb(con, POPT_CALLBACK_REASON_POST, NULL, NULL, opt->descrip);
/*@=noeffectuncon @*/
/*@switchbreak@*/ break;
}
}
}
static void invokeCallbacksOPTION(poptContext con,
const struct poptOption * opt,
const struct poptOption * myOpt,
/*@null@*/ const void * myData, int shorty)
/*@globals internalState@*/
/*@modifies internalState@*/
{
const struct poptOption * cbopt = NULL;
poptArg cbarg = { .ptr = NULL };
if (opt != NULL)
for (; opt->longName || opt->shortName || opt->arg; opt++) {
poptArg arg = { .ptr = opt->arg };
switch (poptArgType(opt)) {
case POPT_ARG_INCLUDE_TABLE: /* Recurse on included sub-tables. */
poptSubstituteHelpI18N(arg.opt); /* XXX side effects */
if (opt->arg != NULL)
invokeCallbacksOPTION(con, opt->arg, myOpt, myData, shorty);
/*@switchbreak@*/ break;
case POPT_ARG_CALLBACK: /* Save callback info. */
if (CBF_ISSET(opt, SKIPOPTION))
/*@switchbreak@*/ break;
cbopt = opt;
cbarg.ptr = opt->arg;
/*@switchbreak@*/ break;
default: /* Perform callback on matching option. */
if (cbopt == NULL || cbarg.cb == NULL)
/*@switchbreak@*/ break;
if ((myOpt->shortName && opt->shortName && shorty &&
myOpt->shortName == opt->shortName)
|| (myOpt->longName != NULL && opt->longName != NULL &&
!strcmp(myOpt->longName, opt->longName)))
{ const void *cbData = (cbopt->descrip ? cbopt->descrip : myData);
/*@-noeffectuncon @*/ /* XXX no known way to annotate (*vector) calls. */
cbarg.cb(con, POPT_CALLBACK_REASON_OPTION,
myOpt, con->os->nextArg, cbData);
/*@=noeffectuncon @*/
/* Terminate (unless explcitly continuing). */
if (!CBF_ISSET(cbopt, CONTINUE))
return;
}
/*@switchbreak@*/ break;
}
}
}
poptContext poptGetContext(const char * name, int argc, const char ** argv,
const struct poptOption * options, unsigned int flags)
{
poptContext con = malloc(sizeof(*con));
if (con == NULL) return NULL; /* XXX can't happen */
memset(con, 0, sizeof(*con));
con->os = con->optionStack;
con->os->argc = argc;
/*@-dependenttrans -assignexpose@*/ /* FIX: W2DO? */
con->os->argv = argv;
/*@=dependenttrans =assignexpose@*/
con->os->argb = NULL;
if (!(flags & POPT_CONTEXT_KEEP_FIRST))
con->os->next = 1; /* skip argv[0] */
con->leftovers = calloc( (size_t)(argc + 1), sizeof(*con->leftovers) );
/*@-dependenttrans -assignexpose@*/ /* FIX: W2DO? */
con->options = options;
/*@=dependenttrans =assignexpose@*/
con->aliases = NULL;
con->numAliases = 0;
con->flags = flags;
con->execs = NULL;
con->numExecs = 0;
con->finalArgvAlloced = argc * 2;
con->finalArgv = calloc( (size_t)con->finalArgvAlloced, sizeof(*con->finalArgv) );
con->execAbsolute = 1;
con->arg_strip = NULL;
if (getenv("POSIXLY_CORRECT") || getenv("POSIX_ME_HARDER"))
con->flags |= POPT_CONTEXT_POSIXMEHARDER;
if (name)
con->appName = xstrdup(name);
invokeCallbacksPRE(con, con->options);
return con;
}
static void cleanOSE(/*@special@*/ struct optionStackEntry *os)
/*@uses os @*/
/*@releases os->nextArg, os->argv, os->argb @*/
/*@modifies os @*/
{
os->nextArg = _free(os->nextArg);
os->argv = _free(os->argv);
os->argb = PBM_FREE(os->argb);
}
void poptResetContext(poptContext con)
{
int i;
if (con == NULL) return;
while (con->os > con->optionStack) {
cleanOSE(con->os--);
}
con->os->argb = PBM_FREE(con->os->argb);
con->os->currAlias = NULL;
con->os->nextCharArg = NULL;
con->os->nextArg = NULL;
con->os->next = 1; /* skip argv[0] */
con->numLeftovers = 0;
con->nextLeftover = 0;
con->restLeftover = 0;
con->doExec = NULL;
if (con->finalArgv != NULL)
for (i = 0; i < con->finalArgvCount; i++) {
/*@-unqualifiedtrans@*/ /* FIX: typedef double indirection. */
con->finalArgv[i] = _free(con->finalArgv[i]);
/*@=unqualifiedtrans@*/
}
con->finalArgvCount = 0;
con->arg_strip = PBM_FREE(con->arg_strip);
/*@-nullstate@*/ /* FIX: con->finalArgv != NULL */
return;
/*@=nullstate@*/
}
/* Only one of longName, shortName should be set, not both. */
static int handleExec(/*@special@*/ poptContext con,
/*@null@*/ const char * longName, char shortName)
/*@uses con->execs, con->numExecs, con->flags, con->doExec,
con->finalArgv, con->finalArgvAlloced, con->finalArgvCount @*/
/*@modifies con @*/
{
poptItem item;
int i;
if (con->execs == NULL || con->numExecs <= 0) /* XXX can't happen */
return 0;
for (i = con->numExecs - 1; i >= 0; i--) {
item = con->execs + i;
if (longName && !(item->option.longName &&
!strcmp(longName, item->option.longName)))
continue;
else if (shortName != item->option.shortName)
continue;
break;
}
if (i < 0) return 0;
if (con->flags & POPT_CONTEXT_NO_EXEC)
return 1;
if (con->doExec == NULL) {
con->doExec = con->execs + i;
return 1;
}
/* We already have an exec to do; remember this option for next
time 'round */
if ((con->finalArgvCount + 1) >= (con->finalArgvAlloced)) {
con->finalArgvAlloced += 10;
con->finalArgv = realloc(con->finalArgv,
sizeof(*con->finalArgv) * con->finalArgvAlloced);
}
i = con->finalArgvCount++;
if (con->finalArgv != NULL) /* XXX can't happen */
{ char *s = malloc((longName ? strlen(longName) : 0) + sizeof("--"));
if (s != NULL) { /* XXX can't happen */
con->finalArgv[i] = s;
*s++ = '-';
if (longName)
s = stpcpy( stpcpy(s, "-"), longName);
else
*s++ = shortName;
*s = '\0';
} else
con->finalArgv[i] = NULL;
}
return 1;
}
/**
* Compare long option for equality, adjusting for POPT_ARGFLAG_TOGGLE.
* @param opt option
* @param longName arg option
* @param longNameLen arg option length
* @return does long option match?
*/
static int
longOptionStrcmp(const struct poptOption * opt,
/*@null@*/ const char * longName, size_t longNameLen)
/*@*/
{
const char * optLongName = opt->longName;
int rc;
if (optLongName == NULL || longName == NULL) /* XXX can't heppen */
return 0;
if (F_ISSET(opt, TOGGLE)) {
if (optLongName[0] == 'n' && optLongName[1] == 'o') {
optLongName += sizeof("no") - 1;
if (optLongName[0] == '-')
optLongName++;
}
if (longName[0] == 'n' && longName[1] == 'o') {
longName += sizeof("no") - 1;
longNameLen -= sizeof("no") - 1;
if (longName[0] == '-') {
longName++;
longNameLen--;
}
}
}
rc = (int)(strlen(optLongName) == longNameLen);
if (rc)
rc = (int)(strncmp(optLongName, longName, longNameLen) == 0);
return rc;
}
/* Only one of longName, shortName may be set at a time */
static int handleAlias(/*@special@*/ poptContext con,
/*@null@*/ const char * longName, size_t longNameLen,
char shortName,
/*@exposed@*/ /*@null@*/ const char * nextArg)
/*@uses con->aliases, con->numAliases, con->optionStack, con->os,
con->os->currAlias, con->os->currAlias->option.longName @*/
/*@modifies con @*/
{
poptItem item = con->os->currAlias;
int rc;
int i;
if (item) {
if (longName && item->option.longName != NULL
&& longOptionStrcmp(&item->option, longName, longNameLen))
return 0;
else
if (shortName && shortName == item->option.shortName)
return 0;
}
if (con->aliases == NULL || con->numAliases <= 0) /* XXX can't happen */
return 0;
for (i = con->numAliases - 1; i >= 0; i--) {
item = con->aliases + i;
if (longName) {
if (item->option.longName == NULL)
continue;
if (!longOptionStrcmp(&item->option, longName, longNameLen))
continue;
} else if (shortName != item->option.shortName)
continue;
break;
}
if (i < 0) return 0;
if ((con->os - con->optionStack + 1) == POPT_OPTION_DEPTH)
return POPT_ERROR_OPTSTOODEEP;
if (longName == NULL && nextArg != NULL && *nextArg != '\0')
con->os->nextCharArg = nextArg;
con->os++;
con->os->next = 0;
con->os->stuffed = 0;
con->os->nextArg = NULL;
con->os->nextCharArg = NULL;
con->os->currAlias = con->aliases + i;
{ const char ** av;
int ac = con->os->currAlias->argc;
/* Append --foo=bar arg to alias argv array (if present). */
if (longName && nextArg != NULL && *nextArg != '\0') {
av = malloc((ac + 1 + 1) * sizeof(*av));
if (av != NULL) { /* XXX won't happen. */
for (i = 0; i < ac; i++) {
av[i] = con->os->currAlias->argv[i];
}
av[ac++] = nextArg;
av[ac] = NULL;
} else /* XXX revert to old popt behavior if malloc fails. */
av = con->os->currAlias->argv;
} else
av = con->os->currAlias->argv;
rc = poptDupArgv(ac, av, &con->os->argc, &con->os->argv);
if (av != NULL && av != con->os->currAlias->argv)
free(av);
}
con->os->argb = NULL;
return (rc ? rc : 1);
}
/**
* Return absolute path to executable by searching PATH.
* @param argv0 name of executable
* @return (malloc'd) absolute path to executable (or NULL)
*/
static /*@null@*/
const char * findProgramPath(/*@null@*/ const char * argv0)
/*@*/
{
char *path = NULL, *s = NULL, *se;
char *t = NULL;
if (argv0 == NULL) return NULL; /* XXX can't happen */
/* If there is a / in argv[0], it has to be an absolute path. */
/* XXX Hmmm, why not if (argv0[0] == '/') ... instead? */
if (strchr(argv0, '/'))
return xstrdup(argv0);
if ((path = getenv("PATH")) == NULL || (path = xstrdup(path)) == NULL)
return NULL;
/* The return buffer in t is big enough for any path. */
if ((t = malloc(strlen(path) + strlen(argv0) + sizeof("/"))) != NULL)
for (s = path; s && *s; s = se) {
/* Snip PATH element into [s,se). */
if ((se = strchr(s, ':')))
*se++ = '\0';
/* Append argv0 to PATH element. */
(void) stpcpy(stpcpy(stpcpy(t, s), "/"), argv0);
/* If file is executable, bingo! */
if (!access(t, X_OK))
break;
}
/* If no executable was found in PATH, return NULL. */
/*@-compdef@*/
if (!(s && *s) && t != NULL)
t = _free(t);
/*@=compdef@*/
/*@-modobserver -observertrans -usedef @*/
path = _free(path);
/*@=modobserver =observertrans =usedef @*/
return t;
}
static int execCommand(poptContext con)
/*@globals internalState @*/
/*@modifies internalState @*/
{
poptItem item = con->doExec;
poptArgv argv = NULL;
int argc = 0;
int rc;
int ec = POPT_ERROR_ERRNO;
if (item == NULL) /*XXX can't happen*/
return POPT_ERROR_NOARG;
if (item->argv == NULL || item->argc < 1 ||
(!con->execAbsolute && strchr(item->argv[0], '/')))
return POPT_ERROR_NOARG;
argv = malloc(sizeof(*argv) *
(6 + item->argc + con->numLeftovers + con->finalArgvCount));
if (argv == NULL) return POPT_ERROR_MALLOC;
if (!strchr(item->argv[0], '/') && con->execPath != NULL) {
char *s = malloc(strlen(con->execPath) + strlen(item->argv[0]) + sizeof("/"));
if (s)
(void)stpcpy(stpcpy(stpcpy(s, con->execPath), "/"), item->argv[0]);
argv[argc] = s;
} else
argv[argc] = findProgramPath(item->argv[0]);
if (argv[argc++] == NULL) {
ec = POPT_ERROR_NOARG;
goto exit;
}
if (item->argc > 1) {
memcpy(argv + argc, item->argv + 1, sizeof(*argv) * (item->argc - 1));
argc += (item->argc - 1);
}
if (con->finalArgv != NULL && con->finalArgvCount > 0) {
memcpy(argv + argc, con->finalArgv,
sizeof(*argv) * con->finalArgvCount);
argc += con->finalArgvCount;
}
if (con->leftovers != NULL && con->numLeftovers > 0) {
memcpy(argv + argc, con->leftovers, sizeof(*argv) * con->numLeftovers);
argc += con->numLeftovers;
}
argv[argc] = NULL;
#if defined(hpux) || defined(__hpux)
rc = setresgid(getgid(), getgid(),-1);
if (rc) goto exit;
rc = setresuid(getuid(), getuid(),-1);
if (rc) goto exit;
#else
/*
* XXX " ... on BSD systems setuid() should be preferred over setreuid()"
* XXX sez' Timur Bakeyev <[email protected]>
* XXX from Norbert Warmuth <[email protected]>
*/
#if defined(HAVE_SETUID)
rc = setgid(getgid());
if (rc) goto exit;
rc = setuid(getuid());
if (rc) goto exit;
#elif defined (HAVE_SETREUID)
rc = setregid(getgid(), getgid());
if (rc) goto exit;
rc = setreuid(getuid(), getuid());
if (rc) goto exit;
#else
; /* Can't drop privileges */
#endif
#endif
#ifdef MYDEBUG
if (_popt_debug)
{ poptArgv avp;
fprintf(stderr, "==> execvp(%s) argv[%d]:", argv[0], argc);
for (avp = argv; *avp; avp++)
fprintf(stderr, " '%s'", *avp);
fprintf(stderr, "\n");
}
#endif
/*@-nullstate@*/
rc = execvp(argv[0], (char *const *)argv);
/*@=nullstate@*/
exit:
if (argv) {
if (argv[0])
free((void *)argv[0]);
free(argv);
}
return ec;
}
/*@observer@*/ /*@null@*/
static const struct poptOption *
findOption(const struct poptOption * opt,
/*@null@*/ const char * longName, size_t longNameLen,
char shortName,
/*@null@*/ /*@out@*/ poptCallbackType * callback,
/*@null@*/ /*@out@*/ const void ** callbackData,
unsigned int argInfo)
/*@modifies *callback, *callbackData */
{
const struct poptOption * cb = NULL;
poptArg cbarg = { .ptr = NULL };
/* This happens when a single - is given */
if (LF_ISSET(ONEDASH) && !shortName && (longName && *longName == '\0'))
shortName = '-';
for (; opt->longName || opt->shortName || opt->arg; opt++) {
poptArg arg = { .ptr = opt->arg };
switch (poptArgType(opt)) {
case POPT_ARG_INCLUDE_TABLE: /* Recurse on included sub-tables. */
{ const struct poptOption * opt2;
poptSubstituteHelpI18N(arg.opt); /* XXX side effects */
if (arg.ptr == NULL) continue; /* XXX program error */
opt2 = findOption(arg.opt, longName, longNameLen, shortName, callback,
callbackData, argInfo);
if (opt2 == NULL) continue;
/* Sub-table data will be inheirited if no data yet. */
/*@-observertrans -dependenttrans @*/
if (callback && *callback
&& callbackData && *callbackData == NULL)
*callbackData = opt->descrip;
/*@=observertrans =dependenttrans @*/
return opt2;
} /*@notreached@*/ /*@switchbreak@*/ break;
case POPT_ARG_CALLBACK:
cb = opt;
cbarg.ptr = opt->arg;
continue;
/*@notreached@*/ /*@switchbreak@*/ break;
default:
/*@switchbreak@*/ break;
}
if (longName != NULL && opt->longName != NULL &&
(!LF_ISSET(ONEDASH) || F_ISSET(opt, ONEDASH)) &&
longOptionStrcmp(opt, longName, longNameLen))
{
break;
} else if (shortName && shortName == opt->shortName) {
break;
}
}
if (opt->longName == NULL && !opt->shortName)
return NULL;
/*@-modobserver -mods @*/
if (callback)
*callback = (cb ? cbarg.cb : NULL);
if (callbackData)
/*@-observertrans -dependenttrans @*/
*callbackData = (cb && !CBF_ISSET(cb, INC_DATA) ? cb->descrip : NULL);
/*@=observertrans =dependenttrans @*/
/*@=modobserver =mods @*/
return opt;
}
static const char * findNextArg(/*@special@*/ poptContext con,
unsigned argx, int delete_arg)
/*@uses con->optionStack, con->os,
con->os->next, con->os->argb, con->os->argc, con->os->argv @*/
/*@modifies con @*/
{
struct optionStackEntry * os = con->os;
const char * arg;
do {
int i;
arg = NULL;
while (os->next == os->argc && os > con->optionStack) os--;
if (os->next == os->argc && os == con->optionStack) break;
if (os->argv != NULL)
for (i = os->next; i < os->argc; i++) {
/*@-sizeoftype@*/
if (os->argb && PBM_ISSET(i, os->argb))
/*@innercontinue@*/ continue;
if (*os->argv[i] == '-')
/*@innercontinue@*/ continue;
if (--argx > 0)
/*@innercontinue@*/ continue;
arg = os->argv[i];
if (delete_arg) {
if (os->argb == NULL) os->argb = PBM_ALLOC(os->argc);
if (os->argb != NULL) /* XXX can't happen */
PBM_SET(i, os->argb);
}
/*@innerbreak@*/ break;
/*@=sizeoftype@*/
}
if (os > con->optionStack) os--;
} while (arg == NULL);
return arg;
}
static /*@only@*/ /*@null@*/ const char *
expandNextArg(/*@special@*/ poptContext con, const char * s)
/*@uses con->optionStack, con->os,
con->os->next, con->os->argb, con->os->argc, con->os->argv @*/
/*@modifies con @*/
{
const char * a = NULL;
char *t, *te;
size_t tn = strlen(s) + 1;
char c;
te = t = malloc(tn);
if (t == NULL) return NULL; /* XXX can't happen */
*t = '\0';
while ((c = *s++) != '\0') {
switch (c) {
#if 0 /* XXX can't do this */
case '\\': /* escape */
c = *s++;
/*@switchbreak@*/ break;
#endif
case '!':
if (!(s[0] == '#' && s[1] == ':' && s[2] == '+'))
/*@switchbreak@*/ break;
/* XXX Make sure that findNextArg deletes only next arg. */
if (a == NULL) {
if ((a = findNextArg(con, 1U, 1)) == NULL)
/*@switchbreak@*/ break;
}
s += sizeof("#:+") - 1;
tn += strlen(a);
{ size_t pos = (size_t) (te - t);
if ((t = realloc(t, tn)) == NULL) /* XXX can't happen */
return NULL;
te = stpcpy(t + pos, a);
}
continue;
/*@notreached@*/ /*@switchbreak@*/ break;
default:
/*@switchbreak@*/ break;
}
*te++ = c;
}
*te++ = '\0';
/* If the new string is longer than needed, shorten. */
if ((t + tn) > te) {
/*@-usereleased@*/ /* XXX splint can't follow the pointers. */
if ((te = realloc(t, (size_t)(te - t))) == NULL)
free(t);
t = te;
/*@=usereleased@*/
}
return t;
}
static void poptStripArg(/*@special@*/ poptContext con, int which)
/*@uses con->optionStack @*/
/*@defines con->arg_strip @*/
/*@modifies con @*/
{
/*@-compdef -sizeoftype -usedef @*/
if (con->arg_strip == NULL)
con->arg_strip = PBM_ALLOC(con->optionStack[0].argc);
if (con->arg_strip != NULL) /* XXX can't happen */
PBM_SET(which, con->arg_strip);
return;
/*@=compdef =sizeoftype =usedef @*/
}
/*@unchecked@*/
unsigned int _poptBitsN = _POPT_BITS_N;
/*@unchecked@*/
unsigned int _poptBitsM = _POPT_BITS_M;
/*@unchecked@*/
unsigned int _poptBitsK = _POPT_BITS_K;
/*@-sizeoftype@*/
static int _poptBitsNew(/*@null@*/ poptBits *bitsp)
/*@globals _poptBitsN, _poptBitsM, _poptBitsK @*/
/*@modifies *bitsp, _poptBitsN, _poptBitsM, _poptBitsK @*/
{
if (bitsp == NULL)
return POPT_ERROR_NULLARG;
/* XXX handle negated initialization. */
if (*bitsp == NULL) {
if (_poptBitsN == 0) {
_poptBitsN = _POPT_BITS_N;
_poptBitsM = _POPT_BITS_M;
}
if (_poptBitsM == 0U) _poptBitsM = (3 * _poptBitsN) / 2;
if (_poptBitsK == 0U || _poptBitsK > 32U) _poptBitsK = _POPT_BITS_K;
*bitsp = PBM_ALLOC(_poptBitsM-1);
}
/*@-nullstate@*/
return 0;
/*@=nullstate@*/
}
int poptBitsAdd(poptBits bits, const char * s)
{
size_t ns = (s ? strlen(s) : 0);
uint32_t h0 = 0;
uint32_t h1 = 0;
if (bits == NULL || ns == 0)
return POPT_ERROR_NULLARG;
poptJlu32lpair(s, ns, &h0, &h1);
for (ns = 0; ns < (size_t)_poptBitsK; ns++) {
uint32_t h = h0 + ns * h1;
uint32_t ix = (h % _poptBitsM);
PBM_SET(ix, bits);
}
return 0;
}
int poptBitsChk(poptBits bits, const char * s)
{
size_t ns = (s ? strlen(s) : 0);
uint32_t h0 = 0;
uint32_t h1 = 0;
int rc = 1;
if (bits == NULL || ns == 0)
return POPT_ERROR_NULLARG;
poptJlu32lpair(s, ns, &h0, &h1);
for (ns = 0; ns < (size_t)_poptBitsK; ns++) {
uint32_t h = h0 + ns * h1;
uint32_t ix = (h % _poptBitsM);
if (PBM_ISSET(ix, bits))
continue;
rc = 0;
break;
}
return rc;
}
int poptBitsClr(poptBits bits)
{
static size_t nbw = (__PBM_NBITS/8);
size_t nw = (__PBM_IX(_poptBitsM-1) + 1);
if (bits == NULL)
return POPT_ERROR_NULLARG;
memset(bits, 0, nw * nbw);
return 0;
}
int poptBitsDel(poptBits bits, const char * s)
{
size_t ns = (s ? strlen(s) : 0);
uint32_t h0 = 0;
uint32_t h1 = 0;
if (bits == NULL || ns == 0)
return POPT_ERROR_NULLARG;
poptJlu32lpair(s, ns, &h0, &h1);
for (ns = 0; ns < (size_t)_poptBitsK; ns++) {
uint32_t h = h0 + ns * h1;
uint32_t ix = (h % _poptBitsM);
PBM_CLR(ix, bits);
}
return 0;
}
int poptBitsIntersect(poptBits *ap, const poptBits b)
{
__pbm_bits *abits;
__pbm_bits *bbits;
__pbm_bits rc = 0;
size_t nw = (__PBM_IX(_poptBitsM-1) + 1);
size_t i;
if (ap == NULL || b == NULL || _poptBitsNew(ap))
return POPT_ERROR_NULLARG;
abits = __PBM_BITS(*ap);
bbits = __PBM_BITS(b);
for (i = 0; i < nw; i++) {
abits[i] &= bbits[i];
rc |= abits[i];
}
return (rc ? 1 : 0);
}
int poptBitsUnion(poptBits *ap, const poptBits b)
{
__pbm_bits *abits;
__pbm_bits *bbits;
__pbm_bits rc = 0;
size_t nw = (__PBM_IX(_poptBitsM-1) + 1);
size_t i;
if (ap == NULL || b == NULL || _poptBitsNew(ap))
return POPT_ERROR_NULLARG;
abits = __PBM_BITS(*ap);
bbits = __PBM_BITS(b);
for (i = 0; i < nw; i++) {
abits[i] |= bbits[i];
rc |= abits[i];
}
return (rc ? 1 : 0);
}
int poptBitsArgs(poptContext con, poptBits *ap)
{
const char ** av;
int rc = 0;
if (con == NULL || ap == NULL || _poptBitsNew(ap) ||
con->leftovers == NULL || con->numLeftovers == con->nextLeftover)
return POPT_ERROR_NULLARG;
/* some apps like [like RPM ;-) ] need this NULL terminated */
con->leftovers[con->numLeftovers] = NULL;
for (av = con->leftovers + con->nextLeftover; *av != NULL; av++) {
if ((rc = poptBitsAdd(*ap, *av)) != 0)
break;
}
/*@-nullstate@*/
return rc;
/*@=nullstate@*/
}
int poptSaveBits(poptBits * bitsp,
/*@unused@*/ UNUSED(unsigned int argInfo), const char * s)
{
char *tbuf = NULL;
char *t, *te;
int rc = 0;
if (bitsp == NULL || s == NULL || *s == '\0' || _poptBitsNew(bitsp))
return POPT_ERROR_NULLARG;
/* Parse comma separated attributes. */
te = tbuf = xstrdup(s);
while ((t = te) != NULL && *t) {
while (*te != '\0' && *te != ',')
te++;
if (*te != '\0')
*te++ = '\0';
/* XXX Ignore empty strings. */
if (*t == '\0')
continue;
/* XXX Permit negated attributes. caveat emptor: false negatives. */
if (*t == '!') {
t++;
if ((rc = poptBitsChk(*bitsp, t)) > 0)
rc = poptBitsDel(*bitsp, t);
} else
rc = poptBitsAdd(*bitsp, t);
if (rc)
break;
}
tbuf = _free(tbuf);
return rc;
}
/*@=sizeoftype@*/
int poptSaveString(const char *** argvp,
/*@unused@*/ UNUSED(unsigned int argInfo), const char * val)
{
int argc = 0;
if (argvp == NULL || val == NULL)
return POPT_ERROR_NULLARG;
/* XXX likely needs an upper bound on argc. */
if (*argvp != NULL)
while ((*argvp)[argc] != NULL)
argc++;
/*@-unqualifiedtrans -nullstate@*/ /* XXX no annotation for (*argvp) */
if ((*argvp = xrealloc(*argvp, (argc + 1 + 1) * sizeof(**argvp))) != NULL) {
(*argvp)[argc++] = xstrdup(val);
(*argvp)[argc ] = NULL;
}
return 0;
/*@=unqualifiedtrans =nullstate@*/
}
/*@unchecked@*/
static unsigned int seed = 0;
int poptSaveLongLong(long long * arg, unsigned int argInfo, long long aLongLong)
{
if (arg == NULL
#ifdef NOTYET
/* XXX Check alignment, may fail on funky platforms. */
|| (((unsigned long long)arg) & (sizeof(*arg)-1))
#endif
)
return POPT_ERROR_NULLARG;
if (aLongLong != 0 && LF_ISSET(RANDOM)) {
#if defined(HAVE_SRANDOM)
if (!seed) {
srandom((unsigned)getpid());
srandom((unsigned)random());
}
aLongLong = (long long)(random() % (aLongLong > 0 ? aLongLong : -aLongLong));
aLongLong++;
#else
/* XXX avoid adding POPT_ERROR_UNIMPLEMENTED to minimize i18n churn. */
return POPT_ERROR_BADOPERATION;
#endif
}