9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: erik quanstrom <quanstro@quanstro.net>
To: Fans of the OS Plan 9 from Bell Labs <9fans@cse.psu.edu>,
	Russ Cox <rsc@swtch.com>
Subject: Re: [9fans] tab completion and command history in rc
Date: Thu,  8 Dec 2005 19:58:41 -0600	[thread overview]
Message-ID: <20051209015841.01BA6590D@rawlings.quanstro.net> (raw)
In-Reply-To: <ee9e417a0511040827i7f76d805y42199469f12b8c33@mail.gmail.com>

nope, i didn't object to the reformatting. due to the snow here i was stuck
for a couple of hrs with an ancient copy of p9p and decided to have another go
at the problem because i wanted history tied to a variable. i came up with this:

- erik

btw: is there any way to simulate byron's ``(new-ifs-list) {cmd} with rc?
for example:
	fu=x
	fn fu {echo x}
	nl = '
'
	for(i in ``$nl {whatis fu}) echo $i

	fu=x
	fn fn {echo x}

; rcsdiff syn.y
===================================================================
RCS file: RCS/syn.y,v
retrieving revision 1.1
diff -r1.1 syn.y
25c25,28
< |	line '\n'		{return !compile($1);}
---
> |	line '\n'			{ int i = !compile($1); 
> 				if (!i) 
> 					whistory($1); 
> 				return i;}
; cat history.c
// feel free to roll your eyes.

#include "rc.h"
#include "exec.h"
#include "fns.h"
#include "io.h"

void
whistory(tree* t){
	var* v;
	char* s;
	int fd;
	io* o;
	int flags = OAPPEND|OWRITE;

	if (!runq->iflag || !t)
		return;

	v = vlook("history");
	if (!v->val || 1 != count(v->val))
		return;

	if (v->fn)
		return;	// future: fn history {echo $*>>$history_file}

	s = v->val->word;
	if((fd=open(s, flags))<0 && (fd=create(s, flags, 0666L))<0){
		//setvar("history", 0);
 		return;
	}
	
	o = openfd(fd);
	pfmt(o, "%t\n", t);
	closeio(o);
}

Russ Cox <rsc@swtch.com> writes

| 
| > i looked at modifying rc to write commands to a history file but it
| > didn't seem to fit very well. maybe a hook would be better as in
| 
| it's not hard.  perhaps you object to rc's reformatting of
| what you typed?
| 
| diff -c ./exec.c h/exec.c
| ./exec.c:9,14 - h/exec.c:9,16
|   #include "exec.h"
|   #include "io.h"
|   #include "fns.h"
| +
| + tree *line;
|   /*
|    * Start executing the given code at the given pc with the given redirection
|    */
| ./exec.c:112,118 - h/exec.c:114,120
|   {
|   	code bootstrap[32];
|   	char num[12], *rcmain;
| - 	int i;
| + 	int i, fd;
|   	
|   	/* needed for rcmain later */
|   	putenv("PLAN9", unsharp("#9"));
| ./exec.c:123,128 - h/exec.c:125,132
|   	if(flag['I']) flag['i'] = 0;
|   	else if(flag['i']==0 && argc==1 && Isatty(0)) flag['i'] = flagset;
|   	rcmain=flag['m']?flag['m'][0]:Rcmain();
| + 	if((fd = open("/tmp/history", OWRITE)) >= 0)
| + 		history=openfd(fd);
|   	err=openfd(2);
|   	kinit();
|   	Trapinit();
| ./exec.c:773,778 - h/exec.c:777,788
|   		}
|   	}
|   	else{
| + 		if(p->iflag && history && line){
| + 			char buf[30];
| + 			strcpy(buf, ctime(time(0)));
| + 			buf[28] = 0;
| + 			pfmt(history, "%s %t\n", buf, line);
| + 		}
|   		ntrap = 0;	/* avoid double-interrupts during blocked writes */
|   		--p->pc;	/* re-execute Xrdcmds after codebuf runs */
|   		start(codebuf, 1, runq->local);
| diff -c ./io.h h/io.h
| ./io.h:11,16 - h/io.h:11,17
|   	char *bufp, *ebuf, *strp, buf[NBUF];
|   };
|   io *err;
| + io *history;
|   io *openfd(int), *openstr(void), *opencore(char *, int);
|   int emptybuf(io*);
|   void pchr(io*, int);
| diff -c ./rc.h h/rc.h
| ./rc.h:143,145 - h/rc.h:143,146
|   int lastc;
|   int lastword;
|   int kidpid;
| + extern tree *line;
| diff -c ./syn.y h/syn.y
| ./syn.y:21,28 - h/syn.y:21,28
|   %type<tree> NOT FOR IN WHILE IF TWIDDLE BANG SUBSHELL SWITCH FN
|   %type<tree> WORD REDIR DUP PIPE
|   %%
| - rc:				{ return 1;}
| - |	line '\n'		{return !compile($1);}
| + rc:				{ line = nil; return 1;}
| + |	line '\n'		{ line = $1; return !compile($1);}
|   line:	cmd
|   |	cmdsa line		{$$=tree2(';', $1, $2);}
|   body:	cmd
sendmail 9fans@cse.psu.edu
From: erik quanstrom <quanstro@quanstro.net>
Reply-To: erik quanstrom <quanstro@speakeasy.net>
To: Fans of the OS Plan 9 from Bell Labs <9fans@cse.psu.edu>,
	Russ Cox <rsc@swtch.com>
