9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: Lucio De Re <lucio@proxima.alt.za>
To: 9fans mailing list <9fans@cse.psu.edu>
Subject: [9fans] Ugly hack
Date: Sat, 15 Feb 2003 17:46:05 +0200	[thread overview]
Message-ID: <20030215174605.I9084@cackle.proxima.alt.za> (raw)

An ugly hack to Tom Duff's little jewel, rc(1).

I've found the ability to limit shell command line argument expansion
(only in English can one write something so succintly and so ugly) to
directories rather than all directory entries by suffixing the last
part with a "/".  This is normal behaviour in the NetBSD shells.

For example:

broken! lc
mkfile             rdesktop-1.1.0.tar trim
openldap-2.0.22    rx-1.5             trim.c
rdesktop-1.1.0     term.8             trim.out
broken! cd rdesk*
Usage: cd [directory]
broken! echo rdesk*
rdesktop-1.1.0 rdesktop-1.1.0.tar
broken! cd rdesk*/
broken! pwd
/usr/lucio/Projects/Sundry/rdesktop-1.1.0
broken!

Note how the trailing slash allows me to supply the cd command with
a single argument.

I have hacked plan9.c, glob.c and fns.h in /sys/src/cmd/rc to achive
this and I'd appreciate some feedback: is this useful to anyone
else?  Does it work properly (I have tested a few limit cases, but
I'm not sure Ive covered all possibilities)?

The patch is not vast, it largely required modifying the Readdir()
function to be rearranged and I'd appreciate comments on possible
improvements to my choice of coding.

All comments (not flames) appreciated.  I hope my sources for rc(1)
are not too far out of date.

term% diff plan9.c `{yesterday plan9.c}
321c321
< int Readdir(int f, char *p, int dironly)
---
> int Readdir(int f, char *p)
326,345c326,334
< 	for (;;) {
< 		if (dir[f].i==dir[f].n){	/* read */
< 			free(dir[f].dbuf);
< 			dir[f].i=0;
< 			dir[f].dbuf=0;
< 			if ((n=dirread(f, &dir[f].dbuf)) <= 0){
< 				dir[f].n=0;
< 				return 0;
< 			} else
< 				dir[f].n=n;
< 		}
< 		while(dir[f].i<dir[f].n){
< // print ("N: '%s(%d)'\n", dir[f].dbuf[dir[f].i].name,(dir[f].dbuf[dir[f].i].mode&0x80000000));
< 			if(!dironly || (dir[f].dbuf[dir[f].i].mode&0x80000000)){
< 				strcpy(p, dir[f].dbuf[dir[f].i].name);
< 				dir[f].i++;
< 				return 1;
< 			}
< 			dir[f].i++;
< 		}
---
> 	if(dir[f].i==dir[f].n){	/* read */
> 		free(dir[f].dbuf);
> 		dir[f].dbuf=0;
> 		n=dirread(f, &dir[f].dbuf);
> 		if(n>=0)
> 			dir[f].n=n;
> 		else
> 			dir[f].n=0;
> 		dir[f].i=0;
347c336,340
< 	return 0;
---
> 	if(dir[f].i==dir[f].n)
> 		return 0;
> 	strcpy(p, dir[f].dbuf[dir[f].i].name);
> 	dir[f].i++;
> 	return 1;

term% diff glob.c `{yesterday glob.c}
37c37
< void globdir(char *p, char *namep, int dironly)
---
> void globdir(char *p, char *namep)
64c64
< 	/* read the directory and recurse for any entry that matches */
---
> 	/* read the directory and recur for any entry that matches */
68,69c68
< // print ("D: %s\n", dironly?"yes":"no");
< 	while(Readdir(f, namep, dironly)){
---
> 	while(Readdir(f, namep)){
72c71
< 			globdir(newp, t, dironly);
---
> 			globdir(newp, t);
84d82
< 	int dironly;
93,99c91
< 	dironly=strlen(p)-1;
< // print ("G: '%s(%d)'\n", p, dironly);
< 	if (dironly>0 && p[dironly]=='/'){
< 		dironly=1;
< 	} else
< 		dironly=0;
< 	globdir(p, globname, dironly);
---
> 	globdir(p, globname);

term% diff fns.h `{yesterday fns.h}
16c16
< int	Readdir(int, char*, int);
---
> int	Readdir(int, char*);




             reply	other threads:[~2003-02-15 15:46 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-02-15 15:46 Lucio De Re [this message]
2003-02-15 16:18 ` Sam
2003-02-15 16:21   ` Dan Cross
2003-02-15 16:29   ` Lucio De Re
2003-02-15 23:56 ` Geoff Collyer
2003-02-16  9:20   ` Lucio De Re
2003-02-16  9:25     ` Lucio De Re
2003-02-16 12:18       ` Lucio De Re
2003-02-16 12:45         ` Lucio De Re
2003-02-16 14:19 ` Russ Cox
2003-02-16 15:00   ` Lucio De Re
2003-02-16 15:31     ` David Butler
2003-02-16 15:40     ` Russ Cox
2003-02-17  5:27       ` Lucio De Re
2003-02-17  9:53 ` John Kodis

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=20030215174605.I9084@cackle.proxima.alt.za \
    --to=lucio@proxima.alt.za \
    --cc=9fans@cse.psu.edu \
    /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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).