#pragma extend

#define RMXRET int
#define RMXOBJ int
#include <rmx.h>

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char *ffirst( char * ), *fnext();

/****************************************************************************/
/* explain_ccode - outputs explanation of erroneous condition codes         */
/****************************************************************************/

const char whilel[]  = "while loading command";
const char whilee[]  = "while executing command";

void explain_ccode( int ccode, const char *msg )
    {
    int     status;
    char    buff[50];
        
    buff[0] = 0;
    rqcformatexception( buff, 50, ccode, 1, &status );

    if ( status != E_OK )
        sprintf( buff, "%04X: E$UNKNOWN$EXCEPTION", ccode );
    else
        (void)cstr( buff, buff );

    fprintf( stderr, "\n%s, %s\n", buff, msg );
    }

/****************************************************************************/
/* main                                                                     */
/****************************************************************************/

int main( int argc, char *argv[] )
    {
    char   *filename, *f, in_cmd[256], *i, out_cmd[1024], *o, c, *this,
           *next, excp_info[5], *dot;
    int    index, status, ci_tkn, co_tkn, cmd_tkn, cmd_status;

    if ( argc < 3 )
        {
        fprintf( stderr, "\nUSE: eo <wildcard-filename> <command> [!<command>]*\n" );
        exit( 0 );
        }

    excp_info[4] = NO_EXCEPTIONS;
    rqsetexceptionhandler( excp_info, &status );
    
    filename = ffirst( argv[1] );
    
    if ( filename != NULL )
        {
        strcpy( in_cmd, argv[2] );
        
        for ( index = 3; index < argc; index++ )
            {
            strcat( in_cmd, " " );
            strcat( in_cmd, argv[index] );
            }

        ci_tkn = rqcgetinputconnection( CONSOLE_INPUT_DEVICE, &status );
        
        if ( status == E_OK )
            co_tkn = rqcgetoutputconnection( CONSOLE_OUTPUT_DEVICE, OVER_PREPOSITION, &status );
            
        if ( status == E_OK )
            cmd_tkn = rqccreatecommandconnection( ci_tkn, co_tkn, 0, &status );
            
        if ( status != E_OK )
            {
            explain_ccode( status, whilee );
            exit(status);
            }

        while ( filename != NULL )
            {
            i = in_cmd;
            o = out_cmd;
            
            while ( ( c = *i++ ) != '\0' )
                {
                if ( c == '#' )
                    {
                    f = filename;
                    
                    while ( ( c = *f++ ) != '\0' )
                        *o++ = c;
                    }
                else
                    if ( c == '@' )
                        {
                        f = filename;
                        dot = strrchr( filename, '.' );
                        
                        if ( dot != NULL )
                            *dot = '\0';
                    
                        while ( ( c = *f++ ) != '\0' )
                            *o++ = c;
                        
                        if ( dot != NULL )
                            *dot = '.';
                        }
                    else
                        *o++ = c;
                }

            *o = '\0';

            this = out_cmd;
            
            while ( ( next = strchr( this, '!' ) ) != NULL )
                {
                *next++ = '\0';
                fprintf( stderr, "EO>> %s\n", this );
                udistr( this, this );

                rqcsendcommand( cmd_tkn, this, &cmd_status, &status );
                
                if ( status != E_OK )
                    explain_ccode( status, whilel );
                    
                if ( cmd_status != E_OK )
                    explain_ccode( cmd_status, whilee );

                this = next;
                }

            fprintf( stderr, "EO>> %s\n", this );
            udistr( this, this );

            rqcsendcommand( cmd_tkn, this, &cmd_status, &status );
            
            if ( status != E_OK )
                explain_ccode( status, whilel );
                
            if ( cmd_status != E_OK )
                explain_ccode( cmd_status, whilee );

            filename = fnext();
            }

        }
    
    exit( 0 );
    }
