| 1 | // Copyright 2010-2025 Chris Spiegel. |
| 2 | // |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | |
| 5 | #include <cerrno> |
| 6 | #include <cstdarg> |
| 7 | #include <cstdio> |
| 8 | #include <cstdlib> |
| 9 | #include <fstream> |
| 10 | #include <functional> |
| 11 | #include <iostream> |
| 12 | #include <memory> |
| 13 | #include <string> |
| 14 | |
| 15 | #include "util.h" |
| 16 | #include "process.h" |
| 17 | #include "screen.h" |
| 18 | #include "types.h" |
| 19 | #include "zterp.h" |
| 20 | |
| 21 | #ifdef ZTERP_GLK |
| 22 | #include <glk.h> |
| 23 | #endif |
| 24 | |
| 25 | // Values are usually stored in a uint16_t because most parts of the |
| 26 | // Z-machine make use of 16-bit unsigned integers. However, in a few |
| 27 | // places the unsigned value must be treated as signed. The “obvious” |
| 28 | // solution is to simply convert to an int16_t, but this is technically |
| 29 | // implementation-defined behavior in C++ and not guaranteed to provide |
| 30 | // the expected result. In order to be maximally portable, this |
| 31 | // function converts a uint16_t to its int16_t counterpart manually. |
| 32 | // Although it ought to be slower, both Gcc and Clang recognize this |
| 33 | // construct and generate the same code as a direct conversion. An |
| 34 | // alternative direct conversion method is included here for reference. |
| 35 | #if 1 |
| 36 | int16_t as_signed(uint16_t n) |
| 37 | { |
| 38 | return ((n & 0x8000) == 0x8000) ? static_cast<long>(n) - 0x10000L : n; |
| 39 | } |
| 40 | #else |
| 41 | int16_t as_signed(uint16_t n) |
| 42 | { |
| 43 | return n; |
| 44 | } |
| 45 | #endif |
| 46 | |
| 47 | #ifndef ZTERP_NO_SAFETY_CHECKS |
| 48 | void assert_fail(const char *fmt, ...) |
| 49 | { |
| 50 | va_list ap; |
| 51 | std::string str; |
| 52 | |
| 53 | va_start(ap, fmt); |
| 54 | str = vstring(fmt, ap); |
| 55 | va_end(ap); |
| 56 | |
| 57 | str += fstring(" (pc = 0x%lx)", current_instruction); |
| 58 | |
| 59 | die("%s", str.c_str()); |
| 60 | } |
| 61 | #endif |
| 62 | |
| 63 | void warning(const char *fmt, ...) |
| 64 | { |
| 65 | va_list ap; |
| 66 | std::string str; |
| 67 | |
| 68 | va_start(ap, fmt); |
| 69 | str = vstring(fmt, ap); |
| 70 | va_end(ap); |
| 71 | |
| 72 | show_message("Warning: %s", str.c_str()); |
| 73 | } |
| 74 | |
| 75 | void die(const char *fmt, ...) |
| 76 | { |
| 77 | va_list ap; |
| 78 | std::string str; |
| 79 | |
| 80 | va_start(ap, fmt); |
| 81 | str = vstring(fmt, ap); |
| 82 | va_end(ap); |
| 83 | |
| 84 | show_message("Fatal error: %s", str.c_str()); |
| 85 | |
| 86 | #ifdef GARGLK |
| 87 | std::cerr << "Fatal error: " << str << std::endl; |
| 88 | #endif |
| 89 | |
| 90 | throw Exit(EXIT_FAILURE); |
| 91 | } |
| 92 | |
| 93 | long parseint(const std::string &s, int base, bool &valid) |
| 94 | { |
| 95 | char *endptr; |
| 96 | const char *cstr = s.c_str(); |
| 97 | |
| 98 | errno = 0; |
| 99 | long ret = std::strtol(cstr, &endptr, base); |
| 100 | valid = |
| 101 | endptr != cstr && |
| 102 | *endptr == 0 && |
| 103 | errno != ERANGE; |
| 104 | |
| 105 | return ret; |
| 106 | } |
| 107 | |
| 108 | std::string vstring(const char *fmt, std::va_list ap) |
| 109 | { |
| 110 | std::va_list ap_copy; |
| 111 | std::string s; |
| 112 | |
| 113 | va_copy(ap_copy, ap); |
| 114 | |
| 115 | int n = std::vsnprintf(nullptr, 0, fmt, ap); |
| 116 | if (n < 0) { |
| 117 | die("error processing format string"); |
| 118 | } |
| 119 | |
| 120 | s.resize(n); |
| 121 | std::vsnprintf(&s[0], n + 1, fmt, ap_copy); |
| 122 | |
| 123 | va_end(ap_copy); |
| 124 | |
| 125 | return s; |
| 126 | } |
| 127 | |
| 128 | std::string fstring(const char *fmt, ...) |
| 129 | { |
| 130 | std::va_list ap; |
| 131 | std::string s; |
| 132 | |
| 133 | va_start(ap, fmt); |
| 134 | s = vstring(fmt, ap); |
| 135 | va_end(ap); |
| 136 | |
| 137 | return s; |
| 138 | } |
| 139 | |
| 140 | std::string ltrim(const std::string &s) |
| 141 | { |
| 142 | auto pos = s.find_first_not_of(" \t\r"); |
| 143 | |
| 144 | if (pos != std::string::npos) { |
| 145 | return s.substr(pos); |
| 146 | } else { |
| 147 | return ""; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | std::string rtrim(const std::string &s) |
| 152 | { |
| 153 | auto pos = s.find_last_not_of(" \t\r"); |
| 154 | |
| 155 | if (pos != std::string::npos) { |
| 156 | return s.substr(0, pos + 1); |
| 157 | } else { |
| 158 | return ""; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | void parse_grouped_file(std::ifstream &f, const std::function<void(const std::string &line, int lineno)> &callback) |
| 163 | { |
| 164 | std::string line; |
| 165 | bool story_matches = true; |
| 166 | |
| 167 | for (int lineno = 1; std::getline(f, line); lineno++) { |
| 168 | line = ltrim(line); |
| 169 | |
| 170 | auto comment = line.find('#'); |
| 171 | if (comment != std::string::npos) { |
| 172 | line.erase(comment); |
| 173 | } |
| 174 | line = rtrim(line); |
| 175 | |
| 176 | if (line.empty()) { |
| 177 | continue; |
| 178 | } |
| 179 | |
| 180 | if (line[0] == '[' && line.back() == ']') { |
| 181 | std::string id = line.substr(1, line.size() - 2); |
| 182 | story_matches = id == get_story_id(); |
| 183 | |
| 184 | continue; |
| 185 | } |
| 186 | |
| 187 | if (!story_matches) { |
| 188 | continue; |
| 189 | } |
| 190 | |
| 191 | callback(line, lineno); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | std::unique_ptr<std::string> zterp_getenv(const std::string &name) |
| 196 | { |
| 197 | const char *val = std::getenv(name.c_str()); |
| 198 | if (val == nullptr) { |
| 199 | return nullptr; |
| 200 | } |
| 201 | |
| 202 | return std::make_unique<std::string>(val); |
| 203 | } |