diff --git a/include/asm/main.h b/include/asm/main.h index 86cb5313..bbc35006 100644 --- a/include/asm/main.h +++ b/include/asm/main.h @@ -19,8 +19,8 @@ extern void opt_Push(void); extern void opt_Pop(void); extern void opt_Parse(char *s); -void fatalerror(char *s); -void yyerror(char *s); +void fatalerror(const char *fmt, ...); +void yyerror(const char *fmt, ...); extern char temptext[1024]; diff --git a/src/asm/fstack.c b/src/asm/fstack.c index 058f3787..b7c3d489 100644 --- a/src/asm/fstack.c +++ b/src/asm/fstack.c @@ -163,7 +163,7 @@ yywrap(void) /* * RGBAsm - FSTACK.C (FileStack routines) * - * Dump the context stack to stdout + * Dump the context stack to stderr * */ @@ -175,11 +175,12 @@ fstk_Dump(void) pLastFile = pFileStack; while (pLastFile) { - printf("%s(%ld) -> ", pLastFile->tzFileName, pLastFile->nLine); + fprintf(stderr, "%s(%ld) -> ", pLastFile->tzFileName, + pLastFile->nLine); pLastFile = pLastFile->pNext; } - printf("%s(%ld)", tzCurrentFileName, nLineNo); + fprintf(stderr, "%s(%ld)", tzCurrentFileName, nLineNo); } /* * RGBAsm - FSTACK.C (FileStack routines) @@ -326,7 +327,7 @@ fstk_RunString(char *s) yy_scan_bytes(pSym->pMacro, strlen(pSym->pMacro)); yy_switch_to_buffer(CurrentFlexHandle); } else - yyerror("No such string symbol"); + yyerror("No such string symbol '%s'", s); } /* * RGBAsm - FSTACK.C (FileStack routines) diff --git a/src/asm/gameboy/yaccprt4.y b/src/asm/gameboy/yaccprt4.y index 2384587e..b4b1cf70 100644 --- a/src/asm/gameboy/yaccprt4.y +++ b/src/asm/gameboy/yaccprt4.y @@ -8,7 +8,7 @@ section: if( $6>=0 && $6<0x10000 ) out_NewAbsSection($2,$4,$6,-1); else - yyerror( "Address must be 16-bit" ); + yyerror("Address $%x not 16-bit", $6); } | T_POP_SECTION string ',' sectiontype ',' T_OP_BANK '[' const ']' { @@ -16,9 +16,9 @@ section: if( $8>=1 && $8<=0x1ff ) out_NewAbsSection($2,$4,-1,$8); else - yyerror( "BANK value out of range" ); + yyerror("ROM bank value $%x out of range (1 to $1ff)", $8); } else - yyerror( "BANK only allowed for CODE/DATA" ); + yyerror("BANK only allowed for CODE/DATA"); } | T_POP_SECTION string ',' sectiontype '[' const ']' ',' T_OP_BANK '[' const ']' { @@ -27,11 +27,11 @@ section: if( $11>=1 && $11<=0x1ff ) out_NewAbsSection($2,$4,$6,$11); else - yyerror( "BANK value out of range" ); + yyerror("ROM bank value $%x out of range (1 to $1ff)", 8); } else - yyerror( "Address must be 16-bit" ); + yyerror("Address $%x not 16-bit", $6); } else - yyerror( "BANK only allowed for CODE/DATA" ); + yyerror("BANK only allowed for CODE/DATA"); } ; @@ -196,7 +196,7 @@ z80_ldio : T_Z80_LDIO T_MODE_A comma op_mem_ind if( (!rpn_isReloc(&$4)) && ($4.nVal<0 || ($4.nVal>0xFF && $4.nVal<0xFF00) || $4.nVal>0xFFFF) ) { - yyerror( "Source must be in the IO/HRAM area" ); + yyerror("Source address $%x not in HRAM ($FF00 to $FFFE)", $4.nVal); } out_AbsByte(0xF0); @@ -210,7 +210,7 @@ z80_ldio : T_Z80_LDIO T_MODE_A comma op_mem_ind if( (!rpn_isReloc(&$2)) && ($2.nVal<0 || ($2.nVal>0xFF && $2.nVal<0xFF00) || $2.nVal>0xFFFF) ) { - yyerror( "Destination must be in the IO/HRAM area" ); + yyerror("Destination address $%x not in HRAM ($FF00 to $FFFE)", $2.nVal); } out_AbsByte(0xE0); @@ -271,7 +271,7 @@ z80_ld_r : T_Z80_LD reg_r comma const_8bit { if( ($2==REG_HL_IND) && ($4==REG_HL_IND) ) { - yyerror( "LD (HL),(HL) not allowed" ); + yyerror("LD [HL],[HL] not a valid instruction"); } else out_AbsByte(0x40|($2<<3)|$4); @@ -284,7 +284,7 @@ z80_ld_a : T_Z80_LD reg_r comma T_MODE_C_IND out_AbsByte(0xF2); else { - yyerror( "Destination operand must be A" ); + yyerror("Destination operand must be A"); } } | T_Z80_LD reg_r comma reg_rr @@ -293,7 +293,7 @@ z80_ld_a : T_Z80_LD reg_r comma T_MODE_C_IND out_AbsByte(0x0A|($4<<4)); else { - yyerror( "Destination operand must be A" ); + yyerror("Destination operand must be A"); } } | T_Z80_LD reg_r comma op_mem_ind @@ -313,7 +313,7 @@ z80_ld_a : T_Z80_LD reg_r comma T_MODE_C_IND } else { - yyerror( "Destination operand must be A" ); + yyerror("Destination operand must be A"); } } ; @@ -390,11 +390,11 @@ z80_rst : T_Z80_RST const_8bit { if( rpn_isReloc(&$2) ) { - yyerror( "Address for RST must be absolute" ); + yyerror("Address for RST must be absolute"); } else if( ($2.nVal&0x38)!=$2.nVal ) { - yyerror( "Invalid address for RST" ); + yyerror("Invalid address $%x for RST", $2.nVal); } else out_AbsByte(0xC7|$2.nVal); diff --git a/src/asm/main.c b/src/asm/main.c index a85ec1ea..80c030ee 100644 --- a/src/asm/main.c +++ b/src/asm/main.c @@ -7,6 +7,7 @@ #include #include +#include #include #include #include @@ -205,18 +206,26 @@ opt_Pop(void) */ void -yyerror(char *s) +yyerror(const char *fmt, ...) { - printf("*ERROR*\t"); + fprintf(stderr, "ERROR:\t"); fstk_Dump(); - printf(" :\n\t%s\n", s); + fprintf(stderr, " :\n\t"); + va_list args; + va_start(args, fmt); + vfprintf(stderr, fmt, args); + va_end(args); + fprintf(stderr, "\n"); nErrors += 1; } void -fatalerror(char *s) +fatalerror(const char *fmt, ...) { - yyerror(s); + va_list args; + va_start(args, fmt); + yyerror(fmt, args); + va_end(args); exit(5); } /* diff --git a/src/asm/rpn.c b/src/asm/rpn.c index fadf7015..e7c7f9e7 100644 --- a/src/asm/rpn.c +++ b/src/asm/rpn.c @@ -155,8 +155,7 @@ rpn_Bank(struct Expression * expr, char *tzSym) psym = sym_FindSymbol(tzSym); if (nPass == 2 && psym == NULL) { - sprintf(temptext, "'%s' not defined", tzSym); - yyerror(temptext); + yyerror("'%s' not defined", tzSym); } expr->isReloc = 1; pushbyte(expr, RPN_BANK); diff --git a/src/asm/symbol.c b/src/asm/symbol.c index ab721e16..aebe5930 100644 --- a/src/asm/symbol.c +++ b/src/asm/symbol.c @@ -217,8 +217,7 @@ sym_Purge(char *tzName) free(pSym); } else { - sprintf(temptext, "'%s' not defined", tzName); - yyerror(temptext); + yyerror("'%s' not defined", tzName); } } /* @@ -245,10 +244,8 @@ sym_isConstDefined(char *tzName) nType & (SYMF_EQU | SYMF_SET | SYMF_MACRO | SYMF_STRING)) { return (1); } else { - sprintf(temptext, - "'%s' is not allowed as argument to the DEF function", - tzName); - fatalerror(temptext); + fatalerror("'%s' is not allowed as argument to the " + "DEF function", tzName); } } return (0); @@ -309,8 +306,7 @@ sym_GetStringValue(char *tzSym) if ((pSym = sym_FindSymbol(tzSym)) != NULL) return (pSym->pMacro); else { - sprintf(temptext, "Stringsymbol '%s' not defined", tzSym); - yyerror(temptext); + yyerror("Stringsymbol '%s' not defined", tzSym); } return (NULL); @@ -339,8 +335,7 @@ sym_GetConstantValue(char *s) fatalerror("Expression must have a constant value"); } } else { - sprintf(temptext, "'%s' not defined", s); - yyerror(temptext); + yyerror("'%s' not defined", s); } return (0); @@ -365,9 +360,7 @@ sym_GetValue(char *s) if ((psym = findsymbol(s, pscope)) != NULL) { if (psym->nType & SYMF_DEFINED) { if (psym->nType & (SYMF_MACRO | SYMF_STRING)) { - sprintf(temptext, - "'%s' is a macro or string symbol", s); - yyerror(temptext); + yyerror("'%s' is a macro or string symbol", s); } return (getvaluefield(psym)); } else { @@ -375,8 +368,7 @@ sym_GetValue(char *s) /* 0x80 seems like a good default value... */ return (0x80); } else { - sprintf(temptext, "'%s' not defined", s); - yyerror(temptext); + yyerror("'%s' not defined", s); } } } else { @@ -384,8 +376,7 @@ sym_GetValue(char *s) createsymbol(s); return (0x80); } else { - sprintf(temptext, "'%s' not defined", s); - yyerror(temptext); + yyerror("'%s' not defined", s); } } @@ -411,18 +402,14 @@ sym_GetDefinedValue(char *s) if ((psym = findsymbol(s, pscope)) != NULL) { if ((psym->nType & SYMF_DEFINED)) { if (psym->nType & (SYMF_MACRO | SYMF_STRING)) { - sprintf(temptext, - "'%s' is a macro or string symbol", s); - yyerror(temptext); + yyerror("'%s' is a macro or string symbol", s); } return (getvaluefield(psym)); } else { - sprintf(temptext, "'%s' not defined", s); - yyerror(temptext); + yyerror("'%s' not defined", s); } } else { - sprintf(temptext, "'%s' not defined", s); - yyerror(temptext); + yyerror("'%s' not defined", s); } return (0); @@ -558,9 +545,7 @@ sym_AddEqu(char *tzSym, SLONG value) if ((nsym = findsymbol(tzSym, NULL)) != NULL) { if (nsym->nType & SYMF_DEFINED) { - sprintf(temptext, "'%s' already defined", - tzSym); - yyerror(temptext); + yyerror("'%s' already defined", tzSym); } } else nsym = createsymbol(tzSym); @@ -586,8 +571,7 @@ sym_AddString(char *tzSym, char *tzValue) if ((nsym = findsymbol(tzSym, NULL)) != NULL) { if (nsym->nType & SYMF_DEFINED) { - sprintf(temptext, "'%s' already defined", tzSym); - yyerror(temptext); + yyerror("'%s' already defined", tzSym); } } else nsym = createsymbol(tzSym); @@ -661,9 +645,7 @@ sym_AddLocalReloc(char *tzSym) if (pScope) { if ((nsym = findsymbol(tzSym, pScope)) != NULL) { if (nsym->nType & SYMF_DEFINED) { - sprintf(temptext, - "'%s' already defined", tzSym); - yyerror(temptext); + yyerror("'%s' already defined", tzSym); } } else nsym = createsymbol(tzSym); @@ -696,9 +678,7 @@ sym_AddReloc(char *tzSym) if ((nsym = findsymbol(tzSym, NULL)) != NULL) { if (nsym->nType & SYMF_DEFINED) { - sprintf(temptext, "'%s' already defined", - tzSym); - yyerror(temptext); + yyerror("'%s' already defined", tzSym); } } else nsym = createsymbol(tzSym); @@ -739,8 +719,7 @@ sym_Export(char *tzSym) if (nsym->nType & SYMF_DEFINED) return; } - sprintf(temptext, "'%s' not defined", tzSym); - yyerror(temptext); + yyerror("'%s' not defined", tzSym); } } @@ -759,8 +738,7 @@ sym_Import(char *tzSym) struct sSymbol *nsym; if (findsymbol(tzSym, NULL)) { - sprintf(temptext, "'%s' already defined", tzSym); - yyerror(temptext); + yyerror("'%s' already defined", tzSym); } if ((nsym = createsymbol(tzSym)) != NULL) nsym->nType |= SYMF_IMPORT; @@ -811,9 +789,7 @@ sym_AddMacro(char *tzSym) if ((nsym = findsymbol(tzSym, NULL)) != NULL) { if (nsym->nType & SYMF_DEFINED) { - sprintf(temptext, "'%s' already defined", - tzSym); - yyerror(temptext); + yyerror("'%s' already defined", tzSym); } } else nsym = createsymbol(tzSym); diff --git a/src/asm/yaccprt3.y b/src/asm/yaccprt3.y index aa9f736f..f0a50a90 100644 --- a/src/asm/yaccprt3.y +++ b/src/asm/yaccprt3.y @@ -47,8 +47,7 @@ macro : T_ID if( !fstk_RunMacro($1) ) { - fprintf(stderr, "Macro '%s' not defined", $1); - yyerror( "No such macro" ); + yyerror("Macro '%s' not defined", $1); } } ; @@ -138,11 +137,11 @@ pushs : T_POP_PUSHS ; fail : T_POP_FAIL string - { fatalerror($2); } + { fatalerror("%s", $2); } ; warn : T_POP_WARN string - { yyerror($2); } + { yyerror("%s", $2); } ; shift : T_POP_SHIFT @@ -268,8 +267,7 @@ include : T_POP_INCLUDE string { if( !fstk_RunInclude($2) ) { - fprintf(stderr, "Could not open file '%s' : %s\n", $2, strerror(errno)); - yyerror( "File not found" ); + yyerror("Could not open file '%s' : %s\n", $2, strerror(errno)); } } ; @@ -328,7 +326,7 @@ const_3bit : const { if( ($1<0) || ($1>7) ) { - yyerror( "Immediate value must be 3-bit" ); + yyerror("Immediate value must be 3-bit"); } else $$=$1&0x7; @@ -366,7 +364,7 @@ const_PCrel : relocconst { $$ = $1; if( !rpn_isPCRelative(&$1) ) - yyerror( "Expression must be PC-relative" ); + yyerror("Expression must be PC-relative"); } ; @@ -374,7 +372,7 @@ const_8bit : relocconst { if( (!rpn_isReloc(&$1)) && (($1.nVal<-128) || ($1.nVal>255)) ) { - yyerror( "Expression must be 8-bit" ); + yyerror("Expression must be 8-bit"); } $$=$1; } @@ -384,7 +382,7 @@ const_16bit : relocconst { if( (!rpn_isReloc(&$1)) && (($1.nVal<-32768) || ($1.nVal>65535)) ) { - yyerror( "Expression must be 16-bit" ); + yyerror("Expression must be 16-bit"); } $$=$1; }