Debian Codegraph @

packages/seacas/applications/aprepro/aprepro.C

raw
1 // Copyright(C) 1999-2021, 2023, 2024, 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 <cstdlib>
8 #include <cstring>
9 #include <exception>
10 #include <fstream>
11 #include <iostream>
12 #include <string>
13 #include <vector>
14
15 #include "aprepro.h"
16
17 namespace {
18 bool is_double(const std::string &myString)
19 {
20 std::istringstream iss(myString);
21 double f;
22 iss >> std::noskipws >> f; // noskipws considers leading whitespace invalid
23 // Check the entire string was consumed and if either failbit or badbit is set
24 return iss.eof() && !iss.fail();
25 }
26 } // namespace
27
28 int main(int argc, char *argv[])
29 {
30 SEAMS::Aprepro aprepro;
31 std::vector<std::string> input_files;
32
33 bool quiet = false;
34
35 int exit_status = EXIT_SUCCESS;
36
37 // Parse all options...
38 for (int ai = 1; ai < argc; ++ai) {
39 std::string arg = argv[ai];
40 if ((arg[0] == '-' && arg[1] == 'q') || (arg[0] == '-' && arg[1] == '-' && arg[2] == 'q')) {
41 quiet = true;
42 }
43
44 if (arg[0] == '-') { // Parse "--arg [val]" or "--arg=val" or "--arg"
45 std::string val = ai + 1 < argc ? argv[ai + 1] : "";
46 ai += aprepro.set_option(arg, val);
47 }
48 else if (arg.find('=') != std::string::npos) { // Parse var=value option.
49 size_t index = arg.find_first_of('=');
50 std::string var = arg.substr(0, index);
51 std::string value = arg.substr(index + 1);
52 if (value[0] == '\"' || value[0] == '\'') {
53 value = value.substr(1, value.length() - 2);
54 aprepro.add_variable(var, value, true); // Make it immutable
55 }
56 // See if `value` contains any characters that are invalid for a number...
57
58 else if (is_double(value)) {
59 try {
60 double dval = std::stod(value);
61 aprepro.add_variable(var, dval, true);
62 }
63 catch (std::exception & /* e */) {
64 // If cannot convert to double; make it a string variable...
65 try {
66 aprepro.add_variable(var, value, true); // Make it immutable
67 }
68 catch (std::exception &e) {
69 std::cerr << "Aprepro terminated due to exception: " << e.what() << '\n';
70 exit_status = EXIT_FAILURE;
71 }
72 }
73 }
74 else {
75 aprepro.add_variable(var, value, true); // Make it immutable
76 }
77 }
78 else {
79 input_files.emplace_back(argv[ai]);
80 }
81 }
82
83 // Size of input_files should be either 0, 1, or 2:
84 // 0 -- interactive, output to std::cout
85 // 1 -- read from input_files[0], output to std::cout
86 // 2 -- read from input_files[0], output to input_files[1]
87
88 if (input_files.empty()) {
89 if (!quiet) {
90 std::cout << aprepro.long_version() << "\n";
91 }
92 aprepro.ap_options.interactive = true;
93 try {
94 aprepro.parse_stream(std::cin, "standard input");
95
96 if (aprepro.ap_options.errors_fatal && aprepro.get_error_count() > 0) {
97 exit_status = EXIT_FAILURE;
98 }
99 if ((aprepro.ap_options.errors_and_warnings_fatal) &&
100 (aprepro.get_error_count() + aprepro.get_warning_count() > 0)) {
101 exit_status = EXIT_FAILURE;
102 }
103 }
104 catch (std::exception &e) {
105 std::cerr << "Aprepro terminated due to exception: " << e.what() << '\n';
106 exit_status = EXIT_FAILURE;
107 }
108 }
109 else {
110 std::fstream infile(input_files[0], std::fstream::in);
111 if (!infile.good()) {
112 if (!aprepro.ap_options.include_path.empty() && input_files[0][0] != '/') {
113 std::string filename = aprepro.ap_options.include_path + "/" + input_files[0];
114 infile.open(filename, std::fstream::in);
115 }
116 if (!infile.good()) {
117 std::cerr << "APREPRO: ERROR: Could not open file: " << input_files[0] << '\n'
118 << " Error Code: " << strerror(errno) << '\n';
119 return EXIT_FAILURE;
120 }
121 }
122
123 // Read and parse a file. The entire file will be parsed and
124 // then the output can be obtained in an std::ostringstream via
125 // Aprepro::parsing_results()
126 try {
127 bool result = aprepro.parse_stream(infile, input_files[0]);
128
129 bool writeResults = true;
130 if (aprepro.ap_options.errors_fatal && aprepro.get_error_count() > 0) {
131 writeResults = false;
132 }
133 if ((aprepro.ap_options.errors_and_warnings_fatal) &&
134 (aprepro.get_error_count() + aprepro.get_warning_count() > 0)) {
135 writeResults = false;
136 }
137
138 if (writeResults) {
139 if (result) {
140 if (input_files.size() > 1) {
141 std::ofstream ofile(input_files[1]);
142 if (!quiet) {
143 ofile << aprepro.long_version() << "\n";
144 }
145 ofile << aprepro.parsing_results().str();
146 }
147 else {
148 if (!quiet) {
149 std::cout << aprepro.long_version() << "\n";
150 }
151 std::cout << aprepro.parsing_results().str();
152 }
153 }
154 }
155 else {
156 exit_status = EXIT_FAILURE;
157 std::cerr << "There were " << aprepro.get_error_count() << " errors and "
158 << aprepro.get_warning_count() << " warnings." << "\n";
159 if (aprepro.ap_options.errors_and_warnings_fatal) {
160 std::cerr << "Errors and warnings are fatal. No output has been written" << "\n";
161 }
162 else if (aprepro.ap_options.errors_fatal) {
163 std::cerr << "Errors are fatal. No output has been written." << "\n";
164 }
165 else {
166 std::cerr << "Neither errors nor warnings are fatal. "
167 << "If you see this message, then there is a bug in Aprepro. "
168 << "No output has been written." << "\n";
169 }
170 }
171 }
172 catch (std::exception &e) {
173 std::cerr << "Aprepro terminated due to exception: " << e.what() << '\n';
174 exit_status = EXIT_FAILURE;
175 }
176 }
177 if (aprepro.ap_options.debugging || aprepro.ap_options.dumpvars) {
178 aprepro.dumpsym("variable", false);
179 }
180 if (aprepro.ap_options.dumpvars_json) {
181 aprepro.dumpsym_json();
182 }
183 if (aprepro.get_error_count() > 0) {
184 exit_status = EXIT_FAILURE;
185 }
186 return exit_status;
187 }