bashkit 0.20.1

Awesomely fast virtual sandbox with bash and file system
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
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
//! Sort and uniq builtins - sort lines and filter duplicates
//!
//! Decision: `sort` mirrors GNU coreutils `sort.c` semantics in the C locale
//! (byte comparison): fields keep their leading blanks unless `-b`, a key
//! with any ordering option does not inherit global ones, `-u`/`-s` disable
//! the whole-line last-resort comparison, and `-u` keeps the first line of
//! each equal run of a stable sort. Character positions in `-k` count UTF-8
//! characters (not bytes) so keys never split a multibyte character.

use std::cmp::Ordering;

use async_trait::async_trait;

use super::{Builtin, Context, read_text_file};
use crate::error::Result;
use crate::fs::vfs_join;
use crate::interpreter::ExecResult;

/// The sort builtin - sort lines of text.
///
/// Usage: sort [OPTION]... [FILE]...
///
/// Ordering options (global or per `-k` key): `-b -d -f -g -h -i -M -n -R -r -V`.
/// Other options: `-c -C -m -o FILE -s -t SEP -k KEYDEF -u -z`, plus the
/// GNU long forms. `-S`, `-T`, `--parallel`, `--compress-program` and
/// `--batch-size` are accepted and ignored (no external resources).
pub struct Sort;

/// Ordering options shared by the global key and per-field keys.
#[derive(Clone, Default, Debug)]
struct KeyOpts {
    skip_sblanks: bool,
    skip_eblanks: bool,
    dictionary: bool,
    nonprinting: bool,
    fold: bool,
    numeric: bool,
    general: bool,
    human: bool,
    month: bool,
    version: bool,
    random: bool,
    reverse: bool,
}

impl KeyOpts {
    /// GNU `default_key_compare`: no ordering option other than `-r`.
    fn is_default(&self) -> bool {
        !(self.skip_sblanks
            || self.skip_eblanks
            || self.dictionary
            || self.nonprinting
            || self.fold
            || self.numeric
            || self.general
            || self.human
            || self.month
            || self.version
            || self.random)
    }

    /// Apply one ordering letter. `end` selects which side `b` applies to
    /// (`None` = both, as for a global option).
    fn set(&mut self, c: char, end: Option<bool>) -> bool {
        match c {
            'b' => match end {
                None => {
                    self.skip_sblanks = true;
                    self.skip_eblanks = true;
                }
                Some(false) => self.skip_sblanks = true,
                Some(true) => self.skip_eblanks = true,
            },
            'd' => self.dictionary = true,
            'f' => self.fold = true,
            'g' => self.general = true,
            'h' => self.human = true,
            'i' => self.nonprinting = true,
            'M' => self.month = true,
            'n' => self.numeric = true,
            'R' => self.random = true,
            'r' => self.reverse = true,
            'V' => self.version = true,
            _ => return false,
        }
        true
    }
}

/// A parsed sort key definition from `-k KEYDEF`.
/// Format: `F[.C][OPTS][,F[.C][OPTS]]`. Words and chars are 0-based here.
#[derive(Clone, Debug)]
struct KeySpec {
    sword: usize,
    schar: usize,
    /// `None` = key extends to end of line.
    eword: Option<usize>,
    /// 0 = end of the end field.
    echar: usize,
    opts: KeyOpts,
}

impl KeySpec {
    /// Parse a KEYDEF string like "2", "2,3", "2.3,3.4", "2n,2", "2nr".
    fn parse(spec: &str) -> std::result::Result<Self, String> {
        let bad = |why: &str| format!("sort: invalid field specification '{spec}': {why}\n");
        let (start, end) = match spec.split_once(',') {
            Some((s, e)) => (s, Some(e)),
            None => (spec, None),
        };
        let mut opts = KeyOpts::default();

        let (f, c, rest) = split_pos(start);
        let f: usize = f
            .parse()
            .map_err(|_| bad("invalid number at field start"))?;
        if f == 0 {
            return Err(bad("field number is zero"));
        }
        let schar = match c {
            Some(c) => {
                let c: usize = c.parse().map_err(|_| bad("invalid number after '.'"))?;
                if c == 0 {
                    return Err(bad("character offset is zero"));
                }
                c - 1
            }
            None => 0,
        };
        for ch in rest.chars() {
            if !opts.set(ch, Some(false)) {
                return Err(format!("sort: stray character in field spec: '{spec}'\n"));
            }
        }

        let (eword, echar) = match end {
            None => (None, 0),
            Some(e) => {
                let (f, c, rest) = split_pos(e);
                let f: usize = f.parse().map_err(|_| bad("invalid number after ','"))?;
                if f == 0 {
                    return Err(bad("field number is zero"));
                }
                let echar = match c {
                    Some(c) => c.parse().map_err(|_| bad("invalid number after '.'"))?,
                    None => 0,
                };
                for ch in rest.chars() {
                    if !opts.set(ch, Some(true)) {
                        return Err(format!("sort: stray character in field spec: '{spec}'\n"));
                    }
                }
                (Some(f - 1), echar)
            }
        };

        Ok(KeySpec {
            sword: f - 1,
            schar,
            eword,
            echar,
            opts,
        })
    }

    /// The global options as a whole-line key.
    fn whole_line(opts: KeyOpts) -> Self {
        KeySpec {
            sword: 0,
            schar: 0,
            eword: None,
            echar: 0,
            opts,
        }
    }
}

/// Split "F[.C]OPTS" into (F, Some(C), OPTS).
fn split_pos(s: &str) -> (&str, Option<&str>, &str) {
    let f_end = s.find(|c: char| !c.is_ascii_digit()).unwrap_or(s.len());
    let (f, rest) = s.split_at(f_end);
    if let Some(after) = rest.strip_prefix('.') {
        let c_end = after
            .find(|c: char| !c.is_ascii_digit())
            .unwrap_or(after.len());
        (f, Some(&after[..c_end]), &after[c_end..])
    } else {
        (f, None, rest)
    }
}

fn is_blank(c: char) -> bool {
    c == ' ' || c == '\t' || c == '\n'
}

fn skip_blanks(s: &str, mut pos: usize) -> usize {
    while let Some(c) = s[pos..].chars().next() {
        if !is_blank(c) {
            break;
        }
        pos += c.len_utf8();
    }
    pos
}

fn skip_nonblanks(s: &str, mut pos: usize) -> usize {
    while let Some(c) = s[pos..].chars().next() {
        if is_blank(c) {
            break;
        }
        pos += c.len_utf8();
    }
    pos
}

/// Position just after the next `tab` (or end of line) from `pos`.
fn next_tab(s: &str, pos: usize, tab: char) -> usize {
    s[pos..].find(tab).map(|i| pos + i).unwrap_or(s.len())
}

fn advance_chars(s: &str, pos: usize, n: usize) -> usize {
    s[pos..]
        .char_indices()
        .nth(n)
        .map(|(i, _)| pos + i)
        .unwrap_or(s.len())
}

