Skip to content
This repository has been archived by the owner on Sep 27, 2023. It is now read-only.

Commit

Permalink
Merge pull request #941 from slacgismo/develop
Browse files Browse the repository at this point in the history
Release beauharnois-24
  • Loading branch information
aivanova5 authored Jul 16, 2021
2 parents 6eca2bf + 3817dfa commit e41cb56
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 25 deletions.
15 changes: 15 additions & 0 deletions docs/Global/Filename.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[[/Global/Filename]] -- Current loader filename

# Synopsis

~~~
#print ${filename}
~~~

# Description

The `filename` global expands to the filename of the current file being processed by the loader.

# See Also

* [[/Global/Modelname]]
10 changes: 10 additions & 0 deletions gldcore/autotest/deferred1.glm
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class test
{
double x;
}

object test
{
name test1;
parent test2;
}
4 changes: 4 additions & 0 deletions gldcore/autotest/deferred2.glm
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
object test
{
name test2;
}
7 changes: 7 additions & 0 deletions gldcore/autotest/loader_variables_include.glm
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#set suppress_repeat_messages=FALSE

#print filename = ${filename}

#print FILENAME = ${FILENAME $filename}
#print FILEPATH = ${FILEPATH $filename}
#print FILETYPE = ${FILETYPE $filename}
5 changes: 5 additions & 0 deletions gldcore/autotest/test_deferred_name_resolution.glm
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#ifexist ../deferred1.glm
#define DIR=..
#endif

#gridlabd ${DIR:-.}/deferred1.glm ${DIR:-.}/deferred2.glm
13 changes: 13 additions & 0 deletions gldcore/autotest/test_loader_variables.glm
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#set suppress_repeat_messages=FALSE

#print filename = ${filename}

#print FILENAME = ${FILENAME $filename}
#print FILEPATH = ${FILEPATH $filename}
#print FILETYPE = ${FILETYPE $filename}

#ifexist ../loader_variables_include.glm
#include "../loader_variables_include.glm"
#else
#include "./loader_variables_include.glm"
#endif
12 changes: 7 additions & 5 deletions gldcore/globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,8 @@ DEPRECATED static struct s_varmap {
{"rusage_rate",PT_int64,&global_rusage_rate,PA_PUBLIC,"rate at which resource usage data is collected (in seconds)"},
{"rusage",PT_char1024,&global_rusage_data,PA_PUBLIC,"rusage data"},
{"echo", PT_bool, &global_echo, PA_PUBLIC, "echo subcommands"},
{"filename",PT_char1024, &global_loader_filename, PA_REFERENCE, "current filename processed by loader"},
// {"linenum",PT_int32, &global_loader_linenum, PA_REFERENCE,"current line number processed by loaded"},
/* add new global variables here */
};

Expand Down Expand Up @@ -1321,7 +1323,7 @@ DEPRECATED const char *global_findfile(char *buffer, int size, const char *spec)

