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
|
/*
Copyright (c) 2024, Lance Borden
All rights reserved.
This software is licensed under the BSD 3-Clause License.
You may obtain a copy of the license at:
https://opensource.org/licenses/BSD-3-Clause
Redistribution and use in source and binary forms, with or without
modification, are permitted under the conditions stated in the BSD 3-Clause
License.
THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTIES,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
#include "lush.h"
#include "hashmap.h"
#include "lauxlib.h"
#include "lua.h"
#include "lua_api.h"
#include "lualib.h"
#include "compat-5.3.h"
#include <asm-generic/ioctls.h>
#include <bits/time.h>
#include <ctype.h>
#include <dirent.h>
#include <fcntl.h>
#include <glob.h>
#include <linux/limits.h>
#include <locale.h>
#include <pwd.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <termios.h>
#include <time.h>
#include <unistd.h>
// Forward declare the open functions for the compat C modules we need to preload
int luaopen_bit32 (lua_State *L);
int luaopen_utf8 (lua_State *L);
#define BUFFER_SIZE 1024
#define MAX_GLOB 512
typedef enum {
OP_PIPE = 1, // |
OP_AND, // &&
OP_OR, // ||
OP_SEMICOLON, // ;
OP_BACKGROUND, // &
OP_REDIRECT_STDOUT, // 1> or >
OP_REDIRECT_STDERR, // 2>
OP_REDIRECT_BOTH, // &>
OP_APPEND_STDOUT, // 1>> or >>
OP_APPEND_STDERR, // 2>>
OP_APPEND_BOTH, // &>>
OP_OTHER // All other operators like parentheses, braces, etc.
} OperatorType;
// initialize prompt format
char *prompt_format = NULL;
// -- aliasing --
hashmap_t *aliases = NULL;
void lush_add_alias(const char *alias, const char *command) {
// make a new map if one doesnt exist
if (aliases == NULL) {
aliases = hm_new_hashmap();
}
hm_set(aliases, (char *)alias, (char *)command);
}
char *lush_get_alias(char *alias) { return hm_get(aliases, alias); }
// -- shell utility --
static void set_raw_mode(struct termios *orig_termios) {
struct termios raw;
tcgetattr(STDIN_FILENO, orig_termios);
raw = *orig_termios;
raw.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &raw);
}
static void reset_terminal_mode(struct termios *orig_termios) {
tcsetattr(STDIN_FILENO, TCSANOW, orig_termios);
}
static int get_terminal_width() {
struct winsize w;
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == -1) {
perror("ioctl");
return -1;
}
return w.ws_col;
}
// -- prompt helper functions --
static size_t get_prompt_size(const char *format, const char *username,
const char *hostname, const char *cwd) {
size_t prompt_len = 0;
while (*format) {
if (strncmp(format, "%u", 2) == 0) {
prompt_len += strlen(username);
format += 2;
} else if (strncmp(format, "%h", 2) == 0) {
prompt_len += strlen(hostname);
format += 2;
} else if (strncmp(format, "%w", 2) == 0) {
prompt_len += strlen(cwd);
format += 2;
} else if (strncmp(format, "%t", 2) == 0) {
prompt_len += 8; // size of time format
format += 2;
} else if (strncmp(format, "%d", 2) == 0) {
prompt_len += 10;
format += 2;
} else {
prompt_len++;
format++;
}
}
return prompt_len;
}
static char *format_prompt_string(const char *input, const char *username,
const char *hostname, const char *cwd) {
// Calculate the size of the new string
size_t new_size = get_prompt_size(input, username, hostname, cwd) + 1;
// Allocate memory for the new string
char *result = (char *)malloc(new_size);
if (!result) {
perror("malloc failed");
return NULL;
}
// Get the current time
time_t current_time;
time(¤t_time);
struct tm *local_time = localtime(¤t_time);
// Replace placeholders in the input string and build the result string
char *dest = result;
while (*input) {
if (strncmp(input, "%u", 2) == 0) {
strcpy(dest, username);
dest += strlen(username);
input += 2;
} else if (strncmp(input, "%h", 2) == 0) {
strcpy(dest, hostname);
dest += strlen(hostname);
input += 2;
} else if (strncmp(input, "%w", 2) == 0) {
strcpy(dest, cwd);
dest += strlen(cwd);
input += 2;
} else if (strncmp(input, "%t", 2) == 0) {
// Format the time as HH:MM:SS
char time_string[9]; // HH:MM:SS is 8 characters + null terminator
strftime(time_string, sizeof(time_string), "%H:%M:%S", local_time);
strcpy(dest, time_string);
dest += strlen(time_string);
input += 2;
} else if (strncmp(input, "%d", 2) == 0) {
// Format the date as mm/dd/yyyy
char date_string[36];
snprintf(date_string, 36, "%02d/%02d/%04d", local_time->tm_mon + 1,
local_time->tm_mday, local_time->tm_year + 1900);
strcpy(dest, date_string);
dest += strlen(date_string);
input += 2;
} else {
*dest++ = *input++;
}
}
*dest = '\0'; // Null-terminate the result string
return result;
}
static char *get_prompt() {
char *username = getenv("USER");
char hostname[256];
gethostname(hostname, sizeof(hostname));
char *cwd = getcwd(NULL, 0);
// Replace /home/<user> with ~
char *home_prefix = "/home/";
size_t home_len = strlen(home_prefix) + strlen(username);
char *prompt_cwd;
if (strncmp(cwd, home_prefix, strlen(home_prefix)) == 0 &&
strncmp(cwd + strlen(home_prefix), username, strlen(username)) == 0) {
prompt_cwd = malloc(strlen(cwd) - home_len +
2); // 1 for ~ and 1 for null terminator
snprintf(prompt_cwd, strlen(cwd) - home_len + 2, "~%s", cwd + home_len);
} else {
prompt_cwd = strdup(cwd);
}
// get the prompt if no format in init.lua
if (prompt_format == NULL) {
size_t prompt_len =
strlen(prompt_cwd) + strlen(username) + strlen(hostname) + 6;
char *prompt = (char *)malloc(prompt_len);
snprintf(prompt, prompt_len, "[%s@%s:%s]", username, hostname,
prompt_cwd);
free(cwd);
return prompt;
}
// get formatted prompt
char *prompt =
format_prompt_string(prompt_format, username, hostname, prompt_cwd);
free(cwd);
return prompt;
}
static size_t get_stripped_length(const char *str) {
size_t len = 0;
size_t i = 0;
// Set the locale to properly handle UTF-8 multi-byte characters
setlocale(LC_CTYPE, "");
while (str[i] != '\0') {
if (str[i] == '\033' && str[i + 1] == '[') {
// Skip over the escape sequence
while (str[i] != 'm' && str[i] != '\0') {
i++;
}
// Include 'm' character to end the escape sequence
if (str[i] == 'm') {
i++;
}
} else if (str[i] == '\n') {
len = 0;
i++;
} else {
// Calculate the length of the current multi-byte character
int char_len = mblen(&str[i], MB_CUR_MAX);
if (char_len < 1) {
char_len = 1; // Fallback in case of errors
}
len++;
i += char_len;
}
}
return len;
}
// physical refers to the number of character spaces it takes up
static size_t get_physical_length(const char *str) {
size_t len = 0;
size_t i = 0;
// Set the locale to properly handle UTF-8 multi-byte characters
setlocale(LC_CTYPE, "");
while (str[i] != '\0') {
if (str[i] == '\033' && str[i + 1] == '[') {
// Skip over the escape sequence
while (str[i] != 'm' && str[i] != '\0') {
i++;
}
// Include 'm' character to end the escape sequence
if (str[i] == 'm') {
i++;
}
} else {
// Calculate the length of the current multi-byte character
int char_len = mblen(&str[i], MB_CUR_MAX);
if (char_len < 1) {
char_len = 1; // Fallback in case of errors
}
len++;
i += char_len;
}
}
return len;
}
static char *get_stripped_prompt(const char *str) {
size_t i = 0, j = 0;
char *clean_str = (char *)malloc(strlen(str) + 1);
if (clean_str == NULL) {
perror("malloc failed");
exit(EXIT_FAILURE);
}
// Set the locale to properly handle UTF-8 multi-byte characters
setlocale(LC_CTYPE, "");
while (str[i] != '\0') {
if (str[i] == '\033' && str[i + 1] == '[') {
// Skip over the escape sequence
while (str[i] != 'm' && str[i] != '\0') {
i++;
}
// Skip the 'm' character to end the escape sequence
if (str[i] == 'm') {
i++;
}
} else {
// Calculate the length of the current multi-byte character
int char_len = mblen(&str[i], MB_CUR_MAX);
if (char_len < 1) {
char_len = 1; // Fallback in case of errors
}
// Copy the non-escape sequence character(s) to the clean string
for (int k = 0; k < char_len; k++) {
clean_str[j++] = str[i++];
}
}
}
// Null-terminate the clean string
clean_str[j] = '\0';
return clean_str;
}
static int get_prompt_newlines(const char *prompt) {
int newlines = 0;
int width = get_terminal_width();
size_t lines = 0;
size_t capacity = 2;
char *stripped_prompt = get_stripped_prompt(prompt);
int *line_widths = (int *)malloc(capacity * sizeof(int));
if (line_widths == NULL) {
perror("malloc failed");
exit(EXIT_FAILURE);
}
setlocale(LC_CTYPE, "");
size_t i = 0, j = 0;
while (i < strlen(stripped_prompt)) {
int char_len = mblen(&stripped_prompt[i], MB_CUR_MAX);
if (char_len < 1)
char_len = 1;
if (stripped_prompt[i] == '\n') {
if (i % width != width - 1)
newlines++;
if (lines >= capacity) {
capacity *= 2;
line_widths = realloc(line_widths, capacity * sizeof(int));
if (line_widths == NULL) {
perror("realloc failed");
exit(EXIT_FAILURE);
}
}
line_widths[lines++] = j;
j = 0;
}
j++;
i += char_len;
}
// also account for if the terminal width causes wrapping
for (size_t k = 0; k < lines; k++) {
newlines += (line_widths[k]) / width;
if ((line_widths[k] % width) == 0)
newlines--;
}
// Total number of lines is newlines plus the number of wrapped lines
free(line_widths);
free(stripped_prompt);
return newlines;
}
// -- autocomplete --
static int alphebetize_strings(const void *a, const void *b) {
return strcmp(*(const char **)a, *(const char **)b);
}
static char **get_suggestions(size_t *count, const char *path) {
DIR *dir;
struct dirent *entry;
size_t capacity = 10;
size_t size = 0;
// Allocate initial space for the array of strings
char **items = malloc(capacity * sizeof(char *));
if (items == NULL) {
perror("Unable to allocate memory");
exit(EXIT_FAILURE);
}
// Open the current directory
dir = opendir(path);
if (dir == NULL) {
dir = opendir(".");
if (dir == NULL) {
perror("Unable to open current directory");
free(items);
exit(EXIT_FAILURE);
}
}
// Read each directory entry
while ((entry = readdir(dir)) != NULL) {
// Allocate space for each filename
if (size >= capacity) {
capacity *= 2;
items = realloc(items, capacity * sizeof(char *));
if (items == NULL) {
perror("Unable to reallocate memory");
closedir(dir);
exit(EXIT_FAILURE);
}
}
// Copy the entry name to the array
items[size] = strdup(entry->d_name);
if (items[size] == NULL) {
perror("Unable to allocate memory for entry");
closedir(dir);
exit(EXIT_FAILURE);
}
size++;
}
// Close the directory
closedir(dir);
// Set the count to the number of items found
*count = size;
// Reallocate the array to the exact size needed
items = realloc(items, size * sizeof(char *));
qsort(items, size, sizeof(char *), alphebetize_strings);
return items;
}
static const char *suggestion_difference(const char *input,
const char *suggestion) {
size_t input_len = strlen(input);
size_t suggestion_len = strlen(suggestion);
if (input_len <= suggestion_len &&
strncmp(input, suggestion, input_len) == 0) {
return &suggestion[input_len];
}
return NULL;
}
static void free_suggestions(char **suggestions, int count) {
for (size_t i = 0; i < count; i++) {
free(suggestions[i]);
}
free(suggestions);
}
static const char *find_suggestion(const char *input, char **suggestions,
size_t count) {
if (strlen(input) == 0)
return NULL;
for (size_t i = 0; i < count; i++) {
if (strncmp(input, suggestions[i], strlen(input)) == 0) {
const char *suggestion =
suggestion_difference(input, suggestions[i]);
return suggestion;
}
}
return NULL;
}
static const char *get_current_token(const char *input) {
const char *last_space = strrchr(input, ' ');
if (last_space == NULL)
return input;
// return the substring after the space
return last_space + 1;
}
static const char *get_current_word(const char *input) {
const char *current_token = get_current_token(input);
const char *last_slash = strrchr(current_token, '/');
if (last_slash == NULL)
return current_token;
return last_slash + 1;
}
char *get_suggestions_path(const char *str) {
if (str == NULL) {
return strdup("./");
}
const char *last_slash = strrchr(str, '/');
if (last_slash == NULL) {
return strdup("./");
}
size_t length = last_slash - str;
// Allocate memory for the substring and copy it
char *result = (char *)malloc(length + 3);
if (result == NULL) {
perror("malloc failed");
exit(EXIT_FAILURE);
}
strncpy(result, "./", 2);
result += 2;
strncpy(result, str, length);
result -= 2;
result[length + 2] = '\0';
return result;
}
// -- shell buffer handling --
static void reprint_buffer(char *buffer, int *last_lines, int *pos,
int history_pos) {
static size_t old_buffer_len = 0;
char suggestion[PATH_MAX] = {0};
char *prompt = get_prompt();
int width = get_terminal_width();
int prompt_newlines = get_prompt_newlines(prompt);
size_t prompt_length = get_stripped_length(prompt);
// handle history before doing calculations
if (history_pos >= 0) {
char *history_line = lush_get_past_command(history_pos);
if (history_line != NULL) {
strncpy(buffer, history_line, BUFFER_SIZE);
free(history_line);
// remove newline from buffer
buffer[strlen(buffer) - 1] = '\0';
*pos = strlen(buffer);
}
}
// handle autocomplete before doing calculations as well
size_t suggestions_count = 0;
const char *current_token = get_current_token(buffer);
char *suggestions_path = get_suggestions_path(current_token);
const char *current_word = get_current_word(buffer);
char **suggestions = get_suggestions(&suggestions_count, suggestions_path);
const char *autocomplete_suggestion =
find_suggestion(current_word, suggestions, suggestions_count);
if (autocomplete_suggestion != NULL) {
strncpy(suggestion, autocomplete_suggestion, PATH_MAX);
}
// free(suggestions_path);
int num_lines = ((strlen(buffer) + prompt_length + 1) / width) + 1;
int cursor_pos = (prompt_length + *pos + 1) % width;
int cursor_line = (prompt_length + *pos + 1) / width + 1;
// move cursor down if it is up a number of lines first
if (num_lines - cursor_line > 0) {
printf("\033[%dB", num_lines - cursor_line);
// compensate for if we have just filled a line
if ((strlen(buffer) + prompt_length + 1) % width == 0 &&
old_buffer_len < strlen(buffer)) {
printf("\033[A");
}
// compense for if we have just emptied a line
if ((strlen(buffer) + prompt_length + 1) % width == width - 1 &&
old_buffer_len > strlen(buffer)) {
printf("\033[B");
}
}
if (old_buffer_len < strlen(buffer) || history_pos >= 0) {
for (int i = 0; i < *last_lines; i++) {
printf("\r\033[K");
if (i > 0)
printf("\033[A");
}
} else {
for (int i = 0; i < num_lines; i++) {
printf("\r\033[K");
if (i > 0)
printf("\033[A");
}
}
*last_lines = num_lines;
// if the prompt has new lines move up
if (prompt_newlines > 0) {
printf("\033[%dA", prompt_newlines);
}
// ensure line is cleared before printing
printf("\r\033[K");
printf("%s ", prompt);
printf("%s", buffer);
if (suggestion_enable)
printf("\033[0;33m%s\033[0m ", suggestion);
// move cursor up and to the right to be in correct position
if (cursor_pos > 0)
printf("\r\033[%dC", cursor_pos);
else
printf("\r");
if (num_lines > 1 && (num_lines - cursor_line) > 0)
printf("\033[%dA", num_lines - cursor_line);
// if autocomplete goes to new line move up
int suggested_lines =
((strlen(buffer) + strlen(suggestion) + prompt_length + 1) / width) + 1;
if (suggested_lines > num_lines)
printf("\033[A");
// cleanup
free(prompt);
old_buffer_len = strlen(buffer);
suggestion[0] = '\0';
free_suggestions(suggestions, suggestions_count);
}
char *lush_read_line() {
struct termios orig_termios;
char *buffer = (char *)calloc(BUFFER_SIZE, sizeof(char));
int pos = 0;
int history_pos = -1;
int last_lines = 1;
char last_command;
int c;
// init buffer and make raw mode
set_raw_mode(&orig_termios);
while (true) {
c = getchar();
if (c == '\033') { // escape sequence
getchar(); // skip [
switch (getchar()) {
case 'A': // up arrow
reprint_buffer(buffer, &last_lines, &pos, ++history_pos);
break;
case 'B': // down arrow
reprint_buffer(buffer, &last_lines, &pos, --history_pos);
if (history_pos < 0)
history_pos = 0;
break;
case 'C': // right arrow
{
int width = get_terminal_width();
char *prompt = get_prompt();
size_t prompt_length = get_stripped_length(prompt);
if ((prompt_length + pos) % width == width - 2) {
printf("\033[B");
}
if (pos < strlen(buffer)) {
pos++;
// if modifying text reset history
history_pos = -1;
reprint_buffer(buffer, &last_lines, &pos, history_pos);
}
free(prompt);
} break;
case 'D': // left arrow
{
int width = get_terminal_width();
char *prompt = get_prompt();
size_t prompt_length = get_stripped_length(prompt);
if ((prompt_length + pos) % width == width - 1) {
printf("\033[A");
}
if (pos > 0) {
pos--;
// if modifying text reset history
history_pos = -1;
reprint_buffer(buffer, &last_lines, &pos, history_pos);
}
free(prompt);
} break;
case '3': // delete
if (getchar() == '~') {
if (pos < strlen(buffer)) {
memmove(&buffer[pos], &buffer[pos + 1],
strlen(&buffer[pos + 1]) + 1);
// if modifying text reset history
history_pos = -1;
reprint_buffer(buffer, &last_lines, &pos, history_pos);
}
}
break;
default:
break;
}
} else if (c == '\177') { // backspace
if (pos > 0) {
memmove(&buffer[pos - 1], &buffer[pos],
strlen(&buffer[pos]) + 1);
pos--;
// handle edge case where cursor should be moved up
int width = get_terminal_width();
char *prompt = get_prompt();
size_t prompt_length = get_stripped_length(prompt);
if ((prompt_length + pos + 1) % width == width - 1 &&
pos <= strlen(buffer)) {
printf("\033[A");
}
// if modifying text reset history
history_pos = -1;
reprint_buffer(buffer, &last_lines, &pos, history_pos);
}
} else if (c == '\t') {
char suggestion[PATH_MAX];
size_t suggestions_count = 0;
const char *current_token = get_current_token(buffer);
char *suggestions_path = get_suggestions_path(current_token);
const char *current_word = get_current_word(buffer);
char **suggestions =
get_suggestions(&suggestions_count, suggestions_path);
const char *autocomplete_suggestion =
find_suggestion(current_word, suggestions, suggestions_count);
if (autocomplete_suggestion != NULL) {
size_t suggestion_len = strlen(autocomplete_suggestion);
size_t current_word_len = strlen(current_word);
// Insert the suggestion in place of the current word
memmove(&buffer[pos + suggestion_len], &buffer[pos],
strlen(&buffer[pos]) + 1);
strncpy(&buffer[pos], autocomplete_suggestion, suggestion_len);
// Move the cursor forward
pos += suggestion_len;
}
reprint_buffer(buffer, &last_lines, &pos, history_pos);
free(suggestions_path);
free_suggestions(suggestions, suggestions_count);
} else if (c == '\n') {
// if modifying text reset history
history_pos = -1;
pos = strlen(buffer);
reprint_buffer(buffer, &last_lines, &pos, history_pos);
break; // submit the command
} else {
if (pos < BUFFER_SIZE - 1) {
// insert text into buffer
memmove(&buffer[pos + 1], &buffer[pos],
strlen(&buffer[pos]) + 1);
buffer[pos] = c;
pos++;
// handle edge case where cursor should be moved down
int width = get_terminal_width();
char *prompt = get_prompt();
size_t prompt_length = get_stripped_length(prompt);
if ((prompt_length + pos) % width == width - 1 &&
pos < strlen(buffer)) {
printf("\033[B");
}
// if modifying text reset history
history_pos = -1;
reprint_buffer(buffer, &last_lines, &pos, history_pos);
}
}
}
reset_terminal_mode(&orig_termios);
return buffer;
}
char *lush_resolve_aliases(char *line) {
// Allocate memory for the new string
char *result = (char *)malloc(BUFFER_SIZE);
if (!result) {
perror("malloc failed");
return NULL;
}
// Create a copy of the input line for tokenization
char *line_copy = strdup(line);
if (!line_copy) {
perror("strdup failed");
free(result);
return NULL;
}
// Start building the result string
char *dest = result;
char *arg = strtok(line_copy, " ");
while (arg != NULL) {
// Check shell aliases
if (aliases != NULL) {
char *alias = hm_get(aliases, arg);
if (alias != NULL) {
// Replace alias
strcpy(dest, alias);
dest += strlen(alias);
} else {
// No alias found, use the original token
strcpy(dest, arg);
dest += strlen(arg);
}
} else {
// No aliases set, just use the original token
strcpy(dest, arg);
dest += strlen(arg);
}
// Add a space after each token (if it's not the last one)
arg = strtok(NULL, " ");
if (arg != NULL) {
*dest++ = ' ';
}
}
*dest = '\0'; // Null-terminate the result string
// Clean up
free(line_copy);
return result;
}
static void lush_expand_globs(char ***args) {
if (args == NULL || *args == NULL) {
return;
}
// iterate over each arg
for (int i = 0; args[i]; i++) {
char **arg_list = args[i];
for (int j = 0; arg_list[j]; j++) {
char *arg = arg_list[j];
// check if the arg string has a glob pattern
if (strpbrk(arg, "*?[") != NULL) {
glob_t glob_result;
memset(&glob_result, 0, sizeof(glob_result));
// use glob to expand the arg
int ret =
glob(arg, GLOB_TILDE | GLOB_BRACE, NULL, &glob_result);
if (ret == 0) {
// if there is a nonzero number of matches malloc new arg
// memory
if (glob_result.gl_pathc > 0) {
char **new_args = (char **)malloc(
(glob_result.gl_pathc + 1) * sizeof(char *));
if (new_args == NULL) {
perror("malloc failed");
globfree(&glob_result);
return;
}
for (size_t k = 0; k < glob_result.gl_pathc; k++) {
new_args[k] = strdup(glob_result.gl_pathv[k]);
if (new_args[k] == NULL) {
perror("strdup failed");
for (size_t l = 0; l < k; l++) {
free(new_args[l]);
}
free(new_args);
globfree(&glob_result);
return;
}
}
new_args[glob_result.gl_pathc] = NULL;
// replace the original arg with new list
arg_list[j] = new_args[0];
for (size_t k = 1; k < glob_result.gl_pathc; k++) {
// insert the new argument
arg_list[j + k] = new_args[k];
}
free(new_args);
}
} else if (ret == GLOB_NOMATCH) {
} else {
fprintf(stderr, "glob failed with error code %d\n", ret);
}
globfree(&glob_result);
}
}
}
}
static int is_operator(const char *str) {
const char *operators[] = {"1>>", "2>>", "&>>", ">>", "1>", "2>", "&>",
"||", "&&", ">", "&", ";", "|"};
int num_operators = sizeof(operators) / sizeof(operators[0]);
for (int i = 0; i < num_operators; i++) {
if (strncmp(str, operators[i], strlen(operators[i])) == 0) {
switch (i) {
case 0:
return OP_APPEND_STDOUT;
case 1:
return OP_APPEND_STDERR;
case 2:
return OP_APPEND_BOTH;
case 3:
return OP_APPEND_STDOUT;
case 4:
return OP_REDIRECT_STDOUT;
case 5:
return OP_REDIRECT_STDERR;
case 6:
return OP_REDIRECT_BOTH;
case 7:
return OP_OR;
case 8:
return OP_AND;
case 9:
return OP_REDIRECT_STDOUT;
case 10:
return OP_BACKGROUND;
case 11:
return OP_SEMICOLON;
case 12:
return OP_PIPE;
default:
return 0;
}
}
}
return 0; // Not an operator
}
static int operator_length(const char *str) {
const char *operators[] = {"1>>", "2>>", "&>>", ">>", "1>", "2>", "&>",
"||", "&&", ">", "&", ";", "|"};
int num_operators = sizeof(operators) / sizeof(operators[0]);
for (int i = 0; i < num_operators; i++) {
if (strncmp(str, operators[i], strlen(operators[i])) == 0) {
switch (i) {
case 0:
case 1:
case 2:
return 3;
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
return 2;
case 9:
case 10:
case 11:
case 12:
return 1;
default:
return 0;
}
}
}
return 0;
}
static char *trim_whitespace(char *str) {
char *end;
// Trim leading space
while (isspace((unsigned char)*str))
str++;
if (*str == 0)
return str; // If all spaces, return empty string
// Trim trailing space
end = str + strlen(str) - 1;
while (end > str && isspace((unsigned char)*end))
end--;
*(end + 1) = '\0';
return str;
}
// Split the command based on various chaining operations
char **lush_split_commands(char *line) {
char **commands = calloc(16, sizeof(char *));
if (!commands) {
perror("calloc failed");
exit(1);
}
int pos = 0;
char *start = line;
bool in_quote = false;
while (*start) {
// Skip leading spaces
while (isspace((unsigned char)*start))
start++;
// Check for operators
int op_len = operator_length(start);
if (op_len > 0) {
// Allocate memory for operator command
char *operator_cmd = calloc(op_len + 1, sizeof(char));
strncpy(operator_cmd, start, op_len);
commands[pos++] = operator_cmd;
start += op_len;
} else {
// Collect regular commands until the next operator or end of string
char *next = start;
while (*next) {
if (*next == '"') {
in_quote = !in_quote;
}
next++;
if (!in_quote && is_operator(next))
break;
}
// Copy the command between start and next
char *command = strndup(start, next - start);
commands[pos++] = trim_whitespace(command);
start = next;
}
if (pos >= 16) {
// Resize if necessary
commands = realloc(commands, (pos + 16) * sizeof(char *));
if (!commands) {
perror("realloc failed");
exit(1);
}
}
}
commands[pos] = NULL;
return commands;
}
char ***lush_split_args(char **commands, int *status) {
int outer_pos = 0;
char ***command_args = calloc(128, sizeof(char **));
if (!command_args) {
perror("calloc failed");
exit(1);
}
for (int i = 0; commands[i]; i++) {
int pos = 0;
char **args = calloc(128, sizeof(char *));
if (!args) {
perror("calloc failed");
exit(1);
}
bool inside_string = false;
char *current_token = &commands[i][0];
for (int j = 0; commands[i][j]; j++) {
if (commands[i][j] == '"' && !inside_string) {
// beginning of a string
commands[i][j++] = '\0';
if (commands[i][j] != '"') {
inside_string = true;
current_token = &commands[i][j];
} else {
commands[i][j] = '\0';
current_token = &commands[i][++j];
}
} else if (inside_string) {
if (commands[i][j] == '"') {
// ending of a string
inside_string = false;
commands[i][j] = '\0';
args[pos++] = current_token;
current_token = NULL;
} else {
// character in string
continue;
}
} else if (commands[i][j] == ' ') {
// space delimeter
if (current_token && *current_token != ' ') {
args[pos++] = current_token;
}
current_token = &commands[i][j + 1]; // go past the space
commands[i][j] = '\0'; // null the space
} else if (commands[i][j] == '$' && commands[i][j + 1] &&
commands[i][j + 1] != ' ') {
// environment variable
args[pos++] = getenv(&commands[i][++j]);
while (commands[i][j]) {
++j;
}
current_token = &commands[i][j + 1];
} else {
// regular character
continue;
}
}
// verify that string literals are finished
if (inside_string) {
*status = -1;
return command_args;
} else if (current_token && *current_token != ' ') {
// tack on last arg
args[pos++] = current_token;
}
// add this commands args array to the outer array
command_args[outer_pos++] = args;
}
*status = outer_pos;
return command_args;
}
static int run_command(lua_State *L, char ***commands) {
// check if the command is a lua script
char *ext = strrchr(commands[0][0], '.');
if (ext) {
ext++;
if (strcmp(ext, "lua") == 0) {
return ((*builtin_func[LUSH_LUA])(L, commands));
}
}
// check shell builtins
for (int j = 0; j < lush_num_builtins(); j++) {
if (strcmp(commands[0][0], builtin_strs[j]) == 0) {
return ((*builtin_func[j])(L, commands));
}
}
return lush_execute_command(commands[0], STDIN_FILENO, STDOUT_FILENO);
}
static int run_command_redirect(lua_State *L, char ***commands, int operator) {
if (commands[2] == NULL)
return -1;
int mode = (operator>= OP_REDIRECT_STDOUT && operator<= OP_REDIRECT_BOTH)
? O_TRUNC
: O_APPEND;
int redirect_flags = 0;
if (operator== OP_REDIRECT_STDOUT || operator== OP_APPEND_STDOUT) {
redirect_flags = 1;
} else if (operator== OP_REDIRECT_STDERR || operator== OP_APPEND_STDERR) {
redirect_flags = 2;
} else {
redirect_flags = 3;
}
int saved_stdout = -1, saved_stderr = -1;
int fd = open(commands[2][0], O_WRONLY | O_CREAT | mode, 0644);
if (fd == -1) {
perror("invalid fd");
return -1;
}
// Redirect stdout
if (redirect_flags & 1) {
saved_stdout = dup(STDOUT_FILENO);
if (saved_stdout == -1) {
perror("dup stdout");
close(fd);
return -1;
}
if (dup2(fd, STDOUT_FILENO) == -1) {
perror("dup2 stdout");
close(fd);
close(saved_stdout);
return -1;
}
}
// Redirect stderr
if (redirect_flags & 2) {
saved_stderr = dup(STDERR_FILENO);
if (saved_stderr == -1) {
perror("dup stderr");
if (saved_stdout != -1) {
dup2(saved_stdout, STDOUT_FILENO);
close(saved_stdout);
}
close(fd);
return -1;
}
if (dup2(fd, STDERR_FILENO) == -1) {
perror("dup2 stderr");
if (saved_stdout != -1) {
dup2(saved_stdout, STDOUT_FILENO);
close(saved_stdout);
}
close(saved_stderr);
close(fd);
return -1;
}
}
close(fd);
// Run the command
run_command(L, commands);
// Restore stdout
if (saved_stdout != -1) {
if (dup2(saved_stdout, STDOUT_FILENO) == -1) {
perror("dup2 restore stdout");
close(saved_stdout);
return -1;
}
close(saved_stdout);
}
// Restore stderr
if (saved_stderr != -1) {
if (dup2(saved_stderr, STDERR_FILENO) == -1) {
perror("dup2 restore stderr");
close(saved_stderr);
return -1;
}
close(saved_stderr);
}
return 0;
}
// TODO: Allow background process to run lua script
static int run_command_background(lua_State *L, char ***commands) {
pid_t pid = fork();
if (pid < 0) {
perror("fork");
return -1;
} else if (pid == 0) {
execvp(commands[0][0], commands[0]);
perror("execvp background");
exit(EXIT_FAILURE);
}
printf("Process %d running in background\n", pid);
return 0;
}
int lush_execute_chain(lua_State *L, char ***commands, int num_commands) {
if (commands[0][0][0] == '\0') {
return 0;
}
int num_actions = (num_commands + 1) / 2;
int last_result = 0;
for (int i = 0; i < num_actions; i++) {
// Handle &&, ||, and ; operators
if (i > 0 && commands[0] != NULL) {
commands--;
if (last_result != 0) {
if (is_operator(commands[0][0]) == OP_AND) {
commands += 3;
continue;
}
} else {
if (is_operator(commands[0][0]) == OP_OR) {
commands += 3;
continue;
}
}
commands++;
if (is_operator(commands[0][0]) == OP_SEMICOLON) {
commands++;
}
}
// Handle other operations
if (commands[1] != NULL) {
int op_type = is_operator(commands[1][0]);
if (op_type == OP_PIPE) {
char ***pipe_commands =
malloc(sizeof(char **) * (num_actions - i));
int pipe_count = 0;
while (i < num_actions - 1 && op_type == OP_PIPE) {
pipe_commands[pipe_count++] = commands[0];
commands += 2;
i++;
if (i < num_actions - 1) {
op_type = is_operator(commands[1][0]);
} else {
break;
}
}
pipe_commands[pipe_count++] = commands[0];
last_result = lush_execute_pipeline(pipe_commands, pipe_count);
free(pipe_commands);
commands += 2;
continue;
} else if (op_type == OP_BACKGROUND) {
last_result = run_command_background(L, commands);
commands += 2;
continue;
} else if (op_type >= OP_REDIRECT_STDOUT &&
op_type <= OP_APPEND_BOTH) {
last_result = run_command_redirect(L, commands, op_type);
commands += 3;
continue;
}
}
// Run the command or move past the operator
if (commands[0] != NULL && !is_operator(commands[0][0])) {
last_result = run_command(L, commands);
commands += 2;
} else {
commands++;
}
}
return 0;
}
int lush_execute_pipeline(char ***commands, int num_commands) {
// no command given
if (commands[0][0][0] == '\0') {
return 0;
}
// create pipes for each command
int **pipes = malloc((num_commands - 1) * sizeof(int *));
for (int i = 0; i < num_commands - 1; i++) {
pipes[i] =
malloc(2 * sizeof(int)); // pipes[i][0] = in, pipes[i][1] = out
if (pipe(pipes[i]) == -1) {
perror("pipe");
return 0;
}
}
// execute commands in the pipeline
for (int i = 0; i < num_commands - 1; i++) {
int input_fd = (i == 0) ? STDIN_FILENO : pipes[i - 1][0];
int output_fd = pipes[i][1];
lush_execute_command(commands[i], input_fd, output_fd);
close(output_fd); // no longer need to write to this pipe
}
// execute last or only command
int input_fd =
(num_commands > 1) ? pipes[num_commands - 2][0] : STDIN_FILENO;
int output_fd = STDOUT_FILENO;
lush_execute_command(commands[num_commands - 1], input_fd, output_fd);
// close pipes
for (int i = 0; i < num_commands - 1; i++) {
close(pipes[i][0]);
close(pipes[i][1]);
free(pipes[i]);
}
free(pipes);
return 0;
}
static void build_alt_command(char *buffer, char **args) {
size_t offset = 0;
for (int i = 0; args[i]; i++) {
if (offset + strlen(args[i]) + 1 < BUFFER_SIZE) {
strcat(buffer, args[i]);
strcat(buffer, " ");
offset = strlen(buffer);
} else {
fprintf(stderr, "command too long\n");
exit(EXIT_FAILURE);
}
}
}
int lush_execute_command(char **args, int input_fd, int output_fd) {
// create child
pid_t pid;
int status;
struct sigaction sa;
if ((pid = fork()) == 0) {
// child process content
// restore default sigint for child
sa.sa_handler = SIG_DFL;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sigaction(SIGINT, &sa, NULL);
// redirect in and out fd's if needed
if (input_fd != STDIN_FILENO) {
dup2(input_fd, STDIN_FILENO);
close(input_fd);
}
if (output_fd != STDOUT_FILENO) {
dup2(output_fd, STDOUT_FILENO);
close(output_fd);
}
// execute the command
if (execvp(args[0], args) == -1) {
if (alt_shell) {
char command[BUFFER_SIZE] = {0};
build_alt_command(command, args);
execlp(alt_shell, alt_shell, "-c", command, (char *)NULL);
perror("alt shell");
} else {
perror("lush");
exit(EXIT_FAILURE);
}
}
} else if (pid < 0) {
// forking failed
perror("fork");
exit(EXIT_FAILURE);
} else {
// parent process
do {
waitpid(pid, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}
if (WIFEXITED(status)) {
return WEXITSTATUS(status);
}
return -1;
}
int lush_run(lua_State *L, char ***commands, int num_commands) {
if (commands[0][0] == NULL) {
// no command given
return 0;
}
return lush_execute_chain(L, commands, num_commands);
}
static void background_handler(int sig) {
// Reap all child processes
while (waitpid(-1, NULL, WNOHANG) > 0)
;
}
int main(int argc, char *argv[]) {
// check if the --version arg was passed
if (argc > 1 && strcmp(argv[1], "--version") == 0) {
#ifdef LUSH_VERSION
printf("Lunar Shell, version %s\n", LUSH_VERSION);
#endif
return 0;
}
// init lua state
lua_State *L = luaL_newstate();
if (!L) {
fprintf(stderr, "Failed to create Lua state\n");
return -1; // or handle appropriately
}
luaL_openlibs(L);
// --- Load compat modules and make them global ---
luaL_requiref(L, "bit32", luaopen_bit32, 1);
lua_pop(L, 1); // luaL_requiref leaves the module on the stack
luaL_requiref(L, "utf8", luaopen_utf8, 1);
lua_pop(L, 1);
// --- End pre-loading ---
lua_register_api(L);
lua_run_init(L);
// check if being run in command string mode
if (argc > 2 && strcmp(argv[1], "-c") == 0) {
// execute the command provided
char *command = argv[2];
char *expanded_command = lush_resolve_aliases(command);
char **commands = lush_split_commands(expanded_command);
int status = 0;
char ***args = lush_split_args(commands, &status);
if (status == -1) {
fprintf(stderr, "lush: Expected end of quoted string\n");
} else {
lush_expand_globs(args);
}
if (lush_run(L, args, status) != 0) {
exit(1);
}
// clean up
for (int i = 0; args[i]; i++) {
free(args[i]);
}
free(args);
free(commands);
free(expanded_command);
lua_close(L);
return 0;
}
// This is the corrected logic for running a script file non-interactively.
if (argc > 1) {
char *ext = strrchr(argv[1], '.');
if (ext && strcmp(ext, ".lua") == 0) {
const char *script_name = argv[1];
// The arguments for the script start at argv[2].
// We create a pointer to that part of the array.
char **script_args = (argc > 2) ? &argv[2] : NULL;
// Call the script loader directly with the script and its arguments.
if (lua_load_script(L, script_name, script_args) != 0) {
exit(1); // Exit if the script had an error
}
lua_close(L); // Clean up and exit
return 0;
}
}
// --- Interactive Shell Mode ---
// eat ^C in main
struct sigaction sa_int;
sa_int.sa_handler = SIG_IGN;
sa_int.sa_flags = 0;
sigemptyset(&sa_int.sa_mask);
sigaction(SIGINT, &sa_int, NULL);
struct sigaction sa_bk;
sa_bk.sa_handler = &background_handler;
sigemptyset(&sa_bk.sa_mask);
sa_bk.sa_flags = SA_RESTART;
sigaction(SIGCHLD, &sa_bk, NULL);
// set custom envars
char hostname[256];
gethostname(hostname, sizeof(hostname));
setenv("HOSTNAME", hostname, 1);
char *cwd = getcwd(NULL, 0);
setenv("OLDPWD", cwd, 1);
free(cwd);
int status = 0;
while (true) {
// Prompt
char *prompt = get_prompt();
printf("%s ", prompt);
char *line = lush_read_line();
lush_push_history(line);
printf("\n");
if (line == NULL || strlen(line) == 0) {
free(line);
continue;
}
char *expanded_line = lush_resolve_aliases(line);
char **commands = lush_split_commands(expanded_line);
char ***args = lush_split_args(commands, &status);
if (status == -1) {
fprintf(stderr, "lush: Expected end of quoted string\n");
} else {
lush_expand_globs(args);
}
if (lush_run(L, args, status) != 0) {
exit(1);
}
for (int i = 0; args[i]; i++) {
free(args[i]);
}
// add last line to history
free(prompt);
free(args);
free(commands);
free(expanded_line);
free(line);
}
lua_close(L);
if (prompt_format != NULL)
free(prompt_format);
if (aliases != NULL)
free(aliases);
if (alt_shell != NULL)
free(alt_shell);
return 0;
}
|