pkgcraft 0.0.31

library of Gentoo functionality
Documentation
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
use std::borrow::Borrow;
use std::collections::HashSet;
use std::hash::{Hash, Hasher};
use std::ops::Deref;
use std::str::FromStr;
use std::sync::LazyLock;
use std::{cmp, fmt};

use indexmap::IndexSet;

use super::get_build_mut;
use super::phase::PhaseKind;
use super::scope::EbuildScope;

mod _new;
mod _phases;
mod _query_cmd;
mod _use_conf;
mod adddeny;
mod addpredict;
mod addread;
mod addwrite;
mod assert;
mod best_version;
mod command_not_found_handle;
mod debug_print;
mod debug_print_function;
mod debug_print_section;
mod default;
mod default_pkg_nofetch;
mod default_src_compile;
mod default_src_configure;
mod default_src_install;
mod default_src_prepare;
mod default_src_test;
mod default_src_unpack;
mod die;
mod diropts;
mod dobin;
mod docinto;
mod docompress;
mod doconfd;
mod dodir;
mod dodoc;
mod doenvd;
mod doexe;
mod doheader;
mod dohtml;
mod doinfo;
mod doinitd;
mod doins;
mod dolib;
mod dolib_a;
mod dolib_so;
mod doman;
mod domo;
mod dosbin;
mod dostrip;
mod dosym;
mod eapply;
mod eapply_user;
mod ebegin;
pub(crate) mod econf;
mod edo;
mod eend;
mod eerror;
mod einfo;
mod einfon;
mod einstall;
pub(super) mod einstalldocs;
mod elog;
mod emake;
mod eqawarn;
mod ewarn;
mod exeinto;
mod exeopts;
mod export_functions;
mod fowners;
mod fperms;
mod get_libdir;
mod has;
mod has_version;
mod hasq;
mod hasv;
mod in_iuse;
mod inherit;
mod insinto;
mod insopts;
mod into;
mod keepdir;
mod libopts;
mod newbin;
mod newconfd;
mod newdoc;
mod newenvd;
mod newexe;
mod newheader;
mod newinitd;
mod newins;
mod newlib_a;
mod newlib_so;
mod newman;
mod newsbin;
mod nonfatal;
mod pipestatus;
mod unpack;
mod use_;
mod use_enable;
mod use_with;
mod useq;
mod usev;
mod usex;
mod ver_cut;
mod ver_replacing;
mod ver_rs;
mod ver_test;

