| 1 | /****************************************************************************** |
| 2 | * * |
| 3 | * Copyright (C) 2006-2009 by Tor Andersson. * |
| 4 | * Copyright (C) 2010 by Ben Cressey. * |
| 5 | * * |
| 6 | * This file is part of Gargoyle. * |
| 7 | * * |
| 8 | * Gargoyle is free software; you can redistribute it and/or modify * |
| 9 | * it under the terms of the GNU General Public License as published by * |
| 10 | * the Free Software Foundation; either version 2 of the License, or * |
| 11 | * (at your option) any later version. * |
| 12 | * * |
| 13 | * Gargoyle is distributed in the hope that it will be useful, * |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 16 | * GNU General Public License for more details. * |
| 17 | * * |
| 18 | * You should have received a copy of the GNU General Public License * |
| 19 | * along with Gargoyle; if not, write to the Free Software * |
| 20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * |
| 21 | * * |
| 22 | *****************************************************************************/ |
| 23 | |
| 24 | /* Adapted from osfrobtads.h from FrobTADS 1.2.3. |
| 25 | * FrobTADS copyright (C) 2009 Nikos Chantziaras. |
| 26 | */ |
| 27 | |
| 28 | #ifndef OSGLK_H |
| 29 | #define OSGLK_H |
| 30 | |
| 31 | #define USE_OS_LINEWRAP /* tell tads not to let os do paging */ |
| 32 | |
| 33 | #include <stdlib.h> |
| 34 | #include <string.h> |
| 35 | #include <stdio.h> |
| 36 | #include <stdint.h> |
| 37 | #include <stdarg.h> |
| 38 | #ifdef _WIN32 |
| 39 | #define WIN32_LEAN_AND_MEAN |
| 40 | #ifndef NOMINMAX |
| 41 | #define NOMINMAX |
| 42 | #endif |
| 43 | #include <windows.h> |
| 44 | #else |
| 45 | #include <dirent.h> |
| 46 | #endif |
| 47 | #include <sys/types.h> |
| 48 | #include <sys/stat.h> |
| 49 | #include <unistd.h> |
| 50 | |
| 51 | /* We don't support the Atari 2600. */ |
| 52 | #include "osbigmem.h" |
| 53 | |
| 54 | /* For strcasecmp() */ |
| 55 | #include <strings.h> |
| 56 | |
| 57 | #include "glk.h" |
| 58 | #include "glkstart.h" |
| 59 | |
| 60 | #define TRUE 1 |
| 61 | #define FALSE 0 |
| 62 | |
| 63 | #ifdef __cplusplus |
| 64 | extern "C" { |
| 65 | #endif |
| 66 | |
| 67 | /* Used by the base code to inhibit "unused parameter" compiler warnings. */ |
| 68 | #define VARUSED(var) (void)var |
| 69 | |
| 70 | /* We assume that the C-compiler is mostly ANSI compatible. */ |
| 71 | #define OSANSI |
| 72 | |
| 73 | /* Special function qualifier needed for certain types of callback |
| 74 | * functions. This is for old 16-bit systems; we don't need it and |
| 75 | * define it to nothing. */ |
| 76 | #define OS_LOADDS |
| 77 | |
| 78 | /* Unices don't suffer the near/far pointers brain damage (thank God) so |
| 79 | * we make this a do-nothing macro. */ |
| 80 | #define osfar_t |
| 81 | |
| 82 | /* This is used to explicitly discard computed values (some compilers |
| 83 | * would otherwise give a warning like "computed value not used" in some |
| 84 | * cases). Casting to void should work on every ANSI-Compiler. */ |
| 85 | #define DISCARD (void) |
| 86 | |
| 87 | /* Copies a struct into another. ANSI C allows the assignment operator |
| 88 | * to be used with structs. */ |
| 89 | #define OSCPYSTRUCT(x,y) ((x)=(y)) |
| 90 | |
| 91 | /* Link error messages into the application. */ |
| 92 | #define ERR_LINK_MESSAGES |
| 93 | |
| 94 | /* Program Exit Codes. */ |
| 95 | #define OSEXSUCC 0 /* Successful completion. */ |
| 96 | #define OSEXFAIL 1 /* Failure. */ |
| 97 | |
| 98 | /* Here we configure the osgen layer; refer to tads2/osgen3.c for more |
| 99 | * information about the meaning of these macros. */ |
| 100 | #define USE_DOSEXT |
| 101 | #define USE_NULLSTYPE |
| 102 | |
| 103 | /* Theoretical maximum osmalloc() size. |
| 104 | * Unix systems have at least a 32-bit memory space. Even on 64-bit |
| 105 | * systems, 2^32 is a good value, so we don't bother trying to find out |
| 106 | * an exact value. */ |
| 107 | #define OSMALMAX 0xffffffffL |
| 108 | |
| 109 | #ifdef _WIN32 |
| 110 | struct Win32Dir { |
| 111 | HANDLE hFindFile; |
| 112 | char *dirname; |
| 113 | WIN32_FIND_DATAA findFileData; |
| 114 | }; |
| 115 | typedef struct Win32Dir *osdirhdl_t; |
| 116 | #else |
| 117 | /* Directory handle for searches via os_open_dir() et al. */ |
| 118 | typedef DIR* osdirhdl_t; |
| 119 | #endif |
| 120 | |
| 121 | /* file type/mode bits */ |
| 122 | #define OSFMODE_FILE S_IFREG |
| 123 | #define OSFMODE_DIR S_IFDIR |
| 124 | #define OSFMODE_CHAR S_IFCHR |
| 125 | #ifdef S_IFBLK |
| 126 | #define OSFMODE_BLK S_IFBLK |
| 127 | #else |
| 128 | #define OSFMODE_BLK 0 |
| 129 | #endif |
| 130 | #ifdef S_IFIFO |
| 131 | #define OSFMODE_PIPE S_IFIFO |
| 132 | #else |
| 133 | #define OSFMODE_PIPE 0 |
| 134 | #endif |
| 135 | #ifdef S_IFLNK |
| 136 | #define OSFMODE_LINK S_IFLNK |
| 137 | #else |
| 138 | #define OSFMODE_LINK 0 |
| 139 | #endif |
| 140 | #ifdef S_IFSOCK |
| 141 | #define OSFMODE_SOCKET S_IFSOCK |
| 142 | #else |
| 143 | #define OSFMODE_SOCKET 0 |
| 144 | #endif |
| 145 | |
| 146 | /* File attribute bits. */ |
| 147 | #define OSFATTR_HIDDEN 0x0001 |
| 148 | #define OSFATTR_SYSTEM 0x0002 |
| 149 | #define OSFATTR_READ 0x0004 |
| 150 | #define OSFATTR_WRITE 0x0008 |
| 151 | |
| 152 | /* Get a file's stat() type. */ |
| 153 | int osfmode( const char* fname, int follow_links, unsigned long* mode, |
| 154 | unsigned long* attr ); |
| 155 | |
| 156 | /* Disable the Tads swap file; computers have plenty of RAM these days. |
| 157 | */ |
| 158 | #define OS_DEFAULT_SWAP_ENABLED 0 |
| 159 | |
| 160 | /* TADS 2 macro/function configuration. Modern configurations always |
| 161 | * use the no-macro versions, so these definitions should always be set |
| 162 | * as shown below. */ |
| 163 | #define OS_MCM_NO_MACRO |
| 164 | #define ERR_NO_MACRO |
| 165 | |
| 166 | /* These values are used for the "mode" parameter of osfseek() to |
| 167 | * indicate where to seek in the file. */ |
| 168 | #define OSFSK_SET SEEK_SET /* Set position relative to the start of the file. */ |
| 169 | #define OSFSK_CUR SEEK_CUR /* Set position relative to the current file position. */ |
| 170 | #define OSFSK_END SEEK_END /* Set position relative to the end of the file. */ |
| 171 | |
| 172 | |
| 173 | /* ============= Functions follow ================ */ |
| 174 | |
| 175 | /* Emglken needs its own implementation as it doesn't have full file system support */ |
| 176 | #ifdef EMGLKEN |
| 177 | |
| 178 | #include "emglken.h" |
| 179 | #include "osemglken.h" |
| 180 | |
| 181 | #else /* EMGLKEN */ |
| 182 | |
| 183 | /* File handle structure for osfxxx functions. */ |
| 184 | typedef FILE osfildef; |
| 185 | |
| 186 | /* Open text file for reading. */ |
| 187 | #define osfoprt(fname,typ) (fopen((fname),"r")) |
| 188 | |
| 189 | /* Open text file for writing. */ |
| 190 | #define osfopwt(fname,typ) (fopen((fname),"w")) |
| 191 | |
| 192 | /* Open text file for reading/writing. If the file already exists, |
| 193 | * truncate the existing contents. Create a new file if it doesn't |
| 194 | * already exist. */ |
| 195 | #define osfoprwtt(fname,typ) (fopen((fname),"w+")) |
| 196 | |
| 197 | /* Open binary file for writing. */ |
| 198 | #define osfopwb(fname,typ) (fopen((fname),"wb")) |
| 199 | |
| 200 | /* Open source file for reading - use the appropriate text or binary |
| 201 | * mode. */ |
| 202 | #define osfoprs osfoprt |
| 203 | |
| 204 | /* Open binary file for reading. */ |
| 205 | #define osfoprb(fname,typ) (fopen((fname),"rb")) |
| 206 | |
| 207 | /* Open binary file for reading/writing. If the file already exists, |
| 208 | * truncate the existing contents. Create a new file if it doesn't |
| 209 | * already exist. */ |
| 210 | #define osfoprwtb(fname,typ) (fopen((fname),"w+b")) |
| 211 | |
| 212 | /* Get a line of text from a text file. */ |
| 213 | #define osfgets fgets |
| 214 | |
| 215 | /* Write a line of text to a text file. */ |
| 216 | #define osfputs fputs |
| 217 | |
| 218 | /* Write bytes to file. */ |
| 219 | #define osfwb(fp,buf,bufl) (fwrite((buf),(bufl),1,(fp))!=1) |
| 220 | |
| 221 | /* Flush buffered writes to a file. */ |
| 222 | #define osfflush fflush |
| 223 | |
| 224 | /* Read bytes from file. */ |
| 225 | #define osfrb(fp,buf,bufl) (fread((buf),(bufl),1,(fp))!=1) |
| 226 | |
| 227 | /* Read bytes from file and return the number of bytes read. */ |
| 228 | #define osfrbc(fp,buf,bufl) (fread((buf),1,(bufl),(fp))) |
| 229 | |
| 230 | /* Get the current seek location in the file. */ |
| 231 | #define osfpos ftell |
| 232 | |
| 233 | /* Seek to a location in the file. */ |
| 234 | #define osfseek fseek |
| 235 | |
| 236 | /* Close a file. */ |
| 237 | #define osfcls fclose |
| 238 | |
| 239 | /* Delete a file. */ |
| 240 | #define osfdel remove |
| 241 | |
| 242 | /* Access a file - determine if the file exists. |
| 243 | * |
| 244 | * We map this to the access() function. It should be available in |
| 245 | * virtually every system out there, as it appears in many standards |
| 246 | * (SVID, AT&T, POSIX, X/OPEN, BSD 4.3, DOS, MS Windows, maybe more). */ |
| 247 | #define osfacc(fname) (access((fname), F_OK)) |
| 248 | |
| 249 | /* Rename a file. */ |
| 250 | #define os_rename_file(from, to) (rename(from, to) == 0) |
| 251 | |
| 252 | /* Get a file's stat() type. */ |
| 253 | struct os_file_stat_t; |
| 254 | int os_file_stat( const char* fname, int follow_links, |
| 255 | struct os_file_stat_t* s ); |
| 256 | |
| 257 | /* Get a character from a file. */ |
| 258 | #define osfgetc fgetc |
| 259 | |
| 260 | #endif /* EMGLKEN */ |
| 261 | |
| 262 | /* Open text file for reading and writing, keeping the file's existing |
| 263 | * contents if the file already exists or creating a new file if no |
| 264 | * such file exists. */ |
| 265 | osfildef* |
| 266 | osfoprwt( const char* fname, os_filetype_t typ ); |
| 267 | |
| 268 | /* Open binary file for reading/writing. If the file already exists, |
| 269 | * keep the existing contents. Create a new file if it doesn't already |
| 270 | * exist. */ |
| 271 | osfildef* |
| 272 | osfoprwb( const char* fname, os_filetype_t typ ); |
| 273 | |
| 274 | /* Allocate a block of memory of the given size in bytes. */ |
| 275 | #define osmalloc malloc |
| 276 | |
| 277 | /* Free memory previously allocated with osmalloc(). */ |
| 278 | #define osfree free |
| 279 | |
| 280 | /* Reallocate memory previously allocated with osmalloc() or osrealloc(), |
| 281 | * changing the block's size to the given number of bytes. */ |
| 282 | #define osrealloc realloc |
| 283 | |
| 284 | /* Set busy cursor. |
| 285 | * |
| 286 | * We don't have a mouse cursor so there's no need to implement this. */ |
| 287 | #define os_csr_busy(a) |
| 288 | |
| 289 | /* Update progress display. |
| 290 | * |
| 291 | * We don't provide any kind of "compilation progress display", so we |
| 292 | * just define this as an empty macro. |
| 293 | */ |
| 294 | #define os_progress(fname,linenum) |
| 295 | |
| 296 | /* Initialize the time zone. |
| 297 | * |
| 298 | * We don't need this (I think). */ |
| 299 | #define os_tzset() |
| 300 | |
| 301 | #define TADS_OEM_NAME "Mr Oizo" |
| 302 | |
| 303 | /* Replace stricmp with strcasecmp */ |
| 304 | #define strnicmp strncasecmp |
| 305 | #define stricmp strcasecmp |
| 306 | |
| 307 | #define memicmp os_memicmp |
| 308 | |
| 309 | int os_memicmp(const char *a, const char *b, int n); |
| 310 | |
| 311 | #define OS_SYSTEM_NAME "GlkTADS" |
| 312 | |
| 313 | #define OSFNMAX 1024 |
| 314 | |
| 315 | #ifdef _WIN32 |
| 316 | #define OSPATHCHAR '\\' |
| 317 | #define OSPATHALT "/:" |
| 318 | #define OSPATHURL "\\/" |
| 319 | #define OSPATHSEP ';' |
| 320 | #define OS_NEWLINE_SEQ "\r\n" |
| 321 | #else |
| 322 | #define OSPATHCHAR '/' |
| 323 | #define OSPATHALT "" |
| 324 | #define OSPATHURL "/" |
| 325 | #define OSPATHSEP ':' |
| 326 | #define OS_NEWLINE_SEQ "\n" |
| 327 | #endif |
| 328 | |
| 329 | #define OSPATHPWD "." |
| 330 | |
| 331 | void os_put_buffer (unsigned char *buf, size_t len); |
| 332 | void os_get_buffer (unsigned char *buf, size_t len, size_t init); |
| 333 | unsigned char *os_fill_buffer (unsigned char *buf, size_t len); |
| 334 | |
| 335 | #define OS_MAXWIDTH 255 |
| 336 | |
| 337 | #define OS_ATTR_HILITE OS_ATTR_BOLD |
| 338 | #define OS_ATTR_EM OS_ATTR_ITALIC |
| 339 | #define OS_ATTR_STRONG OS_ATTR_BOLD |
| 340 | |
| 341 | #define OS_DECLARATIVE_TLS |
| 342 | #define OS_DECL_TLS(t, v) t v |
| 343 | |
| 344 | int os_vasprintf(char **bufptr, const char *fmt, va_list ap); |
| 345 | |
| 346 | // Support both the old garglk_fileref_get_name() and the new |
| 347 | // glkunix_fileref_get_filename() functions. |
| 348 | #if defined(GLK_MODULE_FILEREF_GET_NAME) && !defined(GLKUNIX_FILEREF_GET_FILENAME) |
| 349 | #define glkunix_fileref_get_filename garglk_fileref_get_name |
| 350 | #define GLKUNIX_FILEREF_GET_FILENAME |
| 351 | #endif |
| 352 | |
| 353 | #ifdef __cplusplus |
| 354 | } |
| 355 | #endif |
| 356 | |
| 357 | #endif /* OSGLK_H */ |