From 282ea1a37ec1ad66b1f1074eeb58db296cb85fb0 Mon Sep 17 00:00:00 2001 From: Steffen Nurpmeso Date: Sun, 27 Jul 2014 01:01:28 +0200 Subject: [PATCH] Preproc: use file_case::muxer() for ARGV: refer/ --- src/preproc/refer/command.cpp | 30 ++++++++++++----------- src/preproc/refer/refer.cpp | 55 ++++++++++++++++++++----------------------- 2 files changed, 41 insertions(+), 44 deletions(-) diff --git a/src/preproc/refer/command.cpp b/src/preproc/refer/command.cpp index 01de11f5..afd8ec6e 100644 --- a/src/preproc/refer/command.cpp +++ b/src/preproc/refer/command.cpp @@ -22,6 +22,7 @@ Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */ #include "refer.h" #include "refid.h" #include "search.h" +#include "file_case.h" #include "command.h" cset cs_field_name = csalpha; @@ -148,35 +149,35 @@ int input_stack::peek_char() void input_stack::push_file(const char *fn) { - FILE *fp; + file_case *fcp; if (strcmp(fn, "-") == 0) { - fp = stdin; + fcp = new file_case(stdin, "stdin", + fcp->fc_dont_close | fcp->fc_const_path /*| fcp->fc_have_stdio*/); fn = ""; - } - else { - errno = 0; - fp = fopen(fn, "r"); - if (fp == 0) { + } else { + fcp = file_case::muxer(fn); + if (fcp == NULL) { error("can't open `%1': %2", fn, strerror(errno)); return; } } + string buf; int bol = 1; int lineno = 1; for (;;) { - int c = getc(fp); + int c = fcp->get_c(); if (bol && c == '.') { // replace lines beginning with .R1 or .R2 with a blank line - c = getc(fp); + c = fcp->get_c(); if (c == 'R') { - c = getc(fp); + c = fcp->get_c(); if (c == '1' || c == '2') { int cc = c; - c = getc(fp); + c = fcp->get_c(); if (compatible_flag || c == ' ' || c == '\n' || c == EOF) { while (c != '\n' && c != EOF) - c = getc(fp); + c = fcp->get_c(); } else { buf += '.'; @@ -207,8 +208,9 @@ void input_stack::push_file(const char *fn) bol = 0; } } - if (fp != stdin) - fclose(fp); + + delete fcp; + if (buf.length() > 0 && buf[buf.length() - 1] != '\n') buf += '\n'; input_item *it = new input_item(buf, fn); diff --git a/src/preproc/refer/refer.cpp b/src/preproc/refer/refer.cpp index b593d2c9..c9b1620b 100644 --- a/src/preproc/refer/refer.cpp +++ b/src/preproc/refer/refer.cpp @@ -24,6 +24,7 @@ Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */ #include "ref.h" #include "token.h" #include "search.h" +#include "file_case.h" #include "command.h" extern "C" const char *Version_string; @@ -422,18 +423,13 @@ static int is_list(const string &str) static void do_file(const char *filename) { - FILE *fp; - if (strcmp(filename, "-") == 0) { - fp = stdin; - } - else { - errno = 0; - fp = fopen(filename, "r"); - if (fp == 0) { - error("can't open `%1': %2", filename, strerror(errno)); - return; - } + file_case *fcp; + if ((fcp = file_case::muxer(filename)) == NULL) { + assert(strcmp(filename, "-")); + error("can't open `%1': %2", filename, strerror(errno)); + return; } + current_filename = filename; fprintf(outfp, ".lf 1 %s\n", filename); string line; @@ -441,7 +437,7 @@ static void do_file(const char *filename) for (;;) { line.clear(); for (;;) { - int c = getc(fp); + int c = fcp->get_c(); if (c == EOF) { if (line.length() > 0) line += '\n'; @@ -466,7 +462,7 @@ static void do_file(const char *filename) string post; string pre(line.contents() + 2, line.length() - 3); for (;;) { - int c = getc(fp); + int c = fcp->get_c(); if (c == EOF) { error_with_file_and_line(current_filename, start_lineno, "missing `.]' line"); @@ -475,9 +471,9 @@ static void do_file(const char *filename) if (start_of_line) current_lineno++; if (start_of_line && c == '.') { - int d = getc(fp); + int d = fcp->get_c(); if (d == ']') { - while ((d = getc(fp)) != '\n' && d != EOF) { + while ((d = fcp->get_c()) != '\n' && d != EOF) { if (invalid_input_char(d)) error("invalid input character code %1", d); else @@ -486,7 +482,7 @@ static void do_file(const char *filename) break; } if (d != EOF) - ungetc(d, fp); + fcp->unget_c(d); } if (invalid_input_char(c)) error("invalid input character code %1", c); @@ -549,21 +545,20 @@ static void do_file(const char *filename) int start_of_line = 1; int start_lineno = current_lineno; for (;;) { - int c = getc(fp); + int c = fcp->get_c(); if (c != EOF && start_of_line) current_lineno++; if (start_of_line && c == '.') { - c = getc(fp); - if (c == 'R') { - c = getc(fp); - if (c == '2') { - c = getc(fp); - if (compatible_flag || c == ' ' || c == '\n' || c == EOF) { - while (c != EOF && c != '\n') - c = getc(fp); - break; - } - else { + c = fcp->get_c(); + if (c == 'R') { + c = fcp->get_c(); + if (c == '2') { + c = fcp->get_c(); + if (compatible_flag || c == ' ' || c == '\n' || c == EOF) { + while (c != EOF && c != '\n') + c = fcp->get_c(); + break; + } else { line += '.'; line += 'R'; line += '2'; @@ -604,8 +599,8 @@ static void do_file(const char *filename) } need_syncing = 0; output_pending_line(); - if (fp != stdin) - fclose(fp); + + delete fcp; } class label_processing_state { -- 2.11.4.GIT