From f0982318065617e92c28e23ee382981c2a898ca2 Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Sun, 31 Oct 2021 22:35:41 -0400 Subject: [PATCH] sim: ppc: handle \r in igen inputs [PR sim/28476] Make sure we consume & ignore \r bytes in inputs in case the file encodings are from a non-LF systems (e.g. Windows). --- sim/ppc/table.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/sim/ppc/table.c b/sim/ppc/table.c index 3c7e4464beb..6399bdaddcb 100644 --- a/sim/ppc/table.c +++ b/sim/ppc/table.c @@ -208,7 +208,7 @@ table_entry_read(table *root) /* break the line into its colon delimitered fields */ for (field = 0; field < file->nr_fields-1; field++) { entry->fields[field] = file->pos; - while(*file->pos && *file->pos != ':' && *file->pos != '\n') + while(*file->pos && *file->pos != ':' && *file->pos != '\n' && *file->pos != '\r') file->pos++; if (*file->pos == ':') { *file->pos = '\0'; @@ -219,7 +219,11 @@ table_entry_read(table *root) /* any trailing stuff not the last field */ ASSERT(field == file->nr_fields-1); entry->fields[field] = file->pos; - while (*file->pos && *file->pos != '\n') { + while (*file->pos && *file->pos != '\n' && *file->pos != '\r') { + file->pos++; + } + if (*file->pos == '\r') { + *file->pos = '\0'; file->pos++; } if (*file->pos == '\n') { @@ -243,7 +247,7 @@ table_entry_read(table *root) file->pos++; for (field = 0; field < file->nr_model_fields-1; field++) { model->fields[field] = file->pos; - while(*file->pos && *file->pos != ':' && *file->pos != '\n') + while(*file->pos && *file->pos != ':' && *file->pos != '\n' && *file->pos != '\r') file->pos++; if (*file->pos == ':') { *file->pos = '\0'; @@ -254,7 +258,11 @@ table_entry_read(table *root) /* any trailing stuff not the last field */ ASSERT(field == file->nr_model_fields-1); model->fields[field] = file->pos; - while (*file->pos && *file->pos != '\n') { + while (*file->pos && *file->pos != '\n' && *file->pos != '\r') { + file->pos++; + } + if (*file->pos == '\r') { + *file->pos = '\0'; file->pos++; } if (*file->pos == '\n') { @@ -274,13 +282,13 @@ table_entry_read(table *root) do { do { file->pos++; - } while (*file->pos != '\0' && *file->pos != '\n'); - if (*file->pos == '\n') { + } while (*file->pos != '\0' && *file->pos != '\n' && *file->pos != '\r'); + if (*file->pos == '\n' || *file->pos == '\r') { char *save_pos = ++file->pos; int extra_lines = 0; file->line_nr++; /* Allow tab indented to have blank lines */ - while (*save_pos == '\n') { + while (*save_pos == '\n' || *save_pos == '\r') { save_pos++; extra_lines++; } @@ -290,7 +298,7 @@ table_entry_read(table *root) } } } while (*file->pos != '\0' && *file->pos == '\t'); - if (file->pos[-1] == '\n') + if (file->pos[-1] == '\n' || file->pos[-1] == '\r') file->pos[-1] = '\0'; } else -- 2.30.2