The distprompter Shell Script

[return-to Explanation of distprompter] [table of contents] [index]
#! /bin/sh
# $Id: distprompter,v 1.9 92/08/03 07:31:17 jerry book2 $
### distprompter - replaces "prompter" for MH "dist" command
### Usage (in .mh_profile):    dist: -editor distprompter
##
##  BY DEFAULT, THE MH dist COMMAND USES prompter TO EDIT THE DRAFT
##  MESSAGE.  FOR dist, THAT'S NOT A GREAT CHOICE BECAUSE:
##      - IF YOU ACCIDENTALLY TYPE A BODY, THE MESSAGE CAN'T BE SENT
##      - YOU ALWAYS HAVE TO PRESS CONTROL-D TO SKIP THE BODY
##
##  distprompter IS AN EDITOR DESIGNED FOR dist.  IT READS THE
##  EMPTY HEADER THAT dist GIVES IT, LINE BY LINE.  IF A COMPONENT
##  IS EMPTY, IT PROMPTS YOU.  IF A COMPONENT IS FINISHED, IT DOESN'T
##  PROMPT.  IF A COMPONENT IS ILLEGAL (NOT Resent-xxx:), IT COMPLAINS.
##  WHEN IT'S READ THE HEADER, IT EXITS; YOU DON'T NEED CONTROL-D.
##
##  NOTE TO HACKERS: TABSTOPS ARE SET AT 4 IN THIS CODE

myname="`basename $0`"
err=/tmp/DISTPRe$$ header=/tmp/DISTPRd$$
> $header
chmod 600 $header

# UNCOMMENT THE RIGHT LINE FOR YOUR UNIX:
# echo="echo -n" nnl=       # BSD
# echo=echo      nnl="\c"   # SYSV
echo="echo -n" nnl=     PATH=/usr/bin:$PATH; export PATH    # SunOS

stat=1  # DEFAULT EXIT STATUS; RESET TO 0 FOR NORMAL EXITS
trap 'rm -f $header $err; exit $stat' 0
trap 'echo "$myname: Interrupt!  Cleaning up..." 1>&2; exit' 1 2 15

if [ ! -w "$1" -o -z "$1" ]
then
    echo 1>&2 "$myname: quitting: missing or unwritable draft
    '$1'"
    exit
fi

# READ DRAFT (A COPY OF distcomps FILE) LINE-BY-LINE.
# ACT LIKE prompter, BUT EXIT AFTER WE'VE READ DRAFT FILE
# (WHEN YOU USE dist, THE DRAFT FILE IS ONLY A HEADER).
# read AT TOP OF LOOP GETS STDIN (FD 0), SO SAVE FD 0 NOW:
exec 4<&0   # SAVE ORIGINAL STDIN (USUALLY TTY) AS FD 4
while read label line
do
    case "$label" in
    [Rr]esent-?*:)
        case "$line" in
        ?*) # SHOW LINE ON SCREEN AND PUT INTO HEADER FILE:
            echo "$label $line"
            echo "$label $line" 1>&3
            ;;
        *)  # FILL IT IN OURSELVES:
            $echo "$label $nnl"
            exec 5<&0   # SAVE DRAFT FILE FD; DO NOT CLOSE!
            exec 0<&4   # RESTORE ORIGINAL STDIN
            read ans
            exec 0<&5   # RECONNECT DRAFT FILE TO STDIN
            case "$ans" in
            "") ;;  # EMPTY; DO NOTHING
            *)  echo "$label $ans" 1>&3 ;;
            esac
            ;;
        esac
        ;;
    ""|---*) # END OF HEADER
        echo "-------" 1>&3
        break   # PROBABLY NOT NEEDED...
        ;;
    *)  echo "$myname: illegal header component
        '$label $line'" 1>&2
        break
        ;;
    esac
done <$1 2>$err 3>$header

# IF THE ERROR FILE HAS SOMETHING IN IT, SHOW IT AND QUIT:
if [ -s $err ]
then
    /bin/cat $err 1>&2
    echo "$myname: quitting." 1>&2
else
    if /bin/cp $header $1
    then stat=0
    else echo "$myname: can't replace draft '$1'?" 1>&2
    fi
fi
exit

[Table of Contents] [Index] [Return to Explanation of distprompter]


Last change $Date: 1996/06/06 15:13:38 $

This file is from the third edition of the book MH & xmh: Email for Users & Programmers, ISBN 1-56592-093-7, by Jerry Peek. Copyright © 1991, 1992, 1995 by O'Reilly & Associates, Inc. This file is freely-available; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation. For more information, see the file copying.htm.

Suggestions are welcome: Jerry Peek <jpeek@jpeek.com>