Hi,
On Fri, Jul 11, 2014 at 3:13 PM, Sawada Masahiko <sawada.m...@gmail.com> wrote: > > > > > > To my understating cleanly, you means that line number is not changed > when newline has reached to INT_MAX, is incorrect? > As per my thinking yes. > And the line number should be switched to 1 when line number has > reached to INT_MAX? > Yes, when it goes beyond INT_MAX, wrap around to 1. BTW, I wonder, can't we simply use unsigned int instead? Also, what is the behaviour on LINE n, in error message in case of such wrap-around? > > > > > Or much better, simply get rid of newline, and use cur_line itself, will > > this work well for you? > > this is better. I will change code to this. > Thanks. > I will fix it. > Meanwhile I have tried this. Attached patch to have your suggestion on that. Thanks -- Jeevan B Chalke Principal Software Engineer, Product Development EnterpriseDB Corporation The Enterprise PostgreSQL Company
diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml index 255e8ca..030f4d0 100644 --- a/doc/src/sgml/ref/psql-ref.sgml +++ b/doc/src/sgml/ref/psql-ref.sgml @@ -3298,6 +3298,11 @@ testdb=> <userinput>INSERT INTO my_table VALUES (:'content');</userinput> </varlistentry> <varlistentry> + <term><literal>%l</literal></term> + <listitem><para>The current line number</para></listitem> + </varlistentry> + + <varlistentry> <term><literal>%</literal><replaceable class="parameter">digits</replaceable></term> <listitem> <para> diff --git a/src/bin/psql/mainloop.c b/src/bin/psql/mainloop.c index c3aff20..675b550 100644 --- a/src/bin/psql/mainloop.c +++ b/src/bin/psql/mainloop.c @@ -8,6 +8,7 @@ #include "postgres_fe.h" #include "mainloop.h" +#include <limits.h> #include "command.h" #include "common.h" @@ -58,6 +59,7 @@ MainLoop(FILE *source) 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,6 +227,7 @@ MainLoop(FILE *source) { 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,6 +238,30 @@ MainLoop(FILE *source) 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,6 +283,7 @@ MainLoop(FILE *source) /* 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,6 +331,7 @@ MainLoop(FILE *source) 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) diff --git a/src/bin/psql/prompt.c b/src/bin/psql/prompt.c index 26fca04..6a62e5f 100644 --- a/src/bin/psql/prompt.c +++ b/src/bin/psql/prompt.c @@ -44,6 +44,7 @@ * 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,6 +230,9 @@ get_prompt(promptStatus_t status) } break; + case 'l': + sprintf(buf, "%d", cur_line); + break; case '?': /* not here yet */ break; diff --git a/src/bin/psql/prompt.h b/src/bin/psql/prompt.h index 4d2f7e3..f1f80d2 100644 --- a/src/bin/psql/prompt.h +++ b/src/bin/psql/prompt.h @@ -22,4 +22,7 @@ typedef enum _promptStatus char *get_prompt(promptStatus_t status); +/* Current line number */ +int cur_line; + #endif /* PROMPT_H */
-- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers