Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

new_process throwing exceptions with doc examples

10 views
Skip to first unread message

Chris Nehren

unread,
Jun 16, 2010, 3:50:54 AM6/16/10
to
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

unread,
Jun 16, 2010, 11:09:43 PM6/16/10
to
On Wed, 16 Jun 2010 07:50:54 +0000 (UTC), Chris Nehren <ape...@isuckatdomains.net.invalid>
wrote:

> 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

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
will solve your problem:

> $ 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

unread,
Jun 17, 2010, 12:31:54 AM6/17/10
to
On 2010-06-17, John E. Davis scribbled these curious markings:

> On Wed, 16 Jun 2010 07:50:54 +0000 (UTC), Chris Nehren
> <ape...@isuckatdomains.net.invalid> wrote:
>> 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
>
> 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
> will solve your problem:
>> $ 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
> 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-

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,
> the patch is to swap two lines:

Excellent, thank you! Applied locally and everything works.

0 new messages