| 1 | /****************************************************************************** |
| 2 | * * |
| 3 | * Copyright (C) 2006-2009 by Tor Andersson. * |
| 4 | * Copyright (C) 2010 by Ben Cressey. * |
| 5 | * * |
| 6 | * This file is part of Gargoyle. * |
| 7 | * * |
| 8 | * Gargoyle is free software; you can redistribute it and/or modify * |
| 9 | * it under the terms of the GNU General Public License as published by * |
| 10 | * the Free Software Foundation; either version 2 of the License, or * |
| 11 | * (at your option) any later version. * |
| 12 | * * |
| 13 | * Gargoyle is distributed in the hope that it will be useful, * |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 16 | * GNU General Public License for more details. * |
| 17 | * * |
| 18 | * You should have received a copy of the GNU General Public License * |
| 19 | * along with Gargoyle; if not, write to the Free Software * |
| 20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * |
| 21 | * * |
| 22 | *****************************************************************************/ |
| 23 | |
| 24 | /* osmisc.c -- misc leftovers */ |
| 25 | |
| 26 | #include <string.h> |
| 27 | #include <time.h> |
| 28 | |
| 29 | #include <unistd.h> |
| 30 | |
| 31 | #ifdef _WIN32 |
| 32 | #define WIN32_LEAN_AND_MEAN |
| 33 | #include <windows.h> /* for Sleep() */ |
| 34 | #endif |
| 35 | |
| 36 | #include "os.h" |
| 37 | |
| 38 | /* |
| 39 | * Generate a filename for a character-set mapping file. This function |
| 40 | * should determine the current native character set in use, if |
| 41 | * possible, then generate a filename, according to system-specific |
| 42 | * conventions, that we should attempt to load to get a mapping between |
| 43 | * the current native character set and the internal character set |
| 44 | * identified by 'internal_id'. |
| 45 | * |
| 46 | * The internal character set ID is a string of up to 4 characters. |
| 47 | * |
| 48 | * On DOS, the native character set is a DOS code page. DOS code pages |
| 49 | * are identified by 3- or 4-digit identifiers; for example, code page |
| 50 | * 437 is the default US ASCII DOS code page. We generate the |
| 51 | * character-set mapping filename by appending the internal character |
| 52 | * set identifier to the DOS code page number, then appending ".TCP" to |
| 53 | * the result. So, to map between ISO Latin-1 (internal ID = "La1") and |
| 54 | * DOS code page 437, we would generate the filename "437La1.TCP". |
| 55 | * |
| 56 | * Note that this function should do only two things. First, determine |
| 57 | * the current native character set that's in use. Second, generate a |
| 58 | * filename based on the current native code page and the internal ID. |
| 59 | * This function is NOT responsible for figuring out the mapping or |
| 60 | * anything like that -- it's simply where we generate the correct |
| 61 | * filename based on local convention. |
| 62 | * |
| 63 | * 'filename' is a buffer of at least OSFNMAX characters. |
| 64 | * |
| 65 | * 'argv0' is the executable filename from the original command line. |
| 66 | * This parameter is provided so that the system code can look for |
| 67 | * mapping files in the original TADS executables directory, if desired. |
| 68 | */ |
| 69 | void os_gen_charmap_filename(char *filename, char *internal_id, char *argv0) |
| 70 | { |
| 71 | filename[0] = 0; |
| 72 | } |
| 73 | |
| 74 | |
| 75 | /* ------------------------------------------------------------------------ */ |
| 76 | /* |
| 77 | * get a suitable seed for a random number generator; should use the |
| 78 | * system clock or some other source of an unpredictable and changing |
| 79 | * seed value |
| 80 | */ |
| 81 | void os_rand(long *seed) |
| 82 | { |
| 83 | time_t t; |
| 84 | time( &t ); |
| 85 | *seed = (long)t; |
| 86 | } |
| 87 | |
| 88 | /* |
| 89 | * Xorshift PRNG from http://www.jstatsoft.org/v08/i14/paper |
| 90 | */ |
| 91 | static uint32_t seed[4]; |
| 92 | |
| 93 | static unsigned long xorshift(void) |
| 94 | { |
| 95 | uint32_t x; |
| 96 | x = seed[0] ^ (seed[0] << 11); |
| 97 | seed[0] = seed[1]; |
| 98 | seed[1] = seed[2]; |
| 99 | seed[2] = seed[3]; |
| 100 | |
| 101 | return (seed[3] = (seed[3] ^ (seed[3] >> 19)) ^ (x ^ (x >> 8))); |
| 102 | } |
| 103 | |
| 104 | static void xorinit(void) |
| 105 | { |
| 106 | seed[0] = time(NULL); |
| 107 | seed[1] = seed[0] << 1; |
| 108 | seed[2] = getpid(); |
| 109 | seed[3] = seed[1] & ~seed[2]; |
| 110 | xorshift(); |
| 111 | } |
| 112 | |
| 113 | /* |
| 114 | * Generate random bytes for use in seeding a PRNG. |
| 115 | */ |
| 116 | void os_gen_rand_bytes(unsigned char *buf, size_t len) |
| 117 | { |
| 118 | /* seed the Xorshift PRNG */ |
| 119 | xorinit(); |
| 120 | |
| 121 | int ct, val; |
| 122 | for (ct = 0; ct < len; ct++) |
| 123 | { |
| 124 | val = xorshift(); |
| 125 | buf[ct] = (val - 1) & 0xFF; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | |
| 130 | /* flush any buffered display output */ |
| 131 | void os_flush(void) |
| 132 | { |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | * Update the display - process any pending drawing immediately. This |
| 137 | * only needs to be implemented for operating systems that use |
| 138 | * event-driven drawing based on window invalidations; the Windows and |
| 139 | * Macintosh GUI's both use this method for drawing window contents. |
| 140 | * |
| 141 | * The purpose of this routine is to refresh the display prior to a |
| 142 | * potentially long-running computation, to avoid the appearance that the |
| 143 | * application is frozen during the computation delay. |
| 144 | * |
| 145 | * Platforms that don't need to process events in the main thread in order |
| 146 | * to draw their window contents do not need to do anything here. In |
| 147 | * particular, text-mode implementations generally don't need to implement |
| 148 | * this routine. |
| 149 | * |
| 150 | * This routine doesn't absolutely need a non-empty implementation on any |
| 151 | * platform, but it will provide better visual feedback if implemented for |
| 152 | * those platforms that do use event-driven drawing. |
| 153 | */ |
| 154 | void os_update_display() |
| 155 | { |
| 156 | } |
| 157 | |
| 158 | /* ------------------------------------------------------------------------ */ |
| 159 | /* |
| 160 | * os_plain() - Use plain ascii mode for the display. If possible and |
| 161 | * necessary, turn off any text formatting effects, such as cursor |
| 162 | * positioning, highlighting, or coloring. If this routine is called, |
| 163 | * the terminal should be treated as a simple text stream; users might |
| 164 | * wish to use this mode for things like text-to-speech converters. |
| 165 | * |
| 166 | * Purely graphical implementations that cannot offer a textual mode |
| 167 | * (such as Mac OS or Windows) can ignore this setting. |
| 168 | * |
| 169 | * If this routine is to be called, it must be called BEFORE os_init(). |
| 170 | * The implementation should set a flag so that os_init() will know to |
| 171 | * set up the terminal for plain text output. |
| 172 | */ |
| 173 | /* |
| 174 | * some platforms (e.g. Mac OS) define this to be a null macro, so don't |
| 175 | * define a prototype in those cases |
| 176 | */ |
| 177 | void os_plain(void) |
| 178 | { |
| 179 | } |
| 180 | /* |
| 181 | * Set non-stop mode. This tells the OS layer that it should disable any |
| 182 | * MORE prompting it would normally do. |
| 183 | * |
| 184 | * This routine is needed only when the OS layer handles MORE prompting; on |
| 185 | * character-mode platforms, where the prompting is handled in the portable |
| 186 | * console layer, this can be a dummy implementation. |
| 187 | */ |
| 188 | void os_nonstop_mode(int flag) |
| 189 | { |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | * Sleep for a while. This should simply pause for the given number of |
| 194 | * milliseconds, then return. On multi-tasking systems, this should use |
| 195 | * a system API to unschedule the current process for the desired delay; |
| 196 | * on single-tasking systems, this can simply sit in a wait loop until |
| 197 | * the desired interval has elapsed. |
| 198 | */ |
| 199 | void os_sleep_ms(long delay_in_milliseconds) |
| 200 | { |
| 201 | #ifdef _WIN32 |
| 202 | Sleep(delay_in_milliseconds); |
| 203 | #else |
| 204 | usleep(delay_in_milliseconds * 1000); |
| 205 | #endif |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | * Pause prior to exit, if desired. This is meant to be called by |
| 210 | * portable code just before the program is to be terminated; it can be |
| 211 | * implemented to show a prompt and wait for user acknowledgment before |
| 212 | * proceeding. This is useful for implementations that are using |
| 213 | * something like a character-mode terminal window running on a graphical |
| 214 | * operating system: this gives the implementation a chance to pause |
| 215 | * before exiting, so that the window doesn't just disappear |
| 216 | * unceremoniously. |
| 217 | * |
| 218 | * This is allowed to do nothing at all. For regular character-mode |
| 219 | * systems, this routine usually doesn't do anything, because when the |
| 220 | * program exits, the terminal will simply return to the OS command |
| 221 | * prompt; none of the text displayed just before the program exited will |
| 222 | * be lost, so there's no need for any interactive pause. Likewise, for |
| 223 | * graphical systems where the window will remain open, even after the |
| 224 | * program exits, until the user explicitly closes the window, there's no |
| 225 | * need to do anything here. |
| 226 | * |
| 227 | * If this is implemented to pause, then this routine MUST show some kind |
| 228 | * of prompt to let the user know we're waiting. In the simple case of a |
| 229 | * text-mode terminal window on a graphical OS, this should simply print |
| 230 | * out some prompt text ("Press a key to exit...") and then wait for the |
| 231 | * user to acknowledge the prompt (by pressing a key, for example). For |
| 232 | * graphical systems, the prompt could be placed in the window's title |
| 233 | * bar, or status-bar, or wherever is appropriate for the OS. |
| 234 | */ |
| 235 | void os_expause(void) |
| 236 | { |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | * Install/uninstall the break handler. If possible, the OS code should |
| 241 | * set (if 'install' is true) or clear (if 'install' is false) a signal |
| 242 | * handler for keyboard break signals (control-C, etc, depending on |
| 243 | * local convention). The OS code should set its own handler routine, |
| 244 | * which should note that a break occurred with an internal flag; the |
| 245 | * portable code uses os_break() from time to time to poll this flag. |
| 246 | */ |
| 247 | void os_instbrk(int install) |
| 248 | { |
| 249 | } |
| 250 | |
| 251 | /* |
| 252 | * Set the default saved-game extension. This routine will NOT be |
| 253 | * called when we're using the standard saved game extension; this |
| 254 | * routine will be invoked only if we're running as a stand-alone game, |
| 255 | * and the game author specified a non-standard saved-game extension |
| 256 | * when creating the stand-alone game. |
| 257 | * |
| 258 | * This routine is not required if the system does not use the standard, |
| 259 | * semi-portable os0.c implementation. Even if the system uses the |
| 260 | * standard os0.c implementation, it can provide an empty routine here |
| 261 | * if the system code doesn't need to do anything special with this |
| 262 | * information. |
| 263 | * |
| 264 | * The extension is specified as a null-terminated string. The |
| 265 | * extension does NOT include the leading period. |
| 266 | */ |
| 267 | void os_set_save_ext(const char *ext) |
| 268 | { |
| 269 | } |
| 270 | |
| 271 | /* |
| 272 | * Generate the name of the character set mapping table for Unicode |
| 273 | * characters to and from the given local character set. Fills in the |
| 274 | * buffer with the implementation-dependent name of the desired |
| 275 | * character set map. See below for the character set ID codes. |
| 276 | * |
| 277 | * For example, on Windows, the implementation would obtain the |
| 278 | * appropriate active code page (which is simply a Windows character set |
| 279 | * identifier number) from the operating system, and build the name of |
| 280 | * the Unicode mapping file for that code page, such as "CP1252". On |
| 281 | * Macintosh, the implementation would look up the current script system |
| 282 | * and return the name of the Unicode mapping for that script system, |
| 283 | * such as "ROMAN" or "CENTEURO". |
| 284 | * |
| 285 | * If it is not possible to determine the specific character set that is |
| 286 | * in use, this function should return "asc7dflt" (ASCII 7-bit default) |
| 287 | * as the character set identifier on an ASCII system, or an appropriate |
| 288 | * base character set name on a non-ASCII system. "asc7dflt" is the |
| 289 | * generic character set mapping for plain ASCII characters. |
| 290 | * |
| 291 | * The given buffer must be at least 32 bytes long; the implementation |
| 292 | * must limit the result it stores to 32 bytes. (We use a fixed-size |
| 293 | * buffer in this interface for simplicity, and because there seems no |
| 294 | * need for greater flexibility in the interface; a character set name |
| 295 | * doesn't carry very much information so shouldn't need to be very |
| 296 | * long. Note that this function doesn't generate a filename, but |
| 297 | * simply a mapping name; in practice, a map name will be used to |
| 298 | * construct a mapping file name.) |
| 299 | * |
| 300 | * Because this function obtains the Unicode mapping name, there is no |
| 301 | * need to specify the internal character set to be used: the internal |
| 302 | * character set is Unicode. |
| 303 | */ |
| 304 | /* |
| 305 | * Implementation note: when porting this routine, the convention that |
| 306 | * you use to name your mapping files is up to you. You should simply |
| 307 | * choose a convention for this implementation, and then use the same |
| 308 | * convention for packaging the mapping files for your OS release. In |
| 309 | * most cases, the best convention is to use the names that the Unicode |
| 310 | * consortium uses in their published cross-mapping listings, since |
| 311 | * these listings can be used as the basis of the mapping files that you |
| 312 | * include with your release. For example, on Windows, the convention |
| 313 | * is to use the code page number to construct the map name, as in |
| 314 | * CP1252 or CP1250. |
| 315 | */ |
| 316 | void os_get_charmap(char *mapname, int charmap_id) |
| 317 | { |
| 318 | #ifndef GLK_UNICODE |
| 319 | strcpy(mapname, "asc7dflt"); |
| 320 | #else |
| 321 | switch (charmap_id) |
| 322 | { |
| 323 | case OS_CHARMAP_DISPLAY: |
| 324 | case OS_CHARMAP_FILECONTENTS: |
| 325 | strcpy(mapname, "utf-8"); |
| 326 | break; |
| 327 | |
| 328 | case OS_CHARMAP_FILENAME: |
| 329 | default: |
| 330 | strcpy(mapname, "asc7dflt"); |
| 331 | break; |
| 332 | } |
| 333 | #endif |
| 334 | } |
| 335 | |
| 336 | int os_break(void) |
| 337 | { |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | /* |
| 342 | * Implementation of vasprintf for MinGW. |
| 343 | * NOTE: Because vasprintf is not standard, unconditionally define it |
| 344 | * here, but with a name that won't clash with versions that do exist. |
| 345 | */ |
| 346 | static int xvasprintf(char **sptr, const char *fmt, va_list argv) |
| 347 | { |
| 348 | va_list argv_copy; |
| 349 | int wanted; |
| 350 | |
| 351 | va_copy(argv_copy, argv); |
| 352 | wanted = vsnprintf(*sptr = NULL, 0, fmt, argv); |
| 353 | if((wanted < 0) || ((*sptr = malloc( 1 + wanted )) == NULL)) |
| 354 | return -1; |
| 355 | |
| 356 | return vsprintf(*sptr, fmt, argv_copy); |
| 357 | } |
| 358 | |
| 359 | int os_asprintf(char **bufptr, const char *fmt, ...) |
| 360 | { |
| 361 | int retval; |
| 362 | va_list argv; |
| 363 | va_start(argv, fmt); |
| 364 | retval = xvasprintf(bufptr, fmt, argv); |
| 365 | va_end(argv); |
| 366 | return retval; |
| 367 | } |
| 368 | |
| 369 | int os_vasprintf(char **bufptr, const char *fmt, va_list ap) |
| 370 | { |
| 371 | return xvasprintf(bufptr, fmt, ap); |
| 372 | } |