MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
env.cc
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/mbdyn/base/env.cc,v 1.38 2017/01/12 14:46:09 masarati Exp $ */
2 /*
3  * MBDyn (C) is a multibody analysis code.
4  * http://www.mbdyn.org
5  *
6  * Copyright (C) 1996-2017
7  *
8  * Pierangelo Masarati <masarati@aero.polimi.it>
9  * Paolo Mantegazza <mantegazza@aero.polimi.it>
10  *
11  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
12  * via La Masa, 34 - 20156 Milano, Italy
13  * http://www.aero.polimi.it
14  *
15  * Changing this copyright notice is forbidden.
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation (version 2 of the License).
20  *
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */
31 
32 #include "mbconfig.h" /* This goes first in every *.c,*.cc file */
33 
34 #include <sstream>
35 
36 #include <unistd.h>
37 #include <cerrno>
38 #include <cstdlib>
39 #include <cstdio>
40 #include <cstring>
41 
42 #include "ac/f2c.h"
43 #include "mathp.h"
44 
45 /* environment */
46 extern char **environ;
47 
48 static const char MBDYNPREFIX[] = "MBDYN";
49 
50 extern void GetEnviron(MathParser& MP);
51 
52 void
54 {
55  // cerca la variabile MBDYNVARS
56  std::string MBDYNVARS = std::string(MBDYNPREFIX) + "VARS";
57  char* e = getenv(MBDYNVARS.c_str());
58  if (e != NULL) {
59  DEBUGCOUT("GetEnv: reading variable <" << e << ">" << std::endl);
60  std::istringstream in(e);
61  InputStream In(in);
62  MP.GetLastStmt(In);
63  DEBUGCOUT("GetEnv: variable <" << e << "> read" << std::endl);
64  }
65 
66  /* cerca le variabili definite singolarmente */
67  Table& T = MP.GetSymbolTable();
68  char** env = environ;
69  while (*env) {
70  if (strncmp(*env, MBDYNPREFIX, STRLENOF(MBDYNPREFIX)) == 0) {
71  DEBUGCOUT("reading var <" << *env << ">" << std::endl);
72  char* p = NULL;
73  char* v = NULL;
74  char* n = NULL;
75 
76  SAFESTRDUP(p, *env);
77  v = std::strchr(p, '=');
78  if (v == NULL) {
79  silent_cerr("parse error in envvar <"
80  << p << ">" << std::endl);
81  SAFEDELETEARR(p);
83  }
84 
85  *v = '\0';
86  v++;
87 
88  if (strcmp(p, "MBDYNVARS") == 0) {
89  NO_OP;
90 
91  } else if (strncmp(p, "MBDYN_real_", STRLENOF("MBDYN_real_")) == 0) {
92  n = p + STRLENOF("MBDYN_real_");
93  char *endptr = NULL;
94  errno = 0;
95  doublereal d = strtod(v, &endptr);
96  int save_errno = errno;
97  if (endptr != NULL && endptr[0] != '\0') {
98  silent_cerr("SetEnv: unable to parse "
99  "real <" << v << "> "
100  "for var <" << p << ">"
101  << std::endl);
102  SAFEDELETEARR(p);
104 
105  } else if (save_errno == ERANGE) {
106  silent_cerr("SetEnv: real <" << v << "> "
107  "for var <" << p << "> overflows"
108  << std::endl);
109  SAFEDELETEARR(p);
111  }
112  DEBUGCOUT("setting real var <"
113  << n << "=" << d << ">" << std::endl);
114 
115  if ((T.Get(n)) == NULL) {
116  if (T.Put(n, Real(d)) == NULL) {
117  silent_cerr("SetEnv:"
118  " error in insertion"
119  " of real symbol <"
120  << n << ">" << std::endl);
121  SAFEDELETEARR(p);
123  }
124  }
125 
126  } else if (strncmp(p, "MBDYN_integer_", STRLENOF("MBDYN_integer_")) == 0) {
127  n = p + STRLENOF("MBDYN_integer_");
128 #ifdef HAVE_STRTOL
129  char *endptr = NULL;
130  errno = 0;
131  long i = strtol(v, &endptr, 10);
132  int save_errno = errno;
133  if (endptr != NULL && endptr[0] != '\0') {
134  silent_cerr("SetEnv: unable to parse "
135  "integer <" << v << "> "
136  "for var <" << p << ">"
137  << std::endl);
138  SAFEDELETEARR(p);
140 
141  } else if (save_errno == ERANGE) {
142  silent_cerr("SetEnv: integer <" << v << "> "
143  "for var <" << p << "> overflows"
144  << std::endl);
145  SAFEDELETEARR(p);
147  }
148 #else /* !HAVE_STRTOL */
149  i = atol(v);
150 #endif /* !HAVE_STRTOL */
151  DEBUGCOUT("setting integer var <"
152  << n << "=" << i << ">" << std::endl);
153 
154  if ((T.Get(n)) == NULL) {
155  if (T.Put(n, Int(i)) == NULL) {
156  silent_cerr("SetEnv:"
157  " error in insertion"
158  " of integer symbol <"
159  << n << ">" << std::endl);
160  SAFEDELETEARR(p);
162  }
163  }
164 
165  } else if (strncmp(p, "MBDYN_string_", STRLENOF("MBDYN_string_")) == 0) {
166  n = p + STRLENOF("MBDYN_string_");
167  if ((T.Get(n)) == NULL) {
168  if (T.Put(n, TypedValue(std::string(v))) == NULL) {
169  silent_cerr("SetEnv:"
170  " error in insertion"
171  " of string symbol <"
172  << n << ">" << std::endl);
173  SAFEDELETEARR(p);
175  }
176  }
177 
178  } else {
179  silent_cerr("unknown var type <"
180  << p << ">; skipping ..." << std::endl);
181  }
182 
183  SAFEDELETEARR(p);
184  }
185  env++;
186  }
187 
188  /* altro ... */
189 }
190 
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
Var * Put(const std::string &name, const TypedValue &v)
Definition: table.cc:110
char ** environ
#define SAFEDELETEARR(pnt)
Definition: mynewmem.h:713
#define NO_OP
Definition: myassert.h:74
int Int
Definition: mathtyp.h:40
void GetEnviron(MathParser &MP)
Definition: env.cc:53
#define DEBUGCOUT(msg)
Definition: myassert.h:232
#define STRLENOF(s)
Definition: mbdyn.h:166
static const char MBDYNPREFIX[]
Definition: env.cc:48
NamedValue * Get(const std::string &name) const
Definition: table.cc:150
Real GetLastStmt(Real d=0., Token t=ARGSEP)
Definition: mathp.cc:4402
#define SAFESTRDUP(pnt, src)
Definition: mynewmem.h:707
Definition: table.h:43
double doublereal
Definition: colamd.c:52
double Real
Definition: mathtyp.h:39
Table & GetSymbolTable(void) const
Definition: mathp.cc:1931