From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Eli Zaretskii Newsgroups: gmane.emacs.devel Subject: Re: using libmagic in Emacs? Date: Fri, 21 Aug 2009 14:01:35 +0300 Message-ID: <83k50xh1m8.fsf@gnu.org> References: Reply-To: Eli Zaretskii NNTP-Posting-Host: lo.gmane.org X-Trace: ger.gmane.org 1250852676 30964 80.91.229.12 (21 Aug 2009 11:04:36 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 21 Aug 2009 11:04:36 +0000 (UTC) Cc: schwab@linux-m68k.org, monnier@iro.umontreal.ca, emacs-devel@gnu.org To: joakim@verona.se Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Fri Aug 21 13:04:25 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 1MeRuz-0005cJ-Bu for ged-emacs-devel@m.gmane.org; Fri, 21 Aug 2009 13:04:25 +0200 Original-Received: from localhost ([127.0.0.1]:42321 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MeRuy-0004UD-TI for ged-emacs-devel@m.gmane.org; Fri, 21 Aug 2009 07:04:24 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1MeRuD-00043F-KF for emacs-devel@gnu.org; Fri, 21 Aug 2009 07:03:37 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1MeRuC-00041C-18 for emacs-devel@gnu.org; Fri, 21 Aug 2009 07:03:36 -0400 Original-Received: from [199.232.76.173] (port=60139 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MeRuB-00040n-Pc for emacs-devel@gnu.org; Fri, 21 Aug 2009 07:03:35 -0400 Original-Received: from mtaout4.012.net.il ([84.95.2.10]:10513 helo=mtaout3.012.net.il) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1MeRuB-0001qe-1c for emacs-devel@gnu.org; Fri, 21 Aug 2009 07:03:35 -0400 Original-Received: from conversion-daemon.i_mtaout3.012.net.il by i_mtaout3.012.net.il (HyperSendmail v2004.12) id <0KOQ00I003U1EV00@i_mtaout3.012.net.il> for emacs-devel@gnu.org; Fri, 21 Aug 2009 14:03:33 +0300 (IDT) Original-Received: from HOME-C4E4A596F7 ([84.228.180.85]) by i_mtaout3.012.net.il (HyperSendmail v2004.12) with ESMTPA id <0KOQ00G0X41WVLA0@i_mtaout3.012.net.il>; Fri, 21 Aug 2009 14:03:33 +0300 (IDT) In-reply-to: X-012-Sender: halo1@inter.net.il X-detected-operating-system: by monty-python.gnu.org: Solaris 9.1 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:114471 Archived-At: > From: joakim@verona.se > Date: Fri, 21 Aug 2009 11:55:45 +0200 > Cc: Stefan Monnier , > Emacs Development > > Is autoheader supposed to generate config.in? Yes. > When does that happen? Either run autoheader by hand, or run autoreconf (which AFAIK is supposed to run autoheader as well). > +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. */) This doc string "needs work"(TM). Please use the doc string of visited-file-name as an example. > + (filename_or_buffer) > + Lisp_Object filename_or_buffer; Using a `_' in an argument is un-Lisp'y (IMO). > +{ > + CHECK_STRING_OR_BUFFER (filename_or_buffer); > + magic_t cookie=NULL; > + char* f = NULL; > + const char* rvs; > + > + if (STRINGP (filename_or_buffer)) > + f = SDATA (filename_or_buffer); > + if (BUFFERP (filename_or_buffer)) > + f = SDATA (XBUFFER (filename_or_buffer)->filename); > + 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); 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. > + 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. > + magic_setflags (cookie, MAGIC_MIME_ENCODING | MAGIC_ERROR); > + rvs=magic_file (cookie, f); > + if (rvs == NULL) goto libmagic_error; > + Lisp_Object file_encoding = intern(rvs); 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).