Skip to content

Commit b30b76d

Browse files
Implement System Input Handling for CLI Execution (#384)
* Add CLI input handling and documentation * Fix clang-format
1 parent 1c65351 commit b30b76d

File tree

2 files changed

+72
-6
lines changed

2 files changed

+72
-6
lines changed

src/cli/clirunner.cpp

+61-5
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@
1111

1212
namespace Ripes {
1313

14-
// An extended QVariant-to-string convertion method which handles a few special
15-
// cases.
14+
/**
15+
* An extended QVariant-to-string convertion method which handles a special
16+
* cases such as QVariantMap and QStringList.
17+
*
18+
* @param v The QVariant to convert to a string.
19+
* @returns A string representation of the QVariant.
20+
*/
1621
static QString qVariantToString(QVariant &v) {
1722
QString def = v.toString();
1823
if (!def.isEmpty())
@@ -34,6 +39,14 @@ static QString qVariantToString(QVariant &v) {
3439
return def;
3540
}
3641

42+
/**
43+
* Constructor for the CLIRunner class.
44+
* Initializes the CLI runner for Ripes.
45+
* It configures the environment based on the provided options and prepares the
46+
* SystemIO streams for input and output redirection.
47+
*
48+
* @param options A struct containing the CLI options for Ripes.
49+
*/
3750
CLIRunner::CLIRunner(const CLIModeOptions &options)
3851
: QObject(), m_options(options) {
3952
info("Ripes CLI mode", false, true);
@@ -46,9 +59,18 @@ CLIRunner::CLIRunner(const CLIModeOptions &options)
4659
std::flush(std::cout);
4760
});
4861

49-
// TODO: how to handle system input?
62+
// Handle systemIO input in stdin
63+
SystemIO::setCLIInput();
5064
}
5165

66+
/**
67+
* Main execution method for the CLI runner.
68+
* Runs the CLI process in three phases: process input, run model, and post-run.
69+
* Checks after each phase that the execution was successful, and returns 1 if
70+
* an error occurs during any phase.
71+
*
72+
* @return 0 on success, or 1 if an error occurs during any phase.
73+
*/
5274
int CLIRunner::run() {
5375
if (processInput())
5476
return 1;
@@ -62,6 +84,13 @@ int CLIRunner::run() {
6284
return 0;
6385
}
6486

87+
/**
88+
* Processes the input file based on the file source type in the provided CLI
89+
* options. The method prepares the program for the execution by assembling,
90+
* compiling, or loading the input file (based on the source type).
91+
*
92+
* @return 0 on success, or 1 if an error occurs during input file processing.
93+
*/
6594
int CLIRunner::processInput() {
6695
info("Processing input file", false, true);
6796

@@ -157,11 +186,15 @@ int CLIRunner::processInput() {
157186
return 0;
158187
}
159188

189+
/**
190+
* Runs the processor model for the loaded program until the program is
191+
* finished (so ProcessorHandler::runFinished signal is emitted)
192+
*
193+
* @return 0 on success, or 1 if an error occurs during model execution.
194+
*/
160195
int CLIRunner::runModel() {
161196
info("Running model", false, true);
162197

163-
// Wait until receiving ProcessorHandler::runFinished signal
164-
// before proceeding.
165198
QEventLoop loop;
166199
QObject::connect(ProcessorHandler::get(), &ProcessorHandler::runFinished,
167200
&loop, &QEventLoop::quit);
@@ -212,6 +245,13 @@ int CLIRunner::runModel() {
212245
return 0;
213246
}
214247

248+
/**
249+
* Handles post-execution tasks.
250+
* Open output file (if specified) or defaults to stdout and prints telemetry
251+
* data in either JSON or unstructured format.
252+
*
253+
* @return 0 on success, or 1 if an error occurs during post run tasks.
254+
*/
215255
int CLIRunner::postRun() {
216256
info("Post-run", false, true);
217257

@@ -256,6 +296,17 @@ int CLIRunner::postRun() {
256296
return 0;
257297
}
258298

299+
/**
300+
* Outputs an informational message to the standard output.
301+
* For formatting purposes the message can include a header or a specified
302+
* prefix.
303+
*
304+
* @param msg The QString message to print.
305+
* @param alwaysPrint If true, the message is printed regardless if the verbose
306+
* option is disabled.
307+
* @param header If true, the message is surrounded by a decorative header.
308+
* @param prefix The prefix for the message, added only if "header" is false.
309+
*/
259310
void CLIRunner::info(QString msg, bool alwaysPrint, bool header,
260311
const QString &prefix) {
261312

@@ -276,6 +327,11 @@ void CLIRunner::info(QString msg, bool alwaysPrint, bool header,
276327
}
277328
}
278329

330+
/**
331+
* Prints an error message to stdout with an "ERROR" prefix.
332+
*
333+
* @param msg The error message to print.
334+
*/
279335
void CLIRunner::error(const QString &msg) { info(msg, true, false, "ERROR"); }
280336

281337
} // namespace Ripes

src/syscall/systemio.h

+11-1
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ class SystemIO : public QObject {
371371
postToGUIThread([=] { SystemIOStatusManager::clearStatus(); });
372372
return -1;
373373
}
374-
auto readData = InputStream.read(lengthRequested).toUtf8();
374+
auto readData = InputStream.read(1).toUtf8();
375375
myBuffer.append(readData);
376376
lengthRequested -= readData.length();
377377

@@ -436,6 +436,16 @@ class SystemIO : public QObject {
436436

437437
} // end writeToFile
438438

439+
/**
440+
* Redirects the stream associated with STDIN to the standard input (stdin).
441+
* The method ensures that the STDIN stream is reset (erasing the existing
442+
* mapping) and explicitly maps it to the standard input stream (stdin).
443+
*/
444+
static void setCLIInput() {
445+
FileIOData::streams.erase(STDIN);
446+
FileIOData::streams.emplace(STDIN, stdin);
447+
}
448+
439449
/**
440450
* Close the file with specified file descriptor
441451
*

0 commit comments

Comments
 (0)