I have applied a modified version of your patch.  I moved the prompt
'text' field as optional before name, and removed the default prompt
string, which seemed un-Unix-like.

I also simplified when stdin/stdout is used.  If stdin is the terminal,
it seems it makes no difference if we use the terminal or stdin.  Now,
prompt uses the terminal unless -f is used.  I think this simplifies its
behavior.  I realize cases where stdin is the terminal and stdout is not
behave differently in the new patch (prompt goes to the terminal and not
to stdout), but I think the previous behavior was too confusing to be
useful.  I can adjust this if people want different behavior.

Update version attached.

---------------------------------------------------------------------------

Chad Wagner wrote:
> On 2/17/07, Tom Lane <[EMAIL PROTECTED]> wrote:
> >
> > "Chad Wagner" <[EMAIL PROTECTED]> writes:
> > > Would it make sense to say:
> > > 1. if pset.notty is set and '-f' switch is not set then use
> > simple_prompt
> > > 2. else then use gets_fromFile(stdin) <or some other alternative?>
> >
> > Actually, there's another issue, which is where to send the prompt.
> > If we're using /dev/tty the answer is clear, but if we're proposing to
> > read from stdin then it's not necessarily the case that stdout (or even
> > stderr) is appropriate.
> >
> > Arguably a prompt is useless except to a human user, so maybe the rule
> > is "if stdin is a tty according to pset.notty, then prompt to /dev/tty;
> > otherwise suppress the prompt altogether".  Or we could prompt to stderr
> > instead of /dev/tty in this case.  I'm not sure if there are plausible
> > use-cases where stdin leads to the terminal and stderr doesn't.
> >
> 
> pset.notty will be set to 1 if either stdin or stdout is not a tty.  So in
> the case where they are redirecting both input and output then it will
> prompt on /dev/tty, otherwise the prompt would go out on stdout.
> 
> I was thinking perhaps it should look at the '-q' quiet switch for
> determining whether to display prompt at all.  So perhaps with a '-q' switch
> instead of dumping the prompt to stdout it should always be sent to
> /dev/tty.  Also, I think in general most users of this feature that would be
> "scripting" output (via expect or similar) would probably just use the '-v'
> switches.
> 
> BTW, attached is the latest version of this patch that includes the code
> (and updates to the psql-ref.sgml) I talked about earlier.  Not sure if
> gets_fromFile is favored, or perhaps the creation of a psql_prompt_var
> routine that takes into account some of what simple_prompt is doing but
> considering the logic we are discussing.

[ Attachment, skipping... ]

> 
> ---------------------------(end of broadcast)---------------------------
> TIP 9: In versions below 8.0, the planner will ignore your desire to
>        choose an index scan if your joining column's datatypes do not
>        match

-- 
  Bruce Momjian  <[EMAIL PROTECTED]>          http://momjian.us
  EnterpriseDB                               http://www.enterprisedb.com

  + If your life is a hard drive, Christ can be your backup. +
Index: doc/src/sgml/ref/psql-ref.sgml
===================================================================
RCS file: /cvsroot/pgsql/doc/src/sgml/ref/psql-ref.sgml,v
retrieving revision 1.186
diff -c -c -r1.186 psql-ref.sgml
*** doc/src/sgml/ref/psql-ref.sgml	21 Feb 2007 23:22:42 -0000	1.186
--- doc/src/sgml/ref/psql-ref.sgml	23 Feb 2007 18:14:04 -0000
***************
*** 1430,1435 ****
--- 1430,1453 ----
        </varlistentry>
  
        <varlistentry>
+         <term><literal>\prompt [ <replaceable class="parameter">text</replaceable> ] <replaceable class="parameter">name</replaceable></literal></term>
+         <listitem>
+         <para>
+          Prompts the user to set variable <replaceable
+          class="parameter">name</>.  An optional prompt, <replaceable
+          class="parameter">text</>, can be specified.  (For multi-word
+          prompts, use single-quotes.)
+         </para>
+ 
+         <para>
+          By default, <literal>\prompt</> uses the terminal for input and
+          output.  However, if the <option>-f</> command line switch is
+          used, <literal>\prompt</> uses standard input and standard output.
+         </para>
+         </listitem>
+       </varlistentry>
+ 
+       <varlistentry>
          <term><literal>\pset <replaceable class="parameter">parameter</replaceable> [ <replaceable class="parameter">value</replaceable> ]</literal></term>
  
          <listitem>
