From 7f6458b045e04b97dd06b3171ac67e9ecde32429 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Tue, 5 Jan 2021 23:35:33 -0500 Subject: stats: add threadmaybackground --- src/cmd/sam/cmd.c | 70 ++++++++++++++++++++++++++--------------------------- src/cmd/sam/parse.h | 6 +++-- src/cmd/sam/sam.h | 2 -- 3 files changed, 39 insertions(+), 39 deletions(-) (limited to 'src/cmd/sam') diff --git a/src/cmd/sam/cmd.c b/src/cmd/sam/cmd.c index 386fe8d4..7176a827 100644 --- a/src/cmd/sam/cmd.c +++ b/src/cmd/sam/cmd.c @@ -3,41 +3,41 @@ static char linex[]="\n"; static char wordx[]=" \t\n"; -struct cmdtab cmdtab[]={ +struct Cmdtab cmdtab[]={ /* cmdc text regexp addr defcmd defaddr count token fn */ - '\n', 0, 0, 0, 0, aDot, 0, 0, nl_cmd, - 'a', 1, 0, 0, 0, aDot, 0, 0, a_cmd, - 'b', 0, 0, 0, 0, aNo, 0, linex, b_cmd, - 'B', 0, 0, 0, 0, aNo, 0, linex, b_cmd, - 'c', 1, 0, 0, 0, aDot, 0, 0, c_cmd, - 'd', 0, 0, 0, 0, aDot, 0, 0, d_cmd, - 'D', 0, 0, 0, 0, aNo, 0, linex, D_cmd, - 'e', 0, 0, 0, 0, aNo, 0, wordx, e_cmd, - 'f', 0, 0, 0, 0, aNo, 0, wordx, f_cmd, - 'g', 0, 1, 0, 'p', aDot, 0, 0, g_cmd, - 'i', 1, 0, 0, 0, aDot, 0, 0, i_cmd, - 'k', 0, 0, 0, 0, aDot, 0, 0, k_cmd, - 'm', 0, 0, 1, 0, aDot, 0, 0, m_cmd, - 'n', 0, 0, 0, 0, aNo, 0, 0, n_cmd, - 'p', 0, 0, 0, 0, aDot, 0, 0, p_cmd, - 'q', 0, 0, 0, 0, aNo, 0, 0, q_cmd, - 'r', 0, 0, 0, 0, aDot, 0, wordx, e_cmd, - 's', 0, 1, 0, 0, aDot, 1, 0, s_cmd, - 't', 0, 0, 1, 0, aDot, 0, 0, m_cmd, - 'u', 0, 0, 0, 0, aNo, 2, 0, u_cmd, - 'v', 0, 1, 0, 'p', aDot, 0, 0, g_cmd, - 'w', 0, 0, 0, 0, aAll, 0, wordx, w_cmd, - 'x', 0, 1, 0, 'p', aDot, 0, 0, x_cmd, - 'y', 0, 1, 0, 'p', aDot, 0, 0, x_cmd, - 'X', 0, 1, 0, 'f', aNo, 0, 0, X_cmd, - 'Y', 0, 1, 0, 'f', aNo, 0, 0, X_cmd, - '!', 0, 0, 0, 0, aNo, 0, linex, plan9_cmd, - '>', 0, 0, 0, 0, aDot, 0, linex, plan9_cmd, - '<', 0, 0, 0, 0, aDot, 0, linex, plan9_cmd, - '|', 0, 0, 0, 0, aDot, 0, linex, plan9_cmd, - '=', 0, 0, 0, 0, aDot, 0, linex, eq_cmd, - 'c'|0x100,0, 0, 0, 0, aNo, 0, wordx, cd_cmd, - 0, 0, 0, 0, 0, 0, 0, 0 + {'\n', 0, 0, 0, 0, aDot, 0, 0, nl_cmd}, + {'a', 1, 0, 0, 0, aDot, 0, 0, a_cmd}, + {'b', 0, 0, 0, 0, aNo, 0, linex, b_cmd}, + {'B', 0, 0, 0, 0, aNo, 0, linex, b_cmd}, + {'c', 1, 0, 0, 0, aDot, 0, 0, c_cmd}, + {'d', 0, 0, 0, 0, aDot, 0, 0, d_cmd}, + {'D', 0, 0, 0, 0, aNo, 0, linex, D_cmd}, + {'e', 0, 0, 0, 0, aNo, 0, wordx, e_cmd}, + {'f', 0, 0, 0, 0, aNo, 0, wordx, f_cmd}, + {'g', 0, 1, 0, 'p', aDot, 0, 0, g_cmd}, + {'i', 1, 0, 0, 0, aDot, 0, 0, i_cmd}, + {'k', 0, 0, 0, 0, aDot, 0, 0, k_cmd}, + {'m', 0, 0, 1, 0, aDot, 0, 0, m_cmd}, + {'n', 0, 0, 0, 0, aNo, 0, 0, n_cmd}, + {'p', 0, 0, 0, 0, aDot, 0, 0, p_cmd}, + {'q', 0, 0, 0, 0, aNo, 0, 0, q_cmd}, + {'r', 0, 0, 0, 0, aDot, 0, wordx, e_cmd}, + {'s', 0, 1, 0, 0, aDot, 1, 0, s_cmd}, + {'t', 0, 0, 1, 0, aDot, 0, 0, m_cmd}, + {'u', 0, 0, 0, 0, aNo, 2, 0, u_cmd}, + {'v', 0, 1, 0, 'p', aDot, 0, 0, g_cmd}, + {'w', 0, 0, 0, 0, aAll, 0, wordx, w_cmd}, + {'x', 0, 1, 0, 'p', aDot, 0, 0, x_cmd}, + {'y', 0, 1, 0, 'p', aDot, 0, 0, x_cmd}, + {'X', 0, 1, 0, 'f', aNo, 0, 0, X_cmd}, + {'Y', 0, 1, 0, 'f', aNo, 0, 0, X_cmd}, + {'!', 0, 0, 0, 0, aNo, 0, linex, plan9_cmd}, + {'>', 0, 0, 0, 0, aDot, 0, linex, plan9_cmd}, + {'<', 0, 0, 0, 0, aDot, 0, linex, plan9_cmd}, + {'|', 0, 0, 0, 0, aDot, 0, linex, plan9_cmd}, + {'=', 0, 0, 0, 0, aDot, 0, linex, eq_cmd}, + {'c'|0x100,0, 0, 0, 0, aNo, 0, wordx, cd_cmd}, + {0, 0, 0, 0, 0, 0, 0, 0}, }; Cmd *parsecmd(int); Addr *compoundaddr(void); @@ -402,7 +402,7 @@ Cmd * parsecmd(int nest) { int i, c; - struct cmdtab *ct; + Cmdtab *ct; Cmd *cp, *ncp; Cmd cmd; diff --git a/src/cmd/sam/parse.h b/src/cmd/sam/parse.h index d5fabf14..dd837a48 100644 --- a/src/cmd/sam/parse.h +++ b/src/cmd/sam/parse.h @@ -33,7 +33,8 @@ struct Cmd #define ctext g.text #define caddr g.addr -extern struct cmdtab{ +typedef struct Cmdtab Cmdtab; +struct Cmdtab { ushort cmdc; /* command character */ uchar text; /* takes a textual argument? */ uchar regexp; /* takes a regular expression? */ @@ -43,7 +44,8 @@ extern struct cmdtab{ uchar count; /* takes a count e.g. s2/// */ char *token; /* takes text terminated by one of these */ int (*fn)(File*, Cmd*); /* function to call with parse tree */ -}cmdtab[]; +}; +extern Cmdtab cmdtab[]; enum Defaddr{ /* default addresses */ aNo, diff --git a/src/cmd/sam/sam.h b/src/cmd/sam/sam.h index aae39b4a..6e018156 100644 --- a/src/cmd/sam/sam.h +++ b/src/cmd/sam/sam.h @@ -37,7 +37,6 @@ typedef struct Address Address; typedef struct Block Block; typedef struct Buffer Buffer; typedef struct Disk Disk; -typedef struct Discdesc Discdesc; typedef struct File File; typedef struct List List; typedef struct Range Range; @@ -342,7 +341,6 @@ void warn_S(Warn, String*); int whichmenu(File*); void writef(File*); Posn writeio(File*); -Discdesc *Dstart(void); extern Rune samname[]; /* compiler dependent */ extern Rune *left[]; -- cgit v1.2.3 From 0a513e65607223d11ba94003256b13ef5779e7e8 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Sun, 3 Jan 2021 00:54:20 -0500 Subject: sam: rm dregs --- src/cmd/sam/README | 29 -------- src/cmd/sam/_libc.h | 40 ------------ src/cmd/sam/err | 39 ----------- src/cmd/sam/plan9.c | 185 ---------------------------------------------------- 4 files changed, 293 deletions(-) delete mode 100644 src/cmd/sam/README delete mode 100644 src/cmd/sam/_libc.h delete mode 100644 src/cmd/sam/err delete mode 100644 src/cmd/sam/plan9.c (limited to 'src/cmd/sam') diff --git a/src/cmd/sam/README b/src/cmd/sam/README deleted file mode 100644 index b78a89da..00000000 --- a/src/cmd/sam/README +++ /dev/null @@ -1,29 +0,0 @@ -This is sam (not including samterm) from the 4th edition of Plan 9, -with changes so that it can be compiled under unix. -(Tested on Solaris 7 and Debian 3.0r1.) - -Some extra libraries are needed. First, fetch libutf-2.0 and libfmt-2.0 -from - http://pdos.lcs.mit.edu/~rsc/software/ - -(Beware that in libfmt/fmt.c there is a line that says: - 'u', __ifmt, /* in Plan 9, __flagfmt */ -Thus, sam will have to fmtinstall the other thing. Other ported programs -may have to do the same. The fmt library should probably print messages -about bad format characters to stderr, since no one seems to check the -return codes.) - -Compile and install those two libraries. -Set PREFIX in the Makefile to match, then compile sam. - -Your C compiler will emit many complaints of the form: - sam.c:496: warning: passing arg 1 of `bufread' from incompatible pointer type - -This is because the Plan 9 compiler has a slightly different (better, -ala Oberon) type system than ISO C. Popular compilers generate the right -code, so in an act of civil disobediance I changed just enough to get -it to compile, but left the type errors in. Now the next C standard can -adopt this extension, because at least one important C program uses it! - --- Scott Schwartz, 4 July 2003 - diff --git a/src/cmd/sam/_libc.h b/src/cmd/sam/_libc.h deleted file mode 100644 index 65618918..00000000 --- a/src/cmd/sam/_libc.h +++ /dev/null @@ -1,40 +0,0 @@ -#define __USE_UNIX98 // for pread/pwrite, supposedly -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "utf.h" -#include "fmt.h" - -#define nil 0 -#define dup dup2 -#define exec execv -#define seek lseek -#define getwd getcwd -#define USED(a) -#define SET(a) - -enum { - OREAD = 0, - OWRITE = 1, - ORDWR = 2, - OCEXEC = 4, - ORCLOSE = 8 -}; - -enum { - ERRMAX = 255 -}; - -void exits(const char *); -void _exits(const char *); -int notify (void(*f)(void *, char *)); -int create(char *, int, int); -int errstr(char *, int); diff --git a/src/cmd/sam/err b/src/cmd/sam/err deleted file mode 100644 index 2a36c23b..00000000 --- a/src/cmd/sam/err +++ /dev/null @@ -1,39 +0,0 @@ -address.c: In function `filematch': -address.c:159: warning: passing arg 1 of `bufreset' from incompatible pointer type -address.c:160: warning: passing arg 1 of `bufinsert' from incompatible pointer type -file.c: In function `mergeextend': -file.c:117: warning: passing arg 1 of `bufread' from incompatible pointer type -file.c: In function `fileinsert': -file.c:275: warning: passing arg 1 of `bufinsert' from incompatible pointer type -file.c: In function `filedelete': -file.c:301: warning: passing arg 1 of `bufdelete' from incompatible pointer type -file.c: In function `fileundelete': -file.c:324: warning: passing arg 1 of `bufread' from incompatible pointer type -file.c: In function `filereadc': -file.c:339: warning: passing arg 1 of `bufread' from incompatible pointer type -file.c: In function `fileload': -file.c:405: warning: passing arg 1 of `bufload' from incompatible pointer type -file.c: In function `fileundo': -file.c:528: warning: passing arg 1 of `bufdelete' from incompatible pointer type -file.c:546: warning: passing arg 1 of `bufinsert' from incompatible pointer type -file.c: In function `fileclose': -file.c:604: warning: passing arg 1 of `bufclose' from incompatible pointer type -io.c: In function `readio': -io.c:90: warning: passing arg 1 of `bufload' from incompatible pointer type -io.c: In function `writeio': -io.c:152: warning: passing arg 1 of `bufread' from incompatible pointer type -mesg.c: In function `inmesg': -mesg.c:248: warning: passing arg 1 of `bufread' from incompatible pointer type -mesg.c: In function `snarf': -mesg.c:568: warning: passing arg 1 of `bufread' from incompatible pointer type -mesg.c: In function `setgenstr': -mesg.c:612: warning: passing arg 1 of `bufread' from incompatible pointer type -sam.c: In function `readcmd': -sam.c:496: warning: passing arg 1 of `bufread' from incompatible pointer type -sam.c: In function `copy': -sam.c:676: warning: passing arg 1 of `bufread' from incompatible pointer type -xec.c: In function `s_cmd': -xec.c:234: warning: passing arg 1 of `bufread' from incompatible pointer type -xec.c:243: warning: passing arg 1 of `bufread' from incompatible pointer type -xec.c: In function `display': -xec.c:401: warning: passing arg 1 of `bufread' from incompatible pointer type diff --git a/src/cmd/sam/plan9.c b/src/cmd/sam/plan9.c deleted file mode 100644 index 0a3fe070..00000000 --- a/src/cmd/sam/plan9.c +++ /dev/null @@ -1,185 +0,0 @@ -#include "sam.h" - -Rune samname[] = L"~~sam~~"; - -Rune *left[]= { - L"{[(<«", - L"\n", - L"'\"`", - 0 -}; -Rune *right[]= { - L"}])>»", - L"\n", - L"'\"`", - 0 -}; - -char RSAM[] = "sam"; -char SAMTERM[] = "/bin/aux/samterm"; -char HOME[] = "HOME"; -char TMPDIR[] = "/tmp"; -char SH[] = "rc"; -char SHPATH[] = "/bin/rc"; -char RX[] = "rx"; -char RXPATH[] = "/bin/rx"; -char SAMSAVECMD[] = "/bin/rc\n/sys/lib/samsave"; - -void -dprint(char *z, ...) -{ - char buf[BLOCKSIZE]; - va_list arg; - - va_start(arg, z); - vseprint(buf, &buf[BLOCKSIZE], z, arg); - va_end(arg); - termwrite(buf); -} - -void -print_ss(char *s, String *a, String *b) -{ - dprint("?warning: %s: `%.*S' and `%.*S'\n", s, a->n, a->s, b->n, b->s); -} - -void -print_s(char *s, String *a) -{ - dprint("?warning: %s `%.*S'\n", s, a->n, a->s); -} - -char* -getuser(void) -{ - static char user[64]; - int fd; - - if(user[0] == 0){ - fd = open("/dev/user", 0); - if(fd<0 || read(fd, user, sizeof user-1)<=0) - strcpy(user, "none"); - close(fd); - } - return user; -} - -int -statfile(char *name, ulong *dev, uvlong *id, long *time, long *length, long *appendonly) -{ - Dir *dirb; - - dirb = dirstat(name); - if(dirb == nil) - return -1; - if(dev) - *dev = dirb->type|(dirb->dev<<16); - if(id) - *id = dirb->qid.path; - if(time) - *time = dirb->mtime; - if(length) - *length = dirb->length; - if(appendonly) - *appendonly = dirb->mode & DMAPPEND; - free(dirb); - return 1; -} - -int -statfd(int fd, ulong *dev, uvlong *id, long *time, long *length, long *appendonly) -{ - Dir *dirb; - - dirb = dirfstat(fd); - if(dirb == nil) - return -1; - if(dev) - *dev = dirb->type|(dirb->dev<<16); - if(id) - *id = dirb->qid.path; - if(time) - *time = dirb->mtime; - if(length) - *length = dirb->length; - if(appendonly) - *appendonly = dirb->mode & DMAPPEND; - free(dirb); - return 1; -} - -void -notifyf(void *a, char *s) -{ - USED(a); - if(bpipeok && strcmp(s, "sys: write on closed pipe") == 0) - noted(NCONT); - if(strcmp(s, "interrupt") == 0) - noted(NCONT); - panicking = 1; - rescue(); - noted(NDFLT); -} - -int -newtmp(int num) -{ - int i, fd; - static char tempnam[30]; - - i = getpid(); - do - snprint(tempnam, sizeof tempnam, "%s/%d%.4s%dsam", TMPDIR, num, getuser(), i++); - while(access(tempnam, 0) == 0); - fd = create(tempnam, ORDWR|OCEXEC|ORCLOSE, 0000); - if(fd < 0){ - remove(tempnam); - fd = create(tempnam, ORDWR|OCEXEC|ORCLOSE, 0000); - } - return fd; -} - -int -waitfor(int pid) -{ - int msg; - Waitmsg *w; - - while((w = wait()) != nil){ - if(w->pid != pid){ - free(w); - continue; - } - msg = (w->msg[0] != '\0'); - free(w); - return msg; - } - return -1; -} - -void -samerr(char *buf) -{ - sprint(buf, "%s/sam.err", TMPDIR); -} - -void* -emalloc(ulong n) -{ - void *p; - - p = malloc(n); - if(p == 0) - panic("malloc fails"); - memset(p, 0, n); - return p; -} - -void* -erealloc(void *p, ulong n) -{ - p = realloc(p, n); - if(p == 0) - panic("realloc fails"); - return p; -} -- cgit v1.2.3 From 1c845e0bd5ff897dc5e90f2c24db4ecd81a8f60c Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Tue, 5 Jan 2021 23:38:09 -0500 Subject: acme, sam, samterm: remove weird switch usage For whatever reason all three of these programs contain switches like: switch(x) { case 1: if(cond) case 2: f(); } Like Duff's device, this is legal C but more obscure than it really needs to be. This commit assumes those are intended as written and simply writes them more clearly. I did consider that maybe they are mistakes, but in the case of sam/regexp.c, my rewrite in this commit matches the acme/regx.c that has been in plan9port since I added acme in 2003. (I didn't bother to dig up the old Plan 9 releases.) Assuming acme/regx.c has been correct for the past two decades, this commit should be correct too. --- src/cmd/sam/cmd.c | 6 ++++-- src/cmd/sam/regexp.c | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) (limited to 'src/cmd/sam') diff --git a/src/cmd/sam/cmd.c b/src/cmd/sam/cmd.c index 7176a827..13bd17e0 100644 --- a/src/cmd/sam/cmd.c +++ b/src/cmd/sam/cmd.c @@ -559,9 +559,11 @@ simpleaddr(void) case '.': case '$': case '\'': - if(addr.type!='"') + if(addr.type=='"') + break; + /* fall through */ case '"': - error(Eaddress); + error(Eaddress); break; case 'l': case '#': diff --git a/src/cmd/sam/regexp.c b/src/cmd/sam/regexp.c index 2e369fe1..57c639d9 100644 --- a/src/cmd/sam/regexp.c +++ b/src/cmd/sam/regexp.c @@ -700,11 +700,11 @@ bexecute(File *f, Posn startp) break; case 1: /* expired; wrap to end */ if(sel.p[0].p1>=0) - case 3: goto Return; list[0][0].inst = list[1][0].inst = 0; p = f->b.nc; goto doloop; + case 3: default: goto Return; } -- cgit v1.2.3 From 6a80119eb509bd948d87ad1b84b0a82855a3c691 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Thu, 14 Jan 2021 10:01:25 -0500 Subject: sam: remove backward ?: The exit code here is ignored anyway. --- src/cmd/sam/shell.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/cmd/sam') diff --git a/src/cmd/sam/shell.c b/src/cmd/sam/shell.c index c6efdd57..92bd5277 100644 --- a/src/cmd/sam/shell.c +++ b/src/cmd/sam/shell.c @@ -90,7 +90,7 @@ plan9(File *f, int type, String *s, int nest) free(c); } } - exits(retcode? "error" : 0); + exits(0); } if(pid==-1){ fprint(2, "Can't fork?!\n"); -- cgit v1.2.3 From 3ccd61629b641613bcccbc51125330efab9c89a7 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Thu, 14 Jan 2021 10:05:50 -0500 Subject: sam: avoid out-of-bounds read in rterm Usually r->nused < r->nalloc and the read is in bounds. But it could in theory be right on the line and reading past the end of the allocation. Make it safe but preserve as much of the old semantics as possible. This use of rterm appears to be only for optimization purposes so the result does not matter for correctness. --- src/cmd/sam/rasp.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/cmd/sam') diff --git a/src/cmd/sam/rasp.c b/src/cmd/sam/rasp.c index c96101df..55d16cfb 100644 --- a/src/cmd/sam/rasp.c +++ b/src/cmd/sam/rasp.c @@ -283,8 +283,8 @@ rterm(List *r, Posn p1) for(p = 0,i = 0; inused && p+L(i)<=p1; p+=L(i++)) ; - if(i==r->nused && (i==0 || !T(i-1))) - return 0; + if(i==r->nused) + return i > 0 && T(i-1); return T(i); } -- cgit v1.2.3