-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.c
More file actions
151 lines (128 loc) · 3.49 KB
/
main.c
File metadata and controls
151 lines (128 loc) · 3.49 KB
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
#include <stdio.h>
#include <stdlib.h>
#include "shellparser.h"
#include "shellscanner.h"
#include <sys/types.h>
#include <signal.h>
#include "exec.h"
#include <pwd.h>
#include "termios.h"
#define MAX_NAME_LEN 256
#define MAX_PATH_LEN 1024
pid_t shell_pgid;
struct termios shell_tmodes;
int shell_terminal;
int shell_is_interactive;
char* buildin[256] = {"cd", "quit", "exit", "jobs", "fg", "bg"};
/* The active jobs are linked into a list. This is its head. */
job *first_job = NULL;
void* ParseAlloc (void* (*allocProc)(size_t));
void* Parse (void*, int, const char*, job*);
void* ParseFree (void*, void(*freeProc)(void*));
void type_prompt (void)
{
char path[MAX_PATH_LEN];
getcwd(path, MAX_PATH_LEN);
int i, ilast;
for (i = 0; i < MAX_PATH_LEN && path[i]; ++i)
{
if(path[i] == '/')
ilast = i;
}
printf("%s $shell: ", path + ilast + 1);
}
void handle_signal (int signo)
{
printf("\n");
type_prompt();
fflush(stdout);
}
void init_shell ()
{
/* See if we are running interactively. */
shell_terminal = STDIN_FILENO;
shell_is_interactive = isatty (shell_terminal);
if (shell_is_interactive)
{
/* Loop until we are in the foreground. */
while (tcgetpgrp (shell_terminal) != (shell_pgid = getpgrp ()))
kill (-shell_pgid, SIGTTIN);
/* Ignore interactive and job-control signals. */
signal (SIGINT, handle_signal);
signal (SIGQUIT, SIG_IGN);
signal (SIGTSTP, SIG_IGN);
signal (SIGTTIN, SIG_IGN);
signal (SIGTTOU, SIG_IGN);
//signal (SIGCHLD, SIG_IGN);
/* Put ourselves in our own process group. */
shell_pgid = getpid ();
if (setpgid (shell_pgid, shell_pgid) < 0)
{
perror ("Couldn't put the shell in its own process group");
exit (1);
}
/* Grab control of the terminal. */
tcsetpgrp (shell_terminal, shell_pgid);
/* Save default terminal attributes for shell. */
tcgetattr (shell_terminal, &shell_tmodes);
}
}
int parse_commands (yyscan_t scanner, job* j)
{
// Set up the parser
void* shellParser = ParseAlloc(malloc);
int lexCode;
do {
lexCode = yylex(scanner);
Parse(shellParser, lexCode, strdup(yyget_text(scanner)), j);
if (lexCode == EOL)
{
Parse(shellParser, 0, NULL, j);
break;
}
} while (lexCode > 0);
// Cleanup the parser
ParseFree(shellParser, free);
if (-1 == lexCode) {
fprintf(stderr, "The scanner encountered an error.\n");
return -1;
}
else if (0 == lexCode)
return 0;
return 1;
}
int main (int argc, char** argv) {
init_shell();
int flag;
// Set up the scanner
yyscan_t scanner;
yylex_init(&scanner);
yyset_in(stdin, scanner);
int id = 1;
do {
type_prompt();
job *j = create_job();
flag = parse_commands(scanner, j);
if(j->valid > 0)
{
if(first_job)
{
job *t;
for(t = first_job; t->next; t = t->next);
t->next = j;
}
else
first_job = j;
launch_job(j, j->foreground, &id);
do_job_notification();
//print_job(first_job);
}
else if (j->valid < 0)
{
do_job_notification();
}
else (free_job(j));
} while(flag == 1);
yylex_destroy(scanner);
return 0;
}