More common subroutines
git-svn-id: svn://svn.cc65.org/cc65/trunk@69 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
74
src/common/abend.c
Normal file
74
src/common/abend.c
Normal 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
59
src/common/abend.h
Normal 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
|
||||
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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.
|
||||
|
||||
73
src/common/fname.c
Normal file
73
src/common/fname.c
Normal file
@@ -0,0 +1,73 @@
|
||||
/*****************************************************************************/
|
||||
/* */
|
||||
/* 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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
61
src/common/fname.h
Normal file
61
src/common/fname.h
Normal 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
|
||||
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
|
||||
@@ -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:
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
87
src/common/xmalloc.c
Normal file
87
src/common/xmalloc.c
Normal file
@@ -0,0 +1,87 @@
|
||||
/*****************************************************************************/
|
||||
/* */
|
||||
/* 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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
67
src/common/xmalloc.h
Normal file
67
src/common/xmalloc.h
Normal file
@@ -0,0 +1,67 @@
|
||||
/*****************************************************************************/
|
||||
/* */
|
||||
/* 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
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user