added more error checking when writing files

git-svn-id: https://svn.code.sf.net/p/acme-crossass/code-0/trunk@358 4df02467-bbd4-4a76-a152-e7ce94205b78
This commit is contained in:
marcobaye 2024-03-10 11:24:06 +00:00
parent 640373c54f
commit dbf3cbd61e
6 changed files with 48 additions and 23 deletions

View File

@ -1,5 +1,5 @@
ASSEMBLER6502 = acme
AS_FLAGS = -v9 -Wtype-mismatch
AS_FLAGS = -v8 -Wtype-mismatch
RM = rm
PROGS = c64doubledabble.prg c64misc.prg ddrv128.prg ddrv64.prg macedit.prg reu-detect.prg trigono.o

View File

@ -164,9 +164,11 @@ static void report_init(struct report *report)
// open report file
static int report_open(struct report *report, const char *filename)
{
// show filename _now_ because I do not want to massage it into perror()
printf("Opening report file \"%s\".\n", filename);
report->fd = fopen(filename, FILE_WRITETEXT);
if (report->fd == NULL) {
fprintf(stderr, "Error: Cannot open report file \"%s\".\n", filename);
perror("Error: Cannot open report file");
return 1;
}
return 0; // success
@ -190,24 +192,28 @@ int ACME_finalize(int exit_code)
report_close(report);
if (config.symbollist_filename) {
// show filename _now_ because I do not want to massage it into perror()
printf("Writing symbol list file \"%s\".\n", config.symbollist_filename);
fd = fopen(config.symbollist_filename, FILE_WRITETEXT); // FIXME - what if filename is given via !sl in sub-dir? fix path!
if (fd) {
symbols_list(fd);
fclose(fd);
PLATFORM_SETFILETYPE_TEXT(config.symbollist_filename);
} else {
fprintf(stderr, "Error: Cannot open symbol list file \"%s\".\n", config.symbollist_filename);
perror("Error: Cannot open symbol list file");
exit_code = EXIT_FAILURE;
}
}
if (config.vicelabels_filename) {
// show filename _now_ because I do not want to massage it into perror()
printf("Writing VICE label dump file \"%s\".\n", config.vicelabels_filename);
fd = fopen(config.vicelabels_filename, FILE_WRITETEXT);
if (fd) {
symbols_vicelabels(fd);
fclose(fd);
PLATFORM_SETFILETYPE_TEXT(config.vicelabels_filename);
} else {
fprintf(stderr, "Error: Cannot open VICE label dump file \"%s\".\n", config.vicelabels_filename);
perror("Error: Cannot open VICE label dump file");
exit_code = EXIT_FAILURE;
}
}
@ -230,6 +236,9 @@ static void save_output_file(void)
fputs("No output file specified (use the \"-o\" option or the \"!to\" pseudo opcode).\n", stderr);
return;
}
// show output filename _now_ because I do not want to massage it into
// every possible perror() call.
printf("Writing output file \"%s\".\n", config.output_filename);
// get memory pointer, block size and load address
output_get_result(&body, &amount, &loadaddr);
@ -243,7 +252,7 @@ static void save_output_file(void)
case OUTFILE_FORMAT_CBM:
if (loadaddr > 0xffff) {
fprintf(stderr, "Error: Load address 0x%04lx too large for cbm file format.\n", loadaddr);
exit(EXIT_FAILURE);
exit(ACME_finalize(EXIT_FAILURE));
}
header[0] = loadaddr & 255;
header[1] = loadaddr >> 8;
@ -252,11 +261,11 @@ static void save_output_file(void)
case OUTFILE_FORMAT_APPLE:
if (loadaddr > 0xffff) {
fprintf(stderr, "Error: Load address 0x%04lx too large for apple file format.\n", loadaddr);
exit(EXIT_FAILURE);
exit(ACME_finalize(EXIT_FAILURE));
}
if (amount > 0xffff) {
fprintf(stderr, "Error: File size 0x%04lx too large for apple file format.\n", loadaddr);
exit(EXIT_FAILURE);
exit(ACME_finalize(EXIT_FAILURE));
}
header[0] = loadaddr & 255;
header[1] = loadaddr >> 8;
@ -271,19 +280,35 @@ static void save_output_file(void)
// open file
fd = fopen(config.output_filename, FILE_WRITEBINARY); // FIXME - what if filename is given via !to in sub-dir? fix path!
if (fd == NULL) {
fprintf(stderr, "Error: Cannot open output file \"%s\".\n", config.output_filename);
exit(EXIT_FAILURE);
perror("Error: Cannot open output file");
exit(ACME_finalize(EXIT_FAILURE));
}
if (config.process_verbosity) {
if (config.process_verbosity >= 1) {
printf("Saving %ld (0x%04lx) bytes (0x%04lx - 0x%04lx exclusive).\n",
amount, amount, loadaddr, loadaddr + amount);
}
// write header and body
fwrite(header, headersize, 1, fd);
fwrite(body, amount, 1, fd);
fclose(fd);
// write header and body:
// checking header size and body size explicitly for non-zero values
// facilitates checking the return value of fwrite(), because writing
// one object of size zero returns 0, not 1:
if (headersize) {
if (fwrite(header, headersize, 1, fd) != 1) {
perror("Error: Cannot write header to output file");
exit(ACME_finalize(EXIT_FAILURE));
}
}
if (amount) {
if (fwrite(body, amount, 1, fd) != 1) {
perror("Error: Cannot write to output file");
exit(ACME_finalize(EXIT_FAILURE));
}
}
if (fclose(fd)) {
perror("Error: Cannot flush output file");
exit(ACME_finalize(EXIT_FAILURE));
}
// set file type
switch (config.outfile_format) {
@ -310,7 +335,7 @@ static void perform_pass(void)
int ii;
++pass.number;
if (config.process_verbosity > 1)
if (config.process_verbosity >= 2)
printf("Pass %d:\n", pass.number);
cputype_passinit(); // set default cpu type
output_passinit(); // set initial pc or start with undefined pc
@ -336,7 +361,7 @@ static void perform_pass(void)
// TODO: atm "--from-to" reads two number literals. if that is changed
// in the future to two general expressions, this is the point where
// they would need to be evaluated.
if (config.process_verbosity > 8)
if (config.process_verbosity >= 8)
printf("Found %d undefined expressions.\n", pass.undefined_count);
if (pass.error_count)
exit(ACME_finalize(EXIT_FAILURE));
@ -377,7 +402,7 @@ static void do_actual_work(void)
// if listing report is wanted and there were no errors,
// do another pass to generate listing report
if (config.report_filename) {
if (config.process_verbosity > 1)
if (config.process_verbosity >= 2)
puts("Extra pass to generate listing report.");
if (report_open(report, config.report_filename) == 0) {
perform_pass();
@ -389,7 +414,7 @@ static void do_actual_work(void)
} else {
// There are still errors (unsolvable by doing further passes),
// so perform additional pass to find and show them.
if (config.process_verbosity > 1)
if (config.process_verbosity >= 2)
puts("Extra pass to display errors.");
pass.complain_about_undefined = TRUE; // activate error output
perform_pass(); // perform pass, but now show "value undefined"

View File

@ -56,8 +56,8 @@ void input_parse_and_close_platform_file(const char *eternal_plat_filename, FILE
*outer_input;
// be verbose
if (config.process_verbosity > 2)
printf("Parsing source file '%s'\n", eternal_plat_filename);
if (config.process_verbosity >= 3)
printf("Parsing source file '%s'.\n", eternal_plat_filename);
// set up new input
new_input.plat_pathref_filename = eternal_plat_filename;
new_input.location.plat_filename = eternal_plat_filename;

View File

@ -379,7 +379,7 @@ static void end_segment(void)
// link to segment list
link_segment(out->segment.start, amount);
// announce
if (config.process_verbosity > 1)
if (config.process_verbosity >= 2)
// TODO - change output to start, limit, size, name:
// TODO - output hex numbers as %04x? What about limit 0x10000?
printf("Segment size is %ld (0x%lx) bytes (0x%lx - 0x%lx exclusive).\n",

View File

@ -602,7 +602,7 @@ static enum eos po_binary(void)
}
fclose(stream);
// if verbose, produce some output
if (FIRST_PASS && (config.process_verbosity > 1)) {
if (FIRST_PASS && (config.process_verbosity >= 2)) {
int amount = output_get_statement_size();
printf("Loaded %d (0x%04x) bytes from file offset %ld (0x%04lx).\n",

View File

@ -9,7 +9,7 @@
#define RELEASE "0.97" // update before release FIXME
#define CODENAME "Zem" // update before release
#define CHANGE_DATE "1 Mar" // update before release FIXME
#define CHANGE_DATE "2 Mar" // update before release FIXME
#define CHANGE_YEAR "2024" // update before release
//#define HOME_PAGE "http://home.pages.de/~mac_bacon/smorbrod/acme/"
#define HOME_PAGE "http://sourceforge.net/p/acme-crossass/" // FIXME