summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Hughes <richard@hughsie.com>2022-09-03 18:21:46 +0100
committerRichard Hughes <richard@hughsie.com>2022-09-04 19:23:06 +0100
commit6eb98600ad27e089ac7bca02972a23fcda2390db (patch)
treec758cb806f55d39d9f42f3b355a930c00b78ab92
parenta67a29d04c6aa7bdf1347001399ce00258f50373 (diff)
downloadgusb-6eb98600ad27e089ac7bca02972a23fcda2390db.tar.gz
Use g_autoptr() in a lot more places
We started using this several months ago and nobody even noticed.
-rw-r--r--gusb/gusb-context.c168
-rw-r--r--gusb/gusb-device.c21
-rw-r--r--gusb/gusb-self-test.c81
-rw-r--r--tools/gusb-main.c166
4 files changed, 155 insertions, 281 deletions
diff --git a/gusb/gusb-context.c b/gusb/gusb-context.c
index 2b8677a..8f0f5d6 100644
--- a/gusb/gusb-context.c
+++ b/gusb/gusb-context.c
@@ -17,6 +17,7 @@
#include <libusb.h>
+#include "gusb-autocleanups.h"
#include "gusb-context-private.h"
#include "gusb-device-private.h"
#include "gusb-util.h"
@@ -279,13 +280,13 @@ static void
g_usb_context_add_device (GUsbContext *context,
struct libusb_device *dev)
{
- GUsbDevice *device = NULL;
GUsbContextPrivate *priv = context->priv;
GUsbContextReplugHelper *replug_helper;
const gchar *platform_id;
guint8 bus;
guint8 address;
- GError *error = NULL;
+ g_autoptr(GError) error = NULL;
+ g_autoptr(GUsbDevice) device = NULL;
/* does any existing device exist */
bus = libusb_get_bus_number (dev);
@@ -294,23 +295,21 @@ g_usb_context_add_device (GUsbContext *context,
if (priv->done_enumerate)
device = g_usb_context_find_by_bus_address (context, bus, address, NULL);
if (device != NULL)
- goto out;
+ return;
/* add the device */
device = _g_usb_device_new (context, dev, &error);
if (device == NULL) {
g_debug ("There was a problem creating the device: %s",
error->message);
- g_error_free (error);
- goto out;
+ return;
}
/* auto-open */
if (priv->flags & G_USB_CONTEXT_FLAGS_AUTO_OPEN_DEVICES) {
if (!_g_usb_device_open_internal (device, &error)) {
g_warning ("cannot open the device: %s", error->message);
- g_error_free (error);
- goto out;
+ return;
}
}
@@ -325,26 +324,23 @@ g_usb_context_add_device (GUsbContext *context,
g_object_unref (replug_helper->device);
replug_helper->device = g_object_ref (device);
g_main_loop_quit (replug_helper->loop);
- goto out;
+ return;
}
/* emit signal */
g_usb_context_emit_device_add (context, device);
-out:
- if (device != NULL)
- g_object_unref (device);
}
static void
g_usb_context_remove_device (GUsbContext *context,
struct libusb_device *dev)
{
- GUsbDevice *device = NULL;
GUsbContextPrivate *priv = context->priv;
GUsbContextReplugHelper *replug_helper;
const gchar *platform_id;
guint8 bus;
guint8 address;
+ g_autoptr(GUsbDevice) device = NULL;
/* does any existing device exist */
bus = libusb_get_bus_number (dev);
@@ -363,13 +359,11 @@ g_usb_context_remove_device (GUsbContext *context,
replug_helper = g_hash_table_lookup (priv->dict_replug, platform_id);
if (replug_helper != NULL) {
g_debug ("%s is in replug, ignoring remove", platform_id);
- goto out;
+ return;
}
/* emit signal */
g_usb_context_emit_device_remove (context, device);
-out:
- g_object_unref (device);
}
typedef struct {
@@ -440,26 +434,22 @@ g_usb_context_hotplug_cb (struct libusb_context *ctx,
static void
g_usb_context_rescan (GUsbContext *context)
{
- GList *existing_devices = NULL;
- GList *l;
- GUsbDevice *device;
GUsbContextPrivate *priv = context->priv;
- gboolean found;
- guint i;
libusb_device **dev_list = NULL;
+ g_autoptr(GList) existing_devices = NULL;
/* copy to a context so we can remove from the array */
- for (i = 0; i < priv->devices->len; i++) {
- device = g_ptr_array_index (priv->devices, i);
+ for (guint i = 0; i < priv->devices->len; i++) {
+ GUsbDevice *device = g_ptr_array_index (priv->devices, i);
existing_devices = g_list_prepend (existing_devices, device);
}
/* look for any removed devices */
libusb_get_device_list (priv->ctx, &dev_list);
- for (l = existing_devices; l != NULL; l = l->next) {
- device = G_USB_DEVICE (l->data);
- found = FALSE;
- for (i = 0; dev_list != NULL && dev_list[i] != NULL; i++) {
+ for (GList *l = existing_devices; l != NULL; l = l->next) {
+ GUsbDevice *device = G_USB_DEVICE (l->data);
+ gboolean found = FALSE;
+ for (guint i = 0; dev_list != NULL && dev_list[i] != NULL; i++) {
if (libusb_get_bus_number (dev_list[i]) == g_usb_device_get_bus (device) &&
libusb_get_device_address (dev_list[i]) == g_usb_device_get_address (device)) {
found = TRUE;
@@ -473,10 +463,9 @@ g_usb_context_rescan (GUsbContext *context)
}
/* add any devices not yet added (duplicates will be filtered */
- for (i = 0; dev_list != NULL && dev_list[i] != NULL; i++)
+ for (guint i = 0; dev_list != NULL && dev_list[i] != NULL; i++)
g_usb_context_add_device (context, dev_list[i]);
- g_list_free (existing_devices);
libusb_free_device_list (dev_list, 1);
}
@@ -622,9 +611,10 @@ g_usb_context_enumerate (GUsbContext *context)
priv->done_enumerate = TRUE;
/* emit device-added signals before returning */
- for (guint i = 0; i < priv->devices->len; i++)
+ for (guint i = 0; i < priv->devices->len; i++) {
g_signal_emit (context, signals[DEVICE_ADDED_SIGNAL], 0,
g_ptr_array_index (priv->devices, i));
+ }
/* any queued up hotplug events are queued as idle handlers */
}
@@ -849,8 +839,6 @@ g_usb_context_find_by_bus_address (GUsbContext *context,
GError **error)
{
GUsbContextPrivate *priv;
- GUsbDevice *device = NULL;
- guint i;
g_return_val_if_fail (G_USB_IS_CONTEXT (context), NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
@@ -858,24 +846,19 @@ g_usb_context_find_by_bus_address (GUsbContext *context,
priv = context->priv;
g_usb_context_enumerate (context);
- for (i = 0; i < priv->devices->len; i++) {
- GUsbDevice *curr = g_ptr_array_index (priv->devices, i);
- if (g_usb_device_get_bus (curr) == bus &&
- g_usb_device_get_address (curr) == address) {
- device = g_object_ref (curr);
- break;
+ for (guint i = 0; i < priv->devices->len; i++) {
+ GUsbDevice *device = g_ptr_array_index (priv->devices, i);
+ if (g_usb_device_get_bus (device) == bus &&
+ g_usb_device_get_address (device) == address) {
+ return g_object_ref (device);
}
}
-
- if (device == NULL) {
- g_set_error (error,
- G_USB_DEVICE_ERROR,
- G_USB_DEVICE_ERROR_NO_DEVICE,
- "Failed to find device %04x:%04x",
- bus, address);
- }
-
- return device;
+ g_set_error (error,
+ G_USB_DEVICE_ERROR,
+ G_USB_DEVICE_ERROR_NO_DEVICE,
+ "Failed to find device %04x:%04x",
+ bus, address);
+ return NULL;
}
/**
@@ -896,8 +879,6 @@ g_usb_context_find_by_platform_id (GUsbContext *context,
GError **error)
{
GUsbContextPrivate *priv;
- GUsbDevice *device = NULL;
- guint i;
g_return_val_if_fail (G_USB_IS_CONTEXT (context), NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
@@ -905,23 +886,17 @@ g_usb_context_find_by_platform_id (GUsbContext *context,
priv = context->priv;
g_usb_context_enumerate (context);
- for (i = 0; i < priv->devices->len; i++) {
- GUsbDevice *curr = g_ptr_array_index (priv->devices, i);
- if (g_strcmp0 (g_usb_device_get_platform_id (curr), platform_id) == 0) {
- device = g_object_ref (curr);
- break;
- }
- }
-
- if (device == NULL) {
- g_set_error (error,
- G_USB_DEVICE_ERROR,
- G_USB_DEVICE_ERROR_NO_DEVICE,
- "Failed to find device %s",
- platform_id);
+ for (guint i = 0; i < priv->devices->len; i++) {
+ GUsbDevice *device = g_ptr_array_index (priv->devices, i);
+ if (g_strcmp0 (g_usb_device_get_platform_id (device), platform_id) == 0)
+ return g_object_ref (device);
}
-
- return device;
+ g_set_error (error,
+ G_USB_DEVICE_ERROR,
+ G_USB_DEVICE_ERROR_NO_DEVICE,
+ "Failed to find device %s",
+ platform_id);
+ return NULL;
}
/**
@@ -944,8 +919,6 @@ g_usb_context_find_by_vid_pid (GUsbContext *context,
GError **error)
{
GUsbContextPrivate *priv;
- GUsbDevice *device = NULL;
- guint i;
g_return_val_if_fail (G_USB_IS_CONTEXT (context), NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
@@ -953,34 +926,29 @@ g_usb_context_find_by_vid_pid (GUsbContext *context,
priv = context->priv;
g_usb_context_enumerate (context);
- for (i = 0; i < priv->devices->len; i++) {
- GUsbDevice *curr = g_ptr_array_index (priv->devices, i);
- if (g_usb_device_get_vid (curr) == vid &&
- g_usb_device_get_pid (curr) == pid) {
- device = g_object_ref (curr);
- break;
+ for (guint i = 0; i < priv->devices->len; i++) {
+ GUsbDevice *device = g_ptr_array_index (priv->devices, i);
+ if (g_usb_device_get_vid (device) == vid &&
+ g_usb_device_get_pid (device) == pid) {
+ return g_object_ref (device);
}
}
-
- if (device == NULL) {
- g_set_error (error,
- G_USB_DEVICE_ERROR,
- G_USB_DEVICE_ERROR_NO_DEVICE,
- "Failed to find device %04x:%04x",
- vid, pid);
- }
-
- return device;
+ g_set_error (error,
+ G_USB_DEVICE_ERROR,
+ G_USB_DEVICE_ERROR_NO_DEVICE,
+ "Failed to find device %04x:%04x",
+ vid, pid);
+ return NULL;
}
+
static gboolean
g_usb_context_load_usb_ids (GUsbContext *context,
GError **error)
{
guint16 pid;
guint16 vid = 0x0000;
- guint i;
- gchar *data = NULL;
- gchar **lines = NULL;
+ g_autofree gchar *data = NULL;
+ g_auto(GStrv) lines = NULL;
/* already loaded */
if (g_hash_table_size (context->priv->dict_usb_ids) > 0)
@@ -991,9 +959,7 @@ g_usb_context_load_usb_ids (GUsbContext *context,
return FALSE;
lines = g_strsplit (data, "\n", -1);
- g_free (data);
-
- for (i = 0; lines[i] != NULL; i++) {
+ for (guint i = 0; lines[i] != NULL; i++) {
if (lines[i][0] == '#')
continue;
@@ -1022,8 +988,6 @@ g_usb_context_load_usb_ids (GUsbContext *context,
}
}
- g_strfreev (lines);
-
return TRUE;
}
@@ -1045,7 +1009,7 @@ _g_usb_context_lookup_vendor (GUsbContext *context,
GError **error)
{
const gchar *tmp;
- gchar *key = NULL;
+ g_autofree gchar *key = NULL;
g_return_val_if_fail (G_USB_IS_CONTEXT (context), NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
@@ -1062,12 +1026,9 @@ _g_usb_context_lookup_vendor (GUsbContext *context,
G_USB_CONTEXT_ERROR,
G_USB_CONTEXT_ERROR_INTERNAL,
"failed to find vid %s", key);
- g_free (key);
return NULL;
}
- g_free (key);
-
return tmp;
}
@@ -1091,7 +1052,7 @@ _g_usb_context_lookup_product (GUsbContext *context,
GError **error)
{
const gchar *tmp;
- gchar *key = NULL;
+ g_autofree gchar *key = NULL;
g_return_val_if_fail (G_USB_IS_CONTEXT (context), NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
@@ -1108,12 +1069,9 @@ _g_usb_context_lookup_product (GUsbContext *context,
G_USB_CONTEXT_ERROR,
G_USB_CONTEXT_ERROR_INTERNAL,
"failed to find vid %s", key);
- g_free (key);
return NULL;
}
- g_free (key);
-
return tmp;
}
@@ -1144,6 +1102,8 @@ g_usb_context_replug_timeout_cb (gpointer user_data)
return FALSE;
}
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(GUsbContextReplugHelper, g_usb_context_replug_helper_free)
+
/**
* g_usb_context_wait_for_replug:
* @context: a #GUsbContext
@@ -1167,10 +1127,9 @@ g_usb_context_wait_for_replug (GUsbContext *context,
guint timeout_ms,
GError **error)
{
- GUsbDevice *device_new = NULL;
GUsbContextPrivate *priv = context->priv;
- GUsbContextReplugHelper *replug_helper;
const gchar *platform_id;
+ g_autoptr(GUsbContextReplugHelper) replug_helper = NULL;
g_return_val_if_fail (G_USB_IS_CONTEXT (context), NULL);
@@ -1200,12 +1159,9 @@ g_usb_context_wait_for_replug (GUsbContext *context,
G_USB_CONTEXT_ERROR,
G_USB_CONTEXT_ERROR_INTERNAL,
"request timed out");
- goto out;
+ return NULL;
}
- device_new = g_object_ref (replug_helper->device);
-out:
- g_usb_context_replug_helper_free (replug_helper);
- return device_new;
+ return g_object_ref (replug_helper->device);
}
/**
diff --git a/gusb/gusb-device.c b/gusb/gusb-device.c
index 2f814ae..81f90ad 100644
--- a/gusb/gusb-device.c
+++ b/gusb/gusb-device.c
@@ -261,7 +261,7 @@ g_usb_device_initable_init (GInitable *initable,
priv = device->priv;
- if (!priv->device) {
+ if (priv->device == NULL) {
g_set_error_literal (error, G_USB_DEVICE_ERROR, G_USB_DEVICE_ERROR_INTERNAL,
"Constructed without a libusb_device");
return FALSE;
@@ -469,7 +469,6 @@ g_usb_device_get_custom_index (GUsbDevice *device,
const struct libusb_interface_descriptor *ifp;
gint rc;
guint8 idx = 0x00;
- guint i;
struct libusb_config_descriptor *config;
rc = libusb_get_active_config_descriptor (device->priv->device, &config);
@@ -477,7 +476,7 @@ g_usb_device_get_custom_index (GUsbDevice *device,
return 0x00;
/* find the right data */
- for (i = 0; i < config->bNumInterfaces; i++) {
+ for (guint i = 0; i < config->bNumInterfaces; i++) {
ifp = &config->interface[i].altsetting[0];
if (ifp->bInterfaceClass != class_id)
continue;
@@ -530,7 +529,6 @@ g_usb_device_get_interface (GUsbDevice *device,
const struct libusb_interface_descriptor *ifp;
gint rc;
GUsbInterface *interface = NULL;
- guint i;
struct libusb_config_descriptor *config;
g_return_val_if_fail (G_USB_IS_DEVICE (device), NULL);
@@ -541,7 +539,7 @@ g_usb_device_get_interface (GUsbDevice *device,
return NULL;
/* find the right data */
- for (i = 0; i < config->bNumInterfaces; i++) {
+ for (guint i = 0; i < config->bNumInterfaces; i++) {
ifp = &config->interface[i].altsetting[0];
if (ifp->bInterfaceClass != class_id)
continue;
@@ -583,8 +581,6 @@ g_usb_device_get_interfaces (GUsbDevice *device, GError **error)
{
const struct libusb_interface_descriptor *ifp;
gint rc;
- guint i;
- guint j;
struct libusb_config_descriptor *config;
GPtrArray *array = NULL;
@@ -597,9 +593,9 @@ g_usb_device_get_interfaces (GUsbDevice *device, GError **error)
/* get all interfaces */
array = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
- for (i = 0; i < config->bNumInterfaces; i++) {
+ for (guint i = 0; i < config->bNumInterfaces; i++) {
GUsbInterface *interface = NULL;
- for (j = 0; j < (guint) config->interface[i].num_altsetting; j++) {
+ for (guint j = 0; j < (guint) config->interface[i].num_altsetting; j++) {
ifp = &config->interface[i].altsetting[j];
interface = _g_usb_interface_new (ifp);
g_ptr_array_add (array, interface);
@@ -1705,20 +1701,17 @@ g_usb_device_get_children (GUsbDevice *device)
GPtrArray *children;
GUsbDevice *device_tmp;
GUsbDevicePrivate *priv = device->priv;
- guint i;
- GPtrArray *devices = NULL;
+ g_autoptr(GPtrArray) devices = NULL;
/* find any devices that have @device as a parent */
children = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
devices = g_usb_context_get_devices (priv->context);
- for (i = 0; i < devices->len; i++) {
+ for (guint i = 0; i < devices->len; i++) {
device_tmp = g_ptr_array_index (devices, i);
if (priv->device == libusb_get_parent (device_tmp->priv->device))
g_ptr_array_add (children, g_object_ref (device_tmp));
}
- g_ptr_array_unref (devices);
-
return children;
}
diff --git a/gusb/gusb-self-test.c b/gusb/gusb-self-test.c
index 250176e..f078e1e 100644
--- a/gusb/gusb-self-test.c
+++ b/gusb/gusb-self-test.c
@@ -7,15 +7,16 @@
#include "config.h"
+#include "gusb-autocleanups.h"
#include "gusb-context-private.h"
static void
gusb_device_func (void)
{
- GError *error = NULL;
- GPtrArray *array;
- GUsbContext *ctx;
GUsbDevice *device;
+ g_autoptr(GError) error = NULL;
+ g_autoptr(GPtrArray) array = NULL;
+ g_autoptr(GUsbContext) ctx = NULL;
#ifdef __FreeBSD__
g_test_skip ("Root hubs on FreeBSD have vid and pid set to zero");
@@ -35,16 +36,14 @@ gusb_device_func (void)
g_assert_cmpint (g_usb_device_get_vid (device), >, 0x0000);
g_assert_cmpint (g_usb_device_get_pid (device), >, 0x0000);
-
- g_ptr_array_unref (array);
}
static void
gusb_context_lookup_func (void)
{
- GUsbContext *ctx = NULL;
GError *error = NULL;
const gchar *tmp;
+ g_autoptr(GUsbContext) ctx = NULL;
ctx = g_usb_context_new (&error);
g_assert_no_error (error);
@@ -56,21 +55,17 @@ gusb_context_lookup_func (void)
tmp = _g_usb_context_lookup_product (ctx, 0x04d8, 0xf8da, &error);
g_assert_no_error (error);
g_assert_cmpstr (tmp, ==, "Hughski Ltd. ColorHug");
- g_object_unref (ctx);
}
static void
gusb_context_func (void)
{
- GUsbContext *ctx;
- GError *error = NULL;
GPtrArray *array;
guint old_number_of_devices;
guint8 bus, address;
GUsbDevice *device;
- gchar *manufacturer;
- gchar *product;
- guint i;
+ g_autoptr(GError) error = NULL;
+ g_autoptr(GUsbContext) ctx = NULL;
#ifdef __FreeBSD__
g_test_skip ("Root hubs on FreeBSD have vid and pid set to zero");
@@ -91,7 +86,10 @@ gusb_context_func (void)
/* Print a list (also exercising various bits of g_usb_device) */
g_print ("\n");
- for (i = 0; i < array->len; i++) {
+ for (guint i = 0; i < array->len; i++) {
+ g_autofree gchar *manufacturer = NULL;
+ g_autofree gchar *product = NULL;
+
device = G_USB_DEVICE (g_ptr_array_index (array, i));
g_assert_cmpint (g_usb_device_get_vid (device), >, 0x0000);
@@ -120,9 +118,6 @@ gusb_context_func (void)
g_usb_device_get_pid (device),
manufacturer ? manufacturer : "",
product ? product : "");
-
- g_free (manufacturer);
- g_free (product);
}
g_ptr_array_unref (array);
@@ -155,20 +150,17 @@ gusb_context_func (void)
G_USB_DEVICE_ERROR,
G_USB_DEVICE_ERROR_NO_DEVICE);
g_assert (device == NULL);
- g_clear_error (&error);
-
- g_object_unref (ctx);
}
static void
gusb_device_huey_func (void)
{
- GUsbContext *ctx;
- GError *error = NULL;
- GUsbDevice *device;
gboolean ret;
GCancellable *cancellable = NULL;
const gchar request[8] = { 0x0e, 'G', 'r', 'M', 'b', 'k', 'e', 'd' };
+ g_autoptr(GError) error = NULL;
+ g_autoptr(GUsbContext) ctx = NULL;
+ g_autoptr(GUsbDevice) device = NULL;
ctx = g_usb_context_new (&error);
g_assert_no_error (error);
@@ -185,8 +177,7 @@ gusb_device_huey_func (void)
error->domain == G_USB_DEVICE_ERROR &&
error->code == G_USB_DEVICE_ERROR_NO_DEVICE) {
g_print ("No device detected!\n");
- g_error_free (error);
- goto out;
+ return;
}
g_assert_no_error (error);
g_assert (device != NULL);
@@ -267,10 +258,6 @@ gusb_device_huey_func (void)
ret = g_usb_device_close (device, &error);
g_assert_no_error (error);
g_assert (ret);
-
- g_object_unref (device);
-out:
- g_object_unref (ctx);
}
typedef struct {
@@ -285,19 +272,17 @@ g_usb_device_print_data (const gchar *title,
const guchar *data,
gsize length)
{
- guint i;
-
if (g_strcmp0 (title, "request") == 0)
g_print ("%c[%dm", 0x1B, 31);
if (g_strcmp0 (title, "reply") == 0)
g_print ("%c[%dm", 0x1B, 34);
g_print ("%s\t", title);
- for (i=0; i< length; i++)
+ for (guint i = 0; i< length; i++) {
g_print ("%02x [%c]\t",
data[i],
g_ascii_isprint (data[i]) ? data[i] : '?');
-
+ }
g_print ("%c[%dm\n", 0x1B, 0);
}
@@ -307,15 +292,14 @@ g_usb_test_button_pressed_cb (GObject *source_object,
gpointer user_data)
{
gboolean ret;
- GError *error = NULL;
GUsbDeviceAsyncHelper *helper = (GUsbDeviceAsyncHelper *) user_data;
+ g_autoptr(GError) error = NULL;
ret = g_usb_device_interrupt_transfer_finish (G_USB_DEVICE (source_object),
res, &error);
if (!ret) {
g_error ("%s", error->message);
- g_error_free (error);
return;
}
@@ -329,13 +313,13 @@ g_usb_test_button_pressed_cb (GObject *source_object,
static void
gusb_device_munki_func (void)
{
- GError *error = NULL;
- GUsbContext *ctx;
- GUsbDevice *device;
gboolean ret;
GCancellable *cancellable = NULL;
guint8 request[24];
GUsbDeviceAsyncHelper *helper;
+ g_autoptr(GError) error = NULL;
+ g_autoptr(GUsbContext) ctx = NULL;
+ g_autoptr(GUsbDevice) device = NULL;
ctx = g_usb_context_new (&error);
g_assert_no_error (error);
@@ -352,8 +336,7 @@ gusb_device_munki_func (void)
error->domain == G_USB_DEVICE_ERROR &&
error->code == G_USB_DEVICE_ERROR_NO_DEVICE) {
g_print ("No device detected!\n");
- g_error_free (error);
- goto out;
+ return;
}
g_assert_no_error (error);
g_assert (device != NULL);
@@ -450,21 +433,17 @@ gusb_device_munki_func (void)
ret = g_usb_device_close (device, &error);
g_assert_no_error (error);
g_assert (ret);
-
- g_object_unref (device);
-out:
- g_object_unref (ctx);
}
static void
gusb_device_ch2_func (void)
{
- GError *error = NULL;
- GUsbContext *ctx;
- GUsbDevice *device;
gboolean ret;
- gchar *tmp = NULL;
guint8 idx;
+ g_autofree gchar *tmp = NULL;
+ g_autoptr(GError) error = NULL;
+ g_autoptr(GUsbContext) ctx = NULL;
+ g_autoptr(GUsbDevice) device = NULL;
ctx = g_usb_context_new (&error);
g_assert_no_error (error);
@@ -481,8 +460,7 @@ gusb_device_ch2_func (void)
error->domain == G_USB_DEVICE_ERROR &&
error->code == G_USB_DEVICE_ERROR_NO_DEVICE) {
g_print ("No device detected!\n");
- g_error_free (error);
- goto out;
+ return;
}
g_assert_no_error (error);
g_assert (device != NULL);
@@ -503,16 +481,11 @@ gusb_device_ch2_func (void)
tmp = g_usb_device_get_string_descriptor (device, idx, &error);
g_assert_no_error (error);
g_assert_cmpstr (tmp, ==, "2.0.3");
- g_free (tmp);
/* close */
ret = g_usb_device_close (device, &error);
g_assert_no_error (error);
g_assert (ret);
-
- g_object_unref (device);
-out:
- g_object_unref (ctx);
}
int
diff --git a/tools/gusb-main.c b/tools/gusb-main.c
index 55be333..8caad29 100644
--- a/tools/gusb-main.c
+++ b/tools/gusb-main.c
@@ -77,14 +77,9 @@ gusb_sort_command_name_cb (GUsbCmdItem **item1, GUsbCmdItem **item2)
static void
gusb_cmd_add (GPtrArray *array, const gchar *name, const gchar *description, GUsbCmdPrivateCb callback)
{
- gchar **names;
- guint i;
- GUsbCmdItem *item;
-
- /* add each one */
- names = g_strsplit (name, ",", -1);
- for (i=0; names[i] != NULL; i++) {
- item = g_slice_new0 (GUsbCmdItem);
+ g_auto(GStrv) names = g_strsplit (name, ",", -1);
+ for (guint i = 0; names[i] != NULL; i++) {
+ GUsbCmdItem *item = g_slice_new0 (GUsbCmdItem);
item->name = g_strdup (names[i]);
if (i == 0) {
item->description = g_strdup (description);
@@ -96,28 +91,24 @@ gusb_cmd_add (GPtrArray *array, const gchar *name, const gchar *description, GUs
item->callback = callback;
g_ptr_array_add (array, item);
}
- g_strfreev (names);
}
static gchar *
gusb_cmd_get_descriptions (GPtrArray *array)
{
- guint i;
- guint j;
guint len;
guint max_len = 19;
- GUsbCmdItem *item;
- GString *string;
+ g_autoptr(GString) string = NULL;
/* print each command */
string = g_string_new ("");
- for (i = 0; i < array->len; i++) {
- item = g_ptr_array_index (array, i);
+ for (guint i = 0; i < array->len; i++) {
+ GUsbCmdItem *item = g_ptr_array_index (array, i);
g_string_append (string, " ");
g_string_append (string, item->name);
g_string_append (string, " ");
len = strlen (item->name);
- for (j = len; j < max_len+2; j++)
+ for (guint j = len; j < max_len+2; j++)
g_string_append_c (string, ' ');
g_string_append (string, item->description);
g_string_append_c (string, '\n');
@@ -127,33 +118,30 @@ gusb_cmd_get_descriptions (GPtrArray *array)
if (string->len > 0)
g_string_set_size (string, string->len - 1);
- return g_string_free (string, FALSE);
+ return g_string_free (g_steal_pointer(&string), FALSE);
}
static void
gusb_main_device_open (GUsbDevice *device)
{
- GError *error = NULL;
guint8 idx;
+ g_autoptr(GError) error = NULL;
/* open */
if (!g_usb_device_open (device, &error)) {
g_print ("failed to open: %s\n", error->message);
- g_error_free (error);
return;
}
/* print info we can only get whilst open */
idx = g_usb_device_get_product_index (device);
if (idx != 0x00) {
- gchar *product = g_usb_device_get_string_descriptor (device, idx, &error);
+ g_autofree gchar *product = g_usb_device_get_string_descriptor (device, idx, &error);
if (product == NULL) {
g_print ("failed to get string desc: %s\n", error->message);
- g_error_free (error);
return;
}
g_print ("product: %s\n", product);
- g_free (product);
}
}
@@ -193,12 +181,10 @@ static gboolean
moo_cb (GNode *node, gpointer data)
{
GUsbDevice *device = G_USB_DEVICE (node->data);
- GNode *n;
- guint i;
const gchar *tmp;
- gchar *product = NULL;
- gchar *vendor = NULL;
- GString *str = NULL;
+ g_autofree gchar *product = NULL;
+ g_autofree gchar *vendor = NULL;
+ g_autoptr(GString) str = NULL;
if (device == NULL) {
g_print ("Root Device\n");
@@ -207,7 +193,7 @@ moo_cb (GNode *node, gpointer data)
/* indent */
str = g_string_new ("");
- for (n = node; n->data != NULL; n = n->parent)
+ for (GNode *n = node; n->data != NULL; n = n->parent)
g_string_append (str, " ");
/* add bus:address */
@@ -218,7 +204,7 @@ moo_cb (GNode *node, gpointer data)
g_usb_device_get_pid (device));
/* pad */
- for (i = str->len; i < 30; i++)
+ for (guint i = str->len; i < 30; i++)
g_string_append (str, " ");
/* We don't error check these as not all devices have these
@@ -258,24 +244,16 @@ moo_cb (GNode *node, gpointer data)
/* add bus:address */
g_string_append_printf (str, "%s - %s", vendor, product);
- g_free (product);
- g_free (vendor);
g_print ("%s\n", str->str);
- g_string_free (str, TRUE);
-
return FALSE;
}
static gboolean
gusb_cmd_show (GUsbCmdPrivate *priv, gchar **values, GError **error)
{
- GNode *n;
- GNode *node;
- guint i;
- GUsbDevice *device;
- GUsbDevice *parent;
- GPtrArray *devices = NULL;
+ g_autoptr(GNode) node = NULL;
+ g_autoptr(GPtrArray) devices = NULL;
/* sort */
devices = g_usb_context_get_devices (priv->usb_ctx);
@@ -283,10 +261,10 @@ gusb_cmd_show (GUsbCmdPrivate *priv, gchar **values, GError **error)
/* make a tree of the devices */
node = g_node_new (NULL);
- for (i = 0; i < devices->len; i++) {
- device = g_ptr_array_index (devices, i);
-
- parent = g_usb_device_get_parent (device);
+ for (guint i = 0; i < devices->len; i++) {
+ GNode *n;
+ GUsbDevice *device = g_ptr_array_index (devices, i);
+ GUsbDevice *parent = g_usb_device_get_parent (device);
if (parent == NULL) {
g_node_append_data (node, device);
continue;
@@ -302,24 +280,19 @@ gusb_cmd_show (GUsbCmdPrivate *priv, gchar **values, GError **error)
}
- g_ptr_array_unref (devices);
g_node_traverse (node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, moo_cb, priv);
-
return TRUE;
}
static gboolean
gusb_cmd_watch (GUsbCmdPrivate *priv, gchar **values, GError **error)
{
- gboolean ret = TRUE;
- GPtrArray *devices;
- guint i;
- GUsbDevice *device;
- GMainLoop *loop;
+ g_autoptr(GMainLoop) loop = NULL;
+ g_autoptr(GPtrArray) devices = NULL;
devices = g_usb_context_get_devices (priv->usb_ctx);
- for (i = 0; i < devices->len; i++) {
- device = g_ptr_array_index (devices, i);
+ for (guint i = 0; i < devices->len; i++) {
+ GUsbDevice *device = g_ptr_array_index (devices, i);
g_print ("device %s already present %x:%x\n",
g_usb_device_get_platform_id (device),
g_usb_device_get_bus (device),
@@ -335,18 +308,15 @@ gusb_cmd_watch (GUsbCmdPrivate *priv, gchar **values, GError **error)
G_CALLBACK (gusb_device_list_removed_cb),
priv);
g_main_loop_run (loop);
-
- g_main_loop_unref (loop);
- g_ptr_array_unref (devices);
- return ret;
+ return TRUE;
}
static gboolean
gusb_cmd_replug (GUsbCmdPrivate *priv, gchar **values, GError **error)
{
- GUsbDevice *device;
- GUsbDevice *device_new;
guint16 vid, pid;
+ g_autoptr(GUsbDevice) device = NULL;
+ g_autoptr(GUsbDevice) device_new = NULL;
/* check args */
if (g_strv_length (values) != 2) {
@@ -376,54 +346,53 @@ gusb_cmd_replug (GUsbCmdPrivate *priv, gchar **values, GError **error)
device,
5000,
error);
- if (device_new == NULL)
- return FALSE;
-
- g_object_unref (device);
- return TRUE;
+ return device_new != NULL;
}
static gboolean
gusb_cmd_run (GUsbCmdPrivate *priv, const gchar *command, gchar **values, GError **error)
{
- gboolean ret = FALSE;
- guint i;
- GUsbCmdItem *item;
- GString *string;
+ g_autoptr(GString) string = g_string_new (NULL);
/* find command */
- for (i = 0; i < priv->cmd_array->len; i++) {
- item = g_ptr_array_index (priv->cmd_array, i);
+ for (guint i = 0; i < priv->cmd_array->len; i++) {
+ GUsbCmdItem *item = g_ptr_array_index (priv->cmd_array, i);
if (g_strcmp0 (item->name, command) == 0) {
- ret = item->callback (priv, values, error);
- goto out;
+ return item->callback (priv, values, error);
}
}
- /* not found */
- string = g_string_new ("");
/* TRANSLATORS: error message */
g_string_append_printf (string, "%s\n", "Command not found, valid commands are:");
- for (i = 0; i < priv->cmd_array->len; i++) {
- item = g_ptr_array_index (priv->cmd_array, i);
+ for (guint i = 0; i < priv->cmd_array->len; i++) {
+ GUsbCmdItem *item = g_ptr_array_index (priv->cmd_array, i);
g_string_append_printf (string, " * %s\n", item->name);
}
g_set_error_literal (error, 1, 0, string->str);
- g_string_free (string, TRUE);
-out:
- return ret;
+ return FALSE;
+}
+
+static void
+gusb_cmd_private_free(GUsbCmdPrivate *priv)
+{
+ if (priv->cmd_array != NULL)
+ g_ptr_array_unref (priv->cmd_array);
+ if (priv->usb_ctx != NULL)
+ g_object_unref (priv->usb_ctx);
+ g_option_context_free (priv->context);
+ g_slice_free (GUsbCmdPrivate, priv);
}
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(GUsbCmdPrivate, gusb_cmd_private_free)
+
int
main (int argc, char *argv[])
{
- gboolean ret;
gboolean verbose = FALSE;
- gchar *cmd_descriptions = NULL;
- gchar *options_help = NULL;
- GError *error = NULL;
- gint retval = 0;
- GUsbCmdPrivate *priv;
+ g_autofree gchar *cmd_descriptions = NULL;
+ g_autofree gchar *options_help = NULL;
+ g_autoptr(GError) error = NULL;
+ g_autoptr(GUsbCmdPrivate) priv = NULL;
const GOptionEntry options[] = {
{ "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
@@ -435,14 +404,11 @@ main (int argc, char *argv[])
/* create helper object */
priv = g_slice_new0 (GUsbCmdPrivate);
-
priv->context = g_option_context_new ("GUSB Console Program");
g_option_context_add_main_entries (priv->context, options, NULL);
if (!g_option_context_parse (priv->context, &argc, &argv, &error)) {
g_printerr ("Failed to parse arguments: %s\n", error->message);
- g_error_free (error);
- retval = 2;
- goto out;
+ return 2;
}
/* verbose? */
@@ -491,29 +457,15 @@ main (int argc, char *argv[])
if (argc < 2) {
options_help = g_option_context_get_help (priv->context, TRUE, NULL);
g_print ("%s", options_help);
- goto out;
+ return 1;
}
/* run the specified command */
- ret = gusb_cmd_run (priv, argv[1], (gchar**) &argv[2], &error);
- if (!ret) {
+ if (!gusb_cmd_run (priv, argv[1], (gchar**) &argv[2], &error)) {
g_print ("%s\n", error->message);
- g_error_free (error);
- retval = 1;
- goto out;
- }
-out:
- if (priv != NULL) {
- if (priv->cmd_array != NULL)
- g_ptr_array_unref (priv->cmd_array);
- if (priv->usb_ctx != NULL)
- g_object_unref (priv->usb_ctx);
- g_option_context_free (priv->context);
- g_slice_free (GUsbCmdPrivate, priv);
+ return 1;
}
- /* free state */
- g_free (options_help);
- g_free (cmd_descriptions);
- return retval;
+ /* success */
+ return 0;
}