#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"
FreeExprNodes = N->Left;
} else {
/* Allocate fresh memory */
- N = Xmalloc (sizeof (ExprNode));
+ N = xmalloc (sizeof (ExprNode));
}
N->Op = EXPR_NULL;
N->Left = N->Right = 0;
FreeExprNodes = E;
} else {
/* Free the memory */
- Xfree (E);
+ xfree (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 */
+++ /dev/null
-/*****************************************************************************/
-/* */
-/* fname.c */
-/* */
-/* 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. */
-/* */
-/*****************************************************************************/
-
-
-
-#include <string.h>
-
-#include "mem.h"
-#include "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".
- */
-{
- /* Construct the name */
- char* Result;
- const char* P = strrchr (Origin, '.');
- if (P == 0) {
- /* No dot, add the extension */
- Result = Xmalloc (strlen (Origin) + strlen (Ext) + 1);
- strcpy (Result, Origin);
- strcat (Result, Ext);
- } else {
- Result = Xmalloc (P - Origin + strlen (Ext) + 1);
- memcpy (Result, Origin, P - Origin);
- strcpy (Result + (P - Origin), Ext);
- }
- return Result;
-}
-
-
-
-
-const char* ProgName = "ca65"; /* Program name */
-
/* File names */
const char* InFile = 0; /* Name of input file */
const char* OutFile = 0; /* Name of output file */
unsigned char LooseStringTerm = 0; /* Allow ' as string terminator */
unsigned char AtInIdents = 0; /* Allow '@' in identifiers */
unsigned char DollarInIdents = 0; /* Allow '$' in identifiers */
-
+
-extern const char* ProgName; /* Program name */
-
/* File names */
extern const char* InFile; /* Name of input file */
extern const char* OutFile; /* Name of output file */
# include <unistd.h>
#endif
-#include "mem.h"
+#include "../common/xmalloc.h"
+
#include "incpath.h"
}
/* Allocate memory for the new string */
- NewPath = Xmalloc (OrigLen + NewLen + 2);
+ NewPath = xmalloc (OrigLen + NewLen + 2);
/* Copy the strings */
memcpy (NewPath, Orig, OrigLen);
NewPath [OrigLen+NewLen+1] = '\0';
/* Delete the original path */
- Xfree (Orig);
+ xfree (Orig);
/* Return the new path */
return NewPath;
/* 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 == ';') {
+#include "../common/xmalloc.h"
+
#include "error.h"
-#include "mem.h"
#include "istack.h"
}
/* Create a new stack element */
- E = Xmalloc (sizeof (*E));
+ E = xmalloc (sizeof (*E));
/* Initialize it */
E->Func = Func;
IStack = IStack->Next;
/* And delete it */
- Xfree (E);
+ xfree (E);
}
#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"
}
/* Allocate memory */
- L = Xmalloc (sizeof (ListLine) + Len);
+ L = xmalloc (sizeof (ListLine) + Len);
/* Initialize the fields. */
L->Next = 0;
*/
{
/* 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"
}
/* 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;
}
/* Delete the temporary buffer */
- Xfree (Buf);
+ xfree (Buf);
/* Next line */
L = L->Next;
#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"
{
/* Allocate memory */
unsigned Len = strlen (Id);
- IdDesc* I = Xmalloc (sizeof (IdDesc) + Len);
+ IdDesc* I = xmalloc (sizeof (IdDesc) + Len);
/* Initialize the struct */
I->Next = 0;
{
/* 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;
unsigned I;
/* Allocate memory */
- MacExp* E = Xmalloc (sizeof (MacExp));
+ MacExp* E = xmalloc (sizeof (MacExp));
/* Initialize the data */
E->M = 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;
/* 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) {
}
/* Free the structure itself */
- Xfree (E);
+ xfree (E);
}
#include "instr.h"
#include "listing.h"
#include "macro.h"
-#include "mem.h"
+/*#include "mem.h"*/
#include "nexttok.h"
#include "objcode.h"
#include "objfile.h"
" --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"
" --verbose\t\tIncrease verbosity\n"
" --version\t\tPrint the assembler version\n",
ProgName);
- exit (EXIT_FAILURE);
}
/* The symbol must start with a character or underline */
if (Def [0] != '_' && !isalpha (Def [0])) {
- InvSym (Def);
+ InvDef (Def);
}
P = Def;
/* Do we have a value given? */
if (*P != '=') {
if (*P != '\0') {
- InvSym (Def);
+ InvDef (Def);
}
Val = 0;
} else {
if (*P == '$') {
++P;
if (sscanf (P, "%lx", &Val) != 1) {
- InvSym (Def);
+ InvDef (Def);
}
} else {
if (sscanf (P, "%li", &Val) != 1) {
- InvSym (Def);
+ InvDef (Def);
}
}
}
/* Print usage information and exit */
{
Usage ();
+ exit (EXIT_SUCCESS);
}
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.
} 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;
}
/* 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);
}
ea.o \
error.o \
expr.o \
- fname.o \
fragment.o \
global.o \
incpath.o \
macpack.o \
macro.o \
main.o \
- mem.o \
nexttok.o \
objcode.o \
objfile.o \
ea.obj \
error.obj \
expr.obj \
- fname.obj \
fragment.obj \
global.obj \
incpath.obj \
macpack.obj \
macro.obj \
main.obj \
- mem.obj \
nexttok.obj \
objcode.obj \
objfile.obj \
FILE ea.obj
FILE error.obj
FILE expr.obj
-FILE fname.obj
FILE fragment.obj
FILE global.obj
FILE incpath.obj
FILE macpack.obj
FILE macro.obj
FILE main.obj
-FILE mem.obj
FILE nexttok.obj
FILE objcode.obj
FILE objfile.obj
+++ /dev/null
-/*****************************************************************************/
-/* */
-/* mem.c */
-/* */
-/* Memory allocation for the ca65 macroassembler */
-/* */
-/* */
-/* */
-/* (C) 1998 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 <stdlib.h>
-#include <string.h>
-
-#include "error.h"
-#include "mem.h"
-
-
-
-/*****************************************************************************/
-/* code */
-/*****************************************************************************/
-
-
-
-void* Xmalloc (size_t size)
-/* Allocate memory, check for out of memory condition. Do some debugging */
-{
- void* p;
-
- p = malloc (size);
- if (p == 0 && size != 0) {
- Fatal (FAT_OUT_OF_MEMORY);
- }
-
- /* Return a pointer to the block */
- return p;
-}
-
-
-
-void Xfree (const void* block)
-/* Free the block, do some debugging */
-{
- free ((void*) block);
-}
-
-
-
-char* StrDup (const char* s)
-/* Duplicate a string on the heap. The function checks for out of memory */
-{
- unsigned len;
-
- len = strlen (s) + 1;
- return memcpy (Xmalloc (len), s, len);
-}
-
-
-
+++ /dev/null
-/*****************************************************************************/
-/* */
-/* mem.h */
-/* */
-/* Memory allocation for the ca65 macroassembler */
-/* */
-/* */
-/* */
-/* (C) 1998 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 MEM_H
-#define MEM_H
-
-
-
-#include <stddef.h>
-
-
-
-/*****************************************************************************/
-/* Code */
-/*****************************************************************************/
-
-
-
-void* Xmalloc (size_t size);
-/* Allocate memory, check for out of memory condition. Do some debugging */
-
-void Xfree (const void* block);
-/* Free the block, do some debugging */
-
-char* StrDup (const char* s);
-/* Duplicate a string on the heap. The function checks for out of memory */
-
-
-
-/* End of mem.h */
-
-#endif
-
-
-
#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"
} while (*N);
/* Create a new segment */
- S = Xmalloc (sizeof (*S));
+ S = xmalloc (sizeof (*S));
/* Initialize it */
S->List = 0;
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;
Fragment* F;
/* Create a new fragment */
- F = Xmalloc (sizeof (*F));
+ F = xmalloc (sizeof (*F));
/* Initialize it */
F->List = 0;
LineCur->FragList = F;
/* First fragment - set the PC
LineCur->PC = GetPC ();
- LineCur->Reloc = RelocMode;
+ LineCur->Reloc = RelocMode;
*/
} else {
LineCur->FragLast->LineList = F;
#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"
#include "../common/optdefs.h"
+#include "../common/xmalloc.h"
-#include "mem.h"
#include "error.h"
#include "objfile.h"
#include "options.h"
Option* Opt;
/* Allocate memory */
- Opt = Xmalloc (sizeof (*Opt));
+ Opt = xmalloc (sizeof (*Opt));
/* Initialize fields */
Opt->Next = 0;
Fatal (FAT_STRING_TOO_LONG);
}
O = NewOption (Type);
- O->V.Str = StrDup (Text);
+ O->V.Str = xstrdup (Text);
}
#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"
}
/* Free the allocated memory */
- Xfree (PathName);
+ xfree (PathName);
}
}
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;
/* Cleanup the current stuff */
fclose (I->F);
- Xfree (I);
+ xfree (I);
--ICount;
}
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;
/* Cleanup the current stuff */
if (I->Malloced) {
- Xfree (I->Data);
+ xfree (I->Data);
}
- Xfree (I);
+ xfree (I);
}
#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"
Len = strlen (Name);
/* Allocate memory */
- S = Xmalloc (sizeof (SymEntry) + Len);
+ S = xmalloc (sizeof (SymEntry) + Len);
/* Initialize the entry */
S->Left = 0;
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;
#include <string.h>
-#include "mem.h"
+#include "../common/xmalloc.h"
+
#include "scanner.h"
#include "toklist.h"
/* 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;
void FreeTokNode (TokNode* T)
/* Free the given token node */
{
- Xfree (T);
+ xfree (T);
}
/* 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);
}
/* Free the list structure itself */
- Xfree (List);
+ xfree (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;
-
-
-
#include "../common/filepos.h"
+#include "../common/xmalloc.h"
#include "error.h"
#include "expr.h"
-#include "mem.h"
#include "scanner.h"
#include "ulabel.h"
*/
{
/* Allocate memory for the ULabel structure */
- ULabel* L = Xmalloc (sizeof (ULabel));
+ ULabel* L = xmalloc (sizeof (ULabel));
/* Initialize the fields */
L->Pos = CurPos;
*/
if (ULabCount) {
unsigned I = 0;
- ULabList = Xmalloc (ULabCount * sizeof (ULabel*));
+ ULabList = xmalloc (ULabCount * sizeof (ULabel*));
L = ULabRoot;
while (L) {
ULabList[I] = L;
--- /dev/null
+/*****************************************************************************/
+/* */
+/* 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);
+}
+
+
+
--- /dev/null
+/*****************************************************************************/
+/* */
+/* 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
+
+
+
-#include <stdio.h>
-#include <stdlib.h>
+#include <string.h>
+#include "abend.h"
#include "cmdline.h"
+/* 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;
+ }
+ }
}
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);
}
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);
}
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);
}
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;
+/* Program name - is set after call to InitCmdLine */
+extern const char* ProgName;
+
/* Structure defining a long option */
typedef struct LongOpt LongOpt;
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.
--- /dev/null
+/*****************************************************************************/
+/* */
+/* fname.c */
+/* */
+/* 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. */
+/* */
+/*****************************************************************************/
+
+
+
+#include <string.h>
+
+#include "xmalloc.h"
+#include "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".
+ */
+{
+ /* Construct the name */
+ char* Result;
+ const char* P = strrchr (Origin, '.');
+ if (P == 0) {
+ /* No dot, add the extension */
+ Result = xmalloc (strlen (Origin) + strlen (Ext) + 1);
+ strcpy (Result, Origin);
+ strcat (Result, Ext);
+ } else {
+ Result = xmalloc (P - Origin + strlen (Ext) + 1);
+ memcpy (Result, Origin, P - Origin);
+ strcpy (Result + (P - Origin), Ext);
+ }
+ return Result;
+}
+
+
+
--- /dev/null
+/*****************************************************************************/
+/* */
+/* 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
+
+
+
-OBJS = bitops.o \
+OBJS = abend.o \
+ bitops.o \
cmdline.o \
+ fname.o \
hashstr.o \
+ xmalloc.o \
xsprintf.o
# ------------------------------------------------------------------------------
# All library OBJ files
-OBJS = bitops.obj \
+OBJS = abend.obj \
+ bitops.obj \
cmdline.obj \
+ fname.obj \
hashstr.obj \
wildargv.obj \
+ xmalloc.obj \
xsprintf.obj
+
+
--- /dev/null
+/*****************************************************************************/
+/* */
+/* 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 */
+/* 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 <stdlib.h>
+#include <string.h>
+
+#include "abend.h"
+#include "xmalloc.h"
+
+
+
+/*****************************************************************************/
+/* code */
+/*****************************************************************************/
+
+
+
+void* xmalloc (size_t Size)
+/* Allocate memory, check for out of memory condition. Do some debugging */
+{
+ /* Allocate memory */
+ void* P = malloc (Size);
+
+ /* 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;
+}
+
+
+
+void xfree (const void* Block)
+/* Free the block, do some debugging */
+{
+ free ((void*) Block);
+}
+
+
+
+char* xstrdup (const char* S)
+/* Duplicate a string on the heap. The function checks for out of memory */
+{
+ /* Get the length of the string */
+ unsigned Len = strlen (S) + 1;
+
+ /* Allocate memory and return a copy */
+ return memcpy (xmalloc (Len), S, Len);
+}
+
+
+
+
--- /dev/null
+/*****************************************************************************/
+/* */
+/* xmalloc.h */
+/* */
+/* 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 */
+/* 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 XMALLOC_H
+#define XMALLOC_H
+
+
+
+#include <stddef.h>
+
+
+
+/*****************************************************************************/
+/* Code */
+/*****************************************************************************/
+
+
+
+void* xmalloc (size_t Size);
+/* Allocate memory, check for out of memory condition. Do some debugging */
+
+void xfree (const void* Block);
+/* Free the block, do some debugging */
+
+char* xstrdup (const char* S);
+/* Duplicate a string on the heap. The function checks for out of memory */
+
+
+
+/* End of xmalloc.h */
+
+#endif
+
+
+