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 <[EMAIL PROTECTED]> 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 [email protected]
From: erik quanstrom <[EMAIL PROTECTED]>
Reply-To: erik quanstrom <[EMAIL PROTECTED]>
To: Fans of the OS Plan 9 from Bell Labs <[email protected]>,
        Russ Cox <[EMAIL PROTECTED]>
References: <[EMAIL PROTECTED]>
        <[EMAIL PROTECTED]>
        <[EMAIL PROTECTED]>
        <[EMAIL PROTECTED]>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
In-Reply-To: <[EMAIL PROTECTED]>
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 <[EMAIL PROTECTED]> 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 [email protected]
From: erik quanstrom <[EMAIL PROTECTED]>
Reply-To: erik quanstrom <[EMAIL PROTECTED]>
To: Fans of the OS Plan 9 from Bell Labs <[email protected]>,
        Russ Cox <[EMAIL PROTECTED]>
References: <[EMAIL PROTECTED]>
        <[EMAIL PROTECTED]>
        <[EMAIL PROTECTED]>
        <[EMAIL PROTECTED]>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
In-Reply-To: <[EMAIL PROTECTED]>
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 <[EMAIL PROTECTED]> 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 via email to