]>
Commit | Line | Data |
---|---|---|
d7867b31 KS |
1 | /* |
2 | * Copyright (C) 2007-2009 Kay Sievers <kay.sievers@vrfy.org> | |
3 | * | |
4 | * This program is free software: you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation, either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | */ | |
17 | ||
18 | #include <unistd.h> | |
19 | #include <stdio.h> | |
20 | #include <stdlib.h> | |
21 | #include <stddef.h> | |
22 | #include <string.h> | |
23 | #include <errno.h> | |
24 | #include <getopt.h> | |
25 | ||
26 | #include "udev.h" | |
27 | ||
28 | static const struct udev_builtin *builtins[] = { | |
29 | [UDEV_BUILTIN_PATH_ID] = &udev_builtin_path_id, | |
30 | [UDEV_BUILTIN_USB_ID] = &udev_builtin_usb_id, | |
31 | [UDEV_BUILTIN_INPUT_ID] = &udev_builtin_input_id, | |
81dadce5 KS |
32 | [UDEV_BUILTIN_BLKID] = &udev_builtin_blkid, |
33 | [UDEV_BUILTIN_KMOD] = &udev_builtin_kmod, | |
d7867b31 KS |
34 | }; |
35 | ||
aa29418a KS |
36 | int udev_builtin_load(struct udev *udev) |
37 | { | |
38 | unsigned int i; | |
39 | ||
40 | for (i = 0; i < ARRAY_SIZE(builtins); i++) | |
41 | if (builtins[i]->load) | |
42 | builtins[i]->load(udev); | |
43 | return 0; | |
44 | } | |
45 | ||
46 | int udev_builtin_unload(struct udev *udev) | |
47 | { | |
48 | unsigned int i; | |
49 | ||
50 | for (i = 0; i < ARRAY_SIZE(builtins); i++) | |
51 | if (builtins[i]->unload) | |
52 | builtins[i]->unload(udev); | |
53 | return 0; | |
54 | } | |
55 | ||
d7867b31 KS |
56 | int udev_builtin_list(struct udev *udev) |
57 | { | |
58 | unsigned int i; | |
59 | ||
60 | for (i = 0; i < ARRAY_SIZE(builtins); i++) | |
61 | fprintf(stderr, " %-12s %s\n", builtins[i]->name, builtins[i]->help); | |
62 | return 0; | |
63 | } | |
64 | ||
65 | const char *udev_builtin_name(enum udev_builtin_cmd cmd) | |
66 | { | |
67 | return builtins[cmd]->name; | |
68 | } | |
69 | ||
81dadce5 | 70 | bool udev_builtin_run_once(enum udev_builtin_cmd cmd) |
d7867b31 | 71 | { |
81dadce5 KS |
72 | return builtins[cmd]->run_once; |
73 | } | |
74 | ||
75 | enum udev_builtin_cmd udev_builtin_lookup(const char *command) | |
76 | { | |
77 | char name[UTIL_PATH_SIZE]; | |
d7867b31 | 78 | enum udev_builtin_cmd i; |
81dadce5 | 79 | char *pos; |
d7867b31 | 80 | |
81dadce5 KS |
81 | util_strscpy(name, sizeof(name), command); |
82 | pos = strchr(name, ' '); | |
83 | if (pos) | |
84 | pos[0] = '\0'; | |
d7867b31 KS |
85 | for (i = 0; i < ARRAY_SIZE(builtins); i++) |
86 | if (strcmp(builtins[i]->name, name) == 0) | |
87 | return i; | |
88 | return UDEV_BUILTIN_MAX; | |
89 | } | |
90 | ||
81dadce5 | 91 | int udev_builtin_run(struct udev_device *dev, enum udev_builtin_cmd cmd, const char *command, bool test) |
d7867b31 | 92 | { |
e216e514 KS |
93 | char arg[UTIL_PATH_SIZE]; |
94 | int argc; | |
95 | char *argv[128]; | |
96 | ||
97 | util_strscpy(arg, sizeof(arg), command); | |
98 | udev_build_argv(udev_device_get_udev(dev), arg, &argc, argv); | |
99 | return builtins[cmd]->cmd(dev, argc, argv, test); | |
d7867b31 KS |
100 | } |
101 | ||
102 | int udev_builtin_add_property(struct udev_device *dev, bool test, const char *key, const char *val, ...) | |
103 | { | |
104 | struct udev_list_entry *entry; | |
105 | ||
106 | entry = udev_device_add_property(dev, key, val); | |
107 | /* store in db, skip private keys */ | |
108 | if (key[0] != '.') | |
109 | udev_list_entry_set_num(entry, true); | |
110 | ||
111 | info(udev_device_get_udev(dev), "%s=%s\n", key, val); | |
112 | if (test) | |
113 | printf("%s=%s\n", key, val); | |
114 | return 0; | |
115 | } |