minimal plan9port to cross-build Jehanne

This commit is contained in:
Giacomo Tesio 2021-12-22 10:35:45 +01:00
commit c43269a19e
245 changed files with 26213 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.o

View File

@ -0,0 +1,21 @@
Copyright © 2021 Plan 9 Foundation
Portions Copyright © 2005 Russ Cox, MIT
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

27
README.txt Normal file
View File

@ -0,0 +1,27 @@
A minimal plan9port fork designed just to cross-build Jehanne
=============================================================
The sole goal of these tools is to cross-build Jehanne in a way
that can be more easily ported to Jehanne itself.
Tools available
---------------
- lib
- lib9: stripped down lib9 from plan9port
uses $JEHANNE/hacking instead of $PLAN9 to not mess with
more useful plan9port installation
- bio: plan9port libbio
- yaccpar and yaccpars (parts of "yacc runtime")
- cmd
- yacc (9yacc in $JEHANNE/hacking/bin)
- rc
Commands will be installed in $JEHANNE/hacking/bin
Libraries will be installed in $JEHANNE/hacking/lib
WARNING
-------
Do not use these tools for anything else.

12
build.sh Executable file
View File

@ -0,0 +1,12 @@
#!/bin/sh -e
TARGET=$JEHANNE/hacking/bin/
TRAMPOLINE=$JEHANNE/hacking/src/trampoline
(cd $TRAMPOLINE/lib/lib9/ && ./lib9.sh.build)
(cd $TRAMPOLINE/lib/bio/ && ./libbio.sh.build)
(cd $TRAMPOLINE/cmd/ && \
cc yacc.c -DPLAN9PORT -I$TRAMPOLINE/include -L$JEHANNE/hacking/lib -O0 -lbio -l9 -o $TARGET/9yacc)
(cp lib/yacc* $TARGET/../lib)
(cd $TRAMPOLINE/cmd/rc/ && ./rc.sh.build)

496
cmd/rc/code.c Normal file
View File

