From: joakim@verona.se
To: Eli Zaretskii <eliz@gnu.org>
Cc: schwab@linux-m68k.org, monnier@iro.umontreal.ca, emacs-devel@gnu.org
Subject: Re: using libmagic in Emacs?
Date: Fri, 21 Aug 2009 19:38:15 +0200 [thread overview]
Message-ID: <m3ljldcbjs.fsf@verona.se> (raw)
In-Reply-To: <83k50xh1m8.fsf@gnu.org> (Eli Zaretskii's message of "Fri, 21 Aug 2009 14:01:35 +0300")
[-- Attachment #1: Type: text/plain, Size: 1651 bytes --]
New filemagic patch mostly fixing Eli:s concerns.
Eli Zaretskii <eliz@gnu.org> 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?
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: filemagic3.patch --]
[-- Type: text/x-patch, Size: 5892 bytes --]
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 <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,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
[-- Attachment #3: Type: text/plain, Size: 19 bytes --]
--
Joakim Verona
next prev parent reply other threads:[~2009-08-21 17:38 UTC|newest]
Thread overview: 119+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-08-18 18:35 using libmagic in Emacs? joakim
2009-08-18 19:23 ` Stefan Monnier
2009-08-18 20:01 ` Chong Yidong
2009-08-18 20:35 ` joakim
2009-08-18 21:11 ` Stefan Monnier
2009-08-19 2:58 ` Eli Zaretskii
2009-08-19 3:21 ` Stefan Monnier
2009-08-19 13:47 ` Chong Yidong
2009-08-19 15:57 ` joakim
2009-08-19 19:46 ` next bugfix release? [was: Re: using libmagic in Emacs?] Dan Nicolaescu
2009-08-19 21:06 ` next bugfix release? Chong Yidong
2009-08-19 21:53 ` Dan Nicolaescu
2009-08-19 22:56 ` Alan Mackenzie
2009-08-19 23:16 ` Nick Roberts
2009-08-20 9:02 ` Lennart Borgman
2009-08-20 11:19 ` Eric M. Ludlam
2009-08-20 15:13 ` Alan Mackenzie
2009-08-20 15:47 ` Lennart Borgman
2009-08-19 19:05 ` installing features on trunk (was: using libmagic in Emacs?) Eli Zaretskii
2009-08-21 18:59 ` Bidi support Stefan Monnier
2009-08-21 20:44 ` Eli Zaretskii
2009-08-22 3:39 ` Stefan Monnier
2009-08-22 8:18 ` Jason Rumney
2009-08-22 5:39 ` Stephen J. Turnbull
2009-08-22 7:31 ` Eli Zaretskii
2009-08-24 1:45 ` Kenichi Handa
2009-08-24 3:12 ` Eli Zaretskii
2009-08-24 7:17 ` Kenichi Handa
2009-08-24 3:25 ` Stephen J. Turnbull
2009-08-19 0:57 ` using libmagic in Emacs? Juri Linkov
2009-08-20 3:42 ` Richard Stallman
2009-08-22 23:36 ` Juri Linkov
2009-08-24 0:07 ` Richard Stallman
2009-08-24 0:17 ` Juri Linkov
2009-08-24 7:33 ` joakim
2009-08-25 2:08 ` Richard Stallman
2009-08-25 2:19 ` Miles Bader
2009-08-25 5:09 ` joakim
2009-08-25 13:27 ` James Cloos
2009-08-25 21:41 ` Thien-Thi Nguyen
2009-08-25 17:36 ` Stefan Monnier
2009-08-25 20:37 ` Juri Linkov
2009-08-29 23:19 ` Juri Linkov
2009-08-30 3:09 ` Eli Zaretskii
2009-08-30 20:54 ` Juri Linkov
2009-08-31 2:49 ` Eli Zaretskii
2009-08-31 16:17 ` Juri Linkov
2009-08-31 17:58 ` Eli Zaretskii
2009-09-01 12:16 ` Richard Stallman
2009-09-01 16:12 ` Stefan Monnier
2009-09-01 21:20 ` Richard Stallman
2009-09-03 19:42 ` Stefan Monnier
2009-09-04 7:52 ` Richard Stallman
2009-08-31 22:21 ` Richard Stallman
2009-08-31 3:33 ` Richard Stallman
2009-08-31 15:03 ` Chong Yidong
2009-08-31 16:19 ` Juri Linkov
2009-08-31 23:47 ` Stefan Monnier
2009-09-01 3:16 ` Eli Zaretskii
2009-09-01 5:37 ` Stefan Monnier
2009-09-01 12:16 ` Richard Stallman
2009-08-25 20:36 ` Juri Linkov
2009-08-19 22:49 ` joakim
2009-08-19 23:20 ` Dan Nicolaescu
2009-08-20 1:03 ` Stephen J. Turnbull
2009-08-20 3:12 ` Eli Zaretskii
2009-08-20 4:50 ` Stephen J. Turnbull
2009-08-20 18:20 ` Eli Zaretskii
2009-08-21 0:19 ` Stephen J. Turnbull
2009-08-20 18:32 ` Richard Stallman
2009-08-21 19:10 ` Stefan Monnier
2009-08-22 5:03 ` Stephen J. Turnbull
2009-08-23 1:03 ` Stefan Monnier
2009-08-20 13:57 ` Stefan Monnier
2009-08-20 19:19 ` joakim
2009-08-20 22:08 ` Andreas Schwab
2009-08-21 9:55 ` joakim
2009-08-21 11:01 ` Eli Zaretskii
2009-08-21 17:38 ` joakim [this message]
2009-08-21 17:46 ` Rupert Swarbrick
2009-08-21 18:31 ` Andreas Schwab
2009-08-21 19:13 ` Drew Adams
2009-08-21 18:42 ` Eli Zaretskii
2009-08-21 21:48 ` joakim
2009-08-21 22:46 ` Andreas Schwab
2009-08-22 20:18 ` joakim
2009-08-22 23:13 ` Ken Raeburn
2009-08-23 23:38 ` joakim
2009-08-24 3:05 ` Eli Zaretskii
2009-08-24 12:30 ` joakim
2009-08-23 3:24 ` Eli Zaretskii
2009-08-21 19:18 ` Stefan Monnier
2009-08-21 13:19 ` Andreas Schwab
2009-08-20 18:32 ` Richard Stallman
2009-08-20 20:27 ` Reiner Steib
2009-08-21 14:08 ` Richard Stallman
2009-08-21 19:16 ` Stefan Monnier
2009-08-28 0:27 ` Language identification (was: using libmagic in Emacs) Juri Linkov
2009-08-28 4:58 ` Language identification Stefan Monnier
2009-08-28 9:00 ` Stephen J. Turnbull
2009-08-28 14:56 ` Stefan Monnier
2009-08-29 4:11 ` Stephen J. Turnbull
2009-08-29 14:21 ` Chong Yidong
2009-08-29 0:46 ` Richard Stallman
2009-08-29 4:13 ` Stephen J. Turnbull
2009-08-29 15:28 ` Stefan Monnier
2009-08-29 16:27 ` Stephen J. Turnbull
2009-08-28 19:16 ` Juri Linkov
2009-08-29 1:12 ` Stefan Monnier
2009-08-30 16:01 ` Richard Stallman
2009-08-29 20:20 ` Richard Stallman
2009-08-29 22:48 ` Juri Linkov
2009-08-31 3:32 ` Richard Stallman
2009-08-31 8:42 ` David Kastrup
2009-08-31 8:59 ` Jan D.
2009-08-31 3:33 ` Richard Stallman
2009-08-28 6:45 ` Alex Ott
2009-08-28 6:46 ` Alex Ott
2009-08-28 19:08 ` Juri Linkov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=m3ljldcbjs.fsf@verona.se \
--to=joakim@verona.se \
--cc=eliz@gnu.org \
--cc=emacs-devel@gnu.org \
--cc=monnier@iro.umontreal.ca \
--cc=schwab@linux-m68k.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).