paul@6 | 1 | #!/usr/bin/env python |
paul@6 | 2 | |
paul@328 | 3 | """ |
paul@328 | 4 | POSIX input/output functions. |
paul@328 | 5 | |
paul@527 | 6 | Copyright (C) 2016, 2017 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@347 | 22 | from __builtins__.file import filestream |
paul@347 | 23 | from __builtins__.types import check_int, check_string |
paul@415 | 24 | |
paul@356 | 25 | from native import ( |
paul@356 | 26 | close as _close, |
paul@356 | 27 | fdopen as _fdopen, |
paul@356 | 28 | read as _read, |
paul@356 | 29 | write as _write |
paul@356 | 30 | ) |
paul@328 | 31 | |
paul@415 | 32 | import locale |
paul@415 | 33 | |
paul@336 | 34 | # Abstractions for system-level files and streams. |
paul@336 | 35 | |
paul@332 | 36 | class sysfile: |
paul@332 | 37 | |
paul@332 | 38 | "A system-level file object." |
paul@332 | 39 | |
paul@332 | 40 | def __init__(self, fd): |
paul@332 | 41 | |
paul@332 | 42 | "Initialise the file with the given 'fd'." |
paul@332 | 43 | |
paul@332 | 44 | self.fd = fd |
paul@332 | 45 | |
paul@332 | 46 | def read(self, n): |
paul@332 | 47 | |
paul@336 | 48 | "Read 'n' bytes from the file, returning a string." |
paul@332 | 49 | |
paul@332 | 50 | return read(self.fd, n) |
paul@332 | 51 | |
paul@332 | 52 | def write(self, s): |
paul@332 | 53 | |
paul@336 | 54 | "Write string 's' to the file." |
paul@336 | 55 | |
paul@352 | 56 | return write(self.fd, s) |
paul@352 | 57 | |
paul@352 | 58 | def close(self): |
paul@352 | 59 | |
paul@352 | 60 | "Close the file." |
paul@352 | 61 | |
paul@352 | 62 | close(self.fd) |
paul@336 | 63 | |
paul@347 | 64 | class sysstream(filestream): |
paul@336 | 65 | |
paul@336 | 66 | "A system-level stream object." |
paul@336 | 67 | |
paul@392 | 68 | def __init__(self, fd, mode="r", encoding=None, bufsize=1024): |
paul@336 | 69 | |
paul@392 | 70 | """ |
paul@392 | 71 | Initialise the stream with the given 'fd', 'mode', 'encoding' and |
paul@392 | 72 | 'bufsize'. |
paul@392 | 73 | """ |
paul@336 | 74 | |
paul@527 | 75 | check_int(fd) |
paul@527 | 76 | check_string(mode) |
paul@527 | 77 | |
paul@392 | 78 | get_using(filestream.__init__, self)(encoding, bufsize) |
paul@527 | 79 | self.__data__ = _fdopen(fd, mode) |
paul@336 | 80 | |
paul@337 | 81 | # Standard streams. |
paul@337 | 82 | |
paul@337 | 83 | stdin = sysstream(0) |
paul@337 | 84 | stdout = sysstream(1, "w") |
paul@337 | 85 | stderr = sysstream(2, "w") |
paul@337 | 86 | |
paul@415 | 87 | # Localised streams. |
paul@415 | 88 | # Perform locale initialisation explicitly to ensure that the locale module |
paul@415 | 89 | # and various function defaults have been initialised. |
paul@415 | 90 | |
paul@415 | 91 | locale.initlocale() |
paul@415 | 92 | lstdin = sysstream(0, "r", locale.getpreferredencoding()) |
paul@415 | 93 | |
paul@336 | 94 | # Input/output functions. |
paul@332 | 95 | |
paul@352 | 96 | def close(fd): |
paul@352 | 97 | |
paul@352 | 98 | "Close the file descriptor 'fd'." |
paul@352 | 99 | |
paul@356 | 100 | _close(fd) |
paul@352 | 101 | |
paul@6 | 102 | def closerange(fd_low, fd_high): pass |
paul@6 | 103 | def dup(fd): pass |
paul@6 | 104 | def dup2(old_fd, new_fd): pass |
paul@328 | 105 | def fchdir(fd): pass |
paul@6 | 106 | def fchmod(fd, mode): pass |
paul@6 | 107 | def fchown(fd, uid, gid): pass |
paul@328 | 108 | def fdatasync(fd): pass |
paul@328 | 109 | |
paul@328 | 110 | def fdopen(fd, mode="r"): |
paul@328 | 111 | |
paul@328 | 112 | """ |
paul@328 | 113 | Open a stream for the given file descriptor 'fd', operating in the given |
paul@328 | 114 | 'mode'. |
paul@328 | 115 | """ |
paul@328 | 116 | |
paul@527 | 117 | return sysstream(fd, mode) |
paul@328 | 118 | |
paul@6 | 119 | def fpathconf(fd, name): pass |
paul@6 | 120 | def fstat(fd): pass |
paul@6 | 121 | def fstatvfs(fd): pass |
paul@328 | 122 | def fsync(fd): pass |
paul@6 | 123 | def ftruncate(fd, length): pass |
paul@6 | 124 | def isatty(fd): pass |
paul@328 | 125 | |
paul@328 | 126 | SEEK_CUR = 1 |
paul@328 | 127 | SEEK_END = 2 |
paul@328 | 128 | SEEK_SET = 0 |
paul@328 | 129 | |
paul@6 | 130 | def lseek(fd, pos, how): pass |
paul@6 | 131 | def open(filename, flag, mode=0777): pass |
paul@6 | 132 | def openpty(): pass |
paul@6 | 133 | def pipe(): pass |
paul@6 | 134 | def putenv(key, value): pass |
paul@328 | 135 | |
paul@328 | 136 | def read(fd, n): |
paul@328 | 137 | |
paul@328 | 138 | """ |
paul@328 | 139 | Read using the low-level file descriptor 'fd' the given number of bytes 'n'. |
paul@328 | 140 | """ |
paul@328 | 141 | |
paul@347 | 142 | check_int(fd) |
paul@347 | 143 | check_int(n) |
paul@356 | 144 | return _read(fd, n) |
paul@328 | 145 | |
paul@6 | 146 | def times(): pass |
paul@6 | 147 | def ttyname(fd): pass |
paul@6 | 148 | def umask(new_mask): pass |
paul@6 | 149 | def uname(): pass |
paul@6 | 150 | def unsetenv(key): pass |
paul@328 | 151 | |
paul@328 | 152 | def write(fd, s): |
paul@328 | 153 | |
paul@328 | 154 | "Write using the low-level file descriptor 'fd' the given string 's'." |
paul@328 | 155 | |
paul@347 | 156 | check_int(fd) |
paul@347 | 157 | check_string(s) |
paul@356 | 158 | return _write(fd, s) |
paul@6 | 159 | |
paul@336 | 160 | # Constants. |
paul@336 | 161 | |
paul@6 | 162 | O_APPEND = 1024 |
paul@6 | 163 | O_ASYNC = 8192 |
paul@6 | 164 | O_CREAT = 64 |
paul@6 | 165 | O_DIRECT = 16384 |
paul@6 | 166 | O_DIRECTORY = 65536 |
paul@6 | 167 | O_DSYNC = 4096 |
paul@6 | 168 | O_EXCL = 128 |
paul@6 | 169 | O_LARGEFILE = 32768 |
paul@6 | 170 | O_NDELAY = 2048 |
paul@6 | 171 | O_NOATIME = 262144 |
paul@6 | 172 | O_NOCTTY = 256 |
paul@6 | 173 | O_NOFOLLOW = 131072 |
paul@6 | 174 | O_NONBLOCK = 2048 |
paul@6 | 175 | O_RDONLY = 0 |
paul@6 | 176 | O_RDWR = 2 |
paul@6 | 177 | O_RSYNC = 1052672 |
paul@6 | 178 | O_SYNC = 1052672 |
paul@6 | 179 | O_TRUNC = 512 |
paul@6 | 180 | O_WRONLY = 1 |
paul@6 | 181 | |
paul@6 | 182 | # vim: tabstop=4 expandtab shiftwidth=4 |