From c7b20a867b795d0ebfa6d5572c31a72f3caa926a Mon Sep 17 00:00:00 2001 From: dickmao Date: Sat, 11 Dec 2021 16:21:30 -0500 Subject: [PATCH] [sqlite3] Quis custodiet ipsos custodes * configure.ac (WIDE_EMACS_INT): Default off `--with-sqlite3`. (HAVE_SQLITE3): Opt out of sqlite.o. (SQLITE3_LIBS): Hew to autotools sop. * lisp/sqlite-mode.el (sqlite-execute, sqlite-more-p, sqlite-next, sqlite-columns, sqlite-finalize, sqlite-select, sqlite-open): Forward declare. * src/Makefile.in (SQLITE3_CFLAGS): Hew to autotools sop. * src/alloc.c (union emacs_align_type): Give a hoot, don't pollute. (cleanup_vector): Give a hoot, don't pollute. * src/data.c (syms_of_data): Fence. * src/emacs.c (main): Give a hoot, don't pollute. * src/lisp.h (GCALIGNED_STRUCT): Give a hoot, don't pollute. * src/print.c (print_vectorlike): Fence. * src/sqlite.c (Fsqlitep): What is SQLITE without the P? (Fsqlite_available_p): Unfence. (syms_of_sqlite): Unfence. * test/src/sqlite-tests.el (sqlite-mode, sqlite-execute, sqlite-close, sqlitep, sqlite-available-p, sqlite-finalize, sqlite-next, sqlite-more-p, sqlite-select, sqlite-open): Forward declare. --- configure.ac | 21 ++++++++------ lisp/sqlite-mode.el | 8 ++++++ src/Makefile.in | 12 ++++++-- src/alloc.c | 11 +++++++- src/data.c | 1 + src/emacs.c | 8 +++++- src/lisp.h | 36 ------------------------ src/print.c | 7 ++++- src/sqlite.c | 18 ++---------- src/sqlite.h | 59 ++++++++++++++++++++++++++++++++++++++++ test/Makefile.in | 1 - test/src/sqlite-tests.el | 11 ++++++++ 12 files changed, 127 insertions(+), 66 deletions(-) create mode 100644 src/sqlite.h diff --git a/configure.ac b/configure.ac index 0debc852141..a602e45cefe 100644 --- a/configure.ac +++ b/configure.ac @@ -448,7 +448,7 @@ AC_DEFUN OPTION_DEFAULT_ON([png],[don't compile with PNG image support]) OPTION_DEFAULT_ON([rsvg],[don't compile with SVG image support]) OPTION_DEFAULT_ON([webp],[don't compile with WebP image support]) -OPTION_DEFAULT_ON([sqlite3],[don't compile with sqlite3 support]) +OPTION_DEFAULT_OFF([sqlite3],[don't compile with sqlite3 support]) OPTION_DEFAULT_ON([lcms2],[don't compile with Little CMS support]) OPTION_DEFAULT_ON([libsystemd],[don't compile with libsystemd support]) OPTION_DEFAULT_ON([cairo],[don't compile with Cairo drawing]) @@ -2682,27 +2682,32 @@ AC_DEFUN fi fi -### Use -lsqlite3 if available, unless '--with-sqlite3=no' HAVE_SQLITE3=no +SQLITE3_OBJ= +### Use -lsqlite3 if available, unless '--with-sqlite3=no' if test "${with_sqlite3}" != "no"; then AC_CHECK_LIB(sqlite3, sqlite3_open_v2, HAVE_SQLITE3=yes, HAVE_SQLITE3=no) if test "$HAVE_SQLITE3" = "yes"; then SQLITE3_LIBS=-lsqlite3 - AC_SUBST(SQLITE3_LIBS) - LIBS="$SQLITE3_LIBS $LIBS" + SQLITE3_OBJ=sqlite.o AC_DEFINE(HAVE_SQLITE3, 1, [Define to 1 if you have the libsqlite3 library (-lsqlite).]) - # Windows loads libsqlite dynamically - if test "${opsys}" = "mingw32"; then - SQLITE3_LIBS= - fi AC_CHECK_LIB(sqlite3, sqlite3_load_extension, HAVE_SQLITE3_LOAD_EXTENSION=yes, HAVE_SQLITE3_LOAD_EXTENSION=no) if test "$HAVE_SQLITE3_LOAD_EXTENSION" = "yes"; then AC_DEFINE(HAVE_SQLITE3_LOAD_EXTENSION, 1, [Define to 1 if sqlite3 supports loading extensions.]) fi + # Windows loads libsqlite dynamically + if test "${opsys}" = "mingw32"; then + SQLITE3_LIBS= + fi fi fi +AC_SUBST(HAVE_SQLITE3) +AC_SUBST(SQLITE3_LIBS) +AC_SUBST(SQLITE3_CFLAGS) +AC_SUBST(SQLITE3_OBJ) + HAVE_IMAGEMAGICK=no if test "${HAVE_X11}" = "yes" || test "${HAVE_NS}" = "yes" || test "${HAVE_W32}" = "yes" || \ test "${HAVE_BE_APP}" = "yes"; then diff --git a/lisp/sqlite-mode.el b/lisp/sqlite-mode.el index 9306bd85dcd..823dfeb07d6 100644 --- a/lisp/sqlite-mode.el +++ b/lisp/sqlite-mode.el @@ -25,6 +25,14 @@ (require 'cl-lib) +(declare-function sqlite-execute "sqlite.c") +(declare-function sqlite-more-p "sqlite.c") +(declare-function sqlite-next "sqlite.c") +(declare-function sqlite-columns "sqlite.c") +(declare-function sqlite-finalize "sqlite.c") +(declare-function sqlite-select "sqlite.c") +(declare-function sqlite-open "sqlite.c") + (defvar-keymap sqlite-mode-map "g" #'sqlite-mode-list-tables "c" #'sqlite-mode-list-columns diff --git a/src/Makefile.in b/src/Makefile.in index 3a8445db2d4..85c10103c4e 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -238,6 +238,8 @@ IMAGEMAGICK_CFLAGS= LIBXML2_LIBS = @LIBXML2_LIBS@ LIBXML2_CFLAGS = @LIBXML2_CFLAGS@ +SQLITE3_CFLAGS = @SQLITE3_CFLAGS@ +SQLITE3_OBJ = @SQLITE3_OBJ@ SQLITE3_LIBS = @SQLITE3_LIBS@ GETADDRINFO_A_LIBS = @GETADDRINFO_A_LIBS@ @@ -330,6 +332,10 @@ JSON_LIBS = JSON_CFLAGS = @JSON_CFLAGS@ JSON_OBJ = @JSON_OBJ@ +SQLITE3_LIBS = @SQLITE3_LIBS@ +SQLITE3_CFLAGS = @SQLITE3_CFLAGS@ +SQLITE3_OBJ = @SQLITE3_OBJ@ + INTERVALS_H = dispextern.h intervals.h composite.h GETLOADAVG_LIBS = @GETLOADAVG_LIBS@ @@ -395,7 +401,7 @@ EMACS_CFLAGS= $(HARFBUZZ_CFLAGS) $(LIBOTF_CFLAGS) $(M17N_FLT_CFLAGS) $(DEPFLAGS) \ $(LIBSYSTEMD_CFLAGS) $(JSON_CFLAGS) \ $(LIBGNUTLS_CFLAGS) $(NOTIFY_CFLAGS) $(CAIRO_CFLAGS) \ - $(WERROR_CFLAGS) $(HAIKU_CFLAGS) + $(WERROR_CFLAGS) $(HAIKU_CFLAGS) $(SQLITE3_CFLAGS) ALL_CFLAGS = $(EMACS_CFLAGS) $(WARN_CFLAGS) $(CFLAGS) ALL_OBJC_CFLAGS = $(EMACS_CFLAGS) \ $(filter-out $(NON_OBJC_CFLAGS),$(WARN_CFLAGS)) $(CFLAGS) \ @@ -428,11 +434,11 @@ base_obj = doprnt.o intervals.o textprop.o composite.o xml.o lcms.o $(NOTIFY_OBJ) \ $(XWIDGETS_OBJ) \ profiler.o decompress.o \ - thread.o systhread.o sqlite.o \ + thread.o systhread.o \ $(if $(HYBRID_MALLOC),sheap.o) \ $(MSDOS_OBJ) $(MSDOS_X_OBJ) $(NS_OBJ) $(CYGWIN_OBJ) $(FONT_OBJ) \ $(W32_OBJ) $(WINDOW_SYSTEM_OBJ) $(XGSELOBJ) $(JSON_OBJ) \ - $(HAIKU_OBJ) + $(HAIKU_OBJ) $(SQLITE3_OBJ) doc_obj = $(base_obj) $(NS_OBJC_OBJ) obj = $(doc_obj) $(HAIKU_CXX_OBJ) diff --git a/src/alloc.c b/src/alloc.c index 9f52a414d68..a9ba9885fd8 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -50,6 +50,10 @@ Copyright (C) 1985-1986, 1988, 1993-1995, 1997-2021 Free Software #include TERM_HEADER #endif /* HAVE_WINDOW_SYSTEM */ +#ifdef HAVE_SQLITE3 +#include "sqlite.h" +#endif + #include #include #include /* For backtrace. */ @@ -125,7 +129,6 @@ Copyright (C) 1985-1986, 1988, 1993-1995, 1997-2021 Free Software struct Lisp_Overlay Lisp_Overlay; struct Lisp_Sub_Char_Table Lisp_Sub_Char_Table; struct Lisp_Subr Lisp_Subr; - struct Lisp_Sqlite Lisp_Sqlite; struct Lisp_User_Ptr Lisp_User_Ptr; struct Lisp_Vector Lisp_Vector; struct terminal terminal; @@ -3181,6 +3184,12 @@ cleanup_vector (struct Lisp_Vector *vector) } } #endif +#ifdef HAVE_SQLITE3 + else if (PSEUDOVECTOR_TYPEP (&vector->header, PVEC_SQLITE)) + { + /* clean s___ up. To be implemented. */ + } +#endif } /* Reclaim space used by unmarked vectors. */ diff --git a/src/data.c b/src/data.c index f07667b0003..81ae2f412ad 100644 --- a/src/data.c +++ b/src/data.c @@ -4069,6 +4069,7 @@ #define PUT_ERROR(sym, tail, msg) \ DEFSYM (Qterminal, "terminal"); DEFSYM (Qxwidget, "xwidget"); DEFSYM (Qxwidget_view, "xwidget-view"); + DEFSYM (Qsqlite, "sqlite"); DEFSYM (Qdefun, "defun"); diff --git a/src/emacs.c b/src/emacs.c index 3fc055aed92..05441b06825 100644 --- a/src/emacs.c +++ b/src/emacs.c @@ -81,6 +81,10 @@ #define MAIN_PROGRAM #include TERM_HEADER #endif /* HAVE_WINDOW_SYSTEM */ +#ifdef HAVE_SQLITE3 +#include "sqlite.h" +#endif /* HAVE_SQLITE3 */ + #include "bignum.h" #include "intervals.h" #include "character.h" @@ -2183,8 +2187,10 @@ main (int argc, char **argv) #endif syms_of_window (); syms_of_xdisp (); - syms_of_sqlite (); syms_of_font (); +#ifdef HAVE_SQLITE3 + syms_of_sqlite (); +#endif #ifdef HAVE_WINDOW_SYSTEM syms_of_fringe (); syms_of_image (); diff --git a/src/lisp.h b/src/lisp.h index 92ab05b4228..aa48b218d36 100644 --- a/src/lisp.h +++ b/src/lisp.h @@ -2571,17 +2571,6 @@ xmint_pointer (Lisp_Object a) return XUNTAG (a, Lisp_Vectorlike, struct Lisp_Misc_Ptr)->pointer; } -struct Lisp_Sqlite -{ - union vectorlike_header header; - void *db; - void *stmt; - char *name; - void (*finalizer) (void *); - bool eof; - bool is_statement; -} GCALIGNED_STRUCT; - struct Lisp_User_Ptr { union vectorlike_header header; @@ -2659,31 +2648,6 @@ XUSER_PTR (Lisp_Object a) return XUNTAG (a, Lisp_Vectorlike, struct Lisp_User_Ptr); } -INLINE bool -SQLITEP (Lisp_Object x) -{ - return PSEUDOVECTORP (x, PVEC_SQLITE); -} - -INLINE bool -SQLITE (Lisp_Object a) -{ - return PSEUDOVECTORP (a, PVEC_SQLITE); -} - -INLINE void -CHECK_SQLITE (Lisp_Object x) -{ - CHECK_TYPE (SQLITE (x), Qsqlitep, x); -} - -INLINE struct Lisp_Sqlite * -XSQLITE (Lisp_Object a) -{ - eassert (SQLITEP (a)); - return XUNTAG (a, Lisp_Vectorlike, struct Lisp_Sqlite); -} - INLINE bool BIGNUMP (Lisp_Object x) { diff --git a/src/print.c b/src/print.c index 214f1d12c11..f06ceb308a6 100644 --- a/src/print.c +++ b/src/print.c @@ -40,6 +40,10 @@ Copyright (C) 1985-1986, 1988, 1993-1995, 1997-2021 Free Software #include #include +#ifdef HAVE_SQLITE3 +#include +#endif + #if IEEE_FLOATING_POINT # include #endif @@ -1875,6 +1879,7 @@ print_vectorlike (Lisp_Object obj, Lisp_Object printcharfun, bool escapeflag, } break; #endif +#ifdef HAVE_SQLITE3 case PVEC_SQLITE: { print_c_string ("#', printcharfun); } break; - +#endif default: emacs_abort (); } diff --git a/src/sqlite.c b/src/sqlite.c index d92dcf723c9..f9b57c8d387 100644 --- a/src/sqlite.c +++ b/src/sqlite.c @@ -25,8 +25,7 @@ Copyright (C) 2021 Free Software Foundation, Inc. #include #include "lisp.h" #include "coding.h" - -#ifdef HAVE_SQLITE3 +#include "sqlite.h" #include @@ -644,24 +643,17 @@ DEFUN ("sqlite-finalize", Fsqlite_finalize, Ssqlite_finalize, 1, 1, 0, return Qt; } -#endif /* HAVE_SQLITE3 */ - DEFUN ("sqlitep", Fsqlitep, Ssqlitep, 1, 1, 0, doc: /* Say whether OBJECT is an SQlite object. */) (Lisp_Object object) { -#ifdef HAVE_SQLITE3 - return SQLITE (object)? Qt: Qnil; -#else - return Qnil; -#endif + return SQLITEP (object) ? Qt: Qnil; } DEFUN ("sqlite-available-p", Fsqlite_available_p, Ssqlite_available_p, 0, 0, 0, doc: /* Return t if sqlite3 support is available in this instance of Emacs.*/) (void) { -#ifdef HAVE_SQLITE3 # ifdef WINDOWSNT Lisp_Object found = Fassq (Qsqlite3, Vlibrary_cache); if (CONSP (found)) @@ -671,15 +663,11 @@ DEFUN ("sqlite-available-p", Fsqlite_available_p, Ssqlite_available_p, 0, 0, 0, # else return Qt; #endif -#else - return Qnil; -#endif } void syms_of_sqlite (void) { -#ifdef HAVE_SQLITE3 defsubr (&Ssqlite_open); defsubr (&Ssqlite_close); defsubr (&Ssqlite_execute); @@ -696,7 +684,7 @@ syms_of_sqlite (void) defsubr (&Ssqlite_finalize); DEFSYM (Qset, "set"); DEFSYM (Qfull, "full"); -#endif + defsubr (&Ssqlitep); DEFSYM (Qsqlitep, "sqlitep"); defsubr (&Ssqlite_available_p); diff --git a/src/sqlite.h b/src/sqlite.h new file mode 100644 index 00000000000..0d26f0b7b8f --- /dev/null +++ b/src/sqlite.h @@ -0,0 +1,59 @@ +/* Header file for the sqlite3 integration. + +Copyright (C) 2021 Free Software Foundation, Inc. + +This file is NOT part of GNU Emacs. + +GNU Emacs is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or (at +your option) any later version. + +GNU Emacs is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Emacs. If not, see . */ + +#ifndef EMACS_SQLITE_H +#define EMACS_SQLITE_H + +#include "lisp.h" + +INLINE_HEADER_BEGIN + +struct Lisp_Sqlite +{ + union vectorlike_header header; + void *db; + void *stmt; + char *name; + void (*finalizer) (void *); + bool eof; + bool is_statement; +}; + +INLINE bool +SQLITEP (Lisp_Object x) +{ + return PSEUDOVECTORP (x, PVEC_SQLITE); +} + +INLINE struct Lisp_Sqlite * +XSQLITE (Lisp_Object a) +{ + eassert (SQLITEP (a)); + return XUNTAG (a, Lisp_Vectorlike, struct Lisp_Sqlite); +} + +INLINE void +CHECK_SQLITE (Lisp_Object sql) +{ + CHECK_TYPE (SQLITEP (sql), Qsqlitep, sql); +} + +INLINE_HEADER_END + +#endif /* EMACS_SQLITE_H */ diff --git a/test/Makefile.in b/test/Makefile.in index f2c49584e7f..f3f7855b439 100644 --- a/test/Makefile.in +++ b/test/Makefile.in @@ -48,7 +48,6 @@ SO = SEPCHAR = @SEPCHAR@ HAVE_NATIVE_COMP = @HAVE_NATIVE_COMP@ - REPLACE_FREE = @REPLACE_FREE@ -include ${top_builddir}/src/verbose.mk diff --git a/test/src/sqlite-tests.el b/test/src/sqlite-tests.el index 7ccea1c2a54..58949b2ebc6 100644 --- a/test/src/sqlite-tests.el +++ b/test/src/sqlite-tests.el @@ -25,6 +25,17 @@ (require 'ert) (require 'ert-x) +(require 'sqlite-mode) + +(declare-function sqlite-execute "sqlite.c") +(declare-function sqlite-close "sqlite.c") +(declare-function sqlitep "sqlite.c") +(declare-function sqlite-available-p "sqlite.c") +(declare-function sqlite-finalize "sqlite.c") +(declare-function sqlite-next "sqlite.c") +(declare-function sqlite-more-p "sqlite.c") +(declare-function sqlite-select "sqlite.c") +(declare-function sqlite-open "sqlite.c") (ert-deftest sqlite-select () (skip-unless (sqlite-available-p)) -- 2.26.2