// export command builtins for internal use
pub(crate) mod builtins {
    pub(crate) use super::adddeny::BUILTIN as adddeny;
    pub(crate) use super::addpredict::BUILTIN as addpredict;
    pub(crate) use super::addread::BUILTIN as addread;
    pub(crate) use super::addwrite::BUILTIN as addwrite;
    pub(crate) use super::assert::BUILTIN as assert;
    pub(crate) use super::best_version::BUILTIN as best_version;
    pub(crate) use super::command_not_found_handle::BUILTIN as command_not_found_handle;
    pub(crate) use super::debug_print::BUILTIN as debug_print;
    pub(crate) use super::debug_print_function::BUILTIN as debug_print_function;
    pub(crate) use super::debug_print_section::BUILTIN as debug_print_section;
    pub(crate) use super::default::BUILTIN as default;
    pub(crate) use super::default_pkg_nofetch::BUILTIN as default_pkg_nofetch;
    pub(crate) use super::default_src_compile::BUILTIN as default_src_compile;
    pub(crate) use super::default_src_configure::BUILTIN as default_src_configure;
    pub(crate) use super::default_src_install::BUILTIN as default_src_install;
    pub(crate) use super::default_src_prepare::BUILTIN as default_src_prepare;
    pub(crate) use super::default_src_test::BUILTIN as default_src_test;
    pub(crate) use super::default_src_unpack::BUILTIN as default_src_unpack;
    pub(crate) use super::die::BUILTIN as die;
    pub(crate) use super::diropts::BUILTIN as diropts;
    pub(crate) use super::dobin::BUILTIN as dobin;
    pub(crate) use super::docinto::BUILTIN as docinto;
    pub(crate) use super::docompress::BUILTIN as docompress;
    pub(crate) use super::doconfd::BUILTIN as doconfd;
    pub(crate) use super::dodir::BUILTIN as dodir;
    pub(crate) use super::dodoc::BUILTIN as dodoc;
    pub(crate) use super::doenvd::BUILTIN as doenvd;
    pub(crate) use super::doexe::BUILTIN as doexe;
    pub(crate) use super::doheader::BUILTIN as doheader;
    pub(crate) use super::dohtml::BUILTIN as dohtml;
    pub(crate) use super::doinfo::BUILTIN as doinfo;
    pub(crate) use super::doinitd::BUILTIN as doinitd;
    pub(crate) use super::doins::BUILTIN as doins;
    pub(crate) use super::dolib::BUILTIN as dolib;
    pub(crate) use super::dolib_a::BUILTIN as dolib_a;
    pub(crate) use super::dolib_so::BUILTIN as dolib_so;
    pub(crate) use super::doman::BUILTIN as doman;
    pub(crate) use super::domo::BUILTIN as domo;
    pub(crate) use super::dosbin::BUILTIN as dosbin;
    pub(crate) use super::dostrip::BUILTIN as dostrip;
    pub(crate) use super::dosym::BUILTIN as dosym;
    pub(crate) use super::eapply::BUILTIN as eapply;
    pub(crate) use super::eapply_user::BUILTIN as eapply_user;
    pub(crate) use super::ebegin::BUILTIN as ebegin;
    pub(crate) use super::econf::BUILTIN as econf;
    pub(crate) use super::edo::BUILTIN as edo;
    pub(crate) use super::eend::BUILTIN as eend;
    pub(crate) use super::eerror::BUILTIN as eerror;
    pub(crate) use super::einfo::BUILTIN as einfo;
    pub(crate) use super::einfon::BUILTIN as einfon;
    pub(crate) use super::einstall::BUILTIN as einstall;
    pub(crate) use super::einstalldocs::BUILTIN as einstalldocs;
    pub(crate) use super::elog::BUILTIN as elog;
    pub(crate) use super::emake::BUILTIN as emake;
    pub(crate) use super::eqawarn::BUILTIN as eqawarn;
    pub(crate) use super::ewarn::BUILTIN as ewarn;
    pub(crate) use super::exeinto::BUILTIN as exeinto;
    pub(crate) use super::exeopts::BUILTIN as exeopts;
    pub(crate) use super::export_functions::BUILTIN as export_functions;
    pub(crate) use super::fowners::BUILTIN as fowners;
    pub(crate) use super::fperms::BUILTIN as fperms;
    pub(crate) use super::get_libdir::BUILTIN as get_libdir;
    pub(crate) use super::has::BUILTIN as has;
    pub(crate) use super::has_version::BUILTIN as has_version;
    pub(crate) use super::hasq::BUILTIN as hasq;
    pub(crate) use super::hasv::BUILTIN as hasv;
    pub(crate) use super::in_iuse::BUILTIN as in_iuse;
    pub(crate) use super::inherit::BUILTIN as inherit;
    pub(crate) use super::insinto::BUILTIN as insinto;
    pub(crate) use super::insopts::BUILTIN as insopts;
    pub(crate) use super::into::BUILTIN as into;
    pub(crate) use super::keepdir::BUILTIN as keepdir;
    pub(crate) use super::libopts::BUILTIN as libopts;
    pub(crate) use super::newbin::BUILTIN as newbin;
    pub(crate) use super::newconfd::BUILTIN as newconfd;
    pub(crate) use super::newdoc::BUILTIN as newdoc;
    pub(crate) use super::newenvd::BUILTIN as newenvd;
    pub(crate) use super::newexe::BUILTIN as newexe;
    pub(crate) use super::newheader::BUILTIN as newheader;
    pub(crate) use super::newinitd::BUILTIN as newinitd;
    pub(crate) use super::newins::BUILTIN as newins;
    pub(crate) use super::newlib_a::BUILTIN as newlib_a;
    pub(crate) use super::newlib_so::BUILTIN as newlib_so;
    pub(crate) use super::newman::BUILTIN as newman;
    pub(crate) use super::newsbin::BUILTIN as newsbin;
    pub(crate) use super::nonfatal::BUILTIN as nonfatal;
    pub(crate) use super::pipestatus::BUILTIN as pipestatus;
    pub(crate) use super::unpack::BUILTIN as unpack;
    pub(crate) use super::use_::BUILTIN as use_;
    pub(crate) use super::use_enable::BUILTIN as use_enable;
    pub(crate) use super::use_with::BUILTIN as use_with;
    pub(crate) use super::useq::BUILTIN as useq;
    pub(crate) use super::usev::BUILTIN as usev;
    pub(crate) use super::usex::BUILTIN as usex;
    pub(crate) use super::ver_cut::BUILTIN as ver_cut;
    pub(crate) use super::ver_replacing::BUILTIN as ver_replacing;
    pub(crate) use super::ver_rs::BUILTIN as ver_rs;
    pub(crate) use super::ver_test::BUILTIN as ver_test;
    // phase stubs
    pub(crate) use super::_phases::PKG_CONFIG as pkg_config;
    pub(crate) use super::_phases::PKG_INFO as pkg_info;
    pub(crate) use super::_phases::PKG_NOFETCH as pkg_nofetch;
    pub(crate) use super::_phases::PKG_POSTINST as pkg_postinst;
    pub(crate) use super::_phases::PKG_POSTRM as pkg_postrm;
    pub(crate) use super::_phases::PKG_PREINST as pkg_preinst;
    pub(crate) use super::_phases::PKG_PRERM as pkg_prerm;
    pub(crate) use super::_phases::PKG_PRETEND as pkg_pretend;
    pub(crate) use super::_phases::PKG_SETUP as pkg_setup;
    pub(crate) use super::_phases::SRC_COMPILE as src_compile;
    pub(crate) use super::_phases::SRC_CONFIGURE as src_configure;
    pub(crate) use super::_phases::SRC_INSTALL as src_install;
    pub(crate) use super::_phases::SRC_PREPARE as src_prepare;
    pub(crate) use super::_phases::SRC_TEST as src_test;
    pub(crate) use super::_phases::SRC_UNPACK as src_unpack;
}

