diff --git a/src/bin/pg_dump/Makefile b/src/bin/pg_dump/Makefile index cebb113e1c..3df5ff6d4a 100644 --- a/src/bin/pg_dump/Makefile +++ b/src/bin/pg_dump/Makefile @@ -5,7 +5,7 @@ # Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group # Portions Copyright (c) 1994, Regents of the University of California # -# $Header: /cvsroot/pgsql/src/bin/pg_dump/Makefile,v 1.33 2002/06/20 20:29:41 momjian Exp $ +# $Header: /cvsroot/pgsql/src/bin/pg_dump/Makefile,v 1.34 2002/07/06 20:12:30 momjian Exp $ # #------------------------------------------------------------------------- @@ -13,8 +13,8 @@ subdir = src/bin/pg_dump top_builddir = ../../.. include $(top_builddir)/src/Makefile.global -OBJS= pg_backup_archiver.o pg_backup_db.o pg_backup_custom.o pg_backup_files.o \ - pg_backup_null.o pg_backup_tar.o +OBJS= pg_backup_archiver.o pg_backup_db.o pg_backup_custom.o \ + pg_backup_files.o pg_backup_null.o pg_backup_tar.o sprompt.o ifdef STRDUP OBJS+=$(top_builddir)/src/utils/strdup.o diff --git a/src/bin/pg_dump/pg_backup_db.c b/src/bin/pg_dump/pg_backup_db.c index f9fcaf7f59..a50f71bcaa 100644 --- a/src/bin/pg_dump/pg_backup_db.c +++ b/src/bin/pg_dump/pg_backup_db.c @@ -5,7 +5,7 @@ * Implements the basic DB functions used by the archiver. * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_backup_db.c,v 1.34 2002/07/04 15:35:07 momjian Exp $ + * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_backup_db.c,v 1.35 2002/07/06 20:12:30 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -37,114 +37,6 @@ static char *_sendSQLLine(ArchiveHandle *AH, char *qry, char *eos); static char *_sendCopyLine(ArchiveHandle *AH, char *qry, char *eos); -/* - * simple_prompt --- borrowed from psql - * - * Generalized function especially intended for reading in usernames and - * password interactively. Reads from /dev/tty or stdin/stderr. - * - * prompt: The prompt to print - * maxlen: How many characters to accept - * echo: Set to false if you want to hide what is entered (for passwords) - * - * Returns a malloc()'ed string with the input (w/o trailing newline). - */ -static bool prompt_state = false; - -char * -simple_prompt(const char *prompt, int maxlen, bool echo) -{ - int length; - char *destination; - FILE *termin, - *termout; - -#ifdef HAVE_TERMIOS_H - struct termios t_orig, - t; -#endif - - destination = (char *) malloc(maxlen + 2); - if (!destination) - return NULL; - - prompt_state = true; /* disable SIGINT */ - - /* - * Do not try to collapse these into one "w+" mode file. Doesn't work - * on some platforms (eg, HPUX 10.20). - */ - termin = fopen("/dev/tty", "r"); - termout = fopen("/dev/tty", "w"); - if (!termin || !termout) - { - if (termin) - fclose(termin); - if (termout) - fclose(termout); - termin = stdin; - termout = stderr; - } - -#ifdef HAVE_TERMIOS_H - if (!echo) - { - tcgetattr(fileno(termin), &t); - t_orig = t; - t.c_lflag &= ~ECHO; - tcsetattr(fileno(termin), TCSAFLUSH, &t); - } -#endif - - if (prompt) - { - fputs(gettext(prompt), termout); - fflush(termout); - } - - if (fgets(destination, maxlen, termin) == NULL) - destination[0] = '\0'; - - length = strlen(destination); - if (length > 0 && destination[length - 1] != '\n') - { - /* eat rest of the line */ - char buf[128]; - int buflen; - - do - { - if (fgets(buf, sizeof(buf), termin) == NULL) - break; - buflen = strlen(buf); - } while (buflen > 0 && buf[buflen - 1] != '\n'); - } - - if (length > 0 && destination[length - 1] == '\n') - /* remove trailing newline */ - destination[length - 1] = '\0'; - -#ifdef HAVE_TERMIOS_H - if (!echo) - { - tcsetattr(fileno(termin), TCSAFLUSH, &t_orig); - fputs("\n", termout); - fflush(termout); - } -#endif - - if (termin != stdin) - { - fclose(termin); - fclose(termout); - } - - prompt_state = false; /* SIGINT okay again */ - - return destination; -} - - static int _parse_version(ArchiveHandle *AH, const char *versionString) { diff --git a/src/bin/pg_dump/pg_dump.h b/src/bin/pg_dump/pg_dump.h index 3483fe2c0f..17491b027e 100644 --- a/src/bin/pg_dump/pg_dump.h +++ b/src/bin/pg_dump/pg_dump.h @@ -6,7 +6,7 @@ * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $Id: pg_dump.h,v 1.89 2002/07/02 05:49:52 momjian Exp $ + * $Id: pg_dump.h,v 1.90 2002/07/06 20:12:30 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -209,4 +209,7 @@ extern void dumpTables(Archive *fout, TableInfo tblinfo[], int numTables, const bool schemaOnly, const bool dataOnly); extern void dumpIndexes(Archive *fout, TableInfo *tbinfo, int numTables); +/* sprompt.h */ +extern char *simple_prompt(const char *prompt, int maxlen, bool echo); + #endif /* PG_DUMP_H */ diff --git a/src/bin/pg_dump/sprompt.c b/src/bin/pg_dump/sprompt.c new file mode 100644 index 0000000000..6bc0d986a1 --- /dev/null +++ b/src/bin/pg_dump/sprompt.c @@ -0,0 +1,121 @@ +/* + * psql - the PostgreSQL interactive terminal + * + * Copyright 2000 by PostgreSQL Global Development Group + * + * $Header: /cvsroot/pgsql/src/bin/pg_dump/Attic/sprompt.c,v 1.1 2002/07/06 20:12:30 momjian Exp $ + */ + +/* + * simple_prompt + * + * Generalized function especially intended for reading in usernames and + * password interactively. Reads from /dev/tty or stdin/stderr. + * + * prompt: The prompt to print + * maxlen: How many characters to accept + * echo: Set to false if you want to hide what is entered (for passwords) + * + * Returns a malloc()'ed string with the input (w/o trailing newline). + */ +#include "postgres_fe.h" + +#ifdef HAVE_TERMIOS_H +#include +#endif + +bool prompt_state = false; +extern char *simple_prompt(const char *prompt, int maxlen, bool echo); + +char * +simple_prompt(const char *prompt, int maxlen, bool echo) +{ + int length; + char *destination; + FILE *termin, + *termout; + +#ifdef HAVE_TERMIOS_H + struct termios t_orig, + t; +#endif + + destination = (char *) malloc(maxlen + 2); + if (!destination) + return NULL; + + prompt_state = true; /* disable SIGINT */ + + /* + * Do not try to collapse these into one "w+" mode file. Doesn't work + * on some platforms (eg, HPUX 10.20). + */ + termin = fopen("/dev/tty", "r"); + termout = fopen("/dev/tty", "w"); + if (!termin || !termout) + { + if (termin) + fclose(termin); + if (termout) + fclose(termout); + termin = stdin; + termout = stderr; + } + +#ifdef HAVE_TERMIOS_H + if (!echo) + { + tcgetattr(fileno(termin), &t); + t_orig = t; + t.c_lflag &= ~ECHO; + tcsetattr(fileno(termin), TCSAFLUSH, &t); + } +#endif + + if (prompt) + { + fputs(gettext(prompt), termout); + fflush(termout); + } + + if (fgets(destination, maxlen, termin) == NULL) + destination[0] = '\0'; + + length = strlen(destination); + if (length > 0 && destination[length - 1] != '\n') + { + /* eat rest of the line */ + char buf[128]; + int buflen; + + do + { + if (fgets(buf, sizeof(buf), termin) == NULL) + break; + buflen = strlen(buf); + } while (buflen > 0 && buf[buflen - 1] != '\n'); + } + + if (length > 0 && destination[length - 1] == '\n') + /* remove trailing newline */ + destination[length - 1] = '\0'; + +#ifdef HAVE_TERMIOS_H + if (!echo) + { + tcsetattr(fileno(termin), TCSAFLUSH, &t_orig); + fputs("\n", termout); + fflush(termout); + } +#endif + + if (termin != stdin) + { + fclose(termin); + fclose(termout); + } + + prompt_state = false; /* SIGINT okay again */ + + return destination; +} diff --git a/src/bin/psql/Makefile b/src/bin/psql/Makefile index 7ab5a90656..8a7416a89c 100644 --- a/src/bin/psql/Makefile +++ b/src/bin/psql/Makefile @@ -5,7 +5,7 @@ # Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group # Portions Copyright (c) 1994, Regents of the University of California # -# $Header: /cvsroot/pgsql/src/bin/psql/Makefile,v 1.32 2002/06/20 20:29:42 momjian Exp $ +# $Header: /cvsroot/pgsql/src/bin/psql/Makefile,v 1.33 2002/07/06 20:12:30 momjian Exp $ # #------------------------------------------------------------------------- @@ -19,7 +19,7 @@ override CPPFLAGS := -I$(libpq_srcdir) $(CPPFLAGS) OBJS=command.o common.o help.o input.o stringutils.o mainloop.o \ copy.o startup.o prompt.o variables.o large_obj.o print.o describe.o \ - tab-complete.o mbprint.o + sprompt.o tab-complete.o mbprint.o all: submake psql diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index bd2c87fa7f..1b1e462660 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -3,7 +3,7 @@ * * Copyright 2000 by PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/bin/psql/common.c,v 1.40 2002/03/06 06:10:31 momjian Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/common.c,v 1.41 2002/07/06 20:12:30 momjian Exp $ */ #include "postgres_fe.h" @@ -12,9 +12,6 @@ #include #include #include -#ifdef HAVE_TERMIOS_H -#include -#endif #ifndef HAVE_STRDUP #include #endif @@ -37,6 +34,7 @@ #include "print.h" #include "mainloop.h" +extern bool prompt_state; /* * "Safe" wrapper around strdup() @@ -158,115 +156,6 @@ NoticeProcessor(void *arg, const char *message) -/* - * simple_prompt - * - * Generalized function especially intended for reading in usernames and - * password interactively. Reads from /dev/tty or stdin/stderr. - * - * prompt: The prompt to print - * maxlen: How many characters to accept - * echo: Set to false if you want to hide what is entered (for passwords) - * - * Returns a malloc()'ed string with the input (w/o trailing newline). - */ -static bool prompt_state = false; - -char * -simple_prompt(const char *prompt, int maxlen, bool echo) -{ - int length; - char *destination; - FILE *termin, - *termout; - -#ifdef HAVE_TERMIOS_H - struct termios t_orig, - t; -#endif - - destination = (char *) malloc(maxlen + 2); - if (!destination) - return NULL; - - prompt_state = true; /* disable SIGINT */ - - /* - * Do not try to collapse these into one "w+" mode file. Doesn't work - * on some platforms (eg, HPUX 10.20). - */ - termin = fopen("/dev/tty", "r"); - termout = fopen("/dev/tty", "w"); - if (!termin || !termout) - { - if (termin) - fclose(termin); - if (termout) - fclose(termout); - termin = stdin; - termout = stderr; - } - -#ifdef HAVE_TERMIOS_H - if (!echo) - { - tcgetattr(fileno(termin), &t); - t_orig = t; - t.c_lflag &= ~ECHO; - tcsetattr(fileno(termin), TCSAFLUSH, &t); - } -#endif - - if (prompt) - { - fputs(gettext(prompt), termout); - fflush(termout); - } - - if (fgets(destination, maxlen, termin) == NULL) - destination[0] = '\0'; - - length = strlen(destination); - if (length > 0 && destination[length - 1] != '\n') - { - /* eat rest of the line */ - char buf[128]; - int buflen; - - do - { - if (fgets(buf, sizeof(buf), termin) == NULL) - break; - buflen = strlen(buf); - } while (buflen > 0 && buf[buflen - 1] != '\n'); - } - - if (length > 0 && destination[length - 1] == '\n') - /* remove trailing newline */ - destination[length - 1] = '\0'; - -#ifdef HAVE_TERMIOS_H - if (!echo) - { - tcsetattr(fileno(termin), TCSAFLUSH, &t_orig); - fputs("\n", termout); - fflush(termout); - } -#endif - - if (termin != stdin) - { - fclose(termin); - fclose(termout); - } - - prompt_state = false; /* SIGINT okay again */ - - return destination; -} - - - /* * Code to support query cancellation * @@ -276,7 +165,6 @@ simple_prompt(const char *prompt, int maxlen, bool echo) * so. We use write() to print to stdout because it's better to use simple * facilities in a signal handler. */ - PGconn *cancelConn; volatile bool cancel_pressed; diff --git a/src/bin/psql/common.h b/src/bin/psql/common.h index a148fcce0a..f11a798127 100644 --- a/src/bin/psql/common.h +++ b/src/bin/psql/common.h @@ -3,7 +3,7 @@ * * Copyright 2000 by PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/bin/psql/common.h,v 1.17 2001/11/05 17:46:31 momjian Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/common.h,v 1.18 2002/07/06 20:12:30 momjian Exp $ */ #ifndef COMMON_H #define COMMON_H @@ -37,4 +37,7 @@ extern PGresult *PSQLexec(const char *query); extern bool SendQuery(const char *query); +/* sprompt.h */ +extern char *simple_prompt(const char *prompt, int maxlen, bool echo); + #endif /* COMMON_H */ diff --git a/src/bin/psql/sprompt.c b/src/bin/psql/sprompt.c new file mode 100644 index 0000000000..c9c2ab5cfc --- /dev/null +++ b/src/bin/psql/sprompt.c @@ -0,0 +1,121 @@ +/* + * psql - the PostgreSQL interactive terminal + * + * Copyright 2000 by PostgreSQL Global Development Group + * + * $Header: /cvsroot/pgsql/src/bin/psql/Attic/sprompt.c,v 1.1 2002/07/06 20:12:30 momjian Exp $ + */ + +/* + * simple_prompt + * + * Generalized function especially intended for reading in usernames and + * password interactively. Reads from /dev/tty or stdin/stderr. + * + * prompt: The prompt to print + * maxlen: How many characters to accept + * echo: Set to false if you want to hide what is entered (for passwords) + * + * Returns a malloc()'ed string with the input (w/o trailing newline). + */ +#include "postgres_fe.h" + +#ifdef HAVE_TERMIOS_H +#include +#endif + +bool prompt_state = false; +extern char *simple_prompt(const char *prompt, int maxlen, bool echo); + +char * +simple_prompt(const char *prompt, int maxlen, bool echo) +{ + int length; + char *destination; + FILE *termin, + *termout; + +#ifdef HAVE_TERMIOS_H + struct termios t_orig, + t; +#endif + + destination = (char *) malloc(maxlen + 2); + if (!destination) + return NULL; + + prompt_state = true; /* disable SIGINT */ + + /* + * Do not try to collapse these into one "w+" mode file. Doesn't work + * on some platforms (eg, HPUX 10.20). + */ + termin = fopen("/dev/tty", "r"); + termout = fopen("/dev/tty", "w"); + if (!termin || !termout) + { + if (termin) + fclose(termin); + if (termout) + fclose(termout); + termin = stdin; + termout = stderr; + } + +#ifdef HAVE_TERMIOS_H + if (!echo) + { + tcgetattr(fileno(termin), &t); + t_orig = t; + t.c_lflag &= ~ECHO; + tcsetattr(fileno(termin), TCSAFLUSH, &t); + } +#endif + + if (prompt) + { + fputs(gettext(prompt), termout); + fflush(termout); + } + + if (fgets(destination, maxlen, termin) == NULL) + destination[0] = '\0'; + + length = strlen(destination); + if (length > 0 && destination[length - 1] != '\n') + { + /* eat rest of the line */ + char buf[128]; + int buflen; + + do + { + if (fgets(buf, sizeof(buf), termin) == NULL) + break; + buflen = strlen(buf); + } while (buflen > 0 && buf[buflen - 1] != '\n'); + } + + if (length > 0 && destination[length - 1] == '\n') + /* remove trailing newline */ + destination[length - 1] = '\0'; + +#ifdef HAVE_TERMIOS_H + if (!echo) + { + tcsetattr(fileno(termin), TCSAFLUSH, &t_orig); + fputs("\n", termout); + fflush(termout); + } +#endif + + if (termin != stdin) + { + fclose(termin); + fclose(termout); + } + + prompt_state = false; /* SIGINT okay again */ + + return destination; +}