/// GNU `begfield`: byte offset where the key starts.
fn begfield(line: &str, tab: Option<char>, key: &KeySpec) -> usize {
    let lim = line.len();
    let mut ptr = 0;
    let mut sword = key.sword;
    while ptr < lim && sword > 0 {
        sword -= 1;
        match tab {
            Some(t) => {
                ptr = next_tab(line, ptr, t);
                if ptr < lim {
                    ptr += t.len_utf8();
                }
            }
            None => ptr = skip_nonblanks(line, skip_blanks(line, ptr)),
        }
    }
    if key.opts.skip_sblanks {
        ptr = skip_blanks(line, ptr);
    }
    advance_chars(line, ptr, key.schar)
}

/// GNU `limfield`: byte offset where the key ends.
fn limfield(line: &str, tab: Option<char>, key: &KeySpec) -> usize {
    let lim = line.len();
    let Some(mut eword) = key.eword else {
        return lim;
    };
    let echar = key.echar;
    if echar == 0 {
        eword += 1;
    }
    let mut ptr = 0;
    while ptr < lim && eword > 0 {
        eword -= 1;
        match tab {
            Some(t) => {
                ptr = next_tab(line, ptr, t);
                if ptr < lim && (eword > 0 || echar > 0) {
                    ptr += t.len_utf8();
                }
            }
            None => ptr = skip_nonblanks(line, skip_blanks(line, ptr)),
        }
    }
    if echar != 0 {
        if key.opts.skip_eblanks {
            ptr = skip_blanks(line, ptr);
        }
        ptr = advance_chars(line, ptr, echar);
    }
    ptr
}

fn key_text<'a>(line: &'a str, tab: Option<char>, key: &KeySpec) -> &'a [u8] {
    let beg = begfield(line, tab, key);
    let lim = limfield(line, tab, key).max(beg);
    &line.as_bytes()[beg..lim]
}

fn skip_blank_bytes(s: &[u8]) -> &[u8] {
    let n = s
        .iter()
        .take_while(|&&b| b == b' ' || b == b'\t' || b == b'\n')
        .count();
    &s[n..]
}

/// Decimal number as (negative, integer digits, fraction digits), with
/// insignificant zeros stripped so magnitudes compare exactly.
fn parse_decimal(s: &[u8]) -> (bool, &[u8], &[u8]) {
    let s = skip_blank_bytes(s);
    let (neg, s) = match s.first() {
        Some(b'-') => (true, &s[1..]),
        _ => (false, s),
    };
    let int_len = s.iter().take_while(|b| b.is_ascii_digit()).count();
    let int = &s[..int_len];
    let int = &int[int.iter().take_while(|&&b| b == b'0').count()..];
    let mut frac: &[u8] = &[];
    if s.get(int_len) == Some(&b'.') {
        let f = &s[int_len + 1..];
        let flen = f.iter().take_while(|b| b.is_ascii_digit()).count();
        let f = &f[..flen];
        let trail = f.iter().rev().take_while(|&&b| b == b'0').count();
        frac = &f[..flen - trail];
    }
    let zero = int.is_empty() && frac.is_empty();
    (neg && !zero, int, frac)
}

/// GNU `numcompare`/`strnumcmp`: exact decimal comparison, `-` sign only.
fn numcompare(a: &[u8], b: &[u8]) -> Ordering {
    let (an, ai, af) = parse_decimal(a);
    let (bn, bi, bf) = parse_decimal(b);
    if an != bn {
        return if an {
            Ordering::Less
        } else {
            Ordering::Greater
        };
    }
    let mag = ai
        .len()
        .cmp(&bi.len())
        .then_with(|| ai.cmp(bi))
        .then_with(|| af.cmp(bf));
    if an { mag.reverse() } else { mag }
}

/// GNU `find_unit_order` for `-h`.
fn unit_order(s: &[u8]) -> i32 {
    let s = skip_blank_bytes(s);
    let (neg, s) = match s.first() {
        Some(b'-') => (true, &s[1..]),
        _ => (false, s),
    };
    let mut i = 0;
    let mut nonzero = false;
    while i < s.len() && s[i].is_ascii_digit() {
        nonzero |= s[i] != b'0';
        i += 1;
    }
    if s.get(i) == Some(&b'.') {
        i += 1;
        while i < s.len() && s[i].is_ascii_digit() {
            nonzero |= s[i] != b'0';
            i += 1;
        }
    }
    if !nonzero {
        return 0;
    }
    let order = match s.get(i) {
        Some(b'K') | Some(b'k') => 1,
        Some(b'M') => 2,
        Some(b'G') => 3,
        Some(b'T') => 4,
        Some(b'P') => 5,
        Some(b'E') => 6,
        Some(b'Z') => 7,
        Some(b'Y') => 8,
        Some(b'R') => 9,
        Some(b'Q') => 10,
        _ => 0,
    };
    if neg { -order } else { order }
}

fn human_compare(a: &[u8], b: &[u8]) -> Ordering {
    unit_order(a)
        .cmp(&unit_order(b))
        .then_with(|| numcompare(a, b))
}

/// `strtod`-style prefix parse: `None` when no number could be converted.
fn strtod_prefix(s: &[u8]) -> Option<f64> {
    strtod(s).map(|(v, _)| v)
}

/// C `strtod` on a byte prefix: the value and how many bytes it consumed
/// (leading blanks, sign, decimal or hex-float digits, `inf`/`infinity`/`nan`).
pub(super) fn strtod(s: &[u8]) -> Option<(f64, usize)> {
    let mut i = s
        .iter()
        .take_while(|b| matches!(b, b' ' | b'\t' | b'\n' | b'\r' | 0x0b | 0x0c))
        .count();
    let mut neg = false;
    if let Some(&c) = s.get(i)
        && (c == b'+' || c == b'-')
    {
        neg = c == b'-';
        i += 1;
    }
    let rest = &s[i..];
    let lower: Vec<u8> = rest
        .iter()
        .take(3)
        .map(|b| b.to_ascii_lowercase())
        .collect();
    let sign = if neg { -1.0 } else { 1.0 };
    if lower == b"inf" {
        let long = rest.len() >= 8 && rest[..8].eq_ignore_ascii_case(b"infinity");
        return Some((sign * f64::INFINITY, i + if long { 8 } else { 3 }));
    }
    if lower == b"nan" {
        return Some((f64::NAN, i + 3));
    }
    let hex_digit = |b: Option<&u8>| b.is_some_and(|b| b.is_ascii_hexdigit());
    if rest.len() > 2
        && rest[0] == b'0'
        && (rest[1] == b'x' || rest[1] == b'X')
        && (hex_digit(rest.get(2)) || (rest.get(2) == Some(&b'.') && hex_digit(rest.get(3))))
    {
        let mut j = 2;
        let mut val = 0.0f64;
        while let Some(d) = rest.get(j).and_then(|b| (*b as char).to_digit(16)) {
            val = val * 16.0 + d as f64;
            j += 1;
        }
        if rest.get(j) == Some(&b'.') {
            j += 1;
            let mut scale = 1.0 / 16.0;
            while let Some(d) = rest.get(j).and_then(|b| (*b as char).to_digit(16)) {
                val += d as f64 * scale;
                scale /= 16.0;
                j += 1;
            }
        }
        if matches!(rest.get(j), Some(b'p') | Some(b'P')) {
            let mut k = j + 1;
            let mut eneg = false;
            if let Some(&c) = rest.get(k)
                && (c == b'+' || c == b'-')
            {
                eneg = c == b'-';
                k += 1;
            }
            let ds = rest[k..].iter().take_while(|b| b.is_ascii_digit()).count();
            if ds > 0 {
                let e: i32 = std::str::from_utf8(&rest[k..k + ds])
                    .ok()
                    .and_then(|t| t.parse().ok())
                    .unwrap_or(i32::MAX);
                val *= 2f64.powi(if eneg { -e } else { e });
                j = k + ds;
            }
        }
        return Some((sign * val, i + j));
    }
    let mut j = rest.iter().take_while(|b| b.is_ascii_digit()).count();
    let mut digits = j;
    if rest.get(j) == Some(&b'.') {
        let f = rest[j + 1..]
            .iter()
            .take_while(|b| b.is_ascii_digit())
            .count();
        digits += f;
        j += 1 + f;
    }
    if digits == 0 {
        return None;
    }
    if matches!(rest.get(j), Some(b'e') | Some(b'E')) {
        let mut k = j + 1;
        if matches!(rest.get(k), Some(b'+') | Some(b'-')) {
            k += 1;
        }
        let ds = rest[k..].iter().take_while(|b| b.is_ascii_digit()).count();
        if ds > 0 {
            j = k + ds;
        }
    }
    let text = std::str::from_utf8(&rest[..j]).ok()?;
    let text = text.strip_suffix('.').unwrap_or(text);
    let text = if text.starts_with('.') {
        format!("0{text}")
    } else {
        text.to_string()
    };
    text.parse::<f64>().ok().map(|v| (sign * v, i + j))
}

