Debian Codegraph @

packages/seacas/libraries/aprepro_lib/apr_util.cc

raw
1 // Copyright(C) 1999-2021, 2023, 2024 National Technology & Engineering Solutions
2 // of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
3 // NTESS, the U.S. Government retains certain rights in this software.
4 //
5 // See packages/seacas/LICENSE for details
6
7 #include "aprepro.h" // for symrec, Aprepro, etc
8 #include "aprepro_parser.h" // for Parser, Parser::token, etc
9
10 #include <cctype> // for isalnum, isalpha, isupper, etc
11 #include <cerrno> // for errno, EDOM, ERANGE
12 #include <cfenv> // for fetestexcept, FE_DIVBYZERO, etc
13 #include <cmath> // for math_errhandling, etc
14 #include <cstdio> // for perror
15 #include <cstdlib> // for mkstemp
16 #include <cstring> // for strlen, etc
17 #include <iostream> // for operator<<, cerr, ostream
18 #include <string> // for allocator, operator+, etc
19 #include <sys/stat.h> // for stat, S_ISDIR
20 #include <unistd.h> // for close
21 #include <vector> // for vector
22
23 #if defined(WIN32) || defined(__WIN32__) || defined(_WIN32) || defined(_MSC_VER) || \
24 defined(__MINGW32__) || defined(_WIN64) || defined(__MINGW64__)
25 #include <fcntl.h>
26 #include <io.h>
27 #ifndef NOMINMAX
28 #define NOMINMAX
29 #endif
30 #include <windows.h>
31
32 #if !defined(S_ISDIR)
33 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
34
35 #endif
36 #else
37 #include <unistd.h> // for close
38 #endif
39
40 namespace {
41 std::vector<char *> allocations;
42
43 void copy_string(char *dest, const char *source, size_t elements)
44 {
45 char *d;
46 for (d = dest; d + 1 < dest + elements && *source; d++, source++) {
47 *d = *source;
48 }
49 *d = '\0';
50 }
51
52 void copy_string(char *dest, const std::string &source, size_t elements)
53 {
54 copy_string(dest, source.c_str(), elements);
55 }
56
57 void new_string_int(const char *from, char **to)
58 {
59 auto len = strlen(from);
60 *to = new char[len + 1];
61 copy_string(*to, from, len + 1);
62 allocations.push_back(*to);
63 }
64 } // namespace
65
66 namespace SEAMS {
67 extern Aprepro *aprepro;
68
69 bool arg_check(SEAMS::symrec *symbol, bool is_null)
70 {
71 if (is_null) {
72 std::string err_msg = "Incorrect argument count/type for function '" + symbol->name;
73 err_msg += "'.\n ";
74 err_msg += "The correct syntax is " + symbol->syntax;
75 aprepro->error(err_msg);
76 return false;
77 }
78 return true;
79 }
80
81 void set_type(const SEAMS::Aprepro &apr, SEAMS::symrec *var, int type)
82 {
83 if (var->name[0] == '_' || !apr.state_is_immutable()) {
84 var->type = type;
85 }
86 else {
87 if (type == Parser::token::VAR) {
88 var->type = Parser::token::IMMVAR;
89 }
90 else if (type == Parser::token::SVAR) {
91 var->type = Parser::token::IMMSVAR;
92 }
93 else {
94 var->type = type;
95 }
96 }
97 }
98
99 void new_string(const std::string &from, char **to) { new_string_int(from.c_str(), to); }
100 void new_string(const char *from, char **to) { new_string_int(from, to); }
101
102 void concat_string(const char *from1, const char *from2, char **to)
103 {
104 std::string tmp{from1};
105 tmp += from2;
106 auto len = tmp.length();
107 *to = new char[len + 1];
108 copy_string(*to, tmp, len + 1);
109 allocations.push_back(*to);
110 }
111
112 /* This function returns a pointer to a static character array.
113 * If called multiple times, the name will change on each call,
114 * so don't save the pointer; copy the data it points to if you
115 * need to keep it for awhile.
116 */
117 const char *get_temp_filename()
118 {
119 static char tmp_name[] = "./aprepro_temp_XXXXXX";
120
121 copy_string(tmp_name, "./aprepro_temp_XXXXXX", strlen(tmp_name) + 1);
122 #if defined(__CYGWIN__) && defined(__NO_CYGWIN_OPTION__)
123 int fd = mkstemps(tmp_name, 0);
124 if (fd >= 0)
125 close(fd);
126 #elif defined(WIN32) || defined(__WIN32__) || defined(_WIN32) || defined(_MSC_VER) || \
127 defined(__MINGW32__) || defined(_WIN64) || defined(__MINGW64__)
128 copy_string(tmp_name, _mktemp(tmp_name), strlen(tmp_name) + 1);
129 #else
130 int fd = mkstemp(tmp_name);
131 if (fd >= 0) {
132 close(fd);
133 }
134 #endif
135 return tmp_name;
136 }
137
138 void yyerror(const SEAMS::Aprepro &apr, const std::string &s) { apr.error(s); }
139
140 void immutable_modify(const SEAMS::Aprepro &apr, const SEAMS::symrec *var)
141 {
142 apr.warning("(IMMUTABLE) Variable " + var->name + " is immutable and cannot be modified", true,
143 false);
144 }
145
146 void undefined_error(const SEAMS::Aprepro &apr, const std::string &var)
147 {
148 if (!apr.inIfdefGetvar) {
149 if (apr.ap_options.require_defined) {
150 apr.error("Undefined variable '" + var + "'");
151 }
152 else {
153 apr.warning("Undefined variable '" + var + "'");
154 }
155 }
156 else {
157 apr.inIfdefGetvar = false;
158 }
159 }
160
161 void redefined_warning(const SEAMS::Aprepro &apr, const SEAMS::symrec *var)
162 {
163 if (var->name[0] != '_' && apr.ap_options.warning_msg) {
164 // See if internal or user-defined variable...
165 std::string type;
166 if (var->isInternal) {
167 type = "Pre";
168 }
169 else {
170 type = "User";
171 }
172 apr.warning(type + "-defined Variable '" + var->name + "' redefined");
173 }
174 }
175
176 void warning(const SEAMS::Aprepro &apr, const std::string &s) { apr.warning(s); }
177
178 void math_error(const SEAMS::Aprepro &apr, const char *function)
179 {
180 #if !defined(WIN32) && !defined(__WIN32__) && !defined(_WIN32) && !defined(_MSC_VER) && \
181 !defined(__MINGW32__) && !defined(_WIN64) && !defined(__MINGW64__)
182 #ifndef math_errhandling
183 #define math_errhandling MATH_ERRNO
184 #endif
185
186 if (math_errhandling & MATH_ERRNO) {
187 if (errno != 0) {
188 yyerror(apr, function);
189 }
190 if (errno == EDOM) {
191 perror(" DOMAIN error");
192 }
193 else if (errno == ERANGE) {
194 perror(" RANGE error");
195 }
196 else if (errno != 0) {
197 perror(" Unknown error");
198 }
199 errno = 0;
200 }
201 else if (math_errhandling & MATH_ERREXCEPT) {
202 if (std::fetestexcept(FE_INVALID | FE_OVERFLOW | FE_DIVBYZERO) != 0) {
203 yyerror(apr, function);
204 }
205
206 if (std::fetestexcept(FE_INVALID) != 0) {
207 std::cerr << " DOMAIN error\n";
208 }
209 else if (std::fetestexcept(FE_OVERFLOW) != 0) {
210 std::cerr << " RANGE error -- overflow\n";
211 }
212 else if (std::fetestexcept(FE_DIVBYZERO) != 0) {
213 std::cerr << " RANGE error -- divide by zero\n";
214 }
215 }
216 #endif
217 }
218
219 void math_error(const char *function) { math_error(*SEAMS::aprepro, function); }
220
221 /* Convert string to all lower-case and replace all spaces with '_' */
222 void conv_string(char *string)
223 {
224 char *p = string;
225 while (*p != '\0') {
226 if (*p == ' ') {
227 *p = '_';
228 }
229 else if (isupper(static_cast<int>(*p)) != 0) {
230 *p = static_cast<char>(tolower(static_cast<int>(*p)));
231 }
232 p++;
233 }
234 }
235
236 void cleanup_memory()
237 {
238 for (auto &allocation : allocations) {
239 delete[] allocation;
240 }
241
242 // Clear the vector to avoid stale pointers.
243 allocations.clear();
244 }
245
246 bool is_directory(const std::string &filepath)
247 {
248 struct stat s
249 {
250 };
251 int ok = stat(filepath.c_str(), &s);
252 if (ok == 0) {
253 return S_ISDIR(s.st_mode);
254 }
255
256 return false;
257 }
258
259 bool check_valid_var(const char *var)
260 {
261 /* Check that 'var' meets the restriction for a variable name
262 * L(:|L|D)*
263 * D [0-9]
264 * L [A-Za-z_]
265 */
266
267 auto length = strlen(var);
268 if (length == 0) {
269 return false;
270 }
271
272 if ((isalpha(var[0]) == 0) && var[0] != '_') {
273 return false;
274 }
275
276 for (size_t i = 1; i < length; i++) {
277 char c = var[i];
278 if ((isalnum(c) == 0) && c != ':' && c != '_') {
279 return false;
280 }
281 }
282 return true;
283 }
284 } // namespace SEAMS