Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ssam script #104

Open
TobiasKarnat opened this issue Sep 25, 2019 · 6 comments
Open

Add ssam script #104

TobiasKarnat opened this issue Sep 25, 2019 · 6 comments

Comments

@TobiasKarnat
Copy link

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
@arizvisa
Copy link
Contributor

arizvisa commented Jul 7, 2020

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"

@deadpixi
Copy link
Owner

deadpixi commented Jul 7, 2020

Hey Ali!

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

@arizvisa
Copy link
Contributor

arizvisa commented Jul 7, 2020

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
Copy link
Contributor

arizvisa commented Jul 7, 2020

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).

@klpn
Copy link

klpn commented Jul 8, 2020

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.

deadpixi added a commit that referenced this issue Jul 10, 2020
Trap a couple of signals in ssam in order to guarantee its temporary file is removed
@klpn
Copy link

klpn commented Jul 12, 2020

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants