| 1 | /* Adapted from osfrobtads.h from FrobTADS 1.2.3. |
| 2 | * FrobTADS copyright (C) 2009 Nikos Chantziaras. |
| 3 | * Unused/overridden parts of osfrobtads.h are disabled with #if 0; this |
| 4 | * allows an easy diff with future FrobTADS releases to see changes. |
| 5 | */ |
| 6 | |
| 7 | /* OS-layer functions and macros. |
| 8 | * |
| 9 | * This file does not introduce any curses (or other screen-API) |
| 10 | * dependencies; it can be used for both the interpreter as well as the |
| 11 | * compiler. |
| 12 | */ |
| 13 | #ifndef OSFROBTADS_H |
| 14 | #define OSFROBTADS_H |
| 15 | |
| 16 | /* Defined for Gargoyle. */ |
| 17 | #define HAVE_STDINT_ |
| 18 | |
| 19 | #if 0 |
| 20 | #include "common.h" |
| 21 | #endif |
| 22 | |
| 23 | /* Standard C headers should never be included from inside an extern "C" |
| 24 | * block. However, we are included from tads2/os.h from inside such a |
| 25 | * block ourselves, so everything we include will be extern "C" too. |
| 26 | * We need to reverse this or some compilers will bark (Sun C++ 5.9 on |
| 27 | * Linux, for example.) |
| 28 | */ |
| 29 | #ifdef __cplusplus |
| 30 | extern "C++" { |
| 31 | #endif |
| 32 | #include <stdlib.h> |
| 33 | #include <stdio.h> |
| 34 | #include <sys/types.h> |
| 35 | #include <sys/stat.h> |
| 36 | #include <unistd.h> |
| 37 | #ifdef _WIN32 |
| 38 | #define WIN32_LEAN_AND_MEAN |
| 39 | #define NOMINMAX |
| 40 | #include <windows.h> |
| 41 | #else |
| 42 | #include <dirent.h> |
| 43 | #endif |
| 44 | #ifdef __cplusplus |
| 45 | } |
| 46 | #endif |
| 47 | |
| 48 | /* The base code needs access to the C99 exact-width types (int32_t and |
| 49 | * friends.) Those are provided by <stdint.h>, so we simply include that |
| 50 | * if it's available. If not, we can typedef them ourselves since our |
| 51 | * autoconf script provides us with the exact sizes of the standard |
| 52 | * integer types. |
| 53 | */ |
| 54 | #ifdef HAVE_STDINT_ |
| 55 | # include <stdint.h> |
| 56 | #else |
| 57 | # if SIZEOF_SHORT == 2 |
| 58 | typedef short int16_t; |
| 59 | typedef unsigned short uint16_t; |
| 60 | # elif SIZEOF_INT == 2 |
| 61 | typedef int int16_t; |
| 62 | typedef unsigned int uint16_t; |
| 63 | # endif |
| 64 | # if SIZEOF_INT == 4 |
| 65 | typedef int int32_t; |
| 66 | typedef unsigned int uint32_t; |
| 67 | # elif SIZEOF_LONG == 4 |
| 68 | typedef long int32_t; |
| 69 | typedef unsigned long uint32_t; |
| 70 | # endif |
| 71 | #endif |
| 72 | |
| 73 | #if 0 |
| 74 | /* Some parts in the basecode behave differently when UNIX is defined, |
| 75 | * like using "Makefile.t3m" instead "makefile.t3m" as the default |
| 76 | * project file and turning off stdout buffering in the test suite. |
| 77 | */ |
| 78 | #ifndef UNIX |
| 79 | #define UNIX |
| 80 | #endif |
| 81 | #endif |
| 82 | |
| 83 | /* Used by the base code to inhibit "unused parameter" compiler warnings. */ |
| 84 | #define VARUSED(var) (void)var |
| 85 | |
| 86 | /* We don't support the Atari 2600. */ |
| 87 | #include "osbigmem.h" |
| 88 | |
| 89 | #if 0 |
| 90 | /* Provide some non-standard functions (memicmp(), etc). */ |
| 91 | #include "missing.h" |
| 92 | |
| 93 | /* Our Tads OEM identifier. */ |
| 94 | #define TADS_OEM_NAME PACKAGE_MAINTAINER " <" PACKAGE_BUGREPORT ">" |
| 95 | #endif |
| 96 | |
| 97 | /* We assume that the C-compiler is mostly ANSI compatible. */ |
| 98 | #define OSANSI |
| 99 | |
| 100 | /* Special function qualifier needed for certain types of callback |
| 101 | * functions. This is for old 16-bit systems; we don't need it and |
| 102 | * define it to nothing. */ |
| 103 | #define OS_LOADDS |
| 104 | |
| 105 | /* Unices don't suffer the near/far pointers brain damage (thank God) so |
| 106 | * we make this a do-nothing macro. */ |
| 107 | #define osfar_t |
| 108 | |
| 109 | /* This is used to explicitly discard computed values (some compilers |
| 110 | * would otherwise give a warning like "computed value not used" in some |
| 111 | * cases). Casting to void should work on every ANSI-Compiler. */ |
| 112 | #define DISCARD (void) |
| 113 | |
| 114 | /* Copies a struct into another. ANSI C allows the assignment operator |
| 115 | * to be used with structs. */ |
| 116 | #define OSCPYSTRUCT(x,y) ((x)=(y)) |
| 117 | |
| 118 | /* Link error messages into the application. */ |
| 119 | #define ERR_LINK_MESSAGES |
| 120 | |
| 121 | /* Program Exit Codes. */ |
| 122 | #define OSEXSUCC 0 /* Successful completion. */ |
| 123 | #define OSEXFAIL 1 /* Failure. */ |
| 124 | |
| 125 | #if 0 |
| 126 | /* The default size for the UNDO-buffer is quite small; the player is |
| 127 | * only allowed to UNDO about 5 or 6 times. We'll make the size of this |
| 128 | * buffer configurable by the user. |
| 129 | * |
| 130 | * See tads3/vmparam.h for more info. |
| 131 | * |
| 132 | * We achieve this by redefining the macro that TADS uses to store the |
| 133 | * buffer-size as a normal variable. Fortunately, TADS3 allocates this |
| 134 | * buffer either through 'new' or malloc(), never as a static array, so |
| 135 | * this approach works on every compiler no matter if dynamically-sized |
| 136 | * arrays are supported or not (ISO C++ forbids things like |
| 137 | * "int a[variable];"). |
| 138 | * |
| 139 | * This stuff is only useful for the interpreter, so we omit it when |
| 140 | * building the compiler. |
| 141 | */ |
| 142 | #ifdef RUNTIME |
| 143 | /* First, we need to know what TADS3 uses as default. This is defined |
| 144 | * in vmparam.h. */ |
| 145 | #include "vmparam.h" |
| 146 | /* We store the default in a static variable since the macro-expansion |
| 147 | * needs to happen right now as we redefine this macro later. */ |
| 148 | static const int defaultVmUndoMaxRecords = VM_UNDO_MAX_RECORDS; |
| 149 | /* Since we'll redefine this macro, we must undefine it first, as is |
| 150 | * required by standard C and C++. */ |
| 151 | #undef VM_UNDO_MAX_RECORDS |
| 152 | #undef VM_UNDO_MAX_SAVEPTS |
| 153 | /* Now we'll turn the macro into a normal variable. */ |
| 154 | #define VM_UNDO_MAX_RECORDS frobVmUndoMaxRecords |
| 155 | extern int frobVmUndoMaxRecords; |
| 156 | /* The maximum number of allowed savepoints has a hard-coded upper-limit |
| 157 | * of 255 in the TADS3 base code. Since this value has no direct effect |
| 158 | * on memory consumption (it's the max undo records that matters) we'll |
| 159 | * just use the maximum and won't provide a way to change it. */ |
| 160 | #define VM_UNDO_MAX_SAVEPTS 255 |
| 161 | #endif /* RUNTIME */ |
| 162 | #endif |
| 163 | |
| 164 | /* Here we configure the osgen layer; refer to tads2/osgen3.c for more |
| 165 | * information about the meaning of these macros. */ |
| 166 | #define USE_DOSEXT |
| 167 | #define USE_NULLSTYPE |
| 168 | #if 0 |
| 169 | #define USE_NULL_SET_TITLE |
| 170 | /* STD_OSCLS is undocumented; it enables a standard oscls() |
| 171 | * implementation. */ |
| 172 | #define STD_OSCLS |
| 173 | /* Undocumented; enables a standard implementation of the highlighting & |
| 174 | * colors routines. It also enables the osgen3 banner API |
| 175 | * implementation. */ |
| 176 | #define STD_OS_HILITE |
| 177 | /* The following osgen configuration macros aren't needed by the |
| 178 | * compiler, so we only define them if we are building the interpreter. |
| 179 | */ |
| 180 | #ifdef RUNTIME |
| 181 | # define USE_STATLINE |
| 182 | # define USE_SCROLLBACK |
| 183 | # define USE_HISTORY |
| 184 | /* OS_SBSTAT is undocumented, but needed by osgen3; it should contain |
| 185 | * the string to print when scrollback-mode is activated. */ |
| 186 | # define OS_SBSTAT "Review Mode - Keys: Up/Down/PageUp/PageDown - F1/Esc to exit" |
| 187 | /* Undocumented; size of the command-history buffer. */ |
| 188 | # define HISTBUFSIZE 1000 |
| 189 | /* status_mode is an undocumented variable, but needed by osgen3. It |
| 190 | * keeps track of where game-text should go (statusline, game window, |
| 191 | * nowhere). We don't need to know anything about that though; we just |
| 192 | * define it so that osgen3.c compiles and links without errors, as it |
| 193 | * doesn't define it itself for some reason. */ |
| 194 | extern int status_mode; |
| 195 | #else |
| 196 | /* Tell osgen to use stdio-routines for certain functions (the |
| 197 | * compiler doesn't need anything more than that). */ |
| 198 | # define USE_STDIO |
| 199 | /* Use a do-nothing os_score() function. */ |
| 200 | # define USE_NULLSCORE |
| 201 | /* The compiler doesn't actually use os_xlat_html4(); we just define |
| 202 | * it as an empty macro so the linker won't bark. */ |
| 203 | # define os_xlat_html4(html4_char,result,result_buf_len) |
| 204 | #endif |
| 205 | |
| 206 | /* Tell tads2/osnoui.c that we provide our own implementation of |
| 207 | * tempfile creation/deletion routines. */ |
| 208 | #define OSNOUI_OMIT_TEMPFILE |
| 209 | |
| 210 | /* Override the default makefile for the TADS 3 compiler. We'll |
| 211 | * capitalize the fist letter (as is usual in Unix). */ |
| 212 | #ifdef T3_DEFAULT_PROJ_FILE |
| 213 | # undef T3_DEFAULT_PROJ_FILE |
| 214 | # define T3_DEFAULT_PROJ_FILE "Makefile.t3m" |
| 215 | #endif |
| 216 | |
| 217 | /* System identifier and system descriptive name. We also state |
| 218 | * "Windows" since we compile and run just fine under MS Windows. We |
| 219 | * ommit "curses" in the compilers, as only the interpreter uses |
| 220 | * curses. */ |
| 221 | #ifdef RUNTIME |
| 222 | # define OS_SYSTEM_NAME "CURSES" |
| 223 | # define OS_SYSTEM_LDESC "curses (POSIX/Unix/MS-Windows)" |
| 224 | #else |
| 225 | # define OS_SYSTEM_NAME "POSIX_UNIX_MSWINDOWS" |
| 226 | # define OS_SYSTEM_LDESC "POSIX/Unix/MS-Windows" |
| 227 | #endif |
| 228 | #endif |
| 229 | |
| 230 | /* Theoretical maximum osmalloc() size. |
| 231 | * Unix systems have at least a 32-bit memory space. Even on 64-bit |
| 232 | * systems, 2^32 is a good value, so we don't bother trying to find out |
| 233 | * an exact value. */ |
| 234 | #define OSMALMAX 0xffffffffL |
| 235 | |
| 236 | #if 0 |
| 237 | /* Maximum length of a filename. */ |
| 238 | #define OSFNMAX 255 |
| 239 | |
| 240 | #ifndef OSPATHALT |
| 241 | /* Other path separator characters. */ |
| 242 | #define OSPATHALT "" |
| 243 | #endif |
| 244 | |
| 245 | #ifndef OSPATHURL |
| 246 | /* Path separator characters for URL conversions. */ |
| 247 | #define OSPATHURL "/" |
| 248 | #endif |
| 249 | |
| 250 | #ifndef OSPATHCHAR |
| 251 | /* Normal path separator character. */ |
| 252 | #define OSPATHCHAR '/' |
| 253 | #endif |
| 254 | |
| 255 | #ifndef OSPATHPWD |
| 256 | /* Generic name for current working directory. */ |
| 257 | #define OSPATHPWD "." |
| 258 | #endif |
| 259 | |
| 260 | #ifndef OS_NEWLINE_SEQ |
| 261 | /* ASCII string giving the local newline sequence to write on output. */ |
| 262 | #define OS_NEWLINE_SEQ "\n" |
| 263 | #endif |
| 264 | |
| 265 | /* Directory separator for PATH-style environment variables. */ |
| 266 | #define OSPATHSEP ':' |
| 267 | #endif |
| 268 | |
| 269 | /* File handle structure for osfxxx functions. */ |
| 270 | typedef FILE osfildef; |
| 271 | |
| 272 | #ifdef _WIN32 |
| 273 | struct Win32Dir { |
| 274 | HANDLE hFindFile; |
| 275 | char *dirname; |
| 276 | WIN32_FIND_DATAA findFileData; |
| 277 | }; |
| 278 | typedef struct Win32Dir *osdirhdl_t; |
| 279 | #else /* posix */ |
| 280 | /* Directory handle for searches via os_open_dir() et al. */ |
| 281 | typedef DIR* osdirhdl_t; |
| 282 | #endif |
| 283 | |
| 284 | /* file type/mode bits */ |
| 285 | #define OSFMODE_FILE S_IFREG |
| 286 | #define OSFMODE_DIR S_IFDIR |
| 287 | #define OSFMODE_CHAR S_IFCHR |
| 288 | #ifdef S_IFBLK |
| 289 | #define OSFMODE_BLK S_IFBLK |
| 290 | #else |
| 291 | #define OSFMODE_BLK 0 |
| 292 | #endif |
| 293 | #ifdef S_IFIFO |
| 294 | #define OSFMODE_PIPE S_IFIFO |
| 295 | #else |
| 296 | #define OSFMODE_PIPE 0 |
| 297 | #endif |
| 298 | #ifdef S_IFLNK |
| 299 | #define OSFMODE_LINK S_IFLNK |
| 300 | #else |
| 301 | #define OSFMODE_LINK 0 |
| 302 | #endif |
| 303 | #ifdef S_IFSOCK |
| 304 | #define OSFMODE_SOCKET S_IFSOCK |
| 305 | #else |
| 306 | #define OSFMODE_SOCKET 0 |
| 307 | #endif |
| 308 | |
| 309 | /* File attribute bits. */ |
| 310 | #define OSFATTR_HIDDEN 0x0001 |
| 311 | #define OSFATTR_SYSTEM 0x0002 |
| 312 | #define OSFATTR_READ 0x0004 |
| 313 | #define OSFATTR_WRITE 0x0008 |
| 314 | |
| 315 | /* Get a file's stat() type. */ |
| 316 | int osfmode( const char* fname, int follow_links, unsigned long* mode, |
| 317 | unsigned long* attr ); |
| 318 | |
| 319 | #if 0 |
| 320 | /* The maximum width of a line of text. |
| 321 | * |
| 322 | * We ignore this, but the base code needs it defined. If the |
| 323 | * interpreter is run inside a console or terminal with more columns |
| 324 | * than the value defined here, weird things will happen, so we go safe |
| 325 | * and use a large value. */ |
| 326 | #define OS_MAXWIDTH 255 |
| 327 | #endif |
| 328 | |
| 329 | /* Disable the Tads swap file; computers have plenty of RAM these days. |
| 330 | */ |
| 331 | #define OS_DEFAULT_SWAP_ENABLED 0 |
| 332 | |
| 333 | /* TADS 2 macro/function configuration. Modern configurations always |
| 334 | * use the no-macro versions, so these definitions should always be set |
| 335 | * as shown below. */ |
| 336 | #define OS_MCM_NO_MACRO |
| 337 | #define ERR_NO_MACRO |
| 338 | |
| 339 | #if 0 |
| 340 | /* Not really needed; just a dummy. */ |
| 341 | #define OS_TR_USAGE "usage: frob [options] file" |
| 342 | |
| 343 | /* TADS 2 compiler usage message. We should actually use the name the |
| 344 | * user used to invoke the compiler, but the base code doesn't provide |
| 345 | * for that. */ |
| 346 | #define OS_TC_USAGE "Usage: tadsc [options] file" |
| 347 | |
| 348 | /* File timestamp type. |
| 349 | * |
| 350 | * Needed by the TADS 3 compiler. */ |
| 351 | struct os_file_time_t { |
| 352 | time_t t; |
| 353 | }; |
| 354 | #endif |
| 355 | |
| 356 | /* These values are used for the "mode" parameter of osfseek() to |
| 357 | * indicate where to seek in the file. */ |
| 358 | #define OSFSK_SET SEEK_SET /* Set position relative to the start of the file. */ |
| 359 | #define OSFSK_CUR SEEK_CUR /* Set position relative to the current file position. */ |
| 360 | #define OSFSK_END SEEK_END /* Set position relative to the end of the file. */ |
| 361 | |
| 362 | |
| 363 | /* ============= Functions follow ================ */ |
| 364 | |
| 365 | /* Allocate a block of memory of the given size in bytes. */ |
| 366 | #define osmalloc malloc |
| 367 | |
| 368 | /* Free memory previously allocated with osmalloc(). */ |
| 369 | #define osfree free |
| 370 | |
| 371 | /* Reallocate memory previously allocated with osmalloc() or osrealloc(), |
| 372 | * changing the block's size to the given number of bytes. */ |
| 373 | #define osrealloc realloc |
| 374 | |
| 375 | /* Open text file for reading. */ |
| 376 | #define osfoprt(fname,typ) (fopen((fname),"r")) |
| 377 | |
| 378 | /* Open text file for writing. */ |
| 379 | #define osfopwt(fname,typ) (fopen((fname),"w")) |
| 380 | |
| 381 | /* Open text file for reading and writing, keeping the file's existing |
| 382 | * contents if the file already exists or creating a new file if no |
| 383 | * such file exists. */ |
| 384 | osfildef* |
| 385 | osfoprwt( const char* fname, os_filetype_t typ ); |
| 386 | |
| 387 | /* Open text file for reading/writing. If the file already exists, |
| 388 | * truncate the existing contents. Create a new file if it doesn't |
| 389 | * already exist. */ |
| 390 | #define osfoprwtt(fname,typ) (fopen((fname),"w+")) |
| 391 | |
| 392 | /* Open binary file for writing. */ |
| 393 | #define osfopwb(fname,typ) (fopen((fname),"wb")) |
| 394 | |
| 395 | /* Open source file for reading - use the appropriate text or binary |
| 396 | * mode. */ |
| 397 | #define osfoprs osfoprt |
| 398 | |
| 399 | /* Open binary file for reading. */ |
| 400 | #define osfoprb(fname,typ) (fopen((fname),"rb")) |
| 401 | |
| 402 | /* Open binary file for reading/writing. If the file already exists, |
| 403 | * keep the existing contents. Create a new file if it doesn't already |
| 404 | * exist. */ |
| 405 | osfildef* |
| 406 | osfoprwb( const char* fname, os_filetype_t typ ); |
| 407 | |
| 408 | /* Open binary file for reading/writing. If the file already exists, |
| 409 | * truncate the existing contents. Create a new file if it doesn't |
| 410 | * already exist. */ |
| 411 | #define osfoprwtb(fname,typ) (fopen((fname),"w+b")) |
| 412 | |
| 413 | /* Get a line of text from a text file. */ |
| 414 | #define osfgets fgets |
| 415 | |
| 416 | /* Write a line of text to a text file. */ |
| 417 | #define osfputs fputs |
| 418 | |
| 419 | /* Write bytes to file. */ |
| 420 | #define osfwb(fp,buf,bufl) (fwrite((buf),(bufl),1,(fp))!=1) |
| 421 | |
| 422 | /* Flush buffered writes to a file. */ |
| 423 | #define osfflush fflush |
| 424 | |
| 425 | /* Read bytes from file. */ |
| 426 | #define osfrb(fp,buf,bufl) (fread((buf),(bufl),1,(fp))!=1) |
| 427 | |
| 428 | /* Read bytes from file and return the number of bytes read. */ |
| 429 | #define osfrbc(fp,buf,bufl) (fread((buf),1,(bufl),(fp))) |
| 430 | |
| 431 | /* Get the current seek location in the file. */ |
| 432 | #define osfpos ftell |
| 433 | |
| 434 | /* Seek to a location in the file. */ |
| 435 | #define osfseek fseek |
| 436 | |
| 437 | /* Close a file. */ |
| 438 | #define osfcls fclose |
| 439 | |
| 440 | /* Delete a file. */ |
| 441 | #define osfdel remove |
| 442 | |
| 443 | /* Access a file - determine if the file exists. |
| 444 | * |
| 445 | * We map this to the access() function. It should be available in |
| 446 | * virtually every system out there, as it appears in many standards |
| 447 | * (SVID, AT&T, POSIX, X/OPEN, BSD 4.3, DOS, MS Windows, maybe more). */ |
| 448 | #define osfacc(fname) (access((fname), F_OK)) |
| 449 | |
| 450 | /* Rename a file. */ |
| 451 | #define os_rename_file(from, to) (rename(from, to) == 0) |
| 452 | |
| 453 | /* Get a file's stat() type. */ |
| 454 | struct os_file_stat_t; |
| 455 | int os_file_stat( const char* fname, int follow_links, |
| 456 | struct os_file_stat_t* s ); |
| 457 | |
| 458 | /* Get a character from a file. */ |
| 459 | #define osfgetc fgetc |
| 460 | |
| 461 | /* Set busy cursor. |
| 462 | * |
| 463 | * We don't have a mouse cursor so there's no need to implement this. */ |
| 464 | #define os_csr_busy(a) |
| 465 | |
| 466 | /* Update progress display. |
| 467 | * |
| 468 | * We don't provide any kind of "compilation progress display", so we |
| 469 | * just define this as an empty macro. |
| 470 | */ |
| 471 | #define os_progress(fname,linenum) |
| 472 | |
| 473 | /* Initialize the time zone. |
| 474 | * |
| 475 | * We don't need this (I think). */ |
| 476 | #define os_tzset() |
| 477 | |
| 478 | #if 0 |
| 479 | /* Allocating sprintf/vsprintf. NB: These definitions are |
| 480 | * placeholders. asprintf() and vasprintf() are GNU/BSD extensions |
| 481 | * that aren't available on all Unix variants. Eventually these will |
| 482 | * need to be replaced with more thorough versions that encompass |
| 483 | * other Unix variants. */ |
| 484 | int asprintf(char **strp, const char *fmt, ...); |
| 485 | int vasprintf(char **strp, const char *fmt, va_list ap); |
| 486 | #define os_asprintf asprintf |
| 487 | #define os_vasprintf vasprintf |
| 488 | |
| 489 | /* |
| 490 | * Thread local storage declaration keyword. This is only needed on |
| 491 | * platforms where we implement multithreading, and then only in builds |
| 492 | * that actually use threading; at the moment, this is limited to the |
| 493 | * network-enabled TADS 3 interpreter. |
| 494 | */ |
| 495 | #ifdef OS_DECLARATIVE_TLS |
| 496 | #define OS_DECL_TLS(typ, varname) TLS typ varname |
| 497 | #endif |
| 498 | #endif |
| 499 | |
| 500 | #endif /* OSFROBTADS_H */ |