References: <fc73b7d828b678708affd1677c1c022a@orthanc.cc.titech.ac.jp>
	<436AF189.2080909@lanl.gov>
	<20051104145056.A3EC1651A4@dexter-peak.quanstro.net>
	<ee9e417a0511040827i7f76d805y42199469f12b8c33@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
In-Reply-To: <ee9e417a0511040827i7f76d805y42199469f12b8c33@mail.gmail.com>
Subject: Re: [9fans] tab completion and command history in rc

nope, i didn't object to the reformatting. due to the snow here i was stuck
for a couple of hrs with an ancient copy of p9p and decided to have another go
at the problem because i wanted history tied to a variable. i came up with this:

- erik

btw: is there any way to simulate byron's ``(new-ifs-list) {cmd} with rc?
for example:
	fu=x
	fn fu {echo x}
	nl = '
'
	for(i in ``$nl {whatis fu}) echo $i

	fu=x
	fn fn {echo x}

; rcsdiff syn.y
===================================================================
RCS file: RCS/syn.y,v
retrieving revision 1.1
diff -r1.1 syn.y
25c25,28
< |	line '\n'		{return !compile($1);}
---
> |	line '\n'			{ int i = !compile($1); 
> 				if (!i) 
> 					whistory($1); 
> 				return i;}
; cat history.c
// feel free to roll your eyes.

#include "rc.h"
#include "exec.h"
#include "fns.h"
#include "io.h"

void
whistory(tree* t){
	var* v;
	char* s;
	int fd;
	io* o;
	int flags = OAPPEND|OWRITE;

	if (!runq->iflag || !t)
		return;

	v = vlook("history");
	if (!v->val || 1 != count(v->val))
		return;

	if (v->fn)
		return;	// future: fn history {echo $*>>$history_file}

	s = v->val->word;
	if((fd=open(s, flags))<0 && (fd=create(s, flags, 0666L))<0){
		//setvar("history", 0);
 		return;
	}
	
	o = openfd(fd);
	pfmt(o, "%t\n", t);
	closeio(o);
}

Russ Cox <rsc@swtch.com> writes

| 
| > i looked at modifying rc to write commands to a history file but it
| > didn't seem to fit very well. maybe a hook would be better as in
| 
| it's not hard.  perhaps you object to rc's reformatting of
| what you typed?
| 
| diff -c ./exec.c h/exec.c
| ./exec.c:9,14 - h/exec.c:9,16
|   #include "exec.h"
|   #include "io.h"
|   #include "fns.h"
| +
| + tree *line;
|   /*
|    * Start executing the given code at the given pc with the given redirection
|    */
| ./exec.c:112,118 - h/exec.c:114,120
|   {
|   	code bootstrap[32];
|   	char num[12], *rcmain;
| - 	int i;
| + 	int i, fd;
|   	
|   	/* needed for rcmain later */
|   	putenv("PLAN9", unsharp("#9"));
| ./exec.c:123,128 - h/exec.c:125,132
|   	if(flag['I']) flag['i'] = 0;
|   	else if(flag['i']==0 && argc==1 && Isatty(0)) flag['i'] = flagset;
|   	rcmain=flag['m']?flag['m'][0]:Rcmain();
| + 	if((fd = open("/tmp/history", OWRITE)) >= 0)
| + 		history=openfd(fd);
|   	err=openfd(2);
|   	kinit();
|   	Trapinit();
| ./exec.c:773,778 - h/exec.c:777,788
|   		}
|   	}
|   	else{
| + 		if(p->iflag && history && line){
| + 			char buf[30];
| + 			strcpy(buf, ctime(time(0)));
| + 			buf[28] = 0;
| + 			pfmt(history, "%s %t\n", buf, line);
| + 		}
|   		ntrap = 0;	/* avoid double-interrupts during blocked writes */
|   		--p->pc;	/* re-execute Xrdcmds after codebuf runs */
|   		start(codebuf, 1, runq->local);
| diff -c ./io.h h/io.h
| ./io.h:11,16 - h/io.h:11,17
|   	char *bufp, *ebuf, *strp, buf[NBUF];
|   };
|   io *err;
| + io *history;
|   io *openfd(int), *openstr(void), *opencore(char *, int);
|   int emptybuf(io*);
|   void pchr(io*, int);
| diff -c ./rc.h h/rc.h
| ./rc.h:143,145 - h/rc.h:143,146
|   int lastc;
|   int lastword;
|   int kidpid;
| + extern tree *line;
| diff -c ./syn.y h/syn.y
| ./syn.y:21,28 - h/syn.y:21,28
|   %type<tree> NOT FOR IN WHILE IF TWIDDLE BANG SUBSHELL SWITCH FN
|   %type<tree> WORD REDIR DUP PIPE
|   %%
| - rc:				{ return 1;}
| - |	line '\n'		{return !compile($1);}
| + rc:				{ line = nil; return 1;}
| + |	line '\n'		{ line = $1; return !compile($1);}
|   line:	cmd
|   |	cmdsa line		{$$=tree2(';', $1, $2);}
|   body:	cmd
sendmail 9fans@cse.psu.edu
From: erik quanstrom <quanstro@quanstro.net>
Reply-To: erik quanstrom <quanstro@speakeasy.net>
To: Fans of the OS Plan 9 from Bell Labs <9fans@cse.psu.edu>,
	Russ Cox <rsc@swtch.com>
