mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-20 10:12:06 +00:00
Report "<stdin>" or "<stdout>" when using "-" as a filename placeholder (#1297)
Also fix a memory leak with `targetFileNames`
This commit is contained in:
@@ -10,7 +10,7 @@
|
|||||||
struct Expression;
|
struct Expression;
|
||||||
struct FileStackNode;
|
struct FileStackNode;
|
||||||
|
|
||||||
extern char *objectName;
|
extern const char *objectName;
|
||||||
extern struct Section *sectionList;
|
extern struct Section *sectionList;
|
||||||
|
|
||||||
void out_RegisterNode(struct FileStackNode *node);
|
void out_RegisterNode(struct FileStackNode *node);
|
||||||
|
|||||||
@@ -64,18 +64,4 @@ void error(struct FileStackNode const *where, uint32_t lineNo,
|
|||||||
[[noreturn]] void fatal(struct FileStackNode const *where, uint32_t lineNo,
|
[[noreturn]] void fatal(struct FileStackNode const *where, uint32_t lineNo,
|
||||||
char const *fmt, ...) format_(printf, 3, 4);
|
char const *fmt, ...) format_(printf, 3, 4);
|
||||||
|
|
||||||
/*
|
|
||||||
* Opens a file if specified, and aborts on error.
|
|
||||||
* @param fileName The name of the file to open; if NULL, no file will be opened
|
|
||||||
* @param mode The mode to open the file with
|
|
||||||
* @return A pointer to a valid FILE structure, or NULL if fileName was NULL
|
|
||||||
*/
|
|
||||||
FILE *openFile(char const *fileName, char const *mode);
|
|
||||||
|
|
||||||
#define closeFile(file) do { \
|
|
||||||
FILE *tmp = file; \
|
|
||||||
if (tmp) \
|
|
||||||
fclose(tmp); \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#endif // RGBDS_LINK_MAIN_H
|
#endif // RGBDS_LINK_MAIN_H
|
||||||
|
|||||||
@@ -259,7 +259,7 @@ int main(int argc, char *argv[])
|
|||||||
dependFileName = musl_optarg;
|
dependFileName = musl_optarg;
|
||||||
}
|
}
|
||||||
if (dependfile == NULL)
|
if (dependfile == NULL)
|
||||||
err("Failed to open dependfile %s", dependFileName);
|
err("Failed to open dependfile \"%s\"", dependFileName);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'o':
|
case 'o':
|
||||||
@@ -362,8 +362,9 @@ int main(int argc, char *argv[])
|
|||||||
memcpy(&targetFileName[targetFileNameLen], newTarget, newTargetLen);
|
memcpy(&targetFileName[targetFileNameLen], newTarget, newTargetLen);
|
||||||
if (depType == 'Q')
|
if (depType == 'Q')
|
||||||
free(newTarget);
|
free(newTarget);
|
||||||
|
if (targetFileNameLen > 0)
|
||||||
|
targetFileName[targetFileNameLen - 1] = ' ';
|
||||||
targetFileNameLen += newTargetLen;
|
targetFileNameLen += newTargetLen;
|
||||||
targetFileName[targetFileNameLen - 1] = ' ';
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -376,10 +377,8 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (targetFileName == NULL)
|
if (!targetFileName && objectName)
|
||||||
targetFileName = objectName;
|
targetFileName = strdup(objectName);
|
||||||
else
|
|
||||||
targetFileName[targetFileNameLen - 1] = '\0'; // Overwrite the last space
|
|
||||||
|
|
||||||
if (argc == musl_optind) {
|
if (argc == musl_optind) {
|
||||||
fputs("FATAL: Please specify an input file (pass `-` to read from standard input)\n", stderr);
|
fputs("FATAL: Please specify an input file (pass `-` to read from standard input)\n", stderr);
|
||||||
@@ -415,6 +414,7 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
if (dependfile)
|
if (dependfile)
|
||||||
fclose(dependfile);
|
fclose(dependfile);
|
||||||
|
free(targetFileName);
|
||||||
|
|
||||||
sect_CheckUnionClosed();
|
sect_CheckUnionClosed();
|
||||||
|
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ struct Assertion {
|
|||||||
struct Assertion *next;
|
struct Assertion *next;
|
||||||
};
|
};
|
||||||
|
|
||||||
char *objectName;
|
const char *objectName;
|
||||||
|
|
||||||
struct Section *sectionList;
|
struct Section *sectionList;
|
||||||
|
|
||||||
@@ -480,13 +480,15 @@ static void registerUnregisteredSymbol(struct Symbol *symbol, void *)
|
|||||||
void out_WriteObject(void)
|
void out_WriteObject(void)
|
||||||
{
|
{
|
||||||
FILE *f;
|
FILE *f;
|
||||||
if (strcmp(objectName, "-") != 0)
|
|
||||||
f = fopen(objectName, "wb");
|
|
||||||
else
|
|
||||||
f = fdopen(STDOUT_FILENO, "wb");
|
|
||||||
|
|
||||||
|
if (strcmp(objectName, "-")) {
|
||||||
|
f = fopen(objectName, "wb");
|
||||||
|
} else {
|
||||||
|
objectName = "<stdout>";
|
||||||
|
f = fdopen(STDOUT_FILENO, "wb");
|
||||||
|
}
|
||||||
if (!f)
|
if (!f)
|
||||||
err("Couldn't write file '%s'", objectName);
|
err("Failed to open object file '%s'", objectName);
|
||||||
|
|
||||||
// Also write symbols that weren't written above
|
// Also write symbols that weren't written above
|
||||||
sym_ForEach(registerUnregisteredSymbol, NULL);
|
sym_ForEach(registerUnregisteredSymbol, NULL);
|
||||||
|
|||||||
@@ -142,25 +142,6 @@ void argErr(char flag, char const *fmt, ...)
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
FILE *openFile(char const *fileName, char const *mode)
|
|
||||||
{
|
|
||||||
if (!fileName)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
FILE *file;
|
|
||||||
if (strcmp(fileName, "-") != 0)
|
|
||||||
file = fopen(fileName, mode);
|
|
||||||
else if (mode[0] == 'r')
|
|
||||||
file = fdopen(STDIN_FILENO, mode);
|
|
||||||
else
|
|
||||||
file = fdopen(STDOUT_FILENO, mode);
|
|
||||||
|
|
||||||
if (!file)
|
|
||||||
err("Failed to open file \"%s\"", fileName);
|
|
||||||
|
|
||||||
return file;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Short options
|
// Short options
|
||||||
static const char *optstring = "dl:m:Mn:O:o:p:S:s:tVvWwx";
|
static const char *optstring = "dl:m:Mn:O:o:p:S:s:tVvWwx";
|
||||||
|
|
||||||
|
|||||||
@@ -446,13 +446,14 @@ void obj_ReadFile(char const *fileName, unsigned int fileID)
|
|||||||
{
|
{
|
||||||
FILE *file;
|
FILE *file;
|
||||||
|
|
||||||
if (strcmp("-", fileName) != 0)
|
if (strcmp(fileName, "-")) {
|
||||||
file = fopen(fileName, "rb");
|
file = fopen(fileName, "rb");
|
||||||
else
|
} else {
|
||||||
|
fileName = "<stdin>";
|
||||||
file = fdopen(STDIN_FILENO, "rb"); // `stdin` is in text mode by default
|
file = fdopen(STDIN_FILENO, "rb"); // `stdin` is in text mode by default
|
||||||
|
}
|
||||||
if (!file)
|
if (!file)
|
||||||
err("Failed to open file %s", fileName);
|
err("Failed to open file \"%s\"", fileName);
|
||||||
|
|
||||||
// First, check if the object is a RGBDS object or a SDCC one. If the first byte is 'R',
|
// First, check if the object is a RGBDS object or a SDCC one. If the first byte is 'R',
|
||||||
// we'll assume it's a RGBDS object file, and otherwise, that it's a SDCC object file.
|
// we'll assume it's a RGBDS object file, and otherwise, that it's a SDCC object file.
|
||||||
@@ -495,13 +496,11 @@ void obj_ReadFile(char const *fileName, unsigned int fileID)
|
|||||||
&& matchedElems != strlen(RGBDS_OBJECT_VERSION_STRING))
|
&& matchedElems != strlen(RGBDS_OBJECT_VERSION_STRING))
|
||||||
errx("%s: Not a RGBDS object file", fileName);
|
errx("%s: Not a RGBDS object file", fileName);
|
||||||
|
|
||||||
verbosePrint("Reading object file %s\n",
|
verbosePrint("Reading object file %s\n", fileName);
|
||||||
fileName);
|
|
||||||
|
|
||||||
uint32_t revNum;
|
uint32_t revNum;
|
||||||
|
|
||||||
tryReadlong(revNum, file, "%s: Cannot read revision number: %s",
|
tryReadlong(revNum, file, "%s: Cannot read revision number: %s", fileName);
|
||||||
fileName);
|
|
||||||
if (revNum != RGBDS_OBJECT_REV)
|
if (revNum != RGBDS_OBJECT_REV)
|
||||||
errx("%s: Unsupported object file for rgblink %s; try rebuilding \"%s\"%s"
|
errx("%s: Unsupported object file for rgblink %s; try rebuilding \"%s\"%s"
|
||||||
" (expected revision %d, got %d)", fileName, get_package_version_string(),
|
" (expected revision %d, got %d)", fileName, get_package_version_string(),
|
||||||
@@ -511,10 +510,8 @@ void obj_ReadFile(char const *fileName, unsigned int fileID)
|
|||||||
uint32_t nbSymbols;
|
uint32_t nbSymbols;
|
||||||
uint32_t nbSections;
|
uint32_t nbSections;
|
||||||
|
|
||||||
tryReadlong(nbSymbols, file, "%s: Cannot read number of symbols: %s",
|
tryReadlong(nbSymbols, file, "%s: Cannot read number of symbols: %s", fileName);
|
||||||
fileName);
|
tryReadlong(nbSections, file, "%s: Cannot read number of sections: %s", fileName);
|
||||||
tryReadlong(nbSections, file, "%s: Cannot read number of sections: %s",
|
|
||||||
fileName);
|
|
||||||
|
|
||||||
nbSectionsToAssign += nbSections;
|
nbSectionsToAssign += nbSections;
|
||||||
|
|
||||||
@@ -622,8 +619,7 @@ void obj_ReadFile(char const *fileName, unsigned int fileID)
|
|||||||
|
|
||||||
uint32_t nbAsserts;
|
uint32_t nbAsserts;
|
||||||
|
|
||||||
tryReadlong(nbAsserts, file, "%s: Cannot read number of assertions: %s",
|
tryReadlong(nbAsserts, file, "%s: Cannot read number of assertions: %s", fileName);
|
||||||
fileName);
|
|
||||||
verbosePrint("Reading %" PRIu32 " assertions...\n", nbAsserts);
|
verbosePrint("Reading %" PRIu32 " assertions...\n", nbAsserts);
|
||||||
for (uint32_t i = 0; i < nbAsserts; i++) {
|
for (uint32_t i = 0; i < nbAsserts; i++) {
|
||||||
struct Assertion *assertion = (struct Assertion *)malloc(sizeof(*assertion));
|
struct Assertion *assertion = (struct Assertion *)malloc(sizeof(*assertion));
|
||||||
|
|||||||
@@ -231,8 +231,27 @@ static void writeBank(struct SortedSection *bankSections, uint16_t baseOffset,
|
|||||||
// Writes a ROM file to the output.
|
// Writes a ROM file to the output.
|
||||||
static void writeROM(void)
|
static void writeROM(void)
|
||||||
{
|
{
|
||||||
outputFile = openFile(outputFileName, "wb");
|
if (outputFileName) {
|
||||||
overlayFile = openFile(overlayFileName, "rb");
|
if (strcmp(outputFileName, "-")) {
|
||||||
|
outputFile = fopen(outputFileName, "wb");
|
||||||
|
} else {
|
||||||
|
outputFileName = "<stdout>";
|
||||||
|
outputFile = fdopen(STDOUT_FILENO, "wb");
|
||||||
|
}
|
||||||
|
if (!outputFile)
|
||||||
|
err("Failed to open output file \"%s\"", outputFileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (overlayFile) {
|
||||||
|
if (strcmp(overlayFileName, "-")) {
|
||||||
|
overlayFile = fopen(overlayFileName, "rb");
|
||||||
|
} else {
|
||||||
|
overlayFileName = "<stdin>";
|
||||||
|
overlayFile = fdopen(STDIN_FILENO, "rb");
|
||||||
|
}
|
||||||
|
if (!overlayFile)
|
||||||
|
err("Failed to open overlay file \"%s\"", overlayFileName);
|
||||||
|
}
|
||||||
|
|
||||||
uint32_t nbOverlayBanks = checkOverlaySize();
|
uint32_t nbOverlayBanks = checkOverlaySize();
|
||||||
|
|
||||||
@@ -249,8 +268,10 @@ static void writeROM(void)
|
|||||||
sectionTypeInfo[SECTTYPE_ROMX].startAddr, sectionTypeInfo[SECTTYPE_ROMX].size);
|
sectionTypeInfo[SECTTYPE_ROMX].startAddr, sectionTypeInfo[SECTTYPE_ROMX].size);
|
||||||
}
|
}
|
||||||
|
|
||||||
closeFile(outputFile);
|
if (outputFile)
|
||||||
closeFile(overlayFile);
|
fclose(outputFile);
|
||||||
|
if (overlayFile)
|
||||||
|
fclose(overlayFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -555,7 +576,12 @@ static void writeSym(void)
|
|||||||
if (!symFileName)
|
if (!symFileName)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
symFile = openFile(symFileName, "w");
|
if (strcmp(symFileName, "-")) {
|
||||||
|
symFile = fopen(symFileName, "w");
|
||||||
|
} else {
|
||||||
|
symFileName = "<stdout>";
|
||||||
|
symFile = fdopen(STDOUT_FILENO, "w");
|
||||||
|
}
|
||||||
if (!symFile)
|
if (!symFile)
|
||||||
err("Failed to open sym file \"%s\"", symFileName);
|
err("Failed to open sym file \"%s\"", symFileName);
|
||||||
|
|
||||||
@@ -568,7 +594,7 @@ static void writeSym(void)
|
|||||||
writeSymBank(§ions[type].banks[bank], type, bank);
|
writeSymBank(§ions[type].banks[bank], type, bank);
|
||||||
}
|
}
|
||||||
|
|
||||||
closeFile(symFile);
|
fclose(symFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Writes the map file, if applicable.
|
// Writes the map file, if applicable.
|
||||||
@@ -577,7 +603,12 @@ static void writeMap(void)
|
|||||||
if (!mapFileName)
|
if (!mapFileName)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
mapFile = openFile(mapFileName, "w");
|
if (strcmp(mapFileName, "-")) {
|
||||||
|
mapFile = fopen(mapFileName, "w");
|
||||||
|
} else {
|
||||||
|
mapFileName = "<stdout>";
|
||||||
|
mapFile = fdopen(STDOUT_FILENO, "w");
|
||||||
|
}
|
||||||
if (!mapFile)
|
if (!mapFile)
|
||||||
err("Failed to open map file \"%s\"", mapFileName);
|
err("Failed to open map file \"%s\"", mapFileName);
|
||||||
|
|
||||||
@@ -590,7 +621,7 @@ static void writeMap(void)
|
|||||||
writeMapBank(§ions[type].banks[bank], type, bank);
|
writeMapBank(§ions[type].banks[bank], type, bank);
|
||||||
}
|
}
|
||||||
|
|
||||||
closeFile(mapFile);
|
fclose(mapFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void cleanupSections(struct SortedSection *section)
|
static void cleanupSections(struct SortedSection *section)
|
||||||
|
|||||||
Reference in New Issue
Block a user