From 1081c1dcdddab5598c18398ac0991daf73bc9d8f Mon Sep 17 00:00:00 2001 From: cuz Date: Wed, 14 Jun 2000 09:32:22 +0000 Subject: [PATCH] More common subroutines git-svn-id: svn://svn.cc65.org/cc65/trunk@69 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- src/ca65/expr.c | 10 ++-- src/ca65/global.c | 4 +- src/ca65/global.h | 2 - src/ca65/incpath.c | 11 +++-- src/ca65/istack.c | 7 +-- src/ca65/listing.c | 12 ++--- src/ca65/macro.c | 16 +++--- src/ca65/main.c | 31 +++++------- src/ca65/make/gcc.mak | 2 - src/ca65/make/watcom.mak | 4 -- src/ca65/objcode.c | 10 ++-- src/ca65/objfile.c | 3 +- src/ca65/options.c | 6 +-- src/ca65/scanner.c | 19 +++---- src/ca65/symtab.c | 6 +-- src/ca65/toklist.c | 18 +++---- src/ca65/ulabel.c | 6 +-- src/common/abend.c | 74 ++++++++++++++++++++++++++++ src/common/abend.h | 59 ++++++++++++++++++++++ src/common/cmdline.c | 46 ++++++++++++----- src/common/cmdline.h | 5 +- src/{ca65 => common}/fname.c | 7 ++- src/common/fname.h | 61 +++++++++++++++++++++++ src/common/make/gcc.mak | 5 +- src/common/make/watcom.mak | 7 ++- src/{ca65/mem.c => common/xmalloc.c} | 63 ++++++++++++----------- src/{ca65/mem.h => common/xmalloc.h} | 20 ++++---- 27 files changed, 363 insertions(+), 151 deletions(-) create mode 100644 src/common/abend.c create mode 100644 src/common/abend.h rename src/{ca65 => common}/fname.c (96%) create mode 100644 src/common/fname.h rename src/{ca65/mem.c => common/xmalloc.c} (61%) rename src/{ca65/mem.h => common/xmalloc.h} (88%) diff --git a/src/ca65/expr.c b/src/ca65/expr.c index 509dc6eba..44c7524d1 100644 --- a/src/ca65/expr.c +++ b/src/ca65/expr.c @@ -34,11 +34,11 @@ #include "../common/exprdefs.h" +#include "../common/xmalloc.h" #include "error.h" #include "global.h" #include "instr.h" -#include "mem.h" #include "nexttok.h" #include "objcode.h" #include "objfile.h" @@ -85,7 +85,7 @@ static ExprNode* NewExprNode (void) FreeExprNodes = N->Left; } else { /* Allocate fresh memory */ - N = Xmalloc (sizeof (ExprNode)); + N = xmalloc (sizeof (ExprNode)); } N->Op = EXPR_NULL; N->Left = N->Right = 0; @@ -106,7 +106,7 @@ static void FreeExprNode (ExprNode* E) FreeExprNodes = E; } else { /* Free the memory */ - Xfree (E); + xfree (E); } } } @@ -114,10 +114,10 @@ static void FreeExprNode (ExprNode* E) /*****************************************************************************/ -/* Dump an expression tree on stdout for debugging */ +/* Dump an expression tree on stdout for debugging */ /*****************************************************************************/ - + static void InternalDumpExpr (ExprNode* Expr) /* Dump an expression in UPN */ diff --git a/src/ca65/global.c b/src/ca65/global.c index 24fb34f28..574654edc 100644 --- a/src/ca65/global.c +++ b/src/ca65/global.c @@ -43,8 +43,6 @@ -const char* ProgName = "ca65"; /* Program name */ - /* File names */ const char* InFile = 0; /* Name of input file */ const char* OutFile = 0; /* Name of output file */ @@ -70,7 +68,7 @@ unsigned char NoColonLabels = 0; /* Allow labels without a colon */ unsigned char LooseStringTerm = 0; /* Allow ' as string terminator */ unsigned char AtInIdents = 0; /* Allow '@' in identifiers */ unsigned char DollarInIdents = 0; /* Allow '$' in identifiers */ - + diff --git a/src/ca65/global.h b/src/ca65/global.h index 4bd7f9af0..bcca6eca2 100644 --- a/src/ca65/global.h +++ b/src/ca65/global.h @@ -44,8 +44,6 @@ -extern const char* ProgName; /* Program name */ - /* File names */ extern const char* InFile; /* Name of input file */ extern const char* OutFile; /* Name of output file */ diff --git a/src/ca65/incpath.c b/src/ca65/incpath.c index 287367169..9021f44a6 100644 --- a/src/ca65/incpath.c +++ b/src/ca65/incpath.c @@ -44,7 +44,8 @@ # include #endif -#include "mem.h" +#include "../common/xmalloc.h" + #include "incpath.h" @@ -83,7 +84,7 @@ static char* Add (char* Orig, const char* New) } /* Allocate memory for the new string */ - NewPath = Xmalloc (OrigLen + NewLen + 2); + NewPath = xmalloc (OrigLen + NewLen + 2); /* Copy the strings */ memcpy (NewPath, Orig, OrigLen); @@ -92,7 +93,7 @@ static char* Add (char* Orig, const char* New) NewPath [OrigLen+NewLen+1] = '\0'; /* Delete the original path */ - Xfree (Orig); + xfree (Orig); /* Return the new path */ return NewPath; @@ -138,8 +139,8 @@ static char* Find (const char* Path, const char* File) /* Check if this file exists */ if (access (PathName, R_OK) == 0) { /* The file exists */ - return StrDup (PathName); - } + return xstrdup (PathName); + } /* Skip a list separator if we have one */ if (*P == ';') { diff --git a/src/ca65/istack.c b/src/ca65/istack.c index 6e2f9e241..5ba35a906 100644 --- a/src/ca65/istack.c +++ b/src/ca65/istack.c @@ -33,8 +33,9 @@ +#include "../common/xmalloc.h" + #include "error.h" -#include "mem.h" #include "istack.h" @@ -80,7 +81,7 @@ void PushInput (int (*Func) (void*), void* Data, const char* Desc) } /* Create a new stack element */ - E = Xmalloc (sizeof (*E)); + E = xmalloc (sizeof (*E)); /* Initialize it */ E->Func = Func; @@ -109,7 +110,7 @@ void PopInput (void) IStack = IStack->Next; /* And delete it */ - Xfree (E); + xfree (E); } diff --git a/src/ca65/listing.c b/src/ca65/listing.c index 8177c6e15..05dc6c4ec 100644 --- a/src/ca65/listing.c +++ b/src/ca65/listing.c @@ -37,13 +37,13 @@ #include #include +#include "../common/fname.h" #include "../common/segdefs.h" #include "../common/version.h" +#include "../common/xmalloc.h" #include "error.h" -#include "fname.h" #include "global.h" -#include "mem.h" #include "objcode.h" #include "listing.h" @@ -94,7 +94,7 @@ void NewListingLine (const char* Line, unsigned char File, unsigned char Depth) } /* Allocate memory */ - L = Xmalloc (sizeof (ListLine) + Len); + L = xmalloc (sizeof (ListLine) + Len); /* Initialize the fields. */ L->Next = 0; @@ -201,7 +201,7 @@ static void PrintPageHeader (FILE* F, const ListLine* L) */ { /* Print the header on the new page */ - fprintf (F, + fprintf (F, "ca65 V%u.%u.%u - (C) Copyright 1998-2000 Ullrich von Bassewitz\n" "Main file : %s\n" "Current file: %s\n" @@ -328,7 +328,7 @@ void CreateListing (void) } /* Allocate memory for the given number of bytes */ - Buf = Xmalloc (Count*2+1); + Buf = xmalloc (Count*2+1); /* Copy an ASCII representation of the bytes into the buffer */ B = Buf; @@ -422,7 +422,7 @@ void CreateListing (void) } /* Delete the temporary buffer */ - Xfree (Buf); + xfree (Buf); /* Next line */ L = L->Next; diff --git a/src/ca65/macro.c b/src/ca65/macro.c index a5eb78262..67c6b7b9b 100644 --- a/src/ca65/macro.c +++ b/src/ca65/macro.c @@ -37,11 +37,11 @@ #include #include "../common/hashstr.h" +#include "../common/xmalloc.h" #include "condasm.h" #include "error.h" #include "istack.h" -#include "mem.h" #include "nexttok.h" #include "pseudo.h" #include "toklist.h" @@ -123,7 +123,7 @@ static IdDesc* NewIdDesc (const char* Id) { /* Allocate memory */ unsigned Len = strlen (Id); - IdDesc* I = Xmalloc (sizeof (IdDesc) + Len); + IdDesc* I = xmalloc (sizeof (IdDesc) + Len); /* Initialize the struct */ I->Next = 0; @@ -141,7 +141,7 @@ static Macro* NewMacro (const char* Name, unsigned HashVal, unsigned char Style) { /* Allocate memory */ unsigned Len = strlen (Name); - Macro* M = Xmalloc (sizeof (Macro) + Len); + Macro* M = xmalloc (sizeof (Macro) + Len); /* Initialize the macro struct */ M->LocalCount = 0; @@ -174,7 +174,7 @@ static MacExp* NewMacExp (Macro* M) unsigned I; /* Allocate memory */ - MacExp* E = Xmalloc (sizeof (MacExp)); + MacExp* E = xmalloc (sizeof (MacExp)); /* Initialize the data */ E->M = M; @@ -184,7 +184,7 @@ static MacExp* NewMacExp (Macro* M) E->LocalStart = LocalName; LocalName += M->LocalCount; E->ParamCount = 0; - E->Params = Xmalloc (M->ParamCount * sizeof (TokNode*)); + E->Params = xmalloc (M->ParamCount * sizeof (TokNode*)); E->ParamExp = 0; for (I = 0; I < M->ParamCount; ++I) { E->Params [I] = 0; @@ -209,9 +209,9 @@ static void FreeMacExp (MacExp* E) /* Free the parameter list */ for (I = 0; I < E->ParamCount; ++I) { - Xfree (E->Params [I]); + xfree (E->Params [I]); } - Xfree (E->Params); + xfree (E->Params); /* Free the final token if we have one */ if (E->Final) { @@ -219,7 +219,7 @@ static void FreeMacExp (MacExp* E) } /* Free the structure itself */ - Xfree (E); + xfree (E); } diff --git a/src/ca65/main.c b/src/ca65/main.c index 5d30bcc0c..f224bd429 100644 --- a/src/ca65/main.c +++ b/src/ca65/main.c @@ -49,7 +49,7 @@ #include "instr.h" #include "listing.h" #include "macro.h" -#include "mem.h" +/*#include "mem.h"*/ #include "nexttok.h" #include "objcode.h" #include "objfile.h" @@ -89,7 +89,7 @@ static void Usage (void) " --auto-import\t\tMark unresolved symbols as import\n" " --cpu type\t\tSet cpu type\n" " --debug-info\t\tAdd debug info to object file\n" - " --help\t\tPrint this text\n" + " --help\t\tHelp (this text)\n" " --ignore-case\t\tIgnore case of symbols\n" " --include-dir dir\tSet an include directory search path\n" " --listing\t\tCreate a listing if assembly was ok\n" @@ -98,7 +98,6 @@ static void Usage (void) " --verbose\t\tIncrease verbosity\n" " --version\t\tPrint the assembler version\n", ProgName); - exit (EXIT_FAILURE); } @@ -128,7 +127,7 @@ static void DefineSymbol (const char* Def) /* The symbol must start with a character or underline */ if (Def [0] != '_' && !isalpha (Def [0])) { - InvSym (Def); + InvDef (Def); } P = Def; @@ -145,7 +144,7 @@ static void DefineSymbol (const char* Def) /* Do we have a value given? */ if (*P != '=') { if (*P != '\0') { - InvSym (Def); + InvDef (Def); } Val = 0; } else { @@ -154,11 +153,11 @@ static void DefineSymbol (const char* Def) if (*P == '$') { ++P; if (sscanf (P, "%lx", &Val) != 1) { - InvSym (Def); + InvDef (Def); } } else { if (sscanf (P, "%li", &Val) != 1) { - InvSym (Def); + InvDef (Def); } } } @@ -219,6 +218,7 @@ static void OptHelp (const char* Opt, const char* Arg) /* Print usage information and exit */ { Usage (); + exit (EXIT_SUCCESS); } @@ -450,15 +450,7 @@ int main (int argc, char* argv []) int I; /* Initialize the cmdline module */ - InitCmdLine (argc, argv); - - /* Set the program name */ - ProgName = argv [0]; - - /* We must have a file name */ - if (argc < 2) { - Usage (); - } + InitCmdLine (argc, argv, "ca65"); /* Enter the base lexical level. We must do that here, since we may * define symbols using -D. @@ -532,8 +524,9 @@ int main (int argc, char* argv []) } else { /* Filename. Check if we already had one */ if (InFile) { - fprintf (stderr, "Don't know what to do with `%s'\n", Arg); - Usage (); + fprintf (stderr, "%s: Don't know what to do with `%s'\n", + ProgName, Arg); + exit (EXIT_FAILURE); } else { InFile = Arg; } @@ -545,7 +538,7 @@ int main (int argc, char* argv []) /* Do we have an input file? */ if (InFile == 0) { - fprintf (stderr, "No input file\n"); + fprintf (stderr, "%s: No input files\n", ProgName); exit (EXIT_FAILURE); } diff --git a/src/ca65/make/gcc.mak b/src/ca65/make/gcc.mak index 7476985a0..6013fca2d 100644 --- a/src/ca65/make/gcc.mak +++ b/src/ca65/make/gcc.mak @@ -10,7 +10,6 @@ OBJS = condasm.o \ ea.o \ error.o \ expr.o \ - fname.o \ fragment.o \ global.o \ incpath.o \ @@ -20,7 +19,6 @@ OBJS = condasm.o \ macpack.o \ macro.o \ main.o \ - mem.o \ nexttok.o \ objcode.o \ objfile.o \ diff --git a/src/ca65/make/watcom.mak b/src/ca65/make/watcom.mak index 92e34ce43..8b4eba838 100644 --- a/src/ca65/make/watcom.mak +++ b/src/ca65/make/watcom.mak @@ -67,7 +67,6 @@ OBJS = condasm.obj \ ea.obj \ error.obj \ expr.obj \ - fname.obj \ fragment.obj \ global.obj \ incpath.obj \ @@ -77,7 +76,6 @@ OBJS = condasm.obj \ macpack.obj \ macro.obj \ main.obj \ - mem.obj \ nexttok.obj \ objcode.obj \ objfile.obj \ @@ -113,7 +111,6 @@ FILE condasm.obj FILE ea.obj FILE error.obj FILE expr.obj -FILE fname.obj FILE fragment.obj FILE global.obj FILE incpath.obj @@ -123,7 +120,6 @@ FILE listing.obj FILE macpack.obj FILE macro.obj FILE main.obj -FILE mem.obj FILE nexttok.obj FILE objcode.obj FILE objfile.obj diff --git a/src/ca65/objcode.c b/src/ca65/objcode.c index 029e01e3c..31d9a896b 100644 --- a/src/ca65/objcode.c +++ b/src/ca65/objcode.c @@ -38,12 +38,12 @@ #include #include "../common/segdefs.h" +#include "../common/xmalloc.h" #include "error.h" #include "fragment.h" #include "global.h" #include "listing.h" -#include "mem.h" #include "objfile.h" #include "scanner.h" #include "symtab.h" @@ -140,7 +140,7 @@ static Segment* NewSegment (const char* Name, unsigned SegType) } while (*N); /* Create a new segment */ - S = Xmalloc (sizeof (*S)); + S = xmalloc (sizeof (*S)); /* Initialize it */ S->List = 0; @@ -150,7 +150,7 @@ static Segment* NewSegment (const char* Name, unsigned SegType) S->SegType = SegType; S->PC = 0; S->Num = SegmentCount++; - S->Name = StrDup (Name); + S->Name = xstrdup (Name); /* Insert it into the segment list */ SegmentLast->List = S; @@ -599,7 +599,7 @@ static Fragment* NewFragment (unsigned char Type, unsigned short Len) Fragment* F; /* Create a new fragment */ - F = Xmalloc (sizeof (*F)); + F = xmalloc (sizeof (*F)); /* Initialize it */ F->List = 0; @@ -631,7 +631,7 @@ static Fragment* NewFragment (unsigned char Type, unsigned short Len) LineCur->FragList = F; /* First fragment - set the PC LineCur->PC = GetPC (); - LineCur->Reloc = RelocMode; + LineCur->Reloc = RelocMode; */ } else { LineCur->FragLast->LineList = F; diff --git a/src/ca65/objfile.c b/src/ca65/objfile.c index b79ff0970..ca2e872ad 100644 --- a/src/ca65/objfile.c +++ b/src/ca65/objfile.c @@ -38,12 +38,11 @@ #include #include +#include "../common/fname.h" #include "../common/objdefs.h" #include "global.h" #include "error.h" -#include "fname.h" -#include "mem.h" #include "objfile.h" diff --git a/src/ca65/options.c b/src/ca65/options.c index ca05f0b48..0af381818 100644 --- a/src/ca65/options.c +++ b/src/ca65/options.c @@ -34,8 +34,8 @@ #include "../common/optdefs.h" +#include "../common/xmalloc.h" -#include "mem.h" #include "error.h" #include "objfile.h" #include "options.h" @@ -67,7 +67,7 @@ static Option* NewOption (unsigned char Type) Option* Opt; /* Allocate memory */ - Opt = Xmalloc (sizeof (*Opt)); + Opt = xmalloc (sizeof (*Opt)); /* Initialize fields */ Opt->Next = 0; @@ -101,7 +101,7 @@ void OptStr (unsigned char Type, const char* Text) Fatal (FAT_STRING_TOO_LONG); } O = NewOption (Type); - O->V.Str = StrDup (Text); + O->V.Str = xstrdup (Text); } diff --git a/src/ca65/scanner.c b/src/ca65/scanner.c index 30ba6db7c..7b0718b4d 100644 --- a/src/ca65/scanner.c +++ b/src/ca65/scanner.c @@ -40,16 +40,17 @@ #include #include +#include "../common/fname.h" +#include "../common/xmalloc.h" + #include "condasm.h" #include "error.h" -#include "fname.h" #include "global.h" #include "incpath.h" #include "instr.h" #include "istack.h" #include "listing.h" #include "macro.h" -#include "mem.h" #include "objfile.h" #include "toklist.h" #include "scanner.h" @@ -353,7 +354,7 @@ void NewInputFile (const char* Name) } /* Free the allocated memory */ - Xfree (PathName); + xfree (PathName); } @@ -367,11 +368,11 @@ void NewInputFile (const char* Name) } Files [FileCount].MTime = Buf.st_mtime; Files [FileCount].Size = Buf.st_size; - Files [FileCount].Name = StrDup (Name); + Files [FileCount].Name = xstrdup (Name); ++FileCount; /* Create a new state variable and initialize it */ - I = Xmalloc (sizeof (*I)); + I = xmalloc (sizeof (*I)); I->F = F; I->Pos.Line = 0; I->Pos.Col = 0; @@ -407,7 +408,7 @@ void DoneInputFile (void) /* Cleanup the current stuff */ fclose (I->F); - Xfree (I); + xfree (I); --ICount; } @@ -419,7 +420,7 @@ void NewInputData (const char* Data, int Malloced) InputData* I; /* Create a new state variable and initialize it */ - I = Xmalloc (sizeof (*I)); + I = xmalloc (sizeof (*I)); I->Data = Data; I->Pos = Data; I->Malloced = Malloced; @@ -451,9 +452,9 @@ static void DoneInputData (void) /* Cleanup the current stuff */ if (I->Malloced) { - Xfree (I->Data); + xfree (I->Data); } - Xfree (I); + xfree (I); } diff --git a/src/ca65/symtab.c b/src/ca65/symtab.c index 53843eb1c..d1f7ce101 100644 --- a/src/ca65/symtab.c +++ b/src/ca65/symtab.c @@ -37,10 +37,10 @@ #include "../common/symdefs.h" #include "../common/hashstr.h" +#include "../common/xmalloc.h" #include "global.h" #include "error.h" -#include "mem.h" #include "expr.h" #include "objfile.h" #include "symtab.h" @@ -146,7 +146,7 @@ static SymEntry* NewSymEntry (const char* Name) Len = strlen (Name); /* Allocate memory */ - S = Xmalloc (sizeof (SymEntry) + Len); + S = xmalloc (sizeof (SymEntry) + Len); /* Initialize the entry */ S->Left = 0; @@ -174,7 +174,7 @@ static SymTable* NewSymTable (unsigned Size) SymTable* S; /* Allocate memory */ - S = Xmalloc (sizeof (SymTable) + (Size-1) * sizeof (SymEntry*)); + S = xmalloc (sizeof (SymTable) + (Size-1) * sizeof (SymEntry*)); /* Set variables and clear hash table entries */ S->TableSlots = Size; diff --git a/src/ca65/toklist.c b/src/ca65/toklist.c index dc2712d39..a45d8f0a9 100644 --- a/src/ca65/toklist.c +++ b/src/ca65/toklist.c @@ -35,7 +35,8 @@ #include -#include "mem.h" +#include "../common/xmalloc.h" + #include "scanner.h" #include "toklist.h" @@ -54,7 +55,7 @@ TokNode* NewTokNode (void) /* Allocate memory */ unsigned Len = TokHasSVal (Tok)? strlen (SVal) : 0; - T = Xmalloc (sizeof (TokNode) + Len); + T = xmalloc (sizeof (TokNode) + Len); /* Initialize the token contents */ T->Next = 0; @@ -73,7 +74,7 @@ TokNode* NewTokNode (void) void FreeTokNode (TokNode* T) /* Free the given token node */ { - Xfree (T); + xfree (T); } @@ -132,7 +133,7 @@ TokList* NewTokList (void) /* Create a new, empty token list */ { /* Allocate memory for the list structure */ - TokList* T = Xmalloc (sizeof (TokList)); + TokList* T = xmalloc (sizeof (TokList)); /* Initialize the fields */ InitTokList (T); @@ -155,7 +156,7 @@ void FreeTokList (TokList* List) } /* Free the list structure itself */ - Xfree (List); + xfree (List); } @@ -168,9 +169,9 @@ void AddCurTok (TokList* List) /* Insert the node into the list */ if (List->Root == 0) { - List->Root = T; + List->Root = T; } else { - List->Last->Next = T; + List->Last->Next = T; } List->Last = T; @@ -180,6 +181,3 @@ void AddCurTok (TokList* List) - - - diff --git a/src/ca65/ulabel.c b/src/ca65/ulabel.c index adfe2e5f1..1618afb73 100644 --- a/src/ca65/ulabel.c +++ b/src/ca65/ulabel.c @@ -34,10 +34,10 @@ #include "../common/filepos.h" +#include "../common/xmalloc.h" #include "error.h" #include "expr.h" -#include "mem.h" #include "scanner.h" #include "ulabel.h" @@ -80,7 +80,7 @@ static ULabel* NewULabel (ExprNode* Val) */ { /* Allocate memory for the ULabel structure */ - ULabel* L = Xmalloc (sizeof (ULabel)); + ULabel* L = xmalloc (sizeof (ULabel)); /* Initialize the fields */ L->Pos = CurPos; @@ -227,7 +227,7 @@ void ULabCheck (void) */ if (ULabCount) { unsigned I = 0; - ULabList = Xmalloc (ULabCount * sizeof (ULabel*)); + ULabList = xmalloc (ULabCount * sizeof (ULabel*)); L = ULabRoot; while (L) { ULabList[I] = L; diff --git a/src/common/abend.c b/src/common/abend.c new file mode 100644 index 000000000..741badd9a --- /dev/null +++ b/src/common/abend.c @@ -0,0 +1,74 @@ +/*****************************************************************************/ +/* */ +/* abend.c */ +/* */ +/* Abnormal program end */ +/* */ +/* */ +/* */ +/* (C) 2000 Ullrich von Bassewitz */ +/* Wacholderweg 14 */ +/* D-70597 Stuttgart */ +/* EMail: uz@musoftware.de */ +/* */ +/* */ +/* This software is provided 'as-is', without any expressed or implied */ +/* warranty. In no event will the authors be held liable for any damages */ +/* arising from the use of this software. */ +/* */ +/* Permission is granted to anyone to use this software for any purpose, */ +/* including commercial applications, and to alter it and redistribute it */ +/* freely, subject to the following restrictions: */ +/* */ +/* 1. The origin of this software must not be misrepresented; you must not */ +/* claim that you wrote the original software. If you use this software */ +/* in a product, an acknowledgment in the product documentation would be */ +/* appreciated but is not required. */ +/* 2. Altered source versions must be plainly marked as such, and must not */ +/* be misrepresented as being the original software. */ +/* 3. This notice may not be removed or altered from any source */ +/* distribution. */ +/* */ +/*****************************************************************************/ + + + +#include +#include +#include + +#include "cmdline.h" +#include "abend.h" + + + +/*****************************************************************************/ +/* Code */ +/*****************************************************************************/ + + + +void AbEnd (const char* Format, ...) +/* Print a message preceeded by the program name and terminate the program + * with an error exit code. + */ +{ + va_list ap; + + /* Print the program name */ + fprintf (stderr, "%s: ", ProgName); + + /* Format the given message and print it */ + va_start (ap, Format); + vfprintf (stderr, Format, ap); + va_end (ap); + + /* Add a newline */ + fprintf (stderr, "\n"); + + /* Terminate the program */ + exit (EXIT_FAILURE); +} + + + diff --git a/src/common/abend.h b/src/common/abend.h new file mode 100644 index 000000000..ef6f9cc17 --- /dev/null +++ b/src/common/abend.h @@ -0,0 +1,59 @@ +/*****************************************************************************/ +/* */ +/* abend.h */ +/* */ +/* Abnormal program end */ +/* */ +/* */ +/* */ +/* (C) 2000 Ullrich von Bassewitz */ +/* Wacholderweg 14 */ +/* D-70597 Stuttgart */ +/* EMail: uz@musoftware.de */ +/* */ +/* */ +/* This software is provided 'as-is', without any expressed or implied */ +/* warranty. In no event will the authors be held liable for any damages */ +/* arising from the use of this software. */ +/* */ +/* Permission is granted to anyone to use this software for any purpose, */ +/* including commercial applications, and to alter it and redistribute it */ +/* freely, subject to the following restrictions: */ +/* */ +/* 1. The origin of this software must not be misrepresented; you must not */ +/* claim that you wrote the original software. If you use this software */ +/* in a product, an acknowledgment in the product documentation would be */ +/* appreciated but is not required. */ +/* 2. Altered source versions must be plainly marked as such, and must not */ +/* be misrepresented as being the original software. */ +/* 3. This notice may not be removed or altered from any source */ +/* distribution. */ +/* */ +/*****************************************************************************/ + + + +#ifndef ABEND_H +#define ABEND_H + + + +/*****************************************************************************/ +/* Code */ +/*****************************************************************************/ + + + +void AbEnd (const char* Format, ...); +/* Print a message preceeded by the program name and terminate the program + * with an error exit code. + */ + + + +/* End of abend.h */ + +#endif + + + diff --git a/src/common/cmdline.c b/src/common/cmdline.c index 564a4203a..3403cdd7c 100644 --- a/src/common/cmdline.c +++ b/src/common/cmdline.c @@ -33,9 +33,9 @@ -#include -#include +#include +#include "abend.h" #include "cmdline.h" @@ -46,25 +46,50 @@ +/* Program name - is set after call to InitCmdLine */ +const char* ProgName; + +/* The program argument vector */ static char** ArgVec = 0; static unsigned ArgCount = 0; /*****************************************************************************/ -/* Code */ +/* Code */ /*****************************************************************************/ -void InitCmdLine (unsigned aArgCount, char* aArgVec[]) +void InitCmdLine (unsigned aArgCount, char* aArgVec[], const char* aProgName) /* Initialize command line parsing. aArgVec is the argument array terminated by * a NULL pointer (as usual), ArgCount is the number of valid arguments in the * array. Both arguments are remembered in static storage. */ { + /* Remember the argument vector */ ArgCount = aArgCount; ArgVec = aArgVec; + + /* Get the program name from argv[0] but strip a path */ + if (ArgVec[0] == 0) { + /* Use the default name given */ + ProgName = aProgName; + } else { + /* Strip a path */ + ProgName = strchr (ArgVec[0], '\0'); + while (ProgName > ArgVec[0]) { + --ProgName; + if (*ProgName == '/' || *ProgName == '\\') { + ++ProgName; + break; + } + } + if (ProgName[0] == '\0') { + /* Use the default */ + ProgName = aProgName; + } + } } @@ -72,8 +97,7 @@ void InitCmdLine (unsigned aArgCount, char* aArgVec[]) void UnknownOption (const char* Opt) /* Print an error about an unknown option. */ { - fprintf (stderr, "Unknown option: %s\n", Opt); - exit (EXIT_FAILURE); + AbEnd ("Unknown option: %s\n", Opt); } @@ -81,8 +105,7 @@ void UnknownOption (const char* Opt) void NeedArg (const char* Opt) /* Print an error about a missing option argument and exit. */ { - fprintf (stderr, "Option requires an argument: %s\n", Opt); - exit (EXIT_FAILURE); + AbEnd ("Option requires an argument: %s\n", Opt); } @@ -90,8 +113,7 @@ void NeedArg (const char* Opt) void InvDef (const char* Def) /* Print an error about an invalid definition and die */ { - fprintf (stderr, "Invalid definition: `%s'\n", Def); - exit (EXIT_FAILURE); + AbEnd ("Invalid definition: `%s'\n", Def); } @@ -130,9 +152,9 @@ void LongOption (int* ArgNum, const LongOpt* OptTab, unsigned OptCount) if (strcmp (Opt, OptTab->Option) == 0) { /* Found, call the function */ if (OptTab->ArgCount > 0) { - OptTab->Func (Opt, ArgVec[++(*ArgNum)]); + OptTab->Func (Opt, ArgVec[++(*ArgNum)]); } else { - OptTab->Func (Opt, 0); + OptTab->Func (Opt, 0); } /* Done */ return; diff --git a/src/common/cmdline.h b/src/common/cmdline.h index da620ac44..1173e10b6 100644 --- a/src/common/cmdline.h +++ b/src/common/cmdline.h @@ -44,6 +44,9 @@ +/* Program name - is set after call to InitCmdLine */ +extern const char* ProgName; + /* Structure defining a long option */ typedef struct LongOpt LongOpt; struct LongOpt { @@ -60,7 +63,7 @@ struct LongOpt { -void InitCmdLine (unsigned aArgCount, char* aArgVec[]); +void InitCmdLine (unsigned aArgCount, char* aArgVec[], const char* aProgName); /* Initialize command line parsing. aArgVec is the argument array terminated by * a NULL pointer (as usual), ArgCount is the number of valid arguments in the * array. Both arguments are remembered in static storage. diff --git a/src/ca65/fname.c b/src/common/fname.c similarity index 96% rename from src/ca65/fname.c rename to src/common/fname.c index e764d5202..fd907e82d 100644 --- a/src/ca65/fname.c +++ b/src/common/fname.c @@ -35,7 +35,7 @@ #include -#include "mem.h" +#include "xmalloc.h" #include "fname.h" @@ -58,11 +58,11 @@ char* MakeFilename (const char* Origin, const char* Ext) const char* P = strrchr (Origin, '.'); if (P == 0) { /* No dot, add the extension */ - Result = Xmalloc (strlen (Origin) + strlen (Ext) + 1); + Result = xmalloc (strlen (Origin) + strlen (Ext) + 1); strcpy (Result, Origin); strcat (Result, Ext); } else { - Result = Xmalloc (P - Origin + strlen (Ext) + 1); + Result = xmalloc (P - Origin + strlen (Ext) + 1); memcpy (Result, Origin, P - Origin); strcpy (Result + (P - Origin), Ext); } @@ -71,4 +71,3 @@ char* MakeFilename (const char* Origin, const char* Ext) - diff --git a/src/common/fname.h b/src/common/fname.h new file mode 100644 index 000000000..671bf8043 --- /dev/null +++ b/src/common/fname.h @@ -0,0 +1,61 @@ +/*****************************************************************************/ +/* */ +/* fname.h */ +/* */ +/* File name handling utilities */ +/* */ +/* */ +/* */ +/* (C) 2000 Ullrich von Bassewitz */ +/* Wacholderweg 14 */ +/* D-70597 Stuttgart */ +/* EMail: uz@musoftware.de */ +/* */ +/* */ +/* This software is provided 'as-is', without any expressed or implied */ +/* warranty. In no event will the authors be held liable for any damages */ +/* arising from the use of this software. */ +/* */ +/* Permission is granted to anyone to use this software for any purpose, */ +/* including commercial applications, and to alter it and redistribute it */ +/* freely, subject to the following restrictions: */ +/* */ +/* 1. The origin of this software must not be misrepresented; you must not */ +/* claim that you wrote the original software. If you use this software */ +/* in a product, an acknowledgment in the product documentation would be */ +/* appreciated but is not required. */ +/* 2. Altered source versions must be plainly marked as such, and must not */ +/* be misrepresented as being the original software. */ +/* 3. This notice may not be removed or altered from any source */ +/* distribution. */ +/* */ +/*****************************************************************************/ + + + +#ifndef FNAME_H +#define FNAME_H + + + +/*****************************************************************************/ +/* Code */ +/*****************************************************************************/ + + + +char* MakeFilename (const char* Origin, const char* Ext); +/* Make a new file name from Origin and Ext. If Origin has an extension, it + * is removed and Ext is appended. If Origin has no extension, Ext is simply + * appended. The result is placed in a malloc'ed buffer and returned. + * The function may be used to create "foo.o" from "foo.s". + */ + + + +/* End of fname.h */ + +#endif + + + diff --git a/src/common/make/gcc.mak b/src/common/make/gcc.mak index f50e1f18d..9b49c46bf 100644 --- a/src/common/make/gcc.mak +++ b/src/common/make/gcc.mak @@ -9,9 +9,12 @@ LIB = common.a -OBJS = bitops.o \ +OBJS = abend.o \ + bitops.o \ cmdline.o \ + fname.o \ hashstr.o \ + xmalloc.o \ xsprintf.o diff --git a/src/common/make/watcom.mak b/src/common/make/watcom.mak index 949680695..909f44a80 100644 --- a/src/common/make/watcom.mak +++ b/src/common/make/watcom.mak @@ -65,10 +65,13 @@ CCCFG = -bt=$(TARGET) -d1 -onatx -zp4 -5 -zq -w2 # ------------------------------------------------------------------------------ # All library OBJ files -OBJS = bitops.obj \ +OBJS = abend.obj \ + bitops.obj \ cmdline.obj \ + fname.obj \ hashstr.obj \ wildargv.obj \ + xmalloc.obj \ xsprintf.obj @@ -92,3 +95,5 @@ clean: + + diff --git a/src/ca65/mem.c b/src/common/xmalloc.c similarity index 61% rename from src/ca65/mem.c rename to src/common/xmalloc.c index 7f10aa859..bce75988f 100644 --- a/src/ca65/mem.c +++ b/src/common/xmalloc.c @@ -1,25 +1,25 @@ /*****************************************************************************/ /* */ -/* mem.c */ -/* */ -/* Memory allocation for the ca65 macroassembler */ -/* */ -/* */ -/* */ -/* (C) 1998 Ullrich von Bassewitz */ -/* Wacholderweg 14 */ -/* D-70597 Stuttgart */ -/* EMail: uz@musoftware.de */ -/* */ -/* */ +/* xmalloc.c */ +/* */ +/* Memory allocation subroutines */ +/* */ +/* */ +/* */ +/* (C) 2000 Ullrich von Bassewitz */ +/* Wacholderweg 14 */ +/* D-70597 Stuttgart */ +/* EMail: uz@musoftware.de */ +/* */ +/* */ /* This software is provided 'as-is', without any expressed or implied */ /* warranty. In no event will the authors be held liable for any damages */ /* arising from the use of this software. */ -/* */ +/* */ /* Permission is granted to anyone to use this software for any purpose, */ /* including commercial applications, and to alter it and redistribute it */ /* freely, subject to the following restrictions: */ -/* */ +/* */ /* 1. The origin of this software must not be misrepresented; you must not */ /* claim that you wrote the original software. If you use this software */ /* in a product, an acknowledgment in the product documentation would be */ @@ -28,7 +28,7 @@ /* be misrepresented as being the original software. */ /* 3. This notice may not be removed or altered from any source */ /* distribution. */ -/* */ +/* */ /*****************************************************************************/ @@ -36,49 +36,52 @@ #include #include -#include "error.h" -#include "mem.h" +#include "abend.h" +#include "xmalloc.h" /*****************************************************************************/ -/* code */ +/* code */ /*****************************************************************************/ -void* Xmalloc (size_t size) +void* xmalloc (size_t Size) /* Allocate memory, check for out of memory condition. Do some debugging */ { - void* p; + /* Allocate memory */ + void* P = malloc (Size); - p = malloc (size); - if (p == 0 && size != 0) { - Fatal (FAT_OUT_OF_MEMORY); + /* Check for errors */ + if (P == 0 && Size != 0) { + AbEnd ("Out of memory - requested block size = %lu", (unsigned long) Size); } /* Return a pointer to the block */ - return p; + return P; } -void Xfree (const void* block) +void xfree (const void* Block) /* Free the block, do some debugging */ { - free ((void*) block); + free ((void*) Block); } -char* StrDup (const char* s) +char* xstrdup (const char* S) /* Duplicate a string on the heap. The function checks for out of memory */ { - unsigned len; + /* Get the length of the string */ + unsigned Len = strlen (S) + 1; - len = strlen (s) + 1; - return memcpy (Xmalloc (len), s, len); + /* Allocate memory and return a copy */ + return memcpy (xmalloc (Len), S, Len); } + diff --git a/src/ca65/mem.h b/src/common/xmalloc.h similarity index 88% rename from src/ca65/mem.h rename to src/common/xmalloc.h index 4a6871c37..d90a5c9d3 100644 --- a/src/ca65/mem.h +++ b/src/common/xmalloc.h @@ -1,12 +1,12 @@ /*****************************************************************************/ /* */ -/* mem.h */ +/* xmalloc.h */ /* */ -/* Memory allocation for the ca65 macroassembler */ +/* Memory allocation subroutines */ /* */ /* */ /* */ -/* (C) 1998 Ullrich von Bassewitz */ +/* (C) 2000 Ullrich von Bassewitz */ /* Wacholderweg 14 */ /* D-70597 Stuttgart */ /* EMail: uz@musoftware.de */ @@ -33,8 +33,8 @@ -#ifndef MEM_H -#define MEM_H +#ifndef XMALLOC_H +#define XMALLOC_H @@ -43,23 +43,23 @@ /*****************************************************************************/ -/* Code */ +/* Code */ /*****************************************************************************/ -void* Xmalloc (size_t size); +void* xmalloc (size_t Size); /* Allocate memory, check for out of memory condition. Do some debugging */ -void Xfree (const void* block); +void xfree (const void* Block); /* Free the block, do some debugging */ -char* StrDup (const char* s); +char* xstrdup (const char* S); /* Duplicate a string on the heap. The function checks for out of memory */ -/* End of mem.h */ +/* End of xmalloc.h */ #endif -- 2.39.5