rc-list - mailing list for the rc(1) shell
 help / color / mirror / Atom feed
* Re: Match operator puzzlement
@ 1992-02-01 19:46 malte
  0 siblings, 0 replies; 8+ messages in thread
From: malte @ 1992-02-01 19:46 UTC (permalink / raw)
  To: rc

It seems to me that weird problems deserve weird solutions.
I'd suggest to try selfmodifying rc-code, something like

prog = `function_generating_rc_script

eval $prog

Malte.



^ permalink raw reply	[flat|nested] 8+ messages in thread
* Re: Match operator puzzlement
@ 1992-02-03 17:09 Byron Rakitzis
  0 siblings, 0 replies; 8+ messages in thread
From: Byron Rakitzis @ 1992-02-03 17:09 UTC (permalink / raw)
  To: rc

Re: evaluating *'s. The rule is simple. Globbing happens after
nearly everything else, so if you have a command that begins
with a ~, the ~ will "steal" the metacharacters:

	foo='*'

	eval ~ bar $foo

gets rescanned as

	~ bar *

which returns true.


^ permalink raw reply	[flat|nested] 8+ messages in thread
* Re: Match operator puzzlement
@ 1992-02-02 20:20 Tom Culliton x2278
  0 siblings, 0 replies; 8+ messages in thread
From: Tom Culliton x2278 @ 1992-02-02 20:20 UTC (permalink / raw)
  To: rc

Reply-To: srg!culliton@uunet.uu.net

John Mackin writes:

> The reason a straightforward attempt doesn't work isn't really anything
> to do with rescanning at all, since there IS no rescanning

I thought about adding "or lack thereof", honest I did!  But since it
seemed to imply that not rescanning was wrong it got left out.  The
real difficulty, which I saw quite clearly, is that ~ doesn't accept a
list of patterns (primarily so you can match the empty list) so the
eval is needed to flatten the command out and rescan it (flattening the
list doesn't work because it becomes a single item) but at this point
we get undesired rescanning (in a filename context?) which is not
easily quoted away. 

There also seems to be a minor ambiguity between string matching
contexts and filename expansion contexts. In most contexts $ gives the
contents of variable (or something related) and patterns (using * ? [])
give a list of filenames.  In matching contexts $ works the same but
patterns are compared against strings.  Where the two collide it gets a
bit messy.  Given:

	patterns=('*.o' '*.a')		# or patterns='*.o *.a'
	eval ~ '$i' $patterns

It's not clear to me which context *.o will be evaluated in.  Will it
filename expand to a whole list of patterns which are then matched? 
This matters for obvious reasons.  Maybe Byron can clarify this?

For the curious the application is to scan a development directory tree
automatically generating a makefile/target for configuration management
purposes.  One of the requirements was to be able to exclude files
matching a certain template or set of templates (given on the command
line) from the target, typically things like *.o *.a and other
generated or binary files.  While testing it I actually did encounter a
filename with a $ in it (a backup or temporary file for some app) 8-P
and while I don't like names like that, my program can't just choke on
them!

My less than satisfactory solution was to carefully document that the
patterns had to be quoted, just so, when they are given on the command
line.  Byron's sed script seems like it will solve most of the problem
and John's newline hack should cover any remaining glitches.

Tom


^ permalink raw reply	[flat|nested] 8+ messages in thread
* Re: Match operator puzzlement
@ 1992-02-01 18:43 malte
  0 siblings, 0 replies; 8+ messages in thread
From: malte @ 1992-02-01 18:43 UTC (permalink / raw)
  To: rc

Could someone please give a more elaborated example? I'm afraid
I don't see the point here.

Malte.



^ permalink raw reply	[flat|nested] 8+ messages in thread
* Re: Match operator puzzlement
@ 1992-02-01 17:49 Byron Rakitzis
  1992-02-01 18:02 ` John Mackin
  0 siblings, 1 reply; 8+ messages in thread
From: Byron Rakitzis @ 1992-02-01 17:49 UTC (permalink / raw)
  To: rc

Hm. I think I can do a little better than John here. The two following
sed substitutions should be ok to quote all characters in a word except
the metacharacters *, ? and [. The sed scripts will FAIL if there are
single quotes in the input, but I'm sure there's a way around that
problem too, I just haven't thought of a good way yet.

Anyway:

First you turn all single non-meta characters into quoted characters:
	
	sed 's/\([^[*?]\)/''\1''/g'	# using rc's quoting rules, of course.

e.g., "Hello?" goes to "'H''e''l''l''o'?"

Now you need to remove all '' sequences:

	sed 's/''''//g'

Now "Hello?" becomes "'Hello'?", which is exactly what we want. Not bad.

(BTW, I think I can prove by induction that this method works for all
strings which do not contain a ', but I do not have enough room
to write the proof here :-)

So the ~ example becomes something like:

	eval ~ 'subject' `{echo $funky_pattern | sed garbage}


^ permalink raw reply	[flat|nested] 8+ messages in thread
* Match operator puzzlement
@ 1992-01-31 21:01 Tom Culliton x2278
  1992-02-01 16:02 ` John Mackin
  0 siblings, 1 reply; 8+ messages in thread
From: Tom Culliton x2278 @ 1992-01-31 21:01 UTC (permalink / raw)
  To: rc

Reply-To: srg!culliton@uunet.uu.net

OK maybe it's just 'cuz it's Friday but my poor brain refuses to come
up with a good answer to this one.  While writing some fairly large and
complex rc scripts I had a requirement to match something against a
list of patterns specified at the command line and figured it'd be easy
give rc's ~ match operator.

The first attempt was something like this:

	patterns=`{echo $1}	# go from '*.o *.a' to (*.o *.a)

	# lots of stuffto generate a list on file names....

	for (i in $list) {
		if (~ $i $patterns) {
			dealwith $i
		} else {
			handle $i
		}
	}

Which didn't work as planned for semi-obvious reasons involving
re-scanning.  This didn't distress me too much because I mostly
understood why after a bit of thought.  The next most obvious thing to
try was somthing like this

	patterns=$1	# we don't care if it's a list for this

	# lots of stuff to generate a list on file names....

	for (i in $list) {
		if (eval ~ $i $patterns) {	# etc...

OOPS! I encountered a file name with a $ in it so make that

		if (eval ~ '$i' $patterns) { 	# etc...

But what about patterns with $ and so forth in them?  If we make
$patterns a list again and say '$patterns' to protect against that, we
get literal matching.  I haven't been able to find a way out of the
swamp here any words of wisdom would be appreciated.

Thanks for listening.

Tom


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~1992-02-03 17:09 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1992-02-01 19:46 Match operator puzzlement malte
  -- strict thread matches above, loose matches on Subject: below --
1992-02-03 17:09 Byron Rakitzis
1992-02-02 20:20 Tom Culliton x2278
1992-02-01 18:43 malte
1992-02-01 17:49 Byron Rakitzis
1992-02-01 18:02 ` John Mackin
1992-01-31 21:01 Tom Culliton x2278
1992-02-01 16:02 ` John Mackin

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