Use std::string for target file name

This commit is contained in:
Rangi42
2024-02-25 07:53:04 -05:00
committed by Sylvie
parent 0e19f6c0ae
commit 4b2294292a
3 changed files with 25 additions and 35 deletions

View File

@@ -5,6 +5,7 @@
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <string>
#include "helpers.hpp" #include "helpers.hpp"
@@ -16,7 +17,7 @@ extern bool verbose;
extern bool warnings; // True to enable warnings, false to disable them. extern bool warnings; // True to enable warnings, false to disable them.
extern FILE *dependfile; extern FILE *dependfile;
extern char *targetFileName; extern std::string targetFileName;
extern bool generatedMissingIncludes; extern bool generatedMissingIncludes;
extern bool failedOnMissingInclude; extern bool failedOnMissingInclude;
extern bool generatePhonyDeps; extern bool generatePhonyDeps;

View File

@@ -127,7 +127,7 @@ void fstk_SetPreIncludeFile(char const *path)
static void printDep(char const *path) static void printDep(char const *path)
{ {
if (dependfile) { if (dependfile) {
fprintf(dependfile, "%s: %s\n", targetFileName, path); fprintf(dependfile, "%s: %s\n", targetFileName.c_str(), path);
if (generatePhonyDeps) if (generatePhonyDeps)
fprintf(dependfile, "%s:\n", path); fprintf(dependfile, "%s:\n", path);
} }

View File

@@ -10,6 +10,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <string>
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
@@ -54,7 +55,7 @@ FILE *dependfile = NULL;
bool generatedMissingIncludes = false; bool generatedMissingIncludes = false;
bool failedOnMissingInclude = false; bool failedOnMissingInclude = false;
bool generatePhonyDeps = false; bool generatePhonyDeps = false;
char *targetFileName = NULL; std::string targetFileName;
bool haltNop; bool haltNop;
bool warnOnHaltNop; bool warnOnHaltNop;
@@ -64,23 +65,22 @@ bool verbose;
bool warnings; // True to enable warnings, false to disable them. bool warnings; // True to enable warnings, false to disable them.
// Escapes Make-special chars from a string // Escapes Make-special chars from a string
static char *make_escape(char const *str) static std::string make_escape(std::string &str)
{ {
char *escaped_str = (char *)malloc(strlen(str) * 2 + 1); std::string escaped;
char *dest = escaped_str; size_t pos = 0;
if (escaped_str == NULL) for (;;) {
err("%s: Failed to allocate memory", __func__);
while (*str) {
// All dollars needs to be doubled // All dollars needs to be doubled
if (*str == '$') size_t nextPos = str.find("$", pos);
*dest++ = '$'; if (nextPos == std::string::npos)
*dest++ = *str++; break;
escaped.append(str, pos, nextPos - pos);
escaped.append("$$");
pos = nextPos + sizeof("$") - 1;
} }
*dest = '\0'; escaped.append(str, pos, str.length() - pos);
return escaped;
return escaped_str;
} }
// Short options // Short options
@@ -172,7 +172,7 @@ int main(int argc, char *argv[])
sym_SetExportAll(false); sym_SetExportAll(false);
uint32_t maxDepth = DEFAULT_MAX_DEPTH; uint32_t maxDepth = DEFAULT_MAX_DEPTH;
char const *dependFileName = NULL; char const *dependFileName = NULL;
size_t targetFileNameLen = 0; std::string newTarget;
// Maximum of 100 errors only applies if rgbasm is printing errors to a terminal. // Maximum of 100 errors only applies if rgbasm is printing errors to a terminal.
if (isatty(STDERR_FILENO)) if (isatty(STDERR_FILENO))
@@ -347,24 +347,14 @@ int main(int argc, char *argv[])
generatePhonyDeps = true; generatePhonyDeps = true;
break; break;
char *newTarget;
case 'Q': case 'Q':
case 'T': case 'T':
newTarget = musl_optarg; newTarget = musl_optarg;
if (depType == 'Q') if (depType == 'Q')
newTarget = make_escape(newTarget); newTarget = make_escape(newTarget);
size_t newTargetLen = strlen(newTarget) + 1; // Plus the space if (!targetFileName.empty())
targetFileName += ' ';
targetFileName = (char *)realloc(targetFileName, targetFileName += newTarget;
targetFileNameLen + newTargetLen + 1);
if (targetFileName == NULL)
err("Cannot append new file to target file list");
memcpy(&targetFileName[targetFileNameLen], newTarget, newTargetLen);
if (depType == 'Q')
free(newTarget);
if (targetFileNameLen > 0)
targetFileName[targetFileNameLen - 1] = ' ';
targetFileNameLen += newTargetLen;
break; break;
} }
break; break;
@@ -377,8 +367,8 @@ int main(int argc, char *argv[])
} }
} }
if (!targetFileName && objectName) if (targetFileName.empty() && objectName)
targetFileName = strdup(objectName); targetFileName = objectName;
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);
@@ -396,10 +386,10 @@ int main(int argc, char *argv[])
printf("Assembling %s\n", mainFileName); printf("Assembling %s\n", mainFileName);
if (dependfile) { if (dependfile) {
if (!targetFileName) if (targetFileName.empty())
errx("Dependency files can only be created if a target file is specified with either -o, -MQ or -MT"); errx("Dependency files can only be created if a target file is specified with either -o, -MQ or -MT");
fprintf(dependfile, "%s: %s\n", targetFileName, mainFileName); fprintf(dependfile, "%s: %s\n", targetFileName.c_str(), mainFileName);
} }
charmap_New(DEFAULT_CHARMAP_NAME, NULL); charmap_New(DEFAULT_CHARMAP_NAME, NULL);
@@ -414,7 +404,6 @@ int main(int argc, char *argv[])
if (dependfile) if (dependfile)
fclose(dependfile); fclose(dependfile);
free(targetFileName);
sect_CheckUnionClosed(); sect_CheckUnionClosed();