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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
|
/* $OpenBSD: trigger.c,v 1.7 2008/06/10 03:55:50 joris Exp $ */
/*
* Copyright (c) 2008 Tobias Stoeckmann <tobias@openbsd.org>
* Copyright (c) 2008 Jonathan Armani <dbd@asystant.net>
*
* 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 <sys/queue.h>
#include <sys/types.h>
#include <ctype.h>
#include <errno.h>
#include <libgen.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "config.h"
#include "cvs.h"
static int expand_args(BUF *, struct file_info_list *, const char *,
const char *, char *);
static char *parse_cmd(int, char *, const char *, struct file_info_list *);
static int
expand_args(BUF *buf, struct file_info_list *file_info, const char *repo,
const char *allowed_args, char *format)
{
int oldstyle, quote;
struct file_info *fi = NULL;
char *p, valbuf[2] = { '\0', '\0' };
const char *val;
if (file_info != NULL && !TAILQ_EMPTY(file_info))
fi = TAILQ_FIRST(file_info);
quote = oldstyle = 0;
/* Why does GNU cvs print something if it encounters %{}? */
if (*format == '\0')
oldstyle = 1;
for (p = format; *p != '\0'; p++) {
if (*p != '%' && strchr(allowed_args, *p) == NULL)
return 1;
switch (*p) {
case 's':
case 'V':
case 'v':
quote = 1;
oldstyle = 1;
break;
default:
break;
}
}
if (quote)
cvs_buf_putc(buf, '"');
if (oldstyle) {
cvs_buf_append(buf, repo, strlen(repo));
cvs_buf_putc(buf, ' ');
}
if (*format == '\0')
return 0;
/*
* check like this, add only uses loginfo for directories anyway
*/
if (cvs_cmdop == CVS_OP_ADD) {
cvs_buf_append(buf, "- New directory",
strlen("- New directory"));
if (quote)
cvs_buf_putc(buf, '"');
return (0);
}
for (;;) {
for (p = format; *p != '\0';) {
val = NULL;
switch (*p) {
case '%':
val = "%";
break;
case 'b':
if (fi != NULL) {
valbuf[0] = fi->tag_type;
val = valbuf;
}
break;
case 'o':
if (fi != NULL)
val = fi->tag_op;
break;
case 'p':
val = current_cvsroot->cr_dir;
break;
case 'r':
val = repo;
break;
case 'l':
case 'S':
case 's':
if (fi != NULL) {
val = basename(fi->file_path);
if (val == NULL) {
fatal("basename: %s",
strerror(errno));
}
}
break;
case 't':
if (fi != NULL)
val = fi->tag_new;
break;
case 'V':
if (fi != NULL) {
if (fi->crevstr != NULL &&
!strcmp(fi->crevstr,
"Non-existent"))
val = "NONE";
else
val = fi->crevstr;
}
break;
case 'v':
if (fi != NULL)
val = fi->nrevstr;
break;
default:
return 1;
}
if (val != NULL)
cvs_buf_append(buf, val, strlen(val));
if (*(++p) != '\0')
cvs_buf_putc(buf, ',');
}
if (fi != NULL)
fi = TAILQ_NEXT(fi, flist);
if (fi == NULL)
break;
if (strlen(format) == 1 && (*format == '%' || *format == 'o' ||
*format == 'p' || *format == 'r' || *format == 't'))
break;
cvs_buf_putc(buf, ' ');
}
if (quote)
cvs_buf_putc(buf, '"');
return 0;
}
static char *
parse_cmd(int type, char *cmd, const char *repo,
struct file_info_list *file_info)
{
int expanded = 0;
char argbuf[2] = { '\0', '\0' };
char *allowed_args, *default_args, *args, *p, *q = NULL;
size_t pos;
BUF *buf;
switch (type) {
case CVS_TRIGGER_COMMITINFO:
allowed_args = "prsS{}";
default_args = " %p/%r %S";
break;
case CVS_TRIGGER_LOGINFO:
allowed_args = "prsSvVt{}";
default_args = NULL;
break;
case CVS_TRIGGER_VERIFYMSG:
allowed_args = "l";
default_args = " %l";
break;
case CVS_TRIGGER_TAGINFO:
allowed_args = "btoprsSvV{}";
default_args = " %t %o %p/%r %{sv}";
break;
default:
return (NULL);
}
/* XXX move this out of this function */
/* before doing any stuff, check if the command starts with % */
for (p = cmd; *p != '%' && !isspace(*p) && *p != '\0'; p++)
;
if (*p == '%')
return (NULL);
buf = cvs_buf_alloc(1024);
p = cmd;
again:
for (; *p != '\0'; p++) {
if ((pos = strcspn(p, "%")) != 0) {
cvs_buf_append(buf, p, pos);
p += pos;
}
if (*p++ == '\0')
break;
q = NULL;
switch (*p) {
case '\0':
goto bad;
case '{':
/* XXX do we realy have to check for { in there? */
if (strchr(allowed_args, '{') == NULL)
goto bad;
pos = strcspn(++p, "}");
if (p[pos] == '\0')
goto bad;
q = xmalloc(pos + 1);
memcpy(q, p, pos);
q[pos] = '\0';
args = q;
p += pos;
break;
default:
argbuf[0] = *p;
args = argbuf;
break;
}
if (expand_args(buf, file_info, repo, allowed_args, args))
goto bad;
expanded = 1;
if (q != NULL)
xfree(q);
}
if (!expanded && default_args != NULL) {
p = default_args;
expanded = 1;
goto again;
}
cvs_buf_putc(buf, '\0');
return (cvs_buf_release(buf));
bad:
if (q != NULL)
xfree(q);
cvs_log(LP_NOTICE, "CVSROOT: malformed line %s", cmd);
cvs_buf_free(buf);
return (NULL);
}
int
cvs_trigger_handle(int type, char *repo, char *in, struct trigger_list *list,
struct file_info_list *files)
{
int r;
char *cmd;
struct trigger_line *line;
TAILQ_FOREACH(line, list, flist) {
cmd = parse_cmd(type, line->line, repo, files);
if (cmd != NULL) {
switch(type) {
case CVS_TRIGGER_COMMITINFO:
case CVS_TRIGGER_TAGINFO:
case CVS_TRIGGER_VERIFYMSG:
if ((r = cvs_exec(cmd, NULL, 1)) != 0) {
xfree(cmd);
return r;
}
break;
default:
(void)cvs_exec(cmd, in, 1);
break;
}
xfree(cmd);
}
}
return 0;
}
struct trigger_list *
cvs_trigger_getlines(char * file, char * repo)
{
FILE *fp;
int allow_all, lineno, match = 0;
size_t len;
regex_t preg;
struct trigger_list *list;
struct trigger_line *tline;
char fpath[MAXPATHLEN];
char *currentline, *defaultline = NULL, *nline, *p, *q, *regex;
list = xmalloc(sizeof(*list));
TAILQ_INIT(list);
if (strcmp(file, CVS_PATH_EDITINFO) == 0 ||
strcmp(file, CVS_PATH_VERIFYMSG) == 0)
allow_all = 0;
else
allow_all = 1;
(void)xsnprintf(fpath, MAXPATHLEN, "%s/%s", current_cvsroot->cr_dir,
file);
if ((fp = fopen(fpath, "r")) == NULL) {
if (errno != ENOENT)
cvs_log(LP_ERRNO, "cvs_trigger_getlines: %s", file);
return (NULL);
}
lineno = 0;
nline = NULL;
while ((currentline = fgetln(fp, &len)) != NULL) {
if (currentline[len - 1] == '\n') {
currentline[len - 1] = '\0';
} else {
nline = xmalloc(len + 1);
memcpy(nline, currentline, len);
nline[len] = '\0';
currentline = nline;
}
lineno++;
for (p = currentline; isspace(*p); p++)
;
if (*p == '\0' || *p == '#')
continue;
for (q = p; !isspace(*q) && *q != '\0'; q++)
;
if (*q == '\0')
goto bad;
/* XXX why do you check *p (regex)? */
if (*p == '%')
goto bad;
*q++ = '\0';
regex = p;
for (; isspace(*q); q++)
;
if (*q == '\0')
goto bad;
if (strcmp(regex, "ALL") == 0 && allow_all) {
tline = xmalloc(sizeof(*tline));
tline->line = xstrdup(q);
TAILQ_INSERT_TAIL(list, tline, flist);
} else if (defaultline == NULL && !match &&
strcmp(regex, "DEFAULT") == 0) {
defaultline = xstrdup(q);
} else if (!match) {
if (regcomp(&preg, regex, REG_NOSUB|REG_EXTENDED))
goto bad;
if (regexec(&preg, repo, 0, NULL, 0) != REG_NOMATCH) {
match = 1;
tline = xmalloc(sizeof(*tline));
tline->line = xstrdup(q);
TAILQ_INSERT_HEAD(list, tline, flist);
}
regfree(&preg);
}
}
if (nline != NULL)
xfree(nline);
if (defaultline != NULL) {
if (!match) {
tline = xmalloc(sizeof(*tline));
tline->line = defaultline;
TAILQ_INSERT_HEAD(list, tline, flist);
} else
xfree(defaultline);
}
if (TAILQ_EMPTY(list)) {
xfree(list);
list = NULL;
}
return (list);
bad:
cvs_log(LP_NOTICE, "%s: malformed line %d", file, lineno);
if (defaultline != NULL)
xfree(defaultline);
cvs_trigger_freelist(list);
return (NULL);
}
void
cvs_trigger_freelist(struct trigger_list * list)
{
struct trigger_line *line;
while ((line = TAILQ_FIRST(list)) != NULL) {
TAILQ_REMOVE(list, line, flist);
xfree(line->line);
xfree(line);
}
xfree(list);
}
void
cvs_trigger_freeinfo(struct file_info_list * list)
{
struct file_info * fi;
while ((fi = TAILQ_FIRST(list)) != NULL) {
TAILQ_REMOVE(list, fi, flist);
if (fi->file_path != NULL)
xfree(fi->file_path);
if (fi->file_wd != NULL)
xfree(fi->file_wd);
if (fi->crevstr != NULL)
xfree(fi->crevstr);
if (fi->nrevstr != NULL)
xfree(fi->nrevstr);
if (fi->tag_new != NULL)
xfree(fi->tag_new);
if (fi->tag_old != NULL)
xfree(fi->tag_old);
xfree(fi);
}
}
void
cvs_trigger_loginfo_header(BUF *buf, char *repo)
{
char *dir, pwd[MAXPATHLEN];
char hostname[MAXHOSTNAMELEN];
if (gethostname(hostname, sizeof(hostname)) == -1) {
fatal("cvs_trigger_loginfo_header: gethostname failed %s",
strerror(errno));
}
if (getcwd(pwd, sizeof(pwd)) == NULL)
fatal("cvs_trigger_loginfo_header: Cannot get working "
"directory");
if ((dir = dirname(pwd)) == NULL) {
fatal("cvs_trigger_loginfo_header: dirname failed %s",
strerror(errno));
}
cvs_buf_puts(buf, "Update of ");
cvs_buf_puts(buf, current_cvsroot->cr_dir);
cvs_buf_putc(buf, '/');
cvs_buf_puts(buf, repo);
cvs_buf_putc(buf, '\n');
cvs_buf_puts(buf, "In directory ");
cvs_buf_puts(buf, hostname);
cvs_buf_puts(buf, ":");
cvs_buf_puts(buf, dirname(pwd));
cvs_buf_putc(buf, '/');
cvs_buf_puts(buf, repo);
cvs_buf_putc(buf, '\n');
cvs_buf_putc(buf, '\n');
}
|