38 #include <sys/types.h>
74 silent_cerr(
"Parser error "
87 silent_cerr(
"Error in chdir, path=\"" << sfname <<
"\" at line "
122 const char *sInitialFile)
128 ASSERT(sInitialFile != NULL);
129 #ifdef USE_INCLUDE_PARSER
131 if (getcwd(s,
sizeof(s)) == NULL) {
132 silent_cerr(
"Error in getcwd()" << std::endl);
165 #ifdef USE_INCLUDE_PARSER
177 #ifdef USE_INCLUDE_PARSER
179 <<
">, file <" <<
sCurrFile <<
'>' << std::endl);
193 #ifdef USE_INCLUDE_PARSER
197 <<
"\", file \"" <<
sCurrFile <<
"\"" << std::endl);
199 silent_cerr(
"Error in chdir, path=\""
212 #ifdef USE_INCLUDE_PARSER
239 #ifdef USE_INCLUDE_PARSER
246 #ifdef USE_INCLUDE_PARSER
248 <<
">, file <" <<
sCurrFile <<
'>' << std::endl);
257 #ifdef USE_INCLUDE_PARSER
261 <<
"\", file \"" <<
sCurrFile <<
"\"" << std::endl);
263 silent_cerr(
"Error in chdir, path=\""
285 silent_cerr(
"Parser error in IncludeParser::Include_int(),"
296 if (stat(sfname, &s)) {
297 int save_errno = errno;
299 silent_cerr(
"Cannot stat file <" << sfname <<
"> "
302 <<
" (" << strerror(save_errno) <<
")"
307 if (!S_ISREG(s.st_mode)) {
308 silent_cerr(
"File <" << sfname <<
"> "
310 "not a regular file?" << std::endl);
314 if (!(s.st_mode & S_IRUSR)) {
315 silent_cerr(
"File <" << sfname <<
"> "
317 "no read permissions?" << std::endl);
322 std::ifstream *pf_old =
pf;
338 char *
buf = getcwd(NULL, 0);
340 DEBUGCERR(
"Current directory \"" << buf <<
"\""
352 silent_cerr(
"Invalid file <" << sfname <<
"> "
360 #ifdef USE_INCLUDE_PARSER
365 char* s = (
char*)stmp + strlen(sfname);
366 while (--s >= stmp) {
371 silent_cerr(
"Error in chdir, path="
372 << stmp << std::endl);
376 if (getcwd(p,
sizeof(p)) == NULL) {
377 silent_cerr(
"Error in getcwd()" << std::endl);
383 <<
"\"" << std::endl);
397 if (getcwd(s,
sizeof(s)) == NULL) {
398 silent_cerr(
"Error in getcwd()" << std::endl);
403 <<
"\"" << std::endl);
408 #ifdef USE_INCLUDE_PARSER
411 MyInput(pf_old, pIn_old, sOldPath, sOldFile));
448 DEBUGCOUT(
">> expand_environment: " << in << std::endl);
450 subst[cnt].start = 0;
452 subst[cnt].value = NULL;
453 subst[cnt].length = 0;
454 for (
c = 0; in[
c];
c++) {
457 silent_cerr(
"too many substitutions in \""
458 << in <<
"\"" << std::endl);
463 if (in[
c + 1] ==
'$') {
465 subst[cnt].start =
c;
466 subst[cnt].value =
"";
467 subst[cnt].length = 0;
472 unsigned namepos =
c;
474 const char *end = std::strchr(&in[
c],
'}');
477 silent_cerr(
"missing trailing \"}\" "
478 "in \"" << in <<
"\""
484 unsigned l = end - &in[namepos];
486 memcpy(buf, &in[namepos], l);
488 subst[cnt].value = getenv(buf);
489 if (subst[cnt].value == NULL) {
490 silent_cerr(
"unable to find "
498 c = end - &in[0] + 1;
501 if (in[
c] !=
'_' && !isalpha(in[
c])) {
502 silent_cerr(
"illegal leading char "
504 "variable name in \""
510 for (c++; in[
c]; c++) {
511 if (in[c] !=
'_' && !isalnum(in[c])) {
516 unsigned l = &in[
c] - &in[namepos];
518 memcpy(buf, &in[namepos], l);
521 subst[cnt].value = getenv(buf);
522 if (subst[cnt].value == NULL) {
523 silent_cerr(
"unable to find "
533 subst[cnt].length = strlen(subst[cnt].value);
536 subst[cnt].start =
c;
543 subst[cnt].value = NULL;
544 subst[cnt].length = 0;
547 for (
c = 0;
c < cnt;
c++) {
548 len += (subst[
c].end - subst[
c].start) + subst[
c].length;
550 len += subst[
c].end - subst[
c].start;
555 for (
c = 0;
c < cnt;
c++) {
556 unsigned l = subst[
c].end - subst[
c].start;
558 memcpy(&out[p], &in[subst[
c].start], l);
561 if (subst[
c].length > 0) {
562 memcpy(&out[p], subst[
c].value, subst[
c].length);
563 p += subst[
c].length;
566 unsigned l = subst[
c].end - subst[
c].start;
568 memcpy(&out[p], &in[subst[
c].start], l);
573 DEBUGCOUT(
"<< expand_environment: " << out << std::endl);
584 if (strchr(filename_in,
'$')) {
586 if (filename == NULL) {
590 filename = (
char *)filename_in;
593 if (filename[0] ==
'~') {
599 home = getenv(
"HOME");
608 ll = l + strlen(filename) + 1;
612 strcpy(s + l, filename);
617 #if defined(HAVE_PWD_H)
621 p = std::strchr(filename,
DIR_SEP);
629 memcpy(buf, filename, l);
641 l = strlen(pw->pw_dir);
642 int ll = l + strlen(p) + 1;
645 strncpy(s, pw->pw_dir, l);
655 if (filename != NULL) {
657 if (filename != filename_in) {
663 if (filename != filename_in) {
IncludeParser(MathParser &MP, InputStream &streamIn, const char *initial_file="initial file")
#define MBDYN_EXCEPT_ARGS
bool SetDescData(const std::string &name, DescRead *rf)
static void InitDescData(void)
virtual HighParser::ErrOut GetLineData(void) const
#define SAFEDELETEARR(pnt)
virtual const char * GetFileName(enum Delims Del=DEFAULTDELIM)
virtual ~IncludeParser(void)
bool Read(HighParser &HP)
static char * expand_environment(const char *in)
std::stack< MyInput * > myinput
virtual const char * GetStringWithDelims(enum Delims Del=DEFAULTDELIM, bool escape=true)
const unsigned int iDefaultBufSize
#define ASSERT(expression)
#define SAFENEWWITHCONSTRUCTOR(pnt, item, constructor)
static std::stack< cleanup * > c
#define SAFESTRDUP(pnt, src)
static char * resolve_filename(const char *filename_in)
#define SAFENEWARR(pnt, item, sz)
virtual int GetLineNumber(void) const
char sStringBuf[iDefaultBufSize]
static unsigned desc_done
static doublereal buf[BUFSIZE]
virtual HighParser::ErrOut GetLineData(void) const
bool Read(HighParser &HP)