rc-list - mailing list for the rc(1) shell
 help / color / mirror / Atom feed
* history and multiple substitutions
@ 2001-05-30  6:34 Callum Gibson
  0 siblings, 0 replies; only message in thread
From: Callum Gibson @ 2001-05-30  6:34 UTC (permalink / raw)
  To: rc Shell

G'day.

I'm a recovering *csh user. I hope it's not too presumptuous to post
a patch in my first post, but I figure source code is always welcome.
A colleague at work has put me on to rc and he thought this patch nifty
enough to pass on to you all.

One of the annoyances of csh's ^ (and now rc's :) history substitution
for me has been that it only replaces the first occurrence on the line. I
frequently redirect output into a file based upon the args of the program
I'm running.

e.g.
; prog abc > output.abc
; prog def > output.def
etc.
So ^abc^def (or "- abc:def") will clobber your first output file.

The patch below alters history.c such that consecutive colons (:) in the
- and -- history subsitition function, will subsitute your pattern according
to the number of colons you supply. (A more general approach is to change
the existing syntax to mean replace until there are no more matches, but I
decided to apply POLA here.) The original semantics don't escape unscathed
but I think this is a reasonable compromise.

Thus:
; prog abc > output.abc
; - abc::def
prog def > output.def
; - def::ghi
prog ghi > output.ghi
etc.

I hope someone finds it useful. If there's enough interest I'll update the
manpage accordingly too. Oh, this diff is against rc-1.6 release, but it
looks like history.c hasn't changed in some time in any case.

regards,
    C

(c)2001 Callum Gibson                                  callum.gibson@db.com
Fixed Income IT, Deutsche Bank, Australia                    61 2 9258 1620
### The opinions presented herein do not represent those of my employer ###

--- history.c.orig	Sat Dec  5 03:32:40 1998
+++ history.c	Wed May 30 11:48:07 2001
@@ -26,6 +26,7 @@
 
 static struct {
 	char *old, *new;
+	int reps;	/* no. of repetitions. i.e. 1 means sub twice. */
 } *replace;
 
 static char **search, *progname, *history;
@@ -296,8 +297,12 @@
 			search[nsearch++] = argv[i];
 		else {
 			*(char *)s = '\0';	/* do we confuse ps too much? */
+			replace[nreplace].reps = 0;
+			while(*(++s) == ':') {
+			      replace[nreplace].reps++;
+			}
 			replace[nreplace].old = argv[i];
-			replace[nreplace].new = s + 1;
+			replace[nreplace].new = s;
 			nreplace++;
 		}
 
@@ -312,8 +317,11 @@
 	for (i = 0; i < nreplace; i++)
 		if (!isin(s, replace[i].old))
 			goto next;
-		else
-			s = sub(s, replace[i].old, replace[i].new);
+		else {
+		    	int j;
+			for (j = 0; j <= replace[i].reps; j++)
+			      s = sub(s, replace[i].old, replace[i].new);
+		}
 	if (editit) {
 		s = edit(s);
 		if (s == NULL)


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2001-05-30  8:33 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-05-30  6:34 history and multiple substitutions Callum Gibson

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