| 1 | /* $Header: d:/cvsroot/tads/TADS2/osifc.h,v 1.3 1999/07/11 00:46:34 MJRoberts Exp $ */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 1997, 2002 Michael J. Roberts. All Rights Reserved. |
| 5 | * |
| 6 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 7 | * on using and copying this software. |
| 8 | */ |
| 9 | /* |
| 10 | Name |
| 11 | osifc.h - portable interfaces to OS-specific functions |
| 12 | Function |
| 13 | This file defines interfaces to certain functions that must be called |
| 14 | from portable code, but which must have system-specific implementations. |
| 15 | Notes |
| 16 | DO NOT MODIFY THIS FILE WITH PLATFORM-SPECIFIC DEFINITIONS. If you |
| 17 | wish to add definitions for your platform, add them to the osxxx.h |
| 18 | file specific to your platform. Note that your osxxx.h file is always |
| 19 | included *before* this file, so items in your osxxx.h file are already |
| 20 | defined by the time this file is seen by the compiler. |
| 21 | |
| 22 | To port TADS to a new platform, you should go through this entire file |
| 23 | and provide a definition for each documented macro, typedef, and |
| 24 | function, and you should provide an implementation for each function |
| 25 | prototype. Each prototype provides a portable interface, so it is |
| 26 | the same on all platforms, but each platform requires its own custom |
| 27 | implementation. Put your definitions in your osxxx.h header file; put |
| 28 | your function implementations in your osxxx.c file or files. |
| 29 | |
| 30 | You should not change any macro or typedef that is actually #define'd |
| 31 | in this file. Those definitions are part of the portable interface, |
| 32 | not part of the platform-specific implementation. |
| 33 | |
| 34 | Certain functions are merely documented here, but no prototypes are |
| 35 | provided. For these functions, most platforms use #define macros to |
| 36 | implement the functions in terms of standard C library functions or |
| 37 | OS API functions; we do not provide prototypes for these functions so |
| 38 | that the OS implementation can call the C library or OS API functions |
| 39 | directly through a macro, avoiding an unnecessary extra function call. |
| 40 | However, if you must provide an implementation for these functions, |
| 41 | you can provide your own prototypes for them in your osxxx.h header |
| 42 | file. |
| 43 | |
| 44 | SEE ALSO osifctyp.h, which contains definitions for some of the |
| 45 | interface datatypes. |
| 46 | |
| 47 | Modified |
| 48 | 04/04/99 CNebel - Improve const-ness; use new appctx.h header. |
| 49 | 12/05/97 MJRoberts - Creation |
| 50 | */ |
| 51 | |
| 52 | #ifndef OSIFC_H |
| 53 | #define OSIFC_H |
| 54 | |
| 55 | #include <stdlib.h> |
| 56 | #include <stdarg.h> |
| 57 | #include "appctx.h" |
| 58 | |
| 59 | #ifdef __cplusplus |
| 60 | extern "C" { |
| 61 | #endif |
| 62 | |
| 63 | /* ------------------------------------------------------------------------ */ |
| 64 | /* |
| 65 | * A note on character sets: |
| 66 | * |
| 67 | * Except where noted, all character strings passed to and from the |
| 68 | * osxxx functions defined herein use the local operating system |
| 69 | * representation. On a Windows machine localized to Eastern Europe, |
| 70 | * for example, the character strings passed to and from the osxxx |
| 71 | * functions would use single-byte characters in the Windows code page |
| 72 | * 1250 representation. |
| 73 | * |
| 74 | * Callers that use multiple character sets must implement mappings to |
| 75 | * and from the local character set when calling the osxxx functions. |
| 76 | * The osxxx implementations are thus free to ignore any issues related |
| 77 | * to character set conversion or mapping. |
| 78 | * |
| 79 | * The osxxx implementations are specifically not permitted to use |
| 80 | * double-byte Unicode as the native character set, nor any other |
| 81 | * character set where a null byte could appear as part of a non-null |
| 82 | * character. In particular, callers may assume that null-terminated |
| 83 | * strings passed to and from the osxxx functions contain no embedded |
| 84 | * null bytes. Multi-byte character sets (i.e., character sets with |
| 85 | * mixed single-byte and double-byte characters) may be used as long as |
| 86 | * a null byte is never part of any multi-byte character, since this |
| 87 | * would guarantee that a null byte could always be taken as a null |
| 88 | * character without knowledge of the encoding or context. |
| 89 | */ |
| 90 | |
| 91 | /* ------------------------------------------------------------------------ */ |
| 92 | /* |
| 93 | * "Far" Pointers. Most platforms can ignore this. For platforms with |
| 94 | * mixed-mode addressing models, where pointers of different sizes can |
| 95 | * be used within a single program and hence some pointers require |
| 96 | * qualification to indicate that they use a non-default addressing |
| 97 | * model, the keyword OSFAR should be defined to the appropriate |
| 98 | * compiler-specific extension keyword. |
| 99 | * |
| 100 | * If you don't know what I'm talking about here, you should just ignore |
| 101 | * it, because your platform probably doesn't have anything this |
| 102 | * sinister. As of this writing, this applies only to MS-DOS, and then |
| 103 | * only to 16-bit implementations that must interact with other 16-bit |
| 104 | * programs via dynamic linking or other mechanisms. |
| 105 | */ |
| 106 | |
| 107 | |
| 108 | /* ------------------------------------------------------------------------ */ |
| 109 | /* |
| 110 | * ANSI C99 exact-size integer types. |
| 111 | * |
| 112 | * C99 defines a set of integer types with exact bit sizes, named intXX_t |
| 113 | * for a signed integer with XX bits, and uintXX_t for unsigned. XX can be |
| 114 | * 8, 16, 32, or 64. TADS uses the 16- and 32-bit sizes, so each platform |
| 115 | * is responsible for defining the following types: |
| 116 | * |
| 117 | *. int16_t - a signed integer type storing EXACTLY 16 bits |
| 118 | *. uint16_t - an unsigned integer type storing EXACTLY 16 bits |
| 119 | *. int32_t - a signed integer type storing EXACTLY 32 bits |
| 120 | *. uint32_t - an unsigned integer type storing EXACTLY 32 bits |
| 121 | * |
| 122 | * Many modern compilers provide definitions for these types via the |
| 123 | * standard header stdint.h. Where stdint.h is provided, the platform code |
| 124 | * can merely #include <stdint.h>. |
| 125 | * |
| 126 | * For compilers where stdint.h isn't available, you must provide suitable |
| 127 | * typedefs. Note that the types must be defined with the exact bit sizes |
| 128 | * specified; it's not sufficient to use a bigger type, because we depend |
| 129 | * in some cases on overflow and sign extension behavior at the specific |
| 130 | * bit size. |
| 131 | */ |
| 132 | |
| 133 | |
| 134 | /* ------------------------------------------------------------------------ */ |
| 135 | /* |
| 136 | * Thread-local storage (TLS). |
| 137 | * |
| 138 | * When TADS is compiled with threading support, it requires some variables |
| 139 | * to be "thread-local". This means that the variables have global scope |
| 140 | * (so they're not stored in "auto" variables on the stack), but each |
| 141 | * thread has a private copy of each such variable. |
| 142 | * |
| 143 | * Nearly all systems that support threads also support thread-local |
| 144 | * storage. Like threading support itself, though, TLS support is at |
| 145 | * present implemented only in non-portable OS APIs rather than standard C |
| 146 | * language features. TLS is a requirement if TADS is compiled with |
| 147 | * threading, but it's not needed for non-threaded builds. TADS only |
| 148 | * requires threading at present (version 3.1) for its network features; |
| 149 | * since these features are optional, systems that don't have threading and |
| 150 | * TLS support will simply need to disable the network features, which will |
| 151 | * allow all of the threading and TLS definitions in osifc to be omitted. |
| 152 | * |
| 153 | * There appear to be two common styles of TLS programming models. The |
| 154 | * first provides non-standard compiler syntax for declarative creation of |
| 155 | * thread-local variables. The Microsoft (on Windows) and Gnu compilers |
| 156 | * (on Linux and Unix) do this: they provide custom storage class modifiers |
| 157 | * for declaring thread locals (__declspec(thread) for MSVC, __thread for |
| 158 | * gcc). Compilers that support declarative thread locals handle the |
| 159 | * implementation details through code generation, so the program merely |
| 160 | * needs to add the special TLS storage class qualifier to an otherwise |
| 161 | * ordinary global variable declaration, and then can access the thread |
| 162 | * local as though it were an ordinary global. |
| 163 | * |
| 164 | * The second programming model is via explicit OS API calls to create, |
| 165 | * initialize, and access thread locals. pthreads provides such an API, as |
| 166 | * does Win32. In fact, when you use the declarative syntax with MSVC or |
| 167 | * gcc, the compiler generates the appropriate API calls, but the details |
| 168 | * are transparent to the program; in contrast, when using pthreads |
| 169 | * directly, the program must actively call the relevant APIs. |
| 170 | * |
| 171 | * It's probably the case that every system that has compiler-level support |
| 172 | * for declarative thread local creation also has procedural APIs, so the |
| 173 | * simplest way to abstract the platform differences would be to do |
| 174 | * everything in terms of APIs. However, it seems likely that compilers |
| 175 | * with declarative syntax might be able to generate more efficient code, |
| 176 | * since optimizers always benefit from declarative information. So we'd |
| 177 | * like to use declarative syntax whenever it's available, but fall back on |
| 178 | * explicit API calls when it's not. So our programming model is a union |
| 179 | * of the two styles: |
| 180 | * |
| 181 | * 1. For each thread local, declare the thread local: |
| 182 | *. OS_DECL_TLS(char *, my_local); |
| 183 | * |
| 184 | * 2. At main program startup (for the main thread only), initialize each |
| 185 | * thread local: |
| 186 | *. os_tls_create(my_local); |
| 187 | * |
| 188 | * 3. Never get or set the value of a thread local directly; instead, use |
| 189 | * the get/set functions: |
| 190 | *. char *x = os_tls_get(char *, my_local); |
| 191 | *. os_tls_set(my_local, "hello"); |
| 192 | * |
| 193 | * One key feature of this implementation is that each thread local is |
| 194 | * stored as a (void *) value. We do it this way to allow a simple direct |
| 195 | * mapping to the pthreads APIs, since that's going to be the most common |
| 196 | * non-declarative implementation. This means that a thread local variable |
| 197 | * can contain any pointer type, but *only* a pointer type. The standard |
| 198 | * pattern for dealing with anything more ocmplex is the same as in |
| 199 | * pthreads: gather up the data into a structure, malloc() an instance of |
| 200 | * that structure at entry to each thread (including the main thread), and |
| 201 | * os_tls_set() the variable to contain a pointer to that structure. From |
| 202 | * then on, use os_tls_set(my_struct *, my_local)->member to access the |
| 203 | * member variables in the structure. And finally, each thread must delete |
| 204 | * the structure at thread exit. |
| 205 | */ |
| 206 | |
| 207 | /* |
| 208 | * |
| 209 | * Declare a thread local. |
| 210 | * |
| 211 | * - For compilers that support declarative TLS variables, the local OS |
| 212 | * headers should use the compiler support by #defining OS_DECL_TLS to the |
| 213 | * appropriate local declarative keyword. |
| 214 | * |
| 215 | * - For systems without declarative TLS support but with TLS APIs, the |
| 216 | * global declared by this macro actually stores the slot ID (what pthreads |
| 217 | * calls the "key") for the variable. This macro should therefore expand |
| 218 | * to a declaration of the appropriate API type for a slot ID; for example, |
| 219 | * on pthreads, #define OS_DECL_TLS(t, v) pthread_key_t v. |
| 220 | * |
| 221 | * - For builds with no thread support, simply #define this to declare the |
| 222 | * variable as an ordinary global: #define OS_DECL_TLS(t, v) t v. |
| 223 | */ |
| 224 | /* #define OS_DECL_TLS(typ, varname) __thread typ varname */ |
| 225 | |
| 226 | /* |
| 227 | * For API-based systems without declarative support in the compiler, the |
| 228 | * main program startup code must explicitly create a slot for each thread- |
| 229 | * local variable by calling os_tls_create(). The API returns a slot ID, |
| 230 | * which is shared among threads and therefore can be stored in an ordinary |
| 231 | * global variable. OS_DECL_TLS will have declared the global variable |
| 232 | * name in this case as an ordinary global of the slot ID type. The |
| 233 | * os_tls_create() macro should therefore expand to a call to the slot |
| 234 | * creation API, storing the new slot ID in the global. |
| 235 | * |
| 236 | * Correspondingly, before the main thread exits, it should delete each |
| 237 | * slot it created, b calling os_tls_delete(). |
| 238 | * |
| 239 | * For declarative systems, there's no action required here, so these |
| 240 | * macros can be defined to empty. |
| 241 | */ |
| 242 | /* #define os_tls_create(varname) pthread_key_create(&varname, NULL) */ |
| 243 | /* #define os_tls_delete(varname) pthread_key_delete(varname) */ |
| 244 | |
| 245 | |
| 246 | /* |
| 247 | * On API-based systems, each access to get or set the thread local |
| 248 | * requires an API call, using the slot ID stored in the actual global to |
| 249 | * get the per-thread instance of the variable's storage. |
| 250 | *. #define os_tls_get(typ, varname) ((typ)pthread_getspecific(varname)) |
| 251 | *. #define os_tls_set(varname, val) pthread_setspecific(varname, val) |
| 252 | * |
| 253 | * On declarative systems, the global variable itself is the thread local, |
| 254 | * so get/set can be implemented as direct access to the variable. |
| 255 | *. #define os_tls_get(typ, varname) varname |
| 256 | *. #define os_tls_set(varname, val) varname = (val) |
| 257 | */ |
| 258 | |
| 259 | /* |
| 260 | * Common TLS definitions - declarative thread locals |
| 261 | * |
| 262 | * For systems with declarative TLS support in the compiler, the OS header |
| 263 | * can #define OS_DECLARATIVE_TLS to pick up suitable definitions for the |
| 264 | * os_tls_xxx() macros. The OS header must separately define OS_DECL_TLS |
| 265 | * as appropriate for the local system. |
| 266 | */ |
| 267 | #ifdef OS_DECLARATIVE_TLS |
| 268 | #define os_tls_create(varname) |
| 269 | #define os_tls_delete(varname) |
| 270 | #define os_tls_get(typ, varname) varname |
| 271 | #define os_tls_set(varname, val) varname = (val) |
| 272 | #endif |
| 273 | |
| 274 | /* |
| 275 | * Common TLS definitions - pthreads |
| 276 | * |
| 277 | * For pthreads systems without declarative TLS support in the compiler, |
| 278 | * the OS header can simply #define OS_PTHREAD_TLS to pick up the standard |
| 279 | * definitions below. |
| 280 | */ |
| 281 | #ifdef OS_PTHREAD_TLS |
| 282 | #include <pthread.h> |
| 283 | #define OS_DECL_TLS(typ, varname) pthread_key_t varname |
| 284 | #define os_tls_create(varname) pthread_key_create(&varname, NULL) |
| 285 | #define os_tls_delete(varname) pthread_key_delete(varname) |
| 286 | #define os_tls_get(typ, varname) ((typ)pthread_getspecific(varname)) |
| 287 | #define os_tls_set(varname, val) pthread_setspecific(varname, val) |
| 288 | #endif |
| 289 | |
| 290 | |
| 291 | /* ------------------------------------------------------------------------ */ |
| 292 | /* |
| 293 | * <time.h> definitions. |
| 294 | * |
| 295 | * os_time() should act like Unix time(), returning the number of seconds |
| 296 | * elapsed since January 1, 1970 at midnight UTC. |
| 297 | * |
| 298 | * The original Unix <time.h> package defined time_t as a 32-bit signed |
| 299 | * int, and many subsequent C compilers on other platforms followed suit. |
| 300 | * A signed 32-bit time_t has the well-known year-2038 problem; some later |
| 301 | * C compilers tried to improve matters by using an unsigned 32-bit time_t |
| 302 | * instead, but for many purposes this is even worse since it can't |
| 303 | * represent any date before 1/1/1970. *Most* modern compilers solve the |
| 304 | * problem once and for all (for 300 billion years in either direction of |
| 305 | * 1/1/1970, anyway - enough to represent literally all of eternity in most |
| 306 | * current cosmological models) by defining time_t as a signed 64-bit int. |
| 307 | * But some compilers stubbornly stick to the old 32-bit time_t even in |
| 308 | * newer versions, for the sake of compatibility with older code that might |
| 309 | * be lax about mixing time_t's with ordinary int's. E.g., MSVC2003 does |
| 310 | * this. Fortunately, some of these compilers (such as MSVC2003 again) |
| 311 | * also define a parallel, transitional set of 64-bit time functions that |
| 312 | * you can use by replacing all references to the standard time_t and |
| 313 | * related names with the corresponding 64-bit names. |
| 314 | * |
| 315 | * We'd really like to use a 64-bit time_t wherever we can - the TADS |
| 316 | * release cycle can be a bit slow, and we don't want 2038 to sneak up on |
| 317 | * us and catch us unawares. So for those compilers that offer a choice of |
| 318 | * 32 or 64 bits, we'd like to select the 64 bit version. To facilitate |
| 319 | * this, we define covers here for the time.h types and functions that we |
| 320 | * use. On platforms where the regular time_t is already 64 bits, or where |
| 321 | * there's no 64-bit option at all, you can simply do nothing - the |
| 322 | * defaults defined here use the standard time_t typedef and functions, so |
| 323 | * that's what you'll get if you don't define these in the OS-specific |
| 324 | * headers for your platform. For compilers that provide both a 32-bit |
| 325 | * time_t and a 64-bit other_time_t, the OS headers should #define these |
| 326 | * macros in terms of those compiler-specific 64-bit names. |
| 327 | */ |
| 328 | #ifndef os_time_t |
| 329 | # define os_time_t time_t |
| 330 | # define os_gmtime(t) gmtime(t) |
| 331 | # define os_localtime(t) localtime(t) |
| 332 | # define os_time(t) time(t) |
| 333 | #endif |
| 334 | |
| 335 | /* |
| 336 | * Initialize the time zone. This routine is meant to take care of any |
| 337 | * work that needs to be done prior to calling localtime() and other |
| 338 | * time-zone-dependent routines in the run-time library. For DOS and |
| 339 | * Windows, we need to call the run-time library routine tzset() to set up |
| 340 | * the time zone from the environment; most systems shouldn't need to do |
| 341 | * anything in this routine. It's sufficient to call this once during the |
| 342 | * process lifetime, since it's meant to perform static initialization that |
| 343 | * lasts as long as the process is running. |
| 344 | */ |
| 345 | #ifndef os_tzset |
| 346 | void os_tzset(void); |
| 347 | #endif |
| 348 | |
| 349 | /* |
| 350 | * Higher-precision time. This retrieves the same time information as |
| 351 | * os_time() (i.e., the elapsed time since the standard Unix Epoch, January |
| 352 | * 1, 1970 at midnight UTC), but retrieves it with the highest precision |
| 353 | * available on the local system, up to nanosecond precision. If less |
| 354 | * precision is available, that's fine; just return the time to the best |
| 355 | * precision available, but expressed in terms of the number of |
| 356 | * nanoseconds. For example, if you can retrieve milliseconds, you can |
| 357 | * convert that to nanoseconds by multiplying by 1,000,000. |
| 358 | * |
| 359 | * On return, fills in '*seconds' with the number of whole seconds since |
| 360 | * the Epoch, and fills in '*nanoseconds' with the fractional portion, |
| 361 | * expressed in nanosceconds. Note that '*nanoseconds' is merely the |
| 362 | * fractional portion of the time, so 0 <= *nanoseconds < 1000000000. |
| 363 | */ |
| 364 | void os_time_ns(os_time_t *seconds, long *nanoseconds); |
| 365 | |
| 366 | /* |
| 367 | * Get the local time zone name, as a location name in the IANA zoneinfo |
| 368 | * database. For example, locations using US Pacific Time should return |
| 369 | * "America/Los_Angeles". |
| 370 | * |
| 371 | * Returns true if successful, false if not. If the local operating system |
| 372 | * doesn't have a way to obtain this information, or if it's not available |
| 373 | * in the local machine's configuration, this returns false. |
| 374 | * |
| 375 | * The zoneinfo database is also known as the Olson or TZ (timezone) |
| 376 | * database; it's widely used on Unix systems as the definitive source of |
| 377 | * local time zone settings. See http://www.iana.org/time-zones for more |
| 378 | * information. |
| 379 | * |
| 380 | * On many Unix systems, the TZ environment variable contains the zoneinfo |
| 381 | * zone name when its first character is ':'. Windows uses a proprietary |
| 382 | * list of time zone names that can be mapped to zoneinfo names via a |
| 383 | * hand-coded list (such a list is maintained in the Unicode CLDR; our |
| 384 | * Windows implementation uses the CLDR list to generate the mapping). |
| 385 | * MacOS X uses zoneinfo keys directly; /etc/localtime is a link to the |
| 386 | * zoneinfo file for the local zone as set via the system preferences. |
| 387 | * |
| 388 | * os_tzset() must be invoked at some point before this routine is called. |
| 389 | */ |
| 390 | int os_get_zoneinfo_key(char *buf, size_t buflen); |
| 391 | |
| 392 | /* |
| 393 | * Get a description of the local time zone. Fills in '*info' with the |
| 394 | * available information. Returns true on success, false on failure. |
| 395 | * |
| 396 | * See osstzprs.h/.c for a portable implementation of a parser for |
| 397 | * POSIX-style TZ strings. That can serve as a full implementation of this |
| 398 | * function for systems that use the POSIX TZ environment variable syntax |
| 399 | * to specify the timezone. (That routine simply parses a string from any |
| 400 | * source, so it can be used to parse the TZ syntax even on systems where |
| 401 | * the string comes from somewhere other than the TZ environment variable.) |
| 402 | * |
| 403 | * os_tzset() must be invoked at some point before this routine is called. |
| 404 | * |
| 405 | * The following two structures are used for the return information: |
| 406 | * |
| 407 | * os_tzrule_t - Timezone Rule structure. This describes a rule for an |
| 408 | * annual transition between daylight savings time and standard time in a |
| 409 | * time zone. Most timezones that have recurring standard/daylight changes |
| 410 | * require two of these rules, one for switching to daylight time in the |
| 411 | * spring and one for switching to standard time in the fall. |
| 412 | * |
| 413 | * os_tzinfo_t - Timezone Information structure. This describes a |
| 414 | * timezone's clock settings, name(s), and rules for recurring annual |
| 415 | * changes between standard time and daylight time, if applicable. |
| 416 | */ |
| 417 | struct os_tzrule_t |
| 418 | { |
| 419 | /* |
| 420 | * Day of year, 1-365, NEVER counting Feb 29; set to 0 if not used. |
| 421 | * Corresponds to the "J" format in Unix TZ strings. (Called "Julian |
| 422 | * day" in the POSIX docs, thus the "J", even though it's a bit of a |
| 423 | * misnomer.)(Because of the invariance of the mapping from J-number to |
| 424 | * date, this is just an obtuse way of specifying a month/day date. |
| 425 | * But even so, we'll let the OS layer relay this back to us in |
| 426 | * J-number format and count on the portable caller to work out the |
| 427 | * date, rather than foisting that work on each platform |
| 428 | * implementation.) |
| 429 | */ |
| 430 | int jday; |
| 431 | |
| 432 | /* |
| 433 | * Day of year, 1-366, counting Feb 29 on leap years; set to 0 if not |
| 434 | * used; ignored if 'jday' is nonzero. This corresponds to the Julian |
| 435 | * day sans "J" in TZ strings (almost - that TZ format uses 0-365 as |
| 436 | * its range, so bump it up by one when parsing a TZ string). This |
| 437 | * format is even more obtuse than the J-day format, in that it doesn't |
| 438 | * even have an invariant month/day mapping (not after day 59, anyway - |
| 439 | * day 60 is either February 29 or March 1, depending on the leapness |
| 440 | * of the year, and every day after that is similarly conditional). As |
| 441 | * far as I can tell, no one uses this option, so I'm not sure why it |
| 442 | * exists. The zoneinfo source format doesn't have a way to represent |
| 443 | * it, which says to me that no one has ever used it in a statutory DST |
| 444 | * start/end date definition in the whole history of time zones around |
| 445 | * the world, since the whole history of time zones around the world is |
| 446 | * exactly what the zoneinfo database captures in exhaustive and |
| 447 | * painstaking detail. If anyone had ever used it in defining a time |
| 448 | * zone, zoneinfo would have an option for it. My guess is that it's a |
| 449 | * fossilized bug from some early C RTL that's been retained out of an |
| 450 | * abundance of caution vis-a-vis compatibility, and was entirely |
| 451 | * replaced in practice by the J-number format as soon as someone |
| 452 | * noticed the fiddly leap year behavior. But for the sake of |
| 453 | * completeness... |
| 454 | */ |
| 455 | int yday; |
| 456 | |
| 457 | /* |
| 458 | * The month (1-12), week of the month, and day of the week (1-7 for |
| 459 | * Sunday to Saturday). Week 1 is the first week in which 'day' |
| 460 | * occurs, week 2 is the second, etc.; week 5 is the last occurrence of |
| 461 | * 'day' in the month. These fields are used for "second Sunday in |
| 462 | * March" types of rules. Set these to zero if they're not used; |
| 463 | * they're ignored in any case if 'jday' or 'yday' are non-zero. |
| 464 | */ |
| 465 | int month; |
| 466 | int week; |
| 467 | int day; |
| 468 | |
| 469 | /* time of day, in seconds after midnight (e.g., 2AM is 120 == 2*60*60) */ |
| 470 | int time; |
| 471 | }; |
| 472 | struct os_tzinfo_t |
| 473 | { |
| 474 | /* |
| 475 | * The local offset from GMT, in seconds, for standard time and |
| 476 | * daylight time in this zone. These values are positive for zones |
| 477 | * east of GMT and negative for zones west: New York standard time |
| 478 | * (EST) is 5 hours west of GMT, so its offset is -5*60*60. |
| 479 | * |
| 480 | * Set both of these fields (if possible) regardless of whether |
| 481 | * standard or daylight time is currently in effect in the zone. The |
| 482 | * caller will select which offset to use based on the start/end rules, |
| 483 | * or based on the 'is_dst' flag if no rules are available. |
| 484 | * |
| 485 | * If it's only possible to determine the current wall clock offset, be |
| 486 | * it standard or daylight time, and it's not possible to determine the |
| 487 | * time difference between the two, simply set both of these to the |
| 488 | * current offset. This information isn't available from the standard |
| 489 | * C library, and many OS APIs also lack it. |
| 490 | */ |
| 491 | int32_t std_ofs; |
| 492 | int32_t dst_ofs; |
| 493 | |
| 494 | /* |
| 495 | * The abbreviations for the local zone's standard time and daylight |
| 496 | * time, respectively, when displaying date/time values. E.g., "EST" |
| 497 | * and "EDT" for US Eastern Time. If the zone doesn't observe daylight |
| 498 | * time (it's on standard time year round), set dst_abbr to an empty |
| 499 | * string. |
| 500 | * |
| 501 | * As with std_ofs and dst_ofs, you can set both of these to the same |
| 502 | * string if it's only possible to determine the one that's currently |
| 503 | * in effect. |
| 504 | */ |
| 505 | char std_abbr[16]; |
| 506 | char dst_abbr[16]; |
| 507 | |
| 508 | /* |
| 509 | * The ongoing rules for switching between daylight and standard time |
| 510 | * in this zone, if available. 'dst_start' is the date when daylight |
| 511 | * savings starts, 'dst_end' is the date when standard time resumes. |
| 512 | * Set all fields to 0 if the start/stop dates aren't available, or the |
| 513 | * zone is on standard time year round. |
| 514 | */ |
| 515 | struct os_tzrule_t dst_start; |
| 516 | struct os_tzrule_t dst_end; |
| 517 | |
| 518 | /* |
| 519 | * True -> the zone is CURRENTLY on daylight savings time; false means |
| 520 | * it's currently on standard time. |
| 521 | * |
| 522 | * This is only used if the start/end rules aren't specified. In the |
| 523 | * absence of start/end rules, there's no way to know when the current |
| 524 | * standard/daylight phase ends, so we'll have to assume that the |
| 525 | * current mode is in effect permanently. In this case, the caller |
| 526 | * will use only be able to use the offset and abbreviation for the |
| 527 | * current mode and will have to ignore the other one. |
| 528 | */ |
| 529 | int is_dst; |
| 530 | }; |
| 531 | int os_get_timezone_info(struct os_tzinfo_t *info); |
| 532 | |
| 533 | |
| 534 | /* |
| 535 | * Get the current system high-precision timer. This function returns a |
| 536 | * value giving the wall-clock ("real") time in milliseconds, relative to |
| 537 | * any arbitrary zero point. It doesn't matter what this value is relative |
| 538 | * to -- the only important thing is that the values returned by two |
| 539 | * different calls should differ by the number of actual milliseconds that |
| 540 | * have elapsed between the two calls. This might be the number of |
| 541 | * milliseconds since the computer was booted, since the current user |
| 542 | * logged in, since midnight of the previous night, since the program |
| 543 | * started running, since 1-1-1970, etc - it doesn't matter what the epoch |
| 544 | * is, so the implementation can use whatever's convenient on the local |
| 545 | * system. |
| 546 | * |
| 547 | * True millisecond precision isn't required. Each implementation should |
| 548 | * simply use the best precision available on the system. If your system |
| 549 | * doesn't have any kind of high-precision clock, you can simply use the |
| 550 | * time() function and multiply the result by 1000 (but see the note below |
| 551 | * about exceeding 32-bit precision). |
| 552 | * |
| 553 | * However, it *is* required that the return value be in *units* of |
| 554 | * milliseconds, even if your system clock doesn't have that much |
| 555 | * precision; so on a system that uses its own internal clock units, this |
| 556 | * routine must multiply the clock units by the appropriate factor to yield |
| 557 | * milliseconds for the return value. |
| 558 | * |
| 559 | * It is also required that the values returned by this function be |
| 560 | * monotonically increasing. In other words, each subsequent call must |
| 561 | * return a value that is equal to or greater than the value returned from |
| 562 | * the last call. On some systems, you must be careful of two special |
| 563 | * situations. |
| 564 | * |
| 565 | * First, the system clock may "roll over" to zero at some point; for |
| 566 | * example, on some systems, the internal clock is reset to zero at |
| 567 | * midnight every night. If this happens, you should make sure that you |
| 568 | * apply a bias after a roll-over to make sure that the value returned from |
| 569 | * this return continues to increase despite the reset of the system clock. |
| 570 | * |
| 571 | * Second, a 32-bit signed number can only hold about twenty-three days |
| 572 | * worth of milliseconds. While it seems unlikely that a TADS game would |
| 573 | * run for 23 days without a break, it's certainly reasonable to expect |
| 574 | * that the computer itself may run this long without being rebooted. So, |
| 575 | * if your system uses some large type (a 64-bit number, for example) for |
| 576 | * its high-precision timer, you may want to store a zero point the very |
| 577 | * first time this function is called, and then always subtract this zero |
| 578 | * point from the large value returned by the system clock. If you're |
| 579 | * using time(0)*1000, you should use this technique, since the result of |
| 580 | * time(0)*1000 will almost certainly not fit in 32 bits in most cases. |
| 581 | */ |
| 582 | long os_get_sys_clock_ms(void); |
| 583 | |
| 584 | |
| 585 | /* ------------------------------------------------------------------------ */ |
| 586 | /* |
| 587 | * Hardware Configuration. Define the following functions appropriately |
| 588 | * for your hardware. For efficiency, these functions should be defined |
| 589 | * as macros if possible. |
| 590 | * |
| 591 | * Note that these hardware definitions are independent of the OS, at |
| 592 | * least to the extent that your OS can run on multiple types of |
| 593 | * hardware. So, rather than combining these definitions into your |
| 594 | * osxxx.h header file, we recommend that you put these definitions in a |
| 595 | * separate h_yyy.h header file, which can be configured into os.h with |
| 596 | * an appropriate "_M_yyy" preprocessor symbol. Refer to os.h for |
| 597 | * details of configuring the hardware include file. |
| 598 | */ |
| 599 | |
| 600 | /* |
| 601 | * Round a size up to worst-case alignment boundary. For example, on a |
| 602 | * platform where the largest type must be aligned on a 4-byte boundary, |
| 603 | * this should round the value up to the next higher mutliple of 4 and |
| 604 | * return the result. |
| 605 | */ |
| 606 | /* size_t osrndsz(size_t siz); */ |
| 607 | |
| 608 | /* |
| 609 | * Round a pointer up to worst-case alignment boundary. |
| 610 | */ |
| 611 | /* void *osrndpt(void *ptr); */ |
| 612 | |
| 613 | /* |
| 614 | * Read an unaligned portable unsigned 2-byte value, returning an int |
| 615 | * value. The portable representation has the least significant byte |
| 616 | * first, so the value 0x1234 is represented as the byte 0x34, followed |
| 617 | * by the byte 0x12. |
| 618 | * |
| 619 | * The source value must be treated as unsigned, but the result is |
| 620 | * signed. This is significant on 32- and 64-bit platforms, because it |
| 621 | * means that the source value should never be sign-extended to 32-bits. |
| 622 | * For example, if the source value is 0xffff, the result is 65535, not |
| 623 | * -1. |
| 624 | */ |
| 625 | /* int osrp2(unsigned char *p); */ |
| 626 | |
| 627 | /* |
| 628 | * Read an unaligned portable signed 2-byte value, returning int. This |
| 629 | * differs from osrp2() in that this function treats the source value as |
| 630 | * signed, and returns a signed result; hence, on 32- and 64-bit |
| 631 | * platforms, the result must be sign-extended to the int size. For |
| 632 | * example, if the source value is 0xffff, the result is -1. |
| 633 | */ |
| 634 | /* int osrp2s(unsigned char *p); */ |
| 635 | |
| 636 | /* |
| 637 | * Write unsigned int to unaligned portable 2-byte value. The portable |
| 638 | * representation stores the low-order byte first in memory, so |
| 639 | * oswp2(0x1234) should result in storing a byte value 0x34 in the first |
| 640 | * byte, and 0x12 in the second byte. |
| 641 | */ |
| 642 | /* void oswp2(unsigned char *p, unsigned int i); */ |
| 643 | |
| 644 | /* |
| 645 | * Write signed int to unaligned portable 2-byte value. Negative values |
| 646 | * must be stored in two's complement notation. E.g., -1 is stored as |
| 647 | * FF.FF, -32768 is stored as 00.80 (little-endian). |
| 648 | * |
| 649 | * Virtually all modern hardware uses two's complement notation as the |
| 650 | * native representation, which makes this routine a trivial synonym of |
| 651 | * osrp2() (i.e., #define oswp2s(p,i) oswp2(p,i)). We distinguish the |
| 652 | * signed version on the extremely off chance that TADS is ever ported to |
| 653 | * wacky hardware with a different representation for negative integers |
| 654 | * (one's complement, sign bit, etc). |
| 655 | */ |
| 656 | /* void oswp2s(unsigned char *p, int i); */ |
| 657 | |
| 658 | /* |
| 659 | * Read an unaligned unsigned portable 4-byte value, returning long. The |
| 660 | * underlying value should be considered signed, and the result is signed. |
| 661 | * The portable representation stores the bytes starting with the least |
| 662 | * significant: the value 0x12345678 is stored with 0x78 in the first byte, |
| 663 | * 0x56 in the second byte, 0x34 in the third byte, and 0x12 in the fourth |
| 664 | * byte. |
| 665 | */ |
| 666 | /* unsigned long osrp4(unsigned char *p); */ |
| 667 | |
| 668 | /* |
| 669 | * Read an unaligned signed portable 4-byte value, returning long. |
| 670 | */ |
| 671 | /* long osrp4s(unsigned char *p); */ |
| 672 | |
| 673 | /* |
| 674 | * Write an unsigned long to an unaligned portable 4-byte value. The |
| 675 | * portable representation stores the low-order byte first in memory, so |
| 676 | * 0x12345678 is written to memory as 0x78, 0x56, 0x34, 0x12. |
| 677 | */ |
| 678 | /* void oswp4(unsigned char *p, unsigned long l); */ |
| 679 | |
| 680 | /* |
| 681 | * Write a signed long, using little-endian byte order and two's complement |
| 682 | * notation for negative numbers. This is a trivial synonym for oswp4() |
| 683 | * for all platforms with native two's complement arithmetic (which is |
| 684 | * virtually all modern platforms). See oswp2s() for more discussion. |
| 685 | */ |
| 686 | /* void oswp4s(unsigned char *p, long l); */ |
| 687 | |
| 688 | /* |
| 689 | * For convenience and readability, the 1-byte integer (signed and |
| 690 | * unsigned) equivalents of the above. |
| 691 | */ |
| 692 | #define osrp1(p) (*(unsigned char *)(p)) |
| 693 | #define osrp1s(p) (*(signed char *)(p)) |
| 694 | #define oswp1(p, b) (*(unsigned char *)(p) = (b)) |
| 695 | #define oswp1s(p, b) (*(signed char *)(p) = (b)) |
| 696 | |
| 697 | |
| 698 | /* ------------------------------------------------------------------------ */ |
| 699 | /* |
| 700 | * varargs va_copy() extension. |
| 701 | * |
| 702 | * On some compilers, va_list is a reference type. This means that if a |
| 703 | * va_list value is passed to a function that uses va_arg() to step through |
| 704 | * the referenced arguments, the caller's copy of the va_list might be |
| 705 | * updated on return. This is problematic in cases where the caller needs |
| 706 | * to use the va_list again in another function call, since the va_list is |
| 707 | * no longer pointing to the first argument for the second call. C99 has a |
| 708 | * solution in the form of the va_copy() macro. Unfortunately, this isn't |
| 709 | * typically available in pre-C99 compilers, and isn't standard in *any* |
| 710 | * C++ version. We thus virtualize it here in a macro. |
| 711 | * |
| 712 | * os_va_copy() has identical semantics to C99 va_copy(). A matching call |
| 713 | * to os_va_copy_end() must be made for each call to os_va_copy() before |
| 714 | * the calling function returns; this has identical semantics to C99 |
| 715 | * va_end(). |
| 716 | * |
| 717 | * Because our semantics are identical to the C99 version, we provide a |
| 718 | * default definition here for compilers that define va_copy(). Platform |
| 719 | * headers must provide suitable definitions only if their compilers don't |
| 720 | * have va_copy(). We also provide a definition for GCC compilers that |
| 721 | * define the private __va_copy macro, which also has the same semantics. |
| 722 | */ |
| 723 | #ifdef va_copy |
| 724 | # define os_va_copy(dst, src) va_copy(dst, src) |
| 725 | # define os_va_copy_end(dst) va_end(dst) |
| 726 | #else |
| 727 | # if defined(__GNUC__) && defined(__va_copy) |
| 728 | # define os_va_copy(dst, src) __va_copy(dst, src) |
| 729 | # define os_va_copy_end(dst) va_end(dst) |
| 730 | # endif |
| 731 | #endif |
| 732 | |
| 733 | |
| 734 | |
| 735 | /* ------------------------------------------------------------------------ */ |
| 736 | /* |
| 737 | * Platform Identifiers. You must define the following macros in your |
| 738 | * osxxx.h header file: |
| 739 | * |
| 740 | * OS_SYSTEM_NAME - a string giving the system identifier. This string |
| 741 | * must contain only characters that are valid in a TADS identifier: |
| 742 | * letters, numbers, and underscores; and must start with a letter or |
| 743 | * underscore. For example, on MS-DOS, this string is "MSDOS". |
| 744 | * |
| 745 | * OS_SYSTEM_LDESC - a string giving the system descriptive name. This |
| 746 | * is used in messages displayed to the user. For example, on MS-DOS, |
| 747 | * this string is "MS-DOS". |
| 748 | */ |
| 749 | |
| 750 | |
| 751 | /* ------------------------------------------------------------------------ */ |
| 752 | /* |
| 753 | * Message Linking Configuration. You should #define ERR_LINK_MESSAGES |
| 754 | * in your osxxx.h header file if you want error messages linked into |
| 755 | * the application. Leave this symbol undefined if you want an external |
| 756 | * message file. |
| 757 | */ |
| 758 | |
| 759 | |
| 760 | /* ------------------------------------------------------------------------ */ |
| 761 | /* |
| 762 | * Program Exit Codes. These values are used for the argument to exit() |
| 763 | * to conform to local conventions. Define the following values in your |
| 764 | * OS-specific header: |
| 765 | * |
| 766 | * OSEXSUCC - successful completion. Usually defined to 0. |
| 767 | *. OSEXFAIL - failure. Usually defined to 1. |
| 768 | */ |
| 769 | |
| 770 | |
| 771 | /* ------------------------------------------------------------------------ */ |
| 772 | /* |
| 773 | * Basic memory management interface. These functions are merely |
| 774 | * documented here, but no prototypes are defined, because most |
| 775 | * platforms #define macros for these functions and types, mapping them |
| 776 | * to malloc or other system interfaces. |
| 777 | */ |
| 778 | |
| 779 | /* |
| 780 | * Theoretical maximum osmalloc() size. This may be less than the |
| 781 | * capacity of the argument to osmalloc() on some systems. For example, |
| 782 | * on segmented architectures (such as 16-bit x86), memory is divided into |
| 783 | * segments, so a single memory allocation can allocate only a subset of |
| 784 | * the total addressable memory in the system. This value thus specifies |
| 785 | * the maximum amount of memory that can be allocated in one chunk. |
| 786 | * |
| 787 | * Note that this is an architectural maximum for the hardware and |
| 788 | * operating system. It doesn't have anything to do with the total amount |
| 789 | * of memory actually available at run-time. |
| 790 | * |
| 791 | * #define OSMALMAX to a constant long value with theoretical maximum |
| 792 | * osmalloc() argument value. For a platform with a flat (unsegmented) |
| 793 | * 32-bit memory space, this is usually 0xffffffff; for 16-bit platforms, |
| 794 | * this is usually 0xffff. |
| 795 | */ |
| 796 | /* #define OSMALMAX 0xffffffff */ |
| 797 | |
| 798 | /* |
| 799 | * Allocate a block of memory of the given size in bytes. The actual |
| 800 | * allocation may be larger, but may be no smaller. The block returned |
| 801 | * should be worst-case aligned (i.e., suitably aligned for any type). |
| 802 | * Return null if the given amount of memory is not available. |
| 803 | */ |
| 804 | /* void *osmalloc(size_t siz); */ |
| 805 | |
| 806 | /* |
| 807 | * Free memory previously allocated with osmalloc(). |
| 808 | */ |
| 809 | /* void osfree(void *block); */ |
| 810 | |
| 811 | /* |
| 812 | * Reallocate memory previously allocated with osmalloc() or |
| 813 | * osrealloc(), changing the block's size to the given number of bytes. |
| 814 | * If necessary, a new block at a different address can be allocated, in |
| 815 | * which case the data from the original block is copied (the lesser of |
| 816 | * the old block size and the new size is copied) to the new block, and |
| 817 | * the original block is freed. If the new size is less than the old |
| 818 | * size, this need not do anything at all, since the returned block can |
| 819 | * be larger than the new requested size. If the block cannot be |
| 820 | * enlarged to the requested size, return null. |
| 821 | */ |
| 822 | /* void *osrealloc(void *block, size_t siz); */ |
| 823 | |
| 824 | |
| 825 | /* ------------------------------------------------------------------------ */ |
| 826 | /* |
| 827 | * Basic file I/O interface. These functions are merely documented here, |
| 828 | * but no prototypes are defined, because most platforms #define macros for |
| 829 | * these functions and types, mapping them to stdio or other system I/O |
| 830 | * interfaces. |
| 831 | * |
| 832 | * When writing a file, writes might or might not be buffered in |
| 833 | * application memory; this is up to the OS implementation, which can |
| 834 | * perform buffering according to local conventions and what's most |
| 835 | * efficient. However, it shouldn't make any difference to the caller |
| 836 | * whether writes are buffered or not - the OS implementation must take |
| 837 | * care that any buffering is invisible to the app. (Porters: note that |
| 838 | * the basic C stdio package has the proper behavior here, so you'll get |
| 839 | * the correct semantics if you use a simple stdio implementation.) |
| 840 | * |
| 841 | * Write buffering might be visible to *other* apps, though. In |
| 842 | * particular, another process might not see data written to a file (with |
| 843 | * osfwb(), os_fprint(), etc) immediately, since the write functions might |
| 844 | * hold the written bytes in an internal memory buffer rather than sending |
| 845 | * them to the OS. Any internal buffers are guaranteed to be flushed to |
| 846 | * the OS upon calling osfcls() or osfflush(). Note that it's never |
| 847 | * *necessary* to call osfflush(), because buffered data will always be |
| 848 | * flushed on closing the file with osfcls(). However, if you want other |
| 849 | * apps to be able to see updates immediately, you can use osfflush() to |
| 850 | * ensure that buffers are flushed to a file before you close it. |
| 851 | * |
| 852 | * You can also use osfflush() to check for buffered write errors. When |
| 853 | * you use osfwb() or other write functions to write data, they will return |
| 854 | * a success indication even if the data was only copied into a buffer. |
| 855 | * This means that a write that appeared to succeed might actually fail |
| 856 | * later, when the buffer is flushed. The only way to know for sure is to |
| 857 | * explicitly flush buffers using osfflush(), and check the result code. |
| 858 | * If the original write function and a subsequent osfflush() *both* return |
| 859 | * success indications, then the write has definitely succeeded. |
| 860 | */ |
| 861 | |
| 862 | |
| 863 | /* |
| 864 | * Define the following values in your OS header to indicate local |
| 865 | * file/path syntax conventions: |
| 866 | * |
| 867 | * OSFNMAX - integer indicating maximum length of a filename |
| 868 | * |
| 869 | * OSPATHCHAR - character giving the normal path separator character |
| 870 | *. OSPATHALT - string giving other path separator characters |
| 871 | *. OSPATHURL - string giving path separator characters for URL conversions |
| 872 | *. OSPATHSEP - directory separator for PATH-style environment variables |
| 873 | *. OSPATHPWD - string giving the special path representing the current |
| 874 | *. working directory; for Unix or Windows, this is "." |
| 875 | * |
| 876 | * OSPATHURL is a little different: this specifies the characters that |
| 877 | * should be converted to URL-style separators when converting a path from |
| 878 | * local notation to URL notation. This is usually the same as the union |
| 879 | * of OSPATHCHAR and OSPATHALT, but need not be; for example, on DOS, the |
| 880 | * colon (':') is a path separator for most purposes, but is NOT a path |
| 881 | * character for URL conversions. |
| 882 | */ |
| 883 | |
| 884 | /* |
| 885 | * Define the type osfildef as the appropriate file handle structure for |
| 886 | * your osfxxx functions. This type is always used as a pointer, but |
| 887 | * the value is always obtained from an osfopxxx call, and is never |
| 888 | * synthesized by portable code, so you can use essentially any type |
| 889 | * here that you want. |
| 890 | * |
| 891 | * For platforms that use C stdio functions to implement the osfxxx |
| 892 | * functions, osfildef can simply be defined as FILE. |
| 893 | */ |
| 894 | /* typedef FILE osfildef; */ |
| 895 | |
| 896 | |
| 897 | /* |
| 898 | * File types. |
| 899 | * |
| 900 | * These are symbols of the form OSFTxxxx defining various content types, |
| 901 | * somewhat aking to MIME types. These were mainly designed for the old |
| 902 | * Mac OS (versions up to OS 9), where the file system stored a type tag |
| 903 | * with each file's metadata. The type tags were used for things like |
| 904 | * filtering file selector dialogs and setting file-to-app associations in |
| 905 | * the desktop shell. |
| 906 | * |
| 907 | * Our OSFTxxx symbols are abstract file types that we define, for types |
| 908 | * used within the TADS family of applications. They give us a common, |
| 909 | * cross-platform reference point for each type we use. Each port where |
| 910 | * file types are meaningful then maps our abstract type IDs to the |
| 911 | * corresponding port-specific type IDs. In practice, this has never been |
| 912 | * used anywhere other than the old Mac OS ports; in fact, it's not even |
| 913 | * used in the modern Mac OS (OS X and later), since Apple decided to stop |
| 914 | * fighting the tide and start using filename suffixes for this sort of |
| 915 | * tagging, like everyone else always has. |
| 916 | * |
| 917 | * For the list of file types, see osifctyp.h |
| 918 | */ |
| 919 | |
| 920 | |
| 921 | /* |
| 922 | * Local newline convention. |
| 923 | * |
| 924 | * Because of the pernicious NIH ("Not Invented Here") cultures of the |
| 925 | * major technology vendors, basically every platform out there has its own |
| 926 | * unique way of expressing newlines in text files. Unix uses LF (ASCII |
| 927 | * 10); Mac uses CR (ASCII 13); DOS and Windows use CR-LF pairs. In the |
| 928 | * past there were heaven-only-knows how many other conventions in use, but |
| 929 | * fortunately these three have the market pretty well locked up at this |
| 930 | * point. But we do still have to worry about these three. |
| 931 | * |
| 932 | * Our strategy on input is to be open to just about anything whenever |
| 933 | * possible. So, when we're reading something that we believe to be a text |
| 934 | * file, we'll treat all of these as line endings: CR, LF, CR-LF, and |
| 935 | * LF-CR. It's pretty safe to do this; if we have a CR and LF occurring |
| 936 | * adjacently, it's almost certain that they're intended to be taken |
| 937 | * together as a single newline sequence. Likewise, if there's a lone CR |
| 938 | * or LF, it's rare for it to mean anything other than a newline. |
| 939 | * |
| 940 | * On output, though, we can't be as loose. The problem is that other |
| 941 | * applications on our big three platforms *don't* tend to aim for the same |
| 942 | * flexibility we do on input: other apps usually expect exactly the local |
| 943 | * conventions on input, and don't always work well if they don't get it. |
| 944 | * So it's important that when we're writing a text file, we write newlines |
| 945 | * in the local convention. This means that we sometimes need to know what |
| 946 | * the local convention actually is. That's where this definition comes |
| 947 | * in. |
| 948 | * |
| 949 | * Each port must define OS_NEWLINE_SEQ as an ASCII string giving the local |
| 950 | * newline sequence to write on output. For example, DOS defines it as |
| 951 | * "\r\n" (CR-LF). Always define it as a STRING (not a character |
| 952 | * constant), even if it's only one character long. |
| 953 | * |
| 954 | * (Note that some compilers use wacky mappings for \r and \n. Some older |
| 955 | * Mac compilers, for example, defined \n as CR and \r as LF, because of |
| 956 | * the Mac convention where newline is represented as CR in a text file. |
| 957 | * If there's any such variability on your platform, you can always use the |
| 958 | * octal codes to be unambiguous: \012 for LF and \015 for CR.) |
| 959 | */ |
| 960 | /* #define OS_NEWLINE_SEQ "\r\n" */ |
| 961 | |
| 962 | |
| 963 | |
| 964 | /* |
| 965 | * Open text file for reading. This opens the file with read-only access; |
| 966 | * we're not allowed to write to the file using this handle. Returns NULL |
| 967 | * on error. |
| 968 | * |
| 969 | * A text file differs from a binary file in that some systems perform |
| 970 | * translations to map between C conventions and local file system |
| 971 | * conventions; for example, on DOS, the stdio library maps the DOS CR-LF |
| 972 | * newline convention to the C-style '\n' newline format. On many systems |
| 973 | * (Unix, for example), there is no distinction between text and binary |
| 974 | * files. |
| 975 | * |
| 976 | * On systems that support file sharing and locking, this should open the |
| 977 | * file in "shared read" mode - this means that other processes are allowed |
| 978 | * to simultaneously read from the file, but no other processs should be |
| 979 | * allowed to write to the file as long as we have it open. If another |
| 980 | * process already has the file open with write access, this routine should |
| 981 | * return failure, since we can't take away the write privileges the other |
| 982 | * process already has and thus we can't guarantee that other processes |
| 983 | * won't write to the file while we have it open. |
| 984 | */ |
| 985 | /* osfildef *osfoprt(const char *fname, os_filetype_t typ); */ |
| 986 | |
| 987 | /* |
| 988 | * Open a text file for "volatile" reading: we open the file with read-only |
| 989 | * access, and we explicitly accept instability in the file's contents due |
| 990 | * to other processes simultaneously writing to the file. On systems that |
| 991 | * support file sharing and locking, the file should be opened in "deny |
| 992 | * none" mode, meaning that other processes can simultaneously open the |
| 993 | * file for reading and/or writing even while have the file open. |
| 994 | */ |
| 995 | /* osfildef *osfoprtv(const char *fname, os_filetype_t typ); */ |
| 996 | |
| 997 | /* |
| 998 | * Open text file for writing; returns NULL on error. If the file already |
| 999 | * exists, this truncates the file to zero length, deleting any existing |
| 1000 | * contents. |
| 1001 | */ |
| 1002 | /* osfildef *osfopwt(const char *fname, os_filetype_t typ); */ |
| 1003 | |
| 1004 | /* |
| 1005 | * Open text file for reading and writing, keeping the file's existing |
| 1006 | * contents if the file already exists or creating a new file if no such |
| 1007 | * file exists. Returns NULL on error. |
| 1008 | */ |
| 1009 | /* osfildef *osfoprwt(const char *fname, os_filetype_t typ); */ |
| 1010 | |
| 1011 | /* |
| 1012 | * Open text file for reading/writing. If the file already exists, |
| 1013 | * truncate the existing contents to zero length. Create a new file if it |
| 1014 | * doesn't already exist. Return null on error. |
| 1015 | */ |
| 1016 | /* osfildef *osfoprwtt(const char *fname, os_filetype_t typ); */ |
| 1017 | |
| 1018 | /* |
| 1019 | * Open binary file for writing; returns NULL on error. If the file |
| 1020 | * exists, this truncates the existing contents to zero length. |
| 1021 | */ |
| 1022 | /* osfildef *osfopwb(const char *fname, os_filetype_t typ); */ |
| 1023 | |
| 1024 | /* |
| 1025 | * Open source file for reading - use the appropriate text or binary |
| 1026 | * mode. |
| 1027 | */ |
| 1028 | /* osfildef *osfoprs(const char *fname, os_filetype_t typ); */ |
| 1029 | |
| 1030 | /* |
| 1031 | * Open binary file for reading; returns NULL on error. |
| 1032 | */ |
| 1033 | /* osfildef *osfoprb(const char *fname, os_filetype_t typ); */ |
| 1034 | |
| 1035 | /* |
| 1036 | * Open binary file for 'volatile' reading; returns NULL on error. |
| 1037 | * ("Volatile" means that we'll accept writes from other processes while |
| 1038 | * reading, so the file should be opened in "deny none" mode or the |
| 1039 | * equivalent, to the extent that the local system supports file sharing |
| 1040 | * modes.) |
| 1041 | */ |
| 1042 | /* osfildef *osfoprbv(const char *fname, os_filetype_t typ); */ |
| 1043 | |
| 1044 | /* |
| 1045 | * Open binary file for random-access reading/writing. If the file already |
| 1046 | * exists, keep the existing contents; if the file doesn't already exist, |
| 1047 | * create a new empty file. |
| 1048 | * |
| 1049 | * The caller is allowed to perform any mixture of read and write |
| 1050 | * operations on the returned file handle, and can seek around in the file |
| 1051 | * to read and write at random locations. |
| 1052 | * |
| 1053 | * If the local file system supports file sharing or locking controls, this |
| 1054 | * should generally open the file in something equivalent to "exclusive |
| 1055 | * write, shared read" mode ("deny write" in DENY terms), so that other |
| 1056 | * processes can't modify the file at the same time we're modifying it (but |
| 1057 | * it doesn't bother us to have other processes reading from the file while |
| 1058 | * we're working on it, as long as they don't mind that we could change |
| 1059 | * things on the fly). It's not absolutely necessary to assert these |
| 1060 | * locking semantics, but if there's an option to do so this is preferred. |
| 1061 | * Stricter semantics (such as "exclusive" or "deny all" mode) are better |
| 1062 | * than less strict semantics. Less strict semantics are dicey, because in |
| 1063 | * that case the caller has no way of knowing that another process could be |
| 1064 | * modifying the file at the same time, and no way (through osifc) of |
| 1065 | * coordinating that activity. If less strict semantics are implemented, |
| 1066 | * the caller will basically be relying on luck to avoid corruptions due to |
| 1067 | * writing by other processes. |
| 1068 | * |
| 1069 | * Return null on error. |
| 1070 | */ |
| 1071 | /* osfildef *osfoprwb(const char *fname, os_filetype_t typ); */ |
| 1072 | |
| 1073 | /* |
| 1074 | * Open binary file for random-access reading/writing. If the file already |
| 1075 | * exists, truncate the existing contents (i.e., delete the contents of the |
| 1076 | * file, resetting it to a zero-length file). Create a new file if it |
| 1077 | * doesn't already exist. The caller is allowed to perform any mixture of |
| 1078 | * read and write operations on the returned handle, and can seek around in |
| 1079 | * the file to read and write at random locations. |
| 1080 | * |
| 1081 | * The same comments regarding sharing/locking modes for osfoprwb() apply |
| 1082 | * here as well. |
| 1083 | * |
| 1084 | * Return null on error. |
| 1085 | */ |
| 1086 | /* osfildef *osfoprwtb(const char *fname, os_filetype_t typ); */ |
| 1087 | |
| 1088 | /* |
| 1089 | * Duplicate a file handle. Returns a new osfildef* handle that accesses |
| 1090 | * the same open file as an existing osfildef* handle. The new handle is |
| 1091 | * independent of the original handle, with its own seek position, |
| 1092 | * buffering, etc. The new handle and the original handle must each be |
| 1093 | * closed separately when the caller is done with them (closing one doesn't |
| 1094 | * close the other). The effect should be roughly the same as the Unix |
| 1095 | * dup() function. |
| 1096 | * |
| 1097 | * On success, returns a new, non-null osfildef* handle duplicating the |
| 1098 | * original handle. Returns null on failure. |
| 1099 | * |
| 1100 | * 'mode' is a simplified stdio fopen() mode string. The first |
| 1101 | * character(s) indicate the access type: "r" for read access, "w" for |
| 1102 | * write access, or "r+" for read/write access. Note that "w+" mode is |
| 1103 | * specifically not defined, since the fopen() handling of "w+" is to |
| 1104 | * truncate any existing file contents, which is not desirable when |
| 1105 | * duplicating a handle. The access type can optionally be followed by "t" |
| 1106 | * for text mode, "s" for source file mode, or "b" for binary mode, with |
| 1107 | * the same meanings as for the various osfop*() functions. The default is |
| 1108 | * 't' for text mode if none of these are specified. |
| 1109 | * |
| 1110 | * If the osfop*() functions are implemented in terms of stdio FILE* |
| 1111 | * objects, this can be implemented as fdopen(dup(fileno(orig)), mode), or |
| 1112 | * using equivalents if the local stdio library uses different names for |
| 1113 | * these functions. Note that "s" (source file format) isn't a stdio mode, |
| 1114 | * so implementations must translate it to the appropriate "t" or "b" mode. |
| 1115 | * (For that matter, "t" and "b" modes aren't universally supported either, |
| 1116 | * so some implementations may have to translate these, or more likely |
| 1117 | * simply remove them, as most platforms don't distinguish text and binary |
| 1118 | * modes anyway.) |
| 1119 | */ |
| 1120 | osfildef *osfdup(osfildef *orig, const char *mode); |
| 1121 | |
| 1122 | /* |
| 1123 | * Set a file's type information. This is primarily for implementations on |
| 1124 | * Mac OS 9 and earlier, where the file system keeps file-type metadata |
| 1125 | * separate from the filename. On such systems, this can be used to set |
| 1126 | * the type metadata after a file is created. The system should map the |
| 1127 | * os_filetype_t values to the actual metadata values on the local system. |
| 1128 | * On most systems, there's no such thing as file-type metadata, in which |
| 1129 | * case this function should simply be stubbed out with an empty function. |
| 1130 | */ |
| 1131 | void os_settype(const char *f, os_filetype_t typ); |
| 1132 | |
| 1133 | /* open the error message file for reading */ |
| 1134 | osfildef *oserrop(const char *arg0); |
| 1135 | |
| 1136 | /* |
| 1137 | * Get a line of text from a text file. Uses fgets semantics. |
| 1138 | */ |
| 1139 | /* char *osfgets(char *buf, size_t len, osfildef *fp); */ |
| 1140 | |
| 1141 | /* |
| 1142 | * Write a line of text to a text file. Uses fputs semantics. |
| 1143 | */ |
| 1144 | /* int osfputs(const char *buf, osfildef *fp); */ |
| 1145 | |
| 1146 | /* |
| 1147 | * Write to a text file. os_fprintz() takes a null-terminated string, |
| 1148 | * while os_fprint() takes an explicit separate length argument that might |
| 1149 | * not end with a null terminator. |
| 1150 | */ |
| 1151 | void os_fprintz(osfildef *fp, const char *str); |
| 1152 | void os_fprint(osfildef *fp, const char *str, size_t len); |
| 1153 | |
| 1154 | /* |
| 1155 | * Write bytes to file. Return 0 on success, non-zero on error. |
| 1156 | */ |
| 1157 | /* int osfwb(osfildef *fp, const void *buf, int bufl); */ |
| 1158 | |
| 1159 | /* |
| 1160 | * Flush buffered writes to a file. This ensures that any bytes written to |
| 1161 | * the file (with osfwb(), os_fprint(), etc) are actually sent out to the |
| 1162 | * operating system, rather than being buffered in application memory for |
| 1163 | * later writing. |
| 1164 | * |
| 1165 | * Note that this routine only guarantees that we write through to the |
| 1166 | * operating system. This does *not* guarantee that the data will actually |
| 1167 | * be committed to the underlying physical storage device. Such a |
| 1168 | * guarantee is hard to come by in general, since most modern systems use |
| 1169 | * multiple levels of software and hardware buffering - the OS might buffer |
| 1170 | * some data in system memory, and the physical disk drive might itself |
| 1171 | * buffer data in its own internal cache. This routine thus isn't good |
| 1172 | * enough, for example, to protect transactional data that needs to survive |
| 1173 | * a power failure or a serious system crash. What this routine *does* |
| 1174 | * ensure is that buffered data are written through to the OS; in |
| 1175 | * particular, this ensures that another process that's reading from the |
| 1176 | * same file will see all updates we've made up to this point. |
| 1177 | * |
| 1178 | * Returns 0 on success, non-zero on error. Errors can occur for any |
| 1179 | * reason that they'd occur on an ordinary write - a full disk, a hardware |
| 1180 | * failure, etc. |
| 1181 | */ |
| 1182 | /* int osfflush(osfildef *fp); */ |
| 1183 | |
| 1184 | /* |
| 1185 | * Read a character from a file. Provides the same semantics as fgetc(). |
| 1186 | */ |
| 1187 | /* int osfgetc(osfildef *fp); */ |
| 1188 | |
| 1189 | /* |
| 1190 | * Read bytes from file. Return 0 on success, non-zero on error. |
| 1191 | */ |
| 1192 | /* int osfrb(osfildef *fp, void *buf, int bufl); */ |
| 1193 | |
| 1194 | /* |
| 1195 | * Read bytes from file and return the number of bytes read. 0 |
| 1196 | * indicates that no bytes could be read. |
| 1197 | */ |
| 1198 | /* size_t osfrbc(osfildef *fp, void *buf, size_t bufl); */ |
| 1199 | |
| 1200 | /* |
| 1201 | * Get the current seek location in the file. The first byte of the |
| 1202 | * file has seek position 0. |
| 1203 | */ |
| 1204 | /* long osfpos(osfildef *fp); */ |
| 1205 | |
| 1206 | /* |
| 1207 | * Seek to a location in the file. The first byte of the file has seek |
| 1208 | * position 0. Returns zero on success, non-zero on error. |
| 1209 | * |
| 1210 | * The following constants must be defined in your OS-specific header; |
| 1211 | * these values are used for the "mode" parameter to indicate where to |
| 1212 | * seek in the file: |
| 1213 | * |
| 1214 | * OSFSK_SET - set position relative to the start of the file |
| 1215 | *. OSFSK_CUR - set position relative to the current file position |
| 1216 | *. OSFSK_END - set position relative to the end of the file |
| 1217 | */ |
| 1218 | /* int osfseek(osfildef *fp, long pos, int mode); */ |
| 1219 | |
| 1220 | /* |
| 1221 | * Close a file. |
| 1222 | * |
| 1223 | * If the OS implementation uses buffered writes, this routine guarantees |
| 1224 | * that any buffered data are flushed to the underlying file. So, it's not |
| 1225 | * necessary to call osfflush() before calling this routine. However, |
| 1226 | * since this function doesn't return any error indication, a caller could |
| 1227 | * use osfflush() first to check for errors on any final buffered writes. |
| 1228 | */ |
| 1229 | /* void osfcls(osfildef *fp); */ |
| 1230 | |
| 1231 | /* |
| 1232 | * Delete a file. Returns zero on success, non-zero on error. |
| 1233 | */ |
| 1234 | /* int osfdel(const char *fname); */ |
| 1235 | |
| 1236 | /* |
| 1237 | * Rename/move a file. This should apply the usual C rename() behavior. |
| 1238 | * Renames the old file to the new name, which may be in a new directory |
| 1239 | * location if supported on the local system; moves across devices, |
| 1240 | * volumes, file systems, etc may or may not be supported according to the |
| 1241 | * local system's rules. If the new file already exists, results are |
| 1242 | * undefined. Returns true on success, false on failure. |
| 1243 | */ |
| 1244 | /* int os_rename_file(const char *oldname, const char *newname); */ |
| 1245 | |
| 1246 | |
| 1247 | /* ------------------------------------------------------------------------ */ |
| 1248 | /* |
| 1249 | * File "stat()" information - mode, size, time stamps |
| 1250 | */ |
| 1251 | |
| 1252 | /* |
| 1253 | * Test access to a file - i.e., determine if the file exists. Returns |
| 1254 | * zero if the file exists, non-zero if not. (The semantics may seem |
| 1255 | * backwards, but this is consistent with the conventions used by most of |
| 1256 | * the other osfxxx calls: zero indicates success, non-zero indicates an |
| 1257 | * error. If the file exists, "accessing" it was successful, so osfacc |
| 1258 | * returns zero; if the file doesn't exist, accessing it gets an error, |
| 1259 | * hence a non-zero return code.) |
| 1260 | */ |
| 1261 | /* int osfacc(const char *fname) */ |
| 1262 | |
| 1263 | /* |
| 1264 | * Get a file's mode and attribute flags. This retrieves information on |
| 1265 | * the given file equivalent to the st_mode member of the 'struct stat' |
| 1266 | * data returned by the Unix stat() family of functions, as well as some |
| 1267 | * extra system-specific attributes. On success, fills in *mode (if mode |
| 1268 | * is non-null) with the mode information as a bitwise combination of |
| 1269 | * OSFMODE_xxx values, fills in *attr (if attr is non-null) with a |
| 1270 | * combination of OSFATTR_xxx attribute flags, and returns true; on |
| 1271 | * failure, simply returns false. Failure can occur if the file doesn't |
| 1272 | * exist, can't be accessed due to permissions, etc. |
| 1273 | * |
| 1274 | * Note that 'mode' and/or 'attr' can be null if the caller doesn't need |
| 1275 | * that information. Implementations must check these parameters for null |
| 1276 | * pointers and skip returning the corresponding information if null. |
| 1277 | * |
| 1278 | * If the file in 'fname' is a symbolic link, the behavior depends upon |
| 1279 | * 'follow_links'. If 'follow_links' is true, the function should resolve |
| 1280 | * the link reference (and if that points to another link, the function |
| 1281 | * resolves that link as well, and so on) and return information on the |
| 1282 | * object the link points to. Otherwise, the function returns information |
| 1283 | * on the link itself. This only applies for symbolic links (not for hard |
| 1284 | * links), and only if the underlying OS and file system support this |
| 1285 | * distinction; if the OS transparently resolves links and doesn't allow |
| 1286 | * retrieving information about the link itself, 'follow_links' can be |
| 1287 | * ignored. Likewise, hard links (on systems that support them) are |
| 1288 | * generally indistinguishable from regular files, so this function isn't |
| 1289 | * expected to do anything special with them. |
| 1290 | * |
| 1291 | * The '*mode' value returned is a bitwise combination of OSFMODE_xxx flag. |
| 1292 | * Many of the flags are mutually exclusive; for example, "file" and |
| 1293 | * "directory" should never be combined. It's also possible for '*mode' to |
| 1294 | * be zero for a valid file; this means that the file is of some special |
| 1295 | * type on the local system that doesn't fit any of the OSFMODE_xxx types. |
| 1296 | * (If any ports do encounter such cases, we can add OSFMODE_xxx types to |
| 1297 | * accommodate new types. The list below isn't meant to be final; it's |
| 1298 | * just what we've encountered so far on the platforms where TADS has |
| 1299 | * already been ported.) |
| 1300 | * |
| 1301 | * The OSFMODE_xxx values are left for the OS to define so that they can be |
| 1302 | * mapped directly to the OS API's equivalent constants, if desired. This |
| 1303 | * makes the routine easy to write, since you can simply set *mode directly |
| 1304 | * to the mode information the OS returns from its stat() or equivalent. |
| 1305 | * However, note that these MUST be defined as bit flags - that is, each |
| 1306 | * value must be exactly a power of 2. Windows and Unix-like systems |
| 1307 | * follow this practice, as do most "stat()" functions in C run-time |
| 1308 | * libraries, so this usually works automatically if you map these |
| 1309 | * constants to OS or C library values. However, if a port defines its own |
| 1310 | * values for these, take care that they're all powers of 2. |
| 1311 | * |
| 1312 | * Obviously, a given OS might not have all of the file types listed here. |
| 1313 | * If any OSFMODE_xxx values aren't applicable on the local OS, you can |
| 1314 | * simply define them as zero since they'll never be returned. |
| 1315 | * |
| 1316 | * Notes on attribute flags: |
| 1317 | * |
| 1318 | * OSFATTR_HIDDEN means that the file is conventionally hidden by default |
| 1319 | * in user interface views or listings, but is still fully accessible to |
| 1320 | * the user. Hidden files are also usually excluded by default from |
| 1321 | * wildcard patterns in commands ("rm *.*"). On Unix, a hidden file is one |
| 1322 | * whose name starts with "."; on Windows, it's a file with the HIDDEN bit |
| 1323 | * in its file attributes. On systems where this concept exists, the user |
| 1324 | * can still manipulate these files as normal by naming them explicitly, |
| 1325 | * and can typically make them appear in UI views or directory listings via |
| 1326 | * a preference setting or command flag (e.g., "ls -a" on Unix). The |
| 1327 | * "hidden" flag is explicitly NOT a security or permissions mechanism, and |
| 1328 | * it doesn't protect the file against intentional access by a user; it's |
| 1329 | * merely a convenience designed to reduce clutter by excluding files |
| 1330 | * maintained by the OS or by an application (such as preference files, |
| 1331 | * indices, caches, etc) from casual folder browsing, where a user is |
| 1332 | * typically only concerned with her own document files. On systems where |
| 1333 | * there's no such naming convention or attribute metadata, this flag will |
| 1334 | * never appear. |
| 1335 | * |
| 1336 | * OSFATTR_SYSTEM is similar to 'hidden', but means that the file is |
| 1337 | * specially marked as an operating system file. This is mostly a |
| 1338 | * DOS/Windows concept, where it corresponds to the SYSTEM bit in the file |
| 1339 | * attributes; this flag will probably never appear on other systems. The |
| 1340 | * distinction between 'system' and 'hidden' is somewhat murky even on |
| 1341 | * Windows; most 'system' file are also marked as 'hidden', and in |
| 1342 | * practical terms in the user interface, 'system' files are treated the |
| 1343 | * same as 'hidden'. |
| 1344 | * |
| 1345 | * OSFATTR_READ means that the file is readable by this process. |
| 1346 | * |
| 1347 | * OSFATTR_WRITE means that the file is writable by this process. |
| 1348 | */ |
| 1349 | /* int osfmode(const char *fname, int follow_links, */ |
| 1350 | /* unsigned long *mode, unsigned long *attr); */ |
| 1351 | |
| 1352 | /* file mode/type constants */ |
| 1353 | /* #define OSFMODE_FILE - regular file */ |
| 1354 | /* #define OSFMODE_DIR - directory */ |
| 1355 | /* #define OSFMODE_BLK - block-mode device */ |
| 1356 | /* #define OSFMODE_CHAR - character-mode device */ |
| 1357 | /* #define OSFMODE_PIPE - pipe/FIFO/other character-oriented IPC */ |
| 1358 | /* #define OSFMODE_SOCKET - network socket */ |
| 1359 | /* #define OSFMODE_LINK - symbolic link */ |
| 1360 | |
| 1361 | /* file attribute constants */ |
| 1362 | /* #define OSFATTR_HIDDEN - hidden file */ |
| 1363 | /* #define OSFATTR_SYSTEM - system file */ |
| 1364 | /* #define OSFATTR_READ - the file is readable by this process */ |
| 1365 | /* #define OSFATTR_WRITE - the file is writable by this process */ |
| 1366 | |
| 1367 | /* |
| 1368 | * Get stat() information. This fills in the portable os_file_stat |
| 1369 | * structure with the requested file information. Returns true on success, |
| 1370 | * false on failure (file not found, permissions error, etc). |
| 1371 | * |
| 1372 | * 'follow_links' has the same meaning as for osfmode(). |
| 1373 | */ |
| 1374 | typedef struct os_file_stat_t os_file_stat_t; |
| 1375 | int os_file_stat(const char *fname, int follow_links, os_file_stat_t *s); |
| 1376 | |
| 1377 | struct os_file_stat_t |
| 1378 | { |
| 1379 | /* |
| 1380 | * Size of the file, in bytes. For platforms lacking 64-bit types, we |
| 1381 | * split this into high and low 32-bit portions. Platforms where the |
| 1382 | * native stat() or equivalent only returns a 32-bit file size can |
| 1383 | * simply set sizehi to zero, since sizelo can hold the entire size |
| 1384 | * value. |
| 1385 | */ |
| 1386 | uint32_t sizelo; |
| 1387 | uint32_t sizehi; |
| 1388 | |
| 1389 | /* |
| 1390 | * Creation time, modification time, and last access time. If the file |
| 1391 | * system doesn't keep information on one or more of these, use |
| 1392 | * (os_time_t)0 to indicate that the timestamp isn't available. It's |
| 1393 | * fine to return any subset of these. Per the standard C stat(), |
| 1394 | * these should be expressed as seconds after the Unix Epoch. |
| 1395 | */ |
| 1396 | os_time_t cre_time; |
| 1397 | os_time_t mod_time; |
| 1398 | os_time_t acc_time; |
| 1399 | |
| 1400 | /* file mode, using the same flags as returned from osfmode() */ |
| 1401 | unsigned long mode; |
| 1402 | |
| 1403 | /* file attributes, using the same flags as returned from osfmode() */ |
| 1404 | unsigned long attrs; |
| 1405 | }; |
| 1406 | |
| 1407 | /* |
| 1408 | * Manually resolve a symbolic link. If the local OS and file system |
| 1409 | * support symbolic links, and the given filename is a symbolic link (in |
| 1410 | * which case osfmode(fname, FALSE, &m, &a) will set OSFMODE_LINK in the |
| 1411 | * mode bits), this fills in 'target' with the name of the link target |
| 1412 | * (i.e., the object that the link in 'fname' points to). This should |
| 1413 | * return a fully qualified file system path. Returns true on success, |
| 1414 | * false on failure. |
| 1415 | * |
| 1416 | * This should only resolve a single level of indirection. If the link |
| 1417 | * target of 'fname' is itself a link to a second target, this should only |
| 1418 | * resolve the single reference from 'fname' to its direct direct. Callers |
| 1419 | * that wish to resolve the final target of a chain of link references must |
| 1420 | * iterate until the returned path doesn't refer to a link. |
| 1421 | */ |
| 1422 | int os_resolve_symlink(const char *fname, char *target, size_t target_size); |
| 1423 | |
| 1424 | |
| 1425 | /* ------------------------------------------------------------------------ */ |
| 1426 | /* |
| 1427 | * Get a list of root directories. If 'buf' is non-null, fills in 'buf' |
| 1428 | * with a list of strings giving the root directories for the local, |
| 1429 | * file-oriented devices on the system. The strings are each null |
| 1430 | * terminated and are arranged consecutively in the buffer, with an extra |
| 1431 | * null terminator after the last string to mark the end of the list. |
| 1432 | * |
| 1433 | * The return value is the length of the buffer required to hold the |
| 1434 | * results. If the caller's buffer is null or is too short, the routine |
| 1435 | * should return the full length required, and leaves the contents of the |
| 1436 | * buffer undefined; the caller shouldn't expect any contents to be filled |
| 1437 | * in if the return value is greater than buflen. Both 'buflen' and the |
| 1438 | * return value include the null terminators, including the extra null |
| 1439 | * terminator at the end of the list. If an error occurs, or the system |
| 1440 | * has no concept of a root directory, returns zero. |
| 1441 | * |
| 1442 | * Each result string should be expressed using the syntax for the root |
| 1443 | * directory on a device. For example, on Windows, "C:\" represents the |
| 1444 | * root directory on the C: drive. |
| 1445 | * |
| 1446 | * "Local" means a device is mounted locally, as opposed to being merely |
| 1447 | * visible on the network via some remote node syntax; e.g., on Windows |
| 1448 | * this wouldn't include any UNC-style \\SERVER\SHARE names, and on VMS it |
| 1449 | * excludes any SERVER:: nodes. It's up to each system how to treat |
| 1450 | * virtual local devices, i.e., those that look synctactically like local |
| 1451 | * devices but are actually mounted network devices, such as Windows mapped |
| 1452 | * network drives; we recommend including them if it would take extra work |
| 1453 | * to filter them out, and excluding them if it would take extra work to |
| 1454 | * include them. "File-oriented" means that the returned devices are |
| 1455 | * accessed via file systems, not as character devices or raw block |
| 1456 | * devices; so this would exclude /dev/xxx devices on Unix and things like |
| 1457 | * CON: and LPT1: on Windows. |
| 1458 | * |
| 1459 | * Examples ("." represents a null byte): |
| 1460 | * |
| 1461 | * Windows: C:\.D:\.E:\.. |
| 1462 | * |
| 1463 | * Unix example: /.. |
| 1464 | */ |
| 1465 | size_t os_get_root_dirs(char *buf, size_t buflen); |
| 1466 | |
| 1467 | |
| 1468 | /* ------------------------------------------------------------------------ */ |
| 1469 | /* |
| 1470 | * Open a directory. This begins an enumeration of a directory's contents. |
| 1471 | * 'dirname' is a relative or absolute path to a directory. On success, |
| 1472 | * returns true, and 'handle' is set to a port-defined handle value that's |
| 1473 | * used in subsequent calls to os_read_dir() and os_close_dir(). Returns |
| 1474 | * false on failure. |
| 1475 | * |
| 1476 | * If the routine succeeds, the caller must eventually call os_close_dir() |
| 1477 | * to release the resources associated with the handle. |
| 1478 | */ |
| 1479 | /* typedef <local system type> osdirhdl_t; */ |
| 1480 | int os_open_dir(const char *dirname, /*OUT*/osdirhdl_t *handle); |
| 1481 | |
| 1482 | /* |
| 1483 | * Read the next file in a directory. 'handle' is a handle value obtained |
| 1484 | * from a call to os_open_dir(). On success, returns true and fills in |
| 1485 | * 'fname' with the next filename; the handle is also internally updated so |
| 1486 | * that the next call to this function will retrieve the next file, and so |
| 1487 | * on until all files have been retrieved. If an error occurs, or there |
| 1488 | * are no more files in the directory, returns false. |
| 1489 | * |
| 1490 | * The filename returned is the root filename only, without the path. The |
| 1491 | * caller can build the full path by calling os_build_full_path() or |
| 1492 | * os_combine_paths() with the original directory name and the returned |
| 1493 | * filename as parameters. |
| 1494 | * |
| 1495 | * This routine lists all objects in the directory that are visible to the |
| 1496 | * corresponding native API, and is non-recursive. The listing should thus |
| 1497 | * include subdirectory objects, but not the contents of subdirectories. |
| 1498 | * Implementations are encouraged to simply return all objects returned |
| 1499 | * from the corresponding native directory scan API; there's no need to do |
| 1500 | * any filtering, except perhaps in cases where it's difficult or |
| 1501 | * impossible to represent an object in terms of the osifc APIs (e.g., it |
| 1502 | * might be reasonable to exclude files without names). System relative |
| 1503 | * links, such as the Unix/DOS "." and "..", specifically should be |
| 1504 | * included in the listing. For unusual objects that don't fit into the |
| 1505 | * os_file_stat() taxonomy or that otherwise might create confusion for a |
| 1506 | * caller, err on the side of full disclosure (i.e., just return everything |
| 1507 | * unfiltered); if necessary, we can extend the os_file_stat() taxonomy or |
| 1508 | * add new osifc APIs to create a portable abstraction to handle whatever |
| 1509 | * is unusual or potentially confusing about the native object. For |
| 1510 | * example, Unix implementations should feel free to return symbolic link |
| 1511 | * objects, including dangling links, since we have the portable |
| 1512 | * os_resolve_symlink() that lets the caller examine the meaning of the |
| 1513 | * link object. |
| 1514 | */ |
| 1515 | int os_read_dir(osdirhdl_t handle, char *fname, size_t fname_size); |
| 1516 | |
| 1517 | /* |
| 1518 | * Close a directory handle. This releases the resources associated with a |
| 1519 | * directory search started with os_open_dir(). Every successful call to |
| 1520 | * os_open_dir() must have a matching call to os_close_dir(). As usual for |
| 1521 | * open/close protocols, the handle is invalid after calling this function, |
| 1522 | * so no more calls to os_read_dir() may be made with the handle. |
| 1523 | */ |
| 1524 | void os_close_dir(osdirhdl_t handle); |
| 1525 | |
| 1526 | |
| 1527 | /* ------------------------------------------------------------------------ */ |
| 1528 | /* |
| 1529 | * NB - this routine is DEPRECATED as of TADS 2.5.16/3.1.1. Callers should |
| 1530 | * use os_open_dir(), os_read_dir(), os_close_dir() instead. |
| 1531 | * |
| 1532 | * Find the first file matching a given pattern. The returned context |
| 1533 | * pointer is a pointer to whatever system-dependent context structure is |
| 1534 | * needed to continue the search with the next file, and is opaque to the |
| 1535 | * caller. The caller must pass the context pointer to the next-file |
| 1536 | * routine. The caller can optionally cancel a search by calling the |
| 1537 | * close-search routine with the context pointer. If the return value is |
| 1538 | * null, it indicates that no matching files were found. If a file was |
| 1539 | * found, outbuf will be filled in with its name, and isdir will be set to |
| 1540 | * true if the match is a directory, false if it's a file. If pattern is |
| 1541 | * null, all files in the given directory should be returned; otherwise, |
| 1542 | * pattern is a string containing '*' and '?' as wildcard characters, but |
| 1543 | * not containing any directory separators, and all files in the given |
| 1544 | * directory matching the pattern should be returned. |
| 1545 | * |
| 1546 | * Important: because this routine may allocate memory for the returned |
| 1547 | * context structure, the caller must either call os_find_next_file until |
| 1548 | * that routine returns null, or call os_find_close() to cancel the search, |
| 1549 | * to ensure that the os code has a chance to release the allocated memory. |
| 1550 | * |
| 1551 | * 'outbuf' should be set on output to the name of the matching file, |
| 1552 | * without any path information. |
| 1553 | * |
| 1554 | * 'outpathbuf' should be set on output to full path of the matching file. |
| 1555 | * If possible, 'outpathbuf' should use the same relative or absolute |
| 1556 | * notation that the search criteria used on input. For example, if dir = |
| 1557 | * "resfiles", and the file found is "MyPic.jpg", outpathbuf should be set |
| 1558 | * to "resfiles/MyPic.jpg" (or appropriate syntax for the local platform). |
| 1559 | * Similarly, if dir = "/home/tads/resfiles", outpath buf should be |
| 1560 | * "/home/tads/resfiles/MyPic.jpg". The result should always conform to |
| 1561 | * correct local conventions, which may require some amount of manipulation |
| 1562 | * of the filename; for example, on the Mac, if dir = "resfiles", the |
| 1563 | * result should be ":resfiles:MyPic.jpg" (note the added leading colon) to |
| 1564 | * conform to Macintosh relative path notation. |
| 1565 | * |
| 1566 | * Note that 'outpathbuf' may be null, in which case the caller is not |
| 1567 | * interested in the full path information. |
| 1568 | */ |
| 1569 | /* |
| 1570 | * Note the following possible ways this function may be called: |
| 1571 | * |
| 1572 | * dir = "", pattern = filename - in this case, pattern is the name of a |
| 1573 | * file or directory in the current directory. filename *might* be a |
| 1574 | * relative path specified by the user (on a command line, for example); |
| 1575 | * for instance, on Unix, it could be something like "resfiles/jpegs". |
| 1576 | * |
| 1577 | * dir = path, pattern = filname - same as above, but this time the |
| 1578 | * filename or directory pattern is relative to the given path, rather |
| 1579 | * than to the current directory. For example, we could have dir = |
| 1580 | * "/games/mygame" and pattern = "resfiles/jpegs". |
| 1581 | * |
| 1582 | * dir = path, pattern = 0 (NULL) - this should search for all files in |
| 1583 | * the given path. The path might be absolute or it might be relative. |
| 1584 | * |
| 1585 | * dir = path, pattern = "*" - this should have the same result as when |
| 1586 | * pattern = 0. |
| 1587 | * |
| 1588 | * dir = path, pattern = "*.ext" - this should search for all files in |
| 1589 | * the given path whose names end with ".ext". |
| 1590 | * |
| 1591 | * dir = path, pattern = "abc*" - this should search for all files in |
| 1592 | * the given path whose names start with "abc". |
| 1593 | * |
| 1594 | * All of these combinations are possible because callers, for |
| 1595 | * portability, must generally not manipulate filenames directly; |
| 1596 | * instead, callers obtain paths and search strings from external |
| 1597 | * sources, such as from the user, and present them to this routine with |
| 1598 | * minimal manipulation. |
| 1599 | */ |
| 1600 | void *os_find_first_file(const char *dir, |
| 1601 | char *outbuf, size_t outbufsiz, int *isdir, |
| 1602 | char *outpathbuf, size_t outpathbufsiz); |
| 1603 | |
| 1604 | /* |
| 1605 | * Implementation notes for porting os_find_first_file: |
| 1606 | * |
| 1607 | * The algorithm for this routine should go something like this: |
| 1608 | * |
| 1609 | * - If 'path' is null, create a variable real_path and initialize it |
| 1610 | * with the current directory. Otherwise, copy path to real_path. |
| 1611 | * |
| 1612 | * - If 'pattern' contains any directory separators ("/" on Unix, for |
| 1613 | * example), change real_path so that it reflects the additional leading |
| 1614 | * subdirectories in the path in 'pattern', and remove the leading path |
| 1615 | * information from 'pattern'. For example, on Unix, if real_path |
| 1616 | * starts out as "./subdir", and pattern is "resfiles/jpegs", change |
| 1617 | * real_path to "./subdir/resfiles", and change pattern to "jpegs". |
| 1618 | * Take care to add and remove path separators as needed to keep the |
| 1619 | * path strings well-formed. |
| 1620 | * |
| 1621 | * - Begin a search using appropriate OS API's for all files in |
| 1622 | * real_path. |
| 1623 | * |
| 1624 | * - Check each file found. Skip any files that don't match 'pattern', |
| 1625 | * treating "*" as a wildcard that matches any string of zero or more |
| 1626 | * characters, and "?" as a wildcard that matches any single character |
| 1627 | * (or matches nothing at the end of a string). For example: |
| 1628 | * |
| 1629 | *. "*" matches anything |
| 1630 | *. "abc?" matches "abc", "abcd", "abce", "abcf", but not "abcde" |
| 1631 | *. "abc???" matches "abc", "abcd", "abcde", "abcdef", but not "abcdefg" |
| 1632 | *. "?xyz" matches "wxyz", "axyz", but not "xyz" or "abcxyz" |
| 1633 | * |
| 1634 | * - Return the first file that matches, if any, by filling in 'outbuf' |
| 1635 | * and 'isdir' with appropriate information. Before returning, allocate |
| 1636 | * a context structure (which is entirely for your own use, and opaque |
| 1637 | * to the caller) and fill it in with the information necessary for |
| 1638 | * os_find_next_file to get the next matching file. If no file matches, |
| 1639 | * return null. |
| 1640 | */ |
| 1641 | |
| 1642 | |
| 1643 | /* |
| 1644 | * Find the next matching file, continuing a search started with |
| 1645 | * os_find_first_file(). Returns null if no more files were found, in |
| 1646 | * which case the search will have been automatically closed (i.e., |
| 1647 | * there's no need to call os_find_close() after this routine returns |
| 1648 | * null). Returns a non-null context pointer, which is to be passed to |
| 1649 | * this function again to get the next file, if a file was found. |
| 1650 | * |
| 1651 | * 'outbuf' and 'outpathbuf' are filled in with the filename (without |
| 1652 | * path) and full path (relative or absolute, as appropriate), |
| 1653 | * respectively, in the same manner as they do for os_find_first_file(). |
| 1654 | * |
| 1655 | * Implementation note: if os_find_first_file() allocated memory for the |
| 1656 | * search context, this routine must free the memory if it returs null, |
| 1657 | * because this indicates that the search is finished and the caller |
| 1658 | * need not call os_find_close(). |
| 1659 | */ |
| 1660 | void *os_find_next_file(void *ctx, char *outbuf, size_t outbufsiz, |
| 1661 | int *isdir, char *outpathbuf, size_t outpathbufsiz); |
| 1662 | |
| 1663 | /* |
| 1664 | * Cancel a search. The context pointer returned by the last call to |
| 1665 | * os_find_first_file() or os_find_next_file() is the parameter. There |
| 1666 | * is no need to call this function if find-first or find-next returned |
| 1667 | * null, since they will have automatically closed the search. |
| 1668 | * |
| 1669 | * Implementation note: if os_find_first_file() allocated memory for the |
| 1670 | * search context, this routine should release the memory. |
| 1671 | */ |
| 1672 | void os_find_close(void *ctx); |
| 1673 | |
| 1674 | /* |
| 1675 | * Special filename classification |
| 1676 | */ |
| 1677 | enum os_specfile_t |
| 1678 | { |
| 1679 | /* not a special file */ |
| 1680 | OS_SPECFILE_NONE, |
| 1681 | |
| 1682 | /* |
| 1683 | * current directory link - this is a file like the "." file on Unix |
| 1684 | * or DOS, which is a special link that simply refers to itself |
| 1685 | */ |
| 1686 | OS_SPECFILE_SELF, |
| 1687 | |
| 1688 | /* |
| 1689 | * parent directory link - this is a file like the ".." file on Unix |
| 1690 | * or DOS, which is a special link that refers to the parent |
| 1691 | * directory |
| 1692 | */ |
| 1693 | OS_SPECFILE_PARENT |
| 1694 | }; |
| 1695 | |
| 1696 | /* |
| 1697 | * Determine if the given filename refers to a special file. Returns the |
| 1698 | * appropriate enum value if so, or OS_SPECFILE_NONE if not. The given |
| 1699 | * filename must be a root name - it must not contain a path prefix. The |
| 1700 | * purpose here is to classify the results from os_find_first_file() and |
| 1701 | * os_find_next_file() to identify the special relative links, so callers |
| 1702 | * can avoid infinite recursion when traversing a directory tree. |
| 1703 | */ |
| 1704 | enum os_specfile_t os_is_special_file(const char *fname); |
| 1705 | |
| 1706 | /* ------------------------------------------------------------------------ */ |
| 1707 | /* |
| 1708 | * Convert string to all-lowercase. |
| 1709 | */ |
| 1710 | char *os_strlwr(char *s); |
| 1711 | |
| 1712 | |
| 1713 | /* ------------------------------------------------------------------------ */ |
| 1714 | /* |
| 1715 | * Character classifications for quote characters. os_squote() returns |
| 1716 | * true if its argument is any type of single-quote character; |
| 1717 | * os_dquote() returns true if its argument is any type of double-quote |
| 1718 | * character; and os_qmatch(a, b) returns true if a and b are matching |
| 1719 | * open- and close-quote characters. |
| 1720 | * |
| 1721 | * These functions allow systems with extended character codes with |
| 1722 | * weird quote characters (such as the Mac) to match the weird |
| 1723 | * characters, so that users can use the extended quotes in input. |
| 1724 | * |
| 1725 | * These are usually implemented as macros. The most common |
| 1726 | * implementation simply returns true for the standard ASCII quote |
| 1727 | * characters: |
| 1728 | * |
| 1729 | * #define os_squote(c) ((c) == '\'') |
| 1730 | *. #define os_dquote(c) ((c) == '"') |
| 1731 | *. #define os_qmatch(a, b) ((a) == (b)) |
| 1732 | * |
| 1733 | * These functions take int arguments to allow for the possibility of |
| 1734 | * Unicode input. |
| 1735 | */ |
| 1736 | /* int os_squote(int c); */ |
| 1737 | /* int os_dquote(int c); */ |
| 1738 | /* int os_qmatch(int a, int b); */ |
| 1739 | |
| 1740 | |
| 1741 | /* ------------------------------------------------------------------------ */ |
| 1742 | /* |
| 1743 | * Special file and directory locations |
| 1744 | */ |
| 1745 | |
| 1746 | /* |
| 1747 | * Get the full filename (including directory path) to the executable |
| 1748 | * file, given the argv[0] parameter passed into the main program. This |
| 1749 | * fills in the buffer with a null-terminated string that can be used in |
| 1750 | * osfoprb(), for example, to open the executable file. |
| 1751 | * |
| 1752 | * Returns non-zero on success. If it's not possible to determine the |
| 1753 | * name of the executable file, returns zero. |
| 1754 | * |
| 1755 | * Some operating systems might not provide access to the executable file |
| 1756 | * information, so non-trivial implementation of this routine is optional; |
| 1757 | * if the necessary information is not available, simply implement this to |
| 1758 | * return zero. If the information is not available, callers should offer |
| 1759 | * gracefully degraded functionality if possible. |
| 1760 | */ |
| 1761 | int os_get_exe_filename(char *buf, size_t buflen, const char *argv0); |
| 1762 | |
| 1763 | /* |
| 1764 | * Get a special directory path. Returns the selected path, in a format |
| 1765 | * suitable for use with os_build_full_path(). The main program's argv[0] |
| 1766 | * parameter is provided so that the system code can choose to make the |
| 1767 | * special paths relative to the program install directory, but this is |
| 1768 | * entirely up to the system implementation, so the argv[0] parameter can |
| 1769 | * be ignored if it is not needed. |
| 1770 | * |
| 1771 | * The 'id' parameter selects which special path is requested; this is one |
| 1772 | * of the constants defined below. If the id is not understood, there is |
| 1773 | * no way of signalling an error to the caller; this routine can fail with |
| 1774 | * an assert() in such cases, because it indicates that the OS layer code |
| 1775 | * is out of date with respect to the calling code. |
| 1776 | * |
| 1777 | * This routine can be implemented using one of the strategies below, or a |
| 1778 | * combination of these. These are merely suggestions, though, and systems |
| 1779 | * are free to ignore these and implement this routine using whatever |
| 1780 | * scheme is the best fit to local conventions. |
| 1781 | * |
| 1782 | * - Relative to argv[0]. Some systems use this approach because it keeps |
| 1783 | * all of the TADS files together in a single install directory tree, and |
| 1784 | * doesn't require any extra configuration information to find the install |
| 1785 | * directory. Since we base the path name on the executable that's |
| 1786 | * actually running, we don't need any environment variables or parameter |
| 1787 | * files or registry entries to know where to look for related files. |
| 1788 | * |
| 1789 | * - Environment variables or local equivalent. On some systems, it is |
| 1790 | * conventional to set some form of global system parameter (environment |
| 1791 | * variables on Unix, for example) for this sort of install configuration |
| 1792 | * data. In these cases, this routine can look up the appropriate |
| 1793 | * configuration variables in the system environment. |
| 1794 | * |
| 1795 | * - Hard-coded paths. Some systems have universal conventions for the |
| 1796 | * installation configuration of compiler-like tools, so the paths to our |
| 1797 | * component files can be hard-coded based on these conventions. |
| 1798 | * |
| 1799 | * - Hard-coded default paths with environment variable overrides. Let the |
| 1800 | * user set environment variables if they want, but use the standard system |
| 1801 | * paths as hard-coded defaults if the variables aren't set. This is often |
| 1802 | * the best choice; users who expect the standard system conventions won't |
| 1803 | * have to fuss with any manual settings or even be aware of them, while |
| 1804 | * users who need custom settings aren't stuck with the defaults. |
| 1805 | */ |
| 1806 | void os_get_special_path(char *buf, size_t buflen, |
| 1807 | const char *argv0, int id); |
| 1808 | |
| 1809 | /* |
| 1810 | * TADS 3 system resource path. This path is used to load system |
| 1811 | * resources, such as character mapping files and error message files. |
| 1812 | */ |
| 1813 | #define OS_GSP_T3_RES 1 |
| 1814 | |
| 1815 | /* |
| 1816 | * TADS 3 compiler - system headers. This is the #include path for the |
| 1817 | * header files included with the compiler. |
| 1818 | */ |
| 1819 | #define OS_GSP_T3_INC 2 |
| 1820 | |
| 1821 | /* |
| 1822 | * TADS 3 compiler - system library source code. This is the path to the |
| 1823 | * library source files that the compiler includes in every compilation by |
| 1824 | * default (such as _main.t). |
| 1825 | */ |
| 1826 | #define OS_GSP_T3_LIB 3 |
| 1827 | |
| 1828 | /* |
| 1829 | * TADS 3 compiler - user library path list. This is a list of directory |
| 1830 | * paths, separated by the OSPATHSEP character, that should be searched for |
| 1831 | * user library files. The TADS 3 compiler uses this as an additional set |
| 1832 | * of locations to search after the list of "-Fs" options and before the |
| 1833 | * OS_GSP_T3_LIB directory. |
| 1834 | * |
| 1835 | * This path list is intended for the user's use, so no default value is |
| 1836 | * needed. The value should be user-configurable using local conventions; |
| 1837 | * on Unix, for example, this might be handled with an environment |
| 1838 | * variable. |
| 1839 | */ |
| 1840 | #define OS_GSP_T3_USER_LIBS 4 |
| 1841 | |
| 1842 | /* |
| 1843 | * TADS 3 interpreter - application data path. This is the directory where |
| 1844 | * we should store things like option settings: data that we want to store |
| 1845 | * in a file, global to all games. Depending on local system conventions, |
| 1846 | * this can be a global shared directory for all users, or can be a |
| 1847 | * user-specific directory. |
| 1848 | */ |
| 1849 | #define OS_GSP_T3_APP_DATA 5 |
| 1850 | |
| 1851 | /* |
| 1852 | * TADS 3 interpreter - system configuration files. This is used for files |
| 1853 | * that affect all games, and generally all users on the system, so it |
| 1854 | * should be in a central location. On Windows, for example, we simply |
| 1855 | * store these files in the install directory containing the interpreter |
| 1856 | * binary. |
| 1857 | */ |
| 1858 | #define OS_GSP_T3_SYSCONFIG 6 |
| 1859 | |
| 1860 | /* |
| 1861 | * System log files. This is the directory for system-level status, debug, |
| 1862 | * and error logging files. (Note that we're NOT talking about in-game |
| 1863 | * transcript logging per the SCRIPT command. SCRIPT logs are usually sent |
| 1864 | * to files selected by the user via a save-file dialog, so these don't |
| 1865 | * need a special location.) |
| 1866 | */ |
| 1867 | #define OS_GSP_LOGFILE 7 |
| 1868 | |
| 1869 | |
| 1870 | /* |
| 1871 | * Seek to the resource file embedded in the current executable file, |
| 1872 | * given the main program's argv[0]. |
| 1873 | * |
| 1874 | * On platforms where the executable file format allows additional |
| 1875 | * information to be attached to an executable, this function can be used |
| 1876 | * to find the extra information within the executable. |
| 1877 | * |
| 1878 | * The 'typ' argument gives a resource type to find. This is an arbitrary |
| 1879 | * string that the caller uses to identify what type of object to find. |
| 1880 | * The "TGAM" type, for example, is used by convention to indicate a TADS |
| 1881 | * compiled GAM file. |
| 1882 | */ |
| 1883 | osfildef *os_exeseek(const char *argv0, const char *typ); |
| 1884 | |
| 1885 | |
| 1886 | /* ------------------------------------------------------------------------ */ |
| 1887 | /* |
| 1888 | * Load a string resource. Given a string ID number, load the string |
| 1889 | * into the given buffer. |
| 1890 | * |
| 1891 | * Returns zero on success, non-zero if an error occurs (for example, |
| 1892 | * the buffer is too small, or the requested resource isn't present). |
| 1893 | * |
| 1894 | * Whenever possible, implementations should use an operating system |
| 1895 | * mechanism for loading the string from a user-modifiable resource |
| 1896 | * store; this will make localization of these strings easier, since the |
| 1897 | * resource store can be modified without the need to recompile the |
| 1898 | * application. For example, on the Macintosh, the normal system string |
| 1899 | * resource mechanism should be used to load the string from the |
| 1900 | * application's resource fork. |
| 1901 | * |
| 1902 | * When no operating system mechanism exists, the resources can be |
| 1903 | * stored as an array of strings in a static variable; this isn't ideal, |
| 1904 | * because it makes it much more difficult to localize the application. |
| 1905 | * |
| 1906 | * Resource ID's are application-defined. For example, for TADS 2, |
| 1907 | * "res.h" defines the resource ID's. |
| 1908 | */ |
| 1909 | int os_get_str_rsc(int id, char *buf, size_t buflen); |
| 1910 | |
| 1911 | |
| 1912 | /* ------------------------------------------------------------------------ */ |
| 1913 | /* |
| 1914 | * Look for a file in the "standard locations": current directory, program |
| 1915 | * directory, PATH-like environment variables, etc. The actual standard |
| 1916 | * locations are specific to each platform; the implementation is free to |
| 1917 | * use whatever conventions are appropriate to the local system. On |
| 1918 | * systems that have something like Unix environment variables, it might be |
| 1919 | * desirable to define a TADS-specific variable (TADSPATH, for example) |
| 1920 | * that provides a list of directories to search for TADS-related files. |
| 1921 | * |
| 1922 | * On return, fill in 'buf' with the full filename of the located copy of |
| 1923 | * the file (if a copy was indeed found), in a format suitable for use with |
| 1924 | * the osfopxxx() functions; in other words, after this function returns, |
| 1925 | * the caller should be able to pass the contents of 'buf' to an osfopxxx() |
| 1926 | * function to open the located file. |
| 1927 | * |
| 1928 | * Returns true (non-zero) if a copy of the file was located, false (zero) |
| 1929 | * if the file could not be found in any of the standard locations. |
| 1930 | */ |
| 1931 | int os_locate(const char *fname, int flen, const char *arg0, |
| 1932 | char *buf, size_t bufsiz); |
| 1933 | |
| 1934 | |
| 1935 | /* ------------------------------------------------------------------------ */ |
| 1936 | /* |
| 1937 | * Create and open a temporary file. The file must be opened to allow |
| 1938 | * both reading and writing, and must be in "binary" mode rather than |
| 1939 | * "text" mode, if the system makes such a distinction. Returns null on |
| 1940 | * failure. |
| 1941 | * |
| 1942 | * If 'fname' is non-null, then this routine should create and open a file |
| 1943 | * with the given name. When 'fname' is non-null, this routine does NOT |
| 1944 | * need to store anything in 'buf'. Note that the routine shouldn't try |
| 1945 | * to put the file in a special directory or anything like that; just open |
| 1946 | * the file with the name exactly as given. |
| 1947 | * |
| 1948 | * If 'fname' is null, this routine must choose a file name and fill in |
| 1949 | * 'buf' with the chosen name; if possible, the file should be in the |
| 1950 | * conventional location for temporary files on this system, and should be |
| 1951 | * unique (i.e., it shouldn't be the same as any existing file). The |
| 1952 | * filename stored in 'buf' is opaque to the caller, and cannot be used by |
| 1953 | * the caller except to pass to osfdel_temp(). On some systems, it may |
| 1954 | * not be possible to determine the actual filename of a temporary file; |
| 1955 | * in such cases, the implementation may simply store an empty string in |
| 1956 | * the buffer. (The only way the filename would be unavailable is if the |
| 1957 | * implementation uses a system API that creates a temporary file, and |
| 1958 | * that API doesn't return the name of the created temporary file. In |
| 1959 | * such cases, we don't need the name; the only reason we need the name is |
| 1960 | * so we can pass it to osfdel_temp() later, but since the system is going |
| 1961 | * to delete the file automatically, osfdel_temp() doesn't need to do |
| 1962 | * anything and thus doesn't need the name.) |
| 1963 | * |
| 1964 | * After the caller is done with the file, it should close the file (using |
| 1965 | * osfcls() as normal), then the caller MUST call osfdel_temp() to delete |
| 1966 | * the temporary file. |
| 1967 | * |
| 1968 | * This interface is intended to take advantage of systems that have |
| 1969 | * automatic support for temporary files, while allowing implementation on |
| 1970 | * systems that don't have any special temp file support. On systems that |
| 1971 | * do have automatic delete-on-close support, this routine should use that |
| 1972 | * system-level support, because it helps ensure that temp files will be |
| 1973 | * deleted even if the caller fails to call osfdel_temp() due to a |
| 1974 | * programming error or due to a process or system crash. On systems that |
| 1975 | * don't have any automatic delete-on-close support, this routine can |
| 1976 | * simply use the same underlying system API that osfoprwbt() normally |
| 1977 | * uses (although this routine must also generate a name for the temp file |
| 1978 | * when the caller doesn't supply one). |
| 1979 | * |
| 1980 | * This routine can be implemented using ANSI library functions as |
| 1981 | * follows: if 'fname' is non-null, return fopen(fname,"w+b"); otherwise, |
| 1982 | * set buf[0] to '\0' and return tmpfile(). |
| 1983 | */ |
| 1984 | osfildef *os_create_tempfile(const char *fname, char *buf); |
| 1985 | |
| 1986 | /* |
| 1987 | * Delete a temporary file - this is used to delete a file created with |
| 1988 | * os_create_tempfile(). For most platforms, this can simply be defined |
| 1989 | * the same way as osfdel(). For platforms where the operating system or |
| 1990 | * file manager will automatically delete a file opened as a temporary |
| 1991 | * file, this routine should do nothing at all, since the system will take |
| 1992 | * care of deleting the temp file. |
| 1993 | * |
| 1994 | * Callers are REQUIRED to call this routine after closing a file opened |
| 1995 | * with os_create_tempfile(). When os_create_tempfile() is called with a |
| 1996 | * non-null 'fname' argument, the same value should be passed as 'fname' to |
| 1997 | * this function. When os_create_tempfile() is called with a null 'fname' |
| 1998 | * argument, then the buffer passed in the 'buf' argument to |
| 1999 | * os_create_tempfile() must be passed as the 'fname' argument here. In |
| 2000 | * other words, if the caller explicitly names the temporary file to be |
| 2001 | * opened in os_create_tempfile(), then that same filename must be passed |
| 2002 | * here to delete the named file; if the caller lets os_create_tempfile() |
| 2003 | * generate a filename, then the generated filename must be passed to this |
| 2004 | * routine. |
| 2005 | * |
| 2006 | * If os_create_tempfile() is implemented using ANSI library functions as |
| 2007 | * described above, then this routine can also be implemented with ANSI |
| 2008 | * library calls as follows: if 'fname' is non-null and fname[0] != '\0', |
| 2009 | * then call remove(fname); otherwise do nothing. |
| 2010 | */ |
| 2011 | int osfdel_temp(const char *fname); |
| 2012 | |
| 2013 | /* |
| 2014 | * Get the temporary file path. This should fill in the buffer with a |
| 2015 | * path prefix (suitable for strcat'ing a filename onto) for a good |
| 2016 | * directory for a temporary file, such as the swap file. |
| 2017 | */ |
| 2018 | void os_get_tmp_path(char *buf); |
| 2019 | |
| 2020 | /* |
| 2021 | * Generate a name for a temporary file. This constructs a random file |
| 2022 | * path in the system temp directory that isn't already used by an existing |
| 2023 | * file. |
| 2024 | * |
| 2025 | * On systems with long filenames, this can be implemented by selecting a |
| 2026 | * GUID-strength random name (such as 32 random hex digits) with a decent |
| 2027 | * random number generator. That's long enough that the odds of a |
| 2028 | * collision are essentially zero. On systems that only support short |
| 2029 | * filenames, the odds of a collision are non-zero, so the routine should |
| 2030 | * actually check that the chosen filename doesn't exist. |
| 2031 | * |
| 2032 | * Optionally, before returning, this routine *may* create (and close) an |
| 2033 | * empty placeholder file to "reserve" the chosen filename. This isn't |
| 2034 | * required, and on systems with long filenames it's usually not necessary |
| 2035 | * because of the negligible chance of a collision. On systems with short |
| 2036 | * filenames, a placeholder can be useful to prevent a subsequent call to |
| 2037 | * this routine, or a separate process, from using the same filename before |
| 2038 | * the caller has had a chance to use the returned name to create the |
| 2039 | * actual temp file. |
| 2040 | * |
| 2041 | * Returns true on success, false on failure. This can fail if there's no |
| 2042 | * system temporary directory defined, or the temp directory is so full of |
| 2043 | * other files that we can't find an unused filename. |
| 2044 | */ |
| 2045 | int os_gen_temp_filename(char *buf, size_t buflen); |
| 2046 | |
| 2047 | |
| 2048 | /* ------------------------------------------------------------------------ */ |
| 2049 | /* |
| 2050 | * Basic directory/folder management routines |
| 2051 | */ |
| 2052 | |
| 2053 | /* |
| 2054 | * Switch to a new working directory. |
| 2055 | * |
| 2056 | * This is meant to behave similarly to the Unix concept of a working |
| 2057 | * directory, in that it sets the base directory assumed for subsequent |
| 2058 | * file operations (e.g., the osfopxx() functions, osfdel(), etc - anything |
| 2059 | * that takes a filename or directory name as an argument). The working |
| 2060 | * directory applies to filenames specified with relative paths in the |
| 2061 | * local system notation. File operations on filenames specified with |
| 2062 | * absolute paths, of course, ignore the working directory. |
| 2063 | */ |
| 2064 | void os_set_pwd(const char *dir); |
| 2065 | |
| 2066 | /* |
| 2067 | * Switch the working directory to the directory containing the given |
| 2068 | * file. Generally, this routine should only need to parse the filename |
| 2069 | * enough to determine the part that's the directory path, then use |
| 2070 | * os_set_pwd() to switch to that directory. |
| 2071 | */ |
| 2072 | void os_set_pwd_file(const char *filename); |
| 2073 | |
| 2074 | /* |
| 2075 | * Create a directory. This creates a new directory/folder with the given |
| 2076 | * name, which may be given as a relative or absolute path. Returns true |
| 2077 | * on success, false on failure. |
| 2078 | * |
| 2079 | * If 'create_parents' is true, and the directory has mulitiple path |
| 2080 | * elements, this routine should create each enclosing parent that doesn't |
| 2081 | * already exist. For example, if the path is specified as "a/b/c", and |
| 2082 | * there exists a folder "a" in the working directory, but "a" is empty, |
| 2083 | * this should first create "b" and then create "c". If an error occurs |
| 2084 | * creating any parent, the routine should simply stop and return failure. |
| 2085 | * (Optionally, the routine may attempt to behave atomically by undoing any |
| 2086 | * parent folder creations it accomplished before failing on a nested |
| 2087 | * folder, but this isn't required. To reduce the chances of a failure |
| 2088 | * midway through the operation, the routine might want to scan the |
| 2089 | * filename before starting to ensure that it contains only valid |
| 2090 | * characters, since an invalid character is the most likely reason for a |
| 2091 | * failure part of the way through.) |
| 2092 | * |
| 2093 | * We recommend making the routine flexible in terms of the notation it |
| 2094 | * accepts; e.g., on Unix, "/dir/sub/folder" and "/dir/sub/folder/" should |
| 2095 | * be considered equivalent. |
| 2096 | */ |
| 2097 | int os_mkdir(const char *dir, int create_parents); |
| 2098 | |
| 2099 | /* |
| 2100 | * Remove a directory. Returns true on success, false on failure. |
| 2101 | * |
| 2102 | * If the directory isn't already empty, this routine fails. That is, the |
| 2103 | * routine does NOT recursively delete the contents of a non-empty |
| 2104 | * directory. It's up to the caller to delete any contents before removing |
| 2105 | * the directory, if that's the caller's intention. (Note to implementors: |
| 2106 | * most native OS APIs to remove directories fail by default if the |
| 2107 | * directory isn't empty, so it's usually safe to implement this simply by |
| 2108 | * calling the native API. However, if your system's version of this API |
| 2109 | * can remove a non-empty directory, you MUST add an extra test before |
| 2110 | * removing the directory to ensure it's empty, and return failure if it's |
| 2111 | * not. For the purposes of this test, "empty" should of course ignore any |
| 2112 | * special objects that are automatically or implicitly present in all |
| 2113 | * directories, such as the Unix "." and ".." relative links.) |
| 2114 | */ |
| 2115 | int os_rmdir(const char *dir); |
| 2116 | |
| 2117 | |
| 2118 | /* ------------------------------------------------------------------------ */ |
| 2119 | /* |
| 2120 | * Filename manipulation routines |
| 2121 | */ |
| 2122 | |
| 2123 | /* apply a default extension to a filename, if it doesn't already have one */ |
| 2124 | void os_defext(char *fname, const char *ext); |
| 2125 | |
| 2126 | /* unconditionally add an extention to a filename */ |
| 2127 | void os_addext(char *fname, const char *ext); |
| 2128 | |
| 2129 | /* remove the extension from a filename */ |
| 2130 | void os_remext(char *fname); |
| 2131 | |
| 2132 | /* |
| 2133 | * Compare two file names/paths for syntactic equivalence. Returns true if |
| 2134 | * the names are equivalent names according to the local file system's |
| 2135 | * syntax conventions, false if not. This does a syntax-only comparison of |
| 2136 | * the paths, without looking anything up in the file system. This means |
| 2137 | * that a false return doesn't guarantee that the paths don't point to the |
| 2138 | * same file. |
| 2139 | * |
| 2140 | * This routine DOES make the following equivalences: |
| 2141 | * |
| 2142 | * - if the local file system is insensitive to case, the names are |
| 2143 | * compared ignoring case |
| 2144 | * |
| 2145 | * - meaningless path separator difference are ignored: on Unix, "a/b" == |
| 2146 | * "a//b" == "a/b/"; on Windows, "a/b" == "a\\b" |
| 2147 | * |
| 2148 | * - relative links that are strictly structural or syntactic are applied; |
| 2149 | * for example, on Unix or Windows, "a/./b" == "a/b" = "a/b/c/..". This |
| 2150 | * only applies for special relative links that can be resolved without |
| 2151 | * looking anything up in the file system. |
| 2152 | * |
| 2153 | * This DOES NOT do the following: |
| 2154 | * |
| 2155 | * - it doesn't apply working directories/volums to relative paths |
| 2156 | * |
| 2157 | * - it doesn't follow symbolic links in the file system |
| 2158 | */ |
| 2159 | int os_file_names_equal(const char *a, const char *b); |
| 2160 | |
| 2161 | /* |
| 2162 | * Get a pointer to the root name portion of a filename. This is the part |
| 2163 | * of the filename after any path or directory prefix. For example, on |
| 2164 | * Unix, given the string "/home/mjr/deep.gam", this function should return |
| 2165 | * a pointer to the 'd' in "deep.gam". If the filename doesn't appear to |
| 2166 | * have a path prefix, it should simply return the argument unchanged. |
| 2167 | * |
| 2168 | * IMPORTANT: the returned pointer MUST point into the original 'buf' |
| 2169 | * string, and the contents of that buffer must NOT be modified. The |
| 2170 | * return value must point into the same buffer because there are no |
| 2171 | * allowances for the alternatives. In particular, (a) you can't return a |
| 2172 | * pointer to newly allocated memory, because callers won't free it, so |
| 2173 | * doing so would cause a memory leak; and (b) you can't return a pointer |
| 2174 | * to an internal static buffer, because callers might call this function |
| 2175 | * more than once and still rely on a value returned on an older call, |
| 2176 | * which would be invalid if a static buffer could be overwritten on each |
| 2177 | * call. For these reasons, it's required that the return value point to a |
| 2178 | * position within the original string passed in 'buf'. |
| 2179 | */ |
| 2180 | char *os_get_root_name(const char *buf); |
| 2181 | |
| 2182 | /* |
| 2183 | * Determine whether a filename specifies an absolute or relative path. |
| 2184 | * This is used to analyze filenames provided by the user (for example, |
| 2185 | * in a #include directive, or on a command line) to determine if the |
| 2186 | * filename can be considered relative or absolute. This can be used, |
| 2187 | * for example, to determine whether to search a directory path for a |
| 2188 | * file; if a given filename is absolute, a path search makes no sense. |
| 2189 | * A filename that doesn't specify an absolute path can be combined with |
| 2190 | * a path using os_build_full_path(). |
| 2191 | * |
| 2192 | * Returns true if the filename specifies an absolute path, false if |
| 2193 | * not. |
| 2194 | */ |
| 2195 | int os_is_file_absolute(const char *fname); |
| 2196 | |
| 2197 | /* |
| 2198 | * Extract the path from a filename. Fills in pathbuf with the path |
| 2199 | * portion of the filename. If the filename has no path, the pathbuf |
| 2200 | * should be set appropriately for the current directory (on Unix or DOS, |
| 2201 | * for example, it can be set to an empty string). |
| 2202 | * |
| 2203 | * The result can end with a path separator character or not, depending on |
| 2204 | * local OS conventions. Paths extracted with this function can only be |
| 2205 | * used with os_build_full_path(), so the conventions should match that |
| 2206 | * function's. |
| 2207 | * |
| 2208 | * Unix examples: |
| 2209 | * |
| 2210 | *. /home/mjr/deep.gam -> /home/mjr |
| 2211 | *. games/deep.gam -> games |
| 2212 | *. deep.gam -> [empty string] |
| 2213 | * |
| 2214 | * Mac examples: |
| 2215 | * |
| 2216 | * :home:mjr:deep.gam -> :home:mjr |
| 2217 | *. Hard Disk:games:deep.gam -> Hard Disk:games |
| 2218 | *. Hard Disk:deep.gam -> Hard Disk: |
| 2219 | *. deep.gam -> [empty string] |
| 2220 | * |
| 2221 | * VMS examples: |
| 2222 | * |
| 2223 | *. SYS$DISK:[mjr.games]deep.gam -> SYS$DISK:[mjr.games] |
| 2224 | *. SYS$DISK:[mjr.games] -> SYS$DISK:[mjr] |
| 2225 | *. deep.gam -> [empty string] |
| 2226 | * |
| 2227 | * Note in the last example that we've retained the trailing colon in the |
| 2228 | * path, whereas we didn't in the others; although the others could also |
| 2229 | * retain the trailing colon, it's required only for the last case. The |
| 2230 | * last case requires the colon because it would otherwise be impossible to |
| 2231 | * determine whether "Hard Disk" was a local subdirectory or a volume name. |
| 2232 | * |
| 2233 | */ |
| 2234 | void os_get_path_name(char *pathbuf, size_t pathbuflen, const char *fname); |
| 2235 | |
| 2236 | /* |
| 2237 | * Build a full path name, given a path and a filename. The path may have |
| 2238 | * been specified by the user, or may have been extracted from another file |
| 2239 | * via os_get_path_name(). This routine must take care to add path |
| 2240 | * separators as needed, but also must take care not to add too many path |
| 2241 | * separators. |
| 2242 | * |
| 2243 | * This routine should reformat the path into canonical format to the |
| 2244 | * extent possible purely through syntactic analysis. For example, special |
| 2245 | * relative links, such as Unix "." and "..", should be resolved; for |
| 2246 | * example, combining "a/./b/c" with ".." on Unix should yield "a/b". |
| 2247 | * However, symbolic links that require looking up names in the file system |
| 2248 | * should NOT be resolved. We don't want to perform any actual file system |
| 2249 | * lookups because might want to construct hypothetical paths that don't |
| 2250 | * necessarily relate to files on the local system. |
| 2251 | * |
| 2252 | * Note that relative path names may require special care on some |
| 2253 | * platforms. In particular, if the source path is relative, the result |
| 2254 | * should also be relative. For example, on the Macintosh, a path of |
| 2255 | * "games" and a filename "deep.gam" should yield ":games:deep.gam" - note |
| 2256 | * the addition of the leading colon to make the result path relative. |
| 2257 | * |
| 2258 | * Note also that the 'filename' argument is not only allowed to be an |
| 2259 | * ordinary file, possibly qualified with a relative path, but is also |
| 2260 | * allowed to be a subdirectory. The result in this case should be a path |
| 2261 | * that can be used as the 'path' argument to a subsequent call to |
| 2262 | * os_build_full_path; this allows a path to be built in multiple steps by |
| 2263 | * descending into subdirectories one at a time. |
| 2264 | * |
| 2265 | * Unix examples: |
| 2266 | * |
| 2267 | *. /home/mjr + deep.gam -> /home/mjr/deep.gam" |
| 2268 | *. /home/mjr + .. -> /home |
| 2269 | *. /home/mjr + ../deep.gam -> /home/deep.gam |
| 2270 | *. /home/mjr/ + deep.gam -> /home/mjr/deep.gam" |
| 2271 | *. games + deep.gam -> games/deep.gam" |
| 2272 | *. games/ + deep.gam -> games/deep.gam" |
| 2273 | *. /home/mjr + games/deep.gam -> /home/mjr/games/deep.gam" |
| 2274 | *. games + scifi/deep.gam -> games/scifi/deep.gam" |
| 2275 | *. /home/mjr + games -> /home/mjr/games" |
| 2276 | * |
| 2277 | * Mac examples: |
| 2278 | * |
| 2279 | *. Hard Disk: + deep.gam -> Hard Disk:deep.gam |
| 2280 | *. :games: + deep.gam -> :games:deep.gam |
| 2281 | *. :games:deep + ::test.gam -> :games:test.gam |
| 2282 | *. games + deep.gam -> :games:deep.gam |
| 2283 | *. Hard Disk: + :games:deep.gam -> Hard Disk:games:deep.gam |
| 2284 | *. games + :scifi:deep.gam -> :games:scifi:deep.gam |
| 2285 | *. Hard Disk: + games -> Hard Disk:games |
| 2286 | *. Hard Disk:games + scifi -> Hard Disk:games:scifi |
| 2287 | *. Hard Disk:games:scifi + deep.gam -> Hard Disk:games:scifi:deep.gam |
| 2288 | *. Hard Disk:games + :scifi:deep.gam -> Hard Disk:games:scifi:deep.gam |
| 2289 | * |
| 2290 | * VMS examples: |
| 2291 | * |
| 2292 | *. [home.mjr] + deep.gam -> [home.mjr]deep.gam |
| 2293 | *. [home.mjr] + [-]deep.gam -> [home]deep.gam |
| 2294 | *. mjr.dir + deep.gam -> [.mjr]deep.gam |
| 2295 | *. [home]mjr.dir + deep.gam -> [home.mjr]deep.gam |
| 2296 | *. [home] + [.mjr]deep.gam -> [home.mjr]deep.gam |
| 2297 | */ |
| 2298 | void os_build_full_path(char *fullpathbuf, size_t fullpathbuflen, |
| 2299 | const char *path, const char *filename); |
| 2300 | |
| 2301 | /* |
| 2302 | * Combine a path and a filename to form a full path to the file. This is |
| 2303 | * *almost* the same as os_build_full_path(), but if the 'filename' element |
| 2304 | * is a special relative link, such as Unix '.' or '..', this preserves |
| 2305 | * that special link in the final name. |
| 2306 | * |
| 2307 | * Unix examples: |
| 2308 | * |
| 2309 | *. /home/mjr + deep.gam -> /home/mjr/deep.gam |
| 2310 | *. /home/mjr + . -> /home/mjr/. |
| 2311 | *. /home/mjr + .. -> /home/mjr/.. |
| 2312 | * |
| 2313 | * Mac examples: |
| 2314 | * |
| 2315 | *. Hard Disk:games + deep.gam -> HardDisk:games:deep.gam |
| 2316 | *. Hard Disk:games + :: -> HardDisk:games:: |
| 2317 | * |
| 2318 | * VMS exmaples: |
| 2319 | * |
| 2320 | *. [home.mjr] + deep.gam -> [home.mjr]deep.gam |
| 2321 | *. [home.mjr] + [-] -> [home.mjr.-] |
| 2322 | */ |
| 2323 | void os_combine_paths(char *fullpathbuf, size_t pathbuflen, |
| 2324 | const char *path, const char *filename); |
| 2325 | |
| 2326 | |
| 2327 | /* |
| 2328 | * Get the absolute, fully qualified filename for a file. This fills in |
| 2329 | * 'result_buf' with the absolute path to the given file, taking into |
| 2330 | * account the current working directory and any other implied environment |
| 2331 | * information that affects the way the file system would resolve the given |
| 2332 | * file name to a specific file on disk if we opened the file now using |
| 2333 | * this name. |
| 2334 | * |
| 2335 | * The returned path should be in absolute path form, meaning that it's |
| 2336 | * independent of the current working directory or any other environment |
| 2337 | * settings. That is, this path should still refer to the same file even |
| 2338 | * if the working directory changes. |
| 2339 | * |
| 2340 | * Note that it's valid to get the absolute path for a file that doesn't |
| 2341 | * exist, or for a path with directory components that don't exist. For |
| 2342 | * example, a caller might generate the absolute path for a file that it's |
| 2343 | * about to create, or a hypothetical filename for path comparison |
| 2344 | * purposes. The function should succeed even if the file or any path |
| 2345 | * components don't exist. If the file is in relative format, and any path |
| 2346 | * elements don't exist but are syntactically well-formed, the result |
| 2347 | * should be the path obtained from syntactically combining the working |
| 2348 | * directory with the relative path. |
| 2349 | * |
| 2350 | * On many systems, a given file might be reachable through more than one |
| 2351 | * absolute path. For example, on Unix it might be possible to reach a |
| 2352 | * file through symbolic links to the file itself or to parent directories, |
| 2353 | * or hard links to the file. It's up to the implementation to determine |
| 2354 | * which path to use in such cases. |
| 2355 | * |
| 2356 | * On success, returns true. If it's not possible to resolve the file name |
| 2357 | * to an absolute path, the routine copies the original filename to the |
| 2358 | * result buffer exactly as given, and returns false. |
| 2359 | */ |
| 2360 | int os_get_abs_filename(char *result_buf, size_t result_buf_size, |
| 2361 | const char *filename); |
| 2362 | |
| 2363 | /* |
| 2364 | * Get the relative version of the given filename path 'filename', relative |
| 2365 | * to the given base directory 'basepath'. Both paths must be given in |
| 2366 | * absolute format. |
| 2367 | * |
| 2368 | * Returns true on success, false if it's not possible to rewrite the path |
| 2369 | * in relative terms. For example, on Windows, it's not possible to |
| 2370 | * express a path on the "D:" drive as relative to a base path on the "C:" |
| 2371 | * drive, since each drive letter has an independent root folder; there's |
| 2372 | * no namespace entity enclosing a drive letter's root folder. On |
| 2373 | * Unix-like systems where the entire namespace has a single hierarchical |
| 2374 | * root, it should always be possible to express any path relative to any |
| 2375 | * other. |
| 2376 | * |
| 2377 | * The result should be a relative path that can be combined with |
| 2378 | * 'basepath' using os_build_full_path() to reconstruct a path that |
| 2379 | * identifies the same file as the original 'filename' (it's not important |
| 2380 | * that this procedure would result in the identical string - it just has |
| 2381 | * to point to the same file). If it's not possible to express the |
| 2382 | * filename relative to the base path, fill in 'result_buf' with the |
| 2383 | * original filename and return false. |
| 2384 | * |
| 2385 | * Windows examples: |
| 2386 | * |
| 2387 | *. c:\mjr\games | c:\mjr\games\deep.gam -> deep.gam |
| 2388 | *. c:\mjr\games | c:\mjr\games\tads\deep.gam -> tads\deep.gam |
| 2389 | *. c:\mjr\games | c:\mjr\tads\deep.gam -> ..\tads\deep.gam |
| 2390 | *. c:\mjr\games | d:\deep.gam -> d:\deep.gam (and return false) |
| 2391 | * |
| 2392 | * Mac OS examples: |
| 2393 | * |
| 2394 | *. Mac HD:mjr:games | Mac HD:mjr:games:deep.gam -> deep.gam |
| 2395 | *. Mac HD:mjr:games | Mac HD:mjr:games:tads:deep.gam -> :tads:deep.gam |
| 2396 | *. Mac HD:mjr:games | Ext Disk:deep.gam -> Ext Disk:deep.gam (return false) |
| 2397 | * |
| 2398 | * VMS examples: |
| 2399 | * |
| 2400 | *. SYS$:[mjr.games] | SYS$:[mjr.games]deep.gam -> deep.gam |
| 2401 | *. SYS$:[mjr.games] | SYS$:[mjr.games.tads]deep.gam -> [.tads]deep.gam |
| 2402 | *. SYS$:[mjr.games] | SYS$:[mjr.tads]deep.gam -> [-.tads]deep.gam |
| 2403 | *. SYS$:[mjr.games] | DISK$:[mjr]deep.gam -> DISK$[mjr]deep.gam (ret false) |
| 2404 | */ |
| 2405 | int os_get_rel_path(char *result_buf, size_t result_buf_size, |
| 2406 | const char *basepath, const char *filename); |
| 2407 | |
| 2408 | /* |
| 2409 | * Determine if the given file is in the given directory. Returns true if |
| 2410 | * so, false if not. 'filename' is a relative or absolute file name; |
| 2411 | * 'path' is a relative or absolute directory path, such as one returned |
| 2412 | * from os_get_path_name(). |
| 2413 | * |
| 2414 | * If 'include_subdirs' is true, the function returns true if the file is |
| 2415 | * either directly in the directory 'path', OR it's in any subdirectory of |
| 2416 | * 'path'. If 'include_subdirs' is false, the function returns true only |
| 2417 | * if the file is directly in the given directory. |
| 2418 | * |
| 2419 | * If 'match_self' is true, the function returns true if 'filename' and |
| 2420 | * 'path' are the same directory; otherwise it returns false in this case. |
| 2421 | * |
| 2422 | * This routine is allowed to return "false negatives" - that is, it can |
| 2423 | * claim that the file isn't in the given directory even when it actually |
| 2424 | * is. The reason is that it's not always possible to determine for sure |
| 2425 | * that there's not some way for a given file path to end up in the given |
| 2426 | * directory. In contrast, a positive return must be reliable. |
| 2427 | * |
| 2428 | * If possible, this routine should fully resolve the names through the |
| 2429 | * file system to determine the path relationship, rather than merely |
| 2430 | * analyzing the text superficially. This can be important because many |
| 2431 | * systems have multiple ways to reach a given file, such as via symbolic |
| 2432 | * links on Unix; analyzing the syntax alone wouldn't reveal these multiple |
| 2433 | * pathways. |
| 2434 | * |
| 2435 | * SECURITY NOTE: If possible, implementations should fully resolve all |
| 2436 | * symbolic links, relative paths (e.g., Unix ".."), etc, before rendering |
| 2437 | * judgment. One important application for this routine is to determine if |
| 2438 | * a file is in a sandbox directory, to enforce security restrictions that |
| 2439 | * prevent a program from accessing files outside of a designated folder. |
| 2440 | * If the implementation fails to resolve symbolic links or relative paths, |
| 2441 | * a malicious program or user could bypass the security restriction by, |
| 2442 | * for example, creating a symbolic link within the sandbox directory that |
| 2443 | * points to the root folder. Implementations can avoid this loophole by |
| 2444 | * converting the file and directory names to absolute paths and resolving |
| 2445 | * all symbolic links and relative notation before comparing the paths. |
| 2446 | */ |
| 2447 | int os_is_file_in_dir(const char *filename, const char *path, |
| 2448 | int include_subdirs, int match_self); |
| 2449 | |
| 2450 | |
| 2451 | /* ------------------------------------------------------------------------ */ |
| 2452 | /* |
| 2453 | * Convert an OS filename path to URL-style format. This isn't a true URL |
| 2454 | * conversion; rather, it simply expresses a filename in Unix-style |
| 2455 | * notation, as a series of path elements separated by '/' characters. |
| 2456 | * Unlike true URLs, we don't use % encoding or a scheme prefix (file://, |
| 2457 | * etc). |
| 2458 | * |
| 2459 | * The result path never ends in a trailing '/', unless the entire result |
| 2460 | * path is "/". This is for consistency; even if the source path ends with |
| 2461 | * a local path separator, the result doesn't. |
| 2462 | * |
| 2463 | * If the local file system syntax uses '/' characters as ordinary filename |
| 2464 | * characters, these must be replaced with some other suitable character in |
| 2465 | * the result, since otherwise they'd be taken as path separators when the |
| 2466 | * URL is parsed. If possible, the substitution should be reversible with |
| 2467 | * respect to os_cvt_dir_url(), so that the same URL read back in on this |
| 2468 | * same platform will produce the same original filename. One particular |
| 2469 | * suggestion is that if the local system uses '/' to delimit what would be |
| 2470 | * a filename extension on other platforms, replace '/' with '.', since |
| 2471 | * this will provide reversibility as well as a good mapping if the URL is |
| 2472 | * read back in on another platform. |
| 2473 | * |
| 2474 | * The local equivalents of "." and "..", if they exist, are converted to |
| 2475 | * "." and ".." in the URL notation. |
| 2476 | * |
| 2477 | * Examples: |
| 2478 | * |
| 2479 | *. Windows: images\rooms\startroom.jpg -> images/rooms/startroom.jpg |
| 2480 | *. Windows: ..\startroom.jpg -> ../startroom.jpg |
| 2481 | *. Mac: :images:rooms:startroom.jpg -> images/rooms/startroom.jpg |
| 2482 | *. Mac: ::startroom.jpg -> ../startroom.jpg |
| 2483 | *. VMS: [.images.rooms]startroom.jpg -> images/rooms/startroom.jpg |
| 2484 | *. VMS: [-.images]startroom.jpg -> ../images/startroom.jpg |
| 2485 | *. Unix: images/rooms/startroom.jpg -> images/rooms/startroom.jpg |
| 2486 | *. Unix: ../images/startroom.jpg -> ../images/startroom.jpg |
| 2487 | * |
| 2488 | * If the local name is an absolute path in the local file system (e.g., |
| 2489 | * Unix /file, Windows C:\file), translate as follows. If the local |
| 2490 | * operating system uses a volume or device designator (Windows C:, VMS |
| 2491 | * SYS$DISK:, etc), make the first element of the path the exact local |
| 2492 | * syntax for the device designator: /C:/ on Windows, /SYS$DISK:/ on VMS, |
| 2493 | * etc. Include the local syntax for the device prefix. For a system like |
| 2494 | * Unix with a unified file system root ("/"), simply start with the root |
| 2495 | * directory. Examples: |
| 2496 | * |
| 2497 | *. Windows: C:\games\deep.gam -> /C:/games/deep.gam |
| 2498 | *. Windows: C:games\deep.gam -> /C:./games/deep.gam |
| 2499 | *. Windows: \\SERVER\DISK\games\deep.gam -> /\\SERVER/DISK/games/deep.gam |
| 2500 | *. Mac OS 9: Hard Disk:games:deep.gam -> /Hard Disk:/games/deep.gam |
| 2501 | *. VMS: SYS$DISK:[games]deep.gam -> /SYS$DISK:/games/deep.gam |
| 2502 | *. Unix: /games/deep.gam -> /games/deep.gam |
| 2503 | * |
| 2504 | * Rationale: it's effectively impossible to create a truly portable |
| 2505 | * representation of an absolute path. Operating systems are too different |
| 2506 | * in the way they represent root paths, and even if that were solvable, a |
| 2507 | * root path is essentially unusable across machines anyway because it |
| 2508 | * creates a dependency on the contents of a particular machine's disk. So |
| 2509 | * if we're called upon to translate an absolute path, we can forget about |
| 2510 | * trying to be truly portable and instead focus on round-trip fidelity - |
| 2511 | * i.e., making sure that applying os_cvt_url_dir() to our result recovers |
| 2512 | * the exact original path, assuming it's done on the same operating |
| 2513 | * system. The approach outlined above should achieve round-trip fidelity |
| 2514 | * when a local path is converted to a URL and back on the same machine, |
| 2515 | * since the local URL-to-path converter should recognize its own special |
| 2516 | * type of local absolute path prefix. It also produces reasonable results |
| 2517 | * on other platforms - see the os_cvt_url_dir() comments below for |
| 2518 | * examples of the decoding results for absolute paths moved to new |
| 2519 | * platforms. The result when a device-rooted absolute path is encoded on |
| 2520 | * one machine and then decoded on another will generally be a local path |
| 2521 | * with a root on the default device/volume and an outermost directory with |
| 2522 | * a name based on the original machine's device/volume name. This |
| 2523 | * obviously won't reproduce the exact original path, but since that's |
| 2524 | * impossible anyway, this is probably as good an approximation as we can |
| 2525 | * create. |
| 2526 | * |
| 2527 | * Character sets: the input could be in local or UTF-8 character sets. |
| 2528 | * The implementation shouldn't care, though - just treat bytes in the |
| 2529 | * range 0-127 as plain ASCII, and everything else as opaque. I.e., do not |
| 2530 | * quote or otherwise modify characters outside the 0-127 range. |
| 2531 | */ |
| 2532 | void os_cvt_dir_url(char *result_buf, size_t result_buf_size, |
| 2533 | const char *src_path); |
| 2534 | |
| 2535 | /* |
| 2536 | * Convert a URL-style path into a filename path expressed in the local |
| 2537 | * file system's syntax. Fills in result_buf with a file path, constructed |
| 2538 | * using the local file system syntax, that corresponds to the path in |
| 2539 | * src_url expressed in URL-style syntax. Examples: |
| 2540 | * |
| 2541 | * images/rooms/startroom.jpg -> |
| 2542 | *. Windows -> images\rooms\startroom.jpg |
| 2543 | *. Mac OS 9 -> :images:rooms:startroom.jpg |
| 2544 | *. VMS -> [.images.rooms]startroom.jpg |
| 2545 | * |
| 2546 | * The source format isn't a true URL; it's simply a series of path |
| 2547 | * elements separated by '/' characters. Unlike true URLs, our input |
| 2548 | * format doesn't use % encoding and doesn't have a scheme (file://, etc). |
| 2549 | * (Any % in the source is treated as an ordinary character and left as-is, |
| 2550 | * even if it looks like a %XX sequence. Anything that looks like a scheme |
| 2551 | * prefix is left as-is, with any // treated as path separators. |
| 2552 | * |
| 2553 | * images/file%20name.jpg -> |
| 2554 | *. Windows -> images\file%20name.jpg |
| 2555 | * |
| 2556 | * file://images/file.jpg -> |
| 2557 | *. Windows -> file_\\images\file.jpg |
| 2558 | * |
| 2559 | * Any characters in the path that are invalid in the local file system |
| 2560 | * naming rules are converted to "_", unless "_" is itself invalid, in |
| 2561 | * which case they're converted to "X". One exception is that if '/' is a |
| 2562 | * valid local filename character (rather than a path separator as it is on |
| 2563 | * Unix and Windows), it can be used as the replacement for the character |
| 2564 | * that os_cvt_dir_url uses as its replacement for '/', so that this |
| 2565 | * substitution is reversible when a URL is generated and then read back in |
| 2566 | * on this same platform. |
| 2567 | * |
| 2568 | * images/file:name.jpg -> |
| 2569 | *. Windows -> images\file_name.jpg |
| 2570 | *. Mac OS 9 -> :images:file_name.jpg |
| 2571 | *. Unix -> images/file:name.jpg |
| 2572 | * |
| 2573 | * The path elements "." and ".." are specifically defined as having their |
| 2574 | * Unix meanings: "." is an alias for the preceding path element, or the |
| 2575 | * working directory if it's the first element, and ".." is an alias for |
| 2576 | * the parent of the preceding element. When these appear as path |
| 2577 | * elements, this routine translates them to the appropriate local |
| 2578 | * conventions. "." may be translated simply by removing it from the path, |
| 2579 | * since it reiterates the previous path element. ".." may be translated |
| 2580 | * by removing the previous element - HOWEVER, if ".." appears as the first |
| 2581 | * element, it has to be retained and translated to the equivalent local |
| 2582 | * notation, since it will have to be applied later, when the result_buf |
| 2583 | * path is actually used to open a file, at which point it will combined |
| 2584 | * with the working directory or another base path. |
| 2585 | * |
| 2586 | *. /images/../file.jpg -> [Windows] file.jpg |
| 2587 | *. ../images/file.jpg -> |
| 2588 | *. Windows -> ..\images\file.jpg |
| 2589 | *. Mac OS 9 -> ::images:file.jpg |
| 2590 | *. VMS -> [-.images]file.jpg |
| 2591 | * |
| 2592 | * If the URL path is absolute (starts with a '/'), the routine inspects |
| 2593 | * the path to see if it was created by the same OS, according to the local |
| 2594 | * rules for converting absolute paths in os_cvt_dir_url() (see). If so, |
| 2595 | * we reverse the encoding done there. If it doesn't appear that the name |
| 2596 | * was created by the same operating system - that is, if reversing the |
| 2597 | * encoding doesn't produce a valid local filename - then we create a local |
| 2598 | * absolute path as follows. If the local system uses device/volume |
| 2599 | * designators, we start with the current working device/volume or some |
| 2600 | * other suitable default volume. We then add the first element of the |
| 2601 | * path, if any, as the root directory name, applying the usual "_" or "X" |
| 2602 | * substitution for any characters that aren't allowed in local names. The |
| 2603 | * rest of the path is handled in the usual fashion. |
| 2604 | * |
| 2605 | *. /images/file.jpg -> |
| 2606 | *. Windows -> \images\file.jpg |
| 2607 | *. Unix -> /images/file.jpg |
| 2608 | * |
| 2609 | *. /c:/images/file.jpg -> |
| 2610 | *. Windows -> c:\images\file.jpg |
| 2611 | *. Unix -> /c:/images/file.jpg |
| 2612 | *. VMS -> SYS$DISK:[c__.images]file.jpg |
| 2613 | * |
| 2614 | *. /Hard Disk:/images/file.jpg -> |
| 2615 | *. Windows -> \Hard Disk_\images\file.jpg |
| 2616 | *. Unix -> SYS$DISK:[Hard_Disk_.images]file.jpg |
| 2617 | * |
| 2618 | * Note how the device/volume prefix becomes the top-level directory when |
| 2619 | * moving a path across machines. It's simply not possible to reconstruct |
| 2620 | * the exact original path in such cases, since device/volume syntax rules |
| 2621 | * have little in common across systems. But this seems like a good |
| 2622 | * approximation in that (a) it produces a valid local path, and (b) it |
| 2623 | * gives the user a reasonable basis for creating a set of folders to mimic |
| 2624 | * the original source system, if they want to use that approach to port |
| 2625 | * the data rather than just changing the paths internally in the source |
| 2626 | * material. |
| 2627 | * |
| 2628 | * Character sets: use the same rules as for os_cvt_dir_url(). |
| 2629 | */ |
| 2630 | void os_cvt_url_dir(char *result_buf, size_t result_buf_size, |
| 2631 | const char *src_url); |
| 2632 | |
| 2633 | |
| 2634 | /* ------------------------------------------------------------------------ */ |
| 2635 | /* |
| 2636 | * Get a suitable seed for a random number generator; should use the system |
| 2637 | * clock or some other source of an unpredictable and changing seed value. |
| 2638 | */ |
| 2639 | void os_rand(long *val); |
| 2640 | |
| 2641 | /* |
| 2642 | * Generate random bytes for use in seeding a PRNG (pseudo-random number |
| 2643 | * generator). This is an extended version of os_rand() for PRNGs that use |
| 2644 | * large seed vectors containing many bytes, rather than the simple 32-bit |
| 2645 | * seed that os_rand() assumes. |
| 2646 | * |
| 2647 | * As with os_rand(), this function isn't meant to be used directly as a |
| 2648 | * random number source for ongoing use - instead, this is intended mostly |
| 2649 | * for seeding a PRNG, which will then be used as the primary source of |
| 2650 | * random numbers. The big problem with using this routine directly as a |
| 2651 | * randomness source is that some implementations might rely heavily on |
| 2652 | * environmental randomness, such as the real-time clock or OS usage |
| 2653 | * statistics. Such sources tend to provide reasonable entropy from one |
| 2654 | * run to the next, but not within a single session, as the underlying data |
| 2655 | * sources don't change rapidly enough. |
| 2656 | * |
| 2657 | * Ideally, this routine should generate *truly* random bytes obtained from |
| 2658 | * hardware sources. Not all systems can provide that, though, so true |
| 2659 | * randomness isn't guaranteed. Here are the suggested implementation |
| 2660 | * options, in descending order of desirability: |
| 2661 | * |
| 2662 | * 1. Use a hardware source of true randomness, such as a /dev/rand type |
| 2663 | * of device. However, note that this call should return reasonably |
| 2664 | * quickly, so always use a non-blocking source. Some Unix /dev/rand |
| 2665 | * devices, for example, can block indefinitely to allow sufficient entropy |
| 2666 | * to accumulate. |
| 2667 | * |
| 2668 | * 2. Use a cryptographic random number source provided by the OS. Some |
| 2669 | * systems provide this as an API service. If going this route, be sure |
| 2670 | * that the OS generator is itself "seeded" with some kind of true |
| 2671 | * randomness source, as it defeats the whole purpose if you always return |
| 2672 | * a fixed pseudo-random sequence each time the program runs. |
| 2673 | * |
| 2674 | * 3. Use whatever true random sources are available locally to seed a |
| 2675 | * software pseudo-random number generator, then generate bytes from your |
| 2676 | * PRNG. Some commonly available sources of true randomness are a |
| 2677 | * high-resolution timer, the system clock, the current process ID, |
| 2678 | * logged-in user ID, environment variables, uninitialized pages of memory, |
| 2679 | * the IP address; each of these sources might give you a few bits of |
| 2680 | * entropy at best, so the best bet is to use an ensemble. You could, for |
| 2681 | * example, concatenate a bunch of this type of information together and |
| 2682 | * calculate an MD5 or SHA1 hash to mix the bits more thoroughly. For the |
| 2683 | * PRNG, use a cryptographic generator. (You could use the ISAAC generator |
| 2684 | * from TADS 3, as that's a crypto PRNG, but it's probably better to use a |
| 2685 | * different generator here since TADS 3 is going to turn around and use |
| 2686 | * this function's output to seed ISAAC - seeding one ISAAC instance with |
| 2687 | * another ISAAC's output seems likely to magnify any weaknesses in the |
| 2688 | * ISAAC algorithm.) Note that this option is basically the DIY version of |
| 2689 | * option 2. Option 2 is better because the OS probably has access to |
| 2690 | * better sources of true randomness than an application does. |
| 2691 | */ |
| 2692 | void os_gen_rand_bytes(unsigned char *buf, size_t len); |
| 2693 | |
| 2694 | |
| 2695 | /* ------------------------------------------------------------------------ */ |
| 2696 | /* |
| 2697 | * Display routines. |
| 2698 | * |
| 2699 | * Our display model is a simple stdio-style character stream. |
| 2700 | * |
| 2701 | * In addition, we provide an optional "status line," which is a |
| 2702 | * non-scrolling area where a line of text can be displayed. If the status |
| 2703 | * line is supported, text should only be displayed in this area when |
| 2704 | * os_status() is used to enter status-line mode (mode 1); while in status |
| 2705 | * line mode, text is written to the status line area, otherwise (mode 0) |
| 2706 | * it's written to the normal main text area. The status line is normally |
| 2707 | * shown in a different color to set it off from the rest of the text. |
| 2708 | * |
| 2709 | * The OS layer can provide its own formatting (word wrapping in |
| 2710 | * particular) if it wants, in which case it should also provide pagination |
| 2711 | * using os_more_prompt(). |
| 2712 | */ |
| 2713 | |
| 2714 | /* |
| 2715 | * OS_MAXWIDTH - the maximum width of a line of text. Most platforms use |
| 2716 | * 135 for this, but you can use more or less as appropriate. If you use |
| 2717 | * OS-level line wrapping, then the true width of a text line is |
| 2718 | * irrelevant, and the portable code will use this merely for setting its |
| 2719 | * internal buffer sizes. |
| 2720 | * |
| 2721 | * This must be defined in the os_xxx.h header file for each platform. |
| 2722 | */ |
| 2723 | /*#define OS_MAXWIDTH 135 - example only: define for real in os_xxx.h header*/ |
| 2724 | |
| 2725 | /* |
| 2726 | * Print a string on the console. These routines come in two varieties: |
| 2727 | * |
| 2728 | * os_printz - write a NULL-TERMINATED string |
| 2729 | *. os_print - write a COUNTED-LENGTH string, which may not end with a null |
| 2730 | * |
| 2731 | * These two routines are identical except that os_printz() takes a string |
| 2732 | * which is terminated by a null byte, and os_print() instead takes an |
| 2733 | * explicit length, and a string that may not end with a null byte. |
| 2734 | * |
| 2735 | * os_printz(str) may be implemented as simply os_print(str, strlen(str)). |
| 2736 | * |
| 2737 | * The string is written in one of three ways, depending on the status mode |
| 2738 | * set by os_status(): |
| 2739 | * |
| 2740 | * status mode == 0 -> write to main text window |
| 2741 | *. status mode == 1 -> write to status line |
| 2742 | *. anything else -> do not display the text at all |
| 2743 | * |
| 2744 | * Implementations are free to omit any status line support, in which case |
| 2745 | * they should simply suppress all output when the status mode is anything |
| 2746 | * other than zero. |
| 2747 | * |
| 2748 | * The following special characters must be recognized in the displayed |
| 2749 | * text: |
| 2750 | * |
| 2751 | * '\n' - newline: end the current line and move the cursor to the start of |
| 2752 | * the next line. If the status line is supported, and the current status |
| 2753 | * mode is 1 (i.e., displaying in the status line), then two special rules |
| 2754 | * apply to newline handling: newlines preceding any other text should be |
| 2755 | * ignored, and a newline following any other text should set the status |
| 2756 | * mode to 2, so that all subsequent output is suppressed until the status |
| 2757 | * mode is changed with an explicit call by the client program to |
| 2758 | * os_status(). |
| 2759 | * |
| 2760 | * '\r' - carriage return: end the current line and move the cursor back to |
| 2761 | * the beginning of the current line. Subsequent output is expected to |
| 2762 | * overwrite the text previously on this same line. The implementation |
| 2763 | * may, if desired, IMMEDIATELY clear the previous text when the '\r' is |
| 2764 | * written, rather than waiting for subsequent text to be displayed. |
| 2765 | * |
| 2766 | * All other characters may be assumed to be ordinary printing characters. |
| 2767 | * The routine need not check for any other special characters. |
| 2768 | * |
| 2769 | */ |
| 2770 | void os_printz(const char *str); |
| 2771 | void os_print(const char *str, size_t len); |
| 2772 | |
| 2773 | /* |
| 2774 | * Print to the debugger console. These routines are for interactive |
| 2775 | * debugger builds only: they display the given text to a separate window |
| 2776 | * within the debugger UI (separate from the main game command window) |
| 2777 | * where the debugger displays status information specific to the debugging |
| 2778 | * session (such as compiler/build output, breakpoint status messages, |
| 2779 | * etc). For example, TADS Workbench on Windows displays these messages in |
| 2780 | * its "Debug Log" window. |
| 2781 | * |
| 2782 | * These routines only need to be implemented for interactive debugger |
| 2783 | * builds, such as TADS Workbench on Windows. These can be omitted for |
| 2784 | * regular interpreter builds. |
| 2785 | */ |
| 2786 | void os_dbg_printf(const char *fmt, ...); |
| 2787 | void os_dbg_vprintf(const char *fmt, va_list args); |
| 2788 | |
| 2789 | /* |
| 2790 | * Allocating sprintf and vsprintf. These work like the regular C library |
| 2791 | * sprintf and vsprintf funtions, but they allocate a return buffer that's |
| 2792 | * big enough to hold the result, rather than formatting into a caller's |
| 2793 | * buffer. This protects against buffer overruns and ensures that the |
| 2794 | * result isn't truncated. |
| 2795 | * |
| 2796 | * On return, '*bufptr' is filled in with a pointer to a buffer allocated |
| 2797 | * with osmalloc(). This buffer contains the formatted string result. The |
| 2798 | * caller is responsible for freeing the buffer by calling osfree(). |
| 2799 | * *bufptr can be null on return if an error occurs. |
| 2800 | * |
| 2801 | * The return value is the number of bytes written to the allocated buffer, |
| 2802 | * not including the null terminator. If an error occurs, the return value |
| 2803 | * is -1 and *bufptr is undefined. |
| 2804 | * |
| 2805 | * Many modern C libraries provide equivalents of these, usually called |
| 2806 | * asprintf() and vasprintf(), respectively. |
| 2807 | */ |
| 2808 | /* int os_asprintf(char **bufptr, const char *fmt, ...); */ |
| 2809 | int os_vasprintf(char **bufptr, const char *fmt, va_list ap); |
| 2810 | |
| 2811 | |
| 2812 | /* |
| 2813 | * Set the status line mode. There are three possible settings: |
| 2814 | * |
| 2815 | * 0 -> main text mode. In this mode, all subsequent text written with |
| 2816 | * os_print() and os_printz() is to be displayed to the main text area. |
| 2817 | * This is the normal mode that should be in effect initially. This mode |
| 2818 | * stays in effect until an explicit call to os_status(). |
| 2819 | * |
| 2820 | * 1 -> statusline mode. In this mode, text written with os_print() and |
| 2821 | * os_printz() is written to the status line, which is usually rendered as |
| 2822 | * a one-line area across the top of the terminal screen or application |
| 2823 | * window. In statusline mode, leading newlines ('\n' characters) are to |
| 2824 | * be ignored, and any newline following any other character must change |
| 2825 | * the mode to 2, as though os_status(2) had been called. |
| 2826 | * |
| 2827 | * 2 -> suppress mode. In this mode, all text written with os_print() and |
| 2828 | * os_printz() must simply be ignored, and not displayed at all. This mode |
| 2829 | * stays in effect until an explicit call to os_status(). |
| 2830 | */ |
| 2831 | void os_status(int stat); |
| 2832 | |
| 2833 | /* get the status line mode */ |
| 2834 | int os_get_status(); |
| 2835 | |
| 2836 | /* |
| 2837 | * Set the score value. This displays the given score and turn counts on |
| 2838 | * the status line. In most cases, these values are displayed at the right |
| 2839 | * edge of the status line, in the format "score/turns", but the format is |
| 2840 | * up to the implementation to determine. In most cases, this can simply |
| 2841 | * be implemented as follows: |
| 2842 | * |
| 2843 | *. void os_score(int score, int turncount) |
| 2844 | *. { |
| 2845 | *. char buf[40]; |
| 2846 | *. sprintf(buf, "%d/%d", score, turncount); |
| 2847 | *. os_strsc(buf); |
| 2848 | *. } |
| 2849 | */ |
| 2850 | void os_score(int score, int turncount); |
| 2851 | |
| 2852 | /* display a string in the score area in the status line */ |
| 2853 | void os_strsc(const char *p); |
| 2854 | |
| 2855 | /* clear the screen */ |
| 2856 | void oscls(void); |
| 2857 | |
| 2858 | /* redraw the screen */ |
| 2859 | void os_redraw(void); |
| 2860 | |
| 2861 | /* flush any buffered display output */ |
| 2862 | void os_flush(void); |
| 2863 | |
| 2864 | /* |
| 2865 | * Update the display - process any pending drawing immediately. This |
| 2866 | * only needs to be implemented for operating systems that use |
| 2867 | * event-driven drawing based on window invalidations; the Windows and |
| 2868 | * Macintosh GUI's both use this method for drawing window contents. |
| 2869 | * |
| 2870 | * The purpose of this routine is to refresh the display prior to a |
| 2871 | * potentially long-running computation, to avoid the appearance that the |
| 2872 | * application is frozen during the computation delay. |
| 2873 | * |
| 2874 | * Platforms that don't need to process events in the main thread in order |
| 2875 | * to draw their window contents do not need to do anything here. In |
| 2876 | * particular, text-mode implementations generally don't need to implement |
| 2877 | * this routine. |
| 2878 | * |
| 2879 | * This routine doesn't absolutely need a non-empty implementation on any |
| 2880 | * platform, but it will provide better visual feedback if implemented for |
| 2881 | * those platforms that do use event-driven drawing. |
| 2882 | */ |
| 2883 | void os_update_display(); |
| 2884 | |
| 2885 | |
| 2886 | /* ------------------------------------------------------------------------ */ |
| 2887 | /* |
| 2888 | * Set text attributes. Text subsequently displayed through os_print() and |
| 2889 | * os_printz() are to be displayed with the given attributes. |
| 2890 | * |
| 2891 | * 'attr' is a (bitwise-OR'd) combination of OS_ATTR_xxx values. A value |
| 2892 | * of zero indicates normal text, with no extra attributes. |
| 2893 | */ |
| 2894 | void os_set_text_attr(int attr); |
| 2895 | |
| 2896 | /* attribute code: bold-face */ |
| 2897 | #define OS_ATTR_BOLD 0x0001 |
| 2898 | |
| 2899 | /* attribute code: italic */ |
| 2900 | #define OS_ATTR_ITALIC 0x0002 |
| 2901 | |
| 2902 | /* attribute code: monospace |
| 2903 | * (Introduced for the Glk port -- AW 2023-01-20) */ |
| 2904 | #define OS_ATTR_MONOSP 0x0004 |
| 2905 | |
| 2906 | /* |
| 2907 | * Abstract attribute codes. Each platform can choose a custom rendering |
| 2908 | * for these by #defining them before this point, in the OS-specific header |
| 2909 | * (osdos.h, osmac.h, etc). We provide *default* definitions in case the |
| 2910 | * platform doesn't define these. |
| 2911 | * |
| 2912 | * For compatibility with past versions, we treat HILITE, EM, and BOLD as |
| 2913 | * equivalent. Platforms that can display multiple kinds of text |
| 2914 | * attributes (boldface and italic, say) should feel free to use more |
| 2915 | * conventional HTML mappings, such as EM->italic and STRONG->bold. |
| 2916 | */ |
| 2917 | |
| 2918 | /* |
| 2919 | * "Highlighted" text, as appropriate to the local platform. On most |
| 2920 | * text-mode platforms, the only kind of rendering variation possible is a |
| 2921 | * brighter or intensified color. If actual bold-face is available, that |
| 2922 | * can be used instead. This is the attribute used for text enclosed in a |
| 2923 | * TADS2 "\( \)" sequence. |
| 2924 | */ |
| 2925 | #ifndef OS_ATTR_HILITE |
| 2926 | # define OS_ATTR_HILITE OS_ATTR_BOLD |
| 2927 | #endif |
| 2928 | |
| 2929 | /* HTML <em> attribute - by default, map this to bold-face */ |
| 2930 | #ifndef OS_ATTR_EM |
| 2931 | # define OS_ATTR_EM OS_ATTR_BOLD |
| 2932 | #endif |
| 2933 | |
| 2934 | /* HTML <strong> attribute - by default, this has no effect */ |
| 2935 | #ifndef OS_ATTR_STRONG |
| 2936 | # define OS_ATTR_STRONG 0 |
| 2937 | #endif |
| 2938 | |
| 2939 | |
| 2940 | /* ------------------------------------------------------------------------ */ |
| 2941 | /* |
| 2942 | * Colors. |
| 2943 | * |
| 2944 | * There are two ways of encoding a color. First, a specific color can be |
| 2945 | * specified as an RGB (red-green-blue) value, with discreet levels for |
| 2946 | * each component's intensity, ranging from 0 to 255. Second, a color can |
| 2947 | * be "parameterized," which doesn't specify a color in absolute terms but |
| 2948 | * rather specified one of a number of pre-defined *types* of colors; |
| 2949 | * these pre-defined types can be chosen by the OS implementation, or, on |
| 2950 | * some systems, selected by the user via a preferences mechanism. |
| 2951 | * |
| 2952 | * The os_color_t type encodes a color in 32 bits. The high-order 8 bits |
| 2953 | * of a color value give the parameterized color identifier, or are set to |
| 2954 | * zero to indicate an RGB color. An RGB color is encoded in the |
| 2955 | * low-order 24 bits, via the following formula: |
| 2956 | * |
| 2957 | * (R << 16) + (G << 8) + B |
| 2958 | * |
| 2959 | * R specifies the intensity of the red component of the color, G green, |
| 2960 | * and B blue. Each of R, G, and B must be in the range 0-255. |
| 2961 | */ |
| 2962 | typedef unsigned long os_color_t; |
| 2963 | |
| 2964 | /* encode an R, G, B triplet into an os_color_t value */ |
| 2965 | #define os_rgb_color(r, g, b) (((r) << 16) + ((g) << 8) + (b)) |
| 2966 | |
| 2967 | /* |
| 2968 | * Determine if a color is given as an RGB value or as a parameterized |
| 2969 | * color value. Returns true if the color is given as a parameterized |
| 2970 | * color (one of the OS_COLOR_xxx values), false if it's given as an |
| 2971 | * absolute RGB value. |
| 2972 | */ |
| 2973 | #define os_color_is_param(color) (((color) & 0xFF000000) != 0) |
| 2974 | |
| 2975 | /* get the red/green/blue components of an os_color_t value */ |
| 2976 | #define os_color_get_r(color) ((int)(((color) >> 16) & 0xFF)) |
| 2977 | #define os_color_get_g(color) ((int)(((color) >> 8) & 0xFF)) |
| 2978 | #define os_color_get_b(color) ((int)((color) & 0xFF)) |
| 2979 | |
| 2980 | /* |
| 2981 | * Parameterized color codes. These are os_color_t values that indicate |
| 2982 | * colors by type, rather than by absolute RGB values. |
| 2983 | */ |
| 2984 | |
| 2985 | /* |
| 2986 | * "transparent" - applicable to backgrounds only, this specifies that the |
| 2987 | * current screen background color should be used |
| 2988 | */ |
| 2989 | #define OS_COLOR_P_TRANSPARENT ((os_color_t)0x01000000) |
| 2990 | |
| 2991 | /* "normal text" color (as set via user preferences, if applicable) */ |
| 2992 | #define OS_COLOR_P_TEXT ((os_color_t)0x02000000) |
| 2993 | |
| 2994 | /* normal text background color (from user preferences) */ |
| 2995 | #define OS_COLOR_P_TEXTBG ((os_color_t)0x03000000) |
| 2996 | |
| 2997 | /* "status line" text color (as set via user preferences, if applicable) */ |
| 2998 | #define OS_COLOR_P_STATUSLINE ((os_color_t)0x04000000) |
| 2999 | |
| 3000 | /* status line background color (from user preferences) */ |
| 3001 | #define OS_COLOR_P_STATUSBG ((os_color_t)0x05000000) |
| 3002 | |
| 3003 | /* input text color (as set via user preferences, if applicable) */ |
| 3004 | #define OS_COLOR_P_INPUT ((os_color_t)0x06000000) |
| 3005 | |
| 3006 | /* |
| 3007 | * Set the text foreground and background colors. This sets the text |
| 3008 | * color for subsequent os_printf() and os_vprintf() calls. |
| 3009 | * |
| 3010 | * The background color can be OS_COLOR_TRANSPARENT, in which case the |
| 3011 | * background color is "inherited" from the current screen background. |
| 3012 | * Note that if the platform is capable of keeping old text for |
| 3013 | * "scrollback," then the transparency should be a permanent attribute of |
| 3014 | * the character - in other words, it should not be mapped to the current |
| 3015 | * screen color in the scrollback buffer, because doing so would keep the |
| 3016 | * current screen color even if the screen color changes in the future. |
| 3017 | * |
| 3018 | * Text color support is optional. If the platform doesn't support text |
| 3019 | * colors, this can simply do nothing. If the platform supports text |
| 3020 | * colors, but the requested color or attributes cannot be displayed, the |
| 3021 | * implementation should use the best available approximation. |
| 3022 | */ |
| 3023 | void os_set_text_color(os_color_t fg, os_color_t bg); |
| 3024 | |
| 3025 | /* |
| 3026 | * Set the screen background color. This sets the text color for the |
| 3027 | * background of the screen. If possible, this should immediately redraw |
| 3028 | * the main text area with this background color. The color is given as an |
| 3029 | * OS_COLOR_xxx value. |
| 3030 | * |
| 3031 | * If the platform is capable of redisplaying the existing text, then any |
| 3032 | * existing text that was originally displayed with 'transparent' |
| 3033 | * background color should be redisplayed with the new screen background |
| 3034 | * color. In other words, the 'transparent' background color of previously |
| 3035 | * drawn text should be a permanent attribute of the character - the color |
| 3036 | * should not be mapped on display to the then-current background color, |
| 3037 | * because doing so would lose the transparency and thus retain the old |
| 3038 | * screen color on a screen color change. |
| 3039 | */ |
| 3040 | void os_set_screen_color(os_color_t color); |
| 3041 | |
| 3042 | |
| 3043 | /* ------------------------------------------------------------------------ */ |
| 3044 | /* |
| 3045 | * os_plain() - Use plain ascii mode for the display. If possible and |
| 3046 | * necessary, turn off any text formatting effects, such as cursor |
| 3047 | * positioning, highlighting, or coloring. If this routine is called, |
| 3048 | * the terminal should be treated as a simple text stream; users might |
| 3049 | * wish to use this mode for things like text-to-speech converters. |
| 3050 | * |
| 3051 | * Purely graphical implementations that cannot offer a textual mode |
| 3052 | * (such as Mac OS or Windows) can ignore this setting. |
| 3053 | * |
| 3054 | * If this routine is to be called, it must be called BEFORE os_init(). |
| 3055 | * The implementation should set a flag so that os_init() will know to |
| 3056 | * set up the terminal for plain text output. |
| 3057 | */ |
| 3058 | #ifndef os_plain |
| 3059 | /* |
| 3060 | * some platforms (e.g. Mac OS) define this to be a null macro, so don't |
| 3061 | * define a prototype in those cases |
| 3062 | */ |
| 3063 | void os_plain(void); |
| 3064 | #endif |
| 3065 | |
| 3066 | /* |
| 3067 | * Set the game title. The output layer calls this routine when a game |
| 3068 | * sets its title (via an HTML <title> tag, for example). If it's |
| 3069 | * convenient to do so, the OS layer can use this string to set a window |
| 3070 | * caption, or whatever else makes sense on each system. Most |
| 3071 | * character-mode implementations will provide an empty implementation, |
| 3072 | * since there's not usually any standard way to show the current |
| 3073 | * application title on a character-mode display. |
| 3074 | */ |
| 3075 | void os_set_title(const char *title); |
| 3076 | |
| 3077 | /* |
| 3078 | * Show the system-specific MORE prompt, and wait for the user to respond. |
| 3079 | * Before returning, remove the MORE prompt from the screen. |
| 3080 | * |
| 3081 | * This routine is only used and only needs to be implemented when the OS |
| 3082 | * layer takes responsibility for pagination; this will be the case on |
| 3083 | * most systems that use proportionally-spaced (variable-pitch) fonts or |
| 3084 | * variable-sized windows, since on such platforms the OS layer must do |
| 3085 | * most of the formatting work, leaving the standard output layer unable |
| 3086 | * to guess where pagination should occur. |
| 3087 | * |
| 3088 | * If the portable output formatter handles the MORE prompt, which is the |
| 3089 | * usual case for character-mode or terminal-style implementations, this |
| 3090 | * routine is not used and you don't need to provide an implementation. |
| 3091 | * Note that HTML TADS provides an implementation of this routine, because |
| 3092 | * the HTML renderer handles line breaking and thus must handle |
| 3093 | * pagination. |
| 3094 | */ |
| 3095 | void os_more_prompt(); |
| 3096 | |
| 3097 | /* |
| 3098 | * Interpreter Class Configuration. |
| 3099 | * |
| 3100 | * If this is a TEXT-ONLY interpreter: DO NOT define USE_HTML. |
| 3101 | * |
| 3102 | * If this is a MULTIMEDIA (HTML TADS) interpreter: #define USE_HTML |
| 3103 | * |
| 3104 | * (This really should be called something like OS_USE_HTML - the USE_ name |
| 3105 | * is for historical reasons. This purpose of this macro is to configure |
| 3106 | * the tads 2 VM-level output formatter's line breaking and MORE mode |
| 3107 | * behavior. In HTML mode, the VM-level formatter knows that it's feeding |
| 3108 | * its output to a page layout engine, so the VM-level output is just a |
| 3109 | * text stream. In plain-text mode, the VM formatter *is* the page layout |
| 3110 | * engine, so it needs to do all of the word wrapping and MORE prompting |
| 3111 | * itself. The tads 3 output layer does NOT use this macro for its |
| 3112 | * equivalent configuration, but instead has different .cpp files for the |
| 3113 | * different modes, and you simply link in the one for the configuration |
| 3114 | * you want.) |
| 3115 | */ |
| 3116 | /* #define USE_HTML */ |
| 3117 | |
| 3118 | |
| 3119 | /* |
| 3120 | * Enter HTML mode. This is only used when the run-time is compiled |
| 3121 | * with the USE_HTML flag defined. This call instructs the renderer |
| 3122 | * that HTML sequences should be parsed; until this call is made, the |
| 3123 | * renderer should not interpret output as HTML. Non-HTML |
| 3124 | * implementations do not need to define this routine, since the |
| 3125 | * run-time will not call it if USE_HTML is not defined. |
| 3126 | */ |
| 3127 | void os_start_html(void); |
| 3128 | |
| 3129 | /* exit HTML mode */ |
| 3130 | void os_end_html(void); |
| 3131 | |
| 3132 | /* |
| 3133 | * Global variables with the height and width (in character cells - rows |
| 3134 | * and columns) of the main text display area into which os_printf |
| 3135 | * displays. The height and width are given in text lines and character |
| 3136 | * columns, respectively. The portable code can use these values to |
| 3137 | * format text for display via os_printf(); for example, the caller can |
| 3138 | * use the width to determine where to put line breaks. |
| 3139 | * |
| 3140 | * These values are only needed for systems where os_printf() doesn't |
| 3141 | * perform its own word-wrap formatting. On systems such as the Mac, |
| 3142 | * where os_printf() performs word wrapping, these sizes aren't really |
| 3143 | * important because the portable code doesn't need to perform any real |
| 3144 | * formatting. |
| 3145 | * |
| 3146 | * These variables reflect the size of the "main text area," which is the |
| 3147 | * area of the screen excluding the status line and any "banner" windows |
| 3148 | * (as created with the os_banner_xxx() interfaces). |
| 3149 | * |
| 3150 | * The OS code must initialize these variables during start-up, and must |
| 3151 | * adjust them whenever the display size is changed by user action or |
| 3152 | * other external events (for example, if we're running inside a terminal |
| 3153 | * window, and the user resizes the window, the OS code must recalculate |
| 3154 | * the layout and adjust these accordingly). |
| 3155 | */ |
| 3156 | extern int G_os_pagelength; |
| 3157 | extern int G_os_linewidth; |
| 3158 | |
| 3159 | /* |
| 3160 | * Global flag that tells the output formatter whether to count lines |
| 3161 | * that it's displaying against the total on the screen so far. If this |
| 3162 | * variable is true, lines are counted, and the screen is paused with a |
| 3163 | * [More] message when it's full. When not in MORE mode, lines aren't |
| 3164 | * counted. This variable should be set to false when displaying text |
| 3165 | * that doesn't count against the current page, such as status line |
| 3166 | * information. |
| 3167 | * |
| 3168 | * This flag should not be modified by OS code. Instead, the output |
| 3169 | * formatter will set this flag according to its current state; the OS |
| 3170 | * code can use this flag to determine whether or not to display a MORE |
| 3171 | * prompt during os_printf()-type operations. Note that this flag is |
| 3172 | * normally interesting to the OS code only when the OS code itself is |
| 3173 | * handling the MORE prompt. |
| 3174 | */ |
| 3175 | extern int G_os_moremode; |
| 3176 | |
| 3177 | /* |
| 3178 | * Global buffer containing the name of the byte-code file (the "game |
| 3179 | * file") loaded into the VM. This is used only where applicable, which |
| 3180 | * generally means in TADS Interpreter builds. In other application |
| 3181 | * builds, this is simply left empty. The application is responsible for |
| 3182 | * setting this during start-up (or wherever else the byte-code filename |
| 3183 | * becomes known or changes). |
| 3184 | */ |
| 3185 | extern char G_os_gamename[OSFNMAX]; |
| 3186 | |
| 3187 | /* |
| 3188 | * Set non-stop mode. This tells the OS layer that it should disable any |
| 3189 | * MORE prompting it would normally do. |
| 3190 | * |
| 3191 | * This routine is needed only when the OS layer handles MORE prompting; on |
| 3192 | * character-mode platforms, where the prompting is handled in the portable |
| 3193 | * console layer, this can be a dummy implementation. |
| 3194 | */ |
| 3195 | void os_nonstop_mode(int flag); |
| 3196 | |
| 3197 | /* |
| 3198 | * Update progress display with current info, if appropriate. This can |
| 3199 | * be used to provide a status display during compilation. Most |
| 3200 | * command-line implementations will just ignore this notification; this |
| 3201 | * can be used for GUI compiler implementations to provide regular |
| 3202 | * display updates during compilation to show the progress so far. |
| 3203 | */ |
| 3204 | /* void os_progress(const char *fname, unsigned long linenum); */ |
| 3205 | |
| 3206 | /* |
| 3207 | * Set busy cursor. If 'flag' is true, provide a visual representation |
| 3208 | * that the system or application is busy doing work. If 'flag' is |
| 3209 | * false, remove any visual "busy" indication and show normal status. |
| 3210 | * |
| 3211 | * We provide a prototype here if your osxxx.h header file does not |
| 3212 | * #define a macro for os_csr_busy. On many systems, this function has |
| 3213 | * no effect at all, so the osxxx.h header file simply #define's it to |
| 3214 | * do an empty macro. |
| 3215 | */ |
| 3216 | #ifndef os_csr_busy |
| 3217 | void os_csr_busy(int flag); |
| 3218 | #endif |
| 3219 | |
| 3220 | |
| 3221 | /* ------------------------------------------------------------------------ */ |
| 3222 | /* |
| 3223 | * User Input Routines |
| 3224 | */ |
| 3225 | |
| 3226 | /* |
| 3227 | * Ask the user for a filename, using a system-dependent dialog or other |
| 3228 | * mechanism. Returns one of the OS_AFE_xxx status codes (see below). |
| 3229 | * |
| 3230 | * prompt_type is the type of prompt to provide -- this is one of the |
| 3231 | * OS_AFP_xxx codes (see below). The OS implementation doesn't need to |
| 3232 | * pay any attention to this parameter, but it can be used if desired to |
| 3233 | * determine the type of dialog to present if the system provides |
| 3234 | * different types of dialogs for different types of operations. |
| 3235 | * |
| 3236 | * file_type is one of the OSFTxxx codes for system file type. The OS |
| 3237 | * implementation is free to ignore this information, but can use it to |
| 3238 | * filter the list of files displayed if desired; this can also be used |
| 3239 | * to apply a default suffix on systems that use suffixes to indicate |
| 3240 | * file type. If OSFTUNK is specified, it means that no filtering |
| 3241 | * should be performed, and no default suffix should be applied. |
| 3242 | */ |
| 3243 | int os_askfile(const char *prompt, char *fname_buf, int fname_buf_len, |
| 3244 | int prompt_type, os_filetype_t file_type); |
| 3245 | |
| 3246 | /* |
| 3247 | * os_askfile status codes |
| 3248 | */ |
| 3249 | |
| 3250 | /* success */ |
| 3251 | #define OS_AFE_SUCCESS 0 |
| 3252 | |
| 3253 | /* |
| 3254 | * Generic failure - this is largely provided for compatibility with |
| 3255 | * past versions, in which only zero and non-zero error codes were |
| 3256 | * meaningful; since TRUE is defined as 1 on most platforms, we assume |
| 3257 | * that 1 is probably the generic non-zero error code that most OS |
| 3258 | * implementations have traditionally used. In addition, this can be |
| 3259 | * used to indicate any other error for which there is no more specific |
| 3260 | * error code. |
| 3261 | */ |
| 3262 | #define OS_AFE_FAILURE 1 |
| 3263 | |
| 3264 | /* user cancelled */ |
| 3265 | #define OS_AFE_CANCEL 2 |
| 3266 | |
| 3267 | /* |
| 3268 | * os_askfile prompt types |
| 3269 | * |
| 3270 | * Important note: do not change these values when porting TADS. These |
| 3271 | * values can be used by games, so they must be the same on all |
| 3272 | * platforms. |
| 3273 | */ |
| 3274 | #define OS_AFP_OPEN 1 /* choose an existing file to open for reading */ |
| 3275 | #define OS_AFP_SAVE 2 /* choose a filename for saving to a file */ |
| 3276 | |
| 3277 | |
| 3278 | /* |
| 3279 | * Read a string of input. Fills in the buffer with a null-terminated |
| 3280 | * string containing a line of text read from the standard input. The |
| 3281 | * returned string should NOT contain a trailing newline sequence. On |
| 3282 | * success, returns 'buf'; on failure, including end of file, returns a |
| 3283 | * null pointer. |
| 3284 | */ |
| 3285 | unsigned char *os_gets(unsigned char *buf, size_t bufl); |
| 3286 | |
| 3287 | /* |
| 3288 | * Read a string of input with an optional timeout. This behaves like |
| 3289 | * os_gets(), in that it allows the user to edit a line of text (ideally |
| 3290 | * using the same editing keys that os_gets() does), showing the line of |
| 3291 | * text under construction during editing. This routine differs from |
| 3292 | * os_gets() in that it returns if the given timeout interval expires |
| 3293 | * before the user presses Return (or the local equivalent). |
| 3294 | * |
| 3295 | * If the user presses Return before the timeout expires, we store the |
| 3296 | * command line in the given buffer, just as os_gets() would, and we return |
| 3297 | * OS_EVT_LINE. We also update the display in the same manner that |
| 3298 | * os_gets() would, by moving the cursor to a new line and scrolling the |
| 3299 | * displayed text as needed. |
| 3300 | * |
| 3301 | * If a timeout occurs before the user presses Return, we store the command |
| 3302 | * line so far in the given buffer, statically store the cursor position, |
| 3303 | * insert mode, buffer text, and anything else relevant to the editing |
| 3304 | * state, and we return OS_EVT_TIMEOUT. |
| 3305 | * |
| 3306 | * If the implementation does not support the timeout operation, this |
| 3307 | * routine should simply return OS_EVT_NOTIMEOUT immediately when called; |
| 3308 | * the routine should not allow the user to perform any editing if the |
| 3309 | * timeout is not supported. Callers must use the ordinary os_gets() |
| 3310 | * routine, which has no timeout capabilities, if the timeout is not |
| 3311 | * supported. |
| 3312 | * |
| 3313 | * When we return OS_EVT_TIMEOUT, the caller is responsible for doing one |
| 3314 | * of two things. |
| 3315 | * |
| 3316 | * The first possibility is that the caller performs some work that doesn't |
| 3317 | * require any display operations (in other words, the caller doesn't |
| 3318 | * invoke os_printf, os_getc, or anything else that would update the |
| 3319 | * display), and then calls os_gets_timeout() again. In this case, we will |
| 3320 | * use the editing state that we statically stored before we returned |
| 3321 | * OS_EVT_TIMEOUT to continue editing where we left off. This allows the |
| 3322 | * caller to perform some computation in the middle of user command editing |
| 3323 | * without interrupting the user - the extra computation is transparent to |
| 3324 | * the user, because we act as though we were still in the midst of the |
| 3325 | * original editing. |
| 3326 | * |
| 3327 | * The second possibility is that the caller wants to update the display. |
| 3328 | * In this case, the caller must call os_gets_cancel() BEFORE making any |
| 3329 | * display changes. Then, the caller must do any post-input work of its |
| 3330 | * own, such as updating the display mode (for example, closing HTML font |
| 3331 | * tags that were opened at the start of the input). The caller is now |
| 3332 | * free to do any display work it wants. |
| 3333 | * |
| 3334 | * If we have information stored from a previous call that was interrupted |
| 3335 | * by a timeout, and os_gets_cancel(TRUE) was never called, we will resume |
| 3336 | * editing where we left off when the cancelled call returned; this means |
| 3337 | * that we'll restore the cursor position, insertion state, and anything |
| 3338 | * else relevant. Note that if os_gets_cancel(FALSE) was called, we must |
| 3339 | * re-display the command line under construction, but if os_gets_cancel() |
| 3340 | * was never called, we will not have to make any changes to the display at |
| 3341 | * all. |
| 3342 | * |
| 3343 | * Note that when resuming an interrupted editing session (interrupted via |
| 3344 | * os_gets_cancel()), the caller must re-display the prompt prior to |
| 3345 | * invoking this routine. |
| 3346 | * |
| 3347 | * Note that we can return OS_EVT_EOF in addition to the other codes |
| 3348 | * mentioned above. OS_EVT_EOF indicates that an error occurred reading, |
| 3349 | * which usually indicates that the application is being terminated or that |
| 3350 | * some hardware error occurred reading the keyboard. |
| 3351 | * |
| 3352 | * If 'use_timeout' is false, the timeout should be ignored. Without a |
| 3353 | * timeout, the function behaves the same as os_gets(), except that it will |
| 3354 | * resume editing of a previously-interrupted command line if appropriate. |
| 3355 | * (This difference is why the timeout is optional: a caller might not need |
| 3356 | * a timeout, but might still want to resume a previous input that did time |
| 3357 | * out, in which case the caller would invoke this routine with |
| 3358 | * use_timeout==FALSE. The regular os_gets() would not satisfy this need, |
| 3359 | * because it cannot resume an interrupted input.) |
| 3360 | * |
| 3361 | * Note that a zero timeout has the same meaning as for os_get_event(): if |
| 3362 | * input is available IMMEDIATELY, return the input, otherwise return |
| 3363 | * immediately with the OS_EVT_TIMEOUT result code. |
| 3364 | */ |
| 3365 | int os_gets_timeout(unsigned char *buf, size_t bufl, |
| 3366 | unsigned long timeout_in_milliseconds, int use_timeout); |
| 3367 | |
| 3368 | /* |
| 3369 | * Cancel an interrupted editing session. This MUST be called if any |
| 3370 | * output is to be displayed after a call to os_gets_timeout() returns |
| 3371 | * OS_EVT_TIMEOUT. |
| 3372 | * |
| 3373 | * 'reset' indicates whether or not we will forget the input state saved |
| 3374 | * by os_gets_timeout() when it last returned. If 'reset' is true, we'll |
| 3375 | * clear the input state, so that the next call to os_gets_timeout() will |
| 3376 | * start with an empty input buffer. If 'reset' is false, we will retain |
| 3377 | * the previous input state, if any; this means that the next call to |
| 3378 | * os_gets_timeout() will re-display the same input buffer that was under |
| 3379 | * construction when it last returned. |
| 3380 | * |
| 3381 | * This routine need not be called if os_gets_timeout() is to be called |
| 3382 | * again with no other output operations between the previous |
| 3383 | * os_gets_timeout() call and the next one. |
| 3384 | * |
| 3385 | * Note that this routine needs only a trivial implementation when |
| 3386 | * os_gets_timeout() is not supported (i.e., the function always returns |
| 3387 | * OS_EVT_NOTIMEOUT). |
| 3388 | */ |
| 3389 | void os_gets_cancel(int reset); |
| 3390 | |
| 3391 | /* |
| 3392 | * Read a character from the keyboard. For extended keystrokes, this |
| 3393 | * function returns zero, and then returns the CMD_xxx code for the |
| 3394 | * extended keystroke on the next call. For example, if the user presses |
| 3395 | * the up-arrow key, the first call to os_getc() should return 0, and the |
| 3396 | * next call should return CMD_UP. Refer to the CMD_xxx codes below. |
| 3397 | * |
| 3398 | * os_getc() should return a high-level, translated command code for |
| 3399 | * command editing. This means that, where a functional interpretation of |
| 3400 | * a key and the raw key-cap interpretation both exist as CMD_xxx codes, |
| 3401 | * the functional interpretation should be returned. For example, on Unix, |
| 3402 | * Ctrl-E is conventionally used in command editing to move to the end of |
| 3403 | * the line, following Emacs key bindings. Hence, os_getc() should return |
| 3404 | * CMD_END for this keystroke, rather than a single 0x05 character (ASCII |
| 3405 | * Ctrl-E), because CMD_END is the high-level command code for the |
| 3406 | * operation. |
| 3407 | * |
| 3408 | * The translation ability of this function allows for system-dependent key |
| 3409 | * mappings to functional meanings. |
| 3410 | */ |
| 3411 | int os_getc(void); |
| 3412 | |
| 3413 | /* |
| 3414 | * Read a character from the keyboard, following the same protocol as |
| 3415 | * os_getc() for CMD_xxx codes (i.e., when an extended keystroke is |
| 3416 | * encountered, os_getc_raw() returns zero, then returns the CMD_xxx code |
| 3417 | * on the subsequent call). |
| 3418 | * |
| 3419 | * This function differs from os_getc() in that this function returns the |
| 3420 | * low-level, untranslated key code whenever possible. This means that, |
| 3421 | * when a functional interpretation of a key and the raw key-cap |
| 3422 | * interpretation both exist as CMD_xxx codes, this function returns the |
| 3423 | * key-cap interpretation. For the Unix Ctrl-E example in the comments |
| 3424 | * describing os_getc() above, this function should return 5 (the ASCII |
| 3425 | * code for Ctrl-E), because the ASCII control character interpretation is |
| 3426 | * the low-level key code. |
| 3427 | * |
| 3428 | * This function should return all control keys using their ASCII control |
| 3429 | * codes, whenever possible. Similarly, this function should return ASCII |
| 3430 | * 27 for the Escape key, if possible. |
| 3431 | * |
| 3432 | * For keys for which there is no portable ASCII representation, this |
| 3433 | * should return the CMD_xxx sequence. So, this function acts exactly the |
| 3434 | * same as os_getc() for arrow keys, function keys, and other special keys |
| 3435 | * that have no ASCII representation. This function returns a |
| 3436 | * non-translated version ONLY when an ASCII representation exists - in |
| 3437 | * practice, this means that this function and os_getc() vary only for CTRL |
| 3438 | * keys and Escape. |
| 3439 | */ |
| 3440 | int os_getc_raw(void); |
| 3441 | |
| 3442 | |
| 3443 | /* wait for a character to become available from the keyboard */ |
| 3444 | void os_waitc(void); |
| 3445 | |
| 3446 | /* |
| 3447 | * Constants for os_getc() when returning commands. When used for |
| 3448 | * command line editing, special keys (arrows, END, etc.) should cause |
| 3449 | * os_getc() to return 0, and return the appropriate CMD_ value on the |
| 3450 | * NEXT call. Hence, os_getc() must keep the appropriate information |
| 3451 | * around statically for the next call when a command key is issued. |
| 3452 | * |
| 3453 | * The comments indicate which CMD_xxx codes are "translated" codes and |
| 3454 | * which are "raw"; the difference is that, when a particular keystroke |
| 3455 | * could be interpreted as two different CMD_xxx codes, one translated |
| 3456 | * and the other raw, os_getc() should always return the translated |
| 3457 | * version of the key, and os_getc_raw() should return the raw version. |
| 3458 | */ |
| 3459 | #define CMD_UP 1 /* move up/up arrow (translated) */ |
| 3460 | #define CMD_DOWN 2 /* move down/down arrow (translated) */ |
| 3461 | #define CMD_RIGHT 3 /* move right/right arrow (translated) */ |
| 3462 | #define CMD_LEFT 4 /* move left/left arrow (translated) */ |
| 3463 | #define CMD_END 5 /* move cursor to end of line (translated) */ |
| 3464 | #define CMD_HOME 6 /* move cursor to start of line (translated) */ |
| 3465 | #define CMD_DEOL 7 /* delete to end of line (translated) */ |
| 3466 | #define CMD_KILL 8 /* delete entire line (translated) */ |
| 3467 | #define CMD_DEL 9 /* delete current character (translated) */ |
| 3468 | #define CMD_SCR 10 /* toggle scrollback mode (translated) */ |
| 3469 | #define CMD_PGUP 11 /* page up (translated) */ |
| 3470 | #define CMD_PGDN 12 /* page down (translated) */ |
| 3471 | #define CMD_TOP 13 /* top of file (translated) */ |
| 3472 | #define CMD_BOT 14 /* bottom of file (translated) */ |
| 3473 | #define CMD_F1 15 /* function key F1 (raw) */ |
| 3474 | #define CMD_F2 16 /* function key F2 (raw) */ |
| 3475 | #define CMD_F3 17 /* function key F3 (raw) */ |
| 3476 | #define CMD_F4 18 /* function key F4 (raw) */ |
| 3477 | #define CMD_F5 19 /* function key F5 (raw) */ |
| 3478 | #define CMD_F6 20 /* function key F6 (raw) */ |
| 3479 | #define CMD_F7 21 /* function key F7 (raw) */ |
| 3480 | #define CMD_F8 22 /* function key F8 (raw) */ |
| 3481 | #define CMD_F9 23 /* function key F9 (raw) */ |
| 3482 | #define CMD_F10 24 /* function key F10 (raw) */ |
| 3483 | #define CMD_CHOME 25 /* control-home (raw) */ |
| 3484 | #define CMD_TAB 26 /* tab (translated) */ |
| 3485 | #define CMD_SF2 27 /* shift-F2 (raw) */ |
| 3486 | /* not used (obsolete) - 28 */ |
| 3487 | #define CMD_WORD_LEFT 29 /* word left (ctrl-left on dos) (translated) */ |
| 3488 | #define CMD_WORD_RIGHT 30 /* word right (ctrl-right on dos) (translated) */ |
| 3489 | #define CMD_WORDKILL 31 /* delete word right (translated) */ |
| 3490 | #define CMD_EOF 32 /* end-of-file (raw) */ |
| 3491 | #define CMD_BREAK 33 /* break (Ctrl-C or local equivalent) (translated) */ |
| 3492 | #define CMD_INS 34 /* insert key (raw) */ |
| 3493 | |
| 3494 | |
| 3495 | /* |
| 3496 | * ALT-keys - add alphabetical code to CMD_ALT: ALT-A == CMD_ALT + 0, |
| 3497 | * ALT-B == CMD_ALT + 1, ALT-C == CMD_ALT + 2, etc |
| 3498 | * |
| 3499 | * These keys are all raw (untranslated). |
| 3500 | */ |
| 3501 | #define CMD_ALT 128 /* start of ALT keys */ |
| 3502 | |
| 3503 | |
| 3504 | /* ------------------------------------------------------------------------ */ |
| 3505 | /* |
| 3506 | * Event information structure for os_get_event. The appropriate union |
| 3507 | * member should be filled in, depending on the type of event that |
| 3508 | * occurs. |
| 3509 | */ |
| 3510 | union os_event_info_t |
| 3511 | { |
| 3512 | /* |
| 3513 | * OS_EVT_KEY - this returns the one or two characters of the |
| 3514 | * keystroke. If the key is an extended key, so that os_getc() would |
| 3515 | * return a two-character sequence for the keystroke, the first |
| 3516 | * character should be zero and the second the extended key code. |
| 3517 | * Otherwise, the first character should simply be the ASCII key code. |
| 3518 | * |
| 3519 | * The key code here is the "raw" keycode, equivalent to the codes |
| 3520 | * returned by os_getc_raw(). Note in particular that this means that |
| 3521 | * CTRL and Escape keys are presented as one-byte ASCII control |
| 3522 | * characters, not as two-byte CMD_xxx sequences. |
| 3523 | * |
| 3524 | * For multi-byte character sets (Shift-JIS, for example), note that |
| 3525 | * os_get_event() must NOT return a complete two-byte character here. |
| 3526 | * The two bytes here are exclusively used to represent special |
| 3527 | * CMD_xxx keys (such as arrow keys and function keys). For a |
| 3528 | * keystroke that is represented in a multi-byte character set using |
| 3529 | * more than one byte, os_get_event() must return a series of events. |
| 3530 | * Return an ordinary OS_EVT_KEY for the first byte of the sequence, |
| 3531 | * putting the byte in key[0]; then, return the second byte as a |
| 3532 | * separate OS_EVT_KEY as the next event; and so on for any additional |
| 3533 | * bytes. This will allow callers that are not multibyte-aware to |
| 3534 | * treat multi-byte characters as though they were sequences of |
| 3535 | * one-byte characters. |
| 3536 | */ |
| 3537 | int key[2]; |
| 3538 | |
| 3539 | /* |
| 3540 | * OS_EVT_HREF - this returns the text of the HREF as a |
| 3541 | * null-terminated string. |
| 3542 | */ |
| 3543 | char href[256]; |
| 3544 | |
| 3545 | /* command ID (for OS_EVT_COMMAND) */ |
| 3546 | int cmd_id; |
| 3547 | }; |
| 3548 | typedef union os_event_info_t os_event_info_t; |
| 3549 | |
| 3550 | /* |
| 3551 | * Event types for os_get_event |
| 3552 | */ |
| 3553 | |
| 3554 | /* invalid/no event */ |
| 3555 | #define OS_EVT_NONE 0x0000 |
| 3556 | |
| 3557 | /* OS_EVT_KEY - user typed a key on the keyboard */ |
| 3558 | #define OS_EVT_KEY 0x0001 |
| 3559 | |
| 3560 | /* OS_EVT_TIMEOUT - no event occurred before the timeout elapsed */ |
| 3561 | #define OS_EVT_TIMEOUT 0x0002 |
| 3562 | |
| 3563 | /* |
| 3564 | * OS_EVT_HREF - user clicked on a <A HREF> link. This only applies to |
| 3565 | * the HTML-enabled run-time. |
| 3566 | */ |
| 3567 | #define OS_EVT_HREF 0x0003 |
| 3568 | |
| 3569 | /* |
| 3570 | * OS_EVT_NOTIMEOUT - caller requested a timeout, but timeout is not |
| 3571 | * supported by this version of the run-time |
| 3572 | */ |
| 3573 | #define OS_EVT_NOTIMEOUT 0x0004 |
| 3574 | |
| 3575 | /* |
| 3576 | * OS_EVT_EOF - an error occurred reading the event. This generally |
| 3577 | * means that the application is quitting or we can no longer read from |
| 3578 | * the keyboard or terminal. |
| 3579 | */ |
| 3580 | #define OS_EVT_EOF 0x0005 |
| 3581 | |
| 3582 | /* |
| 3583 | * OS_EVT_LINE - user entered a line of text on the keyboard. This event |
| 3584 | * is not returned from os_get_event(), but rather from os_gets_timeout(). |
| 3585 | */ |
| 3586 | #define OS_EVT_LINE 0x0006 |
| 3587 | |
| 3588 | |
| 3589 | /* |
| 3590 | * Get an input event. The event types are shown above. If use_timeout is |
| 3591 | * false, this routine should simply wait until one of the events it |
| 3592 | * recognizes occurs, then return the appropriate information on the event. |
| 3593 | * If use_timeout is true, this routine should return OS_EVT_TIMEOUT after |
| 3594 | * the given number of milliseconds elapses if no event occurs first. |
| 3595 | * |
| 3596 | * This function is not obligated to obey the timeout. If a timeout is |
| 3597 | * specified and it is not possible to obey the timeout, the function |
| 3598 | * should simply return OS_EVT_NOTIMEOUT. The trivial implementation thus |
| 3599 | * checks for a timeout, returns an error if specified, and otherwise |
| 3600 | * simply waits for the user to press a key. |
| 3601 | * |
| 3602 | * A timeout value of 0 does *not* mean that there's no timeout (i.e., it |
| 3603 | * doesn't mean we should wait indefinitely) - that's specified by passing |
| 3604 | * FALSE for use_timeout. A zero timeout also doesn't meant that the |
| 3605 | * function should unconditionally return OS_EVT_TIMEOUT. Instead, a zero |
| 3606 | * timeout specifically means that IF an event is available IMMEDIATELY, |
| 3607 | * without blocking the thread, we should return that event; otherwise we |
| 3608 | * should immediately return a timeout event. |
| 3609 | */ |
| 3610 | int os_get_event(unsigned long timeout_in_milliseconds, int use_timeout, |
| 3611 | os_event_info_t *info); |
| 3612 | |
| 3613 | |
| 3614 | /* ------------------------------------------------------------------------ */ |
| 3615 | /* |
| 3616 | * Extended os_get_event() codes. |
| 3617 | * |
| 3618 | * THESE ARE NOT USED in the basic osifc implementation - these are only |
| 3619 | * used if the interpreter supports the "extended" interface defined in |
| 3620 | * osifcext.h. |
| 3621 | */ |
| 3622 | |
| 3623 | /* |
| 3624 | * System menu command event. Some interpreters (such as HTML TADS for |
| 3625 | * Windows) provide menu commands for common system-level game verbs - |
| 3626 | * SAVE, RESTORE, UNDO, QUIT. By default, these commands are returned as |
| 3627 | * literal command strings ("save", "restore", etc) via os_gets(), as |
| 3628 | * though the user had typed the commands at the keyboard. The extended OS |
| 3629 | * interface allows the program to receive these commands via |
| 3630 | * os_get_event(). When a command is enabled for os_get_event() via the |
| 3631 | * extended OS interface, and the player selects the command via a menu (or |
| 3632 | * toolbar button, etc) during a call to os_get_event(), os_get_event() |
| 3633 | * returns this event code, with the menu ID stored in the cmd_id field of |
| 3634 | * the event structure. |
| 3635 | */ |
| 3636 | #define OS_EVT_COMMAND 0x0100 |
| 3637 | |
| 3638 | /* command IDs for OS_EVT_COMMAND */ |
| 3639 | #define OS_CMD_NONE 0x0000 /* invalid command ID, for internal use */ |
| 3640 | #define OS_CMD_SAVE 0x0001 /* save game */ |
| 3641 | #define OS_CMD_RESTORE 0x0002 /* restore game */ |
| 3642 | #define OS_CMD_UNDO 0x0003 /* undo last turn */ |
| 3643 | #define OS_CMD_QUIT 0x0004 /* quit game */ |
| 3644 | #define OS_CMD_CLOSE 0x0005 /* close the game window */ |
| 3645 | #define OS_CMD_HELP 0x0006 /* show game help */ |
| 3646 | |
| 3647 | /* highest command ID used in this version of the interface */ |
| 3648 | #define OS_CMD_LAST 0x0006 |
| 3649 | |
| 3650 | |
| 3651 | /* ------------------------------------------------------------------------ */ |
| 3652 | /* |
| 3653 | * Ask for input through a dialog. |
| 3654 | * |
| 3655 | * 'prompt' is a text string to display as a prompting message. For |
| 3656 | * graphical systems, this message should be displayed in the dialog; |
| 3657 | * for text systems, this should be displayed on the terminal after a |
| 3658 | * newline. |
| 3659 | * |
| 3660 | * 'standard_button_set' is one of the OS_INDLG_xxx values defined |
| 3661 | * below, or zero. If this value is zero, no standard button set is to |
| 3662 | * be used; the custom set of buttons defined in 'buttons' is to be used |
| 3663 | * instead. If this value is non-zero, the appropriate set of standard |
| 3664 | * buttons, with labels translated to the local language if possible, is |
| 3665 | * to be used. |
| 3666 | * |
| 3667 | * 'buttons' is an array of strings to use as button labels. |
| 3668 | * 'button_count' gives the number of entries in the 'buttons' array. |
| 3669 | * 'buttons' and 'button_count' are ignored if 'standard_button_set' is |
| 3670 | * non-zero, since a standard set of buttons is used instead. If |
| 3671 | * 'buttons' and 'button_count' are to be used, each entry contains the |
| 3672 | * label of a button to show. |
| 3673 | */ |
| 3674 | /* |
| 3675 | * An ampersand ('&') character in a label string indicates that the |
| 3676 | * next character after the '&' is to be used as the short-cut key for |
| 3677 | * the button, if supported. The '&' should NOT be displayed in the |
| 3678 | * string; instead, the character should be highlighted according to |
| 3679 | * local system conventions. On Windows, for example, the short-cut |
| 3680 | * character should be shown underlined; on a text display, the response |
| 3681 | * might be shown with the short-cut character enclosed in parentheses. |
| 3682 | * If there is no local convention for displaying a short-cut character, |
| 3683 | * then the '&' should simply be removed from the displayed text. |
| 3684 | * |
| 3685 | * The short-cut key specified by each '&' character should be used in |
| 3686 | * processing responses. If the user presses the key corresponding to a |
| 3687 | * button's short-cut, the effect should be the same as if the user |
| 3688 | * clicked the button with the mouse. If local system conventions don't |
| 3689 | * allow for short-cut keys, any short-cut keys can be ignored. |
| 3690 | * |
| 3691 | * 'default_index' is the 1-based index of the button to use as the |
| 3692 | * default. If this value is zero, there is no default response. If |
| 3693 | * the user performs the appropriate system-specific action to select |
| 3694 | * the default response for the dialog, this is the response that is to |
| 3695 | * be selected. On Windows, for example, pressing the "Return" key |
| 3696 | * should select this item. |
| 3697 | * |
| 3698 | * 'cancel_index' is the 1-based index of the button to use as the |
| 3699 | * cancel response. If this value is zero, there is no cancel response. |
| 3700 | * This is the response to be used if the user cancels the dialog using |
| 3701 | * the appropriate system-specific action. On Windows, for example, |
| 3702 | * pressing the "Escape" key should select this item. |
| 3703 | */ |
| 3704 | /* |
| 3705 | * icon_id is one of the OS_INDLG_ICON_xxx values defined below. If |
| 3706 | * possible, an appropriate icon should be displayed in the dialog. |
| 3707 | * This can be ignored in text mode, and also in GUI mode if there is no |
| 3708 | * appropriate system icon. |
| 3709 | * |
| 3710 | * The return value is the 1-based index of the response selected. If |
| 3711 | * an error occurs, return 0. |
| 3712 | */ |
| 3713 | int os_input_dialog(int icon_id, const char *prompt, int standard_button_set, |
| 3714 | const char **buttons, int button_count, |
| 3715 | int default_index, int cancel_index); |
| 3716 | |
| 3717 | /* |
| 3718 | * Standard button set ID's |
| 3719 | */ |
| 3720 | |
| 3721 | /* OK */ |
| 3722 | #define OS_INDLG_OK 1 |
| 3723 | |
| 3724 | /* OK, Cancel */ |
| 3725 | #define OS_INDLG_OKCANCEL 2 |
| 3726 | |
| 3727 | /* Yes, No */ |
| 3728 | #define OS_INDLG_YESNO 3 |
| 3729 | |
| 3730 | /* Yes, No, Cancel */ |
| 3731 | #define OS_INDLG_YESNOCANCEL 4 |
| 3732 | |
| 3733 | /* |
| 3734 | * Dialog icons |
| 3735 | */ |
| 3736 | |
| 3737 | /* no icon */ |
| 3738 | #define OS_INDLG_ICON_NONE 0 |
| 3739 | |
| 3740 | /* warning */ |
| 3741 | #define OS_INDLG_ICON_WARNING 1 |
| 3742 | |
| 3743 | /* information */ |
| 3744 | #define OS_INDLG_ICON_INFO 2 |
| 3745 | |
| 3746 | /* question */ |
| 3747 | #define OS_INDLG_ICON_QUESTION 3 |
| 3748 | |
| 3749 | /* error */ |
| 3750 | #define OS_INDLG_ICON_ERROR 4 |
| 3751 | |
| 3752 | |
| 3753 | |
| 3754 | /* ------------------------------------------------------------------------ */ |
| 3755 | /* |
| 3756 | * OS main entrypoint |
| 3757 | */ |
| 3758 | int os0main(int oargc, char **oargv, |
| 3759 | int (*mainfn)(int, char **, char *), |
| 3760 | const char *before, const char *config); |
| 3761 | |
| 3762 | /* |
| 3763 | * new-style OS main entrypoint - takes an application container context |
| 3764 | */ |
| 3765 | int os0main2(int oargc, char **oargv, |
| 3766 | int (*mainfn)(int, char **, struct appctxdef *, char *), |
| 3767 | const char *before, const char *config, |
| 3768 | struct appctxdef *appctx); |
| 3769 | |
| 3770 | /* |
| 3771 | * OBSOLETE - Get filename from startup parameter, if possible; returns |
| 3772 | * true and fills in the buffer with the parameter filename on success, |
| 3773 | * false if no parameter file could be found. |
| 3774 | * |
| 3775 | * (This was used until TADS 2.2.5 for the benefit of the Mac interpreter, |
| 3776 | * and interpreters on systems with similar desktop shells, to allow the |
| 3777 | * user to launch the terp by double-clicking on a saved game file. The |
| 3778 | * terp would read the launch parameters, discover that a saved game had |
| 3779 | * been used to invoke it, and would then stash away the saved game info |
| 3780 | * for later retrieval from this function. This functionality was replaced |
| 3781 | * in 2.2.5 with a command-line parameter: the terp now uses the desktop |
| 3782 | * launch data to synthesize a suitable argv[] vectro to pass to os0main() |
| 3783 | * or os0main2(). This function should now simply be stubbed out - it |
| 3784 | * should simply return FALSE.) |
| 3785 | */ |
| 3786 | int os_paramfile(char *buf); |
| 3787 | |
| 3788 | /* |
| 3789 | * Initialize. This should be called during program startup to |
| 3790 | * initialize the OS layer and check OS-specific command-line arguments. |
| 3791 | * |
| 3792 | * If 'prompt' and 'buf' are non-null, and there are no arguments on the |
| 3793 | * given command line, the OS code can use the prompt to ask the user to |
| 3794 | * supply a filename, then store the filename in 'buf' and set up |
| 3795 | * argc/argv to give a one-argument command string. (This mechanism for |
| 3796 | * prompting for a filename is obsolescent, and is retained for |
| 3797 | * compatibility with a small number of existing implementations only; |
| 3798 | * new implementations should ignore this mechanism and leave the |
| 3799 | * argc/argv values unchanged.) |
| 3800 | */ |
| 3801 | int os_init(int *argc, char *argv[], const char *prompt, |
| 3802 | char *buf, int bufsiz); |
| 3803 | |
| 3804 | /* |
| 3805 | * Termination functions. There are three main termination functions, |
| 3806 | * described individually below; here's a brief overview of the |
| 3807 | * relationship among the functions. The important thing to realize is |
| 3808 | * that these functions have completely independent purposes; they should |
| 3809 | * never call one another, and they should never do any of the work that's |
| 3810 | * intended for the others. |
| 3811 | * |
| 3812 | * os_uninit() is meant to undo the effects of os_init(). On many |
| 3813 | * systems, os_init() has some global effect, such as setting the terminal |
| 3814 | * to some special input or output mode. os_uninit's purpose is to undo |
| 3815 | * these global effects, returning the terminal mode (and whatever else) |
| 3816 | * to the conditions they were in at program startup. Portable callers |
| 3817 | * are meant to call this routine at some point before terminating if they |
| 3818 | * ever called os_init(). Note that this routine DOES NOT terminate the |
| 3819 | * program - it should simply undo anything that os_init() did and return, |
| 3820 | * to let the caller do any additional termination work of its own. |
| 3821 | * |
| 3822 | * os_expause() optionally pauses before termination, to allow the user to |
| 3823 | * acknowledge any text the program displays just before exiting. This |
| 3824 | * doesn't have to do anything at all, but it's useful on systems where |
| 3825 | * program termination will do something drastic like close the window: |
| 3826 | * without a pause, the user wouldn't have a chance to read any text the |
| 3827 | * program displayed after the last interactive input, because the window |
| 3828 | * would abruptly disappear moments after the text was displayed. For |
| 3829 | * systems where termination doesn't have such drastic effects, there's no |
| 3830 | * need to do anything in this routine. Because it's up to this routine |
| 3831 | * whether or not to pause, this routine must display a prompt if it's |
| 3832 | * going to pause for user input - the portable caller obviously can't do |
| 3833 | * so, because the caller doesn't know if the routine is going to pause or |
| 3834 | * not (so if the caller displayed a prompt assuming the routine would |
| 3835 | * pause, the prompt would show up even on systems where there actually is |
| 3836 | * no pause, which would be confusing). This routine DOES NOT terminate |
| 3837 | * the program; it simply pauses if necessary to allow the user to |
| 3838 | * acknowledge the last bit of text the program displayed, then returns to |
| 3839 | * allow the caller to carry on with its own termination work. |
| 3840 | * |
| 3841 | * os_term() is meant to perform the same function as the C standard |
| 3842 | * library routine exit(): this actually terminates the program, exiting |
| 3843 | * to the operating system. This routine is not meant to return to its |
| 3844 | * caller, because it's supposed to exit the program directly. For many |
| 3845 | * systems, this routine can simply call exit(); the portable code calls |
| 3846 | * this routine instead of calling exit() directly, because some systems |
| 3847 | * have their own OS-specific way of terminating rather than using exit(). |
| 3848 | * This routine MUST NOT undo the effects of os_init(), because callers |
| 3849 | * might not have ever called os_init(); callers are required to call |
| 3850 | * os_uninit() if they ever called os_init(), before calling os_term(), so |
| 3851 | * this routine can simply assume that any global modes set by os_init() |
| 3852 | * have already been undone by the time this is called. |
| 3853 | */ |
| 3854 | |
| 3855 | /* |
| 3856 | * Uninitialize. This is called prior to progam termination to reverse |
| 3857 | * the effect of any changes made in os_init(). For example, if |
| 3858 | * os_init() put the terminal in raw mode, this should restore the |
| 3859 | * previous terminal mode. This routine should not terminate the |
| 3860 | * program (so don't call exit() here) - the caller might have more |
| 3861 | * processing to perform after this routine returns. |
| 3862 | */ |
| 3863 | void os_uninit(void); |
| 3864 | |
| 3865 | /* |
| 3866 | * Pause prior to exit, if desired. This is meant to be called by |
| 3867 | * portable code just before the program is to be terminated; it can be |
| 3868 | * implemented to show a prompt and wait for user acknowledgment before |
| 3869 | * proceeding. This is useful for implementations that are using |
| 3870 | * something like a character-mode terminal window running on a graphical |
| 3871 | * operating system: this gives the implementation a chance to pause |
| 3872 | * before exiting, so that the window doesn't just disappear |
| 3873 | * unceremoniously. |
| 3874 | * |
| 3875 | * This is allowed to do nothing at all. For regular character-mode |
| 3876 | * systems, this routine usually doesn't do anything, because when the |
| 3877 | * program exits, the terminal will simply return to the OS command |
| 3878 | * prompt; none of the text displayed just before the program exited will |
| 3879 | * be lost, so there's no need for any interactive pause. Likewise, for |
| 3880 | * graphical systems where the window will remain open, even after the |
| 3881 | * program exits, until the user explicitly closes the window, there's no |
| 3882 | * need to do anything here. |
| 3883 | * |
| 3884 | * If this is implemented to pause, then this routine MUST show some kind |
| 3885 | * of prompt to let the user know we're waiting. In the simple case of a |
| 3886 | * text-mode terminal window on a graphical OS, this should simply print |
| 3887 | * out some prompt text ("Press a key to exit...") and then wait for the |
| 3888 | * user to acknowledge the prompt (by pressing a key, for example). For |
| 3889 | * graphical systems, the prompt could be placed in the window's title |
| 3890 | * bar, or status-bar, or wherever is appropriate for the OS. |
| 3891 | */ |
| 3892 | void os_expause(void); |
| 3893 | |
| 3894 | /* |
| 3895 | * Terminate. This should exit the program with the given exit status. |
| 3896 | * In general, this should be equivalent to the standard C library |
| 3897 | * exit() function, but we define this interface to allow the OS code to |
| 3898 | * do any necessary pre-termination cleanup. |
| 3899 | */ |
| 3900 | void os_term(int status); |
| 3901 | |
| 3902 | /* |
| 3903 | * Install/uninstall the break handler. If possible, the OS code should |
| 3904 | * set (if 'install' is true) or clear (if 'install' is false) a signal |
| 3905 | * handler for keyboard break signals (control-C, etc, depending on |
| 3906 | * local convention). The OS code should set its own handler routine, |
| 3907 | * which should note that a break occurred with an internal flag; the |
| 3908 | * portable code uses os_break() from time to time to poll this flag. |
| 3909 | */ |
| 3910 | void os_instbrk(int install); |
| 3911 | |
| 3912 | /* |
| 3913 | * Check for user break ("control-C", etc) - returns true if a break is |
| 3914 | * pending, false if not. If this returns true, it should "consume" the |
| 3915 | * pending break (probably by simply clearing the OS code's internal |
| 3916 | * break-pending flag). |
| 3917 | */ |
| 3918 | int os_break(void); |
| 3919 | |
| 3920 | /* |
| 3921 | * Sleep for a given interval. This should simply pause for the given |
| 3922 | * number of milliseconds, then return. On multi-tasking systems, this |
| 3923 | * should use a system API to suspend the current process for the desired |
| 3924 | * delay; on single-tasking systems, this can simply sit in a wait loop |
| 3925 | * until the desired interval has elapsed. |
| 3926 | */ |
| 3927 | void os_sleep_ms(long delay_in_milliseconds); |
| 3928 | |
| 3929 | /* |
| 3930 | * Yield CPU; returns TRUE if user requested an interrupt (a "control-C" |
| 3931 | * type of operation to abort the entire program), FALSE to continue. |
| 3932 | * Portable code should call this routine from time to time during lengthy |
| 3933 | * computations that don't involve any UI operations; if practical, this |
| 3934 | * routine should be invoked roughly every 10 to 100 milliseconds. |
| 3935 | * |
| 3936 | * The purpose of this routine is to support "cooperative multitasking" |
| 3937 | * systems, such as pre-X MacOS, where it's necessary for each running |
| 3938 | * program to call the operating system explicitly in order to yield the |
| 3939 | * CPU from time to time to other concurrently running programs. On |
| 3940 | * cooperative multitasking systems, a program can only lose control of |
| 3941 | * the CPU by making specific system calls, usually related to GUI events; |
| 3942 | * a program can never lose control of the CPU asynchronously. So, a |
| 3943 | * program that performs lengthy computations without any UI interaction |
| 3944 | * can cause the rest of the system to freeze up until the computations |
| 3945 | * are finished; but if a compute-intensive program explicitly yields the |
| 3946 | * CPU from time to time, it allows other programs to remain responsive. |
| 3947 | * Yielding the CPU at least every 100 milliseconds or so will generally |
| 3948 | * allow the UI to remain responsive; yielding more frequently than every |
| 3949 | * 10 ms or so will probably start adding noticeable overhead. |
| 3950 | * |
| 3951 | * On single-tasking systems (such as MS-DOS), there's only one program |
| 3952 | * running at a time, so there's no need to yield the CPU; on virtually |
| 3953 | * every modern system, the OS automatically schedules CPU time without |
| 3954 | * the running programs having any say in the matter, so again there's no |
| 3955 | * need for a program to yield the CPU. For systems where this routine |
| 3956 | * isn't needed, the system header should simply #define os_yield to |
| 3957 | * something like "((void)0)" - this will allow the compiler to completely |
| 3958 | * ignore calls to this routine for systems where they aren't needed. |
| 3959 | * |
| 3960 | * Note that this routine is NOT meant to provide scheduling hinting to |
| 3961 | * modern systems with true multitasking, so a trivial implementation is |
| 3962 | * fine for any modern system. |
| 3963 | */ |
| 3964 | #ifndef os_yield |
| 3965 | int os_yield(void); |
| 3966 | #endif |
| 3967 | |
| 3968 | /* |
| 3969 | * Set the default saved-game extension. This routine will NOT be called |
| 3970 | * when we're using the standard saved game extension; this routine will be |
| 3971 | * invoked only if we're running as a stand-alone game, and the game author |
| 3972 | * specified a non-standard saved-game extension when creating the |
| 3973 | * stand-alone game. |
| 3974 | * |
| 3975 | * This routine is not required if the system does not use the standard, |
| 3976 | * semi-portable os0.c implementation. Even if the system uses the |
| 3977 | * standard os0.c implementation, it can provide an empty routine here if |
| 3978 | * the system code doesn't need to do anything special with this |
| 3979 | * information. |
| 3980 | * |
| 3981 | * The extension is specified as a null-terminated string. The extension |
| 3982 | * does NOT include the leading period. |
| 3983 | */ |
| 3984 | void os_set_save_ext(const char *ext); |
| 3985 | |
| 3986 | /* |
| 3987 | * Get the saved game extension previously set with os_set_save_ext(). |
| 3988 | * Returns null if no custom extension has been set. |
| 3989 | */ |
| 3990 | const char *os_get_save_ext(); |
| 3991 | |
| 3992 | |
| 3993 | /* ------------------------------------------------------------------------*/ |
| 3994 | /* |
| 3995 | * Translate a character from the HTML 4 Unicode character set to the |
| 3996 | * current character set used for display. Takes an HTML 4 character |
| 3997 | * code and returns the appropriate local character code. |
| 3998 | * |
| 3999 | * The result buffer should be filled in with a null-terminated string |
| 4000 | * that should be used to represent the character. Multi-character |
| 4001 | * results are possible, which may be useful for certain approximations |
| 4002 | * (such as using "(c)" for the copyright symbol). |
| 4003 | * |
| 4004 | * Note that we only define this prototype if this symbol isn't already |
| 4005 | * defined as a macro, which may be the case on some platforms. |
| 4006 | * Alternatively, if the function is already defined (for example, as an |
| 4007 | * inline function), the defining code can define OS_XLAT_HTML4_DEFINED, |
| 4008 | * in which case we'll also omit this prototype. |
| 4009 | * |
| 4010 | * Important: this routine provides the *default* mapping that is used |
| 4011 | * when no external character mapping file is present, and for any named |
| 4012 | * entities not defined in the mapping file. Any entities in the |
| 4013 | * mapping file, if used, will override this routine. |
| 4014 | * |
| 4015 | * A trivial implementation of this routine (that simply returns a |
| 4016 | * one-character result consisting of the original input character, |
| 4017 | * truncated to eight bits if necessary) can be used if you want to |
| 4018 | * require an external mapping file to be used for any game that |
| 4019 | * includes HTML character entities. The DOS version implements this |
| 4020 | * routine so that games will still look reasonable when played with no |
| 4021 | * mapping file present, but other systems are not required to do this. |
| 4022 | */ |
| 4023 | #ifndef os_xlat_html4 |
| 4024 | # ifndef OS_XLAT_HTML4_DEFINED |
| 4025 | void os_xlat_html4(unsigned int html4_char, |
| 4026 | char *result, size_t result_buf_len); |
| 4027 | # endif |
| 4028 | #endif |
| 4029 | |
| 4030 | /* |
| 4031 | * Generate a filename for a character-set mapping file. This function |
| 4032 | * should determine the current native character set in use, if |
| 4033 | * possible, then generate a filename, according to system-specific |
| 4034 | * conventions, that we should attempt to load to get a mapping between |
| 4035 | * the current native character set and the internal character set |
| 4036 | * identified by 'internal_id'. |
| 4037 | * |
| 4038 | * The internal character set ID is a string of up to 4 characters. |
| 4039 | * |
| 4040 | * On DOS, the native character set is a DOS code page. DOS code pages |
| 4041 | * are identified by 3- or 4-digit identifiers; for example, code page |
| 4042 | * 437 is the default US ASCII DOS code page. We generate the |
| 4043 | * character-set mapping filename by appending the internal character |
| 4044 | * set identifier to the DOS code page number, then appending ".TCP" to |
| 4045 | * the result. So, to map between ISO Latin-1 (internal ID = "La1") and |
| 4046 | * DOS code page 437, we would generate the filename "437La1.TCP". |
| 4047 | * |
| 4048 | * Note that this function should do only two things. First, determine |
| 4049 | * the current native character set that's in use. Second, generate a |
| 4050 | * filename based on the current native code page and the internal ID. |
| 4051 | * This function is NOT responsible for figuring out the mapping or |
| 4052 | * anything like that -- it's simply where we generate the correct |
| 4053 | * filename based on local convention. |
| 4054 | * |
| 4055 | * 'filename' is a buffer of at least OSFNMAX characters. |
| 4056 | * |
| 4057 | * 'argv0' is the executable filename from the original command line. |
| 4058 | * This parameter is provided so that the system code can look for |
| 4059 | * mapping files in the original TADS executables directory, if desired. |
| 4060 | */ |
| 4061 | void os_gen_charmap_filename(char *filename, char *internal_id, |
| 4062 | char *argv0); |
| 4063 | |
| 4064 | /* |
| 4065 | * Receive notification that a character mapping file has been loaded. |
| 4066 | * The caller doesn't require this routine to do anything at all; this |
| 4067 | * is purely for the system-dependent code's use so that it can take |
| 4068 | * care of any initialization that it must do after the caller has |
| 4069 | * loaded a charater mapping file. 'id' is the character set ID, and |
| 4070 | * 'ldesc' is the display name of the character set. 'sysinfo' is the |
| 4071 | * extra system information string that is stored in the mapping file; |
| 4072 | * the interpretation of this information is up to this routine. |
| 4073 | * |
| 4074 | * For reference, the Windows version uses the extra information as a |
| 4075 | * code page identifier, and chooses its default font character set to |
| 4076 | * match the code page. On DOS, the run-time requires the player to |
| 4077 | * activate an appropriate code page using a DOS command (MODE CON CP |
| 4078 | * SELECT) prior to starting the run-time, so this routine doesn't do |
| 4079 | * anything at all on DOS. |
| 4080 | */ |
| 4081 | void os_advise_load_charmap(char *id, char *ldesc, char *sysinfo); |
| 4082 | |
| 4083 | /* |
| 4084 | * Generate the name of the character set mapping table for Unicode |
| 4085 | * characters to and from the given local character set. Fills in the |
| 4086 | * buffer with the implementation-dependent name of the desired |
| 4087 | * character set map. See below for the character set ID codes. |
| 4088 | * |
| 4089 | * For example, on Windows, the implementation would obtain the |
| 4090 | * appropriate active code page (which is simply a Windows character set |
| 4091 | * identifier number) from the operating system, and build the name of |
| 4092 | * the Unicode mapping file for that code page, such as "CP1252". On |
| 4093 | * Macintosh, the implementation would look up the current script system |
| 4094 | * and return the name of the Unicode mapping for that script system, |
| 4095 | * such as "ROMAN" or "CENTEURO". |
| 4096 | * |
| 4097 | * If it is not possible to determine the specific character set that is |
| 4098 | * in use, this function should return "asc7dflt" (ASCII 7-bit default) |
| 4099 | * as the character set identifier on an ASCII system, or an appropriate |
| 4100 | * base character set name on a non-ASCII system. "asc7dflt" is the |
| 4101 | * generic character set mapping for plain ASCII characters. |
| 4102 | * |
| 4103 | * The given buffer must be at least 32 bytes long; the implementation |
| 4104 | * must limit the result it stores to 32 bytes. (We use a fixed-size |
| 4105 | * buffer in this interface for simplicity, and because there seems no |
| 4106 | * need for greater flexibility in the interface; a character set name |
| 4107 | * doesn't carry very much information so shouldn't need to be very |
| 4108 | * long. Note that this function doesn't generate a filename, but |
| 4109 | * simply a mapping name; in practice, a map name will be used to |
| 4110 | * construct a mapping file name.) |
| 4111 | * |
| 4112 | * Because this function obtains the Unicode mapping name, there is no |
| 4113 | * need to specify the internal character set to be used: the internal |
| 4114 | * character set is Unicode. |
| 4115 | */ |
| 4116 | /* |
| 4117 | * Implementation note: when porting this routine, the convention that |
| 4118 | * you use to name your mapping files is up to you. You should simply |
| 4119 | * choose a convention for this implementation, and then use the same |
| 4120 | * convention for packaging the mapping files for your OS release. In |
| 4121 | * most cases, the best convention is to use the names that the Unicode |
| 4122 | * consortium uses in their published cross-mapping listings, since |
| 4123 | * these listings can be used as the basis of the mapping files that you |
| 4124 | * include with your release. For example, on Windows, the convention |
| 4125 | * is to use the code page number to construct the map name, as in |
| 4126 | * CP1252 or CP1250. |
| 4127 | */ |
| 4128 | void os_get_charmap(char *mapname, int charmap_id); |
| 4129 | |
| 4130 | /* |
| 4131 | * Character map for the display (i.e., for the user interface). This |
| 4132 | * is the character set which is used for input read from the keyboard, |
| 4133 | * and for output displayed on the monitor or terminal. |
| 4134 | */ |
| 4135 | #define OS_CHARMAP_DISPLAY 1 |
| 4136 | |
| 4137 | /* |
| 4138 | * Character map for mapping filename strings. This should identify the |
| 4139 | * character set currently in use on the local system for filenames |
| 4140 | * (i.e., for strings identifying objects in the local file system), |
| 4141 | * providing a suitable name for use in opening a mapping file. |
| 4142 | * |
| 4143 | * On many platforms, the entire system uses only one character set at |
| 4144 | * the OS level, so the file system character set is the same as the |
| 4145 | * display character set. Some systems define a particular character |
| 4146 | * set for file system use, though, because different users might be |
| 4147 | * running applications on terminals that display different character |
| 4148 | * sets. |
| 4149 | */ |
| 4150 | #define OS_CHARMAP_FILENAME 2 |
| 4151 | |
| 4152 | /* |
| 4153 | * Default character map for file contents. On most systems, this will |
| 4154 | * be the same as display. On some systems, it won't be possible to |
| 4155 | * know in general what character set files use; in fact, this isn't |
| 4156 | * possible anywhere, since a file might have been copied without |
| 4157 | * translation from a different type of computer using a different |
| 4158 | * character set. So, this isn't meant to provide a reliable choice of |
| 4159 | * character set for any arbitrary file; it's simply meant to be a good |
| 4160 | * guess that most files on this system are likely to use. |
| 4161 | */ |
| 4162 | #define OS_CHARMAP_FILECONTENTS 3 |
| 4163 | |
| 4164 | /* |
| 4165 | * Default character map for the command line. This is the maping we use |
| 4166 | * to interpret command line arguments passed to our main() or equivalent. |
| 4167 | * On most systems, this will be the same as the display character set. |
| 4168 | */ |
| 4169 | #define OS_CHARMAP_CMDLINE 4 |
| 4170 | |
| 4171 | |
| 4172 | /* ------------------------------------------------------------------------ */ |
| 4173 | /* |
| 4174 | * External Banner Interface. This interface provides the ability to |
| 4175 | * divide the display window into multiple sub-windows, each with its own |
| 4176 | * independent contents. |
| 4177 | * |
| 4178 | * To determine where a new banner is displayed, we look at the banners as |
| 4179 | * a tree, rooted at the "main window," the special banner that the system |
| 4180 | * automatically creates initially for the main game text. We start by |
| 4181 | * allocating the entire display (or the entire application window, if |
| 4182 | * we're running on a GUI system) to the main window. We then traverse |
| 4183 | * the tree, starting with the root window's children. For each child |
| 4184 | * window, we allocate space for the child out of the parent window's |
| 4185 | * area, according to the child's alignment and size settings, and deduct |
| 4186 | * this space from the parent window's size. We then lay out the children |
| 4187 | * of the child. |
| 4188 | * |
| 4189 | * For each banner window, we take its requested space out of the parent |
| 4190 | * window's area by starting at the edge of the parent window rectangle as |
| 4191 | * indicated by the banner's alignment, and taking the requested `width |
| 4192 | * (for a left/right banner) or height (for a top/bottom banner), limiting |
| 4193 | * to the available width/height in the parent window's space. Give the |
| 4194 | * banner the full extent of the parent's space in its other dimension (so |
| 4195 | * a left/right banner gets the full height of the parent space, and a |
| 4196 | * top/bottom banner gets the full width). |
| 4197 | * |
| 4198 | * Note that the layout proceeds exclusively down the tree (i.e., from the |
| 4199 | * root to children to grandchildren, and so on). It *appears* that a |
| 4200 | * child affects its parent, because of the deduction step: a child |
| 4201 | * acquires screen space by carving out a chunk of its parent. The right |
| 4202 | * way to think about this, though, is that the parent's full area is the |
| 4203 | * union of the parent window and all of its children; when viewed this |
| 4204 | * way, the parent's full area is fully determined the instant the parent |
| 4205 | * is laid out, and never changes as its children are laid out. Note in |
| 4206 | * particular that a child can never make a parent larger; the only thing |
| 4207 | * a child can do to a parent is carve out a chunk of the parent for |
| 4208 | * itself, which doesn't affect the boundaries of the union of the parent |
| 4209 | * plus its children. |
| 4210 | * |
| 4211 | * Note also that if the banner has a border, and the implementation |
| 4212 | * actually draws borders, the border must be drawn for the *full* area of |
| 4213 | * the banner, as defined above. For example, suppose we have two |
| 4214 | * borders: banner A is a child of the main window, is top-aligned, and |
| 4215 | * has a border. Banner B is a child of banner A, right-aligned, with no |
| 4216 | * border. Obviously, without considering banner B, banner A's space runs |
| 4217 | * across the entire width of the main window, so its border (at the |
| 4218 | * bottom of its area) runs across the entire width of the main window. |
| 4219 | * Banner B carves out some space from A's right side for itself, so |
| 4220 | * banner A's actual on-screen area runs from the left edge of the main |
| 4221 | * window to banner B's left edge. However, even though banner A itself |
| 4222 | * no longer runs the full width of the main window, banner A's *full* |
| 4223 | * area - that is, the union of banner A's on-screen area and all of its |
| 4224 | * children's full areas - does still run the entire width of the main |
| 4225 | * window, hence banner A's border must still run the full width of the |
| 4226 | * main window. The simple way of looking at this is that a banner's |
| 4227 | * border is always to be drawn exactly the same way, regardless of |
| 4228 | * whether or not the banner has children - simply draw the banner as it |
| 4229 | * would be drawn if the banner had no children. |
| 4230 | * |
| 4231 | * Each time a banner is added or removed, we must recalculate the layout |
| 4232 | * of the remaining banners and main text area. The os_banner_xxx() |
| 4233 | * implementation is responsible for this layout refiguring. |
| 4234 | * |
| 4235 | * The entire external banner window interface is optional, although the |
| 4236 | * functions must at least be defined as dummies to avoid linker errors |
| 4237 | * when building. If a platform doesn't implement this feature, |
| 4238 | * os_banner_create() should simply return null, and the other routines |
| 4239 | * can do nothing. |
| 4240 | */ |
| 4241 | |
| 4242 | /* |
| 4243 | * Create a banner window. 'info' gives the desired parameters for the new |
| 4244 | * banner. |
| 4245 | * |
| 4246 | * Note that certain requested parameter settings might or might not be |
| 4247 | * respected, depending on the capabilities of the platform and user |
| 4248 | * preferences. os_banner_getinfo() can be used after creation to |
| 4249 | * determine which parameter settings are actually used in the new banner. |
| 4250 | * |
| 4251 | * 'parent' gives the parent of this banner; this is the banner handle of |
| 4252 | * another banner window, or null. If 'parent' is null, then the new |
| 4253 | * banner is a child of the main window, which the system creates |
| 4254 | * automatically at startup and which contains the main input/output |
| 4255 | * transcript. The new banner's on-screen area is carved out of the |
| 4256 | * parent's space, according to the alignment and size settings of the new |
| 4257 | * window, so this determines how the window is laid out on the screen. |
| 4258 | * |
| 4259 | * 'where' is OS_BANNER_FIRST to make the new window the first child of its |
| 4260 | * parent; OS_BANNER_LAST to make it the last child of its parent; |
| 4261 | * OS_BANNER_BEFORE to insert it immediately before the existing banner |
| 4262 | * identified by handle in 'other'; or OS_BANNER_AFTER to insert |
| 4263 | * immediately after 'other'. When BEFORE or AFTER is used, 'other' must |
| 4264 | * be another child of the same parent; if it is not, the routine should |
| 4265 | * act as though 'where' were given as OS_BANNER_LAST. |
| 4266 | * |
| 4267 | * 'other' is a banner handle for an existing banner window. This is used |
| 4268 | * to specify the relative position among children of the new banner's |
| 4269 | * parent, if 'where' is either OS_BANNER_BEFORE or OS_BANNER_AFTER. If |
| 4270 | * 'where' is OS_BANNER_FIRST or OS_BANNER_LAST, 'other' is ignored. |
| 4271 | * |
| 4272 | * 'wintype' is the type of the window. This is one of the |
| 4273 | * OS_BANNER_TYPE_xxx codes indicating what kind of window is desired. |
| 4274 | * |
| 4275 | * 'align' is the banner's alignment, given as an OS_BANNER_ALIGN_xxx |
| 4276 | * value. Top/bottom banners are horizontal: they run across the full |
| 4277 | * width of the existing main text area. Left/right banners are vertical: |
| 4278 | * they run down the full height of the existing main text area. |
| 4279 | * |
| 4280 | * 'siz' is the requested size of the new banner. The meaning of 'siz' |
| 4281 | * depends on the value of 'siz_units', which can be OS_BANNER_SIZE_PCT to |
| 4282 | * set the size as a percentage of the REMAINING space, or |
| 4283 | * OS_BANNER_SIZE_ABS to set an absolute size in the "natural" units of the |
| 4284 | * window. The natural units vary by window type: for text and text grid |
| 4285 | * windows, this is in rows/columns of '0' characters in the default font |
| 4286 | * for the window. Note that when OS_BANNER_SIZE_ABS is used in a text or |
| 4287 | * text grid window, the OS implementation MUST add the space needed for |
| 4288 | * margins and borders when determining the actual pixel size of the |
| 4289 | * window; in other words, the window should be large enough that it can |
| 4290 | * actually display the given number or rows or columns. |
| 4291 | * |
| 4292 | * The size is interpreted as a width or height according to the window's |
| 4293 | * orientation. For a TOP or BOTTOM banner, the size is the height; for a |
| 4294 | * LEFT or RIGHT banner, the size is the width. A banner has only one |
| 4295 | * dimension's size given, since the other dimension's size is determined |
| 4296 | * automatically by the layout rules. |
| 4297 | * |
| 4298 | * Note that the window's size can be changed later using |
| 4299 | * banner_size_to_contents() or banner_set_size(). |
| 4300 | * |
| 4301 | * 'style' is a combination of OS_BANNER_STYLE_xxx flags - see below. The |
| 4302 | * style flags give the REQUESTED style for the banner, which might or |
| 4303 | * might not be respected, depending on the platform's capabilities, user |
| 4304 | * preferences, and other factors. os_banner_getinfo() can be used to |
| 4305 | * determine which style flags are actually used. |
| 4306 | * |
| 4307 | * Returns the "handle" to the new banner window, which is an opaque value |
| 4308 | * that is used in subsequent os_banner_xxx calls to operate on the window. |
| 4309 | * Returns null if the window cannot be created. An implementation is not |
| 4310 | * required to support this functionality at all, and can subset it if it |
| 4311 | * does support it (for example, an implementation could support only |
| 4312 | * top/bottom-aligned banners, but not left/right-aligned), so callers must |
| 4313 | * be prepared for this routine to return null. |
| 4314 | */ |
| 4315 | void *os_banner_create(void *parent, int where, void *other, int wintype, |
| 4316 | int align, int siz, int siz_units, |
| 4317 | unsigned long style); |
| 4318 | |
| 4319 | |
| 4320 | /* |
| 4321 | * insertion positions |
| 4322 | */ |
| 4323 | #define OS_BANNER_FIRST 1 |
| 4324 | #define OS_BANNER_LAST 2 |
| 4325 | #define OS_BANNER_BEFORE 3 |
| 4326 | #define OS_BANNER_AFTER 4 |
| 4327 | |
| 4328 | /* |
| 4329 | * banner types |
| 4330 | */ |
| 4331 | |
| 4332 | /* |
| 4333 | * Normal text stream window. This is a text stream that behaves |
| 4334 | * essentially like the main text window: text is displayed to this |
| 4335 | * through os_banner_disp(), always in a stream-like fashion by adding new |
| 4336 | * text to the end of any exiting text. |
| 4337 | * |
| 4338 | * Systems that use proportional fonts should usually simply use the same |
| 4339 | * font they use by default in the main text window. However, note that |
| 4340 | * the OS_BANNER_STYLE_TAB_ALIGN style flag might imply that a fixed-pitch |
| 4341 | * font should be used even when proportional fonts are available, because |
| 4342 | * a fixed-pitch font will allow the calling code to rely on using spaces |
| 4343 | * to align text within the window. |
| 4344 | */ |
| 4345 | #define OS_BANNER_TYPE_TEXT 1 |
| 4346 | |
| 4347 | /* |
| 4348 | * "Text grid" window. This type of window is similar to an normal text |
| 4349 | * window (OS_BANNER_TYPE_TEXT), but is guaranteed to arrange its text in |
| 4350 | * a regular grid of character cells, all of the same size. This means |
| 4351 | * that the output position can be moved to an arbitrary point within the |
| 4352 | * window at any time, so the calling program can precisely control the |
| 4353 | * layout of the text in the window. |
| 4354 | * |
| 4355 | * Because the output position can be moved to arbitrary positions in the |
| 4356 | * window, it is possible to overwrite text previously displayed. When |
| 4357 | * this happens, the old text is completely obliterated by the new text, |
| 4358 | * leaving no trace of the overwritten text. |
| 4359 | * |
| 4360 | * In order to guarantee that character cells are all the same size, this |
| 4361 | * type of window does not allow any text attributes. The implementation |
| 4362 | * should simply ignore any attempts to change text attributes in this |
| 4363 | * type of window. However, colors can be used to the same degree they |
| 4364 | * can be used in an ordinary text window. |
| 4365 | * |
| 4366 | * To guarantee the regular spacing of character cells, all |
| 4367 | * implementations must use fixed-pitch fonts for these windows. This |
| 4368 | * applies even to platforms where proportional fonts are available. |
| 4369 | */ |
| 4370 | #define OS_BANNER_TYPE_TEXTGRID 2 |
| 4371 | |
| 4372 | |
| 4373 | /* |
| 4374 | * banner alignment types |
| 4375 | */ |
| 4376 | #define OS_BANNER_ALIGN_TOP 0 |
| 4377 | #define OS_BANNER_ALIGN_BOTTOM 1 |
| 4378 | #define OS_BANNER_ALIGN_LEFT 2 |
| 4379 | #define OS_BANNER_ALIGN_RIGHT 3 |
| 4380 | |
| 4381 | /* |
| 4382 | * size units |
| 4383 | */ |
| 4384 | #define OS_BANNER_SIZE_PCT 1 |
| 4385 | #define OS_BANNER_SIZE_ABS 2 |
| 4386 | |
| 4387 | |
| 4388 | /* |
| 4389 | * banner style flags |
| 4390 | */ |
| 4391 | |
| 4392 | /* |
| 4393 | * The banner has a visible border; this indicates that a line is to be |
| 4394 | * drawn to separate the banner from the adjacent window or windows |
| 4395 | * "inside" the banner. So, a top-aligned banner will have its border |
| 4396 | * drawn along its bottom edge; a left-aligned banner will show a border |
| 4397 | * along its right edge; and so forth. |
| 4398 | * |
| 4399 | * Note that character-mode platforms generally do NOT respect the border |
| 4400 | * style, since doing so takes up too much screen space. |
| 4401 | */ |
| 4402 | #define OS_BANNER_STYLE_BORDER 0x00000001 |
| 4403 | |
| 4404 | /* |
| 4405 | * The banner has a vertical/horizontal scrollbar. Character-mode |
| 4406 | * platforms generally do not support scrollbars. |
| 4407 | */ |
| 4408 | #define OS_BANNER_STYLE_VSCROLL 0x00000002 |
| 4409 | #define OS_BANNER_STYLE_HSCROLL 0x00000004 |
| 4410 | |
| 4411 | /* |
| 4412 | * Automatically scroll the banner vertically/horizontally whenever new |
| 4413 | * text is displayed in the window. In other words, whenever |
| 4414 | * os_banner_disp() is called, scroll the window so that the text that the |
| 4415 | * new cursor position after the new text is displayed is visible in the |
| 4416 | * window. |
| 4417 | * |
| 4418 | * Note that this style is independent of the presence of scrollbars. |
| 4419 | * Even if there are no scrollbars, we can still scroll the window's |
| 4420 | * contents programmatically. |
| 4421 | * |
| 4422 | * Implementations can, if desired, keep an internal buffer of the |
| 4423 | * window's contents, so that the contents can be recalled via the |
| 4424 | * scrollbars if the text displayed in the banner exceeds the space |
| 4425 | * available in the banner's window on the screen. If the implementation |
| 4426 | * does keep such a buffer, we recommend the following method for managing |
| 4427 | * this buffer. If the AUTO_VSCROLL flag is not set, then the banner's |
| 4428 | * contents should be truncated at the bottom when the contents overflow |
| 4429 | * the buffer; that is, once the banner's internal buffer is full, any new |
| 4430 | * text that the calling program attempts to add to the banner should |
| 4431 | * simply be discarded. If the AUTO_VSCROLL flag is set, then the OLDEST |
| 4432 | * text should be discarded instead, so that the most recent text is |
| 4433 | * always retained. |
| 4434 | */ |
| 4435 | #define OS_BANNER_STYLE_AUTO_VSCROLL 0x00000008 |
| 4436 | #define OS_BANNER_STYLE_AUTO_HSCROLL 0x00000010 |
| 4437 | |
| 4438 | /* |
| 4439 | * Tab-based alignment is required/supported. On creation, this is a hint |
| 4440 | * to the implementation that is sometimes necessary to determine what |
| 4441 | * kind of font to use in the new window, for non-HTML platforms. If this |
| 4442 | * flag is set on creation, the caller is indicating that it wants to use |
| 4443 | * <TAB> tags to align text in the window. |
| 4444 | * |
| 4445 | * Character-mode implementations that use a single font with fixed pitch |
| 4446 | * can simply ignore this. These implementations ALWAYS have a working |
| 4447 | * <TAB> capability, because the portable output formatter provides <TAB> |
| 4448 | * interpretation for a fixed-pitch window. |
| 4449 | * |
| 4450 | * Full HTML TADS implementations can also ignore this. HTML TADS |
| 4451 | * implementations always have full <TAB> support via the HTML |
| 4452 | * parser/renderer. |
| 4453 | * |
| 4454 | * Text-only implementations on GUI platforms (i.e., implementations that |
| 4455 | * are not based on the HTML parser/renderer engine in HTML TADS, but |
| 4456 | * which run on GUI platforms with proportionally-spaced text) should use |
| 4457 | * this flag to determine the font to display. If this flag is NOT set, |
| 4458 | * then the caller doesn't care about <TAB>, and the implementation is |
| 4459 | * free to use a proportionally-spaced font in the window if desired. |
| 4460 | * |
| 4461 | * When retrieving information on an existing banner, this flag indicates |
| 4462 | * that <TAB> alignment is actually supported on the window. |
| 4463 | */ |
| 4464 | #define OS_BANNER_STYLE_TAB_ALIGN 0x00000020 |
| 4465 | |
| 4466 | /* |
| 4467 | * Use "MORE" mode in this window. By default, a banner window should |
| 4468 | * happily allow text to overflow the vertical limits of the window; the |
| 4469 | * only special thing that should happen on overflow is that the window |
| 4470 | * should be srolled down to show the latest text, if the auto-vscroll |
| 4471 | * style is set. With this flag, though, a banner window acts just like |
| 4472 | * the main text window: when the window fills up vertically, we show a |
| 4473 | * MORE prompt (using appropriate system conventions), and wait for the |
| 4474 | * user to indicate that they're ready to see more text. On most systems, |
| 4475 | * the user acknowledges a MORE prompt by pressing a key or scrolling with |
| 4476 | * the mouse, but it's up to the system implementor to decide what's |
| 4477 | * appropriate for the system. |
| 4478 | * |
| 4479 | * Note that MORE mode in ANY banner window should generally override all |
| 4480 | * other user input focus. In other words, if the game in the main window |
| 4481 | * would like to read a keystroke from the user, but one of the banner |
| 4482 | * windows is pausing with a MORE prompt, any keyboard input should be |
| 4483 | * directed to the banner paused at the MORE prompt, not to the main |
| 4484 | * window; the main window should not receive any key events until the MORE |
| 4485 | * prompt has been removed. |
| 4486 | * |
| 4487 | * This style requires the auto-vscroll style. Implementations should |
| 4488 | * assume auto-vscroll when this style is set. This style can be ignored |
| 4489 | * with text grid windows. |
| 4490 | */ |
| 4491 | #define OS_BANNER_STYLE_MOREMODE 0x00000040 |
| 4492 | |
| 4493 | /* |
| 4494 | * This banner is a horizontal/vertical "strut" for sizing purposes. This |
| 4495 | * means that the banner's content size is taken into account when figuring |
| 4496 | * the content size of its *parent* banner. If the banner has the same |
| 4497 | * orientation as the parent, its content size is added to its parent's |
| 4498 | * internal content size to determine the parent's overall content size. |
| 4499 | * If the banner's orientation is orthogonal to the parent's, then the |
| 4500 | * parent's overall content size is the larger of the parent's internal |
| 4501 | * content size and this banner's content size. |
| 4502 | */ |
| 4503 | #define OS_BANNER_STYLE_HSTRUT 0x00000080 |
| 4504 | #define OS_BANNER_STYLE_VSTRUT 0x00000100 |
| 4505 | |
| 4506 | |
| 4507 | /* |
| 4508 | * Delete a banner. This removes the banner from the display, which |
| 4509 | * requires recalculating the entire screen's layout to reallocate this |
| 4510 | * banner's space to other windows. When this routine returns, the banner |
| 4511 | * handle is invalid and can no longer be used in any os_banner_xxx |
| 4512 | * function calls. |
| 4513 | * |
| 4514 | * If the banner has children, the children will no longer be displayed, |
| 4515 | * but will remain valid in memory until deleted. A child window's |
| 4516 | * display area always comes out of its parent's space, so once the parent |
| 4517 | * is gone, a child has no way to acquire any display space; resizing the |
| 4518 | * child won't help, since it simply has no way to obtain any screen space |
| 4519 | * once its parent has been deleted. Even though the window's children |
| 4520 | * will become invisible, their banner handles will remain valid; the |
| 4521 | * caller is responsible for explicitly deleting the children even after |
| 4522 | * deleting their parent. |
| 4523 | */ |
| 4524 | void os_banner_delete(void *banner_handle); |
| 4525 | |
| 4526 | /* |
| 4527 | * "Orphan" a banner. This tells the osifc implementation that the caller |
| 4528 | * wishes to sever all of its ties with the banner (as part of program |
| 4529 | * termination, for example), but that the calling program does not |
| 4530 | * actually require that the banner's on-screen display be immediately |
| 4531 | * removed. |
| 4532 | * |
| 4533 | * The osifc implementation can do one of two things: |
| 4534 | * |
| 4535 | * 1. Simply call os_banner_delete(). If the osifc implementation |
| 4536 | * doesn't want to do anything extra with the banner, it can simply delete |
| 4537 | * the banner, since the caller has no more use for it. |
| 4538 | * |
| 4539 | * 2. Take ownership of the banner. If the osifc implementation wishes |
| 4540 | * to continue displaying the final screen configuration after a program |
| 4541 | * has terminated, it can simply take over the banner and leave it on the |
| 4542 | * screen. The osifc subsystem must eventually delete the banner itself |
| 4543 | * if it takes this routine; for example, if the osifc subsystem allows |
| 4544 | * another client program to be loaded into the same window after a |
| 4545 | * previous program has terminated, it would want to delete any orphaned |
| 4546 | * banners from the previous program when loading a new program. |
| 4547 | */ |
| 4548 | void os_banner_orphan(void *banner_handle); |
| 4549 | |
| 4550 | /* |
| 4551 | * Banner information structure. This is filled in by the system-specific |
| 4552 | * implementation in os_banner_getinfo(). |
| 4553 | */ |
| 4554 | struct os_banner_info_t |
| 4555 | { |
| 4556 | /* alignment */ |
| 4557 | int align; |
| 4558 | |
| 4559 | /* style flags - these indicate the style flags actually in use */ |
| 4560 | unsigned long style; |
| 4561 | |
| 4562 | /* |
| 4563 | * Actual on-screen size of the banner, in rows and columns. If the |
| 4564 | * banner is displayed in a proportional font or can display multiple |
| 4565 | * fonts of different sizes, this is approximated by the number of "0" |
| 4566 | * characters in the window's default font that will fit in the |
| 4567 | * window's display area. |
| 4568 | */ |
| 4569 | int rows; |
| 4570 | int columns; |
| 4571 | |
| 4572 | /* |
| 4573 | * Actual on-screen size of the banner in pixels. This is meaningful |
| 4574 | * only for full HTML interpreter; for text-only interpreters, these |
| 4575 | * are always set to zero. |
| 4576 | * |
| 4577 | * Note that even if we're running on a GUI operating system, these |
| 4578 | * aren't meaningful unless this is a full HTML interpreter. Text-only |
| 4579 | * interpreters should always set these to zero, even on GUI OS's. |
| 4580 | */ |
| 4581 | int pix_width; |
| 4582 | int pix_height; |
| 4583 | |
| 4584 | /* |
| 4585 | * OS line wrapping flag. If this is set, the window uses OS-level |
| 4586 | * line wrapping because the window uses a proportional font, so the |
| 4587 | * caller does not need to (and should not) perform line breaking in |
| 4588 | * text displayed in the window. |
| 4589 | * |
| 4590 | * Note that OS line wrapping is a PERMANENT feature of the window. |
| 4591 | * Callers can note this information once and expect it to remain |
| 4592 | * fixed through the window's lifetime. |
| 4593 | */ |
| 4594 | int os_line_wrap; |
| 4595 | }; |
| 4596 | typedef struct os_banner_info_t os_banner_info_t; |
| 4597 | |
| 4598 | /* |
| 4599 | * Get information on the banner - fills in the information structure with |
| 4600 | * the banner's current settings. Note that this should indicate the |
| 4601 | * ACTUAL properties of the banner, not the requested properties; this |
| 4602 | * allows callers to determine how the banner is actually displayed, which |
| 4603 | * depends upon the platform's capabilities and user preferences. |
| 4604 | * |
| 4605 | * Returns true if the information was successfully obtained, false if |
| 4606 | * not. This can return false if the underlying OS window has already |
| 4607 | * been closed by a user action, for example. |
| 4608 | */ |
| 4609 | int os_banner_getinfo(void *banner_handle, os_banner_info_t *info); |
| 4610 | |
| 4611 | /* |
| 4612 | * Get the character width/height of the banner, for layout purposes. This |
| 4613 | * gives the size of the banner in character cells. |
| 4614 | * |
| 4615 | * These are not meaningful when the underlying window uses a proportional |
| 4616 | * font or varying fonts of different sizes. When the size of text varies |
| 4617 | * in the window, the OS layer is responsible for word-wrapping and other |
| 4618 | * layout, in which case these simply return zero. |
| 4619 | * |
| 4620 | * Note that these routines might appear to be redundant with the 'rows' |
| 4621 | * and 'columns' information returned from os_banner_getinfo(), but these |
| 4622 | * have two important distinctions. First, these routines return only the |
| 4623 | * width and height information, so they can be implemented with less |
| 4624 | * overhead than os_banner_getinfo(); this is important because formatters |
| 4625 | * might need to call these routines frequently while formatting text. |
| 4626 | * Second, these routines are not required to return an approximation for |
| 4627 | * windows using proportional fonts, as os_banner_getinfo() does; these can |
| 4628 | * simply return zero when a proportional font is in use. |
| 4629 | */ |
| 4630 | int os_banner_get_charwidth(void *banner_handle); |
| 4631 | int os_banner_get_charheight(void *banner_handle); |
| 4632 | |
| 4633 | /* clear the contents of a banner */ |
| 4634 | void os_banner_clear(void *banner_handle); |
| 4635 | |
| 4636 | /* |
| 4637 | * Display output on a banner. Writes the output to the window on the |
| 4638 | * display at the current output position. |
| 4639 | * |
| 4640 | * The following special characters should be recognized and handled: |
| 4641 | * |
| 4642 | * '\n' - newline; move output position to the start of the next line. |
| 4643 | * |
| 4644 | * '\r' - move output position to start of current line; subsequent text |
| 4645 | * overwrites any text previously displayed on the current line. It is |
| 4646 | * permissible to delete the old text immediately on seeing the '\r', |
| 4647 | * rather than waiting for additional text to actually overwrite it. |
| 4648 | * |
| 4649 | * All other characters should simply be displayed as ordinary printing |
| 4650 | * text characters. Note that tab characters should not be passed to this |
| 4651 | * routine, but if they are, they can simply be treated as ordinary spaces |
| 4652 | * if desired. Other control characters (backspace, escape, etc) should |
| 4653 | * never be passed to this routine; the implementation is free to ignore |
| 4654 | * any control characters not listed above. |
| 4655 | * |
| 4656 | * If any text displayed here overflows the current boundaries of the |
| 4657 | * window on the screen, the text MUST be "clipped" to the current window |
| 4658 | * boundaries; in other words, anything this routine tries to display |
| 4659 | * outside of the window's on-screen rectangle must not actually be shown |
| 4660 | * on the screen. |
| 4661 | * |
| 4662 | * Text overflowing the display boundaries MUST also be retained in an |
| 4663 | * internal buffer. This internal buffer can be limited to the actual |
| 4664 | * maximum display size of the terminal screen or application window, if |
| 4665 | * desired. It is necessary to retain clipped text, because this allows a |
| 4666 | * window to be expanded to the size of its contents AFTER the contents |
| 4667 | * have already been displayed. |
| 4668 | * |
| 4669 | * If the banner does its own line wrapping, it must indicate this via the |
| 4670 | * os_line_wrap flag in the os_banner_getinfo() return data. If the |
| 4671 | * banner doesn't indicate this flag, then it must not do any line |
| 4672 | * wrapping at all, even if the caller attempts to write text beyond the |
| 4673 | * right edge of the window - any text overflowing the width of the window |
| 4674 | * must simply be clipped. |
| 4675 | * |
| 4676 | * Text grid banners must ALWAYS clip - these banners should never perform |
| 4677 | * any line wrapping. |
| 4678 | */ |
| 4679 | void os_banner_disp(void *banner_handle, const char *txt, size_t len); |
| 4680 | |
| 4681 | /* |
| 4682 | * Set the text attributes in a banner, for subsequent text displays. |
| 4683 | * 'attr' is a (bitwise-OR'd) combination of OS_ATTR_xxx values. |
| 4684 | */ |
| 4685 | void os_banner_set_attr(void *banner_handle, int attr); |
| 4686 | |
| 4687 | /* |
| 4688 | * Set the text color in a banner, for subsequent text displays. The 'fg' |
| 4689 | * and 'bg' colors are given as RGB or parameterized colors; see the |
| 4690 | * definition of os_color_t for details. |
| 4691 | * |
| 4692 | * If the underlying renderer is HTML-enabled, then this should not be |
| 4693 | * used; the appropriate HTML code should simply be displayed to the |
| 4694 | * banner instead. |
| 4695 | */ |
| 4696 | void os_banner_set_color(void *banner_handle, os_color_t fg, os_color_t bg); |
| 4697 | |
| 4698 | /* |
| 4699 | * Set the screen color in the banner - this is analogous to the screen |
| 4700 | * color in the main text area. |
| 4701 | * |
| 4702 | * If the underlying renderer is HTML-enabled, then this should not be |
| 4703 | * used; the HTML <BODY> tag should be used instead. |
| 4704 | */ |
| 4705 | void os_banner_set_screen_color(void *banner_handle, os_color_t color); |
| 4706 | |
| 4707 | /* flush output on a banner */ |
| 4708 | void os_banner_flush(void *banner_handle); |
| 4709 | |
| 4710 | /* |
| 4711 | * Set the banner's size. The size has the same meaning as in |
| 4712 | * os_banner_create(). |
| 4713 | * |
| 4714 | * 'is_advisory' indicates whether the sizing is required or advisory only. |
| 4715 | * If this flag is false, then the size should be set as requested. If |
| 4716 | * this flag is true, it means that the caller intends to call |
| 4717 | * os_banner_size_to_contents() at some point, and that the size being set |
| 4718 | * now is for advisory purposes only. Platforms that support |
| 4719 | * size-to-contents may simply ignore advisory sizing requests, although |
| 4720 | * they might want to ensure that they have sufficient off-screen buffer |
| 4721 | * space to keep track of the requested size of display, so that the |
| 4722 | * information the caller displays in preparation for calling |
| 4723 | * size-to-contents will be retained. Platforms that do not support |
| 4724 | * size-to-contents should set the requested size even when 'is_advisory' |
| 4725 | * is true. |
| 4726 | */ |
| 4727 | void os_banner_set_size(void *banner_handle, int siz, int siz_units, |
| 4728 | int is_advisory); |
| 4729 | |
| 4730 | /* |
| 4731 | * Set the banner to the size of its current contents. This can be used |
| 4732 | * to set the banner's size after some text (or other material) has been |
| 4733 | * displayed to the banner, so that the size can be set according to the |
| 4734 | * banner's actual space requirements. |
| 4735 | * |
| 4736 | * This changes the banner's "requested size" to match the current size. |
| 4737 | * Subsequent calls to os_banner_getinfo() will thus indicate a requested |
| 4738 | * size according to the size set here. |
| 4739 | */ |
| 4740 | void os_banner_size_to_contents(void *banner_handle); |
| 4741 | |
| 4742 | /* |
| 4743 | * Turn HTML mode on/off in the banner window. If the underlying renderer |
| 4744 | * doesn't support HTML, these have no effect. |
| 4745 | */ |
| 4746 | void os_banner_start_html(void *banner_handle); |
| 4747 | void os_banner_end_html(void *banner_handle); |
| 4748 | |
| 4749 | /* |
| 4750 | * Set the output coordinates in a text grid window. The grid window is |
| 4751 | * arranged into character cells numbered from row zero, column zero for |
| 4752 | * the upper left cell. This function can only be used if the window was |
| 4753 | * created with type OS_BANNER_TYPE_TEXTGRID; the request should simply be |
| 4754 | * ignored by other window types. |
| 4755 | * |
| 4756 | * Moving the output position has no immediate effect on the display, and |
| 4757 | * does not itself affect the "content size" for the purposes of |
| 4758 | * os_banner_size_to_contents(). This simply sets the coordinates where |
| 4759 | * any subsequent text is displayed. |
| 4760 | */ |
| 4761 | void os_banner_goto(void *banner_handle, int row, int col); |
| 4762 | |
| 4763 | |
| 4764 | /* ------------------------------------------------------------------------ */ |
| 4765 | /* |
| 4766 | * Get system information. 'code' is a SYSINFO_xxx code, which |
| 4767 | * specifies what type of information to get. The 'param' argument's |
| 4768 | * meaning depends on which code is selected. 'result' is a pointer to |
| 4769 | * an integer that is to be filled in with the result value. If the |
| 4770 | * code is not known, this function should return FALSE. If the code is |
| 4771 | * known, the function should fill in *result and return TRUE. |
| 4772 | */ |
| 4773 | int os_get_sysinfo(int code, void *param, long *result); |
| 4774 | |
| 4775 | /* determine if systemInfo is supported - os_get_sysinfo never gets this */ |
| 4776 | #define SYSINFO_SYSINFO 1 |
| 4777 | |
| 4778 | /* get interpreter version number - os_get_sysinfo never gets this */ |
| 4779 | #define SYSINFO_VERSION 2 |
| 4780 | |
| 4781 | /* get operating system name - os_get_sysinfo never gets this */ |
| 4782 | #define SYSINFO_OS_NAME 3 |
| 4783 | |
| 4784 | /* |
| 4785 | * Can the system process HTML directives? returns 1 if so, 0 if not. |
| 4786 | * Note that if this returns false, then all of the codes below from |
| 4787 | * JPEG to LINKS are implicitly false as well, since TADS can only use |
| 4788 | * images, sounds, and links through HTML. |
| 4789 | */ |
| 4790 | #define SYSINFO_HTML 4 |
| 4791 | |
| 4792 | /* can the system display JPEG's? 1 if yes, 0 if no */ |
| 4793 | #define SYSINFO_JPEG 5 |
| 4794 | |
| 4795 | /* can the system display PNG's? 1 if yes, 0 if no */ |
| 4796 | #define SYSINFO_PNG 6 |
| 4797 | |
| 4798 | /* can the system play WAV's? 1 if yes, 0 if no */ |
| 4799 | #define SYSINFO_WAV 7 |
| 4800 | |
| 4801 | /* can the system play MIDI's? 1 if yes, 0 if no */ |
| 4802 | #define SYSINFO_MIDI 8 |
| 4803 | |
| 4804 | /* can the system play MIDI and WAV's simultaneously? yes=1, no=0 */ |
| 4805 | #define SYSINFO_WAV_MIDI_OVL 9 |
| 4806 | |
| 4807 | /* can the system play multiple WAV's simultaneously? yes=1, no=0 */ |
| 4808 | #define SYSINFO_WAV_OVL 10 |
| 4809 | |
| 4810 | /* |
| 4811 | * GENERAL NOTES ON PREFERENCE SETTINGS: |
| 4812 | * |
| 4813 | * Several of the selectors below refer to the preference settings. We're |
| 4814 | * talking about user-settable options to control various aspects of the |
| 4815 | * interpreter. The conventional GUI for this kind of thing is a dialog |
| 4816 | * box reachable through a menu command named something like "Options" or |
| 4817 | * "Preferences". A couple of general notes about these: |
| 4818 | * |
| 4819 | * 1. The entire existence of a preferences mechanism is optional - |
| 4820 | * interpreter writers aren't required to implement anything along these |
| 4821 | * lines. In some cases the local platforms might not have any suitable |
| 4822 | * conventions for a preferences UI (e.g., character-mode console |
| 4823 | * applications), and in other cases the terp developer might just want to |
| 4824 | * omit a prefs mechanism because of the work involved to implement it, or |
| 4825 | * to keep the UI simpler. |
| 4826 | * |
| 4827 | * 2. If a given SYSINFO_PREF_xxx selector refers to a preference item |
| 4828 | * that's not implemented in the local interpreter, the terp should simply |
| 4829 | * return a suitable default result. For example, if the interpreter |
| 4830 | * doesn't have a preference item to allow the user to turn sounds off, the |
| 4831 | * selector SYSINFO_PREF_SOUNDS should return 1 to indicate that the user |
| 4832 | * has not in fact turned off sounds (because there's no way to do so). |
| 4833 | * |
| 4834 | * 3. The various SYSINFO_PREF_xxx selectors are purely queries - they're |
| 4835 | * NOT a mechanism for enforcing the preferences. For example, if the |
| 4836 | * interpreter provides a "Sounds On/Off" option, it's up to the terp to |
| 4837 | * enforce it the Off setting by ignoring any sound playback requests. The |
| 4838 | * game isn't under any obligation to query any of the preferences or to |
| 4839 | * alter its behavior based on their settings - you should expect that the |
| 4840 | * game will go on trying to play sounds even when "Sounds Off" is selected |
| 4841 | * in the preferences. The purpose of these SYSINFO selectors is to let |
| 4842 | * the game determine the current presentation status, but *only if it |
| 4843 | * cares*. For example, the game might determine whether or not sounds are |
| 4844 | * actually being heard just before playing a sound effect that's important |
| 4845 | * to the progress of the game, so that it can provide a visual alternative |
| 4846 | * if the effect won't be heard. |
| 4847 | */ |
| 4848 | |
| 4849 | /* |
| 4850 | * Get image preference setting - 1 = images can be displayed, 0 = images |
| 4851 | * are not being displayed because the user turned off images in the |
| 4852 | * preferences. This is, of course, irrelevant if images can't be |
| 4853 | * displayed at all. |
| 4854 | * |
| 4855 | * See the general notes on preferences queries above. |
| 4856 | */ |
| 4857 | #define SYSINFO_PREF_IMAGES 11 |
| 4858 | |
| 4859 | /* |
| 4860 | * Get digitized sound effect (WAV) preference setting - 1 = sounds can be |
| 4861 | * played, 0 = sounds are not being played because the user turned off |
| 4862 | * sounds in the preferences. |
| 4863 | * |
| 4864 | * See the general notes on preferences queries above. |
| 4865 | */ |
| 4866 | #define SYSINFO_PREF_SOUNDS 12 |
| 4867 | |
| 4868 | /* |
| 4869 | * Get music (MIDI) preference setting - 1 = music can be played, 0 = music |
| 4870 | * is not being played because the user turned off music in the |
| 4871 | * preferences. |
| 4872 | * |
| 4873 | * See the general notes on preferences queries above. |
| 4874 | */ |
| 4875 | #define SYSINFO_PREF_MUSIC 13 |
| 4876 | |
| 4877 | /* |
| 4878 | * Get link display preference setting - 0 = links are not being displayed |
| 4879 | * because the user set a preference item that suppresses all links (which |
| 4880 | * doesn't actually hide them, but merely displays them and otherwise |
| 4881 | * treats them as ordinary text). 1 = links are to be displayed normally. |
| 4882 | * 2 = links can be displayed temporarily by the user by pressing a key or |
| 4883 | * some similar action, but aren't being displayed at all times. |
| 4884 | * |
| 4885 | * See the general note on preferences queries above. |
| 4886 | */ |
| 4887 | #define SYSINFO_PREF_LINKS 14 |
| 4888 | |
| 4889 | /* can the system play MPEG sounds of any kind? */ |
| 4890 | #define SYSINFO_MPEG 15 |
| 4891 | |
| 4892 | /* can the system play MPEG audio 2.0 layer I/II/III sounds? */ |
| 4893 | #define SYSINFO_MPEG1 16 |
| 4894 | #define SYSINFO_MPEG2 17 |
| 4895 | #define SYSINFO_MPEG3 18 |
| 4896 | |
| 4897 | /* |
| 4898 | * is the system *currently* in HTML mode? os_get_sysinfo never gets |
| 4899 | * this code, since the portable output layer keeps track of this |
| 4900 | */ |
| 4901 | #define SYSINFO_HTML_MODE 19 |
| 4902 | |
| 4903 | /* |
| 4904 | * Does the system allow following external URL links of the various |
| 4905 | * types? These return true if the system is capable of following these |
| 4906 | * types of hypertext links, false if not. Note that, if the system is |
| 4907 | * capable of following these links, these should return true regardless |
| 4908 | * of any current mode settings; in particular, these should not be |
| 4909 | * sensitive to the current HTML mode or the current link display mode, |
| 4910 | * since the question is not whether a link now displayed can be |
| 4911 | * followed by the user, but rather whether the system has the |
| 4912 | * capability to follow these types of links at all. |
| 4913 | */ |
| 4914 | #define SYSINFO_LINKS_HTTP 20 |
| 4915 | #define SYSINFO_LINKS_FTP 21 |
| 4916 | #define SYSINFO_LINKS_NEWS 22 |
| 4917 | #define SYSINFO_LINKS_MAILTO 23 |
| 4918 | #define SYSINFO_LINKS_TELNET 24 |
| 4919 | |
| 4920 | /* is PNG transparency supported? */ |
| 4921 | #define SYSINFO_PNG_TRANS 25 |
| 4922 | |
| 4923 | /* is PNG alpha blending supported? */ |
| 4924 | #define SYSINFO_PNG_ALPHA 26 |
| 4925 | |
| 4926 | /* is the Ogg Vorbis audio format supported? */ |
| 4927 | #define SYSINFO_OGG 27 |
| 4928 | |
| 4929 | /* can the system display MNG's? */ |
| 4930 | #define SYSINFO_MNG 28 |
| 4931 | |
| 4932 | /* can the system display MNG's with transparency? */ |
| 4933 | #define SYSINFO_MNG_TRANS 29 |
| 4934 | |
| 4935 | /* can the system display MNG's with alpha blending? */ |
| 4936 | #define SYSINFO_MNG_ALPHA 30 |
| 4937 | |
| 4938 | /* can we display highlighted text in its own appearance? */ |
| 4939 | #define SYSINFO_TEXT_HILITE 31 |
| 4940 | |
| 4941 | /* |
| 4942 | * Can we display text colors? This returns a SYSINFO_TXC_xxx code |
| 4943 | * indicating the level of color support. |
| 4944 | * |
| 4945 | * The os_xxx interfaces don't presently support anything beyond the ANSI |
| 4946 | * colors; however, HTML-enabled interpreters generally support full RGB |
| 4947 | * colors, so we call this out as a separate level. |
| 4948 | */ |
| 4949 | #define SYSINFO_TEXT_COLORS 32 |
| 4950 | |
| 4951 | /* no text color support */ |
| 4952 | #define SYSINFO_TXC_NONE 0 |
| 4953 | |
| 4954 | /* parameterized color names only (OS_COLOR_P_TEXT, etc) */ |
| 4955 | #define SYSINFO_TXC_PARAM 1 |
| 4956 | |
| 4957 | /* |
| 4958 | * we support only the basic ANSI colors, foreground control only (white, |
| 4959 | * black, blue, red, green, yellow, cyan, magenta) |
| 4960 | */ |
| 4961 | #define SYSINFO_TXC_ANSI_FG 2 |
| 4962 | |
| 4963 | /* ANSI colors, foreground and background */ |
| 4964 | #define SYSINFO_TXC_ANSI_FGBG 3 |
| 4965 | |
| 4966 | /* full RGB support */ |
| 4967 | #define SYSINFO_TXC_RGB 4 |
| 4968 | |
| 4969 | /* are the os_banner_xxx() interfaces supported? */ |
| 4970 | #define SYSINFO_BANNERS 33 |
| 4971 | |
| 4972 | /* Interpreter Class - this returns one of the SYSINFO_ICLASS_xxx codes */ |
| 4973 | #define SYSINFO_INTERP_CLASS 34 |
| 4974 | |
| 4975 | /* |
| 4976 | * Interpreter class: Character-mode Text-Only. Interpreters of this class |
| 4977 | * use a single, fixed-pitch font to display all text, and use the |
| 4978 | * text-only HTML subset, and cannot display graphics. |
| 4979 | */ |
| 4980 | #define SYSINFO_ICLASS_TEXT 1 |
| 4981 | |
| 4982 | /* |
| 4983 | * Interpreter class: Text-Only GUI. Interpreters of this class are |
| 4984 | * traditional text-only interpreters running on graphical operating |
| 4985 | * systems. These interpreters might use multiple fonts (for example, they |
| 4986 | * might display highlighted text in boldface), and might use |
| 4987 | * proportionally-spaced text for some windows. These interpreters support |
| 4988 | * the text-only HTML subset, and cannot display graphics. |
| 4989 | * |
| 4990 | * Text-only GUI interpreters act essentially the same as character-mode |
| 4991 | * text-only interpreters, from the perspective of the client program. |
| 4992 | */ |
| 4993 | #define SYSINFO_ICLASS_TEXTGUI 2 |
| 4994 | |
| 4995 | /* |
| 4996 | * Interpreter class: HTML. Interpreters of this class can display |
| 4997 | * graphics and sounds, can display multiple fonts and font sizes, can use |
| 4998 | * proportional fonts, and support the full HTML TADS markup language for |
| 4999 | * formatting. |
| 5000 | */ |
| 5001 | #define SYSINFO_ICLASS_HTML 3 |
| 5002 | |
| 5003 | /* |
| 5004 | * Audio fade information. |
| 5005 | * |
| 5006 | * SYSINFO_AUDIO_FADE: basic fade-in and fade-out support. Interpreters |
| 5007 | * that don't support audio fade at all should return 0. Interpreters that |
| 5008 | * support fades should return a bitwise OR'd combination of |
| 5009 | * SYSINFO_AUDIOFADE_xxx flags below indicating which formats can be used |
| 5010 | * with fades. |
| 5011 | * |
| 5012 | * SYSINFO_AUDIO_CROSSFADE: cross-fades are supported (i.e., simultaneous |
| 5013 | * play of overlapping tracks, one fading out while the other fades in). |
| 5014 | * If cross-fades aren't supported, return 0. If they're supported, return |
| 5015 | * a combination of SYSINFO_AUDIOFADE_xxx flags indicating which formats |
| 5016 | * can be used with cross-fades. |
| 5017 | */ |
| 5018 | #define SYSINFO_AUDIO_FADE 35 |
| 5019 | #define SYSINFO_AUDIO_CROSSFADE 36 |
| 5020 | |
| 5021 | /* |
| 5022 | * Specific audio fading features. These are bit flags that can be |
| 5023 | * combined to indicate the fading capabilities of the interpreter. |
| 5024 | */ |
| 5025 | #define SYSINFO_AUDIOFADE_MPEG 0x0001 /* supported for MPEG audio */ |
| 5026 | #define SYSINFO_AUDIOFADE_OGG 0x0002 /* supported for Ogg Vorbis */ |
| 5027 | #define SYSINFO_AUDIOFADE_WAV 0x0004 /* supported for WAV */ |
| 5028 | #define SYSINFO_AUDIOFADE_MIDI 0x0008 /* supported for MIDI */ |
| 5029 | |
| 5030 | |
| 5031 | /* ------------------------------------------------------------------------ */ |
| 5032 | /* |
| 5033 | * Integer division operators. For any compiler that follows ANSI C |
| 5034 | * rules, no definitions are required for these routine, since the |
| 5035 | * standard definitions below will work properly. However, if your |
| 5036 | * compiler does not follow ANSI standards with respect to integer |
| 5037 | * division of negative numbers, you must provide implementations of |
| 5038 | * these routines that produce the correct results. |
| 5039 | * |
| 5040 | * Division must "truncate towards zero," which means that any |
| 5041 | * fractional part is dropped from the result. If the result is |
| 5042 | * positive, the result must be the largest integer less than the |
| 5043 | * algebraic result: 11/3 yields 3. If the result is negative, the |
| 5044 | * result must be the smallest integer less than the result: (-11)/3 |
| 5045 | * yields -3. |
| 5046 | * |
| 5047 | * The remainder must obey the relationship (a/b)*b + a%b == a, for any |
| 5048 | * integers a and b (b != 0). |
| 5049 | * |
| 5050 | * If your compiler does not obey the ANSI rules for the division |
| 5051 | * operators, make the following changes in your osxxx.h file |
| 5052 | * |
| 5053 | * - define the symbol OS_NON_ANSI_DIVIDE in the osxxx.h file |
| 5054 | * |
| 5055 | * - either define your own macros for os_divide_long() and |
| 5056 | * os_remainder_long(), or put actual prototypes for these functions |
| 5057 | * into your osxxx.h file and write appropriate implementations of these |
| 5058 | * functions in one of your osxxx.c or .cpp files. |
| 5059 | */ |
| 5060 | /* long os_divide_long(long a, long b); // returns (a/b) with ANSI rules */ |
| 5061 | /* long os_remainder_long(long a, long b); // returns (a%b) with ANSI rules */ |
| 5062 | |
| 5063 | /* standard definitions for any ANSI compiler */ |
| 5064 | #ifndef OS_NON_ANSI_DIVIDE |
| 5065 | #define os_divide_long(a, b) ((a) / (b)) |
| 5066 | #define os_remainder_long(a, b) ((a) % (b)) |
| 5067 | #endif |
| 5068 | |
| 5069 | /* ------------------------------------------------------------------------ */ |
| 5070 | /* |
| 5071 | * Special "switch" statement optimization flags. These definitions are |
| 5072 | * OPTIONAL - if a platform doesn't provide these definitions, suitable |
| 5073 | * code that's fully portable will be used. |
| 5074 | * |
| 5075 | * On some compilers, the performance of a "switch" statement can be |
| 5076 | * improved by fully populating the switch with all possible "case" |
| 5077 | * values. When the compiler can safely assume that every possible "case" |
| 5078 | * value is specifically called out in the switch, the compiler can |
| 5079 | * generate somewhat faster code by omitting any range check for the |
| 5080 | * controlling expression of the switch: a range check is unnecessary |
| 5081 | * because the compiler knows that the value can never be outside the |
| 5082 | * "case" table. |
| 5083 | * |
| 5084 | * This type of optimization doesn't apply to all compilers. When a given |
| 5085 | * platform's compiler can be coerced to produce faster "switch" |
| 5086 | * statements, though, there might be some benefit in defining these |
| 5087 | * symbols according to local platform rules. |
| 5088 | * |
| 5089 | * First, #define OS_FILL_OUT_CASE_TABLES if you want this type of switch |
| 5090 | * optimization at all. This symbol is merely a flag, so it doesn't need |
| 5091 | * a value - #defining it is enough to activate the special code. If you |
| 5092 | * don't define this symbol, then the code that cares about this will |
| 5093 | * simply generate ordinary switches, leaving holes in the case table and |
| 5094 | * using "default:" to cover them. If the platform's osxxx.h header does |
| 5095 | * #define OS_FILL_OUT_CASE_TABLES, then the portable code will know to |
| 5096 | * fill out case tables with all possible alternatives where it's possible |
| 5097 | * and potentially beneficial to do so. |
| 5098 | * |
| 5099 | * Second, if you #define OS_FILL_OUT_CASE_TABLES, you MUST ALSO #define |
| 5100 | * OS_IMPOSSIBLE_DEFAULT_CASE. The value for this symbol must be some |
| 5101 | * code to insert into a "switch" statement at the point where a |
| 5102 | * "default:" case would normally go. On some compilers, special |
| 5103 | * extensions allow the program to explicitly indicate within a switch |
| 5104 | * that all possible cases are covered, so that the compiler doesn't have |
| 5105 | * to be relied upon to infer this for itself (which would be impossible |
| 5106 | * for it to do in many cases anyway). You can use an empty definition |
| 5107 | * for this symbol if your compiler doesn't have any special construct for |
| 5108 | * indicating a fully-populated case table. |
| 5109 | * |
| 5110 | * Note that *most* switch statements in portable code won't care one way |
| 5111 | * or the other about these definitions. There's a time/space trade-off |
| 5112 | * in fully populating a switch's case table, so only the most |
| 5113 | * time-critical code will bother trying. |
| 5114 | */ |
| 5115 | |
| 5116 | |
| 5117 | |
| 5118 | /* ------------------------------------------------------------------------ */ |
| 5119 | /* |
| 5120 | * TADS 2 swapping configuration. Define OS_DEFAULT_SWAP_ENABLED to 0 |
| 5121 | * if you want swapping turned off, 1 if you want it turned on. If we |
| 5122 | * haven't defined a default swapping mode yet, turn swapping on by |
| 5123 | * default. |
| 5124 | */ |
| 5125 | #ifndef OS_DEFAULT_SWAP_ENABLED |
| 5126 | # define OS_DEFAULT_SWAP_ENABLED 1 |
| 5127 | #endif |
| 5128 | |
| 5129 | /* |
| 5130 | * If the system "long description" (for the banner) isn't defined, make |
| 5131 | * it the same as the platform ID string. |
| 5132 | */ |
| 5133 | #ifndef OS_SYSTEM_LDESC |
| 5134 | # define OS_SYSTEM_LDESC OS_SYSTEM_NAME |
| 5135 | #endif |
| 5136 | |
| 5137 | /* |
| 5138 | * TADS 2 Usage Lines |
| 5139 | * |
| 5140 | * If the "usage" lines (i.e., the descriptive lines of text describing |
| 5141 | * how to run the various programs) haven't been otherwise defined, |
| 5142 | * define defaults for them here. Some platforms use names other than |
| 5143 | * tc, tr, and tdb for the tools (for example, on Unix they're usually |
| 5144 | * tadsc, tadsr, and tadsdb), so the usage lines should be adjusted |
| 5145 | * accordingly; simply define them earlier than this point (in this file |
| 5146 | * or in one of the files included by this file, such as osunixt.h) for |
| 5147 | * non-default text. |
| 5148 | */ |
| 5149 | #ifndef OS_TC_USAGE |
| 5150 | # define OS_TC_USAGE "usage: tc [options] file" |
| 5151 | #endif |
| 5152 | #ifndef OS_TR_USAGE |
| 5153 | # define OS_TR_USAGE "usage: tr [options] file" |
| 5154 | #endif |
| 5155 | #ifndef OS_TDB_USAGE |
| 5156 | # define OS_TDB_USAGE "usage: tdb [options] file" |
| 5157 | #endif |
| 5158 | |
| 5159 | /* |
| 5160 | * Ports can define a special TDB startup message, which is displayed |
| 5161 | * after the copyright/version banner. If it's not defined at this |
| 5162 | * point, define it to an empty string. |
| 5163 | */ |
| 5164 | #ifndef OS_TDB_STARTUP_MSG |
| 5165 | # define OS_TDB_STARTUP_MSG "" |
| 5166 | #endif |
| 5167 | |
| 5168 | /* |
| 5169 | * If a system patch sub-level isn't defined, define it here as zero. |
| 5170 | * The patch sub-level is used on some systems where a number of ports |
| 5171 | * are derived from a base port (for example, a large number of ports |
| 5172 | * are based on the generic Unix port). For platforms like the Mac, |
| 5173 | * where the porting work applies only to that one platform, this |
| 5174 | * sub-level isn't meaningful. |
| 5175 | */ |
| 5176 | #ifndef OS_SYSTEM_PATCHSUBLVL |
| 5177 | # define OS_SYSTEM_PATCHSUBLVL "0" |
| 5178 | #endif |
| 5179 | |
| 5180 | |
| 5181 | #ifdef __cplusplus |
| 5182 | } |
| 5183 | #endif |
| 5184 | |
| 5185 | #endif /* OSIFC_H */ |
| 5186 |