tnconfig.cpp

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

CPP
705
字号
	for(tmp = buffer; *tmp != 0; tmp += strlen(tmp) + 1) {
		int flag = 0;
		for(int j = 0; j < MAX_INI_GROUPS; j++) {
			if(!stricmp(ini_groups[j], tmp)) flag = 1;
		}
		if(!flag) {
			aliases[alias_total] = new char[strlen(tmp)+1];
			strcpy(aliases[alias_total], tmp);
			alias_total++;
		}
	}

	delete[] buffer;
}

void TConfig::print_aliases() {
	for(int j = 0; j < alias_total; j++) {
		char alias_name[20];
		set_string(alias_name, aliases[j], sizeof(alias_name));
		for(unsigned int i = strlen(alias_name); i < sizeof(alias_name) - 1; i++)
			alias_name[i] = ' ';
		alias_name[sizeof(alias_name) - 1] = 0;
		printit(alias_name);
		if((j % 4) == 3) printit("\n");
	}
	printit("\n");
}

bool find_alias(const char *alias_name) {
	return false;
}

void TConfig::print_vars() {
	int j;
	for(j = 0; j < MAX_INI_VARS; j++) {
		if(print_value(ini_varlist[j].name) > 40) printit("\n");
		else if(j % 2) printit("\n");
		else printit("\t");
	}
	if(j % 2) printit("\n");
}

// Paul Brannan 9/3/98
void TConfig::print_vars(char *s) {
	if(!strnicmp(s, "all", 3)) {					// Print out all vars
		print_vars();
		return;
	}

	// See if the group exists
	int j, flag;
	for(j = 0, flag = 0; j < MAX_INI_GROUPS; j++)
		if(!stricmp(ini_groups[j], s)) break;
	// If not, print out the value of the variable by that name
	if(j == MAX_INI_GROUPS) {
		print_value(s);
		printit("\n");
		return;
	}
	
	// Print out the vars in the given group
	int count = 0;
	for(j = 0; j < MAX_INI_VARS; j++) {
		if(ini_varlist[j].section == NULL) continue;
		if(!stricmp(ini_varlist[j].section, s)) {
			if(print_value(ini_varlist[j].name) > 40) printit("\n");
			else if(count % 2) printit("\n");
			else printit("\t");
			count++;
		}
	}
	if(count % 2) printit("\n");
}

// Paul Brannan 9/3/98
void TConfig::print_groups() {
	for(int j = 0; j < MAX_INI_GROUPS; j++) {
		char group_name[20];
		set_string(group_name, ini_groups[j], sizeof(group_name));
		for(unsigned int i = strlen(group_name); i < sizeof(group_name) - 1; i++)
			group_name[i] = ' ';
		group_name[sizeof(group_name) - 1] = 0;
		printit(group_name);
		if((j % 4) == 3) printit("\n");
	}
	printit("\n");
}

// Ioannou : The index in the while causes segfaults if there is no match
// changes to for(), and strcmp to stricmp (prompt gives rong names)

bool TConfig::set_value(const char *var, const char *value) {
   //int j = 0;
   //while(strcmp(var, ini_varlist[j].name) && j < MAX_INI_VARS) j++;
   for (int j = 0; j < MAX_INI_VARS; j++)
   {
      if (stricmp(var, ini_varlist[j].name) == 0)
      {
         switch(ini_varlist[j].data_type) {
            case INI_STRING:
               set_string((char *)ini_varlist[j].ini_data, value,
                  ini_varlist[j].max_size);
               break;
            case INI_INT:
               *(int *)ini_varlist[j].ini_data = atoi(value);
               break;
            case INI_BOOL:
               set_bool((bool *)ini_varlist[j].ini_data, value);
               break;
         }
         // j = MAX_INI_VARS;
		 return TRUE;
      }
   }
   return FALSE;
}

