From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from imr1.bain.com.au ([203.0.62.7]) by hawkwind.utcs.utoronto.ca with SMTP id <45564>; Wed, 30 May 2001 03:33:12 -0500 Received: from bmr1-e1.aus.deuba.com by imr1.bain.com.au id f4U5YY826129; Wed, 30 May 2001 15:34:34 +1000 (EST) Received: from mailhost.aus.deuba.com [10.179.4.6] by bmr1-e1.aus.deuba.com id f4U5YX027913; Wed, 30 May 2001 15:34:33 +1000 (EST) Received: from merton.aus.deuba.com (merton.bain.com.au [10.179.23.56]) by mailhost.aus.deuba.com (8.9.1/8.9.1) with ESMTP id PAA12433 for ; Wed, 30 May 2001 15:34:33 +1000 (EST) Received: (from callum@localhost) by merton.aus.deuba.com (8.11.2/8.11.2/SuSE Linux 8.11.1-0.5) id f4U5YWh18448 for rc@hawkwind.utcs.utoronto.ca; Wed, 30 May 2001 15:34:32 +1000 From: Callum Gibson Message-Id: <200105300534.f4U5YWh18448@merton.aus.deuba.com> Subject: history and multiple substitutions To: rc@hawkwind.utcs.utoronto.ca (rc Shell) Date: Wed, 30 May 2001 01:34:32 -0500 X-Mailer: ELM [version 2.5 PL2] MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit 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)