psql_ir.patch
text/x-patch
Filename: psql_ir.patch
Type: text/x-patch
Part: 0
Patch
Same data as JSON:
GET /api/v1/attachments/:id/patch
the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes.
API reference →
Format: unified
| File | + | − |
|---|---|---|
| doc/src/sgml/ref/psql-ref.sgml | 16 | 0 |
| src/bin/psql/command.c | 14 | 2 |
| src/bin/psql/settings.h | 1 | 1 |
diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml
index ac351d3..e37f75f 100644
--- a/doc/src/sgml/ref/psql-ref.sgml
+++ b/doc/src/sgml/ref/psql-ref.sgml
@@ -1628,6 +1628,22 @@ Tue Oct 26 21:40:57 CEST 1999
<varlistentry>
+ <term><literal>\ir <replaceable class="parameter">filename</replaceable></literal></term>
+ <listitem>
+ <para>
+ When used within a script, if the <replaceable class="parameter">filename</replaceable>
+ uses relative path notation, then the file will be looked up relative to currently
+ executing file's location.
+
+ If the <replaceable class="parameter">filename</replaceable> uses an absolute path
+ notation, or if this command is being used in interactive mode, then it behaves the
+ same as <literal>\i</> command.
+ </para>
+ </listitem>
+ </varlistentry>
+
+
+ <varlistentry>
<term><literal>\l</literal> (or <literal>\list</literal>)</term>
<term><literal>\l+</literal> (or <literal>\list+</literal>)</term>
<listitem>
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index 5eefcbf..ee887f0 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -1974,7 +1974,7 @@ do_edit(const char *filename_arg, PQExpBuffer query_buf,
* process_file
*
* Read commands from filename and then them to the main processing loop
- * Handler for \i, but can be used for other things as well. Returns
+ * Handler for \i and \ir, but can be used for other things as well. Returns
* MainLoop() error code.
*
* If use_relative_path is true and filename is not an absolute path, then open
@@ -1986,6 +1986,7 @@ process_file(char *filename, bool single_txn, bool use_relative_path)
FILE *fd;
int result;
char *oldfilename;
+ char *relative_file = NULL;
PGresult *res;
if (!filename)
@@ -1995,6 +1996,14 @@ process_file(char *filename, bool single_txn, bool use_relative_path)
{
canonicalize_path(filename);
+ /*
+ * If the currently processing file uses \ir command, and the parameter
+ * to the command is a relative file path, then we resolve this path
+ * relative to currently processing file.
+ *
+ * If the \ir command was executed in interactive mode (i.e. not in a
+ * script) the we treat it the same as \i command.
+ */
if (use_relative_path && pset.inputfile)
{
char *last_slash;
@@ -2007,7 +2016,7 @@ process_file(char *filename, bool single_txn, bool use_relative_path)
size_t dir_len = (last_slash - pset.inputfile) + 1;
size_t file_len = strlen(filename);
- char *relative_file = pg_malloc(dir_len + 1 + file_len + 1);
+ relative_file = pg_malloc(dir_len + 1 + file_len + 1);
relative_file[0] = '\0';
strncat(relative_file, pset.inputfile, dir_len);
@@ -2026,6 +2035,9 @@ process_file(char *filename, bool single_txn, bool use_relative_path)
if (!fd)
{
+ if (relative_path != NULL)
+ free(relative_path);
+
psql_error("%s: %s\n", filename, strerror(errno));
return EXIT_FAILURE;
}
diff --git a/src/bin/psql/settings.h b/src/bin/psql/settings.h
index 7228f9d..2e28d86 100644
--- a/src/bin/psql/settings.h
+++ b/src/bin/psql/settings.h
@@ -81,7 +81,7 @@ typedef struct _psqlSettings
bool cur_cmd_interactive;
int sversion; /* backend server version */
const char *progname; /* in case you renamed psql */
- char *inputfile; /* for error reporting */
+ char *inputfile; /* File being currently processed, if any */
char *dirname; /* current directory for \s display */
uint64 lineno; /* also for error reporting */