sam icon indicating copy to clipboard operation
sam copied to clipboard

Add ssam script

Open TobiasKarnat opened this issue 4 years ago • 6 comments

Please add the ssam script as in the plan9port: https://9fans.github.io/plan9port/man/man1/ssam.html https://github.com/9fans/plan9port/blob/master/bin/ssam

I did a quick implementation in bash:

#!/bin/bash
if [[ ( -t 0 && "$#" -ge 2 ) || (! -t 0 && "$#" -eq 1) ]]; then
    cmd=$1; shift
    tmp=/tmp/ssam.tmp.$USER.$$
    cat $* > $tmp
else
    echo "Usage: ssam SCRIPT [FILE]..."
    exit
fi

{
    # select entire file
    echo ',{'
    echo k
    echo '}'
    echo 0k

    # sam commands
    [ -f $cmd ] && cat $cmd || echo $cmd
} | LANG=C.UTF-8 sam -d $tmp 2>/dev/null

rm -f $tmp

TobiasKarnat avatar Sep 25 '19 18:09 TobiasKarnat

Added one that uses the same parameters as ssam from plan9ports, but it uses getopts which can probably be written out (but i need my quotez!). Can probably also use trap to ensure the file gets rm'd...

#!/bin/sh
arg0=`basename "$0"`

usage()
{
    printf "usage: %s [-n] [-e script] [-f sfile] [file ...]\n" "$1"
}

flagn=0
flage=""
flagf=""

while getopts ne:f:h OPT; do
    case "$OPT" in
        n)
            flagn=1
            ;;

        e)
            if [ -z "$OPTARG" ]; then
                usage "$arg0"
                exit 1
            fi
            flage="$OPTARG"
            ;;
        f)
            if [ -z "$OPTARG" ]; then
                usage "$arg0"
                exit 1
            fi
            flagf="$OPTARG"
            ;;
        h)
            usage "$arg0"
            exit 0
            ;;
    esac
done
shift `expr "$OPTIND" - 1`

if [ -z "$TMPDIR" ]; then
    TMPDIR="/tmp"
fi
tmp="$TMPDIR/ssam.tmp.$USER.$$"

cat "$@" >$tmp

input()
{
    # select entire file
    echo ',{'
    echo k
    echo '}'
    echo 0k

    # run scripts, print
    [ ! -z "$flagf" ] && cat "$flagf"
    [ ! -z "$flage" ] && echo "$flage"
    [ "$flagn" -eq 0 ] && echo ','
}

input | sam -d "$tmp" 2>/dev/null

rm -f "$tmp"

arizvisa avatar Jul 07 '20 20:07 arizvisa

Hey Ali!

Added your ssam to the repo; let me know if it looks all right.

deadpixi avatar Jul 07 '20 20:07 deadpixi

Hah! Heyo good sir.

Actually, let me tweak it a bit and submit you a PR. The only issue is that the "$tmp" file isn't guaranteed to be removed if some signal hits the shellscript which we can trap to do. I tried to keep its appearance exact same as the plan9ports' version, but rc has some implicit guarantees that bourne requires you to be explicit with.

arizvisa avatar Jul 07 '20 21:07 arizvisa

Okay, sir. PR #112 handles those issues that I mentioned.

Thx for maintaining this btw, I'm trying to convince my coworkers how fucking awesome sam is. So I'm trying to get them to drop sed/awk for it (hence the need for ssam).

arizvisa avatar Jul 07 '20 22:07 arizvisa

make install now complains about missing destination file operand for cp after ssam. The problem seems to be that the makefile now refers the BINDIR variable, which is defined in config.mk and not included in the top makefile.

klpn avatar Jul 08 '20 10:07 klpn

I note that a ssam makefile has been added in e0a526e; I guess the top makefile should be updated with a ssamdir target and the additions to that file in 1dcf1de should be removed?

klpn avatar Jul 12 '20 15:07 klpn