-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
91 lines (81 loc) · 2.43 KB
/
main.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
/**
* NAME: smallsh - a small shell program
* SYNOPSIS: smallsh
* DESCRIPTION:
* Implements a subset of features of well-known shells, such as bash:
* - Provides a prompt for running commands
* - Handles blank lines for comments (beginning with '#')
* - Provides expansion for the variable $$
* - Executes 3 commands built into the shell: exit, cd, and status
* - Executes other commands by creating new processes using a function
* from the exec family of functions
* - Supports input and output redirection
* - Supports running commands in foreground and background processes
* - Uses custom handlers for 2 signals: SIGINT and SIGTSTP
* AUTHOR: Allen Blanton (CS 344, Spring 2022)
*/
#define _POSIX_SOURCE
#include <stdlib.h>
#include <string.h>
#include "llist.h"
#include "signal_handlers.h"
#include "input_parsing.h"
#include "process_control.h"
#include "shell_commands.h"
#include "utilities.h"
// Boolean for foreground-only mode
volatile sig_atomic_t fg_mode = 0;
int
main(void)
{
// Declare parent signal behavior
struct sigaction ignore_action, SIGTSTP_action = {0};
SIGTSTP_action.sa_handler = toggle_fg_mode_on;
SIGTSTP_action.sa_flags = 0;
ignore_action.sa_handler = SIG_IGN;
sigaction(SIGTSTP, &SIGTSTP_action, NULL); // parent will catch SIGTSTP
sigaction(SIGINT, &ignore_action, NULL); // parent will ignore SIGINT
int exitStatus = 0;
struct Input *input = get_userinput();
struct Llist *bgLlist = init_llist(); // llist to keep track of bg processes
// Parse user input
while (1)
{
if (input->args == NULL || input->args[0][0] == '#')
{ // ignore empty inputs and comments; skip to end of if/else block
}
else if (!strcmp(input->args[0], "exit") && !builtin_exit(input))
{
break;
}
else if (!strcmp(input->args[0], "status"))
{
builtin_status(input, exitStatus);
}
else if (!strcmp(input->args[0], "cd"))
{
builtin_cd(input);
}
else
{ // try to execute non-built-in command
if (!fg_mode && input->background)
{
append_node(bgLlist, fork_child_bg(input));
}
else
{
exitStatus = fork_child_fg(input);
}
}
// Attempt to reap any bg processes
reap(bgLlist);
// Proceed to the next prompt for user input
cleanup_input(input);
input = get_userinput();
}
// Final cleanup
kill_bg(bgLlist);
cleanup_llist(bgLlist);
cleanup_input(input);
return EXIT_SUCCESS;
}