emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: using libmagic in Emacs?


From: joakim
Subject: Re: using libmagic in Emacs?
Date: Thu, 20 Aug 2009 21:19:28 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1.50 (gnu/linux)

Stefan Monnier <address@hidden> writes:

>>> I think it's a good idea.  It may require some non-trivial changes on
>>> the Lisp side, since libmagic's information is not quite the same as
>>> what Emacs currently uses: we'll probably want to use libmagic to get
>>> a MIME-type and then have a table mapping mime-types to major modes or
>>> some such.
>
>> I attach an early draft filemagic patch.
>
>> Some notes:
>
>> - The mime type info usualy is less granular than the free
>> text info:
>
> We can provide both.  I think we'd want 2 functions: one to get the free
> text info, which just returns a string (or nil), and another to get the
> MIME info, which returns a cons, whose car is a symbol such as
> application/octet-stream, and whose cdr is an alist representing
> the additional optional info.
>
>> file --mime /tmp/tst.xcf
>> /tmp/tst.xcf: application/octet-stream; charset=binary
>
> This would look like (application/octet-stream (charset . "binary"))
>
> A few more comments:
> - please follow the GNU coding convention.  I.e. put spaces where
>   they need to be (e.g. around parens and operators).
> - don't bother with a new file.  Just put it into fileio.c.
> - as someone else mentioned, CPP macros in the Makefile.in are things
>   we'd like to get rid of, so please don't put more of them there.
>   Use autoconf's m4 macros instead, thank you.
>
>
>         Stefan

Find attached a new slightly improved patch according to the suggestions
of the list.

However, I see now I didnt properly read your text above, the
function I wrote now returns a list with 3 elements, (MIME_TYPE
MIME_ENCODING DESCRIPTION). Do we still need 2 functions as you write
above?

