int error_detection_offset = expr_scanner_offset(state) - 1;
YYSTYPE lval;
char *full_line;
- size_t l;
/*
* While parsing an expression, we may not have collected the whole line
/* skip */ ;
}
+ /* Extract the line, trimming trailing newline if any */
full_line = expr_scanner_get_substring(state,
expr_start_offset,
- expr_scanner_offset(state));
- /* Trim trailing newline if any */
- l = strlen(full_line);
- while (l > 0 && full_line[l - 1] == '\n')
- full_line[--l] = '\0';
+ expr_scanner_offset(state),
+ true);
syntax_error(expr_source, expr_lineno, full_line, expr_command,
message, more, error_detection_offset - expr_start_offset);
/*
* Get a malloc'd copy of the lexer input string from start_offset
- * to just before end_offset.
+ * to just before end_offset. If chomp is true, drop any trailing
+ * newline(s).
*/
char *
expr_scanner_get_substring(PsqlScanState state,
- int start_offset, int end_offset)
+ int start_offset, int end_offset,
+ bool chomp)
{
char *result;
+ const char *scanptr = state->scanbuf + start_offset;
int slen = end_offset - start_offset;
Assert(slen >= 0);
Assert(end_offset <= strlen(state->scanbuf));
+
+ if (chomp)
+ {
+ while (slen > 0 &&
+ (scanptr[slen - 1] == '\n' || scanptr[slen - 1] == '\r'))
+ slen--;
+ }
+
result = (char *) pg_malloc(slen + 1);
- memcpy(result, state->scanbuf + start_offset, slen);
+ memcpy(result, scanptr, slen);
result[slen] = '\0';
return result;
PQExpBufferData word_buf;
int word_offset;
int offsets[MAX_ARGS]; /* offsets of argument words */
- int start_offset,
- end_offset;
+ int start_offset;
int lineno;
int j;
my_command->expr = expr_parse_result;
- /* Get location of the ending newline */
- end_offset = expr_scanner_offset(sstate) - 1;
-
- /* Save line */
+ /* Save line, trimming any trailing newline */
my_command->line = expr_scanner_get_substring(sstate,
start_offset,
- end_offset);
+ expr_scanner_offset(sstate),
+ true);
expr_scanner_finish(yyscanner);
my_command->argc++;
}
- /* Get location of the ending newline */
- end_offset = expr_scanner_offset(sstate) - 1;
-
- /* Save line */
+ /* Save line, trimming any trailing newline */
my_command->line = expr_scanner_get_substring(sstate,
start_offset,
- end_offset);
+ expr_scanner_offset(sstate),
+ true);
if (pg_strcasecmp(my_command->argv[0], "sleep") == 0)
{
extern void expr_scanner_finish(yyscan_t yyscanner);
extern int expr_scanner_offset(PsqlScanState state);
extern char *expr_scanner_get_substring(PsqlScanState state,
- int start_offset, int end_offset);
+ int start_offset, int end_offset,
+ bool chomp);
extern int expr_scanner_get_lineno(PsqlScanState state, int offset);
extern void syntax_error(const char *source, int lineno, const char *line,