int TConfig::print_value(const char *var) {
	//int j = 0;
	//while(strcmp(var, ini_varlist[j].name) && j < MAX_INI_VARS) j++;
	int Result = 0;
	for (int j = 0; j < MAX_INI_VARS; j++)
	{
		if (stricmp(var, ini_varlist[j].name) == 0)
		{
			char var_name[25];
			set_string(var_name, var, sizeof(var_name));
			for(unsigned int i = strlen(var_name); i < sizeof(var_name) - 1; i++)
				var_name[i] = ' ';
			var_name[sizeof(var_name) - 1] = 0;
			Result = sizeof(var_name);

			printit(var_name);
			printit("\t");
			Result = Result / 8 + 8;
			
			switch(ini_varlist[j].data_type) {
            case INI_STRING:
				printit((char *)ini_varlist[j].ini_data);
				Result += strlen((char *)ini_varlist[j].ini_data);
				break;
            case INI_INT:
				char buffer[20]; // this may not be safe
				// Ioannou : Paul this was _itoa, but Borland needs itoa !!
				itoa(*(int *)ini_varlist[j].ini_data, buffer, 10);
				printit(buffer);
				Result += strlen(buffer);
				break;
            case INI_BOOL:
				if(*(bool *)ini_varlist[j].ini_data == true) {
					printit("on");
					Result += 2;
				} else {
					printit("off");
					Result += 3;
				}
			}
			// printit("\n");
			j = MAX_INI_VARS;
		}
	}
	return Result;
}

void TConfig::init_vars() {
	char buffer[4096];
	for(int j = 0; j < MAX_INI_VARS; j++) {
		if(ini_varlist[j].section != NULL) {
			GetPrivateProfileString(ini_varlist[j].section, ini_varlist[j].name, "",
				buffer, sizeof(buffer), inifile);
			if(*buffer != 0) set_value(ini_varlist[j].name, buffer);
		}
	}
}

void TConfig::inifile_init() {
	// B. K. Oxley 9/16/98	
	char* env_telnet_ini = getenv (ENV_TELNET_INI);
	if (env_telnet_ini && *env_telnet_ini) {
		strncpy (inifile, env_telnet_ini, sizeof(inifile));
		return;
	}

	strcpy(inifile, startdir);
	if (sizeof(inifile) >= strlen(inifile)+strlen("telnet.ini")) {
		strcat(inifile,"telnet.ini"); // add the default filename to the path
	} else {
		// if there is not enough room set the path to nothing
		strcpy(inifile,"");
	}
}

void TConfig::keyfile_init() {
	// check to see if there is a key config file environment variable.
	char *k;
	if ((k = getenv(ENV_TELNET_CFG)) == NULL){
		// if there is no environment variable
		GetPrivateProfileString("Keyboard", "Keyfile", "", keyfile,
			sizeof(keyfile), inifile);
		if(keyfile == 0 || *keyfile == 0) {
			// and there is no profile string
			strcpy(keyfile, startdir);
			if (sizeof(keyfile) >= strlen(keyfile)+strlen("telnet.cfg")) {
				struct stat buf;

				strcat(keyfile,"telnet.cfg"); // add the default filename to the path
				if(stat(keyfile, &buf) != 0) {
					char *s = keyfile + strlen(keyfile) - strlen("telnet.cfg");
					strcpy(s, "keys.cfg");
				}
			} else {
				// if there is not enough room set the path to nothing
				strcpy(keyfile,"");
			}

		// Vassili Bourdo (vassili_bourdo@softhome.net)
		} else {
			// check that keyfile really exists
			if( access(keyfile,04) == -1 ) {
				//it does not...
				char pathbuf[MAX_PATH], *fn;
				//substitute keyfile path with startdir path
				if((fn = strrchr(keyfile,'\\'))) strcpy(keyfile,fn);
					strcat(strcpy(pathbuf,startdir),keyfile);
				//check that startdir\keyfile does exist
				if( access(pathbuf,04) == -1 ) {
					//it does not...
					//so, look for it in all paths
					_searchenv(keyfile, "PATH", pathbuf);
					if( *pathbuf == 0 ) //no luck - revert it to INI file value
						GetPrivateProfileString("Keyboard", "Keyfile", "",
							keyfile, sizeof(keyfile), inifile);
				} else {
					strcpy(keyfile, pathbuf);
				}
			}
		}
		////

	} else {
		// set the keyfile to the value of the environment variable
		strncpy(keyfile, k, sizeof(keyfile));
	}
}

