view src/list.c @ 45:be459d69f481

Fixed stupid error in opening list file
author lost
date Sun, 04 Jan 2009 06:54:58 +0000
parents 2330b88f9600
children 804d7465e0f9
line wrap: on
line source

/*
list.c
Copyright © 2009 William Astle

This file is part of LWASM.

LWASM is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.

You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.

Contains code for displaying a program listings, etc.
*/

#define __list_c_seen__

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

#include "lwasm.h"

void lwasm_show_errors(asmstate_t *as)
{
	lwasm_line_t *l;
	lwasm_error_t *e;
	
	for (l = as -> lineshead; l; l = l -> next)
	{
		if (l -> err)
		{
			for (e = l -> err; e; e = e -> next)
			{
				fprintf(stderr, "ERROR: %s\n", e -> mess);
			}
			fprintf(stderr, "%s:%d: %s\n", l -> filename, l -> lineno, l -> text);
		}
	}
}

void lwasm_list(asmstate_t *as)
{
	FILE *lf;
	lwasm_line_t *l;
	int c;
	char *p;
	
	if (as -> listfile[0] == '-' && as -> listfile[1] == '\0')
		lf = stdout;
	else
	{
		lf = fopen(as -> listfile, "w");
		if (!lf)
		{
			fprintf(stderr, "Unable to open list file '%s'. No listing will be generated: ", as -> listfile);
			perror("");
			goto showerr;
		}
	}

	for (l = as -> lineshead; l; l = l -> next)
	{
		fprintf(lf, "%04X ", l -> codeaddr);
		
		if (l -> codelen > 0)
		{
			for (c = 0; c < l -> codelen && c < 5; c++)
			{
				fprintf(lf, "%02X", l -> bytes[c]);
			}
		}
		while (c < 5)
		{
			fprintf(lf, "  ");
			c++;
		}
		fprintf(lf, " %20.20s:%05d ", l -> filename, l -> lineno);
		
		// print line here
		for (c = 0, p = l -> text; *p; c++, p++)
		{
			if (*p == '\t')
			{
				int c2;
				c2 = 8 - (c % 8);
				while (c2--) fputc(' ', lf);
			}
			else
				fputc(*p, lf);
		}
		fputc('\n', lf);
		
		if (l -> codelen > 5)
		{
			fprintf(lf, "     ");
			for (c = 5; c < l -> codelen; c++)
			{
				if (!(c % 5) && c != 5)
				{
					fprintf(lf, "\n     ");
				}
				fprintf(lf, "%02X", l -> bytes[c]);
			}
			fputc('\n', lf);
		}
	}
	if (lf != stdout)
		fclose(lf);

showerr:
	lwasm_show_errors(as);
}