Dev - C++ - Implementar redirecciones y tuberias en una shell en C++(Ayudaa)

 
Vista:

Implementar redirecciones y tuberias en una shell en C++(Ayudaa)

Publicado por Yazaida Hernandez (2 intervenciones) el 16/12/2012 04:36:08
a ver empiezo...la idea es que estoy creando una mini shell, ya me ejeuta los comandos basicos y ahora tengo que implementarle en primer lugar las redirecciones, esta seria mi estructura actual:

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
bool defaultCommandCallback(const std::string& command,
cli::ShellArguments const& arguments)
{
using namespace cli::prettyprint;
int status1;
while (waitpid(-1, NULL, WNOHANG) > 0);
pid_t cpid = fork();
 
//hijo
if (cpid==0) {
 
if (!arguments.arguments.empty()){
 
char** argv=cli::utility::stdVectorStringToArgV(arguments .arguments);
execvp(argv[0],argv);
 
//error
std::cout << "Se ha producido un error al invocar el comando";
exit(-1);
}
//padre
}else if (cpid > 0) {
if (arguments.terminator==cli::ShellArguments::NORMAL ){
waitpid(cpid, &status1, 0);
}
}else{
std::cout << "Se ha producido un error al invocar el comando";
 
}

a ver empiezo...la idea es que estoy creando una mini shell, ya me ejeuta los comandos basicos y ahora tengo que implementarle en primer lugar las redirecciones, esta seria mi estructura actual:

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
bool defaultCommandCallback(const std::string& command,
cli::ShellArguments const& arguments)
{
using namespace cli::prettyprint;
int status1;
while (waitpid(-1, NULL, WNOHANG) > 0);
pid_t cpid = fork();
 
//hijo
if (cpid==0) {
 
if (!arguments.arguments.empty()){
 
char** argv=cli::utility::stdVectorStringToArgV(arguments .arguments);
execvp(argv[0],argv);
 
//error
std::cout << "Se ha producido un error al invocar el comando";
exit(-1);
}
//padre
}else if (cpid > 0) {
if (arguments.terminator==cli::ShellArguments::NORMAL ){
waitpid(cpid, &status1, 0);
}
}else{
std::cout << "Se ha producido un error al invocar el comando";
 
}
return false;
}

-------------------------------------------------------------------------------------------
ahora segun tengo entendido, dentro de el hijo deberia añadir algo mas o menos asi:
Aunque nose como generar el bucle for que me revise la salida del comando a ver si se trata de una redirección:

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
if (iter->type==StdioRedirection::TRUNCATED_INPUT){
int fd= open (iter -> argument.c.str(), O_RDONLY);
if (fd>=0){
dup2(fd,0);
close(fd);}
else{
std::cout << "Ha habido un error al tratar de abrir el archivo";}
}
 
if (iter->type==StdioRedirection::TRUNCATED_OUTPUT){
int fd= open (iter -> argument.c.str(), O_CREATE | O_TRUNC| O_RDWR , S_IWUSR| S_IRUSR| S_IRGRP |S_IWGRP | S_IXGRP | S_IROTH);
if (fd>=0){
dup2(fd,0);
close(fd);}
else{
std::cout << "Ha habido un error al tratar de abrir el archivo";}
}
 
if (iter->type==StdioRedirection::APPENDED_OUTPUT){
int fd= open (iter -> argument.c.str(), O_CREAT | O_APPEND | O_RDWR , S_IWUSR| S_IRUSR| S_IRGRP |S_IWGRP | S_IXGRP | S_IROTH );
if (fd>=0){
dup2(fd,0);
close(fd);}
else{std::cout << "Ha habido un error al tratar de abrir el archivo";}
 
}
  	Editar/Borrar Mensaje
return false;
}

-------------------------------------------------------------------------------------------
ahora segun tengo entendido, dentro de el hijo deberia añadir algo mas o menos asi:
Aunque nose como generar el bucle for que me revise la salida del comando a ver si se trata de una redirección:
a ver empiezo...la idea es que estoy creando una mini shell, ya me ejeuta los comandos basicos y ahora tengo que implementarle en primer lugar las redirecciones, esta seria mi estructura actual:

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
bool defaultCommandCallback(const std::string& command,
cli::ShellArguments const& arguments)
{
using namespace cli::prettyprint;
int status1;
while (waitpid(-1, NULL, WNOHANG) > 0);
pid_t cpid = fork();
 
//hijo
if (cpid==0) {
 
if (!arguments.arguments.empty()){
 
char** argv=cli::utility::stdVectorStringToArgV(arguments .arguments);
execvp(argv[0],argv);
 
//error
std::cout << "Se ha producido un error al invocar el comando";
exit(-1);
}
//padre
}else if (cpid > 0) {
if (arguments.terminator==cli::ShellArguments::NORMAL ){
waitpid(cpid, &status1, 0);
}
}else{
std::cout << "Se ha producido un error al invocar el comando";
 
}
return false;
}

-------------------------------------------------------------------------------------------
ahora segun tengo entendido, dentro de el hijo deberia añadir algo mas o menos asi:
Aunque nose como generar el bucle for que me revise la salida del comando a ver si se trata de una redirección:

if (iter->type==StdioRedirection::TRUNCATED_INPUT){
int fd= open (iter -> argument.c.str(), O_RDONLY);
if (fd>=0){
dup2(fd,0);
close(fd);}
else{
std::cout << "Ha habido un error al tratar de abrir el archivo";}
}

if (iter->type==StdioRedirection::TRUNCATED_OUTPUT){
int fd= open (iter -> argument.c.str(), O_CREATE | O_TRUNC| O_RDWR , S_IWUSR| S_IRUSR| S_IRGRP |S_IWGRP | S_IXGRP | S_IROTH);
if (fd>=0){
dup2(fd,0);
close(fd);}
else{
std::cout << "Ha habido un error al tratar de abrir el archivo";}
}

if (iter->type==StdioRedirection::APPENDED_OUTPUT){
int fd= open (iter -> argument.c.str(), O_CREAT | O_APPEND | O_RDWR , S_IWUSR| S_IRUSR| S_IRGRP |S_IWGRP | S_IXGRP | S_IROTH );
if (fd>=0){
dup2(fd,0);
close(fd);}
else{std::cout << "Ha habido un error al tratar de abrir el archivo";}

}
Editar/Borrar Mensaje
if (iter->type==StdioRedirection::TRUNCATED_INPUT){
int fd= open (iter -> argument.c.str(), O_RDONLY);
if (fd>=0){
dup2(fd,0);
close(fd);}
else{
std::cout << "Ha habido un error al tratar de abrir el archivo";}
}

if (iter->type==StdioRedirection::TRUNCATED_OUTPUT){
int fd= open (iter -> argument.c.str(), O_CREATE | O_TRUNC| O_RDWR , S_IWUSR| S_IRUSR| S_IRGRP |S_IWGRP | S_IXGRP | S_IROTH);
if (fd>=0){
dup2(fd,0);
close(fd);}
else{
std::cout << "Ha habido un error al tratar de abrir el archivo";}
}

if (iter->type==StdioRedirection::APPENDED_OUTPUT){
int fd= open (iter -> argument.c.str(), O_CREAT | O_APPEND | O_RDWR , S_IWUSR| S_IRUSR| S_IRGRP |S_IWGRP | S_IXGRP | S_IROTH );
if (fd>=0){
dup2(fd,0);
close(fd);}
else{std::cout << "Ha habido un error al tratar de abrir el archivo";}

}
Editar/Borrar Mensaje
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder