<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">RESOLVED:<div class=""><font face="Courier New" class=""><br class=""></font></div><div class=""><font face="Courier New" class="">:- pragma foreign_proc(<br class=""> "C",<br class=""> ln_read(Prompt::in, Line::out, LineNull::out, IOin::di, IOout::uo),<br class=""> [will_not_throw_exception, promise_pure],<br class=""> "<br class=""> char* line = linenoise((const char*)Prompt);<br class=""> if (line) {<br class=""> linenoiseHistoryAdd(line);<br class=""> LineNull = 0;<br class=""> Line = line;<br class=""> } else {<br class=""> LineNull = 1;<br class=""><b class=""> Line = \"\";<br class=""></b> }<br class=""> IOout = IOin;<br class=""> "<br class="">).</font></div><div class=""><br class=""></div><div class="">Use of CTRL-D now behaves itself. I guess I was not smart enough to fully grok your comment first time around Julien! I still don’t know —why— it cures the issue as I wasn’t using that variable (Line) if LineNull indicated it was not to be trusted… is it something internal in the FFI interface, I have no clue on that.</div><div class=""><br class=""></div><div class="">Thanks all,</div><div class="">Sean</div><div class=""><br class=""></div><div class=""><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On 19 Jun 2021, at 13:31, Julien Fischer <<a href="mailto:jfischer@opturion.com" class="">jfischer@opturion.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class=""><br class="">Hi Sean,<br class=""><br class="">On Sat, 19 Jun 2021, Sean Charles (emacstheviking) wrote:<br class=""><br class=""><blockquote type="cite" class="">It all works beautifully, as expected but when I use CTRL-D it balks with this output:<br class="">*** Mercury runtime: caught segmentation violation ***<br class="">cause: address not mapped to object<br class="">address involved: 0x0<br class="">This may have been caused by a stack overflow, due to unbounded recursion.<br class="">exiting from signal handler<br class="">[1] 68417 segmentation fault ./felt -r<br class="">I have attached the full FFI code at the end. I’ve tried to work it through. The source code for the input function is here:<br class="">I studied the source code, it is using fgets() which returns NULL on end-of-file and I thought I was managing that just fine!<br class="">Does Mercury have something to with this e.g. a SIGINT handler or something that might be clashing with the linenoise-ng code I wonder ?<br class="">The calling code in my REPL loop is as follows:<br class="">:- pred run_loop(<br class=""> command_line.felt_options::in,<br class=""> replcon::in, replcon::out,<br class=""> io::di, io::uo) is det.<br class="">run_loop(Options, !Repl, !IO) :-<br class=""> ln_read(prompt, Line, LineNull, !IO),<br class=""> io.format("LNG: NULL:%d, len:%d, \n", [i(LineNull), i(length(Line))], !IO),<br class=""></blockquote><br class="">Ok, so we are doing stuff with the variable Line here ...<br class=""><br class=""><blockquote type="cite" class=""> (if LineNull = 1 then<br class=""> io.format("\n%sBe excellent to one another.\n",<br class=""> [s(prompt)], !IO)<br class=""> else<br class=""> execute(Line, !Repl, !IO),<br class=""> run_loop(Options, !Repl, !IO)<br class=""> ).<br class=""></blockquote><br class="">...<br class=""><br class=""><blockquote type="cite" class="">:- pred ln_setup(string::in, int::in, io::di, io::uo) is det.<br class="">:- pred ln_shutdown(string::in, io::di, io::uo) is det.<br class="">:- pred ln_read(string::in, string::out, int::out, io::di, io::uo) is det.<br class="">:- pred ln_free(string::in, io::di, io::uo) is det.<br class=""> % ln_read: Reads and returns a single line.<br class=""> % Line is unified with whataver the library read from stdin.<br class=""> % LineNull is unified with 1 if NULL(CTRL+D) is detected, else 0.<br class=""> % If the line is not empty then we automatically add it to the history.<br class=""> %<br class="">:- pragma foreign_proc(<br class=""> "C",<br class=""> ln_read(Prompt::in, Line::out, LineNull::out, IOin::di, IOout::uo),<br class=""> [will_not_throw_exception, promise_pure],<br class=""> "<br class=""> char* line = linenoise((const char*)Prompt);<br class=""> if (line) {<br class=""> linenoiseHistoryAdd(line);<br class=""> LineNull = 0;<br class=""> Line = line;<br class=""></blockquote><br class="">... which is set to a value in this branch , but ...<br class=""><br class=""><blockquote type="cite" class=""> } else {<br class=""> LineNull = 1;<br class=""></blockquote><br class="">... not in this one.<br class=""><br class=""><br class=""><blockquote type="cite" class=""> }<br class=""> printf(\"leaving, strlen() is %d\\n\", strlen(line));<br class=""> IOout = IOin;<br class=""> "<br class=""></blockquote><br class="">I'm not familiar with the linenoise library, but I am assuming that the<br class="">function linenoise() returns NULL on EOF?<br class=""><br class="">Julien.</div></div></blockquote></div><br class=""></div></body></html>