summaryrefslogtreecommitdiff
path: root/Userland/Shell/Builtin.cpp
blob: 2238de8b3c5a8c1d78be46f4c012ccf515b5003a (plain)
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
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
/*
 * Copyright (c) 2020-2021, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "AST.h"
#include "Formatter.h"
#include "PosixParser.h"
#include "Shell.h"
#include <AK/DeprecatedString.h>
#include <AK/LexicalPath.h>
#include <AK/ScopeGuard.h>
#include <AK/Statistics.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/EventLoop.h>
#include <LibCore/File.h>
#include <LibCore/System.h>
#include <LibFileSystem/FileSystem.h>
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
#include <signal.h>
#include <sys/wait.h>
#include <unistd.h>

extern char** environ;

namespace Shell {

ErrorOr<int> Shell::builtin_noop(Main::Arguments)
{
    return 0;
}

ErrorOr<int> Shell::builtin_dump(Main::Arguments arguments)
{
    bool posix = false;
    StringView source;

    Core::ArgsParser parser;
    parser.add_positional_argument(source, "Shell code to parse and dump", "source");
    parser.add_option(posix, "Use the POSIX parser", "posix", 'p');

    if (!parser.parse(arguments, Core::ArgsParser::FailureBehavior::PrintUsage))
        return 1;

    TRY((posix ? Posix::Parser { source }.parse() : Parser { source }.parse())->dump(0));
    return 0;
}

enum FollowSymlinks {
    Yes,
    No
};

static Vector<String> find_matching_executables_in_path(StringView filename, FollowSymlinks follow_symlinks = FollowSymlinks::No)
{
    // Edge cases in which there are guaranteed no solutions
    if (filename.is_empty() || filename.contains('/'))
        return {};

    char const* path_str = getenv("PATH");
    auto path = DEFAULT_PATH_SV;
    if (path_str != nullptr) // maybe && *path_str
        path = { path_str, strlen(path_str) };

    Vector<String> executables;
    auto directories = path.split_view(':');
    for (auto directory : directories) {
        auto file = String::formatted("{}/{}", directory, filename).release_value_but_fixme_should_propagate_errors();

        if (follow_symlinks == FollowSymlinks::Yes) {
            auto path_or_error = FileSystem::read_link(file);
            if (!path_or_error.is_error())
                file = path_or_error.release_value();
        }
        if (!Core::System::access(file, X_OK).is_error())
            executables.append(move(file));
    }

    return executables;
}

ErrorOr<int> Shell::builtin_where(Main::Arguments arguments)
{
    Vector<StringView> values_to_look_up;
    bool do_only_path_search { false };
    bool do_follow_symlinks { false };
    bool do_print_only_type { false };

    Core::ArgsParser parser;
    parser.add_positional_argument(values_to_look_up, "List of shell builtins, aliases or executables", "arguments");
    parser.add_option(do_only_path_search, "Search only for executables in the PATH environment variable", "path-only", 'p');
    parser.add_option(do_follow_symlinks, "Follow symlinks and print the symlink free path", "follow-symlink", 's');
    parser.add_option(do_print_only_type, "Print the argument type instead of a human readable description", "type", 'w');

    if (!parser.parse(arguments, Core::ArgsParser::FailureBehavior::PrintUsage))
        return 1;

    auto const look_up_alias = [do_only_path_search, &m_aliases = this->m_aliases](StringView alias) -> Optional<DeprecatedString> {
        if (do_only_path_search)
            return {};
        return m_aliases.get(alias);
    };

    auto const look_up_builtin = [do_only_path_search](StringView builtin) -> Optional<DeprecatedString> {
        if (do_only_path_search)
            return {};
        for (auto const& _builtin : builtin_names) {
            if (_builtin == builtin) {
                return builtin;
            }
        }
        return {};
    };

    bool at_least_one_succeded { false };
    for (auto const& argument : values_to_look_up) {
        auto const alias = look_up_alias(argument);
        if (alias.has_value()) {
            if (do_print_only_type)
                outln("{}: alias", argument);
            else
                outln("{}: aliased to {}", argument, alias.value());
            at_least_one_succeded = true;
        }

        auto const builtin = look_up_builtin(argument);
        if (builtin.has_value()) {
            if (do_print_only_type)
                outln("{}: builtin", builtin.value());
            else
                outln("{}: shell built-in command", builtin.value());
            at_least_one_succeded = true;
        }

        auto const executables = find_matching_executables_in_path(argument, do_follow_symlinks ? FollowSymlinks::Yes : FollowSymlinks::No);
        for (auto const& path : executables) {
            if (do_print_only_type)
                outln("{}: command", argument);
            else
                outln(path);
            at_least_one_succeded = true;
        }
        if (!at_least_one_succeded)
            warnln("{} not found", argument);
    }
    return at_least_one_succeded ? 0 : 1;
}

ErrorOr<int> Shell::builtin_alias(Main::Arguments arguments)
{
    Vector<DeprecatedString> aliases;

    Core::ArgsParser parser;
    parser.add_positional_argument(aliases, "List of name[=values]'s", "name[=value]", Core::ArgsParser::Required::No);

    if (!parser.parse(arguments, Core::ArgsParser::FailureBehavior::PrintUsage))
        return 1;

    if (aliases.is_empty()) {
        for (auto& alias : m_aliases)
            printf("%s=%s\n", escape_token(alias.key).characters(), escape_token(alias.value).characters());
        return 0;
    }

    bool fail = false;
    for (auto& argument : aliases) {
        auto parts = argument.split_limit('=', 2, SplitBehavior::KeepEmpty);
        if (parts.size() == 1) {
            auto alias = m_aliases.get(parts[0]);
            if (alias.has_value()) {
                printf("%s=%s\n", escape_token(parts[0]).characters(), escape_token(alias.value()).characters());
            } else {
                fail = true;
            }
        } else {
            m_aliases.set(parts[0], parts[1]);
            add_entry_to_cache({ RunnablePath::Kind::Alias, parts[0] });
        }
    }

    return fail ? 1 : 0;
}

ErrorOr<int> Shell::builtin_unalias(Main::Arguments arguments)
{
    bool remove_all { false };
    Vector<DeprecatedString> aliases;

    Core::ArgsParser parser;
    parser.set_general_help("Remove alias from the list of aliases");
    parser.add_option(remove_all, "Remove all aliases", nullptr, 'a');
    parser.add_positional_argument(aliases, "List of aliases to remove", "alias", Core::ArgsParser::Required::Yes);

    if (!parser.parse(arguments, Core::ArgsParser::FailureBehavior::PrintUsage))
        return 1;

    if (remove_all) {
        m_aliases.clear();
        cache_path();
        return 0;
    }

    bool failed { false };
    for (auto& argument : aliases) {
        if (!m_aliases.contains(argument)) {
            warnln("unalias: {}: alias not found", argument);
            failed = true;
            continue;
        }
        m_aliases.remove(argument);
        remove_entry_from_cache(argument);
    }

    return failed ? 1 : 0;
}

ErrorOr<int> Shell::builtin_break(Main::Arguments arguments)
{
    unsigned count = 1;

    Core::ArgsParser parser;
    parser.add_positional_argument(count, "Number of loops to 'break' out of", "count", Core::ArgsParser::Required::No);

    if (!parser.parse(arguments, Core::ArgsParser::FailureBehavior::PrintUsage))
        return 1;

    if (count != 1) {
        raise_error(ShellError::EvaluatedSyntaxError, "break: count must be equal to 1 (NYI)");
        return 1;
    }

    raise_error(ShellError::InternalControlFlowBreak, "POSIX break");

    return 0;
}

ErrorOr<int> Shell::builtin_continue(Main::Arguments arguments)
{
    unsigned count = 1;

    Core::ArgsParser parser;
    parser.add_positional_argument(count, "Number of loops to 'continue' out of", "count", Core::ArgsParser::Required::No);

    if (!parser.parse(arguments, Core::ArgsParser::FailureBehavior::PrintUsage))
        return 1;

    if (count != 0) {
        raise_error(ShellError::EvaluatedSyntaxError, "continue: count must be equal to 1 (NYI)");
        return 1;
    }

    raise_error(ShellError::InternalControlFlowContinue, "POSIX continue");

    return 0;
}

ErrorOr<int> Shell::builtin_bg(Main::Arguments arguments)
{
    int job_id = -1;
    bool is_pid = false;

    Core::ArgsParser parser;
    parser.add_positional_argument(Core::ArgsParser::Arg {
        .help_string = "Job ID or Jobspec to run in background",
        .name = "job-id",
        .min_values = 0,
        .max_values = 1,
        .accept_value = [&](StringView value) -> bool {
            // Check if it's a pid (i.e. literal integer)
            if (auto number = value.to_uint(); number.has_value()) {
                job_id = number.value();
                is_pid = true;
                return true;
            }

            // Check if it's a jobspec
            if (auto id = resolve_job_spec(value); id.has_value()) {
                job_id = id.value();
                is_pid = false;
                return true;
            }

            return false;
        } });

    if (!parser.parse(arguments, Core::ArgsParser::FailureBehavior::PrintUsage))
        return 1;

    if (job_id == -1 && !jobs.is_empty())
        job_id = find_last_job_id();

    auto* job = const_cast<Job*>(find_job(job_id, is_pid));

    if (!job) {
        if (job_id == -1) {
            warnln("bg: No current job");
        } else {
            warnln("bg: Job with id/pid {} not found", job_id);
        }
        return 1;
    }

    job->set_running_in_background(true);
    job->set_should_announce_exit(true);
    job->set_shell_did_continue(true);

    dbgln("Resuming {} ({})", job->pid(), job->cmd());
    warnln("Resuming job {} - {}", job->job_id(), job->cmd());

    // Try using the PGID, but if that fails, just use the PID.
    if (killpg(job->pgid(), SIGCONT) < 0) {
        if (kill(job->pid(), SIGCONT) < 0) {
            perror("kill");
            return 1;
        }
    }

    return 0;
}

ErrorOr<int> Shell::builtin_type(Main::Arguments arguments)
{
    Vector<DeprecatedString> commands;
    bool dont_show_function_source = false;

    Core::ArgsParser parser;
    parser.set_general_help("Display information about commands.");
    parser.add_positional_argument(commands, "Command(s) to list info about", "command");
    parser.add_option(dont_show_function_source, "Do not show functions source.", "no-fn-source", 'f');

    if (!parser.parse(arguments, Core::ArgsParser::FailureBehavior::PrintUsage))
        return 1;

    bool something_not_found = false;

    for (auto& command : commands) {
        // check if it is an alias
        if (auto alias = m_aliases.get(command); alias.has_value()) {
            printf("%s is aliased to `%s`\n", escape_token(command).characters(), escape_token(alias.value()).characters());
            continue;
        }

        // check if it is a function
        if (auto function = m_functions.get(command); function.has_value()) {
            auto fn = function.value();
            printf("%s is a function\n", command.characters());
            if (!dont_show_function_source) {
                StringBuilder builder;
                builder.append(fn.name);
                builder.append('(');
                for (size_t i = 0; i < fn.arguments.size(); i++) {
                    builder.append(fn.arguments[i]);
                    if (!(i == fn.arguments.size() - 1))
                        builder.append(' ');
                }
                builder.append(") {\n"sv);
                if (fn.body) {
                    auto formatter = Formatter(*fn.body);
                    builder.append(formatter.format());
                    printf("%s\n}\n", builder.to_deprecated_string().characters());
                } else {
                    printf("%s\n}\n", builder.to_deprecated_string().characters());
                }
            }
            continue;
        }

        // check if its a builtin
        if (has_builtin(command)) {
            printf("%s is a shell builtin\n", command.characters());
            continue;
        }

        // check if its an executable in PATH
        auto fullpath = FileSystem::resolve_executable_from_environment(command);
        if (!fullpath.is_error()) {
            printf("%s is %s\n", command.characters(), escape_token(fullpath.release_value()).characters());
            continue;
        }
        something_not_found = true;
        printf("type: %s not found\n", command.characters());
    }

    if (something_not_found)
        return 1;
    else
        return 0;
}

ErrorOr<int> Shell::builtin_cd(Main::Arguments arguments)
{
    StringView arg_path;

    Core::ArgsParser parser;
    parser.add_positional_argument(arg_path, "Path to change to", "path", Core::ArgsParser::Required::No);

    if (!parser.parse(arguments, Core::ArgsParser::FailureBehavior::PrintUsage))
        return 1;

    DeprecatedString new_path;

    if (arg_path.is_empty()) {
        new_path = home;
    } else {
        if (arg_path == "-"sv) {
            char* oldpwd = getenv("OLDPWD");
            if (oldpwd == nullptr)
                return 1;
            new_path = oldpwd;
        } else {
            new_path = arg_path;
        }
    }

    auto real_path_or_error = FileSystem::real_path(new_path);
    if (real_path_or_error.is_error()) {
        warnln("Invalid path '{}'", new_path);
        return 1;
    }
    auto real_path = real_path_or_error.release_value().to_deprecated_string();

    if (cd_history.is_empty() || cd_history.last() != real_path)
        cd_history.enqueue(real_path);

    auto path_relative_to_current_directory = LexicalPath::relative_path(real_path, cwd);
    if (path_relative_to_current_directory.is_empty())
        path_relative_to_current_directory = real_path;
    char const* path = path_relative_to_current_directory.characters();

    int rc = chdir(path);
    if (rc < 0) {
        if (errno == ENOTDIR) {
            warnln("Not a directory: {}", path);
        } else {
            warnln("chdir({}) failed: {}", path, strerror(errno));
        }
        return 1;
    }
    setenv("OLDPWD", cwd.characters(), 1);
    cwd = move(real_path);
    setenv("PWD", cwd.characters(), 1);
    return 0;
}

ErrorOr<int> Shell::builtin_cdh(Main::Arguments arguments)
{
    int index = -1;

    Core::ArgsParser parser;
    parser.add_positional_argument(index, "Index of the cd history entry (leave out for a list)", "index", Core::ArgsParser::Required::No);

    if (!parser.parse(arguments, Core::ArgsParser::FailureBehavior::PrintUsage))
        return 1;

    if (index == -1) {
        if (cd_history.is_empty()) {
            warnln("cdh: no history available");
            return 0;
        }

        for (ssize_t i = cd_history.size() - 1; i >= 0; --i)
            printf("%zu: %s\n", cd_history.size() - i, cd_history.at(i).characters());
        return 0;
    }

    if (index < 1 || (size_t)index > cd_history.size()) {
        warnln("cdh: history index out of bounds: {} not in (0, {})", index, cd_history.size());
        return 1;
    }

    StringView path = cd_history.at(cd_history.size() - index);
    StringView cd_args[] = { "cd"sv, path };
    return Shell::builtin_cd({ .argc = 0, .argv = 0, .strings = cd_args });
}

ErrorOr<int> Shell::builtin_dirs(Main::Arguments arguments)
{
    // The first directory in the stack is ALWAYS the current directory
    directory_stack.at(0) = cwd.characters();

    bool clear = false;
    bool print = false;
    bool number_when_printing = false;
    char separator = ' ';

    Vector<DeprecatedString> paths;

    Core::ArgsParser parser;
    parser.add_option(clear, "Clear the directory stack", "clear", 'c');
    parser.add_option(print, "Print directory entries one per line", "print", 'p');
    parser.add_option(number_when_printing, "Number the directories in the stack when printing", "number", 'v');
    parser.add_positional_argument(paths, "Extra paths to put on the stack", "path", Core::ArgsParser::Required::No);

    if (!parser.parse(arguments, Core::ArgsParser::FailureBehavior::PrintUsage))
        return 1;

    // -v implies -p
    print = print || number_when_printing;

    if (print) {
        if (!paths.is_empty()) {
            warnln("dirs: 'print' and 'number' are not allowed when any path is specified");
            return 1;
        }
        separator = '\n';
    }

    if (clear) {
        for (size_t i = 1; i < directory_stack.size(); i++)
            directory_stack.remove(i);
    }

    for (auto& path : paths)
        directory_stack.append(path);

    if (print || (!clear && paths.is_empty())) {
        int index = 0;
        for (auto& directory : directory_stack) {
            if (number_when_printing)
                printf("%d ", index++);
            print_path(directory);
            fputc(separator, stdout);
        }
    }

    return 0;
}

ErrorOr<int> Shell::builtin_exec(Main::Arguments arguments)
{
    if (arguments.strings.size() < 2) {
        warnln("Shell: No command given to exec");
        return 1;
    }

    TRY(execute_process(arguments.strings.slice(1)));
    // NOTE: Won't get here.
    return 0;
}

ErrorOr<int> Shell::builtin_exit(Main::Arguments arguments)
{
    int exit_code = 0;
    Core::ArgsParser parser;
    parser.add_positional_argument(exit_code, "Exit code", "code", Core::ArgsParser::Required::No);
    if (!parser.parse(arguments, Core::ArgsParser::FailureBehavior::PrintUsage))
        return 1;

    if (m_is_interactive) {
        if (!jobs.is_empty()) {
            if (!m_should_ignore_jobs_on_next_exit) {
                warnln("Shell: You have {} active job{}, run 'exit' again to really exit.", jobs.size(), jobs.size() > 1 ? "s" : "");
                m_should_ignore_jobs_on_next_exit = true;
                return 1;
            }
        }
    }
    stop_all_jobs();
    if (m_is_interactive) {
        m_editor->save_history(get_history_path());
        printf("Good-bye!\n");
    }
    exit(exit_code);
}

ErrorOr<int> Shell::builtin_export(Main::Arguments arguments)
{
    Vector<DeprecatedString> vars;

    Core::ArgsParser parser;
    parser.add_positional_argument(vars, "List of variable[=value]'s", "values", Core::ArgsParser::Required::No);

    if (!parser.parse(arguments, Core::ArgsParser::FailureBehavior::PrintUsage))
        return 1;

    if (vars.is_empty()) {
        for (size_t i = 0; environ[i]; ++i)
            puts(environ[i]);
        return 0;
    }

    for (auto& value : vars) {
        auto parts = value.split_limit('=', 2);
        if (parts.is_empty()) {
            warnln("Shell: Invalid export spec '{}', expected `variable=value' or `variable'", value);
            return 1;
        }

        if (parts.size() == 1) {
            auto value = TRY(look_up_local_variable(parts[0]));
            if (value) {
                auto values = TRY(const_cast<AST::Value&>(*value).resolve_as_list(*this));
                StringBuilder builder;
                builder.join(' ', values);
                parts.append(builder.to_deprecated_string());
            } else {
                // Ignore the export.
                continue;
            }
        }

        int setenv_return = setenv(parts[0].characters(), parts[1].characters(), 1);

        if (setenv_return != 0) {
            perror("setenv");
            return 1;
        }

        if (parts[0] == "PATH")
            cache_path();
    }

    return 0;
}

ErrorOr<int> Shell::builtin_glob(Main::Arguments arguments)
{
    Vector<DeprecatedString> globs;
    Core::ArgsParser parser;
    parser.add_positional_argument(globs, "Globs to resolve", "glob");

    if (!parser.parse(arguments, Core::ArgsParser::FailureBehavior::PrintUsage))
        return 1;

    for (auto& glob : globs) {
        for (auto& expanded : expand_globs(glob, cwd))
            outln("{}", expanded);
    }

    return 0;
}

ErrorOr<int> Shell::builtin_fg(Main::Arguments arguments)
{
    int job_id = -1;
    bool is_pid = false;

    Core::ArgsParser parser;
    parser.add_positional_argument(Core::ArgsParser::Arg {
        .help_string = "Job ID or Jobspec to bring to foreground",
        .name = "job-id",
        .min_values = 0,
        .max_values = 1,
        .accept_value = [&](StringView value) -> bool {
            // Check if it's a pid (i.e. literal integer)
            if (auto number = value.to_uint(); number.has_value()) {
                job_id = number.value();
                is_pid = true;
                return true;
            }

            // Check if it's a jobspec
            if (auto id = resolve_job_spec(value); id.has_value()) {
                job_id = id.value();
                is_pid = false;
                return true;
            }

            return false;
        } });

    if (!parser.parse(arguments, Core::ArgsParser::FailureBehavior::PrintUsage))
        return 1;

    if (job_id == -1 && !jobs.is_empty())
        job_id = find_last_job_id();

    RefPtr<Job> job = find_job(job_id, is_pid);

    if (!job) {
        if (job_id == -1) {
            warnln("fg: No current job");
        } else {
            warnln("fg: Job with id/pid {} not found", job_id);
        }
        return 1;
    }

    job->set_running_in_background(false);
    job->set_shell_did_continue(true);

    dbgln("Resuming {} ({})", job->pid(), job->cmd());
    warnln("Resuming job {} - {}", job->job_id(), job->cmd());

    tcsetpgrp(STDOUT_FILENO, job->pgid());
    tcsetpgrp(STDIN_FILENO, job->pgid());

    // Try using the PGID, but if that fails, just use the PID.
    if (killpg(job->pgid(), SIGCONT) < 0) {
        if (kill(job->pid(), SIGCONT) < 0) {
            perror("kill");
            return 1;
        }
    }

    block_on_job(job);

    if (job->exited())
        return job->exit_code();
    else
        return 0;
}

ErrorOr<int> Shell::builtin_disown(Main::Arguments arguments)
{
    Vector<int> job_ids;
    Vector<bool> id_is_pid;

    Core::ArgsParser parser;
    parser.add_positional_argument(Core::ArgsParser::Arg {
        .help_string = "Job IDs or Jobspecs to disown",
        .name = "job-id",
        .min_values = 0,
        .max_values = INT_MAX,
        .accept_value = [&](StringView value) -> bool {
            // Check if it's a pid (i.e. literal integer)
            if (auto number = value.to_uint(); number.has_value()) {
                job_ids.append(number.value());
                id_is_pid.append(true);
                return true;
            }

            // Check if it's a jobspec
            if (auto id = resolve_job_spec(value); id.has_value()) {
                job_ids.append(id.value());
                id_is_pid.append(false);
                return true;
            }

            return false;
        } });

    if (!parser.parse(arguments, Core::ArgsParser::FailureBehavior::PrintUsage))
        return 1;

    if (job_ids.is_empty()) {
        job_ids.append(find_last_job_id());
        id_is_pid.append(false);
    }

    Vector<Job const*> jobs_to_disown;

    for (size_t i = 0; i < job_ids.size(); ++i) {
        auto id = job_ids[i];
        auto is_pid = id_is_pid[i];
        auto job = find_job(id, is_pid);
        if (!job)
            warnln("disown: Job with id/pid {} not found", id);
        else
            jobs_to_disown.append(job);
    }

    if (jobs_to_disown.is_empty()) {
        if (job_ids.is_empty())
            warnln("disown: No current job");
        // An error message has already been printed about the nonexistence of each listed job.
        return 1;
    }

    for (auto job : jobs_to_disown) {
        job->deactivate();

        if (!job->is_running_in_background())
            warnln("disown warning: Job {} is currently not running, 'kill -{} {}' to make it continue", job->job_id(), SIGCONT, job->pid());

        jobs.remove(job->pid());
    }

    return 0;
}

ErrorOr<int> Shell::builtin_history(Main::Arguments)
{
    for (size_t i = 0; i < m_editor->history().size(); ++i) {
        printf("%6zu  %s\n", i + 1, m_editor->history()[i].entry.characters());
    }
    return 0;
}

ErrorOr<int> Shell::builtin_jobs(Main::Arguments arguments)
{
    bool list = false, show_pid = false;

    Core::ArgsParser parser;
    parser.add_option(list, "List all information about jobs", "list", 'l');
    parser.add_option(show_pid, "Display the PID of the jobs", "pid", 'p');

    if (!parser.parse(arguments, Core::ArgsParser::FailureBehavior::PrintUsage))
        return 1;

    Job::PrintStatusMode mode = Job::PrintStatusMode::Basic;

    if (show_pid)
        mode = Job::PrintStatusMode::OnlyPID;

    if (list)
        mode = Job::PrintStatusMode::ListAll;

    for (auto& it : jobs) {
        if (!it.value->print_status(mode))
            return 1;
    }

    return 0;
}

ErrorOr<int> Shell::builtin_popd(Main::Arguments arguments)
{
    if (directory_stack.size() <= 1) {
        warnln("Shell: popd: directory stack empty");
        return 1;
    }

    bool should_not_switch = false;
    Core::ArgsParser parser;
    parser.add_option(should_not_switch, "Do not switch dirs", "no-switch", 'n');

    if (!parser.parse(arguments, Core::ArgsParser::FailureBehavior::PrintUsage))
        return 1;

    auto popped_path = directory_stack.take_last();

    if (should_not_switch)
        return 0;

    auto new_path = LexicalPath::canonicalized_path(popped_path);
    if (chdir(new_path.characters()) < 0) {
        warnln("chdir({}) failed: {}", new_path, strerror(errno));
        return 1;
    }
    cwd = new_path;
    return 0;
}

ErrorOr<int> Shell::builtin_pushd(Main::Arguments arguments)
{
    StringBuilder path_builder;
    bool should_switch = true;

    // From the BASH reference manual: https://www.gnu.org/software/bash/manual/html_node/Directory-Stack-Builtins.html
    // With no arguments, pushd exchanges the top two directories and makes the new top the current directory.
    if (arguments.strings.size() == 1) {
        if (directory_stack.size() < 2) {
            warnln("pushd: no other directory");
            return 1;
        }

        DeprecatedString dir1 = directory_stack.take_first();
        DeprecatedString dir2 = directory_stack.take_first();
        directory_stack.insert(0, dir2);
        directory_stack.insert(1, dir1);

        int rc = chdir(dir2.characters());
        if (rc < 0) {
            warnln("chdir({}) failed: {}", dir2, strerror(errno));
            return 1;
        }

        cwd = dir2;

        return 0;
    }

    // Let's assume the user's typed in 'pushd <dir>'
    if (arguments.strings.size() == 2) {
        directory_stack.append(cwd.characters());
        if (arguments.strings[1].starts_with('/')) {
            path_builder.append(arguments.strings[1]);
        } else {
            path_builder.appendff("{}/{}", cwd, arguments.strings[1]);
        }
    } else if (arguments.strings.size() == 3) {
        directory_stack.append(cwd.characters());
        for (size_t i = 1; i < arguments.strings.size(); i++) {
            auto arg = arguments.strings[i];

            if (arg.starts_with('-')) {
                if (arg.starts_with('/')) {
                    path_builder.append(arg);
                } else {
                    path_builder.appendff("{}/{}", cwd, arg);
                }
            }

            if (arg == "-n"sv)
                should_switch = false;
        }
    }

    auto real_path = LexicalPath::canonicalized_path(path_builder.to_deprecated_string());

    struct stat st;
    int rc = stat(real_path.characters(), &st);
    if (rc < 0) {
        warnln("stat({}) failed: {}", real_path, strerror(errno));
        return 1;
    }

    if (!S_ISDIR(st.st_mode)) {
        warnln("Not a directory: {}", real_path);
        return 1;
    }

    if (should_switch) {
        int rc = chdir(real_path.characters());
        if (rc < 0) {
            warnln("chdir({}) failed: {}", real_path, strerror(errno));
            return 1;
        }

        cwd = real_path;
    }

    return 0;
}

ErrorOr<int> Shell::builtin_pwd(Main::Arguments)
{
    print_path(cwd);
    fputc('\n', stdout);
    return 0;
}

ErrorOr<int> Shell::builtin_setopt(Main::Arguments arguments)
{
    if (arguments.strings.size() == 1) {
#define __ENUMERATE_SHELL_OPTION(name, default_, description) \
    if (options.name)                                         \
        warnln("{}", #name);

        ENUMERATE_SHELL_OPTIONS();

#undef __ENUMERATE_SHELL_OPTION
    }

    Core::ArgsParser parser;
#define __ENUMERATE_SHELL_OPTION(name, default_, description)     \
    bool name = false;                                            \
    bool not_##name = false;                                      \
    parser.add_option(name, "Enable: " description, #name, '\0'); \
    parser.add_option(not_##name, "Disable: " description, "no_" #name, '\0');

    ENUMERATE_SHELL_OPTIONS();

#undef __ENUMERATE_SHELL_OPTION

    if (!parser.parse(arguments, Core::ArgsParser::FailureBehavior::PrintUsage))
        return 1;

#define __ENUMERATE_SHELL_OPTION(name, default_, description) \
    if (name)                                                 \
        options.name = true;                                  \
    if (not_##name)                                           \
        options.name = false;

    ENUMERATE_SHELL_OPTIONS();

#undef __ENUMERATE_SHELL_OPTION

    return 0;
}

ErrorOr<int> Shell::builtin_shift(Main::Arguments arguments)
{
    int count = 1;

    Core::ArgsParser parser;
    parser.add_positional_argument(count, "Shift count", "count", Core::ArgsParser::Required::No);

    if (!parser.parse(arguments, Core::ArgsParser::FailureBehavior::PrintUsage))
        return 1;

    if (count < 1)
        return 0;

    auto argv_ = TRY(look_up_local_variable("ARGV"sv));
    if (!argv_) {
        warnln("shift: ARGV is unset");
        return 1;
    }

    if (!argv_->is_list())
        argv_ = adopt_ref(*new AST::ListValue({ const_cast<AST::Value&>(*argv_) }));

    auto& values = const_cast<AST::ListValue*>(static_cast<AST::ListValue const*>(argv_.ptr()))->values();
    if ((size_t)count > values.size()) {
        warnln("shift: shift count must not be greater than {}", values.size());
        return 1;
    }

    for (auto i = 0; i < count; ++i)
        (void)values.take_first();

    return 0;
}

ErrorOr<int> Shell::builtin_source(Main::Arguments arguments)
{
    StringView file_to_source;
    Vector<StringView> args;

    Core::ArgsParser parser;
    parser.add_positional_argument(file_to_source, "File to read commands from", "path");
    parser.add_positional_argument(args, "ARGV for the sourced file", "args", Core::ArgsParser::Required::No);

    if (!parser.parse(arguments))
        return 1;

    auto previous_argv = TRY(look_up_local_variable("ARGV"sv));
    ScopeGuard guard { [&] {
        if (!args.is_empty())
            set_local_variable("ARGV", const_cast<AST::Value&>(*previous_argv));
    } };

    if (!args.is_empty()) {
        Vector<String> arguments;
        arguments.ensure_capacity(args.size());
        for (auto& arg : args)
            arguments.append(TRY(String::from_utf8(arg)));

        set_local_variable("ARGV", AST::make_ref_counted<AST::ListValue>(move(arguments)));
    }

    if (!run_file(file_to_source, true))
        return 126;

    return 0;
}

ErrorOr<int> Shell::builtin_time(Main::Arguments arguments)
{
    Vector<StringView> args;

    int number_of_iterations = 1;

    Core::ArgsParser parser;
    parser.add_option(number_of_iterations, "Number of iterations", "iterations", 'n', "iterations");
    parser.set_stop_on_first_non_option(true);
    parser.add_positional_argument(args, "Command to execute with arguments", "command", Core::ArgsParser::Required::Yes);

    if (!parser.parse(arguments, Core::ArgsParser::FailureBehavior::PrintUsage))
        return 1;

    if (number_of_iterations < 1)
        return 1;

    AST::Command command;
    TRY(command.argv.try_ensure_capacity(args.size()));
    for (auto& arg : args)
        command.argv.append(TRY(String::from_utf8(arg)));

    auto commands = TRY(expand_aliases({ move(command) }));

    AK::Statistics iteration_times;

    int exit_code = 1;
    for (int i = 0; i < number_of_iterations; ++i) {
        auto timer = Core::ElapsedTimer::start_new();
        for (auto& job : run_commands(commands)) {
            block_on_job(job);
            exit_code = job->exit_code();
        }
        iteration_times.add(static_cast<float>(timer.elapsed()));
    }

    if (number_of_iterations == 1) {
        warnln("Time: {} ms", iteration_times.values().first());
    } else {
        AK::Statistics iteration_times_excluding_first;
        for (size_t i = 1; i < iteration_times.size(); i++)
            iteration_times_excluding_first.add(iteration_times.values()[i]);

        warnln("Timing report: {} ms", iteration_times.sum());
        warnln("==============");
        warnln("Command:         {}", DeprecatedString::join(' ', arguments.strings));
        warnln("Average time:    {:.2} ms (median: {}, stddev: {:.2}, min: {}, max:{})",
            iteration_times.average(), iteration_times.median(),
            iteration_times.standard_deviation(),
            iteration_times.min(), iteration_times.max());
        warnln("Excluding first: {:.2} ms (median: {}, stddev: {:.2}, min: {}, max:{})",
            iteration_times_excluding_first.average(), iteration_times_excluding_first.median(),
            iteration_times_excluding_first.standard_deviation(),
            iteration_times_excluding_first.min(), iteration_times_excluding_first.max());
    }

    return exit_code;
}

ErrorOr<int> Shell::builtin_umask(Main::Arguments arguments)
{
    StringView mask_text;

    Core::ArgsParser parser;
    parser.add_positional_argument(mask_text, "New mask (omit to get current mask)", "octal-mask", Core::ArgsParser::Required::No);

    if (!parser.parse(arguments, Core::ArgsParser::FailureBehavior::PrintUsage))
        return 1;

    if (mask_text.is_empty()) {
        mode_t old_mask = umask(0);
        printf("%#o\n", old_mask);
        umask(old_mask);
        return 0;
    }

    unsigned mask = 0;
    auto matches = true;

    // FIXME: There's currently no way to parse an StringView as an octal integer,
    //        when that becomes available, replace this code.
    for (auto byte : mask_text.bytes()) {
        if (isspace(byte))
            continue;
        if (!is_ascii_octal_digit(byte)) {
            matches = false;
            break;
        }

        mask = (mask << 3) + (byte - '0');
    }
    if (matches) {
        umask(mask);
        return 0;
    }

    warnln("umask: Invalid mask '{}'", mask_text);
    return 1;
}

ErrorOr<int> Shell::builtin_wait(Main::Arguments arguments)
{
    Vector<int> job_ids;
    Vector<bool> id_is_pid;

    Core::ArgsParser parser;
    parser.add_positional_argument(Core::ArgsParser::Arg {
        .help_string = "Job IDs or Jobspecs to wait for",
        .name = "job-id",
        .min_values = 0,
        .max_values = INT_MAX,
        .accept_value = [&](StringView value) -> bool {
            // Check if it's a pid (i.e. literal integer)
            if (auto number = value.to_uint(); number.has_value()) {
                job_ids.append(number.value());
                id_is_pid.append(true);
                return true;
            }

            // Check if it's a jobspec
            if (auto id = resolve_job_spec(value); id.has_value()) {
                job_ids.append(id.value());
                id_is_pid.append(false);
                return true;
            }

            return false;
        } });

    if (!parser.parse(arguments, Core::ArgsParser::FailureBehavior::PrintUsage))
        return 1;

    Vector<NonnullRefPtr<Job>> jobs_to_wait_for;

    for (size_t i = 0; i < job_ids.size(); ++i) {
        auto id = job_ids[i];
        auto is_pid = id_is_pid[i];
        auto job = find_job(id, is_pid);
        if (!job)
            warnln("wait: Job with id/pid {} not found", id);
        else
            jobs_to_wait_for.append(*job);
    }

    if (job_ids.is_empty()) {
        for (auto const& it : jobs)
            jobs_to_wait_for.append(it.value);
    }

    for (auto& job : jobs_to_wait_for) {
        job->set_running_in_background(false);
        block_on_job(job);
    }

    return 0;
}

ErrorOr<int> Shell::builtin_unset(Main::Arguments arguments)
{
    Vector<DeprecatedString> vars;
    bool unset_only_variables = false; // POSIX only.

    Core::ArgsParser parser;
    parser.add_positional_argument(vars, "List of variables", "variables", Core::ArgsParser::Required::Yes);
    if (m_in_posix_mode)
        parser.add_option(unset_only_variables, "Unset only variables", "variables", 'v');

    if (!parser.parse(arguments, Core::ArgsParser::FailureBehavior::PrintUsage))
        return 1;

    bool did_touch_path = false;
    for (auto& value : vars) {
        if (!did_touch_path && value == "PATH"sv)
            did_touch_path = true;

        if (TRY(look_up_local_variable(value)) != nullptr) {
            unset_local_variable(value);
        } else if (!unset_only_variables) {
            unsetenv(value.characters());
        }
    }

    if (did_touch_path)
        cache_path();

    return 0;
}

ErrorOr<int> Shell::builtin_not(Main::Arguments arguments)
{
    Vector<StringView> args;

    Core::ArgsParser parser;
    parser.set_stop_on_first_non_option(true);
    parser.add_positional_argument(args, "Command to run followed by its arguments", "string");

    if (!parser.parse(arguments, Core::ArgsParser::FailureBehavior::Ignore))
        return 1;

    AST::Command command;
    TRY(command.argv.try_ensure_capacity(args.size()));
    for (auto& arg : args)
        command.argv.unchecked_append(TRY(String::from_utf8(arg)));

    auto commands = TRY(expand_aliases({ move(command) }));
    int exit_code = 1;
    auto found_a_job = false;
    for (auto& job : run_commands(commands)) {
        found_a_job = true;
        block_on_job(job);
        exit_code = job->exit_code();
    }
    // In case it was a function.
    if (!found_a_job)
        exit_code = last_return_code.value_or(0);
    return exit_code == 0 ? 1 : 0;
}

ErrorOr<int> Shell::builtin_kill(Main::Arguments arguments)
{
    // Simply translate the arguments and pass them to `kill'
    Vector<String> replaced_values;
    auto kill_path_or_error = FileSystem::resolve_executable_from_environment("kill"sv);
    if (kill_path_or_error.is_error()) {
        warnln("kill: `kill' not found in PATH");
        return 126;
    }

    replaced_values.append(kill_path_or_error.release_value());
    for (size_t i = 1; i < arguments.strings.size(); ++i) {
        if (auto job_id = resolve_job_spec(arguments.strings[i]); job_id.has_value()) {
            auto job = find_job(job_id.value());
            if (job) {
                replaced_values.append(TRY(String::number(job->pid())));
            } else {
                warnln("kill: Job with pid {} not found", job_id.value());
                return 1;
            }
        } else {
            replaced_values.append(TRY(String::from_utf8(arguments.strings[i])));
        }
    }

    // Now just run `kill'
    AST::Command command;
    command.argv = move(replaced_values);
    command.position = m_source_position.has_value() ? m_source_position->position : Optional<AST::Position> {};

    auto exit_code = 1;
    auto job_result = run_command(command);
    if (job_result.is_error()) {
        warnln("kill: Failed to run {}: {}", command.argv.first(), job_result.error());
        return exit_code;
    }

    if (auto job = job_result.release_value()) {
        block_on_job(job);
        exit_code = job->exit_code();
    }
    return exit_code;
}

ErrorOr<bool> Shell::run_builtin(const AST::Command& command, Vector<NonnullRefPtr<AST::Rewiring>> const& rewirings, int& retval)
{
    if (command.argv.is_empty())
        return false;

    if (!has_builtin(command.argv.first()))
        return false;

    Vector<StringView> arguments;
    TRY(arguments.try_ensure_capacity(command.argv.size()));
    for (auto& arg : command.argv)
        arguments.unchecked_append(arg);

    Main::Arguments arguments_object {
        .argc = 0,
        .argv = nullptr,
        .strings = arguments
    };

    StringView name = command.argv.first();

    SavedFileDescriptors fds { rewirings };

    for (auto& rewiring : rewirings) {
        int rc = dup2(rewiring->old_fd, rewiring->new_fd);
        if (rc < 0) {
            perror("dup2(run)");
            return false;
        }
    }

    Core::EventLoop loop;
    setup_signals();

    if (name == ":"sv)
        name = "noop"sv;
    else if (m_in_posix_mode && name == "."sv)
        name = "source"sv;

#define __ENUMERATE_SHELL_BUILTIN(builtin, _mode)                        \
    if (name == #builtin) {                                              \
        retval = TRY(builtin_##builtin(arguments_object));               \
        if (!has_error(ShellError::None))                                \
            raise_error(m_error, m_error_description, command.position); \
        fflush(stdout);                                                  \
        fflush(stderr);                                                  \
        return true;                                                     \
    }

    ENUMERATE_SHELL_BUILTINS();

#undef __ENUMERATE_SHELL_BUILTIN
    return false;
}

ErrorOr<int> Shell::builtin_argsparser_parse(Main::Arguments arguments)
{
    // argsparser_parse
    //   --add-option variable [--type (bool | string | i32 | u32 | double | size)] --help-string "" --long-name "" --short-name "" [--value-name "" <if not --type bool>] --list
    //   --add-positional-argument variable [--type (bool | string | i32 | u32 | double | size)] ([--min n] [--max n] | [--required]) --help-string "" --value-name ""
    //   [--general-help ""]
    //   [--stop-on-first-non-option]
    //   --
    //   $args_to_parse
    Core::ArgsParser parser;

    Core::ArgsParser user_parser;

    Vector<StringView> descriptors;
    Variant<Core::ArgsParser::Option, Core::ArgsParser::Arg, Empty> current;
    DeprecatedString help_string_storage;
    DeprecatedString long_name_storage;
    DeprecatedString value_name_storage;
    DeprecatedString name_storage;
    DeprecatedString current_variable;
    // if max > 1 or min < 1, or explicit `--list`.
    bool treat_arg_as_list = false;
    enum class Type {
        Bool,
        String,
        I32,
        U32,
        Double,
        Size,
    };

    auto type = Type::String;

    auto try_convert = [](StringView value, Type type) -> ErrorOr<Optional<RefPtr<AST::Value>>> {
        switch (type) {
        case Type::Bool:
            return AST::make_ref_counted<AST::StringValue>(TRY("true"_string));
        case Type::String:
            return AST::make_ref_counted<AST::StringValue>(TRY(String::from_utf8(value)));
        case Type::I32:
            if (auto number = value.to_int(); number.has_value())
                return AST::make_ref_counted<AST::StringValue>(TRY(String::number(*number)));

            warnln("Invalid value for type i32: {}", value);
            return OptionalNone {};
        case Type::U32:
        case Type::Size:
            if (auto number = value.to_uint(); number.has_value())
                return AST::make_ref_counted<AST::StringValue>(TRY(String::number(*number)));

            warnln("Invalid value for type u32|size: {}", value);
            return OptionalNone {};
        case Type::Double: {
            DeprecatedString string = value;
            char* endptr = nullptr;
            auto number = strtod(string.characters(), &endptr);
            if (endptr != string.characters() + string.length()) {
                warnln("Invalid value for type double: {}", value);
                return OptionalNone {};
            }

            return AST::make_ref_counted<AST::StringValue>(TRY(String::number(number)));
        }
        default:
            VERIFY_NOT_REACHED();
        }
    };

    auto enlist = [&](auto name, auto value) -> ErrorOr<NonnullRefPtr<AST::Value>> {
        auto variable = TRY(look_up_local_variable(name));
        if (variable) {
            auto list = TRY(const_cast<AST::Value&>(*variable).resolve_as_list(*this));
            auto new_value = TRY(value->resolve_as_string(*this));
            list.append(move(new_value));
            return try_make_ref_counted<AST::ListValue>(move(list));
        }
        return *value;
    };

    // FIXME: We cannot return ErrorOr<T> from Core::ArgsParser::Option::accept_value(), fix the MUST's here whenever that function is ErrorOr-aware.
    auto commit = [&] {
        return current.visit(
            [&](Core::ArgsParser::Option& option) {
                if (!option.long_name && !option.short_name) {
                    warnln("Defined option must have at least one of --long-name or --short-name");
                    return false;
                }
                option.accept_value = [&, current_variable, treat_arg_as_list, type](StringView value) {
                    auto result = MUST(try_convert(value, type));
                    if (result.has_value()) {
                        auto value = result.release_value();
                        if (treat_arg_as_list)
                            value = MUST(enlist(current_variable, move(value)));
                        this->set_local_variable(current_variable, move(value), true);
                        return true;
                    }

                    return false;
                };
                user_parser.add_option(move(option));
                type = Type::String;
                treat_arg_as_list = false;
                return true;
            },
            [&](Core::ArgsParser::Arg& arg) {
                if (!arg.name) {
                    warnln("Defined positional argument must have a name");
                    return false;
                }
                arg.accept_value = [&, current_variable, treat_arg_as_list, type](StringView value) {
                    auto result = MUST(try_convert(value, type));
                    if (result.has_value()) {
                        auto value = result.release_value();
                        if (treat_arg_as_list)
                            value = MUST(enlist(current_variable, move(value)));
                        this->set_local_variable(current_variable, move(value), true);
                        return true;
                    }

                    return false;
                };
                user_parser.add_positional_argument(move(arg));
                type = Type::String;
                treat_arg_as_list = false;
                return true;
            },
            [&](Empty) {
                return true;
            });
    };

    parser.add_option(Core::ArgsParser::Option {
        .argument_mode = Core::ArgsParser::OptionArgumentMode::None,
        .help_string = "Stop processing descriptors after a non-argument parameter is seen",
        .long_name = "stop-on-first-non-option",
        .accept_value = [&](auto) {
            user_parser.set_stop_on_first_non_option(true);
            return true;
        },
    });
    parser.add_option(Core::ArgsParser::Option {
        .argument_mode = Core::ArgsParser::OptionArgumentMode::Required,
        .help_string = "Set the general help string for the parser",
        .long_name = "general-help",
        .value_name = "string",
        .accept_value = [&](StringView value) {
            VERIFY(strlen(value.characters_without_null_termination()) == value.length());
            user_parser.set_general_help(value.characters_without_null_termination());
            return true;
        },
    });
    parser.add_option(Core::ArgsParser::Option {
        .argument_mode = Core::ArgsParser::OptionArgumentMode::Required,
        .help_string = "Start describing an option",
        .long_name = "add-option",
        .value_name = "variable-name",
        .accept_value = [&](auto name) {
            if (!commit())
                return false;

            current = Core::ArgsParser::Option {};
            current_variable = name;
            if (current_variable.is_empty() || !all_of(current_variable, [](auto ch) { return ch == '_' || isalnum(ch); })) {
                warnln("Option variable name must be a valid identifier");
                return false;
            }

            return true;
        },
    });
    parser.add_option(Core::ArgsParser::Option {
        .argument_mode = Core::ArgsParser::OptionArgumentMode::None,
        .help_string = "Accept multiple of the current option being given",
        .long_name = "list",
        .accept_value = [&](auto) {
            if (!current.has<Core::ArgsParser::Option>()) {
                warnln("Must be defining an option to use --list");
                return false;
            }
            treat_arg_as_list = true;
            return true;
        },
    });
    parser.add_option(Core::ArgsParser::Option {
        .argument_mode = Core::ArgsParser::OptionArgumentMode::Required,
        .help_string = "Define the type of the option or argument being described",
        .long_name = "type",
        .value_name = "type",
        .accept_value = [&](StringView ty) {
            if (current.has<Empty>()) {
                warnln("Must be defining an argument or option to use --type");
                return false;
            }

            if (ty == "bool") {
                if (auto option = current.get_pointer<Core::ArgsParser::Option>()) {
                    if (option->value_name != nullptr) {
                        warnln("Type 'bool' does not apply to options with a value (value name is set to {})", option->value_name);
                        return false;
                    }
                }
                type = Type::Bool;
            } else if (ty == "string") {
                type = Type::String;
            } else if (ty == "i32") {
                type = Type::I32;
            } else if (ty == "u32") {
                type = Type::U32;
            } else if (ty == "double") {
                type = Type::Double;
            } else if (ty == "size") {
                type = Type::Size;
            } else {
                warnln("Invalid type '{}', expected one of bool | string | i32 | u32 | double | size", ty);
                return false;
            }

            if (type == Type::Bool) {
                set_local_variable(
                    current_variable,
                    make_ref_counted<AST::StringValue>(MUST("false"_string)),
                    true);
            }
            return true;
        },
    });

    parser.add_option(Core::ArgsParser::Option {
        .argument_mode = Core::ArgsParser::OptionArgumentMode::Required,
        .help_string = "Set the help string of the option or argument being defined",
        .long_name = "help-string",
        .value_name = "string",
        .accept_value = [&](StringView value) {
            return current.visit(
                [](Empty) {
                    warnln("Must be defining an option or argument to use --help-string");
                    return false;
                },
                [&](auto& option) {
                    help_string_storage = value;
                    option.help_string = help_string_storage.characters();
                    return true;
                });
        },
    });
    parser.add_option(Core::ArgsParser::Option {
        .argument_mode = Core::ArgsParser::OptionArgumentMode::Required,
        .help_string = "Set the long name of the option being defined",
        .long_name = "long-name",
        .value_name = "name",
        .accept_value = [&](StringView value) {
            auto option = current.get_pointer<Core::ArgsParser::Option>();
            if (!option) {
                warnln("Must be defining an option to use --long-name");
                return false;
            }
            if (option->long_name) {
                warnln("Repeated application of --long-name is not allowed, current option has long name set to \"{}\"", option->long_name);
                return false;
            }

            long_name_storage = value;
            option->long_name = long_name_storage.characters();
            return true;
        },
    });
    parser.add_option(Core::ArgsParser::Option {
        .argument_mode = Core::ArgsParser::OptionArgumentMode::Required,
        .help_string = "Set the short name of the option being defined",
        .long_name = "short-name",
        .value_name = "char",
        .accept_value = [&](StringView value) {
            auto option = current.get_pointer<Core::ArgsParser::Option>();
            if (!option) {
                warnln("Must be defining an option to use --short-name");
                return false;
            }
            if (value.length() != 1) {
                warnln("Option short name ('{}') must be exactly one character long", value);
                return false;
            }
            if (option->short_name) {
                warnln("Repeated application of --short-name is not allowed, current option has short name set to '{}'", option->short_name);
                return false;
            }
            option->short_name = value[0];
            return true;
        },
    });
    parser.add_option(Core::ArgsParser::Option {
        .argument_mode = Core::ArgsParser::OptionArgumentMode::Required,
        .help_string = "Set the value name of the option being defined",
        .long_name = "value-name",
        .value_name = "string",
        .accept_value = [&](StringView value) {
            return current.visit(
                [](Empty) {
                    warnln("Must be defining an option or a positional argument to use --value-name");
                    return false;
                },
                [&](Core::ArgsParser::Option& option) {
                    if (option.value_name) {
                        warnln("Repeated application of --value-name is not allowed, current option has value name set to \"{}\"", option.value_name);
                        return false;
                    }
                    if (type == Type::Bool) {
                        warnln("Options of type bool cannot have a value name");
                        return false;
                    }

                    value_name_storage = value;
                    option.value_name = value_name_storage.characters();
                    return true;
                },
                [&](Core::ArgsParser::Arg& arg) {
                    if (arg.name) {
                        warnln("Repeated application of --value-name is not allowed, current argument has value name set to \"{}\"", arg.name);
                        return false;
                    }

                    name_storage = value;
                    arg.name = name_storage.characters();
                    return true;
                });
        },
    });
    parser.add_option(Core::ArgsParser::Option {
        .argument_mode = Core::ArgsParser::OptionArgumentMode::Required,
        .help_string = "Start describing a positional argument",
        .long_name = "add-positional-argument",
        .value_name = "variable",
        .accept_value = [&](auto value) {
            if (!commit())
                return false;

            current = Core::ArgsParser::Arg {};
            current_variable = value;
            if (current_variable.is_empty() || !all_of(current_variable, [](auto ch) { return ch == '_' || isalnum(ch); })) {
                warnln("Argument variable name must be a valid identifier");
                return false;
            }

            return true;
        },
    });
    parser.add_option(Core::ArgsParser::Option {
        .argument_mode = Core::ArgsParser::OptionArgumentMode::Required,
        .help_string = "Set the minimum required number of positional descriptors for the argument being described",
        .long_name = "min",
        .value_name = "n",
        .accept_value = [&](StringView value) {
            auto arg = current.get_pointer<Core::ArgsParser::Arg>();
            if (!arg) {
                warnln("Must be describing a positional argument to use --min");
                return false;
            }

            auto number = value.to_uint();
            if (!number.has_value()) {
                warnln("Invalid value for --min: '{}', expected a non-negative number", value);
                return false;
            }

            if (static_cast<unsigned>(arg->max_values) < *number) {
                warnln("Invalid value for --min: {}, min must not be larger than max ({})", *number, arg->max_values);
                return false;
            }

            arg->min_values = *number;
            treat_arg_as_list = arg->max_values > 1 || arg->min_values < 1;
            return true;
        },
    });
    parser.add_option(Core::ArgsParser::Option {
        .argument_mode = Core::ArgsParser::OptionArgumentMode::Required,
        .help_string = "Set the maximum required number of positional descriptors for the argument being described",
        .long_name = "max",
        .value_name = "n",
        .accept_value = [&](StringView value) {
            auto arg = current.get_pointer<Core::ArgsParser::Arg>();
            if (!arg) {
                warnln("Must be describing a positional argument to use --max");
                return false;
            }

            auto number = value.to_uint();
            if (!number.has_value()) {
                warnln("Invalid value for --max: '{}', expected a non-negative number", value);
                return false;
            }

            if (static_cast<unsigned>(arg->min_values) > *number) {
                warnln("Invalid value for --max: {}, max must not be smaller than min ({})", *number, arg->min_values);
                return false;
            }

            arg->max_values = *number;
            treat_arg_as_list = arg->max_values > 1 || arg->min_values < 1;
            return true;
        },
    });
    parser.add_option(Core::ArgsParser::Option {
        .argument_mode = Core::ArgsParser::OptionArgumentMode::None,
        .help_string = "Mark the positional argument being described as required (shorthand for --min 1)",
        .long_name = "required",
        .accept_value = [&](auto) {
            auto arg = current.get_pointer<Core::ArgsParser::Arg>();
            if (!arg) {
                warnln("Must be describing a positional argument to use --required");
                return false;
            }
            arg->min_values = 1;
            if (arg->max_values < arg->min_values)
                arg->max_values = 1;
            treat_arg_as_list = arg->max_values > 1 || arg->min_values < 1;
            return true;
        },
    });
    parser.add_positional_argument(descriptors, "Arguments to parse via the described ArgsParser configuration", "arg", Core::ArgsParser::Required::No);

    if (!parser.parse(arguments, Core::ArgsParser::FailureBehavior::Ignore))
        return 2;

    if (!commit())
        return 2;

    if (!user_parser.parse(descriptors, Core::ArgsParser::FailureBehavior::Ignore))
        return 1;

    return 0;
}

ErrorOr<int> Shell::builtin_read(Main::Arguments arguments)
{
    bool no_escape = false;
    Vector<DeprecatedString> variables;

    Core::ArgsParser parser;
    parser.add_option(no_escape, "Do not interpret backslash escapes", "no-escape", 'r');
    parser.add_positional_argument(variables, "Variables to read into", "variable", Core::ArgsParser::Required::Yes);

    if (!parser.parse(arguments, Core::ArgsParser::FailureBehavior::Ignore))
        return 1;

    auto split_by_any_of = " \t\n"_short_string;

    if (auto const* value_from_env = getenv("IFS"); value_from_env)
        split_by_any_of = TRY(String::from_utf8({ value_from_env, strlen(value_from_env) }));
    else if (auto split_by_variable = TRY(look_up_local_variable("IFS"sv)); split_by_variable)
        split_by_any_of = TRY(const_cast<AST::Value&>(*split_by_variable).resolve_as_string(*this));

    auto file = TRY(Core::File::standard_input());
    auto buffered_stream = TRY(Core::InputBufferedFile::create(move(file)));

    StringBuilder builder;
    ByteBuffer buffer;

    enum class LineState {
        Done,
        EscapedNewline,
    };
    auto read_line = [&]() -> ErrorOr<LineState> {
        if (m_is_interactive && isatty(STDIN_FILENO)) {
            // Show prompt
            warn("read: ");
        }
        size_t attempted_line_size = 32;

        for (;;) {
            auto result = buffered_stream->read_line(TRY(buffer.get_bytes_for_writing(attempted_line_size)));
            if (result.is_error() && result.error().is_errno() && result.error().code() == EMSGSIZE) {
                attempted_line_size *= 2;
                continue;
            }

            auto used_bytes = TRY(move(result));
            if (!no_escape && used_bytes.ends_with("\\\n"sv)) {
                builder.append(used_bytes.substring_view(0, used_bytes.length() - 2));
                return LineState::EscapedNewline;
            }

            if (used_bytes.ends_with("\n"sv))
                used_bytes = used_bytes.substring_view(0, used_bytes.length() - 1);

            builder.append(used_bytes);
            return LineState::Done;
        }
    };

    LineState state;
    do {
        state = TRY(read_line());
    } while (state == LineState::EscapedNewline);

    auto line = builder.string_view();
    if (variables.size() == 1) {
        set_local_variable(variables[0], make_ref_counted<AST::StringValue>(TRY(String::from_utf8(line))));
        return 0;
    }

    auto fields = line.split_view_if(is_any_of(split_by_any_of), SplitBehavior::KeepEmpty);

    for (size_t i = 0; i < variables.size(); ++i) {
        auto& variable = variables[i];
        StringView variable_value;
        if (i >= fields.size())
            variable_value = ""sv;
        else if (i == variables.size() - 1)
            variable_value = line.substring_view_starting_from_substring(fields[i]);
        else
            variable_value = fields[i];

        set_local_variable(variable, make_ref_counted<AST::StringValue>(TRY(String::from_utf8(variable_value))));
    }

    return 0;
}

ErrorOr<int> Shell::builtin_run_with_env(Main::Arguments arguments)
{
    Vector<DeprecatedString> environment_variables;
    Vector<StringView> command_and_arguments;

    Core::ArgsParser parser;
    parser.add_option(environment_variables, "Environment variables to set", "env", 'e', "NAME=VALUE");
    parser.add_positional_argument(command_and_arguments, "Command and arguments to run", "command", Core::ArgsParser::Required::Yes);
    parser.set_stop_on_first_non_option(true);

    if (!parser.parse(arguments, Core::ArgsParser::FailureBehavior::Ignore))
        return 1;

    if (command_and_arguments.is_empty()) {
        warnln("run_with_env: No command to run");
        return 1;
    }

    AST::Command command;
    TRY(command.argv.try_ensure_capacity(command_and_arguments.size()));
    for (auto& arg : command_and_arguments)
        command.argv.append(TRY(String::from_utf8(arg)));

    auto commands = TRY(expand_aliases({ move(command) }));

    HashMap<DeprecatedString, Optional<DeprecatedString>> old_environment_entries;
    for (auto& variable : environment_variables) {
        auto parts = variable.split_limit('=', 2, SplitBehavior::KeepEmpty);
        if (parts.size() != 2) {
            warnln("run_with_env: Invalid environment variable: '{}'", variable);
            return 1;
        }

        DeprecatedString name = parts[0];
        old_environment_entries.set(name, getenv(name.characters()) ?: Optional<DeprecatedString> {});

        DeprecatedString value = parts[1];
        setenv(name.characters(), value.characters(), 1);
    }

    int exit_code = 0;
    for (auto& job : run_commands(commands)) {
        block_on_job(job);
        exit_code = job->exit_code();
    }

    for (auto& entry : old_environment_entries) {
        if (entry.value.has_value())
            setenv(entry.key.characters(), entry.value->characters(), 1);
        else
            unsetenv(entry.key.characters());
    }

    return exit_code;
}

bool Shell::has_builtin(StringView name) const
{
    if (name == ":"sv || (m_in_posix_mode && name == "."sv))
        return true;

#define __ENUMERATE_SHELL_BUILTIN(builtin, mode)                            \
    if (name == #builtin) {                                                 \
        if (POSIXModeRequirement::mode == POSIXModeRequirement::InAllModes) \
            return true;                                                    \
        return m_in_posix_mode;                                             \
    }

    ENUMERATE_SHELL_BUILTINS();

#undef __ENUMERATE_SHELL_BUILTIN
    return false;
}
}