// export command functions for internal use
#[allow(unused_imports)]
pub(crate) mod functions {
    pub(crate) use super::adddeny::run as adddeny;
    pub(crate) use super::addpredict::run as addpredict;
    pub(crate) use super::addread::run as addread;
    pub(crate) use super::addwrite::run as addwrite;
    pub(crate) use super::assert::run as assert;
    pub(crate) use super::best_version::run as best_version;
    pub(crate) use super::debug_print::run as debug_print;
    pub(crate) use super::debug_print_function::run as debug_print_function;
    pub(crate) use super::debug_print_section::run as debug_print_section;
    pub(crate) use super::default::run as default;
    pub(crate) use super::default_pkg_nofetch::run as default_pkg_nofetch;
    pub(crate) use super::default_src_compile::run as default_src_compile;
    pub(crate) use super::default_src_configure::run as default_src_configure;
    pub(crate) use super::default_src_install::run as default_src_install;
    pub(crate) use super::default_src_prepare::run as default_src_prepare;
    pub(crate) use super::default_src_test::run as default_src_test;
    pub(crate) use super::default_src_unpack::run as default_src_unpack;
    pub(crate) use super::die::run as die;
    pub(crate) use super::diropts::run as diropts;
    pub(crate) use super::dobin::run as dobin;
    pub(crate) use super::docinto::run as docinto;
    pub(crate) use super::docompress::run as docompress;
    pub(crate) use super::doconfd::run as doconfd;
    pub(crate) use super::dodir::run as dodir;
    pub(crate) use super::dodoc::run as dodoc;
    pub(crate) use super::doenvd::run as doenvd;
    pub(crate) use super::doexe::run as doexe;
    pub(crate) use super::doheader::run as doheader;
    pub(crate) use super::dohtml::run as dohtml;
    pub(crate) use super::doinfo::run as doinfo;
    pub(crate) use super::doinitd::run as doinitd;
    pub(crate) use super::doins::run as doins;
    pub(crate) use super::dolib::run as dolib;
    pub(crate) use super::dolib_a::run as dolib_a;
    pub(crate) use super::dolib_so::run as dolib_so;
    pub(crate) use super::doman::run as doman;
    pub(crate) use super::domo::run as domo;
    pub(crate) use super::dosbin::run as dosbin;
    pub(crate) use super::dostrip::run as dostrip;
    pub(crate) use super::dosym::run as dosym;
    pub(crate) use super::eapply::run as eapply;
    pub(crate) use super::eapply_user::run as eapply_user;
    pub(crate) use super::ebegin::run as ebegin;
    pub(crate) use super::econf::run as econf;
    pub(crate) use super::edo::run as edo;
    pub(crate) use super::eend::run as eend;
    pub(crate) use super::eerror::run as eerror;
    pub(crate) use super::einfo::run as einfo;
    pub(crate) use super::einfon::run as einfon;
    pub(crate) use super::einstall::run as einstall;
    pub(crate) use super::einstalldocs::run as einstalldocs;
    pub(crate) use super::elog::run as elog;
    pub(crate) use super::emake::run as emake;
    pub(crate) use super::eqawarn::run as eqawarn;
    pub(crate) use super::ewarn::run as ewarn;
    pub(crate) use super::exeinto::run as exeinto;
    pub(crate) use super::exeopts::run as exeopts;
    pub(crate) use super::export_functions::run as export_functions;
    pub(crate) use super::fowners::run as fowners;
    pub(crate) use super::fperms::run as fperms;
    pub(crate) use super::get_libdir::run as get_libdir;
    pub(crate) use super::has::run as has;
    pub(crate) use super::has_version::run as has_version;
    pub(crate) use super::hasq::run as hasq;
    pub(crate) use super::hasv::run as hasv;
    pub(crate) use super::in_iuse::run as in_iuse;
    pub(crate) use super::inherit::run as inherit;
    pub(crate) use super::insinto::run as insinto;
    pub(crate) use super::insopts::run as insopts;
    pub(crate) use super::into::run as into;
    pub(crate) use super::keepdir::run as keepdir;
    pub(crate) use super::libopts::run as libopts;
    pub(crate) use super::newbin::run as newbin;
    pub(crate) use super::newconfd::run as newconfd;
    pub(crate) use super::newdoc::run as newdoc;
    pub(crate) use super::newenvd::run as newenvd;
    pub(crate) use super::newexe::run as newexe;
    pub(crate) use super::newheader::run as newheader;
    pub(crate) use super::newinitd::run as newinitd;
    pub(crate) use super::newins::run as newins;
    pub(crate) use super::newlib_a::run as newlib_a;
    pub(crate) use super::newlib_so::run as newlib_so;
    pub(crate) use super::newman::run as newman;
    pub(crate) use super::newsbin::run as newsbin;
    pub(crate) use super::nonfatal::run as nonfatal;
    pub(crate) use super::pipestatus::run as pipestatus;
    pub(crate) use super::unpack::run as unpack;
    pub(crate) use super::use_::run as use_;
    pub(crate) use super::use_enable::run as use_enable;
    pub(crate) use super::use_with::run as use_with;
    pub(crate) use super::useq::run as useq;
    pub(crate) use super::usev::run as usev;
    pub(crate) use super::usex::run as usex;
    pub(crate) use super::ver_cut::run as ver_cut;
    pub(crate) use super::ver_replacing::run as ver_replacing;
    pub(crate) use super::ver_rs::run as ver_rs;
    pub(crate) use super::ver_test::run as ver_test;
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, Clone)]
pub(crate) struct Builtin(scallop::builtins::Builtin);

