tnmain.cpp

来自「一个类似windows」· C++ 代码 · 共 719 行 · 第 1/2 页

CPP
719
字号
	//// (Paul Brannan 5/14/98)
	
	if(ini.get_term_width() != -1 || ini.get_term_height() != -1) {
		SetConsoleScreenBufferSize(
			GetStdHandle(STD_OUTPUT_HANDLE),	// handle of console screen buffer
			ConsoleScreenBufferInfo.dwSize		// new size in character rows and cols.
			);
		SetConsoleWindowInfo(
			GetStdHandle(STD_OUTPUT_HANDLE),	// handle of console screen buffer
			TRUE,								// coordinate type flag
			&ConsoleScreenBufferInfo.srWindow 	// address of new window rectangle
			);
	}
	SetConsoleTextAttribute(
		GetStdHandle(STD_OUTPUT_HANDLE),		// handle of console screen buffer
		ConsoleScreenBufferInfo.wAttributes 	// text and background colors
		);

	// Restore the original console title
	// ("Pedro A. Aranda Guti閞rez" <paag@coppi.tid.es>)
	SetConsoleTitle(ConsoleTitle);

	return 0;
}

// AVS
enum {
	BAD_USAGE = -3,
		EMPTY_LINE = -2,
		INVALID_CMD = -1,
		__FIRST_COMMAND = 0,
		
		OPEN = __FIRST_COMMAND,
		CLOSE,
		KEYS,
		QUIT,
		HELP,
		HELP2,				// there is way for synonims
		K_LOAD,				// subcommand of 'keys'
		K_SWITCH,			// subcommand of 'keys'
		K_DISPLAY,			// subcommand of 'keys'
		
		SET,				// Paul Brannan 5/30/98
		
		SUSPEND,
		FASTQUIT,			// Thomas Briggs 8/11/98
		CMD_HISTORY,		// crn@ozemail.com.au
		CLEAR_HISTORY,		// crn@ozemail.com.au
		
		ALIASES,			// Paul Brannan 1/1/99
		
		__COMMAND_LIST_SIZE	// must be last
};


struct command {
	const char* cmd;				// command
	int   minLen,			// minimal length for match
		  minParms,			// minimal count of parms
		  maxParms;			// maximal -/- (negative disables)
	int   isSubCmd,			// is a subcommand - number of wich command
		  haveSubCmd;		// have subcommands? 0 or 1
	const char* usage;			// text of usage
};

command cmdList[__COMMAND_LIST_SIZE] = {
	{"open",     1,	1,  2,	-1,		0,	"o[pen] host [port]\n"},
	{"close",    2,	0,  0,	-1,		0,	NULL},
	{"keys",     2,	1,  3,	-1,		1,	"ke[ys] l[oad] keymapname [file]\n"
										"ke[ys] d[isplay]\n"
										"ke[ys] s[witch] number\n"},
	// Ioannou : i change it to q, to be more compatible with unix telnet
	{"quit",     1,	0,  0,	-1,		0,	NULL}, // must type it exactly
	{"?",        1,	0,  0,	-1,		0,	NULL},
	{"help",     1,	0,  0,	-1,		0,	NULL},
	{"load",     1,	1,  2,	KEYS,	0,	NULL},
	{"switch",   1,	1,  1,	KEYS,	0,	NULL},
	{"display",  1,	0,  0,	KEYS,	0,	NULL},
	// Paul Brannan 5/30/98
	{"set",      3,	0,	2,	-1,		0,	"set will display available groups.\n"
										"set groupname will display all variables/values in a group.\n"
										"set [variable [value]] will set variable to value.\n"},
	// Thomas Briggs 8/11/98
	{"z",		1,	0,	0,	-1,		0,	"suspend telnet\n"},
	{"\04",		1,	0,	0,	-1,		0,	NULL},
	// crn@ozemail.com.au
	{"history",	2,	0,	0,	-1,	0,	"show command history"},
	{"flush",	2,	0,	0,	-1,	0,	"flush history buffer"},
	// Paul Brannan 1/1/99
	{"aliases",	5,	0,	0,	-1,		0,	NULL}
};

// a maximal count of parms
#define MAX_PARM_COUNT 3
#define MAX_TOKEN_COUNT (MAX_PARM_COUNT+2)

