Output to stderr in REBOL2? - cgi

I am trying to get my CGI scripts running on my web host (which runs on FreeBSD). To debug why I keep getting the dreaded "premature end of script headers" error, their support recommended that I redirect all my output to stderr, rather than printing it. Looking up how to do this, I came across a very old RAMBO ticket about it, but it looks like it was never implemented.
Per some of the answers to this question, it seems like I should be able to do a call {echo Hello, world >&2} to achieve this, but it doesn't work.
How can I write to stderr in REBOL2?

For my CGI-specific scenario, I have a truly awful workaround. Since writing to stderr in Perl (with which I am entirely unfamiliar) is a one-liner, I'm currently calling the REBOL script from Perl and printing its output to stderr from there:
#!/usr/bin/perl
use strict;
use warnings;
use CGI;
# Note the backticks
my $the_string = `/home/public/rebol -csw test-reb.cgi`;
print STDERR $the_string;

This webpage has some suggestions http://www.liquidweb.com/kb/apache-error-premature-end-of-script-headers/
to solve your real problem. Perhaps you did not have the headers printed as first thing in your script, this must be the first thing to do. Maybe the rights are not sufficient, or the .r file type was not properly added in your .htaccess as cgi able file. Your (correct!) rebol core exe has not the correct rights. Or your script ends up in an endless loop?

Some hints to redirect errors for Rebol cgi script:
http://www.rebol.com/docs/core23/rebolcore-2.html#section-6.2

Better late than never... I've just implemented it for Rebol3 in my Rebol fork.
https://github.com/Oldes/Rebol-issues/issues/2468
The syntax will be probably changed a little bit, because I don't like that the system console port is named input, although it is not just for the input.
So far it is:
print 1 ;<- std_out
modify system/ports/input 'error on
print 2 ;<- std_err
modify system/ports/input 'error off
print 3 ;<- std_out

Related

How do I tell Octave where to find functions without picking up other files?

I've written an octave script, hello.m, which calls subfunc.m, and which takes a single input file, a command line argument, data.txt, which it loads with load(argv(){1}).
If I put all three files in the same directory, and call it like
./hello.m data.txt
then all is well.
But if I've got another data.txt in another directory, and I want to run my script on it, and I call
../helloscript/hello.m data.txt
this fails because hello.m can't find subfunc.m.
If I call
octave --path "../helloscript" ../helloscript/hello.m data.txt
then that seems to work fine.
The problem is that if I don't have a data.txt in the directory, then the script will pick up any data.txt that is lying around in ../helloscript.
This seems a bit fragile. Is there any way to tell octave, preferably in the script itself, to get subfunctions from the same directory as the script, but to get everything else relative to the current directory.
The best robust solution I can think of at the moment is to inline the subfunction in the script, which is a bit nasty.
Is there a good way to do this, or is it just a thorny problem that will cause occasional hard to find problems and can't be avoided?
Is this in fact just a general problem with scripting languages that I've just never noticed before? How does e.g. python deal with it?
It seems like there should be some sort of library-load-path that can be set without altering the data-load-path.
Adding all your subfunctions to your program file is not nasty at all. Why would you think so? It is perfectly normal to have function definitions in your script. The only language I know that does not do this is Matlab but that's just braindead.
The other alternative you have is to check that the input file argument, data.txt exists. Like so:
fpath = argv (){1};
[info, err, msg] = stat (fpath);
if (err)
error ("could not stat `%s' : %s", fpath, msg);
endif
## continue your script knowing the file exists
But really, I would recommend you to use both. Add your subfunctions in your main program, the only reason to have it on separate file is if you plan on sharing with other programs, and always check input arguments.

SCIP write best feasible solution in automated test

