diff -Nur bash-3.0/Makefile.in bash-3.0-builtin-sql-1.0.1/Makefile.in --- bash-3.0/Makefile.in 2004-03-17 14:34:39.000000000 +0100 +++ bash-3.0-builtin-sql-1.0.1/Makefile.in 2006-08-02 23:42:39.000000000 +0200 @@ -125,10 +125,10 @@ LOCALE_DEFS = -DLOCALEDIR='"$(localedir)"' -DPACKAGE='"$(PACKAGE)"' -LOCAL_LIBS = @LOCAL_LIBS@ +LOCAL_LIBS = @LOCAL_LIBS@ @MYSQL_LIB@ LIBS = $(BUILTINS_LIB) $(LIBRARIES) @LIBS@ STATIC_LD = @STATIC_LD@ -LOCAL_LDFLAGS = @LOCAL_LDFLAGS@ +LOCAL_LDFLAGS = @LOCAL_LDFLAGS@ @MYSQL_LDFLAGS@ SYSTEM_FLAGS = -DPROGRAM='"$(Program)"' -DCONF_HOSTTYPE='"$(Machine)"' -DCONF_OSTYPE='"$(OS)"' -DCONF_MACHTYPE='"$(MACHTYPE)"' -DCONF_VENDOR='"$(VENDOR)"' $(LOCALE_DEFS) diff -Nur bash-3.0/array.c bash-3.0-builtin-sql-1.0.1/array.c --- bash-3.0/array.c 2004-05-06 14:24:13.000000000 +0200 +++ bash-3.0-builtin-sql-1.0.1/array.c 2006-08-02 23:42:39.000000000 +0200 @@ -791,7 +791,7 @@ } GENERIC_LIST * -list_reverse (list) +bash_list_reverse (list) GENERIC_LIST *list; { register GENERIC_LIST *next, *prev; diff -Nur bash-3.0/builtins/Makefile.in bash-3.0-builtin-sql-1.0.1/builtins/Makefile.in --- bash-3.0/builtins/Makefile.in 2004-01-21 20:22:50.000000000 +0100 +++ bash-3.0-builtin-sql-1.0.1/builtins/Makefile.in 2006-08-02 23:42:39.000000000 +0200 @@ -133,7 +133,7 @@ $(srcdir)/times.def $(srcdir)/trap.def $(srcdir)/type.def \ $(srcdir)/ulimit.def $(srcdir)/umask.def $(srcdir)/wait.def \ $(srcdir)/reserved.def $(srcdir)/pushd.def $(srcdir)/shopt.def \ - $(srcdir)/printf.def $(srcdir)/complete.def + $(srcdir)/printf.def $(srcdir)/complete.def $(srcdir)/mysql.def STATIC_SOURCE = common.c evalstring.c evalfile.c getopt.c bashgetopt.c \ getopt.h @@ -147,13 +147,15 @@ suspend.o test.o times.o trap.o type.o ulimit.o umask.o \ wait.o getopts.o shopt.o printf.o getopt.o bashgetopt.o complete.o +MYSQL = mysql.o + CREATED_FILES = builtext.h builtins.c psize.aux pipesize.h all: $(MKBUILTINS) libbuiltins.a -libbuiltins.a: $(MKBUILTINS) $(OFILES) builtins.o +libbuiltins.a: $(MKBUILTINS) $(OFILES) $(MYSQL) builtins.o $(RM) $@ - $(AR) $(ARFLAGS) $@ $(OFILES) + $(AR) $(ARFLAGS) $@ $(OFILES) $(MYSQL) -$(RANLIB) $@ builtext.h builtins.c: $(MKBUILTINS) $(DEFSRC) @@ -218,11 +220,11 @@ ./$(MKBUILTINS) -documentonly $(DEFSRC) clean: - $(RM) $(OFILES) $(CREATED_FILES) $(MKBUILTINS) mkbuiltins.o libbuiltins.a + $(RM) $(OFILES) $(MYSQL) $(CREATED_FILES) $(MKBUILTINS) mkbuiltins.o libbuiltins.a -test -d helpfiles && $(RM) -r helpfiles mostlyclean: - $(RM) $(OFILES) libbuiltins.a + $(RM) $(OFILES) $(MYSQL) libbuiltins.a distclean maintainer-clean: clean $(RM) Makefile diff -Nur bash-3.0/builtins/mysql.def bash-3.0-builtin-sql-1.0.1/builtins/mysql.def --- bash-3.0/builtins/mysql.def 1970-01-01 01:00:00.000000000 +0100 +++ bash-3.0-builtin-sql-1.0.1/builtins/mysql.def 2006-08-02 23:44:18.000000000 +0200 @@ -0,0 +1,239 @@ +This file is mysql.def, from which is created mysql.c. +It implements native MySQL support in Bash. + +Copyright (C) 2006 Maik Broemme + +This file is part of GNU Bash, the Bourne Again SHell. + +Bash 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, or (at your option) any later +version. + +Bash 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 Bash; see the file COPYING. If not, write to the Free Software +Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. + +$PRODUCES mysql.c + +$BUILTIN mysql_query +$FUNCTION mysql_builtin +$SHORT_DOC mysql_query [-h host -P port -d dbname -u user -p password ] SQL... +Interface to MySQL 3/4/5 (www.mysql.com). After connecting to database, +arguments are feed to SQL engine sequentially. It returns 1 immediately on +any error. If all arguments are processed without error, then returns +success (0). Each 'mysql_query' command is complete, in that it opens and +closes connection with the database. The connect is using a transaction and +commits the database changes on successful execution or rolls back if query +failed. + + mysql_query [-hpduP ...] SQL... + Print query result (if any) to stdout. Fields are separated by \\t, + one row per line. + +Options -[hpduP] are optional. Default value will be used for missing +parameters. For convenience, shell variables MYSQL_COLUMS and MYSQL_ROWS +are set from the last query result. +$END + +/* + * mysql.c -- native mysql query engine for bash. + * + * Copyright (C) 2006 Maik Broemme + * + * Differences between BashDiff and Bash-SQL: + * + * - working with MySQL 3/4/5. + * - working multirow queries and results support. + * - using transactions to commit and rollback actions. + * - error reporting like the mysql command line client. + * - possibilty to handle multiple result sets from multiple + * statement executions or stored procedures. + * + * This source was adepted from the BashDiff project. The BashDiff + * project belongs to the following authors: + * + * William Park + * + * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include + +#if defined (HAVE_UNISTD_H) +# ifdef _MINIX +# include +# endif +# include +#endif + +#include /* puts(), printf(), BUFSIZE, ... */ + +#include "shell.h" /* includes lots of headers */ +#include "bashansi.h" /* string.h, strings.h, stdlib.h */ +#include "bashgetopt.h" /* internal_getopt(), ... */ +#include "builtins.h" /* xxx_doc[], builtin xxx_struct */ + +#if defined (HAVE_MYSQL) +#include + +int +mysql_builtin (list) + WORD_LIST *list; +{ + int opt, out, j; + char *host, *port, *dbname, *user, *password, *text, *t; + + MYSQL mysql; + MYSQL_RES *result; + MYSQL_ROW row; + unsigned long MYSQL_ROWS; + unsigned int MYSQL_COLUMS; + + host = port = dbname = user = password = (char *)NULL; + + reset_internal_getopt (); + while ((opt = internal_getopt (list, "h:P:d:u:p:")) != -1) { + switch (opt) { + case 'h': + host = list_optarg; + break; + case 'P': + port = list_optarg; + break; + case 'd': + dbname = list_optarg; + break; + case 'u': + user = list_optarg; + break; + case 'p': + password = list_optarg; + break; + default: + builtin_usage (); + return (EX_BADUSAGE); + } + } + + /* skip over possible */ + list = loptend; + + /* 0 argument */ + if (list == 0) { + builtin_usage (); + return (EX_BADUSAGE); + } + + unbind_variable ("MYSQL_COLUMS"); + unbind_variable ("MYSQL_ROWS"); + + /* 1 or more arguments: mysql_query [-option] SQL... */ + + if (mysql_init (&mysql) == 0) { + printf ("cannot initialize mysql\n"); + return (EXECUTION_FAILURE); + } + + if (mysql_real_connect ( + &mysql, + host, + user, + password, + dbname, /* NULL means default value */ + (unsigned int)atoi (port? port: "0"), /* 0 means default port */ + (char *)NULL, /* unix socket */ + CLIENT_MULTI_RESULTS /* client flag */ + ) == 0) { + printf ("ERROR %d (%s): %s\n", mysql_errno(&mysql), mysql_sqlstate(&mysql), mysql_error(&mysql)); /* cannot connect to server */ + mysql_close (&mysql); + return (EXECUTION_FAILURE); + } + + /* disable auto commit of database changes. */ + mysql_autocommit(&mysql, 0); + + out = EXECUTION_SUCCESS; + for ( ; list && out == EXECUTION_SUCCESS; list = list->next) { + text = list->word->word; + + /* check if query execution was ok */ + if (mysql_query (&mysql, text)) { + + /* set return value and show the error. */ + out = EXECUTION_FAILURE; + printf ("ERROR %d (%s): %s\n", mysql_errno(&mysql), mysql_sqlstate(&mysql), mysql_error(&mysql)); + + /* rollback execution. */ + mysql_rollback (&mysql); + } else { + + /* if query was a select, try to fetch the result. */ + result = mysql_store_result (&mysql); + if (result) { + + /* fill variables with mysql values */ + MYSQL_ROWS = mysql_num_rows (result); + MYSQL_COLUMS = mysql_num_fields (result); + + /* export variables to environment */ + bind_var_to_int ("MYSQL_ROWS", MYSQL_ROWS); + bind_var_to_int ("MYSQL_COLUMS", MYSQL_COLUMS); + + while (row = mysql_fetch_row (result)) { + for (j = 0; j < MYSQL_COLUMS; j++) { + printf ("%s", row[j]); + putchar ((j < MYSQL_COLUMS-1) ? '\t' : '\n'); + } + } + } else { + + if (mysql_field_count (&mysql)) { + out = EXECUTION_FAILURE; + printf ("ERROR %d (%s): %s\n", mysql_errno(&mysql), mysql_sqlstate(&mysql), mysql_error(&mysql)); + } + } + + /* commit changes to the database. */ + mysql_commit (&mysql); + + /* free the memory of the result. */ + mysql_free_result (result); + } + } + + /* close the connection. */ + mysql_close (&mysql); + + /* return with the result of the query. */ + return (out); +} + +#else /* HAVE_MYSQL */ +int +mysql_builtin (list) + WORD_LIST *list; +{ + + /* 0 argument */ + printf ("mysql-support disabled at compilation time, enable it with '--enable-builtin-mysql'\n"); + return (EX_BADUSAGE); +} +#endif /* HAVE_MYSQL */ diff -Nur bash-3.0/config.h.in bash-3.0-builtin-sql-1.0.1/config.h.in --- bash-3.0/config.h.in 2004-07-21 22:08:31.000000000 +0200 +++ bash-3.0-builtin-sql-1.0.1/config.h.in 2006-08-02 23:42:39.000000000 +0200 @@ -158,6 +158,9 @@ #include "config-top.h" +/* Define HAVE_MYSQL if you want the bash to use a native MySQL backend. */ +#undef HAVE_MYSQL + /* Beginning of autoconf additions. */ /* Characteristics of the C compiler */ diff -Nur bash-3.0/configure.in bash-3.0-builtin-sql-1.0.1/configure.in --- bash-3.0/configure.in 2004-07-21 22:06:54.000000000 +0200 +++ bash-3.0-builtin-sql-1.0.1/configure.in 2006-08-02 23:42:39.000000000 +0200 @@ -181,6 +181,7 @@ opt_separate_help=no opt_multibyte=yes opt_debugger=yes +opt_builtin_mysql=no dnl options that affect how bash is compiled and linked opt_static_link=no @@ -199,7 +200,7 @@ opt_brace_expansion=no opt_disabled_builtins=no opt_command_timing=no opt_extended_glob=no opt_cond_command=no opt_arith_for_command=no opt_net_redirs=no opt_progcomp=no opt_separate_help=no - opt_multibyte=yes opt_cond_regexp=no + opt_multibyte=yes opt_cond_regexp=no opt_builtin_mysql=no fi AC_ARG_ENABLE(alias, AC_HELP_STRING([--enable-alias], [enable shell aliases]), opt_alias=$enableval) @@ -229,6 +230,7 @@ AC_ARG_ENABLE(separate-helpfiles, AC_HELP_STRING([--enable-separate-helpfiles], [use external files for help builtin documentation]), opt_separate_help=$enableval) AC_ARG_ENABLE(usg-echo-default, AC_HELP_STRING([--enable-usg-echo-default], [a synonym for --enable-xpg-echo-default]), opt_xpg_echo=$enableval) AC_ARG_ENABLE(xpg-echo-default, AC_HELP_STRING([--enable-xpg-echo-default], [make the echo builtin expand escape sequences by default]), opt_xpg_echo=$enableval) +AC_ARG_ENABLE(builtin-mysql, AC_HELP_STRING([--enable-builtin-mysql], [include builtin to native MySQL interface]), opt_builtin_mysql=$enableval) dnl options that alter how bash is compiled and linked AC_ARG_ENABLE(mem-scramble, AC_HELP_STRING([--enable-mem-scramble], [scramble memory on calls to malloc and free]), opt_memscramble=$enableval) @@ -579,6 +581,23 @@ sys/time.h sys/times.h sys/types.h sys/wait.h) AC_CHECK_HEADERS(netinet/in.h arpa/inet.h) +dnl Check for mysql.h (MySQL). If it's not picked up automatically, then +dnl try something like +dnl (cd /usr/local/include; ln -s ../mysql/include mysql) +dnl (cd /usr/local/lib; ln -s ../mysql/lib mysql) +if test $opt_builtin_mysql = yes; then + AC_CHECK_HEADER(mysql/mysql.h) + if test "$ac_cv_header_mysql_mysql_h" = yes; then + AC_DEFINE(HAVE_MYSQL) + MYSQL_LIB=$(mysql_config --libs) + MYSQL_LDFLAGS=$(mysql_config --libs) + AC_SUBST(MYSQL_LIB) + AC_SUBST(MYSQL_LDFLAGS) + else + AC_MSG_ERROR([*** mysql not found]) + fi +fi + dnl special checks for libc functions AC_FUNC_ALLOCA AC_FUNC_GETPGRP diff -Nur bash-3.0/examples/loadables/getconf.c bash-3.0-builtin-sql-1.0.1/examples/loadables/getconf.c --- bash-3.0/examples/loadables/getconf.c 2002-09-20 21:49:27.000000000 +0200 +++ bash-3.0-builtin-sql-1.0.1/examples/loadables/getconf.c 2006-08-02 23:42:39.000000000 +0200 @@ -1155,7 +1155,7 @@ } list = loptend; - if ((aflag == 0 && list == 0) || (aflag && list) || list_length(list) > 2) { + if ((aflag == 0 && list == 0) || (aflag && list) || bash_list_length(list) > 2) { builtin_usage(); return(EX_USAGE); } diff -Nur bash-3.0/execute_cmd.c bash-3.0-builtin-sql-1.0.1/execute_cmd.c --- bash-3.0/execute_cmd.c 2004-07-04 20:12:58.000000000 +0200 +++ bash-3.0-builtin-sql-1.0.1/execute_cmd.c 2006-08-02 23:42:39.000000000 +0200 @@ -2056,7 +2056,7 @@ /* command and arithmetic substitution, parameter and variable expansion, word splitting, pathname expansion, and quote removal. */ list = releaser = expand_words_no_vars (select_command->map_list); - list_len = list_length (list); + list_len = bash_list_length (list); if (list == 0 || list_len == 0) { if (list) diff -Nur bash-3.0/externs.h bash-3.0-builtin-sql-1.0.1/externs.h --- bash-3.0/externs.h 2004-04-13 05:30:08.000000000 +0200 +++ bash-3.0-builtin-sql-1.0.1/externs.h 2006-08-02 23:42:39.000000000 +0200 @@ -118,12 +118,12 @@ extern char *localeexpand __P((char *, int, int, int, int *)); /* Declarations for functions defined in list.c. */ -extern void list_walk __P((GENERIC_LIST *, sh_glist_func_t *)); -extern void wlist_walk __P((WORD_LIST *, sh_icpfunc_t *)); -extern GENERIC_LIST *list_reverse (); -extern int list_length (); -extern GENERIC_LIST *list_append (); -extern GENERIC_LIST *list_remove (); +extern void bash_list_walk __P((GENERIC_LIST *, sh_glist_func_t *)); +extern void bash_wlist_walk __P((WORD_LIST *, sh_icpfunc_t *)); +extern GENERIC_LIST *bash_list_reverse (); +extern int bash_list_length (); +extern GENERIC_LIST *bash_list_append (); +extern GENERIC_LIST *bash_list_remove (); /* Declarations for functions defined in stringlib.c */ extern int find_string_in_alist __P((char *, STRING_INT_ALIST *, int)); diff -Nur bash-3.0/general.h bash-3.0-builtin-sql-1.0.1/general.h --- bash-3.0/general.h 2004-04-15 01:44:58.000000000 +0200 +++ bash-3.0-builtin-sql-1.0.1/general.h 2006-08-02 23:42:39.000000000 +0200 @@ -125,7 +125,7 @@ /* A macro to avoid making an uneccessary function call. */ #define REVERSE_LIST(list, type) \ - ((list && list->next) ? (type)list_reverse ((GENERIC_LIST *)list) \ + ((list && list->next) ? (type)bash_list_reverse ((GENERIC_LIST *)list) \ : (type)(list)) #if __GNUC__ > 1 diff -Nur bash-3.0/lib/sh/stringlist.c bash-3.0-builtin-sql-1.0.1/lib/sh/stringlist.c --- bash-3.0/lib/sh/stringlist.c 2002-03-25 19:03:32.000000000 +0100 +++ bash-3.0-builtin-sql-1.0.1/lib/sh/stringlist.c 2006-08-02 23:42:39.000000000 +0200 @@ -272,7 +272,7 @@ *ip = 0; return ((STRINGLIST *)0); } - slen = list_length (list); + slen = bash_list_length (list); ret = (STRINGLIST *)xmalloc (sizeof (STRINGLIST)); ret->list = strvec_from_word_list (list, alloc, starting_index, &len); ret->list_size = slen + starting_index; diff -Nur bash-3.0/lib/sh/stringvec.c bash-3.0-builtin-sql-1.0.1/lib/sh/stringvec.c --- bash-3.0/lib/sh/stringvec.c 2002-03-25 19:00:46.000000000 +0100 +++ bash-3.0-builtin-sql-1.0.1/lib/sh/stringvec.c 2006-08-02 23:42:39.000000000 +0200 @@ -184,7 +184,7 @@ int count; char **array; - count = list_length (list); + count = bash_list_length (list); array = (char **)xmalloc ((1 + count + starting_index) * sizeof (char *)); for (count = 0; count < starting_index; count++) diff -Nur bash-3.0/list.c bash-3.0-builtin-sql-1.0.1/list.c --- bash-3.0/list.c 2002-03-18 19:13:54.000000000 +0100 +++ bash-3.0-builtin-sql-1.0.1/list.c 2006-08-02 23:42:39.000000000 +0200 @@ -36,7 +36,7 @@ #ifdef INCLUDE_UNUSED /* Call FUNCTION on every member of LIST, a generic list. */ void -list_walk (list, function) +bash_list_walk (list, function) GENERIC_LIST *list; sh_glist_func_t *function; { @@ -47,7 +47,7 @@ /* Call FUNCTION on every string in WORDS. */ void -wlist_walk (words, function) +bash_wlist_walk (words, function) WORD_LIST *words; sh_icpfunc_t *function; { @@ -61,7 +61,7 @@ of the chain. You should always assign the output value of this function to something, or you will lose the chain. */ GENERIC_LIST * -list_reverse (list) +bash_list_reverse (list) GENERIC_LIST *list; { register GENERIC_LIST *next, *prev; @@ -78,7 +78,7 @@ /* Return the number of elements in LIST, a generic list. */ int -list_length (list) +bash_list_length (list) GENERIC_LIST *list; { register int i; @@ -89,7 +89,7 @@ /* Append TAIL to HEAD. Return the header of the list. */ GENERIC_LIST * -list_append (head, tail) +bash_list_append (head, tail) GENERIC_LIST *head, *tail; { register GENERIC_LIST *t_head; @@ -110,11 +110,11 @@ then ARG. Note that LIST contains the address of a variable which points to the list. You might call this function like this: - SHELL_VAR *elt = list_remove (&variable_list, check_var_has_name, "foo"); + SHELL_VAR *elt = bash_list_remove (&variable_list, check_var_has_name, "foo"); dispose_variable (elt); */ GENERIC_LIST * -list_remove (list, comparer, arg) +bash_list_remove (list, comparer, arg) GENERIC_LIST **list; Function *comparer; char *arg; diff -Nur bash-3.0/pcomplete.c bash-3.0-builtin-sql-1.0.1/pcomplete.c --- bash-3.0/pcomplete.c 2004-01-08 16:36:17.000000000 +0100 +++ bash-3.0-builtin-sql-1.0.1/pcomplete.c 2006-08-02 23:42:39.000000000 +0200 @@ -830,7 +830,7 @@ l2 = expand_words_shellexp (l); dispose_words (l); - nw = list_length (l2); + nw = bash_list_length (l2); sl = strlist_create (nw + 1); ntxt = bash_dequote_text (text); diff -Nur bash-3.0/subst.c bash-3.0-builtin-sql-1.0.1/subst.c --- bash-3.0/subst.c 2004-07-04 19:56:13.000000000 +0200 +++ bash-3.0-builtin-sql-1.0.1/subst.c 2006-08-02 23:42:39.000000000 +0200 @@ -6766,7 +6766,7 @@ for (t = list, result = (WORD_LIST *)NULL; t; t = t->next) { tresult = make_word_list (word_quote_removal (t->word, quoted), (WORD_LIST *)NULL); - result = (WORD_LIST *) list_append (result, tresult); + result = (WORD_LIST *) bash_list_append (result, tresult); } return (result); } @@ -6839,7 +6839,7 @@ for (t = list, result = (WORD_LIST *)NULL; t; t = t->next) { tresult = word_split (t->word, ifs_value); - result = (WORD_LIST *) list_append (result, tresult); + result = (WORD_LIST *) bash_list_append (result, tresult); } return (result); } @@ -7070,7 +7070,7 @@ if (glob_list) { - output_list = (WORD_LIST *)list_append (glob_list, output_list); + output_list = (WORD_LIST *)bash_list_append (glob_list, output_list); PREPEND_LIST (tlist, disposables); } else if (fail_glob_expansion != 0) @@ -7250,7 +7250,7 @@ } expanded = REVERSE_LIST (temp_list, WORD_LIST *); - new_list = (WORD_LIST *)list_append (expanded, new_list); + new_list = (WORD_LIST *)bash_list_append (expanded, new_list); } if (orig_list) @@ -7379,7 +7379,7 @@ } #if 0 - tint = list_length (new_list) + 1; + tint = bash_list_length (new_list) + 1; RESIZE_MALLOCED_BUFFER (glob_argv_flags, 0, tint, glob_argv_flags_size, 16); for (tint = 0, temp_list = new_list; temp_list; temp_list = temp_list->next) glob_argv_flags[tint++] = (temp_list->word->flags & W_GLOBEXP) ? '1' : '0';