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 exporttag(int tag);
SC_FUNC void sc_attachdocumentation(symbol *sym); SC_FUNC void sc_attachdocumentation(symbol *sym);
SC_FUNC int get_actual_compound(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 */ /* function prototypes in SC2.C */
#define PUSHSTK_P(v) { stkitem s_; s_.pv=(v); pushstk(s_); } #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); 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) static void inst_binary_name(char *binfname)
{ {
size_t i, len; size_t i, len;
char *binptr; char *binptr;
char newpath[512], newname[512]; char newname[_MAX_PATH];
int slashchar; int slashchar;
binptr = NULL; binptr = NULL;
@ -435,11 +471,9 @@ static void inst_binary_name(char *binfname)
binptr = binfname; binptr = binfname;
} }
snprintf(newpath, sizeof(newpath), "\"%s\"", binfname);
snprintf(newname, sizeof(newname), "\"%s\"", binptr); snprintf(newname, sizeof(newname), "\"%s\"", binptr);
insert_subst("__BINARY_PATH__", newpath, 15); insert_subst("__BINARY__", newname, 10);
insert_subst("__BINARY_NAME__", newname, 15);
} }
/* "main" of the compiler /* "main" of the compiler
@ -598,6 +632,7 @@ int pc_compile(int argc, char *argv[])
delete_substtable(); delete_substtable();
inst_datetime_defines(); inst_datetime_defines();
inst_binary_name(binfname); inst_binary_name(binfname);
inst_file_name(inpfname, TRUE);
#endif #endif
resetglobals(); resetglobals();
sc_ctrlchar=sc_ctrlchar_org; sc_ctrlchar=sc_ctrlchar_org;
@ -663,6 +698,7 @@ int pc_compile(int argc, char *argv[])
delete_substtable(); delete_substtable();
inst_datetime_defines(); inst_datetime_defines();
inst_binary_name(binfname); inst_binary_name(binfname);
inst_file_name(inpfname, TRUE);
#endif #endif
resetglobals(); resetglobals();
sc_ctrlchar=sc_ctrlchar_org; sc_ctrlchar=sc_ctrlchar_org;

View File

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