1
0
mirror of https://github.com/cc65/cc65.git synced 2024-06-17 00:29:31 +00:00

More common subroutines

git-svn-id: svn://svn.cc65.org/cc65/trunk@69 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
cuz 2000-06-14 09:32:22 +00:00
parent 114bc5a370
commit 1081c1dcdd
27 changed files with 363 additions and 151 deletions

View File

@ -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 */

View File

@ -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 */

View File

@ -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 */

View File

@ -44,7 +44,8 @@
# include <unistd.h>
#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 == ';') {

View File

@ -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);
}

View File

@ -37,13 +37,13 @@
#include <string.h>
#include <errno.h>
#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;

View File

@ -37,11 +37,11 @@
#include <string.h>
#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);
}

View File

@ -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);
}

View File

@ -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 \

View File

@ -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

View File

@ -38,12 +38,12 @@
#include <ctype.h>
#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;

View File

@ -38,12 +38,11 @@
#include <string.h>
#include <errno.h>
#include "../common/fname.h"
#include "../common/objdefs.h"
#include "global.h"
#include "error.h"
#include "fname.h"
#include "mem.h"
#include "objfile.h"

View File

@ -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);
}

View File

@ -40,16 +40,17 @@
#include <errno.h>
#include <sys/stat.h>
#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);
}

View File

@ -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;

View File

@ -35,7 +35,8 @@
#include <string.h>
#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)

View File

@ -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;

74
src/common/abend.c Normal file
View File

@ -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 <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#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);
}

59
src/common/abend.h Normal file
View File

@ -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

View File

@ -33,9 +33,9 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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;

View File

@ -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.

View File

@ -35,7 +35,7 @@
#include <string.h>
#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)

61
src/common/fname.h Normal file
View File

@ -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

View File

@ -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

View File

@ -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:

View File

@ -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 <stdlib.h>
#include <string.h>
#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);
}

View File

@ -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