-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyshell.c
937 lines (831 loc) · 25.1 KB
/
myshell.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
#include "myshell.h"
// Global variables to hold process IDs
pid_t pid = -1;
pid_t pipe_pid = -1;
// Global struct and variables to store user variables
typedef struct
{
char name[MAX_COMMAND_LENGTH];
char value[MAX_COMMAND_LENGTH];
} Variable;
Variable variables[MAX_ARG_COUNT];
int variable_count = 0;
// Global variable to store the exit status of the last executed command
int last_exit_status = 0;
// Global variables to be reached from any function needed
int amper, redirect_out, redirect_err, redirect_out_app;
char *outfile, *errfile;
// Global variables to be handle history of commands
char command_history[MAX_HISTORY_SIZE][MAX_COMMAND_LENGTH];
int history_count = 0;
int current_history_index = -1;
char command[MAX_COMMAND_LENGTH];
char curr_command[MAX_COMMAND_LENGTH];
char last_command[MAX_COMMAND_LENGTH] = "";
// Global variable to store prompt name
char *prompt_name;
struct termios orig_termios;
// Disables raw mode and restores original terminal settings
void disable_raw_mode()
{
tcsetattr(STDIN_FILENO, TCSANOW, &orig_termios);
}
// Enables raw mode for the terminal to handle each keystroke directly
void enable_raw_mode()
{
tcgetattr(STDIN_FILENO, &orig_termios);
atexit(disable_raw_mode);
struct termios raw = orig_termios;
raw.c_lflag &= ~(ECHO | ICANON);
raw.c_cc[VMIN] = 1;
raw.c_cc[VTIME] = 0;
tcsetattr(STDIN_FILENO, TCSANOW, &raw);
}
// Prints the exit status of the last executed command
void print_status()
{
printf("Last command exit status: %d\n", last_exit_status);
}
// Signal handler for SIGINT (Ctrl+C), prints a message and attempts to kill child processes
void handle_sigint()
{
printf("\nYou typed Control-C!\n");
// Check if the process IDs are valid and active
if (pid > 0)
{
// Kill the child process or process group
killpg(pid, SIGKILL);
}
if (pipe_pid > 0)
{
// Kill the child process or process group
killpg(pipe_pid, SIGKILL);
}
}
// Function to trim leading and trailing spaces
char *trim(char *str)
{
char *end;
// Trim leading space
while (isspace((unsigned char)*str))
str++;
if (*str == 0)
return str; // All spaces?
// Trim trailing space
end = str + strlen(str) - 1;
while (end > str && isspace((unsigned char)*end))
end--;
// Write new null terminator
*(end + 1) = 0;
return str;
}
// Custom function to duplicate a string
char *my_strdup(const char *s)
{
size_t len = strlen(s) + 1;
char *dup = malloc(len);
if (dup)
{
memcpy(dup, s, len);
}
return dup;
}
// Function to split a string by a delimiter and handle multiple spaces
char **split_string(const char *str, const char delimiter, int *num_tokens)
{
int count = 0;
const char *temp = str;
// Count the number of delimiters
while (*temp)
{
if (*temp == delimiter)
count++;
temp++;
}
// Allocate memory for tokens
char **tokens = malloc((count + 1) * sizeof(char *));
if (tokens == NULL)
{
fprintf(stderr, "Memory allocation failed\n");
exit(EXIT_FAILURE);
}
int index = 0;
char *start = my_strdup(str); // Duplicate the input string
if (start == NULL)
{
fprintf(stderr, "Memory allocation failed\n");
free(tokens);
exit(EXIT_FAILURE);
}
char *end = strchr(start, delimiter);
while (end != NULL)
{
*end = '\0';
tokens[index++] = trim(start);
start = end + 1;
end = strchr(start, delimiter);
}
tokens[index++] = trim(start);
*num_tokens = index;
return tokens;
}
// Allocates memory for a 3D array of strings to store multiple commands and arguments
void argvAllocate(char ****argv)
{
char ***argvVal = *argv;
for (int i = 0; i < MAX_ARG_COUNT; i++)
{
argvVal[i] = (char **)(malloc(sizeof(char *) * 10));
for (int j = 0; j < MAX_COMMAND_LENGTH; j++)
{
argvVal[i][j] = (char *)(malloc(sizeof(char *) * MAX_SUBCOMMAND_LENGTH));
}
}
}
// Parses a command string into individual commands and arguments, updating argc and argv_count
void parse_command(char *command, char ****argv, int *argc, int *argv_count)
{
int num_tokens;
char ***argvArray = *argv;
char **commands = split_string(command, '|', &num_tokens);
*argv_count = num_tokens;
for (int i = 0; i < num_tokens; i++)
{
// Tokenize each command by spaces
int num_subtokens;
argvArray[i] = split_string(commands[i], ' ', &num_subtokens);
argc[i] = num_subtokens;
}
free(commands); // Free the memory allocated for commands
}
// Retrieves the value of a shell variable given its name
char *get_variable_value(const char *name)
{
for (int i = 0; i < variable_count; i++)
{
if (strcmp(variables[i].name, name) == 0)
{
return variables[i].value;
}
}
return NULL;
}
// Sets the value of a shell variable, adding it if it does not exist
void set_variable_value(const char *name, const char *value)
{
// check if variable already exist
for (int i = 0; i <= variable_count; i++)
{
if (strcmp(variables[i].name, name) == 0)
{
strcpy(variables[i].value, value);
return;
}
}
if (variable_count < MAX_ARG_COUNT)
{
strcpy(variables[variable_count].name, name);
strcpy(variables[variable_count].value, value);
variable_count++;
}
else
{
fprintf(stderr, "Max variable count reached.\n");
}
}
// Adds a command to the history, shifting the oldest commands if the history is full
void add_to_history(const char *command)
{
if (history_count < MAX_HISTORY_SIZE)
{
strcpy(command_history[history_count], command);
history_count++;
}
else
{
// Shift existing history to make space for new command
for (int i = 0; i < MAX_HISTORY_SIZE - 1; i++)
{
strcpy(command_history[i], command_history[i + 1]);
}
strcpy(command_history[MAX_HISTORY_SIZE - 1], command);
}
current_history_index = history_count;
}
// Displays a command from the history at the current history index
void display_command_from_history(char *command, const char *prompt_name)
{
if (current_history_index >= 0 && current_history_index < history_count)
{
strcpy(command, command_history[current_history_index]);
printf("\r%s: %s\033[K", prompt_name, command); // Clear line after the command
fflush(stdout);
}
}
// Handles arrow key presses to navigate through command history and updates the current command
void handle_arrow_key_press(int key, char *command, const char *prompt_name)
{
if (key == UP_ARROW)
{
if (current_history_index > 0)
{
current_history_index--;
display_command_from_history(command, prompt_name);
}
}
else if (key == DOWN_ARROW)
{
if (current_history_index < history_count)
{
current_history_index++;
if (current_history_index == history_count)
{
// Clear the line for new command
printf("\r%s: \033[K", prompt_name);
fflush(stdout);
command[0] = '\0'; // Clear the command buffer
}
else
{
display_command_from_history(command, prompt_name);
}
}
}
}
// Reads user input with command history navigation support, storing the input in the command buffer
void read_input_with_history(char *command, const char *prompt_name)
{
enable_raw_mode();
int c;
int pos = 0;
memset(command, 0, MAX_COMMAND_LENGTH);
printf("%s ", prompt_name);
fflush(stdout);
while (1)
{
signal(SIGINT, handle_sigint);
if ((c = getchar()) == EOF)
{
printf("%s: ", prompt_name);
continue;
}
if (c == ESCAPE_KEY)
{
if (getchar() == '[')
{
switch (getchar())
{
case 'A': // Up arrow
handle_arrow_key_press(UP_ARROW, command, prompt_name);
pos = strlen(command);
break;
case 'B': // Down arrow
handle_arrow_key_press(DOWN_ARROW, command, prompt_name);
pos = strlen(command);
break;
}
}
}
else if (c == '\n')
{
command[pos] = '\0';
printf("\n");
break;
}
else if (c == BACKSPACE)
{
if (pos > 0)
{
pos--;
command[pos] = '\0';
printf("\b \b"); // Move cursor back, print space, move cursor back again
fflush(stdout);
}
}
else
{
if (pos < MAX_COMMAND_LENGTH - 1)
{
command[pos++] = c;
printf("%c", c);
fflush(stdout);
}
}
}
disable_raw_mode();
}
// Handles the execution of commands connected by pipes, setting up file descriptors and forking processes
void handle_pipes(char ***argv, int argv_count)
{
int fildes[2];
int fildes_prev[2];
int status;
pid_t pid;
for (int i = 0; i < argv_count; i++)
{
if (i < argv_count - 1)
{
// Create a pipe
if (pipe(fildes) == -1)
{
perror("pipe");
exit(1);
}
}
// Fork a child process
pid = fork();
if (pid == 0)
{
// Child process
if (i > 0)
{
// Redirect input from the previous pipe
dup2(fildes_prev[0], STDIN_FILENO);
close(fildes_prev[0]);
close(fildes_prev[1]);
}
if (i < argv_count - 1)
{
// Redirect output to the next pipe
close(fildes[0]);
dup2(fildes[1], STDOUT_FILENO);
close(fildes[1]);
}
// Handle output redirection
if (redirect_out)
{
int fd = creat(outfile, 0660);
dup2(fd, STDOUT_FILENO);
close(fd);
}
else if (redirect_err)
{
int fd_err = creat(errfile, 0660);
dup2(fd_err, STDERR_FILENO);
close(fd_err);
}
else if (redirect_out_app)
{
int fd = open(outfile, O_WRONLY | O_CREAT | O_APPEND, 0660);
dup2(fd, STDOUT_FILENO);
close(fd);
}
if (execvp(argv[i][0], argv[i]) == -1)
{
fprintf(stderr, "Command execution failed: %s\n", strerror(errno));
exit(errno);
}
}
else if (pid > 0)
{
// Parent process
if (i > 0)
{
// Close the previous pipe
close(fildes_prev[0]);
close(fildes_prev[1]);
}
if (i < argv_count - 1)
{
// Save the current pipe for the next iteration
fildes_prev[0] = fildes[0];
fildes_prev[1] = fildes[1];
}
// Wait for the child process to finish
if (!amper)
{
waitpid(pid, &status, 0);
last_exit_status = status;
}
}
else
{
perror("fork");
exit(1);
}
}
}
// Parses and executes a simple if-else command structure within the shell
void execute_if_else(char *command)
{
int argc;
char **argv1 = split_string(command, ' ', &argc);
if (argc < 5 || strcmp(argv1[0], "if") != 0)
{
fprintf(stderr, "Invalid if statement syntax\n");
return;
}
int then_index = -1;
int else_index = -1;
int fi_index = -1;
// Find the positions of 'then', 'else', and 'fi' in the argument list
for (int i = 1; i < argc; i++)
{
if (strcmp(argv1[i], "then") == 0)
{
then_index = i;
}
else if (strcmp(argv1[i], "else") == 0)
{
else_index = i;
}
else if (strcmp(argv1[i], "fi") == 0)
{
fi_index = i;
}
}
if (then_index == -1 || else_index == -1 || fi_index == -1)
{
fprintf(stderr, "Invalid if statement syntax: must be then , else and fi\n");
return;
}
// Ensure 'then' comes before 'else' if both are present
if (then_index > else_index)
{
fprintf(stderr, "Invalid if statement syntax: 'then' must come before 'else'\n");
return;
}
if (else_index > fi_index)
{
fprintf(stderr, "Invalid if statement syntax: 'else' must come before 'fi'\n");
return;
}
// Extract the condition
char condition[MAX_COMMAND_LENGTH] = "";
for (int i = 1; i < then_index; i++)
{
strcat(condition, argv1[i]);
if (i < then_index - 1)
{
strcat(condition, " ");
}
}
int argc1[MAX_SUBCOMMAND_COUNTER] = {0};
int argv_count;
char ***argv;
argv = (char ***)malloc(MAX_ARG_COUNT * sizeof(char **));
if (argv == NULL)
{
fprintf(stderr, "Memory allocation failed for argv\n");
return; // Return error code
}
argvAllocate(&argv);
parse_command(condition, &argv, argc1, &argv_count);
int fd;
int po;
// Expand commands
expand_commands(&argv, &po, argc1, condition);
int original_stdout = dup(STDOUT_FILENO);
// Handle output redirection
if (redirect_out)
{
fd = creat(outfile, 0660);
dup2(fd, STDOUT_FILENO);
close(fd);
}
else if (redirect_err)
{
fd = creat(errfile, 0660);
dup2(fd, STDERR_FILENO);
close(fd);
}
else if (redirect_out_app)
{
fd = open(outfile, O_WRONLY | O_CREAT | O_APPEND, 0660);
dup2(fd, STDOUT_FILENO);
close(fd);
}
// // Execute the condition command
handle_pipes(argv, argv_count);
// Restore the original file descriptor of stdout
dup2(original_stdout, STDOUT_FILENO);
close(original_stdout);
// Check the condition command's exit status
if (WIFEXITED(last_exit_status))
{
int condition_exit_status = last_exit_status;
if (condition_exit_status == 0)
{
// Condition is true
if (then_index != -1 && else_index != -1)
{
// 'then' and 'else' blocks both exist
for (int i = then_index + 1; i < else_index; i++)
{
// Execute the 'then' block
char *then_argv[MAX_ARG_COUNT];
int then_argc = 0;
while (argv1[i] != NULL && strcmp(argv1[i], "else") != 0)
{
then_argv[then_argc++] = argv1[i++];
}
then_argv[then_argc] = NULL;
// Create a temporary 3D array to pass to expand_commands
char ****temp_argv = (char ****)malloc(sizeof(char ***));
*temp_argv = (char ***)malloc(sizeof(char **));
(*temp_argv)[0] = then_argv;
int need_fork = 1;
expand_commands(temp_argv, &need_fork, &then_argc, then_argv[0]);
free(*temp_argv);
free(temp_argv);
if (need_fork == 0)
{
continue;
}
pid_t then_pid = fork();
if (then_pid == 0)
{
execvp(then_argv[0], then_argv);
perror("execvp failed");
exit(EXIT_FAILURE);
}
else if (then_pid > 0)
{
int then_status;
waitpid(then_pid, &then_status, 0);
}
else
{
perror("fork failed");
}
}
}
else if (then_index != -1 && else_index == -1 && fi_index != -1)
{
// 'then' block only exists
for (int i = then_index + 1; i < fi_index; i++)
{
// Execute the 'then' block
char *then_argv[MAX_ARG_COUNT];
int then_argc = 0;
while (argv1[i] != NULL && strcmp(argv1[i], "fi") != 0)
{
then_argv[then_argc++] = argv1[i++];
}
then_argv[then_argc] = NULL;
// Create a temporary 3D array to pass to expand_commands
char ****temp_argv = (char ****)malloc(sizeof(char ***));
*temp_argv = (char ***)malloc(sizeof(char **));
(*temp_argv)[0] = then_argv;
int need_fork = 1;
expand_commands(temp_argv, &need_fork, &then_argc, then_argv[0]);
free(*temp_argv);
free(temp_argv);
if (need_fork == 0)
{
continue;
}
pid_t then_pid = fork();
if (then_pid == 0)
{
execvp(then_argv[0], then_argv);
perror("execvp failed");
exit(EXIT_FAILURE);
}
else if (then_pid > 0)
{
int then_status;
waitpid(then_pid, &then_status, 0);
}
else
{
perror("fork failed");
}
}
}
}
else if (else_index != -1 && fi_index != -1)
{
// 'else' block exists
for (int i = else_index + 1; i < fi_index; i++)
{
// Execute the 'else' block
char *else_argv[MAX_ARG_COUNT];
int else_argc = 0;
while (argv1[i] != NULL && strcmp(argv1[i], "fi") != 0)
{
else_argv[else_argc++] = argv1[i++];
}
else_argv[else_argc] = NULL;
// Create a temporary 3D array to pass to expand_commands
char ****temp_argv = (char ****)malloc(sizeof(char ***));
*temp_argv = (char ***)malloc(sizeof(char **));
(*temp_argv)[0] = else_argv;
int need_fork = 1;
expand_commands(temp_argv, &need_fork, &else_argc, else_argv[0]);
free(*temp_argv);
free(temp_argv);
if (need_fork == 0)
{
continue;
}
pid_t else_pid = fork();
if (else_pid == 0)
{
execvp(else_argv[0], else_argv);
perror("execvp failed");
exit(EXIT_FAILURE);
}
else if (else_pid > 0)
{
int else_status;
waitpid(else_pid, &else_status, 0);
}
else
{
perror("fork failed");
}
}
}
}
}
// Expands shell-specific commands or variables in the given command string and updates argv
void expand_commands(char ****argv, int *need_fork, int *argc, char *command)
{
char ***argvMat = *argv;
// Check for the !! command
if (strcmp(command, "!!") == 0)
{
if (strlen(last_command) == 0)
{
printf("No previous command to repeat.\n");
*need_fork = 0;
return;
}
strcpy(command, last_command);
int num_subtokens;
argvMat[0] = split_string(last_command, ' ', &num_subtokens);
}
else
{
strcpy(last_command, command); // Store the current command as the last command
}
strcpy(curr_command, command);
// Check if the command is empty
if (argvMat[0][0] == NULL)
{
*need_fork = 0;
return;
}
int argc1 = argc[0];
// Check for background execution
if (argc1 > 0 && strcmp(argvMat[0][argc1 - 1], "&") == 0)
{
amper = 1;
argvMat[0][argc1 - 1] = NULL;
}
else
{
amper = 0;
}
add_to_history(curr_command);
// Check for output redirection
if (argc1 > 2 && strcmp(argvMat[0][argc1 - 2], ">") == 0)
{
redirect_out = 1;
argvMat[0][argc1 - 2] = NULL;
outfile = argvMat[0][argc1 - 1];
}
else if (argc1 > 2 && strcmp(argvMat[0][argc1 - 2], "2>") == 0)
{
redirect_err = 1;
argvMat[0][argc1 - 2] = NULL;
errfile = argvMat[0][argc1 - 1];
}
else if (argc1 > 2 && strcmp(argvMat[0][argc1 - 2], ">>") == 0)
{
redirect_out_app = 1;
argvMat[0][argc1 - 2] = NULL;
outfile = argvMat[0][argc1 - 1];
}
else
{
redirect_out = 0;
redirect_out_app = 0;
redirect_err = 0;
}
// Check for built-in commands
if (argc1 > 1 && strcmp(argvMat[0][0], "prompt") == 0)
{
free(prompt_name);
prompt_name = malloc(strlen(argvMat[0][argc1 - 1]) + 1);
if (prompt_name == NULL)
{
perror("Memory allocation failed");
exit(EXIT_FAILURE);
}
strcpy(prompt_name, argvMat[0][argc1 - 1]);
*need_fork = 0;
}
else if (argc1 > 1 && strcmp(argvMat[0][0], "echo") == 0)
{
if (strcmp(argvMat[0][1], "$?") == 0)
{
printf("%d \n", last_exit_status);
}
else
{
// Check for variable substitution
for (int i = 0; argvMat[0][i] != NULL; i++)
{
char *value = get_variable_value(argvMat[0][i]);
if (value != NULL)
{
argvMat[0][i] = value;
}
}
for (int i = 1; i < argc1; i++)
{
printf("%s ", argvMat[0][i]);
}
printf("\n");
}
*need_fork = 0;
}
else if (argc1 > 1 && strcmp(argvMat[0][0], "cd") == 0)
{
if (chdir(argvMat[0][1]) != 0)
{
perror("chdir failed");
}
*need_fork = 0;
}
else if (argc1 == 1 && strcmp(argvMat[0][0], "quit") == 0)
{
exit(EXIT_SUCCESS);
}
else if (argc1 > 2 && argvMat[0][argc1 - 2] != NULL && strcmp(argvMat[0][argc1 - 2], "=") == 0)
{
set_variable_value(argvMat[0][argc1 - 3], argvMat[0][argc1 - 1]);
*need_fork = 0;
}
else if (argc1 == 2 && strcmp(argvMat[0][0], "read") == 0)
{
char value[MAX_COMMAND_LENGTH];
if (fgets(value, sizeof(value), stdin) == NULL)
{
perror("fgets failed");
*need_fork = 0;
}
value[strcspn(value, "\n")] = '\0'; // Remove trailing newline
// Add a $ before argv1[1] using strcat
char var_name[MAX_COMMAND_LENGTH] = "$";
strcat(var_name, argvMat[0][1]);
set_variable_value(var_name, value);
*need_fork = 0;
}
}
int main()
{
char ***argv;
prompt_name = malloc(strlen("hello:") + 1);
if (prompt_name == NULL)
{
perror("Memory allocation failed");
exit(EXIT_FAILURE);
}
argv = (char ***)malloc(MAX_ARG_COUNT * sizeof(char **));
if (argv == NULL)
{
fprintf(stderr, "Memory allocation failed for argv\n");
return 1; // Return error code
}
argvAllocate(&argv);
strcpy(prompt_name, "hello:");
// Save the original stderr file descriptor
int original_stderr = dup(STDERR_FILENO);
// Register the signal handler for SIGINT
signal(SIGINT, handle_sigint);
while (1)
{
// Register the signal handler for SIGINT
signal(SIGINT, handle_sigint);
command[MAX_COMMAND_LENGTH - 1] = '\0'; // Remove trailing newline
int argc[MAX_SUBCOMMAND_COUNTER] = {0};
int argv_count;
int needfork = 1;
read_input_with_history(command, prompt_name);
parse_command(command, &argv, argc, &argv_count);
// Check if the command is empty
if (argv[0][0] == NULL)
continue;
// Handle if-else statements
if (argc[0] > 0 && strcmp(argv[0][0], "if") == 0)
{
execute_if_else(command);
needfork = 0;
}
// Expand commands
expand_commands(&argv, &needfork, argc, command);
if (needfork == 0)
{
continue;
}
// Handle the piping commands
handle_pipes(argv, argv_count);
}
// Close the original stderr file descriptor
close(original_stderr);
free(prompt_name);
return 0;
}