2
votes

I am trying to determine the system's OS with a lisp program running on emacs+slime, using code from the internet (because I am a newbie in lisp). In particular, I use the following code:

;; check OS type
(cond
 ((string-equal system-type "windows-nt") ; Microsoft Windows
  (progn
    (message "Microsoft Windows") )
  )
 ((string-equal system-type "darwin")   ; Mac OS X
  (progn
    (message "Mac OS X")
    )
  )
 ((string-equal system-type "gnu/linux") ; linux
  (progn
    (message "Linux") )
  )
)

(I found this code in this website.)

I put the code above in a file (02.lisp) and after the last parenthesis I press C-c C-c to compile it. But this gives me the following error:

Unbound variable: SYSTEM-TYPE

   [Condition of type UNBOUND-VARIABLE]

Same thing happens when I put the code directly in the top level. This happens both in my Windows computer where I am running the lisp program in a lispbox installation of emacs+slime, and in my Linux computer where I am running the lisp program in a standard debian installation of emacs+slime via apt-get.

Why am I getting this error, and what would be the right way to find out, in common lisp (so inside the .lisp program) which operating system it is in? Thank you very much in advance, and please keep in mind that I am very new to lisp+emacs, so apologies if this is a silly/confused question.

----Edit to add more details on my problem:

I want to be able to do this within the lisp program (02.lisp) because I would like to be able to load a database via this 02.lisp. So the code I was planning to actually use with the above way (which I now see is completely wrong, thanks to jch's answer), is the following:

(cond
 ((string-equal system-type "windows-nt") ; Microsoft Windows
  (progn
    (message "Microsoft Windows") 
    (with-open-file (in "g:/lisp programs/implications.db")
      (with-standard-io-syntax
    (setf *db* (read in))))
)
  )
  ((string-equal system-type "gnu/linux") ; linux
  (progn
    (message "Linux")
    (with-open-file (in "/media/NANO16GB/lisp programs/implications.db")
      (with-standard-io-syntax
    (setf *db* (read in)))
    )
  )))

I want to have this because I work on this program in two computers, depending where I am, and I want the program to load a database from my usb stick.

So the question is, how to determine which OS am I running the program in, within the .lisp program, in order to use the appropriate code to load the database. Of course, a different code, that helps me load the database in my situation, would help, but by now I'm interested in determining the OS from within the .lisp program, just because I can't find out how to do it.

-----------Solved: thanks to jch's comments, I got to the following code that works:

(cond
 ((string-equal (software-type) "Microsoft Windows") ; Microsoft Windows
  (progn
    (format t "Microsoft Windows") 
    (with-open-file (in "g:/lisp programs/implications.db")
      (with-standard-io-syntax
    (setf *db* (read in))))
)
  )
  ((string-equal (software-type) "Linux") ; linux
  (progn
    (format t "Linux")
     (with-open-file (in "/media/NANO16GB/lisp programs/implications.db")
      (with-standard-io-syntax
    (setf *db* (read in)))
    )
  )))
2

2 Answers

4
votes

If you're using Emacs with SLIME, you've got two Lisp implementations running at the same time:

  • Emacs Lisp, which is part of Emacs, which is case-sensitive and defines a function called message and a variable system-type;
  • Common Lisp, which is not case-sensitive, and doesn't define either.

Your problem is that you're trying to get the Common Lisp implementation to evaluate Emacs Lisp code. That is not going to work.

Emacs Lisp code should go into files which end in .el, and you evaluate an expression using C-x C-e. Common Lisp code should go into files which end in .lisp, and you tell SLIME to evaluate an expression using C-c C-c.

3
votes

If you use a recent version of ASDF, you can portably use the function uiop/os:detect-os, which returns a keyword, or the predicates uiop/os:os-unix-p, uiop/os:os-macosx-p, uiop/os:os-windows-p, and uiop/os:os-genera-p.