DEPRECATED const char *global_filename(char *buffer, int size, const char *spec)
{
char var[1024];
char var[size+1];
if ( spec[0] != '$' )
{
strncpy(var,spec,sizeof(var)-1);
Expand Down Expand Up @@ -1351,7 +1353,7 @@ DEPRECATED const char *global_filename(char *buffer, int size, const char *spec)

DEPRECATED const char *global_filepath(char *buffer, int size, const char *spec)
{
char var[1024];
char var[size+1];
if ( spec[0] != '$' )
{
strncpy(var,spec,sizeof(var)-1);
Expand All @@ -1361,11 +1363,11 @@ DEPRECATED const char *global_filepath(char *buffer, int size, const char *spec)
output_error("global_filename(buffer=%x,size=%d,spec='%s'): global '%s' is not found");
return NULL;
}
strncpy(buffer,var,size);
char *dir = strrchr(buffer,'/');
char *dir = strrchr(var,'/');
if ( dir != NULL )
{
*dir = '\0';
strncpy(buffer,var,size);
}
else
{
Expand All @@ -1376,7 +1378,7 @@ DEPRECATED const char *global_filepath(char *buffer, int size, const char *spec)

DEPRECATED const char *global_filetype(char *buffer, int size, const char *spec)
{
char var[1024];
char var[size+1];
if ( spec[0] != '$' )
{
strncpy(var,spec,sizeof(var)-1);
Expand Down
6 changes: 6 additions & 0 deletions gldcore/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,12 @@ GLOBAL bool global_server_keepalive INIT(FALSE); /**< keep server alive after si
/* Variable: global_echo */
GLOBAL bool global_echo INIT(FALSE); /**< echo subcommands */

/* Variable: global_loader_filename */
GLOBAL char1024 global_loader_filename INIT("");

/* Variable: global_loader_linenum */
GLOBAL int32 global_loader_linenum INIT(0);

/* Variable: global_json_complex_format */
#define JCF_STRING 0x0000
#define JCF_LIST 0x0001
Expand Down
41 changes: 27 additions & 14 deletions gldcore/load.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ GldLoader::UNRESOLVED *GldLoader::add_unresolved(OBJECT *by, PROPERTYTYPE ptype,
return item;
}

int GldLoader::resolve_object(UNRESOLVED *item, const char *filename)
int GldLoader::resolve_object(UNRESOLVED *item, const char *filename, bool deferred)
{
OBJECT *obj;
char classname[65];
Expand Down Expand Up @@ -873,6 +873,7 @@ int GldLoader::resolve_object(UNRESOLVED *item, const char *filename)
}
if ( obj == NULL )
{
if ( deferred ) return SUCCESS;
syntax_error(filename,item->line,"cannot resolve explicit reference from %s to %s",
format_object(item->by).c_str(), item->id);
return FAILED;
Expand All @@ -890,6 +891,7 @@ int GldLoader::resolve_object(UNRESOLVED *item, const char *filename)
obj = get_next_unlinked(oclass);
if ( obj == NULL )
{
if ( deferred ) return SUCCESS;
syntax_error(filename,item->line,"cannot resolve last reference from %s to %s",
format_object(item->by).c_str(), item->id);
return FAILED;
Expand All @@ -901,6 +903,7 @@ int GldLoader::resolve_object(UNRESOLVED *item, const char *filename)
}
else
{
if ( deferred ) return SUCCESS;
syntax_error(filename,item->line,"'%s' not found", item->id);
return FAILED;
}
Expand All @@ -912,7 +915,7 @@ int GldLoader::resolve_object(UNRESOLVED *item, const char *filename)
return SUCCESS;
}

int GldLoader::resolve_double(UNRESOLVED *item, const char *context)
int GldLoader::resolve_double(UNRESOLVED *item, const char *context, bool deferred)
{
char oname[65];
char pname[65];
Expand Down Expand Up @@ -956,6 +959,7 @@ int GldLoader::resolve_double(UNRESOLVED *item, const char *context)
}
if ( ref == NULL )
{
if ( deferred ) return SUCCESS;
syntax_error(filename,item->line,"transform reference not found");
return FAILED;
}
Expand Down Expand Up @@ -995,7 +999,7 @@ int GldLoader::resolve_double(UNRESOLVED *item, const char *context)
return FAILED;
}

STATUS GldLoader::resolve_list(UNRESOLVED *item)
STATUS GldLoader::resolve_list(UNRESOLVED *item, bool deferred)
{
UNRESOLVED *next;
const char *filename = NULL;
Expand All @@ -1017,14 +1021,14 @@ STATUS GldLoader::resolve_list(UNRESOLVED *item)
// handle different reference types
switch (item->ptype) {
case PT_object:
if (resolve_object(item, filename)==FAILED)
if (resolve_object(item, filename, deferred)==FAILED)
return FAILED;
break;
case PT_double:
case PT_complex:
case PT_loadshape:
case PT_enduse:
if (resolve_double(item, filename)==FAILED)
if (resolve_double(item, filename, deferred)==FAILED)
return FAILED;
break;
default:
Expand All @@ -1040,9 +1044,9 @@ STATUS GldLoader::resolve_list(UNRESOLVED *item)
return SUCCESS;
}

STATUS GldLoader::load_resolve_all(void)
STATUS GldLoader::load_resolve_all(bool deferred)
{
STATUS result = resolve_list(first_unresolved);
STATUS result = resolve_list(first_unresolved,deferred);
first_unresolved = NULL;
return result;
}
Expand Down Expand Up @@ -2968,7 +2972,7 @@ int GldLoader::source_code(PARSER, char *code, int size)
char c1 = _p[0];
char c2 = _p[1];
if (c1=='\n')
linenum++;
inc_linenum();
if (size==0)
{
syntax_error(filename,linenum,"insufficient buffer space to load code");
Expand Down Expand Up @@ -5258,7 +5262,7 @@ int GldLoader::gnuplot(PARSER, GUIENTITY *entity)
int _n = 0;
while ( _p[_n]!='}' )
{
if (_p[_n]=='\n') linenum++;
if (_p[_n]=='\n') inc_linenum();
*p++ = _p[_n++];
if ( p>entity->gnuplot+sizeof(entity->gnuplot) )
{
Expand Down Expand Up @@ -5618,7 +5622,7 @@ int GldLoader::C_code_block(PARSER, char *buffer, int size)
case '}': if (!ignore_curly) n_curly--; break;
case '/': if (_p[1]=='*') skip=1, in_comment=1; else if (_p[1]=='/') skip=1, in_linecomment=1; break;
case '*': if (_p[1]=='/' && in_comment) skip=1, in_comment=0; break;
case '\n': in_linecomment=0; linenum++; break;
case '\n': in_linecomment=0; inc_linenum(); break;
default: break;
}
*d++ = *_p;
Expand Down Expand Up @@ -6416,7 +6420,7 @@ int GldLoader::buffer_read(FILE *fp, char *buffer, char *filename, int size)

/* comments must have preceding whitespace in macros */
char *c = ( ( line[0] != '#' ) ? strstr(line,"//") : strstr(line, " " "//") );
linenum++;
inc_linenum();
if ( c != NULL )
{
/* truncate at comment */
Expand Down Expand Up @@ -6837,9 +6841,13 @@ int GldLoader::include_file(char *incname, char *buffer, int size, int _linenum)
incname, buffer, size, getenv("GLPATH") ? getenv("GLPATH") : "NULL", ff);
}
add_depend(incname,ff);
char1024 parent_file;
strcpy(parent_file,global_loader_filename);
int32 parent_line = global_loader_linenum;
strcpy(global_loader_filename,incname);

old_linenum = linenum;
linenum = 1;
global_loader_linenum = linenum = 1;

if(fstat(fileno(fp), &stat) == 0){
if(stat.st_mtime > modtime){
Expand Down Expand Up @@ -6886,6 +6894,8 @@ int GldLoader::include_file(char *incname, char *buffer, int size, int _linenum)

linenum = old_linenum;
fclose(fp);
strcpy(global_loader_filename,parent_file);
global_loader_linenum = parent_line;
return count;
}

Expand Down Expand Up @@ -8094,6 +8104,8 @@ STATUS GldLoader::loadall_glm(const char *fname) /**< a pointer to the first cha
}
IN_MYCONTEXT output_verbose("file '%s' is %d bytes long", file,fsize);
add_depend(filename,file);
strcpy(global_loader_filename,filename);
global_loader_linenum = 1;

/* removed malloc check since it doesn't malloc any more */
buffer[0] = '\0';
Expand Down Expand Up @@ -8144,7 +8156,7 @@ STATUS GldLoader::loadall_glm(const char *fname) /**< a pointer to the first cha
output_error("%s doesn't appear to be a GLM file", file);
goto Failed;
}
else if ((status=load_resolve_all())==FAILED)
else if ((status=load_resolve_all(true))==FAILED)
goto Failed;

/* establish ranks */
Expand All @@ -8163,8 +8175,9 @@ STATUS GldLoader::loadall_glm(const char *fname) /**< a pointer to the first cha
Done:
//free(buffer);
free_index();
linenum=1; // parser starts at one
global_loader_linenum = linenum = 1; // parser starts at one
if (fp!=NULL) fclose(fp);
strcpy(global_loader_filename,"");
return status;
}

Expand Down
12 changes: 8 additions & 4 deletions gldcore/load.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,12 @@ class GldLoader
OBJECT *get_next_unlinked(CLASS *oclass);
void free_index(void);
UNRESOLVED *add_unresolved(OBJECT *by, PROPERTYTYPE ptype, void *ref, CLASS *oclass, char *id, char *file, unsigned int line, int flags);
int resolve_object(UNRESOLVED *item, const char *filename);
int resolve_double(UNRESOLVED *item, const char *context);
STATUS resolve_list(UNRESOLVED *item);
STATUS load_resolve_all(void);
int resolve_object(UNRESOLVED *item, const char *filename, bool deferred);
int resolve_double(UNRESOLVED *item, const char *context, bool deferred);
STATUS resolve_list(UNRESOLVED *item, bool deferred);
public:
STATUS load_resolve_all(bool deferred=false);
private:
void start_parse(int &mm, int &m, int &n, int &l, int linenum);
void syntax_error_here(const char *p);
int white(PARSER);
Expand Down Expand Up @@ -379,6 +381,8 @@ class GldLoader
void set_last_term(const char *p);
void save_last_term(const char *p);
const char *get_last_term(void);
private:
void inc_linenum() { linenum++; global_loader_linenum = linenum; };
};

#endif
13 changes: 12 additions & 1 deletion gldcore/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ GldMain::GldMain(int argc, const char *argv[])
IN_MYCONTEXT output_verbose("using %d helper thread(s)", global_threadcount);

/* process command line arguments */
if (cmdarg.load(argc,argv)==FAILED)
if ( cmdarg.load(argc,argv) == FAILED )
{
output_fatal("shutdown after command line rejected");
/* TROUBLESHOOT
Expand All @@ -151,6 +151,17 @@ GldMain::GldMain(int argc, const char *argv[])
exec.mls_done();
return;
}
if ( loader.load_resolve_all() == FAILED )
{
output_fatal("shutdown after command loader name resolution failed");
/* TROUBLESHOOT
The loaded files are not valid and the system did not
complete its startup procedure. Correct the problem
with the command line and try again.
*/
exec.mls_done();
return;
}

/* stitch clock */
global_clock = global_starttime;
Expand Down
2 changes: 1 addition & 1 deletion gldcore/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

#define REV_MAJOR 4
#define REV_MINOR 2
#define REV_PATCH 23
#define REV_PATCH 24

#ifdef HAVE_CONFIG_H
#include "config.h"
Expand Down

0 comments on commit e41cb56

Please sign in to comment.