main.c 6.57 KB

/**************************************************************************
 *                                                                        *
 *               Copyright (C) 1994, Silicon Graphics, Inc.               *
 *                                                                        *
 *  These coded instructions, statements, and computer programs  contain  *
 *  unpublished  proprietary  information of Silicon Graphics, Inc., and  *
 *  are protected by Federal copyright  law.  They  may not be disclosed  *
 *  to  third  parties  or copied or duplicated in any form, in whole or  *
 *  in part, without the prior written consent of Silicon Graphics, Inc.  *
 *                                                                        *
 *************************************************************************/

/*
 * Main routine of program. Sit and wait for display lists to be passed
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#ifdef __sgi
#include <os.h>
#endif
#include <gu.h>
#include <sys/u64gio.h>
#include <ultrahost.h>
#include <signal.h>
#include "dlprint.h"

memNode  *firstMemNode;
memNode  *lastMemNode;

int             inFD;

/* 
 * need to catch signals, and exit, to cause data that has been redirected
 * to be flushed from buffer when program is killed 
 */
void mySigHandler(int signo)
{
    uhCloseGame(inFD);
    exit(-1);
    
}

/*
 * main routine.
 */
int main(int argc, char *argv[])
{

    int             bytesRead;
    guDLPrintCB     readBlk;
    memNode         *node,*nextNode;
    u32             flags = 0;
    u32             done = 0;
    u32             server_mode = FALSE;
    u64             *rdpData;
    u8              *abiData;
    char            errMessage[256];


    struct sigaction sigAct;

    if(argc > 1) 
    {
        if(argv[1][1] == 's')
	    server_mode = TRUE;
	else
	{
	    PRINTF("usage: -s servermode\n");
	    exit(-1);
	}
    }


    /* Set up the signal handling */
#ifdef __sgi__
    sigAct.sa_handler = (__sigret_t*)mySigHandler;
#else
    sigAct.sa_handler = mySigHandler;
#endif
    sigemptyset(&sigAct.sa_mask);
    sigAct.sa_flags = 0;

    if(sigaction(SIGCHLD,&sigAct,NULL) < 0)
    {
        perror("Unable to install signal handler SIGCHLD");
        exit(-1);
    }
    if(sigaction(SIGINT,&sigAct,NULL) < 0)
    {
        perror("Unable to install signal handler SIGINT");
        exit(-1);
    }
    if(sigaction(SIGTERM,&sigAct,NULL) < 0)
    {
        perror("Unable to install signal handler SIGTERM");
        exit(-1);
    }
    if(sigaction(SIGQUIT,&sigAct,NULL) < 0)
    {
        perror("Unable to install signal handler SIGQUIT");
        exit(-1);
    }

#ifdef __linux
    inFD = uhOpenGame("/tmp/u64_data");
#else
    inFD = uhOpenGame(DEV_U64_DATA);
#endif
    if(inFD == -1)
    {
	sprintf(errMessage, "dlprint: unable to open %s", DEV_U64_DATA);
	perror(errMessage);
	exit(1);
    }


    while(!done)
    {
        bytesRead = uhReadGame(inFD, (void*)&readBlk, sizeof(readBlk) );
        if(bytesRead != sizeof(readBlk))
        {
            printf("Failure reading readBlk, only got %d bytes\n",bytesRead);
            exit(-1);
        }
	if(readBlk.dlType == GU_PARSE_STRING_TYPE)
	{
	    rdpData = (u64*)malloc(readBlk.dataSize);
	    if(!rdpData)
	    {
		printf("Unable to allocate memory for rdp data, needed %d bytes\n",readBlk.dataSize);
		exit(-1);
	    }
            bytesRead = uhReadGame(inFD, rdpData, readBlk.dataSize);
            if(bytesRead != readBlk.dataSize)
            {
                printf("Failure reading dataBlk, only got %d bytes\n",bytesRead);
                exit(-1);
            }

	    if(!strncmp((char *) rdpData, "END DUMP", 8)) 
		done = 1;
	    else
		printf("%s", (char*)rdpData);

	    free(rdpData);
	}
	else if(readBlk.dlType == GU_PARSE_RDP_TYPE)
	{
	    rdpData = (u64*)malloc(readBlk.dataSize);
	    if(!rdpData)
	    {
		printf("Unable to allocate memory for rdp data, needed %d bytes\n",readBlk.dataSize);
		exit(-1);
	    }
            bytesRead = uhReadGame(inFD, rdpData, readBlk.dataSize);
            if(bytesRead != readBlk.dataSize)
            {
                printf("Failure reading dataBlk, only got %d bytes\n",bytesRead);
                exit(-1);
            }
	    ParseRdpDL(rdpData, (u32)readBlk.dataSize, readBlk.flags);
	    if(!server_mode)
		done = 1;
	    free(rdpData);
	}
	else if(readBlk.dlType == GU_PARSE_ABI_TYPE)
	{
	    abiData = (u8*)malloc(readBlk.dataSize);
	    if(!abiData)
	    {
		printf("Unable to allocate memory for abi data, needed %d bytes\n",readBlk.dataSize);
		exit(-1);
	    }
            bytesRead = uhReadGame(inFD, abiData, readBlk.dataSize);
            if(bytesRead != readBlk.dataSize)
            {
                printf("Failure reading dataBlk, only got %d bytes\n",bytesRead);
                exit(-1);
            }
	    ParseAbiCL(abiData, (u32)readBlk.dataSize);
	    if(!server_mode)
		done = 1;
	    free(abiData);
	}
        else if(readBlk.dlType == GU_PARSE_GBI_TYPE || readBlk.dlType == GU_PARSE_MEM_BLOCK)
        {
            node = (memNode*)malloc(sizeof(memNode));
            if(!node)
            {
                printf("Unable to allocate memory for display list node\n");
                exit(-1);
            }
            node->next = 0;
            node->paddr = readBlk.paddr;
            node->data = (u8*)malloc(readBlk.dataSize);
            if(!node->data)
            {
                printf("Unable to allocate memory for data. Needed %d bytes\n",readBlk.dataSize);
                exit(-1);
            }
            bytesRead = uhReadGame(inFD, node->data, readBlk.dataSize);
            if(bytesRead != readBlk.dataSize)
            {
                printf("Failure reading dataBlk, only got %d bytes\n",bytesRead);
                exit(-1);
            }
            if(lastMemNode) /* this is not the first node */
            {
                lastMemNode->next = (void*)node;
                lastMemNode = node;
            }
            else /* this is the first nodes */
            {
                flags = (u32)readBlk.flags;
                firstMemNode = node;
                lastMemNode = node;
            }
        }
        else if(readBlk.dlType == GU_PARSE_READY)
        {
            ParseGbiDL(flags); 
	    PRINTF("Display list processing completed\n");
            while(firstMemNode)
            {
                nextNode = (memNode*)firstMemNode->next;
                free(firstMemNode->data);
                free(firstMemNode);
                firstMemNode = nextNode;
            }
            lastMemNode = 0;
	    if(!server_mode)
		done = 1;
        }
    }
    return 0;
}