@ -0,0 +1,496 @@
#include "rc.h"
#include "io.h"
#include "exec.h"
#include "fns.h"
#include "getflags.h"
#define c0 t->child[0]
#define c1 t->child[1]
#define c2 t->child[2]
int codep, ncode;
#define emitf(x) ((void)(codep!=ncode || morecode()), codebuf[codep].f = (x), codep++)
#define emiti(x) ((void)(codep!=ncode || morecode()), codebuf[codep].i = (x), codep++)
#define emits(x) ((void)(codep!=ncode || morecode()), codebuf[codep].s = (x), codep++)
void stuffdot(int);
char *fnstr(tree*);
void outcode(tree*, int);
void codeswitch(tree*, int);
int iscase(tree*);
code *codecopy(code*);
void codefree(code*);
int
morecode(void)
{
ncode+=100;
codebuf = (code *)realloc((char *)codebuf, ncode*sizeof codebuf[0]);
if(codebuf==0)
panic("Can't realloc %d bytes in morecode!",
ncode*sizeof codebuf[0]);
memset(codebuf+ncode-100, 0, 100*sizeof codebuf[0]);
return 0;
}
void
stuffdot(int a)
{
if(a<0 || codep<=a)
panic("Bad address %d in stuffdot", a);
codebuf[a].i = codep;
}
int
compile(tree *t)
{
if(flag['D']) {
struct io *s;
s = openstr();
pfmt(s, "compile: %u\n", t);
write(2, s->strp, strlen(s->strp));
closeio(s);
if(eflagok) // made it out of rcmain - stop executing commands, just print them
t = nil;
}
ncode = 100;
codebuf = (code *)emalloc(ncode*sizeof codebuf[0]);
codep = 0;
emiti(0); /* reference count */
outcode(t, flag['e']?1:0);
if(nerror){
efree((char *)codebuf);
return 0;
}
readhere();
emitf(Xreturn);
emitf(0);
return 1;
}
void
cleanhere(char *f)
{
emitf(Xdelhere);
emits(strdup(f));
}
char*
fnstr(tree *t)
{
io *f = openstr();
char *v;
extern char nl;
char svnl = nl;
nl=';';
pfmt(f, "%t", t);
nl = svnl;
v = f->strp;
f->strp = 0;
closeio(f);
return v;
}
void
outcode(tree *t, int eflag)
{
int p, q;
tree *tt;
if(t==0)
return;
if(t->type!=NOT && t->type!=';')
runq->iflast = 0;
switch(t->type){
default:
pfmt(err, "bad type %d in outcode\n", t->type);
break;
case '$':
emitf(Xmark);
outcode(c0, eflag);
emitf(Xdol);
break;
case '"':
emitf(Xmark);
outcode(c0, eflag);
emitf(Xqdol);
break;
case SUB:
emitf(Xmark);
outcode(c0, eflag);
emitf(Xmark);
outcode(c1, eflag);
emitf(Xsub);
break;
case '&':
emitf(Xasync);
if(havefork){
p = emiti(0);
outcode(c0, eflag);
emitf(Xexit);
stuffdot(p);
} else
emits(fnstr(c0));
break;
case ';':
outcode(c0, eflag);
outcode(c1, eflag);
break;
case '^':
emitf(Xmark);
outcode(c1, eflag);
emitf(Xmark);
outcode(c0, eflag);
emitf(Xconc);
break;
case '`':
emitf(Xbackq);
if(havefork){
p = emiti(0);
outcode(c0, 0);
emitf(Xexit);
stuffdot(p);
} else
emits(fnstr(c0));
break;
case ANDAND:
outcode(c0, 0);
emitf(Xtrue);
p = emiti(0);
outcode(c1, eflag);
stuffdot(p);
break;
case ARGLIST:
outcode(c1, eflag);
outcode(c0, eflag);
break;
case BANG:
outcode(c0, eflag);
emitf(Xbang);
break;
case PCMD:
case BRACE:
outcode(c0, eflag);
break;
case COUNT:
emitf(Xmark);
outcode(c0, eflag);
emitf(Xcount);
break;
case FN:
emitf(Xmark);
outcode(c0, eflag);
if(c1){
emitf(Xfn);
p = emiti(0);
emits(fnstr(c1));
outcode(c1, eflag);
emitf(Xunlocal); /* get rid of $* */
emitf(Xreturn);
stuffdot(p);
}
else
emitf(Xdelfn);
break;
case IF:
outcode(c0, 0);
emitf(Xif);
p = emiti(0);
outcode(c1, eflag);
emitf(Xwastrue);
stuffdot(p);
break;
case NOT:
if(!runq->iflast)
yyerror("`if not' does not follow `if(...)'");
emitf(Xifnot);
p = emiti(0);
outcode(c0, eflag);
stuffdot(p);
break;
case OROR:
outcode(c0, 0);
emitf(Xfalse);
p = emiti(0);
outcode(c1, eflag);
stuffdot(p);
break;
case PAREN:
outcode(c0, eflag);
break;
case SIMPLE:
emitf(Xmark);
outcode(c0, eflag);
emitf(Xsimple);
if(eflag)
emitf(Xeflag);
break;
case SUBSHELL:
emitf(Xsubshell);
if(havefork){
p = emiti(0);
outcode(c0, eflag);
emitf(Xexit);
stuffdot(p);
} else
emits(fnstr(c0));
if(eflag)
emitf(Xeflag);
break;
case SWITCH:
codeswitch(t, eflag);
break;
case TWIDDLE:
emitf(Xmark);
outcode(c1, eflag);
emitf(Xmark);
outcode(c0, eflag);
emitf(Xmatch);
if(eflag)
emitf(Xeflag);
break;
case WHILE:
q = codep;
outcode(c0, 0);
if(q==codep)
emitf(Xsettrue); /* empty condition == while(true) */
emitf(Xtrue);
p = emiti(0);
outcode(c1, eflag);
emitf(Xjump);
emiti(q);
stuffdot(p);
break;
case WORDS:
outcode(c1, eflag);
outcode(c0, eflag);
break;
case FOR:
emitf(Xmark);
if(c1){
outcode(c1, eflag);
emitf(Xglob);
}
else{
emitf(Xmark);
emitf(Xword);
emits(strdup("*"));
emitf(Xdol);
}
emitf(Xmark); /* dummy value for Xlocal */
emitf(Xmark);
outcode(c0, eflag);
emitf(Xlocal);
p = emitf(Xfor);
q = emiti(0);
outcode(c2, eflag);
emitf(Xjump);
emiti(p);
stuffdot(q);
emitf(Xunlocal);
break;
case WORD:
emitf(Xword);
emits(strdup(t->str));
break;
case DUP:
if(t->rtype==DUPFD){
emitf(Xdup);
emiti(t->fd0);
emiti(t->fd1);
}
else{
emitf(Xclose);
emiti(t->fd0);
}
outcode(c1, eflag);
emitf(Xpopredir);
break;
case PIPEFD:
emitf(Xpipefd);
emiti(t->rtype);
if(havefork){
p = emiti(0);
outcode(c0, eflag);
emitf(Xexit);
stuffdot(p);
} else {
emits(fnstr(c0));
}
break;
case REDIR:
emitf(Xmark);
outcode(c0, eflag);
emitf(Xglob);
switch(t->rtype){
case APPEND:
emitf(Xappend);
break;
case WRITE:
emitf(Xwrite);
break;
case READ:
case HERE:
emitf(Xread);
break;
case RDWR:
emitf(Xrdwr);
break;
}
emiti(t->fd0);
outcode(c1, eflag);
emitf(Xpopredir);
break;
case '=':
tt = t;
for(;t && t->type=='=';t = c2);
if(t){
for(t = tt;t->type=='=';t = c2){
emitf(Xmark);
outcode(c1, eflag);
emitf(Xmark);
outcode(c0, eflag);
emitf(Xlocal);
}
outcode(t, eflag);
for(t = tt; t->type=='='; t = c2)
emitf(Xunlocal);
}
else{
for(t = tt;t;t = c2){
emitf(Xmark);
outcode(c1, eflag);
emitf(Xmark);
outcode(c0, eflag);
emitf(Xassign);
}
}
t = tt; /* so tests below will work */
break;
case PIPE:
emitf(Xpipe);
emiti(t->fd0);
emiti(t->fd1);
if(havefork){
p = emiti(0);
q = emiti(0);
outcode(c0, eflag);
emitf(Xexit);
stuffdot(p);
} else {
emits(fnstr(c0));
q = emiti(0);
}
outcode(c1, eflag);
emitf(Xreturn);
stuffdot(q);
emitf(Xpipewait);
break;
}
if(t->type!=NOT && t->type!=';')
runq->iflast = t->type==IF;
else if(c0) runq->iflast = c0->type==IF;
}
/*
* switch code looks like this:
* Xmark
* (get switch value)
* Xjump 1f
* out: Xjump leave
* 1: Xmark
* (get case values)
* Xcase 1f
* (commands)
* Xjump out
* 1: Xmark
* (get case values)
* Xcase 1f
* (commands)
* Xjump out
* 1:
* leave:
* Xpopm
*/
void
codeswitch(tree *t, int eflag)
{
int leave; /* patch jump address to leave switch */
int out; /* jump here to leave switch */
int nextcase; /* patch jump address to next case */
tree *tt;
if(c1->child[0]==nil
|| c1->child[0]->type!=';'
|| !iscase(c1->child[0]->child[0])){
yyerror("case missing in switch");
return;
}
emitf(Xmark);
outcode(c0, eflag);
emitf(Xjump);
nextcase = emiti(0);
out = emitf(Xjump);
leave = emiti(0);
stuffdot(nextcase);
t = c1->child[0];
while(t->type==';'){
tt = c1;
emitf(Xmark);
for(t = c0->child[0];t->type==ARGLIST;t = c0) outcode(c1, eflag);
emitf(Xcase);
nextcase = emiti(0);
t = tt;
for(;;){
if(t->type==';'){
if(iscase(c0)) break;
outcode(c0, eflag);
t = c1;
}
else{
if(!iscase(t)) outcode(t, eflag);
break;
}
}
emitf(Xjump);
emiti(out);
stuffdot(nextcase);
}
stuffdot(leave);
emitf(Xpopm);
}
int
iscase(tree *t)
{
if(t->type!=SIMPLE)
return 0;
do t = c0; while(t->type==ARGLIST);
return t->type==WORD && !t->quoted && strcmp(t->str, "case")==0;
}
code*
codecopy(code *cp)
{
cp[0].i++;
return cp;
}
void
codefree(code *cp)
{
code *p;
if(--cp[0].i!=0)
return;
for(p = cp+1;p->f;p++){
if(p->f==Xappend || p->f==Xclose || p->f==Xread || p->f==Xwrite
|| p->f==Xrdwr
|| p->f==Xasync || p->f==Xbackq || p->f==Xcase || p->f==Xfalse
|| p->f==Xfor || p->f==Xjump
|| p->f==Xsubshell || p->f==Xtrue) p++;
else if(p->f==Xdup || p->f==Xpipefd) p+=2;
else if(p->f==Xpipe) p+=4;
else if(p->f==Xword || p->f==Xdelhere) efree((++p)->s);
else if(p->f==Xfn){
efree(p[2].s);
p+=2;
}
}
efree((char *)cp);
}

