src/bin/psql/command.c
static bool
editFile(const char *fname, int lineno)
{
	const char *editorName;
	const char *editor_lineno_arg = NULL;
	char	   *sys;
	int			result;

	Assert(fname != NULL);

	/* Find an editor to use */
	editorName = getenv("PSQL_EDITOR");
	if (!editorName)
		editorName = getenv("EDITOR");
	if (!editorName)
		editorName = getenv("VISUAL");
	if (!editorName)
		editorName = DEFAULT_EDITOR;

	/* Get line number argument, if we need it. */
	if (lineno > 0)
	{
		editor_lineno_arg = getenv("PSQL_EDITOR_LINENUMBER_ARG");
#ifdef DEFAULT_EDITOR_LINENUMBER_ARG
		if (!editor_lineno_arg)
			editor_lineno_arg = DEFAULT_EDITOR_LINENUMBER_ARG;
#endif
		if (!editor_lineno_arg)
		{
			pg_log_error("environment variable PSQL_EDITOR_LINENUMBER_ARG must be set to specify a line number");
			return false;
		}
	}

	/*
	 * On Unix the EDITOR value should *not* be quoted, since it might include
	 * switches, eg, EDITOR="pico -t"; it's up to the user to put quotes in it
	 * if necessary.  But this policy is not very workable on Windows, due to
	 * severe brain damage in their command shell plus the fact that standard
	 * program paths include spaces.
	 */
#ifndef WIN32
	if (lineno > 0)
		sys = psprintf("exec %s %s%d '%s'",
					   editorName, editor_lineno_arg, lineno, fname);
	else
		sys = psprintf("exec %s '%s'",
					   editorName, fname);
#else
	if (lineno > 0)
		sys = psprintf("\"%s\" %s%d \"%s\"",
					   editorName, editor_lineno_arg, lineno, fname);
	else
		sys = psprintf("\"%s\" \"%s\"",
					   editorName, fname);
#endif
	result = system(sys);
	if (result == -1)
		pg_log_error("could not start editor \"%s\"", editorName);
	else if (result == 127)
		pg_log_error("could not start /bin/sh");
	free(sys);

	return result == 0;
}
/*
 * helper for do_edit(): actually invoke the editor
 *
 * Returns true on success, false if we failed to invoke the editor or
 * it returned nonzero status.  (An error message is printed for failed-
 * to-invoke cases, but not if the editor returns nonzero status.)
 */
helper for do_edit(): actually invoke the editor
Returns true on success, false if we failed to invoke the editor or
it returned nonzero status.  (An error message is printed for failed
to-invoke cases, but not if the editor returns nonzero status.)
helper for do_edit () : actually invoke the editor Returns true on success false if we failed to invoke the editor or it returned nonzero status . ( An error message is printed for failed to - invoke cases but not if the editor returns nonzero status . )
static bool
editFile(const char *fname, int lineno)
{
	const char *editorName;
	const char *editor_lineno_arg = NULL;
	char	   *sys;
	int			result;
	Assert(fname != NULL);
	editorName = getenv("PSQL_EDITOR");
	if (!editorName)
		editorName = getenv("EDITOR");
	if (!editorName)
		editorName = getenv("VISUAL");
	if (!editorName)
		editorName = DEFAULT_EDITOR;
	if (lineno > 0)
	{
		editor_lineno_arg = getenv("PSQL_EDITOR_LINENUMBER_ARG");
#ifdef DEFAULT_EDITOR_LINENUMBER_ARG
		if (!editor_lineno_arg)
			editor_lineno_arg = DEFAULT_EDITOR_LINENUMBER_ARG;
#endif
		if (!editor_lineno_arg)
		{
			pg_log_error("environment variable PSQL_EDITOR_LINENUMBER_ARG must be set to specify a line number");
			return false;
		}
	}
#ifndef WIN32
	if (lineno > 0)
		sys = psprintf("exec %s %s%d '%s'",
					   editorName, editor_lineno_arg, lineno, fname);
	else
		sys = psprintf("exec %s '%s'",
					   editorName, fname);
#else
	if (lineno > 0)
		sys = psprintf("\"%s\" %s%d \"%s\"",
					   editorName, editor_lineno_arg, lineno, fname);
	else
		sys = psprintf("\"%s\" \"%s\"",
					   editorName, fname);
#endif
	result = system(sys);
	if (result == -1)
		pg_log_error("could not start editor \"%s\"", editorName);
	else if (result == 127)
		pg_log_error("could not start /bin/sh");
	free(sys);
	return result == 0;
}
static bool editFile ( const char * fname , int lineno ) { const char * editorName ; const char * editor_lineno_arg = NULL ; char * sys ; int result ; Assert ( fname != NULL ) ; editorName = getenv ( " " ) ; if ( ! editorName ) editorName = getenv ( " " ) ; if ( ! editorName ) editorName = getenv ( " " ) ; if ( ! editorName ) editorName = DEFAULT_EDITOR ; if ( lineno > 0 ) { editor_lineno_arg = getenv ( " " ) ; #ifdef DEFAULT_EDITOR_LINENUMBER_ARG if ( ! editor_lineno_arg ) editor_lineno_arg = DEFAULT_EDITOR_LINENUMBER_ARG ; #endif if ( ! editor_lineno_arg ) { pg_log_error ( " " ) ; return false ; } } #ifndef WIN32 if ( lineno > 0 ) sys = psprintf ( " " , editorName , editor_lineno_arg , lineno , fname ) ; else sys = psprintf ( " " , editorName , fname ) ; #else if ( lineno > 0 ) sys = psprintf ( " \" \" \" \" " , editorName , editor_lineno_arg , lineno , fname ) ; else sys = psprintf ( " \" \" \" \" " , editorName , fname ) ; #endif result = system ( sys ) ; if ( result == -1 ) pg_log_error ( " \" \" " , editorName ) ; else if ( result == 127 ) pg_log_error ( " " ) ; free ( sys ) ; return result == 0 ; }
helper for do_edit(): actually invoke the editor
 Returns true on success, false if we failed to invoke the editor or
 it returned nonzero status.
helper for do_edit () : actually invoke the editor Returns true on success false if we failed to invoke the editor or it returned nonzero status .
/* Find an editor to use */ /* Get line number argument, if we need it. */ /*
	 * On Unix the EDITOR value should *not* be quoted, since it might include
	 * switches, eg, EDITOR="pico -t"; it's up to the user to put quotes in it
	 * if necessary.  But this policy is not very workable on Windows, due to
	 * severe brain damage in their command shell plus the fact that standard
	 * program paths include spaces.
	 */