~ chicken-core (chicken-5) /posix.scm
Trap1;;;; posix.scm - Platform-specific routines2;3; Copyright (c) 2008-2022, The CHICKEN Team4; Copyright (c) 2000-2007, Felix L. Winkelmann5; All rights reserved.6;7; Redistribution and use in source and binary forms, with or without8; modification, are permitted provided that the following conditions are9; met:10;11; Redistributions of source code must retain the above copyright12; notice, this list of conditions and the following disclaimer.13;14; Redistributions in binary form must reproduce the above copyright15; notice, this list of conditions and the following disclaimer in the16; documentation and/or other materials provided with the distribution.17;18; Neither the name of the author nor the names of its contributors may19; be used to endorse or promote products derived from this software20; without specific prior written permission.21;22; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS23; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT24; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR25; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT26; HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,27; INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,28; BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS29; OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND30; ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR31; TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE32; USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH33; DAMAGE.343536(declare37 (unit posix)38 (uses scheduler pathname extras port lolevel)39 (disable-interrupts)40 (not inline ##sys#interrupt-hook ##sys#user-interrupt-hook))414243(module chicken.file.posix44 (create-fifo create-symbolic-link read-symbolic-link45 duplicate-fileno fcntl/dupfd fcntl/getfd fcntl/getfl fcntl/setfd46 fcntl/setfl file-access-time file-change-time file-modification-time47 file-close file-control file-creation-mode file-group file-link48 file-lock file-lock/blocking file-mkstemp file-open file-owner49 file-permissions file-position file-read file-select file-size50 file-stat file-test-lock file-truncate file-unlock file-write51 file-type block-device? character-device? directory? fifo?52 regular-file? socket? symbolic-link?53 fileno/stderr fileno/stdin fileno/stdout54 open-input-file* open-output-file*55 open/append open/binary open/creat open/excl open/fsync open/noctty56 open/noinherit open/nonblock open/rdonly open/rdwr open/read57 open/sync open/text open/trunc open/write open/wronly58 perm/irgrp perm/iroth perm/irusr perm/irwxg perm/irwxo perm/irwxu59 perm/isgid perm/isuid perm/isvtx perm/iwgrp perm/iwoth perm/iwusr60 perm/ixgrp perm/ixoth perm/ixusr61 port->fileno set-file-group! set-file-owner!62 set-file-permissions! set-file-position! set-file-times!63 seek/cur seek/set seek/end)6465(import scheme)6667;; These are all set! inside the posix module68(define create-fifo)69(define create-symbolic-link)70(define read-symbolic-link)71(define duplicate-fileno)7273(define fcntl/dupfd)74(define fcntl/getfd)75(define fcntl/getfl)76(define fcntl/setfd)77(define fcntl/setfl)7879(define file-access-time)80(define file-change-time)81(define file-modification-time)82(define file-close)83(define file-control)84(define file-creation-mode)85(define file-group)86(define file-link)87(define file-lock)88(define file-lock/blocking)89(define file-mkstemp)90(define file-open)91(define file-owner)92(define file-permissions)93(define file-position)94(define file-read)95(define file-select)96(define file-size)97(define file-stat)98(define file-test-lock)99(define file-truncate)100(define file-unlock)101(define file-write)102(define file-type)103104(define block-device?)105(define character-device?)106(define directory?)107(define fifo?)108(define regular-file?)109(define socket?)110(define symbolic-link?)111112(define fileno/stderr)113(define fileno/stdin)114(define fileno/stdout)115116(define open-input-file*)117(define open-output-file*)118119(define open/append)120(define open/binary)121(define open/creat)122(define open/excl)123(define open/fsync)124(define open/noctty)125(define open/noinherit)126(define open/nonblock)127(define open/rdonly)128(define open/rdwr)129(define open/read)130(define open/sync)131(define open/text)132(define open/trunc)133(define open/write)134(define open/wronly)135136(define perm/irgrp)137(define perm/iroth)138(define perm/irusr)139(define perm/irwxg)140(define perm/irwxo)141(define perm/irwxu)142(define perm/isgid)143(define perm/isuid)144(define perm/isvtx)145(define perm/iwgrp)146(define perm/iwoth)147(define perm/iwusr)148(define perm/ixgrp)149(define perm/ixoth)150(define perm/ixusr)151152(define port->fileno)153154(define seek/cur)155(define seek/end)156(define seek/set)157158(define set-file-group!)159(define set-file-owner!)160(define set-file-permissions!)161(define set-file-position!)162(define set-file-times!)163) ; chicken.file.posix164165166(module chicken.time.posix167 (seconds->utc-time utc-time->seconds seconds->local-time168 seconds->string local-time->seconds string->time time->string169 local-timezone-abbreviation)170171(import scheme)172173;; These are all set! inside the posix module174(define seconds->utc-time)175(define utc-time->seconds)176(define seconds->local-time)177(define seconds->string)178(define local-time->seconds)179(define string->time)180(define time->string)181(define local-timezone-abbreviation)182) ; chicken.time.posix183184185(module chicken.process186 (qs system system* process-execute process-fork process-run187 process-signal process-spawn process-wait call-with-input-pipe188 call-with-output-pipe close-input-pipe close-output-pipe create-pipe189 open-input-pipe open-output-pipe with-input-from-pipe190 with-output-to-pipe process process* process-sleep pipe/buf191 spawn/overlay spawn/wait spawn/nowait spawn/nowaito spawn/detach)192193(import scheme chicken.base chicken.fixnum chicken.platform)194195196;;; Execute a shell command:197198(define (system cmd)199 (##sys#check-string cmd 'system)200 (let ((r (##core#inline "C_execute_shell_command" cmd)))201 (cond ((fx< r 0)202 (##sys#signal-hook/errno203 #:process-error (##sys#update-errno) 'system "`system' invocation failed" cmd))204 (else r))))205206;;; Like `system', but bombs on nonzero return code:207208(define (system* str)209 (let ((n (system str)))210 (unless (zero? n)211 (##sys#error "shell invocation failed with non-zero return status" str n))))212213214;;; Quote string for shell:215216(define (qs str #!optional (platform (software-version)))217 (let* ((delim (if (eq? platform 'mingw32) #\" #\'))218 (escaped (if (eq? platform 'mingw32) "\"\"" "'\\''"))219 (escaped-parts220 (map (lambda (c)221 (cond222 ((char=? c delim) escaped)223 ((char=? c #\nul)224 (error 'qs "NUL character can not be represented in shell string" str))225 (else (string c))))226 (string->list str))))227 (string-append228 (string delim)229 (apply string-append escaped-parts)230 (string delim))))231232233;; These are all set! inside the posix module234(define process-execute)235(define process-fork)236(define process-run)237(define process-signal)238(define process-spawn)239(define process-wait)240241(define call-with-input-pipe)242(define call-with-output-pipe)243(define close-input-pipe)244(define close-output-pipe)245(define create-pipe)246(define open-input-pipe)247(define open-output-pipe)248(define with-input-from-pipe)249(define with-output-to-pipe)250251(define process)252(define process*)253(define process-sleep)254255(define pipe/buf)256257(define spawn/overlay)258(define spawn/wait)259(define spawn/nowait)260(define spawn/nowaito)261(define spawn/detach)262) ; chicken.process263264265(module chicken.process.signal266 (set-alarm! set-signal-mask!267 make-signal-handler signal-ignore signal-default268 set-signal-handler! signal-handler ; DEPRECATED269 signal-mask signal-mask! signal-masked? signal-unmask!270 signal/abrt signal/alrm signal/break signal/bus signal/chld271 signal/cont signal/fpe signal/hup signal/ill signal/int signal/io272 signal/kill signal/pipe signal/prof signal/quit signal/segv273 signal/stop signal/term signal/trap signal/tstp signal/urg274 signal/usr1 signal/usr2 signal/vtalrm signal/winch signal/xcpu275 signal/xfsz signals-list)276277(import scheme)278279;; These are all set! inside the posix module280(define set-alarm!)281(define set-signal-handler!) ; DEPRECATED282(define set-signal-mask!)283(define signal-handler) ; DEPRECATED284(define make-signal-handler)285(define signal-ignore)286(define signal-default)287288(define signal-mask)289(define signal-mask!)290(define signal-masked?)291(define signal-unmask!)292293(define signal/abrt)294(define signal/alrm)295(define signal/break)296(define signal/bus)297(define signal/chld)298(define signal/cont)299(define signal/fpe)300(define signal/hup)301(define signal/ill)302(define signal/int)303(define signal/io)304(define signal/kill)305(define signal/pipe)306(define signal/prof)307(define signal/quit)308(define signal/segv)309(define signal/stop)310(define signal/term)311(define signal/trap)312(define signal/tstp)313(define signal/urg)314(define signal/usr1)315(define signal/usr2)316(define signal/vtalrm)317(define signal/winch)318(define signal/xcpu)319(define signal/xfsz)320321(define signals-list)322) ; chicken.process.signal323324325(module chicken.process-context.posix326 (change-directory* set-root-directory!327 current-effective-group-id current-effective-user-id328 current-process-id current-group-id current-user-id329 parent-process-id current-user-name330 current-effective-user-name create-session331 process-group-id user-information)332333(import scheme)334335(define change-directory*)336(define set-root-directory!)337(define current-effective-group-id)338(define current-effective-user-id)339(define current-group-id)340(define current-user-id)341(define current-process-id)342(define parent-process-id)343(define current-user-name)344(define current-effective-user-name)345(define create-session)346(define process-group-id)347(define user-information)348) ; chicken.process-context.posix349350351;; This module really exports nothing. It is used to keep all the352;; posix stuff in one place, in a clean namespace. The included file353;; will set! values from the modules defined above.354(module chicken.posix ()355356(import scheme357 chicken.base358 chicken.bitwise359 chicken.condition360 chicken.fixnum361 chicken.foreign362 chicken.memory363 chicken.pathname364 chicken.port365 chicken.process-context366 chicken.time)367368(cond-expand369 (platform-unix370 (include "posixunix.scm"))371 (platform-windows372 (include "posixwin.scm")))373374) ; chicken.posix [internal, no implib generated]375376377(module chicken.errno *378(import scheme)379(define (errno) (##sys#errno))380(define errno/2big _e2big)381(define errno/acces _eacces)382(define errno/again _eagain)383(define errno/badf _ebadf)384(define errno/busy _ebusy)385(define errno/child _echild)386(define errno/deadlk _edeadlk)387(define errno/dom _edom)388(define errno/exist _eexist)389(define errno/fault _efault)390(define errno/fbig _efbig)391(define errno/ilseq _eilseq)392(define errno/intr _eintr)393(define errno/inval _einval)394(define errno/io _eio)395(define errno/isdir _eisdir)396(define errno/mfile _emfile)397(define errno/mlink _emlink)398(define errno/nametoolong _enametoolong)399(define errno/nfile _enfile)400(define errno/nodev _enodev)401(define errno/noent _enoent)402(define errno/noexec _enoexec)403(define errno/nolck _enolck)404(define errno/nomem _enomem)405(define errno/nospc _enospc)406(define errno/nosys _enosys)407(define errno/notdir _enotdir)408(define errno/notempty _enotempty)409(define errno/notty _enotty)410(define errno/nxio _enxio)411(define errno/perm _eperm)412(define errno/pipe _epipe)413(define errno/range _erange)414(define errno/rofs _erofs)415(define errno/spipe _espipe)416(define errno/srch _esrch)417(define errno/wouldblock _ewouldblock)418(define errno/xdev _exdev)419) ; chicken.errno