///////////////////////////////////////////////////////////////////////////////
//
// main.cpp
//
// $Id$
//
// Main program, options, etc etc etc
//
// Copyright (C) Simon Collis 2000-2018
//
//-----------------------------------------------------------------------------
//
// This file is part of The A6 Cross-Assembler.
//
// a6 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.
//
// a6 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 a6. If not, see <http://www.gnu.org/licenses/>.
//
///////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Assembler.h"
#include "Conditional.h"
#include "Config.h"
#include "Cpu.h"
#include "Error.h"
#include "Label.h"
#include "OutputFactory.h"
#include "String.h"
#include "SymbolTable.h"
#include "Text.h"
#include "Main.h"
// *** ProcessCommandLine
void ProcessCommandLine(Assembler *asm, int argc, char **argv)
{
}
/*===========================================================================*
* MAIN *
*===========================================================================*/
int main(int argc, char **argv)
{
// Process command line arguments
AsmOptions* opts = ProcessCommandLine(asm, argc, argv);
// Setup for pass 2
if (opts->OutputFileName() == null)
{
// Build output filename
}
/* char *s;*/
size_t i;
int freeoutname = 0;
/* char *outname = NULL;*/
FILE *infile = NULL;
/* TODO -- add code to get a6_homedir */
/* Defaults before command line */
g_listout = stdout;
ctx->ProcessCommandLine(argc, argv);
/* If no input name, show usage and exit */
if(inname == 0)
{
usage();
exit(EXIT_FAILURE);
}
/* If no output name, generate it */
if(g_outname == NULL)
{
g_outname = (char *)a6malloc(strlen(inname) + 5);
freeoutname = 1;
strcpy(g_outname, inname);
i = strlen(g_outname);
while(i > 0 && g_outname[i] != '.') i--;
if(i == 0)
i = strlen(g_outname);
g_outname[i++] = '.';
g_outname[i++] = outf_extension[g_outf_format][0];
g_outname[i++] = outf_extension[g_outf_format][1];
g_outname[i++] = outf_extension[g_outf_format][2];
g_outname[i] = '\0';
}
/* Check that main file exists */
infile = fopen(inname, "r");
if(infile == NULL)
{
printf("file not found: '%s'\n", inname);
exit(EXIT_FAILURE);
}
/* CALL ALL ASSEMBLY ROUTINES HERE */
//----------------------------------------------------------------------------
/* Pass 1 */
if (ctx->IsVerbose())
{
printf("\npass 1\n\n");
}
g_undocopsflag = 0; /* Undoc. ops OFF by default */
pstext_setcset(CSET_PETSCII); /* Default = PET ASCII */
lbl_init(); /* Set up label indexing */
/* Actual pass */
af_open(inname, infile);
while(af_parseline());
//----------------------------------------------------------------------------
// Pass 2
ctx->BeginPass2();
if (ctx->IsVerbose())
{
printf("\npass 2\n\n");
}
// Reset conditional assembly stack
Conditional::Clear();
// Clear up asmfiles (This has to be the last cleanup!)
af_reset(); /* Reset af_postotal */
while(af_close() != 0);
/* ------------------------------------------------------- */
/* Pass 2 */
g_undocopsflag = 0; /* Undoc. ops OFF by default */
lbl_reset(); /* Reset local labels */
pstext_setcset(CSET_PETSCII); /* Default = PET ASCII */
outf_setpc(0); /* Reset PC counter */
outf_open(g_outname); /* Open output file */
/* Actual pass */
af_open(inname, 0);
while(af_parseline());
//----------------------------------------------------------------------------
// End of pass 2
// Warning if conditional stack not empty
if(Conditional::OpenItems() > 0)
{
ErrorHandler::RaiseError(Warning, UnclosedCond);
}
/* Cleanup input files */
while(af_close() != 0);
/* List symbol table if required */
if(symflag != 0)
{
if(verbose)
printf("\n\n");
lbl_dumpsym();
}
/* Tidy everything up */
af_cleanup(); /* Clean up assembly files */
lbl_destroy(); /* Delete all allocated labels */
outf_close();
if(freeoutname)
free(g_outname);
/* ------------------------------------------------------- */
/* Final message */
if(ErrorHandler::ErrorCount() > 0)
{
printf("\n %u errors; assembly failed.\n", ErrorHandler::ErrorCount());
exit(EXIT_FAILURE);
}
if(verbose)
printf("\n assembly successful; no errors.\n");
return(EXIT_SUCCESS);
}