MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
parsinc.cc File Reference
#include "mbconfig.h"
#include <cstring>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include "parsinc.h"
#include "filename.h"
Include dependency graph for parsinc.cc:

Go to the source code of this file.

Classes

struct  IncludeDR
 
struct  ChDirDR
 

Macros

#define PATH_MAX   4096
 
#define MAXSUBST   10
 

Functions

static void InitDescData (void)
 
static char * expand_environment (const char *in)
 
static char * resolve_filename (const char *filename_in)
 

Variables

static unsigned desc_done
 

Macro Definition Documentation

#define MAXSUBST   10

Referenced by expand_environment().

#define PATH_MAX   4096

Definition at line 49 of file parsinc.cc.

Referenced by IncludeParser::Include_int(), and IncludeParser::IncludeParser().

Function Documentation

static char* expand_environment ( const char *  in)
static

Definition at line 436 of file parsinc.cc.

References buf, c, DEBUGCOUT, MAXSUBST, and SAFENEWARR.

Referenced by resolve_filename().

437 {
438  char *out = NULL;
439 #define MAXSUBST 10
440  struct {
441  unsigned start;
442  unsigned end;
443  const char *value;
444  unsigned length;
445  } subst[MAXSUBST];
446  unsigned cnt = 0, c;
447 
448  DEBUGCOUT(">> expand_environment: " << in << std::endl);
449 
450  subst[cnt].start = 0;
451  subst[cnt].end = 0;
452  subst[cnt].value = NULL;
453  subst[cnt].length = 0;
454  for (c = 0; in[c]; c++) {
455  if (in[c] == '$') {
456  if (cnt >= MAXSUBST - 2) {
457  silent_cerr("too many substitutions in \""
458  << in << "\"" << std::endl);
459  return NULL;
460  }
461 
462  subst[cnt].end = c;
463  if (in[c + 1] == '$') {
464  c++;
465  subst[cnt].start = c;
466  subst[cnt].value = "";
467  subst[cnt].length = 0;
468  continue;
469  }
470 
471  c++;
472  unsigned namepos = c;
473  if (in[c] == '{') {
474  const char *end = std::strchr(&in[c], '}');
475 
476  if (end == NULL) {
477  silent_cerr("missing trailing \"}\" "
478  "in \"" << in << "\""
479  << std::endl);
480  return NULL;
481  }
482 
483  namepos++;
484  unsigned l = end - &in[namepos];
485  char buf[l + 1];
486  memcpy(buf, &in[namepos], l);
487  buf[l] = '\0';
488  subst[cnt].value = getenv(buf);
489  if (subst[cnt].value == NULL) {
490  silent_cerr("unable to find "
491  "environment "
492  "variable \""
493  << buf << "\""
494  << std::endl);
495  return NULL;
496  }
497 
498  c = end - &in[0] + 1;
499 
500  } else {
501  if (in[c] != '_' && !isalpha(in[c])) {
502  silent_cerr("illegal leading char "
503  "in environment "
504  "variable name in \""
505  << in << "\""
506  << std::endl);
507  return NULL;
508  }
509 
510  for (c++; in[c]; c++) {
511  if (in[c] != '_' && !isalnum(in[c])) {
512  break;
513  }
514  }
515 
516  unsigned l = &in[c] - &in[namepos];
517  char buf[l + 1];
518  memcpy(buf, &in[namepos], l);
519  buf[l] = '\0';
520 
521  subst[cnt].value = getenv(buf);
522  if (subst[cnt].value == NULL) {
523  silent_cerr("unable to find "
524  "environment "
525  "variable \""
526  << buf << "\""
527  << std::endl);
528  return NULL;
529  }
530  }
531 
532  /* can't be NULL */
533  subst[cnt].length = strlen(subst[cnt].value);
534 
535  cnt++;
536  subst[cnt].start = c;
537 
538  /* because it's incremented again by "for" */
539  c--;
540  }
541  }
542  subst[cnt].end = c;
543  subst[cnt].value = NULL;
544  subst[cnt].length = 0;
545 
546  unsigned len = 0;
547  for (c = 0; c < cnt; c++) {
548  len += (subst[c].end - subst[c].start) + subst[c].length;
549  }
550  len += subst[c].end - subst[c].start;
551 
552  SAFENEWARR(out, char, len + 1);
553 
554  unsigned p = 0;
555  for (c = 0; c < cnt; c++) {
556  unsigned l = subst[c].end - subst[c].start;
557  if (l > 0) {
558  memcpy(&out[p], &in[subst[c].start], l);
559  p += l;
560  }
561  if (subst[c].length > 0) {
562  memcpy(&out[p], subst[c].value, subst[c].length);
563  p += subst[c].length;
564  }
565  }
566  unsigned l = subst[c].end - subst[c].start;
567  if (l > 0) {
568  memcpy(&out[p], &in[subst[c].start], l);
569  p += l;
570  }
571  out[p] = '\0';
572 
573  DEBUGCOUT("<< expand_environment: " << out << std::endl);
574 
575  return out;
576 }
#define MAXSUBST
#define DEBUGCOUT(msg)
Definition: myassert.h:232
static std::stack< cleanup * > c
Definition: cleanup.cc:59
#define SAFENEWARR(pnt, item, sz)
Definition: mynewmem.h:701
static doublereal buf[BUFSIZE]
Definition: discctrl.cc:333
static void InitDescData ( void  )
static

