075be960a80da83749c49669fdaa04fb3c23758d
[uffi.git] / src / os.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: UFFI -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          os.lisp
6 ;;;; Purpose:       Operating system interface for UFFI
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Sep 2002 
9 ;;;;
10 ;;;; $Id$
11 ;;;;
12 ;;;; This file, part of UFFI, is Copyright (c) 2002-2005 by Kevin M. Rosenberg.
13 ;;;;
14 ;;;; *************************************************************************
15
16 (in-package #:uffi)
17
18
19 (defun getenv (var)
20   "Return the value of the environment variable."
21   #+allegro (sys::getenv (string var))
22   #+clisp (sys::getenv (string var))
23   #+cmu (cdr (assoc (string var) ext:*environment-list* :test #'equalp
24                     :key #'string))
25   #+gcl (si:getenv (string var))
26   #+lispworks (lw:environment-variable (string var))
27   #+lucid (lcl:environment-variable (string var))
28   #+mcl (ccl::getenv var)
29   #+sbcl (sb-ext:posix-getenv var)
30   #-(or allegro clisp cmu gcl lispworks lucid mcl sbcl)
31   (error 'not-implemented :proc (list 'getenv var)))
32
33 (defun (setf getenv) (val var)
34   "Set an environment variable."
35   #+allegro (setf (sys::getenv (string var)) (string val))
36   #+clisp (setf (sys::getenv (string var)) (string val))
37   #+cmu (let ((cell (assoc (string var) ext:*environment-list* :test #'equalp
38                            :key #'string)))
39           (if cell
40               (setf (cdr cell) (string val))
41               (push (cons (intern (string var) "KEYWORD") (string val)) ext:*environment-list*)))
42   #+gcl (si:setenv (string var) (string val))
43   #+lispworks (setf (lw:environment-variable (string var)) (string val))
44   #+lucid (setf (lcl:environment-variable (string var)) (string val))
45   #-(or allegro clisp cmu gcl lispworks lucid)
46   (error 'not-implemented :proc (list '(setf getenv) var)))
47
48
49 ;; modified from function ASDF -- Copyright Dan Barlow and Contributors
50
51 (defun run-shell-command (control-string  &rest args &key output)
52   "Interpolate ARGS into CONTROL-STRING as if by FORMAT, and
53 synchronously execute the result using a Bourne-compatible shell, with
54 output to *trace-output*.  Returns the shell's exit code."
55   (unless output
56     (setq output *trace-output*))
57
58   (let ((command (apply #'format nil control-string args)))
59     #+sbcl
60     (sb-impl::process-exit-code
61      (sb-ext:run-program  
62       "/bin/sh"
63       (list "-c" command)
64       :input nil :output output))
65     
66     #+(or cmu scl)
67     (ext:process-exit-code
68      (ext:run-program  
69       "/bin/sh"
70       (list "-c" command)
71       :input nil :output output))
72
73     #+allegro
74     (excl:run-shell-command command :input nil :output output)
75     
76     #+lispworks
77     (system:call-system-showing-output
78      command
79      :shell-type "/bin/sh"
80      :output-stream output)
81     
82     #+clisp                             ;XXX not exactly *trace-output*, I know
83     (ext:run-shell-command  command :output :terminal :wait t)
84
85     #+openmcl
86     (nth-value 1
87                (ccl:external-process-status
88                 (ccl:run-program "/bin/sh" (list "-c" command)
89                                  :input nil :output output
90                                  :wait t)))
91
92     #-(or openmcl clisp lispworks allegro scl cmu sbcl)
93     (error "RUN-SHELL-PROGRAM not implemented for this Lisp")
94     ))