0.9.17.13: SB-POSIX:CFSET*SPEED, SB-POSIX:CFGET*SPEED, and related constants
[sbcl.git] / contrib / sb-posix / interface.lisp
1 (cl:in-package :sb-posix)
2
3 (defmacro define-protocol-class
4     (name alien-type superclasses slots &rest options)
5   (let ((to-protocol (intern (format nil "ALIEN-TO-~A" name)))
6         (to-alien (intern (format nil "~A-TO-ALIEN" name))))
7     `(progn
8       (defclass ,name ,superclasses
9         ,(loop for slotd in slots
10                collect (ldiff slotd (member :array-length slotd)))
11         ,@options)
12       (declaim (inline ,to-alien ,to-protocol))
13       (declaim (inline ,to-protocol ,to-alien))
14       (defun ,to-protocol (alien &optional instance)
15         (declare (type (sb-alien:alien (* ,alien-type)) alien)
16                  (type (or null ,name) instance))
17         (unless instance
18           (setf instance (make-instance ',name)))
19         ,@(loop for slotd in slots
20                 ;; FIXME: slotds in source are more complicated in general
21                 ;;
22                 ;; FIXME: baroque construction of intricate fragility
23                 for array-length = (getf (cdr slotd) :array-length)
24                 if array-length
25                   collect `(progn
26                              (let ((array (make-array ,array-length)))
27                                (setf (slot-value instance ',(car slotd))
28                                      array)
29                                (dotimes (i ,array-length)
30                                  (setf (aref array i)
31                                        (sb-alien:deref
32                                         (sb-alien:slot alien ',(car slotd))
33                                         i)))))
34                 else
35                   collect `(setf (slot-value instance ',(car slotd))
36                                  (sb-alien:slot alien ',(car slotd))))
37         instance)
38       (defun ,to-alien (instance &optional alien)
39         (declare (type (or null (sb-alien:alien (* ,alien-type))) alien)
40                  (type ,name instance))
41         (unless alien
42           (setf alien (sb-alien:make-alien ,alien-type)))
43         ,@(loop for slotd in slots
44                 for array-length = (getf (cdr slotd) :array-length)
45                 if array-length
46                   collect `(progn
47                              (let ((array (slot-value instance ',(car slotd))))
48                                (dotimes (i ,array-length)
49                                  (setf (sb-alien:deref
50                                         (sb-alien:slot alien ',(car slotd))
51                                         i)
52                                        (aref array i)))))
53                 else
54                   collect `(setf (sb-alien:slot alien ',(car slotd))
55                                  (slot-value instance ',(car slotd)))))
56       (find-class ',name))))
57
58 (define-condition sb-posix:syscall-error (error)
59   ((errno :initarg :errno :reader sb-posix:syscall-errno))
60   (:report (lambda (c s)
61              (let ((errno (sb-posix:syscall-errno c)))
62                (format s "System call error ~A (~A)"
63                        errno (sb-int:strerror errno))))))
64
65 (defun syscall-error ()
66   (error 'sb-posix:syscall-error :errno (get-errno)))
67
68 (declaim (inline never-fails))
69 (defun never-fails (&rest args)
70   (declare (ignore args))
71   nil)
72
73 ;;; filesystem access
74 (defmacro define-call* (name &rest arguments)
75   #-win32 `(define-call ,name ,@arguments)
76   #+win32 `(define-call ,(concatenate 'string "_" name) ,@arguments))
77
78 (define-call* "access" int minusp (pathname filename) (mode int))
79 (define-call* "chdir" int minusp (pathname filename))
80 (define-call* "chmod" int minusp (pathname filename) (mode mode-t))
81 (define-call* "close" int minusp (fd file-descriptor))
82 (define-call* "creat" int minusp (pathname filename) (mode mode-t))
83 (define-call* "dup" int minusp (oldfd file-descriptor))
84 (define-call* "dup2" int minusp (oldfd file-descriptor)
85               (newfd file-descriptor))
86 (define-call* "lseek" off-t minusp (fd file-descriptor) (offset off-t)
87               (whence int))
88 (define-call* "mkdir" int minusp (pathname filename) (mode mode-t))
89 (macrolet ((def (x)
90                `(progn
91                  (define-call-internally open-with-mode ,x int minusp
92                    (pathname filename) (flags int) (mode mode-t))
93                  (define-call-internally open-without-mode ,x int minusp
94                    (pathname filename) (flags int))
95                  (define-entry-point ,x
96                      (pathname flags &optional (mode nil mode-supplied))
97                    (if mode-supplied
98                        (open-with-mode pathname flags mode)
99                        (open-without-mode pathname flags))))))
100     (def #-win32 "open" #+win32 "_open"))
101 (define-call "rename" int minusp (oldpath filename) (newpath filename))
102 (define-call* "rmdir" int minusp (pathname filename))
103 (define-call* "unlink" int minusp (pathname filename))
104 (define-call "opendir" (* t) null-alien (pathname filename))
105 (define-call "readdir" (* dirent)
106   ;; readdir() has the worst error convention in the world.  It's just
107   ;; too painful to support.  (return is NULL _and_ errno "unchanged"
108   ;; is not an error, it's EOF).
109   not
110   (dir (* t)))
111 (define-call "closedir" int minusp (dir (* t)))
112 ;; need to do this here because we can't do it in the DEFPACKAGE
113 (define-call* "umask" mode-t never-fails (mode mode-t))
114 (define-call* "getpid" pid-t never-fails)
115
116 #-win32
117 (progn
118   (define-call "chown" int minusp (pathname filename)
119                (owner uid-t) (group gid-t))
120   (define-call "chroot" int minusp (pathname filename))
121   (define-call "fchdir" int minusp (fd file-descriptor))
122   (define-call "fchmod" int minusp (fd file-descriptor) (mode mode-t))
123   (define-call "fchown" int minusp (fd file-descriptor)
124              (owner uid-t)  (group gid-t))
125   (define-call "fdatasync" int minusp (fd file-descriptor))
126   (define-call "ftruncate" int minusp (fd file-descriptor) (length off-t))
127   (define-call "fsync" int minusp (fd file-descriptor))
128   (define-call "lchown" int minusp (pathname filename)
129                (owner uid-t)  (group gid-t))
130   (define-call "link" int minusp (oldpath filename) (newpath filename))
131   (define-call "mkfifo" int minusp (pathname filename) (mode mode-t))
132   (define-call "symlink" int minusp (oldpath filename) (newpath filename))
133   (define-call "sync" void never-fails)
134   (define-call "truncate" int minusp (pathname filename) (length off-t))
135   ;; FIXME: Windows does have _mktemp, which has a slightlty different
136   ;; interface
137   (define-call "mkstemp" int minusp (template c-string))
138   (define-call-internally ioctl-without-arg "ioctl" int minusp
139                           (fd file-descriptor) (cmd int))
140   (define-call-internally ioctl-with-int-arg "ioctl" int minusp
141                           (fd file-descriptor) (cmd int) (arg int))
142   (define-call-internally ioctl-with-pointer-arg "ioctl" int minusp
143                           (fd file-descriptor) (cmd int)
144                           (arg alien-pointer-to-anything-or-nil))
145   (define-entry-point "ioctl" (fd cmd &optional (arg nil argp))
146     (if argp
147         (etypecase arg
148           ((alien int) (ioctl-with-int-arg fd cmd arg))
149           ((or (alien (* t)) null) (ioctl-with-pointer-arg fd cmd arg)))
150         (ioctl-without-arg fd cmd)))
151   (define-call-internally fcntl-without-arg "fcntl" int minusp
152                           (fd file-descriptor) (cmd int))
153   (define-call-internally fcntl-with-int-arg "fcntl" int minusp
154                           (fd file-descriptor) (cmd int) (arg int))
155   (define-call-internally fcntl-with-pointer-arg "fcntl" int minusp
156                           (fd file-descriptor) (cmd int)
157                           (arg alien-pointer-to-anything-or-nil))
158   (define-entry-point "fcntl" (fd cmd &optional (arg nil argp))
159     (if argp
160         (etypecase arg
161           ((alien int) (fcntl-with-int-arg fd cmd arg))
162           ((or (alien (* t)) null) (fcntl-with-pointer-arg fd cmd arg)))
163         (fcntl-without-arg fd cmd)))
164
165   ;; uid, gid
166   (define-call "geteuid" uid-t never-fails) ; "always successful", it says
167   (define-call "getresuid" uid-t never-fails)
168   (define-call "getuid" uid-t never-fails)
169   (define-call "seteuid" int minusp (uid uid-t))
170   (define-call "setfsuid" int minusp (uid uid-t))
171   (define-call "setreuid" int minusp (ruid uid-t) (euid uid-t))
172   (define-call "setresuid" int minusp (ruid uid-t) (euid uid-t) (suid uid-t))
173   (define-call "setuid" int minusp (uid uid-t))
174   (define-call "getegid" gid-t never-fails)
175   (define-call "getgid" gid-t never-fails)
176   (define-call "getresgid" gid-t never-fails)
177   (define-call "setegid" int minusp (gid gid-t))
178   (define-call "setfsgid" int minusp (gid gid-t))
179   (define-call "setgid" int minusp (gid gid-t))
180   (define-call "setregid" int minusp (rgid gid-t) (egid gid-t))
181   (define-call "setresgid" int minusp (rgid gid-t) (egid gid-t) (sgid gid-t))
182
183   ;; processes, signals
184   (define-call "alarm" int never-fails (seconds unsigned))
185   (define-call "fork" pid-t minusp)
186   (define-call "getpgid" pid-t minusp (pid pid-t))
187   (define-call "getppid" pid-t never-fails)
188   (define-call "getpgrp" pid-t never-fails)
189   (define-call "getsid" pid-t minusp  (pid pid-t))
190   (define-call "kill" int minusp (pid pid-t) (signal int))
191   (define-call "killpg" int minusp (pgrp int) (signal int))
192   (define-call "pause" int minusp)
193   (define-call "setpgid" int minusp (pid pid-t) (pgid pid-t))
194   (define-call "setpgrp" int minusp))
195
196 ;;(define-call "readlink" int minusp (path filename) (buf (* t)) (len int))
197
198 #-win32
199 (progn
200  (export 'wait :sb-posix)
201  (declaim (inline wait))
202  (defun wait (&optional statusptr)
203    (declare (type (or null (simple-array (signed-byte 32) (1))) statusptr))
204    (let* ((ptr (or statusptr (make-array 1 :element-type '(signed-byte 32))))
205           (pid (alien-funcall
206                 (extern-alien "wait" (function pid-t (* int)))
207                 (sb-sys:vector-sap ptr))))
208      (if (minusp pid)
209          (syscall-error)
210          (values pid (aref ptr 0))))))
211
212 #-win32
213 (progn
214  (export 'waitpid :sb-posix)
215  (declaim (inline waitpid))
216  (defun waitpid (pid options &optional statusptr)
217    (declare (type (sb-alien:alien pid-t) pid)
218             (type (sb-alien:alien int) options)
219             (type (or null (simple-array (signed-byte 32) (1))) statusptr))
220    (let* ((ptr (or statusptr (make-array 1 :element-type '(signed-byte 32))))
221           (pid (alien-funcall
222                 (extern-alien "waitpid" (function pid-t
223                                                   pid-t (* int) int))
224                 pid (sb-sys:vector-sap ptr) options)))
225      (if (minusp pid)
226          (syscall-error)
227          (values pid (aref ptr 0)))))
228  ;; waitpid macros
229  (define-call "wifexited" boolean never-fails (status int))
230  (define-call "wexitstatus" int never-fails (status int))
231  (define-call "wifsignaled" boolean never-fails (status int))
232  (define-call "wtermsig" int never-fails (status int))
233  (define-call "wifstopped" boolean never-fails (status int))
234  (define-call "wstopsig" int never-fails (status int))
235  #+nil ; see alien/waitpid-macros.c
236  (define-call "wifcontinued" boolean never-fails (status int)))
237
238 ;;; mmap, msync
239 #-win32
240 (progn
241  (define-call "mmap" sb-sys:system-area-pointer
242    (lambda (res)
243      (= (sb-sys:sap-int res) #.(1- (expt 2 sb-vm::n-machine-word-bits))))
244    (addr sap-or-nil) (length unsigned) (prot unsigned)
245    (flags unsigned) (fd file-descriptor) (offset off-t))
246
247  (define-call "munmap" int minusp
248    (start sb-sys:system-area-pointer) (length unsigned))
249
250 (define-call "msync" int minusp
251   (addr sb-sys:system-area-pointer) (length unsigned) (flags int)))
252
253 #-win32
254 (define-call "getpagesize" int minusp)
255 #+win32
256 ;;; KLUDGE: This could be taken from GetSystemInfo
257 (export (defun getpagesize () 4096))
258
259 ;;; passwd database
260 #-win32
261 (define-protocol-class passwd alien-passwd ()
262   ((name :initarg :name :accessor passwd-name)
263    (passwd :initarg :passwd :accessor passwd-passwd)
264    (uid :initarg :uid :accessor passwd-uid)
265    (gid :initarg :gid :accessor passwd-gid)
266    (gecos :initarg :gecos :accessor passwd-gecos)
267    (dir :initarg :dir :accessor passwd-dir)
268    (shell :initarg :shell :accessor passwd-shell)))
269
270 (defmacro define-pw-call (name arg type)
271   #-win32
272   ;; FIXME: this isn't the documented way of doing this, surely?
273   (let ((lisp-name (intern (string-upcase name) :sb-posix)))
274     `(progn
275       (export ',lisp-name :sb-posix)
276       (declaim (inline ,lisp-name))
277       (defun ,lisp-name (,arg)
278         (let ((r (alien-funcall (extern-alien ,name ,type) ,arg)))
279           (if (null r)
280               r
281               (alien-to-passwd r)))))))
282
283 (define-pw-call "getpwnam" login-name (function (* alien-passwd) c-string))
284 (define-pw-call "getpwuid" uid (function (* alien-passwd) uid-t))
285
286 (define-protocol-class stat alien-stat ()
287   ((mode :initarg :mode :accessor stat-mode)
288    (ino :initarg :ino :accessor stat-ino)
289    (dev :initarg :dev :accessor stat-dev)
290    (nlink :initarg :nlink :accessor stat-nlink)
291    (uid :initarg :uid :accessor stat-uid)
292    (gid :initarg :gid :accessor stat-gid)
293    (size :initarg :size :accessor stat-size)
294    (atime :initarg :atime :accessor stat-atime)
295    (mtime :initarg :mtime :accessor stat-mtime)
296    (ctime :initarg :ctime :accessor stat-ctime)))
297
298 (defmacro define-stat-call (name arg designator-fun type)
299   ;; FIXME: this isn't the documented way of doing this, surely?
300   (let ((lisp-name (lisp-for-c-symbol name)))
301     `(progn
302       (export ',lisp-name :sb-posix)
303       (declaim (inline ,lisp-name))
304       (defun ,lisp-name (,arg &optional stat)
305         (declare (type (or null (sb-alien:alien (* alien-stat))) stat))
306         (with-alien-stat a-stat ()
307           (let ((r (alien-funcall
308                     (extern-alien ,name ,type)
309                     (,designator-fun ,arg)
310                     a-stat)))
311             (when (minusp r)
312               (syscall-error))
313             (alien-to-stat a-stat stat)))))))
314
315 (define-stat-call #-win32 "stat" #+win32 "_stat" pathname filename
316                   (function int c-string (* alien-stat)))
317
318 #-win32
319 (define-stat-call "lstat" pathname filename
320                   (function int c-string (* alien-stat)))
321 ;;; No symbolic links on Windows, so use stat
322 #+win32
323 (progn
324   (declaim (inline lstat))
325   (export (defun lstat (filename &optional stat)
326             (if stat (stat filename stat) (stat filename)))))
327
328 (define-stat-call #-win32 "fstat" #+win32 "_fstat" fd file-descriptor
329                   (function int int (* alien-stat)))
330
331
332 ;;; mode flags
333 (define-call "s_isreg" boolean never-fails (mode mode-t))
334 (define-call "s_isdir" boolean never-fails (mode mode-t))
335 (define-call "s_ischr" boolean never-fails (mode mode-t))
336 (define-call "s_isblk" boolean never-fails (mode mode-t))
337 (define-call "s_isfifo" boolean never-fails (mode mode-t))
338 (define-call "s_islnk" boolean never-fails (mode mode-t))
339 (define-call "s_issock" boolean never-fails (mode mode-t))
340
341 #-win32
342 (progn
343  (export 'pipe :sb-posix)
344  (declaim (inline pipe))
345  (defun pipe (&optional filedes2)
346    (declare (type (or null (simple-array (signed-byte 32) (2))) filedes2))
347    (unless filedes2
348      (setq filedes2 (make-array 2 :element-type '(signed-byte 32))))
349    (let ((r (alien-funcall
350              ;; FIXME: (* INT)?  (ARRAY INT 2) would be better
351              (extern-alien "pipe" (function int (* int)))
352              (sb-sys:vector-sap filedes2))))
353      (when (minusp r)
354        (syscall-error)))
355    (values (aref filedes2 0) (aref filedes2 1))))
356
357 #-win32
358 (define-protocol-class termios alien-termios ()
359   ((iflag :initarg :iflag :accessor sb-posix:termios-iflag)
360    (oflag :initarg :oflag :accessor sb-posix:termios-oflag)
361    (cflag :initarg :cflag :accessor sb-posix:termios-cflag)
362    (lflag :initarg :lflag :accessor sb-posix:termios-lflag)
363    (cc :initarg :cc :accessor sb-posix:termios-cc :array-length nccs)))
364
365 #-win32
366 (progn
367  (export 'tcsetattr :sb-posix)
368  (declaim (inline tcsetattr))
369  (defun tcsetattr (fd actions termios)
370    (declare (type termios termios))
371    (with-alien-termios a-termios ()
372      (termios-to-alien termios a-termios)
373      (let ((fd (file-descriptor fd)))
374        (let* ((r (alien-funcall
375                   (extern-alien
376                    "tcsetattr"
377                    (function int int int (* alien-termios)))
378                   fd actions a-termios)))
379          (when (minusp r)
380            (syscall-error)))
381        (values))))
382  (export 'tcgetattr :sb-posix)
383  (declaim (inline tcgetattr))
384  (defun tcgetattr (fd &optional termios)
385    (declare (type (or null termios) termios))
386    (with-alien-termios a-termios ()
387      (let ((r (alien-funcall
388                (extern-alien "tcgetattr"
389                              (function int int (* alien-termios)))
390                (file-descriptor fd)
391                a-termios)))
392        (when (minusp r)
393          (syscall-error))
394        (setf termios (alien-to-termios a-termios termios))))
395    termios)
396  (export 'cfsetispeed :sb-posix)
397  (declaim (inline cfsetispeed))
398  (defun cfsetispeed (speed &optional termios)
399    (declare (type (or null termios) termios))
400    (with-alien-termios a-termios ()
401      (let ((r (alien-funcall
402                (extern-alien "cfsetispeed"
403                              (function int (* alien-termios) speed-t))
404                a-termios
405                speed)))
406        (when (minusp r)
407          (syscall-error))
408        (setf termios (alien-to-termios a-termios termios))))
409    termios)
410  (export 'cfsetospeed :sb-posix)
411  (declaim (inline cfsetospeed))
412  (defun cfsetospeed (speed &optional termios)
413    (declare (type (or null termios) termios))
414    (with-alien-termios a-termios ()
415      (let ((r (alien-funcall
416                (extern-alien "cfsetospeed"
417                              (function int (* alien-termios) speed-t))
418                a-termios
419                speed)))
420        (when (minusp r)
421          (syscall-error))
422        (setf termios (alien-to-termios a-termios termios))))
423    termios)
424  (export 'cfgetispeed :sb-posix)
425  (declaim (inline cfgetispeed))
426  (defun cfgetispeed (termios)
427    (declare (type termios termios))
428    (with-alien-termios a-termios ()
429      (termios-to-alien termios a-termios)
430      (alien-funcall (extern-alien "cfgetispeed"
431                                   (function speed-t (* alien-termios)))
432                     a-termios)))
433  (export 'cfgetospeed :sb-posix)
434  (declaim (inline cfgetospeed))
435  (defun cfgetospeed (termios)
436    (declare (type termios termios))
437    (with-alien-termios a-termios ()
438      (termios-to-alien termios a-termios)
439      (alien-funcall (extern-alien "cfgetospeed"
440                                  (function speed-t (* alien-termios)))
441                     a-termios))))
442
443 ;;; environment
444
445 (export 'getenv :sb-posix)
446 (defun getenv (name)
447   (let ((r (alien-funcall
448             (extern-alien "getenv" (function (* char) c-string))
449             name)))
450     (declare (type (alien (* char)) r))
451     (unless (null-alien r)
452       (cast r c-string))))
453 (define-call "putenv" int minusp (string c-string))