/// GNU `general_numcompare`: conversion errors < NaN < numbers.
fn general_compare(a: &[u8], b: &[u8]) -> Ordering {
    match (strtod_prefix(a), strtod_prefix(b)) {
        (None, None) => Ordering::Equal,
        (None, Some(_)) => Ordering::Less,
        (Some(_), None) => Ordering::Greater,
        (Some(x), Some(y)) => match (x.is_nan(), y.is_nan()) {
            (true, true) => Ordering::Equal,
            (true, false) => Ordering::Less,
            (false, true) => Ordering::Greater,
            _ => x.partial_cmp(&y).unwrap_or(Ordering::Equal),
        },
    }
}

/// Month number (1-12) when the blank-trimmed text starts with an
/// abbreviated month name, else 0.
fn month_ordinal(s: &[u8]) -> u32 {
    const MONTHS: [&[u8; 3]; 12] = [
        b"JAN", b"FEB", b"MAR", b"APR", b"MAY", b"JUN", b"JUL", b"AUG", b"SEP", b"OCT", b"NOV",
        b"DEC",
    ];
    let s = skip_blank_bytes(s);
    if s.len() < 3 {
        return 0;
    }
    let head = [
        s[0].to_ascii_uppercase(),
        s[1].to_ascii_uppercase(),
        s[2].to_ascii_uppercase(),
    ];
    MONTHS
        .iter()
        .position(|m| **m == head)
        .map(|i| i as u32 + 1)
        .unwrap_or(0)
}

/// gnulib `file_prefixlen`: length without the trailing
/// `(\.[A-Za-z~][A-Za-z0-9~]*)*` suffix.
fn file_prefixlen(s: &[u8]) -> usize {
    let n = s.len();
    let mut prefixlen = 0;
    let mut i = 0;
    loop {
        if i == n {
            return prefixlen;
        }
        i += 1;
        prefixlen = i;
        while i + 1 < n && s[i] == b'.' && (s[i + 1].is_ascii_alphabetic() || s[i + 1] == b'~') {
            i += 2;
            while i < n && (s[i].is_ascii_alphanumeric() || s[i] == b'~') {
                i += 1;
            }
        }
    }
}

fn ver_order(s: &[u8], pos: usize) -> i32 {
    match s.get(pos) {
        None => -1,
        Some(c) if c.is_ascii_digit() => 0,
        Some(c) if c.is_ascii_alphabetic() => *c as i32,
        Some(b'~') => -2,
        Some(c) => *c as i32 + 256,
    }
}

/// dpkg-style `verrevcmp` used by gnulib `filevercmp`.
fn verrevcmp(a: &[u8], b: &[u8]) -> Ordering {
    let (mut i, mut j) = (0, 0);
    while i < a.len() || j < b.len() {
        let mut first_diff = 0i32;
        while (i < a.len() && !a[i].is_ascii_digit()) || (j < b.len() && !b[j].is_ascii_digit()) {
            let (ac, bc) = (ver_order(a, i), ver_order(b, j));
            if ac != bc {
                return ac.cmp(&bc);
            }
            i += 1;
            j += 1;
        }
        while i < a.len() && a[i] == b'0' {
            i += 1;
        }
        while j < b.len() && b[j] == b'0' {
            j += 1;
        }
        while i < a.len() && j < b.len() && a[i].is_ascii_digit() && b[j].is_ascii_digit() {
            if first_diff == 0 {
                first_diff = a[i] as i32 - b[j] as i32;
            }
            i += 1;
            j += 1;
        }
        if i < a.len() && a[i].is_ascii_digit() {
            return Ordering::Greater;
        }
        if j < b.len() && b[j].is_ascii_digit() {
            return Ordering::Less;
        }
        if first_diff != 0 {
            return first_diff.cmp(&0);
        }
    }
    Ordering::Equal
}

/// gnulib `filevercmp` (what `sort -V` uses).
fn filevercmp(a: &[u8], b: &[u8]) -> Ordering {
    match (a.is_empty(), b.is_empty()) {
        (true, true) => return Ordering::Equal,
        (true, false) => return Ordering::Less,
        (false, true) => return Ordering::Greater,
        _ => {}
    }
    if a[0] == b'.' {
        if b[0] != b'.' {
            return Ordering::Less;
        }
        let (adot, bdot) = (a == b".", b == b".");
        if adot || bdot {
            return bdot.cmp(&adot);
        }
        let (add, bdd) = (a == b"..", b == b"..");
        if add || bdd {
            return bdd.cmp(&add);
        }
    } else if b[0] == b'.' {
        return Ordering::Greater;
    }
    let (ap, bp) = (file_prefixlen(a), file_prefixlen(b));
    let one_pass = ap == a.len() && bp == b.len();
    let r = verrevcmp(&a[..ap], &b[..bp]);
    if r != Ordering::Equal || one_pass {
        r
    } else {
        verrevcmp(a, b)
    }
}

/// Compare two strings using version sort order (`sort -V`).
#[cfg(test)]
fn version_cmp(a: &str, b: &str) -> Ordering {
    filevercmp(a.as_bytes(), b.as_bytes())
}

struct SortConfig {
    keys: Vec<KeySpec>,
    tab: Option<char>,
    reverse: bool,
    unique: bool,
    stable: bool,
    random: std::collections::hash_map::RandomState,
}

