From 826a14f54ab8bef2472ef16876706ee2f95897e5 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Mon, 24 Jun 2019 14:16:00 -0400 Subject: [PATCH 1/4] add workaround for handline include files # Conflicts: # src/input.h --- src/input.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/input.cpp b/src/input.cpp index 0111cb57384..32b6b697b1a 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -78,6 +78,7 @@ Input::Input(LAMMPS *lmp, int argc, char **argv) : Pointers(lmp) echo_screen = 0; echo_log = 1; + eof_return = 0; label_active = 0; labelstr = NULL; @@ -206,6 +207,7 @@ void Input::file() MPI_Bcast(&n,1,MPI_INT,0,world); if (n == 0) { if (label_active) error->all(FLERR,"Label wasn't found in input script"); + if (eof_return) break; if (me == 0) { if (infile != stdin) { fclose(infile); @@ -1057,6 +1059,11 @@ void Input::include() error->one(FLERR,str); } infiles[nfile++] = infile; + eof_return = 1; + file(); + eof_return = 0; + nfile--; + infile = infiles[nfile-1]; } } From 7f26862f9841ebaab34961288eca4a5ba1187e9c Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Mon, 24 Jun 2019 14:16:51 -0400 Subject: [PATCH 2/4] simplify nested include file handling # Conflicts: # src/input.h --- src/input.cpp | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/src/input.cpp b/src/input.cpp index 32b6b697b1a..e5fbec6d5ab 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -78,7 +78,6 @@ Input::Input(LAMMPS *lmp, int argc, char **argv) : Pointers(lmp) echo_screen = 0; echo_log = 1; - eof_return = 0; label_active = 0; labelstr = NULL; @@ -207,18 +206,7 @@ void Input::file() MPI_Bcast(&n,1,MPI_INT,0,world); if (n == 0) { if (label_active) error->all(FLERR,"Label wasn't found in input script"); - if (eof_return) break; - if (me == 0) { - if (infile != stdin) { - fclose(infile); - infile = NULL; - } - nfile--; - } - MPI_Bcast(&nfile,1,MPI_INT,0,world); - if (nfile == 0) break; - if (me == 0) infile = infiles[nfile-1]; - continue; + break; } if (n > maxline) reallocate(line,maxline,n); @@ -1059,9 +1047,8 @@ void Input::include() error->one(FLERR,str); } infiles[nfile++] = infile; - eof_return = 1; file(); - eof_return = 0; + fclose(infile); nfile--; infile = infiles[nfile-1]; } From 13237155c8f3d01be9a7b9375672d56bd2e787d5 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Mon, 24 Jun 2019 14:24:54 -0400 Subject: [PATCH 3/4] make processing of Input::file(const char* filename) more like processing an include file --- src/input.cpp | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/src/input.cpp b/src/input.cpp index e5fbec6d5ab..644446710f2 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -240,8 +240,8 @@ void Input::file() } /* ---------------------------------------------------------------------- - process all input from filename - called from library interface + process all input from file at filename + mostly called from library interface ------------------------------------------------------------------------- */ void Input::file(const char *filename) @@ -251,21 +251,30 @@ void Input::file(const char *filename) // call to file() will close filename and decrement nfile if (me == 0) { - if (nfile > 1) - error->one(FLERR,"Invalid use of library file() function"); + if (nfile == maxfile) { + maxfile++; + infiles = (FILE **) + memory->srealloc(infiles,maxfile*sizeof(FILE *),"input:infiles"); + } - if (infile && infile != stdin) fclose(infile); infile = fopen(filename,"r"); if (infile == NULL) { char str[128]; snprintf(str,128,"Cannot open input script %s",filename); error->one(FLERR,str); } - infiles[0] = infile; - nfile = 1; + infiles[nfile++] = infile; } + // process contents of file + file(); + + if (me == 0) { + fclose(infile); + nfile--; + infile = infiles[nfile-1]; + } } /* ---------------------------------------------------------------------- @@ -1047,7 +1056,13 @@ void Input::include() error->one(FLERR,str); } infiles[nfile++] = infile; - file(); + } + + // process contents of file + + file(); + + if (me == 0) { fclose(infile); nfile--; infile = infiles[nfile-1]; From 62bddd47ee99ecbc2f7bee7a5d091e26ba667242 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Mon, 1 Jul 2019 00:01:00 -0400 Subject: [PATCH 4/4] limit number of nested include file levels to 16 --- src/input.cpp | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/input.cpp b/src/input.cpp index 644446710f2..ca9defc66bd 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -85,8 +85,9 @@ Input::Input(LAMMPS *lmp, int argc, char **argv) : Pointers(lmp) ifthenelse_flag = 0; if (me == 0) { - nfile = maxfile = 1; - infiles = (FILE **) memory->smalloc(sizeof(FILE *),"input:infiles"); + nfile = 1; + maxfile = 16; + infiles = new FILE *[maxfile]; infiles[0] = infile; } else infiles = NULL; @@ -138,7 +139,7 @@ Input::~Input() memory->sfree(work); if (labelstr) delete [] labelstr; memory->sfree(arg); - memory->sfree(infiles); + delete [] infiles; delete variable; delete command_map; @@ -251,11 +252,8 @@ void Input::file(const char *filename) // call to file() will close filename and decrement nfile if (me == 0) { - if (nfile == maxfile) { - maxfile++; - infiles = (FILE **) - memory->srealloc(infiles,maxfile*sizeof(FILE *),"input:infiles"); - } + if (nfile == maxfile) + error->one(FLERR,"Too many nested levels of input scripts"); infile = fopen(filename,"r"); if (infile == NULL) { @@ -1044,11 +1042,9 @@ void Input::include() error->all(FLERR,"Cannot use include command within an if command"); if (me == 0) { - if (nfile == maxfile) { - maxfile++; - infiles = (FILE **) - memory->srealloc(infiles,maxfile*sizeof(FILE *),"input:infiles"); - } + if (nfile == maxfile) + error->one(FLERR,"Too many nested levels of input scripts"); + infile = fopen(arg[0],"r"); if (infile == NULL) { char str[128];