The edprofile Shell Script

[return-to Explanation of edprofile] [table of contents] [index]
#! /bin/sh
# $Id: edprofile,v 1.0 92/08/03 08:30:09 jerry book2 $
### edprofile - edit line in $MH file (default: $HOME/.xmh_profile)
### Usage: edprofile [-v] component [new-value] --
##
##  xmh USES THE DEFAULT MH COMMANDS scan, sortm, AND OTHERS.  xmh MAKES IT
##  TOUGH TO SET ANY OPTIONS, LIKE THE SORTING ORDER (ADDED IN MH 6.7),
##  WHILE xmh IS RUNNING.  YOU CAN EDIT THE MH PROFILE FILE IN AN xterm
##  WINDOW, OR PLAY SOME TRICKS WITH THE MhPath AND FRONT-END SHELL
##  SCRIPTS, BUT THAT'S ABOUT ALL.  THIS edprofile SCRIPT WORKS WITH xmh
##  RELEASE 5 AND ITS XmhShellCommand() TO LET YOU EDIT LINES IN THE MH
##  PROFILE WHILE YOU'RE RUNNING xmh.  YOU CAN BIND edprofile TO A BUTTON
##  THAT, SAY, CHANGES THE sort: OPTIONS.  YOUR NEXT SORT WILL USE THOSE
##  OPTIONS.  IF new-value IS EMPTY, SCRIPT WILL LEAVE AN EMPTY COMPONENT
##  THERE FOR NEXT TIME (THIS DOESN'T SEEM TO BOTHER MH).
##
##  THE -v FLAG WRITES A MESSAGE TO THE STANDARD ERROR WITH THE NEW
##  CONTENTS OF THE MH PROFILE LINE.  xmh R5 SHOWS THAT IN A DIALOG BOX.
##
##  IF THE MH ENVIRONMENT VARIABLE HAS BEEN DEFINED, edprofile EDITS THE
##  FILE THAT POINTS TO.  THE DEFAULT FILE IS $HOME/.mh_profile.
##
##  HERE'S AN EXAMPLE--HOW TO MAKE TWO BUTTONS THAT CHANGE sortm.
##  PUT THESE LINES IN YOUR RESOURCE FILE:
##
##  Xmh*CommandButtonCount: 2
##  Xmh*commandBox.button3.label: From sort
##  Xmh*commandBox.button3.translations: #override\
##    <Btn1Down>,<Btn1Up>: XmhShellCommand(edprofile sortm -textfield from --) unset()
##  Xmh*commandBox.button1.label: date sort
##  Xmh*commandBox.button1.translations: #override\
##    <Btn1Down>,<Btn1Up>: XmhShellCommand(edprofile -v sortm --) unset()
##
##  THE FIRST BUTTON, LABELED From sort, RUNS THE COMMAND
##      edprofile sortm -textfield from --
##  THAT CHANGES THE MH PROFILE sortm ENTRY TO LOOK LIKE THIS:
##      sortm: -textfield from
##  ANY SWITCHES ON THE LINE BEFORE ARE TAKEN AWAY.
##
##  IF YOU CLICK THE SECOND BUTTON, IT WILL RUN THE COMMAND:
##      edprofile -v sortm
##  THAT LINE WILL BE CHANGED TO THIS:
##      sortm:
##
##  UNFORTUNATELY, THERE MUST BE A CURRENT OR SELECTED MESSAGE BEFORE YOU
##  RUN XmhShellComand.  IF THERE'S NOT A MESSAGE SELECTED, PICK ANY ONE
##  BEFORE YOU CLICK THE BUTTON THAT RUNS edprofile.
##
##  NOTE TO HACKERS: TABSTOPS ARE SET AT 4 IN THIS CODE

# ON ERROR, XmhShellCommand SHOWS ALL ARGUMENTS IN NOTICE BOX; WE DON'T HAVE TO.

grep=/bin/grep

: ${MH=${HOME?}/.mh_profile}    # THE PROFILE FILE WE EDIT

case "$1" in
-v) verbose=yes; shift;;
esac
# COMPONENT WE SEARCH FOR IS IN $1; REST OF ARGS HANDLED IN for LOOP:
case $# in
0) echo "`basename $0` quitting: not enough arguments." 1>&2; exit 1 ;;
*)  cmd="$1"; shift;;
esac

ended= newargs=
for arg
do
    case "$arg" in
    --) ended=yes; break ;;
    *)  newargs="$newargs $arg" ;;
    esac
done

if [ "$ended" != yes ]; then
    echo "`basename $0` quitting: missing '--' after argument list." 1>&2
    exit 1
fi

if [ ! -r "$MH" -o ! -w "$MH" ]; then
    echo "`basename $0` quitting: can't read and/or write profile
    '$MH'" 1>&2
    exit 1
elif $grep "^$cmd:" "$MH" >/dev/null; then
    # DO THE EDIT.  SAVE ed OUTPUT AND STATUS FOR TESTING:
    errs=`/bin/ed - "$MH" << ENDEDIT 2>&1
    /^$cmd:[    ]*/s/:.*/: $newargs/
    w
ENDEDIT`
    status=$?
    if [ -n "$errs" -o $status != 0 ]; then
        echo "`basename $0`: edit bombed?  ed status=$status, messages:
        $errs" 1>&2
        exit 1
    elif [ "$verbose" = yes ]; then
        case "$newargs" in
        "") echo "$cmd set to default (no parameters)" 1>&2 ;;
        *)  echo "$cmd set to: $newargs" 1>&2 ;;
        esac
    fi
    exit 0
else
    echo "`basename $0` quitting: can't find '$cmd:' in profile
    '$MH'" 1>&2
    exit 1
fi

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


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

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>