Esempio n. 1
0
error_t 
config_parse_opt (int key,
                  char *arg,
                  struct argp_state *state,
                  struct config_arguments *config_args)
{
  struct config_keypair *kp = NULL;
  struct config_section_str *sstr = NULL;
  char *section_name = NULL;
  char *key_name = NULL;
  char *value = NULL;

  switch (key)
    {
    case CONFIG_ARGP_CHECKOUT_KEY:
      if (!config_args->action)
        config_args->action = CONFIG_ACTION_CHECKOUT;
      else
        config_args->action = -1;
      break;
    case CONFIG_ARGP_COMMIT_KEY:
      if (!config_args->action)
        config_args->action = CONFIG_ACTION_COMMIT;
      else
        config_args->action = -1;
      break;
    case CONFIG_ARGP_DIFF_KEY:
      if (!config_args->action)
        config_args->action = CONFIG_ACTION_DIFF;
      else
        config_args->action = -1;
      break;
    case CONFIG_ARGP_FILENAME_KEY:
      if (config_args->filename) /* If specified more than once */
        free (config_args->filename);
      if (!(config_args->filename = strdup (arg)))
        {
          perror("strdup");
          exit(1);
        }
      break;
    case CONFIG_ARGP_KEYPAIR_KEY:
      if (config_keypair_parse_string(arg,
                                      &section_name,
                                      &key_name,
                                      &value) < 0)
        {
          /* error printed in function call */
          exit(1);
        }
      if (!(kp = config_keypair_create(section_name,
                                       key_name,
                                       value)))
        {
          fprintf(stderr,
                  "config_keypair_create error\n");
          exit(1);
        }
      if (config_keypair_append(&(config_args->keypairs),
                                kp) < 0)
        {
          /* error printed in function call */
          exit(1);
        }
      if (section_name)
        free(section_name);
      section_name = NULL;
      if (key_name)
        free(key_name);
      key_name = NULL;
      if (value)
        free(value);
      value = NULL;
      kp = NULL;
      break;
    case CONFIG_ARGP_SECTIONS_KEY:
      if (!(sstr = config_section_str_create(arg)))
        {
          fprintf(stderr,
                  "config_section_str_create error\n");
          exit(1);
        }
      if (config_section_str_append(&(config_args->section_strs),
                                    sstr) < 0)
        {
          /* error printed in function call */
          exit(1);
        }
      sstr = NULL;
      break;
    case CONFIG_ARGP_LIST_SECTIONS_KEY:
      if (!config_args->action)
        config_args->action = CONFIG_ACTION_LIST_SECTIONS;
      else
        config_args->action = -1;
      break;
    case CONFIG_ARGP_VERBOSE_KEY:
      config_args->verbose = 1;
      break;
    default:
      return ARGP_ERR_UNKNOWN;
    }
 
  return 0;
}
Esempio n. 2
0
error_t
config_parse_opt (int key,
                  char *arg,
                  struct config_arguments *config_args)
{
  struct config_keypair *kp = NULL;
  struct config_section_str *sstr = NULL;
  char *section_name = NULL;
  char *key_name = NULL;
  char *value = NULL;

  assert (config_args);

  switch (key)
    {
    case CONFIG_ARGP_CHECKOUT_KEY:
      if (!config_args->action)
        config_args->action = CONFIG_ACTION_CHECKOUT;
      else
        config_args->action = -1;
      break;
    case CONFIG_ARGP_COMMIT_KEY:
      if (!config_args->action)
        config_args->action = CONFIG_ACTION_COMMIT;
      else
        config_args->action = -1;
      break;
    case CONFIG_ARGP_DIFF_KEY:
      if (!config_args->action)
        config_args->action = CONFIG_ACTION_DIFF;
      else
        config_args->action = -1;
      break;
    case CONFIG_ARGP_FILENAME_KEY:
      free (config_args->filename);
      if (!(config_args->filename = strdup (arg)))
        {
          perror ("strdup");
          exit (EXIT_FAILURE);
        }
      break;
    case CONFIG_ARGP_KEYPAIR_KEY:
      if (config_keypair_parse_string (arg,
                                       &section_name,
                                       &key_name,
                                       &value) < 0)
        {
          /* error printed in function call */
          exit (EXIT_FAILURE);
        }
      if (!(kp = config_keypair_create (section_name,
                                        key_name,
                                        value)))
        {
          fprintf (stderr,
                   "config_keypair_create error\n");
          exit (EXIT_FAILURE);
        }
      if (config_keypair_append (&(config_args->keypairs),
                                 kp) < 0)
        {
          /* error printed in function call */
          exit (EXIT_FAILURE);
        }
      free (section_name);
      free (key_name);
      free (value);
      break;
    case CONFIG_ARGP_SECTIONS_KEY:
      if (!(sstr = config_section_str_create (arg)))
        {
          fprintf (stderr,
                   "config_section_str_create error\n");
          exit (EXIT_FAILURE);
        }
      if (config_section_str_append (&(config_args->section_strs),
                                     sstr) < 0)
        {
          /* error printed in function call */
          exit (EXIT_FAILURE);
        }
      sstr = NULL;
      break;
    case CONFIG_ARGP_LIST_SECTIONS_KEY:
      if (!config_args->action)
        config_args->action = CONFIG_ACTION_LIST_SECTIONS;
      else
        config_args->action = -1;
      break;
    case CONFIG_ARGP_VERBOSE_KEY:
      config_args->verbose_count++;
      break;
    case CONFIG_ARGP_LAN_CHANNEL_NUMBER_KEY:
      _config_parse_channel_number (arg,
                                    &(config_args->lan_channel_number),
                                    &(config_args->lan_channel_number_set));
      break;
    case CONFIG_ARGP_SERIAL_CHANNEL_NUMBER_KEY:
      _config_parse_channel_number (arg,
                                    &(config_args->serial_channel_number),
                                    &(config_args->serial_channel_number_set));
      break;
    case CONFIG_ARGP_SOL_CHANNEL_NUMBER_KEY:
      _config_parse_channel_number (arg,
                                    &(config_args->sol_channel_number),
                                    &(config_args->sol_channel_number_set));
      break;
    default:
      return (ARGP_ERR_UNKNOWN);
    }

  return (0);
}