[comp.mail.mh] "folder", "inc", "refile" hang or create folder you might not want

jdpeek@rodan.acs.syr.EDU (Jerry Peek) (03/01/90)

I do a lot of shell programming with MH.  It's mostly *great* for
programming (especially since mh.6 came out)!  But one of the "gotchas"
to watch for is the way that "folder" creates folders.  Here are some
problems it might cause you in your programs, plus an idea for a change.

>From the folder(1) man page:
     "If the specified (or default) folder doesn't exist, the user
      will be queried if the folder should be created."
"refile" and "inc" work the same way.

An unattended shell script can do funny things when a user types (oops):
	% somescript +inboz &
If the script was started from a shell with job control, any "folder +inboz"
command in the script will hang the script -- while the shell waits for the
user to bring the job into the foreground and answer y/n to "folder" about
creating +inboz.  This happens even when stdout and stderr are redirected to
a file, so the user can't see the prompt from "folder"!  On shells without job
control, "folder" seems to always create the folder; see the next paragraph.

If "folder" can read *anything* from stdin, like this:
	folder +inboz </dev/null
it *will* create a folder *unless* it reads an "n".  The "refile" and
"inc" commands do the same thing.  (Background jobs on non-job-control
shells always take stdin from /dev/null -- true?  If so, the same
thing will *always* happen there.)

Even more, "inc -silent" works like "inc </dev/null"... it always
creates a folder (silently) if needed, and incorporates mail into it.

I'm not excited about this "create unless they answer 'n'" behavior.  The
work-around is code like this in shell scripts before every "refile", "inc",
or "folder" (or, at least, before the first use of any folder *name*):
	if test -d `mhpath +$folderarg`
	then folder +$folderarg >/dev/null 2>&1
	else #complain that folder doesn't exist...
	fi
I've learned this (the hard way)... but it could burn people who are just
getting started with MH programming.

I think it would be nice to have a new switch like "-nocreate" that tells
"folder", "inc", "refile", etc. to just abort (with a non-zero status) if
the folder doesn't exist.  Do I need to R some FM?  Are there better fixes? 
Can/should this go in MH 6.6.1 -- or at least mention the behavior
in the refile(1), folder(1), and inc(1) man pages?

--Jerry Peek; Syracuse University Academic Computing Services; Syracuse, NY
  jdpeek@rodan.acs.syr.edu, JDPEEK@SUNRISE.BITNET        +1 315 443-3995

P.S.  syntax: folder [+folder] [msg] [switches]
      switches are:
      -all -[no]fast -[no]header -[no]pack -[no]recurse -[no]total
      -[no]print -[no]list -push -pop -(help)

    version: MH 6.6 #1[UCI] (ics) of Tue May 24 15:51:53 PDT 1988
    options: [BSD42] [BSD43] [BERK] [MHRC] [DUMB] [RPOP] [NETWORK] [BIND]
	     [RPATHS] [UCI] [MHE] [SENDMTS] [POP]

jromine@ics.uci.edu (John Romine) (03/02/90)

jdpeek@rodan.acs.syr.EDU (Jerry Peek) writes about "folder +inboz"
querying you to create +inboz if it doesn't exist

From the folder man page:

     If the specified (or default) folder doesn't exist, the user
     will  be  queried if the folder should be created.  (This is
     the easy way to create an empty folder for use later.)

Folder checks to see whether stdin (not stdout or stderr) is a tty or
not.  If it's not a tty, it doesn't ask questions, it just assumes a
reasonable answer: in this case "yes".

Some history:

It used to be "folder +inboz" was considered an error if +inboz didn't
exist.  I requested [mostly for shell script use] that it create the
folder instead of being an error.  The alternative was to use "mkdir
`mhpath +inboz`".

If folder was called interactively, we decided that querying the user
seemed reasonable.  Some other MH programs do this under some
circumstances as well.  There was considerable discussion about whether
changing the default behavior and querying the user were "good things".

Your current choices for shell scripts are:
  1. redirect stdin and the folder will be created.
  2. Let the user decide when folder gives its query.

I treat MH scripts like MH programs: if you're running something that
will be interactive, let folder, et. al., query the user if it needs to.
If you're running something that won't be interactive, assume the user
typed what he meant (i.e., just create +inboz and use it).  Nearly all
UNIX programs assume the latter, so it seems like that's what a user
would expect to happen.

If you want to abort if the folder doesn't exist, you have to use
"test -d" as you mentioned.  MH programs have become more user-oriented
and less tool-oriented.  As I mentioned above, this isn't necessarily
a "good thing".  Of course, you can do almost anything you want by
calling "mhpath" and using standard UNIX tools.
--
John Romine