chiark / gitweb /
builtin: move usb-db, pci-db to builtins
[elogind.git] / udev / udev-builtin-hwdb.c
similarity index 56%
rename from extras/usb-db/usb-db.c
rename to udev/udev-builtin-hwdb.c
index fe563c2e758f70d2cec5d91e6dbbc78f77414e3e..e38c6b9d268c3afd5767873430d708cff4bb00b4 100644 (file)
@@ -1,15 +1,21 @@
 /*
+ * usb-db, pci-db - lookup vendor/product database
+ *
  * Copyright (C) 2009 Lennart Poettering <lennart@poettering.net>
+ * Copyright (C) 2011 Kay Sievers <kay.sievers@vrfy.org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
  *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of the
- * License, or (at your option) any later version.
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
  *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * General Public License for more details:
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
 #include <stdio.h>
 #include <ctype.h>
 #include <stdlib.h>
 
-#include <libudev.h>
-
-#if defined(BUILD_FOR_USB)
-# define DATABASE USB_DATABASE
-# define SUBSYSTEM "usb"
-# define DEVTYPE "usb_device"
-# define VENDOR_ATTR "idVendor"
-# define PRODUCT_ATTR "idProduct"
-#elif defined(BUILD_FOR_PCI)
-# define DATABASE PCI_DATABASE
-# define SUBSYSTEM "pci"
-# define DEVTYPE NULL
-# define VENDOR_ATTR "vendor"
-# define PRODUCT_ATTR "device"
-#else
-# error "Are you havin' a laugh?"
-#endif
+#include "udev.h"
 
 static int get_id_attr(
        struct udev_device *parent,
@@ -64,17 +54,19 @@ static int get_id_attr(
 
 static int get_vid_pid(
        struct udev_device *parent,
+       const char *vendor_attr,
+       const char *product_attr,
        uint16_t *vid,
        uint16_t *pid) {
 
-       if (get_id_attr(parent, VENDOR_ATTR, vid) < 0)
+       if (get_id_attr(parent, vendor_attr, vid) < 0)
                return -1;
        else if (*vid <= 0) {
                fprintf(stderr, "Invalid vendor id.\n");
                return -1;
        }
 
-       if (get_id_attr(parent, PRODUCT_ATTR, pid) < 0)
+       if (get_id_attr(parent, product_attr, pid) < 0)
                return -1;
 
        return 0;
@@ -89,12 +81,10 @@ static void rstrip(char *n) {
 
 #define HEXCHARS "0123456789abcdefABCDEF"
 #define WHITESPACE " \t\n\r"
-
-static int lookup_vid_pid(
-       uint16_t vid,
-       uint16_t pid,
-       char **vendor,
-       char **product) {
+static int lookup_vid_pid(const char *database,
+                         uint16_t vid, uint16_t pid,
+                         char **vendor, char **product)
+{
 
        FILE *f;
        int ret = -1;
@@ -103,8 +93,8 @@ static int lookup_vid_pid(
 
        *vendor = *product = NULL;
 
-       if (!(f = fopen(DATABASE, "r"))) {
-               fprintf(stderr, "Failed to open database file "DATABASE": %s\n", strerror(errno));
+       if (!(f = fopen(database, "rme"))) {
+               fprintf(stderr, "Failed to open database file '%s': %s\n", database, strerror(errno));
                return -1;
        }
 
@@ -201,64 +191,62 @@ static struct udev_device *find_device(struct udev_device *dev, const char *subs
        }
        return dev;
 try_parent:
-       return udev_device_get_parent_with_subsystem_devtype(dev, SUBSYSTEM, DEVTYPE);
+       return udev_device_get_parent_with_subsystem_devtype(dev, subsys, devtype);
 }
 
-int main(int argc, char*argv[]) {
 
-       struct udev *udev = NULL;
-       int ret = 1;
-       char *sp;
-       struct udev_device *dev = NULL, *parent = NULL;
+static int builtin_db(struct udev_device *dev, bool test,
+                     const char *database,
+                     const char *vendor_attr, const char *product_attr,
+                     const char *subsys, const char *devtype)
+{
+       struct udev_device *parent;
        uint16_t vid = 0, pid = 0;
        char *vendor = NULL, *product = NULL;
 
-       if (argc < 2) {
-               fprintf(stderr, "Need to pass sysfs path.\n");
-               goto finish;
-       }
-
-       if (!(udev = udev_new()))
-               goto finish;
-
-       if (asprintf(&sp, "%s%s", udev_get_sys_path(udev), argv[1]) < 0) {
-               fprintf(stderr, "Failed to allocate sysfs path.\n");
-               goto finish;
-       }
-
-       dev = udev_device_new_from_syspath(udev, sp);
-       free(sp);
-
-       if (!dev) {
-               fprintf(stderr, "Failed to access %s.\n", argv[1]);
-               goto finish;
-       }
-
-       parent = find_device(dev, SUBSYSTEM, DEVTYPE);
+       parent = find_device(dev, subsys, devtype);
        if (!parent) {
                fprintf(stderr, "Failed to find device.\n");
                goto finish;
        }
 
-       if (get_vid_pid(parent, &vid, &pid) < 0)
+       if (get_vid_pid(parent, vendor_attr, product_attr, &vid, &pid) < 0)
                goto finish;
 
-       if (lookup_vid_pid(vid, pid, &vendor, &product) < 0)
+       if (lookup_vid_pid(database, vid, pid, &vendor, &product) < 0)
                goto finish;
 
        if (vendor)
-               printf("ID_VENDOR_FROM_DATABASE=%s\n", vendor);
-
+               udev_builtin_add_property(dev, test, "ID_VENDOR_FROM_DATABASE", vendor);
        if (product)
-               printf("ID_MODEL_FROM_DATABASE=%s\n", product);
-
-       ret = 0;
+               udev_builtin_add_property(dev, test, "ID_MODEL_FROM_DATABASE", product);
 
 finish:
-       udev_device_unref(dev);
-       udev_unref(udev);
        free(vendor);
        free(product);
+       return 0;
+}
 
-       return ret;
+static int builtin_usb_db(struct udev_device *dev, int argc, char *argv[], bool test)
+{
+       return builtin_db(dev, test, USB_DATABASE, "idVendor", "idProduct", "usb", "usb_device");
 }
+
+static int builtin_pci_db(struct udev_device *dev, int argc, char *argv[], bool test)
+{
+       return builtin_db(dev, test, PCI_DATABASE, "vendor", "device", "pci", NULL);
+}
+
+const struct udev_builtin udev_builtin_usb_db = {
+       .name = "usb-db",
+       .cmd = builtin_usb_db,
+       .help = "USB vendor/product database",
+       .run_once = true,
+};
+
+const struct udev_builtin udev_builtin_pci_db = {
+       .name = "pci-db",
+       .cmd = builtin_pci_db,
+       .help = "PCI vendor/product database",
+       .run_once = true,
+};