impl Builtin {
    pub(crate) fn allowed_in<I>(self, scopes: I) -> Command
    where
        I: IntoIterator,
        I::Item: Into<EbuildScope>,
    {
        Command {
            builtin: self,
            allowed: scopes.into_iter().map(Into::into).collect(),
            die_on_failure: true,
        }
    }
}

impl From<&Builtin> for scallop::builtins::Builtin {
    fn from(value: &Builtin) -> Self {
        value.0
    }
}

impl fmt::Display for Builtin {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

// TODO: replace with callable trait implementation if it's ever stabilized
// https://github.com/rust-lang/rust/issues/29625
impl Deref for Builtin {
    type Target = scallop::builtins::Builtin;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

#[derive(Debug, Clone)]
pub struct Command {
    builtin: Builtin,
    pub allowed: HashSet<EbuildScope>,
    pub die_on_failure: bool,
}

impl PartialEq for Command {
    fn eq(&self, other: &Self) -> bool {
        self.builtin == other.builtin
    }
}

impl Eq for Command {}

impl Hash for Command {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.builtin.hash(state);
    }
}

impl Ord for Command {
    fn cmp(&self, other: &Self) -> cmp::Ordering {
        self.builtin.cmp(&other.builtin)
    }
}

impl PartialOrd for Command {
    fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Borrow<Builtin> for Command {
    fn borrow(&self) -> &Builtin {
        &self.builtin
    }
}

impl Borrow<str> for Command {
    fn borrow(&self) -> &str {
        self.builtin.0.borrow()
    }
}

impl AsRef<str> for Command {
    fn as_ref(&self) -> &str {
        self.builtin.0.as_ref()
    }
}

impl fmt::Display for Command {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.builtin)
    }
}

// TODO: replace with callable trait implementation if it's ever stabilized
// https://github.com/rust-lang/rust/issues/29625
impl Deref for Command {
    type Target = scallop::builtins::Builtin;

    fn deref(&self) -> &Self::Target {
        &self.builtin.0
    }
}

impl Command {
    ///  Explicitly set if a command calls `die` on failure.
    pub(crate) fn die(mut self, value: bool) -> Self {
        self.die_on_failure = value;
        self
    }

    /// Determine if the command is allowed in a given `Scope`.
    pub fn is_allowed<T>(&self, value: &T) -> bool
    where
        EbuildScope: PartialEq<T>,
    {
        self.allowed.iter().any(|x| x == value)
    }

