shithub: rgbds

Download patch

ref: 0b08fca9f229b40275542544eda14195e85ea8ce
parent: bb85782faa5411052dfd0ee29f5e8b1c7711e78e
author: bentley <[email protected]>
date: Thu Jan 14 18:17:22 EST 2010

xlink: replace awkward fatalerror() with standard errx(3)

fatalerror() prints a given string to stdout and exits(5). It
cannot format text, so there was a string temptext used with
sprintf() to format text and pass it to fatalerror().

errx() can format text, and it prints to stderr.

As a bonus, this fixes the linking warnings from use of sprintf().

--- a/include/link/main.h
+++ b/include/link/main.h
@@ -4,8 +4,6 @@
 #include "link/types.h"
 
 extern void PrintUsage(void);
-extern void fatalerror(char *s);
-extern char temptext[1024];
 extern SLONG fillchar;
 extern char smartlinkstartsymbol[256];
 
--- a/src/link/assign.c
+++ b/src/link/assign.c
@@ -1,3 +1,4 @@
+#include <err.h>
 #include <stdio.h>
 #include <stdlib.h>
 
@@ -71,7 +72,7 @@
 
 						return (org);
 					} else
-						fatalerror("Out of memory!");
+						errx(5, "Out of memory!");
 				}
 			}
 		}
@@ -164,7 +165,7 @@
 			pSection->oAssigned = 1;
 			DOMAXBANK(pSection->nBank);
 		} else
-			fatalerror("Unable to place CODE section anywhere");
+			errx(5, "Unable to place CODE section anywhere");
 	}
 }
 
@@ -185,7 +186,7 @@
 		BankFree[i] = malloc(sizeof *BankFree[i]);
 
 		if (!BankFree[i])
-			fatalerror("Out of memory!");
+			errx(5, "Out of memory!");
 
 		if (i == 0) {
 			BankFree[i]->nOrg = 0x0000;
@@ -243,10 +244,7 @@
 				if (area_AllocAbs
 				    (&BankFree[BANK_BSS], pSection->nOrg,
 					pSection->nByteSize) != pSection->nOrg) {
-					sprintf(temptext,
-					    "Unable to load fixed BSS section at $%lX",
-					    pSection->nOrg);
-					fatalerror(temptext);
+					errx(5, "Unable to load fixed BSS section at $%lX", pSection->nOrg);
 				}
 				pSection->oAssigned = 1;
 				pSection->nBank = BANK_BSS;
@@ -255,10 +253,7 @@
 				if (area_AllocAbs
 				    (&BankFree[BANK_HRAM], pSection->nOrg,
 					pSection->nByteSize) != pSection->nOrg) {
-					sprintf(temptext,
-					    "Unable to load fixed HRAM section at $%lX",
-					    pSection->nOrg);
-					fatalerror(temptext);
+					errx(5, "Unable to load fixed HRAM section at $%lX", pSection->nOrg);
 				}
 				pSection->oAssigned = 1;
 				pSection->nBank = BANK_HRAM;
@@ -267,10 +262,7 @@
 				if (area_AllocAbs
 				    (&BankFree[BANK_VRAM], pSection->nOrg,
 					pSection->nByteSize) != pSection->nOrg) {
-					sprintf(temptext,
-					    "Unable to load fixed VRAM section at $%lX",
-					    pSection->nOrg);
-					fatalerror(temptext);
+					errx(5, "Unable to load fixed VRAM section at $%lX", pSection->nOrg);
 				}
 				pSection->oAssigned = 1;
 				pSection->nBank = BANK_VRAM;
@@ -279,10 +271,7 @@
 				if (area_AllocAbs
 				    (&BankFree[BANK_HOME], pSection->nOrg,
 					pSection->nByteSize) != pSection->nOrg) {
-					sprintf(temptext,
-					    "Unable to load fixed HOME section at $%lX",
-					    pSection->nOrg);
-					fatalerror(temptext);
+					errx(5, "Unable to load fixed HOME section at $%lX", pSection->nOrg);
 				}
 				pSection->oAssigned = 1;
 				pSection->nBank = BANK_HOME;
@@ -327,26 +316,13 @@
 								pSection->
 								nByteSize) !=
 							    pSection->nOrg) {
-								sprintf
-								    (temptext,
-								    "Unable to load fixed CODE/DATA section at $%lX in bank $%02lX",
-								    pSection->
-								    nOrg,
-								    pSection->
-								    nBank);
-								fatalerror
-								    (temptext);
+								errx(5, "Unable to load fixed CODE/DATA section at $%lX in bank $%02lX", pSection->nOrg, pSection->nBank);
 							}
 							DOMAXBANK(pSection->
 							    nBank);
 							pSection->oAssigned = 1;
 						} else {
-							sprintf(temptext,
-							    "Unable to load fixed CODE/DATA section at $%lX in bank $%02lX",
-							    pSection->nOrg,
-							    pSection->
-							    nBank);
-							fatalerror(temptext);
+							errx(5, "Unable to load fixed CODE/DATA section at $%lX in bank $%02lX", pSection->nOrg, pSection->nBank);
 						}
 					}
 
