Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | \ ========== Copyright Header Begin ========================================== |
2 | \ | |
3 | \ Hypervisor Software File: sysdisk.fth | |
4 | \ | |
5 | \ Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. | |
6 | \ | |
7 | \ - Do no alter or remove copyright notices | |
8 | \ | |
9 | \ - Redistribution and use of this software in source and binary forms, with | |
10 | \ or without modification, are permitted provided that the following | |
11 | \ conditions are met: | |
12 | \ | |
13 | \ - Redistribution of source code must retain the above copyright notice, | |
14 | \ this list of conditions and the following disclaimer. | |
15 | \ | |
16 | \ - Redistribution in binary form must reproduce the above copyright notice, | |
17 | \ this list of conditions and the following disclaimer in the | |
18 | \ documentation and/or other materials provided with the distribution. | |
19 | \ | |
20 | \ Neither the name of Sun Microsystems, Inc. or the names of contributors | |
21 | \ may be used to endorse or promote products derived from this software | |
22 | \ without specific prior written permission. | |
23 | \ | |
24 | \ This software is provided "AS IS," without a warranty of any kind. | |
25 | \ ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, | |
26 | \ INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A | |
27 | \ PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN | |
28 | \ MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR | |
29 | \ ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR | |
30 | \ DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN | |
31 | \ OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR | |
32 | \ FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE | |
33 | \ DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, | |
34 | \ ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF | |
35 | \ SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. | |
36 | \ | |
37 | \ You acknowledge that this software is not designed, licensed or | |
38 | \ intended for use in the design, construction, operation or maintenance of | |
39 | \ any nuclear facility. | |
40 | \ | |
41 | \ ========== Copyright Header End ============================================ | |
42 | \ @(#)sysdisk.fth 2.11 01/05/18 | |
43 | \ Copyright 1985-1994 Bradley Forthware | |
44 | \ Copyright 1994-2001 Sun Microsystems, Inc. All Rights Reserved | |
45 | ||
46 | \ File I/O interface using the C wrapper program | |
47 | headerless | |
48 | decimal | |
49 | ||
50 | \ Closes an open file, freeing its descriptor for reuse. | |
51 | : _fclose ( file# -- ) | |
52 | bfbase @ bflimit @ over - free-mem \ Hack! Hack! | |
53 | 4 syscall drop | |
54 | ; | |
55 | ||
56 | \ Writes "count" bytes from the buffer at address "adr" to a file. | |
57 | \ Returns the number of bytes actually written. | |
58 | ||
59 | : _fwrite ( adr #bytes file# -- #written ) | |
60 | >r swap r> 6 syscall 3drop retval error? if drop 0 then ( #written) | |
61 | ; | |
62 | ||
63 | \ Reads at most "count" bytes into the buffer at address "adr" from a file. | |
64 | \ Returns the number of bytes actually read. | |
65 | ||
66 | : _fread ( adr #bytes file# -- #read ) | |
67 | >r swap r> 5 syscall 3drop retval error? abort" _fread failed" ( #read ) | |
68 | ; | |
69 | ||
70 | \ Used by _fseek, _ftell, and _fsize | |
71 | ||
72 | : _lseek ( whence l.byte# file# -- l.byte# error? ) | |
73 | 10 syscall drop drop drop retval error? | |
74 | ; | |
75 | ||
76 | \ Positions to byte number "l.byte#" in a file | |
77 | ||
78 | : _fseek ( l.byte# file# -- ) | |
79 | 0 -rot _lseek abort" _fseek failed" drop | |
80 | ; | |
81 | : _dfseek ( d.byte# file# -- ) | |
82 | swap abort" _dfseek argument too large" _fseek | |
83 | ; | |
84 | ||
85 | \ Returns the current position "l.current-position" within a file | |
86 | ||
87 | : _ftell ( file# -- l.byte# ) 1 0 rot _lseek abort" _ftell failed" ; | |
88 | : _dftell ( file# -- d.byte# ) _ftell 0 ; | |
89 | ||
90 | \ Returns the current size "l.size" of a file | |
91 | ||
92 | : _fsize ( file# -- l.size ) | |
93 | \ remember the current position | |
94 | >r r@ _ftell ( l.position ) | |
95 | ||
96 | \ seek to end of file to find out where the eof is | |
97 | 2 0 r@ _lseek abort" _fsize failed" ( l.pos l.size ) | |
98 | ||
99 | \ return to the original position | |
100 | swap r> _fseek ( l.size ) | |
101 | ; | |
102 | : _dfsize ( file# -- d.size ) _fsize 0 ; | |
103 | ||
104 | \ Protection to be assigned to newly-created files | |
105 | \ Defaults to public read permission, owner and group write permission. | |
106 | ||
107 | variable file-protection | |
108 | -1 is file-protection \ Use system default until overridden | |
109 | ||
110 | \ Prepares a file for later access. Name is the pathname of the file | |
111 | \ and mode is the mode (0 read, 1 write, 2 modify). If the operation | |
112 | \ succeeds, returns the addresses of routines to perform I/O on the | |
113 | \ open file and true. If the operation fails, returns false. | |
114 | ||
115 | : sys_fopen | |
116 | ( name mode -- [ fid mode sizeop alignop closeop writeop readop ] okay? ) | |
117 | >r r@ swap cstr file-protection @ -rot ( prot mode name ) | |
118 | over 8 and if ( prot mode name ) | |
119 | nip 3 syscall 2drop retval ( fid ) | |
120 | else ( prot mode name ) | |
121 | 2 syscall 3drop retval ( fid ) | |
122 | then ( fid ) | |
123 | error? if r> drop drop false exit then ( fid ) | |
124 | r@ ['] _dfsize ['] _dfalign ['] _fclose ['] _dfseek | |
125 | r@ read = if ['] nullwrite else ['] _fwrite then | |
126 | r> write = if ['] nullread else ['] _fread then | |
127 | true | |
128 | ; | |
129 | ||
130 | \ Removes the named file from its directory. | |
131 | ||
132 | headers | |
133 | : _delete ( name -- err? ) cstr 11 syscall drop retval ; | |
134 | headerless | |
135 | ||
136 | : sys_newline ( -- adr ) 28 syscall retval ; | |
137 | ||
138 | : install-disk-io ( -- ) | |
139 | ['] sys_newline is newline-pstring | |
140 | ['] sys_fopen is do-fopen | |
141 | ; | |
142 | ||
143 | \ Line terminators for various operating systems | |
144 | create lf-pstr 1 c, linefeed c, \ Unix | |
145 | create cr-pstr 1 c, carret c, \ Macintosh, OS-9 | |
146 | create crlf-pstr 2 c, carret c, linefeed c, \ DOS | |
147 | ||
148 | ||
149 | \ Aligns to a 512-byte boundary for Unix | |
150 | ||
151 | : _falign ( byte# fd -- aligned ) drop h# 1ff invert and ; | |
152 | : _dfalign ( d.byte# fd -- d.aligned ) drop swap h# 1ff invert and swap ; | |
153 | ||
154 | th 1b4 is file-protection \ rw-rw-r-- Unix file protection code | |
155 | ||
156 | chain: unix-init-io | |
157 | install-disk-io | |
158 | ; | |
159 | ||
160 | headers |