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
|
/* This file is part of genrc
Copyryght (C) 2018 Sergey Poznyakoff
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
*/
#include "genrc.h"
struct genrc_pid_source {
char *name;
size_t maxwords;
GENRC_PID_CLOSURE *(*init)(int argc, char **argv);
};
struct genrc_pid_source sourcetab[] = {
{ "FILE", 2, genrc_pid_file_init },
{ "CONFIG", 4, genrc_pid_config_init },
{ "GREP", 3, genrc_pid_grep_init },
{ "PROC", 3, genrc_pid_proc_init },
{ "PS", 3, genrc_pid_ps_init },
{ NULL }
};
GENRC_PID_CLOSURE *
get_pid_closure(char const *str)
{
struct genrc_pid_source *s;
struct wordsplit ws;
GENRC_PID_CLOSURE *pc;
size_t len = strcspn(str, ":");
int wsflags;
if (len == 0)
usage_error("GENRC_PID_FROM argument is empty");
for (s = sourcetab; ; s++) {
if (!s->name)
usage_error("%s: unsupported PID source", str);
if (strlen(s->name) == len && memcmp(s->name, str, len) == 0)
break;
}
ws.ws_error = genrc_error;
ws.ws_delim = ":";
wsflags = WRDSF_NOCMD
| WRDSF_NOVAR
| WRDSF_QUOTE
| WRDSF_DELIM
| WRDSF_ENOMEMABRT
| WRDSF_SHOWERR
| WRDSF_ERROR;
if (s->maxwords) {
ws.ws_maxwords = s->maxwords;
ws.ws_options = WRDSO_MAXWORDS;
wsflags |= WRDSF_OPTIONS;
}
if (wordsplit(str, &ws, wsflags))
exit(1);
pc = s->init(ws.ws_wordc, ws.ws_wordv);
if (!pc)
exit(1);
pc->name = xstrdup(str);
return pc;
}
int
get_pid_list(GENRC_PID_CLOSURE *clos, PIDLIST *plist)
{
pidlist_clear(plist);
return clos->pid(clos, plist);
}
|