@@ -372,18 +348,12 @@
 				if ((pSection->nOrg =
 					area_Alloc(&BankFree[pSection->nBank],
 					    pSection->nByteSize)) == -1) {
-					sprintf(temptext,
-					    "Unable to load fixed CODE/DATA section into bank $%02lX",
-					    pSection->nBank);
-					fatalerror(temptext);
+					errx(5, "Unable to load fixed CODE/DATA section into bank $%02lX", pSection->nBank);
 				}
 				pSection->oAssigned = 1;
 				DOMAXBANK(pSection->nBank);
 			} else {
-				sprintf(temptext,
-				    "Unable to load fixed CODE/DATA section into bank $%02lX",
-				    pSection->nBank);
-				fatalerror(temptext);
+				errx(5, "Unable to load fixed CODE/DATA section into bank $%02lX", pSection->nBank);
 			}
 		}
 		pSection = pSection->pNext;
@@ -405,10 +375,7 @@
 				area_AllocAbsCODEAnyBank(pSection->nOrg,
 				    pSection->nByteSize)) ==
 			    -1) {
-				sprintf(temptext,
-				    "Unable to load fixed CODE/DATA section at $%lX into any bank",
-				    pSection->nOrg);
-				fatalerror(temptext);
+				errx(5, "Unable to load fixed CODE/DATA section at $%lX into any bank", pSection->nOrg);
 			}
 			pSection->oAssigned = 1;
 			DOMAXBANK(pSection->nBank);
@@ -430,7 +397,7 @@
 				if ((pSection->nOrg =
 					area_Alloc(&BankFree[BANK_BSS],
 					    pSection->nByteSize)) == -1) {
-					fatalerror("BSS section too large\n");
+					errx(5, "BSS section too large");
 				}
 				pSection->nBank = BANK_BSS;
 				pSection->oAssigned = 1;
@@ -439,7 +406,7 @@
 				if ((pSection->nOrg =
 					area_Alloc(&BankFree[BANK_HRAM],
 					    pSection->nByteSize)) == -1) {
-					fatalerror("HRAM section too large");
+					errx(5, "HRAM section too large");
 				}
 				pSection->nBank = BANK_HRAM;
 				pSection->oAssigned = 1;
@@ -448,7 +415,7 @@
 				if ((pSection->nOrg =
 					area_Alloc(&BankFree[BANK_VRAM],
 					    pSection->nByteSize)) == -1) {
-					fatalerror("VRAM section too large");
+					errx(5, "VRAM section too large");
 				}
 				pSection->nBank = BANK_VRAM;
 				pSection->oAssigned = 1;
@@ -457,7 +424,7 @@
 				if ((pSection->nOrg =
 					area_Alloc(&BankFree[BANK_HOME],
 					    pSection->nByteSize)) == -1) {
-					fatalerror("HOME section too large");
+					errx(5, "HOME section too large");
 				}
 				pSection->nBank = BANK_HOME;
 				pSection->oAssigned = 1;
@@ -465,7 +432,7 @@
 			case SECT_CODE:
 				break;
 			default:
-				fatalerror("(INTERNAL) Unknown section type!");
+				errx(5, "(INTERNAL) Unknown section type!");
 				break;
 			}
 		}
@@ -482,7 +449,7 @@
 
 	BankFree[0] = malloc(sizeof *BankFree[0]);
 	if (!BankFree[0])
-		fatalerror("Out of memory!");
+		errx(5, "Out of memory!");
 
 	BankFree[0]->nOrg = 0x0000;
 	BankFree[0]->nSize = 0x10000;
--- a/src/link/library.c
+++ b/src/link/library.c
@@ -1,3 +1,4 @@
+#include <err.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -91,9 +92,8 @@
 	}
 	if (options & OPT_SMART_C_LINK) {
 		if (!addmodulecontaining(smartlinkstartsymbol)) {
-			sprintf(temptext, "Can't find start symbol '%s'",
+			errx(5, "Can't find start symbol '%s'",
 			    smartlinkstartsymbol);
-			fatalerror(temptext);
 		} else
 			printf("Smart linking with symbol '%s'\n",
 			    smartlinkstartsymbol);
--- a/src/link/main.c
+++ b/src/link/main.c
@@ -1,3 +1,4 @@
+#include <err.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -32,17 +33,6 @@
 char smartlinkstartsymbol[256];
 
 /*
- * Print out an errormessage
- *
- */
-
-void 
-fatalerror(char *s)
-{
-	printf("*ERROR* : %s\n", s);
-	exit(5);
-}
-/*
  * Print the usagescreen
  *
  */
@@ -76,8 +66,7 @@
 
 	pLinkfile = fopen(tzLinkfile, "rt");
 	if (!pLinkfile) {
-		sprintf(temptext, "Unable to find linkfile '%s'\n", tzLinkfile);
-		fatalerror(temptext);
+		errx(5, "Unable to find linkfile '%s'", tzLinkfile);
 	}
 	while (!feof(pLinkfile)) {
 		char tzLine[256];
@@ -99,10 +88,9 @@
 					CurrentBlock = BLOCK_COMMENT;
 				else {
 					fclose(pLinkfile);
-					sprintf(temptext,
-					    "Unknown block '%s'\n",
+					errx(5, 
+					    "Unknown block '%s'",
 					    tzLine);
-					fatalerror(temptext);
 				}
 			} else {
 				switch (CurrentBlock) {
@@ -167,9 +155,8 @@
 				outputtype = OUTPUT_PSION2;
 				break;
 			default:
-				sprintf(temptext, "Unknown option 't%c'\n",
+				errx(5, "Unknown option 't%c'",
 				    opt);
-				fatalerror(temptext);
 				break;
 			}
 			break;
@@ -184,12 +171,12 @@
 					    sscanf(argv[argn - 1] + 2, "%lx",
 					    &fillchar);
 					if (!((result == EOF) || (result == 1))) {
-						fatalerror
-						    ("Invalid argument for option 'z'\n");
+						errx(5, 
+						    "Invalid argument for option 'z'");
 					}
 				}
 			} else {
-				fatalerror("Invalid argument for option 'z'\n");
+				errx(5, "Invalid argument for option 'z'");
 			}
 			break;
 		case 's':
@@ -197,8 +184,7 @@
 			strcpy(smartlinkstartsymbol, argv[argn - 1] + 2);
 			break;
 		default:
-			sprintf(temptext, "Unknown option '%c'\n", opt);
-			fatalerror(temptext);
+			errx(5, "Unknown option '%c'", opt);
 			break;
 		}
 	}
--- a/src/link/mapfile.c
+++ b/src/link/mapfile.c
@@ -19,7 +19,7 @@
 	mf = fopen(name, "wt");
 
 	if (!mf)
-		fatalerror("Unable to open mapfile for writing");
+		errx(5, "Unable to open mapfile for writing");
 }
 
 void 
@@ -28,7 +28,7 @@
 	sf = fopen(name, "wt");
 
 	if (!sf)
-		fatalerror("Unable to open symfile for writing");
+		errx(5, "Unable to open symfile for writing");
 
 	fprintf(sf, ";File generated by xLink v" LINK_VERSION "\n\n");
 }
--- a/src/link/object.c
+++ b/src/link/object.c
@@ -3,6 +3,7 @@
  *
  */
 
+#include <err.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -79,7 +80,7 @@
 
 	*ppSections = malloc(sizeof **ppSections);
 	if (!*ppSections) {
-		fatalerror("Out of memory!");
+		errx(5, "Out of memory!");
 		return NULL;
 	}
 	(*ppSections)->tSymbols = tSymbols;
@@ -101,12 +102,12 @@
 
 	pSym = malloc(sizeof *pSym);
 	if (!pSym)
-		fatalerror("Out of memory!");
+		errx(5, "Out of memory!");
 
 	readasciiz(s, f);
 	pSym->pzName = malloc(strlen(s) + 1);
 	if (!pSym->pzName)
