1 /* Native functions for input/output. 2 3 Copyright (C) 2016 Paul Boddie <paul@boddie.org.uk> 4 5 This program is free software; you can redistribute it and/or modify it under 6 the terms of the GNU General Public License as published by the Free Software 7 Foundation; either version 3 of the License, or (at your option) any later 8 version. 9 10 This program is distributed in the hope that it will be useful, but WITHOUT 11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 12 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 13 details. 14 15 You should have received a copy of the GNU General Public License along with 16 this program. If not, see <http://www.gnu.org/licenses/>. 17 */ 18 19 #include <unistd.h> /* read, write */ 20 #include <string.h> /* strcmp, memcpy */ 21 #include <stdio.h> /* fdopen, snprintf */ 22 #include <errno.h> /* errno */ 23 #include "native/common.h" 24 #include "types.h" 25 #include "exceptions.h" 26 #include "ops.h" 27 #include "progconsts.h" 28 #include "progops.h" 29 #include "progtypes.h" 30 #include "main.h" 31 32 /* Input/output. */ 33 34 __attr __fn_native_io_fclose(__attr __args[]) 35 { 36 __attr * const fp = &__args[1]; 37 /* fp interpreted as FILE reference */ 38 FILE *f = (FILE *) fp->datavalue; 39 40 errno = 0; 41 if (fclose(f)) 42 __raise_io_error(__new_int(errno)); 43 44 return __builtins___none_None; 45 } 46 47 __attr __fn_native_io_fflush(__attr __args[]) 48 { 49 __attr * const fp = &__args[1]; 50 /* fp interpreted as FILE reference */ 51 FILE *f = (FILE *) fp->datavalue; 52 53 errno = 0; 54 if (fflush(f)) 55 __raise_io_error(__new_int(errno)); 56 57 return __builtins___none_None; 58 } 59 60 __attr __fn_native_io_fopen(__attr __args[]) 61 { 62 __attr * const filename = &__args[1]; 63 __attr * const mode = &__args[2]; 64 /* filename.__data__ interpreted as string */ 65 char *fn = __load_via_object(filename->value, __pos___data__).strvalue; 66 /* mode.__data__ interpreted as string */ 67 char *s = __load_via_object(mode->value, __pos___data__).strvalue; 68 FILE *f; 69 __attr attr; 70 71 errno = 0; 72 f = fopen(fn, s); 73 74 /* Produce an exception if the operation failed. */ 75 76 if (f == NULL) 77 __raise_io_error(__new_int(errno)); 78 79 /* Return the __data__ attribute. */ 80 81 else 82 { 83 attr.context = 0; 84 attr.datavalue = (void *) f; 85 return attr; 86 } 87 } 88 89 __attr __fn_native_io_fdopen(__attr __args[]) 90 { 91 __attr * const fd = &__args[1]; 92 __attr * const mode = &__args[2]; 93 /* fd.__data__ interpreted as int */ 94 int i = __load_via_object(fd->value, __pos___data__).intvalue; 95 /* mode.__data__ interpreted as string */ 96 char *s = __load_via_object(mode->value, __pos___data__).strvalue; 97 FILE *f; 98 __attr attr; 99 100 errno = 0; 101 f = fdopen(i, s); 102 103 /* Produce an exception if the operation failed. */ 104 105 if (f == NULL) 106 __raise_io_error(__new_int(errno)); 107 108 /* Return the __data__ attribute. */ 109 110 else 111 { 112 attr.context = 0; 113 attr.datavalue = (void *) f; 114 return attr; 115 } 116 } 117 118 __attr __fn_native_io_fread(__attr __args[]) 119 { 120 __attr * const fp = &__args[1]; 121 __attr * const size = &__args[2]; 122 /* fp interpreted as FILE reference */ 123 FILE *f = (FILE *) fp->datavalue; 124 /* size.__data__ interpreted as int */ 125 int to_read = __load_via_object(size->value, __pos___data__).intvalue; 126 char buf[to_read]; 127 size_t have_read; 128 int error; 129 char *s; 130 131 have_read = fread(buf, sizeof(char), to_read, f); 132 133 if (have_read != to_read) 134 { 135 if (feof(f) && (have_read == 0)) 136 __raise_eof_error(); 137 else if (error = ferror(f)) 138 __raise_io_error(__new_int(error)); 139 } 140 141 /* Reserve space for a new string. */ 142 143 s = __ALLOCATE(have_read + 1, sizeof(char)); 144 memcpy(s, (char *) buf, have_read); /* does not null terminate but final byte should be zero */ 145 return __new_str(s, have_read); 146 } 147 148 __attr __fn_native_io_fwrite(__attr __args[]) 149 { 150 __attr * const fp = &__args[1]; 151 __attr * const str = &__args[2]; 152 /* fp interpreted as FILE reference */ 153 FILE *f = (FILE *) fp->datavalue; 154 /* str.__data__ interpreted as string */ 155 __attr sa = __load_via_object(str->value, __pos___data__); 156 char *s = sa.strvalue; 157 size_t to_write = sa.size; 158 size_t have_written = fwrite(s, sizeof(char), to_write, f); 159 int error; 160 161 if (have_written != to_write) 162 { 163 if (feof(f)) 164 __raise_eof_error(); 165 else if (error = ferror(f)) 166 __raise_io_error(__new_int(error)); 167 } 168 169 return __builtins___none_None; 170 } 171 172 __attr __fn_native_io_close(__attr __args[]) 173 { 174 __attr * const fd = &__args[1]; 175 /* fd.__data__ interpreted as int */ 176 int i = __load_via_object(fd->value, __pos___data__).intvalue; 177 178 errno = 0; 179 if (close(i) == -1) 180 __raise_io_error(__new_int(errno)); 181 182 return __builtins___none_None; 183 } 184 185 __attr __fn_native_io_read(__attr __args[]) 186 { 187 __attr * const fd = &__args[1]; 188 __attr * const n = &__args[2]; 189 /* fd.__data__ interpreted as int */ 190 int i = __load_via_object(fd->value, __pos___data__).intvalue; 191 /* n.__data__ interpreted as int */ 192 int to_read = __load_via_object(n->value, __pos___data__).intvalue; 193 char buf[to_read]; 194 ssize_t have_read; 195 char *s; 196 197 errno = 0; 198 have_read = read(i, buf, to_read * sizeof(char)); 199 200 if (have_read == -1) 201 __raise_io_error(__new_int(errno)); 202 203 /* Reserve space for a new string. */ 204 205 s = __ALLOCATE(have_read + 1, 1); 206 memcpy(s, (char *) buf, have_read); /* does not null terminate but final byte should be zero */ 207 return __new_str(s, have_read); 208 } 209 210 __attr __fn_native_io_write(__attr __args[]) 211 { 212 __attr * const fd = &__args[1]; 213 __attr * const str = &__args[2]; 214 /* fd.__data__ interpreted as int */ 215 int i = __load_via_object(fd->value, __pos___data__).intvalue; 216 /* str.__data__ interpreted as string */ 217 __attr sa = __load_via_object(str->value, __pos___data__); 218 char *s = sa.strvalue; 219 ssize_t have_written; 220 221 errno = 0; 222 have_written = write(i, s, sizeof(char) * sa.size); 223 224 if (have_written == -1) 225 __raise_io_error(__new_int(errno)); 226 227 return __new_int(have_written); 228 } 229 230 /* Module initialisation. */ 231 232 void __main_native_io() 233 { 234 }