• Domingo 28 de Abril de 2024, 16:30

Autor Tema:  Implementar Shell en C++ (redirecciones y tuberias) para poder ejecutarlas  (Leído 1619 veces)

yasi92

  • Nuevo Miembro
  • *
  • Mensajes: 2
    • Ver Perfil
Implementar Shell en C++ (redirecciones y tuberias) para poder ejecutarlas
« en: Domingo 16 de Diciembre de 2012, 02:28 »
0
Holaaaa soy estudiante de informatica y necesito ayuda para implementar las redirecciones y tuberias en una shell. Mi problema es que esty en primero de carrera y desconozco el c++ para realizar esta practica de 2 año. Agradeceria mucho si alguien pudiera echarmee una mano...-.-". Son cosas sencillas pero con mis conocimientos me es imposible sacarlo.Gracias de antemano. Mi correo es yasi92_25@hotmail.com.

yasi92

  • Nuevo Miembro
  • *
  • Mensajes: 2
    • Ver Perfil
Re:Implementar Shell en C++ (redirecciones y tuberias) para poder ejecutarlas
« Respuesta #1 en: Domingo 16 de Diciembre de 2012, 04:39 »
0
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:

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";}

}