References: <fc73b7d828b678708affd1677c1c022a@orthanc.cc.titech.ac.jp>
	<436AF189.2080909@lanl.gov>
	<20051104145056.A3EC1651A4@dexter-peak.quanstro.net>
	<ee9e417a0511040827i7f76d805y42199469f12b8c33@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
In-Reply-To: <ee9e417a0511040827i7f76d805y42199469f12b8c33@mail.gmail.com>
Subject: Re: [9fans] tab completion and command history in rc

nope, i didn't object to the reformatting. due to the snow here i was stuck
for a couple of hrs with an ancient copy of p9p and decided to have another go
at the problem because i wanted history tied to a variable. i came up with this:

- erik

btw: is there any way to simulate byron's ``(new-ifs-list) {cmd} with rc?
for example:
	fu=x
	fn fu {echo x}
	nl = '
'
	for(i in ``$nl {whatis fu}) echo $i

	fu=x
	fn fn {echo x}

; rcsdiff syn.y
===================================================================
RCS file: RCS/syn.y,v
retrieving revision 1.1
diff -r1.1 syn.y
25c25,28
< |	line '\n'		{return !compile($1);}
---
> |	line '\n'			{ int i = !compile($1); 
> 				if (!i) 
> 					whistory($1); 
> 				return i;}
; cat history.c
// feel free to roll your eyes.

#include "rc.h"
#include "exec.h"
#include "fns.h"
#include "io.h"

void
whistory(tree* t){
	var* v;
	char* s;
	int fd;
	io* o;
	int flags = OAPPEND|OWRITE;

	if (!runq->iflag || !t)
		return;

	v = vlook("history");
	if (!v->val || 1 != count(v->val))
		return;

	if (v->fn)
		return;	// future: fn history {echo $*>>$history_file}

	s = v->val->word;
	if((fd=open(s, flags))<0 && (fd=create(s, flags, 0666L))<0){
		//setvar("history", 0);
 		return;
	}
	
	o = openfd(fd);
	pfmt(o, "%t\n", t);
	closeio(o);
}

Russ Cox <rsc@swtch.com> writes

