-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcfg.c
181 lines (144 loc) · 4.89 KB
/
cfg.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
* author: Aleksei Kozadaev (2019)
*/
#include "build_host.h"
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include "log.h"
#include "cfg.h"
#include "cmd.h"
#include "files.h"
#define DEFAULT_SLEEP_DELAY 300
static void usage(const char *pname);
inline void cfg_add_fd(struct config *cfg, int fd) {
cfg->fds[cfg->nfds++] = fd;
}
void cfg_parse_args(struct config *cfg, int argc, char **argv) {
int ch;
opterr = 0;
if (argc == 1) {
usage(argv[0]);
}
cfg->sleep_delay = DEFAULT_SLEEP_DELAY;
while ((ch = getopt(argc, argv, "c:d:ihk:orsS:t:vw")) != -1) {
switch (ch) {
case 'h':
usage(argv[0]);
exit(EXIT_SUCCESS);
case 'c':
cfg->cmd->path = optarg;
break;
case 'd':
cfg->cmd->teardown = optarg;
cfg->cmd->spawn = true;
break;
case 'i':
cfg->immediate_run = true;
break;
case 'k':
cfg->cmd->timeout_ms = atoi(optarg);
cfg->cmd->spawn = true;
break;
case 'o':
cfg->onerun = true;
break;
case 'r':
cfg->recurse = true;
break;
case 's':
cfg->cmd->wrap_shell = false;
cfg->cmd->spawn = true;
break;
case 'S':
cfg->sleep_delay = atoi(optarg);
break;
case 't':
cfg->threshold = atoi(optarg);
break;
case 'v':
printf("%s\n", VERSION);
exit(EXIT_SUCCESS);
case 'w':
cfg->cmd->spawn = true;
break;
default:
LOG_ERROR("unknown argument");
exit(EXIT_FAILURE);
}
}
if (cfg->recurse) {
cfg->files = files_parse((const char **) argv + optind, &cfg->nfiles);
} else {
cfg->files = argv + optind;
cfg->nfiles = argc - optind;
}
if (cfg->nfiles == 0) {
LOG_ERROR("no files to monitor");
exit(EXIT_FAILURE);
}
if (cfg->cmd->path == NULL) {
LOG_ERROR("command was not specified");
exit(EXIT_FAILURE);
}
if ((cfg->fds = malloc(sizeof(*cfg->fds) * cfg->nfiles)) == NULL) {
LOG_PERROR("malloc");
exit(EXIT_FAILURE);
}
}
void cfg_free(const struct config *cfg) {
if (cfg->fds) {
free(cfg->fds);
}
files_free();
}
void cfg_print_header(const struct config *cfg) {
printf(":: [%s] start monitoring: ", get_time());
cmd_print_header(cfg->cmd);
if (cfg->threshold > 0) {
printf("threshold[%d] ", cfg->threshold);
}
if (cfg->sleep_delay != DEFAULT_SLEEP_DELAY) {
printf("sleep-delay[%d] ", cfg->sleep_delay);
}
if (cfg->onerun) {
printf("once ");
}
if (cfg->recurse) {
printf("recurse ");
}
printf("files[");
for (int i = 0; i < cfg->nfiles; ++i) {
if (i < cfg->nfiles - 1) {
printf("%s ", cfg->files[i]);
} else {
printf("%s", cfg->files[i]);
}
}
printf("]\n");
}
void usage(const char *pname) {
fprintf(stdout, "usage: %s -c <command> [options] <file/dir ...>\n\n"
" The options are as follows:\n"
" -c <cmd> - command to execute when event is triggered\n"
" -d <cmd> - teardown command to execute when -k timeout occurs "
"(assumes -w). The PID is available in CMD_PID environment variable.\n"
" -i - run the command immediately before waiting for changes\n"
" -h - display this text and exit\n"
" -k <ms> - timeout after which to kill the command subproces "
"(default - do not kill. Assumes -w.)\n"
" -o - exit after the first iteration\n"
" -r - if a directory is supplied, add all its sub-directories "
"as well\n"
" -s - do not wrap the command in a shell (assumes -w)\n"
" -S <ms> - number of ms to sleep before reattaching in case of "
"DELETE event (default 300)\n"
" -t <sec> - number of seconds to skip after the last executed "
"command (default 0)\n"
" -v - display the version [%s]\n"
" -w - spawn a subprocess for command (if not specified imk runs the command in a "
"libc system() which wraps the command in a shell like: sh -c 'cmd')\n"
" <file/dir ...> - list of files or folders to monitor\n\n"
" Please use quotes around the command and teardown command if it is "
"composed of multiple words\n\n", pname, VERSION);
}