-
Notifications
You must be signed in to change notification settings - Fork 6.1k
add a way to read from stdin in combination with other input_files args #622
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -306,9 +306,35 @@ void CommandLineInterface::handleFormal() | |
| cout << "Formal version:" << endl << m_compiler->formalTranslation() << endl; | ||
| } | ||
|
|
||
| void CommandLineInterface::readInputFilesAndConfigureRemappings() | ||
| void CommandLineInterface::readInputFilesAndConfigureRemappings(bool usestdin, vector<string> inputFiles) | ||
| { | ||
| if (!m_args.count("input-file")) | ||
| for (string path: inputFiles) | ||
| { | ||
| auto eq = find(path.begin(), path.end(), '='); | ||
| if (eq != path.end()) | ||
| path = string(eq + 1, path.end()); | ||
| else | ||
| { | ||
| auto infile = boost::filesystem::path(path); | ||
| if (!boost::filesystem::exists(infile)) | ||
| { | ||
| cerr << "Skipping non existant input file \"" << infile << "\"" << endl; | ||
| continue; | ||
| } | ||
|
|
||
| if (!boost::filesystem::is_regular_file(infile)) | ||
| { | ||
| cerr << "\"" << infile << "\" is not a valid file. Skipping" << endl; | ||
| continue; | ||
| } | ||
|
|
||
| m_sourceCodes[infile.string()] = dev::contentsString(infile.string()); | ||
| path = boost::filesystem::canonical(infile).string(); | ||
| } | ||
| m_allowedDirectories.push_back(boost::filesystem::path(path).remove_filename()); | ||
| } | ||
|
|
||
| if (usestdin) | ||
| { | ||
| string s; | ||
| while (!cin.eof()) | ||
|
|
@@ -317,32 +343,6 @@ void CommandLineInterface::readInputFilesAndConfigureRemappings() | |
| m_sourceCodes[g_stdinFileName].append(s + '\n'); | ||
| } | ||
| } | ||
| else | ||
| for (string path: m_args["input-file"].as<vector<string>>()) | ||
| { | ||
| auto eq = find(path.begin(), path.end(), '='); | ||
| if (eq != path.end()) | ||
| path = string(eq + 1, path.end()); | ||
| else | ||
| { | ||
| auto infile = boost::filesystem::path(path); | ||
| if (!boost::filesystem::exists(infile)) | ||
| { | ||
| cerr << "Skipping non existant input file \"" << infile << "\"" << endl; | ||
| continue; | ||
| } | ||
|
|
||
| if (!boost::filesystem::is_regular_file(infile)) | ||
| { | ||
| cerr << "\"" << infile << "\" is not a valid file. Skipping" << endl; | ||
| continue; | ||
| } | ||
|
|
||
| m_sourceCodes[infile.string()] = dev::contentsString(infile.string()); | ||
| path = boost::filesystem::canonical(infile).string(); | ||
| } | ||
| m_allowedDirectories.push_back(boost::filesystem::path(path).remove_filename()); | ||
| } | ||
| } | ||
|
|
||
| bool CommandLineInterface::parseLibraryOption(string const& _input) | ||
|
|
@@ -402,6 +402,8 @@ outputs the components specified in the options at standard output or in files i | |
| the output directory, if specified. | ||
| Example: solc --bin -o /tmp/solcoutput contract.sol | ||
|
|
||
| When - is specified as one of the input files it will add the current directory to the allowed directories and read from standard input | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the justification for adding the current directory to the allowed directories if you use stdin? |
||
|
|
||
| Allowed options)", | ||
| po::options_description::m_default_line_length, | ||
| po::options_description::m_default_line_length - 23); | ||
|
|
@@ -509,7 +511,32 @@ Allowed options)", | |
|
|
||
| bool CommandLineInterface::processInput() | ||
| { | ||
| readInputFilesAndConfigureRemappings(); | ||
| // determine if we should use stdin | ||
| bool usestdin = false; | ||
| vector<string> inputFiles; | ||
|
|
||
| if (!m_args.count("input-file")) { | ||
| inputFiles = vector<string>(); | ||
| usestdin = true; | ||
| } else { | ||
| inputFiles = m_args["input-file"].as<vector<string>>(); | ||
|
|
||
| if (inputFiles.size() == 0) | ||
| usestdin = true; | ||
| else | ||
| // search for "-" in input files to enable stdin (and filter out the "-") | ||
| for (unsigned idx = 0; idx < inputFiles.size(); ++idx) { | ||
| string path = inputFiles[idx]; | ||
| if (path == "-") { | ||
| inputFiles.erase(inputFiles.begin() + idx); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be better to loop through Also the current routine does not work correctly if you specify |
||
| usestdin = true; | ||
| m_allowedDirectories.push_back(boost::filesystem::current_path()); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| readInputFilesAndConfigureRemappings(usestdin, inputFiles); | ||
|
|
||
| if (m_args.count("libraries")) | ||
| for (string const& library: m_args["libraries"].as<vector<string>>()) | ||
|
|
@@ -564,8 +591,8 @@ bool CommandLineInterface::processInput() | |
| auto scannerFromSourceName = [&](string const& _sourceName) -> solidity::Scanner const& { return m_compiler->scanner(_sourceName); }; | ||
| try | ||
| { | ||
| if (m_args.count("input-file")) | ||
| m_compiler->setRemappings(m_args["input-file"].as<vector<string>>()); | ||
| if (inputFiles.size()) | ||
| m_compiler->setRemappings(inputFiles); | ||
| for (auto const& sourceCode: m_sourceCodes) | ||
| m_compiler->addSource(sourceCode.first, sourceCode.second); | ||
| // TODO: Perhaps we should not compile unless requested | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Arguments should have
_as prefix and vectors should be passed by const reference.