Index: src/bin/psql/command.c
===================================================================
RCS file: /cvsroot/pgsql/src/bin/psql/command.c,v
retrieving revision 1.177
diff -c -c -r1.177 command.c
*** src/bin/psql/command.c	5 Jan 2007 22:19:49 -0000	1.177
--- src/bin/psql/command.c	23 Feb 2007 18:14:06 -0000
***************
*** 712,717 ****
--- 712,768 ----
  		free(pw2);
  	}
  
+ 	/* \prompt -- prompt and set variable */
+ 	else if (strcmp(cmd, "prompt") == 0)
+ 	{
+ 		char	   *opt, *prompt_text = NULL;
+ 		char	   *arg1, *arg2;
+ 
+ 		arg1 = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
+ 		arg2 = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
+ 
+ 		if (!arg1)
+ 		{
+ 			psql_error("\\%s: missing required argument\n", cmd);
+ 			success = false;
+ 		}
+ 		else
+ 		{
+  			char	   *result;
+ 
+ 			if (arg2)
+ 			{
+ 				prompt_text = arg1;
+ 				opt = arg2;
+ 			}
+ 			else
+ 				opt = arg1;
+ 
+ 			if (!pset.inputfile)
+ 				result = simple_prompt(prompt_text, 4096, true);
+ 			else
+ 			{
+ 				if (prompt_text)
+ 				{
+ 					fputs(prompt_text, stdout);
+ 					fflush(stdout);
+ 				}
+ 				result = gets_fromFile(stdin);
+ 			}
+ 
+ 			if (!SetVariable(pset.vars, opt, result))
+ 			{
+ 				psql_error("\\%s: error\n", cmd);
+ 				success = false;
+ 			}
+ 
+ 			free(result);
+ 			if (prompt_text)
+ 				free(prompt_text);
+ 			free(opt);
+ 		}
+ 	}
+ 
  	/* \pset -- set printing parameters */
  	else if (strcmp(cmd, "pset") == 0)
  	{
Index: src/bin/psql/help.c
===================================================================
RCS file: /cvsroot/pgsql/src/bin/psql/help.c,v
retrieving revision 1.116
diff -c -c -r1.116 help.c
*** src/bin/psql/help.c	5 Jan 2007 22:19:49 -0000	1.116
--- src/bin/psql/help.c	23 Feb 2007 18:14:06 -0000
***************
*** 161,167 ****
  {
  	FILE	   *output;
  
! 	output = PageOutput(67, pager);
  
  	/* if you add/remove a line here, change the row count above */
  
--- 161,167 ----
  {
  	FILE	   *output;
  
! 	output = PageOutput(69, pager);
  
  	/* if you add/remove a line here, change the row count above */
  
***************
*** 184,189 ****
--- 184,191 ----
  	fprintf(output, _("  \\timing        toggle timing of commands (currently %s)\n"),
  			ON(pset.timing));
  	fprintf(output, _("  \\unset NAME    unset (delete) internal variable\n"));
+ 	fprintf(output, _("  \\prompt [TEXT] NAME\n"
+ 					  "                 prompt user to set internal variable\n"));
  	fprintf(output, _("  \\! [COMMAND]   execute command in shell or start interactive shell\n"));
  	fprintf(output, "\n");
  
Index: src/bin/psql/tab-complete.c
===================================================================
RCS file: /cvsroot/pgsql/src/bin/psql/tab-complete.c,v
retrieving revision 1.158
diff -c -c -r1.158 tab-complete.c
*** src/bin/psql/tab-complete.c	7 Feb 2007 00:52:35 -0000	1.158
--- src/bin/psql/tab-complete.c	23 Feb 2007 18:14:07 -0000
***************
*** 542,548 ****
  		"\\e", "\\echo", "\\encoding",
  		"\\f", "\\g", "\\h", "\\help", "\\H", "\\i", "\\l",
  		"\\lo_import", "\\lo_export", "\\lo_list", "\\lo_unlink",
! 		"\\o", "\\p", "\\password", "\\pset", "\\q", "\\qecho", "\\r",
  		"\\set", "\\t", "\\T",
  		"\\timing", "\\unset", "\\x", "\\w", "\\z", "\\!", NULL
  	};
--- 542,548 ----
  		"\\e", "\\echo", "\\encoding",
  		"\\f", "\\g", "\\h", "\\help", "\\H", "\\i", "\\l",
  		"\\lo_import", "\\lo_export", "\\lo_list", "\\lo_unlink",
! 		"\\o", "\\p", "\\password", "\\prompt", "\\pset", "\\q", "\\qecho", "\\r",
  		"\\set", "\\t", "\\T",
  		"\\timing", "\\unset", "\\x", "\\w", "\\z", "\\!", NULL
  	};
---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

Reply via email to