tin with the new - plan9port - [fork] Plan 9 from user space
 (HTM) git clone git://src.adamsgaard.dk/plan9port
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit b2ad2ef1387571c7b917a7fd63e8670582ae8b7f
 (DIR) parent 48bfee4e5b72db021da3538c97ef68ce2308f12b
 (HTM) Author: rsc <devnull@localhost>
       Date:   Tue,  4 Jan 2005 22:30:59 +0000
       
       in with the new
       
       Diffstat:
         A src/cmd/acmeevent.c                 |      93 +++++++++++++++++++++++++++++++
         D src/cmd/aescbc.c                    |     144 -------------------------------
         A src/cmd/delatex.lx                  |      64 +++++++++++++++++++++++++++++++
         A src/cmd/dial.c                      |      62 +++++++++++++++++++++++++++++++
         A src/cmd/read.c                      |      91 +++++++++++++++++++++++++++++++
       
       5 files changed, 310 insertions(+), 144 deletions(-)
       ---
 (DIR) diff --git a/src/cmd/acmeevent.c b/src/cmd/acmeevent.c
       t@@ -0,0 +1,93 @@
       +#include <u.h>
       +#include <libc.h>
       +#include <bio.h>
       +
       +
       +int
       +getn(Biobuf *b)
       +{
       +        int c, n;
       +
       +        n = 0;
       +        while((c = Bgetc(b)) != -1 && '0'<=c && c<='9')
       +                n = n*10+c-'0';
       +        if(c != ' ')
       +                sysfatal("bad number syntax");
       +        return n;
       +}
       +
       +char*
       +getrune(Biobuf *b, char *p)
       +{
       +        int c;
       +        char *q;
       +
       +        c = Bgetc(b);
       +        if(c == -1)
       +                sysfatal("eof");
       +        q = p;
       +        *q++ = c;
       +        if(c >= Runeself){
       +                while(!fullrune(p, q-p)){
       +                        c = Bgetc(b);
       +                        if(c == -1)
       +                                sysfatal("eof");
       +                        *q++ = c;
       +                }
       +        }
       +        return q;
       +}
       +
       +void
       +getevent(Biobuf *b, int *c1, int *c2, int *q0, int *q1, int *flag, int *nr, char *buf)
       +{
       +        int i;
       +        char *p;
       +
       +        *c1 = Bgetc(b);
       +        if(*c1 == -1)
       +                exits(0);
       +        *c2 = Bgetc(b);
       +        *q0 = getn(b);
       +        *q1 = getn(b);
       +        *flag = getn(b);
       +        *nr = getn(b);
       +        if(*nr >= 256)
       +                sysfatal("event string too long");
       +        p = buf;
       +        for(i=0; i<*nr; i++)
       +                p = getrune(b, p);
       +        *p = 0;
       +        if(Bgetc(b) != '\n')
       +                sysfatal("expected newline");
       +}
       +
       +void
       +main(void)
       +{
       +        int c1, c2, q0, q1, eq0, eq1, flag, nr, x;
       +        Biobuf b;
       +        char buf[2000], buf2[2000], buf3[2000];
       +
       +        doquote = needsrcquote;
       +        quotefmtinstall();
       +        Binit(&b, 0, OREAD);
       +        for(;;){
       +                getevent(&b, &c1, &c2, &q0, &q1, &flag, &nr, buf);
       +                eq0 = q0;
       +                eq1 = q1;
       +                buf2[0] = 0;
       +                buf3[0] = 0;
       +                if(flag & 2){
       +                        /* null string with non-null expansion */
       +                        getevent(&b, &x, &x, &eq0, &eq1, &x, &nr, buf);
       +                }
       +                if(flag & 8){
       +                        /* chorded argument */
       +                        getevent(&b, &x, &x, &x, &x, &x, &x, buf2);
       +                        getevent(&b, &x, &x, &x, &x, &x, &x, buf3);
       +                }
       +                print("event %c %c %d %d %d %d %d %d %q %q %q\n",
       +                        c1, c2, q0, q1, eq0, eq1, flag, nr, buf, buf2, buf3);
       +        }
       +}
 (DIR) diff --git a/src/cmd/aescbc.c b/src/cmd/aescbc.c
       t@@ -1,144 +0,0 @@
       -/* encrypt file by writing
       -        v2hdr,
       -        16byte initialization vector,
       -        AES-CBC(key, random | file),
       -    HMAC_SHA1(md5(key), AES-CBC(random | file))
       -
       -With CBC, if the first plaintext block is 0, the first ciphertext block is
       -E(IV).  Using the overflow technique adopted for compatibility with cryptolib
       -makes the last cipertext block decryptable.  Hence the random prefix to file.
       -*/
       -#include <u.h>
       -#include <libc.h>
       -#include <bio.h>
       -#include <mp.h>
       -#include <libsec.h>
       -
       -enum{ CHK = 16, BUF = 4096 };
       -
       -uchar v2hdr[AESbsize+1] = "AES CBC SHA1  2\n";
       -Biobuf bin;
       -Biobuf bout;
       -
       -void
       -safewrite(uchar *buf, int n)
       -{
       -        int i = Bwrite(&bout, buf, n);
       -
       -        if(i == n)
       -                return;
       -        fprint(2, "write error\n");
       -        exits("write error");
       -}
       -
       -void
       -saferead(uchar *buf, int n)
       -{
       -        int i = Bread(&bin, buf, n);
       -
       -        if(i == n)
       -                return;
       -        fprint(2, "read error\n");
       -        exits("read error");
       -}
       -
       -int
       -main(int argc, char **argv)
       -{
       -        int encrypt = 0;  /* 0=decrypt, 1=encrypt */
       -        int n, nkey;
       -        char *hex, *msg = nil;
       -        uchar key[AESmaxkey], key2[MD5dlen];
       -        uchar buf[BUF+SHA1dlen];    /* assumption: CHK <= SHA1dlen */
       -        AESstate aes;
       -        DigestState *dstate;
       -
       -        if(argc!=2 || argv[1][0]!='-'){
       -                fprint(2,"usage: HEX=key %s -d < cipher.aes > clear.txt\n", argv[0]);
       -                fprint(2,"   or: HEX=key %s -e < clear.txt > cipher.aes\n", argv[0]);
       -                exits("usage");
       -        }
       -        if(argv[1][1] == 'e')
       -                encrypt = 1;
       -        Binit(&bin, 0, OREAD);
       -        Binit(&bout, 1, OWRITE);
       -
       -        if((hex = getenv("HEX")) == nil)
       -                hex = getpass("enter key: ");
       -        nkey = 0;
       -        if(hex != nil)
       -                nkey = strlen(hex);
       -        if(nkey == 0 || (nkey&1) || nkey>2*AESmaxkey){
       -                fprint(2,"key should be 32 hex digits\n");
       -                exits("key");
       -        }
       -        nkey = dec16(key, sizeof key, hex, nkey);
       -        md5(key, nkey, key2, 0);  /* so even if HMAC_SHA1 is broken, encryption key is protected */
       -
       -        if(encrypt){
       -                safewrite(v2hdr, AESbsize);
       -                genrandom(buf,2*AESbsize); /* CBC is semantically secure if IV is unpredictable. */
       -                setupAESstate(&aes, key, nkey, buf);  /* use first AESbsize bytes as IV */
       -                aesCBCencrypt(buf+AESbsize, AESbsize, &aes);  /* use second AESbsize bytes as initial plaintext */
       -                safewrite(buf, 2*AESbsize);
       -                dstate = hmac_sha1(buf+AESbsize, AESbsize, key2, MD5dlen, 0, 0);
       -                while(1){
       -                        n = Bread(&bin, buf, BUF);
       -                        if(n < 0){
       -                                msg = "read error";
       -                                goto Exit;
       -                        }
       -                        aesCBCencrypt(buf, n, &aes);
       -                        safewrite(buf, n);
       -                        dstate = hmac_sha1(buf, n, key2, MD5dlen, 0, dstate);
       -                        if(n < BUF)
       -                                break; /* EOF */
       -                }
       -                hmac_sha1(0, 0, key2, MD5dlen, buf, dstate);
       -                safewrite(buf, SHA1dlen);
       -        }else{ /* decrypt */
       -                Bread(&bin, buf, AESbsize);
       -                if(memcmp(buf, v2hdr, AESbsize) == 0){
       -                        saferead(buf, 2*AESbsize);  /* read IV and random initial plaintext */
       -                        setupAESstate(&aes, key, nkey, buf);
       -                        dstate = hmac_sha1(buf+AESbsize, AESbsize, key2, MD5dlen, 0, 0);
       -                        aesCBCdecrypt(buf+AESbsize, AESbsize, &aes);
       -                        saferead(buf, SHA1dlen);
       -                        while((n = Bread(&bin, buf+SHA1dlen, BUF)) > 0){
       -                                dstate = hmac_sha1(buf, n, key2, MD5dlen, 0, dstate);
       -                                aesCBCdecrypt(buf, n, &aes);
       -                                safewrite(buf, n);
       -                                memmove(buf, buf+n, SHA1dlen);  /* these bytes are not yet decrypted */
       -                        }
       -                        hmac_sha1(0, 0, key2, MD5dlen, buf+SHA1dlen, dstate);
       -                        if(memcmp(buf, buf+SHA1dlen, SHA1dlen) != 0){
       -                                msg = "decrypted file failed to authenticate!";
       -                                goto Exit;
       -                        }
       -                }else{ /* compatibility with past mistake */
       -                        // if file was encrypted with bad aescbc use this:
       -                        //         memset(key, 0, AESmaxkey);
       -                        //    else assume we're decrypting secstore files
       -                        setupAESstate(&aes, key, 0, buf);
       -                        saferead(buf, CHK);
       -                        aesCBCdecrypt(buf, CHK, &aes);
       -                        while((n = Bread(&bin, buf+CHK, BUF)) > 0){
       -                                aesCBCdecrypt(buf+CHK, n, &aes);
       -                                safewrite(buf, n);
       -                                memmove(buf, buf+n, CHK);
       -                        }
       -                        if(memcmp(buf, "XXXXXXXXXXXXXXXX", CHK) != 0){
       -                                msg = "decrypted file failed to authenticate";
       -                                goto Exit;
       -                        }
       -                }
       -        }
       - Exit:
       -        memset(key, 0, sizeof(key));
       -        memset(key2, 0, sizeof(key2));
       -        memset(buf, 0, sizeof(buf));
       -        if(msg != nil)
       -                fprint(2, "%s\n", msg);
       -        exits(msg);
       -        return 1;        /* gcc */
       -}
 (DIR) diff --git a/src/cmd/delatex.lx b/src/cmd/delatex.lx
       t@@ -0,0 +1,64 @@
       +/* Make this with:  lex delatex.lex;  cc lex.yy.c -ll -o delatex */
       +L [A-Za-z]
       +%Start Display Math Normal Tag
       +%%
       +<Normal>\'        {yyleng--; yymore(); /* ignore apostrophes */}
       +<Normal>{L}+\\- {yyleng-=2; yymore(); /* ignore hyphens */}
       +<Normal>[a-z]/[^A-Za-z] ; /* ignore single letter "words" */
       +<Normal>[A-Z]+        ; /* ignore words all in uppercase */
       +<Normal>{L}+('{L}*)*{L}        {printf("%s\n",yytext); /* any other letter seq is a word */} 
       +<Normal>"%".*        ; /* ignore comments */
       +<Normal>\\{L}+        ; /* ignore other control sequences */
       +<Normal>"\\begin{"        BEGIN Tag; /* ignore this and up to next "}" */
       +<Normal>"\\bibitem{"        BEGIN Tag;
       +<Normal>"\\bibliography{"        BEGIN Tag;
       +<Normal>"\\bibstyle{"        BEGIN Tag;
       +<Normal>"\\cite{"        BEGIN Tag;
       +<Normal>"\\end{"                BEGIN Tag;
       +<Normal>"\\include{"        BEGIN Tag;
       +<Normal>"\\includeonly{"        BEGIN Tag;
       +<Normal>"\\input{"        BEGIN Tag;
       +<Normal>"\\label{"        BEGIN Tag;
       +<Normal>"\\pageref{"        BEGIN Tag;
       +<Normal>"\\ref{"                BEGIN Tag;
       +<Tag>[^}]                ; /* ignore things up to next "}" */
       +<Tag>"}"                BEGIN Normal;
       +<Normal>[0-9]+        ; /* ignore numbers */
       +<Normal>"\\("                BEGIN Math; /* begin latex math mode */
       +<Math>"\\)"                BEGIN Normal; /* end latex math mode */
       +<Math>.|\\[^)]|\n        ; /* ignore anything else in latex math mode */
       +<Normal>"\\["                BEGIN Display; /* now in Latex display mode */
       +<Display>[^$]|\\[^\]]        ; /* ignore most things in display math mode */
       +<Display>"\\]"        BEGIN Normal; /* get out of Display math mode */
       +<Normal>\\.        ; /* ignore other single character control sequences */
       +<Normal>\\\n        ; /* more of the same */
       +<Normal>\n|.        ; /* ignore anything else, a character at a time */
       +%%
       +#include <stdio.h>
       +#include <stdlib.h>
       +
       +int
       +main(int argc, char **argv)
       +{
       +        int i;
       +        
       +        BEGIN Normal; /* Starts yylex off in the right state */
       +        if (argc==1) {
       +            yyin = stdin;
       +            yylex();
       +            }
       +        else for (i=1; i<argc; i++) {
       +            yyin = fopen(argv[i],"r");
       +            if (yyin==NULL) {
       +                fprintf(stderr,"can't open %s\n",argv[i]);
       +                exit(1);
       +                }
       +            yylex();
       +            }
       +        exit(0);
       +}
       +int
       +yywrap(void)
       +{
       +        return 1;
       +}
 (DIR) diff --git a/src/cmd/dial.c b/src/cmd/dial.c
       t@@ -0,0 +1,62 @@
       +#include <u.h>
       +#include <libc.h>
       +
       +void
       +usage(void)
       +{
       +        fprint(2, "usage: dial [-e] addr\n");
       +        exits("usage");
       +}
       +
       +void
       +killer(void *x, char *msg)
       +{
       +        USED(x);
       +        if(strcmp(msg, "kill") == 0)
       +                exits(0);
       +        noted(NDFLT);
       +}
       +
       +void
       +main(int argc, char **argv)
       +{
       +        int fd, pid;
       +        char buf[8192];
       +        int n, waitforeof;
       +
       +        notify(killer);
       +        waitforeof = 0;
       +        ARGBEGIN{
       +        case 'e':
       +                waitforeof = 1;
       +                break;
       +        default:
       +                usage();
       +        }ARGEND
       +
       +        if(argc != 1)
       +                usage();
       +
       +        if((fd = dial(argv[0], nil, nil, nil)) < 0)
       +                sysfatal("dial: %r");
       +
       +        switch(pid = fork()){
       +        case -1:
       +                sysfatal("fork: %r");
       +        case 0:
       +                while((n = read(0, buf, sizeof buf)) > 0)
       +                        if(write(0, buf, n) < 0)
       +                                break;
       +                if(!waitforeof)
       +                        postnote(PNPROC, getppid(), "kill");
       +                exits(nil);
       +        }
       +
       +        while((n = read(fd, buf, sizeof buf)) > 0)
       +                if(write(1, buf, n) < 0)
       +                        break;
       +
       +        postnote(PNPROC, pid, "kill");
       +        waitpid();
       +        exits(0);
       +}
 (DIR) diff --git a/src/cmd/read.c b/src/cmd/read.c
       t@@ -0,0 +1,91 @@
       +#include <u.h>
       +#include <libc.h>
       +
       +int        multi;
       +int        nlines;
       +char        *status = nil;
       +
       +int
       +line(int fd, char *file)
       +{
       +        char c;
       +        int m, n, nalloc;
       +        char *buf;
       +
       +        nalloc = 0;
       +        buf = nil;
       +        for(m=0; ; ){
       +                n = read(fd, &c, 1);
       +                if(n < 0){
       +                        fprint(2, "read: error reading %s: %r\n", file);
       +                        exits("read error");
       +                }
       +                if(n == 0){
       +                        if(m == 0)
       +                                status = "eof";
       +                        break;
       +                }
       +                if(m == nalloc){
       +                        nalloc += 1024;
       +                        buf = realloc(buf, nalloc);
       +                        if(buf == nil){
       +                                fprint(2, "read: malloc error: %r\n");
       +                                exits("malloc");
       +                        }
       +                }
       +                buf[m++] = c;
       +                if(c == '\n')
       +                        break;
       +        }
       +        if(m > 0)
       +                write(1, buf, m);
       +        free(buf);
       +        return m;
       +}
       +
       +void
       +lines(int fd, char *file)
       +{
       +        do{
       +                if(line(fd, file) == 0)
       +                        break;
       +        }while(multi || --nlines>0);
       +}
       +
       +void
       +main(int argc, char *argv[])
       +{
       +        int i, fd;
       +        char *s;
       +
       +        ARGBEGIN{
       +        case 'm':
       +                multi = 1;
       +                break;
       +        case 'n':
       +                s = ARGF();
       +                if(s){
       +                        nlines = atoi(s);
       +                        break;
       +                }
       +                /* fall through */
       +        default:
       +                fprint(2, "usage: read [-m] [-n nlines] [files...]\n");
       +                exits("usage");
       +        }ARGEND
       +
       +        if(argc == 0)
       +                lines(0, "<stdin>");
       +        else
       +                for(i=0; i<argc; i++){
       +                        fd = open(argv[i], OREAD);
       +                        if(fd < 0){
       +                                fprint(2, "read: can't open %s: %r\n", argv[i]);
       +                                exits("open");
       +                        }
       +                        lines(fd, argv[i]);
       +                        close(fd);
       +                }
       +
       +        exits(status);
       +}