*** a/doc/src/sgml/ref/psql-ref.sgml
--- b/doc/src/sgml/ref/psql-ref.sgml
***************
*** 3312,3317 **** testdb=> INSERT INTO my_table VALUES (:'content');
--- 3312,3322 ----
+ %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;
+ cur_line = 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,267 ----
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')
+ cur_line++;
+ }
+
+ /* The one new line is always added to tail of query_buf */
+ if (scan_result != PSCAN_INCOMPLETE)
+ cur_line++;
+
+ /*
+ * If we overflow, then we start at INT_MIN and move towards 0. So
+ * to get +ve wrap-around line number we have to add INT_MAX + 2 to
+ * this number. We add 2 due to the fact that we have difference
+ * of 1 in absolute value of INT_MIN and INT_MAX and another 1 as
+ * line number starts at one and not at zero.
+ */
+ if (cur_line < 0)
+ cur_line += INT_MAX + 2;
+
/*
* Send command if semicolon found, or if end of line and we're in
* single-line mode.
***************
*** 256,261 **** MainLoop(FILE *source)
--- 283,289 ----
/* execute query */
success = SendQuery(query_buf->data);
slashCmdStatus = success ? PSQL_CMD_SEND : PSQL_CMD_ERROR;
+ cur_line = 1;
/* transfer query to previous_buf by pointer-swapping */
{
***************
*** 303,308 **** MainLoop(FILE *source)
--- 331,337 ----
query_buf : previous_buf);
success = slashCmdStatus != PSQL_CMD_ERROR;
+ cur_line = 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, "%d", cur_line);
+ break;
case '?':
/* not here yet */
break;
*** a/src/bin/psql/prompt.h
--- b/src/bin/psql/prompt.h
***************
*** 22,25 **** typedef enum _promptStatus
--- 22,28 ----
char *get_prompt(promptStatus_t status);
+ /* Current line number */
+ int cur_line;
+
#endif /* PROMPT_H */