Esempio n. 1
0
int main(int argc, char *argv[]) {
  fd_set fds;
  int keep_going = 1;

  want_debug = (argc > 1) && (!strcmp(argv[1], "-debug"));

  if (want_debug) fprintf(stderr, "execdaemon starting.\n");

  while (keep_going) {
    struct timeval timeout;
    timeout.tv_sec = 0;
    timeout.tv_usec = 100000; /* 100 ms */

    FD_ZERO(&fds);
    FD_SET(0, &fds);

    switch (select(1, &fds, NULL, NULL, &timeout)) {
      case 1: {
	if (!read_command()) {
	  keep_going = 0;
	} else {
	  eval_command();
	}
	break;
      }

      case 0:
	break;

      case -1:
	perror("select");
	exit(1);
    }

    if (kid != 0) {
      int pidstatus;
      pid_t result = waitpid(kid, &pidstatus, WNOHANG);
      if (result != 0) {
	if (WIFEXITED(pidstatus)) {
	  write_response("exit", num_buf(WEXITSTATUS(pidstatus)));
	} else if (WIFSIGNALED(pidstatus)) {
	  write_response("signal", num_buf(WTERMSIG(pidstatus)));
	} else {
	  write_response("died", "");
	}
	kid = 0;
      }
    }
  }

  if (want_debug) fprintf(stderr, "execdaemon exiting.\n");
  return 0;
}
Esempio n. 2
0
int main(int argc,char ** argv){
    if(argc <= 1){
        std::cout<<"projectModel a.obj -tn/-n/-v b.zip  ";
        return -1;
    }
    Command command ;
    if(argc==2){
        command.objectFileName =std::string( argv[1] );
        command.type = Type::Vertex ;
        command.projectObjectFileName = command.objectFileName+".zip";
    }else if(argc==3){
        command.objectFileName =std::string( argv[1] );
        std::string t_(argv[2]);
        if(t_=="-n"){
            command.type = Type::Vertex|Type::Normal;
        }else if(t_=="-v"){
            command.type = Type::Vertex;
        }
        else if(t_=="-tn"){
            command.type = Type::Vertex|
                    Type::Normal|
                    Type::Texture;
		}
		else {
			return -1;
		}
        command.projectObjectFileName = command.objectFileName+".zip";
    }else{
        command.objectFileName =std::string( argv[1] );
        std::string t_(argv[2]);
        if(t_=="-n"){
            command.type = Type::Vertex|Type::Normal;
        }else if(t_=="-v"){
            command.type = Type::Vertex;
        }else if(t_=="-tn"){
            command.type = Type::Vertex|
                    Type::Normal|
                    Type::Texture;
		}
		else {
			return -1;
		}
        command.projectObjectFileName = std::string(argv[3]);
    }


    eval_command(command );
    std::quick_exit(0);
return 0;
}
Esempio n. 3
0
void eval_command_line(const char* s, zombies* z) {
    int type;
    char* token;
    // commandlist contains command groups separated by ';' or '&'
    command** commandlist = (command**) malloc(sizeof(command*));
    // keep track of how many command groups wehave
    int listcontent = 1;
    // Your code here!

    // build the command
    command* c = command_alloc();
    // the first command group
    commandlist[0] = c;
    while ((s = parse_shell_token(s, &type, &token)) != NULL) {
        if(type == TOKEN_CONTROL) {
            // seeing a ';' means we have to make a new command group
            if(token[0] == ';') {
                c = command_alloc();
                commandlist = (command**) realloc(commandlist, (listcontent + 1)*sizeof(command*));
                commandlist[listcontent] = c;
                ++listcontent;
                // seeing a '&' means we have to make a new command group
            } else if(token[0] == '&' && !token[1]) {
                c->background = 1;
                c = command_alloc();
                commandlist = (command**) realloc(commandlist, (listcontent + 1)*sizeof(command*));
                commandlist[listcontent] = c;
                ++listcontent;
                // putting into new command group so that we can conditionally evaluate it
            } else if(token[0] == '&' && token[1] == '&') {
                c = command_alloc();
                c->needcondition = 0;
                commandlist = (command**) realloc(commandlist, (listcontent + 1)*sizeof(command*));
                commandlist[listcontent] = c;
                ++listcontent;
                // within a command group, mark it as piping if seeing '|'
            } else if(token[0] == '|' && !token[1]) {
                c->piping = 1;
                command_append_arg(c, type, token);
                // putting into new command group so that we can conditionally evaluate it
            } else if(token[0] == '|' && token[1] == '|') {
                c = command_alloc();
                c->needcondition = 1;
                commandlist = (command**) realloc(commandlist, (listcontent + 1)*sizeof(command*));
                commandlist[listcontent] = c;
                ++listcontent;
            }
        } else if(type == TOKEN_REDIRECTION) {
            // redirecting standard input
            if(token[0] == '<' || token[0] == '0') {
                c->redirectstdin = 1;
                // redirecting standard output
            } else if(token[0] == '>' || token[0] == '1') {
                c->redirectstdout = 1;
                // redirecting standard error
            } else if(token[0] == '2' && token[1] == '>') {
                c->redirectstderr = 1;
            }
        } else {
            // the token following a redirection token must be a file name that should be dup'ed
            // then clears the flag
            if(c->redirectstdin) {
                c->stdinfilename = token;
                c->redirectstdin = 0;
            } else if(c->redirectstdout) {
                c->stdoutfilename = token;
                c->redirectstdout = 0;
            } else if(c->redirectstderr) {
                c->stderrfilename = token;
                c->redirectstderr = 0;
                // all other normal tokens
            } else {
                command_append_arg(c, type, token);
            }
        }
    }

    // execute the command
    int condition = -1;	// exit condition of previous command
    for(int i = 0; i < listcontent; i++) {
        if (commandlist[i]->argc)
            // test if this command group should run
            if (commandlist[i]->needcondition == -1 || (commandlist[i]->needcondition == 0 && condition == 0) || (commandlist[i]->needcondition == 1 && condition != 0))
                eval_command(commandlist[i], &condition, z);
        command_free(commandlist[i]);
    }
    free(commandlist);
}