cgore.comcgore@cgore.commicroblogblogphotosChristianitycryptographyjournalingoutdoorsmy master's thesisprogramming
Thinking Bicycle
Github @cgoreFacebook @cgoreInstagram @christopher.mark.gore

This website is copyright © 1995-2016 Christopher Mark Gore, all rights reserved. The content may not be copied or republished via any means, traditional or electronic, without express written permission of the author.

Chris Gore: Programming: Lisp: SBCL and Apache

Strangely, one of the biggest hurdles to writing web code in SBCL is at the very start of the game: getting anything at all to show up in Apache. SBCL is a great language for CGI, but not without a few tweaks and weird flags. The first problem is in Apache itself. If you want Apache to recognize somefile.lisp as a CGI executable and not just plaintext, you will need the following line in your Apache configuration file:

AddHandler cgi-script .lisp

If you want to be able to have the main page for a directory be one of these Lisp CGI scripts, you will also need to add the following line to your Apache configuration file:

DirectoryIndex index.lisp

You will also need to make sure that CGIs are allowed in the directory you are trying to keep the script in. That is easy enough.

Here comes the hard part though, getting SBCL to play nice as a CGI script: not printing miles of debugging info, banners, and who knows what else, taking simple input from stdin and putting its output simply to stdout, and so forth. In my case I also load a special lisp core, although that is entirely optional. Specifically, for this website I have a bash file named cgore-runlisp on the path with the following contents:

#! /bin/bash

/usr/bin/sbcl \
    --core /var/www/ \
    --noinform --disable-debugger \
    --eval '(set-dispatch-macro-character
              ## #!
              (lambda (stream bang number)
                (declare (ignore bang number))
                (read-line stream)
                t))' \
    --load $1 --eval '(quit)'

After that it just boils down to simple CGI stuff and a lot of lisp code. I can make a file, let's say foo.lisp, and its contents could be as such:

#! /usr/bin/env cgore-runlisp

(webpage t '("Programming" "Lisp")
         (p "I am a big fan of Lisp, "
            (href "" "SBCL (Steel Bank Common Lisp)")
            " more specifically: it is my absolute favorite way to tell a computer to do things."))

This has my totally awesome web framework in it (the source of the webpage, p, and href functions, as well as many others), but you get the idea. You could use the format built-in to do it all instead. Congratulations, you are doing CGI scripts in SBCL.