chiark / gitweb /
Prep v228: With most functions split out, clean up the enormous includes list in...
[elogind.git] / src / basic / verbs.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2014 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include "string-util.h"
23 #include "util.h"
24 #include "verbs.h"
25
26 int dispatch_verb(int argc, char *argv[], const Verb verbs[], void *userdata) {
27         const Verb *verb;
28         const char *name;
29         unsigned i;
30         int left;
31
32         assert(verbs);
33         assert(verbs[0].dispatch);
34         assert(argc >= 0);
35         assert(argv);
36         assert(argc >= optind);
37
38         left = argc - optind;
39         name = argv[optind];
40
41         for (i = 0;; i++) {
42                 bool found;
43
44                 /* At the end of the list? */
45                 if (!verbs[i].dispatch) {
46                         if (name)
47                                 log_error("Unknown operation %s.", name);
48                         else
49                                 log_error("Requires operation parameter.");
50                         return -EINVAL;
51                 }
52
53                 if (name)
54                         found = streq(name, verbs[i].verb);
55                 else
56                         found = !!(verbs[i].flags & VERB_DEFAULT);
57
58                 if (found) {
59                         verb = &verbs[i];
60                         break;
61                 }
62         }
63
64         assert(verb);
65
66         if (!name)
67                 left = 1;
68
69         if (verb->min_args != VERB_ANY &&
70             (unsigned) left < verb->min_args) {
71                 log_error("Too few arguments.");
72                 return -EINVAL;
73         }
74
75         if (verb->max_args != VERB_ANY &&
76             (unsigned) left > verb->max_args) {
77                 log_error("Too many arguments.");
78                 return -EINVAL;
79         }
80
81         if (name)
82                 return verb->dispatch(left, argv + optind, userdata);
83         else {
84                 char* fake[2] = {
85                         (char*) verb->verb,
86                         NULL
87                 };
88
89                 return verb->dispatch(1, fake, userdata);
90         }
91 }