Lucas De Marchi | 8900b91 | 2011-12-22 02:33:36 -0200 | [diff] [blame^] | 1 | /* |
| 2 | * kmod - one tool to rule them all |
| 3 | * |
| 4 | * Copyright (C) 2011 ProFUSION embedded systems |
| 5 | * |
| 6 | * This program is free software: you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation, either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <errno.h> |
| 23 | #include <string.h> |
| 24 | #include <libkmod.h> |
| 25 | #include "kmod.h" |
| 26 | |
| 27 | static const struct kmod_cmd kmod_cmd_help; |
| 28 | |
| 29 | static const struct kmod_cmd *kmod_cmds[] = { |
| 30 | &kmod_cmd_help, |
| 31 | }; |
| 32 | |
| 33 | static int kmod_help(int argc, char *argv[]) |
| 34 | { |
| 35 | size_t i; |
| 36 | |
| 37 | puts("Manage kernel modules: list, load, unload, etc\n" |
| 38 | "Usage: kmod COMMAND [COMMAND_OPTIONS]\n\n" |
| 39 | "Available commands:"); |
| 40 | |
| 41 | for (i = 0; i < ARRAY_SIZE(kmod_cmds); i++) { |
| 42 | if (kmod_cmds[i]->help != NULL) { |
| 43 | printf(" %-12s %s\n", kmod_cmds[i]->name, |
| 44 | kmod_cmds[i]->help); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | return EXIT_SUCCESS; |
| 49 | } |
| 50 | |
| 51 | static const struct kmod_cmd kmod_cmd_help = { |
| 52 | .name = "help", |
| 53 | .cmd = kmod_help, |
| 54 | .help = "Show help message", |
| 55 | }; |
| 56 | |
| 57 | int main(int argc, char *argv[]) |
| 58 | { |
| 59 | const char *cmd; |
| 60 | int err = 0; |
| 61 | size_t i; |
| 62 | |
| 63 | if (argc < 2) { |
| 64 | err = -ENOENT; |
| 65 | goto finish; |
| 66 | } |
| 67 | |
| 68 | cmd = argv[1]; |
| 69 | |
| 70 | for (i = 0; i < ARRAY_SIZE(kmod_cmds); i++) { |
| 71 | if (strcmp(kmod_cmds[i]->name, cmd) != 0) |
| 72 | continue; |
| 73 | |
| 74 | err = kmod_cmds[i]->cmd(--argc, ++argv); |
| 75 | } |
| 76 | |
| 77 | finish: |
| 78 | if (err < 0) { |
| 79 | fputs("missing or unknown command; " |
| 80 | "see 'kmod help' for a list of available commands\n", stderr); |
| 81 | } |
| 82 | |
| 83 | return err; |
| 84 | } |