void TConfig::redir_init() {
	// check to see if the environment variable 'TELNET_REDIR' is not 0;
	char* p = getenv(ENV_TELNET_REDIR);
	if (p) {
		input_redir = output_redir = atoi(p);
		if((p = getenv(ENV_INPUT_REDIR))) input_redir = atoi(p);
		if((p = getenv(ENV_OUTPUT_REDIR))) output_redir = atoi(p);
	} else {
		input_redir = output_redir = GetPrivateProfileInt("Terminal",
			"Telnet_Redir", 0, inifile);
		input_redir = GetPrivateProfileInt("Terminal",
			"Input_Redir", input_redir, inifile);
		output_redir = GetPrivateProfileInt("Terminal",
			"Output_Redir", output_redir, inifile);
	}
	if ((input_redir > 1) || (output_redir > 1))
		setlocale(LC_CTYPE,"");
	// tell isprint() to not ignore local characters, if the environment
	// variable "LANG" has a valid value (e.g. LANG=de for german characters)
	// and the file LOCALE.BLL is installed somewhere along the PATH.
}

// Modified not to use getopt() by Paul Brannan 12/17/98
bool TConfig::Process_Params(int argc, char *argv[]) {
	int optind = 1;
	char *optarg = argv[optind];
	char c;

	while(optind < argc) {
		if(argv[optind][0] != '-') break;

		// getopt
		c = argv[optind][1];
		if(argv[optind][2] == 0)
			optarg = argv[++optind];
		else
			optarg = &argv[optind][2];
		optind++;

		switch(c) {
			case 'd':
				set_string(dumpfile, optarg, sizeof(dumpfile));
				printm(0, FALSE, MSG_DUMPFILE, dumpfile);
				break;
			// added support for setting options on the command-line
			// (Paul Brannan 7/31/98)
			case '-':
				{
					int j;
					for(j = 0; optarg[j] != ' ' && optarg[j] != '=' && optarg[j] != 0; j++);
					if(optarg == 0) {
						printm(0, FALSE, MSG_USAGE);		// print a usage message
						printm(0, FALSE, MSG_USAGE_1);
						return FALSE;
					}
					optarg[j] = 0;
					if(!set_value(optarg, &optarg[j+1]))
						printm(0, FALSE, MSG_BADVAL, optarg);
				}
				break;
			default:
				printm(0, FALSE, MSG_USAGE);		// print a usage message
				printm(0, FALSE, MSG_USAGE_1);
				return FALSE;
		}
	}
	if(optind < argc)
		set_string(host, argv[optind++], sizeof(host)-1);
	if(!strnicmp(host, "telnet://", 9)) {
		// we have a URL to parse
		char *s, *t;

		for(s = host+9, t = host; *s != 0; *(t++) = *(s++));
		*t = 0;
		for(s = host; *s != ':' && *s != 0; s++);
		if(*s != 0) {
			*(s++) = 0;
			port = s;
		}
	}		
	if(optind < argc)
		port = argv[optind++];

	return TRUE;
}

void TConfig::set_string(char *dest, const char *src, const int length) {
   int l = length;
   strncpy(dest, src, l);
 //  dest[length-1] = '\0';
 // Ioannou : this messes strings - is this really needed ?
 // The target string, dest, might not be null-terminated
 // if the length of src is length or more.
 // it should be dest[length] = '\0' for strings with length 1
 // (Escape_string etc), but doesn't work with others (like host).
 // dest is long enough to avoid this in all the tested cases
}

// Ioannou : ignore case for true or on

void TConfig::set_bool(bool *boolval, const char *str) {
   if(!stricmp(str, "true")) *boolval = true;
   else if(!stricmp(str, "on")) *boolval = true;
	else *boolval = (bool)atoi(str);
}

⌨️ 快捷键说明

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