From: Bengt Kleberg <bengt@softwell.se>
To: sam-fans@hawkwind.utcs.toronto.edu, wilyfans@jli.com
Subject: Re: ssam, again
Date: Mon, 6 Mar 2000 06:06:49 -0500 [thread overview]
Message-ID: <200003061105.MAA22737@trillian.softwell.se> (raw)
Thanks to suggestion and code by "James A. Robinson" <jim.robinson@stanford.edu>
I have managed to cut disk usage in 1/2, using a fifo instead of a tmp file.
I now think it is good enough to ship with sam-9libs. Any one aginst?
(it is still not a stream editor, and would not work if /tmp is 50 MB and
the file to edit is 50 MB, too)
script follows:
#! /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}
# there is no change-file-into-pipe cmd, so remove it before mkfifo
# this allows for a denial-of-service attack, but mkfifo -m protect against disclosure
rm $tmp_file
}
mkfifo -m 0700 $tmp_file
if (! ~ $status 0) {
echo mkfifo failed, someone is deliberatly "stealing" your filename
echo this indicates an attempt to read the text you want to edit
exit 4
}
# 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
}
# send edit scripts to sam and contents of edit script files
# then print contents, unless -n argument
# quit (twice since file has probably not been saved)
i = 1
max = `{expr $#edit_script + 1}
{
echo r $tmp_file
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 >[2] /dev/null &
#}
sam_pid = $apid
# anything left is file(s) to be edited, otherwise use stdin
if (~ $#* 0) {
cat >> $tmp_file
} else {
cat $* >> $tmp_file
}
wait $sam_pid
rm $tmp_file
exit 0
next reply other threads:[~2000-03-12 2:32 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <bengt@softwell.se>
2000-03-06 11:06 ` Bengt Kleberg [this message]
2000-03-07 0:36 ` James A. Robinson
2000-03-01 15:04 Bengt Kleberg
2000-03-01 16:17 ` James A. Robinson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=200003061105.MAA22737@trillian.softwell.se \
--to=bengt@softwell.se \
--cc=sam-fans@hawkwind.utcs.toronto.edu \
--cc=wilyfans@jli.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).