예제 #1
0
void
ConsoleImpl::update(float delta)
{
  for(Buffer::iterator i = buffer.begin(); i != buffer.end(); ++i)
    {
      i->display_time += delta;
    }  

  if (active)
    {
      InputEventLst events = InputManager::get_controller().get_events();
  
      for (InputEventLst::iterator i = events.begin(); i != events.end(); ++i)
        {
          if ((*i).type == KEYBOARD_EVENT)
            {
              if ((*i).keyboard.key_type == KeyboardEvent::LETTER)
                {
                  if (cursor_pos == int(command_line.size()))
                    {
                      command_line += (char)(*i).keyboard.code;
                      cursor_pos += 1;
                    }
                  else
                    {
                      command_line.insert(cursor_pos, std::string(1, (char)(*i).keyboard.code));
                      cursor_pos += 1;
                    }
                }
              else if ((*i).keyboard.key_type == KeyboardEvent::SPECIAL)
                {
                  //console << "special: " << i->keyboard.code << std::endl;

                  switch (i->keyboard.code)
                    {
                    case SDLK_BACKSPACE:
                      if (!command_line.empty() && cursor_pos > 0)
                        {
                          command_line.erase(cursor_pos - 1, 1);
                          cursor_pos -= 1;
                        }
                      break;

                    case SDLK_DELETE:
                      if (!command_line.empty())
                        {
                          command_line.erase(cursor_pos, 1);
                        }
                      break;

                    case SDLK_DOWN:
                      if (!history.empty())
                        {
                          history_position += 1;
                          if (history_position > int(history.size())-1)
                            history_position = int(history.size())-1;
                          command_line = history[history_position];
                          cursor_pos = command_line.size();
                        }
                      break;

                    case SDLK_HOME:
                      cursor_pos = 0;
                      break;
                      
                    case SDLK_END:
                      cursor_pos = command_line.size();
                      break;
                        
                    case SDLK_PAGEUP:
                      console.scroll(10);
                      break;

                    case SDLK_PAGEDOWN:
                      console.scroll(-10);
                      break;

                    case SDLK_TAB:
                      tab_complete();
                      break;

                    case SDLK_UP:
                      if (!history.empty())
                        {
                          history_position -= 1;
                          if (history_position < 0)
                            history_position = 0;

                          command_line = history[history_position];
                          cursor_pos = command_line.size();
                        }
                      break;

                    case SDLK_LEFT:
                      cursor_pos -= 1;
                      if (cursor_pos < 0)
                        cursor_pos = 0;
                      break;

                    case SDLK_RIGHT:
                      cursor_pos += 1;
                      if (cursor_pos > int(command_line.size()))
                        cursor_pos = command_line.size();
                      break;

                    case SDLK_RETURN:
                      eval_command_line();
                      break;

                    case SDLK_ESCAPE:
                    case SDLK_F1:
                      console.deactive();
                      break;
                    }
                }
            }
        }
    }
}
예제 #2
0
int main(int argc, char* argv[]) {
    int command_file = stdin;
    int quiet = 0;
    int r = 0;
    // initialize zombies struct
    zombies* z = zombies_alloc();

    // Check for '-q' option: be quiet (print no prompts)
    if (argc > 1 && strcmp(argv[1], "-q") == 0) {
        quiet = 1;
        --argc, ++argv;
    }

    // Check for filename option: read commands from file
    if (argc > 1) {
        command_file = fopen(argv[1], "rb");
        if (!command_file) {
            perror(argv[1]);
            exit(1);
        }
    }

    char buf[BUFSIZ];
    int bufpos = 0;
    int needprompt = 1;

    while (!feof(command_file)) {
        // Print the prompt at the beginning of the line
        if (needprompt && !quiet) {
            printf("sh61[%d]$ ", getpid());
            fflush(stdout);
            needprompt = 0;
        }

        // Read a string, checking for error or EOF
        if (fgets(&buf[bufpos], BUFSIZ - bufpos, command_file) == NULL) {
            if (ferror(command_file) && errno == EINTR) {
                // ignore EINTR errors
                clearerr(command_file);
                buf[bufpos] = 0;
            } else {
                if (ferror(command_file))
                    perror("sh61");
                break;
            }
        }

        // If a complete command line has been provided, run it
        bufpos = strlen(buf);
        if (bufpos == BUFSIZ - 1 || (bufpos > 0 && buf[bufpos - 1] == '\n')) {
            eval_command_line(buf, z);
            bufpos = 0;
            needprompt = 1;
        }

        // kill zombie processes
        for(int i = 0; i < z->numzombies; ++i)
            waitpid(z->zombielist[i], NULL, WNOHANG | WUNTRACED);
    }

    zombies_free(z);
    return 0;
}