⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 con.c

📁 这是一个同样来自贝尔实验室的和UNIX有着渊源的操作系统, 其简洁的设计和实现易于我们学习和理解
💻 C
📖 第 1 页 / 共 2 页
字号:
#include	"all.h"#include	"mem.h"static	char	conline[2*Maxword];static	Command	command[100];static	Flag	flag[35];static	void	installcmds(void);static	void	consserve1(void);static	char	statsdef[20];	/* default stats list */static	int	whoflag;voidconsserve(void){	int i;	strncpy(cons.chan->whochan, "console", sizeof(cons.chan->whochan));	installcmds();	con_session();	cmd_exec("cfs");	cmd_exec("users");	cmd_exec("version");	for(i = 0; command[i].arg0; i++){		if(strcmp("cwcmd", command[i].arg0) == 0){			cmd_exec("cwcmd touchsb");			break;		}	}	userinit(consserve1, 0, "con");}staticvoidconsserve1(void){	int i, ch;	/*conslock();*/loop:	print("%s: ", service);	for(i=0;;) {		ch = getc();		switch(ch) {		default:			if(i < nelem(conline)-2)				conline[i++] = ch;			break;		case '\b':			if(i > 0)				i--;			break;		case 'U' - '@':			i = 0;			break;		case '\n':			conline[i] = 0;			cmd_exec(conline);		case -1:			goto loop;		case 'D' - '@':			print("\n");			/*conslock();*/			goto loop;		}	}}staticintcmdcmp(void *va, void *vb){	Command *a, *b;	a = va;	b = vb;	return strcmp(a->arg0, b->arg0);}voidcmd_install(char *arg0, char *help, void (*func)(int, char*[])){	int i;	qlock(&cons);	for(i=0; command[i].arg0; i++)		;	if(i >= nelem(command)-2) {		qunlock(&cons);		print("cmd_install: too many commands\n");		return;	}	command[i+1].arg0 = 0;	command[i].help = help;	command[i].func = func;	command[i].arg0 = arg0;	qsort(command, i+1, sizeof(Command), cmdcmp);	qunlock(&cons);}voidcmd_exec(char *arg){	char line[2*Maxword], *s;	char *argv[10];	int argc, i, c;	if(strlen(arg) >= nelem(line)-2) {		print("cmd_exec: line too long\n");		return;	}	strcpy(line, arg);	argc = 0;	s = line;	c = *s++;	for(;;) {		while(c == ' ' || c == '\t')			c = *s++;		if(c == 0)			break;		if(argc >= nelem(argv)-2) {			print("cmd_exec: too many args\n");			return;		}		argv[argc++] = s-1;		while(c != ' ' && c != '\t' && c != 0)			c = *s++;		s[-1] = 0;	}	if(argc <= 0)		return;	for(i=0; s=command[i].arg0; i++)		if(strcmp(argv[0], s) == 0) {			(*command[i].func)(argc, argv);			prflush();			return;		}	print("cmd_exec: unknown command: %s\n", argv[0]);}staticvoidcmd_halt(int, char *[]){	wlock(&mainlock);	/* halt */	sync("halt");	exit();}staticvoidcmd_duallow(int argc, char *argv[]){	int uid;	if(argc <= 1) {		duallow = 0;		return;	}	uid = strtouid(argv[1]);	if(uid < 0)		uid = number(argv[1], -2, 10);	if(uid < 0) {		print("bad uid %s\n", argv[1]);		return;	}	duallow = uid;}staticvoidcmd_stats(int argc, char *argv[]){	int i, c;	char buf[30], *s, *p, *q;	if(argc <= 1) {		if(statsdef[0] == 0)			strcpy(statsdef, "a");		sprint(buf, "stats s%s", statsdef);		cmd_exec(buf);		return;	}	strcpy(buf, "stat");	p = strchr(buf, 0);	p[1] = 0;	q = 0;	for(i=1; i<argc; i++) {		for(s=argv[i]; c=*s; s++) {			if(c == 's')				continue;			if(c == '-') {				q = statsdef;				continue;			}			if(q) {				*q++ = c;				*q = 0;			}			*p = c;			cmd_exec(buf);		}	}}staticvoidcmd_stata(int, char *[]){	int i;	print("cons stats\n");	print("	work =%7W%7W%7W rps\n", cons.work+0, cons.work+1, cons.work+2);	print("	rate =%7W%7W%7W tBps\n", cons.rate+0, cons.rate+1, cons.rate+2);	print("	hits =%7W%7W%7W iops\n", cons.bhit+0, cons.bhit+1, cons.bhit+2);	print("	read =%7W%7W%7W iops\n", cons.bread+0, cons.bread+1, cons.bread+2);	print("	rah  =%7W%7W%7W iops\n", cons.brahead+0, cons.brahead+1, cons.brahead+2);	print("	init =%7W%7W%7W iops\n", cons.binit+0, cons.binit+1, cons.binit+2);	print("	bufs =    %3ld sm %3ld lg %ld res\n",		cons.nsmall, cons.nlarge, cons.nreseq);	for(i=0; i<nelem(mballocs); i++)		if(mballocs[i])			print("	[%d]=%d\n", i, mballocs[i]);	print("	ioerr=    %3ld wr %3ld ww %3ld dr %3ld dw\n",		cons.nwormre, cons.nwormwe, cons.nwrenre, cons.nwrenwe);	print("	cache=     %9ld hit %9ld miss\n",		cons.nwormhit, cons.nwormmiss);}staticintflagcmp(void *va, void *vb){	Flag *a, *b;	a = va;	b = vb;	return strcmp(a->arg0, b->arg0);}ulongflag_install(char *arg, char *help){	int i;	qlock(&cons);	for(i=0; flag[i].arg0; i++)		;	if(i >= 32) {		qunlock(&cons);		print("flag_install: too many flags\n");		return 0;	}	flag[i+1].arg0 = 0;	flag[i].arg0 = arg;	flag[i].help = help;	flag[i].flag = 1<<i;	qsort(flag, i+1, sizeof(Flag), flagcmp);	qunlock(&cons);	return 1<<i;}voidcmd_flag(int argc, char *argv[]){	int f, n, i, j;	char *s;	Chan *cp;	if(argc <= 1) {		for(i=0; flag[i].arg0; i++)			print("%.4lux %s %s\n",				flag[i].flag, flag[i].arg0, flag[i].help);		if(cons.flags)			print("flag[*]   = %.4lux\n", cons.flags);		for(cp = chans; cp; cp = cp->next)			if(cp->flags)				print("flag[%3d] = %.4lux\n", cp->chan, cp->flags);		return;	}	f = 0;	n = -1;	for(i=1; i<argc; i++) {		for(j=0; s=flag[j].arg0; j++)			if(strcmp(s, argv[i]) == 0)				goto found;		j = number(argv[i], -1, 10);		if(j < 0) {			print("bad flag argument: %s\n", argv[i]);			continue;		}		n = j;		continue;	found:		f |= flag[j].flag;	}	if(n < 0) {		cons.flags ^= f;		if(f == 0)			cons.flags = 0;		print("flag      = %.8lux\n", cons.flags);		return;	}	for(cp = chans; cp; cp = cp->next) {		if(cp->chan == n) {			cp->flags ^= f;			if(f == 0)				cp->flags = 0;			print("flag[%3d] = %.8lux\n", cp->chan, cp->flags);			return;		}	}	print("no such channel\n");}staticvoidcmd_who(int argc, char *argv[]){	Chan *cp;	int i, c;	c = 0;	for(cp = chans; cp; cp = cp->next) {		if(cp->whotime == 0 && !(cons.flags & whoflag)) {			c++;			continue;		}		if(argc > 1) {			for(i=1; i<argc; i++)				if(strcmp(argv[i], cp->whoname) == 0)					break;			if(i >= argc) {				c++;				continue;			}		}		print("%3d: %10s %24s%7W%7W",			cp->chan,			cp->whoname ? cp->whoname: "<nowhoname>",			cp->whochan,			&cp->work,			&cp->rate);		if(cp->whoprint)			cp->whoprint(cp);		print("\n");		prflush();	}	if(c > 0)		print("%d chans not listed\n", c);}staticvoidcmd_hangup(int argc, char *argv[]){	Chan *cp;	int n;	if(argc < 2) {		print("usage: hangup chan number\n");		return;	}	n = number(argv[1], -1, 10);	for(cp = chans; cp; cp = cp->next) {		if(cp->whotime == 0) {			if(cp->chan == n)				print("that chan is hung up\n");			continue;		}		if(cp->chan == n)			fileinit(cp);	}}staticvoidcmd_sync(int, char *[]){	wlock(&mainlock);	/* sync */	sync("command");	wunlock(&mainlock);	print("\n");}staticvoidcmd_help(int argc, char *argv[]){	char *arg;	int i, j;	for(i=0; arg=command[i].arg0; i++) {		if(argc > 1) {			for(j=1; j<argc; j++)				if(strcmp(argv[j], arg) == 0)					goto found;			continue;		}	found:		print("	%s %s\n", arg, command[i].help);		prflush();	}}staticvoidcmd_date(int argc, char *argv[]){	Timet ct, t;	char *arg;	if(argc <= 1)		goto out;	ct = time();	arg = argv[1];	switch(*arg) {	default:		t = number(arg, -1, 10);		if(t <= 0)			goto out;		ct = t;		break;	case '+':		t = number(arg+1, 0, 10);		ct += t;		break;	case '-':		t = number(arg+1, 0, 10);		ct -= t;	}	settime(ct);	setrtc(ct);out:	prdate();}voidcmd_fstat(int argc, char *argv[]){	int i;	for(i=1; i<argc; i++) {		if(walkto(argv[i])) {			print("cant stat %s\n", argv[i]);			continue;		}		con_fstat(FID2);	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -