Esempio n. 1
0
File: shell.c Progetto: dnery/plush
void sh_continue(job_t *job, int foreground)
{
        /* Mark suspended job as running again */
        process_t *p = job->first_process;
        for (; p != NULL; p = p->next)
                p->suspended = 0;
        job->notified = 0;

        /* Put in fg or bg, sending non-zero cont */
        if (foreground)
                put_in_foreground(job, 1);
        else
                put_in_background(job, 1);
}
// move a job into the background
int cmd_bg(tok_t arg[]){
  process* p;
  if( arg[0] != NULL ){
    p = get_process( atoi( arg[0] ) );
  }
  else if( first_process != NULL ){
    p = first_process->prev;
  }
  printf( "%d\n", p->pid );
  if( p != NULL ){
    put_in_background( p, true );
  }
  return 1;
}
int
shell( int argc, char *argv[] ) {

  init_shell(); // initialize the shell
  
  pid_t pid = getpid();	       // get current processe's PID
  pid_t ppid = getppid();      // get parent's PID 
  pid_t cpid, tcpid, cpgid;

  print_welcome( argv[0], pid, ppid );
  print_prompt();
  char *s = malloc(INPUT_STRING_SIZE + 1); // user input string
  
  while( (s = freadln(stdin)) ){ // read in the input string
    process *p = create_process( s );
    if( p != NULL ){ // do we need to run a newly created process?
      cpid = fork(); // fork off a child
      if( cpid == 0 ) { // child
	p->pid = getpid();
	launch_process( p );
      } // if( cpid == 0 )
      else if( cpid > 0 ){ // parent
	p->pid = cpid;
	if( shell_is_interactive ){ // are we running shell from a terminal?
	  // put 'p' into its own process group
	  // since our shell is not supporting pipes, we treat
	  // each process by itself as a 'job' to be done
	  if( setpgid( cpid, cpid ) < 0 ) 
	    printf( "Failed to put the child into its own process group.\n" );
	  if( p->background )
	    put_in_background( p, false );
	  else
	    put_in_foreground( p, false );
	} // if( shell_is_interactive )
	else
	  wait_for_process( p );
      } // else if( cpid > 0 )
      else // fork() failed
	printf( "fork() failed\n" );
    } // if( p != NULL )
    print_prompt();
  } // while

  return EXIT_SUCCESS;
} // shell
Esempio n. 4
0
/**
 * \brief Inform the layer that a mouse button has been pressed.
 * \param key The value of the maintained button.
 * \param pos The current position of the cursor.
 */
bool bear::item_information_layer::mouse_pressed
( bear::input::mouse::mouse_code key,
  const claw::math::coordinate_2d<unsigned int>& pos )
{
  bool result = false;

  switch(key)
    {
    case bear::input::mouse::mc_left_button:
      result = grab_info_box(pos);
      break;
    case bear::input::mouse::mc_right_button:
      result = put_in_background(pos);
      break;
    case bear::input::mouse::mc_middle_button:
      result = close_info_box(pos);
      break;
    }

  if (!result)
    switch(key)
      {
      case bear::input::mouse::mc_left_button:
        result = show_item( pos, get_level().get_camera_focus() );
        break;
      case bear::input::mouse::mc_right_button:
        result = follow_item( pos, get_level().get_camera_focus() );
        break;
      case bear::input::mouse::mc_middle_button:
        if ( !m_info_box.empty() )
          {
            result = true;
            clear();
          }
        break;
      }

  return result;
} // item_information_layer::mouse_pressed()
Esempio n. 5
0
File: shell.c Progetto: dnery/plush
void sh_launch_job(job_t *job, int foreground)
{
        process_t *p;                                   /* Process iteration */
        pid_t pid;                                      /* PID for the newly spawned process */
        int output;                                     /* Output file descriptor */
        int input = job->stdin;                         /* Input file descriptor */
        int iopipe[2] = {STDIN_FILENO, STDOUT_FILENO};  /* Inter-process comms pipe */

        for (p = job->first_process; p != NULL; p = p->next) {

                /* Set up pipes if applicable, set output */
                if (p->next != NULL) {
                        check(pipe(iopipe) >= 0);
                        output = iopipe[1];
                } else {
                        output = job->stdout;
                }

                /* Fork child process and launch */
                check((pid = fork()) >= 0);

                if (pid == 0) {
                        /* I am the fork */
                        sh_launch_process(p, job->pgid, input, output,
                                        job->stderr, foreground);
                } else {
                        /* I am parent */
                        p->pid = pid;

                        if (shell_interactive) {

                                /*
                                 * if (job->pgid == 0)
                                 *      job->pgid = pid;
                                 *
                                 * MAN specifies that if pgid is 0, pid of
                                 * indicated process (pid) shall be used.
                                 */
                                setpgid(pid, job->pgid);
                        }
                }

                /* Clean up the pipes, set input */
                if (input != job->stdin)
                        close(input);
                if (output != job->stdout)
                        close(output);

                input = iopipe[0];
        }

        format_job_info(job, "launched");

        /* Either wait until job is finished, or engage new */
        if (!shell_interactive)
                job_wait_blocked(job);
        else if (foreground)
              put_in_foreground(job, 0);
        else
              put_in_background(job, 0);
}