rc-list - mailing list for the rc(1) shell
 help / color / mirror / Atom feed
From: Callum Gibson <callum.gibson@db.com>
To: rc@hawkwind.utcs.utoronto.ca (rc Shell)
Subject: history and multiple substitutions
Date: Wed, 30 May 2001 01:34:32 -0500	[thread overview]
Message-ID: <200105300534.f4U5YWh18448@merton.aus.deuba.com> (raw)

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)


                 reply	other threads:[~2001-05-30  8:33 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200105300534.f4U5YWh18448@merton.aus.deuba.com \
    --to=callum.gibson@db.com \
    --cc=rc@hawkwind.utcs.utoronto.ca \
    --subject='Re: history and multiple substitutions' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

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