1001
cmd/rc/exec.c Normal file

File diff suppressed because it is too large Load Diff

76
cmd/rc/exec.h Normal file
View File

@ -0,0 +1,76 @@
/*
* Definitions used in the interpreter
*/
extern void Xappend(void), Xasync(void), Xbackq(void), Xbang(void), Xclose(void);
extern void Xconc(void), Xcount(void), Xdelfn(void), Xdol(void), Xqdol(void), Xdup(void);
extern void Xexit(void), Xfalse(void), Xfn(void), Xfor(void), Xglob(void);
extern void Xjump(void), Xmark(void), Xmatch(void), Xpipe(void), Xread(void);
extern void Xrdwr(void);
extern void Xrdfn(void), Xunredir(void), Xstar(void), Xreturn(void), Xsubshell(void);
extern void Xtrue(void), Xword(void), Xwrite(void), Xpipefd(void), Xcase(void);
extern void Xlocal(void), Xunlocal(void), Xassign(void), Xsimple(void), Xpopm(void);
extern void Xrdcmds(void), Xwastrue(void), Xif(void), Xifnot(void), Xpipewait(void);
extern void Xdelhere(void), Xpopredir(void), Xsub(void), Xeflag(void), Xsettrue(void);
extern void Xerror(char*);
extern void Xerror1(char*);
/*
* word lists are in correct order,
* i.e. word0->word1->word2->word3->0
*/
struct word{
char *word;
word *next;
};
struct list{
word *words;
list *next;
};
word *newword(char *, word *), *copywords(word *, word *);
struct redir{
char type; /* what to do */
short from, to; /* what to do it to */
struct redir *next; /* what else to do (reverse order) */
};
#define NSTATUS ERRMAX /* length of status (from plan 9) */
/*
* redir types
*/
#define ROPEN 1 /* dup2(from, to); close(from); */
#define RDUP 2 /* dup2(from, to); */
#define RCLOSE 3 /* close(from); */
struct thread{
union code *code; /* code for this thread */
int pc; /* code[pc] is the next instruction */
struct list *argv; /* argument stack */
struct redir *redir; /* redirection stack */
struct redir *startredir; /* redir inheritance point */
struct var *local; /* list of local variables */
char *cmdfile; /* file name in Xrdcmd */
struct io *cmdfd; /* file descriptor for Xrdcmd */
int iflast; /* static `if not' checking */
int eof; /* is cmdfd at eof? */
int iflag; /* interactive? */
int lineno; /* linenumber */
int pid; /* process for Xpipewait to wait for */
char status[NSTATUS]; /* status for Xpipewait */
tree *treenodes; /* tree nodes created by this process */
thread *ret; /* who continues when this finishes */
};
thread *runq;
code *codecopy(code*);
code *codebuf; /* compiler output */
int ntrap; /* number of outstanding traps */
int trap[NSIG]; /* number of outstanding traps per type */
struct builtin{
char *name;
void (*fnc)(void);
};
extern struct builtin Builtin[];
int eflagok; /* kludge flag so that -e doesn't exit in startup */
extern int havefork;
void execcd(void), execwhatis(void), execeval(void), execexec(void);
int execforkexec(void);
void execexit(void), execshift(void);
void execwait(void), execumask(void), execdot(void), execflag(void);
void execfunc(var*), execcmds(io *);

