sam-fans - fans of the sam editor
 help / color / mirror / Atom feed
* ssam, script
@ 2000-03-01 15:04 Bengt Kleberg
  0 siblings, 0 replies; only message in thread
From: Bengt Kleberg @ 2000-03-01 15:04 UTC (permalink / raw)
  To: sam-fans, wilyfans

#! /usr/local/plan9/bin/rc

cmd_name = $0
fn usage {
	echo Usage: $cmd_name [-n] [-e script ] [-f sfile ] [ script ] [--] [ file ] ...
}

# directory for saving all text to be edited
# use /tmp or TMPDIR if set
tmp_dir=/tmp
if (! ~ $TMPDIR ()) {
	tmp_dir=$TMPDIR
}

# file for saving all text to be edited
tmp_file = $tmp_dir/ssam.$pid.XXXXXXXX
# if OpenBSD mktemp exist, use it
if (test -x /usr/bin/mktemp) {
	tmp_file = `{/usr/bin/mktemp $tmp_file}
}


# clean up on signals
fn sighup {
	rm $tmp_file
	exit 3
}
fn sigint {
	rm $tmp_file
	exit 3
}
fn sigquit {
	rm $tmp_file
	exit 3
}

# default values for variables
printall = 1
edit_script = ()
edit_type = ()

# parse command line arguments
while ({~ $1 -*} && {! ~ $1 --}) {
	switch ($1) {
	case -n
		printall = 0
		shift
	case -e
		shift
		edit_script = ($edit_script $1)
		edit_type = ($edit_type e)
		shift
	case -f
		shift
		if (test -f $1) {
			edit_script = ($edit_script $1)
			edit_type = ($edit_type f)
			shift
		} else {
			echo $0: No such sfile: $1
			exit 2
		}
	case *
		usage
		exit 1
	}
}

# anything left that is not a file is a (single) edit script
if (! test -f $1) {
		edit_script = ($edit_script $1)
		edit_type = ($edit_type e)
		shift
}

# anything left is file(s) to be edited, otherwise use stdin
if (~ $#* 0) {
	cat >> $tmp_file
} else {
	cat $* >> $tmp_file
}

# send edit scripts to sam and contents of edit files
# then print contents, unless -n argument
# quit (twice since file has probably not been saved)
i = 1
max = `{expr $#edit_script + 1}
{
while (! ~ $i $max) {
	switch ($edit_type($i)) {
	case e
		echo $edit_script($i)
	case f
		cat $edit_script($i)
	}
	i = `{expr $i + 1}
}
if (~ $printall 1) {
	echo '1,$ p'
}
echo q
echo q
} | sam -d $tmp_file >[2] /dev/null
#}

rm $tmp_file
exit 0


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2000-03-01 18:08 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-03-01 15:04 ssam, script Bengt Kleberg

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).