Index: /trunk/Ohana/configure.tcsh
===================================================================
--- /trunk/Ohana/configure.tcsh	(revision 13614)
+++ /trunk/Ohana/configure.tcsh	(revision 13615)
@@ -150,8 +150,8 @@
 
 if (! $?CPPFLAGS) then
-  set CPPFLAGS = 
+  set CPPFLAGS = ""
 endif  
-if ($pedantic) set CPPFLAGS = "$CPPFLAGS -Wall -Werror"
-if ($memcheck) set CPPFLAGS = "$CPPFLAGS -DOHANA_MEMORY"
+if ($pedantic) set CPPFLAGS = "$CPPFLAGS-Wall -Werror "
+if ($memcheck) set CPPFLAGS = "$CPPFLAGS-DOHANA_MEMORY "
 
 if (! $?LDFLAGS) then
@@ -169,5 +169,7 @@
 set needlibs   = "$needlibs m"
 
-# /usr/local/include/libpng is really pretty lame...
+set optlibs    = ""
+set optlibs    = "$optlibs mysqlclient"
+
 set sysincpath = "/usr/include /usr/local/include /usr/X11R6/include"
 
@@ -211,4 +213,7 @@
 set needincs = "$needincs zlib.h"
 
+set optincs  = ""
+set optincs = "$optincs mysql.h"
+
 # XXX need to have options for non-ANSI includes? (ie, varargs.h)
 # set needincs = "$needincs cfuncs.h" - from non-ANSI option in ohana.h
@@ -304,5 +309,5 @@
 # check for basic libraries
 echo ""
-echo "searching for needed external libraries..."
+echo "searching for required external libraries..."
 set faillibs = ""
 set libflags = ""
@@ -310,5 +315,5 @@
 set nonomatch
 foreach f ( $needlibs )
-  foreach g ( $syslibpath $libdir $libpath )
+  foreach g ( $libpath $libdir $syslibpath )
     set name = $g/lib$f.a
     if (-e $name[1]) goto got_lib;
@@ -335,5 +340,5 @@
 # check for termcap, curses, etc
 foreach f ( ncurses curses termcap )
-    foreach g ( $syslibpath $libdir $libpath )
+    foreach g ( $libpath $libdir $syslibpath )
         set name = $g/lib$f.a
         if (-e $name[1]) goto got_curses;
@@ -356,5 +361,5 @@
   echo "$libflags" | grep -- "-l$f " > /dev/null
   if ($status) then
-    set libflags = "$libflags-l$f"
+    set libflags = "$libflags-l$f "
   endif
 
@@ -366,4 +371,33 @@
 endif    
 
+# check for optional libraries
+echo ""
+echo "searching for optional external libraries..."
+foreach f ( $optlibs )
+  foreach g ( $libpath $libdir $syslibpath )
+    set name = $g/lib$f.a
+    if (-e $name[1]) goto got_optlib;
+    set name = $g/lib$f.$dlltype
+    if (-e $name[1]) goto got_optlib;
+    set name = $g/*/lib$f.a
+    if (-e $name[1]) goto got_optlib;
+    set name = $g/*/lib$f.$dlltype
+    if (-e $name[1]) goto got_optlib;
+  end
+  echo "missing lib$f; skipping"
+  continue
+got_optlib:
+  echo "found lib$f ($name[1])"
+  set gotlibdir = `dirname $name[1]`
+  echo "$libdirs" | grep -- "-L$gotlibdir " > /dev/null
+  if ($status) then
+    set libdirs  = "$libdirs-L$gotlibdir "
+  endif
+  echo "$libflags" | grep -- "-l$f " > /dev/null
+  if ($status) then
+    set libflags = "$libflags-l$f "
+  endif
+end
+
 # add the CPATH
 if ($?CPATH) then 
@@ -373,7 +407,7 @@
 endif
 
-# check for headers (including in subdirectories)
+# check for required headers (including in subdirectories)
 echo ""
-echo "searching for needed external header files..."
+echo "searching for required external header files..."
 set failincs = ""
 set incdirs = ""
@@ -382,4 +416,10 @@
     set name = "$g/$f"
     if (-e $name) goto got_inc;