impl SortConfig {
    fn compare_key(&self, a: &[u8], b: &[u8], o: &KeyOpts) -> Ordering {
        let transform = |s: &[u8]| -> Vec<u8> {
            s.iter()
                .filter(|&&c| {
                    (!o.dictionary || c.is_ascii_alphanumeric() || c == b' ' || c == b'\t')
                        && (!o.nonprinting || (0x20..0x7f).contains(&c))
                })
                .map(|&c| if o.fold { c.to_ascii_uppercase() } else { c })
                .collect()
        };
        let (ta, tb);
        let (a, b) = if o.dictionary || o.nonprinting || o.fold {
            ta = transform(a);
            tb = transform(b);
            (&ta[..], &tb[..])
        } else {
            (a, b)
        };
        if o.numeric {
            numcompare(a, b)
        } else if o.general {
            general_compare(a, b)
        } else if o.human {
            human_compare(a, b)
        } else if o.month {
            month_ordinal(a).cmp(&month_ordinal(b))
        } else if o.random {
            use std::hash::BuildHasher;
            self.random
                .hash_one(a)
                .cmp(&self.random.hash_one(b))
                .then_with(|| a.cmp(b))
        } else if o.version {
            filevercmp(a, b)
        } else {
            a.cmp(b)
        }
    }

    fn compare(&self, a: &str, b: &str) -> Ordering {
        if !self.keys.is_empty() {
            for key in &self.keys {
                let ka = key_text(a, self.tab, key);
                let kb = key_text(b, self.tab, key);
                let d = self.compare_key(ka, kb, &key.opts);
                let d = if key.opts.reverse { d.reverse() } else { d };
                if d != Ordering::Equal {
                    return d;
                }
            }
            if self.unique || self.stable {
                return Ordering::Equal;
            }
        }
        let d = a.as_bytes().cmp(b.as_bytes());
        if self.reverse { d.reverse() } else { d }
    }
}

/// Split input into records: empty records are kept (they sort first), only
/// the final terminator is dropped, and empty input has no records.
fn split_records(text: &str, sep: char) -> impl Iterator<Item = &str> {
    let body = text.strip_suffix(sep).unwrap_or(text);
    (!text.is_empty())
        .then(|| body.split(sep))
        .into_iter()
        .flatten()
}

enum CheckMode {
    Off,
    Diagnose,
    Quiet,
}

const SORT_HELP: &str = "Usage: sort [OPTION]... [FILE]...\nWrite sorted concatenation of all FILE(s) to standard output.\n\n  -b\t\tignore leading blanks\n  -d\t\tconsider only blanks and alphanumeric characters\n  -f\t\tfold lower case to upper case characters\n  -g\t\tcompare according to general numerical value\n  -i\t\tconsider only printable characters\n  -M\t\tcompare (unknown) < 'JAN' < ... < 'DEC'\n  -h\t\tcompare human readable numbers (e.g., 2K 1G)\n  -n\t\tcompare according to string numerical value\n  -R\t\tshuffle, but group identical keys\n  -r\t\treverse the result of comparisons\n  -V\t\tnatural sort of (version) numbers within text\n  --sort=WORD\tsort according to WORD\n  -c, -C\tcheck for sorted input\n  -k KEYDEF\tsort via a key definition\n  -m\t\tmerge already sorted files\n  -o FILE\twrite output to FILE\n  -s\t\tstable sort\n  -t SEP\tuse SEP as field separator\n  -u\t\toutput only the first of an equal run\n  -z\t\tline delimiter is NUL, not newline\n  --help\tdisplay this help and exit\n  --version\toutput version information and exit\n";

fn sort_err(msg: impl Into<String>) -> ExecResult {
    ExecResult::err(msg.into(), 2)
}