Definition at line 98 of file parsinc.cc.

References desc_done, and SetDescData().

Referenced by IncludeParser::IncludeParser().

99 {
100  // NOTE: data will be destroyed when the underlying HighParser is destroyed (is this what we want?)
101  if (::desc_done++ > 0) {
102  return;
103  }
104 
105  SetDescData("include", new IncludeDR);
106  SetDescData("chdir", new ChDirDR);
107 
108  /* NOTE: add here initialization of new built-in descriptions;
109  * alternative ways to register new custom descriptions are:
110  * - call SetDescData() from anywhere in the code
111  * - write a module that calls SetDescData() from inside a function
112  * called module_init(), and run-time load it using "module load"
113  * in the input file.
114  */
115 }
bool SetDescData(const std::string &name, DescRead *rf)
Definition: parser.cc:301
static unsigned desc_done
Definition: parsinc.cc:95

Here is the call graph for this function:

static char* resolve_filename ( const char *  filename_in)
static

Definition at line 579 of file parsinc.cc.

References buf, DIR_SEP, expand_environment(), filename, SAFEDELETEARR, SAFENEWARR, and SAFESTRDUP.

Referenced by IncludeParser::GetFileName().

580 {
581  char *res = NULL,
582  *filename = NULL;;
583 
584  if (strchr(filename_in, '$')) {
585  filename = expand_environment(filename_in);
586  if (filename == NULL) {
587  goto error_return;
588  }
589  } else {
590  filename = (char *)filename_in;
591  }
592 
593  if (filename[0] == '~') {
594  filename++;
595  if (filename[0] == DIR_SEP) {
596  /* do environment stuff */
597  char *home;
598 
599  home = getenv("HOME");
600  if (home == NULL) {
601  goto error_return;
602  }
603 
604  char *s = NULL;
605  int l, ll;
606 
607  l = strlen(home);
608  ll = l + strlen(filename) + 1;
609  SAFENEWARR(s, char, ll);
610 
611  strncpy(s, home, l);
612  strcpy(s + l, filename);
613 
614  res = s;
615  goto error_return;
616 
617 #if defined(HAVE_PWD_H)
618  } else {
619  const char *p;
620 
621  p = std::strchr(filename, DIR_SEP);
622  if (p == NULL) {
623  goto error_return;
624  }
625 
626  int l = p - filename;
627 
628  char buf[l + 1];
629  memcpy(buf, filename, l);
630  buf[l] = '\0';
631 
632  /* do passwd stuff */
633  struct passwd *pw;
634 
635  pw = getpwnam(buf);
636 
637  if (pw == NULL ) {
638  goto error_return;
639  }
640 
641  l = strlen(pw->pw_dir);
642  int ll = l + strlen(p) + 1;
643  char *s = NULL;
644  SAFENEWARR(s, char, ll);
645  strncpy(s, pw->pw_dir, l);
646  strcpy(s + l, p);
647 
648  res = s;
649  goto error_return;
650 #endif /* HAVE_PWD_H */
651  }
652  }
653 
654 error_return:;
655  if (filename != NULL) {
656  if (res == NULL) {
657  if (filename != filename_in) {
658  res = filename;
659  } else {
660  SAFESTRDUP(res, filename_in);
661  }
662  } else {
663  if (filename != filename_in) {
664  SAFEDELETEARR(filename);
665  }
666  }
667  }
668 
669  return res;
670 }
static char * filename
Definition: ann_sf.c:71
#define SAFEDELETEARR(pnt)
Definition: mynewmem.h:713
static char * expand_environment(const char *in)
Definition: parsinc.cc:436
const char DIR_SEP
Definition: filename.h:88
#define SAFESTRDUP(pnt, src)
Definition: mynewmem.h:707
#define SAFENEWARR(pnt, item, sz)
Definition: mynewmem.h:701
static doublereal buf[BUFSIZE]
Definition: discctrl.cc:333

Here is the call graph for this function:

Variable Documentation

unsigned desc_done
static

Definition at line 95 of file parsinc.cc.

Referenced by InitDescData().