Oops, I forgot about the ide issues.

Here's another approach:  2!:0 'cc 2>&1; echo $?'

This redirects stderr to stdout so it will be captured for the IDE.

And the echo command serves two purposes:

(1) It captures the error code
(2) It overrides the error code (setting it to 0) which allows 2!:0 to
return its result.

Of course, giving cc something to chew on also works.

Or, if I might offer a variation on Joey Tuttle's host:

   host=: [: 2!:0 '(' , ] , ')2>&1||:'"_

This uses : instead of true (: in unix shell is a null command which always
succeeds and does nothing) and redirects stderr so that you can read it
even if you're not seeing the console.

Note also the parenthesis trick - parenthesis in a shell fork the entire
contained expression, allowing you to capture everything, so:

   <;._2 host 'echo one; echo two; echo three'
one
two
three
Segmentation fault (core dumped)

Er... wait, it's not supposed to work like that. What happened?

Trying again:

   $ 2!:0 '(echo one; echo two; echo three) 2>&1 || true'
one
two
three
Segmentation fault (core dumped)

But this works:

   $ 2!:0 '(echo one; echo two; echo three) 2>&1'
14

Hmm.. running under gcc:

   $ 2!:0 '(echo one; echo two; echo three) 2>&1 || true'
one
two
three

Program received signal SIGSEGV, Segmentation fault.
0xf7e59bf2 in fclose () from /lib/i386-linux-gnu/libc.so.6
(gdb) where
#0  0xf7e59bf2 in fclose () from /lib/i386-linux-gnu/libc.so.6
#1  0xf7dc279b in jthost () from /home/ubuntu/j602/bin/libj.so
#2  0xf7daf653 in secf1 () from /home/ubuntu/j602/bin/libj.so
#3  0xf7c27a67 in jtdfs1 () from /home/ubuntu/j602/bin/libj.so
#4  0xf7c6db44 in jtmonad () from /home/ubuntu/j602/bin/libj.so
#5  0xf7c6d4c9 in jtparsea () from /home/ubuntu/j602/bin/libj.so
#6  0xf7c6d682 in jtparse () from /home/ubuntu/j602/bin/libj.so
#7  0xf7c70c51 in jtimmex () from /home/ubuntu/j602/bin/libj.so
#8  0xf7c65593 in jdo () from /home/ubuntu/j602/bin/libj.so
#9  0xf7c65664 in JDo () from /home/ubuntu/j602/bin/libj.so
#10 0x0804a11c in jedo ()
#11 0x08049e6d in main ()

Looking at jthost:

F1(jthost){A z;
 F1RANK(1,jthost,0);
 RZ(w=vs(w));
#if (SYS & SYS_PCWIN)
 ASSERT(0,EVDOMAIN);
#else
{
 A t;I b;C*fn,*s;F f;I n;
 n=AN(w);
 GA(t,LIT,n+5+L_tmpnam,1,0); s=CAV(t);
 fn=5+n+s; MC(s,AV(w),n);
 MC(n+s,"   > ",5L); {C* t=tmpnam(fn);}
 b=!system(s);
 if(b){f=fopen(fn,FREAD); z=rd(f,0L,-1L); fclose(f);}
 unlink(fn);
 ASSERT(b&&f,EVFACE);
}
#endif
 R z;
}

The problem is that fopen did not succeed. See.. the problem is I
redirected stderr before stdout was redirected. And || true means that the
redirect only happens if the command fails. So here's a workaround:
Anyways, what happens is that libc's fclose gets really unhappy and throws
a tantrum, taking J down with it.

So here's a workaround:

   host=: [: 2!:0 '((' , ] , ')2>&1||:)'"_
   host 'echo one; echo two; echo three'
one
two
three

or:

   <;._2 host 'echo one; echo two; echo three'
┌───┬───┬─────┐
│one│two│three│
└───┴───┴─────┘

Tada!...

(Note that I used : in this revised version of host, instead of true - they
do the same thing in the context of unix shell, but : is slightly more
compact.)

The moral of the story here is that if you want to be a programmer you're
sometimes going to have to do some extra work to find the definitions of
things. It'll be a mix of frustration and elation, quite often. There's
really no way of entirely avoiding that, but we do what we can.

Thanks,

-- 
Raul



On Fri, Mar 21, 2014 at 10:05 AM, Marc Simpson <[email protected]> wrote:

> Note that the issue with the 'cc' case is that (i) you're not
> providing any input files and (ii) an error message is printed to
> stderr but not displayed to the IDE terminal. Consider the same call
> in jconsole,
>
>     2!:0'cc'
>   clang: error: no input files
>   |interface error
>   |       2!:0'cc'
>
> (Note the second line.)
>
> This should work,
>
>   '/tmp/foo.c'fwrites~'#include<stdio.h>',LF,'int
> main(void){printf("C.\n");return 0;}'
>   2!:0'cc /tmp/foo.c'
>   2!:0'./a.out'
>
> /M
>
> On Fri, Mar 21, 2014 at 11:16 AM, kamakura <[email protected]>
> wrote:
> > Hello there!
> >
> > I have a question about usage of 2!:0 (invoking a unix command).
> > Sometimes I come across an error as follows:
> >
> >     2!:0'pwd'
> > /Applications/j64-801/bin
> >     2!:0'cc'
> > |interface error
> > |       2!:0'cc'
> >
> >
> > It seems that 2!:0 does not work properly.
> >
> > Toshinari Kamakura
> >
> > ----------------------------------------------------------------------
> > For information about J forums see http://www.jsoftware.com/forums.htm
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to