void ViewerCmdLine::parseOptionsFile() { StringStorage pathToIniFile = m_options[OPTIONS_FILE]; if (pathToIniFile.findChar(_T('\\')) == -1) { StringStorage newPathToIniFile; newPathToIniFile.format(_T(".\\%s"), pathToIniFile.getString()); pathToIniFile = newPathToIniFile; } IniFileSettingsManager sm(pathToIniFile.getString()); sm.setApplicationName(_T("connection")); StringStorage host; if (!sm.getString(_T("host"), &host)) { throw CommandLineFormatException(_T("Could not read options file.")); } StringStorage port; if (sm.getString(_T("port"), &port)) { StringStorage hostString; hostString.format(_T("%s:%s"), host.getString(), port.getString()); m_conData->setHost(&hostString); } else { m_conData->setHost(&host); } StringStorage password; sm.getString(_T("password"), &password); if (!password.isEmpty()) { m_conData->setCryptedPassword(&password); } else { parsePassword(); } sm.setApplicationName(_T("options")); m_conConf->loadFromStorage(&sm); }
int main(int argc, char ** argv) { // Global errno reset to properly parse errors from fopen() function call errno = 0; // User specified passphrase char password[BUFSIZ]; // Length of the password int length = 0; // Randomly generated key char key[BUFSIZ]; // Bitmask used to encrypt file char mask[BUFSIZ]; // Set to user-specified file FILE * file = 0; // Check if a file is specified if(--argc != 1) { (void)fprintf(stderr, USAGE); return EXIT_FAILURE; } file = fopen(argv[1], READ_MODE); // Check if file is valid if(file == NULL) { if(errno == EACCES) { // User does not have permission to read/write file, print error (void)fprintf(stderr, NO_ACCESS); return EXIT_FAILURE; } else { // File itself is invalid, print error (void)fprintf(stderr, FILE_INVALID); return EXIT_FAILURE; } } else { (void)printf(PASS_PROMPT); (void)scanf("%s", password); // Check if password is at least 8 characters long if(!parsePassword(password, &length)) { (void)fprintf(stderr, PASS_SHORT); return EXIT_FAILURE; } // Randomly generate the key based on password length (void)generateKey(length, key); (void)generateMask(key, password, mask, length); } return EXIT_SUCCESS; }
bool parseCommandLine(const std::vector<Common::UString> &argv, int &returnValue, Command &command, Common::UString &archive, std::set<Common::UString> &files, Aurora::GameID &game, std::vector<byte> &password) { archive.clear(); files.clear(); std::vector<Common::UString> args; bool optionsEnd = false; for (size_t i = 1; i < argv.size(); i++) { bool isOption = false; // A "--" marks an end to all options if (argv[i] == "--") { optionsEnd = true; continue; } // We're still handling options if (!optionsEnd) { // Help text if ((argv[i] == "-h") || (argv[i] == "--help")) { printUsage(stdout, argv[0]); returnValue = 0; return false; } if (argv[i] == "--version") { printVersion(); returnValue = 0; return false; } if (argv[i] == "--nwn2") { isOption = true; game = Aurora::kGameIDNWN2; } else if (argv[i] == "--jade") { isOption = true; game = Aurora::kGameIDJade; } else if (argv[i] == "--pass") { isOption = true; // Needs the password as the next parameter if (i++ == (argv.size() - 1)) { printUsage(stdout, argv[0]); returnValue = 1; return false; } parsePassword(argv[i], password); } else if (argv[i] == "--nwm") { isOption = true; // Needs the file as the next parameter if (i++ == (argv.size() - 1)) { printUsage(stdout, argv[0]); returnValue = 1; return false; } readNWMMD5(argv[i], password); } else if (argv[i].beginsWith("-") || argv[i].beginsWith("--")) { // An options, but we already checked for all known ones printUsage(stderr, argv[0]); returnValue = 1; return false; } } // Was this a valid option? If so, don't try to use it as a file if (isOption) continue; args.push_back(argv[i]); } if (args.size() < 2) { printUsage(stderr, argv[0]); returnValue = 1; return false; } std::vector<Common::UString>::iterator arg = args.begin(); // Find out what we should do command = kCommandNone; for (int i = 0; i < kCommandMAX; i++) if (!strcmp(arg->c_str(), kCommandChar[i])) command = (Command) i; // Unknown command if (command == kCommandNone) { printUsage(stderr, argv[0]); returnValue = 1; return false; } ++arg; archive = *arg; ++arg; files.insert(arg, args.end()); return true; }
void ViewerCmdLine::parse() { const CmdLineOption options[] = { HELP, HELP_SHORT, OPTIONS_FILE, LISTEN, HOST, PORT, PASSWORD, SHOW_CONTROLS, VIEW_ONLY, USE_CLIPBOARD, SCALE, FULL_SCREEN, WARN_FULL_SCREEN, ENCODING, COPY_RECT, MOUSE_CURSOR, MOUSE_LOCAL, MOUSE_SWAP, JPEG_IMAGE_QUALITY, COMPRESSION_LEVEL }; if (!processCmdLine(&options[0], sizeof(options) / sizeof(CmdLineOption))) { throw CommandLineFormatException(); } if (isHelpPresent()) { throw CommandLineFormatHelp(); } if (m_wpcl.getArgumentsCount() > 2) { throw CommandLineFormatException(); } if (m_wpcl.getArgumentsCount() > 1) { if (isPresent(ViewerCmdLine::HOST)) { throw CommandLineFormatException(); } } if (isPresent(ViewerCmdLine::OPTIONS_FILE)) { parseOptionsFile(); } else { if (isPresent(ViewerCmdLine::LISTEN)) { *m_isListening = true; } else { if (!parseHost()) { throw CommandLineFormatException(); } } } parsePassword(); parseEncoding(); parseMouseShape(); parseMouseCursor(); parseScale(); parseFullScreen(); parseCompressionLevel(); parseWarnFullScr(); parseMouseSwap(); parseUseClipboard(); parseShowControl(); parseCopyRect(); parseViewOnly(); parseJpegImageQuality(); }