src/bin/psql/command.c
static backslashResult
exec_command_edit(PsqlScanState scan_state, bool active_branch,
				  PQExpBuffer query_buf, PQExpBuffer previous_buf)
{
	backslashResult status = PSQL_CMD_SKIP_LINE;

	if (active_branch)
	{
		if (!query_buf)
		{
			pg_log_error("no query buffer");
			status = PSQL_CMD_ERROR;
		}
		else
		{
			char	   *fname;
			char	   *ln = NULL;
			int			lineno = -1;

			fname = psql_scan_slash_option(scan_state,
										   OT_NORMAL, NULL, true);
			if (fname)
			{
				/* try to get separate lineno arg */
				ln = psql_scan_slash_option(scan_state,
											OT_NORMAL, NULL, true);
				if (ln == NULL)
				{
					/* only one arg; maybe it is lineno not fname */
					if (fname[0] &&
						strspn(fname, "0123456789") == strlen(fname))
					{
						/* all digits, so assume it is lineno */
						ln = fname;
						fname = NULL;
					}
				}
			}
			if (ln)
			{
				lineno = atoi(ln);
				if (lineno < 1)
				{
					pg_log_error("invalid line number: %s", ln);
					status = PSQL_CMD_ERROR;
				}
			}
			if (status != PSQL_CMD_ERROR)
			{
				bool		discard_on_quit;

				expand_tilde(&fname);
				if (fname)
				{
					canonicalize_path(fname);
					/* Always clear buffer if the file isn't modified */
					discard_on_quit = true;
				}
				else
				{
					/*
					 * If query_buf is empty, recall previous query for
					 * editing.  But in that case, the query buffer should be
					 * emptied if editing doesn't modify the file.
					 */
					discard_on_quit = copy_previous_query(query_buf,
														  previous_buf);
				}

				if (do_edit(fname, query_buf, lineno, discard_on_quit, NULL))
					status = PSQL_CMD_NEWEDIT;
				else
					status = PSQL_CMD_ERROR;
			}
			if (fname)
				free(fname);
			if (ln)
				free(ln);
		}
	}
	else
		ignore_slash_options(scan_state);

	return status;
}
/*
 * \e or \edit -- edit the current query buffer, or edit a file and
 * make it the query buffer
 */
\e or \edit -- edit the current query buffer, or edit a file and
make it the query buffer
\ e or \ edit -- edit the current query buffer or edit a file and make it the query buffer
static backslashResult
exec_command_edit(PsqlScanState scan_state, bool active_branch,
				  PQExpBuffer query_buf, PQExpBuffer previous_buf)
{
	backslashResult status = PSQL_CMD_SKIP_LINE;
	if (active_branch)
	{
		if (!query_buf)
		{
			pg_log_error("no query buffer");
			status = PSQL_CMD_ERROR;
		}
		else
		{
			char	   *fname;
			char	   *ln = NULL;
			int			lineno = -1;
			fname = psql_scan_slash_option(scan_state,
										   OT_NORMAL, NULL, true);
			if (fname)
			{
				ln = psql_scan_slash_option(scan_state,
											OT_NORMAL, NULL, true);
				if (ln == NULL)
				{
					if (fname[0] &&
						strspn(fname, "0123456789") == strlen(fname))
					{
						ln = fname;
						fname = NULL;
					}
				}
			}
			if (ln)
			{
				lineno = atoi(ln);
				if (lineno < 1)
				{
					pg_log_error("invalid line number: %s", ln);
					status = PSQL_CMD_ERROR;
				}
			}
			if (status != PSQL_CMD_ERROR)
			{
				bool		discard_on_quit;
				expand_tilde(&fname);
				if (fname)
				{
					canonicalize_path(fname);
					discard_on_quit = true;
				}
				else
				{
					discard_on_quit = copy_previous_query(query_buf,
														  previous_buf);
				}
				if (do_edit(fname, query_buf, lineno, discard_on_quit, NULL))
					status = PSQL_CMD_NEWEDIT;
				else
					status = PSQL_CMD_ERROR;
			}
			if (fname)
				free(fname);
			if (ln)
				free(ln);
		}
	}
	else
		ignore_slash_options(scan_state);
	return status;
}
static backslashResult exec_command_edit ( PsqlScanState scan_state , bool active_branch , PQExpBuffer query_buf , PQExpBuffer previous_buf ) { backslashResult status = PSQL_CMD_SKIP_LINE ; if ( active_branch ) { if ( ! query_buf ) { pg_log_error ( " " ) ; status = PSQL_CMD_ERROR ; } else { char * fname ; char * ln = NULL ; int lineno = -1 ; fname = psql_scan_slash_option ( scan_state , OT_NORMAL , NULL , true ) ; if ( fname ) { ln = psql_scan_slash_option ( scan_state , OT_NORMAL , NULL , true ) ; if ( ln == NULL ) { if ( fname [ 0 ] && strspn ( fname , " " ) == strlen ( fname ) ) { ln = fname ; fname = NULL ; } } } if ( ln ) { lineno = atoi ( ln ) ; if ( lineno < 1 ) { pg_log_error ( " " , ln ) ; status = PSQL_CMD_ERROR ; } } if ( status != PSQL_CMD_ERROR ) { bool discard_on_quit ; expand_tilde ( & fname ) ; if ( fname ) { canonicalize_path ( fname ) ; discard_on_quit = true ; } else { discard_on_quit = copy_previous_query ( query_buf , previous_buf ) ; } if ( do_edit ( fname , query_buf , lineno , discard_on_quit , NULL ) ) status = PSQL_CMD_NEWEDIT ; else status = PSQL_CMD_ERROR ; } if ( fname ) free ( fname ) ; if ( ln ) free ( ln ) ; } } else ignore_slash_options ( scan_state ) ; return status ; }
\e or \edit -- edit the current query buffer, or edit a file and
 make it the query buffer
\ e or \ edit -- edit the current query buffer or edit a file and make it the query buffer
/* try to get separate lineno arg */ /* only one arg; maybe it is lineno not fname */ /* all digits, so assume it is lineno */ /* Always clear buffer if the file isn't modified */ /*
					 * If query_buf is empty, recall previous query for
					 * editing.  But in that case, the query buffer should be
					 * emptied if editing doesn't modify the file.
					 */