From 73d108fa6e42081d9a3698199b8ba8915f294b84 Mon Sep 17 00:00:00 2001 From: su8 Date: Sun, 8 Apr 2018 14:41:40 +0200 Subject: [PATCH] Add sqlite support --- README.md | 7 ++- configure.ac | 1 + m4/sqlite.m4 | 96 +++++++++++++++++++++++++++++++++++++++ src/Makefail.skel | 4 +- src/include/functions_constants.h | 4 +- src/options.c | 11 +++++ src/prototypes/sqlite.h | 27 +++++++++++ src/sqlite.c | 76 +++++++++++++++++++++++++++++++ 8 files changed, 221 insertions(+), 5 deletions(-) create mode 100644 m4/sqlite.m4 create mode 100644 src/prototypes/sqlite.h create mode 100644 src/sqlite.c diff --git a/README.md b/README.md index 3b34944..6aac99b 100644 --- a/README.md +++ b/README.md @@ -117,6 +117,7 @@ The order of supplied options will dictate how, where and what system informatio | | --cpp | Extend pinkybar with your cpp, learn more from the Opt-in section. | | | --slang | Extend pinkybar with your slang, learn more from the Opt-in section. | | | --tcl | Extend pinkybar with your tcl, learn more from the Opt-in section. | +| | --sqlite | Connect to sqlite db and perform SELECT operation, [argument e.g: "SELECT * from COMPANY where ID=1"] | | -q | --weather | Show the temperature outside [argument - London,uk] | | -U | --uptime | The system uptime | | -w | --loadavg | The system average load for past 1, 5 and 15 minutes | @@ -134,7 +135,7 @@ The order of supplied options will dictate how, where and what system informatio | -B | --ipmask | The NIC subnet mask [argument - eth0] | | -D | --ipcast | The NIC broadcast address [argument - eth0] | | -E | --iplookup | Mini website IP lookup [website argument - google.com] | -| | --pingtime | Perform a GET request and measure the round trip. [website argument https://wordpress.com] | +| | --pingtime | Perform a GET request and measure the round trip time. [website argument https://wordpress.com] | Be aware of the options that mention **Uses assembly** are tested only on AMD and Intel CPUs (starting from pentium 4 onwards). @@ -223,7 +224,9 @@ It's up to you to decide which features suit you best. | github\_token=foo | | [Generate token for specific scope](https://github.com/settings/tokens/new?scopes=notifications&description=pinky-bar), must be combined **--with-github** | | --with-reddit | --without-reddit | Query reddit and number all unread notifications | | reddit\_feed=foo | | ![](img/reddit.png)[copy the JSON link and paste it to this variable](https://www.reddit.com/prefs/feeds/), must be combined **--with-reddit** | -| --with-pingtime | --without-pingtime | Perform a GET request and measure the round trip | +| --with-pingtime | --without-pingtime | Perform a GET request and measure the round trip time | +| --with-sqlite | --without-sqlite | Connect to sqlite db and perform SELECT operation | +| sqlite\_db=foo | | The place where your db is located | | --prefix=/tmp | | The directory where the program will be installed | | mobo\_sensor='dev.aibs.0' | | FreeBSD motherboard sensor module name to use in the sysctl calls. Read the FreeBSD installation below | | cpu\_sensor='dev.cpu.0.temperature' | | FreeBSD cpu temperature module name to use in the sysctl calls . Read the FreeBSD installation below | diff --git a/configure.ac b/configure.ac index 2026e85..38c6c1d 100644 --- a/configure.ac +++ b/configure.ac @@ -52,6 +52,7 @@ TEST_GO TEST_CPP TEST_SLANG TEST_TCL +TEST_SQLITE AC_CONFIG_FILES([ Makefile diff --git a/m4/sqlite.m4 b/m4/sqlite.m4 new file mode 100644 index 0000000..b8c1beb --- /dev/null +++ b/m4/sqlite.m4 @@ -0,0 +1,96 @@ +dnl 08/03/2016 + +dnl This program is free software; you can redistribute it and/or modify +dnl it under the terms of the GNU General Public License as published by +dnl the Free Software Foundation; either version 2 of the License, or +dnl (at your option) any later version. + +dnl This program is distributed in the hope that it will be useful, +dnl but WITHOUT ANY WARRANTY; without even the implied warranty of +dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +dnl GNU General Public License for more details. + +dnl You should have received a copy of the GNU General Public License +dnl along with this program; if not, write to the Free Software +dnl Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, +dnl MA 02110-1301, USA. + + +dnl TEST_SQLITE() function in configure.ac +dnl +dnl Substitute the linker flags -lsqlite3 to the +dnl the variable 'SQLITE_LIBS' if the user enabled +dnl the --with-sqlite switch +AC_DEFUN([TEST_SQLITE],[ + SQLITE_LIBS="" + SQLITE_DB="" + WITH_SQLITE=0 + + AC_ARG_WITH([sqlite], + AS_HELP_STRING([--with-sqlite], + [Connect to sqlite db and perform SELECT operation]), + [], + [with_sqlite=no] + ) + + AC_ARG_VAR(sqlite_db, [sqlite db]) + if [[ ! -z "${sqlite_db}" ]] + then + SQLITE_DB=\""${sqlite_db}"\" + AC_DEFINE_UNQUOTED([SQLITE_DB],[$SQLITE_DB],[sqlite db]) + fi + + AS_IF([test "x$with_sqlite" = "xyes"], [ + AC_CHECK_HEADERS([sqlite3.h], [ + SQLITE_LIBS="-lsqlite3" + ],[ + ERR_MUST_INSTALL([sqlite]) + ]) + WITH_SQLITE=1 + + m4_foreach([LiB], [ + sqlite3_open_v2 , + sqlite3_exec , + sqlite3_errmsg , + sqlite3_free , + sqlite3_close_v2 + ],[ + AC_CHECK_LIB(sqlite3,LiB,[],[ + MISSING_FUNC() + ]) + ]) + ]) + + AC_SUBST(SQLITE_LIBS) + AC_DEFINE_UNQUOTED([WITH_SQLITE],[$WITH_SQLITE],[Connect to sqlite db and perform SELECT operation]) + + AS_IF([test "x$with_sqlite" = "xyes"], [ + AC_LINK_IFELSE([ + AC_LANG_SOURCE([[ + #include + #include + #include + int main(void) { + sqlite3 *db; + char *zErrMsg = 0; + int rc; + + rc = sqlite3_open("test.db", &db); + + if( rc ) { + fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); + return(-1); + } else { + fprintf(stderr, "Opened database successfully\n"); + } + sqlite3_close(db); + return 0; + } + ]]) + ],[],[ + LINK_FAILED([sqlite3]) + ] + ) + ]) + +]) diff --git a/src/Makefail.skel b/src/Makefail.skel index 0f94c64..f51e0d0 100644 --- a/src/Makefail.skel +++ b/src/Makefail.skel @@ -3,7 +3,7 @@ AM_CFLAGS = {amCF} \ bin_PROGRAMS = pinkybar -pinkybar_LDADD = $(X_LIBS) $(ALSA_LIBS) $(MPD_LIBS) $(PCI_LIBS) $(DVD_LIBS) $(SENSORS_LIBS) $(CURL_LIBS) $(LIBNL_LZ) $(PERL_LZ) $(LUA_LIBS) $(PYTHON_LZ) $(RUBY_LZ) $(R_LZ) $(ECL_LIBS) $(OCAML_LZ) $(RUST_LIBS) $(GO_LIBS) $(SLANG_LIBS) $(TCL_LIBS) {bzdlibs} +pinkybar_LDADD = $(X_LIBS) $(ALSA_LIBS) $(MPD_LIBS) $(PCI_LIBS) $(DVD_LIBS) $(SENSORS_LIBS) $(CURL_LIBS) $(LIBNL_LZ) $(PERL_LZ) $(LUA_LIBS) $(PYTHON_LZ) $(RUBY_LZ) $(R_LZ) $(ECL_LIBS) $(OCAML_LZ) $(RUST_LIBS) $(GO_LIBS) $(SLANG_LIBS) $(TCL_LIBS) $(SQLITE_LIBS) {bzdlibs} pinkybar_SOURCES = main.c \ common.c \ cpu.c \ @@ -17,7 +17,9 @@ pinkybar_SOURCES = main.c \ extend.c \ x11.c \ mail.c \ + sqlite.c \ prototypes/common.h \ + prototypes/sqlite.h \ prototypes/mail.h \ prototypes/curl.h \ prototypes/x11.h \ diff --git a/src/include/functions_constants.h b/src/include/functions_constants.h index 0dd2a26..30334a8 100644 --- a/src/include/functions_constants.h +++ b/src/include/functions_constants.h @@ -35,10 +35,10 @@ #define SCAN_UINTX "%"SCNxMAX /* hex */ /* stay away from va_list */ -#define FILL_ARR(x, z, ...) (snprintf(x, VLA, z, __VA_ARGS__)) +#define FILL_ARR(x, z, ...) (snprintf(x, VLA-1, z, __VA_ARGS__)) #define FILL_UINT_ARR(x, z) (FILL_ARR(x, FMT_UINT, z)) #define FILL_STR_ARR(x, z, ...) (FILL_ARR(z, (1 == x ? "%s" : "%s %s"), __VA_ARGS__)) -#define GLUE2(x, z, ...) (x+=snprintf(x, VLA, z, __VA_ARGS__)) +#define GLUE2(x, z, ...) (x+=snprintf(x, VLA-1, z, __VA_ARGS__)) /* temperature sensors */ #define HWMON_DIR "/sys/class/hwmon/hwmon0" diff --git a/src/options.c b/src/options.c index fd4e9d3..509b959 100644 --- a/src/options.c +++ b/src/options.c @@ -33,6 +33,7 @@ #include "prototypes/extend.h" #include "prototypes/x11.h" #include "prototypes/curl.h" +#include "prototypes/sqlite.h" /* Because we ran out of a-z A-Z options, * only long ones will be supported from now on. @@ -68,6 +69,7 @@ enum { REDDIT, NOTES, PING, + SQLITEE, BULLSHIFT }; const char *argp_program_version = PACKAGE_STRING; @@ -121,6 +123,10 @@ static const struct argp_option options[] = { { .name = "password", .key = PASSWORD, .doc = "Generate 20 character long password." }, { .name = "notes", .key = NOTES, .arg = "Do Stuff", .doc = "Static string that's displayed to you, could be a TODO or notes." }, +#if WITH_SQLITE == 1 + { .name = "sqlite", .key = SQLITEE, .arg = "string", .doc = "Connect to sqlite db and perform SELECT operation." }, +#endif /* WITH_SQLITE */ + #if WITH_PING == 1 { .name = "pingtime", .key = PING, .arg = "url", .doc = "Perform a GET request and measure the round trip." }, #endif /* WITH_PING */ @@ -343,6 +349,11 @@ parse_opt(int key, char *arg, struct argp_state *state) { NEW_ARG_LABEL(NOTES, char notes[VLA], notes, FMT_KERN); +#if WITH_SQLITE == 1 + NEW_ARG_LABEL(SQLITEE, char sqlite[VLA], sqlite, FMT_KERN); +#endif /* WITH_SQLITE */ + + #if WITH_PING == 1 NEW_ARG_LABEL(PING, char ping[VLA], ping, FMT_KERN); #endif /* WITH_PING */ diff --git a/src/prototypes/sqlite.h b/src/prototypes/sqlite.h new file mode 100644 index 0000000..167fd50 --- /dev/null +++ b/src/prototypes/sqlite.h @@ -0,0 +1,27 @@ +/* + 04/08/2018 + + This program 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 2 of the License, or + (at your option) any later version. + + This program 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 this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + MA 02110-1301, USA. +*/ + +#ifndef SQLITEEE_H_ +#define SQLITEEE_H_ + +#if WITH_SQLITE == 1 +void get_sqlite(char *, char *); +#endif /* WITH_SQLITE */ + +#endif /* SQLITEEE_H_ */ diff --git a/src/sqlite.c b/src/sqlite.c new file mode 100644 index 0000000..8f1472d --- /dev/null +++ b/src/sqlite.c @@ -0,0 +1,76 @@ +/* + 04/08/2018 + + This program 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 2 of the License, or + (at your option) any later version. + + This program 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 this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + MA 02110-1301, USA. +*/ + +#include "config.h" /* Auto-generated */ + +#if WITH_SQLITE == 1 +# include +# include "include/headers.h" +#endif /* WITH_SQLITE */ + +#include "prototypes/sqlite.h" + +#if WITH_SQLITE == 1 +static int read_sqlite_data_cb(void *, int, char **, char **); + +static int +read_sqlite_data_cb(void *str1, int count, char **data, char **coloums) { + size_t len = ((size_t)count * VLA); + char *buf = (char *)malloc(len); + char *all = buf; + int x = 0; + + if (NULL == buf) { + return EXIT_FAILURE; + } + + for (; x < count; x++) { + GLUE2(all, "%s %s ", coloums[x], (data[x] ? data[x] : "NULL")); + } + *(--all) = '\0'; + + FILL_STR_ARR(1, str1, buf); + free(buf); + return EXIT_SUCCESS; +} + + +/* + Based on: + https://www.tutorialspoint.com/sqlite/sqlite_c_cpp.htm +*/ +void +get_sqlite(char *str1, char *str2) { + sqlite3 *db = NULL; + char *err = "empty"; + + if ((sqlite3_open_v2(SQLITE_DB, &db, SQLITE_OPEN_READONLY, NULL))) { + exit_with_err(ERR, sqlite3_errmsg(db)); + } + if (SQLITE_OK != (sqlite3_exec(db, str2, read_sqlite_data_cb, str1, &err))) { + exit_with_err(ERR, err); + } + + sqlite3_free(err); + sqlite3_close_v2(db); +} + +#else +char *l33t3; +#endif /* WITH_SQLITE */ -- 2.11.0