162
cmd/rc/fmtquote.c Normal file
View File

@ -0,0 +1,162 @@
/*
* The authors of this software are Rob Pike and Ken Thompson.
* Copyright (c) 2002 by Lucent Technologies.
* Permission to use, copy, modify, and distribute this software for any
* purpose without fee is hereby granted, provided that this entire notice
* is included in all copies of any software which is or includes a copy
* or modification of this software and in all copies of the supporting
* documentation for such software.
* THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE ANY
* REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
* OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
*/
#include <u.h>
#include <libc.h>
#include "fmt.h"
#include "fmtdef.h"
extern int (*doquote)(int);
/*
* How many bytes of output UTF will be produced by quoting (if necessary) this string?
* How many runes? How much of the input will be consumed?
* The parameter q is filled in by _quotesetup.
* The string may be UTF or Runes (s or r).
* Return count does not include NUL.
* Terminate the scan at the first of:
* NUL in input
* count exceeded in input
* count exceeded on output
* *ninp is set to number of input bytes accepted.
* nin may be <0 initially, to avoid checking input by count.
*/
void
__quotesetup(char *s, int nin, int nout, Quoteinfo *q, int sharp)
{
int c;
q->quoted = 0;
q->nbytesout = 0;
q->nrunesout = 0;
q->nbytesin = 0;
q->nrunesin = 0;
if(sharp || nin==0 || *s=='\0'){
if(nout < 2)
return;
q->quoted = 1;
q->nbytesout = 2;
q->nrunesout = 2;
}
for(; nin!=0; nin-=1){
c = *s;
if(c == '\0')
break;
if(q->nrunesout+1 > nout)
break;
if((c <= L' ') || (c == L'\'') || (doquote!=nil && doquote(c))){
if(!q->quoted){
if(1+q->nrunesout+1+1 > nout) /* no room for quotes */
break;
q->nrunesout += 2; /* include quotes */
q->nbytesout += 2; /* include quotes */
q->quoted = 1;
}
if(c == '\'') {
q->nbytesout++;
q->nrunesout++; /* quotes reproduce as two characters */
}
}
/* advance input */
s++;
q->nbytesin++;
q->nrunesin++;
/* advance output */
q->nbytesout++;
q->nrunesout++;
}
}
static int
qstrfmt(char *sin, Quoteinfo *q, Fmt *f)
{
int r;
char *t, *s, *m, *me;
ulong fl;
int nc, w;
m = sin;
me = m + q->nbytesin;
w = f->width;
fl = f->flags;
if(!(fl & FmtLeft) && __fmtpad(f, w - q->nbytesout) < 0)
return -1;
t = f->to;
s = f->stop;
FMTCHAR(f, t, s, '\'');
for(nc = q->nrunesin; nc > 0; nc--){
r = *(uchar*)m++;
FMTCHAR(f, t, s, r);
if(r == '\'')
FMTCHAR(f, t, s, r);
}
FMTCHAR(f, t, s, '\'');
f->nfmt += t - (char *)f->to;
f->to = t;
if(fl & FmtLeft && __fmtpad(f, w - q->nbytesout) < 0)
return -1;
return 0;
}
int
__quotestrfmt(int runesin, Fmt *f)
{
int outlen;
char *s;
Quoteinfo q;
f->flags &= ~FmtPrec; /* ignored for %q %Q, so disable for %s %S in easy case */
s = va_arg(f->args, char *);
if(!s)
return __fmtcpy(f, "<nil>", 5, 5);
if(f->flush)
outlen = 0x7FFFFFFF; /* if we can flush, no output limit */
else
outlen = (char*)f->stop - (char*)f->to;
__quotesetup(s, -1, outlen, &q, f->flags&FmtSharp);
if(!q.quoted)
return __fmtcpy(f, s, q.nrunesin, q.nbytesin);
return qstrfmt(s, &q, f);
}
int
quotestrfmt(Fmt *f)
{
return __quotestrfmt(0, f);
}
void
quotefmtinstall(void)
{
fmtinstall('q', quotestrfmt);
}
int
__needsquotes(char *s, int *quotelenp)
{
Quoteinfo q;
__quotesetup(s, -1, 0x7FFFFFFF, &q, 0);
*quotelenp = q.nbytesout;
return q.quoted;
}

