| 1 | module scheme |
| 2 | |
| 3 | import os |
| 4 | import reader |
| 5 | |
| 6 | pub enum PortBufferMode as u8 { |
| 7 | block |
| 8 | none |
| 9 | line |
| 10 | } |
| 11 | |
| 12 | // These options belong to the selected open-file-*-port constructors. The |
| 13 | // existing R5RS open-output-file keeps its truncate-or-create behavior. |
| 14 | @[params] |
| 15 | pub struct FilePortOptions { |
| 16 | pub: |
| 17 | no_create bool |
| 18 | no_fail bool |
| 19 | no_truncate bool |
| 20 | buffer_mode PortBufferMode = .block |
| 21 | transcoder Value = Value{ tag: .boolean } |
| 22 | } |
| 23 | |
| 24 | // Created once at process startup, never in a caller's borrowed arena. All |
| 25 | // literals share one immutable enumeration identity and its eight small sets. |
| 26 | const file_option_sets = make_file_option_sets() |
| 27 | const file_buffer_mode_symbols = [text_value(.symbol, 'block'), text_value(.symbol, 'none'), |
| 28 | text_value(.symbol, 'line')] |
| 29 | |
| 30 | fn make_file_option_sets() []Value { |
| 31 | universe := &EnumerationUniverse{ |
| 32 | symbols: [text_value(.symbol, 'no-create'), text_value(.symbol, 'no-fail'), |
| 33 | text_value(.symbol, 'no-truncate')] |
| 34 | indices: { |
| 35 | 'no-create': 0 |
| 36 | 'no-fail': 1 |
| 37 | 'no-truncate': 2 |
| 38 | } |
| 39 | } |
| 40 | full := new_enumeration(universe) |
| 41 | enumeration := full.enumeration_pointer().enumeration |
| 42 | mut result := value_storage(8) |
| 43 | for mask in 0 .. 8 { |
| 44 | container_store_validated(mut result, mask, if mask == 7 { |
| 45 | full |
| 46 | } else { |
| 47 | payload_value(.enumeration, &EnumerationSet{ enumeration: enumeration, low: u64(mask) }) |
| 48 | }) |
| 49 | } |
| 50 | return result |
| 51 | } |
| 52 | |
| 53 | fn file_option_bit(name string) !u8 { |
| 54 | return match name { |
| 55 | 'no-create' { u8(1) } |
| 56 | 'no-fail' { u8(2) } |
| 57 | 'no-truncate' { u8(4) } |
| 58 | else { return failure('unknown file option: ${name}') } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | fn file_option_mask(value Value) !u8 { |
| 63 | set := checked_enumeration(value)! |
| 64 | if voidptr(set.enumeration) == voidptr(file_option_sets[7].enumeration_pointer().enumeration) { |
| 65 | return u8(set.low) |
| 66 | } |
| 67 | // Other enum-set constructors may supply a compatible subset universe. |
| 68 | // Reject unknown universe members even when they are absent from the set. |
| 69 | mut mask := u8(0) |
| 70 | for index, name in set.enumeration.universe.symbols { |
| 71 | bit := file_option_bit(name.text())! |
| 72 | if enumeration_contains(set, index) { mask |= bit } |
| 73 | } |
| 74 | return mask |
| 75 | } |
| 76 | |
| 77 | fn parse_buffer_mode(value Value) !PortBufferMode { |
| 78 | if value.tag != .symbol { return failure('buffer mode must be a symbol') } |
| 79 | return parse_buffer_mode_name(value.text()) |
| 80 | } |
| 81 | |
| 82 | fn parse_buffer_mode_name(name string) !PortBufferMode { |
| 83 | return match name { |
| 84 | 'block' { PortBufferMode.block } |
| 85 | 'none' { PortBufferMode.none } |
| 86 | 'line' { PortBufferMode.line } |
| 87 | else { return failure('unknown buffer mode: ${name}') } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | fn buffer_mode_name(mode PortBufferMode) string { |
| 92 | return match mode { |
| 93 | .block { 'block' } |
| 94 | .none { 'none' } |
| 95 | .line { 'line' } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | fn (mut b Builder) file_options_syntax(nodes []reader.NodeId, origin reader.NodeId) ! { |
| 100 | mut mask := u8(0) |
| 101 | for node in nodes[1..] { |
| 102 | if b.code.node(node)!.kind != .symbol { |
| 103 | return failure('file-options requires literal symbol names') |
| 104 | } |
| 105 | mask |= file_option_bit(b.code.datum_name(node)!)! |
| 106 | } |
| 107 | b.constant(file_option_sets[mask], origin) |
| 108 | } |
| 109 | |
| 110 | const selected_file_port_names = 'file-options buffer-mode buffer-mode? output-port-buffer-mode open-file-input-port open-file-output-port open-file-input/output-port' |
| 111 | |
| 112 | const selected_file_port_descriptors = [ |
| 113 | NativeDescriptor{'buffer-mode?', file_buffer_modep} |
| 114 | NativeDescriptor{'output-port-buffer-mode', file_output_buffer_mode} |
| 115 | NativeDescriptor{'open-file-input-port', file_open_selected_input} |
| 116 | NativeDescriptor{'open-file-output-port', file_open_selected_output} |
| 117 | NativeDescriptor{'open-file-input/output-port', file_open_selected_combined}, |
| 118 | ]! |
| 119 | |
| 120 | fn (mut vm VM) install_selected_file_ports() { |
| 121 | for i in 0 .. selected_file_port_descriptors.len { |
| 122 | entry := &selected_file_port_descriptors[i] |
| 123 | vm.bind_global(entry.name, procedure_value(&Procedure{ mode: .native, owner: vm.owner, name: entry.name, native: entry.callback })) |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | fn file_buffer_modep(mut _vm VM, args []Value) !Value { |
| 128 | check_arity('buffer-mode?', args.len, 1, 1)! |
| 129 | return boolean(args[0].tag == .symbol && args[0].text() in ['block', 'none', 'line']) |
| 130 | } |
| 131 | |
| 132 | fn file_output_buffer_mode(mut vm VM, args []Value) !Value { |
| 133 | check_arity('output-port-buffer-mode', args.len, 1, 1)! |
| 134 | return file_buffer_mode_symbols[int(vm.output_owner(args[0])!.buffer_mode)] |
| 135 | } |
| 136 | |
| 137 | fn file_port_options(args []Value) !FilePortOptions { |
| 138 | mask := if args.len > 1 { file_option_mask(args[1])! } else { u8(0) } |
| 139 | mode := if args.len > 2 { parse_buffer_mode(args[2])! } else { PortBufferMode.block } |
| 140 | transcoder := if args.len > 3 { args[3] } else { boolean(false) } |
| 141 | checked_optional_transcoder(transcoder)! |
| 142 | return FilePortOptions{ no_create: mask & 1 != 0, no_fail: mask & 2 != 0, no_truncate: mask & 4 != 0, buffer_mode: mode, transcoder: transcoder } |
| 143 | } |
| 144 | |
| 145 | fn file_open_selected_input(mut vm VM, args []Value) !Value { |
| 146 | check_arity('open-file-input-port', args.len, 1, 4)! |
| 147 | path := args[0].as_string()! |
| 148 | return vm.open_file_input_port(path, file_port_options(args)!) |
| 149 | } |
| 150 | |
| 151 | fn file_open_selected_output(mut vm VM, args []Value) !Value { |
| 152 | check_arity('open-file-output-port', args.len, 1, 4)! |
| 153 | path := args[0].as_string()! |
| 154 | return vm.open_file_output_port(path, file_port_options(args)!) |
| 155 | } |
| 156 | |
| 157 | fn checked_file_port_options(path string, options FilePortOptions) ! { |
| 158 | if path.contains('\x00') { return failure('file name contains NUL') } |
| 159 | if options.buffer_mode !in [.block, .none, .line] { return failure('invalid port buffer mode') } |
| 160 | checked_optional_transcoder(options.transcoder)! |
| 161 | } |
| 162 | |
| 163 | pub fn (mut vm VM) open_file_input_port(path string, options FilePortOptions) !Value { |
| 164 | vm.ready()! |
| 165 | checked_file_port_options(path, options)! |
| 166 | mut reservation := vm.reserve_file_resource()! |
| 167 | defer { reservation.rollback() } |
| 168 | mut stream := &PortFileStream{ raw: true } |
| 169 | reservation.attach(mut stream) |
| 170 | stream.file = os.open_file(path, 'rb')! |
| 171 | register_port_file(stream) |
| 172 | value := vm.input_port(stream, |
| 173 | binary: true |
| 174 | read_size: if options.buffer_mode == .none { 1 } else { 4096 } |
| 175 | )! |
| 176 | mut port := value.input_pointer() |
| 177 | port.file = &stream.file |
| 178 | port.stream = stream |
| 179 | port.owns_file = true |
| 180 | result := maybe_transcode(mut vm, value, options.transcoder)! |
| 181 | reservation.commit() |
| 182 | return result |
| 183 | } |
| 184 | |
| 185 | pub fn (mut vm VM) open_file_output_port(path string, options FilePortOptions) !Value { |
| 186 | vm.ready()! |
| 187 | checked_file_port_options(path, options)! |
| 188 | mask := u8(if options.no_create { 1 } else { 0 }) | u8(if options.no_fail { 2 } else { 0 }) | u8(if options.no_truncate { |
| 189 | 4 |
| 190 | } else { |
| 191 | 0 |
| 192 | }) |
| 193 | mut reservation := vm.reserve_file_resource()! |
| 194 | defer { reservation.rollback() } |
| 195 | mut stream := &PortFileStream{ raw: true, descriptor_only: true, buffer_mode: options.buffer_mode } |
| 196 | reservation.attach(mut stream) |
| 197 | fd := file_open_output_descriptor(path, mask)! |
| 198 | stream.file = os.File{ fd: fd, is_opened: true } |
| 199 | register_port_file(stream) |
| 200 | value := vm.output_port(stream, binary: true)! |
| 201 | mut port := value.output_pointer() |
| 202 | port.file = &stream.file |
| 203 | port.stream = stream |
| 204 | port.owns_file = true |
| 205 | port.buffer_mode = options.buffer_mode |
| 206 | result := maybe_transcode(mut vm, value, options.transcoder)! |
| 207 | reservation.commit() |
| 208 | return result |
| 209 | } |
| 210 | |