    /// Determine if the command is a phase stub.
    pub fn is_phase(&self) -> bool {
        PhaseKind::from_str(self.as_ref()).is_ok()
    }
}

/// Try to parse the arguments for a given command.
trait TryParseArgs: Sized {
    fn try_parse_args(args: &[&str]) -> scallop::Result<Self>;
}

impl<P: clap::Parser> TryParseArgs for P {
    fn try_parse_args(args: &[&str]) -> scallop::Result<Self> {
        let cmd = Self::command();
        let name = cmd.get_name();

        // HACK: work around clap parsing always treating -- as a delimiter
        // See https://github.com/clap-rs/clap/issues/5055.
        let args = RawArgsIter {
            args: args.iter(),
            injected_arg: None,
            seen: false,
        };

        let args = [name].into_iter().chain(args);
        Self::try_parse_from(args).map_err(|e| scallop::Error::Base(format!("{name}: {e}")))
    }
}

struct RawArgsIter<'a> {
    args: std::slice::Iter<'a, &'a str>,
    injected_arg: Option<&'a str>,
    seen: bool,
}

impl<'a> Iterator for RawArgsIter<'a> {
    type Item = &'a str;

    fn next(&mut self) -> Option<Self::Item> {
        self.injected_arg.take().or_else(|| {
            self.args.next().copied().inspect(|x| {
                if *x == "--" && !self.seen {
                    let _ = self.injected_arg.insert(x);
                    self.seen = true;
                }
            })
        })
    }
}

/// Ordered set of all known builtins.
pub(crate) static BUILTINS: LazyLock<IndexSet<Builtin>> = LazyLock::new(|| {
    [
        builtins::adddeny,
        builtins::addpredict,
        builtins::addread,
        builtins::addwrite,
        builtins::assert,
        builtins::best_version,
        builtins::command_not_found_handle,
        builtins::debug_print,
        builtins::debug_print_function,
        builtins::debug_print_section,
        builtins::default,
        builtins::default_pkg_nofetch,
        builtins::default_src_compile,
        builtins::default_src_configure,
        builtins::default_src_install,
        builtins::default_src_prepare,
        builtins::default_src_test,
        builtins::default_src_unpack,
        builtins::die,
        builtins::diropts,
        builtins::dobin,
        builtins::docinto,
        builtins::docompress,
        builtins::doconfd,
        builtins::dodir,
        builtins::dodoc,
        builtins::doenvd,
        builtins::doexe,
        builtins::doheader,
        builtins::dohtml,
        builtins::doinfo,
        builtins::doinitd,
        builtins::doins,
        builtins::dolib,
        builtins::dolib_a,
        builtins::dolib_so,
        builtins::doman,
        builtins::domo,
        builtins::dosbin,
        builtins::dostrip,
        builtins::dosym,
        builtins::eapply,
        builtins::eapply_user,
        builtins::ebegin,
        builtins::econf,
        builtins::edo,
        builtins::eend,
        builtins::eerror,
        builtins::einfo,
        builtins::einfon,
        builtins::einstall,
        builtins::einstalldocs,
        builtins::elog,
        builtins::emake,
        builtins::eqawarn,
        builtins::ewarn,
        builtins::exeinto,
        builtins::exeopts,
        builtins::export_functions,
        builtins::fowners,
        builtins::fperms,
        builtins::get_libdir,
        builtins::has,
        builtins::has_version,
        builtins::hasq,
        builtins::hasv,
        builtins::in_iuse,
        builtins::inherit,
        builtins::insinto,
        builtins::insopts,
        builtins::into,
        builtins::keepdir,
        builtins::libopts,
        builtins::newbin,
        builtins::newconfd,
        builtins::newdoc,
        builtins::newenvd,
        builtins::newexe,
        builtins::newheader,
        builtins::newinitd,
        builtins::newins,
        builtins::newlib_a,
        builtins::newlib_so,
        builtins::newman,
        builtins::newsbin,
        builtins::nonfatal,
        builtins::pipestatus,
        builtins::unpack,
        builtins::use_,
        builtins::use_enable,
        builtins::use_with,
        builtins::useq,
        builtins::usev,
        builtins::usex,
        builtins::ver_cut,
        builtins::ver_replacing,
        builtins::ver_rs,
        builtins::ver_test,
        // phase stubs
        builtins::pkg_config,
        builtins::pkg_info,
        builtins::pkg_nofetch,
        builtins::pkg_postinst,
        builtins::pkg_postrm,
        builtins::pkg_preinst,
        builtins::pkg_prerm,
        builtins::pkg_pretend,
        builtins::pkg_setup,
        builtins::src_compile,
        builtins::src_configure,
        builtins::src_install,
        builtins::src_prepare,
        builtins::src_test,
        builtins::src_unpack,
    ]
    .into_iter()
    .collect()
});

