1 ;; ffi-magic.el --- Lisp bindings into file(1)'s libmagic.so -*- Emacs-Lisp -*-
3 ;; Copyright (C) 2008 Steve Youngs
5 ;; Author: Steve Youngs <steve@sxemacs.org>
6 ;; Maintainer: Steve Youngs <steve@sxemacs.org>
7 ;; Created: <2008-04-02>
8 ;; Homepage: https://www.sxemacs.org
9 ;; Keywords: ffi, file, magic, extension
11 ;; This file is part of SXEmacs.
13 ;; Redistribution and use in source and binary forms, with or without
14 ;; modification, are permitted provided that the following conditions
17 ;; 1. Redistributions of source code must retain the above copyright
18 ;; notice, this list of conditions and the following disclaimer.
20 ;; 2. Redistributions in binary form must reproduce the above copyright
21 ;; notice, this list of conditions and the following disclaimer in the
22 ;; documentation and/or other materials provided with the distribution.
24 ;; 3. Neither the name of the author nor the names of any contributors
25 ;; may be used to endorse or promote products derived from this
26 ;; software without specific prior written permission.
28 ;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
29 ;; IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
30 ;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31 ;; DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 ;; FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 ;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 ;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
35 ;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
36 ;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
37 ;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
38 ;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 ;; Mimic file(1)'s basic usage. At the moment, this is quite raw
43 ;; and single-minded. It will only use the default magic db and
44 ;; doesn't allow use of any of file(1)'s options.
46 ;; (magic:file-type (expand-file-name "about.el" lisp-directory))
47 ;; => "Lisp/Scheme program, ISO-8859 text"
51 ;; o Optionally output MIME type strings like "text/plain",
52 ;; "applicaton/octet-stream"
54 ;; o Other options from file(1).
60 ;; Can't do anything without this
63 (defvar ffi-magic-shared nil
64 "Shared context with preloaded magic file, to speed up things.")
67 (define-ffi-type magic_t pointer)
69 (define-ffi-function magic-open (flag)
70 "Call libmagic's magic_open()."
71 '(function magic_t int)
74 (define-ffi-function magic-load (magic magicfile)
75 "Call libmagic's magic_load()."
76 '(function int magic_t c-string)
79 (define-ffi-function magic-file (magic file)
80 "Call libmagic's magic_file()."
81 '(function safe-string magic_t c-string)
84 (define-ffi-function magic-close (magic)
85 "Call libmagic's magic_close()."
86 '(function void magic_t)
89 (define-ffi-function magic-error (magic)
90 "Call libmagic's magic_error()."
91 '(function safe-string magic_t)
95 (defun magic:file-type (file)
96 "Return as a string what type FILE is using libmagic."
97 (interactive "fFile name: ")
98 (unless ffi-magic-shared
99 (setq ffi-magic-shared (magic-open 0))
100 (magic-load ffi-magic-shared (ffi-null-pointer)))
102 (let ((ftype (magic-file ffi-magic-shared (expand-file-name file))))
107 (defun magic:error (&optional magic)
108 (magic-error (or magic ffi-magic-shared)))
111 ;;; ffi-magic.el ends here