Compiler: Add __FILE__, remove __BINARY_PATH__ and rename __BINARY_NAME__ to __BINARY__ (#546)

* Compiler: Add __FILE__ and remove __BINARY_PATH__ constants

* Compiler: Rename __BINARY_NAME__ to __BINARY__
This commit is contained in:
Vincent Herbet 2018-09-03 21:28:28 +02:00 committed by GitHub
parent c16bd47b30
commit 582df637ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 5 deletions

View File

@ -521,6 +521,10 @@ SC_FUNC symbol *add_constant(char *name,cell val,int vclass,int tag);
SC_FUNC void exporttag(int tag);
SC_FUNC void sc_attachdocumentation(symbol *sym);
SC_FUNC int get_actual_compound(symbol *sym);
#if !defined NO_DEFINE
SC_FUNC void inst_file_name(char* filename, int strip_path);
#endif
/* function prototypes in SC2.C */
#define PUSHSTK_P(v) { stkitem s_; s_.pv=(v); pushstk(s_); }

View File

@ -408,11 +408,47 @@ void inst_datetime_defines()
insert_subst("__TIME__", ltime, 8);
}
void inst_file_name(char *file, int strip_path)
{
char newname[_MAX_PATH];
char *fileptr;
fileptr = NULL;
if (strip_path) {
size_t i, len;
int slashchar;
len = strlen(file);
for (i = len - 1; i < len; i--)
{
slashchar = file[i] == '/';
#if defined WIN32 || defined _WIN32
slashchar = slashchar || file[i] == '\\';
#endif
if (slashchar)
{
fileptr = &file[i + 1];
break;
}
}
}
if (fileptr == NULL) {
fileptr = file;
}
snprintf(newname, sizeof(newname), "\"%s\"", fileptr);
insert_subst("__FILE__", newname, 8);
}
static void inst_binary_name(char *binfname)
{
size_t i, len;
char *binptr;
char newpath[512], newname[512];
char newname[_MAX_PATH];
int slashchar;
binptr = NULL;
@ -435,11 +471,9 @@ static void inst_binary_name(char *binfname)
binptr = binfname;
}
snprintf(newpath, sizeof(newpath), "\"%s\"", binfname);
snprintf(newname, sizeof(newname), "\"%s\"", binptr);
insert_subst("__BINARY_PATH__", newpath, 15);
insert_subst("__BINARY_NAME__", newname, 15);
insert_subst("__BINARY__", newname, 10);
}
/* "main" of the compiler
@ -598,6 +632,7 @@ int pc_compile(int argc, char *argv[])
delete_substtable();
inst_datetime_defines();
inst_binary_name(binfname);
inst_file_name(inpfname, TRUE);
#endif
resetglobals();
sc_ctrlchar=sc_ctrlchar_org;
@ -663,6 +698,7 @@ int pc_compile(int argc, char *argv[])
delete_substtable();
inst_datetime_defines();
inst_binary_name(binfname);
inst_file_name(inpfname, TRUE);
#endif
resetglobals();
sc_ctrlchar=sc_ctrlchar_org;

View File

@ -270,6 +270,11 @@ static void doinclude(int silent)
result=plungefile(name,(c!='>'),TRUE);
if (!result && !silent)
error(100,name); /* cannot read from ... (fatal error) */
#if !defined NO_DEFINE
if (result) {
inst_file_name(name, FALSE);
}
#endif
}
/* readline
@ -324,6 +329,9 @@ static void readline(unsigned char *line)
inpf=(FILE *)POPSTK_P();
insert_dbgfile(inpfname);
setfiledirect(inpfname);
#if !defined NO_DEFINE
inst_file_name(inpfname, TRUE);
#endif
assert(sc_status==statFIRST || strcmp(get_inputfile(fcurrent),inpfname)==0);
listline=-1; /* force a #line directive when changing the file */
} /* if */
@ -972,8 +980,14 @@ static int command(void)
if (strlen(pathname)>0) {
free(inpfname);
inpfname=duplicatestring(pathname);
if (inpfname==NULL)
if (inpfname==NULL) {
error(103); /* insufficient memory */
}
#if !defined NO_DEFINE
else {
inst_file_name(inpfname, TRUE);
}
#endif
} /* if */
} /* if */
check_empty(lptr);