]> granicus.if.org Git - postgresql/commitdiff
Add \cd command to psql.
authorPeter Eisentraut <peter_e@gmx.net>
Mon, 7 May 2001 19:31:33 +0000 (19:31 +0000)
committerPeter Eisentraut <peter_e@gmx.net>
Mon, 7 May 2001 19:31:33 +0000 (19:31 +0000)
doc/src/sgml/ref/psql-ref.sgml
src/bin/psql/command.c
src/bin/psql/help.c
src/bin/psql/tab-complete.c

index 06b0cbb2b17fe3c277e09e65869834ea871ad87f..c1f405f3f3e6e8081e434bb5cb41216eb9211ad7 100644 (file)
@@ -1,5 +1,5 @@
 <!--
-$Header: /cvsroot/pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.49 2001/05/06 17:38:31 petere Exp $
+$Header: /cvsroot/pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.50 2001/05/07 19:31:33 petere Exp $
 Postgres documentation
 -->
 
@@ -217,6 +217,23 @@ testdb=>
         </listitem>
       </varlistentry>
 
+      <varlistentry>
+       <term><literal>\cd</literal> <optional><replaceable>directory</replaceable></optional></term>
+       <listitem>
+        <para>
+        Change the current working directory to
+        <replaceable>directory</replaceable>.  Without argument,
+        change to the current user's home directory.
+        </para>
+
+       <tip>
+        <para>
+         To print your current working directory, use <literal>\!pwd</literal>.
+        </para>
+       </tip>
+       </listitem>
+      </varlistentry>
+
       <varlistentry>
         <term><literal>\C</literal> [ <replaceable class="parameter">title</replaceable> ]</term>
         <listitem>
index eb4a2e406ec55ca538571e387ed7a61776ac06ec..fd3c9f8d92e53007207f811c3de657432c5e8784 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright 2000 by PostgreSQL Global Development Group
  *
- * $Header: /cvsroot/pgsql/src/bin/psql/command.c,v 1.50 2001/05/06 21:15:51 petere Exp $
+ * $Header: /cvsroot/pgsql/src/bin/psql/command.c,v 1.51 2001/05/07 19:31:33 petere Exp $
  */
 #include "postgres_fe.h"
 #include "command.h"
@@ -11,6 +11,9 @@
 #include <errno.h>
 #include <assert.h>
 #include <ctype.h>
+#ifdef HAVE_PWD_H
+#include <pwd.h>
+#endif
 #ifndef WIN32
 #include <sys/types.h>                 /* for umask() */
 #include <sys/stat.h>                  /* for stat() */
@@ -256,6 +259,45 @@ exec_command(const char *cmd,
                free(opt2);
        }
 
+       /* \cd */
+       else if (strcmp(cmd, "cd") == 0)
+       {
+               char   *opt = scan_option(&string, OT_NORMAL, NULL);
+               char   *dir;
+
+               if (opt)
+                       dir = opt;
+               else
+               {
+#ifndef WIN32
+                       struct passwd *pw;
+
+                       pw = getpwuid(geteuid());
+                       if (!pw)
+                       {
+                               psql_error("could not get home directory: %s\n", strerror(errno));
+                               exit(EXIT_FAILURE);
+                       }
+                       dir = pw->pw_dir;
+#else /* WIN32 */
+                       /* On Windows, 'cd' without arguments prints the current
+               directory, so if someone wants to code this here
+               instead... */
+                       dir = "/";
+#endif /* WIN32 */
+               }
+
+               if (chdir(dir) == -1)
+               {
+                       psql_error("\\%s: could not change directory to '%s': %s\n",
+                                          cmd, dir, strerror(errno));
+                       success = false;
+               }
+
+               if (opt)
+                       free(opt);
+       }
+
        /* \copy */
        else if (strcasecmp(cmd, "copy") == 0)
        {
index 10392766512a265c65fb2baa4412fe0d206b0fdd..d5f15b5ba775f901a11974e44c89a540ae4a1cde 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright 2000 by PostgreSQL Global Development Group
  *
- * $Header: /cvsroot/pgsql/src/bin/psql/help.c,v 1.37 2001/03/22 04:00:20 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/bin/psql/help.c,v 1.38 2001/05/07 19:31:33 petere Exp $
  */
 #include "postgres_fe.h"
 #include "help.h"
@@ -196,6 +196,7 @@ slashUsage(void)
        fprintf(fout, " \\c[onnect] [dbname|- [user]]\n"
                        "                connect to new database (currently '%s')\n", PQdb(pset.db));
        fprintf(fout, " \\C <title>     table title\n");
+       fprintf(fout, " \\cd [<dir>]    change the current working directory\n");
        fprintf(fout, " \\copy ...      perform SQL COPY with data stream to the client machine\n");
        fprintf(fout, " \\copyright     show PostgreSQL usage and distribution terms\n");
        fprintf(fout, " \\d <table>     describe table (or view, index, sequence)\n");
index d80465af5929c9253be27b249e57b312f4258e24..ce2a6692b77e19bdee53bfd846467a3b798faabc 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright 2000 by PostgreSQL Global Development Group
  *
- * $Header: /cvsroot/pgsql/src/bin/psql/tab-complete.c,v 1.30 2001/04/14 22:55:02 petere Exp $
+ * $Header: /cvsroot/pgsql/src/bin/psql/tab-complete.c,v 1.31 2001/05/07 19:31:33 petere Exp $
  */
 
 /*----------------------------------------------------------------------
@@ -725,12 +725,13 @@ psql_completion(char *text, int start, int end)
 
                COMPLETE_WITH_LIST(my_list);
        }
-       else if (strcmp(prev_wd, "\\e") == 0 || strcmp(prev_wd, "\\edit") == 0 ||
+       else if (strcmp(prev_wd, "\\cd") == 0 ||
+                        strcmp(prev_wd, "\\e") == 0 || strcmp(prev_wd, "\\edit") == 0 ||
                         strcmp(prev_wd, "\\g") == 0 ||
                         strcmp(prev_wd, "\\i") == 0 || strcmp(prev_wd, "\\include") == 0 ||
-                 strcmp(prev_wd, "\\o") == 0 || strcmp(prev_wd, "\\out") == 0 ||
+                        strcmp(prev_wd, "\\o") == 0 || strcmp(prev_wd, "\\out") == 0 ||
                         strcmp(prev_wd, "\\s") == 0 ||
-                  strcmp(prev_wd, "\\w") == 0 || strcmp(prev_wd, "\\write") == 0
+                        strcmp(prev_wd, "\\w") == 0 || strcmp(prev_wd, "\\write") == 0
                )
                matches = completion_matches(text, filename_completion_function);