-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPipeCommand.cc
More file actions
134 lines (110 loc) · 3.42 KB
/
PipeCommand.cc
File metadata and controls
134 lines (110 loc) · 3.42 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
WeChat: cstutorcs
QQ: 749389476
Email: tutorcs@163.com
/*
* CS252: Shell project
*
* Template file.
* You will need to add more code here to execute the command table.
*
* NOTE: You are responsible for fixing any bugs this code may have!
*
* DO NOT PUT THIS PROJECT IN A PUBLIC REPOSITORY LIKE GIT. IF YOU WANT
* TO MAKE IT PUBLICALLY AVAILABLE YOU NEED TO REMOVE ANY SKELETON CODE
* AND REWRITE YOUR PROJECT SO IT IMPLEMENTS FUNCTIONALITY DIFFERENT THAN
* WHAT IS SPECIFIED IN THE HANDOUT. WE OFTEN REUSE PART OF THE PROJECTS FROM
* SEMESTER TO SEMESTER AND PUTTING YOUR CODE IN A PUBLIC REPOSITORY
* MAY FACILITATE ACADEMIC DISHONESTY.
*/
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include "PipeCommand.hh"
#include "Shell.hh"
PipeCommand::PipeCommand() {
// Initialize a new vector of Simple PipeCommands
_simpleCommands = std::vector<SimpleCommand *>();
_outFile = NULL;
_inFile = NULL;
_errFile = NULL;
_background = false;
}
void PipeCommand::insertSimpleCommand( SimpleCommand * simplePipeCommand ) {
// add the simple command to the vector
_simpleCommands.push_back(simplePipeCommand);
}
void PipeCommand::clear() {
// deallocate all the simple commands in the command vector
for (auto simplePipeCommand : _simpleCommands) {
delete simplePipeCommand;
}
// remove all references to the simple commands we've deallocated
// (basically just sets the size to 0)
_simpleCommands.clear();
if ( _outFile ) {
delete _outFile;
}
_outFile = NULL;
if ( _inFile ) {
delete _inFile;
}
_inFile = NULL;
if ( _errFile ) {
delete _errFile;
}
_errFile = NULL;
_background = false;
}
void PipeCommand::print() {
printf("\n\n");
//printf(" COMMAND TABLE \n");
printf("\n");
printf(" # Simple PipeCommands\n");
printf(" --- ----------------------------------------------------------\n");
int i = 0;
// iterate over the simple commands and print them nicely
for ( auto & simplePipeCommand : _simpleCommands ) {
printf(" %-3d ", i++ );
simplePipeCommand->print();
}
printf( "\n\n" );
printf( " Output Input Error Background\n" );
printf( " ------------ ------------ ------------ ------------\n" );
printf( " %-12s %-12s %-12s %-12s\n",
_outFile?_outFile->c_str():"default",
_inFile?_inFile->c_str():"default",
_errFile?_errFile->c_str():"default",
_background?"YES":"NO");
printf( "\n\n" );
}
void PipeCommand::execute() {
// Don't do anything if there are no simple commands
if ( _simpleCommands.size() == 0 ) {
Shell::TheShell->prompt();
return;
}
// Print contents of PipeCommand data structure
print();
// Add execution here
// For every simple command fork a new process
// Setup i/o redirection
// and call exec
// Clear to prepare for next command
clear();
// Print new prompt
//Shell::TheShell->prompt();
}
// Expands environment vars and wildcards of a SimpleCommand and
// returns the arguments to pass to execvp.
char **
PipeCommand::expandEnvVarsAndWildcards(SimpleCommand * simpleCommandNumber)
{
simpleCommandNumber->print();
return NULL;
}