static int cmdMatch(const char* cmd, const char* token, int tokenLen, int minM) {
    if ( tokenLen < minM ) return 0;
	// The (unsigned) gets rid of a compiler warning (Paul Brannan 5/25/98)
    if ( (unsigned)tokenLen > strlen(cmd) ) return 0;
    if ( strcmp(cmd,token) == 0 ) return 1;
	
    int i;
    for ( i = 0; i < minM; i++ ) if ( cmd[i] != token[i] ) return 0;
	
    for ( i = minM; i < tokenLen; i++ ) if ( cmd[i] != token[i] ) return 0;
	
    return 1;
};

static void printUsage(int cmd) {
	if ( cmdList[cmd].usage != NULL ) {
		printit(cmdList[cmd].usage);
		return;
	};
	if ( cmdList[cmd].isSubCmd >= 0 ) {
		printUsage(cmdList[cmd].isSubCmd);
		return;
	   }
	   printm(0, FALSE, MSG_BADUSAGE);
};

int tokenizeCommand(char* szCommand, int& argc, char** argv) {
    char* tokens[MAX_TOKEN_COUNT];
    char* p;
    int   args = 0;

	if(!szCommand || !*szCommand) return EMPTY_LINE;

	// Removed strtok to handle tokens with spaces; this is handled with
	// quotes.  (Paul Brannan 3/18/99)
	char *token_start = szCommand;
	for(p = szCommand;; p++) {
		if(*p == '\"') {
			char *tmp = p;
			for(p++; *p != '\"' && *p != 0; p++);	// Find the next quote
			if(*p != 0) strcpy(p, p + 1);			// Remove quote#2
			strcpy(tmp, tmp + 1);					// Remove quote#1
		}
		if(*p == 0 || *p == ' ' || *p == '\t') {
			tokens[args] = token_start;
			args++;
			if(args >= MAX_TOKEN_COUNT) break;		// Break if too many args
			token_start = p + 1;
			if(*p == 0) break;
			*p = 0;
		}
	}
	// while ( (p = strtok((args?NULL:szCommand), " \t")) != NULL && args < MAX_TOKEN_COUNT ) {
	// 	tokens[args] = p;
	// 	args++;
	// };
	
    if ( !args ) return EMPTY_LINE;
    argc = args - 1;
    args = 0;
    int curCmd = -1;
    int ok = -1;
    while ( ok < 0 ) {
		int tokenLen = strlen(tokens[args]);
		int match = 0;
		for ( int i = 0; i<__COMMAND_LIST_SIZE; i++ ) {
			if ( cmdMatch(cmdList[i].cmd, tokens[args], tokenLen, cmdList[i].minLen) ) {
				if (argc < cmdList[i].minParms || argc > cmdList[i].maxParms) {
					printUsage(i);
					return BAD_USAGE;
				};
				if ( cmdList[i].haveSubCmd && curCmd == cmdList[i].isSubCmd) {
					curCmd = i;
					args++;
					argc--;
					match = 1;
					break;
				};
				if ( curCmd == cmdList[i].isSubCmd ) {
					ok = i;
					match = 1;
					break;
				};
				printUsage(i);
				return BAD_USAGE;
			};
		};
		if ( !match ) {
			if ( curCmd < 0 ) return INVALID_CMD;
			printUsage(curCmd);
			return -3;
		};
    };
	
    for ( int i = 0; i<argc; i++ ) {
        argv[i] = tokens[i+args+1];
    };
    return ok;
	
};

