tnconfig.cpp
上传用户:tigerk9
上传日期:2020-03-10
资源大小:237k
文件大小:20k
源码类别:

Telnet客户端

开发平台:

Visual C++

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //Telnet Win32 : an ANSI telnet client.
  3. //Copyright (C) 1998-2000 Paul Brannan
  4. //Copyright (C) 1998 I.Ioannou
  5. //Copyright (C) 1997 Brad Johnson
  6. //
  7. //This program is free software; you can redistribute it and/or
  8. //modify it under the terms of the GNU General Public License
  9. //as published by the Free Software Foundation; either version 2
  10. //of the License, or (at your option) any later version.
  11. //
  12. //This program is distributed in the hope that it will be useful,
  13. //but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. //GNU General Public License for more details.
  16. //
  17. //You should have received a copy of the GNU General Public License
  18. //along with this program; if not, write to the Free Software
  19. //Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. //
  21. //I.Ioannou
  22. //roryt@hol.gr
  23. //
  24. ///////////////////////////////////////////////////////////////////////////
  25. // tnconfig.cpp
  26. // Written by Paul Brannan <pbranna@clemson.edu>
  27. // Last modified August 30, 1998
  28. //
  29. // This is a class designed for use with Brad Johnson's Console Telnet
  30. // see the file tnconfig.h for more information
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <locale.h>
  34. #include <memory.h>
  35. #include <io.h>
  36. #include <sys/types.h>
  37. #include <sys/stat.h>
  38. #include "tnconfig.h"
  39. // Turn off the "forcing value to bool 'true' or 'false'" warning
  40. #ifdef _MSC_VER
  41. #pragma warning(disable: 4800)
  42. #endif
  43. // This is the ini variable that is used for everybody
  44. TConfig ini;
  45. TConfig::TConfig() {
  46. // set all default values
  47. startdir[0] = '';
  48. keyfile[0] = '';
  49. inifile[0] = '';
  50. dumpfile[0] = '';
  51. term[0] = '';
  52. default_config[0] = '';
  53. strcpy(printer_name, "LPT1");
  54. input_redir = 0;
  55. output_redir = 0;
  56. strip_redir = FALSE;
  57. dstrbksp = FALSE;
  58. eightbit_ansi = FALSE;
  59. vt100_mode = FALSE;
  60. disable_break = FALSE;
  61. speaker_beep = TRUE;
  62. do_beep = TRUE;
  63. preserve_colors = FALSE;
  64. wrapline = TRUE;
  65. lock_linewrap = FALSE;
  66. fast_write = TRUE;
  67. enable_mouse = TRUE;
  68. alt_erase = FALSE;
  69. wide_enable = FALSE;
  70. keyboard_paste = FALSE;
  71. set_title = TRUE;
  72. blink_bg = -1;
  73. blink_fg = 2;
  74. underline_bg = -1;
  75. underline_fg = 3;
  76. ulblink_bg = -1;
  77. ulblink_fg = 1;
  78. normal_bg = -1;
  79. normal_fg = -1;
  80. scroll_bg = 0;
  81. scroll_fg = 7;
  82. status_bg = 1;
  83. status_fg = 15;
  84. buffer_size = 2048;
  85. term_width = -1;
  86. term_height = -1;
  87. window_width = -1;
  88. window_height = -1;
  89. strcpy(escape_key, "]");
  90. strcpy(scrollback_key, "[");
  91. strcpy(dial_key, "\");
  92. strcpy(default_config, "ANSI");
  93. strcpy(term, "ansi");
  94. strcpy(scroll_mode, "DUMP");
  95. scroll_size=32000;
  96. scroll_enable=TRUE;
  97. host[0] = '';
  98. port = "23";
  99. init_varlist();
  100. aliases = NULL;
  101. }
  102. TConfig::~TConfig() {
  103. if(aliases) {
  104. for(int j = 0; j < alias_total; j++) delete[] aliases[j];
  105. delete[] aliases;
  106. }
  107. }
  108. enum ini_data_type {
  109. INI_STRING,
  110. INI_INT,
  111. INI_BOOL
  112. };
  113. enum {
  114. INIFILE,
  115. KEYFILE,
  116. DUMPFILE,
  117. DEFAULT_CONFIG,
  118. TERM,
  119. INPUT_REDIR,
  120. OUTPUT_REDIR,
  121. STRIP_REDIR,
  122. DSTRBKSP,
  123. EIGHTBIT_ANSI,
  124. VT100_MODE,
  125. DISABLE_BREAK,
  126. SPEAKER_BEEP,
  127. DO_BEEP,
  128. PRESERVE_COLORS,
  129. WRAP_LINE,
  130. LOCK_LINEWRAP,
  131. FAST_WRITE,
  132. TERM_WIDTH,
  133. TERM_HEIGHT,
  134. WINDOW_WIDTH,
  135. WINDOW_HEIGHT,
  136. WIDE_ENABLE,
  137. CTRLBREAK_AS_CTRLC,
  138. BUFFER_SIZE,
  139. SET_TITLE,
  140. BLINK_BG,
  141. BLINK_FG,
  142. UNDERLINE_BG,
  143. UNDERLINE_FG,
  144. ULBLINK_BG,
  145. ULBLINK_FG,
  146. NORMAL_BG,
  147. NORMAL_FG,
  148. SCROLL_BG,
  149. SCROLL_FG,
  150. STATUS_BG,
  151. STATUS_FG,
  152. PRINTER_NAME,
  153. ENABLE_MOUSE,
  154. ESCAPE_KEY,
  155. SCROLLBACK_KEY,
  156. DIAL_KEY,
  157. ALT_ERASE,
  158. KEYBOARD_PASTE,
  159. SCROLL_MODE,
  160. SCROLL_SIZE,
  161. SCROLL_ENABLE,
  162. SCRIPTNAME,
  163. SCRIPT_ENABLE,
  164. NETPIPE,
  165. IOPIPE,
  166. MAX_INI_VARS // must be last
  167. };
  168. struct ini_variable {
  169. char *name; // variable name
  170. char *section; // name of ini file section the variable is in
  171. enum ini_data_type data_type; // type of data
  172. void *ini_data; // pointer to data
  173. int  max_size; // max size if string
  174. };
  175. // Note: default values are set in the constructor, TConfig()
  176. ini_variable ini_varlist[MAX_INI_VARS];
  177. enum {
  178. KEYBOARD,
  179. TERMINAL,
  180. COLORS,
  181. MOUSE,
  182. PRINTER,
  183. SCROLLBACK,
  184. SCRIPTING,
  185. PIPES,
  186. MAX_INI_GROUPS // Must be last
  187. };
  188. char *ini_groups[MAX_INI_GROUPS];
  189. void TConfig::init_varlist() {
  190. static const ini_variable static_ini_varlist[MAX_INI_VARS] = {
  191. {"Inifile", NULL, INI_STRING, &inifile, sizeof(inifile)},
  192. {"Keyfile", "Keyboard", INI_STRING, &keyfile, sizeof(keyfile)},
  193. {"Dumpfile", "Terminal", INI_STRING, &dumpfile, sizeof(dumpfile)},
  194. {"Default_Config","Keyboard",   INI_STRING, &default_config, sizeof(default_config)},
  195. {"Term", "Terminal", INI_STRING, &term, sizeof(term)},
  196. {"Input_Redir", "Terminal", INI_INT, &input_redir, 0},
  197. {"Output_Redir","Terminal", INI_INT, &output_redir, 0},
  198. {"Strip_Redir", "Terminal", INI_BOOL, &strip_redir, 0},
  199. {"Destructive_Backspace","Terminal",INI_BOOL, &dstrbksp, 0},
  200. {"EightBit_Ansi","Terminal", INI_BOOL, &eightbit_ansi, 0},
  201. {"VT100_Mode", "Terminal", INI_BOOL, &vt100_mode, 0},
  202. {"Disable_Break","Terminal", INI_BOOL, &disable_break, 0},
  203. {"Speaker_Beep","Terminal", INI_BOOL, &speaker_beep, 0},
  204. {"Beep", "Terminal", INI_BOOL, &do_beep, 0},
  205. {"Preserve_Colors","Terminal", INI_BOOL, &preserve_colors, 0},
  206. {"Wrap_Line", "Terminal", INI_BOOL, &wrapline, 0},
  207. {"Lock_linewrap","Terminal", INI_BOOL, &lock_linewrap, 0},
  208. {"Fast_Write", "Terminal", INI_BOOL, &fast_write, 0},
  209. {"Term_Width", "Terminal", INI_INT, &term_width, 0},
  210. {"Term_Height", "Terminal", INI_INT, &term_height, 0},
  211. {"Window_Width","Terminal", INI_INT, &window_width, 0},
  212. {"Window_Height","Terminal", INI_INT, &window_height, 0},
  213. {"Wide_Enable", "Terminal", INI_BOOL, &wide_enable, 0},
  214. {"Ctrlbreak_as_Ctrlc","Keyboard", INI_BOOL, &ctrlbreak_as_ctrlc, 0},
  215. {"Buffer_Size", "Terminal", INI_INT, &buffer_size, 0},
  216. {"Set_Title", "Terminal", INI_BOOL, &set_title, 0},
  217. {"Blink_bg", "Colors", INI_INT, &blink_bg, 0},
  218. {"Blink_fg", "Colors", INI_INT, &blink_fg, 0},
  219. {"Underline_bg","Colors", INI_INT, &underline_bg, 0},
  220. {"Underline_fg","Colors", INI_INT, &underline_fg, 0},
  221. {"UlBlink_bg", "Colors", INI_INT, &ulblink_bg, 0},
  222. {"UlBlink_fg", "Colors", INI_INT, &ulblink_fg, 0},
  223. {"Normal_bg", "Colors", INI_INT, &normal_bg, 0},
  224. {"Normal_fg", "Colors", INI_INT, &normal_fg, 0},
  225. {"Scroll_bg", "Colors", INI_INT, &scroll_bg, 0},
  226. {"Scroll_fg", "Colors", INI_INT, &scroll_fg, 0},
  227. {"Status_bg", "Colors", INI_INT, &status_bg, 0},
  228. {"Status_fg", "Colors", INI_INT, &status_fg, 0},
  229. {"Enable_Mouse","Mouse", INI_BOOL, &enable_mouse, 0},
  230. {"Printer_Name","Printer", INI_STRING, &printer_name, sizeof(printer_name)},
  231. {"Escape_Key", "Keyboard", INI_STRING, &escape_key, 1},
  232. {"Scrollback_Key","Keyboard", INI_STRING, &scrollback_key, 1},
  233. {"Dial_Key", "Keyboard", INI_STRING, &dial_key, 1},
  234. {"Alt_Erase", "Keyboard", INI_BOOL, &alt_erase, 0},
  235. {"Keyboard_Paste","Keyboard", INI_BOOL, &keyboard_paste, 0},
  236. {"Scroll_Mode", "Scrollback", INI_STRING, &scroll_mode, sizeof(scroll_mode)},
  237. {"Scroll_Size", "Scrollback", INI_INT, &scroll_size, 0},
  238. {"Scroll_Enable","Scrollback", INI_BOOL, &scroll_enable, 0},
  239. {"Scriptname", "Scripting", INI_STRING, &scriptname, sizeof(scriptname)},
  240. {"Script_enable","Scripting", INI_BOOL, &script_enable, 0},
  241. {"Netpipe", "Pipes", INI_STRING, &netpipe, sizeof(netpipe)},
  242. {"Iopipe", "Pipes", INI_STRING, &iopipe, sizeof(iopipe)}
  243. };
  244. static const char *static_ini_groups[MAX_INI_GROUPS] = {
  245. "Keyboard",
  246. "Terminal",
  247. "Colors",
  248. "Mouse",
  249. "Printer",
  250. "Scrollback",
  251. "Scripting",
  252. "Pipes"
  253. };
  254. memcpy(ini_varlist, static_ini_varlist, sizeof(ini_varlist));
  255. memcpy(ini_groups, static_ini_groups, sizeof(ini_groups));
  256. }
  257. void TConfig::init(char *dirname, char *execname) {
  258. // Copy temporary dirname to permanent startdir
  259. strncpy(startdir, dirname, sizeof(startdir));
  260. startdir[sizeof(startdir) - 1] = 0;
  261. // Copy temp execname to permanent exename (Thomas Briggs 12/7/98)
  262. strncpy(exename, execname, sizeof(exename));
  263. exename[sizeof(exename) - 1] = 0;
  264. // Initialize INI file
  265. inifile_init();
  266. // Initialize redir
  267. // Note that this must be done early, so error messages will be printed
  268. // properly
  269. redir_init();
  270. // Initialize aliases (Paul Brannan 1/1/99)
  271. init_aliases();
  272. // Make sure the file that we're trying to work with exists
  273. int iResult = access(inifile, 04);
  274. // Thomas Briggs 9/14/98
  275. if( iResult == 0 )
  276. // Tell the user what file we are reading
  277. // We cannot print any messages before initializing telnet_redir
  278. printm(0, FALSE, MSG_CONFIG, inifile);
  279. else
  280. // Tell the user that the file doesn't exist, but later read the
  281. // file anyway simply to populate the defaults
  282. printm(0, FALSE, MSG_NOINI, inifile);
  283. init_vars(); // Initialize misc. vars
  284. keyfile_init(); // Initialize keyfile
  285. }
  286. // Alias support (Paul Brannan 1/1/99)
  287. void TConfig::init_aliases() {
  288. char *buffer;
  289. alias_total = 0;
  290. // Find the correct buffer size
  291. // FIX ME!! some implementations of Mingw32 don't have a
  292. // GetPrivateProfileSecionNames function.  What do we do about this?
  293. #ifndef __MINGW32__
  294. {
  295. int size=1024, Result = 0;
  296. for(;;) {
  297. buffer = new char[size];
  298. Result = GetPrivateProfileSectionNames(buffer, size, inifile);
  299. if(Result < size - 2) break;
  300. size *= 2;
  301. delete[] buffer;
  302. }
  303. }
  304. #else
  305. return;
  306. #endif
  307. // Find the maximum number of aliases
  308. int max = 0;
  309. char *tmp;
  310. for(tmp = buffer; *tmp != 0; tmp += strlen(tmp) + 1)
  311. max++;
  312. aliases = new char*[max];
  313. // Load the aliases into an array
  314. for(tmp = buffer; *tmp != 0; tmp += strlen(tmp) + 1) {
  315. int flag = 0;
  316. for(int j = 0; j < MAX_INI_GROUPS; j++) {
  317. if(!stricmp(ini_groups[j], tmp)) flag = 1;
  318. }
  319. if(!flag) {
  320. aliases[alias_total] = new char[strlen(tmp)+1];
  321. strcpy(aliases[alias_total], tmp);
  322. alias_total++;
  323. }
  324. }
  325. delete[] buffer;
  326. }
  327. void TConfig::print_aliases() {
  328. for(int j = 0; j < alias_total; j++) {
  329. char alias_name[20];
  330. set_string(alias_name, aliases[j], sizeof(alias_name));
  331. for(unsigned int i = strlen(alias_name); i < sizeof(alias_name) - 1; i++)
  332. alias_name[i] = ' ';
  333. alias_name[sizeof(alias_name) - 1] = 0;
  334. printit(alias_name);
  335. if((j % 4) == 3) printit("n");
  336. }
  337. printit("n");
  338. }
  339. bool find_alias(const char *alias_name) {
  340. return false;
  341. }
  342. void TConfig::print_vars() {
  343. int j;
  344. for(j = 0; j < MAX_INI_VARS; j++) {
  345. if(print_value(ini_varlist[j].name) > 40) printit("n");
  346. else if(j % 2) printit("n");
  347. else printit("t");
  348. }
  349. if(j % 2) printit("n");
  350. }
  351. // Paul Brannan 9/3/98
  352. void TConfig::print_vars(char *s) {
  353. if(!strnicmp(s, "all", 3)) { // Print out all vars
  354. print_vars();
  355. return;
  356. }
  357. // See if the group exists
  358. int j, flag;
  359. for(j = 0, flag = 0; j < MAX_INI_GROUPS; j++)
  360. if(!stricmp(ini_groups[j], s)) break;
  361. // If not, print out the value of the variable by that name
  362. if(j == MAX_INI_GROUPS) {
  363. print_value(s);
  364. printit("n");
  365. return;
  366. }
  367. // Print out the vars in the given group
  368. int count = 0;
  369. for(j = 0; j < MAX_INI_VARS; j++) {
  370. if(ini_varlist[j].section == NULL) continue;
  371. if(!stricmp(ini_varlist[j].section, s)) {
  372. if(print_value(ini_varlist[j].name) > 40) printit("n");
  373. else if(count % 2) printit("n");
  374. else printit("t");
  375. count++;
  376. }
  377. }
  378. if(count % 2) printit("n");
  379. }
  380. // Paul Brannan 9/3/98
  381. void TConfig::print_groups() {
  382. for(int j = 0; j < MAX_INI_GROUPS; j++) {
  383. char group_name[20];
  384. set_string(group_name, ini_groups[j], sizeof(group_name));
  385. for(unsigned int i = strlen(group_name); i < sizeof(group_name) - 1; i++)
  386. group_name[i] = ' ';
  387. group_name[sizeof(group_name) - 1] = 0;
  388. printit(group_name);
  389. if((j % 4) == 3) printit("n");
  390. }
  391. printit("n");
  392. }
  393. // Ioannou : The index in the while causes segfaults if there is no match
  394. // changes to for(), and strcmp to stricmp (prompt gives rong names)
  395. bool TConfig::set_value(const char *var, const char *value) {
  396.    //int j = 0;
  397.    //while(strcmp(var, ini_varlist[j].name) && j < MAX_INI_VARS) j++;
  398.    for (int j = 0; j < MAX_INI_VARS; j++)
  399.    {
  400.       if (stricmp(var, ini_varlist[j].name) == 0)
  401.       {
  402.          switch(ini_varlist[j].data_type) {
  403.             case INI_STRING:
  404.                set_string((char *)ini_varlist[j].ini_data, value,
  405.                   ini_varlist[j].max_size);
  406.                break;
  407.             case INI_INT:
  408.                *(int *)ini_varlist[j].ini_data = atoi(value);
  409.                break;
  410.             case INI_BOOL:
  411.                set_bool((bool *)ini_varlist[j].ini_data, value);
  412.                break;
  413.          }
  414.          // j = MAX_INI_VARS;
  415.  return TRUE;
  416.       }
  417.    }
  418.    return FALSE;
  419. }
  420. int TConfig::print_value(const char *var) {
  421. //int j = 0;
  422. //while(strcmp(var, ini_varlist[j].name) && j < MAX_INI_VARS) j++;
  423. int Result = 0;
  424. for (int j = 0; j < MAX_INI_VARS; j++)
  425. {
  426. if (stricmp(var, ini_varlist[j].name) == 0)
  427. {
  428. char var_name[25];
  429. set_string(var_name, var, sizeof(var_name));
  430. for(unsigned int i = strlen(var_name); i < sizeof(var_name) - 1; i++)
  431. var_name[i] = ' ';
  432. var_name[sizeof(var_name) - 1] = 0;
  433. Result = sizeof(var_name);
  434. printit(var_name);
  435. printit("t");
  436. Result = Result / 8 + 8;
  437. switch(ini_varlist[j].data_type) {
  438.             case INI_STRING:
  439. printit((char *)ini_varlist[j].ini_data);
  440. Result += strlen((char *)ini_varlist[j].ini_data);
  441. break;
  442.             case INI_INT:
  443. char buffer[20]; // this may not be safe
  444. // Ioannou : Paul this was _itoa, but Borland needs itoa !!
  445. itoa(*(int *)ini_varlist[j].ini_data, buffer, 10);
  446. printit(buffer);
  447. Result += strlen(buffer);
  448. break;
  449.             case INI_BOOL:
  450. if(*(bool *)ini_varlist[j].ini_data == true) {
  451. printit("on");
  452. Result += 2;
  453. } else {
  454. printit("off");
  455. Result += 3;
  456. }
  457. }
  458. // printit("n");
  459. j = MAX_INI_VARS;
  460. }
  461. }
  462. return Result;
  463. }
  464. void TConfig::init_vars() {
  465. char buffer[4096];
  466. for(int j = 0; j < MAX_INI_VARS; j++) {
  467. if(ini_varlist[j].section != NULL) {
  468. GetPrivateProfileString(ini_varlist[j].section, ini_varlist[j].name, "",
  469. buffer, sizeof(buffer), inifile);
  470. if(*buffer != 0) set_value(ini_varlist[j].name, buffer);
  471. }
  472. }
  473. }
  474. void TConfig::inifile_init() {
  475. // B. K. Oxley 9/16/98
  476. char* env_telnet_ini = getenv (ENV_TELNET_INI);
  477. if (env_telnet_ini && *env_telnet_ini) {
  478. strncpy (inifile, env_telnet_ini, sizeof(inifile));
  479. return;
  480. }
  481. strcpy(inifile, startdir);
  482. if (sizeof(inifile) >= strlen(inifile)+strlen("telnet.ini")) {
  483. strcat(inifile,"telnet.ini"); // add the default filename to the path
  484. } else {
  485. // if there is not enough room set the path to nothing
  486. strcpy(inifile,"");
  487. }
  488. }
  489. void TConfig::keyfile_init() {
  490. // check to see if there is a key config file environment variable.
  491. char *k;
  492. if ((k = getenv(ENV_TELNET_CFG)) == NULL){
  493. // if there is no environment variable
  494. GetPrivateProfileString("Keyboard", "Keyfile", "", keyfile,
  495. sizeof(keyfile), inifile);
  496. if(keyfile == 0 || *keyfile == 0) {
  497. // and there is no profile string
  498. strcpy(keyfile, startdir);
  499. if (sizeof(keyfile) >= strlen(keyfile)+strlen("telnet.cfg")) {
  500. struct stat buf;
  501. strcat(keyfile,"telnet.cfg"); // add the default filename to the path
  502. if(stat(keyfile, &buf) != 0) {
  503. char *s = keyfile + strlen(keyfile) - strlen("telnet.cfg");
  504. strcpy(s, "keys.cfg");
  505. }
  506. } else {
  507. // if there is not enough room set the path to nothing
  508. strcpy(keyfile,"");
  509. }
  510. // Vassili Bourdo (vassili_bourdo@softhome.net)
  511. } else {
  512. // check that keyfile really exists
  513. if( access(keyfile,04) == -1 ) {
  514. //it does not...
  515. char pathbuf[MAX_PATH], *fn;
  516. //substitute keyfile path with startdir path
  517. if((fn = strrchr(keyfile,'\'))) strcpy(keyfile,fn);
  518. strcat(strcpy(pathbuf,startdir),keyfile);
  519. //check that startdirkeyfile does exist
  520. if( access(pathbuf,04) == -1 ) {
  521. //it does not...
  522. //so, look for it in all paths
  523. _searchenv(keyfile, "PATH", pathbuf);
  524. if( *pathbuf == 0 ) //no luck - revert it to INI file value
  525. GetPrivateProfileString("Keyboard", "Keyfile", "",
  526. keyfile, sizeof(keyfile), inifile);
  527. } else {
  528. strcpy(keyfile, pathbuf);
  529. }
  530. }
  531. }
  532. ////
  533. } else {
  534. // set the keyfile to the value of the environment variable
  535. strncpy(keyfile, k, sizeof(keyfile));
  536. }
  537. }
  538. void TConfig::redir_init() {
  539. // check to see if the environment variable 'TELNET_REDIR' is not 0;
  540. char* p = getenv(ENV_TELNET_REDIR);
  541. if (p) {
  542. input_redir = output_redir = atoi(p);
  543. if((p = getenv(ENV_INPUT_REDIR))) input_redir = atoi(p);
  544. if((p = getenv(ENV_OUTPUT_REDIR))) output_redir = atoi(p);
  545. } else {
  546. input_redir = output_redir = GetPrivateProfileInt("Terminal",
  547. "Telnet_Redir", 0, inifile);
  548. input_redir = GetPrivateProfileInt("Terminal",
  549. "Input_Redir", input_redir, inifile);
  550. output_redir = GetPrivateProfileInt("Terminal",
  551. "Output_Redir", output_redir, inifile);
  552. }
  553. if ((input_redir > 1) || (output_redir > 1))
  554. setlocale(LC_CTYPE,"");
  555. // tell isprint() to not ignore local characters, if the environment
  556. // variable "LANG" has a valid value (e.g. LANG=de for german characters)
  557. // and the file LOCALE.BLL is installed somewhere along the PATH.
  558. }
  559. // Modified not to use getopt() by Paul Brannan 12/17/98
  560. bool TConfig::Process_Params(int argc, char *argv[]) {
  561. int optind = 1;
  562. char *optarg = argv[optind];
  563. char c;
  564. while(optind < argc) {
  565. if(argv[optind][0] != '-') break;
  566. // getopt
  567. c = argv[optind][1];
  568. if(argv[optind][2] == 0)
  569. optarg = argv[++optind];
  570. else
  571. optarg = &argv[optind][2];
  572. optind++;
  573. switch(c) {
  574. case 'd':
  575. set_string(dumpfile, optarg, sizeof(dumpfile));
  576. printm(0, FALSE, MSG_DUMPFILE, dumpfile);
  577. break;
  578. // added support for setting options on the command-line
  579. // (Paul Brannan 7/31/98)
  580. case '-':
  581. {
  582. int j;
  583. for(j = 0; optarg[j] != ' ' && optarg[j] != '=' && optarg[j] != 0; j++);
  584. if(optarg == 0) {
  585. printm(0, FALSE, MSG_USAGE); // print a usage message
  586. printm(0, FALSE, MSG_USAGE_1);
  587. return FALSE;
  588. }
  589. optarg[j] = 0;
  590. if(!set_value(optarg, &optarg[j+1]))
  591. printm(0, FALSE, MSG_BADVAL, optarg);
  592. }
  593. break;
  594. default:
  595. printm(0, FALSE, MSG_USAGE); // print a usage message
  596. printm(0, FALSE, MSG_USAGE_1);
  597. return FALSE;
  598. }
  599. }
  600. if(optind < argc)
  601. set_string(host, argv[optind++], sizeof(host)-1);
  602. if(!strnicmp(host, "telnet://", 9)) {
  603. // we have a URL to parse
  604. char *s, *t;
  605. for(s = host+9, t = host; *s != 0; *(t++) = *(s++));
  606. *t = 0;
  607. for(s = host; *s != ':' && *s != 0; s++);
  608. if(*s != 0) {
  609. *(s++) = 0;
  610. port = s;
  611. }
  612. }
  613. if(optind < argc)
  614. port = argv[optind++];
  615. return TRUE;
  616. }
  617. void TConfig::set_string(char *dest, const char *src, const int length) {
  618.    int l = length;
  619.    strncpy(dest, src, l);
  620.  //  dest[length-1] = '';
  621.  // Ioannou : this messes strings - is this really needed ?
  622.  // The target string, dest, might not be null-terminated
  623.  // if the length of src is length or more.
  624.  // it should be dest[length] = '' for strings with length 1
  625.  // (Escape_string etc), but doesn't work with others (like host).
  626.  // dest is long enough to avoid this in all the tested cases
  627. }
  628. // Ioannou : ignore case for true or on
  629. void TConfig::set_bool(bool *boolval, const char *str) {
  630.    if(!stricmp(str, "true")) *boolval = true;
  631.    else if(!stricmp(str, "on")) *boolval = true;
  632. else *boolval = (bool)atoi(str);
  633. }