68
cmd/rc/fns.h Normal file
View File

@ -0,0 +1,68 @@
void Abort(void);
void Closedir(int);
int Creat(char*);
int Dup(int, int);
int Dup1(int);
int Eintr(void);
int Executable(char*);
void Execute(word*, word*);
void Exit(char*);
int ForkExecute(char*, char**, int, int, int);
int Globsize(char*);
int Isatty(int);
void Memcpy(char*, char*, long);
void Noerror(void);
int Opendir(char*);
long Read(int, char*, long);
int Readdir(int, char*, int);
long Seek(int, long, long);
void Trapinit(void);
void Unlink(char*);
void Updenv(void);
void Vinit(void);
int Waitfor(int, int);
long Write(int, char*, long);
void addwaitpid(int);
int advance(void);
int back(int);
void cleanhere(char*);
void codefree(code*);
int compile(tree*);
char * list2str(word*);
int count(word*);
void deglob(char*);
void delwaitpid(int);
void dotrap(void);
void freenodes(void);
void freewords(word*);
void globlist(void);
int havewaitpid(int);
int idchr(int);
void inttoascii(char*, long);
void kinit(void);
int mapfd(int);
int match(char*, char*, int);
int matchfn(char*, char*);
char** mkargv(word*);
void clearwaitpids(void);
void panic(char*, int);
void pathinit(void);
void poplist(void);
void popword(void);
void pprompt(void);
void pushlist(void);
void pushredir(int, int, int);
void pushword(char*);
void readhere(void);
word* searchpath(char*);
void setstatus(char*);
void setvar(char*, word*);
void skipnl(void);
void start(code*, int, var*);
int truestatus(void);
void usage(char*);
int wordchr(int);
void yyerror(char*);
int yylex(void);
int yyparse(void);
int parse(void);

