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
|
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin III - Tab completion
//
// Copyright (C) 2002 - 2014, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
// tab-complete.cpp - Functions interfacing with tab-complete from psql
//
//////////////////////////////////////////////////////////////////////////
/*
* BUG: Must compile as C and not C++, becase of
* http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B315481
* Adds a bit of C<->C++ cruft...
*/
#define _CRT_NONSTDC_NO_DEPRECATE
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <libpq-fe.h>
#include <ctype.h>
/*
* Callbacks to the C++ world
*/
char *pg_query_to_single_ordered_string(char *query, void *dbptr);
/*
* Global vars for readline emulation
*/
static char *rl_line_buffer;
/*
* Macros to ease typing present in psql, rewritten for our API
*/
#define COMPLETE_WITH_QUERY(query) \
do { matches = complete_from_query(text,query,NULL,dbptr); } while (0)
#define COMPLETE_WITH_SCHEMA_QUERY(query, addon) \
do { matches = complete_from_schema_query(text,&query, addon, dbptr); } while (0)
#define COMPLETE_WITH_LIST(list) \
do { matches = complete_from_list(text,list); } while (0)
#define COMPLETE_WITH_CONST(str) \
do { matches = complete_from_const(text,str); } while (0)
#define COMPLETE_WITH_ATTR(table) \
do { matches = complete_from_query(text,Query_for_list_of_attributes, table, dbptr); } while (0)
/*
* Functions used by the tab completion in psql normally found in pgport
*/
static void *pg_malloc(size_t size)
{
return malloc(size);
}
static int pg_strcasecmp(const char *s1, const char *s2)
{
#ifdef WIN32
return _stricmp(s1, s2);
#else
return strcasecmp(s1, s2);
#endif
}
static int pg_strncasecmp(const char *s1, const char *s2, int len)
{
#ifdef WIN32
return _strnicmp(s1, s2, len);
#else
return strncasecmp(s1, s2, len);
#endif
}
/*
* Return the word (space delimited) before point. Set skip > 0 to
* skip that many words; e.g. skip=1 finds the word before the
* previous one. Return value is NULL or a malloc'ed string.
*
* Copied directly from psql.
*/
static char *
previous_word(int point, int skip)
{
int i,
start = 0,
end = -1,
inquotes = 0;
char *s;
while (skip-- >= 0)
{
/* first we look for a space before the current word */
for (i = point; i >= 0; i--)
if (rl_line_buffer[i] == ' ')
break;
/* now find the first non-space which then constitutes the end */
for (; i >= 0; i--)
if (rl_line_buffer[i] != ' ')
{
end = i;
break;
}
/*
* If no end found we return null, because there is no word before the
* point
*/
if (end == -1)
return NULL;
/*
* Otherwise we now look for the start. The start is either the last
* character before any space going backwards from the end, or it's
* simply character 0
*/
for (start = end; start > 0; start--)
{
if (rl_line_buffer[start] == '"')
inquotes = !inquotes;
if ((rl_line_buffer[start - 1] == ' ') && inquotes == 0)
break;
}
point = start;
}
/* make a copy */
s = (char *)pg_malloc(end - start + 2);
strncpy(s, &rl_line_buffer[start], end - start + 1);
s[end - start + 1] = '\0';
return s;
}
/* Find the parenthesis after the last word */
/* Copied directly from psql */
static int find_open_parenthesis(int end)
{
int i = end-1;
while((rl_line_buffer[i]!=' ')&&(i>=0))
{
if (rl_line_buffer[i]=='(') return 1;
i--;
}
while((rl_line_buffer[i]==' ')&&(i>=0))
{
i--;
}
if (rl_line_buffer[i]=='(')
{
return 1;
}
return 0;
}
/*
* Forward declarations
*/
static char *complete_from_list(const char *text, const char * const *list);
static char *complete_from_const(const char *text, const char *string);
static char *complete_from_query(const char *text, const char *query, const char *addon, void *dbptr);
static char *complete_from_schema_query(const char *text, const void* query, const char *addon, void *dbptr);
static char *complete_create_command(char *text);
static char *complete_filename();
/*
* Include the main tab completion functionality from psql
*/
#include "tab-complete.inc"
/*
* Completion functions mimicking those from psql, only returning a single space separated
* string instead of being called multiple times by the readline library.
*/
static char *_complete_from_list(const char *text, const char * const *list, int casesensitive)
{
int string_length = strlen(text);
int size = 0;
int i;
char *r;
for (i = 0; list[i] != NULL; i++)
{
if (casesensitive && strncmp(text, list[i], string_length) == 0)
size += strlen(list[i]);
if (!casesensitive && pg_strncasecmp(text, list[i], string_length) == 0)
size += strlen(list[i]);
}
if (size == 0)
return NULL;
r = calloc(size + i*2 + 1, 1);
for (i = 0; list[i] != NULL; i++)
{
if ((casesensitive && strncmp(text, list[i], string_length) == 0)
||
(!casesensitive && pg_strncasecmp(text, list[i], string_length) == 0))
{
strcat(r,list[i]);
strcat(r," \t");
}
}
r[strlen(r)-1] = '\0'; /* Chop of trailing space */
return r;
}
static char *complete_from_list(const char *text, const char * const *list)
{
char *r = _complete_from_list(text, list, 1);
if (r)
return r;
return _complete_from_list(text, list, 0);
}
static char *complete_from_const(const char *text, const char *string)
{
return strdup(string);
}
static char *_complete_from_query(const char *text, const char *query, const SchemaQuery *squery, const char *addon, void *dbptr)
{
int string_length = strlen(text);
char *e_text;
char *complete_query = NULL;
char *t;
e_text = malloc(string_length*2+1);
PQescapeString(e_text, text, string_length);
if (query != NULL)
{
/* Normal query */
int bufsize = 1024;
char *e_addon;
/* Normal query needs escaped string */
if (addon)
{
e_addon = malloc(strlen(addon)*2+1);
PQescapeString(e_addon, addon, strlen(addon));
}
else
e_addon = strdup("");
while (1)
{
int r;
complete_query = realloc(complete_query, bufsize);
#ifdef WIN32
r = _snprintf(complete_query, bufsize, query, string_length, e_text, e_addon);
#else
r = snprintf(complete_query, bufsize, query, string_length, e_text, e_addon);
#endif
if (r < 0 || r >= bufsize)
bufsize *= 2;
else
break;
}
free(e_addon);
}
else
{
/* Schema query */
char *selcondition = NULL;
char *viscondition = NULL;
char *suppress_str = NULL;
const char *qualresult;
int bufsize = 2048;
if (squery->selcondition)
{
selcondition = malloc(strlen(squery->selcondition)+10);
sprintf(selcondition,"%s AND ",squery->selcondition);
}
else
selcondition = strdup("");
if (squery->viscondition)
{
viscondition = malloc(strlen(squery->viscondition)+10);
sprintf(viscondition, " AND %s",squery->viscondition);
}
else
viscondition = strdup("");
if (strcmp(squery->catname,"pg_catalog.pg_class c") == 0 &&
strncmp(text, "pg_", 3) != 0)
suppress_str = " AND c.relnamespace <> (SELECT oid FROM pg_catalog.pg_namespace WHERE nspname = 'pg_catalog')";
else
suppress_str = "";
qualresult = squery->qualresult;
if (qualresult == NULL)
qualresult = squery->result;
while (1)
{
int r;
complete_query = realloc(complete_query, bufsize);
#ifdef WIN32
r = _snprintf(complete_query, bufsize,
#else
r = snprintf(complete_query, bufsize,
#endif
"SELECT %s FROM %s WHERE %s substring(%s,1,%d)='%s' %s %s "
"\nUNION\n"
"SELECT pg_catalog.quote_ident(n.nspname) || '.' FROM pg_catalog.pg_namespace n WHERE substring(pg_catalog.quote_ident(n.nspname) || '.',1,%d)='%s' AND (SELECT pg_catalog.count(*) FROM pg_catalog.pg_namespace WHERE substring(pg_catalog.quote_ident(nspname) || '.',1,%d)= substring('%s',1,pg_catalog.length(pg_catalog.quote_ident(nspname))+1))>1"
"\nUNION\n"
"SELECT pg_catalog.quote_ident(n.nspname) || '.' || %s FROM %s, pg_catalog.pg_namespace n WHERE %s = n.oid AND %s substring(pg_catalog.quote_ident(n.nspname) || '.' || %s,1,%d)='%s' AND substring(pg_catalog.quote_ident(n.nspname) || '.',1,%d) = substring('%s',1,pg_catalog.length(pg_catalog.quote_ident(n.nspname))+1) AND (SELECT pg_catalog.count(*) FROM pg_catalog.pg_namespace WHERE substring(pg_catalog.quote_ident(nspname) || '.',1,%d) = substring('%s',1,pg_catalog.length(pg_catalog.quote_ident(nspname))+1)) = 1"
"\n%s",
squery->result,
squery->catname,
selcondition,
squery->result,
string_length,
e_text,
viscondition,
suppress_str,
string_length,
e_text,
string_length,
e_text,
qualresult,
squery->catname,
squery->namespace,
selcondition,
qualresult,
string_length,
e_text,
string_length,
e_text,
string_length,
e_text,
addon?addon:"");
if (r < 0 || r >= bufsize)
bufsize *= 2;
else
break;
}
free(viscondition);
free(selcondition);
}
t = pg_query_to_single_ordered_string(complete_query, dbptr);
if (complete_query)
free(complete_query);
free(e_text);
return t;
}
static char *complete_from_query(const char *text, const char *query, const char *addon, void *dbptr)
{
return _complete_from_query(text, query, NULL, addon, dbptr);
}
static char *complete_from_schema_query(const char *text, const void* query, const char *addon, void *dbptr)
{
/* query really is a SchemaQuery, but we don't know about SchemaQuery early enough. */
return _complete_from_query(text, NULL, (SchemaQuery *)query, addon, dbptr);
}
static char *complete_create_command(char *text)
{
int string_length = strlen(text);
int size = 0;
int i;
char *r;
for (i = 0; words_after_create[i].name != NULL; i++)
{
if (pg_strncasecmp(text, words_after_create[i].name, string_length) == 0)
size += strlen(words_after_create[i].name);
}
r = calloc(size + i*2 + 1, 1);
for (i = 0; words_after_create[i].name != NULL; i++)
{
if (pg_strncasecmp(text, words_after_create[i].name, string_length) == 0)
{
strcat(r,words_after_create[i].name);
strcat(r," \t");
}
}
r[strlen(r)-1] = '\0'; /* Chop of trailing space */
return r;
}
/* Not implemented */
static char *complete_filename()
{
return NULL;
}
/*
* Entrypoint from the C++ world
*/
char *tab_complete(const char *allstr, const int startptr, const int endptr, void *dbptr)
{
rl_line_buffer = (char *)allstr;
return psql_completion((char *)(allstr + startptr), startptr,endptr,dbptr);
}
|