From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: joakim@verona.se Newsgroups: gmane.emacs.devel Subject: Re: using libmagic in Emacs? Date: Thu, 20 Aug 2009 00:49:53 +0200 Message-ID: References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: ger.gmane.org 1250722224 14706 80.91.229.12 (19 Aug 2009 22:50:24 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 19 Aug 2009 22:50:24 +0000 (UTC) Cc: Emacs Development To: Stefan Monnier Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Thu Aug 20 00:50:17 2009 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1Mdtyw-0007eC-Rk for ged-emacs-devel@m.gmane.org; Thu, 20 Aug 2009 00:50:15 +0200 Original-Received: from localhost ([127.0.0.1]:49322 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Mdtyw-0002Hv-Ab for ged-emacs-devel@m.gmane.org; Wed, 19 Aug 2009 18:50:14 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1Mdtyp-0002He-4W for emacs-devel@gnu.org; Wed, 19 Aug 2009 18:50:07 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1Mdtyk-0002H3-3c for emacs-devel@gnu.org; Wed, 19 Aug 2009 18:50:06 -0400 Original-Received: from [199.232.76.173] (port=60560 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Mdtyj-0002Go-TD for emacs-devel@gnu.org; Wed, 19 Aug 2009 18:50:01 -0400 Original-Received: from proxy3.bredband.net ([195.54.101.73]:52974) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1Mdtyg-0002T8-0i for emacs-devel@gnu.org; Wed, 19 Aug 2009 18:50:00 -0400 Original-Received: from iph1.telenor.se (195.54.127.132) by proxy3.bredband.net (7.3.140.3) id 49F597CD02E662F8 for emacs-devel@gnu.org; Thu, 20 Aug 2009 00:49:55 +0200 X-SMTPAUTH-B2: joakvero X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Aj2GAPAejEpT44qWPGdsb2JhbACWToQ3AQEBATe8DoQaBQ X-IronPort-AV: E=Sophos;i="4.43,410,1246831200"; d="scan'208";a="37693440" Original-Received: from ua-83-227-138-150.cust.bredbandsbolaget.se (HELO exodia) ([83.227.138.150]) by iph1.telenor.se with ESMTP; 20 Aug 2009 00:49:55 +0200 Original-Received: from localhost.localdomain (DIR-655.lan [192.168.200.113]) (authenticated bits=0) by exodia (8.14.3/8.14.3) with ESMTP id n7JMnrRf001751 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES128-SHA bits=128 verify=NO); Thu, 20 Aug 2009 00:49:54 +0200 In-Reply-To: (Stefan Monnier's message of "Tue, 18 Aug 2009 15:23:06 -0400") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1.50 (gnu/linux) X-detected-operating-system: by monty-python.gnu.org: Genre and OS details not recognized. X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:114419 Archived-At: --=-=-= Stefan Monnier 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. > > > Stefan I attach an early draft filemagic patch. Some notes: - The mime type info usualy is less granular than the free text info: file --mime /tmp/tst.xcf /tmp/tst.xcf: application/octet-stream; charset=binary file /tmp/tst.xcf /tmp/tst.xcf: GIMP XCF image data, version 0, 640 x 480, RGB Color This is dependent on the file magic info file used. - We can probably have much fun debating what the interface should look like at the lisp level. Any ideas? --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=filemagic1.patch 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..b80255a 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@ @@ -511,6 +512,12 @@ MSDOS_OBJ = dosfns.o msdos.o w16select.o xmenu.o #endif #endif +#ifdef HAVE_LIBMAGIC +FILEMAGIC_OBJ = filemagic.o +#else +FILEMAGIC_OBJ = +#endif + #ifdef CYGWIN CYGWIN_OBJ = sheap.o #endif @@ -551,7 +558,7 @@ obj= dispnew.o frame.o scroll.o xdisp.o menu.o $(XMENU_OBJ) window.o \ syntax.o UNEXEC bytecode.o \ process.o callproc.o \ region-cache.o sound.o atimer.o \ - doprnt.o strftime.o intervals.o textprop.o composite.o md5.o \ + doprnt.o strftime.o intervals.o textprop.o composite.o md5.o ${FILEMAGIC_OBJ} \ $(MSDOS_OBJ) $(NS_OBJ) $(CYGWIN_OBJ) $(FONT_DRIVERS) /* Object files used on some machine or other. @@ -878,7 +885,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 . */ /* 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/emacs.c b/src/emacs.c index 657465d..03d7744 100644 --- a/src/emacs.c +++ b/src/emacs.c @@ -1683,6 +1683,9 @@ main (int argc, char **argv) syms_of_window (); syms_of_xdisp (); syms_of_font (); +#ifdef HAVE_LIBMAGIC + syms_of_filemagic(); +#endif #ifdef HAVE_WINDOW_SYSTEM syms_of_fringe (); syms_of_image (); diff --git a/src/filemagic.c b/src/filemagic.c new file mode 100644 index 0000000..1dcf065 --- /dev/null +++ b/src/filemagic.c @@ -0,0 +1,65 @@ +#include +#include +#include +#include +#include + +#include "lisp.h" +/* + + */ +/* + magic_t + magic_open(int flags); + + void + magic_close(magic_t cookie); + + const char * + magic_error(magic_t cookie); + + int + magic_errno(magic_t cookie); + + const char * + magic_file(magic_t cookie, const char *filename); + + const char * + magic_buffer(magic_t cookie, const void *buffer, size_t length); + + int + magic_setflags(magic_t cookie, int flags); + + int + magic_check(magic_t cookie, const char *filename); + + int + magic_compile(magic_t cookie, const char *filename); + + int + magic_load(magic_t cookie, const char *filename); +*/ + + +DEFUN ("file-magic-file", Ffile_magic_file, Sfile_magic_file, 1,1,0, + doc: /* return libmagic file description for filename */) + (filename) + Lisp_Object filename; +{ + if (!STRINGP (filename)) return Qnil; + printf("filename:%s\n",SDATA(filename)); + magic_t cookie= magic_open(MAGIC_MIME_TYPE); + magic_load(cookie,NULL); + printf("cookie:%d\n",cookie); + char *rvs=magic_file(cookie, SDATA(filename)); + printf("rvs:%s\n",rvs); + Lisp_Object rv=intern(rvs); + magic_close(cookie); + return rv; +} + +void +syms_of_filemagic () +{ + defsubr (&Sfile_magic_file); +} --=-=-= -- Joakim Verona --=-=-=--