#!/bin/sh
psh << '--PSH_EOF--'
%
% This file is a product of Sun Microsystems, Inc. and is provided for
% unrestricted use provided that this legend is included on all tape
% media and as a part of the software program in whole or part.  Users
% may copy or modify this file without charge, but are not authorized to
% license or distribute it to anyone else except as part of a product
% or program developed by the user.
% 
% THIS FILE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
% WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
% PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
% 
% This file is provided with no support and without any obligation on the
% part of Sun Microsystems, Inc. to assist in its use, correction,
% modification or enhancement.
% 
% SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
% INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS FILE
% OR ANY PART THEREOF.
% 
% In no event will Sun Microsystems, Inc. be liable for any lost revenue
% or profits or other special, indirect and consequential damages, even
% if Sun has been advised of the possibility of such damages.
% 
% Sun Microsystems, Inc.
% 2550 Garcia Avenue
% Mountain View, California  94043
%
%
%	psps 23.2 90/06/19
%
% A NeWS process lister by James Graham
%

currentprocess /ProcessName (psps) put

/applyformat {	% (fmtstr) [objarray] => (formatted string)
		% objn ... obj1 (fmtstr) => (formatted string)
    dup type /arraytype eq {
	3 exch {
	    1 index 1 roll
	    1 add
	} forall
	pop
    } if					% on...o1 fmt
    dup length string copy			% on...o1 fmt'
    dup
    {						% on...oi fmt' fmt'
	 (%) search {				% on...oi fmt' fmt'' (%) ( )
	    exch 0 ( ) 0 get put		% on...oi fmt' fmt'' ( )
	    4 -1 roll				% on... fmt' fmt'' ( ) oi
	    dup type /stringtype ne		% on... fmt' fmt'' ( ) oi b
	    { 100 string cvs } if		% on... fmt' fmt'' ( ) si
	    dup length				% on... fmt' fmt'' ( ) si li
	    2 index length gt {			% on... fmt' fmt'' ( ) si
		0 2 index length getinterval	% on... fmt' fmt'' ( ) si'
	    } if
	    0 exch putinterval			% on... fmt' fmt''
	} {
	    exit				% fmt' fmt''
	} ifelse
    } loop					% fmt' fmt''
    pop						% fmt'
} def

/psformat ( %        %          %   %   %   %   %) def
psformat [() (ID) (State) (Pri) (ESS) (OSS) (DSS)] applyformat
(Name\n) append
print

getprocesses {						% [process ...]
    (>) exch						% (>) [process ...]
    {							% > proc
	dup /DictionaryStack get length			% > proc ds
	1 index /OperandStack get length		% > proc ds os
	2 index /ExecutionStack get length 2 idiv	% > proc ds os es
	3 index /Priority get				% > proc ds os es pri
	4 index /State get				% > proc d o e p state
	5 index 100 string cvs
	    (\() search {
		pop pop (,) search {
		    3 1 roll pop pop
		} if
	    } if					% > proc d o e p s id
	8 -1 roll					% proc d o e p s id >
	psformat applyformat				% proc ()
	exch /ProcessName get append
	dup length 79 gt {0 79 getinterval} if		% ()
	print (\n) print
	()
	pause
    } forall
    pop
} forall

--PSH_EOF--