Based on steps in http://scip.zib.de/doc/html/TEST.php, I have managed to set up an automated test using SCIP. However, I'd like to write the solution (best feasible solution) to a file, instead of just getting the objective value. Is there anyway to do it in the automated test?
I did a hack in check.sh by replacing
OPTCOMMAND=optimize; write solution myfilename.sol;
But too bad, it doesn't seem to work, when I tried to make TEST=mytest test, this line is observed from the output
bash ./check.sh mytest bin/scip-3.1.0.linux.x86_64.gnu.opt.spx default scip-3.1.0.linux.x86_64.gnu.opt.spx 3600 2100000000 6144 1 default 10000 false false 3.1.0 spx false /tmp optimize;
write: solution is not logged in on myfilename.sol
I know it is possible to write the solution via interactive shell, but I am trying to automate the test in order to retrieve both solution and obj value. Any help or clarification will be much appreciated!
You are getting an error because with the syntax you are using, you try to invoke a bash command called "write" because of the semicolon:
The write utility allows you to communicate with other users, by
copying lines from your terminal to theirs.
Just try without semicolon ;)
The cleaner solution would be to modify the file "check/configuration_tmpfile_setup_scip.sh"
and add the line
echo write solution /absolute/path/to/solutions/${INSTANCE}.sol >> $TMPFILE
before the quit command. This configuration file sets up a batch file to feed SCIP with all commands that the interactive shell should execute, and you can model arbitrary user behavior.

In Lua, how to print the console output into a file (piping) instead of using the standard output?

I workin' with Torch7 and Lua programming languages. I need a command that redirects the output of my console to a file, instead of printing it into my shell.
For example, in Linux, when you type:
$ ls > dir.txt
The system will print the output of the command "ls" to the file dir.txt, instead of printing it to the default output console.
I need a similar command for Lua. Does anyone know it?
[EDIT] An user suggests to me that this operation is called piping. So, the question should be: "How to make piping in Lua?"
[EDIT2] I would use this # command to do:
$ torch 'my_program' # printed_output.txt
Have a look here -> http://www.lua.org/pil/21.1.html
io.write seems to be what you are looking for.
Lua has no default function to create a file from the console output.
If your applications logs its output -which you're probably trying to do-, it will only be possible to do this by modifying the Lua C++ source code.
If your internal system has access to the output of the console, you could do something similar to this (and set it on a timer, so it runs every 25ms or so):
dumpoutput = function()
local file = io.write([path to file dump here], "w+")
for i, line in ipairs ([console output function]) do
file:write("\n"..line);
end
end
Note that the console output function has to store the output of the console in a table.
To clear the console at the end, just do os.execute( "cls" ).

How to interact with an external command in vimscript?

I have a script which interacts with user (prints some questions to stderr and gets input from stdin) and then prints some data to stdin. I want to put the output of the script to a variable in vimscript. It probably should look like this:
let a = system("./script")
The supposed behavior is that script runs, interacts with user, and after all a is assigned with its output to stdout. But instead a is assigned both with outputs to stdout and stderr, so user seed no prompts.
Could you help me fixing it?
Interactive commands are best avoided from within Vim; especially with GVIM (on Windows), a new console window pops up; you may not have a fully functional terminal, ...
Better query any needed arguments in Vimscript itself (with input(); or pass them on from a custom Vim :command), and just use the external script non-interactively, feeding it everything it needs.
What gets captured by system() (as well as :!) is controlled by the 'shellredir' option. Its usual value, >%s 2>&1 captures stdout as well as stderr. Your script needs to choose one (e.g. stdout) for its output, and the other for user interaction, and the Vimscript wrapper that invokes it must (temporarily) change the option.
:let save_shellredir = &shellredir
:set shellredir=>
:let a = system('./script') " The script should interact via stderr.
:let &shellredir = save_shellredir
Call the script within the other as,
. ./script.sh
I think this is what you meant.

How do I get the .write function to work with PyScripter?

To reproduce problem:
In the PyScripter editor, write:
outf = open('output.txt', 'w')
outf.write('hello, world!')
Result:
For me at least, here is what happens, when output.txt does not already exist.
output.txt is created
output.txt will contain no data or text at all when opened by any text editor.
So my question is, how do I make this work?
Other information:
I am using PyScripter 2.5.3.0 x64, with Python 2.7.3, 64 bit as the interpreter.
Printing to the console works fine, all other functions and code work fine.
When I use python in Command Prompt, I can write to output files fine. My problem is only in PyScripter.
Thanks,
DS
The question was already resolved in comments, but I will post a complete answer regardless.
Writes to files may be delayed. To make sure they aren't, force a flush by closing the file:
outf.close()
If you don't want to explicitly call close, try using with ... as:
with open('output.txt', 'w') as outf:
outf.write('hello, world!')