+    set nonomatch
+    set name = $g/*/$f
+    echo "$name" | grep "*" > /dev/null
+    if (! $status) continue
+    unset nonomatch
+    if (-e $name[1]) goto got_inc;
   end
   echo "missing $f"
@@ -387,8 +427,13 @@
   continue
 got_inc:
-  echo "found $f ($name[1])"
-  echo "$incdirs" | grep -- "-I$g " > /dev/null
-  if ($status) then
-    set incdirs = "$incdirs-I$g "
+  set gotinc = $name[1]
+  echo "found $f ($gotinc)"
+  set gotincdir = `dirname $name[1]`
+  if (`dirname $f` != ".") then
+    set gotincdir = `dirname $gotincdir`
+  endif
+  echo "$incdirs" | grep -- "-I$gotincdir " > /dev/null
+  if ($status) then
+    set incdirs = "$incdirs-I$gotincdir "
   endif
 end
@@ -399,4 +444,35 @@
   exit 1
 endif    
+
+# check for optional headers (including in subdirectories)
+echo ""
+echo "searching for optional external header files..."
+foreach f ( $optincs )
+  foreach g ( $incdir $incpath $sysincpath )
+    set name = "$g/$f"
+    if (-e $name) goto got_optinc;
+    set nonomatch
+    set name = $g/*/$f
+    echo "$name" | grep "*" > /dev/null
+    if (! $status) continue
+    unset nonomatch
+    if (-e $name[1]) goto got_optinc;
+  end
+  echo "missing $f; skipping"
+  continue
+got_optinc:
+  set gotinc = $name[1]
+  echo "found $f ($gotinc)"
+  set gotincdir = `dirname $name[1]`
+  if (`dirname $f` != ".") then
+    set gotincdir = `dirname $gotincdir`
+  endif
+  echo "$incdirs" | grep -- "-I$gotincdir " > /dev/null
+  if ($status) then
+    set incdirs = "$incdirs-I$gotincdir "
+  endif
+  set haveflag = `echo $f | tr '[:lower:]' '[:upper:]' | tr '.' '_'`
+  set CPPFLAGS = "$CPPFLAGS-DHAVE_$haveflag "
+end
 
 echo 
Index: /trunk/Ohana/src/opihi/Makefile.Common
===================================================================
--- /trunk/Ohana/src/opihi/Makefile.Common	(revision 13614)
+++ /trunk/Ohana/src/opihi/Makefile.Common	(revision 13615)
@@ -4,5 +4,5 @@
 
 BASE_CFLAGS   =	$(CFLAGS)
-BASE_CPPFLAGS =	$(CPPFLAGS) -I$(INC) -I$(DESTINC) $(INCDIRS) -D$(ARCH) 
+BASE_CPPFLAGS =	$(CPPFLAGS) -I$(INC) -I$(DESTINC) $(INCDIRS) -D$(ARCH)
 BASE_LDFLAGS  = $(LDFLAGS) -L$(LIB) -L$(DESTLIB) $(LIBDIRS) $(LIBFLAGS)
 
Index: /trunk/Ohana/src/opihi/cmd.data/Makefile
===================================================================
--- /trunk/Ohana/src/opihi/cmd.data/Makefile	(revision 13614)
+++ /trunk/Ohana/src/opihi/cmd.data/Makefile	(revision 13615)
@@ -38,4 +38,6 @@
 $(SRC)/dimendown.$(ARCH).o	\
 $(SRC)/dimenup.$(ARCH).o	\
+$(SRC)/dbconnect.$(ARCH).o	\
+$(SRC)/dbselect.$(ARCH).o	\
 $(SRC)/erase.$(ARCH).o		\
 $(SRC)/extract.$(ARCH).o	\
Index: /trunk/Ohana/src/opihi/cmd.data/dbconnect.c
===================================================================
--- /trunk/Ohana/src/opihi/cmd.data/dbconnect.c	(revision 13615)
+++ /trunk/Ohana/src/opihi/cmd.data/dbconnect.c	(revision 13615)
@@ -0,0 +1,115 @@
+# include "data.h"
+# if (HAVE_MYSQL_H)
+# include "mysql.h"
+
+MYSQL mysql;
+MYSQL *connection = NULL;
+
+int dbconnect (int argc, char **argv) {
+  
+  char c;
+  int i;
+  int Nrows;
+  char query[256];
+  char password[1024];
+  MYSQL_RES *result;
+  MYSQL_ROW row;
+
+  if (argc != 4) {
+    gprint (GP_ERR, "USAGE: dbconnect (hostname) (username) (database)\n");
+    return FALSE;
+  }
+
+  fprintf (stdout, "Enter password: ");
+  scan_line (stdin, password);
+
+# if (0)
+  initscr();
+  noecho();
+  i = 0;
+  while (((c = getch()) != EOF) && (c != '\n')) {
+    password[i] = c;
+    i++;
+  }
+  password[i] = 0;
+# endif
+
+  mysql_init (&mysql);
+  connection = mysql_real_connect (&mysql, argv[1], argv[2], password, argv[3], 0, 0, 0);
+
+  if (connection == NULL) {
+    gprint (GP_ERR, "failed to connect to database\n");
+    gprint (GP_ERR, "%s\n", mysql_error (&mysql));
+    return (FALSE);
+  }
+    
+  sprintf (query, "set @@interactive_timeout = 30000");
+  if (mysql_query (connection, query)) {
+    gprint (GP_ERR, "failed to set interactive timout\n");
+    gprint (GP_ERR, "%s\n", mysql_error (connection));
+    free (query);
+    return (FALSE);
+  }
+  result = mysql_store_result (connection);
+  mysql_free_result (result);
+    
+  sprintf (query, "set @@wait_timeout = 30000");
+  if (mysql_query (connection, query)) {
+    gprint (GP_ERR, "failed to set wait timout\n");
+    gprint (GP_ERR, "%s\n", mysql_error (connection));
+    free (query);
+    return (FALSE);
+  }
+  result = mysql_store_result (connection);
+  mysql_free_result (result);
+    
+# if (0)
+  sprintf (query, "select @@interactive_timeout");
+  if (mysql_query (connection, query)) {
+    gprint (GP_ERR, "failed to get timout\n");
+    gprint (GP_ERR, "%s\n", mysql_error (connection));
+    free (query);
+    return (FALSE);
+  }
+  result = mysql_store_result (connection);
+  Nrows = mysql_num_rows(result);
+  row = mysql_fetch_row(result);
+  fprintf (stderr, "interactive timeout: %s\n", row[0]);
+  mysql_free_result (result);
+
+  sprintf (query, "select @@wait_timeout");
+  if (mysql_query (connection, query)) {
+    gprint (GP_ERR, "failed to get timout\n");
+    gprint (GP_ERR, "%s\n", mysql_error (connection));
+    free (query);
+    return (FALSE);
+  }
+  result = mysql_store_result (connection);
+  Nrows = mysql_num_rows(result);
+  row = mysql_fetch_row(result);
+  fprintf (stderr, "wait timeout: %s\n", row[0]);
+  mysql_free_result (result);
+# endif
+    
+  return (TRUE);
+}
+
+// XXX do I need to close the connection before opening a new one? 
+
+void *db_getConnection () {
+  return connection;
+}
+
+# else 
+
+int dbconnect (int argc, char **argv) {
+
+  gprint (GP_ERR, "mysql library is not available\n");
+  return FALSE;
+}
+
+void *db_getConnection () {
+  return NULL;
+}
+
+# endif
Index: /trunk/Ohana/src/opihi/cmd.data/dbselect.c
===================================================================
--- /trunk/Ohana/src/opihi/cmd.data/dbselect.c	(revision 13615)
+++ /trunk/Ohana/src/opihi/cmd.data/dbselect.c	(revision 13615)
@@ -0,0 +1,100 @@
+# include "data.h"
+# if (HAVE_MYSQL_H) 
+# include "mysql.h"
+
+int dbselect (int argc, char **argv) {
+  
+  int i, j, Nbytes, Ncols, Nrows;
+  char *query;
+  Vector **vec;
+
+  MYSQL_RES *result;
+  MYSQL_ROW row;
+  MYSQL_FIELD *fields;
+  MYSQL *connection = NULL;
+
+  if (argc < 4) {
+    gprint (GP_ERR, "USAGE: dbselect (fields) from (table) [where]\n");
+    return FALSE;
+  }
+
+  connection = db_getConnection ();
+  if (connection == NULL) {
+    gprint (GP_ERR, "database not defined; use dbconnect\n");
+    return (FALSE);
+  }
+
+  // generate the query line (concat the argv[i] entries)
+  Nbytes = 0;
+  for (i = 1; i < argc; i++) {
+    Nbytes += strlen(argv[i]) + 1;
+  }
+  Nbytes += 10;
+
+  ALLOCATE (query, char, Nbytes);
+  bzero (query, Nbytes);
+  strcat (query, "select ");
+  for (i = 1; i < argc; i++) {
+    strcat (query, argv[i]);
+    strcat (query, " ");
+  }
+  // strcat (query, ";");
+  // fprintf (stderr, "query: %s\n", query);
+
+  if (mysql_query (connection, query)) {
+    gprint (GP_ERR, "problem with query\n");
+    gprint (GP_ERR, "%s\n", mysql_error (connection));
+    free (query);
+    return (FALSE);
+  }
+    
+  result = mysql_store_result (connection);
+
+  Nrows = mysql_num_rows(result);
+  Ncols = mysql_num_fields(result);
+  fields = mysql_fetch_fields (result);
+
+  // fprintf (stderr, "Nrows: %d\n", Nrows);
+  // fprintf (stderr, "Ncols: %d\n", Ncols);
+
+  ALLOCATE (vec, Vector *, Ncols);
+  for (i = 0; i < Ncols; i++) {
+    if ((vec[i] = SelectVector (fields[i].name, ANYVECTOR, TRUE)) == NULL) {
+      gprint (GP_ERR, "trouble creating vector named %s\n", fields[i].name);
+      free (query);
+      free (vec);
+      return (FALSE);
+    }
+    REALLOCATE (vec[i][0].elements, float, Nrows);
+    vec[i][0].Nelements = Nrows;
+  }
+
+  for (j = 0; j < Nrows; j++) {
+    row = mysql_fetch_row(result);
+    if (row == NULL) {
+      gprint (GP_ERR, "inconsistent row count: expected %d, got %d\n", Nrows, j);
+      free (query);
+      free (vec);
+      mysql_free_result (result);
+      return (FALSE);
+    }
+    for (i = 0; i < Ncols; i++) {
+      vec[i][0].elements[j] = atof (row[i]);
+    }
+  }
+
+  free (query);
+  free (vec);
+  mysql_free_result (result);
+  return (TRUE);
+}
+
+# else 
+
+int dbselect (int argc, char **argv) {
+
+  gprint (GP_ERR, "mysql library is not available\n");
+  return FALSE;
+}
+
+# endif
Index: /trunk/Ohana/src/opihi/cmd.data/init.c
===================================================================
--- /trunk/Ohana/src/opihi/cmd.data/init.c	(revision 13614)
+++ /trunk/Ohana/src/opihi/cmd.data/init.c	(revision 13615)
@@ -18,4 +18,6 @@
 int cut              PROTO((int, char **));
 int datafile         PROTO((int, char **));
+int dbconnect        PROTO((int, char **));
+int dbselect         PROTO((int, char **));
 int delete           PROTO((int, char **));
 int device           PROTO((int, char **));
@@ -132,4 +134,6 @@
   {"cut",	   cut,		     "extract a cut across an image"},
   {"datafile",     datafile,	     "define file to read vectors"},
+  {"dbconnect",    dbconnect,	     "setup mysql db connection"},
+  {"dbselect", 	   dbselect,	     "extract vectors from mysql database table"},
   {"delete",  	   delete,	     "delete vectors or matrices"},
   {"device",  	   device,	     "set / get current graphics device"},
Index: /trunk/Ohana/src/opihi/include/data.h
===================================================================
--- /trunk/Ohana/src/opihi/include/data.h	(revision 13614)
+++ /trunk/Ohana/src/opihi/include/data.h	(revision 13615)
@@ -165,4 +165,7 @@
 
 int read_table_vectors (int argc, char **argv, char *extname);
+
+void *db_getConnection ();
+
 # endif
 
