Beispiel #1
0
// construct a method from the type and a stream with properties
// the stream is supposed to consist of lines of the "property_name property_value" form.
nKrawall::nMethod::nMethod( char const * method_, std::istream & properties )
{
    method = method_;
    
    while ( !properties.eof() )
    {
        tString property;
        properties >> property;
        tToLower( property );

        std::ws( properties );
        if ( property == "prefix" )
        {
            prefix.ReadLine( properties );
        }
        else if ( property == "suffix" )
        {
            suffix.ReadLine( properties );
        }

    }
}
static int parse_cmdline(int argc, char *argv[])
{
   int c, ndx = 0;
   tString tmp;
   struct option options[] = { { "version", 0, 0, LONGONLYOPT_VERSION },
         { "help", 0, 0, LONGONLYOPT_HELP },
         { "user", 1, 0, 'u' },
         { "group", 1, 0, 'g' },
         { "label", 1, 0, 'l' },
         { "pool", 1, 0, LONGONLYOPT_POOL },
         { 0, 0, 0, 0 } };

   cmdl.print_version = false;
   cmdl.print_help = false;
   cmdl.command = 0;
   cmdl.slot = 0;
   cmdl.drive = 0;
   cmdl.mag_bay = 0;
   cmdl.count = 0;
   cmdl.label_prefix.clear();
   cmdl.pool.clear();
   cmdl.runas_user.clear();
   cmdl.runas_group.clear();
   cmdl.config_file.clear();
   cmdl.archive_device.clear();
   /* process the command line */
   for (;;) {
      c = getopt_long(argc ,argv, "u:g:l:", options, NULL);
      if (c == -1) break;
      switch (c) {
      case LONGONLYOPT_VERSION:
         cmdl.print_version = true;
         cmdl.print_help = false;
         return 0;
      case LONGONLYOPT_HELP:
         cmdl.print_version = false;
         cmdl.print_help = true;
         return 0;
      case 'u':
         cmdl.runas_user = optarg;
         break;
      case 'g':
         cmdl.runas_group = optarg;
         break;
      case 'l':
         cmdl.label_prefix = optarg;
         break;
      case LONGONLYOPT_POOL:
         cmdl.pool = optarg;
         break;
      default:
         fprintf(stderr, "unknown option %s\n", optarg);
         return -1;
      }
   }

   /* process positional params */
   ndx = optind;
   /* First parameter is the vchanger config file path */
   if (ndx >= argc) {
      fprintf(stderr, "missing parameter 1 (config_file)\n");
      return -1;
   }
   cmdl.config_file = argv[ndx];
   /* Second parameter is the command */
   ++ndx;
   if (ndx >= argc) {
      fprintf(stderr, "missing parameter 2 (command)\n");
      return -1;
   }
   tmp = argv[ndx];
   tToLower(tStrip(tmp));
   for (cmdl.command = 0; cmdl.command < NUM_AUTOCHANGER_COMMANDS; cmdl.command++) {
      if (tmp == autochanger_command[cmdl.command]) break;
   }
   if (cmdl.command >= NUM_AUTOCHANGER_COMMANDS) {
      fprintf(stderr, "'%s' is not a recognized command", argv[ndx]);
      return -1;
   }
   /* Make sure only CREATEVOLS command has -l flag */
   if (!cmdl.label_prefix.empty() && cmdl.command != CMD_CREATEVOLS) {
      fprintf(stderr, "flag -l not valid for this command\n");
      return -1;
   }
   /* Make sure only CREATEVOLS command has --pool flag */
   if (!cmdl.pool.empty() && cmdl.command != CMD_CREATEVOLS) {
      fprintf(stderr, "flag --pool not valid for this command\n");
      return -1;
   }
   /* Check param 3 exists */
   ++ndx;
   if (ndx >= argc) {
      /* Only 2 parameters given */
      switch (cmdl.command) {
      case CMD_LIST:
      case CMD_LISTALL:
      case CMD_SLOTS:
      case CMD_LISTMAGS:
      case CMD_REFRESH:
         return 0;   /* OK, because these commands only need 2 parameters */
      case CMD_CREATEVOLS:
         fprintf(stderr, "missing parameter 3 (magazine index)\n");
         break;
      default:
         fprintf(stderr, "missing parameter 3 (slot number)\n");
         break;
      }
      return -1;
   }
   /* Process parameter 3 */
   switch (cmdl.command) {
   case CMD_LIST:
   case CMD_LISTALL:
   case CMD_SLOTS:
   case CMD_LISTMAGS:
   case CMD_REFRESH:
      return 0;  /* These commands only need 2 params, so ignore extraneous */
   case CMD_CREATEVOLS:
      /* Param 3 for CREATEVOLS command is magazine index */
      cmdl.mag_bay = (int)strtol(argv[ndx], NULL, 10);
      if (cmdl.mag_bay < 0) {
         fprintf(stderr, "invalid magazine index in parameter 3\n");
         return -1;
      }
      break;
   case CMD_LOADED:
      /* slot is ignored for LOADED command, so just set to 1 */
      cmdl.slot = 1;
      break;
   default:
      /* Param 3 for all other commands is the slot number */
      cmdl.slot = (int)strtol(argv[ndx], NULL, 10);
      if (cmdl.slot < 1) {
         fprintf(stderr, "invalid slot number in parameter 3\n");
         return -1;
      }
      break;
   }
   /* Check param 4 exists */
   ++ndx;
   if (ndx >= argc) {
      /* Only 3 parameters given */
      switch (cmdl.command) {
      case CMD_CREATEVOLS:
         fprintf(stderr, "missing parameter 4 (count)\n");
         break;
      default:
         fprintf(stderr, "missing parameter 4 (archive device)\n");
         break;
      }
      return -1;
   }
   /* Process param 4 */
   switch (cmdl.command) {
   case CMD_CREATEVOLS:
      /* Param 4 for CREATEVOLS command is volume count */
      cmdl.count = (int)strtol(argv[ndx], NULL, 10);
      if (cmdl.count <= 0 ) {
         fprintf(stderr, "invalid count in parameter 4\n");
         return -1;
      }
      break;
   default:
      /* Param 4 for all other commands is the archive device path */
      cmdl.archive_device = argv[ndx];
      break;
   }
   /* Check param 5 exists */
   ++ndx;
   if (ndx >= argc) {
      /* Only 4 parameters given */
      switch (cmdl.command) {
      case CMD_CREATEVOLS:
         cmdl.slot = -1;
         return 0; /* OK, because parameter 5 optional */
      default:
         fprintf(stderr, "missing parameter 5 (drive index)\n");
         break;
      }
      return -1;
   }
   switch (cmdl.command) {
   case CMD_CREATEVOLS:
      cmdl.slot = (int)strtol(argv[ndx], NULL, 10);
      if (cmdl.slot < 0) cmdl.slot = -1;
      break;
   default:
      /* Param 5 for all other commands is drive index number */
      if (!isdigit(argv[ndx][0])) {
         fprintf(stderr, "invalid drive index in parameter 5\n");
         return -1;
      }
      cmdl.drive = (int)strtol(argv[ndx], NULL, 10);
      if (cmdl.drive < 0) {
         fprintf(stderr, "invalid drive index in parameter 5\n");
         return -1;
      }
      break;
   }

   /*  note that any extraneous parameters are simply ignored */
   return 0;
}