/// USE flag variant for `use` and `usev` commands.
#[derive(Debug, Clone)]
struct UseFlag {
    flag: String,
    inverted: bool,
}

impl FromStr for UseFlag {
    type Err = crate::Error;

    fn from_str(s: &str) -> crate::Result<Self> {
        let (inverted, flag) = s.strip_prefix('!').map(|x| (true, x)).unwrap_or((false, s));

        crate::dep::parse::use_flag(flag).map(|value| Self {
            flag: value.to_string(),
            inverted,
        })
    }
}

peg::parser! {
    grammar cmd() for str {
        rule version_separator() -> &'input str
            = s:$([^ 'a'..='z' | 'A'..='Z' | '0'..='9']+) { s }

        rule version_component() -> &'input str
            = s:$(['0'..='9']+) { s }
            / s:$(['a'..='z' | 'A'..='Z']+) { s }

        rule version_element() -> [&'input str; 2]
            = sep:version_separator() comp:version_component()?
            { [sep, comp.unwrap_or_default()] }
            / sep:version_separator()? comp:version_component()
            { [sep.unwrap_or_default(), comp] }

        // Split version strings for ver_rs and ver_cut.
        pub(super) rule version_split() -> Vec<&'input str>
            = vals:version_element()* { vals.into_iter().flatten().collect() }

        // Parse ranges for ver_rs and ver_cut.
        pub(super) rule range(max: usize) -> (usize, usize)
            = start:$(['0'..='9']+) "-" end:$(['0'..='9']+) {?
                if let (Ok(start), Ok(end)) = (start.parse(), end.parse()) {
                    Ok((start, end))
                } else {
                    Err("range value overflow")
                }
            } / start:$(['0'..='9']+) "-" {?
                match start.parse() {
                    Ok(start) if start <= max => Ok((start, max)),
                    Ok(start) => Ok((start, start)),
                    _ => Err("range value overflow"),
                }
            } / start:$(['0'..='9']+) {?
                let start = start.parse().map_err(|_| "range value overflow")?;
                Ok((start, start))
            }
    }
}

// provide public parsing functionality while converting error types
mod parse {
    use crate::Error;
    use crate::error::peg_error;

    use super::cmd;

    pub(super) fn version_split(s: &str) -> crate::Result<Vec<&str>> {
        cmd::version_split(s).map_err(|e| peg_error("invalid version string", s, e))
    }

    pub(super) fn range(s: &str, max: usize) -> crate::Result<(usize, usize)> {
        let (start, end) = cmd::range(s, max).map_err(|e| peg_error("invalid range", s, e))?;
        if end < start {
            return Err(Error::InvalidValue(format!(
                "start of range ({start}) is greater than end ({end})",
            )));
        }
        Ok((start, end))
    }
}

/// Run a command given its name and argument list from bash.
fn run(name: &str, args: *mut scallop::bash::WordList) -> scallop::ExecStatus {
    use scallop::builtins::handle_error;
    use scallop::{Error, traits::IntoWords};

    let build = get_build_mut();
    let eapi = build.eapi();
    let scope = &build.scope;

    // run if enabled for the current build state
    let result = match eapi.commands().get(name) {
        Some(cmd) if cmd.is_allowed(scope) => {
            // convert raw command args into &str
            let args = args.to_words();
            let args: Result<Vec<_>, _> = args.into_iter().collect();
            // run command if args are valid utf8
            match args {
                Ok(args) => cmd.call(&args),
                Err(e) => Err(Error::Base(format!("invalid args: {e}"))),
            }
        }
        Some(_) => Err(Error::Base(format!("disabled in {scope} scope"))),
        None => Err(Error::Base(format!("disabled in EAPI {eapi}"))),
    };

    // handle errors, bailing out when running normally
    result.unwrap_or_else(|e| match e {
        Error::Base(s) if !build.nonfatal => handle_error(name, Error::Bail(s)),
        _ => handle_error(name, e),
    })
}

/// Create a static [`Builtin`] object for registry in bash.
#[macro_export]
macro_rules! make_builtin {
    ($name:expr, $func_name:ident) => {
        make_builtin!($name, $func_name, BUILTIN, "");
    };
    ($name:expr, $func_name:ident, $builtin:ident) => {
        make_builtin!($name, $func_name, $builtin, "");
    };
    ($name:expr, $func_name:ident, $builtin:ident, $usage:expr) => {
        #[unsafe(no_mangle)]
        extern "C" fn $func_name(args: *mut scallop::bash::WordList) -> std::ffi::c_int {
            i32::from($crate::shell::commands::run($name, args))
        }

        pub(crate) static $builtin: $crate::shell::commands::Builtin =
            $crate::shell::commands::Builtin(scallop::builtins::Builtin {
                name: $name,
                func: run,
                cfunc: $func_name,
                flags: scallop::builtins::Attr::ENABLED.bits(),
                help: "",
                usage: $usage,
            });
    };
}
use make_builtin;

#[cfg(test)]
fn assert_invalid_args(cmd: scallop::builtins::BuiltinFn, nums: &[u32]) {
    for n in nums {
        let args: Vec<_> = (0..*n).map(|n| n.to_string()).collect();
        let args: Vec<_> = args.iter().map(|s| s.as_str()).collect();
        let re = format!("^.*, got {n}");
        crate::test::assert_err_re!(cmd(&args), re);
    }
}

#[cfg(test)]
fn assert_invalid_cmd(cmd: scallop::builtins::BuiltinFn, nums: &[u32]) {
    for n in nums {
        let args: Vec<_> = (0..*n).map(|n| n.to_string()).collect();
        let args: Vec<_> = args.iter().map(|s| s.as_str()).collect();
        assert!(cmd(&args).is_err());
    }
}

#[cfg(test)]
macro_rules! cmd_scope_tests {
    ($cmd:expr) => {
        #[test]
        fn cmd_scope() {
            use std::collections::HashSet;

            use itertools::Itertools;

            use crate::config::Config;
            use crate::eapi::EAPIS_OFFICIAL;
            use crate::pkg::Source;
            use crate::repo::ebuild::EbuildRepoBuilder;
            use crate::shell::scope::{EbuildScope, Scope};
            use crate::test::assert_err_re;

            let cmd = $cmd;
            let mut args = cmd.split(' ').peekable();
            let name = args.next().unwrap();
            let has_args = args.peek().is_some();
            let invalid_cmd = [name]
                .into_iter()
                .chain(args.map(|_| r#"$'\x02\xc5\xd8'"#))
                .join(" ");
            let non_utf8_args_err = format!("{name}: error: invalid args: invalid utf-8 .*");
            let mut config = Config::default();
            let mut temp = EbuildRepoBuilder::new().build().unwrap();
            // create eclasses
            let eclass = indoc::formatdoc! {r#"
                # stub eclass
                VAR=1
                {cmd}
                VAR=2
            "#};
            temp.create_eclass("e1", &eclass).unwrap();
            let eclass = indoc::formatdoc! {r#"
                # stub eclass
                {invalid_cmd}
            "#};
            temp.create_eclass("invalid", &eclass).unwrap();
            let repo = config.add_repo(&temp).unwrap().into_ebuild().unwrap();
            config.finalize().unwrap();
            let all_scopes: HashSet<_> = EbuildScope::All.into_iter().collect();

            for eapi in &*EAPIS_OFFICIAL {
                if let Some(cmd) = eapi.commands().get(name) {
                    let scopes: HashSet<_> =
                        cmd.allowed.iter().flat_map(|x| x.iter()).collect();
                    // test non-utf8 args for commands that accept arguments
                    if has_args {
                        for scope in &scopes {
                            let info = format!("EAPI={eapi}, scope: {scope}");
                            match scope {
                                Scope::Eclass(_) => {
                                    let data = indoc::formatdoc! {r#"
                                        EAPI={eapi}
                                        inherit invalid
                                        DESCRIPTION="testing eclass scope invalid args"
                                        SLOT=0
                                    "#};
                                    temp.create_ebuild_from_str("cat/pkg-1", &data).unwrap();
                                    let raw_pkg = repo.get_pkg_raw("cat/pkg-1").unwrap();
                                    let r = raw_pkg.source();
                                    assert_err_re!(r, non_utf8_args_err, &info);
                                }
                                Scope::Global => {
                                    let data = indoc::formatdoc! {r#"
                                        EAPI={eapi}
                                        DESCRIPTION="testing global scope failures"
                                        SLOT=0
                                        VAR=1
                                        {invalid_cmd}
                                        VAR=2
                                    "#};
                                    temp.create_ebuild_from_str("cat/pkg-1", &data).unwrap();
                                    let raw_pkg = repo.get_pkg_raw("cat/pkg-1").unwrap();
                                    let r = raw_pkg.source();
                                    assert_err_re!(r, non_utf8_args_err, &info);
                                }
                                Scope::Phase(phase) => {
                                    let data = indoc::formatdoc! {r#"
                                        EAPI={eapi}
                                        DESCRIPTION="testing phase scope failures"
                                        SLOT=0
                                        VAR=1
                                        {phase}() {{
                                            {invalid_cmd}
                                            VAR=2
                                        }}
                                    "#};
                                    temp.create_ebuild_from_str("cat/pkg-1", &data).unwrap();
                                    let pkg = repo.get_pkg("cat/pkg-1").unwrap();
                                    pkg.source().unwrap();
                                    let phase = eapi.phases().get(phase).unwrap();
                                    let r = phase.run();
                                    assert_err_re!(r, non_utf8_args_err, &info);
                                }
                            }
                        }
                    }

                    // test invalid scope usage
                    for scope in all_scopes.difference(&scopes) {
                        let info = format!("EAPI={eapi}, scope: {scope}");
                        match scope {
                            Scope::Eclass(_) => {
                                let data = indoc::formatdoc! {r#"
                                    EAPI={eapi}
                                    inherit e1
                                    DESCRIPTION="testing eclass scope failures"
                                    SLOT=0
                                "#};
                                temp.create_ebuild_from_str("cat/pkg-1", &data).unwrap();
                                let raw_pkg = repo.get_pkg_raw("cat/pkg-1").unwrap();
                                let r = raw_pkg.source();
                                // verify sourcing stops at unknown command
                                assert_eq!(scallop::variables::optional("VAR").unwrap(), "1");
                                // verify error output
                                let err = format!("{name}: error: disabled in eclass scope");
                                assert_err_re!(r, err, &info);
                            }
                            Scope::Global => {
                                let data = indoc::formatdoc! {r#"
                                    EAPI={eapi}
                                    DESCRIPTION="testing global scope failures"
                                    SLOT=0
                                    VAR=1
                                    {cmd}
                                    VAR=2
                                "#};
                                temp.create_ebuild_from_str("cat/pkg-1", &data).unwrap();
                                let raw_pkg = repo.get_pkg_raw("cat/pkg-1").unwrap();
                                let r = raw_pkg.source();
                                // verify sourcing stops at unknown command
                                assert_eq!(scallop::variables::optional("VAR").unwrap(), "1");
                                // verify error output
                                let err = format!("{name}: error: disabled in global scope");
                                assert_err_re!(r, err, &info);
                            }
                            Scope::Phase(phase) => {
                                let data = indoc::formatdoc! {r#"
                                    EAPI={eapi}
                                    DESCRIPTION="testing phase scope failures"
                                    SLOT=0
                                    VAR=1
                                    {phase}() {{
                                        {cmd}
                                        VAR=2
                                    }}
                                "#};
                                temp.create_ebuild_from_str("cat/pkg-1", &data).unwrap();
                                let pkg = repo.get_pkg("cat/pkg-1").unwrap();
                                pkg.source().unwrap();
                                let phase = eapi.phases().get(phase).unwrap();
                                let r = phase.run();
                                // verify function stops at unknown command
                                assert_eq!(
                                    scallop::variables::optional("VAR").as_deref(),
                                    Some("1")
                                );
                                // verify error output
                                let err = format!("{name}: error: disabled in {phase} scope");
                                assert_err_re!(r, err, &info);
                            }
                        }
                    }
                } else {
                    let data = indoc::formatdoc! {r#"
                        EAPI={eapi}
                        DESCRIPTION="testing command disabled in EAPI failures"
                        SLOT=0
                        VAR=1
                        {cmd}
                        VAR=2
                    "#};
                    temp.create_ebuild_from_str("cat/pkg-1", &data).unwrap();
                    let raw_pkg = repo.get_pkg_raw("cat/pkg-1").unwrap();
                    let r = raw_pkg.source();
                    // verify sourcing stops at unknown command
                    assert_eq!(scallop::variables::optional("VAR").unwrap(), "1");
                    // verify error output
                    let err = format!("{name}: error: disabled in EAPI {eapi}");
                    assert_err_re!(r, err);
                }
            }
        }
    };
}
#[cfg(test)]
use cmd_scope_tests;

#[cfg(test)]
mod tests {
    use crate::eapi::EAPI_LATEST_OFFICIAL;

    #[test]
    fn command_traits() {
        let dobin = EAPI_LATEST_OFFICIAL.commands().get("dobin").unwrap();
        assert_eq!(dobin.to_string(), "dobin");
        assert!(format!("{dobin:?}").contains("dobin"));

        let dodir = EAPI_LATEST_OFFICIAL.commands().get("dodir").unwrap();
        assert!(dobin != dodir);
        assert!(dobin < dodir);
    }
}