]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/verbs.c
util-lib: split our string related calls from util.[ch] into its own file string...
[thirdparty/systemd.git] / src / basic / verbs.c
CommitLineData
dca59f62
LP
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
07630cea 22#include "string-util.h"
dca59f62
LP
23#include "util.h"
24#include "verbs.h"
25
26int 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
43343ee7 53 if (name)
dca59f62 54 found = streq(name, verbs[i].verb);
43343ee7
LP
55 else
56 found = !!(verbs[i].flags & VERB_DEFAULT);
dca59f62
LP
57
58 if (found) {
59 verb = &verbs[i];
60 break;
61 }
62 }
63
43343ee7
LP
64 assert(verb);
65
66 if (!name)
67 left = 1;
68
dca59f62
LP
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) {
540d8581 77 log_error("Too many arguments.");
dca59f62
LP
78 return -EINVAL;
79 }
80
43343ee7
LP
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 }
dca59f62 91}