//***********************************************************************************************************
//**
//**	hp21asm - Listing Routines
//**
//**	2014 Nov: split from single file / bh
//**
//***********************************************************************************************************
#include <stdio.h>
#include "util.h"


//* END Refs
//***********************************************************************************************************
//* Internals

static char			*list__srcLn;
static int			list__lnPrinted;				// TRUE if current line has been printed
static char			*list__errInLine;


//* END Internals
//***********************************************************************************************************
//* Interface

FILE				*ListFile;
int				Err_count;


//----------------------------------------------------
//*  Create listing file

int
List_create( fname )
	char			*fname;
 {
	ListFile = fopen( fname, "w" );
	if( !ListFile )
		return( FALSE );

	Err_count = 0;

	return( TRUE );
 }


//----------------------------------------------------
//*  Complete listing file

void
List_close()
 {
	fprintf( ListFile, "%d ERRORS\n", Err_count );

	fclose( ListFile );
 }


//----------------------------------------------------

void
List_lines( n )
	int			n;
 {
	while( n-- ) fprintf( ListFile, "\n" );
	list__lnPrinted = TRUE;
 }


//----------------------------------------------------

void
List_addrSrc( addr )
	int			addr;
 {
	fprintf( ListFile, "  %05o %6s  %s\n", addr, " ", list__srcLn );
	list__lnPrinted = TRUE;
 }


//----------------------------------------------------

void
List_valSrc( val )
	int			val;
 {
	fprintf( ListFile, "  %5s %06o  %s\n", " ", val, list__srcLn );
	list__lnPrinted = TRUE;
 }


//----------------------------------------------------

void
List_addrCodeSrc( addr, code )
	int			addr, code;
 {
	fprintf( ListFile, "  %05o %06o", addr, code );
	if( !list__lnPrinted ) fprintf( ListFile, "  %s", list__srcLn );	// so multi-word emits only print source once 
	fprintf( ListFile, "\n" );
	list__lnPrinted = TRUE;
 }


//----------------------------------------------------
// Queue an error message for the current source line.
// The string pointed to by text must remain valid until List_lnFin is called.
// First-queued error takes precedence, others are dropped.

void
List_err( text )
	char			*text;
 {
	if( list__errInLine )
		return;

	list__errInLine = text;
	++Err_count;
 }


//----------------------------------------------------
// Indicate the start of processing of a source line.
// The string pointed to by srcLn must remain valid until List_lnFin is called.
// All listing activity should take place between a List_lnStart and List_lnFin pair.

void
List_lnStart( srcLn )
	char			*srcLn;
 {
	list__srcLn = srcLn;
	list__lnPrinted = FALSE;
	list__errInLine = NULL;
 }


//----------------------------------------------------
// Finish dealing with the current source line.
// If it hasn't been printed, do so.
// If an error for the line was queued, print the error message.

void
List_lnFin( doSrc )
	int			doSrc;
 {
	if( (doSrc || list__errInLine) && !list__lnPrinted )
		fprintf( ListFile, "  %12s  %s\n", " ", list__srcLn );

	if( list__errInLine )
		fprintf( ListFile, "ERROR: %s\n", list__errInLine );
 }


//* EOF
//***********************************************************************************************************
