OSDN Git Service

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 06b0cbb..c1f405f 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
 -->
 
@@ -218,6 +218,23 @@ testdb=>
       </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>
         <para>
index eb4a2e4..fd3c9f8 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 1039276..d5f15b5 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 d80465a..ce2a669 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);