*** 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(); *************** *** 204,213 **** MainLoop(FILE *source) /* insert newlines into query buffer between source lines */ if (query_buf->len > 0) - { - appendPQExpBufferChar(query_buf, '\n'); added_nl_pos = query_buf->len; - } else added_nl_pos = -1; /* flag we didn't add one */ --- 206,212 ---- *************** *** 225,230 **** MainLoop(FILE *source) --- 224,231 ---- { 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) --- 236,261 ---- exit(EXIT_FAILURE); } + /* Count the number of new line for calculate of line number */ + while (*tmp != '\0' && scan_result != PSCAN_INCOMPLETE) + { + if (*(tmp++) == '\n') + newline++; + } + + /* Calculate the line number */ + if (scan_result != PSCAN_INCOMPLETE) + { + newline = (newline != 0) ? newline : 1; + cur_line += newline; + } + + /* Avoid exceed the limit of integer */ + if (cur_line >= INT_MAX) + { + cur_line=1; + } + /* * Send command if semicolon found, or if end of line and we're in * single-line mode. *************** *** 256,261 **** MainLoop(FILE *source) --- 277,283 ---- /* 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 */ { *************** *** 284,290 **** MainLoop(FILE *source) */ if (query_buf->len == added_nl_pos) { ! query_buf->data[--query_buf->len] = '\0'; pg_send_history(history_buf); } added_nl_pos = -1; --- 306,312 ---- */ if (query_buf->len == added_nl_pos) { ! appendPQExpBufferChar(query_buf, '\0'); pg_send_history(history_buf); } added_nl_pos = -1; *************** *** 303,308 **** MainLoop(FILE *source) --- 325,331 ---- 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 */