int telCommandLine (Telnet &MyConnection){
#define HISTLENGTH 25
	int i, retval;
	char* Parms[MAX_PARM_COUNT];
	char szCommand[80];
	int bDone = 0;
	char *extitle, *newtitle;
	struct cmdHistory *cmdhist;
	cmdhist = NULL;
	
	// printit("\n");  // crn@ozemail.com.au 14/12/98
	while (!bDone){
		// printit("\n"); // Paul Brannan 5/25/98
		printit( "telnet>");
		cmdhist = cfgets (szCommand, 79, cmdhist);
		printit( "\n");

		strlwr(szCommand);  // convert command line to lower
		// i = sscanf(szCommand,"%80s %80s %80s %80s", szCmd, szArg1, szArg2, szArg3);
		switch ( tokenizeCommand(szCommand, i, Parms) ) {
		case BAD_USAGE:   break;
		case EMPTY_LINE:  
			if(MyConnection.Resume() == TNPROMPT) {
				printit("\n");
				break;
			}
			else
			 	return 1;
		case INVALID_CMD:
			printm(0, FALSE, MSG_INVCMD);
			break;			
		case OPEN:
			if (i == 1)
				retval = MyConnection.Open(Parms[0], "23");
			else
				retval = MyConnection.Open(Parms[0], Parms[1]);
			if(retval != TNNOCON && retval != TNPROMPT) return 1;
			if(retval == TNPROMPT) printit("\n");
			break;
		case CLOSE:
			MyConnection.Close();
			break;
		case FASTQUIT: // Thomas Briggs 8/11/98
		case QUIT:
			MyConnection.Close();
			bDone = 1;
			break;
		case HELP:
		case HELP2:
			printm(0, FALSE, MSG_HELP);
			printm(0, FALSE, MSG_HELP_1);
			break;
			// case KEYS: we should never get it
		case K_LOAD:
			if ( i == 1 ) {
				// Ioannou : changed to ini.get_keyfile()
				if(MyConnection.LoadKeyMap( ini.get_keyfile(), Parms[0]) != 1)
					printit("Error loading keymap.\n");
				break;
			};
			if(MyConnection.LoadKeyMap( Parms[1], Parms[0]) != 1)
				printit("Error loading keymap.\n");
			break;
		case K_DISPLAY:
			MyConnection.DisplayKeyMap();
			break;
		case K_SWITCH:
			MyConnection.SwitchKeyMap(atoi(Parms[0]));
			break;
			
			// Paul Brannan 5/30/98
		case SET:
			if(i == 0) {
				printit("Available groups:\n");		// Print out groups
				ini.print_groups();					// (Paul Brannan 9/3/98)
			} else if(i == 1) {
				ini.print_vars(Parms[0]);
			} else if(i >= 2) {
				ini.set_value(Parms[0], Parms[1]);
				// FIX ME !!! Ioannou: here we must call the parser routine for
				// wrap line, not the ini.set_value
				//  something like Parser.ConLineWrap(Wrap_Line);
			}
			break;
			
		case SUSPEND: // Thomas Briggs 8/11/98
			
			// remind the user we're suspended -crn@ozemail.com.au 15/12/98
			extitle = new char[128];
			GetConsoleTitle (extitle, 128);
			
			newtitle = new char[128+sizeof("[suspended]")];
			strcpy(newtitle, extitle);
			strncat(newtitle, "[suspended]", 128+sizeof("[suspended]"));
			if(ini.get_set_title()) SetConsoleTitle (newtitle);
			delete[] newtitle;
			
			if (getenv("comspec") == NULL) {
				switch (GetWin32Version()) {
				case 2:		// 'cmd' is faster than 'command' in NT -crn@ozemail.com.au
					system ("cmd");
					break;
				default:
					system ("command");
					break;
				}
			} else {
				system(getenv("comspec"));
			}
			
			if(ini.get_set_title()) SetConsoleTitle (extitle);
			delete[] extitle;
			///
			
			break;
			
		case CMD_HISTORY:	//crn@ozemail.com.au
			if (cmdhist != NULL) {
				while (cmdhist->prev != NULL)
					cmdhist = cmdhist->prev;	//rewind
				printf ("Command history:\n");
				while (1) {
					printf ("\t%s\n", cmdhist->cmd);
					
					if (cmdhist->next != NULL)
						cmdhist = cmdhist->next;
					else
						break;
				}
			} else
				printf ("No command history available.\n");
			
			break;
			
		case CLEAR_HISTORY:	//crn@ozemail.com.au
			if (cmdhist != NULL) {
				while (cmdhist->next != NULL)
					cmdhist = cmdhist->next;	//fast forward
				while (cmdhist->prev != NULL) {
					cmdhist = cmdhist->prev;
					delete cmdhist->next;
				}
				delete cmdhist;
				cmdhist = NULL;
				printf ("Command history cleared.\n");
			} else
				printf ("No command history available.\n");
			
		case ALIASES: // Paul Brannan 1/1/99
			ini.print_aliases();
			break;
			
		default: // paranoik
			printm(0, FALSE, MSG_INVCMD);
			break;
		}

	}

	return 0;
}

⌨️ 快捷键说明

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