commands.c

来自「linux下常用的网络工具的代码」· C语言 代码 · 共 2,831 行 · 第 1/5 页

C
2,831
字号
{    return dolmmode(bit, 1);}intclear_mode(int bit){    return dolmmode(bit, 0);}struct modelist {	const char	*name;		/* command name */	const char	*help;		/* help string */	int	(*handler)();	/* routine which executes command */	int	needconnect;	/* Do we need to be connected to execute? */	int	arg1;};extern int modehelp();static struct modelist ModeList[] = {    { "character", "Disable LINEMODE option",	docharmode, 1 },#ifdef	KLUDGELINEMODE    { "",	"(or disable obsolete line-by-line mode)", 0 },#endif    { "line",	"Enable LINEMODE option",	dolinemode, 1 },#ifdef	KLUDGELINEMODE    { "",	"(or enable obsolete line-by-line mode)", 0 },#endif    { "", "", 0 },    { "",	"These require the LINEMODE option to be enabled", 0 },    { "isig",	"Enable signal trapping",	set_mode, 1, MODE_TRAPSIG },    { "+isig",	0,				set_mode, 1, MODE_TRAPSIG },    { "-isig",	"Disable signal trapping",	clear_mode, 1, MODE_TRAPSIG },    { "edit",	"Enable character editing",	set_mode, 1, MODE_EDIT },    { "+edit",	0,				set_mode, 1, MODE_EDIT },    { "-edit",	"Disable character editing",	clear_mode, 1, MODE_EDIT },    { "softtabs", "Enable tab expansion",	set_mode, 1, MODE_SOFT_TAB },    { "+softtabs", 0,				set_mode, 1, MODE_SOFT_TAB },    { "-softtabs", "Disable character editing",	clear_mode, 1, MODE_SOFT_TAB },    { "litecho", "Enable literal character echo", set_mode, 1, MODE_LIT_ECHO },    { "+litecho", 0,				set_mode, 1, MODE_LIT_ECHO },    { "-litecho", "Disable literal character echo", clear_mode, 1, MODE_LIT_ECHO },    { "help",	0,				modehelp, 0 },#ifdef	KLUDGELINEMODE    { "kludgeline", 0,				dokludgemode, 1 },#endif    { "", "", 0 },    { "?",	"Print help information",	modehelp, 0 },    { 0 },};intmodehelp(){    struct modelist *mt;    printf("format is:  'mode Mode', where 'Mode' is one of:\n\n");    for (mt = ModeList; mt->name; mt++) {	if (mt->help) {	    if (*mt->help)		printf("%-15s %s\n", mt->name, mt->help);	    else		printf("\n");	}    }    return 0;}#define	GETMODECMD(name) (struct modelist *) \		genget(name, (char **) ModeList, sizeof(struct modelist))static intmodecmd(int  argc, char *argv[]){    struct modelist *mt;    if (argc != 2) {	printf("'mode' command requires an argument\n");	printf("'mode ?' for help.\n");    } else if ((mt = GETMODECMD(argv[1])) == 0) {	fprintf(stderr, "Unknown mode '%s' ('mode ?' for help).\n", argv[1]);    } else if (Ambiguous(mt)) {	fprintf(stderr, "Ambiguous mode '%s' ('mode ?' for help).\n", argv[1]);    } else if (mt->needconnect && !connected) {	printf("?Need to be connected first.\n");	printf("'mode ?' for help.\n");    } else if (mt->handler) {	return (*mt->handler)(mt->arg1);    }    return 0;}/* * The following data structures and routines implement the * "display" command. */static intdisplay(int  argc, char *argv[]){    struct togglelist *tl;    struct setlist *sl;#define	dotog(tl)	if (tl->variable && tl->actionexplanation) { \			    if (*tl->variable) { \				printf("will"); \			    } else { \				printf("won't"); \			    } \			    printf(" %s.\n", tl->actionexplanation); \			}#define	doset(sl)   if (sl->name && *sl->name != ' ') { \			if (sl->handler == 0) \			    printf("%-15s [%s]\n", sl->name, control(*sl->charp)); \			else \			    printf("%-15s \"%s\"\n", sl->name, (char *)sl->charp); \		    }    if (argc == 1) {	for (tl = Togglelist; tl->name; tl++) {	    dotog(tl);	}	printf("\n");	for (sl = Setlist; sl->name; sl++) {	    doset(sl);	}    } else {	int i;	for (i = 1; i < argc; i++) {	    sl = getset(argv[i]);	    tl = GETTOGGLE(argv[i]);	    if (Ambiguous(sl) || Ambiguous(tl)) {		printf("?Ambiguous argument '%s'.\n", argv[i]);		return 0;	    } else if (!sl && !tl) {		printf("?Unknown argument '%s'.\n", argv[i]);		return 0;	    } else {		if (tl) {		    dotog(tl);		}		if (sl) {		    doset(sl);		}	    }	}    }/*@*/optionstatus();#ifdef	ENCRYPTION    EncryptStatus();#endif	/* ENCRYPTION */    return 1;#undef	doset#undef	dotog}/* * The following are the data structures, and many of the routines, * relating to command processing. *//* * Set the escape character. */static intsetescape(int argc, char *argv[]){	register char *arg;	char buf[50];	printf(	    "Deprecated usage - please use 'set escape%s%s' in the future.\n",				(argc > 2)? " ":"", (argc > 2)? argv[1]: "");	if (argc > 2)		arg = argv[1];	else {		printf("new escape character: ");		fgets(buf, sizeof(buf), stdin);		arg = buf;	}	if (arg[0] != '\0')		escape = arg[0];	if (!In3270) {		printf("Escape character is '%s'.\n", control(escape));	}	fflush(stdout);	return 1;}    /*VARARGS*/static inttogcrmod(){    crmod = !crmod;    printf("Deprecated usage - please use 'toggle crmod' in the future.\n");    printf("%s map carriage return on output.\n", crmod ? "Will" : "Won't");    fflush(stdout);    return 1;}    /*VARARGS*/intsuspend(){#ifdef	SIGTSTP    setcommandmode();    {	long oldrows, oldcols, newrows, newcols, err;	err = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;	kill(0, SIGTSTP);	/*	 * If we didn't get the window size before the SUSPEND, but we	 * can get them now (?), then send the NAWS to make sure that	 * we are set up for the right window size.	 */	if (TerminalWindowSize(&newrows, &newcols) && connected &&	    (err || ((oldrows != newrows) || (oldcols != newcols)))) {		sendnaws();	}    }    /* reget parameters in case they were changed */    TerminalSaveState();    setconnmode(0);#else    printf("Suspend is not supported.  Try the '!' command instead\n");#endif    return 1;}#if	!defined(TN3270)    /*ARGSUSED*/intshell(int argc, char *argv[]){    long oldrows, oldcols, newrows, newcols, err;    setcommandmode();    err = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;    switch(vfork()) {    case -1:	perror("Fork failed\n");	break;    case 0:	{	    /*	     * Fire up the shell in the child.	     */	    register char *shellp, *shellname;#ifndef strrchr	    extern char *strrchr();#endif	    shellp = getenv("SHELL");	    if (shellp == NULL)		shellp = "/bin/sh";	    if ((shellname = strrchr(shellp, '/')) == 0)		shellname = shellp;	    else		shellname++;	    if (argc > 1)		execl(shellp, shellname, "-c", &saveline[1], 0);	    else		execl(shellp, shellname, 0);	    perror("Execl");	    _exit(1);	}    default:	    wait((int *)0);	/* Wait for the shell to complete */	    if (TerminalWindowSize(&newrows, &newcols) && connected &&		(err || ((oldrows != newrows) || (oldcols != newcols)))) {		    sendnaws();	    }	    break;    }    return 1;}#else	/* !defined(TN3270) */extern int shell();#endif	/* !defined(TN3270) */    /*VARARGS*//* int  argc;	 Number of arguments *//* char *argv[]; arguments */staticbye(int  argc, char *argv[]){    extern int resettermname;    if (connected) {	shutdown(net, 2);	printf("Connection closed.\n");	NetClose(net);	connected = 0;	resettermname = 1;#if	defined(AUTHENTICATION) || defined(ENCRYPTION)	auth_encrypt_connect(connected);#endif	/* defined(AUTHENTICATION) || defined(ENCRYPTION) */	/* reset options */	tninit();#if	defined(TN3270)	SetIn3270();		/* Get out of 3270 mode */#endif	/* defined(TN3270) */    }    if ((argc != 2) || (strcmp(argv[1], "fromquit") != 0)) {	longjmp(toplevel, 1);	/* NOTREACHED */    }    return 1;			/* Keep lint, etc., happy */}/*VARARGS*/intquit(){	call(bye, "bye", "fromquit", 0);	Exit(0);	/*NOTREACHED*/}/*VARARGS*/intlogout(){	send_do(TELOPT_LOGOUT, 1);	netflush();	return 1;}/* * The SLC command. */struct slclist {	const char	*name;	const char	*help;	void	(*handler)();	int	arg;};static void slc_help();struct slclist SlcList[] = {    { "export",	"Use local special character definitions",						slc_mode_export,	0 },    { "import",	"Use remote special character definitions",						slc_mode_import,	1 },    { "check",	"Verify remote special character definitions",						slc_mode_import,	0 },    { "help",	0,				slc_help,		0 },    { "?",	"Print help information",	slc_help,		0 },    { 0 },};static voidslc_help(){    struct slclist *c;    for (c = SlcList; c->name; c++) {	if (c->help) {	    if (*c->help)		printf("%-15s %s\n", c->name, c->help);	    else		printf("\n");	}    }}static struct slclist *getslc(name)    char *name;{    return (struct slclist *)		genget(name, (char **) SlcList, sizeof(struct slclist));}staticslccmd(argc, argv)    int  argc;    char *argv[];{    struct slclist *c;    if (argc != 2) {	fprintf(stderr,	    "Need an argument to 'slc' command.  'slc ?' for help.\n");	return 0;    }    c = getslc(argv[1]);    if (c == 0) {        fprintf(stderr, "'%s': unknown argument ('slc ?' for help).\n",    				argv[1]);        return 0;    }    if (Ambiguous(c)) {        fprintf(stderr, "'%s': ambiguous argument ('slc ?' for help).\n",    				argv[1]);        return 0;    }    (*c->handler)(c->arg);    slcstate();    return 1;}/* * The ENVIRON command. */struct envlist {	const char	*name;	const char	*help;	void	(*handler)();	int	narg;};extern struct env_lst *	env_define P((unsigned char *, unsigned char *));extern void	env_undefine P((unsigned char *)),	env_export P((unsigned char *)),	env_unexport P((unsigned char *)),	env_send P((unsigned char *)),#if defined(OLD_ENVIRON) && defined(ENV_HACK)	env_varval P((unsigned char *)),#endif	env_list P((void));static void	env_help P((void));struct envlist EnvList[] = {    { "define",	"Define an environment variable",						(void (*)())env_define,	2 },    { "undefine", "Undefine an environment variable",						env_undefine,	1 },    { "export",	"Mark an environment variable for automatic export",						env_export,	1 },    { "unexport", "Don't mark an environment variable for automatic export",						env_unexport,	1 },    { "send",	"Send an environment variable", env_send,	1 },    { "list",	"List the current environment variables",						env_list,	0 },#if defined(OLD_ENVIRON) && defined(ENV_HACK)    { "varval", "Reverse VAR and VALUE (auto, right, wrong, status)",						env_varval,    1 },#endif    { "help",	0,				env_help,		0 },    { "?",	"Print help information",	env_help,		0 },    { 0 },};static voidenv_help(){    struct envlist *c;    for (c = EnvList; c->name; c++) {	if (c->help) {	    if (*c->help)		printf("%-15s %s\n", c->name, c->help);	    else		printf("\n");	}    }}static struct envlist *getenvcmd(char *name){    return (struct envlist *)		genget(name, (char **) EnvList, sizeof(struct envlist));}intenv_cmd(int  argc, char *argv[]){    struct envlist *c;    if (argc < 2) {	fprintf(stderr,	    "Need an argument to 'environ' command.  'environ ?' for help.\n");	return 0;    }    c = getenvcmd(argv[1]);    if (c == 0) {        fprintf(stderr, "'%s': unknown argument ('environ ?' for help).\n",    				argv[1]);        return 0;    }    if (Ambiguous(c)) {        fprintf(stderr, "'%s': ambiguous argument ('environ ?' for help).\n",    				argv[1]);        return 0;    }    if (c->narg + 2 != argc) {	fprintf(stderr,	    "Need %s%d argument%s to 'environ %s' command.  'environ ?' for help.\n",		c->narg < argc + 2 ? "only " : "",		c->narg, c->narg == 1 ? "" : "s", c->name);	return 0;    }    (*c->handler)(argv[2], argv[3]);    return 1;}struct env_lst {	struct env_lst *next;	/* pointer to next structure */	struct env_lst *prev;	/* pointer to previous structure */	unsigned char *var;	/* pointer to variable name */	unsigned char *value;	/* pointer to variable value */	int export;		/* 1 -> export with default list of variables */	int welldefined;	/* A well defined variable */};struct env_lst envlisthead;struct env_lst *env_find(unsigned char *var){	register struct env_lst *ep;	for (ep = envlisthead.next; ep; ep = ep->next) {		if (strcmp((char *)ep->var, (char *)var) == 0)			return(ep);	}	return(NULL);}voidenv_init(){	extern char **environ;	register char **epp, *cp;	register struct env_lst *ep;#ifndef strchr	extern char *strchr();#endif	for (epp = environ; *epp; epp++) {		if (cp = strchr(*epp, '=')) {			*cp = '\0';			ep = env_define((unsigned char *)*epp,					(unsigned char *)cp+1);			ep->export = 0;			*cp = '=';		}	}	/*	 * Special case for DISPLAY variable.  If it is ":0.0" or	 * "unix:0.0", we have to get rid of "unix" and insert our	 * hostname.	 */	if ((ep = env_find("DISPLAY"))	    && ((*ep->value == ':')	        || (strncmp((char *)ep->value, "unix:", 5) == 0))) {		char *hostname = localhost ();		char *cp2 = strchr((char *)ep->value, ':');		cp = malloc(strlen(hostname) + strlen(cp2) + 1);

⌨️ 快捷键说明

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