244
cmd/rc/getflags.c Normal file
View File

@ -0,0 +1,244 @@
/*% cyntax -DTEST % && cc -DTEST -go # %
*/
#include "rc.h"
#include "getflags.h"
#include "fns.h"
char *flagset[] = {"<flag>"};
char **flag[NFLAG];
char cmdline[NCMDLINE+1];
char *cmdname;
static char *flagarg="";
static void reverse(char**, char**);
static int scanflag(int, char*);
static void errn(char*, int);
static void errs(char*);
static void errc(int);
static int reason;
#define RESET 1
#define FEWARGS 2
#define FLAGSYN 3
#define BADFLAG 4
static int badflag;
int
getflags(int argc, char *argv[], char *flags, int stop)
{
char *s, *t;
int i, j, c, count;
flagarg = flags;
if(cmdname==0)
cmdname = argv[0];
s = cmdline;
for(i = 0;i!=argc;i++){
for(t = argv[i];*t;t++)
if(s!=&cmdline[NCMDLINE])
*s++=*t;
if(i!=argc-1 && s!=&cmdline[NCMDLINE])
*s++=' ';
}
*s='\0';
i = 1;
while(i!=argc){
if(argv[i][0]!='-' || argv[i][1]=='\0'){
if(stop)
return argc;
i++;
continue;
}
s = argv[i]+1;
while(*s){
c=*s++;
count = scanflag(c, flags);
if(count==-1)
return -1;
if(flag[c]){ reason = RESET; badflag = c; return -1; }
if(count==0){
flag[c] = flagset;
if(*s=='\0'){
for(j = i+1;j<=argc;j++)
argv[j-1] = argv[j];
--argc;
}
}
else{
if(*s=='\0'){
for(j = i+1;j<=argc;j++)
argv[j-1] = argv[j];
--argc;
s = argv[i];
}
if(argc-i<count){
reason = FEWARGS;
badflag = c;
return -1;
}
reverse(argv+i, argv+argc);
reverse(argv+i, argv+argc-count);
reverse(argv+argc-count+1, argv+argc);
argc-=count;
flag[c] = argv+argc+1;
flag[c][0] = s;
s="";
}
}
}
return argc;
}
static void
reverse(char **p, char **q)
{
char *t;
for(;p<q;p++,--q){ t=*p; *p=*q; *q = t; }
}
static int
scanflag(int c, char *f)
{
int fc, count;
if(0<=c && c<NFLAG)
while(*f){
if(*f==' '){
f++;
continue;
}
fc=*f++;
if(*f==':'){
f++;
if(*f<'0' || '9'<*f){ reason = FLAGSYN; return -1; }
count = 0;
while('0'<=*f && *f<='9') count = count*10+*f++-'0';
}
else
count = 0;
if(*f=='['){
do{
f++;
if(*f=='\0'){ reason = FLAGSYN; return -1; }
}while(*f!=']');
f++;
}
if(c==fc)
return count;
}
reason = BADFLAG;
badflag = c;
return -1;
}
void
usage(char *tail)
{
char *s, *t, c;
int count, nflag = 0;
switch(reason){
case RESET:
errs("Flag -");
errc(badflag);
errs(": set twice\n");
break;
case FEWARGS:
errs("Flag -");
errc(badflag);
errs(": too few arguments\n");
break;
case FLAGSYN:
errs("Bad argument to getflags!\n");
break;
case BADFLAG:
errs("Illegal flag -");
errc(badflag);
errc('\n');
break;
}
errs("Usage: ");
errs(cmdname);
for(s = flagarg;*s;){
c=*s;
if(*s++==' ')
continue;
if(*s==':'){
s++;
count = 0;
while('0'<=*s && *s<='9') count = count*10+*s++-'0';
}
else count = 0;
if(count==0){
if(nflag==0)
errs(" [-");
nflag++;
errc(c);
}
if(*s=='['){
s++;
while(*s!=']' && *s!='\0') s++;
if(*s==']')
s++;
}
}
if(nflag)
errs("]");
for(s = flagarg;*s;){
c=*s;
if(*s++==' ')
continue;
if(*s==':'){
s++;
count = 0;
while('0'<=*s && *s<='9') count = count*10+*s++-'0';
}
else count = 0;
if(count!=0){
errs(" [-");
errc(c);
if(*s=='['){
s++;
t = s;
while(*s!=']' && *s!='\0') s++;
errs(" ");
errn(t, s-t);
if(*s==']')
s++;
}
else
while(count--) errs(" arg");
errs("]");
}
else if(*s=='['){
s++;
while(*s!=']' && *s!='\0') s++;
if(*s==']')
s++;
}
}
if(tail){
errs(" ");
errs(tail);
}
errs("\n");
Exit("bad flags");
}
static void
errn(char *s, int count)
{
while(count){ errc(*s++); --count; }
}
static void
errs(char *s)
{
while(*s) errc(*s++);
}
#define NBUF 80
static char buf[NBUF], *bufp = buf;
static void
errc(int c)
{
*bufp++=c;
if(bufp==&buf[NBUF] || c=='\n'){
Write(2, buf, bufp-buf);
bufp = buf;
}
}

