1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
|
/* File: aql.c
* Author: Fred Wobus (fw@sanger.ac.uk)
* Copyright (C) J Thierry-Mieg and R Durbin, 1997
* -------------------------------------------------------------------
* Acedb 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.
* or see the on-line version at http://www.gnu.org/copyleft/gpl.txt
* -------------------------------------------------------------------
* This file is part of the ACEDB genome database package, written by
* Richard Durbin (MRC LMB, UK) rd@sanger.ac.uk, and
* Jean Thierry-Mieg (CRBM du CNRS, France) mieg@kaa.cnrs-mop.fr
*
* SCCS: $Id: taqlmain.c,v 1.20 2007/06/27 07:35:56 edgrif Exp $
* Description: top main-function for stand-alone AQL application
* Exported functions:
* HISTORY:
* Last edited: Jun 27 08:32 2007 (edgrif)
* * Mar 22 14:45 1999 (edgrif): Replaced messErrorInit with messSetMsgInfo
* * Dec 2 16:27 1998 (edgrif): Added code to record build time of this module.
* * Aug 17 16:53 1998 (fw): changed tableOut style to 'a' (like tace now)
* * Aug 3 14:53 1998 (fw): accept an optional debug level parameter for -d option
* * Jul 30 09:26 1998 (fw): check for env-var $ACEDB
* * Jul 29 10:09 1998 (fw): reintroduced the prompt for number of line to display
* * Jul 28 17:42 1998 (fw): use of aqlQueryExecute(), testQuery removed
* * Jul 23 14:53 1998 (fw): removed the single value return-value thing
as aqlEval now always returns a table
* * Jul 16 16:55 1998 (fw): accounted for scalar return value instead of table
* * Jul 16 16:35 1998 (fw): slightly changed the interface,
global variable isDebug, activated
by -d option, removed -u option instead
* Created: Wed Jul 22 11:16:44 1998 (fw)
*-------------------------------------------------------------------
*/
/***************************************************************/
#include <wh/acedb.h>
#include <wh/aceio.h>
#include <wh/aql.h>
#include <wh/table.h>
#include <wh/aceio.h>
#include <wh/apputils.h>
#include <wh/session.h>
#include <wh/version.h>
#include <wh/aceversion.h>
#include <wh/sigsubs.h>
#include <wh/pref.h>
#include <readline/readline.h>
/***************************************************************/
/* Defines a routine to return the compile date of this file. */
UT_MAKE_GETCOMPILEDATEROUTINE()
static void printUsage (void);
/***************************************************************/
/* command-line interface for AQL in a stand-alone application */
/***************************************************************/
int main (int argc, char **argv)
{
char *cp;
char *dbDir;
BOOL isQuiet = FALSE ;
char outputStyle = 'h';
int parseDebugLevel = 0; /* default if no -d is given */
int evalDebugLevel = 0; /* default if no -d is given */
int paramValueInt;
float paramValueFloat;
mytime_t paramValueDate;
char *paramValueText = NULL;
char *paramName = NULL;
char *paramType = NULL;
ACEOUT fo;
BOOL isInteractive;
char *value;
Stack queryStack;
char *outputfilename = NULL;
char *cl_query = NULL;
/* init message system details. */
messSetMsgInfo(argv[0], aceGetVersionString()) ;
/**** parse command-line arguments ******/
/* Does not return if user typed "-version" option. */
appCmdlineVersion(&argc, argv) ;
/*****************************/
prefInit() ; /* Set up App preferences. */
/*****************************/
isInteractive = TRUE ;
/* do NOT test stdinIsInteractive(),
* this would changes the behaviour of the
* code in pipe calls
*/
++argv ; --argc ;
if (argc && (!strcmp("-h", *argv) || !strcmp("-help", *argv)))
{
printUsage();
return EXIT_FAILURE;
}
/*****************************/
/* There is a problem with finding this symbol in the readline lib. on darwin.... */
#if !defined(DARWIN)
rl_extend_line_buffer (2000);
#endif
while (argc)
{
if (strcmp("-d", *argv) == 0)
{
parseDebugLevel = evalDebugLevel = 1; /* default if -d is given */
++argv ; --argc ;
if (argc && (*argv[0] >= '0' && *argv[0] <= '4'))
{
int l;
sscanf (*argv, "%d", &l);
parseDebugLevel = evalDebugLevel = l;
}
}
else if (strcmp("-dp", *argv) == 0)
{
parseDebugLevel = 1; /* default if -d is given */
++argv ; --argc ;
if (argc && (*argv[0] >= '0' && *argv[0] <= '4'))
{
int l;
sscanf (*argv, "%d", &l);
parseDebugLevel = l;
}
}
else if (strcmp("-de", *argv) == 0)
{
evalDebugLevel = 1; /* default if -d is given */
++argv ; --argc ;
if (argc && (*argv[0] >= '0' && *argv[0] <= '4'))
{
int l;
sscanf (*argv, "%d", &l);
evalDebugLevel = l;
}
}
else if (strcmp("-q", *argv) == 0)
{
isQuiet = TRUE ;
isInteractive = FALSE;
putenv ("ACEDB_NO_BANNER="); /* silence the kernel as well */
}
else if (strcmp("-a", *argv) == 0)
{
outputStyle = 'a';
}
else if (strcmp("-A", *argv) == 0)
{
outputStyle = 'A';
}
else if (strcmp("-j", *argv) == 0)
{
outputStyle = 'j';
}
else if (strcmp("-J", *argv) == 0)
{
outputStyle = 'J';
}
else if (strcmp("-param", *argv) == 0 && paramName == NULL)
{
++argv ; --argc ;
if (argc < 3)
{
printUsage();
return EXIT_FAILURE;
}
paramName = strnew (*argv, 0);
++argv ; --argc ;
paramType = strnew (*argv, 0);
++argv ; --argc ;
if (strncasecmp(paramType, "Int", 3) == 0)
sscanf (*argv, "%d", ¶mValueInt);
else if (strcasecmp(paramType, "Float") == 0)
sscanf (*argv, "%f", ¶mValueFloat);
else if (strncasecmp(paramType, "Date", 4) == 0)
paramValueDate = timeParse(*argv);
else if (strcasecmp(paramType, "Text") == 0 || strcasecmp(paramType, "String") == 0)
paramValueText = strnew(*argv, 0);
else
{
printUsage();
return EXIT_FAILURE;
}
}
else if (strcmp("-o", *argv) == 0)
{
++argv ; --argc ;
if (argc > 0)
{
outputfilename = *argv;
}
}
else if (strcmp("-e", *argv) == 0)
{
++argv ; --argc ;
if (argc > 0)
{
cl_query = *argv;
}
}
else
{
/* the next command line argument could be a database directory */
if (argc == 0)
/* no more command line args, check for $ACEDB */
{
if (!(dbDir = getenv("ACEDB")))
/* no env-var set, so show usage */
{
printUsage();
return EXIT_FAILURE;
}
}
else
/* we do have another command line argument,
so we take that to be the database directory */
dbDir = *argv;
}
++argv ; --argc ;
}
sessionInit (dbDir) ;
/* Set some process resources to unlimited, as we are running interactive */
/* the user is given chance to exit if some resources are too limited. */
utUnlimitResources(TRUE) ;
/* Check "-tsuser" option to allow a different user to be specified as the database
* user to be recorded in the timestamps. */
appTSUser(&argc, argv) ;
signalCatchInit(TRUE, /* init Ctrl-C as well */
(getCmdLineOption(&argc, argv, NOSIGCATCH_OPT, NULL) ? TRUE : FALSE)) ;
if (outputfilename != NULL) {
fo = aceOutCreateToFile (outputfilename, "w", 0);
} else {
fo = aceOutCreateToStdout (0);
}
queryStack = stackCreate (1000);
while (TRUE) {
char *line = NULL;
if (cl_query == NULL) {
char *prompt;
if (strlen(stackText(queryStack, 0)) == 0)
prompt = "acedb-aql> ";
else
prompt = "> "; /* continuation line */
line = readline (prompt);
if (line == NULL) /* got EOF */
break;
}
if (cl_query == NULL && (strlen(line) > 0))
{
if (strlen(stackText(queryStack, 0)) > 0)
catText (queryStack, "\n");
catText (queryStack, line);
if (line != NULL) free (line);
}
else
{
AQL aql;
TABLE *result;
char *query = (cl_query != NULL) ? cl_query : stackText(queryStack, 0);
int i;
if (line != NULL) free (line);
add_history(query);
for (i = 0; i < strlen(query); ++i)
if (query[i] == '\n')
query[i] = ' ';
if (paramName != NULL)
{
aql = aqlCreate(query, fo, 0, parseDebugLevel, evalDebugLevel, messprintf("$%s", paramName), (char *)NULL) ;
if (strncasecmp(paramType, "Int", 3) == 0)
result = aqlExecute(aql, 0, 0, 0, messprintf ("%s $%s", paramType, paramName), paramValueInt, (char *)NULL) ;
else if (strcasecmp(paramType, "Float") == 0)
result = aqlExecute(aql, 0, 0, 0, messprintf ("%s $%s", paramType, paramName), paramValueFloat, (char *)NULL) ;
else if (strncasecmp(paramType, "Date", 4) == 0)
result = aqlExecute(aql, 0, 0, 0, messprintf ("%s $%s", paramType, paramName), paramValueDate, (char *)NULL) ;
else if (strcasecmp(paramType, "Text") == 0 || strcasecmp(paramType, "String") == 0)
result = aqlExecute(aql, 0, 0, 0, messprintf ("%s $%s", paramType, paramName), paramValueText, (char *)NULL) ;
}
else
{
aql = aqlCreate(query,
fo,
0, /* beauty */
parseDebugLevel, /* parseDebugLevel*/
evalDebugLevel, /* evalDebugLevel */
(char *)NULL) ; /* terminator */
result = aqlExecute(aql, NULL, NULL, NULL, (char *)NULL);
}
if (aqlIsError(aql))
{
aceOutPrint (fo, "%s", aqlGetErrorReport(aql));
}
else
{
/* output the results table */
if (isInteractive)
aceOutPrint (fo, "Result table with %d line%s\n",
tableMax(result),
tableMax(result) == 1 ? "" : "s") ;
/* output the table with selected style */
if (isInteractive)
aceOutPrint (fo, "------------------------------\n");
tableOut (fo, result, '\t', outputStyle) ;
if (isInteractive)
aceOutPrint (fo, "------------------------------\n");
}
aqlDestroy (aql);
tableDestroy (result);
stackClear (queryStack);
}
if (cl_query != NULL) {
break;
}
} /* end-while */
if (isInteractive)
aceOutPrint (fo, "\n");
sessionClose (FALSE); /* clean-up without save */
aceOutDestroy (fo);
return (EXIT_SUCCESS);
} /* main */
/************************************************************/
static void printUsage (void)
{
printf ("\n"
" This is a simple command line interface for AQL. \n");
printf (" Type in a query (multiple lines are fine), followed by a blank line.\n"
" Syntax and examples on http://www.sanger.ac.uk/Software/Acedb/AQL/\n"
" Please record crashes and errors for Fred Wobus (details please).\n\n") ;
printf (" Usage: \n");
printf (" taql -h prints this info\n\n");
printf (" taql [ options ] [<database>]\n");
printf (" Where options may include\n");
printf (" -q quiet run - no messages (prompts etc.) to terminal\n"
" useful if the program is run as 'taql -q < queryfile.txt'\n"
" e.g. from within a script.\n\n");
printf (" -a output in Acedb format (strings are quoted etc.)\n"
" -A output in extended Acedb format (results are quoted with their class)\n"
" -j output in Java parsable format\n"
" -J output in 'Java Style' format\n"
" the default output style is plain, i.e. just text\n\n");
printf (" -param <name> <type> <value>\n"
" pass a scalar context variable\n"
" name - variable name, the query may then refer to $name\n"
" type - one of Int, Float, Text, DateType\n"
" value - the scalar value, text values may have to be quoted\n\n");
printf (" -d gives all technical debug info\n"
" level 0 - no debug info\n"
" 1 - internal query structure (default if -d is set)\n"
" 2 - all intermediate parsetrees\n"
" 3 - some tracking infor during query exection\n"
" 4 - all debug info during query execution\n\n") ;
printf (" The <database> parameter is the dircetory the ACeDB database to be accessed.\n"
" if no database is specified on the command line the value\n"
" of the environment variable $ACEDB is enquired.\n\n");
return;
} /* printUsage */
/**********************************************************************/
|