*** a/doc/src/sgml/ref/psql-ref.sgml
--- b/doc/src/sgml/ref/psql-ref.sgml
***************
*** 3298,3303 **** testdb=> INSERT INTO my_table VALUES (:'content');
--- 3298,3308 ----
+ %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,234 ----
{
PsqlScanResult scan_result;
promptStatus_t prompt_tmp = prompt_status;
+ char *tmp = line;
+ int newline = 0;
scan_result = psql_scan(scan_state, query_buf, &prompt_tmp);
prompt_status = prompt_tmp;
***************
*** 235,240 **** MainLoop(FILE *source)
--- 239,265 ----
exit(EXIT_FAILURE);
}
+ /* Count the number of new line for calculate ofline number */
+ while (*tmp != '\0' && scan_result != PSCAN_INCOMPLETE)
+ {
+ if (*(tmp++) == '\n')
+ newline++;
+ }
+
+ /* Calculate the line number */
+ if (scan_result != PSCAN_INCOMPLETE)
+ {
+ /* The one new line is always added to tail of query_buf */
+ newline = (newline != 0) ? (newline + 1) : 1;
+ cur_line += newline;
+ }
+
+ /* Avoid cur_line and newline exceeds the INT_MAX */
+ if (cur_line >= INT_MAX || cur_line < 0)
+ {
+ cur_line = INT_MAX - 1;
+ }
+
/*
* Send command if semicolon found, or if end of line and we're in
* single-line mode.
***************
*** 256,261 **** MainLoop(FILE *source)
--- 281,287 ----
/* 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)
--- 329,335 ----
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 */