Subject: scripts using 3/12 sam command pipe?

From: Ed Kubaitis - CCSO <[email-masked]>
Date: Wed, 17 Mar 1993 12:59:12 -0500

Here's a pretty obvious one. Anyone else have one to share?

Ed
===============================================================================
#! /usr/local/bin/perl
#
#   samtag - generate Sam commands from ctags(1) tags file
#
#   usage: !samtag tag     -OR-    (select-tag-with-mouse)
#                                  >samtag

$user = $ENV{'USER'};
$user = $ENV{'LOGNAME'} unless $user;
$user || die "samtag: no USER or LOGNAME environment variable.\n";

$pipe = ".sam.$user";
$pipe .= ".$ENV{'DISPLAY'}" if $ENV{'DISPLAY'};

$Pipe = "/tmp/$pipe";
$Pipe = "/usr/tmp/$pipe" unless -p $Pipe;
-p $Pipe || die "samtag: no sam pipe($pipe) in /tmp or /usr/tmp\n";

$Tag = shift;
$Tag = <STDIN> unless $Tag;
die "samtag: no tag specified." unless $Tag;

open(Tags, "tags") || die "samtag: tags: $!\n";
while(<Tags>) {
   /^([^\t]+)\t([^\t]+)\t(.+)$/;
   $tag = $1; $file =$2; $re =$3;
   last if $tag eq $Tag
   }
die "samtag: tag <$Tag> not found.\n" unless $tag eq $Tag;
$re =~ s/([()*[\]])/\\$1/g; # escape metacharacters in re

open(Pipe, ">$Pipe") || die "samtag: $Pipe: $!\n";
print Pipe "B $file\n$re\n";
close(Pipe);

---

From: [email-masked]
Date: Wed, 17 Mar 1993 13:28:23 -0500

I had a test copy of Boyd's stuff and have been using this for a
while.  Similar but written in rc (uses look and sed).  You'll have
to edit the "sp=" line ...

It is probably just that I haven't been able to really wrap my mind
around structural R.E.'s all that much, but find that I am not using
them all that much.  I use the multi-file aspect all the time; it is
my primary reason for using sam.  As a result, I am often writing little
shell snippets like this:
	for (i in stderr stdout nolog console) {
	    echo 'X/./ ,g/svc_c_' ^ $i ^ '/ {'
	    echo ',x/svc_c_' ^ $i ^ '/ x/svc_c_/ a/route_/'
	    echo '}'
	} >>$h/.sam.rsalz
Actually, I often write those snippets in an "unnamed" window, select
them, and then do ">rc >$sp" where sp is an envvar naming the sam pipe.
	/r$

#! /bin/rc
##  Output a series of commands that will make same go to the tag named on
##  the command line.  If no argument, use the word in the current X
##  selection.  The best way to get the selection is to install xcb; available
##  as contrib/xcb-2.1.tar.Z on export.lcs.mit.edu.

##  Useful variables
~ $#tagspath 0 && tagspath = (. .. /usr/lib/tags)
nl = '
'
tab='	'
cat=()

##  Print usage message and exit.
fn usage {
    echo Usage: $0 '[-o] [-i | tag]' >[1=2]
    exit 1
}

##  Parse flags.
while ( ! ~ $#* 0 ) {
    switch ( $1 ) {
    case -i
	! ~ $#function 0 && usage
	function = `` ($nl) {sed 1q}
    case -o
	cat=cat
    case -*
	usage
    case *
	! ~ $#function 0 && usage
	function = $1
    }
    shift
}

##  Sending to sam?
~ $#cat 0 && {
    ~ $#sp 0 && {
	sp=$home/.sam.$USER
	! ~ $#DISPLAY 0 && sp=$sp.$DISPLAY
    }
    test -r $sp || {
	echo 'No sam fifo' >[1=2]
	usage
    }
    cat='cat >$sp'
}

##  Get the current X selection.
~ $#function 0 && function = `{xcb -p 0}

function = $function ^ $tab

for (dir in $tagspath)
    test -f $dir ^ /tags && {
	line = `` ($nl) { look $function $dir ^ /tags ; echo $status }
	~ $#line 2 && ~ $line(2) 0 && {
	    {
		* = `` ($tab $nl) { echo $line(1) }
		if ( ~ $dir . || ~ $2 /* ) {
		    echo B $2 ^ $nl ^ b $2
		} else {
		    echo B $dir ^ '/' ^ $2 ^ $nl ^ b $dir ^ '/' ^ $2
		}
		shift ; shift
		{
		    echo -n $1
		    while (shift && ! ~ $#* 0)
			echo -n $tab ^ $1
		    echo
		} | sed -e 's/\[/\\[/g' \
			-e 's/[].+?|()*$^]/\\&/g' \
			-e 's/\/\\\^/\/\^/' \
			-e 's/\\\$\/$/\$\//' \
			-e 's/	/	+/g'
	    } | eval $cat
	    exit 0
	}
    }
exit 1