*** a/doc/src/sgml/ref/psql-ref.sgml
--- b/doc/src/sgml/ref/psql-ref.sgml
***************
*** 3316,3321 **** testdb=> INSERT INTO my_table VALUES (:'content');
--- 3316,3326 ----
+ %l
+ The current line number
+
+
+
%digits
*** a/src/bin/psql/mainloop.c
--- b/src/bin/psql/mainloop.c
***************
*** 8,13 ****
--- 8,14 ----
#include "postgres_fe.h"
#include "mainloop.h"
+ #include
#include "command.h"
#include "common.h"
***************
*** 58,63 **** MainLoop(FILE *source)
--- 59,65 ----
pset.cur_cmd_source = source;
pset.cur_cmd_interactive = ((source == stdin) && !pset.notty);
pset.lineno = 0;
+ pset.cur_lineno = 1;
/* Create working state */
scan_state = psql_scan_create();
***************
*** 225,230 **** MainLoop(FILE *source)
--- 227,233 ----
{
PsqlScanResult scan_result;
promptStatus_t prompt_tmp = prompt_status;
+ char *tmp = line;
scan_result = psql_scan(scan_state, query_buf, &prompt_tmp);
prompt_status = prompt_tmp;
***************
*** 235,240 **** MainLoop(FILE *source)
--- 238,257 ----
exit(EXIT_FAILURE);
}
+ /*
+ * Increase current line number counter with the new lines present
+ * in the line buffer
+ */
+ while (*tmp != '\0' && scan_result != PSCAN_INCOMPLETE)
+ {
+ if (*(tmp++) == '\n')
+ pset.cur_lineno++;
+ }
+
+ /* The one new line is always added to tail of query_buf */
+ if (scan_result != PSCAN_INCOMPLETE)
+ pset.cur_lineno++;
+
/*
* Send command if semicolon found, or if end of line and we're in
* single-line mode.
***************
*** 256,261 **** MainLoop(FILE *source)
--- 273,279 ----
/* execute query */
success = SendQuery(query_buf->data);
slashCmdStatus = success ? PSQL_CMD_SEND : PSQL_CMD_ERROR;
+ pset.cur_lineno = 1;
/* transfer query to previous_buf by pointer-swapping */
{
***************
*** 303,308 **** MainLoop(FILE *source)
--- 321,327 ----
query_buf : previous_buf);
success = slashCmdStatus != PSQL_CMD_ERROR;
+ pset.cur_lineno = 1;
if ((slashCmdStatus == PSQL_CMD_SEND || slashCmdStatus == PSQL_CMD_NEWEDIT) &&
query_buf->len == 0)
*** a/src/bin/psql/prompt.c
--- b/src/bin/psql/prompt.c
***************
*** 44,49 ****
--- 44,50 ----
* in prompt2 -, *, ', or ";
* in prompt3 nothing
* %x - transaction status: empty, *, !, ? (unknown or no connection)
+ * %l - the line number
* %? - the error code of the last query (not yet implemented)
* %% - a percent sign
*
***************
*** 229,234 **** get_prompt(promptStatus_t status)
--- 230,238 ----
}
break;
+ case 'l':
+ sprintf(buf, "%u", pset.cur_lineno);
+ break;
case '?':
/* not here yet */
break;
*** a/src/bin/psql/settings.h
--- b/src/bin/psql/settings.h
***************
*** 88,93 **** typedef struct _psqlSettings
--- 88,94 ----
const char *progname; /* in case you renamed psql */
char *inputfile; /* file being currently processed, if any */
uint64 lineno; /* also for error reporting */
+ uint64 cur_lineno; /* current line number */
bool timing; /* enable timing of all queries */