#[async_trait]
impl Builtin for Sort {
    async fn execute(&self, ctx: Context<'_>) -> Result<ExecResult> {
        if let Some(r) = super::check_help_version(ctx.args, SORT_HELP, Some("sort (bashkit) 0.1"))
        {
            return Ok(r);
        }
        let mut gkey = KeyOpts::default();
        let mut unique = false;
        let mut stable = false;
        let mut check = CheckMode::Off;
        let mut merge = false;
        let mut tab: Option<char> = None;
        let mut key_specs: Vec<KeySpec> = Vec::new();
        let mut output_file: Option<String> = None;
        let mut zero_terminated = false;
        let mut files: Vec<String> = Vec::new();

        #[allow(clippy::result_large_err)]
        let set_tab = |val: &str, tab: &mut Option<char>| -> std::result::Result<(), ExecResult> {
            let c = if val == "\\0" {
                '\0'
            } else {
                let mut it = val.chars();
                match (it.next(), it.next()) {
                    (None, _) => return Err(sort_err("sort: empty tab\n")),
                    (Some(c), None) => c,
                    _ => return Err(sort_err(format!("sort: multi-character tab '{val}'\n"))),
                }
            };
            *tab = Some(c);
            Ok(())
        };

        let args = ctx.args;
        let mut i = 0;
        let mut opts_done = false;
        while i < args.len() {
            let arg = args[i].as_str();
            i += 1;
            if opts_done || arg == "-" || !arg.starts_with('-') {
                files.push(arg.to_string());
                continue;
            }
            if arg == "--" {
                opts_done = true;
                continue;
            }
            if let Some(long) = arg.strip_prefix("--") {
                let (name, inline) = match long.split_once('=') {
                    Some((n, v)) => (n, Some(v.to_string())),
                    None => (long, None),
                };
                const LONGS: &[(&str, bool)] = &[
                    ("batch-size", true),
                    ("buffer-size", true),
                    ("check", false),
                    ("compress-program", true),
                    ("debug", false),
                    ("dictionary-order", false),
                    ("field-separator", true),
                    ("general-numeric-sort", false),
                    ("human-numeric-sort", false),
                    ("ignore-case", false),
                    ("ignore-leading-blanks", false),
                    ("ignore-nonprinting", false),
                    ("key", true),
                    ("merge", false),
                    ("month-sort", false),
                    ("numeric-sort", false),
                    ("output", true),
                    ("parallel", true),
                    ("random-sort", false),
                    ("random-source", true),
                    ("reverse", false),
                    ("sort", true),
                    ("stable", false),
                    ("temporary-directory", true),
                    ("unique", false),
                    ("version-sort", false),
                    ("zero-terminated", false),
                ];
                // Unambiguous prefixes are accepted, as getopt_long does.
                let matches: Vec<&(&str, bool)> = match LONGS.iter().find(|(n, _)| *n == name) {
                    Some(m) => vec![m],
                    None => LONGS.iter().filter(|(n, _)| n.starts_with(name)).collect(),
                };
                let &(lname, takes) = match matches.as_slice() {
                    [m] => *m,
                    [] => return Ok(super::invalid_option("sort", arg, 2)),
                    _ => {
                        return Ok(sort_err(format!("sort: option '--{name}' is ambiguous\n")));
                    }
                };
                let value = if takes {
                    match inline {
                        Some(v) => v,
                        None if i < args.len() => {
                            i += 1;
                            args[i - 1].clone()
                        }
                        None => {
                            return Ok(sort_err(format!(
                                "sort: option '--{lname}' requires an argument\n"
                            )));
                        }
                    }
                } else {
                    inline.clone().unwrap_or_default()
                };
                match lname {
                    "check" => {
                        check = match value.as_str() {
                            "" | "diagnose-first" => CheckMode::Diagnose,
                            "quiet" | "silent" => CheckMode::Quiet,
                            other => {
                                return Ok(sort_err(format!(
                                    "sort: invalid argument '{other}' for '--check'\n"
                                )));
                            }
                        }
                    }
                    "dictionary-order" => gkey.dictionary = true,
                    "field-separator" => {
                        if let Err(e) = set_tab(&value, &mut tab) {
                            return Ok(e);
                        }
                    }
                    "general-numeric-sort" => gkey.general = true,
                    "human-numeric-sort" => gkey.human = true,
                    "ignore-case" => gkey.fold = true,
                    "ignore-leading-blanks" => {
                        gkey.set('b', None);
                    }
                    "ignore-nonprinting" => gkey.nonprinting = true,
                    "key" => match KeySpec::parse(&value) {
                        Ok(k) => key_specs.push(k),
                        Err(e) => return Ok(sort_err(e)),
                    },
                    "merge" => merge = true,
                    "month-sort" => gkey.month = true,
                    "numeric-sort" => gkey.numeric = true,
                    "output" => output_file = Some(value),
                    "random-sort" => gkey.random = true,
                    "reverse" => gkey.reverse = true,
                    "sort" => {
                        // Unambiguous prefixes are accepted, like `--sort=num`.
                        const WORDS: [(&str, char); 6] = [
                            ("general-numeric", 'g'),
                            ("human-numeric", 'h'),
                            ("month", 'M'),
                            ("numeric", 'n'),
                            ("random", 'R'),
                            ("version", 'V'),
                        ];
                        let hits: Vec<char> = match WORDS.iter().find(|(w, _)| *w == value) {
                            Some((_, c)) => vec![*c],
                            None => WORDS
                                .iter()
                                .filter(|(w, _)| !value.is_empty() && w.starts_with(&*value))
                                .map(|(_, c)| *c)
                                .collect(),
                        };
                        match hits.as_slice() {
                            [c] => {
                                gkey.set(*c, None);
                            }
                            _ => {
                                return Ok(sort_err(format!(
                                    "sort: invalid argument '{value}' for '--sort'\n"
                                )));
                            }
                        }
                    }
                    "stable" => stable = true,
                    "unique" => unique = true,
                    "version-sort" => gkey.version = true,
                    "zero-terminated" => zero_terminated = true,
                    // Resource-tuning options have nothing to tune here.
                    _ => {}
                }
                continue;
            }
            // Bundle of short options.
            let body = &arg[1..];
            for (pos, c) in body.char_indices() {
                match c {
                    'k' | 'o' | 't' | 'S' | 'T' | 'y' => {
                        let attached = &body[pos + c.len_utf8()..];
                        let value = if !attached.is_empty() {
                            attached.to_string()
                        } else if c == 'y' {
                            String::new()
                        } else if i < args.len() {
                            i += 1;
                            args[i - 1].clone()
                        } else {
                            return Ok(sort_err(format!(
                                "sort: option requires an argument -- '{c}'\n"
                            )));
                        };
                        match c {
                            'k' => match KeySpec::parse(&value) {
                                Ok(k) => key_specs.push(k),
                                Err(e) => return Ok(sort_err(e)),
                            },
                            'o' => output_file = Some(value),
                            't' => {
                                if let Err(e) = set_tab(&value, &mut tab) {
                                    return Ok(e);
                                }
                            }
                            _ => {}
                        }
                        break;
                    }
                    'c' => check = CheckMode::Diagnose,
                    'C' => check = CheckMode::Quiet,
                    'm' => merge = true,
                    's' => stable = true,
                    'u' => unique = true,
                    'z' => zero_terminated = true,
                    c => {
                        if !gkey.set(c, None) {
                            return Ok(super::invalid_option("sort", &format!("-{c}"), 2));
                        }
                    }
                }
            }
        }

        // Keys without ordering options inherit the global ones (GNU rule).
        for key in &mut key_specs {
            if key.opts.is_default() && !key.opts.reverse {
                key.opts = gkey.clone();
            }
        }
        if key_specs.is_empty() && !gkey.is_default() {
            key_specs.push(KeySpec::whole_line(gkey.clone()));
        }
        let cfg = SortConfig {
            keys: key_specs,
            tab,
            reverse: gkey.reverse,
            unique,
            stable,
            random: std::collections::hash_map::RandomState::new(),
        };

        if files.is_empty() {
            files.push("-".to_string());
        }
        let line_sep = if zero_terminated { '\0' } else { '\n' };
        let mut streams: Vec<Vec<String>> = Vec::new();
        for file in &files {
            let text = if file == "-" {
                ctx.stdin.map(|s| s.to_string()).unwrap_or_default()
            } else {
                let path = if file.starts_with('/') {
                    std::path::PathBuf::from(file)
                } else {
                    vfs_join(ctx.cwd, file)
                };
                match read_text_file(&*ctx.fs, &path, "sort").await {
                    Ok(t) => t,
                    Err(mut e) => {
                        e.exit_code = 2;
                        return Ok(e);
                    }
                }
            };
            streams.push(split_records(&text, line_sep).map(str::to_string).collect());
        }

        match check {
            CheckMode::Off => {}
            mode => {
                let lines = streams.concat();
                for i in 1..lines.len() {
                    let d = cfg.compare(&lines[i - 1], &lines[i]);
                    if d == Ordering::Greater || (unique && d == Ordering::Equal) {
                        let stderr = match mode {
                            CheckMode::Diagnose => {
                                format!("sort: {}:{}: disorder: {}\n", files[0], i + 1, lines[i])
                            }
                            _ => String::new(),
                        };
                        return Ok(ExecResult::err(stderr, 1));
                    }
                }
                return Ok(ExecResult::ok(String::new()));
            }
        }

        let mut all_lines: Vec<String> = if merge {
            // k-way merge of pre-sorted inputs; ties go to the earlier input.
            let mut idx = vec![0usize; streams.len()];
            let mut merged = Vec::new();
            loop {
                let mut best: Option<usize> = None;
                for (s, stream) in streams.iter().enumerate() {
                    if idx[s] >= stream.len() {
                        continue;
                    }
                    best = match best {
                        Some(b)
                            if cfg.compare(&stream[idx[s]], &streams[b][idx[b]])
                                != Ordering::Less =>
                        {
                            Some(b)
                        }
                        _ => Some(s),
                    };
                }
                let Some(b) = best else { break };
                merged.push(std::mem::take(&mut streams[b][idx[b]]));
                idx[b] += 1;
            }
            merged
        } else {
            let mut v = streams.concat();
            // Stable so `-u` keeps the first input line of each equal run.
            v.sort_by(|a, b| cfg.compare(a, b));
            v
        };

        if unique {
            let mut out: Vec<String> = Vec::with_capacity(all_lines.len());
            for line in all_lines.drain(..) {
                if out
                    .last()
                    .is_none_or(|prev| cfg.compare(prev, &line) != Ordering::Equal)
                {
                    out.push(line);
                }
            }
            all_lines = out;
        }

        let sep = if zero_terminated { "\0" } else { "\n" };
        let mut output = all_lines.join(sep);
        if !all_lines.is_empty() {
            output.push_str(sep);
        }

        // Write to output file if -o specified
        if let Some(ref outfile) = output_file {
            let path = if outfile.starts_with('/') {
                std::path::PathBuf::from(outfile)
            } else {
                vfs_join(ctx.cwd, outfile)
            };
            if let Err(e) = ctx.fs.write_file(&path, output.as_bytes()).await {
                return Ok(sort_err(format!("sort: {}: {}\n", outfile, e)));
            }
            return Ok(ExecResult::ok(String::new()));
        }

        Ok(ExecResult::ok(output))
    }
}

