Debian Codegraph @

terps/tads/tads2/beos/osbeos.cpp

raw symbols SCIP
1 /*
2 * osbeos.c -- copyright (c) 2000 by Christopher Tate, all rights reserved.
3 *
4 * Please see the accompanying license file, LICENSE.TXT, for information
5 * on using and copying this software.
6 */
7
8 #include <stdlib.h>
9 #include <ctype.h>
10 #include <termios.h>
11 #include <sys/stat.h>
12 #include <app/Application.h>
13 #include <app/Roster.h>
14 #include <kernel/OS.h>
15 #include <storage/File.h>
16 #include <storage/Path.h>
17 #include <storage/Resources.h>
18
19 #include "appctx.h"
20 #include "os.h"
21
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25
26 /* cbreak mode is useful; code borrowed from jzip's unix port */
27 void set_cbreak_mode (int mode);
28 void set_cbreak_mode (int mode)
29 {
30 int status;
31 struct termios new_termios;
32 static struct termios old_termios;
33
34 if (mode)
35 status = tcgetattr (fileno (stdin), &old_termios);
36 else
37 status = tcsetattr (fileno (stdin), TCSANOW, &old_termios);
38 if (status) {
39 perror ("ioctl");
40 exit (1);
41 }
42
43 if (mode) {
44 status = tcgetattr (fileno (stdin), &new_termios);
45 if (status) {
46 perror ("ioctl");
47 exit (1);
48 }
49
50 new_termios.c_lflag &= ~(ICANON | ECHO);
51
52 /* the next two lines of code added by Mark Phillips. The patch */
53 /* was for sun and __hpux, active only if those were #defined, */
54 /* but most POSIX boxen (SunOS, HP-UX, Dec OSF, Irix for sure) */
55 /* can use this... It makes character input work. VMIN and */
56 /* VTIME are reused on some systems, so when the mode is switched*/
57 /* to RAW all character access is, by default, buffered wrong. */
58 /* For the curious: VMIN='\004' and VTIME='\005' octal on */
59 /* these systems. VMIN is usually EOF and VTIME is EOL. (JDH) */
60 new_termios.c_cc[VMIN]=1;
61 new_termios.c_cc[VTIME]=2;
62
63 status = tcsetattr (fileno (stdin), TCSANOW, &new_termios);
64 if (status) {
65 perror ("ioctl");
66 exit (1);
67 }
68 }
69
70 }/* set_cbreak_mode */
71
72 /* ------------------------------------------------------------------------ */
73 /*
74 * A note on character sets:
75 *
76 * Except where noted, all character strings passed to and from the
77 * osxxx functions defined herein use the local operating system
78 * representation. On a Windows machine localized to Eastern Europe,
79 * for example, the character strings passed to and from the osxxx
80 * functions would use single-byte characters in the Windows code page
81 * 1250 representation.
82 *
83 * Callers that use multiple character sets must implement mappings to
84 * and from the local character set when calling the osxxx functions.
85 * The osxxx implementations are thus free to ignore any issues related
86 * to character set conversion or mapping.
87 *
88 * The osxxx implementations are specifically not permitted to use
89 * double-byte Unicode as the native character set, nor any other
90 * character set where a null byte could appear as part of a non-null
91 * character. In particular, callers may assume that null-terminated
92 * strings passed to and from the osxxx functions contain no embedded
93 * null bytes. Multi-byte character sets (i.e., character sets with
94 * mixed single-byte and double-byte characters) may be used as long as
95 * a null byte is never part of any multi-byte character, since this
96 * would guarantee that a null byte could always be taken as a null
97 * character without knowledge of the encoding or context.
98 */
99
100 /* ------------------------------------------------------------------------ */
101 /*
102 * "Far" Pointers. Most platforms can ignore this. For platforms with
103 * mixed-mode addressing models, where pointers of different sizes can
104 * be used within a single program and hence some pointers require
105 * qualification to indicate that they use a non-default addressing
106 * model, the keyword OSFAR should be defined to the appropriate
107 * compiler-specific extension keyword.
108 *
109 * If you don't know what I'm talking about here, you should just ignore
110 * it, because your platform probably doesn't have anything this
111 * sinister. As of this writing, this applies only to MS-DOS, and then
112 * only to 16-bit implementations that must interact with other 16-bit
113 * programs via dynamic linking or other mechanisms.
114 */
115
116
117 /* ------------------------------------------------------------------------ */
118 /*
119 * Hardware Configuration. Define the following functions appropriately
120 * for your hardware. For efficiency, these functions should be defined
121 * as macros if possible.
122 *
123 * Note that these hardware definitions are independent of the OS, at
124 * least to the extent that your OS can run on multiple types of
125 * hardware. So, rather than combining these definitions into your
126 * osxxx.h header file, we recommend that you put these definitions in a
127 * separate h_yyy.h header file, which can be configured into os.h with
128 * an appropriate "_M_yyy" preprocessor symbol. Refer to os.h for
129 * details of configuring the hardware include file.
130 */
131
132 /*
133 * Round a size up to worst-case alignment boundary. For example, on a
134 * platform where the largest type must be aligned on a 4-byte boundary,
135 * this should round the value up to the next higher mutliple of 4 and
136 * return the result.
137 */
138 /* size_t osrndsz(size_t siz); */
139
140 /*
141 * Round a pointer up to worst-case alignment boundary.
142 */
143 /* unsigned char *osrndpt(unsigned char *ptr); */
144
145 /*
146 * Read an unaligned portable unsigned 2-byte value, returning an int
147 * value. The portable representation has the least significant byte
148 * first, so the value 0x1234 is represented as the byte 0x34, followed
149 * by the byte 0x12.
150 *
151 * The source value must be treated as unsigned, but the result is
152 * signed. This is significant on 32- and 64-bit platforms, because it
153 * means that the source value should never be sign-extended to 32-bits.
154 * For example, if the source value is 0xffff, the result is 65535, not
155 * -1.
156 */
157 /* int osrp2(unsigned char *p); */
158
159 /*
160 * Read an unaligned portable signed 2-byte value, returning int. This
161 * differs from osrp2() in that this function treats the source value as
162 * signed, and returns a signed result; hence, on 32- and 64-bit
163 * platforms, the result must be sign-extended to the int size. For
164 * example, if the source value is 0xffff, the result is -1.
165 */
166 /* int osrp2s(unsigned char *p); */
167
168 /*
169 * Write int to unaligned portable 2-byte value. The portable
170 * representation stores the low-order byte first in memory, so
171 * oswp2(0x1234) should result in storing a byte value 0x34 in the first
172 * byte, and 0x12 in the second byte.
173 */
174 /* void oswp2(unsigned char *p, int i); */
175
176 /*
177 * Read an unaligned portable 4-byte value, returning long. The
178 * underlying value should be considered signed, and the result is
179 * signed. The portable representation stores the bytes starting with
180 * the least significant: the value 0x12345678 is stored with 0x78 in
181 * the first byte, 0x56 in the second byte, 0x34 in the third byte, and
182 * 0x12 in the fourth byte.
183 */
184 /* long osrp4(unsigned char *p); */
185
186 /*
187 * Write a long to an unaligned portable 4-byte value. The portable
188 * representation stores the low-order byte first in memory, so
189 * 0x12345678 is written to memory as 0x78, 0x56, 0x34, 0x12.
190 */
191 /* void oswp2(unsigned char *p, long l); */
192
193
194
195 /* ------------------------------------------------------------------------ */
196 /*
197 * Platform Identifiers. You must define the following macros in your
198 * osxxx.h header file:
199 *
200 * OS_SYSTEM_NAME - a string giving the system identifier. This string
201 * must contain only characters that are valid in a TADS identifier:
202 * letters, numbers, and underscores; and must start with a letter or
203 * underscore. For example, on MS-DOS, this string is "MSDOS".
204 *
205 * OS_SYSTEM_LDESC - a string giving the system descriptive name. This
206 * is used in messages displayed to the user. For example, on MS-DOS,
207 * this string is "MS-DOS".
208 */
209
210
211 /* ------------------------------------------------------------------------ */
212 /*
213 * Message Linking Configuration. You should #define ERR_LINK_MESSAGES
214 * in your osxxx.h header file if you want error messages linked into
215 * the application. Leave this symbol undefined if you want an external
216 * message file.
217 */
218
219
220 /* ------------------------------------------------------------------------ */
221 /*
222 * Program Exit Codes. These values are used for the argument to exit()
223 * to conform to local conventions. Define the following values in your
224 * OS-specific header:
225 *
226 * OSEXSUCC - successful completion. Usually defined to 0.
227 *. OSEXFAIL - failure. Usually defined to 1.
228 */
229
230
231 /* ------------------------------------------------------------------------ */
232 /*
233 * Basic memory management interface. These functions are merely
234 * documented here, but no prototypes are defined, because most
235 * platforms #define macros for these functions and types, mapping them
236 * to malloc or other system interfaces.
237 */
238
239 /*
240 * Theoretical maximum osmalloc() size. This may be less than the
241 * capacity of the argument to osmalloc() on some systems. For example,
242 * on segmented architectures (such as 16-bit x86), memory is divided
243 * into segments, so a single memory allocation can allocate only a
244 * subset of the total addressable memory in the system. This value
245 * thus specifies the maximum amount of memory that can be allocated in
246 * one chunk.
247 *
248 * Note that this is an architectural maximum for the hardware and
249 * operating system. It doesn't have anything to do with the total
250 * amount of memory actually available at run-time.
251 *
252 * OSMALMAX - #define to a constant long value with theoretical maximum
253 * osmalloc() argument value.
254 */
255
256
257 /*
258 * Allocate a block of memory of the given size in bytes. The actual
259 * allocation may be larger, but may be no smaller. The block returned
260 * should be worst-case aligned (i.e., suitably aligned for any type).
261 * Return null if the given amount of memory is not available.
262 */
263 /* void *osmalloc(size_t siz); */
264
265 /*
266 * Free memory previously allocated with osmalloc().
267 */
268 /* void osfree(void *block); */
269
270 /*
271 * Reallocate memory previously allocated with osmalloc() or
272 * osrealloc(), changing the block's size to the given number of bytes.
273 * If necessary, a new block at a different address can be allocated, in
274 * which case the data from the original block is copied (the lesser of
275 * the old block size and the new size is copied) to the new block, and
276 * the original block is freed. If the new size is less than the old
277 * size, this need not do anything at all, since the returned block can
278 * be larger than the new requested size. If the block cannot be
279 * enlarged to the requested size, return null.
280 */
281 /* void *osrealloc(void *block, size_t siz); */
282
283
284 /* ------------------------------------------------------------------------ */
285 /*
286 * Basic file I/O interface. These functions are merely documented
287 * here, but no prototypes are defined, because most platforms #define
288 * macros for these functions and types, mapping them to stdio or other
289 * system I/O interfaces.
290 */
291
292
293 /*
294 * Define the following values in your OS header to indicate local
295 * conventions:
296 *
297 * OSFNMAX - integer indicating maximum length of a filename
298 *
299 * OSPATHCHAR - character giving the normal path separator character
300 *. OSPATHALT - string giving other path separator characters
301 *. OSPATHSEP - directory separator for PATH-style environment variables
302 *
303 * For example, on DOS, OSPATHCHAR is '\\', OSPATHALT is "/:", and
304 * OSPATHSEP is ';'. On Unix, OSPATHCHAR is '\', OSPATHALT is "", and
305 * OSPATHSEP is ':'.
306 */
307
308 /*
309 * Define the type osfildef as the appropriate file handle structure for
310 * your osfxxx functions. This type is always used as a pointer, but
311 * the value is always obtained from an osfopxxx call, and is never
312 * synthesized by portable code, so you can use essentially any type
313 * here that you want.
314 *
315 * For platforms that use C stdio functions to implement the osfxxx
316 * functions, osfildef can simply be defined as FILE.
317 */
318 /* typedef FILE osfildef; */
319
320
321 /*
322 * File types - see osifctyp.h
323 */
324
325
326 /*
327 * Open text file for reading. Returns NULL on error.
328 *
329 * A text file differs from a binary file in that some systems perform
330 * translations to map between C conventions and local file system
331 * conventions; for example, on DOS, the stdio library maps the DOS
332 * CR-LF newline convention to the C-style '\n' newline format. On many
333 * systems (Unix, for example), there is no distinction between text and
334 * binary files.
335 */
336 /* osfildef *osfoprt(const char *fname, os_filetype_t typ); */
337
338 /*
339 * Open text file for writing; returns NULL on error
340 */
341 /* osfildef *osfopwt(const char *fname, os_filetype_t typ); */
342
343 /*
344 * Open text file for reading and writing; returns NULL on error
345 */
346 osfildef *osfoprwt(const char *fname, os_filetype_t /*typ*/)
347 {
348 osfildef *fp;
349
350 /* try opening an existing file in read/write mode */
351 fp = fopen(fname, "r+");
352
353 /* if that failed, create a new file in read/write mode */
354 if (fp == 0)
355 fp = fopen(fname, "w+");
356
357 /* return the file */
358 return fp;
359 }
360
361 /*
362 * Open text file for reading/writing. If the file already exists,
363 * truncate the existing contents. Create a new file if it doesn't
364 * already exist. Return null on error.
365 */
366 /* osfildef *osfoprwtt(const char *fname, os_filetype_t typ); */
367
368 /*
369 * Open binary file for writing; returns NULL on error.
370 */
371 /* osfildef *osfopwb(const char *fname, os_filetype_t typ); */
372
373 /*
374 * Open source file for reading - use the appropriate text or binary
375 * mode.
376 */
377 /* osfildef *osfoprs(const char *fname, os_filetype_t typ); */
378
379 /*
380 * Open binary file for reading; returns NULL on error.
381 */
382 /* osfildef *osfoprb(const char *fname, os_filetype_t typ); */
383
384 /*
385 * Get a line of text from a text file. Uses fgets semantics.
386 */
387 /* char *osfgets(char *buf, size_t len, osfildef *fp); */
388
389 /*
390 * Write a line of text to a text file. Uses fputs semantics.
391 */
392 /* void osfputs(const char *buf, osfildef *fp); */
393
394 /*
395 * Open binary file for reading/writing. If the file already exists,
396 * keep the existing contents. Create a new file if it doesn't already
397 * exist. Return null on error.
398 */
399 osfildef *osfoprwb(const char *fname, os_filetype_t /*typ*/)
400 {
401 osfildef *fp;
402
403 /* try opening an existing file in read/write mode */
404 fp = fopen(fname, "r+b");
405
406 /* if that failed, create a new file in read/write mode */
407 if (fp == 0)
408 fp = fopen(fname, "w+b");
409
410 /* return the file */
411 return fp;
412 }
413
414 /*
415 * Open binary file for reading/writing. If the file already exists,
416 * truncate the existing contents. Create a new file if it doesn't
417 * already exist. Return null on error.
418 */
419 /* osfildef *osfoprwtb(const char *fname, os_filetype_t typ); */
420
421 /*
422 * Write bytes to file. Return 0 on success, non-zero on error.
423 */
424 /* int osfwb(osfildef *fp, const uchar *buf, int bufl); */
425
426 /*
427 * Read bytes from file. Return 0 on success, non-zero on error.
428 */
429 /* int osfrb(osfildef *fp, uchar *buf, int bufl); */
430
431 /*
432 * Read bytes from file and return the number of bytes read. 0
433 * indicates that no bytes could be read.
434 */
435 /* size_t osfrbc(osfildef *fp, uchar *buf, size_t bufl); */
436
437 /*
438 * Get the current seek location in the file. The first byte of the
439 * file has seek position 0.
440 */
441 /* long osfpos(osfildef *fp); */
442
443 /*
444 * Seek to a location in the file. The first byte of the file has seek
445 * position 0. Returns zero on success, non-zero on error.
446 *
447 * The following constants must be defined in your OS-specific header;
448 * these values are used for the "mode" parameter to indicate where to
449 * seek in the file:
450 *
451 * OSFSK_SET - set position relative to the start of the file
452 *. OSFSK_CUR - set position relative to the current file position
453 *. OSFSK_END - set position relative to the end of the file
454 */
455 /* int osfseek(osfildef *fp, long pos, int mode); */
456
457 /*
458 * Close a file.
459 */
460 /* void osfcls(osfildef *fp); */
461
462 /*
463 * Delete a file. Returns zero on success, non-zero on error.
464 */
465 /* int osfdel(const char *fname); */
466
467 /*
468 * Access a file - determine if the file exists. Returns zero if the
469 * file exists, non-zero if not. (The semantics may seem a little
470 * weird, but this is consistent with the conventions used by most of
471 * the other osfxxx calls: zero indicates success, non-zero indicates an
472 * error. If the file exists, "accessing" it was successful, so osfacc
473 * returns zero; if the file doesn't exist, accessing it gets an error,
474 * hence a non-zero return code.)
475 */
476 /* int osfacc(const char *fname) */
477
478 /*
479 * Get a character from a file. Provides the same semantics as fgetc().
480 */
481 /* int osfgetc(osfildef *fp); */
482
483 /* ------------------------------------------------------------------------ */
484 /*
485 * File time stamps
486 */
487
488 /*
489 * File timestamp type. This type must be defined by the
490 * system-specific header, and should map to a local type that can be
491 * used to obtain information on a file's creation, modification, or
492 * access time. Generic code is not allowed to do anything with data of
493 * this type except pass them to the routines defined here that take
494 * values of this type as parameters.
495 *
496 * On Unix, for example, this structure can be defined as having a
497 * single member of type time_t. (We define this as an incomplete
498 * structure type here so that we can refer to it in the prototypes
499 * below.)
500 */
501 typedef struct os_file_time_t os_file_time_t;
502
503 /*
504 * Get the creation/modification/access time for a file. Fills in the
505 * os_file_time_t value with the time for the file. Returns zero on
506 * success, non-zero on failure (the file doesn't exist, the program has
507 * insufficient privileges to access the file, etc).
508 */
509 int os_get_file_cre_time(os_file_time_t *t, const char *fname)
510 {
511 struct stat info;
512
513 /* get the file information */
514 if (stat(fname, &info))
515 return 1;
516
517 /* set the creation time in the return structure */
518 t->t = info.st_ctime;
519 return 0;
520 }
521
522 int os_get_file_mod_time(os_file_time_t *t, const char *fname)
523 {
524 struct stat info;
525
526 /* get the file information */
527 if (stat(fname, &info))
528 return 1;
529
530 /* set the modification time in the return structure */
531 t->t = info.st_mtime;
532 return 0;
533 }
534
535 int os_get_file_acc_time(os_file_time_t *t, const char *fname)
536 {
537 struct stat info;
538
539 /* get the file information */
540 if (stat(fname, &info))
541 return 1;
542
543 /* set the access time in the return structure */
544 t->t = info.st_atime;
545 return 0;
546 }
547
548 /*
549 * Compare two file time values. These values must be obtained with one
550 * of the os_get_file_xxx_time() functions. Returns 1 if the first time
551 * is later than the second time; 0 if the two times are the same; and
552 * -1 if the first time is earlier than the second time.
553 */
554 int os_cmp_file_times(const os_file_time_t *a, const os_file_time_t *b)
555 {
556 if (a->t < b->t)
557 return -1;
558 else if (a->t == b->t)
559 return 0;
560 else
561 return 1;
562 }
563
564
565 /* ------------------------------------------------------------------------ */
566 /*
567 * Find the first file matching a given pattern. The returned context
568 * pointer is a pointer to whatever system-dependent context structure
569 * is needed to continue the search with the next file, and is opaque to
570 * the caller. The caller must pass the context pointer to the
571 * next-file routine. The caller can optionally cancel a search by
572 * calling the close-search routine with the context pointer. If the
573 * return value is null, it indicates that no matching files were found.
574 * If a file was found, outbuf will be filled in with its name, and
575 * isdir will be set to true if the match is a directory, false if it's
576 * a file. If pattern is null, all files in the given directory should
577 * be returned; otherwise, pattern is a string containing '*' and '?' as
578 * wildcard characters, but not containing any directory separators, and
579 * all files in the given directory matching the pattern should be
580 * returned.
581 *
582 * Important: because this routine may allocate memory for the returned
583 * context structure, the caller must either call os_find_next_file
584 * until that routine returns null, or call os_find_close() to cancel
585 * the search, to ensure that the os code has a chance to release the
586 * allocated memory.
587 *
588 * 'outbuf' should be set on output to the name of the matching file,
589 * without any path information.
590 *
591 * 'outpathbuf' should be set on output to full path of the matching
592 * file. If possible, 'outpathbuf' should use the same relative or
593 * absolute notation that the search criteria used on input. For
594 * example, if dir = "resfiles", and the file found is "MyPic.jpg",
595 * outpathbuf should be set to "resfiles/MyPic.jpg" (or appropriate
596 * syntax for the local platform). Similarly, if dir =
597 * "/home/tads/resfiles", outpath buf should be
598 * "/home/tads/resfiles/MyPic.jpg". The result should always conform to
599 * correct local conventions, which may require some amount of
600 * manipulation of the filename; for example, on the Mac, if dir =
601 * "resfiles", the result should be ":resfiles:MyPic.jpg" (note the
602 * added leading colon) to conform to Macintosh relative path notation.
603 *
604 * Note that 'outpathbuf' may be null, in which case the caller is not
605 * interested in the full path information.
606 */
607 /*
608 * Note the following possible ways this function may be called:
609 *
610 * dir = "", pattern = filename - in this case, pattern is the name of a
611 * file or directory in the current directory. filename *might* be a
612 * relative path specified by the user (on a command line, for example);
613 * for instance, on Unix, it could be something like "resfiles/jpegs".
614 *
615 * dir = path, pattern = filname - same as above, but this time the
616 * filename or directory pattern is relative to the given path, rather
617 * than to the current directory. For example, we could have dir =
618 * "/games/mygame" and pattern = "resfiles/jpegs".
619 *
620 * dir = path, pattern = 0 (NULL) - this should search for all files in
621 * the given path. The path might be absolute or it might be relative.
622 *
623 * dir = path, pattern = "*" - this should have the same result as when
624 * pattern = 0.
625 *
626 * dir = path, pattern = "*.ext" - this should search for all files in
627 * the given path whose names end with ".ext".
628 *
629 * dir = path, pattern = "abc*" - this should search for all files in
630 * the given path whose names start with "abc".
631 *
632 * All of these combinations are possible because callers, for
633 * portability, must generally not manipulate filenames directly;
634 * instead, callers obtain paths and search strings from external
635 * sources, such as from the user, and present them to this routine with
636 * minimal manipulation.
637 */
638 void *os_find_first_file(const char *dir, const char *pattern,
639 char *outbuf, size_t outbufsiz, int *isdir,
640 char *outpathbuf, size_t outpathbufsiz)
641 {
642 debugger("os_find_first_file() not implemented");
643 return NULL;
644 }
645
646 /*
647 * Implementation notes for porting os_find_first_file:
648 *
649 * The algorithm for this routine should go something like this:
650 *
651 * - If 'path' is null, create a variable real_path and initialize it
652 * with the current directory. Otherwise, copy path to real_path.
653 *
654 * - If 'pattern' contains any directory separators ("/" on Unix, for
655 * example), change real_path so that it reflects the additional leading
656 * subdirectories in the path in 'pattern', and remove the leading path
657 * information from 'pattern'. For example, on Unix, if real_path
658 * starts out as "./subdir", and pattern is "resfiles/jpegs", change
659 * real_path to "./subdir/resfiles", and change pattern to "jpegs".
660 * Take care to add and remove path separators as needed to keep the
661 * path strings well-formed.
662 *
663 * - Begin a search using appropriate OS API's for all files in
664 * real_path.
665 *
666 * - Check each file found. Skip any files that don't match 'pattern',
667 * treating "*" as a wildcard that matches any string of zero or more
668 * characters, and "?" as a wildcard that matches any single character
669 * (or matches nothing at the end of a string). For example:
670 *
671 *. "*" matches anything
672 *. "abc?" matches "abc", "abcd", "abce", "abcf", but not "abcde"
673 *. "abc???" matches "abc", "abcd", "abcde", "abcdef", but not "abcdefg"
674 *. "?xyz" matches "wxyz", "axyz", but not "xyz" or "abcxyz"
675 *
676 * - Return the first file that matches, if any, by filling in 'outbuf'
677 * and 'isdir' with appropriate information. Before returning, allocate
678 * a context structure (which is entirely for your own use, and opaque
679 * to the caller) and fill it in with the information necessary for
680 * os_find_next_file to get the next matching file. If no file matches,
681 * return null.
682 */
683
684
685 /*
686 * Find the next matching file, continuing a search started with
687 * os_find_first_file(). Returns null if no more files were found, in
688 * which case the search will have been automatically closed (i.e.,
689 * there's no need to call os_find_close() after this routine returns
690 * null). Returns a non-null context pointer, which is to be passed to
691 * this function again to get the next file, if a file was found.
692 *
693 * 'outbuf' and 'outpathbuf' are filled in with the filename (without
694 * path) and full path (relative or absolute, as appropriate),
695 * respectively, in the same manner as they do for os_find_first_file().
696 *
697 * Implementation note: if os_find_first_file() allocated memory for the
698 * search context, this routine must free the memory if it returs null,
699 * because this indicates that the search is finished and the caller
700 * need not call os_find_close().
701 */
702 void *os_find_next_file(void *ctx, char *outbuf, size_t outbufsiz,
703 int *isdir, char *outpathbuf, size_t outpathbufsiz)
704 {
705 debugger("os_find_next_file() not implemented");
706 return NULL;
707 }
708
709 /*
710 * Cancel a search. The context pointer returned by the last call to
711 * os_find_first_file() or os_find_next_file() is the parameter. There
712 * is no need to call this function if find-first or find-next returned
713 * null, since they will have automatically closed the search.
714 *
715 * Implementation note: if os_find_first_file() allocated memory for the
716 * search context, this routine should release the memory.
717 */
718 void os_find_close(void *ctx)
719 {
720 debugger("os_find_close() not implemented");
721 }
722
723 /*
724 * Determine if the given filename refers to a special file. Returns
725 * the appropriate enum value if so, or OS_SPECFILE_NONE if not. The
726 * given filename must be a root name - it must not contain a path
727 * prefix. The primary purpose of this function is to classify the
728 * 'outbuf' results from os_find_first/next_file().
729 */
730 enum os_specfile_t os_is_special_file(const char *fname)
731 {
732 const char* lastchar = fname + strlen(fname) - 1;
733 if ((lastchar < fname) || (*lastchar != '.')) return OS_SPECFILE_NONE;
734
735 // it's either "." or "..", so check the next-to-last char
736 lastchar--;
737 if ((lastchar < fname) || (*lastchar != '.')) return OS_SPECFILE_SELF;
738 else return OS_SPECFILE_PARENT;
739 }
740
741 /* ------------------------------------------------------------------------ */
742 /*
743 * Convert string to all-lowercase.
744 */
745 char *os_strlwr(char *s)
746 {
747 char *start;
748 start = s;
749 while (*s) {
750 if (isupper(*s))
751 *s = tolower(*s);
752
753 s++;
754 }
755 return start;
756 }
757
758
759 /* ------------------------------------------------------------------------ */
760 /*
761 * Character classifications for quote characters. os_squote() returns
762 * true if its argument is any type of single-quote character;
763 * os_dquote() returns true if its argument is any type of double-quote
764 * character; and os_qmatch(a, b) returns true if a and b are matching
765 * open- and close-quote characters.
766 *
767 * These functions allow systems with extended character codes with
768 * weird quote characters (such as the Mac) to match the weird
769 * characters, so that users can use the extended quotes in input.
770 *
771 * These are usually implemented as macros. The most common
772 * implementation simply returns true for the standard ASCII quote
773 * characters:
774 *
775 * #define os_squote(c) ((c) == '\'')
776 *. #define os_dquote(c) ((c) == '"')
777 *. #define os_qmatch(a, b) ((a) == (b))
778 *
779 * These functions take int arguments to allow for the possibility of
780 * Unicode input.
781 */
782 /* int os_squote(int c); */
783 /* int os_dquote(int c); */
784 /* int os_qmatch(int a, int b); */
785
786
787 /* ------------------------------------------------------------------------ */
788 /*
789 * Seek to the resource file embedded in the current executable file.
790 * On platforms where the executable file format allows additional
791 * information to be attached to an executable, this function can be
792 * used to find the extra information within the executable.
793 *
794 * The 'typ' argument gives a resource type to find. This is an
795 * arbitrary string that the caller uses to identify what type of object
796 * to find. The "TGAM" type, for example, is used by convention to
797 * indicate a TADS compiled GAM file.
798 */
799 osfildef *os_exeseek(const char* /*exefile*/, const char* /*typ*/)
800 {
801 return NULL;
802 }
803
804
805 /* ------------------------------------------------------------------------ */
806 /*
807 * Load a string resource. Given a string ID number, load the string
808 * into the given buffer.
809 *
810 * Returns zero on success, non-zero if an error occurs (for example,
811 * the buffer is too small, or the requested resource isn't present).
812 *
813 * Whenever possible, implementations should use an operating system
814 * mechanism for loading the string from a user-modifiable resource
815 * store; this will make localization of these strings easier, since the
816 * resource store can be modified without the need to recompile the
817 * application. For example, on the Macintosh, the normal system string
818 * resource mechanism should be used to load the string from the
819 * application's resource fork.
820 *
821 * When no operating system mechanism exists, the resources can be
822 * stored as an array of strings in a static variable; this isn't ideal,
823 * because it makes it much more difficult to localize the application.
824 *
825 * Resource ID's are application-defined. For example, for TADS 2,
826 * "res.h" defines the resource ID's.
827 */
828 int os_get_str_rsc(int id, char *buf, size_t buflen)
829 {
830 app_info appInfo;
831 be_app->GetAppInfo(&appInfo);
832 BFile appFile(&appInfo.ref, B_READ_ONLY);
833 BResources res(&appFile);
834
835 size_t strSize;
836 const char* str = (const char*) res.LoadResource(B_STRING_TYPE, id, &strSize);
837 if (str)
838 {
839 if (strSize < buflen - 1) // room for the trailing null?
840 {
841 memcpy(buf, str, strSize);
842 buf[strSize] = '\0'; // ensure that it's null terminated
843 return 0; // success!
844 }
845 }
846 return -1; // couldn't load the resource, or resource too small
847 }
848
849
850 /* ------------------------------------------------------------------------ */
851 /*
852 * External, dynamically-loaded code. These routines allow us to load
853 * and invoke external native code.
854 */
855
856 /* load an external function from a file given the name of the file */
857 int (*os_exfil(const char */*name*/))(void *)
858 {
859 return NULL;
860 }
861
862 /* load an external function from an open file */
863 int (*os_exfld(osfildef */*fp*/, unsigned /*len*/))(void *)
864 {
865 return NULL;
866 }
867
868 /*
869 * call an external function, passing it an argument (a string pointer),
870 * and passing back the string pointer returned by the external function
871 */
872 int os_excall(int (*/*extfn*/)(void *), void */*arg*/)
873 {
874 return 0;
875 }
876
877
878 /* ------------------------------------------------------------------------ */
879 /*
880 * look for a file in the standard locations: current directory, program
881 * directory, TADS path
882 */
883 int os_locate(const char *fname, int flen, const char *arg0,
884 char *buf, size_t bufsiz);
885
886
887 /* ------------------------------------------------------------------------ */
888 /*
889 * Create and open a temporary file. Creates and opens a temporary
890 * file. If 'fname' is null, this routine must choose a file name and
891 * fill in 'buf' with the chosen name; if possible, the file should be
892 * in the conventional location for temporary files on this system, and
893 * should be unique (i.e., it shouldn't be the same as any existing
894 * file).
895 *
896 * The filename stored in 'buf' should not be used by the caller except
897 * to pass to osfdel_temp(). On some systems, it may not be possible to
898 * determine the actual filename of a temporary file; in such cases, the
899 * implementation may store an empty string in the buffer.
900 *
901 * After the caller is done with this file, it should close the file
902 * (using osfcls() as normal), then call osfdel_temp() to delete the
903 * temporary file.
904 */
905 osfildef *os_create_tempfile(const char *fname, char *buf);
906
907 /*
908 * Delete a temporary file - this is used to delete a file created with
909 * os_create_tempfile(). For most platforms, this can simply be defined
910 * the same way as osfdel(). For platforms where the operating system
911 * or file manager will automatically delete a file opened as a
912 * temporary file, this routine should do nothing at all, since the
913 * system will take care of deleting the temp file.
914 */
915 int osfdel_temp(const char *fname);
916
917
918 /*
919 * Get the temporary file path. This should fill in the buffer with a
920 * path prefix (suitable for strcat'ing a filename onto) for a good
921 * directory for a temporary file, such as the swap file.
922 */
923 void os_get_tmp_path(char *buf)
924 {
925 strcpy(buf, "/tmp/");
926 }
927
928
929 /* ------------------------------------------------------------------------ */
930 /*
931 * Switch to a new working directory.
932 */
933 void os_set_pwd(const char *dir);
934
935 /*
936 * Switch the working directory to the directory containing the given
937 * file. Generally, this routine should only need to parse the filename
938 * enough to determine the part that's the directory path, then use
939 * os_set_pwd() to switch to that directory.
940 */
941 void os_set_pwd_file(const char *filename);
942
943
944 /* ------------------------------------------------------------------------ */
945 /*
946 * Filename manipulation routines
947 */
948
949 /* apply a default extension to a filename, if it doesn't already have one */
950 void os_defext(char *fname, const char *ext)
951 {
952 char* p = fname + strlen(fname); // find the end of the string
953 if (p > fname) // did we legitimately find it?
954 {
955 p -= (strlen(ext) + 1); // where the "." and the extension itself should be
956 if (p > fname) // still looking good?
957 {
958 if ((*p == '.') && (!strcmp(p+1, ext)))
959 {
960 return; // that's it, fname already ends with the extension
961 }
962 }
963 }
964
965 // if we get here, we need to append "." and the extension
966 strcat(fname, ".");
967 strcat(fname, ext);
968 }
969
970 /* unconditionally add an extention to a filename */
971 void os_addext(char *fname, const char *ext)
972 {
973 strcat(fname, ".");
974 strcat(fname, ext);
975 }
976
977 /* remove the extension from a filename */
978 void os_remext(char *fname)
979 {
980 char* p = fname + strlen(fname) - 1;
981 while ((p > fname) && (*p != '.')) p--;
982 if (*p == '.') *p = '\0';
983 }
984
985 /*
986 * Get a pointer to the root name portion of a filename. This is the
987 * part of the filename after any path or directory prefix. For
988 * example, on Unix, given the string "/home/mjr/deep.gam", this
989 * function should return a pointer to the 'd' in "deep.gam". If the
990 * filename doesn't appear to have a path prefix, it should simply
991 * return the argument unchanged.
992 */
993 char *os_get_root_name(char *buf)
994 {
995 char* p = strrchr(buf, '/');
996 if (p > buf) p++;
997 else p = buf;
998 return p;
999 }
1000
1001 /*
1002 * Determine whether a filename specifies an absolute or relative path.
1003 * This is used to analyze filenames provided by the user (for example,
1004 * in a #include directive, or on a command line) to determine if the
1005 * filename can be considered relative or absolute. This can be used,
1006 * for example, to determine whether to search a directory path for a
1007 * file; if a given filename is absolute, a path search makes no sense.
1008 * A filename that doesn't specify an absolute path can be combined with
1009 * a path using os_build_full_path().
1010 *
1011 * Returns true if the filename specifies an absolute path, false if
1012 * not.
1013 */
1014 int os_is_file_absolute(const char *fname)
1015 {
1016 if (fname && (fname[0] == '/')) return TRUE;
1017 else return FALSE;
1018 }
1019
1020 /*
1021 * Extract the path from a filename. Fills in pathbuf with the path
1022 * portion of the filename. If the filename has no path, the pathbuf
1023 * should be set appropriately for the current directory (on Unix or
1024 * DOS, for example, it can be set to an empty string).
1025 *
1026 * The result can end with a path separator character or not, depending
1027 * on local OS conventions. Paths extracted with this function can only
1028 * be used with os_build_full_path(), so the conventions should match
1029 * that function's.
1030 *
1031 * Unix examples:
1032 *
1033 * "/home/mjr/deep.gam" -> "/home/mjr"
1034 *. "deep.gam" -> ""
1035 *. "games/deep.gam" -> "games"
1036 *
1037 * Mac examples:
1038 *
1039 * ":home:mjr:deep.gam" -> ":home:mjr"
1040 *. "Hard Disk:games:deep.gam" -> "Hard Disk:games"
1041 *. "Hard Disk:deep.gam" -> "Hard Disk:"
1042 *
1043 * Note in the last example that we've retained the trailing colon in
1044 * the path, whereas we didn't in the others; although the others could
1045 * also retain the trailing colon, it's required only for the last case.
1046 * The last case requires the colon because it would otherwise be
1047 * impossible to determine whether "Hard Disk" was a local subdirectory
1048 * or a volume name.
1049 */
1050 void os_get_path_name(char *pathbuf, size_t pathbuflen, const char *fname)
1051 {
1052 BPath filepath(fname);
1053 BPath parent;
1054 status_t err = filepath.GetParent(&parent);
1055 if (!err)
1056 {
1057 const char* parentPath = parent.Path();
1058 if (strlen(parentPath) < pathbuflen)
1059 {
1060 strcpy(pathbuf, parentPath);
1061 }
1062 }
1063 }
1064
1065 /*
1066 * Build a full path name, given a path and a filename. The path may
1067 * have been specified by the user, or may have been extracted from
1068 * another file via os_get_path_name(). This routine must take care to
1069 * add path separators as needed, but also must take care not to add too
1070 * many path separators.
1071 *
1072 * Note that relative path names may require special care on some
1073 * platforms. For example, on the Macintosh, a path of "games" and a
1074 * filename "deep.gam" should yield ":games:deep.gam" (note the addition
1075 * of the leading colon).
1076 *
1077 * Unix examples:
1078 *
1079 * "/home/mjr", "deep.gam" -> "/home/mjr/deep.gam"
1080 *. "/home/mjr/", "deep.gam" -> "/home/mjr/deep.gam"
1081 *. "games", "deep.gam" -> "games/deep.gam"
1082 *. "games/", "deep.gam" -> "games/deep.gam"
1083 *
1084 * Mac examples:
1085 *
1086 * "Hard Disk:", "deep.gam" -> "Hard Disk:deep.gam"
1087 *. ":games:", "deep.gam" -> ":games:deep.gam"
1088 *. "games", "deep.gam" -> ":games:deep.gam"
1089 */
1090 void os_build_full_path(char *fullpathbuf, size_t fullpathbuflen,
1091 const char *path, const char *filename)
1092 {
1093 const char* p;
1094 BPath pathobj(path, filename, true); // force normalization of the path
1095 p = (*path) ? pathobj.Path() : filename; // only use the BPath for non-null paths
1096 if (strlen(p) < fullpathbuflen)
1097 {
1098 strcpy(fullpathbuf, p);
1099 }
1100 }
1101
1102 /*
1103 * Convert an OS filename path to a relative URL. Paths provided to
1104 * this function should always be relative to the current directory, so
1105 * the resulting URL should be relative. If end_sep is true, it means
1106 * that the last character of the result should be a '/', even if the
1107 * input path doesn't end with a path separator character.
1108 *
1109 * This routine should replace all system-specific path separator
1110 * characters in the input name with forward slashes ('/'). In
1111 * addition, if a relative path on the system starts with a path
1112 * separator, that leading path separator should be removed; for
1113 * example, on the Macintosh, a path of ":images:rooms:startroom.jpeg"
1114 * should be converted to "images/rooms/startroom.jpeg".
1115 */
1116 void os_cvt_dir_url(char *result_buf, size_t result_buf_size,
1117 const char *src_path, int end_sep);
1118
1119 /*
1120 * Convert a relative URL into a relative filename path. Paths provided
1121 * to this function should always be relative, so the resulting path
1122 * should be relative to the current directory. This function
1123 * essentially reverses the transformation done by os_cvt_dir_url(). If
1124 * end_sep is true, the path should end with a path separator character,
1125 * so that filenames can be strcat'ed onto the result to form a full
1126 * filename.
1127 */
1128 void os_cvt_url_dir(char *result_buf, size_t result_buf_size,
1129 const char *src_url, int end_sep);
1130
1131
1132 /* ------------------------------------------------------------------------ */
1133 /*
1134 * get a suitable seed for a random number generator; should use the
1135 * system clock or some other source of an unpredictable and changing
1136 * seed value
1137 */
1138 void os_rand(long *val);
1139
1140
1141 /* ------------------------------------------------------------------------ */
1142 /*
1143 * Display routines.
1144 *
1145 * Our display model is a simple stdio-style character stream.
1146 *
1147 * In addition, we provide an optional "status line," which is a
1148 * non-scrolling area where a line of text can be displayed. If the
1149 * status line is supported, text should only be displayed in this area
1150 * when os_status() is used to enter status-line mode; while in status
1151 * line mode, text is written to the status line area, otherwise it's
1152 * written to the normal scrolling area. The status line is normally
1153 * shown in a different color to set it off from the rest of the text.
1154 *
1155 * The OS layer can provide its own formatting (word wrapping in
1156 * particular) if it wants, in which case it should also provide
1157 * pagination using os_more_prompt().
1158 */
1159
1160 /* redraw the screen */
1161 void os_redraw(void)
1162 {
1163 debugger("os_redraw() not implemented");
1164 }
1165
1166 /*
1167 * Show the system-specific MORE prompt, and wait for the user to
1168 * respond. Before returning, remove the MORE prompt from the screen.
1169 *
1170 * This routine is only used and only needs to be implemented when the
1171 * OS layer takes responsibility for pagination; this will be the case
1172 * on most systems that use proportional fonts or variable-sized
1173 * windows, since on such platforms the OS layer must do most of the
1174 * formatting work, leaving the standard output layer unable to guess
1175 * where pagination should occur.
1176 *
1177 * If the portable output formatter handles the MORE prompt, which is
1178 * the usual case for character-mode or terminal-style implementations,
1179 * this routine is not used and you don't need to provide an
1180 * implementation. Note that HTML TADS provides an implementation of
1181 * this routine, because the HTML renderer handles line breaking and
1182 * thus must handle pagination.
1183 */
1184 void os_more_prompt();
1185
1186
1187 /*
1188 * Enter HTML mode. This is only used when the run-time is compiled
1189 * with the USE_HTML flag defined. This call instructs the renderer
1190 * that HTML sequences should be parsed; until this call is made, the
1191 * renderer should not interpret output as HTML. Non-HTML
1192 * implementations do not need to define this routine, since the
1193 * run-time will not call it if USE_HTML is not defined.
1194 */
1195 void os_start_html(void);
1196
1197 /* exit HTML mode */
1198 void os_end_html(void);
1199
1200 /*
1201 * Global variables with the height and width of the main text display
1202 * area into which os_print displays. The height and width are given
1203 * in text lines and character columns, respectively. The portable code
1204 * can use these values to format text for display via os_print(); for
1205 * example, the caller can use the width to determine where to put line
1206 * breaks.
1207 *
1208 * These values are only needed for systems where os_print() doesn't
1209 * perform its own word-wrap formatting. On systems such as the Mac,
1210 * where os_print() performs word wrapping, these sizes aren't really
1211 * important because the portable code doesn't need to perform any real
1212 * formatting.
1213 */
1214 extern int G_os_pagelength;
1215 extern int G_os_linewidth;
1216
1217 /*
1218 * Global flag that tells the output formatter whether to count lines
1219 * that it's displaying against the total on the screen so far. If this
1220 * variable is true, lines are counted, and the screen is paused with a
1221 * [More] message when it's full. When not in MORE mode, lines aren't
1222 * counted. This variable should be set to false when displaying text
1223 * that doesn't count against the current page, such as status line
1224 * information.
1225 *
1226 * This flag should not be modified by OS code. Instead, the output
1227 * formatter will set this flag according to its current state; the OS
1228 * code can use this flag to determine whether or not to display a MORE
1229 * prompt during os_print()-type operations. Note that this flag is
1230 * normally interesting to the OS code only when the OS code itself is
1231 * handling the MORE prompt.
1232 */
1233 extern int G_os_moremode;
1234
1235 /*
1236 * Update progress display with current info, if appropriate. This can
1237 * be used to provide a status display during compilation. Most
1238 * command-line implementations will just ignore this notification; this
1239 * can be used for GUI compiler implementations to provide regular
1240 * display updates during compilation to show the progress so far.
1241 */
1242 /* void os_progress(const char *fname, unsigned long linenum); */
1243
1244 /*
1245 * Set busy cursor. If 'flag' is true, provide a visual representation
1246 * that the system or application is busy doing work. If 'flag' is
1247 * false, remove any visual "busy" indication and show normal status.
1248 *
1249 * We provide a prototype here if your osxxx.h header file does not
1250 * #define a macro for os_csr_busy. On many systems, this function has
1251 * no effect at all, so the osxxx.h header file simply #define's it to
1252 * do an empty macro.
1253 */
1254 #ifndef os_csr_busy
1255 void os_csr_busy(int flag)
1256 {
1257 // do nothing under BeOS
1258 }
1259 #endif
1260
1261
1262 /* ------------------------------------------------------------------------ */
1263 /*
1264 * User Input Routines
1265 */
1266
1267 /*
1268 * Ask the user for a filename, using a system-dependent dialog or other
1269 * mechanism. Returns one of the OS_AFE_xxx status codes (see below).
1270 *
1271 * prompt_type is the type of prompt to provide -- this is one of the
1272 * OS_AFP_xxx codes (see below). The OS implementation doesn't need to
1273 * pay any attention to this parameter, but it can be used if desired to
1274 * determine the type of dialog to present if the system provides
1275 * different types of dialogs for different types of operations.
1276 *
1277 * file_type is one of the OSFTxxx codes for system file type. The OS
1278 * implementation is free to ignore this information, but can use it to
1279 * filter the list of files displayed if desired; this can also be used
1280 * to apply a default suffix on systems that use suffixes to indicate
1281 * file type. If OSFTUNK is specified, it means that no filtering
1282 * should be performed, and no default suffix should be applied.
1283 */
1284 int os_askfile(const char *prompt, char *fname_buf, int fname_buf_len,
1285 int prompt_type, os_filetype_t file_type)
1286 {
1287 os_printz(prompt);
1288 os_printz("> ");
1289 os_gets((unsigned char*) fname_buf, fname_buf_len);
1290 return OS_AFE_SUCCESS;
1291 }
1292
1293 /* ------------------------------------------------------------------------ */
1294 /*
1295 * Ask for input through a dialog.
1296 *
1297 * 'prompt' is a text string to display as a prompting message. For
1298 * graphical systems, this message should be displayed in the dialog;
1299 * for text systems, this should be displayed on the terminal after a
1300 * newline.
1301 *
1302 * 'standard_button_set' is one of the OS_INDLG_xxx values defined
1303 * below, or zero. If this value is zero, no standard button set is to
1304 * be used; the custom set of buttons defined in 'buttons' is to be used
1305 * instead. If this value is non-zero, the appropriate set of standard
1306 * buttons, with labels translated to the local language if possible, is
1307 * to be used.
1308 *
1309 * 'buttons' is an array of strings to use as button labels.
1310 * 'button_count' gives the number of entries in the 'buttons' array.
1311 * 'buttons' and 'button_count' are ignored in 'standard_button_set' is
1312 * non-zero, since a standard set of buttons is used instead. If
1313 * 'buttons' and 'button_count' are to be used, each entry contains the
1314 * label of a button to show.
1315 */
1316 /*
1317 * An ampersand ('&') character in a label string indicates that the
1318 * next character after the '&' is to be used as the short-cut key for
1319 * the button, if supported. The '&' should NOT be displayed in the
1320 * string; instead, the character should be highlighted according to
1321 * local system conventions. On Windows, for example, the short-cut
1322 * character should be shown underlined; on a text display, the response
1323 * might be shown with the short-cut character enclosed in parentheses.
1324 * If there is no local convention for displaying a short-cut character,
1325 * then the '&' should simply be removed from the displayed text.
1326 *
1327 * The short-cut key specified by each '&' character should be used in
1328 * processing responses. If the user presses the key corresponding to a
1329 * button's short-cut, the effect should be the same as if the user
1330 * clicked the button with the mouse. If local system conventions don't
1331 * allow for short-cut keys, any short-cut keys can be ignored.
1332 *
1333 * 'default_index' is the 1-based index of the button to use as the
1334 * default. If this value is zero, there is no default response. If
1335 * the user performs the appropriate system-specific action to select
1336 * the default response for the dialog, this is the response that is to
1337 * be selected. On Windows, for example, pressing the "Return" key
1338 * should select this item.
1339 *
1340 * 'cancel_index' is the 1-based index of the button to use as the
1341 * cancel response. If this value is zero, there is no cancel response.
1342 * This is the response to be used if the user cancels the dialog using
1343 * the appropriate system-specific action. On Windows, for example,
1344 * pressing the "Escape" key should select this item.
1345 */
1346 /*
1347 * icon_id is one of the OS_INDLG_ICON_xxx values defined below. If
1348 * possible, an appropriate icon should be displayed in the dialog.
1349 * This can be ignored in text mode, and also in GUI mode if there is no
1350 * appropriate system icon.
1351 *
1352 * The return value is the 1-based index of the response selected. If
1353 * an error occurs, return 0.
1354 */
1355 int os_input_dialog(int icon_id, const char *prompt, int standard_button_set,
1356 const char **buttons, int button_count,
1357 int default_index, int cancel_index);
1358
1359 /*
1360 * Standard button set ID's
1361 */
1362
1363 /* OK */
1364 #define OS_INDLG_OK 1
1365
1366 /* OK, Cancel */
1367 #define OS_INDLG_OKCANCEL 2
1368
1369 /* Yes, No */
1370 #define OS_INDLG_YESNO 3
1371
1372 /* Yes, No, Cancel */
1373 #define OS_INDLG_YESNOCANCEL 4
1374
1375 /*
1376 * Dialog icons
1377 */
1378
1379 /* no icon */
1380 #define OS_INDLG_ICON_NONE 0
1381
1382 /* warning */
1383 #define OS_INDLG_ICON_WARNING 1
1384
1385 /* information */
1386 #define OS_INDLG_ICON_INFO 2
1387
1388 /* question */
1389 #define OS_INDLG_ICON_QUESTION 3
1390
1391 /* error */
1392 #define OS_INDLG_ICON_ERROR 4
1393
1394
1395
1396 /* ------------------------------------------------------------------------ */
1397 /*
1398 * Get the current system high-precision timer. This function returns a
1399 * value giving the wall-clock ("real") time in milliseconds, relative
1400 * to any arbitrary zero point. It doesn't matter what this value is
1401 * relative to -- the only important thing is that the values returned
1402 * by two different calls should differ by the number of actual
1403 * milliseconds that have elapsed between the two calls. On most
1404 * single-user systems, for example, this will probably return the
1405 * number of milliseconds since the user turned on the computer.
1406 *
1407 * True millisecond precision is NOT required. Each implementation
1408 * should simply use the best precision available on the system. If
1409 * your system doesn't have any kind of high-precision clock, you can
1410 * simply use the time() function and multiply the result by 1000 (but
1411 * see the note below about exceeding 32-bit precision).
1412 *
1413 * However, it *is* required that the return value be in *units* of
1414 * milliseconds, even if your system clock doesn't have that much
1415 * precision; so on a system that uses its own internal clock units,
1416 * this routine must multiply the clock units by the appropriate factor
1417 * to yield milliseconds for the return value.
1418 *
1419 * It is also required that the values returned by this function be
1420 * monotonically increasing. In other words, each subsequent call must
1421 * return a value that is equal to or greater than the value returned
1422 * from the last call. On some systems, you must be careful of two
1423 * special situations.
1424 *
1425 * First, the system clock may "roll over" to zero at some point; for
1426 * example, on some systems, the internal clock is reset to zero at
1427 * midnight every night. If this happens, you should make sure that you
1428 * apply a bias after a roll-over to make sure that the value returned
1429 * from this return continues to increase despite the reset of the
1430 * system clock.
1431 *
1432 * Second, a 32-bit signed number can only hold about twenty-three days
1433 * worth of milliseconds. While it seems unlikely that a TADS game
1434 * would run for 23 days without a break, it's certainly reasonable to
1435 * expect that the computer itself may run this long without being
1436 * rebooted. So, if your system uses some large type (a 64-bit number,
1437 * for example) for its high-precision timer, you may want to store a
1438 * zero point the very first time this function is called, and then
1439 * always subtract this zero point from the large value returned by the
1440 * system clock. If you're using time(0)*1000, you should use this
1441 * technique, since the result of time(0)*1000 will almost certainly not
1442 * fit in 32 bits in most cases.
1443 */
1444 long os_get_sys_clock_ms(void)
1445 {
1446 return system_time() / 1000;
1447 }
1448
1449 /*
1450 * Sleep for a while. This should simply pause for the given number of
1451 * milliseconds, then return. On multi-tasking systems, this should use
1452 * a system API to unschedule the current process for the desired delay;
1453 * on single-tasking systems, this can simply sit in a wait loop until
1454 * the desired interval has elapsed.
1455 */
1456 void os_sleep_ms(long delay_in_milliseconds)
1457 {
1458 bigtime_t target = system_time() + 1000LL * delay_in_milliseconds;
1459 while (system_time() < target) snooze(target - system_time());
1460 }
1461
1462 /* set a file's type information */
1463 void os_settype(const char *f, os_filetype_t typ);
1464
1465 /* open the error message file for reading */
1466 osfildef *oserrop(const char *arg0);
1467
1468 /* ------------------------------------------------------------------------ */
1469 /*
1470 * OS main entrypoint
1471 */
1472 int os0main(int oargc, char **oargv,
1473 int (*mainfn)(int, char **, char *),
1474 const char *before, const char *config);
1475
1476 /*
1477 * new-style OS main entrypoint - takes an application container context
1478 */
1479 int os0main2(int oargc, char **oargv,
1480 int (*mainfn)(int, char **, struct appctxdef *, char *),
1481 const char *before, const char *config,
1482 struct appctxdef *appctx);
1483
1484 /*
1485 * get filename from startup parameter, if possible; returns true and
1486 * fills in the buffer with the parameter filename on success, false if
1487 * no parameter file could be found
1488 */
1489 int os_paramfile(char */*buf*/)
1490 {
1491 return FALSE;
1492 }
1493
1494 /*
1495 * Initialize. This should be called during program startup to
1496 * initialize the OS layer and check OS-specific command-line arguments.
1497 *
1498 * If 'prompt' and 'buf' are non-null, and there are no arguments on the
1499 * given command line, the OS code can use the prompt to ask the user to
1500 * supply a filename, then store the filename in 'buf' and set up
1501 * argc/argv to give a one-argument command string. (This mechanism for
1502 * prompting for a filename is obsolescent, and is retained for
1503 * compatibility with a small number of existing implementations only;
1504 * new implementations should ignore this mechanism and leave the
1505 * argc/argv values unchanged.)
1506 */
1507 int os_init(int */*argc*/, char **/*argv*/, const char */*prompt*/, char */*buf*/, int /*bufsiz*/)
1508 {
1509 // this doesn't actually *do* anything except remember the original cbreak mode so
1510 // that we're guaranteed to properly restore it in os_uninit()
1511 set_cbreak_mode(1);
1512 set_cbreak_mode(0);
1513
1514 // nothing else to do here on BeOS
1515 return 0;
1516 }
1517
1518
1519 /*
1520 * Uninitialize. This is called prior to progam termination to reverse
1521 * the effect of any changes made in os_init(). For example, if
1522 * os_init() put the terminal in raw mode, this should restore the
1523 * previous terminal mode. This routine should not terminate the
1524 * program (so don't call exit() here) - the caller might have more
1525 * processing to perform after this routine returns.
1526 */
1527 void os_uninit(void)
1528 {
1529 set_cbreak_mode(0);
1530 }
1531
1532 /*
1533 * Terminate. This should exit the program with the given exit status.
1534 * In general, this should be equivalent to the standard C library
1535 * exit() function, but we define this interface to allow the OS code to
1536 * do any necessary pre-termination cleanup.
1537 */
1538 void os_term(int status)
1539 {
1540 exit(status);
1541 }
1542
1543 /*
1544 * Install/uninstall the break handler. If possible, the OS code should
1545 * set (if 'install' is true) or clear (if 'install' is false) a signal
1546 * handler for keyboard break signals (control-C, etc, depending on
1547 * local convention). The OS code should set its own handler routine,
1548 * which should note that a break occurred with an internal flag; the
1549 * portable code uses os_break() from time to time to poll this flag.
1550 */
1551 void os_instbrk(int /*install*/)
1552 {
1553 }
1554
1555 /*
1556 * Yield CPU; returns TRUE if user requested interrupt, FALSE to
1557 * continue. Normally, this doesn't do anything on DOS.
1558 */
1559 #ifndef os_yield
1560 int os_yield(void);
1561 #endif
1562
1563 /*
1564 * Initialize the time zone. This routine is meant to take care of any
1565 * work that needs to be done prior to calling localtime() and other
1566 * time-zone-dependent routines in the run-time library. For DOS and
1567 * Windows, we need to call the run-time library routine tzset() to set
1568 * up the time zone from the environment; most systems shouldn't need to
1569 * do anything in this routine.
1570 */
1571 #ifndef os_tzset
1572 void os_tzset(void);
1573 #endif
1574
1575 /*
1576 * Set the default saved-game extension. This routine will NOT be
1577 * called when we're using the standard saved game extension; this
1578 * routine will be invoked only if we're running as a stand-alone game,
1579 * and the game author specified a non-standard saved-game extension
1580 * when creating the stand-alone game.
1581 *
1582 * This routine is not required if the system does not use the standard,
1583 * semi-portable os0.c implementation. Even if the system uses the
1584 * standard os0.c implementation, it can provide an empty routine here
1585 * if the system code doesn't need to do anything special with this
1586 * information.
1587 *
1588 * The extension is specified as a null-terminated string. The
1589 * extension does NOT include the leading period.
1590 */
1591 void os_set_save_ext(const char */*ext*/)
1592 {
1593 // ignore
1594 }
1595
1596 /*
1597 * Generate a filename for a character-set mapping file. This function
1598 * should determine the current native character set in use, if
1599 * possible, then generate a filename, according to system-specific
1600 * conventions, that we should attempt to load to get a mapping between
1601 * the current native character set and the internal character set
1602 * identified by 'internal_id'.
1603 *
1604 * The internal character set ID is a string of up to 4 characters.
1605 *
1606 * On DOS, the native character set is a DOS code page. DOS code pages
1607 * are identified by 3- or 4-digit identifiers; for example, code page
1608 * 437 is the default US ASCII DOS code page. We generate the
1609 * character-set mapping filename by appending the internal character
1610 * set identifier to the DOS code page number, then appending ".TCP" to
1611 * the result. So, to map between ISO Latin-1 (internal ID = "La1") and
1612 * DOS code page 437, we would generate the filename "437La1.TCP".
1613 *
1614 * Note that this function should do only two things. First, determine
1615 * the current native character set that's in use. Second, generate a
1616 * filename based on the current native code page and the internal ID.
1617 * This function is NOT responsible for figuring out the mapping or
1618 * anything like that -- it's simply where we generate the correct
1619 * filename based on local convention.
1620 *
1621 * 'filename' is a buffer of at least OSFNMAX characters.
1622 *
1623 * 'argv0' is the executable filename from the original command line.
1624 * This parameter is provided so that the system code can look for
1625 * mapping files in the original TADS executables directory, if desired.
1626 */
1627 void os_gen_charmap_filename(char */*filename*/, char */*internal_id*/, char */*argv0*/)
1628 {
1629 debugger("os_gen_charmap_filename() not implemented");
1630 }
1631
1632 /*
1633 * Receive notification that a character mapping file has been loaded.
1634 * The caller doesn't require this routine to do anything at all; this
1635 * is purely for the system-dependent code's use so that it can take
1636 * care of any initialization that it must do after the caller has
1637 * loaded a charater mapping file. 'id' is the character set ID, and
1638 * 'ldesc' is the display name of the character set. 'sysinfo' is the
1639 * extra system information string that is stored in the mapping file;
1640 * the interpretation of this information is up to this routine.
1641 *
1642 * For reference, the Windows version uses the extra information as a
1643 * code page identifier, and chooses its default font character set to
1644 * match the code page. On DOS, the run-time requires the player to
1645 * activate an appropriate code page using a DOS command (MODE CON CP
1646 * SELECT) prior to starting the run-time, so this routine doesn't do
1647 * anything at all on DOS.
1648 */
1649 void os_advise_load_charmap(char */*id*/, char */*ldesc*/, char */*sysinfo*/)
1650 {
1651 // do nothing with charmaps under BeOS
1652 }
1653
1654 /*
1655 * Generate the name of the character set mapping table for Unicode
1656 * characters to and from the given local character set. Fills in the
1657 * buffer with the implementation-dependent name of the desired
1658 * character set map. See below for the character set ID codes.
1659 *
1660 * For example, on Windows, the implementation would obtain the
1661 * appropriate active code page (which is simply a Windows character set
1662 * identifier number) from the operating system, and build the name of
1663 * the Unicode mapping file for that code page, such as "CP1252". On
1664 * Macintosh, the implementation would look up the current script system
1665 * and return the name of the Unicode mapping for that script system,
1666 * such as "ROMAN" or "CENTEURO".
1667 *
1668 * If it is not possible to determine the specific character set that is
1669 * in use, this function should return "asc7dflt" (ASCII 7-bit default)
1670 * as the character set identifier on an ASCII system, or an appropriate
1671 * base character set name on a non-ASCII system. "asc7dflt" is the
1672 * generic character set mapping for plain ASCII characters.
1673 *
1674 * The given buffer must be at least 32 bytes long; the implementation
1675 * must limit the result it stores to 32 bytes. (We use a fixed-size
1676 * buffer in this interface for simplicity, and because there seems no
1677 * need for greater flexibility in the interface; a character set name
1678 * doesn't carry very much information so shouldn't need to be very
1679 * long. Note that this function doesn't generate a filename, but
1680 * simply a mapping name; in practice, a map name will be used to
1681 * construct a mapping file name.)
1682 *
1683 * Because this function obtains the Unicode mapping name, there is no
1684 * need to specify the internal character set to be used: the internal
1685 * character set is Unicode.
1686 */
1687 /*
1688 * Implementation note: when porting this routine, the convention that
1689 * you use to name your mapping files is up to you. You should simply
1690 * choose a convention for this implementation, and then use the same
1691 * convention for packaging the mapping files for your OS release. In
1692 * most cases, the best convention is to use the names that the Unicode
1693 * consortium uses in their published cross-mapping listings, since
1694 * these listings can be used as the basis of the mapping files that you
1695 * include with your release. For example, on Windows, the convention
1696 * is to use the code page number to construct the map name, as in
1697 * CP1252 or CP1250.
1698 */
1699 void os_get_charmap(char *mapname, int /*charmap_id*/)
1700 {
1701 // BeOS always uses native UTF-8, which makes TADS very happy.
1702 // "utf8" is a special name to TADS3, indicating that the runtime should
1703 // just trust the OS to Do The Right Thing (tm) with raw UTF-8 strings.
1704 strcpy(mapname, "utf8");
1705 }
1706
1707 /*
1708 * Get system information. 'code' is a SYSINFO_xxx code, which
1709 * specifies what type of information to get. The 'param' argument's
1710 * meaning depends on which code is selected. 'result' is a pointer to
1711 * an integer that is to be filled in with the result value. If the
1712 * code is not known, this function should return FALSE. If the code is
1713 * known, the function should fill in *result and return TRUE.
1714 */
1715 int os_get_sysinfo(int code, void */*param*/, long *result)
1716 {
1717 switch(code)
1718 {
1719 case SYSINFO_HTML:
1720 case SYSINFO_JPEG:
1721 case SYSINFO_PNG:
1722 case SYSINFO_WAV:
1723 case SYSINFO_MIDI:
1724 case SYSINFO_WAV_MIDI_OVL:
1725 case SYSINFO_WAV_OVL:
1726 case SYSINFO_PREF_IMAGES:
1727 case SYSINFO_PREF_SOUNDS:
1728 case SYSINFO_PREF_MUSIC:
1729 case SYSINFO_PREF_LINKS:
1730 case SYSINFO_MPEG:
1731 case SYSINFO_MPEG1:
1732 case SYSINFO_MPEG2:
1733 case SYSINFO_MPEG3:
1734 case SYSINFO_LINKS_HTTP:
1735 case SYSINFO_LINKS_FTP:
1736 case SYSINFO_LINKS_NEWS:
1737 case SYSINFO_LINKS_MAILTO:
1738 case SYSINFO_LINKS_TELNET:
1739 case SYSINFO_PNG_TRANS:
1740 case SYSINFO_PNG_ALPHA:
1741 case SYSINFO_OGG:
1742 case SYSINFO_MNG:
1743 case SYSINFO_MNG_TRANS:
1744 case SYSINFO_MNG_ALPHA:
1745 case SYSINFO_TEXT_HILITE:
1746 case SYSINFO_TEXT_COLORS:
1747 case SYSINFO_BANNERS:
1748 /*
1749 * we don't support any of these features - set the result to 0
1750 * to indicate this
1751 */
1752 *result = 0;
1753
1754 /* return true to indicate that we recognized the code */
1755 return TRUE;
1756
1757 case SYSINFO_INTERP_CLASS:
1758 /* we're a text-only interpreter in text-only mode */
1759 *result = SYSINFO_ICLASS_TEXT;
1760 return TRUE;
1761
1762 default:
1763 /* we don't recognize other codes */
1764 return FALSE;
1765 }
1766 }
1767
1768 /* ------------------------------------------------------------------------ */
1769 /*
1770 * Integer division operators. For any compiler that follows ANSI C
1771 * rules, no definitions are required for these routine, since the
1772 * standard definitions below will work properly. However, if your
1773 * compiler does not follow ANSI standards with respect to integer
1774 * division of negative numbers, you must provide implementations of
1775 * these routines that produce the correct results.
1776 *
1777 * Division must "truncate towards zero," which means that any
1778 * fractional part is dropped from the result. If the result is
1779 * positive, the result must be the largest integer less than the
1780 * algebraic result: 11/3 yields 3. If the result is negative, the
1781 * result must be the smallest integer less than the result: (-11)/3
1782 * yields -3.
1783 *
1784 * The remainder must obey the relationship (a/b)*b + a%b == a, for any
1785 * integers a and b (b != 0).
1786 *
1787 * If your compiler does not obey the ANSI rules for the division
1788 * operators, make the following changes in your osxxx.h file
1789 *
1790 * - define the symbol OS_NON_ANSI_DIVIDE in the osxxx.h file
1791 *
1792 * - either define your own macros for os_divide_long() and
1793 * os_remainder_long(), or put actual prototypes for these functions
1794 * into your osxxx.h file and write appropriate implementations of these
1795 * functions in one of your osxxx.c or .cpp files.
1796 */
1797 /* long os_divide_long(long a, long b); // returns (a/b) with ANSI rules */
1798 /* long os_remainder_long(long a, long b); // returns (a%b) with ANSI rules */
1799
1800 /* standard definitions for any ANSI compiler */
1801 #ifndef OS_NON_ANSI_DIVIDE
1802 #define os_divide_long(a, b) ((a) / (b))
1803 #define os_remainder_long(a, b) ((a) % (b))
1804 #endif
1805
1806 /* ------------------------------------------------------------------------ */
1807 /*
1808 * TADS 2 swapping configuration. Define OS_DEFAULT_SWAP_ENABLED to 0
1809 * if you want swapping turned off, 1 if you want it turned on. If we
1810 * haven't defined a default swapping mode yet, turn swapping on by
1811 * default.
1812 */
1813 #ifndef OS_DEFAULT_SWAP_ENABLED
1814 # define OS_DEFAULT_SWAP_ENABLED 1
1815 #endif
1816
1817 /*
1818 * If the system "long description" (for the banner) isn't defined, make
1819 * it the same as the platform ID string.
1820 */
1821 #ifndef OS_SYSTEM_LDESC
1822 # define OS_SYSTEM_LDESC OS_SYSTEM_NAME
1823 #endif
1824
1825 /*
1826 * TADS 2 Usage Lines
1827 *
1828 * If the "usage" lines (i.e., the descriptive lines of text describing
1829 * how to run the various programs) haven't been otherwise defined,
1830 * define defaults for them here. Some platforms use names other than
1831 * tc, tr, and tdb for the tools (for example, on Unix they're usually
1832 * tadsc, tadsr, and tadsdb), so the usage lines should be adjusted
1833 * accordingly; simply define them earlier than this point (in this file
1834 * or in one of the files included by this file, such as osunixt.h) for
1835 * non-default text.
1836 */
1837 #ifndef OS_TC_USAGE
1838 # define OS_TC_USAGE "usage: tc [options] file"
1839 #endif
1840 #ifndef OS_TR_USAGE
1841 # define OS_TR_USAGE "usage: tr [options] file"
1842 #endif
1843 #ifndef OS_TDB_USAGE
1844 # define OS_TDB_USAGE "usage: tdb [options] file"
1845 #endif
1846
1847 /*
1848 * Ports can define a special TDB startup message, which is displayed
1849 * after the copyright/version banner. If it's not defined at this
1850 * point, define it to an empty string.
1851 */
1852 #ifndef OS_TDB_STARTUP_MSG
1853 # define OS_TDB_STARTUP_MSG ""
1854 #endif
1855
1856 /*
1857 * If a system patch sub-level isn't defined, define it here as zero.
1858 * The patch sub-level is used on some systems where a number of ports
1859 * are derived from a base port (for example, a large number of ports
1860 * are based on the generic Unix port). For platforms like the Mac,
1861 * where the porting work applies only to that one platform, this
1862 * sub-level isn't meaningful.
1863 */
1864 #ifndef OS_SYSTEM_PATCHSUBLVL
1865 # define OS_SYSTEM_PATCHSUBLVL "0"
1866 #endif
1867
1868 /*
1869 * Provide memicmp since it's not a standard C or POSIX function.
1870 */
1871 int memicmp(const char *s1, const char *s2, size_t len)
1872 {
1873 char *x1, *x2;
1874 size_t i;
1875
1876 x1 = new char[len];
1877 x2 = new char[len];
1878
1879 for (i = 0; i < len; i++) {
1880 x1[i] = tolower(s1[i]);
1881 x2[i] = tolower(s2[i]);
1882 }
1883
1884 int result = memcmp(x1, x2, len);
1885 delete [] x1;
1886 delete [] x2;
1887 return result;
1888 }
1889
1890 // BeOS R5 doesn't provide the proper wcs*() functions, so we do here
1891 wchar_t* wcscpy(wchar_t* dest, const wchar_t* src)
1892 {
1893 wchar_t* orig_dest = dest;
1894 while (*src)
1895 {
1896 *dest++ = *src++;
1897 }
1898 *dest = 0;
1899 return orig_dest;
1900 }
1901
1902 size_t wcslen(const wchar_t* str)
1903 {
1904 size_t len = 0;
1905 while (*str)
1906 {
1907 len++;
1908 str++;
1909 }
1910 return len;
1911 }
1912
1913 // This wcstol() doesn't set the error conditions according to the C++ standard
1914 long wcstol(const wchar_t* str, wchar_t** endptr, int base)
1915 {
1916 long result = 0;
1917 while (*str && (*str >= '0') && (*str - '0' < base))
1918 {
1919 result = result*base + *str - '0';
1920 str++;
1921 }
1922 if (endptr)
1923 {
1924 *endptr = (wchar_t*) str;
1925 }
1926 return result;
1927 }
1928
1929 #ifdef __cplusplus
1930 }
1931 #endif