/
Sapphire/bin/
Sapphire/db/
Sapphire/db/OLC_rooms/
Sapphire/db/abi/
Sapphire/db/em_src/
Sapphire/db/helps/
Sapphire/db/helps/emman/ifunc/
Sapphire/db/npcs/Tatt/
Sapphire/db/objects/Tatt/
Sapphire/db/q_data/
Sapphire/db/rooms/Tatt/
Sapphire/doc/
Sapphire/doc/em/
Sapphire/etc/
Sapphire/src/abic/
Sapphire/src/areacon/
Sapphire/src/client/
Sapphire/src/embc/
Sapphire/src/emi/
Sapphire/src/emi/test/
Sapphire/src/include/
Sapphire/src/sapphire/em/
Sapphire/src/tcon/
/*
 * Copyright (C) 1995-1997 Christopher D. Granz
 *
 * This header may not be removed.
 *
 * Refer to the file "License" included in this package for further
 * information and before using any of the following.
 */

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

#include "emc.h"


/*
 * Globals
 */
extern FILE *                                                      yyin;
extern FILE *                                                     yyout;
extern char *                                                    yytext;

char                                                          cNullChar;

unsigned short int                                    usiSymbolListSize;
SYM_ENTRY *                                                 pSymbolList;

unsigned short int                                      usiFuncListSize;
FUNC_ENTRY *                                                  pFuncList;


/*
 * Functions
 */
static long lParseLevel = 0;

static void show_parse_tree( NODE *pNode )
{
    if ( pNode == NULL )
        return;

    lParseLevel++;
    printf( "\nlevel: %ld; left: ", lParseLevel );
    show_parse_tree( pNode->uL.pLeft );
    printf( "; right: " );
    show_parse_tree( pNode->uR.pRight );
    lParseLevel--;
}


int main( int iArgC, char *pArgV[] )
{
    char cBuf[1028];
    char *pBuf;

    if ( iArgC < 2 )
    {
        fprintf( stderr,
          "\nUsage: %s [-c] <object files|code files>\n", pArgV[0] );
        return ( 0 );
    }

    if ( iArgC > 1 )
    {
        strncpy( cBuf, pArgV[1], 1024 );
        cBuf[1024] = '\0';
        yyin       = open_file( cBuf, "r" );

        if ( iArgC > 2 )
            strncpy( cBuf, pArgV[2], 1024 );
        else
        {
            pBuf   = strrchr( cBuf, '.' );

            if ( !strcmp( pBuf, ".em" ) || !strcmp( pBuf, ".EM" ) )
                strcpy( pBuf, ".emo" );
            else
                strcat( pBuf, ".emo" );
        }
    }
    else
    {
        yyin       = stdin;
        strcpy( cBuf, "_a.emo" );
    }

    if ( ( yyout = fopen( cBuf, "wb" ) ) == NULL )
    {
        fprintf( stderr, "\n%s: %s.\n", pArgV[0], strerror( errno ) );

        if ( yyin != stdin )
            close_file( yyin );

        return ( 0 );
    }

    if ( yyin != stdin )
        fprintf( stderr, "%s: %s -> %s\n", pArgV[0], pArgV[1], cBuf );

    while ( feof( yyin ) == 0 )
    {
        if ( yyparse( ) == 0 )
            fprintf( stderr, "Parse successful.\n" );
        else
        {
            fprintf( stderr, "Parse failed.\n" );
            break;
        }

        printf( "Parse tree: " );
        show_parse_tree( pNodeTree );
    }

    write_emo( yyout );
    fclose( yyout );

    if ( yyin != stdin )
        close_file( yyin );

    return ( 0 );
}


void write_emo( FILE *pFile )
{
    SYM_ENTRY *pSymbol = pSymbolList;
    FUNC_ENTRY *pFunc  = pFuncList;
    i4 i               = 0L;
    i4 iIndex;

    i                  = EM_MAGIC_NUMBER;
    fwrite( &i, sizeof( i4 ), 1, pFile );

    i                  = 0L;
    i                  = EM_LOADER_VERSION;
    fwrite( &i, 1, 1, pFile );

    fwrite( &usiSymbolListSize, sizeof( i2 ), 1, pFile );

    for ( iIndex = 0L; iIndex < usiSymbolListSize; iIndex++ )
    {
        i              = 0L;
        i              = strlen( pSymbol->pSymbol );
        fwrite( &i, sizeof( i2 ), 1, pFile );
        fwrite( pSymbol->pSymbol, 1, i, pFile );
    }

    fwrite( &usiFuncListSize, sizeof( i2 ), 1, pFile );

    for ( iIndex = 0L; iIndex < usiFuncListSize; iIndex++ )
    {
        fwrite( &pFunc->fFuncFlags, sizeof( i4 ), 1, pFile );
        fwrite( &pFunc->iNumArgs, 1, 1, pFile );
        fwrite( &pFunc->iNumReturn, 1, 1, pFile );
        fwrite( &pFunc->usiSymName, sizeof( i2 ), 1, pFile );

        fwrite( &pFunc->ulCodeSize, sizeof( i4 ), 1, pFile );
        fwrite( pFunc->pCode, pFunc->ulCodeSize, 1, pFile );
    }
}


/*
 * End of main.c
 */