-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecuteHelper.cpp
More file actions
90 lines (76 loc) · 2.82 KB
/
executeHelper.cpp
File metadata and controls
90 lines (76 loc) · 2.82 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
#include "executeHelper.h"
#include <unistd.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <sys/wait.h>
#include <iomanip>
#include "Commands.h"
#include <cstring>
#include <regex>
#include <cstddef>
#include <thread>
#include <fcntl.h>
#include <cmath>
#include <experimental/filesystem>
#include <sys/stat.h>
#include <memory>
using namespace std;
void ForegroundCommand::executeFgByID(){
std::string job_id_s = this->getCmdArgs()[1];
try {
int job_id = stoi(job_id_s);
if (this->m_small_shell.getJobsList().getJobById(job_id) == nullptr) {
std::cerr << "smash error: fg: job-id " << job_id << " does not exist" << std::endl;
return;
}
auto job = this->m_small_shell.getJobsList().getJobById(job_id);
std::cout << job->getCommand() << " " << job->getPid() << std::endl;
this->m_small_shell.setFgProcPID(job->getPid());
waitpid(job->getPid(), nullptr, 0);
this->m_small_shell.getJobsList().removeJobById(job->getJobId());
if(job_id == this->m_small_shell.getJobsList().getMaxId()){
this->m_small_shell.getJobsList().updateMaxJobId();
}
this->m_small_shell.setFgProcPID(0);
}
catch(...){
std::cerr << "smash error: fg: invalid arguments" << std::endl;
return;
}
}
void ForegroundCommand::executeLastId(){
if(this->m_small_shell.getJobsList().getJobsList().empty()){
std::cerr << "smash error: fg: jobs list is empty" << std::endl;
return;
}
auto job = this->m_small_shell.getJobsList().getLastJob();
this->m_small_shell.setFgProcPID(job->getPid());
std::cout << job->getCommand() << " " << job->getPid() << std::endl;
waitpid(job->getPid(), nullptr,0);
this->m_small_shell.getJobsList().removeJobById(job->getJobId());
this->m_small_shell.getJobsList().updateMaxJobId();
this->m_small_shell.setFgProcPID(0);
}
void UnAliasCommand::removeAllAlias(int numOfArgs){
std::map<string,string>& alias_map = this->m_small_shell.getMap();
std::vector<std::string>& ordered_vector = this->m_small_shell.getOrder();
for(int i = 1; i < numOfArgs; ++i){
bool exists = alias_map.find(this->getCmdArgs()[i]) != alias_map.end();
string alias_to_remove = this->getCmdArgs()[i];
if(exists){
alias_map.erase(alias_to_remove);
auto alias_to_remove_ord = std::find(ordered_vector.begin(),
ordered_vector.end(), this->getCmdArgs()[i]);
ordered_vector.erase(alias_to_remove_ord);
}
else{
std::cerr << "smash error: " + alias_to_remove + " alias does not exist" << std::endl;
break;
}
}
}
void JobsList::updateMaxJobId(){
this->setMaxId(this->getLastJob()->getJobId());
}