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: Fri, 21 Aug 2009 19:38:15 +0200 Message-ID: References: <83k50xh1m8.fsf@gnu.org> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: ger.gmane.org 1250876339 13209 80.91.229.12 (21 Aug 2009 17:38:59 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 21 Aug 2009 17:38:59 +0000 (UTC) Cc: schwab@linux-m68k.org, monnier@iro.umontreal.ca, emacs-devel@gnu.org To: Eli Zaretskii Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Fri Aug 21 19:38:51 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 1MeY4g-0006pc-NM for ged-emacs-devel@m.gmane.org; Fri, 21 Aug 2009 19:38:51 +0200 Original-Received: from localhost ([127.0.0.1]:57245 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MeY4g-00068j-2W for ged-emacs-devel@m.gmane.org; Fri, 21 Aug 2009 13:38:50 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1MeY4W-00063f-4s for emacs-devel@gnu.org; Fri, 21 Aug 2009 13:38:40 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1MeY4S-0005sj-8g for emacs-devel@gnu.org; Fri, 21 Aug 2009 13:38:39 -0400 Original-Received: from [199.232.76.173] (port=56022 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MeY4R-0005s0-OP for emacs-devel@gnu.org; Fri, 21 Aug 2009 13:38:35 -0400 Original-Received: from proxy1.bredband.net ([195.54.101.71]:51842) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1MeY4O-0004bS-Lk; Fri, 21 Aug 2009 13:38:33 -0400 Original-Received: from iph2.telenor.se (195.54.127.133) by proxy1.bredband.net (7.3.140.3) id 49F5A15202E2B5E8; Fri, 21 Aug 2009 19:38:31 +0200 X-SMTPAUTH-B2: X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Av+QAJJ4jkpT44qWPGdsb2JhbACWSoQ3AQEBATe7eIQaBYFP X-IronPort-AV: E=Sophos;i="4.44,251,1249250400"; d="scan'208";a="37017600" Original-Received: from ua-83-227-138-150.cust.bredbandsbolaget.se (HELO exodia) ([83.227.138.150]) by iph2.telenor.se with ESMTP; 21 Aug 2009 19:38:30 +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 n7LHcFwT006441 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES128-SHA bits=128 verify=NO); Fri, 21 Aug 2009 19:38:16 +0200 In-Reply-To: <83k50xh1m8.fsf@gnu.org> (Eli Zaretskii's message of "Fri, 21 Aug 2009 14:01:35 +0300") 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:114482 Archived-At: --=-=-= New filemagic patch mostly fixing Eli:s concerns. Eli Zaretskii writes: >> +DEFUN ("libmagic-file-internal", Flibmagic_file_internal, Slibmagic_file_internal, 1,1,0, >> + doc: /* Return (MIME_TYPE MIME_ENCODING DESCRIPTION) for FILENAME_OR_BUFFER. >> +Return nil on error. */) Renamed entry point to libmagic-file-internal since its meant to be of internal usage for a lisp wrapper, yet to be written. Should that be a new file BTW? > This doc string "needs work"(TM). Please use the doc string of > visited-file-name as an example. I worked on this > >> + (filename_or_buffer) >> + Lisp_Object filename_or_buffer; > > Using a `_' in an argument is un-Lisp'y (IMO). Ok. > You need to encode file names before you pass them to C APIs. Use > ENCODE_FILE to do that; see file-attributes for an example of how this > is done. Ok. >> + if (rvs == NULL) goto libmagic_error; >> + Lisp_Object file_mime = intern (rvs); > > You cannot declare variables in the middle of a block: Emacs does not > require a C99 compiler yet and need to support C90 or even older > compilers, which will reject this code. I'm habing trouble remembering not to use c99. Is there some convenient compiler flag to force lower versions? Fixed the errors I saw. > Is file_encoding supposed to be a valid encoding, one of those for > which Emacs has a coding-system? If so, perhaps you should make sure > you indeed return a valid coding-system or its alias, or otherwise > tell in the doc string that it's not guaranteed to be valid (so that > the caller should validate it before using). I described a bit more in the doc string. Ok? --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=filemagic3.patch diff --git a/configure.in b/configure.in index f4096db..49a3f15 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([libmagic],[don't compile with libmagic 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_libmagic}" != "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/fileio.c b/src/fileio.c index 3702d4c..cbb0461 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 +#endif + /* Non-zero means call move-file-to-trash in Fdelete_file or Fdelete_directory. */ int delete_by_moving_to_trash; @@ -2997,6 +3001,77 @@ DEFUN ("unix-sync", Funix_sync, Sunix_sync, 0, 0, "", #endif /* HAVE_SYNC */ +#ifdef HAVE_LIBMAGIC +DEFUN ("libmagic-file-internal", Flibmagic_file_internal, Slibmagic_file_internal, 1,1,0, + doc: /* Return (MIME-TYPE MIME-ENCODING DESCRIPTION) for +FILENAME-OR-BUFFER using libmagic. If FILENAME-OR-BUFFER is a file, +return information about the file. If FILENAME-OR-BUFFER is a buffer, +return information about the file of the buffer. MIME-TYPE and +MIME-ENCODING are the mime type and mime encoding as determined by +libmagic. DESCRIPTION is the human readable descripton offered by +libmagic for the file. + +The default libmagic database is used, and the quality of information +given depends on your version of that database. Often the mime type is +less exact than the description. + + */) + (filename_or_buffer) + Lisp_Object filename_or_buffer; +{ + CHECK_STRING_OR_BUFFER (filename_or_buffer); + magic_t cookie=NULL; + char* f = NULL; + const char* rvs; + Lisp_Object file_freetext; + Lisp_Object rv; + Lisp_Object file_mime; + Lisp_Object file_encoding; + + Lisp_Object filename, absname, encoded_absname; + struct gcpro gcpro1; + + GCPRO1 (f); + + if (STRINGP (filename_or_buffer)) + filename = filename_or_buffer; + if (BUFFERP (filename_or_buffer)) + filename = XBUFFER (filename_or_buffer)->filename; + absname = Fexpand_file_name (filename, current_buffer->directory); + f = SDATA(ENCODE_FILE (absname)); + + cookie = magic_open (MAGIC_ERROR); + if (cookie == NULL) goto libmagic_error; + magic_load (cookie, NULL); /* load default database */ + + magic_setflags (cookie, MAGIC_MIME_TYPE | MAGIC_ERROR); + rvs = magic_file (cookie, f); + if (rvs == NULL) goto libmagic_error; + file_mime = intern (rvs); + + magic_setflags (cookie, MAGIC_MIME_ENCODING | MAGIC_ERROR); + rvs=magic_file (cookie, f); + if (rvs == NULL) goto libmagic_error; + file_encoding = intern(rvs); + + magic_setflags (cookie, MAGIC_NONE | MAGIC_ERROR); + rvs=magic_file (cookie, f); + if (rvs == NULL) goto libmagic_error; + + file_freetext = build_string (rvs); + rv = Fcons (file_mime, Fcons (file_encoding, Fcons (file_freetext, Qnil))); + + magic_close (cookie); + UNGCPRO; + return rv; + libmagic_error: + if (cookie != NULL) magic_close (cookie); + report_file_error("Libmagic error",Qnil); + UNGCPRO; + 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 +5856,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 (&Slibmagic_file_internal); +#endif } /* arch-tag: 64ba3fd7-f844-4fb2-ba4b-427eb928786c --=-=-= -- Joakim Verona --=-=-=--