7
cmd/rc/getflags.h Normal file
View File

@ -0,0 +1,7 @@
#define NFLAG 128
#define NCMDLINE 512
extern char **flag[NFLAG];
extern char cmdline[NCMDLINE+1];
extern char *cmdname;
extern char *flagset[];
int getflags(int, char*[], char*, int);

267
cmd/rc/glob.c Normal file
View File

@ -0,0 +1,267 @@
#include "rc.h"
#include "exec.h"
#include "fns.h"
char *globname;
struct word *globv;
/*
* delete all the GLOB marks from s, in place
*/
void
deglob(char *s)
{
char *t = s;
do{
if(*t==GLOB)
t++;
*s++=*t;
}while(*t++);
}
int
globcmp(const void *s, const void *t)
{
return strcmp(*(char**)s, *(char**)t);
}
void
globsort(word *left, word *right)
{
char **list;
word *a;
int n = 0;
for(a = left;a!=right;a = a->next) n++;
list = (char **)emalloc(n*sizeof(char *));
for(a = left,n = 0;a!=right;a = a->next,n++) list[n] = a->word;
qsort((void *)list, n, sizeof(void *), globcmp);
for(a = left,n = 0;a!=right;a = a->next,n++) a->word = list[n];
efree((char *)list);
}
/*
* Push names prefixed by globname and suffixed by a match of p onto the astack.
* namep points to the end of the prefix in globname.
*/
void
globdir(char *p, char *namep)
{
char *t, *newp;
int f;
/* scan the pattern looking for a component with a metacharacter in it */
if(*p=='\0'){
globv = newword(globname, globv);
return;
}
t = namep;
newp = p;
while(*newp){
if(*newp==GLOB)
break;
*t=*newp++;
if(*t++=='/'){
namep = t;
p = newp;
}
}
/* If we ran out of pattern, append the name if accessible */
if(*newp=='\0'){
*t='\0';
if(access(globname, 0)==0)
globv = newword(globname, globv);
return;
}
/* read the directory and recur for any entry that matches */
*namep='\0';
if((f = Opendir(globname[0]?globname:"."))<0) return;
while(*newp!='/' && *newp!='\0') newp++;
while(Readdir(f, namep, *newp=='/')){
if(matchfn(namep, p)){
for(t = namep;*t;t++);
globdir(newp, t);
}
}
Closedir(f);
}
/*
* Push all file names matched by p on the current thread's stack.
* If there are no matches, the list consists of p.
*/
void
glob(char *p)
{
word *svglobv = globv;
int globlen = Globsize(p);
if(!globlen){
deglob(p);
globv = newword(p, globv);
return;
}
globname = emalloc(globlen);
globname[0]='\0';
globdir(p, globname);
efree(globname);
if(svglobv==globv){
deglob(p);
globv = newword(p, globv);
}
else
globsort(globv, svglobv);
}
/*
* Do p and q point at equal utf codes
*/
int
equtf(char *p, char *q)
{
if(*p!=*q)
return 0;
if(twobyte(*p)) return p[1]==q[1];
if(threebyte(*p)){
if(p[1]!=q[1])
return 0;
if(p[1]=='\0')
return 1; /* broken code at end of string! */
return p[2]==q[2];
}
if(fourbyte(*p)){
if(p[1]!=q[1])
return 0;
if(p[1]=='\0')
return 1;
if(p[2]!=q[2])
return 0;
if(p[2]=='\0')
return 1;
return p[3]==q[3];
}
return 1;
}
/*
* Return a pointer to the next utf code in the string,
* not jumping past nuls in broken utf codes!
*/
char*
nextutf(char *p)
{
if(twobyte(*p)) return p[1]=='\0'?p+1:p+2;
if(threebyte(*p)) return p[1]=='\0'?p+1:p[2]=='\0'?p+2:p+3;
if(fourbyte(*p)) return p[1]=='\0'?p+1:p[2]=='\0'?p+2:p[3]=='\0'?p+3:p+4;
return p+1;
}
/*
* Convert the utf code at *p to a unicode value
*/
int
unicode(char *p)
{
int u=*p&0xff;
if(twobyte(u)) return ((u&0x1f)<<6)|(p[1]&0x3f);
if(threebyte(u)) return (u<<12)|((p[1]&0x3f)<<6)|(p[2]&0x3f);
if(fourbyte(u)) return (u<<18)|((p[1]&0x3f)<<12)|((p[2]&0x3f)<<6)|(p[3]&0x3f);
return u;
}
/*
* Does the string s match the pattern p
* . and .. are only matched by patterns starting with .
* * matches any sequence of characters
* ? matches any single character
* [...] matches the enclosed list of characters
*/
int
matchfn(char *s, char *p)
{
if(s[0]=='.' && (s[1]=='\0' || s[1]=='.' && s[2]=='\0') && p[0]!='.')
return 0;
return match(s, p, '/');
}
int
match(char *s, char *p, int stop)
{
int compl, hit, lo, hi, t, c;
for(;*p!=stop && *p!='\0';s = nextutf(s),p = nextutf(p)){
if(*p!=GLOB){
if(!equtf(p, s)) return 0;
}
else switch(*++p){
case GLOB:
if(*s!=GLOB)
return 0;
break;
case '*':
for(;;){
if(match(s, nextutf(p), stop)) return 1;
if(!*s)
break;
s = nextutf(s);
}
return 0;
case '?':
if(*s=='\0')
return 0;
break;
case '[':
if(*s=='\0')
return 0;
c = unicode(s);
p++;
compl=*p=='~';
if(compl)
p++;
hit = 0;
while(*p!=']'){
if(*p=='\0')
return 0; /* syntax error */
lo = unicode(p);
p = nextutf(p);
if(*p!='-')
hi = lo;
else{
p++;
if(*p=='\0')
return 0; /* syntax error */
hi = unicode(p);
p = nextutf(p);
if(hi<lo){ t = lo; lo = hi; hi = t; }
}
if(lo<=c && c<=hi)
hit = 1;
}
if(compl)
hit=!hit;
if(!hit)
return 0;
break;
}
}
return *s=='\0';
}
void
globlist1(word *gl)
{
if(gl){
globlist1(gl->next);
glob(gl->word);
}
}
void
globlist(void)
{
word *a;
globv = 0;
globlist1(runq->argv->words);
poplist();
pushlist();
if(globv){
for(a = globv;a->next;a = a->next);
a->next = runq->argv->words;
runq->argv->words = globv;
}
}