/// The uniq builtin - report or omit repeated lines.
///
/// Usage: uniq [-cdDiuz] [-f N] [-s N] [-w N] [INPUT [OUTPUT]]
///
/// Options:
///   -c   Prefix lines by the number of occurrences
///   -d   Only print duplicate lines, one for each group
///   -D   Print all duplicate lines
///   -u   Only print unique lines
///   -i   Case insensitive comparison
///   -f N Skip N fields before comparing
///   -s N Skip N characters before comparing
///   -w N Compare no more than N characters
///   -z   Line delimiter is NUL, not newline
pub struct Uniq;

#[derive(Default)]
struct UniqKeyOpts {
    skip_fields: usize,
    skip_chars: usize,
    check_chars: Option<usize>,
    case_insensitive: bool,
}

/// Get the comparison key for a line: GNU skips N blank-then-nonblank
/// fields, then N characters, then compares at most `-w` characters.
fn uniq_key(line: &str, o: &UniqKeyOpts) -> String {
    let is_blank = |c: char| c == ' ' || c == '\t';
    let mut rest = line;
    for _ in 0..o.skip_fields {
        rest = rest.trim_start_matches(is_blank);
        rest = rest.trim_start_matches(|c| !is_blank(c));
    }
    let start = advance_chars(rest, 0, o.skip_chars);
    let rest = &rest[start..];
    let end = o
        .check_chars
        .map(|n| advance_chars(rest, 0, n))
        .unwrap_or(rest.len());
    let key = &rest[..end];
    if o.case_insensitive {
        key.to_lowercase()
    } else {
        key.to_string()
    }
}

#[allow(clippy::result_large_err)]
fn parse_uniq_count(opt: &str, val: &str) -> std::result::Result<usize, ExecResult> {
    val.trim().parse().map_err(|_| {
        let what = match opt {
            "f" => "number of fields to skip",
            "s" => "number of bytes to skip",
            _ => "number of bytes to compare",
        };
        ExecResult::err(format!("uniq: {val}: invalid {what}\n"), 1)
    })
}

