Charles.Forsyth20060303644188019 years agoHistory
1285 lines · 23.0 KB · C
RawDownload
#include <lib9.h>#include <bio.h>#include <ctype.h>#include "mach.h"#include "regexp.h"#define Extern extern#include "acid.h"#include "y.tab.h" void	cvtatof(Node*, Node*);void	cvtatoi(Node*, Node*);void	cvtitoa(Node*, Node*);void	bprint(Node*, Node*);void	funcbound(Node*, Node*);void	printto(Node*, Node*);void	getfile(Node*, Node*);void	fmt(Node*, Node*);void	pcfile(Node*, Node*);void	pcline(Node*, Node*);void	setproc(Node*, Node*);void	strace(Node*, Node*);void	follow(Node*, Node*);void	reason(Node*, Node*);void	newproc(Node*, Node*);void	startstop(Node*, Node*);void	match(Node*, Node*);void	status(Node*, Node*);void	dokill(Node*,Node*);void	waitstop(Node*, Node*);void	stop(Node*, Node*);void	start(Node*, Node*);void	filepc(Node*, Node*);void	doerror(Node*, Node*);void	rc(Node*, Node*);void	doaccess(Node*, Node*);void	map(Node*, Node*);void	readfile(Node*, Node*);void	interpret(Node*, Node*);void	include(Node*, Node*);void	regexp(Node*, Node*);void	_bpcondset(Node*, Node*);void	_bpconddel(Node*, Node*);void	setdebug(Node*, Node*); typedef struct Btab Btab;struct Btab{	char	*name;	void	(*fn)(Node*, Node*);} tab[] ={	"atof",		cvtatof,	"atoi",		cvtatoi,	"error",	doerror,	"file",		getfile,	"readfile",	readfile,	"access",	doaccess,	"filepc",	filepc,	"fnbound",	funcbound,	"fmt",		fmt,	"follow",	follow,	"itoa",		cvtitoa,	"kill",		dokill,	"match",	match,	"newproc",	newproc,	"pcfile",	pcfile,	"pcline",	pcline,	"print",	bprint,	"printto",	printto,	"rc",		rc,	"reason",	reason,	"setproc",	setproc,	"sh",		rc,	"start",	start,	"startstop",	startstop,	"status",	status,	"stop",		stop,	"strace",	strace,	"waitstop",	waitstop,	"map",		map,	"interpret",	interpret,	"include",	include,	"regexp",	regexp,	"debug",	setdebug,	"_bpcondset",	_bpcondset,	"_bpconddel",	_bpconddel,	0}; voidmkprint(Lsym *s){	prnt = gmalloc(sizeof(Node));	prnt->op = OCALL;	prnt->left = gmalloc(sizeof(Node));	prnt->left->sym = s;} voidinstallbuiltin(void){	Btab *b;	Lsym *s; 	b = tab;	while(b->name) {		s = look(b->name);		if(s == 0)			s = enter(b->name, Tid); 		s->builtin = b->fn;		if(b->fn == bprint)			mkprint(s);		b++;	}} voidmatch(Node *r, Node *args){	int i;	List *f;	Node *av[Maxarg];	Node resi, resl; 	na = 0;	flatten(av, args);	if(na != 2)		error("match(obj, list): arg count"); 	expr(av[1], &resl);	if(resl.type != TLIST)		error("match(obj, list): need list");	expr(av[0], &resi); 	r->op = OCONST;	r->type = TINT;	r->nstore.fmt = 'D';	r->nstore.u0.sival = -1; 	i = 0;	for(f = resl.nstore.u0.sl; f; f = f->next) {		if(resi.type == f->type) {			switch(resi.type) {			case TINT:				if(resi.nstore.u0.sival == f->lstore.u0.sival) {					r->nstore.u0.sival = i;					return;				}				break;			case TFLOAT:				if(resi.nstore.u0.sfval == f->lstore.u0.sfval) {					r->nstore.u0.sival = i;					return;				}				break;			case TSTRING:				if(scmp(resi.nstore.u0.sstring, f->lstore.u0.sstring)) {					r->nstore.u0.sival = i;					return;				}				break;			case TLIST:				error("match(obj, list): not defined for list");			}		}		i++;	}} voidnewproc(Node *r, Node *args){	int i;	Node res;	char *p, *e;	char *argv[Maxarg], buf[Strsize]; 	i = 1;	argv[0] = aout; 	if(args) {		expr(args, &res);		if(res.type != TSTRING)			error("newproc(): arg not string");		if(res.nstore.u0.sstring->len >= sizeof(buf))			error("newproc(): too many arguments");		memmove(buf, res.nstore.u0.sstring->string, res.nstore.u0.sstring->len);		buf[res.nstore.u0.sstring->len] = '\0';		p = buf;		e = buf+res.nstore.u0.sstring->len;		for(;;) {			while(p < e && (*p == '\t' || *p == ' '))				*p++ = '\0';			if(p >= e)				break;			argv[i++] = p;			if(i >= Maxarg)				error("newproc: too many arguments");			while(p < e && *p != '\t' && *p != ' ')				p++;		}	}	argv[i] = 0;	r->op = OCONST;	r->type = TINT;	r->nstore.fmt = 'D';	r->nstore.u0.sival = nproc(argv);}voidstartstop(Node *r, Node *args){	Node res; 	if(args == 0)		error("startstop(pid): no pid");	expr(args, &res);	if(res.type != TINT)		error("startstop(pid): arg type");	if(rdebug) {		Lsym *s;		r->op = OCONST;		r->type = TINT;		r->nstore.u0.sival = remcondstartstop(res.nstore.u0.sival);		r->nstore.fmt = 'D'; 		s = look("_breakid");		if(s)			s->v->vstore.u0.sival = (int)r->nstore.u0.sival;	} else		msg(res.nstore.u0.sival, "startstop");	notes(res.nstore.u0.sival);	dostop(res.nstore.u0.sival);} voidwaitstop(Node *r, Node *args){	Node res; 	USED(r);	if(args == 0)		error("waitstop(pid): no pid");	expr(args, &res);	if(res.type != TINT)		error("waitstop(pid): arg type"); 	Bflush(bout);	msg(res.nstore.u0.sival, "waitstop");	notes(res.nstore.u0.sival);	dostop(res.nstore.u0.sival);} voidstart(Node *r, Node *args){	Node res; 	USED(r);	if(args == 0)		error("start(pid): no pid");	expr(args, &res);	if(res.type != TINT)		error("start(pid): arg type"); 	msg(res.nstore.u0.sival, "start");} voidstop(Node *r, Node *args){	Node res; 	USED(r);	if(args == 0)		error("stop(pid): no pid");	expr(args, &res);	if(res.type != TINT)		error("stop(pid): arg type"); 	Bflush(bout);	msg(res.nstore.u0.sival, "stop");	notes(res.nstore.u0.sival);	dostop(res.nstore.u0.sival);} voiddokill(Node *r, Node *args){	Node res; 	USED(r);	if(args == 0)		error("kill(pid): no pid");	expr(args, &res);	if(res.type != TINT)		error("kill(pid): arg type"); 	msg(res.nstore.u0.sival, "kill");	deinstall(res.nstore.u0.sival);} voidstatus(Node *r, Node *args){	Node res;	char *p; 	USED(r);	if(args == 0)		error("status(pid): no pid");	expr(args, &res);	if(res.type != TINT)		error("status(pid): arg type"); 	p = getstatus(res.nstore.u0.sival);	r->nstore.u0.sstring = strnode(p);	r->op = OCONST;	r->nstore.fmt = 's';	r->type = TSTRING;} voidreason(Node *r, Node *args){	Node res; 	if(args == 0)		error("reason(cause): no cause");	expr(args, &res);	if(res.type != TINT)		error("reason(cause): arg type"); 	r->op = OCONST;	r->type = TSTRING;	r->nstore.fmt = 's';	r->nstore.u0.sstring = strnode((*machdata->excep)(cormap, rget));} voidfollow(Node *r, Node *args){	int n, i;	Node res;	ulong f[10];	List **tail, *l; 	if(args == 0)		error("follow(addr): no addr");	expr(args, &res);	if(res.type != TINT)		error("follow(addr): arg type"); 	n = (*machdata->foll)(cormap, res.nstore.u0.sival, rget, f);	if (n < 0)		error("follow(addr): %r");	tail = &r->nstore.u0.sl;	for(i = 0; i < n; i++) {		l = al(TINT);		l->lstore.u0.sival = f[i];		l->lstore.fmt = 'X';		*tail = l;		tail = &l->next;	}} voidfuncbound(Node *r, Node *args){	int n;	Node res;	ulong bounds[2];	List *l; 	if(args == 0)		error("fnbound(addr): no addr");	expr(args, &res);	if(res.type != TINT)		error("fnbound(addr): arg type"); 	n = fnbound(res.nstore.u0.sival, bounds);	if (n != 0) {		r->nstore.u0.sl = al(TINT);		l = r->nstore.u0.sl;		l->lstore.u0.sival = bounds[0];		l->lstore.fmt = 'X';		l->next = al(TINT);		l = l->next;		l->lstore.u0.sival = bounds[1];		l->lstore.fmt = 'X';	}} voidsetproc(Node *r, Node *args){	Node res; 	USED(r);	if(args == 0)		error("setproc(pid): no pid");	expr(args, &res);	if(res.type != TINT)		error("setproc(pid): arg type"); 	sproc(res.nstore.u0.sival);} voidfilepc(Node *r, Node *args){	Node res;	char *p, c; 	if(args == 0)		error("filepc(filename:line): arg count");	expr(args, &res);	if(res.type != TSTRING)		error("filepc(filename:line): arg type"); 	p = strchr(res.nstore.u0.sstring->string, ':');	if(p == 0)		error("filepc(filename:line): bad arg format"); 	c = *p;	*p++ = '\0';	r->nstore.u0.sival = file2pc(res.nstore.u0.sstring->string, atoi(p));	p[-1] = c;	if(r->nstore.u0.sival == -1)		error("filepc(filename:line): can't find address"); 	r->op = OCONST;	r->type = TINT;	r->nstore.fmt = 'D';} voidinterpret(Node *r, Node *args){	Node res;	int isave; 	if(args == 0)		error("interpret(string): arg count");	expr(args, &res);	if(res.type != TSTRING)		error("interpret(string): arg type"); 	pushstr(&res); 	isave = interactive;	interactive = 0;	r->nstore.u0.sival = yyparse();	interactive = isave;	popio();	r->op = OCONST;	r->type = TINT;	r->nstore.fmt = 'D';} voidinclude(Node *r, Node *args){	Node res;	int isave; 	if(args == 0)		error("include(string): arg count");	expr(args, &res);	if(res.type != TSTRING)		error("include(string): arg type"); 	pushfile(res.nstore.u0.sstring->string); 	isave = interactive;	interactive = 0;	r->nstore.u0.sival = yyparse();	interactive = isave;	popio();	r->op = OCONST;	r->type = TINT;	r->nstore.fmt = 'D';} voidrc(Node *r, Node *args){	Node res; 	char *p, *q; 	USED(r);	if(args == 0)		error("error(string): arg count");	expr(args, &res);	if(res.type != TSTRING)		error("error(string): arg type"); 	p = runcmd(res.nstore.u0.sstring->string);	q = strrchr(p, ':');

Lines 1–500 of 1285.