Example #1
0
void VNCOptions::SetFromCommandLine(LPTSTR szCmdLine) {
  // We assume no quoting here.
  // Copy the command line - we don't know what might happen to the original
  config_specified=false;
  int cmdlinelen = _tcslen(szCmdLine);
  if (cmdlinelen == 0) return;
	
  TCHAR *cmd = new TCHAR[cmdlinelen + 1];
  _tcscpy(cmd, szCmdLine);
	
	// Count the number of spaces
	// This may be more than the number of arguments, but that doesn't matter.
  int nspaces = 0;
  TCHAR *p = cmd;
  TCHAR *pos = cmd;
  while ( ( pos = _tcschr(p, ' ') ) != NULL ) {
    nspaces ++;
    p = pos + 1;
  }
	
  // Create the array to hold pointers to each bit of string
  TCHAR **args = new LPTSTR[nspaces + 1];
	
  // replace spaces with nulls and
  // create an array of TCHAR*'s which points to start of each bit.
  pos = cmd;
  int i = 0;
  args[i] = cmd;
  bool inquote=false;
  for (pos = cmd; *pos != 0; pos++) {
    // Arguments are normally separated by spaces, unless there's quoting
    if ((*pos == ' ') && !inquote) {
      *pos = '\0';
      p = pos + 1;
      args[++i] = p;
    }
    if (*pos == '"') {  
      if (!inquote) {      // Are we starting a quoted argument?
        args[i] = ++pos; // It starts just after the quote
      } else {
        *pos = '\0';     // Finish a quoted argument?
      }
      inquote = !inquote;
    }
  }
  i++;

  bool hostGiven = false, portGiven = false;
  // take in order.
  for (int j = 0; j < i; j++) {
    if ( SwitchMatch(args[j], _T("help")) ||
         SwitchMatch(args[j], _T("?")) ||
         SwitchMatch(args[j], _T("h")))
	{
	  m_NoStatus = true;
      ShowUsage();
      exit(1);
    }
	else if ( SwitchMatch(args[j], _T("listen")))
	{
      m_listening = true;
      if (j+1 < i && args[j+1][0] >= '0' && args[j+1][0] <= '9') {
        if (_stscanf(args[j+1], _T("%d"), &m_listenPort) != 1) {
          ArgError(sz_D3);
          continue;
        }
        j++;
      }
    } else if (SwitchMatch(args[j], _T("fttimeout"))) { //PGM @ Advantig
      if (j+1 < i && args[j+1][0] >= '0' && args[j+1][0] <= '9') {
        if (_stscanf(args[j+1], _T("%d"), &m_FTTimeout) != 1) {
          ArgError(sz_D3);
          continue;
        }
        if (m_FTTimeout > 60)
            m_FTTimeout = 60;
        j++;
      }
    }  else if (SwitchMatch(args[j], _T("keepalive"))) { //PGM @ Advantig
      if (j+1 < i && args[j+1][0] >= '0' && args[j+1][0] <= '9') {
        if (_stscanf(args[j+1], _T("%d"), &m_keepAliveInterval) != 1) {
          ArgError(sz_D3);
          continue;
        }
        if (m_keepAliveInterval >= (m_FTTimeout - KEEPALIVE_HEADROOM))
          m_keepAliveInterval = (m_FTTimeout - KEEPALIVE_HEADROOM); 
        j++;
      }
    } else if ( SwitchMatch(args[j], _T("askexit"))) { //PGM @ Advantig
      m_fExitCheck = true; //PGM @ Advantig
    } else if ( SwitchMatch(args[j], _T("restricted"))) {
      m_restricted = true;
    } else if ( SwitchMatch(args[j], _T("viewonly"))) {
      m_ViewOnly = true;
	} else if ( SwitchMatch(args[j], _T("nostatus"))) {
      m_NoStatus = true;
	} else if ( SwitchMatch(args[j], _T("nohotkeys"))) {
      m_NoHotKeys = true;
    } else if ( SwitchMatch(args[j], _T("notoolbar"))) {
      m_ShowToolbar = false;
    } else if ( SwitchMatch(args[j], _T("autoscaling"))) {
      m_fAutoScaling = true;
    } else if ( SwitchMatch(args[j], _T("fullscreen"))) {
      m_FullScreen = true;
    } else if ( SwitchMatch(args[j], _T("noauto"))) {
      autoDetect = false;
	  m_quickoption = 0;
    } else if ( SwitchMatch(args[j], _T("8bit"))) {
      m_Use8Bit = rfbPF256Colors; //true;
	} else if ( SwitchMatch(args[j], _T("256colors"))) {
      m_Use8Bit = rfbPF256Colors; //true;
    } else if ( SwitchMatch(args[j], _T("fullcolors"))) {
      m_Use8Bit = rfbPFFullColors;
    } else if ( SwitchMatch(args[j], _T("64colors"))) {
      m_Use8Bit = rfbPF64Colors;
    } else if ( SwitchMatch(args[j], _T("8colors"))) {
      m_Use8Bit = rfbPF8Colors;
    } else if ( SwitchMatch(args[j], _T("8greycolors"))) {
      m_Use8Bit = rfbPF8GreyColors;
    } else if ( SwitchMatch(args[j], _T("4greycolors"))) {
      m_Use8Bit = rfbPF4GreyColors;
    } else if ( SwitchMatch(args[j], _T("2greycolors"))) {
      m_Use8Bit = rfbPF2GreyColors;
    } else if ( SwitchMatch(args[j], _T("shared"))) {
      m_Shared = true;
    } else if ( SwitchMatch(args[j], _T("swapmouse"))) {
      m_SwapMouse = true;
    } else if ( SwitchMatch(args[j], _T("nocursor"))) {
      m_localCursor = NOCURSOR;
    } else if ( SwitchMatch(args[j], _T("dotcursor"))) {
      m_localCursor = DOTCURSOR;
    } else if ( SwitchMatch(args[j], _T("normalcursor"))) {
      m_localCursor = NORMALCURSOR;
    } else if ( SwitchMatch(args[j], _T("belldeiconify") )) {
      m_DeiconifyOnBell = true;
    } else if ( SwitchMatch(args[j], _T("emulate3") )) {
      m_Emul3Buttons = true;
	} else if ( SwitchMatch(args[j], _T("JapKeyboard") )) {
      m_JapKeyboard = true;
    } else if ( SwitchMatch(args[j], _T("noemulate3") )) {
      m_Emul3Buttons = false;
	} else if ( SwitchMatch(args[j], _T("nocursorshape") )) {
			m_requestShapeUpdates = false;
	} else if ( SwitchMatch(args[j], _T("noremotecursor") )) {
			m_requestShapeUpdates = true;
			m_ignoreShapeUpdates = true;
    } else if ( SwitchMatch(args[j], _T("scale") )) {
      if (++j == i) {
        ArgError(sz_D4);
        continue;
      }
      int numscales = _stscanf(args[j], _T("%d/%d"), &m_scale_num, &m_scale_den);
      if (numscales < 1) {
        ArgError(sz_D5);
        continue;
      }
      if (numscales == 1) 
        m_scale_den = 1; // needed if you're overriding a previous setting

    } else if ( SwitchMatch(args[j], _T("emulate3timeout") )) {
      if (++j == i) {
        ArgError(sz_D6);
        continue;
      }
      if (_stscanf(args[j], _T("%d"), &m_Emul3Timeout) != 1) {
        ArgError(sz_D7);
        continue;
      }
			
    } else if ( SwitchMatch(args[j], _T("emulate3fuzz") )) {
      if (++j == i) {
        ArgError(sz_D8);
        continue;
      }
      if (_stscanf(args[j], _T("%d"), &m_Emul3Fuzz) != 1) {
        ArgError(sz_D9);
        continue;
      }
			
    } else if ( SwitchMatch(args[j], _T("disableclipboard") )) {
      m_DisableClipboard = true;
    }
#ifdef UNDER_CE
    // Manual setting of palm vs hpc aspect ratio for dialog boxes.
    else if ( SwitchMatch(args[j], _T("hpc") )) {
      m_palmpc = false;
    } else if ( SwitchMatch(args[j], _T("palm") )) {
      m_palmpc = true;
    } else if ( SwitchMatch(args[j], _T("slow") )) {
      m_slowgdi = true;
    } 
#endif
    else if ( SwitchMatch(args[j], _T("delay") )) {
      if (++j == i) {
        ArgError(sz_D10);
        continue;
      }
      if (_stscanf(args[j], _T("%d"), &m_delay) != 1) {
        ArgError(sz_D11);
        continue;
      }
			
    } else if ( SwitchMatch(args[j], _T("loglevel") )) {
      if (++j == i) {
        ArgError(sz_D12);
        continue;
      }
      if (_stscanf(args[j], _T("%d"), &m_logLevel) != 1) {
        ArgError(sz_D13);
        continue;
      }
			
    } else if ( SwitchMatch(args[j], _T("console") )) {
      m_logToConsole = true;
    } else if ( SwitchMatch(args[j], _T("logfile") )) {
      if (++j == i) {
        ArgError(sz_D14);
        continue;
      }
      if (_stscanf(args[j], _T("%s"), m_logFilename) != 1) {
        ArgError(sz_D15);
        continue;
      } else {
        m_logToFile = true;
      }
    } else if ( SwitchMatch(args[j], _T("config") )) {
      if (++j == i) {
        ArgError(sz_D16);
        continue;
      }
	  else
	  {
		  config_specified=true;
	  }
      // The GetPrivateProfile* stuff seems not to like some relative paths
      _fullpath(m_configFilename, args[j], _MAX_PATH);
      if (_access(m_configFilename, 04)) {
        ArgError(sz_D17);
        PostQuitMessage(1);
        continue;
      } else {
        Load(m_configFilename);
        m_configSpecified = true;
      }
    } else if ( SwitchMatch(args[j], _T("register") )) {
      Register();
      PostQuitMessage(0);
	
	}
	else if ( SwitchMatch(args[j], _T("encoding") )) {
			if (++j == i) {
				ArgError(sz_D18);
				continue;
			}
			int enc = -1;
			if (_tcsicmp(args[j], _T("raw")) == 0) {
				enc = rfbEncodingRaw;
			} else if (_tcsicmp(args[j], _T("rre")) == 0) {
				enc = rfbEncodingRRE;
			} else if (_tcsicmp(args[j], _T("corre")) == 0) {
				enc = rfbEncodingCoRRE;
			} else if (_tcsicmp(args[j], _T("hextile")) == 0) {
				enc = rfbEncodingHextile;
			} else if (_tcsicmp(args[j], _T("zlib")) == 0) {
				enc = rfbEncodingZlib;
			} else if (_tcsicmp(args[j], _T("zlibhex")) == 0) {
				enc = rfbEncodingZlibHex;
			} else if (_tcsicmp(args[j], _T("tight")) == 0) {
				enc = rfbEncodingTight;
			} else if (_tcsicmp(args[j], _T("ultra")) == 0) {
				enc = rfbEncodingUltra;
			} else if (_tcsicmp(args[j], _T("zrle")) == 0) { 
				enc = rfbEncodingZRLE; 
			} else if (_tcsicmp(args[j], _T("zywrle")) == 0) { 
				enc = rfbEncodingZYWRLE; 
			} else {
				ArgError(sz_D19);
				continue;
			}
			if (enc != -1) {
				m_UseEnc[enc] = true;
				m_PreferredEncoding = enc;
			}
	}
	// Tight options
	else if ( SwitchMatch(args[j], _T("compresslevel") )) {
			if (++j == i) {
				ArgError(sz_D20);
				continue;
			}
			m_useCompressLevel = true;
			if (_stscanf(args[j], _T("%d"), &m_compressLevel) != 1) {
				ArgError(sz_D21);
				continue;
			}
		} else if ( SwitchMatch(args[j], _T("quality") )) {
			if (++j == i) {
				ArgError(sz_D22);
				continue;
			}
			m_enableJpegCompression = true;
			if (_stscanf(args[j], _T("%d"), &m_jpegQualityLevel) != 1) {
				ArgError(sz_D23);
				continue;
			}
	}
	// act : add user option on command line
	else if ( SwitchMatch(args[j], _T("user") ))
	{
			if (++j == i)
			{
				ArgError(sz_D24);
				continue;
			}
			strcpy(m_cmdlnUser, args[j]);
	} // act : add user option on command line
	// Modif sf@2002 : password in the command line
	else if ( SwitchMatch(args[j], _T("password") ))
	{
			if (++j == i)
			{
				ArgError(sz_D24);
				continue;
			}
			strcpy(m_clearPassword, args[j]);
	} // Modif sf@2002
	else if ( SwitchMatch(args[j], _T("serverscale") ))
	{
		if (++j == i)
		{
			ArgError(sz_D25);
			continue;
		}
		_stscanf(args[j], _T("%d"), &m_nServerScale);
		if (m_nServerScale < 1 || m_nServerScale > 9) m_nServerScale = 1;
	}
	// Modif sf@2002
	else if ( SwitchMatch(args[j], _T("quickoption") )) 
	{
		if (++j == i)
		{
			ArgError(sz_D26);
			continue;
		}
		_stscanf(args[j], _T("%d"), &m_quickoption);
	}
	// Modif sf@2002 - DSM Plugin 
	else if ( SwitchMatch(args[j], _T("dsmplugin") ))
	{
		if (++j == i)
		{
			ArgError(sz_D27);
			continue;
		}
		m_fUseDSMPlugin = true;
		strcpy(m_szDSMPluginFilename, args[j]);
	}
	else if ( SwitchMatch(args[j], _T("proxy") ))
	{
		if (++j == i)
		{
			ArgError(sz_D27); // sf@ - Todo: put correct message here
			continue;
		}
		TCHAR proxyhost[256];
		if (!ParseDisplay(args[j], proxyhost, 255, &m_proxyport)) {
			ShowUsage(sz_D28);
			PostQuitMessage(1);
		} else {
			m_fUseProxy = true;
			_tcscpy(m_proxyhost, proxyhost);
		}
	}
	else if (SwitchMatch(args[j], _T("autoreconnect"))) 
	{
        if (++j == i) {
            ArgError(_T("You must specify an autoreconnect delay (default is 10s)"));
            PostQuitMessage(1);
            continue;
        }
		_stscanf(args[j], _T("%d"), &m_autoReconnect);
	}
	else
	{
      TCHAR phost[256];
      if (!ParseDisplay(args[j], phost, 255, &m_port)) {
        ShowUsage(sz_D28);
        PostQuitMessage(1);
      } else {
		  for (size_t i = 0, len = strlen(phost); i < len; i++)
						{
							phost[i] = toupper(phost[i]);
						} 
        _tcscpy(m_host_options, phost);
        m_connectionSpecified = true;
      }
    }
  }       
	
  if (m_scale_num != 1 || m_scale_den != 1) 			
    m_scaling = true;

	// reduce scaling factors by greatest common denominator
  if (m_scaling) {
    FixScaling();
  }
  // tidy up
  delete [] cmd;
  delete [] args;
}
Example #2
0
void VNCOptions::SetFromCommandLine(LPTSTR szCmdLine) {
	// We assume no quoting here.
	// Copy the command line - we don't know what might happen to the original
	int cmdlinelen = _tcslen(szCmdLine);
	if (cmdlinelen == 0) return;
	
	TCHAR *cmd = new TCHAR[cmdlinelen + 1];
	_tcscpy(cmd, szCmdLine);
	
	// Count the number of spaces
	// This may be more than the number of arguments, but that doesn't matter.
	int nspaces = 0;
	TCHAR *p = cmd;
	TCHAR *pos = cmd;
	while ( ( pos = _tcschr(p, ' ') ) != NULL ) {
		nspaces ++;
		p = pos + 1;
	}
	
	// Create the array to hold pointers to each bit of string
	TCHAR **args = new LPTSTR[nspaces + 1];
	
	// replace spaces with nulls and
	// create an array of TCHAR*'s which points to start of each bit.
	pos = cmd;
	int i = 0;
	args[i] = cmd;
	bool inquote=false;
	for (pos = cmd; *pos != 0; pos++) {
		// Arguments are normally separated by spaces, unless there's quoting
		if ((*pos == ' ') && !inquote) {
			*pos = '\0';
			p = pos + 1;
			args[++i] = p;
		}
		if (*pos == '"') {  
			if (!inquote) {      // Are we starting a quoted argument?
				args[i] = ++pos; // It starts just after the quote
			} else {
				*pos = '\0';     // Finish a quoted argument?
			}
			inquote = !inquote;
		}
	}
	i++;

	bool hostGiven = false, portGiven = false;
	// take in order.
	for (int j = 0; j < i; j++) {
		if ( SwitchMatch(args[j], _T("help")) ||
			SwitchMatch(args[j], _T("?")) ||
			SwitchMatch(args[j], _T("h"))) {
			ShowUsage();
			PostQuitMessage(1);
		} else if ( SwitchMatch(args[j], _T("listen"))) {
			m_listening = true;
		} else if ( SwitchMatch(args[j], _T("restricted"))) {
			m_restricted = true;
		} else if ( SwitchMatch(args[j], _T("viewonly"))) {
			m_ViewOnly = true;
		} else if ( SwitchMatch(args[j], _T("fullscreen"))) {
			m_FullScreen = true;
		} else if ( SwitchMatch(args[j], _T("8bit"))) {
			m_Use8Bit = true;
		} else if ( SwitchMatch(args[j], _T("shared"))) {
			m_Shared = true;
		} else if ( SwitchMatch(args[j], _T("swapmouse"))) {
			m_SwapMouse = true;
		} else if ( SwitchMatch(args[j], _T("nocursor"))) {
			m_localCursor = NOCURSOR;
		} else if ( SwitchMatch(args[j], _T("dotcursor"))) {
			m_localCursor = DOTCURSOR;
		} else if ( SwitchMatch(args[j], _T("normalcursor"))) {
			m_localCursor = NORMALCURSOR;
		} else if ( SwitchMatch(args[j], _T("belldeiconify") )) {
			m_DeiconifyOnBell = true;
		} else if ( SwitchMatch(args[j], _T("emulate3") )) {
			m_Emul3Buttons = true;
		} else if ( SwitchMatch(args[j], _T("noemulate3") )) {
			m_Emul3Buttons = false;
		} else if ( SwitchMatch(args[j], _T("scale") )) {
			if (++j == i) {
				ArgError(_T("No scaling factor specified"));
				continue;
			}
			int numscales = _stscanf(args[j], _T("%d/%d"), &m_scale_num, &m_scale_den);
			if (numscales < 1) {
				ArgError(_T("Invalid scaling specified"));
				continue;
			}
			if (numscales == 1) 
				m_scale_den = 1; // needed if you're overriding a previous setting

		} else if ( SwitchMatch(args[j], _T("emulate3timeout") )) {
			if (++j == i) {
				ArgError(_T("No timeout specified"));
				continue;
			}
			if (_stscanf(args[j], _T("%d"), &m_Emul3Timeout) != 1) {
				ArgError(_T("Invalid timeout specified"));
				continue;
			}
			
		} else if ( SwitchMatch(args[j], _T("emulate3fuzz") )) {
			if (++j == i) {
				ArgError(_T("No fuzz specified"));
				continue;
			}
			if (_stscanf(args[j], _T("%d"), &m_Emul3Fuzz) != 1) {
				ArgError(_T("Invalid fuzz specified"));
				continue;
			}
			
		} else if ( SwitchMatch(args[j], _T("disableclipboard") )) {
			m_DisableClipboard = true;
		}
#ifdef UNDER_CE
		// Manual setting of palm vs hpc aspect ratio for dialog boxes.
		else if ( SwitchMatch(args[j], _T("hpc") )) {
			m_palmpc = false;
		} else if ( SwitchMatch(args[j], _T("palm") )) {
			m_palmpc = true;
		} else if ( SwitchMatch(args[j], _T("slow") )) {
			m_slowgdi = true;
		} 
#endif
		else if ( SwitchMatch(args[j], _T("delay") )) {
			if (++j == i) {
				ArgError(_T("No delay specified"));
				continue;
			}
			if (_stscanf(args[j], _T("%d"), &m_delay) != 1) {
				ArgError(_T("Invalid delay specified"));
				continue;
			}
			
		} else if ( SwitchMatch(args[j], _T("loglevel") )) {
			if (++j == i) {
				ArgError(_T("No loglevel specified"));
				continue;
			}
			if (_stscanf(args[j], _T("%d"), &m_logLevel) != 1) {
				ArgError(_T("Invalid loglevel specified"));
				continue;
			}
			
		} else if ( SwitchMatch(args[j], _T("console") )) {
			m_logToConsole = true;
		} else if ( SwitchMatch(args[j], _T("logfile") )) {
			if (++j == i) {
				ArgError(_T("No logfile specified"));
				continue;
			}
			if (_stscanf(args[j], _T("%s"), &m_logFilename) != 1) {
				ArgError(_T("Invalid logfile specified"));
				continue;
			} else {
				m_logToFile = true;
			}
		} else if ( SwitchMatch(args[j], _T("config") )) {
			if (++j == i) {
				ArgError(_T("No config file specified"));
				continue;
			}
			// The GetPrivateProfile* stuff seems not to like some relative paths
			_fullpath(m_configFilename, args[j], _MAX_PATH);
			if (_access(m_configFilename, 04)) {
				ArgError(_T("Can't open specified config file for reading."));
				PostQuitMessage(1);
				continue;
			} else {
				Load(m_configFilename);
				m_configSpecified = true;
			}
		} else if ( SwitchMatch(args[j], _T("register") )) {
			Register();
			PostQuitMessage(0);
		} else {
			TCHAR phost[256];
			if (!ParseDisplay(args[j], phost, 255, &m_port)) {
				ShowUsage(_T("Invalid VNC server specified."));
				PostQuitMessage(1);
			} else {
				_tcscpy(m_host, phost);
				m_connectionSpecified = true;
			}
		}
	}       
	
	if (m_scale_num != 1 || m_scale_den != 1) 			
		m_scaling = true;

	// reduce scaling factors by greatest common denominator
	if (m_scaling) {
		FixScaling();
	}
	// tidy up
	delete [] cmd;
	delete [] args;
}