int main(int argc, char** argv) {
    setlocale(LC_ALL, "en_US.UTF-8");
    int i;
    for (i = 1; i < argc; ++i) {
        TokenScanner* token_scanner = FileTokenScanner_new(argv[i]);
        TokenMatcher* token_matcher = TokenMatcher_new(L"en");
        Builder* builder = TokenFormatterBuilder_new();
        Parser* parser = Parser_new(builder);
        Parser_parse(parser, token_matcher, token_scanner);
        Parser_delete(parser);
        TokenFormatterBuilder_delete(builder);
        TokenMatcher_delete(token_matcher);
        TokenScanner_delete(token_scanner);
    }
    return 0;
}
示例#2
0
char *constructPrompt( char *Prompt, int maxLen )
{
    /*
     * We will use the current user's name, the current host and the current
     * directory to construct the prompt
     */

    /* An array of environment variable to search */
    char *find[] = { "USER", "HOSTNAME", "PWD", NULL };

    char **searcher, *finder;
    int curLen = 0;

    Parser *p;
    int count;

    /* Restart the prompt */
    Prompt[0] = '\0';

    /* Create a new parser for the PWD */
    p = Parser_new();
    Parser_setDelimiters(p, "/");
    
    /* 
     * Find the proper environment variables and construct the prompt
     */
    for( searcher = find; *searcher != NULL; searcher++ )
    {
        finder = getenv( *searcher );
        curLen = maxLen - strlen( Prompt );
    
        if( finder != NULL )
        {
            /* We are now messing with the PWD */
            if( !strncmp( *searcher, "PWD", 3 ) )
            {
                /* Parse the directory */
                Parser_parse(p, finder );
                count = Parser_numTokens(p);

                /* 
                 * There are more than two direcories in the Path
                 * so we need to some more work
                 */
                if( count > 2 )
                {
                    /*
                     * Forget all but the last two tokens
                     */
                    for( count = Parser_numTokens(p) - 2; count > 0; 
                                                                count-- )
                    {
                        Parser_next(p);
                    }

                    /* 
                     * Add the last two tokens to our prompt 
                     */
                    strncat( Prompt, Parser_next(p), strlen(Prompt) - 1 );
                    strncat( Prompt, "/", 1 );
                    strncat( Prompt, Parser_next(p), strlen(Prompt) - 1 );
                    strncat( Prompt, ":", 1 );

                    continue;
                }
            }

            strncat( Prompt, finder, --curLen );
        }
        else
        {
            strncat( Prompt, "(null)", --curLen );
        }

        strncat( Prompt, ":", curLen - 1 );
    }

    strncat( Prompt, ">", curLen );
    
    return Prompt;
}
示例#3
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;
}