*
* Copyright (c) 2000-2005, PostgreSQL Global Development Group
*
- * $PostgreSQL: pgsql/src/bin/psql/tab-complete.c,v 1.139 2005/11/07 17:36:45 tgl Exp $
+ * $PostgreSQL: pgsql/src/bin/psql/tab-complete.c,v 1.140 2005/11/14 17:48:43 momjian Exp $
*/
/*----------------------------------------------------------------------
matches = completion_matches(text, filename_completion_function);
- /*
- *
- * Fi
- * n
- * a
- * l
- * l
- * y
- * ,
- *
- * we
- *
- * lo
- * o
- * k
- *
- * th
- * r
- * o
- * u
- * g
- * h
- *
- * th
- * e
- *
- * li
- * s
- * t
- *
- * of
- *
- * "t
- * h
- * i
- * n
- * g
- * s
- * "
- * ,
- *
- * su
- * c
- * h
- *
- * as
- *
- * TA
- * B
- * L
- * E
- * ,
- *
- * IN
- * D
- * E
- * X
- *
- * an
- * d
- *
- * ch
- * e
- * c
- * k
- *
- * if
- *
- * th
- * a
- * t
- *
- * wa
- * s
- *
- * th
- * e
- *
- * pr
- * e
- * v
- * i
- * o
- * u
- * s
- *
- * wo
- * r
- * d
- * .
- *
- * If
- *
- * so
- * ,
- *
- * ex
- * e
- * c
- * u
- * t
- * e
- *
- * th
- * e
- *
- * qu
- * e
- * r
- * y
- *
- * to
- *
- * ge
- * t
- *
- * a
- * li
- * s
- * t
- *
- * of
- *
- * th
- * e
- * m
- * .
- * */
+ /*
+ * Finally, we look through the list of "things", such as TABLE, INDEX and
+ * check if that was the previous word. If so, execute the query to get a
+ * list of them.
+ */
else
{
int i;
}
}
- /*
- *
- * If
- *
- * we
- *
- * st
- * i
- * l
- * l
- *
- * do
- * n
- * '
- * t
- *
- * ha
- * v
- * e
- *
- * an
- * y
- * t
- * h
- * i
- * n
- * g
- *
- * to
- *
- * ma
- * t
- * c
- * h
- *
- * we
- *
- * ha
- * v
- * e
- *
- * to
- *
- * fa
- * b
- * r
- * i
- * c
- * a
- * t
- * e
- *
- * so
- * m
- * e
- *
- * so
- * r
- * t
- *
- * of
- *
- * de
- * f
- * a
- * u
- * l
- * t
- *
- * li
- * s
- * t
- * .
- *
- * If
- *
- * we
- *
- * we
- * r
- * e
- *
- * to
- *
- * ju
- * s
- * t
- *
- * re
- * t
- * u
- * r
- * n
- *
- * NU
- * L
- * L
- * ,
- *
- * re
- * a
- * d
- * l
- * i
- * n
- * e
- *
- * au
- * t
- * o
- * m
- * a
- * t
- * i
- * c
- * a
- * l
- * l
- * y
- *
- * at
- * t
- * e
- * m
- * p
- * t
- * s
- *
- * fi
- * l
- * e
- * n
- * a
- * m
- * e
- *
- * co
- * m
- * p
- * l
- * e
- * t
- * i
- * o
- * n
- * ,
- *
- * an
- * d
- *
- * th
- * a
- * t
- * '
- * s
- *
- * us
- * u
- * a
- * l
- * l
- * y
- *
- * no
- *
- * go
- * o
- * d
- * .
- * */
- if (matches == NULL)
- {
- COMPLETE_WITH_CONST("");
+ /*
+ * If we still don't have anything to match we have to fabricate some sort
+ * of default list. If we were to just return NULL, readline automatically
+ * attempts filename completion, and that's usually no good.
+ */
+ if (matches == NULL)
+ {
+ COMPLETE_WITH_CONST("");
#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
- rl_completion_append_character = '\0';
+ rl_completion_append_character = '\0';
#endif
- }
-
- /*
- * f
- * r
- * e
- * e
- *
- * st
- * o
- * r
- * a
- * g
- * e
- *
- */
- free(prev_wd);
- free(prev2_wd);
- free(prev3_wd);
- free(prev4_wd);
- free(prev5_wd);
-
- /*
- * R
- * e
- * t
- * u
- * r
- * n
- *
- * ou
- * r
- *
- * Gr
- * a
- * n
- * d
- *
- * Li
- * s
- * t
- *
- * O'
- *
- * Ma
- * t
- * c
- * h
- * e
- * s
- *
- */
- return matches;
- }
+ }
+
+ /* free storage */
+ free(prev_wd);
+ free(prev2_wd);
+ free(prev3_wd);
+ free(prev4_wd);
+ free(prev5_wd);
+
+ /* Return our Grand List O' Matches */
+ return matches;
+}
/* This one gives you one from a list of things you can put after CREATE or DROP
as defined above.
*/
- static char *
- create_command_generator(const char *text, int state)
- {
- static int list_index,
- string_length;
- const char *name;
-
- /*
- * I
- * f
- *
- * th
- * i
- * s
- *
- * is
- *
- * th
- * e
- *
- * fi
- * r
- * s
- * t
- *
- * ti
- * m
- * e
- *
- * fo
- * r
- *
- * th
- * i
- * s
- *
- * co
- * m
- * p
- * l
- * e
- * t
- * i
- * o
- * n
- * ,
- *
- * in
- * i
- * t
- *
- * so
- * m
- * e
- *
- * va
- * l
- * u
- * e
- * s
- *
- */
- if (state == 0)
- {
- list_index = 0;
- string_length = strlen(text);
- }
-
- /*
- * f
- * i
- * n
- * d
- *
- * so
- * m
- * e
- * t
- * h
- * i
- * n
- * g
- *
- * th
- * a
- * t
- *
- * ma
- * t
- * c
- * h
- * e
- * s
- *
- */
- while ((name = words_after_create[list_index++].name))
- if (pg_strncasecmp(name, text, string_length) == 0)
- return pg_strdup(name);
-
- /*
- * i
- * f
- *
- * no
- * t
- * h
- * i
- * n
- * g
- *
- * ma
- * t
- * c
- * h
- * e
- * s
- * ,
- *
- * re
- * t
- * u
- * r
- * n
- *
- * NU
- * L
- * L
- *
- */
- return NULL;
- }
+static char *
+create_command_generator(const char *text, int state)
+{
+ static int list_index,
+ string_length;
+ const char *name;
+
+ /* If this is the first time for this completion, init some values */
+ if (state == 0)
+ {
+ list_index = 0;
+ string_length = strlen(text);
+ }
+
+ /* find something that matches */
+ while ((name = words_after_create[list_index++].name))
+ if (pg_strncasecmp(name, text, string_length) == 0)
+ return pg_strdup(name);
+
+ /* if nothing matches, return NULL */
+ return NULL;
+}
/* The following two functions are wrappers for _complete_from_query */
- static char *
- complete_from_query(const char *text, int state)
- {
- return _complete_from_query(0, text, state);
- }
+static char *
+complete_from_query(const char *text, int state)
+{
+ return _complete_from_query(0, text, state);
+}
- static char *
- complete_from_schema_query(const char *text, int state)
- {
- return _complete_from_query(1, text, state);
- }
+static char *
+complete_from_schema_query(const char *text, int state)
+{
+ return _complete_from_query(1, text, state);
+}
/* This creates a list of matching things, according to a query pointed to
See top of file for examples of both kinds of query.
*/
- static char *
- _complete_from_query(int is_schema_query, const char *text, int state)
- {
- static int list_index,
- string_length;
- static PGresult *result = NULL;
-
- /*
- *
- * If
- *
- * th
- * i
- * s
- *
- * is
- *
- * th
- * e
- *
- * fi
- * r
- * s
- * t
- *
- * ti
- * m
- * e
- *
- * fo
- * r
- *
- * th
- * i
- * s
- *
- * co
- * m
- * p
- * l
- * e
- * t
- * i
- * o
- * n
- * ,
- *
- * we
- *
- * fe
- * t
- * c
- * h
- *
- * a
- * li
- * s
- * t
- *
- * of
- *
- * ou
- * r
- *
- * "t
- * h
- * i
- * n
- * g
- * s
- * "
- *
- * fr
- * o
- * m
- *
- * th
- * e
- *
- * ba
- * c
- * k
- * e
- * n
- * d
- * .
- * */
- if (state == 0)
- {
- PQExpBufferData query_buffer;
- char *e_text;
- char *e_info_charp;
-
- list_index = 0;
- string_length = strlen(text);
-
- /*
- * F
- * r
- * e
- * e
- *
- * an
- * y
- *
- * pr
- * i
- * o
- * r
- *
- * re
- * s
- * u
- * l
- * t
- *
- */
- PQclear(result);
- result = NULL;
-
- /*
- * S
- * e
- * t
- *
- * up
- *
- * su
- * i
- * t
- * a
- * b
- * l
- * y
- * -
- * e
- * s
- * c
- * a
- * p
- * e
- * d
- *
- * co
- * p
- * i
- * e
- * s
- *
- * of
- *
- * te
- * x
- * t
- * u
- * a
- * l
- *
- * in
- * p
- * u
- * t
- * s
- *
- */
- e_text = pg_malloc(string_length * 2 + 1);
- PQescapeString(e_text, text, string_length);
-
- if (completion_info_charp)
- {
- size_t charp_len;
-
- charp_len = strlen(completion_info_charp);
- e_info_charp = pg_malloc(charp_len * 2 + 1);
- PQescapeString(e_info_charp, completion_info_charp,
- charp_len);
- }
- else
- e_info_charp = NULL;
-
- initPQExpBuffer(&query_buffer);
-
- if (is_schema_query)
- {
- /*
- * c
- * o
- * m
- * p
- * l
- * e
- * t
- * i
- * o
- * n
- * _
- * s
- * q
- * u
- * e
- * r
- * y
- *
- * gi
- * v
- * e
- * s
- *
- * us
- *
- * th
- * e
- *
- * pi
- * e
- * c
- * e
- * s
- *
- * to
- *
- * as
- * s
- * e
- * m
- * b
- * l
- * e
- *
- */
- const char *qualresult = completion_squery->qualresult;
-
- if (qualresult == NULL)
- qualresult = completion_squery->result;
-
- /*
- * G
- * e
- * t
- *
- * un
- * q
- * u
- * a
- * l
- * i
- * f
- * i
- * e
- * d
- *
- * na
- * m
- * e
- * s
- *
- * ma
- * t
- * c
- * h
- * i
- * n
- * g
- *
- * th
- * e
- *
- * in
- * p
- * u
- * t
- * -
- * s
- * o
- * -
- * f
- * a
- * r
- *
- */
- appendPQExpBuffer(&query_buffer, "SELECT %s FROM %s WHERE ",
- completion_squery->result,
- completion_squery->catname);
- if (completion_squery->selcondition)
- appendPQExpBuffer(&query_buffer, "%s AND ",
- completion_squery->selcondition);
- appendPQExpBuffer(&query_buffer, "substring(%s,1,%d)='%s'",
- completion_squery->result,
- string_length, e_text);
- appendPQExpBuffer(&query_buffer, " AND %s",
- completion_squery->viscondition);
-
- /*
- *
- * Wh
- * e
- * n
- *
- * fe
- * t
- * c
- * h
- * i
- * n
- * g
- *
- * re
- * l
- * a
- * t
- * i
- * o
- * n
- *
- * na
- * m
- * e
- * s
- * ,
- *
- * su
- * p
- * p
- * r
- * e
- * s
- * s
- *
- * sy
- * s
- * t
- * e
- * m
- *
- * ca
- * t
- * a
- * l
- * o
- * g
- * s
- *
- * un
- * l
- * e
- * s
- * s
- *
- * th
- * e
- *
- * in
- * p
- * u
- * t
- * -
- * s
- * o
- * -
- * f
- * a
- * r
- *
- * be
- * g
- * i
- * n
- * s
- *
- * wi
- * t
- * h
- *
- * "p
- * g
- * _
- * "
- * .
- *
- * Th
- * i
- * s
- *
- * is
- *
- * a
- * co
- * m
- * p
- * r
- * o
- * m
- * i
- * s
- * e
- *
- * be
- * t
- * w
- * e
- * e
- * n
- *
- * no
- * t
- *
- * of
- * f
- * e
- * r
- * i
- * n
- * g
- *
- * sy
- * s
- * t
- * e
- * m
- *
- * ca
- * t
- * a
- * l
- * o
- * g
- * s
- *
- * fo
- * r
- *
- * co
- * m
- * p
- * l
- * e
- * t
- * i
- * o
- * n
- *
- * at
- *
- * al
- * l
- * ,
- *
- * an
- * d
- *
- * ha
- * v
- * i
- * n
- * g
- *
- * th
- * e
- * m
- *
- * sw
- * a
- * m
- * p
- *
- * th
- * e
- *
- * re
- * s
- * u
- * l
- * t
- *
- * wh
- * e
- * n
- *
- * th
- * e
- *
- * in
- * p
- * u
- * t
- *
- * is
- *
- * ju
- * s
- * t
- *
- * "p
- * "
- * .
- * */
- if (strcmp(completion_squery->catname,
- "pg_catalog.pg_class c") == 0 &&
- strncmp(text, "pg_", 3) !=0)
- {
- appendPQExpBuffer(&query_buffer,
- " AND c.relnamespace <> (SELECT oid FROM"
- " pg_catalog.pg_namespace WHERE nspname = 'pg_catalog')");
- }
-
- /*
- *
- * Ad
- * d
- *
- * in
- *
- * ma
- * t
- * c
- * h
- * i
- * n
- * g
- *
- * sc
- * h
- * e
- * m
- * a
- *
- * na
- * m
- * e
- * s
- * ,
- *
- * bu
- * t
- *
- * on
- * l
- * y
- *
- * if
- *
- * th
- * e
- * r
- * e
- *
- * is
- *
- * mo
- * r
- * e
- *
- * th
- * a
- * n
- *
- * on
- * e
- *
- * po
- * t
- * e
- * n
- * t
- * i
- * a
- * l
- *
- * ma
- * t
- * c
- * h
- *
- * am
- * o
- * n
- * g
- *
- * sc
- * h
- * e
- * m
- * a
- *
- * na
- * m
- * e
- * s
- * .
- * */
- appendPQExpBuffer(&query_buffer, "\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'",
- string_length, e_text);
- appendPQExpBuffer(&query_buffer,
- " 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",
- string_length, e_text);
-
- /*
- *
- * Ad
- * d
- *
- * in
- *
- * ma
- * t
- * c
- * h
- * i
- * n
- * g
- *
- * qu
- * a
- * l
- * i
- * f
- * i
- * e
- * d
- *
- * na
- * m
- * e
- * s
- * ,
- *
- * bu
- * t
- *
- * on
- * l
- * y
- *
- * if
- *
- * th
- * e
- * r
- * e
- *
- * is
- *
- * ex
- * a
- * c
- * t
- * l
- * y
- *
- * on
- * e
- *
- * sc
- * h
- * e
- * m
- * a
- *
- * ma
- * t
- * c
- * h
- * i
- * n
- * g
- *
- * th
- * e
- *
- * in
- * p
- * u
- * t
- * -
- * s
- * o
- * -
- * f
- * a
- * r
- * .
- * */
- appendPQExpBuffer(&query_buffer, "\nUNION\n"
- "SELECT pg_catalog.quote_ident(n.nspname) || '.' || %s "
- "FROM %s, pg_catalog.pg_namespace n "
- "WHERE %s = n.oid AND ",
- qualresult,
- completion_squery->catname,
- completion_squery->namespace);
- if (completion_squery->selcondition)
- appendPQExpBuffer(&query_buffer, "%s AND ",
- completion_squery->selcondition);
- appendPQExpBuffer(&query_buffer, "substring(pg_catalog.quote_ident(n.nspname) || '.' || %s,1,%d)='%s'",
- qualresult,
- string_length, e_text);
-
- /*
- *
- * Th
- * i
- * s
- *
- * co
- * n
- * d
- * i
- * t
- * i
- * o
- * n
- *
- * ex
- * p
- * l
- * o
- * i
- * t
- * s
- *
- * th
- * e
- *
- * si
- * n
- * g
- * l
- * e
- * -
- * m
- * a
- * t
- * c
- * h
- * i
- * n
- * g
- * -
- * s
- * c
- * h
- * e
- * m
- * a
- *
- * ru
- * l
- * e
- *
- * to
- *
- * sp
- * e
- * e
- * d
- *
- * up
- *
- * th
- * e
- *
- * qu
- * e
- * r
- * y
- * */
- appendPQExpBuffer(&query_buffer,
- " AND substring(pg_catalog.quote_ident(n.nspname) || '.',1,%d) ="
- " substring('%s',1,pg_catalog.length(pg_catalog.quote_ident(n.nspname))+1)",
- string_length, e_text);
- appendPQExpBuffer(&query_buffer,
- " 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",
- string_length, e_text);
-
- /*
- * I
- * f
- *
- * an
- *
- * ad
- * d
- * o
- * n
- *
- * qu
- * e
- * r
- * y
-