From ea9a855bf0773f098bc459b34fff9475819c421c Mon Sep 17 00:00:00 2001 From: Daiki Ueno Date: Tue, 16 Apr 2013 15:17:14 +0900 Subject: [PATCH] Support CR/LF line terminators in Python sources even on Unix. --- gettext-tools/src/ChangeLog | 6 +++++ gettext-tools/src/x-python.c | 52 ++++++++++++++++++++++++++++-------- 2 files changed, 47 insertions(+), 11 deletions(-) diff --git a/gettext-tools/src/ChangeLog b/gettext-tools/src/ChangeLog index 4b92aeb87..9493a5ed7 100644 --- a/gettext-tools/src/ChangeLog +++ b/gettext-tools/src/ChangeLog @@ -1,3 +1,9 @@ +2013-04-22 Daiki Ueno + + Support CR/LF line terminators in Python sources even on Unix. + * x-python.c (phase0_getc, phase0_ungetc): New functions. + (phase1_getc): Use them instead of calling getc/ungetc directly. + 2013-04-22 Daiki Ueno Support explicit string concatenation in Python. diff --git a/gettext-tools/src/x-python.c b/gettext-tools/src/x-python.c index cdca2551c..a3a106cfc 100644 --- a/gettext-tools/src/x-python.c +++ b/gettext-tools/src/x-python.c @@ -149,6 +149,46 @@ static int line_number; static FILE *fp; +/* 0. Terminate line by \n, regardless whether the external + representation of a line terminator is CR (Mac), and CR/LF + (DOS/Windows), as Python treats them equally. */ +static int +phase0_getc () +{ + int c; + + c = getc (fp); + if (c == EOF) + { + if (ferror (fp)) + error (EXIT_FAILURE, errno, _("error while reading \"%s\""), + real_file_name); + return EOF; + } + + if (c == '\r') + { + int c1 = getc (fp); + + if (c1 != EOF && c1 != '\n') + ungetc (c1, fp); + + /* Seen line terminator CR or CR/LF. */ + return '\n'; + } + + return c; +} + +/* Supports only one pushback character, and not '\n'. */ +static inline void +phase0_ungetc (int c) +{ + if (c != EOF) + ungetc (c, fp); +} + + /* 1. line_number handling. */ /* Maximum used, roughly a safer MB_LEN_MAX. */ @@ -165,17 +205,7 @@ phase1_getc () if (phase1_pushback_length) c = phase1_pushback[--phase1_pushback_length]; else - { - c = getc (fp); - - if (c == EOF) - { - if (ferror (fp)) - error (EXIT_FAILURE, errno, _("error while reading \"%s\""), - real_file_name); - return EOF; - } - } + c = phase0_getc (); if (c == '\n') ++line_number; -- 2.47.2