Cleaned up some printf's.
[mmh] / uip / mhmail
index 6a91600..96ceddc 100755 (executable)
@@ -8,6 +8,7 @@
 #
 # Emulation of compiled mhmail(1), with these differences:
 # * Adds -send/-nosend, -headerfield, and -attach options.
+# * Adds optional -to switch for recipient addresses.
 # * Supports all post(8) (by default, without -profile) or send(1)
 #   (with -profile) options.
 # * Optionally (with -profile) obeys the users profile, including
@@ -17,7 +18,7 @@
 # * The compiled mhmail dropped a trailing newline from the -body argument.
 # * Drops support for undocumented -queue option.
 
-usage='Usage: mhmail [addrs ... [switches]]
+usage='Usage: mhmail [-t(o)] addrs ... [switches]
   switches are:
   -at(tach) file [-at(tach) file] ...
   -b(ody) text
@@ -31,11 +32,26 @@ usage='Usage: mhmail [addrs ... [switches]]
   -nose(nd)
   -v(ersion)
   -hel(p)
-  and all post(8)/send(1) switches'
+  and all post(8)/send(1) switches
+  mhmail with no arguments is equivalent to inc'
 
-bindir=`dirname $0`
+
+#### Find directory of this script.  Bourne shell just puts the program
+#### name in $0 if it's found from the PATH, so search that if necessary.
+finddir() {
+  case $0 in
+    */*) dirname $1 ;;
+    *  ) IFS=:
+         for d in $PATH; do
+           [ -f "${d:=.}/$1"  -a  -x "$d/$1" ]  &&  printf "$d"  &&  break
+         done ;;
+  esac
+}
+
+bindir=`finddir $0`
 nmhbindir=`cd "${bindir}" && pwd`
 
+
 #### Checks for missing mandatory arguments.
 checkforargs() {
   if [ ${attacharg} -eq 1 ]; then
@@ -50,6 +66,8 @@ checkforargs() {
     printf "mhmail: missing argument to -headerfield\n"; exit 1
   elif [ ${subjectarg} -eq 1 ]; then
     printf "mhmail: missing argument to -subject\n"; exit 1
+  elif [ ${toarg} -eq 1 ]; then
+    printf "mhmail: missing argument to -to\n"; exit 1
   fi
 }
 
@@ -59,6 +77,7 @@ if [ $# -eq 0 ]; then
 else
   #### Go through all the switches so we can build the draft.
   tolist=                     ## To: addresses
+  toarg=0                     ## whether currently handling -to
   attacharg=0                 ## whether currently handling -attach
   attach_send_switch_added=0  ## whether added "-attach Nmh-Attachment" switch
   body=                       ## contents of the message body
@@ -78,7 +97,7 @@ else
   use_send=0                  ## use post (default) or send (-profile)
   sendsw=1                    ## to send (default) or not to send
   for arg in "$@"; do
-    case "${arg}" in
+    case ${arg} in
       #### Post and send won't accept -f -or -s because they'd be
       #### ambiguous, so no conflicts with them.  And they don't have
       #### -b, -c, -r, -t.  For the new switches that compiled mhmail
@@ -100,7 +119,7 @@ else
       -f|-fr|-fro|-from) mhmailswitch=1; fromarg=1 ;;
       -hea|-head|-heade|-header|-headerf|-headerfi|-headerfie|-headerfiel|\
 -headerfield) mhmailswitch=1; headerfieldarg=1 ;;
-      -hel|-help) printf "%s\n" "${usage}"; exit ;;
+      -hel|-help) printf "${usage}\n"; exit ;;
       -nose|-nosen|-nosend) mhmailswitch=1; sendsw=0 ;;
       -p|-pr|-pro|-prof|-profi|-profil|-profile) mhmailswitch=1; use_send=1 ;;
       -resend) printf "mhmail: did you mean -resent instead of -resend?\n" 1>&2
@@ -108,6 +127,7 @@ else
       -r|-re|-res|-rese|-resen|-resent) mhmailswitch=1; resent=1 ;;
       -se|-sen|-send) mhmailswitch=1; sendsw=1 ;;
       -su|-sub|-subj|-subje|-subjec|-subject) mhmailswitch=1; subjectarg=1 ;;
+      -t|-to) toarg=1; ccarg=0 ;;
       -v|-ve|-ver|-vers|-versi|-versio|-version)
          #### Cheat instead of using autoconf and make to fill in the version.
          "${nmhbindir}"/mhpath -v | sed 's/mhpath/mhmail/'; exit ;;
@@ -147,11 +167,12 @@ Nmh-Attachment: ${arg}
          elif [ ${post_send_switch_arg} -eq 1 ]; then
            postsendargs="${postsendargs:+${postsendargs} }${arg}"
          elif [ ${ccarg} -eq 1 ]; then
-           #### Never reset ccarg to 0, for compatibilty with compiled mhmail.
+           #### ccarg can only be reset to 0 by -to.
            cclist="${cclist:+${cclist}, }${arg}"
          else
            #### An address.
            tolist="${tolist:+${tolist}, }${arg}"
+           toarg=0
          fi ;;
     esac
   done
@@ -218,7 +239,7 @@ Nmh-Attachment: ${arg}
 
   if [ "${body}"x = x ]; then
     #### First put message header in the file.
-    printf "%s" "${header}" >"${tmpfile}" || exit 2
+    printf "${header}" >"${tmpfile}" || exit 2
 
     tmpfile_size_before=`wc -c "${message_file}"`
     #### Now grab the body from stdin.  cat >> handles blank lines
@@ -244,12 +265,12 @@ Nmh-Attachment: ${arg}
 
     if [ "${tmpfileresent}" ]; then
       #### Put just the new message header in the file.
-      printf "%s" "${header}" >"${tmpfile}" || exit 2
+      printf "${header}" >"${tmpfile}" || exit 2
       #### and the body in the file to resend.
       printf "${body}" >"${tmpfileresent}" || exit 2
     else
       #### Put message header and body in the file.
-      printf "%s" "${header}${body}" >"${tmpfile}" || exit 2
+      printf "${header}${body}" >"${tmpfile}" || exit 2
     fi
   fi
 
@@ -265,10 +286,10 @@ Nmh-Attachment: ${arg}
     if "${post_or_send}" "${tmpfile}" ${postsendargs}; then
       exit
     else
+      status=$?
+      mv -f "${tmpfile}" dead.letter
       printf "Letter saved in dead.letter\n"
-      #### exec skips the trap set above.
-      [ "${tmpfileresent}" ]  &&  rm -f "${tmpfileresent}"
-      exec mv "${tmpfile}" dead.letter
+      exit $status
     fi
   fi
 fi