#[async_trait]
impl Builtin for Uniq {
    async fn execute(&self, ctx: Context<'_>) -> Result<ExecResult> {
        if let Some(r) = super::check_help_version(
            ctx.args,
            "Usage: uniq [OPTION]... [INPUT [OUTPUT]]\nFilter adjacent matching lines from INPUT, writing to OUTPUT.\n\n  -c\t\tprefix lines by the number of occurrences\n  -d\t\tonly print duplicate lines, one for each group\n  -D\t\tprint all duplicate lines\n  -u\t\tonly print unique lines\n  -i\t\tignore differences in case when comparing\n  -f NUM\tavoid comparing the first NUM fields\n  -s NUM\tavoid comparing the first NUM characters\n  -w NUM\tcompare no more than NUM characters in lines\n  -z\t\tline delimiter is NUL, not newline\n  --help\tdisplay this help and exit\n  --version\toutput version information and exit\n",
            Some("uniq (bashkit) 0.1"),
        ) {
            return Ok(r);
        }
        let mut count = false;
        let mut only_duplicates = false;
        let mut all_duplicates = false;
        let mut only_unique = false;
        let mut zero_terminated = false;
        let mut key_opts = UniqKeyOpts::default();
        let mut files = Vec::new();

        let args = ctx.args;
        let mut i = 0;
        let mut opts_done = false;
        while i < args.len() {
            let arg = args[i].as_str();
            i += 1;
            if opts_done || arg == "-" || !arg.starts_with('-') {
                files.push(arg.to_string());
                continue;
            }
            if arg == "--" {
                opts_done = true;
                continue;
            }
            if let Some(long) = arg.strip_prefix("--") {
                let (name, inline) = match long.split_once('=') {
                    Some((n, v)) => (n, Some(v.to_string())),
                    None => (long, None),
                };
                let short = match name {
                    "count" => 'c',
                    "repeated" => 'd',
                    "all-repeated" => 'D',
                    "unique" => 'u',
                    "ignore-case" => 'i',
                    "zero-terminated" => 'z',
                    "skip-fields" => 'f',
                    "skip-chars" => 's',
                    "check-chars" => 'w',
                    _ => return Ok(super::invalid_option("uniq", arg, 1)),
                };
                if matches!(short, 'f' | 's' | 'w') {
                    let val = match inline {
                        Some(v) => v,
                        None if i < args.len() => {
                            i += 1;
                            args[i - 1].clone()
                        }
                        None => {
                            return Ok(ExecResult::err(
                                format!("uniq: option '--{name}' requires an argument\n"),
                                1,
                            ));
                        }
                    };
                    let n = match parse_uniq_count(&short.to_string(), &val) {
                        Ok(n) => n,
                        Err(e) => return Ok(e),
                    };
                    match short {
                        'f' => key_opts.skip_fields = n,
                        's' => key_opts.skip_chars = n,
                        _ => key_opts.check_chars = Some(n),
                    }
                    continue;
                }
                match short {
                    'c' => count = true,
                    'd' => only_duplicates = true,
                    'D' => all_duplicates = true,
                    'u' => only_unique = true,
                    'i' => key_opts.case_insensitive = true,
                    _ => zero_terminated = true,
                }
                continue;
            }
            let body = &arg[1..];
            for (pos, c) in body.char_indices() {
                match c {
                    'f' | 's' | 'w' => {
                        let attached = &body[pos + 1..];
                        let val = if !attached.is_empty() {
                            attached.to_string()
                        } else if i < args.len() {
                            i += 1;
                            args[i - 1].clone()
                        } else {
                            return Ok(ExecResult::err(
                                format!("uniq: option requires an argument -- '{c}'\n"),
                                1,
                            ));
                        };
                        let n = match parse_uniq_count(&c.to_string(), &val) {
                            Ok(n) => n,
                            Err(e) => return Ok(e),
                        };
                        match c {
                            'f' => key_opts.skip_fields = n,
                            's' => key_opts.skip_chars = n,
                            _ => key_opts.check_chars = Some(n),
                        }
                        break;
                    }
                    'c' => count = true,
                    'd' => only_duplicates = true,
                    'D' => all_duplicates = true,
                    'u' => only_unique = true,
                    'i' => key_opts.case_insensitive = true,
                    'z' => zero_terminated = true,
                    _ => return Ok(super::invalid_option("uniq", &format!("-{c}"), 1)),
                }
            }
        }

        let sep = if zero_terminated { '\0' } else { '\n' };
        let text = match files.first().map(String::as_str) {
            None | Some("-") => ctx.stdin.map(|s| s.to_string()).unwrap_or_default(),
            Some(file) => {
                let path = if file.starts_with('/') {
                    std::path::PathBuf::from(file)
                } else {
                    vfs_join(ctx.cwd, file)
                };
                match read_text_file(&*ctx.fs, &path, "uniq").await {
                    Ok(text) => text,
                    Err(e) => return Ok(e),
                }
            }
        };
        let lines: Vec<&str> = split_records(&text, sep).collect();

        // Group adjacent lines with equal keys.
        let mut groups: Vec<(usize, usize)> = Vec::new(); // (start, len)
        let mut prev_key: Option<String> = None;
        for (idx, line) in lines.iter().enumerate() {
            let key = uniq_key(line, &key_opts);
            match (&prev_key, groups.last_mut()) {
                (Some(pk), Some(g)) if *pk == key => g.1 += 1,
                _ => groups.push((idx, 1)),
            }
            prev_key = Some(key);
        }

        let mut output = String::new();
        let mut emit = |line: &str, n: usize| {
            if count {
                output.push_str(&format!("{:>7} ", n));
            }
            output.push_str(line);
            output.push(sep);
        };
        for &(start, n) in &groups {
            if all_duplicates {
                if n > 1 {
                    for line in &lines[start..start + n] {
                        emit(line, n);
                    }
                }
                continue;
            }
            let keep = if only_duplicates && only_unique {
                false
            } else if only_duplicates {
                n > 1
            } else if only_unique {
                n == 1
            } else {
                true
            };
            if keep {
                emit(lines[start], n);
            }
        }

        if let Some(out) = files.get(1) {
            let path = if out.starts_with('/') {
                std::path::PathBuf::from(out)
            } else {
                vfs_join(ctx.cwd, out)
            };
            if let Err(e) = ctx.fs.write_file(&path, output.as_bytes()).await {
                return Ok(ExecResult::err(format!("uniq: {out}: {e}\n"), 1));
            }
            return Ok(ExecResult::ok(String::new()));
        }

        Ok(ExecResult::ok(output))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashMap;
    use std::path::PathBuf;
    use std::sync::Arc;

    use crate::fs::InMemoryFs;

    async fn run_sort(args: &[&str], stdin: Option<&str>) -> ExecResult {
        let fs = Arc::new(InMemoryFs::new());
        let mut variables = HashMap::new();
        let env = HashMap::new();
        let mut cwd = PathBuf::from("/");

        let args: Vec<String> = args.iter().map(|s| s.to_string()).collect();
        let ctx = Context {
            args: &args,
            env: &env,
            variables: &mut variables,
            cwd: &mut cwd,
            fs,
            stdin: crate::builtins::test_stream_opt(stdin),
            #[cfg(feature = "http_client")]
            http_client: None,
            #[cfg(feature = "git")]
            git_client: None,
            #[cfg(feature = "ssh")]
            ssh_client: None,
            shell: None,
        };

        Sort.execute(ctx).await.unwrap()
    }

    async fn run_uniq(args: &[&str], stdin: Option<&str>) -> ExecResult {
        let fs = Arc::new(InMemoryFs::new());
        let mut variables = HashMap::new();
        let env = HashMap::new();
        let mut cwd = PathBuf::from("/");

        let args: Vec<String> = args.iter().map(|s| s.to_string()).collect();
        let ctx = Context {
            args: &args,
            env: &env,
            variables: &mut variables,
            cwd: &mut cwd,
            fs,
            stdin: crate::builtins::test_stream_opt(stdin),
            #[cfg(feature = "http_client")]
            http_client: None,
            #[cfg(feature = "git")]
            git_client: None,
            #[cfg(feature = "ssh")]
            ssh_client: None,
            shell: None,
        };

        Uniq.execute(ctx).await.unwrap()
    }

    #[tokio::test]
    async fn test_sort_basic() {
        let result = run_sort(&[], Some("banana\napple\ncherry\n")).await;
        assert_eq!(result.exit_code, 0);
        assert_eq!(result.stdout, "apple\nbanana\ncherry\n");
    }

    #[tokio::test]
    async fn test_sort_reverse() {
        let result = run_sort(&["-r"], Some("apple\nbanana\ncherry\n")).await;
        assert_eq!(result.exit_code, 0);
        assert_eq!(result.stdout, "cherry\nbanana\napple\n");
    }

    #[tokio::test]
    async fn test_sort_numeric() {
        let result = run_sort(&["-n"], Some("10\n2\n1\n20\n")).await;
        assert_eq!(result.exit_code, 0);
        assert_eq!(result.stdout, "1\n2\n10\n20\n");
    }

    #[tokio::test]
    async fn test_sort_unique() {
        let result = run_sort(&["-u"], Some("apple\nbanana\napple\ncherry\nbanana\n")).await;
        assert_eq!(result.exit_code, 0);
        assert_eq!(result.stdout, "apple\nbanana\ncherry\n");
    }

    #[tokio::test]
    async fn test_sort_fold_case() {
        let result = run_sort(&["-f"], Some("Banana\napple\nCherry\n")).await;
        assert_eq!(result.exit_code, 0);
        assert_eq!(result.stdout, "apple\nBanana\nCherry\n");
    }

    #[tokio::test]
    async fn test_uniq_basic() {
        let result = run_uniq(&[], Some("a\na\nb\nb\nb\nc\n")).await;
        assert_eq!(result.exit_code, 0);
        assert_eq!(result.stdout, "a\nb\nc\n");
    }

    #[tokio::test]
    async fn test_uniq_count() {
        let result = run_uniq(&["-c"], Some("a\na\nb\nc\nc\nc\n")).await;
        assert_eq!(result.exit_code, 0);
        assert!(result.stdout.contains("2 a"));
        assert!(result.stdout.contains("1 b"));
        assert!(result.stdout.contains("3 c"));
    }

    #[tokio::test]
    async fn test_uniq_duplicates_only() {
        let result = run_uniq(&["-d"], Some("a\na\nb\nc\nc\n")).await;
        assert_eq!(result.exit_code, 0);
        assert!(result.stdout.contains("a"));
        assert!(result.stdout.contains("c"));
        assert!(!result.stdout.contains("b\n"));
    }

    #[tokio::test]
    async fn test_uniq_unique_only() {
        let result = run_uniq(&["-u"], Some("a\na\nb\nc\nc\n")).await;
        assert_eq!(result.exit_code, 0);
        assert!(result.stdout.contains("b"));
        assert!(!result.stdout.contains("a\n"));
        assert!(!result.stdout.contains("c\n"));
    }

    #[tokio::test]
    async fn test_sort_key_field() {
        let result = run_sort(&["-k2n"], Some("Bob 25\nAlice 30\nDavid 20\n")).await;
        assert_eq!(result.exit_code, 0);
        assert_eq!(result.stdout, "David 20\nBob 25\nAlice 30\n");
    }

    #[tokio::test]
    async fn test_sort_delimiter_key() {
        let result = run_sort(&["-t:", "-k2n"], Some("b:2\na:1\nc:3\n")).await;
        assert_eq!(result.exit_code, 0);
        assert_eq!(result.stdout, "a:1\nb:2\nc:3\n");
    }

    #[tokio::test]
    async fn test_sort_check_sorted() {
        let result = run_sort(&["-c"], Some("a\nb\nc\n")).await;
        assert_eq!(result.exit_code, 0);
    }

    #[tokio::test]
    async fn test_sort_check_unsorted() {
        let result = run_sort(&["-c"], Some("b\na\nc\n")).await;
        assert_eq!(result.exit_code, 1);
    }

    #[tokio::test]
    async fn test_sort_human_numeric() {
        let result = run_sort(&["-h"], Some("10K\n1K\n100M\n1G\n")).await;
        assert_eq!(result.exit_code, 0);
        assert_eq!(result.stdout, "1K\n10K\n100M\n1G\n");
    }

    #[tokio::test]
    async fn test_sort_month() {
        let result = run_sort(&["-M"], Some("Mar\nJan\nFeb\n")).await;
        assert_eq!(result.exit_code, 0);
        assert_eq!(result.stdout, "Jan\nFeb\nMar\n");
    }

    #[tokio::test]
    async fn test_uniq_case_insensitive() {
        let result = run_uniq(&["-i"], Some("a\nA\nb\nB\n")).await;
        assert_eq!(result.exit_code, 0);
        assert_eq!(result.stdout, "a\nb\n");
    }

    #[tokio::test]
    async fn test_uniq_skip_fields() {
        let result = run_uniq(&["-f1"], Some("x a\ny a\nx b\n")).await;
        assert_eq!(result.exit_code, 0);
        assert_eq!(result.stdout, "x a\nx b\n");
    }

    fn key(spec: &str, line: &str, tab: Option<char>) -> String {
        let k = KeySpec::parse(spec).unwrap();
        String::from_utf8(key_text(line, tab, &k).to_vec()).unwrap()
    }

    #[test]
    fn test_key_text() {
        assert_eq!(key("2,2", "a:b:c", Some(':')), "b");
        assert_eq!(key("2", "a:b:c", Some(':')), "b:c");
        assert_eq!(key("1,1", "hello world", None), "hello");
        // Fields keep their leading blanks unless -b; no end = end of line.
        assert_eq!(key("2", "hello  world x", None), "  world x");
        assert_eq!(key("2b,2", "hello  world x", None), "world");
        assert_eq!(key("5", "x", None), "");
        // Char offsets may run past the field end into the rest of the line.
        assert_eq!(key("1.3", "a b", None), "b");
        // Char positions count UTF-8 characters.
        assert_eq!(key("1.2,1.2", "éa", None), "a");
        assert_eq!(key("1.2,2.1", "éx yz", None), "x ");
    }

    #[test]
    fn test_keyspec_parse() {
        let k = KeySpec::parse("2n,3r").unwrap();
        assert_eq!((k.sword, k.eword), (1, Some(2)));
        assert!(k.opts.numeric && k.opts.reverse);

        let k2 = KeySpec::parse("1.2,1.5f").unwrap();
        assert_eq!((k2.sword, k2.schar, k2.eword, k2.echar), (0, 1, Some(0), 5));
        assert!(k2.opts.fold);

        assert!(KeySpec::parse("0").is_err());
        assert!(KeySpec::parse("1.0").is_err());
        assert!(KeySpec::parse("1x").is_err());
    }

    #[test]
    fn test_version_cmp() {
        assert_eq!(version_cmp("1.2", "1.10"), Ordering::Less);
        assert_eq!(version_cmp("2.0", "1.9"), Ordering::Greater);
        assert_eq!(version_cmp("1.0", "1.0"), Ordering::Equal);
        assert_eq!(version_cmp("a-1.9~rc", "a-1.9"), Ordering::Less);
        assert_eq!(version_cmp(".", ".."), Ordering::Less);
        assert_eq!(version_cmp("file.tar.gz", "file2.tar.gz"), Ordering::Less);
    }

    #[test]
    fn test_numeric_compare_is_exact() {
        assert_eq!(
            numcompare(b"100000000000000000001", b"100000000000000000000"),
            Ordering::Greater
        );
        assert_eq!(numcompare(b"-0", b"0"), Ordering::Equal);
        assert_eq!(numcompare(b"+5", b"0"), Ordering::Equal);
        assert_eq!(numcompare(b".5", b"0.50"), Ordering::Equal);
        assert_eq!(numcompare(b"-2", b"-10"), Ordering::Greater);
    }

    #[test]
    fn test_human_and_general_compare() {
        assert_eq!(human_compare(b"1023K", b"1M"), Ordering::Less);
        assert_eq!(human_compare(b"-1K", b"-2"), Ordering::Less);
        assert_eq!(general_compare(b"abc", b"nan"), Ordering::Less);
        assert_eq!(general_compare(b"0x10", b"1e3"), Ordering::Less);
        assert_eq!(general_compare(b"10", b"0x10"), Ordering::Less);
    }

    #[test]
    fn test_month_ordinal() {
        assert_eq!(month_ordinal(b"JAN"), 1);
        assert_eq!(month_ordinal(b"  feb"), 2);
        assert_eq!(month_ordinal(b"december"), 12);
        assert_eq!(month_ordinal(b"xyz"), 0);
    }

    #[tokio::test]
    async fn test_sort_key_options_override_global() {
        let result = run_sort(&["-r", "-k2n"], Some("a 2 z\nb 10 a\nc 2 b\n")).await;
        assert_eq!(result.stdout, "c 2 b\na 2 z\nb 10 a\n");
    }

    #[tokio::test]
    async fn test_sort_unique_uses_keys_only() {
        let result = run_sort(&["-nu"], Some("1\n01\nb\na\n")).await;
        assert_eq!(result.stdout, "b\n1\n");
    }

    #[tokio::test]
    async fn test_sort_bad_key_is_error() {
        let result = run_sort(&["-k0"], Some("a\n")).await;
        assert_eq!(result.exit_code, 2);
        assert!(result.stderr.contains("field number is zero"));
    }

    #[tokio::test]
    async fn test_sort_rejects_unknown_option() {
        let result = run_sort(&["-Q"], Some("a\n")).await;
        assert_eq!(result.exit_code, 2);
        assert!(result.stderr.contains("invalid option -- 'Q'"));
    }

    #[tokio::test]
    async fn test_uniq_rejects_unknown_option() {
        let result = run_uniq(&["-Q"], Some("a\n")).await;
        assert_eq!(result.exit_code, 1);
        assert!(result.stderr.contains("invalid option -- 'Q'"));
    }
}