[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 02/11] diagnostics: style: add caret_set_file
From: |
Akim Demaille |
Subject: |
[PATCH 02/11] diagnostics: style: add caret_set_file |
Date: |
Sat, 21 Sep 2019 11:59:55 +0200 |
To make the following commits easier to read.
* src/location.c (caret_set_file): New.
---
src/location.c | 66 ++++++++++++++++++++++++++++----------------------
1 file changed, 37 insertions(+), 29 deletions(-)
diff --git a/src/location.c b/src/location.c
index cfbd6a78..5b54e44d 100644
--- a/src/location.c
+++ b/src/location.c
@@ -159,6 +159,7 @@ location_print (location loc, FILE *out)
same file all over for each error. */
static struct
{
+ /* Raw input file. */
FILE *source;
/* The last file we tried to open. If non NULL, but SOURCE is NULL,
it means this file is special and should not be quoted. */
@@ -168,6 +169,41 @@ static struct
size_t offset;
} caret_info;
+
+static bool
+caret_set_file (const char *file)
+{
+ /* If a different source than before, close and let the rest open
+ the new one. */
+ if (caret_info.file && caret_info.file != file)
+ {
+ caret_free ();
+ caret_info.file = NULL;
+ }
+ if (!caret_info.file)
+ {
+ caret_info.file = file;
+ if ((caret_info.source = fopen (caret_info.file, "r")))
+ {
+ /* If the file is not regular (imagine #line 1 "/dev/stdin"
+ in the input file for instance), don't try to quote the
+ source. Keep caret_info.file set so that we don't try to
+ open it again, but leave caret_info.source NULL so that
+ we don't try to quote it. */
+ struct stat buf;
+ if (fstat (fileno (caret_info.source), &buf) == 0
+ && buf.st_mode & S_IFREG)
+ {
+ caret_info.line = 1;
+ caret_info.offset = 0;
+ }
+ else
+ caret_free ();
+ }
+ }
+ return caret_info.source;
+}
+
void
caret_free (void)
{
@@ -200,35 +236,7 @@ location_caret (location loc, const char *style, FILE *out)
{
if (loc.start.column == -1 || loc.start.line == -1)
return;
- /* If a different source than before, close and let the rest open
- the new one. */
- if (caret_info.file && caret_info.file != loc.start.file)
- {
- caret_free ();
- caret_info.file = NULL;
- }
- if (!caret_info.file)
- {
- caret_info.file = loc.start.file;
- if ((caret_info.source = fopen (caret_info.file, "r")))
- {
- /* If the file is not regular (imagine #line 1 "/dev/stdin"
- in the input file for instance), don't try to quote the
- source. Keep caret_info.file set so that we don't try to
- open it again, but leave caret_info.source NULL so that
- we don't try to quote it. */
- struct stat buf;
- if (fstat (fileno (caret_info.source), &buf) == 0
- && buf.st_mode & S_IFREG)
- {
- caret_info.line = 1;
- caret_info.offset = 0;
- }
- else
- caret_free ();
- }
- }
- if (!caret_info.source)
+ if (!caret_set_file (loc.start.file))
return;
--
2.23.0
- [PATCH 00/11] diagnostics: truncate quoted sources to fit the screen width, Akim Demaille, 2019/09/21
- [PATCH 01/11] diagnostics: style: minor changes, Akim Demaille, 2019/09/21
- [PATCH 04/11] diagnostics: style: use a boundary to track the caret_info, Akim Demaille, 2019/09/21
- [PATCH 02/11] diagnostics: style: add caret_set_file,
Akim Demaille <=
- [PATCH 07/11] diagnostics: truncate quoted sources to fit the screen, Akim Demaille, 2019/09/21
- [PATCH 03/11] diagnostics: extract boundary_compute from location_compute, Akim Demaille, 2019/09/21
- [PATCH 06/11] diagnostics: learn how to count column number with multibyte chars, Akim Demaille, 2019/09/21
- [PATCH 05/11] diagnostics: style: rename member for clariy, Akim Demaille, 2019/09/21
- [PATCH 09/11] diagnostics: also show truncation at the end of line with "...", Akim Demaille, 2019/09/21
- [PATCH 10/11] diagnostics: don't print ellipsis on the caret line, Akim Demaille, 2019/09/21
- [PATCH 11/11] diagnostics: get the screen width from the terminal, Akim Demaille, 2019/09/21
- [PATCH 08/11] diagnostics: check that quoted lines are truncated, Akim Demaille, 2019/09/21
- [PATCH 11/11] diagnotics: get the screen width from the terminal, Akim Demaille, 2019/09/21