Ejemplo n.º 1
0
int main( int argc, char *argv[], char *env[] )
{
    char Prompt[MAXSIZE];
    char Input[MAXSIZE];
    int i;
    
    /* Point to our name */
    PROGNAME = argv[0];

    /* 
     * Starting from the first true argument, scan the argument table
     */
    for( i = 1; i < argc; i++ )
    {
        if( !strncmp( argv[i], "--version", 10 ) )
        {
            Version();
        }

        if( !strncmp( argv[i], "--help", 7 ) )
        {
            Usage();
        }
    }

    /* Construct the prompt */
    constructPrompt( Prompt, MAXSIZE );

    /* 
     * Infinite loop to read in from the user 
     * If the user presses Ctrl-D then the loop exits
     */
    for( printf("%s", Prompt); fgets( Input, MAXSIZE, stdin) != NULL; 
            printf("%s", Prompt) )
    {
        fprintf(stdout,"Input from user: %s\n", Input );
        
        /*
         * The user has typed in quit all by itself
         */
        if( !strncmp( Input, "quit\n", 5 ) )
        {
            break;
        }
    }

    printf("\nProgram exit.\n");

    return 0;
}
Ejemplo n.º 2
0
int main( int argc, char *argv[], char *env[] )
{
    char Prompt[MAXSIZE];
    char Input[MAXSIZE];
    int i;
    int count;

    char *look;
    
    Process *child;
    Parser *p; 
    Executor *e;
    
    /* Point to our name */
    PROGNAME = argv[0];

    /*
     * Construct a parser to handle the input
     *
     * We delimit on the space and "|" for now, but need to keep the 
     * "|" so we need to it as a Special Delimiter
     */
    p = Parser_new();
    Parser_setDelimiters( p, " |" );
    Parser_setSpecialDelimiters( p, "|" );

    /*
     * Construct an executor to run the commands
     *
     * We delimit on the "|" for now
     */
    e = Executor_new();
    Executor_setExecDelimiters( e, "|" );

    /*
     * Setup the signals
     */
    setupSignals();

    /* 
     * Starting from the first true argument, scan the argument table
     */
    for( i = 1; i < argc; i++ )
    {
        if( !strncmp( argv[i], "--version", 10 ) )
        {
            Version();
        }

        if( !strncmp( argv[i], "--help", 7 ) )
        {
            Usage();
        }
    }

    /* Construct the prompt */
    constructPrompt( Prompt, MAXSIZE );

    do
    {
        gotSignal = 0;
        /* Infinite loop to read in from the user */
        for( printf("%s ", Prompt); fgets( Input, MAXSIZE, stdin) != NULL; 
                                                printf("%s ", Prompt) )
        {

            /* They just pressed Enter */
            if( *Input == '\n' )
            {
                continue;
            }

            /* Quit */
            if( !strncmp( Input, "exit\n", 5 ) )
            {
                gotSignal = 0;
                break;
            }

            /* 
             * Background the process */
            if( !strncmp( Input, "bg\n", 3 ) )
            {
                child = ProcControl_get(ProcControl_numProcs());
                
                if( child != NULL )
                {
                    printf("Backgrounded %d ", Process_getPID(child));
                    Process_printCommand( child, stdout );
                    printf("\n");

                    Process_resume(child);
                    Parser_clear( p );
                    gotSignal = 0;
                }
                else
                {
                    fprintf( stderr, "\n!!! There is no child to background.\n" );
                }

                ProcControl_printTable( stdout );

                continue;
            }

            /*
             * List current jobs
             */
            if( !strncmp( Input, "jobs\n", 5 ) )
            {
                ProcControl_printTable( stdout );
                Parser_clear(p);
                gotSignal = 0;
                continue;
            }

            /*
             * Foreground the last process
             */
            if( !strncmp( Input, "fg\n", 3 ) )
            {
                child = ProcControl_get(ProcControl_numProcs());
                RUNNING = child;
                if( child != NULL )
                {
                    Process_resume( RUNNING );
                    printf("Forground process %d ", Process_getPID(RUNNING) );
                    Process_printCommand( RUNNING, stdout );
                    printf("\n");
                    Process_wait( RUNNING, PROCESS_WAIT );
                    Parser_clear(p);
                    gotSignal = 0;
                }
                else
                {
                    fprintf(stderr, "\n!!! There are no backgrounded children to "
                                    "bring to the fore.\n\n");
                }
                continue;
            }

            /* Parse the input */
            Parser_parse( p, Input );

            while( (RUNNING = Executor_execute( e, p ) ) != NULL)
            {
                
                ProcControl_add(RUNNING);
                Process_start( RUNNING );
                
                if( RUNNING != NULL )
                {
                    Process_wait( RUNNING, PROCESS_WAIT);
                }
            }

            /* Clear the parser's token list */
            Parser_clear( p );
            gotSignal = 0;
        }
    }while( gotSignal );

    printf("\nProgram exit.\n");

    /* Clean up children that haven't finished */
    ProcControl_free();

    /* Free our parser and executor */
    Parser_free( p );
    Executor_free( e );
    Signal_clear();

    return 0;
}