Discussion:
new_process throwing exceptions with doc examples
(too old to reply)
Chris Nehren
2010-06-16 07:50:54 UTC
Permalink
I'm trying to use new_process to call a Unix filter (specifically, I
want to use a Perl program I wrote to filter article text in slrn).
However, it throws an exception even with the examples from the docs.
Here's one such example:

#!/usr/local/bin/slsh
require("process");
variable pgm = ["/usr/home/apeiron/bin/fix_articles"];
obj = new_process (pgm; write={1,2}, read=0);
% doesn't get here

Then when I try to run it:

$ cat google_bad_quoting | ./filter.sl
Unable to typecast Null_Type to String_Type
Traceback: fdopen
/usr/local/share/slsh/process.sl:338:new_process:Type Mismatch

Am I doing something wrong here? I'm running against s-lang version
2.2.2.
--
Thanks and best regards,
Chris Nehren
Unless noted, all content I post is CC-BY-SA.
John E. Davis
2010-06-17 03:09:43 UTC
Permalink
Post by Chris Nehren
I'm trying to use new_process to call a Unix filter (specifically, I
want to use a Perl program I wrote to filter article text in slrn).
However, it throws an exception even with the examples from the docs.
#!/usr/local/bin/slsh
require("process");
variable pgm = ["/usr/home/apeiron/bin/fix_articles"];
obj = new_process (pgm; write={1,2}, read=0);
% doesn't get here
There is a bug in process.sl-- a patch is attached below. The patch
is also in the version in the git repo linked at
<www.jedsoft.org/snapshots/>. However, it is unclear to me that this
Post by Chris Nehren
$ cat google_bad_quoting | ./filter.sl
Here you want filter.sl to read from slsh's stdin. But your script
creates a process for `fix_articles` with stdin/stdout/stderr attached
to the obj object--- not stdin/out/err of slsh. For the latter you
want to use just

obj = new_process (pgm);

For this use, the bug-fix appended below will not matter.
Finally, do not forget to "wait" on the process:

s = obj.wait();

You would use the read/write qualifiers only if you intend to read or
write to the subprocess from the script. Here is a simple example
sends a string to `cat` and then reads the output of `cat`:
#v+
require("process");
define slsh_main ()
{
variable
line,
pgm = ["cat"],
obj = new_process (pgm; write={1,2}, read=0);

() = fputs ("Test 123", obj.fp0);
() = fclose (obj.fp0);
() = fgets (&line, obj.fp1);
() = obj.wait();
() = fprintf (stdout, "Read: `%s'\n", line);
}
#v-

Finally, here is the patch to slsh/lib/process.sl. As you can see,
the patch is to swap two lines:

#v+
diff --git a/slsh/lib/process.sl b/slsh/lib/process.sl
index a84415f..0b3ff1f 100644
--- a/slsh/lib/process.sl
+++ b/slsh/lib/process.sl
@@ -290,8 +290,8 @@ define new_process ()
{
list_append (struct_fields,"fd$ifd"$);
list_append (struct_fields,"fp$ifd"$);
- (child_fds[i], parent_fds[i]) = pipe (); i++;
modes[i] = "w";
+ (child_fds[i], parent_fds[i]) = pipe (); i++;
}

#v-

Thanks,
--John
Chris Nehren
2010-06-17 04:31:54 UTC
Permalink
On Wed, 16 Jun 2010 07:50:54 +0000 (UTC), Chris Nehren
Post by Chris Nehren
I'm trying to use new_process to call a Unix filter (specifically, I
want to use a Perl program I wrote to filter article text in slrn).
However, it throws an exception even with the examples from the docs.
#!/usr/local/bin/slsh
require("process");
variable pgm = ["/usr/home/apeiron/bin/fix_articles"];
obj = new_process (pgm; write={1,2}, read=0);
% doesn't get here
There is a bug in process.sl-- a patch is attached below.
Ah. I was suspecting this but wanted another pair of eyes.
The patch is also in the version in the git repo linked at
<www.jedsoft.org/snapshots/>. However, it is unclear to me that this
Post by Chris Nehren
$ cat google_bad_quoting | ./filter.sl
Right, the code I pasted was the simplest possible test case I could
produce which exhibited the problem. In actuality the program calls
slrn's article_as_string and then replace_article--I wanted to keep
my post as specific to s-lang as possible.
You would use the read/write qualifiers only if you intend to read or
write to the subprocess from the script. Here is a simple example
#v+
require("process");
define slsh_main ()
{
variable
line,
pgm = ["cat"],
obj = new_process (pgm; write={1,2}, read=0);
() = fputs ("Test 123", obj.fp0);
() = fclose (obj.fp0);
() = fgets (&line, obj.fp1);
() = obj.wait();
() = fprintf (stdout, "Read: `%s'\n", line);
}
#v-
Yes, I had something very similar to this before trimming it down to
provide the test case here, including the obj.wait() call of course.
Finally, here is the patch to slsh/lib/process.sl. As you can see,
Excellent, thank you! Applied locally and everything works.
--
Thanks and best regards,
Chris Nehren
Unless noted, all content I post is CC-BY-SA.
Loading...