#! /bin/csh -f
#
#-	Kill - kill process with shell form interface
#-
#-	This  program will use  shell form to   display current user's
#-	process  generated by  ptree command.    You  can  tab to  the
#-	desired process  and select proper signal to  be  sent  to the
#-	process.  Press return will trigger the action.
#-
#	Author:		Paul Lew, General Systems Group, Inc.
#	Created at:	05/12/88  07:56 PM
#	Last update:	07/26/88  10:56 AM  (Edition: 32)
#
#-	Usage:		Kill [string]
#-	where option:
#-		string	if specified, will do 'ptree ax | grep string'
#-			and display only matched process.
#
set tmpfile = "/tmp/tmpfile.$$"
set sofile = "/tmp/sofile.$$"
set ptfile = "/tmp/ptfile.$$"
#---------------------------------------------------------------#
#	      Display help if requested by user			#
#---------------------------------------------------------------#
switch ( "$1" )
	case -H[xX]:
		set echo; set verbose; shift
		breaksw
	case -H*:
		show_help `which $0` $1
		goto end
	default:
	endsw
#---------------------------------------------------------------#
#			Process Arguments			#
#---------------------------------------------------------------#
set greparg = '.'
set ptflag = 'x'
if ( "$1" != '' ) then
	set greparg = $1
	set ptflag = 'ax'
	endif
#---------------------------------------------------------------#
#		  Create sf description file			#
#---------------------------------------------------------------#
pt p$ptflag > $tmpfile
tr '~' '?' < $tmpfile | sed -n \
   -e '1s/COMMAND/SIGNAL &/p;1d' \
   -e  '/Kill/d' \
   -e "/$greparg/"'{;s/^\(..............................\)\(.*\)$/\1~~~~~~ \2/p;}' \
   > $ptfile
sed 's/^......//' $ptfile | colrm 80 > $tmpfile
@ lcount = `cat $tmpfile | wc -l`
@ lcount--
if ( $lcount == 0 ) then
	echo "...Pattern $greparg not found in pt list, aborted..."
	goto end
	endif
cat << cat_eof >> $tmpfile

[TAB (next process), SPACE (next signal), - (no signal), RETURN (execute)]

cat_eof
@ i = 0
set sig = "/-     /HUP/INT/QUIT/ILL/TRAP/ABRT/EMT/FPE/KILL/BUS/SEGV/SYS"
set sig = "$sig/PIPE/ALRM/TERM/URG/STOP/TSTP/CONT/CHLD/TTIN/TTOU/IO/XCPU"
set sig = "$sig/XFSZ/VTALRM/PROF/USR1/USR2/PWR/LOST/"
while ( $i < $lcount )
	@ i++
	echo ".v=var$i.s=$sig." >> $tmpfile
	end
#---------------------------------------------------------------#
#		   run sf to get user input			#
#---------------------------------------------------------------#
sf -u -o $sofile < $tmpfile
if ( ! -e $sofile ) goto end
source $sofile
@ i = 0
while ( $i < $lcount )
	@ i++
	set var = `eval echo \$var$i`
	if ( "$var" != '-' ) then
		@ line = $i + 1
		kill -$var `sed -n "$line"'s/^ *\([0-9]*\).*$/\1/p' $ptfile`
		endif
	end
#---------------------------------------------------------------#
#		Clean up and exit here...			#
#---------------------------------------------------------------#
end:
	/bin/rm -f $tmpfile $sofile $ptfile
	unset tmpfile sofile ptfile i lcount var line greparg ptflag