diff --git a/configure.in b/configure.in
index f4096db..cb74523 100644
--- a/configure.in
+++ b/configure.in
@@ -137,6 +137,8 @@ OPTION_DEFAULT_ON([xft],[don't use XFT for anti aliased 
fonts])
 OPTION_DEFAULT_ON([libotf],[don't use libotf for OpenType font support])
 OPTION_DEFAULT_ON([m17n-flt],[don't use m17n-flt for text shaping])
 
+OPTION_DEFAULT_ON([filemagic],[don't compile with filemagic support])
+
 OPTION_DEFAULT_ON([toolkit-scroll-bars],[don't use Motif or Xaw3d scroll bars])
 OPTION_DEFAULT_ON([xaw3d],[don't use Xaw3d])
 OPTION_DEFAULT_ON([xim],[don't use X11 XIM])
@@ -2223,6 +2225,19 @@ if test x"$ac_cv_func_alloca_works" != xyes; then
    AC_MSG_ERROR( [a system implementation of alloca is required] )
 fi
 
+
+HAVE_LIBMAGIC=no
+if test "${with_filemagic}" != "no"; then
+  #libmagic support
+  AC_CHECK_HEADERS(magic.h, [  
AC_CHECK_LIB(magic,magic_open,HAVE_LIBMAGIC=yes) ])
+fi
+
+if test "${HAVE_LIBMAGIC}" = "yes"; then
+  LIBMAGIC=-lmagic
+  AC_SUBST(LIBMAGIC)
+  AC_DEFINE(HAVE_LIBMAGIC, 1, [Define to 1 if using libmagic.])  
+fi
+
 # fmod, logb, and frexp are found in -lm on most systems.
 # On HPUX 9.01, -lm does not contain logb, so check for sqrt.
 AC_CHECK_LIB(m, sqrt)
@@ -2954,6 +2969,7 @@ echo "  Does Emacs use -lpng?                             
      ${HAVE_PNG}"
 echo "  Does Emacs use -lrsvg-2?                                ${HAVE_RSVG}"
 echo "  Does Emacs use -lgpm?                                   ${HAVE_GPM}"
 echo "  Does Emacs use -ldbus?                                  ${HAVE_DBUS}"
+echo "  Does Emacs use -lmagic?                                 
${HAVE_LIBMAGIC}"
 
 echo "  Does Emacs use -lfreetype?                              
${HAVE_FREETYPE}"
 echo "  Does Emacs use -lm17n-flt?                              
${HAVE_M17N_FLT}"
diff --git a/src/Makefile.in b/src/Makefile.in
index 425cf98..33d1a14 100644
--- a/src/Makefile.in
+++ b/src/Makefile.in
@@ -420,6 +420,7 @@ LIBX= $(LIBXMENU) LD_SWITCH_X_SITE
 #endif /* not HAVE_LIBRESOLV */
 
 LIBSOUND= @LIBSOUND@
+LIBMAGIC= @LIBMAGIC@
 CFLAGS_SOUND= @CFLAGS_SOUND@
 
 RSVG_LIBS= @RSVG_LIBS@
@@ -878,7 +879,7 @@ SOME_MACHINE_LISP = ../lisp/mouse.elc \
    duplicated symbols.  If the standard libraries were compiled
    with GCC, we might need gnulib again after them.  */
 
-LIBES = $(LOADLIBES) $(LIBS) $(LIBX) $(LIBSOUND) $(RSVG_LIBS) $(DBUS_LIBS) \
+LIBES = $(LOADLIBES) $(LIBS) $(LIBX) $(LIBSOUND) $(LIBMAGIC) $(RSVG_LIBS) 
$(DBUS_LIBS) \
    LIBGPM LIBRESOLV LIBS_SYSTEM LIBS_MACHINE LIBS_TERMCAP \
    LIBS_DEBUG $(GETLOADAVG_LIBS) \
    @FREETYPE_LIBS@ @FONTCONFIG_LIBS@ @LIBOTF_LIBS@ @M17N_FLT_LIBS@ \
diff --git a/src/config.in b/src/config.in
index 404e00b..c966a09 100644
--- a/src/config.in
+++ b/src/config.in
@@ -262,6 +262,9 @@ along with GNU Emacs.  If not, see 
<http://www.gnu.org/licenses/>.  */
 /* Define to 1 if you have the gpm library (-lgpm). */
 #undef HAVE_GPM
 
+/* Define to 1 if you have the filemagic library (-lmagic). */
+#undef HAVE_LIBMAGIC
+
 /* Define to 1 if you have the `grantpt' function. */
 #undef HAVE_GRANTPT
 
diff --git a/src/fileio.c b/src/fileio.c
index 3702d4c..375502e 100644
--- a/src/fileio.c
+++ b/src/fileio.c
@@ -205,6 +205,10 @@ Lisp_Object Vdirectory_sep_char;
 int write_region_inhibit_fsync;
 #endif
 
+#ifdef HAVE_LIBMAGIC
+#include <magic.h>
+#endif
+
 /* Non-zero means call move-file-to-trash in Fdelete_file or
    Fdelete_directory.  */
 int delete_by_moving_to_trash;
@@ -2997,6 +3001,45 @@ DEFUN ("unix-sync", Funix_sync, Sunix_sync, 0, 0, "",
 
 #endif /* HAVE_SYNC */
 
+#ifdef HAVE_LIBMAGIC
+DEFUN ("file-magic-file", Ffile_magic_file, Sfile_magic_file, 1,1,0,
+       doc: /* Return (MIME_TYPE MIME_ENCODING DESCRIPTION) for FILENAME.
+Return nil on error. */)
+  (filename)
+     Lisp_Object filename;
+{
+  magic_t cookie=NULL;
+  if (!STRINGP (filename)) goto libmagic_error;
+  char* f = SDATA (filename);
+  char* rvs;
+  cookie = magic_open (MAGIC_NONE);
+  magic_load (cookie,NULL); //load default database
+
+  magic_setflags (cookie, MAGIC_MIME_TYPE);  
+  rvs = magic_file (cookie, f);
+  if (rvs == NULL) goto libmagic_error;
+  Lisp_Object file_mime = intern (rvs);
+
+  magic_setflags (cookie, MAGIC_MIME_ENCODING);
+  rvs=magic_file (cookie, f);
+  if (rvs == NULL) goto libmagic_error;
+  Lisp_Object file_encoding = intern(rvs);  
+
+  magic_setflags (cookie, MAGIC_NONE);
+  rvs=magic_file (cookie, f);
+  if (rvs == NULL) goto libmagic_error;
+
+  Lisp_Object file_freetext = make_specified_string (rvs, strlen(rvs), 
strlen(rvs), NULL);    
+  Lisp_Object rv = Fcons (file_mime, Fcons (file_encoding, Fcons 
(file_freetext, Qnil)));
+  
+  magic_close (cookie);
+  return rv;
+ libmagic_error:
+  if (cookie != NULL) magic_close (cookie);  
+  return Qnil;
+}
+#endif
+
 DEFUN ("file-newer-than-file-p", Ffile_newer_than_file_p, 
Sfile_newer_than_file_p, 2, 2, 0,
        doc: /* Return t if file FILE1 is newer than file FILE2.
 If FILE1 does not exist, the answer is nil;
@@ -5781,6 +5824,9 @@ When non-nil, the function `move-file-to-trash' will be 
used by
 #ifdef HAVE_SYNC
   defsubr (&Sunix_sync);
 #endif
+#ifdef HAVE_LIBMAGIC
+    defsubr (&Sfile_magic_file);
+#endif
 }
 
 /* arch-tag: 64ba3fd7-f844-4fb2-ba4b-427eb928786c

-- 
Joakim Verona

reply via email to

[Prev in Thread] Current Thread [Next in Thread]