-
Notifications
You must be signed in to change notification settings - Fork 0
/
builtin.c
169 lines (141 loc) · 4.55 KB
/
builtin.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
#include "csapp.h"
#include "jobs.h"
#include "dirs.h"
#include "exit.h"
bool builtin_waiting = false;
jobid_t parse_jobid (char* arg) {
if (*arg == '\0') {
return INVALID_JOBID;
}
char* end = arg;
jobid_t jobid = strtol(arg, &end, 10);
if (*end != '\0') {
// L'argument ne contient pas que des digits
return INVALID_JOBID;
}
if (jobid <= 0 || jobid > MAXJOBS) {
return INVALID_JOBID;
}
return jobid - 1;
}
pid_t parse_pid(char* arg) {
if (*arg == '\0') {
return 0;
}
char* end = arg;
pid_t pid = strtol(arg, &end, 10);
return *end != '\0'
? 0 // L'argument ne contient pas que des digits
: pid;
}
// Essaie de lire l'argument 1 de `argv` en tant que jobid
// Verifie la correspondance entre le status du job et le masque `status`
// Si l'argument est vide, choisi un job par défaut en accordance avec `status`
//
// Retourne le jobid en question
// ou INVALID_JOBID si aucun job adéquate n'a été trouvé
jobid_t read_jobid (char** argv, JobStatus status) {
if (argv[1] == NULL) { // Pas d'argument spécifié pour la commande
// Cherche un jobid par défaut.
jobid_t jobid = jobs_find_first_by_status(status);
if (jobid == INVALID_JOBID) {
fprintf(stderr, "No default job found for command `%s`\n", argv[0]);
return INVALID_JOBID;
}
return jobid;
}
jobid_t jobid;
if (argv[1][0] == '%') {
// L'argument est un id de job
jobid = parse_jobid(argv[1] + 1);
if (jobid == INVALID_JOBID) {
fprintf(stderr, "Wrong jobid `%s`, expected number between 1 and %d\n",
argv[1] + 1, MAXJOBS);
return INVALID_JOBID;
}
if (job_status_match(jobid, FREE)) {
fprintf(stderr, "No job with id `%d`\n", jobid + 1);
return INVALID_JOBID;
}
}
else {
// L'argument est un id de processus
pid_t pid = parse_pid(argv[1]);
if (pid == 0) {
fprintf(stderr, "Invalid pid `%s`\n", argv[1]);
return INVALID_JOBID;
}
jobid = jobs_find_by_pid(pid);
if (jobid == INVALID_JOBID) {
fprintf(stderr, "No job found for pid `%d`\n", pid);
return INVALID_JOBID;
}
}
if (!job_status_match(jobid, status)) {
fprintf(stderr, "Can't use command `%s` for job with status `%s`\n",
argv[0], job_status_str(jobid));
return INVALID_JOBID;
}
return jobid;
}
void builtin_jobs () {
jobs_print(STOPPED | RUNNING, false);
}
void builtin_fg (char** argv) {
jobid_t jobid = read_jobid(argv, STOPPED | RUNNING);
if (jobid == INVALID_JOBID)
return;
job_fg_wait(jobid, true);
}
void builtin_bg (char** argv) {
jobid_t jobid = read_jobid(argv, STOPPED);
if (jobid != INVALID_JOBID)
job_change_status(jobid, SIGCONT);
}
void builtin_int (char** argv) {
jobid_t jobid = read_jobid(argv, STOPPED | RUNNING);
if (jobid != INVALID_JOBID)
job_change_status(jobid, SIGINT);
}
void builtin_stop (char** argv) {
jobid_t jobid = read_jobid(argv, RUNNING);
if (jobid != INVALID_JOBID)
job_change_status(jobid, SIGSTOP);
}
void builtin_term (char** argv) {
jobid_t jobid = read_jobid(argv, STOPPED | RUNNING);
if (jobid != INVALID_JOBID)
job_change_status(jobid, SIGTERM);
}
void builtin_wait () {
jobid_t jobid;
builtin_waiting = true;
while (builtin_waiting && (jobid = jobs_find_first_by_status(~(FREE | DONE | STOPPED))) != INVALID_JOBID) {
sleep(0);
jobs_print_update();
}
builtin_waiting = false;
}
#define builtin(name, exec) if (strcmp(argv[0], name) == 0) { exec; return true; }
// si le premier parametre est une commande integree,
// l'executer et renvoyer "vrai"
bool builtin_command(char **argv) {
if (argv[0] == NULL) // commande vide
return true;
if (strcmp(argv[0], "&") == 0) // ignorer & tout seul
return true;
builtin("exit", exit_try());
builtin("quit", exit_try());
builtin("jobs", builtin_jobs());
builtin("fg", builtin_fg(argv));
builtin("bg", builtin_bg(argv));
builtin("int", builtin_int(argv));
builtin("term", builtin_term(argv));
builtin("stop", builtin_stop(argv));
builtin("wait", builtin_wait());
builtin("dirs", dirs_print());
builtin("cd", dirs_cd(argv[1]));
builtin("pushd", dirs_pushd(argv[1]));
builtin("popd", dirs_popd());
return false; // ce n'est pas une commande integree
}