| 
| > i looked at modifying rc to write commands to a history file but it
| > didn't seem to fit very well. maybe a hook would be better as in
| 
| it's not hard.  perhaps you object to rc's reformatting of
| what you typed?
| 
| diff -c ./exec.c h/exec.c
| ./exec.c:9,14 - h/exec.c:9,16
|   #include "exec.h"
|   #include "io.h"
|   #include "fns.h"
| +
| + tree *line;
|   /*
|    * Start executing the given code at the given pc with the given redirection
|    */
| ./exec.c:112,118 - h/exec.c:114,120
|   {
|   	code bootstrap[32];
|   	char num[12], *rcmain;
| - 	int i;
| + 	int i, fd;
|   	
|   	/* needed for rcmain later */
|   	putenv("PLAN9", unsharp("#9"));
| ./exec.c:123,128 - h/exec.c:125,132
|   	if(flag['I']) flag['i'] = 0;
|   	else if(flag['i']==0 && argc==1 && Isatty(0)) flag['i'] = flagset;
|   	rcmain=flag['m']?flag['m'][0]:Rcmain();
| + 	if((fd = open("/tmp/history", OWRITE)) >= 0)
| + 		history=openfd(fd);
|   	err=openfd(2);
|   	kinit();
|   	Trapinit();
| ./exec.c:773,778 - h/exec.c:777,788
|   		}
|   	}
|   	else{
| + 		if(p->iflag && history && line){
| + 			char buf[30];
| + 			strcpy(buf, ctime(time(0)));
| + 			buf[28] = 0;
| + 			pfmt(history, "%s %t\n", buf, line);
| + 		}
|   		ntrap = 0;	/* avoid double-interrupts during blocked writes */
|   		--p->pc;	/* re-execute Xrdcmds after codebuf runs */
|   		start(codebuf, 1, runq->local);
| diff -c ./io.h h/io.h
| ./io.h:11,16 - h/io.h:11,17
|   	char *bufp, *ebuf, *strp, buf[NBUF];
|   };
|   io *err;
| + io *history;
|   io *openfd(int), *openstr(void), *opencore(char *, int);
|   int emptybuf(io*);
|   void pchr(io*, int);
| diff -c ./rc.h h/rc.h
| ./rc.h:143,145 - h/rc.h:143,146
|   int lastc;
|   int lastword;
|   int kidpid;
| + extern tree *line;
| diff -c ./syn.y h/syn.y
| ./syn.y:21,28 - h/syn.y:21,28
|   %type<tree> NOT FOR IN WHILE IF TWIDDLE BANG SUBSHELL SWITCH FN
|   %type<tree> WORD REDIR DUP PIPE
|   %%
| - rc:				{ return 1;}
| - |	line '\n'		{return !compile($1);}
| + rc:				{ line = nil; return 1;}
| + |	line '\n'		{ line = $1; return !compile($1);}
|   line:	cmd
|   |	cmdsa line		{$$=tree2(';', $1, $2);}
|   body:	cmd


  reply	other threads:[~2005-12-09  1:58 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-11-04  4:27 YAMANASHI Takeshi
2005-11-04  5:28 ` Ronald G Minnich
2005-11-04  4:41   ` Matthew J. Sottile
2005-11-04  5:26   ` Jack Johnson
2005-11-04  8:50   ` lucio
2005-11-04 15:06   ` erik quanstrom
2005-11-04 15:24     ` Axel Belinfante
2005-11-04 15:29       ` erik quanstrom
2005-11-04 15:26     ` Charles Forsyth
2005-11-04 15:54       ` erik quanstrom
2005-11-05  9:20         ` Charles Forsyth
2005-11-04 16:27     ` Russ Cox
2005-12-09  1:58       ` erik quanstrom [this message]
2005-11-05  8:54     ` Scott Schwartz
2005-11-05 12:13       ` erik quanstrom
2005-11-05 16:01       ` Ronald G Minnich
     [not found] <000201c5e0e5$bbd617f0$14aaa8c0@utelsystems.local>
2005-11-04  6:46 ` Nils O. Selåsdal
  -- strict thread matches above, loose matches on Subject: below --
2005-11-04  4:01 YAMANASHI Takeshi
2005-11-04  3:42 YAMANASHI Takeshi
2005-11-04  3:45 ` Russ Cox
2005-11-04  2:30 YAMANASHI Takeshi
2005-11-04  3:08 ` Russ Cox
2005-11-04  2:01 Federico G. Benavento
2005-11-04  1:29 Federico G. Benavento
2005-11-04  0:59 Rian Hunter
2005-11-04  1:06 ` Uriel
2005-11-04  1:07 ` Russ Cox
2005-11-04  1:14 ` John Floren
2005-11-04  1:22   ` Russ Cox
2005-11-04  1:22   ` Uriel
2005-11-04  1:27     ` Uriel
2005-11-04  2:02       ` Russ Cox
2005-11-04  1:56     ` Rian Hunter
2005-11-04  2:08     ` Skip Tavakkolian
2005-11-04  9:20       ` William Staniewicz
     [not found]     ` <000001c5e0e1$76791f80$14aaa8c0@utelsystems.local>
2005-11-04  6:58       ` Nils O. Selåsdal
2005-11-04  1:23   ` Paul Lalonde
2005-11-04  9:02   ` Scott Schwartz
2005-11-04 15:13   ` "Nils O. Selåsdal"

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=20051209015841.01BA6590D@rawlings.quanstro.net \
    --to=quanstro@quanstro.net \
    --cc=9fans@cse.psu.edu \
    --cc=rsc@swtch.com \
    /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).