paul@6 | 1 | #!/usr/bin/env python |
paul@6 | 2 | |
paul@328 | 3 | """ |
paul@328 | 4 | POSIX input/output functions. |
paul@328 | 5 | |
paul@328 | 6 | Copyright (C) 2016 Paul Boddie <paul@boddie.org.uk> |
paul@328 | 7 | |
paul@328 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@328 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@328 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@328 | 11 | version. |
paul@328 | 12 | |
paul@328 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@328 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@328 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@328 | 16 | details. |
paul@328 | 17 | |
paul@328 | 18 | You should have received a copy of the GNU General Public License along with |
paul@328 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@328 | 20 | """ |
paul@328 | 21 | |
paul@328 | 22 | import native |
paul@328 | 23 | |
paul@336 | 24 | # Abstractions for system-level files and streams. |
paul@336 | 25 | |
paul@332 | 26 | class sysfile: |
paul@332 | 27 | |
paul@332 | 28 | "A system-level file object." |
paul@332 | 29 | |
paul@332 | 30 | def __init__(self, fd): |
paul@332 | 31 | |
paul@332 | 32 | "Initialise the file with the given 'fd'." |
paul@332 | 33 | |
paul@332 | 34 | self.fd = fd |
paul@332 | 35 | |
paul@332 | 36 | def read(self, n): |
paul@332 | 37 | |
paul@336 | 38 | "Read 'n' bytes from the file, returning a string." |
paul@332 | 39 | |
paul@336 | 40 | _check_int(n) |
paul@332 | 41 | return read(self.fd, n) |
paul@332 | 42 | |
paul@332 | 43 | def write(self, s): |
paul@332 | 44 | |
paul@336 | 45 | "Write string 's' to the file." |
paul@336 | 46 | |
paul@336 | 47 | _check_string(s) |
paul@336 | 48 | write(self.fd, s) |
paul@336 | 49 | |
paul@336 | 50 | class sysstream: |
paul@336 | 51 | |
paul@336 | 52 | "A system-level stream object." |
paul@336 | 53 | |
paul@343 | 54 | def __init__(self, fd, mode="r", bufsize=1024): |
paul@336 | 55 | |
paul@336 | 56 | "Initialise the stream with the given 'fd' and 'mode'." |
paul@336 | 57 | |
paul@336 | 58 | self.__data__ = fdopen(fd, mode) |
paul@343 | 59 | self.bufsize = bufsize |
paul@332 | 60 | |
paul@343 | 61 | def read(self, n=0): |
paul@336 | 62 | |
paul@336 | 63 | "Read 'n' bytes from the stream." |
paul@336 | 64 | |
paul@336 | 65 | _check_int(n) |
paul@343 | 66 | |
paul@343 | 67 | # Read any indicated number of bytes. |
paul@343 | 68 | |
paul@343 | 69 | if n > 0: |
paul@343 | 70 | return native._fread(self.__data__, n) |
paul@343 | 71 | |
paul@343 | 72 | # Read all remaining bytes. |
paul@343 | 73 | |
paul@343 | 74 | else: |
paul@343 | 75 | l = [] |
paul@343 | 76 | |
paul@343 | 77 | # Read until end-of-file. |
paul@343 | 78 | |
paul@343 | 79 | try: |
paul@343 | 80 | while True: |
paul@343 | 81 | l.append(native._fread(self.__data__, self.bufsize)) |
paul@343 | 82 | |
paul@343 | 83 | # Handle end-of-file reads. |
paul@343 | 84 | |
paul@343 | 85 | except EOFError: |
paul@343 | 86 | pass |
paul@343 | 87 | |
paul@343 | 88 | return "".join(l) |
paul@336 | 89 | |
paul@336 | 90 | def write(self, s): |
paul@336 | 91 | |
paul@336 | 92 | "Write string 's' to the stream." |
paul@336 | 93 | |
paul@336 | 94 | _check_string(s) |
paul@336 | 95 | native._fwrite(self.__data__, s) |
paul@336 | 96 | |
paul@337 | 97 | # Standard streams. |
paul@337 | 98 | |
paul@337 | 99 | stdin = sysstream(0) |
paul@337 | 100 | stdout = sysstream(1, "w") |
paul@337 | 101 | stderr = sysstream(2, "w") |
paul@337 | 102 | |
paul@336 | 103 | # Input/output functions. |
paul@332 | 104 | |
paul@6 | 105 | def close(fd): pass |
paul@6 | 106 | def closerange(fd_low, fd_high): pass |
paul@6 | 107 | def dup(fd): pass |
paul@6 | 108 | def dup2(old_fd, new_fd): pass |
paul@328 | 109 | def fchdir(fd): pass |
paul@6 | 110 | def fchmod(fd, mode): pass |
paul@6 | 111 | def fchown(fd, uid, gid): pass |
paul@328 | 112 | def fdatasync(fd): pass |
paul@328 | 113 | |
paul@328 | 114 | def fdopen(fd, mode="r"): |
paul@328 | 115 | |
paul@328 | 116 | """ |
paul@328 | 117 | Open a stream for the given file descriptor 'fd', operating in the given |
paul@328 | 118 | 'mode'. |
paul@328 | 119 | """ |
paul@328 | 120 | |
paul@328 | 121 | _check_fd(fd) |
paul@328 | 122 | _check_string(mode) |
paul@328 | 123 | return native._fdopen(fd, mode) |
paul@328 | 124 | |
paul@6 | 125 | def fpathconf(fd, name): pass |
paul@6 | 126 | def fstat(fd): pass |
paul@6 | 127 | def fstatvfs(fd): pass |
paul@328 | 128 | def fsync(fd): pass |
paul@6 | 129 | def ftruncate(fd, length): pass |
paul@6 | 130 | def isatty(fd): pass |
paul@328 | 131 | |
paul@328 | 132 | SEEK_CUR = 1 |
paul@328 | 133 | SEEK_END = 2 |
paul@328 | 134 | SEEK_SET = 0 |
paul@328 | 135 | |
paul@6 | 136 | def lseek(fd, pos, how): pass |
paul@6 | 137 | def open(filename, flag, mode=0777): pass |
paul@6 | 138 | def openpty(): pass |
paul@6 | 139 | def pipe(): pass |
paul@6 | 140 | def putenv(key, value): pass |
paul@328 | 141 | |
paul@328 | 142 | def read(fd, n): |
paul@328 | 143 | |
paul@328 | 144 | """ |
paul@328 | 145 | Read using the low-level file descriptor 'fd' the given number of bytes 'n'. |
paul@328 | 146 | """ |
paul@328 | 147 | |
paul@328 | 148 | _check_fd(fd) |
paul@328 | 149 | _check_int(n) |
paul@328 | 150 | return native._read(fd, n) |
paul@328 | 151 | |
paul@6 | 152 | def times(): pass |
paul@6 | 153 | def ttyname(fd): pass |
paul@6 | 154 | def umask(new_mask): pass |
paul@6 | 155 | def uname(): pass |
paul@6 | 156 | def unsetenv(key): pass |
paul@328 | 157 | |
paul@328 | 158 | def write(fd, s): |
paul@328 | 159 | |
paul@328 | 160 | "Write using the low-level file descriptor 'fd' the given string 's'." |
paul@328 | 161 | |
paul@328 | 162 | _check_fd(fd) |
paul@328 | 163 | _check_string(s) |
paul@328 | 164 | native._write(fd, s) |
paul@6 | 165 | |
paul@336 | 166 | # Constants. |
paul@336 | 167 | |
paul@6 | 168 | O_APPEND = 1024 |
paul@6 | 169 | O_ASYNC = 8192 |
paul@6 | 170 | O_CREAT = 64 |
paul@6 | 171 | O_DIRECT = 16384 |
paul@6 | 172 | O_DIRECTORY = 65536 |
paul@6 | 173 | O_DSYNC = 4096 |
paul@6 | 174 | O_EXCL = 128 |
paul@6 | 175 | O_LARGEFILE = 32768 |
paul@6 | 176 | O_NDELAY = 2048 |
paul@6 | 177 | O_NOATIME = 262144 |
paul@6 | 178 | O_NOCTTY = 256 |
paul@6 | 179 | O_NOFOLLOW = 131072 |
paul@6 | 180 | O_NONBLOCK = 2048 |
paul@6 | 181 | O_RDONLY = 0 |
paul@6 | 182 | O_RDWR = 2 |
paul@6 | 183 | O_RSYNC = 1052672 |
paul@6 | 184 | O_SYNC = 1052672 |
paul@6 | 185 | O_TRUNC = 512 |
paul@6 | 186 | O_WRONLY = 1 |
paul@6 | 187 | |
paul@336 | 188 | # Type validation functions. |
paul@336 | 189 | |
paul@328 | 190 | def _check_fd(fd): |
paul@328 | 191 | |
paul@328 | 192 | "Check the given low-level file descriptor 'fd'." |
paul@328 | 193 | |
paul@328 | 194 | if not native._isinstance(fd, int): |
paul@328 | 195 | raise ValueError(fd) |
paul@328 | 196 | |
paul@328 | 197 | def _check_int(i): |
paul@328 | 198 | |
paul@328 | 199 | "Check the given int 'i'." |
paul@328 | 200 | |
paul@328 | 201 | if not native._isinstance(i, int): |
paul@328 | 202 | raise ValueError(i) |
paul@328 | 203 | |
paul@328 | 204 | def _check_string(s): |
paul@328 | 205 | |
paul@328 | 206 | "Check the given string 's'." |
paul@328 | 207 | |
paul@328 | 208 | if not native._isinstance(s, string): |
paul@328 | 209 | raise ValueError(s) |
paul@6 | 210 | |
paul@6 | 211 | # vim: tabstop=4 expandtab shiftwidth=4 |