initial import of fs-tools package
[project/fstools.git] / fs-state.c
1 /*
2  * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License version 2.1
6  * as published by the Free Software Foundation
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13
14 #include <stdio.h>
15 #include <string.h>
16
17 #include "fs-state.h"
18
19 static LIST_HEAD(backends);
20
21 void
22 register_backend(struct backend *b)
23 {
24         list_add(&b->list, &backends);
25 }
26
27 struct backend*
28 find_backend(char *name)
29 {
30         struct backend *b;
31
32         list_for_each_entry(b, &backends, list)
33                 if (!strcmp(name, b->name))
34                         return b;
35         return NULL;
36 }
37
38 static void
39 help(void)
40 {
41         struct backend *b;
42
43         list_for_each_entry(b, &backends, list) {
44                 int i;
45
46                 if (b->desc)
47                         fprintf(stderr, "-> %s\n", b->name);
48                 for (i = 0; i < b->num_handlers; i++)
49                         if (b->handlers[i].desc)
50                                 fprintf(stderr, "--> %s\n", b->handlers[i].name);
51         }
52 }
53
54 int
55 main(int argc, char **argv)
56 {
57         struct backend *b;
58
59         if (argc > 1) list_for_each_entry(b, &backends, list) {
60                 int i;
61
62                 srand(time(NULL));
63
64                 if (strcmp(argv[1], b->name))
65                         continue;
66
67                 for (i = 0; i < b->num_handlers; i++)
68                         if (!strcmp(argv[2], b->handlers[i].name))
69                                 return b->handlers[i].cli(argc - 2, &argv[2]);
70
71                 if (b->cli)
72                         return b->cli(argc - 1, &argv[1]);
73
74                 break;
75         }
76
77         help();
78
79         return 0;
80 }