forked from ryanflannery/vitunes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ecmd.c
93 lines (84 loc) · 2.74 KB
/
ecmd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
* Copyright (c) 2010, 2011, 2012 Ryan Flannery <[email protected]>
* Copyright (c) 2013 Tiago Cunha <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "ecmd.h"
static int
ecmd_parse(const struct ecmd *ecmd, int argc, char ***argv)
{
/* reset getopt(3) variables */
optind = 0;
optreset = 1;
/* parse command line and if valid, skip parsed arguments */
if (ecmd->parse != NULL) {
/* parse error */
if (ecmd->parse(argc, *argv) == -1)
return -1;
if (ecmd->check != NULL && ecmd->check() == -1)
return -1;
argc -= optind;
*argv += optind;
} else {
/* no parse function; skip only its name */
argc--;
(*argv)++;
}
/* invalid number of arguments */
if (argc < ecmd->args_lower)
return -1;
if (ecmd->args_upper >= 0 && argc > ecmd->args_upper)
return -1;
/* return updated (no name) number of arguments */
return argc;
}
int
ecmd_exec(const char *ecmd, int argc, char **argv)
{
/* set of e-commands */
static const struct ecmd *ecmdtab[] = {
&ecmd_add,
&ecmd_addurl,
&ecmd_check,
&ecmd_flush,
&ecmd_help,
&ecmd_init,
&ecmd_rmfile,
&ecmd_tag,
&ecmd_update,
};
static const int ecmdtab_size = sizeof ecmdtab / sizeof ecmdtab[0];
int i;
/* search for e-command (first by name and then by alias) */
for (i = 0; i < ecmdtab_size; i++) {
if (strcmp(ecmd, ecmdtab[i]->name) == 0)
break;
if (ecmdtab[i]->alias != NULL && strcmp(ecmd, ecmdtab[i]->alias) == 0)
break;
}
/* not found; bail out */
if (i == ecmdtab_size) {
warnx("Unknown e-command '%s'. See 'vitunes -e help' for list.", ecmd);
return -1;
}
/* parse e-command arguments */
if ((argc = ecmd_parse(ecmdtab[i], argc, &argv)) == -1) {
fprintf(stderr, "usage: %s -e %s %s\n", progname, ecmdtab[i]->name,
ecmdtab[i]->usage != NULL ? ecmdtab[i]->usage : "");
return 1;
}
/* finally execute it */
ecmdtab[i]->exec(argc, argv);
return 0;
}