[comp.lang.rexx] Follow the true Way -- A PLEA!! for good program design.

psop@watcsc.waterloo.edu (Paul Sop) (11/17/90)

This message is empty.

psop@watcsc.waterloo.edu (Paul Sop) (11/17/90)

I want everyone, from now on, everywhere, who owns an amiga, to, when
they write AREXX programs (note! I didn't say 'Scripts'), include the
following **EXTREMELY IMPORTANT** line:

call close 'STDIN';call open 'STDIN','*','R'

NOTE the '*'.  If more amiga programs used this method of reading input,
life would be considerably better, easier, and all round more fun.

Why? You query?  Well...

To illustrate what happens when you LEAVE the magic line out of an AREXX
program, consider the following itty bitty AREXX program:

/* a wonderful illustration of what happens when you leave it out.rexx */
options prompt 'A wonderful prompt> '
do forever
   pull null
   say null
   end
exit 1

Ok now... You can Rx out.rexx  and it works just like you expect.. but what
if you:

Run rx >con:0/0/640/400/Window Out.Rexx

In this example, the program would just run forever, never able to get
input from the user.  It would just keep printing the prompt, and
printing null strings (and boy is that one teriffic waste of CPU time
let me tell you! :-)

The same sort of thing happens when you

run dir ?

The template comes up, but you can't enter anything, and the program quits.
This 'feature' can be devastating, as in the example:

run format drive df0: name Uh-Oh

which would wipe out whatever disk is in DF0: without prompting you.


The line I expect you all to add from now on (re-iterated here:)

call close 'STDIN';call open 'STDIN','*','R'

Will ** FIX ** this.  Now you'll be able to RUN programs in windows
and be able to get output from them, AND send input TO them!
That means, you can RUN programs, but still enter a few parameters before
they leave and run in the background.  You can now pipe output to console
windows, AND TYPE INTO THEM TOO!  And as soon as the program exits, it's
window is automatically closed.  Wonderful stuff.


You all have (no doubt) noticed that if you did something dumb like:

ADDRESS COMMAND "dir ?"

from an AREXX program, the effect would be similar to running it, in that
you wouldn't be able to interact with the directory command (a major
pitfall!).  If the DIR command opened it's file channel as '*' for input,

                IT WOULD WORK!

In this way, you could transparently INVOKE INTERACTIVE CLI COMMANDS
from AREXX programs!  I will post some skelleton C code to let you do this.


If you don't do your line-oriented I/O in this SIMPLE SIMPLE SIMPLE way,
then your programs, and their integration with other system programs
and standards, will SUFFER.


A bonus of doing something like:

run rx >con:0/0/640/400/Out Out.rexx     /* The previous example */

is that when the program finished (by a ^C maybe?) then the window
automatically closes.  The only other way you can achieve this is by
running it from (gag... dare we say) Workbench (where the window
management provided by AREXX is slow as heck), or starting it from
a newcli batchfile like this:

rx out.rexx
endcli

(a script which is invoked like: 
              newcli window con:0/0/640/400/Out from batchfile.  )


Anyway....


Thank you for lending me your ears.  I trust that you will realize the
importance of writing programs this way, and that you will do it from
now on and make everyone happy.


     Simulating psychedelic milieus with flashing lights and hallucinogenics:
.............................................................................
z s w a m p . f i d o n e t . o r g ! l i l l i p u t ! u n i x 4 ! p s o p 4
    - O R -                   p s o p @ w a t c s c . w a t e r l o o . e d u
.............................................................................
                                                      Mail me if you're mean.

lphillips@lpami.wimsey.bc.ca (Larry Phillips) (11/18/90)

In <1990Nov17.080652.28036@watcsc.waterloo.edu>, psop@watcsc.waterloo.edu (Paul Sop) writes:
>I want everyone, from now on, everywhere, who owns an amiga, to, when
>they write AREXX programs (note! I didn't say 'Scripts'), include the
>following **EXTREMELY IMPORTANT** line:
>
>call close 'STDIN';call open 'STDIN','*','R'
>
>NOTE the '*'.  If more amiga programs used this method of reading input,
>life would be considerably better, easier, and all round more fun.

That's fine for some things I suppose, but breaks other things. I have a little
ARexx program I call 'hilite', that lets me see text patterns in an alternate
colour. I typically use it in a pipe, so that I can see the context of the
output, which is a lot better at times than using grep and getting only single
lines out. Here's the program...


 /* hilite.rexx - hilite a substring
    usage is from stdin, with pipe.
    hilites the argument string
 */
parse arg x
do forever
 parse pull ln
 if EOF(stdin) then exit
 if pos(translate(x),translate(ln)) = 0 then
  say ln
 else
  do
   call writech(stdout,substr(ln,1,pos(translate(x),translate(ln))-1))
   call writech(stdout,'1b'x || '[33m' || x || '1b'x || '[31m')
   call writech(stdout,substr(ln,pos(translate(x),translate(ln))+ length(x)) || '0a'x)
  end
end

An example of calling it...

     dir all | hilite foo

Using your open/close stdin line breaks it.

Other than the special cases, though. it's a good idea.

-larry

--
The only things to survive a nuclear war will be cockroaches and IBM PCs.
+-----------------------------------------------------------------------+ 
|   //   Larry Phillips                                                 |
| \X/    lphillips@lpami.wimsey.bc.ca -or- uunet!van-bc!lpami!lphillips |
|        COMPUSERVE: 76703,4322  -or-  76703.4322@compuserve.com        |
+-----------------------------------------------------------------------+

peter@sugar.hackercorp.com (Peter da Silva) (11/18/90)

In article <1990Nov17.080652.28036@watcsc.waterloo.edu> psop@watcsc.waterloo.edu (Paul Sop) writes:
> I want everyone, from now on, everywhere, who owns an amiga, to, when
> they write AREXX programs (note! I didn't say 'Scripts'), include the
> following **EXTREMELY IMPORTANT** line:

> call close 'STDIN';call open 'STDIN','*','R'

I hope you only do this for interactive programs, because otherwise it makes
it pretty hard to do something like:

	rx <textfile program

Or, for that matter:

	program | rx program

Should you have a sufficiently capable shell. And even for interactive work
I've occasionally had reason to do something like:

	dir <file ?

Where the file was the result of some automatic processing of (say) list
output.
-- 
Peter da Silva.   `-_-'
<peter@sugar.hackercorp.com>.

jms@tardis.Tymnet.COM (Joe Smith) (11/20/90)

In article <1990Nov17.080652.28036@watcsc.waterloo.edu> psop@watcsc.waterloo.edu (Paul Sop) writes:
>I want everyone, from now on, everywhere, who owns an amiga, to add
>call close 'STDIN';call open 'STDIN','*','R'

I will not add this to my programs.  That is not the appropriate place to
put a bug fix.  Instead, I will wait for the Arexx interpreter itself to be
fixed.  Putting kludges in all your programs to get around a bug in one
particular version of the interpeter is not what I'd call "clean coding".
-- 
Joe Smith (408)922-6220 | SMTP: jms@tardis.tymnet.com or jms@gemini.tymnet.com
BT Tymnet Tech Services | UUCP: ...!{ames,pyramid}!oliveb!tymix!tardis!jms
PO Box 49019, MS-C41    | BIX: smithjoe | 12 PDP-10s still running! "POPJ P,"
San Jose, CA 95161-9019 | humorous dislaimer: "My Amiga 3000 speaks for me."

psop@watcsc.waterloo.edu (Paul Sop) (11/22/90)

In article <1343@tardis.Tymnet.COM> jms@tardis.Tymnet.COM (Joe Smith) writes:
>In article <1990Nov17.080652.28036@watcsc.waterloo.edu> psop@watcsc.waterloo.edu (Paul Sop) writes:
>>I want everyone, from now on, everywhere, who owns an amiga, to add
>>call close 'STDIN';call open 'STDIN','*','R'
>
>I will not add this to my programs.  That is not the appropriate place to
>put a bug fix.  Instead, I will wait for the Arexx interpreter itself to be
>fixed.  Putting kludges in all your programs to get around a bug in one
>particular version of the interpeter is not what I'd call "clean coding".

There are no bugs like this that need to be fixed.
The problem pertains to certain amiga features and how to use them
to their fullest.

Arexx isn't broken.  Nor are other amiga programs (the whole point was
that opening * (a console sort of thing) lets you achieve things you
otherwise couldn't)

I see now that it interferes with piping under normal Amiga DOS shells
(my shell takes care of this), so programmers may not want to use it
everywhere.

It would be good though, since it requires so little code, to
specify it as an option for text-based line-oriented-i/o applications.

Something along these lines would be nice:

COMMAND -* parm1 parm2 parm3...


Thanks for all the interesting replies people.

But back to your reply....  There's nothing kludgie about it.
Most languages allow you to close stdin and re-direct it to some other
channel.  There is nothing broken in AREXX that my suggestion
'patches'.  It only helps you in certain situations.

Your message contained a good amount of agitation, and this is not
the place for such agitation.  You're obviously ill-informed when it
comes to this topic.  Some honest advice with good intent; from me,
to you:  Chill out a bit.

     Simulating psychedelic milieus with flashing lights and hallucinogenics:
.............................................................................
z s w a m p . f i d o n e t . o r g ! l i l l i p u t ! u n i x 4 ! p s o p 4
    - O R -                   p s o p @ w a t c s c . w a t e r l o o . e d u
.............................................................................
                                                      Mail me if you're mean.

lshaw@ccwf.cc.utexas.edu (logan shaw) (11/26/90)

In article <1990Nov22.012641.22866@watcsc.waterloo.edu> psop@watcsc.waterloo.edu (Paul Sop) writes:
>Your message contained a good amount of agitation, and this is not
>the place for such agitation.  You're obviously ill-informed when it
>comes to this topic.  Some honest advice with good intent; from me,
>to you:  Chill out a bit.

Ah, but so what if he's ill-informed?  You've just informed him (and me) better.
I actually _learned_ something as a result of the fact that he posted what he did.
Wow, this group is actually USEFUL!!!

Besides, in this newsgroup, we need all the postings we can get.  :-)

BTW, what _exactly_ is the advantage of closing and reopening STDIN?  What does
it allow me to do that I couldn't before?

>     Simulating psychedelic milieus with flashing lights and hallucinogenics:
>.............................................................................
>z s w a m p . f i d o n e t . o r g ! l i l l i p u t ! u n i x 4 ! p s o p 4
>    - O R -                   p s o p @ w a t c s c . w a t e r l o o . e d u
>.............................................................................
>                                                      Mail me if you're mean.
-- 
But there's more to this life than living and dying,\               Logan Shaw
More than just trying to make it through the day,    \lshaw@ccwf.cc.utexas.edu
More to this life, more than these eyes alone can see,\    Amiga 2000, C= 1084
And there's more than this life alone can be.          \        GVP 40Q, 8-up!