Skip to content

Commit 9232b7e

Browse files
committed
polish up the fix for #576
1 parent 744bc3f commit 9232b7e

4 files changed

Lines changed: 30 additions & 11 deletions

File tree

EidosScribe/EidosHelpFunctions.rtf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6686,6 +6686,24 @@ The task keys recognized, and the tasks they govern, are:\
66866686
\f3\fs20 \cf2 Typically, a dictionary of task keys and thread counts is read from a file and set up with this function at initialization time, but it is also possible to change new task thread counts dynamically. If Eidos is not configured to run multithreaded, this function has no effect.\
66876687
\pard\pardeftab397\li720\fi-446\ri720\sb180\sa60\partightenfactor0
66886688

6689+
\f1\fs18 \cf2 (string$)readLine(void)\
6690+
\pard\pardeftab397\li547\ri720\sb60\sa60\partightenfactor0
6691+
6692+
\f0\b\fs20 \cf2 Reads a line of input from the \'93standard input\'94 file.
6693+
\f3\b0 This function is intended to allow for communication between a running Eidos script (such as a SLiM simulation) and an external process that is sending input to it. It reads one line from the standard input and returns it as a singleton
6694+
\f1\fs18 string
6695+
\f3\fs20 . This is done with the C++ function
6696+
\f1\fs18 std::getline()
6697+
\f3\fs20 , using the
6698+
\f1\fs18 std::cin
6699+
\f3\fs20 file. If that call returns false (typically because the end-of-file was reached or there was a read error of some kind), the empty string
6700+
\f1\fs18 ""
6701+
\f3\fs20 will be returned. The
6702+
\f1\fs18 system()
6703+
\f3\fs20 function can also be helpful for related situations.\
6704+
This function will raise an error if called in a GUI environment such as EidosScribe, SLiMgui, or SLiMguiLegacy, since there is no standard input set up for the running script in those environments.\
6705+
\pard\pardeftab397\li720\fi-446\ri720\sb180\sa60\partightenfactor0
6706+
66896707
\f1\fs18 \cf2 (void)rm([Ns\'a0variableNames\'a0=\'a0NULL])\
66906708
\pard\pardeftab397\li547\ri720\sb60\sa60\partightenfactor0
66916709

QtSLiM/help/EidosHelpFunctions.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,9 @@
648648
"UNIQUE_MUTRUNS"<span class="Apple-tab-span"> </span></span>uniquing mutation runs (internal bookkeeping)<span class="s2"><br>
649649
"SURVIVAL"<span class="Apple-tab-span"> </span></span>survival evaluation (no callbacks)</p>
650650
<p class="p5">Typically, a dictionary of task keys and thread counts is read from a file and set up with this function at initialization time, but it is also possible to change new task thread counts dynamically.<span class="Apple-converted-space">  </span>If Eidos is not configured to run multithreaded, this function has no effect.</p>
651+
<p class="p4">(string$)readLine(void)</p>
652+
<p class="p5"><b>Reads a line of input from the “standard input” file.</b><span class="Apple-converted-space">  </span>This function is intended to allow for communication between a running Eidos script (such as a SLiM simulation) and an external process that is sending input to it.<span class="Apple-converted-space">  </span>It reads one line from the standard input and returns it as a singleton <span class="s2">string</span>.<span class="Apple-converted-space">  </span>This is done with the C++ function <span class="s2">std::getline()</span>, using the <span class="s2">std::cin</span> file.<span class="Apple-converted-space">  </span>If that call returns false (typically because the end-of-file was reached or there was a read error of some kind), the empty string <span class="s2">""</span> will be returned.<span class="Apple-converted-space">  </span>The <span class="s2">system()</span> function can also be helpful for related situations.</p>
653+
<p class="p5">This function will raise an error if called in a GUI environment such as EidosScribe, SLiMgui, or SLiMguiLegacy, since there is no standard input set up for the running script in those environments.</p>
651654
<p class="p4">(void)rm([Ns variableNames = NULL])</p>
652655
<p class="p5"><b>Removes variables</b> from the Eidos namespace; in other words, it causes the variables to become undefined.<span class="Apple-converted-space">  </span>Variables are specified by their <span class="s2">string</span> name in the <span class="s2">variableNames</span> parameter.<span class="Apple-converted-space">  </span>If the optional <span class="s2">variableNames</span> parameter is <span class="s2">NULL</span> (the default), <i>all</i> variables will be removed (be careful!).</p>
653656
<p class="p5">In SLiM 3, there was an optional parameter <span class="s2">removeConstants</span> that, if <span class="s2">T</span>, allowed you to remove defined constants (and then potentially redefine them to have a different value).<span class="Apple-converted-space">  </span>The <span class="s2">removeConstants</span> parameter was removed in SLiM 4, since the <span class="s2">defineGlobal()</span> function now provides the ability to define (and redefine) global variables that are not constant.</p>

VERSIONS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ development head (in the master branch):
1919
fix various bugs involving conflicts between defined constants and other symbols, including #573 and #574; this sets new definition rules that could break some existing scripts (but is unlikely to)
2020
fix #575, QtSLiM terminates early when single-stepping with a rescheduled script block
2121
fix #579, crash in models with (a) tree-seq recording, (b) multiple chromosomes, AND (c) rejection of proposed offspring in modifyChild()
22+
add readLine() function for obtaining stdin input to running models, thanks to Chris Talbot
2223

2324

2425
version 5.1 (Eidos version 4.1):

eidos/eidos_functions_files.cpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -225,17 +225,15 @@ EidosValue_SP Eidos_ExecuteFunction_readFile(const std::vector<EidosValue_SP> &p
225225
// (string$)readLine(void)
226226
EidosValue_SP Eidos_ExecuteFunction_readLine(const std::vector<EidosValue_SP> &p_arguments, EidosInterpreter &p_interpreter)
227227
{
228-
#pragma unused (p_arguments)
228+
#pragma unused (p_arguments, p_interpreter)
229229

230-
#ifdef EIDOS_GUI
231-
EIDOS_TERMINATION << "ERROR (Eidos_ExecuteFunction_readLine): function readLine() is not available in GUI environments (SLiMgui, SLiMguiLegacy, or EidosScribe)." << EidosTerminate(nullptr);
232-
#endif
233-
234230
// This function was implemented by Chris Talbot 11/19/25 for use in reinforcement learning environments.
235231
// Associated with issue #576.
236232

237-
EidosValue_SP result_SP(nullptr);
238-
233+
#ifdef EIDOS_GUI
234+
// Cannot be called inside a GUI since there is (presumably) no stdin set up for the script to read.
235+
EIDOS_TERMINATION << "ERROR (Eidos_ExecuteFunction_readLine): function readLine() is not available in GUI environments (SLiMgui, SLiMguiLegacy, or EidosScribe)." << EidosTerminate(nullptr);
236+
#else
239237
// Read a single line from stdin (command-line mode with interactive terminal)
240238
std::string line;
241239

@@ -245,15 +243,14 @@ EidosValue_SP Eidos_ExecuteFunction_readLine(const std::vector<EidosValue_SP> &p
245243
if (!line.empty() && line[line.size() - 1] == '\r')
246244
line.pop_back();
247245

248-
result_SP = EidosValue_SP(new (gEidosValuePool->AllocateChunk()) EidosValue_String(line));
246+
return EidosValue_SP(new (gEidosValuePool->AllocateChunk()) EidosValue_String(line));
249247
}
250248
else
251249
{
252250
// EOF or error reading from stdin; return empty string
253-
result_SP = EidosValue_SP(new (gEidosValuePool->AllocateChunk()) EidosValue_String(""));
251+
return gStaticEidosValue_StringEmpty;
254252
}
255-
256-
return result_SP;
253+
#endif
257254
}
258255

259256
// (string$)setwd(string$ path)

0 commit comments

Comments
 (0)