static gboolean
parse_options_callback (const gchar  *option_name,
                        const gchar  *value,
                        gpointer      data,
                        GError      **error)
{
  char **list;
  int    i;

  /* skip "-" or "--" */
  option_name++;
  if (*option_name == '-')
    option_name++;

  if (strcmp (OPTION_VENDOR, option_name) == 0)
    {
      if (vendor_name)
        {
          g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
                       _("Can only specify --vendor once"));
          return FALSE;
        }
      
      vendor_name = g_strdup (value);
    }

  else if (strcmp (OPTION_DIR, option_name) == 0)
    {
      if (target_dir)
        {
          g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
                       _("Can only specify --dir once"));
          return FALSE;
        }

      target_dir = g_strdup (value);
    }

#define PARSE_OPTION_LIST(glist)                                        \
  do {                                                                  \
    list = g_strsplit (value, ";", 0);                                  \
    for (i = 0; list[i]; i++)                                           \
      {                                                                 \
        if (*list[i] == '\0')                                           \
          continue;                                                     \
                                                                        \
        glist = g_slist_prepend (glist, g_strdup (list[i]));            \
      }                                                                 \
  } while (0)

  else if (strcmp (OPTION_ADD_CATEGORY, option_name) == 0)
    {
      PARSE_OPTION_LIST (added_categories);
    }

  else if (strcmp (OPTION_REMOVE_CATEGORY, option_name) == 0)
    {
      PARSE_OPTION_LIST (removed_categories);
    }

  else if (strcmp (OPTION_ADD_ONLY_SHOW_IN, option_name) == 0)
    {
      PARSE_OPTION_LIST (added_only_show_in);
    }

  else if (strcmp (OPTION_REMOVE_ONLY_SHOW_IN, option_name) == 0)
    {
      PARSE_OPTION_LIST (removed_only_show_in);
    }

  else if (strcmp (OPTION_MODE, option_name) == 0 ||
           strcmp ("m", option_name) == 0)
    {
      unsigned long ul;
      char *end;
      
      end = NULL;
      ul = strtoul (value, &end, 8);
      if (*value && end && *end == '\0')
        permissions = ul;
      else
        {
          g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
                       _("Could not parse mode string \"%s\""), value);
          
          return FALSE;
        }
    }

  else if (strcmp (OPTION_REMOVE_KEY, option_name) == 0)
    {
      removed_keys = g_slist_prepend (removed_keys,
                                      g_strdup (value));
    }

  else if (strcmp (OPTION_ADD_MIME_TYPE, option_name) == 0)
    {
      PARSE_OPTION_LIST (added_mime_types);
    }

  else if (strcmp (OPTION_REMOVE_MIME_TYPE, option_name) == 0)
    {
      PARSE_OPTION_LIST (removed_mime_types);
    }

  else
    {
        g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
                     _("Unknown option \"%s\""), option_name);
        
        return FALSE;
    }

  return TRUE;
}
예제 #2
0
static gboolean
parse_edit_options_callback (const gchar  *option_name,
                             const gchar  *value,
                             gpointer      data,
                             GError      **error)
{
  /* Note: we prepend actions to the list, so we'll need to reverse it later */

  DfuEditAction  *action;
  char          **list;
  int             i;

  /* skip "-" or "--" */
  option_name++;
  if (*option_name == '-')
    option_name++;

#define PARSE_OPTION_LIST(type, key)                                    \
  do {                                                                  \
    list = g_strsplit (value, ";", 0);                                  \
    for (i = 0; list[i]; i++)                                           \
      {                                                                 \
        if (*list[i] == '\0')                                           \
          continue;                                                     \
                                                                        \
        action = dfu_edit_action_new (type, key, list[i]);              \
        edit_actions = g_slist_prepend (edit_actions, action);          \
      }                                                                 \
  } while (0)

  if (strcmp (OPTION_SET_KEY, option_name) == 0)
    {
      action = dfu_edit_action_new (DFU_SET_KEY_BUILDING, value, NULL);
      edit_actions = g_slist_prepend (edit_actions, action);
    }

  else if (strcmp (OPTION_SET_VALUE, option_name) == 0)
    {
      gboolean handled = FALSE;

      if (edit_actions != NULL)
        {
          action = edit_actions->data;
          if (action->type == DFU_SET_KEY_BUILDING)
            {
              action->type = DFU_SET_KEY;
              action->action_value = g_strdup (value);

              handled = TRUE;
            }
        }

      if (!handled)
        {
          g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
                       _("Option \"--%s\" used without a prior \"--%s\" option"),
                       OPTION_SET_VALUE, OPTION_SET_KEY);

          return FALSE;
        }
    }

  else if (strcmp (OPTION_SET_NAME, option_name) == 0)
    {
      action = dfu_edit_action_new (DFU_SET_KEY, "Name", value);
      edit_actions = g_slist_prepend (edit_actions, action);
    }

  else if (strcmp (OPTION_COPY_GENERIC_NAME, option_name) == 0)
    {
      action = dfu_edit_action_new (DFU_COPY_KEY, "GenericName", "Name");
      edit_actions = g_slist_prepend (edit_actions, action);
    }

  else if (strcmp (OPTION_SET_GENERIC_NAME, option_name) == 0)
    {
      action = dfu_edit_action_new (DFU_SET_KEY, "GenericName", value);
      edit_actions = g_slist_prepend (edit_actions, action);
    }

  else if (strcmp (OPTION_COPY_NAME, option_name) == 0)
    {
      action = dfu_edit_action_new (DFU_COPY_KEY, "Name", "GenericName");
      edit_actions = g_slist_prepend (edit_actions, action);
    }

  else if (strcmp (OPTION_SET_COMMENT, option_name) == 0)
    {
      action = dfu_edit_action_new (DFU_SET_KEY, "Comment", value);
      edit_actions = g_slist_prepend (edit_actions, action);
    }

  else if (strcmp (OPTION_SET_ICON, option_name) == 0)
    {
      action = dfu_edit_action_new (DFU_SET_KEY, "Icon", value);
      edit_actions = g_slist_prepend (edit_actions, action);
    }

  else if (strcmp (OPTION_ADD_CATEGORY, option_name) == 0)
    {
      PARSE_OPTION_LIST (DFU_ADD_TO_LIST, "Categories");
    }

  else if (strcmp (OPTION_REMOVE_CATEGORY, option_name) == 0)
    {
      PARSE_OPTION_LIST (DFU_REMOVE_FROM_LIST, "Categories");
    }

  else if (strcmp (OPTION_ADD_MIME_TYPE, option_name) == 0)
    {
      PARSE_OPTION_LIST (DFU_ADD_TO_LIST, "MimeType");
    }

  else if (strcmp (OPTION_REMOVE_MIME_TYPE, option_name) == 0)
    {
      PARSE_OPTION_LIST (DFU_REMOVE_FROM_LIST, "MimeType");
    }

  else if (strcmp (OPTION_ADD_ONLY_SHOW_IN, option_name) == 0)
    {
      PARSE_OPTION_LIST (DFU_ADD_TO_LIST, "OnlyShowIn");
    }

  else if (strcmp (OPTION_REMOVE_ONLY_SHOW_IN, option_name) == 0)
    {
      PARSE_OPTION_LIST (DFU_REMOVE_FROM_LIST, "OnlyShowIn");
    }

  else if (strcmp (OPTION_ADD_NOT_SHOW_IN, option_name) == 0)
    {
      PARSE_OPTION_LIST (DFU_ADD_TO_LIST, "NotShowIn");
    }

  else if (strcmp (OPTION_REMOVE_NOT_SHOW_IN, option_name) == 0)
    {
      PARSE_OPTION_LIST (DFU_REMOVE_FROM_LIST, "NotShowIn");
    }

  else if (strcmp (OPTION_REMOVE_KEY, option_name) == 0)
    {
      action = dfu_edit_action_new (DFU_REMOVE_KEY, value, NULL);
      edit_actions = g_slist_prepend (edit_actions, action);
    }

  else
    {
      g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
                   _("Unknown option \"%s\""), option_name);

      return FALSE;
    }

  return TRUE;
}