298
cmd/rc/havefork.c Normal file
View File

@ -0,0 +1,298 @@
#include <u.h>
#include <signal.h>
#if defined(PLAN9PORT) && defined(__sun__)
# define BSD_COMP /* sigh. for TIOCNOTTY */
#endif
#include <sys/ioctl.h>
#include "rc.h"
#include "getflags.h"
#include "exec.h"
#include "io.h"
#include "fns.h"
int havefork = 1;
void
Xasync(void)
{
int null = open("/dev/null", 0);
int tty;
int pid;
char npid[10];
if(null<0){
Xerror("Can't open /dev/null\n");
return;
}
switch(pid = rfork(RFFDG|RFPROC|RFNOTEG)){
case -1:
close(null);
Xerror("try again");
break;
case 0:
clearwaitpids();
/*
* I don't know what the right thing to do here is,
* so this is all experimentally determined.
* If we just dup /dev/null onto 0, then running
* ssh foo & will reopen /dev/tty, try to read a password,
* get a signal, and repeat, in a tight loop, forever.
* Arguably this is a bug in ssh (it behaves the same
* way under bash as under rc) but I'm fixing it here
* anyway. If we dissociate the process from the tty,
* then it won't be able to open /dev/tty ever again.
* The SIG_IGN on SIGTTOU makes writing the tty
* (via fd 1 or 2, for example) succeed even though
* our pgrp is not the terminal's controlling pgrp.
*/
if((tty = open("/dev/tty", OREAD)) >= 0){
/*
* Should make reads of tty fail, writes succeed.
*/
signal(SIGTTIN, SIG_IGN);
signal(SIGTTOU, SIG_IGN);
ioctl(tty, TIOCNOTTY);
close(tty);
}
if(isatty(0))
pushredir(ROPEN, null, 0);
else
close(null);
start(runq->code, runq->pc+1, runq->local);
runq->ret = 0;