-		fatalerror("Out of memory!");
+		errx(5, "Out of memory!");
 
 	strcpy(pSym->pzName, s);
 	if ((pSym->Type = (enum eSymbolType) fgetc(f)) != SYM_IMPORT) {
@@ -145,7 +146,7 @@
 		if (pSection->nByteSize) {
 			pSection->pData = malloc(pSection->nByteSize);
 			if (!pSection->pData)
-				fatalerror("Out of memory!");
+				errx(5, "Out of memory!");
 
 			SLONG nNumberOfPatches;
 			struct sPatch **ppPatch, *pPatch;
@@ -163,7 +164,7 @@
 			while (nNumberOfPatches--) {
 				pPatch = malloc(sizeof *pPatch);
 				if (!pPatch)
-					fatalerror("Out of memory!");
+					errx(5, "Out of memory!");
 
 				*ppPatch = pPatch;
 				readasciiz(s, f);
@@ -170,7 +171,7 @@
 
 				pPatch->pzFilename = malloc(strlen(s) + 1);
 				if (!pPatch->pzFilename)
-					fatalerror("Out of memory!");
+					errx(5, "Out of memory!");
 
 				strcpy(pPatch->pzFilename, s);
 
@@ -185,7 +186,7 @@
 				if ((pPatch->nRPNSize = readlong(f)) > 0) {
 					pPatch->pRPN = malloc(pPatch->nRPNSize);
 					if (!pPatch->pRPN)
-						fatalerror("Out of memory!");
+						errx(5, "Out of memory!");
 
 					fread(pPatch->pRPN, sizeof(UBYTE),
 					    pPatch->nRPNSize, f);
@@ -218,7 +219,7 @@
 	if (nNumberOfSymbols) {
 		tSymbols = malloc(nNumberOfSymbols * sizeof(struct sSymbol *));
 		if (!tSymbols)
-			fatalerror("Out of memory!");
+			errx(5, "Out of memory!");
 
 		for (i = 0; i < nNumberOfSymbols; i += 1)
 			tSymbols[i] = obj_ReadSymbol(pObjfile);
@@ -293,7 +294,7 @@
 		if (pSection->nByteSize) {
 			pSection->pData = malloc(pSection->nByteSize);
 			if (!pSection->pData)
-				fatalerror("Out of memory!");
+				errx(5, "Out of memory!");
 
 			SLONG nNumberOfPatches;
 			struct sPatch **ppPatch, *pPatch;
@@ -311,13 +312,13 @@
 			while (nNumberOfPatches--) {
 				pPatch = malloc(sizeof *pPatch);
 				if (!pPatch)
-					fatalerror("Out of memory!");
+					errx(5, "Out of memory!");
 
 				*ppPatch = pPatch;
 				readasciiz(s, f);
 				pPatch->pzFilename = malloc(strlen(s) + 1);
 				if (!pPatch->pzFilename)
-					fatalerror("Out of memory!");
+					errx(5, "Out of memory!");
 
 				strcpy(pPatch->pzFilename, s);
 				pPatch->nLineNo = readlong(f);
@@ -326,7 +327,7 @@
 				if ((pPatch->nRPNSize = readlong(f)) > 0) {
 					pPatch->pRPN = malloc(pPatch->nRPNSize);
 					if (!pPatch->pRPN)
-						fatalerror("Out of memory!");
+						errx(5, "Out of memory!");
 
 					fread(pPatch->pRPN, sizeof(UBYTE),
 					    pPatch->nRPNSize, f);
@@ -359,7 +360,7 @@
 	if (nNumberOfSymbols) {
 		tSymbols = malloc(nNumberOfSymbols * sizeof *tSymbols);
 		if (!tSymbols)
-			fatalerror("Out of memory!");
+			errx(5, "Out of memory!");
 
 		for (i = 0; i < nNumberOfSymbols; i += 1)
 			tSymbols[i] = obj_ReadSymbol(pObjfile);
@@ -422,14 +423,12 @@
 			    obj_ReadRGB1(pObjfile);
 			break;
 		default:
-			sprintf(temptext, "'%s' is an unsupported version\n",
+			errx(5, "'%s' is an unsupported version",
 			    tzObjectfile);
-			fatalerror(temptext);
 			break;
 		}
 	} else {
-		sprintf(temptext, "'%s' is not a valid object\n", tzObjectfile);
-		fatalerror(temptext);
+		errx(5, "'%s' is not a valid object", tzObjectfile);
 	}
 }
 
@@ -445,8 +444,7 @@
 
 	pObjfile = fopen(tzObjectfile, "rb");
 	if (!pObjfile) {
-		sprintf(temptext, "Unable to open '%s'\n", tzObjectfile);
-		fatalerror(temptext);
+		errx(5, "Unable to open '%s'", tzObjectfile);
 	}
 	obj_ReadOpenFile(pObjfile, tzObjectfile);
 	fclose(pObjfile);
@@ -496,8 +494,7 @@
 
 	pObjfile = fopen(tzLibfile, "rb");
 	if (!pObjfile) {
-		sprintf(temptext, "Unable to open '%s'\n", tzLibfile);
-		fatalerror(temptext);
+		errx(5, "Unable to open '%s'", tzLibfile);
 	}
 	char tzHeader[5];
 
@@ -506,9 +503,8 @@
 	if (strcmp(tzHeader, "XLB0") == 0)
 		lib_ReadXLB0(pObjfile);
 	else {
-		sprintf(temptext, "'%s' is an invalid library\n",
+		errx(5, "'%s' is an invalid library",
 		    tzLibfile);
-		fatalerror(temptext);
 	}
 	fclose(pObjfile);
 }
--- a/src/link/patch.c
+++ b/src/link/patch.c
@@ -1,3 +1,4 @@
+#include <err.h>
 #include <stdio.h>
 #include <string.h>
 
@@ -45,8 +46,7 @@
 	default:
 		break;
 	}
-	fatalerror("*INTERNAL* UNKNOWN SYMBOL TYPE");
-	return (0);
+	errx(5, "*INTERNAL* UNKNOWN SYMBOL TYPE");
 }
 
 SLONG 
@@ -63,8 +63,7 @@
 	default:
 		break;
 	}
-	fatalerror("*INTERNAL* UNKNOWN SYMBOL TYPE");
-	return (0);
+	errx(5, "*INTERNAL* UNKNOWN SYMBOL TYPE");
 }
 
 SLONG 
@@ -158,10 +157,9 @@
 			t = rpnpop();
 			rpnpush(t & 0xFF);
 			if (t < 0 || (t > 0xFF && t < 0xFF00) || t > 0xFFFF) {
-				sprintf(temptext,
+				errx(5,
 				    "%s(%ld) : Value must be in the HRAM area",
 				    pPatch->pzFilename, pPatch->nLineNo);
-				fatalerror(temptext);
 			}
 			break;
 		case RPN_PCEZP:
@@ -168,10 +166,9 @@
 			t = rpnpop();
 			rpnpush(t & 0xFF);
 			if (t < 0x2000 || t > 0x20FF) {
-				sprintf(temptext,
+				errx(5,
 				    "%s(%ld) : Value must be in the ZP area",
 				    pPatch->pzFilename, pPatch->nLineNo);
-				fatalerror(temptext);
 			}
 			break;
 		case RPN_CONST:
@@ -216,11 +213,10 @@
 				high |= (*rpn++) << 24;
 				t = rpnpop();
 				if (t < low || t > high) {
-					sprintf(temptext,
+					errx(5,
 					    "%s(%ld) : Value must be in the range [%ld;%ld]",
 					    pPatch->pzFilename,
 					    pPatch->nLineNo, low, high);
-					fatalerror(temptext);
 				}
 				rpnpush(t);
 				size -= 8;
@@ -254,11 +250,10 @@
 					pSect->pData[pPatch->nOffset] =
 					    (UBYTE) t;
 				} else {
-					sprintf(temptext,
+					errx(5,
 					    "%s(%ld) : Value must be 8-bit\n",
 					    pPatch->pzFilename,
 					    pPatch->nLineNo);
-					fatalerror(temptext);
 				}
 				break;
 			case PATCH_WORD_L:
@@ -279,11 +274,10 @@
 						    1] = t & 0xFF;
 					}
 				} else {
-					sprintf(temptext,
+					errx(5,
 					    "%s(%ld) : Value must be 16-bit\n",
 					    pPatch->pzFilename,
 					    pPatch->nLineNo);
-					fatalerror(temptext);
 				}
 				break;
 			case PATCH_LONG_L:
--- a/src/link/symbol.c
+++ b/src/link/symbol.c
@@ -1,3 +1,4 @@
+#include <err.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -53,9 +54,7 @@
 			}
 		}
 
-		sprintf(temptext, "Unknown symbol '%s'", tzName);
-		fatalerror(temptext);
-		return (0);
+		errx(5, "Unknown symbol '%s'", tzName);
 	}
 }
 
@@ -73,9 +72,7 @@
 		}
 	}
 
-	sprintf(temptext, "Unknown symbol '%s'", tzName);
-	fatalerror(temptext);
-	return (0);
+	errx(5, "Unknown symbol '%s'", tzName);
 }
 
 void 
@@ -95,10 +92,9 @@
 			if (nBank == -1)
 				return;
 
-			sprintf(temptext,
+			errx(5,
 			    "Symbol '%s' defined more than once\n",
 			    tzName);
-			fatalerror(temptext);
 		}
 	}