Logo Search packages:      
Sourcecode: jigdo version File versions

JigdoFileCmd::Command JigdoFileCmd::cmdOptions ( int  argc,
char *  argv[] 
) [static, private]

Defined in jigdo-file.cc - reads command line options and sets the static vars above, returns command requested by user. Will throw Cleanup() for things like --help, --version or invalid cmd line args.

Definition at line 473 of file jigdo-file.cc.

References RecurseDir::addFile(), RecurseDir::addFilesFrom(), binName, and RecurseDir::empty().

                                                                   {
# if !WINDOWS
  binaryName = argv[0];
# endif
  bool error = false;
  optReporter = &reporterDefault;

  while (true) {
    static const struct option longopts[] = {
      { "cache",              required_argument, 0, 'c' },
      { "cache-expiry",       required_argument, 0, LONGOPT_CACHEEXPIRY },
      { "check-files",        no_argument,       0, LONGOPT_MKIMAGECHECK },
      { "debug",              optional_argument, 0, LONGOPT_DEBUG },
      { "files-from",         required_argument, 0, 'T' }, // "-T" like tar's
      { "force",              no_argument,       0, 'f' },
      { "help",               no_argument,       0, 'h' },
      { "help-all",           no_argument,       0, 'H' },
      { "hex",                no_argument,       0, LONGOPT_HEX },
      { "image",              required_argument, 0, 'i' },
      { "image-section",      no_argument,       0, LONGOPT_ADDIMAGE },
      { "jigdo",              required_argument, 0, 'j' },
      { "label",              required_argument, 0, LONGOPT_LABEL },
      { "md5-block-size",     required_argument, 0, LONGOPT_MD5SIZE },
      { "match-exec",         required_argument, 0, LONGOPT_MATCHEXEC },
      { "merge",              required_argument, 0, LONGOPT_MERGE },
      { "min-length",         required_argument, 0, LONGOPT_MINSIZE },
      { "no-cache",           no_argument,       0, LONGOPT_NOCACHE },
      { "no-check-files",     no_argument,       0, LONGOPT_NOMKIMAGECHECK },
      { "no-debug",           no_argument,       0, LONGOPT_NODEBUG },
      { "no-force",           no_argument,       0, LONGOPT_NOFORCE },
      { "no-hex",             no_argument,       0, LONGOPT_NOHEX },
      { "no-image-section",   no_argument,       0, LONGOPT_NOADDIMAGE },
      { "no-servers-section", no_argument,       0, LONGOPT_NOADDSERVERS },
      { "readbuffer",         required_argument, 0, LONGOPT_BUFSIZE },
      { "report",             required_argument, 0, 'r' },
      { "servers-section",    no_argument,       0, LONGOPT_ADDSERVERS },
      { "template",           required_argument, 0, 't' },
      { "uri",                required_argument, 0, LONGOPT_URI },
      { "version",            no_argument,       0, 'v' },
      { 0, 0, 0, 0 }
    };

    int c = getopt_long(argc, argv, "0123456789hHvT:i:j:t:c:fr:",
                        longopts, 0);
    if (c == -1) break;

    switch (c) {
    case '0': case '1': case '2': case '3': case '4':
    case '5': case '6': case '7': case '8': case '9':
      optZipQuality = c - '0'; break;
    case 'h': case 'H': optHelp = c; break;
    case 'v': optVersion = true; break;
    case 'T': fileNames.addFilesFrom(
                strcmp(optarg, "-") == 0 ? "" : optarg); break;
    case 'i': imageFile = optarg; break;
    case 'j': jigdoFile = optarg; break;
    case 't': templFile = optarg; break;
    case LONGOPT_MERGE: jigdoMergeFile = optarg; break;
    case 'c': cacheFile = optarg; break;
    case LONGOPT_NOCACHE: cacheFile.erase(); break;
    case LONGOPT_CACHEEXPIRY: optCacheExpiry = scanTimespan(optarg); break;
    case 'f': optForce = true; break;
    case LONGOPT_NOFORCE: optForce = false; break;
    case LONGOPT_MINSIZE:    blockLength = scanMemSize(optarg); break;
    case LONGOPT_MD5SIZE: md5BlockLength = scanMemSize(optarg); break;
    case LONGOPT_BUFSIZE:     readAmount = scanMemSize(optarg); break;
    case 'r':
      if (strcmp(optarg, "default") == 0) {
        optReporter = &reporterDefault;
      } else if (strcmp(optarg, "noprogress") == 0) {
        optReporter = &reporterNoprogress;
      } else if (strcmp(optarg, "quiet") == 0) {
        optReporter = &reporterQuiet;
      } else if (strcmp(optarg, "grep") == 0) {
        optReporter = &reporterGrep;
      } else {
        cerr << subst(_("%1: Invalid argument to --report (allowed: "
                        "default noprogress quiet grep)"), binName())
             << '\n';
        error = true;
      }
      break;
    case LONGOPT_MKIMAGECHECK: optMkImageCheck = true; break;
    case LONGOPT_NOMKIMAGECHECK: optMkImageCheck = false; break;
    case LONGOPT_ADDSERVERS: optAddServers = true; break;
    case LONGOPT_NOADDSERVERS: optAddServers = false; break;
    case LONGOPT_ADDIMAGE: optAddImage = true; break;
    case LONGOPT_NOADDIMAGE: optAddImage = false; break;
    case LONGOPT_LABEL: optLabels.push_back(string(optarg)); break;
    case LONGOPT_URI: optUris.push_back(string(optarg)); break;
    case LONGOPT_HEX: optHex = true; break;
    case LONGOPT_NOHEX: optHex = false; break;
    case LONGOPT_DEBUG:
      if (optarg) optDebug = optarg; else optDebug = "all";
      break;
    case LONGOPT_NODEBUG: optDebug.erase(); break;
    case LONGOPT_MATCHEXEC: optMatchExec = optarg; break;
    case '?': error = true;
    case ':': break;
    default:
      msg("getopt returned %1", static_cast<int>(c));
      break;
    }
  }

  if (error) exit_tryHelp();

  if (optHelp != '\0' || optVersion) {
    if (optVersion) cout << "jigdo-file version " JIGDO_VERSION << endl;
    if (optHelp != '\0') printUsage(optHelp == 'H', blockLength,
                                    md5BlockLength, readAmount);
    throw Cleanup(0);
  }

# if WINDOWS
  Logger::scanOptions(optDebug, binName());
# else
  Logger::scanOptions(optDebug, binName().c_str());
# endif
  //______________________________

  // Silently correct invalid blockLength/md5BlockLength args
  if (blockLength < MINIMUM_BLOCKLENGTH) blockLength = MINIMUM_BLOCKLENGTH;
  if (blockLength >= md5BlockLength) md5BlockLength = blockLength + 1;
  // Round to next k*64+55 for efficient MD5 calculation
  md5BlockLength = ((md5BlockLength + 63 - 55) & ~63U) + 55;

  Paranoid(blockLength >= MINIMUM_BLOCKLENGTH
           && blockLength < md5BlockLength);
  //______________________________

  // Complain if name of command isn't there
  if (optind >= argc) {
    cerr << subst(_("%1: Please specify a command"), binName()) << '\n';
    exit_tryHelp();
  }

  // Find Command code corresponding to command string on command line :)
  Command result;
  {
    const char* command = argv[optind++];
    struct CodesEntry { char* name; Command code; };
    const CodesEntry codes[] = {
      { "make-template",     MAKE_TEMPLATE },
      { "mt",                MAKE_TEMPLATE },
      { "make-image",        MAKE_IMAGE },
      { "mi",                MAKE_IMAGE },
      { "print-missing",     PRINT_MISSING },
      { "pm",                PRINT_MISSING },
      { "print-missing-all", PRINT_MISSING_ALL },
      { "pma",               PRINT_MISSING_ALL },
      { "verify",            VERIFY },
      { "ver",               VERIFY },
      { "scan",              SCAN },
      { "sc",                SCAN },
      { "list-template",     LIST_TEMPLATE },
      { "ls",                LIST_TEMPLATE },
      { "md5sum",            MD5SUM },
      { "md5",               MD5SUM }
    };

    const CodesEntry *c = codes;
    const CodesEntry *end = codes+sizeof(codes)/sizeof(CodesEntry);
    while (true) {
      if (strcmp(command, c->name) == 0) {
        result = c->code;
        break;
      }
      ++c;
      if (c == end) {
        string allCommands;
        for (const CodesEntry *c = codes,
              *end = codes+sizeof(codes)/sizeof(CodesEntry); c != end; ++c) {
          allCommands += ' ';
          allCommands += c->name;
        }
        cerr << subst(_("%1: Invalid command `%2'\n(Must be one of:%3)"),
                      binName(), command, allCommands) << '\n';
        exit_tryHelp();
      }
    }
  }
  //____________________

  while (optind < argc) fileNames.addFile(argv[optind++]);

# if 0
  /* If no --files-from given and no files on command line, assume we
     are to read any list of filenames from stdin. */
  if (fileNames.empty()) fileNames.addFilesFrom("");
# endif
  //____________________

  // If --image, --jigdo or --template not given, create name from other args
  if (!imageFile.empty() && imageFile != "-") {
    deduceName(jigdoFile, EXTSEPS"jigdo", imageFile);
    deduceName(templFile, EXTSEPS"template", imageFile);
  } else if (!jigdoFile.empty() && jigdoFile != "-") {
    deduceName(imageFile, "", jigdoFile);
    deduceName(templFile, EXTSEPS"template", jigdoFile);
  } else if (!templFile.empty() && templFile != "-") {
    deduceName(imageFile, "", templFile);
    deduceName(jigdoFile, EXTSEPS"jigdo", templFile);
  }

  if (imageFile != "-") {
    struct stat fileInfo;
    if (stat(imageFile.c_str(), &fileInfo) == 0)
      imageSize = fileInfo.st_size;
  }
  //____________________

  if (msg) {
    msg("Image file: %1", imageFile);
    msg("Jigdo:      %1", jigdoFile);
    msg("Template:   %1", templFile);
  }

  return result;
}


Generated by  Doxygen 1.6.0   Back to index