diff --git a/source/common.h b/source/common.h index 2b46e9d2ba..89657317e1 100644 --- a/source/common.h +++ b/source/common.h @@ -799,7 +799,9 @@ class cmdline_processor [](auto& a, auto& b){ return a.group < b.group || (a.group == b.group && a.name < b.name); } ); - print("\nUsage: cppfront [options] file ...\n\nOptions:\n"); + print("\nUsage: cppfront [options] file ...\n"); + print("\n file source file(s) (can be 'stdin')\n"); + print("\nOptions: \n"); int last_group = -1; for (auto& flag : flags) { // Skip hidden flags diff --git a/source/cppfront.cpp b/source/cppfront.cpp index 15226521da..573e312d51 100644 --- a/source/cppfront.cpp +++ b/source/cppfront.cpp @@ -79,7 +79,12 @@ auto main( auto& out = flag_cpp1_filename != "stdout" ? std::cout : std::cerr; - if (!flag_quiet) { + if ( + !flag_quiet + && arg.text != "stdin" + && flag_cpp1_filename != "stdout" + ) + { out << arg.text << "..."; } @@ -92,7 +97,10 @@ auto main( // If there were no errors, say so and generate Cpp1 if (c.had_no_errors()) { - if (!flag_quiet) + if ( + !flag_quiet + && flag_cpp1_filename != "stdout" + ) { if (!c.has_cpp1()) { out << " ok (all Cpp2, passes safety checks)\n"; diff --git a/source/io.h b/source/io.h index 9da2128e7c..f950c52a94 100644 --- a/source/io.h +++ b/source/io.h @@ -882,11 +882,17 @@ class source ) -> bool { - std::ifstream in{ filename }; - if (!in.is_open()) { - return false; + // If filename is stdin, we read from stdin, otherwise we try to read the file + // + auto is_stdin = filename == "stdin"; + std::ifstream fss; + if (!is_stdin) + { + fss.open(filename); + if( !fss.is_open()) { return false; } } - + std::istream& in = is_stdin ? std::cin : fss; + auto in_comment = false; auto in_string_literal = false; auto in_raw_string_literal = false; diff --git a/source/to_cpp1.h b/source/to_cpp1.h index 73b904ecba..480c9b00fe 100644 --- a/source/to_cpp1.h +++ b/source/to_cpp1.h @@ -1179,6 +1179,7 @@ class cppfront if ( !sourcefile.ends_with(".cpp2") && !sourcefile.ends_with(".h2") + && sourcefile != "stdin" ) { errors.emplace_back( @@ -1257,14 +1258,18 @@ class cppfront } // Now we'll open the Cpp1 file - auto cpp1_filename = sourcefile.substr(0, std::ssize(sourcefile) - 1); + // Default to stdout if input is stdin + auto cpp1_filename = std::string{"stdout"}; + if (sourcefile != "stdin") { + assert(sourcefile.ends_with("2")); + cpp1_filename = sourcefile.substr(0, std::ssize(sourcefile) - 1); + } - // Use explicit filename override if present, - // otherwise strip leading path + // Use explicit filename override if present, otherwise strip leading path if (!flag_cpp1_filename.empty()) { cpp1_filename = flag_cpp1_filename; } - else { + else if (cpp1_filename != "stdout") { cpp1_filename = std::filesystem::path(cpp1_filename).filename().string(); }